1 /* $Id: mveplay.c,v 1.2 2003-02-18 23:28:47 btb Exp $ */
15 #include <sys/types.h>
20 #if defined(STANDALONE) || defined(AUDIO)
25 #include "mve_audio.h"
36 #define d_malloc(size) malloc(size)
37 #define d_free(ptr) free(ptr)
41 #define MIN(a,b) ((a)<(b)?(a):(b))
44 #define MVE_OPCODE_ENDOFSTREAM 0x00
45 #define MVE_OPCODE_ENDOFCHUNK 0x01
46 #define MVE_OPCODE_CREATETIMER 0x02
47 #define MVE_OPCODE_INITAUDIOBUFFERS 0x03
48 #define MVE_OPCODE_STARTSTOPAUDIO 0x04
49 #define MVE_OPCODE_INITVIDEOBUFFERS 0x05
51 #define MVE_OPCODE_DISPLAYVIDEO 0x07
52 #define MVE_OPCODE_AUDIOFRAMEDATA 0x08
53 #define MVE_OPCODE_AUDIOFRAMESILENCE 0x09
54 #define MVE_OPCODE_INITVIDEOMODE 0x0A
56 #define MVE_OPCODE_SETPALETTE 0x0C
57 #define MVE_OPCODE_SETPALETTECOMPRESSED 0x0D
59 #define MVE_OPCODE_SETDECODINGMAP 0x0F
61 #define MVE_OPCODE_VIDEODATA 0x11
63 #define MVE_AUDIO_FLAGS_STEREO 1
64 #define MVE_AUDIO_FLAGS_16BIT 2
65 #define MVE_AUDIO_FLAGS_COMPRESSED 4
68 static int g_spdFactorDenom=10;
69 static int g_frameUpdated = 0;
72 static int playing = 1;
73 int g_sdlVidFlags = SDL_ANYFORMAT | SDL_DOUBLEBUF;
76 void initializeMovie(MVESTREAM *mve);
77 void playMovie(MVESTREAM *mve);
78 void shutdownMovie(MVESTREAM *mve);
81 static short get_short(unsigned char *data)
84 value = data[0] | (data[1] << 8);
88 static unsigned short get_ushort(unsigned char *data)
91 value = data[0] | (data[1] << 8);
95 static int get_int(unsigned char *data)
98 value = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
102 static unsigned int unhandled_chunks[32*256];
104 static int default_seg_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
106 unhandled_chunks[major<<8|minor]++;
107 //fprintf(stderr, "unknown chunk type %02x/%02x\n", major, minor);
112 /*************************
114 *************************/
115 static int end_movie_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
120 /*************************
122 *************************/
127 static int timer_created = 0;
128 static int micro_frame_delay=0;
129 static int timer_started=0;
130 static struct timeval timer_expire = {0, 0};
133 #include <sys/timeb.h>
137 long int tv_sec; /* Seconds. */
138 long int tv_nsec; /* Nanoseconds. */
141 int gettimeofday(struct timeval *tv, void *tz)
143 static int counter = 0;
146 counter++; /* to avoid collisions */
148 tv->tv_sec = tm.time;
149 tv->tv_usec = (tm.millitm * 1000) + counter;
154 int nanosleep(struct timespec *ts, void *rem)
156 sleep(ts->tv_sec * 1000 + ts->tv_nsec / 1000000);
162 static int create_timer_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
164 __extension__ long long temp;
171 micro_frame_delay = get_int(data) * (int)get_short(data+4);
172 if (g_spdFactorNum != 0)
174 temp = micro_frame_delay;
175 temp *= g_spdFactorNum;
176 temp /= g_spdFactorDenom;
177 micro_frame_delay = (int)temp;
183 static void timer_stop(void)
185 timer_expire.tv_sec = 0;
186 timer_expire.tv_usec = 0;
190 static void timer_start(void)
193 gettimeofday(&timer_expire, NULL);
194 timer_expire.tv_usec += micro_frame_delay;
195 if (timer_expire.tv_usec > 1000000)
197 nsec = timer_expire.tv_usec / 1000000;
198 timer_expire.tv_sec += nsec;
199 timer_expire.tv_usec -= nsec*1000000;
204 static void do_timer_wait(void)
212 gettimeofday(&tv, NULL);
213 if (tv.tv_sec > timer_expire.tv_sec)
215 else if (tv.tv_sec == timer_expire.tv_sec && tv.tv_usec >= timer_expire.tv_usec)
218 ts.tv_sec = timer_expire.tv_sec - tv.tv_sec;
219 ts.tv_nsec = 1000 * (timer_expire.tv_usec - tv.tv_usec);
222 ts.tv_nsec += 1000000000UL;
226 usleep(ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
228 if (nanosleep(&ts, NULL) == -1 && errno == EINTR)
233 timer_expire.tv_usec += micro_frame_delay;
234 if (timer_expire.tv_usec > 1000000)
236 nsec = timer_expire.tv_usec / 1000000;
237 timer_expire.tv_sec += nsec;
238 timer_expire.tv_usec -= nsec*1000000;
242 /*************************
244 *************************/
246 #define TOTAL_AUDIO_BUFFERS 64
248 static int audiobuf_created = 0;
249 static void mve_audio_callback(void *userdata, unsigned char *stream, int len);
250 static short *mve_audio_buffers[TOTAL_AUDIO_BUFFERS];
251 static int mve_audio_buflens[TOTAL_AUDIO_BUFFERS];
252 static int mve_audio_curbuf_curpos=0;
253 static int mve_audio_bufhead=0;
254 static int mve_audio_buftail=0;
255 static int mve_audio_playing=0;
256 static int mve_audio_canplay=0;
257 static int mve_audio_compressed=0;
258 static SDL_AudioSpec *mve_audio_spec=NULL;
260 static void mve_audio_callback(void *userdata, unsigned char *stream, int len)
264 if (mve_audio_bufhead == mve_audio_buftail)
267 //fprintf(stderr, "+ <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
269 while (mve_audio_bufhead != mve_audio_buftail /* while we have more buffers */
270 && len > (mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos)) /* and while we need more data */
272 length = mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos;
273 memcpy(stream, /* cur output position */
274 ((unsigned char *)mve_audio_buffers[mve_audio_bufhead])+mve_audio_curbuf_curpos, /* cur input position */
275 length); /* cur input length */
278 stream += length; /* advance output */
279 len -= length; /* decrement avail ospace */
280 d_free(mve_audio_buffers[mve_audio_bufhead]); /* free the buffer */
281 mve_audio_buffers[mve_audio_bufhead]=NULL; /* free the buffer */
282 mve_audio_buflens[mve_audio_bufhead]=0; /* free the buffer */
284 if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS) /* next buffer */
285 mve_audio_bufhead = 0;
286 mve_audio_curbuf_curpos = 0;
289 //fprintf(stderr, "= <%d (%d), %d, %d>: %d\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len, total);
292 if (len != 0 /* ospace remaining */
293 && mve_audio_bufhead != mve_audio_buftail) /* buffers remaining */
295 memcpy(stream, /* dest */
296 ((unsigned char *)mve_audio_buffers[mve_audio_bufhead]) + mve_audio_curbuf_curpos, /* src */
299 mve_audio_curbuf_curpos += len; /* advance input */
300 stream += len; /* advance output (unnecessary) */
301 len -= len; /* advance output (unnecessary) */
303 if (mve_audio_curbuf_curpos >= mve_audio_buflens[mve_audio_bufhead]) /* if this ends the current chunk */
305 d_free(mve_audio_buffers[mve_audio_bufhead]); /* free buffer */
306 mve_audio_buffers[mve_audio_bufhead]=NULL;
307 mve_audio_buflens[mve_audio_bufhead]=0;
309 if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS) /* next buffer */
310 mve_audio_bufhead = 0;
311 mve_audio_curbuf_curpos = 0;
315 //fprintf(stderr, "- <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
319 static int create_audiobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
332 if (audiobuf_created)
335 audiobuf_created = 1;
338 if (FindArg("-nosound"))
342 flags = get_ushort(data + 2);
343 sample_rate = get_ushort(data + 4);
344 desired_buffer = get_int(data + 6);
346 stereo = (flags & MVE_AUDIO_FLAGS_STEREO) ? 1 : 0;
347 bitsize = (flags & MVE_AUDIO_FLAGS_16BIT) ? 1 : 0;
350 compressed = flags & MVE_AUDIO_FLAGS_COMPRESSED ? 1 : 0;
355 mve_audio_compressed = compressed;
358 format = AUDIO_S16LSB;
363 fprintf(stderr, "creating audio buffers:\n");
364 fprintf(stderr, "sample rate = %d, stereo = %d, bitsize = %d, compressed = %d\n",
365 sample_rate, stereo, bitsize ? 16 : 8, compressed);
367 mve_audio_spec = (SDL_AudioSpec *)d_malloc(sizeof(SDL_AudioSpec));
368 mve_audio_spec->freq = sample_rate;
369 mve_audio_spec->format = format;
370 mve_audio_spec->channels = (stereo) ? 2 : 1;
371 mve_audio_spec->samples = 4096;
372 mve_audio_spec->callback = mve_audio_callback;
373 mve_audio_spec->userdata = NULL;
374 if (SDL_OpenAudio(mve_audio_spec, NULL) >= 0)
376 fprintf(stderr, " success\n");
377 mve_audio_canplay = 1;
381 fprintf(stderr, " failure : %s\n", SDL_GetError());
382 mve_audio_canplay = 0;
385 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
386 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
392 static int play_audio_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
395 if (mve_audio_canplay && !mve_audio_playing && mve_audio_bufhead != mve_audio_buftail)
398 mve_audio_playing = 1;
404 static int audio_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
407 static const int selected_chan=1;
410 if (mve_audio_canplay)
412 if (mve_audio_playing)
415 chan = get_ushort(data + 2);
416 nsamp = get_ushort(data + 4);
417 if (chan & selected_chan)
419 /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
420 if (major == MVE_OPCODE_AUDIOFRAMEDATA) {
421 if (mve_audio_compressed) {
424 mve_audio_buflens[mve_audio_buftail] = nsamp;
425 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
426 mveaudio_uncompress(mve_audio_buffers[mve_audio_buftail], data, -1); /* XXX */
431 mve_audio_buflens[mve_audio_buftail] = nsamp;
432 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
433 memcpy(mve_audio_buffers[mve_audio_buftail], data, nsamp);
436 mve_audio_buflens[mve_audio_buftail] = nsamp;
437 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
439 memset(mve_audio_buffers[mve_audio_buftail], 0, nsamp); /* XXX */
442 if (++mve_audio_buftail == TOTAL_AUDIO_BUFFERS)
443 mve_audio_buftail = 0;
445 if (mve_audio_buftail == mve_audio_bufhead)
446 fprintf(stderr, "d'oh! buffer ring overrun (%d)\n", mve_audio_bufhead);
449 if (mve_audio_playing)
457 /*************************
459 *************************/
460 static int videobuf_created = 0;
461 static int video_initialized = 0;
462 int g_width, g_height;
463 void *g_vBuffers = NULL, *g_vBackBuf1, *g_vBackBuf2;
466 static SDL_Surface *g_screen;
468 static int g_destX, g_destY;
470 static int g_screenWidth, g_screenHeight;
471 static unsigned char g_palette[768];
472 static unsigned char *g_pCurMap=NULL;
473 static int g_nMapLength=0;
474 static int g_truecolor;
476 static int create_videobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
479 short count, truecolor;
481 if (videobuf_created)
484 videobuf_created = 1;
487 h = get_short(data+2);
490 count = get_short(data+4);
496 truecolor = get_short(data+6);
504 /* TODO: * 4 causes crashes on some files */
505 g_vBackBuf1 = g_vBuffers = d_malloc(g_width * g_height * 8);
507 g_vBackBuf2 = (unsigned short *)g_vBackBuf1 + (g_width * g_height);
509 g_vBackBuf2 = (unsigned char *)g_vBackBuf1 + (g_width * g_height);
512 memset(g_vBackBuf1, 0, g_width * g_height * 4);
515 fprintf(stderr, "DEBUG: w,h=%d,%d count=%d, tc=%d\n", w, h, count, truecolor);
518 g_truecolor = truecolor;
524 static int do_sdl_events()
528 while (SDL_PollEvent(&event)) {
534 if (event.key.keysym.sym == SDLK_ESCAPE)
540 case SDL_MOUSEBUTTONDOWN:
542 if (event.button.button == SDL_BUTTON_LEFT) {
543 printf("GRID: %d,%d (pix:%d,%d)\n",
544 event.button.x / 16, event.button.y / 8,
545 event.button.x, event.button.y);
557 static void ConvertAndDraw()
560 unsigned char *pal = g_palette;
561 unsigned char *pDest;
562 unsigned char *pixels = g_vBackBuf1;
563 SDL_Surface *screenSprite, *initSprite;
567 initSprite = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, g_truecolor?16:8, 0x7C00, 0x03E0, 0x001F, 0);
570 for(i = 0; i < 256; i++) {
571 initSprite->format->palette->colors[i].r = (*pal++) << 2;
572 initSprite->format->palette->colors[i].g = (*pal++) << 2;
573 initSprite->format->palette->colors[i].b = (*pal++) << 2;
574 initSprite->format->palette->colors[i].unused = 0;
578 pDest = initSprite->pixels;
580 if (0 /*g_truecolor*/) {
582 unsigned short *pSrcs, *pDests;
584 pSrcs = (unsigned short *)pixels;
585 pDests = (unsigned short *)pDest;
587 for (y=0; y<g_height; y++) {
588 for (x = 0; x < g_width; x++) {
589 pDests[x] = (1<<15)|*pSrcs;
592 pDests += g_screenWidth;
597 for (i=0; i<g_height; i++) {
598 memcpy(pDest, pixels, g_width * (g_truecolor?2:1));
599 pixels += g_width* (g_truecolor?2:1);
600 pDest += initSprite->pitch;
604 screenSprite = SDL_DisplayFormat(initSprite);
605 SDL_FreeSurface(initSprite);
607 if (g_screenWidth > screenSprite->w)
608 x = (g_screenWidth - screenSprite->w) >> 1;
611 if (g_screenHeight > screenSprite->h)
612 y = (g_screenHeight - screenSprite->h) >> 1;
617 renderArea.w = MIN(g_screenWidth - x, screenSprite->w);
618 renderArea.h = MIN(g_screenHeight - y, screenSprite->h);
619 SDL_BlitSurface(screenSprite, NULL, g_screen, &renderArea);
621 SDL_FreeSurface(screenSprite);
625 static int display_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
636 bitmap = gr_create_bitmap_raw(g_width, g_height, g_vBackBuf1);
638 if (g_destX == -1) // center it
639 g_destX = (g_screenWidth - g_width) >> 1;
640 if (g_destY == -1) // center it
641 g_destY = (g_screenHeight - g_height) >> 1;
643 gr_bitmap(g_destX, g_destY, bitmap);
645 gr_free_sub_bitmap(bitmap);
647 gr_palette_load(g_palette);
654 static int init_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
658 if (video_initialized)
661 video_initialized = 1;
663 width = get_short(data);
664 height = get_short(data+2);
666 g_screen = SDL_SetVideoMode(width, height, 16, g_sdlVidFlags);
668 g_screenWidth = width;
669 g_screenHeight = height;
670 memset(g_palette, 0, 765);
671 // 255 needs to default to white, for subtitles, etc
672 memset(g_palette + 765, 63, 3);
677 static int video_palette_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
680 start = get_short(data);
681 count = get_short(data+2);
682 memcpy(g_palette + 3*start, data+4, 3*count);
687 static int video_codemap_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
694 void decodeFrame16(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain);
695 void decodeFrame8(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain);
697 static int video_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
699 short nFrameHot, nFrameCold;
700 short nXoffset, nYoffset;
701 short nXsize, nYsize;
702 unsigned short nFlags;
705 nFrameHot = get_short(data);
706 nFrameCold = get_short(data+2);
707 nXoffset = get_short(data+4);
708 nYoffset = get_short(data+6);
709 nXsize = get_short(data+8);
710 nYsize = get_short(data+10);
711 nFlags = get_ushort(data+12);
715 temp = (unsigned char *)g_vBackBuf1;
716 g_vBackBuf1 = g_vBackBuf2;
720 /* convert the frame */
722 decodeFrame16((unsigned char *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
724 decodeFrame8(g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
730 static int end_chunk_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
738 void initializeMovie(MVESTREAM *mve)
742 for (i = 0; i < 32; i++)
743 mve_set_handler(mve, i, default_seg_handler);
745 memset(unhandled_chunks, 0, 32*256);
747 mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM, end_movie_handler);
748 mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK, end_chunk_handler);
749 mve_set_handler(mve, MVE_OPCODE_CREATETIMER, create_timer_handler);
750 mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS, create_audiobuf_handler);
751 mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO, play_audio_handler);
752 mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS, create_videobuf_handler);
754 mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO, display_video_handler);
755 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA, audio_data_handler);
756 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE, audio_data_handler);
757 mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE, init_video_handler);
759 mve_set_handler(mve, MVE_OPCODE_SETPALETTE, video_palette_handler);
760 mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, video_palette_handler);
761 mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP, video_codemap_handler);
763 mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
766 void playMovie(MVESTREAM *mve)
770 while (cont && playing)
772 cont = mve_play_next_chunk(mve);
773 if (micro_frame_delay && !init_timer)
781 if (g_loop && !cont) {
788 void shutdownMovie(MVESTREAM *mve)
797 for (i = 0; i < 32*256; i++) {
798 if (unhandled_chunks[i]) {
799 fprintf(stderr, "unhandled chunks of type %02x/%02x: %d\n", i>>8, i&0xFF, unhandled_chunks[i]);
806 static MVESTREAM *mve = NULL;
808 int MVE_rmPrepMovie(int filehandle, int x, int y, int track)
817 mve = mve_open_filehandle(filehandle);
825 for (i = 0; i < 32; i++)
826 mve_set_handler(mve, i, default_seg_handler);
828 mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM, end_movie_handler);
829 mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK, end_chunk_handler);
830 mve_set_handler(mve, MVE_OPCODE_CREATETIMER, create_timer_handler);
831 mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS, create_audiobuf_handler);
832 mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO, play_audio_handler);
833 mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS, create_videobuf_handler);
835 mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO, display_video_handler);
836 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA, audio_data_handler);
837 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE, audio_data_handler);
838 mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE, init_video_handler);
840 mve_set_handler(mve, MVE_OPCODE_SETPALETTE, video_palette_handler);
841 mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, default_seg_handler);
843 mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP, video_codemap_handler);
845 mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
850 int MVE_rmStepMovie()
852 static int init_timer=0;
858 while (cont && !g_frameUpdated) // make a "step" be a frame, not a chunk...
859 cont = mve_play_next_chunk(mve);
862 if (micro_frame_delay && !init_timer) {
875 void MVE_rmEndMovie()
886 if (mve_audio_canplay) {
887 // only close audio if we opened it
888 mve_audio_canplay = 0;
890 for (i = 0; i < TOTAL_AUDIO_BUFFERS; i++)
891 if (mve_audio_buffers[i] != NULL)
892 d_free(mve_audio_buffers[i]);
893 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
894 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
895 mve_audio_curbuf_curpos=0;
900 mve_audio_compressed=0;
902 d_free(mve_audio_spec);
904 audiobuf_created = 0;
911 videobuf_created = 0;
912 video_initialized = 0;
914 mve_close_filehandle(mve);
919 void MVE_rmHoldMovie()