Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 9 | pmbaty | 1 | |
| 2 | // compile with -Ilib/SDL2 -Ilib/miniaudio/include/miniaudio -Ilib/libsmacker | 
        ||
| 3 | |||
| 4 | #include <stdio.h> | 
        ||
| 5 | |||
| 6 | #define SDL_MAIN_HANDLED | 
        ||
| 7 | #include "SDL.h" | 
        ||
| 8 | #include "miniaudio.h" | 
        ||
| 9 | #include "smacker.h" | 
        ||
| 10 | |||
| 11 | typedef struct {  | 
        ||
| 12 |     ma_paged_audio_buffer paged_audio_buffer; | 
        ||
| 13 |     ma_paged_audio_buffer_data paged_audio_buffer_data; | 
        ||
| 14 | int frame_size_in_bytes;  | 
        ||
| 15 | } smacker_data_source;  | 
        ||
| 16 | |||
| 17 | static ma_engine engine;  | 
        ||
| 18 | |||
| 19 | void PlaySmackerFile(SDL_Window *window, SDL_Renderer *renderer, const char *path) {  | 
        ||
| 20 | unsigned int i, j;  | 
        ||
| 21 |     smk s; | 
        ||
| 22 | |||
| 23 | unsigned long w, h, f;  | 
        ||
| 24 | double usf;  | 
        ||
| 25 | |||
| 26 |     SDL_Event event; | 
        ||
| 27 | SDL_Colour palette[256];  | 
        ||
| 28 |     Uint32 real_format; | 
        ||
| 29 | int real_access;  | 
        ||
| 30 | int real_w;  | 
        ||
| 31 | int real_h;  | 
        ||
| 32 | void* pixels;  | 
        ||
| 33 | int pixel_pitch;  | 
        ||
| 34 | |||
| 35 |     Uint64 counter_frametime; | 
        ||
| 36 |     Uint64 counter_deadline_next_frame; | 
        ||
| 37 | |||
| 38 | unsigned char track_mask;  | 
        ||
| 39 | unsigned char channels_smk[7];  | 
        ||
| 40 | unsigned char bitdepth_smk[7];  | 
        ||
| 41 | unsigned long samplerate_smk[7];  | 
        ||
| 42 | |||
| 43 |     ma_result result_ma; | 
        ||
| 44 |     ma_format audioformat_ma; | 
        ||
| 45 | int need_resampler;  | 
        ||
| 46 |     ma_sound sound; | 
        ||
| 47 | |||
| 48 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Trying to open smack file '%s'", path);  | 
        ||
| 49 | s = smk_open_file(path, SMK_MODE_MEMORY);  | 
        ||
| 50 | if (s == NULL) {  | 
        ||
| 51 | SDL_Log("smk_open_file failed");  | 
        ||
| 52 | return;  | 
        ||
| 53 |     } | 
        ||
| 54 | smk_info_all(s, NULL, &f, &usf);  | 
        ||
| 55 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "frame count=%"SDL_PRIu64", frame time=%gus", (Uint64)f, usf);  | 
        ||
| 56 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "duration=%gs", (double)f * usf / 1000000.);  | 
        ||
| 57 | |||
| 58 | counter_frametime = (Uint64)((double)SDL_GetPerformanceFrequency() * usf / 1000000);  | 
        ||
| 59 | |||
| 60 | smk_info_video(s, &w, &h, NULL);  | 
        ||
| 61 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "size=%"SDL_PRIu64"x%"SDL_PRIu64"", (Uint64)w, (Uint64)h);  | 
        ||
| 62 | |||
| 63 |     smacker_data_source data_source; | 
        ||
| 64 | SDL_SetWindowTitle(window, path);  | 
        ||
| 65 | |||
| 66 | smk_info_audio(s, &track_mask, channels_smk, bitdepth_smk, samplerate_smk);  | 
        ||
| 67 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "tracks mask = 0x%x", track_mask);  | 
        ||
