Subversion Repositories Games.Carmageddon

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "include/smackw32/smackw32.h"
  2.  
  3. #include <assert.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #include "harness/hooks.h"
  10. #include "harness/trace.h"
  11. // lib/libsmacker
  12. #include "smacker.h"
  13.  
  14. #include "../miniaudio/include/miniaudio/miniaudio.h"
  15. extern ma_engine miniaudio_engine; // defined in miniaudio_backend.c
  16. extern int gSound_enabled; // defined in globvars.h
  17.  
  18. static uint32_t smack_last_frame_time = 0;
  19.  
  20. static void copy_palette(Smack* smack) {
  21.     const unsigned char* pal = smk_get_palette(smack->smk_handle);
  22.     memcpy(smack->Palette, pal, 256 * 3);
  23. }
  24.  
  25. Smack* SmackOpen(const char* name, uint32_t flags, uint32_t extrabuf) {
  26.     ma_format audioformat_ma;
  27.     ma_paged_audio_buffer_config paged_audio_buffer_config;
  28.     ma_data_converter_config data_converter_config;
  29.     unsigned char track_mask_smk;
  30.     unsigned char channels_smk[7];
  31.     unsigned char bitdepth_smk[7];
  32.     unsigned long sample_rate_smk[7];
  33.     double microsecs_per_frame;
  34.     Smack* smack;
  35.     double fps;
  36.  
  37.     smk smk_handle = smk_open_file(name, SMK_MODE_MEMORY);
  38.     if (smk_handle == NULL) {
  39.         return NULL;
  40.     }
  41.  
  42.     smack = malloc(sizeof(Smack));
  43.  
  44.     // libsmacker doesn't tell us whether the palette is new on each frame or not, so just assume it always is new
  45.     smack->NewPalette = 1;
  46.  
  47.     // smk_handle is added to hold a pointer to the underlying libsmacker instance
  48.     smack->smk_handle = smk_handle;
  49.  
  50.     smk_info_all(smk_handle, NULL, &smack->Frames, &microsecs_per_frame);
  51.     fps = 1000000.0 / microsecs_per_frame;
  52.     smack->MSPerFrame = (unsigned long) ((1 / fps) * 1000); // Pierre-Marie Baty -- added type cast
  53.     smk_info_video(smk_handle, &smack->Width, &smack->Height, NULL);
  54.     smk_enable_video(smk_handle, 1);
  55.  
  56.     if (smk_first(smk_handle) == SMK_ERROR) {
  57.         smk_close(smk_handle);
  58.         free(smack);
  59.         return NULL;
  60.     }
  61.     copy_palette(smack);
  62.  
  63.     // initialize video sound if required and present
  64.     smack->audio_sample_rate = 0;
  65.     smack->audio_frame_size_in_bytes = 0;
  66.     smack->audio_paged_buffer = NULL;
  67.     smack->audio_pages_accumulated_cnt = 0;
  68.     smack->audio_pages_accumulated_max = 0;
  69.     smack->audio_paged_buffer_data = NULL;
  70.     smack->audio_converter = NULL;
  71.     smack->audio_track = NULL;
  72.     if (!gSound_enabled) {
  73.         return smack;
  74.     }
  75.  
  76.     // get info about the audio tracks in this video
  77.     smk_info_audio(smk_handle, &track_mask_smk, channels_smk, bitdepth_smk, sample_rate_smk);
  78.     if (!(track_mask_smk & SMK_AUDIO_TRACK_0)) {
  79.         LOG_INFO("Smacker file does not contain audio");
  80.         return smack;
  81.     }
  82.     smack->audio_sample_rate = sample_rate_smk[0];
  83.     switch (bitdepth_smk[0]) {
  84.     case 8:
  85.         audioformat_ma = ma_format_u8;
  86.         smack->audio_frame_size_in_bytes = 1 * channels_smk[0];
  87.         break;
  88.     case 16:
  89.         audioformat_ma = ma_format_s16;
  90.         smack->audio_frame_size_in_bytes = 2 * channels_smk[0];
  91.         break;
  92.     case 24:
  93.         audioformat_ma = ma_format_s24;
  94.         smack->audio_frame_size_in_bytes = 3 * channels_smk[0];
  95.         break;
  96.     case 32:
  97.         audioformat_ma = ma_format_s32;
  98.         smack->audio_frame_size_in_bytes = 4 * channels_smk[0];
  99.         break;
  100.     default:
  101.         LOG_WARN("Smacker audio stream has invalid bit depth: %d", bitdepth_smk[0]);
  102.         goto audio_fail;
  103.     }
  104.  
  105.     smack->audio_pages_accumulated_max = smack->Frames / 100; // divider discovered by trial and error. Syncs audio to video in all the intro and intermission videos without starving.
  106.     if (smack->audio_pages_accumulated_max < 1)
  107.         smack->audio_pages_accumulated_max = 1;
  108.     //LOG_INFO("Smacker audio: will accumulate %d pages", smack->audio_pages_accumulated_max);
  109.  
  110.     // allocate and initialize paged buffer data
  111.     smack->audio_paged_buffer_data = malloc(sizeof(ma_paged_audio_buffer_data));
  112.  
  113.     if ((smack->audio_frame_size_in_bytes == 0) || (ma_paged_audio_buffer_data_init(audioformat_ma, channels_smk[0], smack->audio_paged_buffer_data) != MA_SUCCESS)) {
  114.         LOG_WARN("Failed to create paged audio buffer data");
  115.         goto audio_fail;
  116.     }
  117.  
  118.     // allocate and initialize paged buffer
  119.     smack->audio_paged_buffer = malloc(sizeof(ma_paged_audio_buffer));
  120.  
  121.     paged_audio_buffer_config = ma_paged_audio_buffer_config_init(smack->audio_paged_buffer_data);
  122.     if (ma_paged_audio_buffer_init (&paged_audio_buffer_config, smack->audio_paged_buffer) != MA_SUCCESS) {
  123.         LOG_WARN("Failed to create paged audio buffer for smacker audio stream");
  124.         goto audio_fail;
  125.     }
  126.  
  127.     // allocate and initialize sound
  128.     smack->audio_track = malloc(sizeof(ma_sound));
  129.  
  130.     if (ma_sound_init_from_data_source(&miniaudio_engine, smack->audio_paged_buffer, MA_SOUND_FLAG_NO_PITCH | MA_SOUND_FLAG_NO_SPATIALIZATION, NULL, smack->audio_track) != MA_SUCCESS) {
  131.         LOG_WARN("Failed to create sound from data source");
  132.         goto audio_fail;
  133.     }
  134.  
  135.     // allocate and initialize data converter
  136.     smack->audio_converter = malloc(sizeof(ma_data_converter));
  137.  
  138.     data_converter_config = ma_data_converter_config_init(audioformat_ma, audioformat_ma, channels_smk[0], channels_smk[0], smack->audio_sample_rate, ma_engine_get_sample_rate (&miniaudio_engine));
  139.     if (ma_data_converter_init(&data_converter_config, NULL, smack->audio_converter) != MA_SUCCESS) {
  140.         LOG_WARN("Failed to create sound data converter");
  141.         goto audio_fail;
  142.     }
  143.  
  144.     // tell libsmacker we can process audio now
  145.     smk_enable_audio(smk_handle, 0, 1);
  146.     return smack;
  147.  
  148. audio_fail:
  149.     if (smack->audio_converter != NULL) {
  150.         free(smack->audio_converter);
  151.         smack->audio_converter = NULL;
  152.     }
  153.     if (smack->audio_track != NULL) {
  154.         ma_sound_uninit(smack->audio_track);
  155.         free(smack->audio_track);
  156.         smack->audio_track = NULL;
  157.     }
  158.     if (smack->audio_paged_buffer != NULL) {
  159.         ma_paged_audio_buffer_uninit(smack->audio_paged_buffer);
  160.         free(smack->audio_paged_buffer);
  161.         smack->audio_paged_buffer = NULL;
  162.     }
  163.     if (smack->audio_paged_buffer_data != NULL) {
  164.         ma_paged_audio_buffer_data_uninit (smack->audio_paged_buffer_data, NULL);
  165.         free(smack->audio_paged_buffer_data);
  166.         smack->audio_paged_buffer_data = NULL;
  167.     }
  168.     return smack;
  169. }
  170.  
  171. int SmackSoundUseDirectSound(void* dd) {
  172.     // TODO: do some miniaudio init
  173.  
  174.     return 0;
  175. }
  176.  
  177. void SmackToBuffer(Smack* smack, uint32_t left, uint32_t top, uint32_t pitch, uint32_t destheight, void* buf, uint32_t flags) {
  178.     unsigned long i; // Pierre-Marie Baty -- fixed type
  179.  
  180.     // minimal implementation
  181.     assert(left == 0);
  182.     assert(top == 0);
  183.     assert(flags == 0);
  184.  
  185.     char* char_buf = buf;
  186.  
  187.     const unsigned char* frame = smk_get_video(smack->smk_handle);
  188.     for (i = 0; i < smack->Height; i++) {
  189.         memcpy(&char_buf[(i * pitch)], &frame[i * smack->Width], smack->Width);
  190.     }
  191. }
  192.  
  193. uint32_t SmackDoFrame(Smack* smack) {
  194.     const unsigned char *audio_data;
  195.     unsigned long audio_data_size;
  196.     ma_paged_audio_buffer_page *newPage;
  197.     ma_uint64 current_pos;
  198.     ma_uint64 nb_frames_in;
  199.     ma_uint64 nb_frames_out;
  200.     ma_uint64 seek_pos;
  201.  
  202.     // process audio if we have some
  203.     if (smack->audio_converter != NULL) {
  204.         audio_data = smk_get_audio (smack->smk_handle, 0);
  205.         audio_data_size = smk_get_audio_size (smack->smk_handle, 0);
  206.         if ((audio_data == NULL) || (audio_data_size == 0)) {
  207.             return 0;
  208.         }
  209.  
  210.         if (ma_paged_audio_buffer_get_length_in_pcm_frames (smack->audio_paged_buffer, &current_pos) != MA_SUCCESS) {
  211.             LOG_WARN("ma_paged_audio_buffer_get_length_in_pcm_frames failed");
  212.             return 0;
  213.         }
  214.  
  215.         nb_frames_in = audio_data_size / smack->audio_frame_size_in_bytes;
  216.         nb_frames_out = nb_frames_in * ma_engine_get_sample_rate (&miniaudio_engine) / smack->audio_sample_rate;
  217.  
  218.         if (ma_paged_audio_buffer_data_allocate_page (smack->audio_paged_buffer_data, nb_frames_out, NULL, NULL, &newPage) != MA_SUCCESS) {
  219.             LOG_WARN("ma_paged_audio_buffer_data_allocate_page failed");
  220.             return 0;
  221.         }
  222.         else if (ma_data_converter_process_pcm_frames (smack->audio_converter, audio_data, &nb_frames_in, newPage->pAudioData, &nb_frames_out) != MA_SUCCESS) {
  223.             LOG_WARN("ma_data_converter_process_pcm_frames failed");
  224.             return 0;
  225.         }
  226.         else if (ma_paged_audio_buffer_data_append_page (smack->audio_paged_buffer_data, newPage) != MA_SUCCESS) {
  227.             LOG_WARN("ma_paged_audio_buffer_data_append_page failed");
  228.             return 0;
  229.         }
  230.         smack->audio_pages_accumulated_cnt++;
  231.  
  232.         if (smack->audio_pages_accumulated_cnt > smack->audio_pages_accumulated_max)
  233.         {
  234.             if (!ma_sound_is_playing (smack->audio_track))
  235.             {
  236.                 // seek either at start, or where the accumulated value hasn't played yet
  237.                 seek_pos = ((smack->audio_pages_accumulated_max - 1) * nb_frames_out < current_pos ? current_pos - (smack->audio_pages_accumulated_max - 1) * nb_frames_out : 0);
  238.                 if (ma_sound_seek_to_pcm_frame (smack->audio_track, seek_pos) != MA_SUCCESS) {
  239.                     LOG_WARN("ma_sound_seek_to_pcm_frame failed");
  240.                 }
  241.                 if (ma_sound_start (smack->audio_track) != MA_SUCCESS) {
  242.                     LOG_WARN("ma_sound_start failed");
  243.                 }
  244.             }
  245.             if (ma_sound_at_end (smack->audio_track)) {
  246.                 LOG_WARN ("video not playing fast enough: sound starved!");
  247.             }
  248.         }
  249.     }
  250.  
  251.     return 0;
  252. }
  253.  
  254. void SmackNextFrame(Smack* smack) {
  255.     smk_next(smack->smk_handle);
  256.     copy_palette(smack);
  257. }
  258.  
  259. uint32_t SmackWait(Smack* smack) {
  260.     uint32_t now = gHarness_platform.GetTicks();
  261.     if (now < smack_last_frame_time + smack->MSPerFrame) {
  262.         gHarness_platform.Sleep(1);
  263.         return 1;
  264.     }
  265.     smack_last_frame_time = now;
  266.     return 0;
  267. }
  268.  
  269. void SmackClose(Smack* smack) {
  270.     if (smack->audio_converter != NULL) {
  271.         ma_data_converter_uninit(smack->audio_converter, NULL);
  272.         free(smack->audio_converter);
  273.     }
  274.     if (smack->audio_track != NULL) {
  275.         ma_sound_stop(smack->audio_track);
  276.         ma_sound_uninit(smack->audio_track);
  277.         free(smack->audio_track);
  278.     }
  279.     if (smack->audio_paged_buffer != NULL) {
  280.         ma_paged_audio_buffer_uninit(smack->audio_paged_buffer);
  281.         free(smack->audio_paged_buffer);
  282.     }
  283.     if (smack->audio_paged_buffer_data != NULL) {
  284.         ma_paged_audio_buffer_data_uninit(smack->audio_paged_buffer_data, NULL);
  285.         free(smack->audio_paged_buffer_data);
  286.     }
  287.     smk_close(smack->smk_handle);
  288.     free(smack);
  289. }
  290.