1 /* $Id: mveplay.c,v 1.4 2003-02-19 03:09:38 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 int mve_audio_enabled = 1;
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 (!mve_audio_enabled)
336 if (audiobuf_created)
339 audiobuf_created = 1;
341 flags = get_ushort(data + 2);
342 sample_rate = get_ushort(data + 4);
343 desired_buffer = get_int(data + 6);
345 stereo = (flags & MVE_AUDIO_FLAGS_STEREO) ? 1 : 0;
346 bitsize = (flags & MVE_AUDIO_FLAGS_16BIT) ? 1 : 0;
349 compressed = flags & MVE_AUDIO_FLAGS_COMPRESSED ? 1 : 0;
354 mve_audio_compressed = compressed;
357 #ifdef WORDS_BIGENDIAN
358 format = AUDIO_S16MSB;
360 format = AUDIO_S16LSB;
366 fprintf(stderr, "creating audio buffers:\n");
367 fprintf(stderr, "sample rate = %d, stereo = %d, bitsize = %d, compressed = %d\n",
368 sample_rate, stereo, bitsize ? 16 : 8, compressed);
370 mve_audio_spec = (SDL_AudioSpec *)d_malloc(sizeof(SDL_AudioSpec));
371 mve_audio_spec->freq = sample_rate;
372 mve_audio_spec->format = format;
373 mve_audio_spec->channels = (stereo) ? 2 : 1;
374 mve_audio_spec->samples = 4096;
375 mve_audio_spec->callback = mve_audio_callback;
376 mve_audio_spec->userdata = NULL;
377 if (SDL_OpenAudio(mve_audio_spec, NULL) >= 0)
379 fprintf(stderr, " success\n");
380 mve_audio_canplay = 1;
384 fprintf(stderr, " failure : %s\n", SDL_GetError());
385 mve_audio_canplay = 0;
388 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
389 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
395 static int play_audio_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
398 if (mve_audio_canplay && !mve_audio_playing && mve_audio_bufhead != mve_audio_buftail)
401 mve_audio_playing = 1;
407 static int audio_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
410 static const int selected_chan=1;
413 if (mve_audio_canplay)
415 if (mve_audio_playing)
418 chan = get_ushort(data + 2);
419 nsamp = get_ushort(data + 4);
420 if (chan & selected_chan)
422 /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
423 if (major == MVE_OPCODE_AUDIOFRAMEDATA) {
424 if (mve_audio_compressed) {
427 mve_audio_buflens[mve_audio_buftail] = nsamp;
428 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
429 mveaudio_uncompress(mve_audio_buffers[mve_audio_buftail], data, -1); /* XXX */
434 mve_audio_buflens[mve_audio_buftail] = nsamp;
435 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
436 memcpy(mve_audio_buffers[mve_audio_buftail], data, nsamp);
439 mve_audio_buflens[mve_audio_buftail] = nsamp;
440 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
442 memset(mve_audio_buffers[mve_audio_buftail], 0, nsamp); /* XXX */
445 if (++mve_audio_buftail == TOTAL_AUDIO_BUFFERS)
446 mve_audio_buftail = 0;
448 if (mve_audio_buftail == mve_audio_bufhead)
449 fprintf(stderr, "d'oh! buffer ring overrun (%d)\n", mve_audio_bufhead);
452 if (mve_audio_playing)
460 /*************************
462 *************************/
463 static int videobuf_created = 0;
464 static int video_initialized = 0;
465 int g_width, g_height;
466 void *g_vBuffers = NULL, *g_vBackBuf1, *g_vBackBuf2;
469 static SDL_Surface *g_screen;
471 static int g_destX, g_destY;
473 static int g_screenWidth, g_screenHeight;
474 static unsigned char g_palette[768];
475 static unsigned char *g_pCurMap=NULL;
476 static int g_nMapLength=0;
477 static int g_truecolor;
479 static int create_videobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
482 short count, truecolor;
484 if (videobuf_created)
487 videobuf_created = 1;
490 h = get_short(data+2);
493 count = get_short(data+4);
499 truecolor = get_short(data+6);
507 /* TODO: * 4 causes crashes on some files */
508 g_vBackBuf1 = g_vBuffers = d_malloc(g_width * g_height * 8);
510 g_vBackBuf2 = (unsigned short *)g_vBackBuf1 + (g_width * g_height);
512 g_vBackBuf2 = (unsigned char *)g_vBackBuf1 + (g_width * g_height);
515 memset(g_vBackBuf1, 0, g_width * g_height * 4);
518 fprintf(stderr, "DEBUG: w,h=%d,%d count=%d, tc=%d\n", w, h, count, truecolor);
521 g_truecolor = truecolor;
527 static int do_sdl_events()
531 while (SDL_PollEvent(&event)) {
537 if (event.key.keysym.sym == SDLK_ESCAPE)
543 case SDL_MOUSEBUTTONDOWN:
545 if (event.button.button == SDL_BUTTON_LEFT) {
546 printf("GRID: %d,%d (pix:%d,%d)\n",
547 event.button.x / 16, event.button.y / 8,
548 event.button.x, event.button.y);
560 static void ConvertAndDraw()
563 unsigned char *pal = g_palette;
564 unsigned char *pDest;
565 unsigned char *pixels = g_vBackBuf1;
566 SDL_Surface *screenSprite, *initSprite;
570 initSprite = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, g_truecolor?16:8, 0x7C00, 0x03E0, 0x001F, 0);
573 for(i = 0; i < 256; i++) {
574 initSprite->format->palette->colors[i].r = (*pal++) << 2;
575 initSprite->format->palette->colors[i].g = (*pal++) << 2;
576 initSprite->format->palette->colors[i].b = (*pal++) << 2;
577 initSprite->format->palette->colors[i].unused = 0;
581 pDest = initSprite->pixels;
583 if (0 /*g_truecolor*/) {
585 unsigned short *pSrcs, *pDests;
587 pSrcs = (unsigned short *)pixels;
588 pDests = (unsigned short *)pDest;
590 for (y=0; y<g_height; y++) {
591 for (x = 0; x < g_width; x++) {
592 pDests[x] = (1<<15)|*pSrcs;
595 pDests += g_screenWidth;
600 for (i=0; i<g_height; i++) {
601 memcpy(pDest, pixels, g_width * (g_truecolor?2:1));
602 pixels += g_width* (g_truecolor?2:1);
603 pDest += initSprite->pitch;
607 screenSprite = SDL_DisplayFormat(initSprite);
608 SDL_FreeSurface(initSprite);
610 if (g_screenWidth > screenSprite->w)
611 x = (g_screenWidth - screenSprite->w) >> 1;
614 if (g_screenHeight > screenSprite->h)
615 y = (g_screenHeight - screenSprite->h) >> 1;
620 renderArea.w = MIN(g_screenWidth - x, screenSprite->w);
621 renderArea.h = MIN(g_screenHeight - y, screenSprite->h);
622 SDL_BlitSurface(screenSprite, NULL, g_screen, &renderArea);
624 SDL_FreeSurface(screenSprite);
628 static int display_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
639 bitmap = gr_create_bitmap_raw(g_width, g_height, g_vBackBuf1);
641 if (g_destX == -1) // center it
642 g_destX = (g_screenWidth - g_width) >> 1;
643 if (g_destY == -1) // center it
644 g_destY = (g_screenHeight - g_height) >> 1;
646 gr_bitmap(g_destX, g_destY, bitmap);
648 gr_free_sub_bitmap(bitmap);
650 gr_palette_load(g_palette);
657 static int init_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
661 if (video_initialized)
664 video_initialized = 1;
666 width = get_short(data);
667 height = get_short(data+2);
669 g_screen = SDL_SetVideoMode(width, height, 16, g_sdlVidFlags);
671 g_screenWidth = width;
672 g_screenHeight = height;
673 memset(g_palette, 0, 765);
674 // 255 needs to default to white, for subtitles, etc
675 memset(g_palette + 765, 63, 3);
680 static int video_palette_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
683 start = get_short(data);
684 count = get_short(data+2);
685 memcpy(g_palette + 3*start, data+4, 3*count);
690 static int video_codemap_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
697 void decodeFrame16(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain);
698 void decodeFrame8(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain);
700 static int video_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
702 short nFrameHot, nFrameCold;
703 short nXoffset, nYoffset;
704 short nXsize, nYsize;
705 unsigned short nFlags;
708 nFrameHot = get_short(data);
709 nFrameCold = get_short(data+2);
710 nXoffset = get_short(data+4);
711 nYoffset = get_short(data+6);
712 nXsize = get_short(data+8);
713 nYsize = get_short(data+10);
714 nFlags = get_ushort(data+12);
718 temp = (unsigned char *)g_vBackBuf1;
719 g_vBackBuf1 = g_vBackBuf2;
723 /* convert the frame */
725 decodeFrame16((unsigned char *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
727 decodeFrame8(g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
733 static int end_chunk_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
741 void initializeMovie(MVESTREAM *mve)
745 for (i = 0; i < 32; i++)
746 mve_set_handler(mve, i, default_seg_handler);
748 memset(unhandled_chunks, 0, 32*256);
750 mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM, end_movie_handler);
751 mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK, end_chunk_handler);
752 mve_set_handler(mve, MVE_OPCODE_CREATETIMER, create_timer_handler);
753 mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS, create_audiobuf_handler);
754 mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO, play_audio_handler);
755 mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS, create_videobuf_handler);
757 mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO, display_video_handler);
758 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA, audio_data_handler);
759 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE, audio_data_handler);
760 mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE, init_video_handler);
762 mve_set_handler(mve, MVE_OPCODE_SETPALETTE, video_palette_handler);
763 mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, video_palette_handler);
764 mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP, video_codemap_handler);
766 mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
769 void playMovie(MVESTREAM *mve)
774 mve_audio_enabled = 1;
776 while (cont && playing)
778 cont = mve_play_next_chunk(mve);
779 if (micro_frame_delay && !init_timer)
787 if (g_loop && !cont) {
794 void shutdownMovie(MVESTREAM *mve)
803 for (i = 0; i < 32*256; i++) {
804 if (unhandled_chunks[i]) {
805 fprintf(stderr, "unhandled chunks of type %02x/%02x: %d\n", i>>8, i&0xFF, unhandled_chunks[i]);
812 static MVESTREAM *mve = NULL;
814 int MVE_rmPrepMovie(int filehandle, int x, int y, int track)
823 mve = mve_open_filehandle(filehandle);
831 for (i = 0; i < 32; i++)
832 mve_set_handler(mve, i, default_seg_handler);
834 mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM, end_movie_handler);
835 mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK, end_chunk_handler);
836 mve_set_handler(mve, MVE_OPCODE_CREATETIMER, create_timer_handler);
837 mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS, create_audiobuf_handler);
838 mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO, play_audio_handler);
839 mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS, create_videobuf_handler);
841 mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO, display_video_handler);
842 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA, audio_data_handler);
843 mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE, audio_data_handler);
844 mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE, init_video_handler);
846 mve_set_handler(mve, MVE_OPCODE_SETPALETTE, video_palette_handler);
847 mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, default_seg_handler);
849 mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP, video_codemap_handler);
851 mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
856 int MVE_rmStepMovie()
858 static int init_timer=0;
864 while (cont && !g_frameUpdated) // make a "step" be a frame, not a chunk...
865 cont = mve_play_next_chunk(mve);
868 if (micro_frame_delay && !init_timer) {
881 void MVE_rmEndMovie()
891 if (mve_audio_canplay) {
892 // only close audio if we opened it
894 mve_audio_canplay = 0;
896 for (i = 0; i < TOTAL_AUDIO_BUFFERS; i++)
897 if (mve_audio_buffers[i] != NULL)
898 d_free(mve_audio_buffers[i]);
899 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
900 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
901 mve_audio_curbuf_curpos=0;
906 mve_audio_compressed=0;
908 d_free(mve_audio_spec);
910 audiobuf_created = 0;
917 videobuf_created = 0;
918 video_initialized = 0;
920 mve_close_filehandle(mve);
925 void MVE_rmHoldMovie()
931 void MVE_sndInit(int x)
935 mve_audio_enabled = 0;
937 mve_audio_enabled = 1;