| 68 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "#channels = %d %d %d %d %d %d %d", channels_smk[0], channels_smk[1], channels_smk[2], channels_smk[3], channels_smk[4], channels_smk[5], channels_smk[6]);  | 
        ||
| 69 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "sample bitsizes = %d %d %d %d %d %d %d", bitdepth_smk[0], bitdepth_smk[1], bitdepth_smk[2], bitdepth_smk[3], bitdepth_smk[4], bitdepth_smk[5], bitdepth_smk[6]);  | 
        ||
| 70 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "sample rates = %"SDL_PRIu64" %"SDL_PRIu64" %"SDL_PRIu64" %"SDL_PRIu64" %"SDL_PRIu64" %"SDL_PRIu64" %"SDL_PRIu64"", (Uint64)samplerate_smk[0], (Uint64)samplerate_smk[1], (Uint64)samplerate_smk[2], (Uint64)samplerate_smk[3], (Uint64)samplerate_smk[4], (Uint64)samplerate_smk[5], (Uint64)samplerate_smk[6]);  | 
        ||
| 71 | |||
| 72 | switch (bitdepth_smk[0]) {  | 
        ||
| 73 | case 8:  | 
        ||
| 74 | audioformat_ma = ma_format_u8;  | 
        ||
| 75 | data_source.frame_size_in_bytes = 1 * channels_smk[0];  | 
        ||
| 76 | break;  | 
        ||
| 77 | case 16:  | 
        ||
| 78 | audioformat_ma = ma_format_s16;  | 
        ||
| 79 | data_source.frame_size_in_bytes = 2 * channels_smk[0];  | 
        ||
| 80 | break;  | 
        ||
| 81 | case 24:  | 
        ||
| 82 | audioformat_ma = ma_format_s24;  | 
        ||
| 83 | data_source.frame_size_in_bytes = 3 * channels_smk[0];  | 
        ||
| 84 | break;  | 
        ||
| 85 | case 32:  | 
        ||
| 86 | audioformat_ma = ma_format_s32;  | 
        ||
| 87 | data_source.frame_size_in_bytes = 4 * channels_smk[0];  | 
        ||
| 88 | break;  | 
        ||
| 89 | default:  | 
        ||
| 90 | audioformat_ma = ma_format_unknown;  | 
        ||
| 91 | SDL_Log("Smacker audio stream has invalid bit depth: %d", bitdepth_smk[0]);  | 
        ||
| 92 | break;  | 
        ||
| 93 |     } | 
        ||
| 94 | result_ma = ma_paged_audio_buffer_data_init(audioformat_ma, channels_smk[0], &data_source.paged_audio_buffer_data);  | 
        ||
| 95 | if (result_ma != MA_SUCCESS) {  | 
        ||
| 96 | SDL_Log("Failed to create paged audio buffer data (%s)", ma_result_description(result_ma));  | 
        ||
| 97 |     } | 
        ||
| 98 | |||
| 99 | ma_paged_audio_buffer_config paged_audio_buffer_config = ma_paged_audio_buffer_config_init(&data_source.paged_audio_buffer_data);  | 
        ||
| 100 | result_ma = ma_paged_audio_buffer_init(&paged_audio_buffer_config, &data_source.paged_audio_buffer);  | 
        ||
| 101 | if (result_ma != MA_SUCCESS) {  | 
        ||
| 102 | SDL_Log("Failed to create paged audio buffer for smacker audio stream (%s)", ma_result_description(result_ma));  | 
        ||
| 103 |     } | 
        ||
| 104 | ma_sound_config soundConfig = ma_sound_config_init();  | 
        ||
| 105 | soundConfig.pDataSource = &data_source;  | 
        ||
| 106 | soundConfig.flags = MA_SOUND_FLAG_NO_PITCH | MA_SOUND_FLAG_NO_SPATIALIZATION;  | 
        ||
| 107 | result_ma = ma_sound_init_ex(&engine, &soundConfig, &sound);  | 
        ||
| 108 | if (result_ma != MA_SUCCESS) {  | 
        ||
| 109 | SDL_Log("Failed to create sound from data source (%s)", ma_result_description(result_ma));  | 
        ||
| 110 |     } | 
        ||
