1 /* $Id: mveplay.c,v 1.7 2003-03-19 19:21:34 btb Exp $ */
15 #include <sys/types.h>
20 #if defined(STANDALONE) || defined(AUDIO)
25 #include "mve_audio.h"
37 #define d_malloc(size) malloc(size)
38 #define d_free(ptr) free(ptr)
42 #define MIN(a,b) ((a)<(b)?(a):(b))
45 #define MVE_OPCODE_ENDOFSTREAM 0x00
46 #define MVE_OPCODE_ENDOFCHUNK 0x01
47 #define MVE_OPCODE_CREATETIMER 0x02
48 #define MVE_OPCODE_INITAUDIOBUFFERS 0x03
49 #define MVE_OPCODE_STARTSTOPAUDIO 0x04
50 #define MVE_OPCODE_INITVIDEOBUFFERS 0x05
52 #define MVE_OPCODE_DISPLAYVIDEO 0x07
53 #define MVE_OPCODE_AUDIOFRAMEDATA 0x08
54 #define MVE_OPCODE_AUDIOFRAMESILENCE 0x09
55 #define MVE_OPCODE_INITVIDEOMODE 0x0A
57 #define MVE_OPCODE_SETPALETTE 0x0C
58 #define MVE_OPCODE_SETPALETTECOMPRESSED 0x0D
60 #define MVE_OPCODE_SETDECODINGMAP 0x0F
62 #define MVE_OPCODE_VIDEODATA 0x11
64 #define MVE_AUDIO_FLAGS_STEREO 1
65 #define MVE_AUDIO_FLAGS_16BIT 2
66 #define MVE_AUDIO_FLAGS_COMPRESSED 4
69 static int g_spdFactorDenom=10;
70 static int g_frameUpdated = 0;
73 static int playing = 1;
74 int g_sdlVidFlags = SDL_ANYFORMAT | SDL_DOUBLEBUF;
77 void initializeMovie(MVESTREAM *mve);
78 void playMovie(MVESTREAM *mve);
79 void shutdownMovie(MVESTREAM *mve);
82 static short get_short(unsigned char *data)
85 value = data[0] | (data[1] << 8);
89 static unsigned short get_ushort(unsigned char *data)
92 value = data[0] | (data[1] << 8);
96 static int get_int(unsigned char *data)
99 value = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
103 static unsigned int unhandled_chunks[32*256];
105 static int default_seg_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
107 unhandled_chunks[major<<8|minor]++;
108 //fprintf(stderr, "unknown chunk type %02x/%02x\n", major, minor);
113 /*************************
115 *************************/
116 static int end_movie_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
121 /*************************
123 *************************/
128 static int timer_created = 0;
129 static int micro_frame_delay=0;
130 static int timer_started=0;
131 static struct timeval timer_expire = {0, 0};
134 int nanosleep(struct timespec *ts, void *rem);
138 #include <sys/timeb.h>
142 long int tv_sec; /* Seconds. */
143 long int tv_nsec; /* Nanoseconds. */
146 int gettimeofday(struct timeval *tv, void *tz)
148 static int counter = 0;
151 counter++; /* to avoid collisions */
153 tv->tv_sec = tm.time;
154 tv->tv_usec = (tm.millitm * 1000) + counter;
159 int nanosleep(struct timespec *ts, void *rem)
161 sleep(ts->tv_sec * 1000 + ts->tv_nsec / 1000000);
167 static int create_timer_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
169 __extension__ long long temp;
176 micro_frame_delay = get_int(data) * (int)get_short(data+4);
177 if (g_spdFactorNum != 0)
179 temp = micro_frame_delay;
180 temp *= g_spdFactorNum;
181 temp /= g_spdFactorDenom;
182 micro_frame_delay = (int)temp;
188 static void timer_stop(void)
190 timer_expire.tv_sec = 0;
191 timer_expire.tv_usec = 0;
195 static void timer_start(void)
198 gettimeofday(&timer_expire, NULL);
199 timer_expire.tv_usec += micro_frame_delay;
200 if (timer_expire.tv_usec > 1000000)
202 nsec = timer_expire.tv_usec / 1000000;
203 timer_expire.tv_sec += nsec;
204 timer_expire.tv_usec -= nsec*1000000;
209 static void do_timer_wait(void)
217 gettimeofday(&tv, NULL);
218 if (tv.tv_sec > timer_expire.tv_sec)
220 else if (tv.tv_sec == timer_expire.tv_sec && tv.tv_usec >= timer_expire.tv_usec)
223 ts.tv_sec = timer_expire.tv_sec - tv.tv_sec;
224 ts.tv_nsec = 1000 * (timer_expire.tv_usec - tv.tv_usec);
227 ts.tv_nsec += 1000000000UL;
231 usleep(ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
233 if (nanosleep(&ts, NULL) == -1 && errno == EINTR)
238 timer_expire.tv_usec += micro_frame_delay;
239 if (timer_expire.tv_usec > 1000000)
241 nsec = timer_expire.tv_usec / 1000000;
242 timer_expire.tv_sec += nsec;
243 timer_expire.tv_usec -= nsec*1000000;
247 /*************************
249 *************************/
251 #define TOTAL_AUDIO_BUFFERS 64
253 static int audiobuf_created = 0;
254 static void mve_audio_callback(void *userdata, unsigned char *stream, int len);
255 static short *mve_audio_buffers[TOTAL_AUDIO_BUFFERS];
256 static int mve_audio_buflens[TOTAL_AUDIO_BUFFERS];
257 static int mve_audio_curbuf_curpos=0;
258 static int mve_audio_bufhead=0;
259 static int mve_audio_buftail=0;
260 static int mve_audio_playing=0;
261 static int mve_audio_canplay=0;
262 static int mve_audio_compressed=0;
263 static int mve_audio_enabled = 1;
264 static SDL_AudioSpec *mve_audio_spec=NULL;
266 static void mve_audio_callback(void *userdata, unsigned char *stream, int len)
270 if (mve_audio_bufhead == mve_audio_buftail)
273 //fprintf(stderr, "+ <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
275 while (mve_audio_bufhead != mve_audio_buftail /* while we have more buffers */
276 && len > (mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos)) /* and while we need more data */
278 length = mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos;
279 memcpy(stream, /* cur output position */
280 ((unsigned char *)mve_audio_buffers[mve_audio_bufhead])+mve_audio_curbuf_curpos, /* cur input position */
281 length); /* cur input length */
284 stream += length; /* advance output */
285 len -= length; /* decrement avail ospace */
286 d_free(mve_audio_buffers[mve_audio_bufhead]); /* free the buffer */
287 mve_audio_buffers[mve_audio_bufhead]=NULL; /* free the buffer */
288 mve_audio_buflens[mve_audio_bufhead]=0; /* free the buffer */
290 if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS) /* next buffer */
291 mve_audio_bufhead = 0;
292 mve_audio_curbuf_curpos = 0;
295 //fprintf(stderr, "= <%d (%d), %d, %d>: %d\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len, total);
298 if (len != 0 /* ospace remaining */
299 && mve_audio_bufhead != mve_audio_buftail) /* buffers remaining */
301 memcpy(stream, /* dest */
302 ((unsigned char *)mve_audio_buffers[mve_audio_bufhead]) + mve_audio_curbuf_curpos, /* src */
305 mve_audio_curbuf_curpos += len; /* advance input */
306 stream += len; /* advance output (unnecessary) */
307 len -= len; /* advance output (unnecessary) */
309 if (mve_audio_curbuf_curpos >= mve_audio_buflens[mve_audio_bufhead]) /* if this ends the current chunk */
311 d_free(mve_audio_buffers[mve_audio_bufhead]); /* free buffer */
312 mve_audio_buffers[mve_audio_bufhead]=NULL;
313 mve_audio_buflens[mve_audio_bufhead]=0;
315 if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS) /* next buffer */
316 mve_audio_bufhead = 0;
317 mve_audio_curbuf_curpos = 0;
321 //fprintf(stderr, "- <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
325 static int create_audiobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
338 if (!mve_audio_enabled)
341 if (audiobuf_created)
344 audiobuf_created = 1;
346 flags = get_ushort(data + 2);
347 sample_rate = get_ushort(data + 4);
348 desired_buffer = get_int(data + 6);
350 stereo = (flags & MVE_AUDIO_FLAGS_STEREO) ? 1 : 0;
351 bitsize = (flags & MVE_AUDIO_FLAGS_16BIT) ? 1 : 0;
354 compressed = flags & MVE_AUDIO_FLAGS_COMPRESSED ? 1 : 0;
359 mve_audio_compressed = compressed;
362 #ifdef WORDS_BIGENDIAN
363 format = AUDIO_S16MSB;
365 format = AUDIO_S16LSB;
371 fprintf(stderr, "creating audio buffers:\n");
372 fprintf(stderr, "sample rate = %d, stereo = %d, bitsize = %d, compressed = %d\n",
373 sample_rate, stereo, bitsize ? 16 : 8, compressed);
375 mve_audio_spec = (SDL_AudioSpec *)d_malloc(sizeof(SDL_AudioSpec));
376 mve_audio_spec->freq = sample_rate;
377 mve_audio_spec->format = format;
378 mve_audio_spec->channels = (stereo) ? 2 : 1;
379 mve_audio_spec->samples = 4096;
380 mve_audio_spec->callback = mve_audio_callback;
381 mve_audio_spec->userdata = NULL;
382 if (SDL_OpenAudio(mve_audio_spec, NULL) >= 0)
384 fprintf(stderr, " success\n");
385 mve_audio_canplay = 1;
389 fprintf(stderr, " failure : %s\n", SDL_GetError());
390 mve_audio_canplay = 0;
393 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
394 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
400 static int play_audio_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
403 if (mve_audio_canplay && !mve_audio_playing && mve_audio_bufhead != mve_audio_buftail)
406 mve_audio_playing = 1;
412 static int audio_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
415 static const int selected_chan=1;
418 if (mve_audio_canplay)
420 if (mve_audio_playing)
423 chan = get_ushort(data + 2);
424 nsamp = get_ushort(data + 4);
425 if (chan & selected_chan)
427 /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
428 if (major == MVE_OPCODE_AUDIOFRAMEDATA) {
429 if (mve_audio_compressed) {
432 mve_audio_buflens[mve_audio_buftail] = nsamp;
433 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
434 mveaudio_uncompress(mve_audio_buffers[mve_audio_buftail], data, -1); /* XXX */
439 mve_audio_buflens[mve_audio_buftail] = nsamp;
440 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
441 memcpy(mve_audio_buffers[mve_audio_buftail], data, nsamp);
444 mve_audio_buflens[mve_audio_buftail] = nsamp;
445 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
447 memset(mve_audio_buffers[mve_audio_buftail], 0, nsamp); /* XXX */
450 if (++mve_audio_buftail == TOTAL_AUDIO_BUFFERS)
451 mve_audio_buftail = 0;
453 if (mve_audio_buftail == mve_audio_bufhead)
454 fprintf(stderr, "d'oh! buffer ring overrun (%d)\n", mve_audio_bufhead);
457 if (mve_audio_playing)
465 /*************************
467 *************************/
468 static int videobuf_created = 0;
469 static int video_initialized = 0;
470 int g_width, g_height;
471 void *g_vBuffers = NULL, *g_vBackBuf1, *g_vBackBuf2;
474 static SDL_Surface *g_screen;
476 static int g_destX, g_destY;
478 static int g_screenWidth, g_screenHeight;
479 static unsigned char g_palette[768];
480 static unsigned char *g_pCurMap=NULL;
481 static int g_nMapLength=0;
482 static int g_truecolor;
484 static int create_videobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
487 short count, truecolor;
489 if (videobuf_created)
492 videobuf_created = 1;
495 h = get_short(data+2);
498 count = get_short(data+4);
504 truecolor = get_short(data+6);
512 /* TODO: * 4 causes crashes on some files */
513 g_vBackBuf1 = g_vBuffers = d_malloc(g_width * g_height * 8);
515 g_vBackBuf2 = (unsigned short *)g_vBackBuf1 + (g_width * g_height);
517 g_vBackBuf2 = (unsigned char *)g_vBackBuf1 + (g_width * g_height);
520 memset(g_vBackBuf1, 0, g_width * g_height * 4);
523 fprintf(stderr, "DEBUG: w,h=%d,%d count=%d, tc=%d\n", w, h, count, truecolor);
526 g_truecolor = truecolor;
532 static int do_sdl_events()
536 while (SDL_PollEvent(&event)) {
542 if (event.key.keysym.sym == SDLK_ESCAPE)
548 case SDL_MOUSEBUTTONDOWN:
550 if (event.button.button == SDL_BUTTON_LEFT) {
551 printf("GRID: %d,%d (pix:%d,%d)\n",
552 event.button.x / 16, event.button.y / 8,
553 event.button.x, event.button.y);
565 static void ConvertAndDraw()
568 unsigned char *pal = g_palette;
569 unsigned char *pDest;
570 unsigned char *pixels = g_vBackBuf1;
571 SDL_Surface *screenSprite, *initSprite;
575 initSprite = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, g_truecolor?16:8, 0x7C00, 0x03E0, 0x001F, 0);
578 for(i = 0; i < 256; i++) {
579 initSprite->format->palette->colors[i].r = (*pal++) << 2;
580 initSprite->format->palette->colors[i].g = (*pal++) << 2;
581 initSprite->format->palette->colors[i].b = (*pal++) << 2;
582 initSprite->format->palette->colors[i].unused = 0;
586 pDest = initSprite->pixels;
588 if (0 /*g_truecolor*/) {
590 unsigned short *pSrcs, *pDests;
592 pSrcs = (unsigned short *)pixels;
593 pDests = (unsigned short *)pDest;
595 for (y=0; y<g_height; y++) {
596 for (x = 0; x < g_width; x++) {
597 pDests[x] = (1<<15)|*pSrcs;
600 pDests += g_screenWidth;
605 for (i=0; i<g_height; i++) {
606 memcpy(pDest, pixels, g_width * (g_truecolor?2:1));
607 pixels += g_width* (g_truecolor?2:1);
608 pDest += initSprite->pitch;
612 screenSprite = SDL_DisplayFormat(initSprite);
613 SDL_FreeSurface(initSprite);
615 if (g_screenWidth > screenSprite->w)
616 x = (g_screenWidth - screenSprite->w) >> 1;
619 if (g_screenHeight > screenSprite->h)
620 y = (g_screenHeight - screenSprite->h) >> 1;
625 renderArea.w = MIN(g_screenWidth - x, screenSprite->w);
626 renderArea.h = MIN(g_screenHeight - y, screenSprite->h);
627 SDL_BlitSurface(screenSprite, NULL, g_screen, &renderArea);
629 SDL_FreeSurface(screenSprite);
633 static int display_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
644 bitmap = gr_create_bitmap_raw(g_width, g_height, g_vBackBuf1);
646 if (g_destX == -1) // center it
647 g_destX = (g_screenWidth - g_width) >> 1;
648 if (g_destY == -1) // center it
649 g_destY = (g_screenHeight - g_height) >> 1;
651 gr_palette_load(g_palette);
653 gr_bitmap(g_destX, g_destY, bitmap);
655 gr_free_sub_bitmap(bitmap);
662 static int init_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
666 if (video_initialized)
669 video_initialized = 1;
671 width = get_short(data);
672 height = get_short(data+2);
674 g_screen = SDL_SetVideoMode(width, height, 16, g_sdlVidFlags);
676 g_screenWidth = width;
677 g_screenHeight = height;
678 memset(g_palette, 0, 765);
679 // 255 needs to default to white, for subtitles, etc
680 memset(g_palette + 765, 63, 3);
685 static int video_palette_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
688 start = get_short(data);
689 count = get_short(data+2);
690 memcpy(g_palette + 3*start, data+4, 3*count);
695 static int video_codemap_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
702 static int video_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
704 short nFrameHot, nFrameCold;
705 short nXoffset, nYoffset;
706 short nXsize, nYsize;
707 unsigned short nFlags;
710 nFrameHot = get_short(data);
711 nFrameCold = get_short(data+2);
712 nXoffset = get_short(data+4);
713 nYoffset = get_short(data+6);
714 nXsize = get_short(data+8);
715 nYsize = get_short(data+10);
716 nFlags = get_ushort(data+12);
720 temp = (unsigned char *)g_vBackBuf1;
721 g_vBackBuf1 = g_vBackBuf2;
725 /* convert the frame */
727 decodeFrame16((unsigned char *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
729 decodeFrame8(g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
735 static int end_chunk_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
743 void initializeMovie(MVESTREAM *mve)
747 for (i = 0; i < 32; i++)
748 mve_set_handler(mve, i, default_seg_handler);
750 memset(unhandled_chunks, 0, 32*256);
752 mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM, end_movie_handler);
753 mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK, end_chunk_handler);
754 mve_set_handler(mve, MVE_OPCODE_CREATETIMER, create_timer_handler);
755 mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS, create_audiobuf_handler);
756 mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO, play_audio_handler);
757 mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS, create_videobuf_handler);
759 mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO, display_video_handler);
760 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA, audio_data_handler);
761 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE, audio_data_handler);
762 mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE, init_video_handler);
764 mve_set_handler(mve, MVE_OPCODE_SETPALETTE, video_palette_handler);
765 mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, video_palette_handler);
766 mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP, video_codemap_handler);
768 mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
771 void playMovie(MVESTREAM *mve)
776 mve_audio_enabled = 1;
778 while (cont && playing)
780 cont = mve_play_next_chunk(mve);
781 if (micro_frame_delay && !init_timer)
789 if (g_loop && !cont) {
796 void shutdownMovie(MVESTREAM *mve)
805 for (i = 0; i < 32*256; i++) {
806 if (unhandled_chunks[i]) {
807 fprintf(stderr, "unhandled chunks of type %02x/%02x: %d\n", i>>8, i&0xFF, unhandled_chunks[i]);
814 static MVESTREAM *mve = NULL;
816 int MVE_rmPrepMovie(int filehandle, int x, int y, int track)
825 mve = mve_open_filehandle(filehandle);
833 for (i = 0; i < 32; i++)
834 mve_set_handler(mve, i, default_seg_handler);
836 mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM, end_movie_handler);
837 mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK, end_chunk_handler);
838 mve_set_handler(mve, MVE_OPCODE_CREATETIMER, create_timer_handler);
839 mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS, create_audiobuf_handler);
840 mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO, play_audio_handler);
841 mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS, create_videobuf_handler);
843 mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO, display_video_handler);
844 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA, audio_data_handler);
845 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE, audio_data_handler);
846 mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE, init_video_handler);
848 mve_set_handler(mve, MVE_OPCODE_SETPALETTE, video_palette_handler);
849 mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, default_seg_handler);
851 mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP, video_codemap_handler);
853 mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
858 int MVE_rmStepMovie()
860 static int init_timer=0;
866 while (cont && !g_frameUpdated) // make a "step" be a frame, not a chunk...
867 cont = mve_play_next_chunk(mve);
870 if (micro_frame_delay && !init_timer) {
883 void MVE_rmEndMovie()
893 if (mve_audio_canplay) {
894 // only close audio if we opened it
896 mve_audio_canplay = 0;
898 for (i = 0; i < TOTAL_AUDIO_BUFFERS; i++)
899 if (mve_audio_buffers[i] != NULL)
900 d_free(mve_audio_buffers[i]);
901 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
902 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
903 mve_audio_curbuf_curpos=0;
908 mve_audio_compressed=0;
910 d_free(mve_audio_spec);
912 audiobuf_created = 0;
919 videobuf_created = 0;
920 video_initialized = 0;
922 mve_close_filehandle(mve);
927 void MVE_rmHoldMovie()
933 void MVE_sndInit(int x)
937 mve_audio_enabled = 0;
939 mve_audio_enabled = 1;