1 /* $Id: mveplay.c,v 1.3 2003-02-19 00:42:40 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 #include <sys/timeb.h>
138 long int tv_sec; /* Seconds. */
139 long int tv_nsec; /* Nanoseconds. */
142 int gettimeofday(struct timeval *tv, void *tz)
144 static int counter = 0;
147 counter++; /* to avoid collisions */
149 tv->tv_sec = tm.time;
150 tv->tv_usec = (tm.millitm * 1000) + counter;
155 int nanosleep(struct timespec *ts, void *rem)
157 sleep(ts->tv_sec * 1000 + ts->tv_nsec / 1000000);
163 static int create_timer_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
165 __extension__ long long temp;
172 micro_frame_delay = get_int(data) * (int)get_short(data+4);
173 if (g_spdFactorNum != 0)
175 temp = micro_frame_delay;
176 temp *= g_spdFactorNum;
177 temp /= g_spdFactorDenom;
178 micro_frame_delay = (int)temp;
184 static void timer_stop(void)
186 timer_expire.tv_sec = 0;
187 timer_expire.tv_usec = 0;
191 static void timer_start(void)
194 gettimeofday(&timer_expire, NULL);
195 timer_expire.tv_usec += micro_frame_delay;
196 if (timer_expire.tv_usec > 1000000)
198 nsec = timer_expire.tv_usec / 1000000;
199 timer_expire.tv_sec += nsec;
200 timer_expire.tv_usec -= nsec*1000000;
205 static void do_timer_wait(void)
213 gettimeofday(&tv, NULL);
214 if (tv.tv_sec > timer_expire.tv_sec)
216 else if (tv.tv_sec == timer_expire.tv_sec && tv.tv_usec >= timer_expire.tv_usec)
219 ts.tv_sec = timer_expire.tv_sec - tv.tv_sec;
220 ts.tv_nsec = 1000 * (timer_expire.tv_usec - tv.tv_usec);
223 ts.tv_nsec += 1000000000UL;
227 usleep(ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
229 if (nanosleep(&ts, NULL) == -1 && errno == EINTR)
234 timer_expire.tv_usec += micro_frame_delay;
235 if (timer_expire.tv_usec > 1000000)
237 nsec = timer_expire.tv_usec / 1000000;
238 timer_expire.tv_sec += nsec;
239 timer_expire.tv_usec -= nsec*1000000;
243 /*************************
245 *************************/
247 #define TOTAL_AUDIO_BUFFERS 64
249 static int audiobuf_created = 0;
250 static void mve_audio_callback(void *userdata, unsigned char *stream, int len);
251 static short *mve_audio_buffers[TOTAL_AUDIO_BUFFERS];
252 static int mve_audio_buflens[TOTAL_AUDIO_BUFFERS];
253 static int mve_audio_curbuf_curpos=0;
254 static int mve_audio_bufhead=0;
255 static int mve_audio_buftail=0;
256 static int mve_audio_playing=0;
257 static int mve_audio_canplay=0;
258 static int mve_audio_compressed=0;
259 static SDL_AudioSpec *mve_audio_spec=NULL;
261 static void mve_audio_callback(void *userdata, unsigned char *stream, int len)
265 if (mve_audio_bufhead == mve_audio_buftail)
268 //fprintf(stderr, "+ <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
270 while (mve_audio_bufhead != mve_audio_buftail /* while we have more buffers */
271 && len > (mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos)) /* and while we need more data */
273 length = mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos;
274 memcpy(stream, /* cur output position */
275 ((unsigned char *)mve_audio_buffers[mve_audio_bufhead])+mve_audio_curbuf_curpos, /* cur input position */
276 length); /* cur input length */
279 stream += length; /* advance output */
280 len -= length; /* decrement avail ospace */
281 d_free(mve_audio_buffers[mve_audio_bufhead]); /* free the buffer */
282 mve_audio_buffers[mve_audio_bufhead]=NULL; /* free the buffer */
283 mve_audio_buflens[mve_audio_bufhead]=0; /* free the buffer */
285 if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS) /* next buffer */
286 mve_audio_bufhead = 0;
287 mve_audio_curbuf_curpos = 0;
290 //fprintf(stderr, "= <%d (%d), %d, %d>: %d\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len, total);
293 if (len != 0 /* ospace remaining */
294 && mve_audio_bufhead != mve_audio_buftail) /* buffers remaining */
296 memcpy(stream, /* dest */
297 ((unsigned char *)mve_audio_buffers[mve_audio_bufhead]) + mve_audio_curbuf_curpos, /* src */
300 mve_audio_curbuf_curpos += len; /* advance input */
301 stream += len; /* advance output (unnecessary) */
302 len -= len; /* advance output (unnecessary) */
304 if (mve_audio_curbuf_curpos >= mve_audio_buflens[mve_audio_bufhead]) /* if this ends the current chunk */
306 d_free(mve_audio_buffers[mve_audio_bufhead]); /* free buffer */
307 mve_audio_buffers[mve_audio_bufhead]=NULL;
308 mve_audio_buflens[mve_audio_bufhead]=0;
310 if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS) /* next buffer */
311 mve_audio_bufhead = 0;
312 mve_audio_curbuf_curpos = 0;
316 //fprintf(stderr, "- <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
320 static int create_audiobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
333 if (audiobuf_created)
336 audiobuf_created = 1;
339 if (FindArg("-nosound"))
343 flags = get_ushort(data + 2);
344 sample_rate = get_ushort(data + 4);
345 desired_buffer = get_int(data + 6);
347 stereo = (flags & MVE_AUDIO_FLAGS_STEREO) ? 1 : 0;
348 bitsize = (flags & MVE_AUDIO_FLAGS_16BIT) ? 1 : 0;
351 compressed = flags & MVE_AUDIO_FLAGS_COMPRESSED ? 1 : 0;
356 mve_audio_compressed = compressed;
359 #ifdef WORDS_BIGENDIAN
360 format = AUDIO_S16MSB;
362 format = AUDIO_S16LSB;
368 fprintf(stderr, "creating audio buffers:\n");
369 fprintf(stderr, "sample rate = %d, stereo = %d, bitsize = %d, compressed = %d\n",
370 sample_rate, stereo, bitsize ? 16 : 8, compressed);
372 mve_audio_spec = (SDL_AudioSpec *)d_malloc(sizeof(SDL_AudioSpec));
373 mve_audio_spec->freq = sample_rate;
374 mve_audio_spec->format = format;
375 mve_audio_spec->channels = (stereo) ? 2 : 1;
376 mve_audio_spec->samples = 4096;
377 mve_audio_spec->callback = mve_audio_callback;
378 mve_audio_spec->userdata = NULL;
379 if (SDL_OpenAudio(mve_audio_spec, NULL) >= 0)
381 fprintf(stderr, " success\n");
382 mve_audio_canplay = 1;
386 fprintf(stderr, " failure : %s\n", SDL_GetError());
387 mve_audio_canplay = 0;
390 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
391 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
397 static int play_audio_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
400 if (mve_audio_canplay && !mve_audio_playing && mve_audio_bufhead != mve_audio_buftail)
403 mve_audio_playing = 1;
409 static int audio_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
412 static const int selected_chan=1;
415 if (mve_audio_canplay)
417 if (mve_audio_playing)
420 chan = get_ushort(data + 2);
421 nsamp = get_ushort(data + 4);
422 if (chan & selected_chan)
424 /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
425 if (major == MVE_OPCODE_AUDIOFRAMEDATA) {
426 if (mve_audio_compressed) {
429 mve_audio_buflens[mve_audio_buftail] = nsamp;
430 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
431 mveaudio_uncompress(mve_audio_buffers[mve_audio_buftail], data, -1); /* XXX */
436 mve_audio_buflens[mve_audio_buftail] = nsamp;
437 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
438 memcpy(mve_audio_buffers[mve_audio_buftail], data, nsamp);
441 mve_audio_buflens[mve_audio_buftail] = nsamp;
442 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
444 memset(mve_audio_buffers[mve_audio_buftail], 0, nsamp); /* XXX */
447 if (++mve_audio_buftail == TOTAL_AUDIO_BUFFERS)
448 mve_audio_buftail = 0;
450 if (mve_audio_buftail == mve_audio_bufhead)
451 fprintf(stderr, "d'oh! buffer ring overrun (%d)\n", mve_audio_bufhead);
454 if (mve_audio_playing)
462 /*************************
464 *************************/
465 static int videobuf_created = 0;
466 static int video_initialized = 0;
467 int g_width, g_height;
468 void *g_vBuffers = NULL, *g_vBackBuf1, *g_vBackBuf2;
471 static SDL_Surface *g_screen;
473 static int g_destX, g_destY;
475 static int g_screenWidth, g_screenHeight;
476 static unsigned char g_palette[768];
477 static unsigned char *g_pCurMap=NULL;
478 static int g_nMapLength=0;
479 static int g_truecolor;
481 static int create_videobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
484 short count, truecolor;
486 if (videobuf_created)
489 videobuf_created = 1;
492 h = get_short(data+2);
495 count = get_short(data+4);
501 truecolor = get_short(data+6);
509 /* TODO: * 4 causes crashes on some files */
510 g_vBackBuf1 = g_vBuffers = d_malloc(g_width * g_height * 8);
512 g_vBackBuf2 = (unsigned short *)g_vBackBuf1 + (g_width * g_height);
514 g_vBackBuf2 = (unsigned char *)g_vBackBuf1 + (g_width * g_height);
517 memset(g_vBackBuf1, 0, g_width * g_height * 4);
520 fprintf(stderr, "DEBUG: w,h=%d,%d count=%d, tc=%d\n", w, h, count, truecolor);
523 g_truecolor = truecolor;
529 static int do_sdl_events()
533 while (SDL_PollEvent(&event)) {
539 if (event.key.keysym.sym == SDLK_ESCAPE)
545 case SDL_MOUSEBUTTONDOWN:
547 if (event.button.button == SDL_BUTTON_LEFT) {
548 printf("GRID: %d,%d (pix:%d,%d)\n",
549 event.button.x / 16, event.button.y / 8,
550 event.button.x, event.button.y);
562 static void ConvertAndDraw()
565 unsigned char *pal = g_palette;
566 unsigned char *pDest;
567 unsigned char *pixels = g_vBackBuf1;
568 SDL_Surface *screenSprite, *initSprite;
572 initSprite = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, g_truecolor?16:8, 0x7C00, 0x03E0, 0x001F, 0);
575 for(i = 0; i < 256; i++) {
576 initSprite->format->palette->colors[i].r = (*pal++) << 2;
577 initSprite->format->palette->colors[i].g = (*pal++) << 2;
578 initSprite->format->palette->colors[i].b = (*pal++) << 2;
579 initSprite->format->palette->colors[i].unused = 0;
583 pDest = initSprite->pixels;
585 if (0 /*g_truecolor*/) {
587 unsigned short *pSrcs, *pDests;
589 pSrcs = (unsigned short *)pixels;
590 pDests = (unsigned short *)pDest;
592 for (y=0; y<g_height; y++) {
593 for (x = 0; x < g_width; x++) {
594 pDests[x] = (1<<15)|*pSrcs;
597 pDests += g_screenWidth;
602 for (i=0; i<g_height; i++) {
603 memcpy(pDest, pixels, g_width * (g_truecolor?2:1));
604 pixels += g_width* (g_truecolor?2:1);
605 pDest += initSprite->pitch;
609 screenSprite = SDL_DisplayFormat(initSprite);
610 SDL_FreeSurface(initSprite);
612 if (g_screenWidth > screenSprite->w)
613 x = (g_screenWidth - screenSprite->w) >> 1;
616 if (g_screenHeight > screenSprite->h)
617 y = (g_screenHeight - screenSprite->h) >> 1;
622 renderArea.w = MIN(g_screenWidth - x, screenSprite->w);
623 renderArea.h = MIN(g_screenHeight - y, screenSprite->h);
624 SDL_BlitSurface(screenSprite, NULL, g_screen, &renderArea);
626 SDL_FreeSurface(screenSprite);
630 static int display_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
641 bitmap = gr_create_bitmap_raw(g_width, g_height, g_vBackBuf1);
643 if (g_destX == -1) // center it
644 g_destX = (g_screenWidth - g_width) >> 1;
645 if (g_destY == -1) // center it
646 g_destY = (g_screenHeight - g_height) >> 1;
648 gr_bitmap(g_destX, g_destY, bitmap);
650 gr_free_sub_bitmap(bitmap);
652 gr_palette_load(g_palette);
659 static int init_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
663 if (video_initialized)
666 video_initialized = 1;
668 width = get_short(data);
669 height = get_short(data+2);
671 g_screen = SDL_SetVideoMode(width, height, 16, g_sdlVidFlags);
673 g_screenWidth = width;
674 g_screenHeight = height;
675 memset(g_palette, 0, 765);
676 // 255 needs to default to white, for subtitles, etc
677 memset(g_palette + 765, 63, 3);
682 static int video_palette_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
685 start = get_short(data);
686 count = get_short(data+2);
687 memcpy(g_palette + 3*start, data+4, 3*count);
692 static int video_codemap_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
699 void decodeFrame16(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain);
700 void decodeFrame8(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain);
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)
775 while (cont && playing)
777 cont = mve_play_next_chunk(mve);
778 if (micro_frame_delay && !init_timer)
786 if (g_loop && !cont) {
793 void shutdownMovie(MVESTREAM *mve)
802 for (i = 0; i < 32*256; i++) {
803 if (unhandled_chunks[i]) {
804 fprintf(stderr, "unhandled chunks of type %02x/%02x: %d\n", i>>8, i&0xFF, unhandled_chunks[i]);
811 static MVESTREAM *mve = NULL;
813 int MVE_rmPrepMovie(int filehandle, int x, int y, int track)
822 mve = mve_open_filehandle(filehandle);
830 for (i = 0; i < 32; i++)
831 mve_set_handler(mve, i, default_seg_handler);
833 mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM, end_movie_handler);
834 mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK, end_chunk_handler);
835 mve_set_handler(mve, MVE_OPCODE_CREATETIMER, create_timer_handler);
836 mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS, create_audiobuf_handler);
837 mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO, play_audio_handler);
838 mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS, create_videobuf_handler);
840 mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO, display_video_handler);
841 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA, audio_data_handler);
842 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE, audio_data_handler);
843 mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE, init_video_handler);
845 mve_set_handler(mve, MVE_OPCODE_SETPALETTE, video_palette_handler);
846 mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, default_seg_handler);
848 mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP, video_codemap_handler);
850 mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
855 int MVE_rmStepMovie()
857 static int init_timer=0;
863 while (cont && !g_frameUpdated) // make a "step" be a frame, not a chunk...
864 cont = mve_play_next_chunk(mve);
867 if (micro_frame_delay && !init_timer) {
880 void MVE_rmEndMovie()
891 if (mve_audio_canplay) {
892 // only close audio if we opened it
893 mve_audio_canplay = 0;
895 for (i = 0; i < TOTAL_AUDIO_BUFFERS; i++)
896 if (mve_audio_buffers[i] != NULL)
897 d_free(mve_audio_buffers[i]);
898 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
899 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
900 mve_audio_curbuf_curpos=0;
905 mve_audio_compressed=0;
907 d_free(mve_audio_spec);
909 audiobuf_created = 0;
916 videobuf_created = 0;
917 video_initialized = 0;
919 mve_close_filehandle(mve);
924 void MVE_rmHoldMovie()