| 111 | |||
| 112 | need_resampler = ma_engine_get_sample_rate(&engine) != samplerate_smk[0];  | 
        ||
| 113 | |||
| 114 |     ma_data_converter data_converter; | 
        ||
| 115 | if (need_resampler) {  | 
        ||
| 116 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "will resample from %d to %d", samplerate_smk[0], ma_engine_get_sample_rate(&engine));  | 
        ||
| 117 | ma_data_converter_config dataConverterConfig = ma_data_converter_config_init(audioformat_ma, audioformat_ma, channels_smk[0], channels_smk[0], (ma_uint32)samplerate_smk[0],  | 
        ||
| 118 | ma_engine_get_sample_rate(&engine));  | 
        ||
| 119 | result_ma = ma_data_converter_init(&dataConverterConfig, NULL, &data_converter);  | 
        ||
| 120 | if (result_ma != MA_SUCCESS) {  | 
        ||
| 121 | SDL_Log("Failed to create data converter (%s)", ma_result_description(result_ma));  | 
        ||
| 122 |         } | 
        ||
| 123 |     } | 
        ||
| 124 | |||
| 125 | smk_enable_video(s, 1);  | 
        ||
| 126 | smk_enable_audio(s, 0, 1);  | 
        ||
| 127 | |||
| 128 | SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, (int)w, (int)h);  | 
        ||
| 129 | if (texture == NULL) {  | 
        ||
| 130 | SDL_Log("SDL_CreateTexture returned NULL (%s)", SDL_GetError());  | 
        ||
| 131 | return;  | 
        ||
| 132 |     } | 
        ||
| 133 | |||
| 134 | SDL_QueryTexture(texture, &real_format, &real_access, &real_w, &real_h);  | 
        ||
| 135 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "format: %s (requested=%s)", SDL_GetPixelFormatName(real_format), SDL_GetPixelFormatName(SDL_PIXELFORMAT_RGB24));  | 
        ||
| 136 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "access: %d (requested=%d)", real_access, SDL_TEXTUREACCESS_STREAMING);  | 
        ||
| 137 | SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "size: %dx%d (requested=%"SDL_PRIu64"x%"SDL_PRIu64")", real_w, real_h, (Uint64)w, (Uint64)h);  | 
        ||
| 138 | |||
| 139 | smk_first(s);  | 
        ||
| 140 | counter_deadline_next_frame = SDL_GetTicks() * 10000;  | 
        ||
| 141 | int audio_started = 0;  | 
        ||
| 142 | do {  | 
        ||
| 143 | const unsigned char* raw_palette = smk_get_palette(s);  | 
        ||
| 144 | for (i = 0; i < 256; i++) {  | 
        ||
| 145 | palette[i].r = raw_palette[3 * i + 0];  | 
        ||
| 146 | palette[i].g = raw_palette[3 * i + 1];  | 
        ||
| 147 | palette[i].b = raw_palette[3 * i + 2];  | 
        ||
| 148 | palette[i].a = 0;  | 
        ||
| 149 |         } | 
        ||
| 150 | const unsigned char *audio_data = smk_get_audio(s, 0);  | 
        ||
| 151 | unsigned long audio_data_size = smk_get_audio_size(s, 0);  | 
        ||
| 152 | const unsigned char* frame = smk_get_video(s);  | 
        ||
| 153 | |||
| 154 | if (audio_data != NULL) {  | 
        ||
| 155 | if (need_resampler) {  | 
        ||
| 156 | ma_uint64 framesIn = audio_data_size / (ma_uint64)data_source.frame_size_in_bytes;  | 
        ||
| 157 | ma_uint64 framesOut = framesIn * ma_engine_get_sample_rate(&engine) / samplerate_smk[0];  | 
        ||
| 158 | |||
| 159 | ma_paged_audio_buffer_page *newPage;  | 
        ||
| 160 | result_ma = ma_paged_audio_buffer_data_allocate_page(&data_source.paged_audio_buffer_data, framesOut, NULL, NULL, &newPage);  | 
        ||
| 161 | if (result_ma != MA_SUCCESS) {  | 
        ||
| 162 | SDL_Log("ma_paged_audio_buffer_data_allocate_page failed (%s)", ma_result_description(result_ma));  | 
        ||
| 163 |                 } | 
        ||
| 164 | |||
| 165 | ma_data_converter_process_pcm_frames(&data_converter,  | 
        ||
| 166 |                                                 audio_data, | 
        ||
| 167 | &framesIn,  | 
        ||
| 168 | newPage->pAudioData,  | 
        ||
| 169 | &framesOut);  | 
        ||
| 170 | result_ma = ma_paged_audio_buffer_data_append_page(&data_source.paged_audio_buffer_data, newPage);  | 
        ||
| 171 | if (result_ma != MA_SUCCESS) {  | 
        ||
| 172 | SDL_Log("ma_paged_audio_buffer_data_append_page failed (%s)", ma_result_description(result_ma));  | 
        ||
| 173 |                 } | 
        ||
| 174 | } else {  | 
        ||
| 175 |                 ma_paged_audio_buffer_data_allocate_and_append_page( | 
        ||
| 176 | &data_source.paged_audio_buffer_data,  | 
        ||
| 177 | (ma_uint32)(audio_data_size / (ma_uint64)data_source.frame_size_in_bytes),  | 
        ||
| 178 |                         audio_data, | 
        ||
| 179 | NULL);  | 
        ||
| 180 | |||
| 181 |             } | 
        ||
| 182 |         } | 
        ||
| 183 | |||
| 184 | SDL_LockTexture(texture, NULL, &pixels, &pixel_pitch);  | 
        ||
| 185 | Uint8* current_row = pixels;  | 
        ||
| 186 | for (i = 0; i < h; i++) {  | 
        ||
| 187 | Uint8* pixel_ptr = current_row;  | 
        ||
| 188 | for (j = 0; j < w; j++) {  | 
        ||
| 189 | pixel_ptr[0] = palette[frame[w * i + j]].r;  | 
        ||
| 190 | pixel_ptr[1] = palette[frame[w * i + j]].g;  | 
        ||
| 191 | pixel_ptr[2] = palette[frame[w * i + j]].b;  | 
        ||
| 192 | pixel_ptr += 3;  | 
        ||
| 193 |             } | 
        ||
| 194 | current_row += pixel_pitch;  | 
        ||
| 195 |         } | 
        ||
| 196 | SDL_UnlockTexture(texture);  | 
        ||
| 197 | while (SDL_GetTicks() * 10000 < counter_deadline_next_frame) {  | 
        ||
| 198 | while (SDL_PollEvent(&event)) {  | 
        ||
| 199 | switch (event.type) {  | 
        ||
| 200 | case SDL_QUIT:  | 
        ||
| 201 | exit(0);  | 
        ||
| 202 | break;  | 
        ||
| 203 |                 } | 
        ||
| 204 |             } | 
        ||
| 205 | SDL_Delay(1);  | 
        ||
| 206 |         } | 
        ||
| 207 | counter_deadline_next_frame += counter_frametime;  | 
        ||
| 208 | |||
| 209 | SDL_RenderCopy(renderer, texture, NULL, NULL);  | 
        ||
| 210 | if (!audio_started) {  | 
        ||
| 211 | ma_sound_start(&sound);  | 
        ||
| 212 | audio_started = 1;  | 
        ||
| 213 |         } | 
        ||
| 214 | SDL_RenderPresent(renderer);  | 
        ||
| 215 | } while (smk_next(s) == SMK_MORE);  | 
        ||
| 216 | if (need_resampler) {  | 
        ||
| 217 | ma_data_converter_uninit(&data_converter, NULL);  | 
        ||
| 218 |     } | 
        ||
| 219 | while (ma_sound_is_playing(&sound)) {  | 
        ||
| 220 | SDL_Delay(1);  | 
        ||
| 221 |     } | 
        ||
| 222 | |||
| 223 | ma_sound_stop(&sound);  | 
        ||
| 224 | ma_sound_uninit(&sound);  | 
        ||
| 225 | ma_paged_audio_buffer_uninit(&data_source.paged_audio_buffer);  | 
        ||
| 226 | ma_paged_audio_buffer_data_uninit(&data_source.paged_audio_buffer_data, NULL);  | 
        ||
| 227 | smk_close(s);  | 
        ||
| 228 | } | 
        ||
| 229 | |||
| 230 | static void PrintUsage(int argc, char *argv[]) {  | 
        ||
| 231 | printf("Usage: %s [--debug] FILE [FILE ...]\n", argv[0]);  | 
        ||
| 232 | printf("\n");  | 
        ||
| 233 | printf("Required options:\n");  | 
        ||
| 234 | printf("\n");  | 
        ||
| 235 | printf(" FILE Path to a smack video file.\n");  | 
        ||
| 236 | printf("\n");  | 
        ||
| 237 | printf("Optional options:\n");  | 
        ||
| 238 | printf("\n");  | 
        ||
| 239 | printf(" --debug Print debug information.\n");  | 
        ||
| 240 | } | 
        ||
| 241 | |||
| 242 | int main(int argc, char *argv[]) {  | 
        ||
| 243 | int i;  | 
        ||
| 244 | |||
| 245 |     ma_result result_ma; | 
        ||
| 246 |     ma_engine_config engineConfig; | 
        ||
| 247 | |||
| 248 | SDL_Window *window;  | 
        ||
| 249 | SDL_Renderer *renderer;  | 
        ||
| 250 | |||
| 251 | int start_i = 1;  | 
        ||
| 252 | while (start_i < argc) {  | 
        ||
| 253 | if (strcmp(argv[start_i], "--debug") == 0) {  | 
        ||
| 254 | SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG);  | 
        ||
| 255 | } else {  | 
        ||
| 256 | break;  | 
        ||
| 257 |         } | 
        ||
| 258 |         start_i++; | 
        ||
| 259 |     } | 
        ||
| 260 | |||
| 261 | if (start_i >= argc) {  | 
        ||
| 262 | PrintUsage(argc, argv);  | 
        ||
| 263 | return 1;  | 
        ||
| 264 |     } | 
        ||
| 265 | |||
| 266 | |||
| 267 | if (SDL_Init(SDL_INIT_VIDEO) < 0) {  | 
        ||
| 268 | SDL_Log("SDL_Init failed (%s)", SDL_GetError());  | 
        ||
| 269 | return 1;  | 
        ||
| 270 |     } | 
        ||
| 271 | |||
| 272 | engineConfig = ma_engine_config_init();  | 
        ||
| 273 | result_ma = ma_engine_init(&engineConfig, &engine);  | 
        ||
| 274 | if (result_ma != MA_SUCCESS) {  | 
        ||
| 275 | SDL_Log("ma_engine_init failed (%s)", ma_result_description(result_ma));  | 
        ||
| 276 | return 1;  | 
        ||
| 277 |     } | 
        ||
| 278 | |||
| 279 | if (SDL_CreateWindowAndRenderer(640, 480, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN, &window, &renderer) < 0) {  | 
        ||
| 280 | SDL_Log("SDL_CreateWindowAndRenderer failed (%s)", SDL_GetError());  | 
        ||
| 281 | return 1;  | 
        ||
| 282 |     } | 
        ||
| 283 | |||
| 284 | SDL_ShowWindow(window);  | 
        ||
| 285 | for (i = start_i; i < argc; i++) {  | 
        ||
| 286 | PlaySmackerFile(window, renderer, argv[i]);  | 
        ||
| 287 |     } | 
        ||
| 288 | |||
| 289 | SDL_DestroyRenderer(renderer);  | 
        ||
| 290 | SDL_DestroyWindow(window);  | 
        ||
| 291 | ma_engine_uninit(&engine);  | 
        ||
| 292 | SDL_Quit();  | 
        ||
| 293 | return 0;  | 
        ||
| 294 | } |