14 #include <SDL/SDL_thread.h>
20 #include "mve_audio.h"
27 #define MIN(a,b) ((a)<(b)?(a):(b))
32 static int g_spdFactorNum=0;
33 static int g_spdFactorDenom=10;
35 static short get_short(unsigned char *data)
38 value = data[0] | (data[1] << 8);
42 static int get_int(unsigned char *data)
45 value = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
49 static int default_seg_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
51 Warning("mveplay: unknown chunk type %02x/%02x\n", major, minor);
55 /*************************
57 *************************/
58 static int end_movie_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
63 /*************************
65 *************************/
70 static fix fix_frame_delay = F0_0;
71 static int timer_started = 0;
72 static fix timer_expire = F0_0;
74 static int create_timer_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
77 int micro_frame_delay = get_int(data) * (int)get_short(data+4);
79 if (g_spdFactorNum != 0)
81 temp = micro_frame_delay;
82 temp *= g_spdFactorNum;
83 temp /= g_spdFactorDenom;
84 micro_frame_delay = (int)temp;
86 fix_frame_delay = approx_usec_to_fsec(micro_frame_delay);
91 static void timer_stop(void)
97 static void timer_start(void)
99 timer_expire = timer_get_fixed_seconds();
101 timer_expire += fix_frame_delay;
106 static void do_timer_wait(void)
113 tv = timer_get_fixed_seconds();
115 if (tv > timer_expire)
118 ts = timer_expire - tv;
123 timer_expire += fix_frame_delay;
126 /*************************
128 *************************/
129 static void mve_audio_callback(void *userdata, Uint8 *stream, int len);
130 static short *mve_audio_buffers[64];
131 static int mve_audio_buflens[64];
132 static int mve_audio_curbuf_curpos=0;
133 static int mve_audio_bufhead=0;
134 static int mve_audio_buftail=0;
135 static int mve_audio_playing=0;
136 static int mve_audio_canplay=0;
137 static int mve_audio_compressed=0;
138 static SDL_AudioSpec *mve_audio_spec=NULL;
139 static SDL_mutex *mve_audio_mutex = NULL;
141 static void mve_audio_callback(void *userdata, Uint8 *stream, int len)
145 if (mve_audio_bufhead == mve_audio_buftail)
149 fprintf(stderr, "+ <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
152 SDL_mutexP(mve_audio_mutex);
154 while (mve_audio_bufhead != mve_audio_buftail /* while we have more buffers */
155 && len > (mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos)) /* and while we need more data */
157 length = mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos;
158 __builtin_memcpy(stream, /* cur output position */
159 mve_audio_buffers[mve_audio_bufhead]+mve_audio_curbuf_curpos, /* cur input position */
160 length); /* cur input length */
163 stream += length; /* advance output */
164 len -= length; /* decrement avail ospace */
165 d_free(mve_audio_buffers[mve_audio_bufhead]); /* free the buffer */
166 mve_audio_buffers[mve_audio_bufhead]=NULL; /* free the buffer */
167 mve_audio_buflens[mve_audio_bufhead]=0; /* free the buffer */
169 if (++mve_audio_bufhead == 64) /* next buffer */
170 mve_audio_bufhead = 0;
171 mve_audio_curbuf_curpos = 0;
175 fprintf(stderr, "= <%d (%d), %d, %d>: %d\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len, total);
179 if (len != 0 /* ospace remaining */
180 && mve_audio_bufhead != mve_audio_buftail) /* buffers remaining */
182 __builtin_memcpy(stream, /* dest */
183 mve_audio_buffers[mve_audio_bufhead] + mve_audio_curbuf_curpos, /* src */
186 mve_audio_curbuf_curpos += len; /* advance input */
187 stream += len; /* advance output (unnecessary) */
188 len -= len; /* advance output (unnecessary) */
190 if (mve_audio_curbuf_curpos >= mve_audio_buflens[mve_audio_bufhead]) /* if this ends the current chunk */
192 d_free(mve_audio_buffers[mve_audio_bufhead]); /* free buffer */
193 mve_audio_buffers[mve_audio_bufhead]=NULL;
194 mve_audio_buflens[mve_audio_bufhead]=0;
196 if (++mve_audio_bufhead == 64) /* next buffer */
197 mve_audio_bufhead = 0;
198 mve_audio_curbuf_curpos = 0;
203 fprintf(stderr, "- <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
205 SDL_mutexV(mve_audio_mutex);
208 static int create_audiobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
215 fprintf(stderr, "creating audio buffers\n");
218 mve_audio_mutex = SDL_CreateMutex();
220 flags = get_short(data + 2);
221 sample_rate = get_short(data + 4);
222 desired_buffer = get_int(data + 6);
225 fprintf(stderr, "stereo=%d 16bit=%d compressed=%d sample_rate=%d desired_buffer=%d\n",
226 flags & 1, (flags >> 1) & 1, (flags >> 2) & 1, sample_rate, desired_buffer);
229 mve_audio_compressed = flags & MVE_AUDIO_FLAGS_COMPRESSED;
230 mve_audio_spec = (SDL_AudioSpec *)d_malloc(sizeof(SDL_AudioSpec));
231 mve_audio_spec->freq = sample_rate;
232 #ifdef WORDS_BIGENDIAN
233 mve_audio_spec->format = (flags & MVE_AUDIO_FLAGS_16BIT)?AUDIO_S16MSB:AUDIO_U8;
235 mve_audio_spec->format = (flags & MVE_AUDIO_FLAGS_16BIT)?AUDIO_S16LSB:AUDIO_U8;
237 mve_audio_spec->channels = (flags & MVE_AUDIO_FLAGS_STEREO)?2:1;
238 mve_audio_spec->samples = 32768;
239 mve_audio_spec->callback = mve_audio_callback;
240 mve_audio_spec->userdata = NULL;
241 if (SDL_OpenAudio(mve_audio_spec, NULL) >= 0)
244 fprintf(stderr, " success\n");
246 mve_audio_canplay = 1;
251 fprintf(stderr, " failure : %s\n", SDL_GetError());
253 Warning("mveplay: failed to create audio buffers\n");
254 mve_audio_canplay = 0;
257 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
258 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
263 static int play_audio_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
265 if (mve_audio_canplay && !mve_audio_playing && mve_audio_bufhead != mve_audio_buftail)
268 mve_audio_playing = 1;
274 static int audio_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
276 static const int selected_chan=1;
279 if (mve_audio_canplay)
281 if (mve_audio_playing)
284 chan = get_short(data + 2);
285 nsamp = get_short(data + 4);
286 if (chan & selected_chan)
288 SDL_mutexP(mve_audio_mutex);
289 mve_audio_buflens[mve_audio_buftail] = nsamp;
290 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp+4);
292 if (mve_audio_compressed)
293 mveaudio_uncompress(mve_audio_buffers[mve_audio_buftail], data, -1); /* XXX */
295 memcpy(mve_audio_buffers[mve_audio_buftail], data + 6, nsamp);
297 memset(mve_audio_buffers[mve_audio_buftail], 0, nsamp); /* XXX */
299 if (++mve_audio_buftail == 64)
300 mve_audio_buftail = 0;
302 if (mve_audio_buftail == mve_audio_bufhead)
303 Warning("mveplay: d'oh! buffer ring overrun (%d)\n", mve_audio_bufhead);
304 SDL_mutexV(mve_audio_mutex);
307 if (mve_audio_playing)
314 /*************************
316 *************************/
317 static grs_bitmap *g_screen;
318 static int g_screenWidth, g_screenHeight;
319 static int g_width, g_height;
320 static unsigned char g_palette[768];
321 static unsigned char *g_vBackBuf1, *g_vBackBuf2;
322 static unsigned char *g_pCurMap=NULL;
323 static int g_nMapLength=0;
325 static int create_videobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
329 h = get_short(data+2);
333 fprintf(stderr, "g_width, g_height: %d, %d\n", g_width, g_height);
335 Assert((g_width == g_screen->bm_w) && (g_height == g_screen->bm_h));
336 g_vBackBuf1 = d_malloc(g_width * g_height);
337 g_vBackBuf2 = d_malloc(g_width * g_height);
338 memset(g_vBackBuf1, 0, g_width * g_height);
339 memset(g_vBackBuf2, 0, g_width * g_height);
343 static int display_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
345 gr_palette_load(g_palette);
347 memcpy(g_screen->bm_data, g_vBackBuf1, g_width * g_height);
352 static int init_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
355 width = get_short(data);
356 height = get_short(data+2);
357 g_screenWidth = width;
358 g_screenHeight = height;
359 memset(g_palette, 0, 765);
360 memset(g_palette + 765, 63, 3); // 255 should be white
365 static int video_palette_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
368 start = get_short(data);
369 count = get_short(data+2);
370 memcpy(g_palette + 3*start, data+4, 3*count);
374 static int video_codemap_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
381 static void decodeFrame(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain);
383 static int video_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
385 short nFrameHot, nFrameCold;
386 short nXoffset, nYoffset;
387 short nXsize, nYsize;
391 nFrameHot = get_short(data);
392 nFrameCold = get_short(data+2);
393 nXoffset = get_short(data+4);
394 nYoffset = get_short(data+6);
395 nXsize = get_short(data+8);
396 nYsize = get_short(data+10);
397 nFlags = get_short(data+12);
402 g_vBackBuf1 = g_vBackBuf2;
406 /* convert the frame */
407 decodeFrame(g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
412 static int end_chunk_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
418 /************************************************************
419 * public mveplay functions
420 ************************************************************/
422 void mveplay_initializeMovie(MVESTREAM *mve, grs_bitmap *mve_bitmap)
424 g_screen = mve_bitmap;
426 mve_set_handler(mve, 0x00, end_movie_handler);
427 mve_set_handler(mve, 0x01, end_chunk_handler);
428 mve_set_handler(mve, 0x02, create_timer_handler);
429 mve_set_handler(mve, 0x03, create_audiobuf_handler);
430 mve_set_handler(mve, 0x04, play_audio_handler);
431 mve_set_handler(mve, 0x05, create_videobuf_handler);
432 mve_set_handler(mve, 0x06, default_seg_handler);
433 mve_set_handler(mve, 0x07, display_video_handler);
434 mve_set_handler(mve, 0x08, audio_data_handler);
435 mve_set_handler(mve, 0x09, audio_data_handler);
436 mve_set_handler(mve, 0x0a, init_video_handler);
437 mve_set_handler(mve, 0x0b, default_seg_handler);
438 mve_set_handler(mve, 0x0c, video_palette_handler);
439 mve_set_handler(mve, 0x0d, default_seg_handler);
440 mve_set_handler(mve, 0x0e, default_seg_handler);
441 mve_set_handler(mve, 0x0f, video_codemap_handler);
442 mve_set_handler(mve, 0x10, default_seg_handler);
443 mve_set_handler(mve, 0x11, video_data_handler);
444 mve_set_handler(mve, 0x12, default_seg_handler);
445 //mve_set_handler(mve, 0x13, default_seg_handler);
446 mve_set_handler(mve, 0x14, default_seg_handler);
447 //mve_set_handler(mve, 0x15, default_seg_handler);
450 int mveplay_stepMovie(MVESTREAM *mve)
452 static int init_timer=0;
458 cont = mve_play_next_chunk(mve);
459 if (fix_frame_delay && !init_timer) {
469 void mveplay_restartTimer(MVESTREAM *mve)
474 void mveplay_shutdownMovie(MVESTREAM *mve)
480 for (i = 0; i < 64; i++)
481 if (mve_audio_buffers[i] != NULL)
482 d_free(mve_audio_buffers[i]);
483 memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
484 memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
485 mve_audio_curbuf_curpos=0;
490 mve_audio_compressed=0;
491 mve_audio_mutex = NULL;
493 d_free(mve_audio_spec);
505 /************************************************************
506 * internal decoding functions
507 ************************************************************/
509 static void dispatchDecoder(unsigned char **pFrame, unsigned char codeType, unsigned char **pData, int *pDataRemain, int *curXb, int *curYb);
511 static void decodeFrame(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain)
520 for (i=0; i<xb/2; i++)
522 dispatchDecoder(&pFrame, (*pMap) & 0xf, &pData, &dataRemain, &i, &j);
523 if (pFrame < g_vBackBuf1)
524 Warning("mveplay: danger! pointing out of bounds below after dispatch decoder: %d, %d (1) [%x]\n", i, j, (*pMap) & 0xf);
525 else if (pFrame >= g_vBackBuf1 + g_width*g_height)
526 Warning("mveplay: danger! pointing out of bounds above after dispatch decoder: %d, %d (1) [%x]\n", i, j, (*pMap) & 0xf);
527 dispatchDecoder(&pFrame, (*pMap) >> 4, &pData, &dataRemain, &i, &j);
528 if (pFrame < g_vBackBuf1)
529 Warning("mveplay: danger! pointing out of bounds below after dispatch decoder: %d, %d (2) [%x]\n", i, j, (*pMap) >> 4);
530 else if (pFrame >= g_vBackBuf1 + g_width*g_height)
531 Warning("mveplay: danger! pointing out of bounds above after dispatch decoder: %d, %d (2) [%x]\n", i, j, (*pMap) >> 4);
541 static void relClose(int i, int *x, int *y)
552 static void relFar(int i, int sign, int *x, int *y)
556 *x = sign * (8 + (i % 7));
561 *x = sign * (-14 + (i - 56) % 29);
562 *y = sign * (8 + (i - 56) / 29);
566 static void copyFrame(unsigned char *pDest, unsigned char *pSrc)
572 memcpy(pDest, pSrc, 8);
578 static void patternRow4Pixels(unsigned char *pFrame,
579 unsigned char pat0, unsigned char pat1,
582 unsigned short mask=0x0003;
583 unsigned short shift=0;
584 unsigned short pattern = (pat1 << 8) | pat0;
588 *pFrame++ = p[(mask & pattern) >> shift];
594 static void patternRow4Pixels2(unsigned char *pFrame,
598 unsigned char mask=0x03;
599 unsigned char shift=0;
605 pel = p[(mask & pat0) >> shift];
608 pFrame[g_width + 0] = pel;
609 pFrame[g_width + 2] = pel;
617 static void patternRow4Pixels2x1(unsigned char *pFrame, unsigned char pat, unsigned char *p)
619 unsigned char mask=0x03;
620 unsigned char shift=0;
625 pel = p[(mask & pat) >> shift];
634 static void patternQuadrant4Pixels(unsigned char *pFrame, unsigned char pat0, unsigned char pat1, unsigned char pat2, unsigned char pat3, unsigned char *p)
636 unsigned long mask = 0x00000003UL;
639 unsigned long pat = (pat3 << 24) | (pat2 << 16) | (pat1 << 8) | pat0;
643 pFrame[i&3] = p[(pat & mask) >> shift];
654 static void patternRow2Pixels(unsigned char *pFrame, unsigned char pat, unsigned char *p)
656 unsigned char mask=0x01;
660 *pFrame++ = p[(mask & pat) ? 1 : 0];
665 static void patternRow2Pixels2(unsigned char *pFrame, unsigned char pat, unsigned char *p)
668 unsigned char mask=0x1;
673 pel = p[(mask & pat) ? 1 : 0];
676 pFrame[g_width + 0] = pel;
677 pFrame[g_width + 2] = pel;
684 static void patternQuadrant2Pixels(unsigned char *pFrame, unsigned char pat0, unsigned char pat1, unsigned char *p)
686 unsigned short mask = 0x0001;
688 unsigned short pat = (pat1 << 8) | pat0;
692 pFrame[i&3] = p[(pat & mask) ? 1 : 0];
701 static void dispatchDecoder(unsigned char **pFrame, unsigned char codeType, unsigned char **pData, int *pDataRemain, int *curXb, int *curYb)
704 unsigned char pat[16];
711 copyFrame(*pFrame, *pFrame + (g_vBackBuf2 - g_vBackBuf1));
717 relFar(*(*pData)++, 1, &x, &y);
718 copyFrame(*pFrame, *pFrame + x + y*g_width);
724 relFar(*(*pData)++, -1, &x, &y);
725 copyFrame(*pFrame, *pFrame + x + y*g_width);
731 relClose(*(*pData)++, &x, &y);
732 copyFrame(*pFrame, *pFrame + (g_vBackBuf2 - g_vBackBuf1) + x + y*g_width);
738 x = (char)*(*pData)++;
739 y = (char)*(*pData)++;
740 copyFrame(*pFrame, *pFrame + (g_vBackBuf2 - g_vBackBuf1) + x + y*g_width);
749 if (++*curXb == (g_width >> 3))
751 *pFrame += 7*g_width;
753 if (++*curYb == (g_height >> 3))
766 patternRow2Pixels(*pFrame, *(*pData)++, p);
774 patternRow2Pixels2(*pFrame, *(*pData) & 0xf, p);
775 *pFrame += 2*g_width;
776 patternRow2Pixels2(*pFrame, *(*pData)++ >> 4, p);
777 *pFrame += 2*g_width;
780 *pFrame -= (8*g_width - 8);
784 if ( (*pData)[0] <= (*pData)[1])
790 pat[0] = *(*pData)++;
791 pat[1] = *(*pData)++;
792 patternQuadrant2Pixels(*pFrame, pat[0], pat[1], p);
795 *pFrame -= (4*g_width - 4);
797 *pFrame += 4*g_width;
800 else if ( (*pData)[6] <= (*pData)[7])
809 pat[0] = *(*pData)++;
810 pat[1] = *(*pData)++;
811 patternQuadrant2Pixels(*pFrame, pat[0], pat[1], p);
814 *pFrame -= (4*g_width - 4);
816 *pFrame += 4*g_width;
828 patternRow2Pixels(*pFrame, *(*pData)++, p);
831 *pFrame -= (8*g_width - 8);
836 if ( (*pData)[0] <= (*pData)[1])
838 if ( (*pData)[2] <= (*pData)[3])
847 pat[0] = *(*pData)++;
848 pat[1] = *(*pData)++;
849 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
853 *pFrame -= (8*g_width - 8);
862 patternRow4Pixels2(*pFrame, *(*pData)++, p);
863 *pFrame += 2*g_width;
864 patternRow4Pixels2(*pFrame, *(*pData)++, p);
865 *pFrame += 2*g_width;
866 patternRow4Pixels2(*pFrame, *(*pData)++, p);
867 *pFrame += 2*g_width;
868 patternRow4Pixels2(*pFrame, *(*pData)++, p);
869 *pFrame -= (6*g_width - 8);
874 if ( (*pData)[2] <= (*pData)[3])
883 pat[0] = *(*pData)++;
884 patternRow4Pixels2x1(*pFrame, pat[0], p);
888 *pFrame -= (8*g_width - 8);
899 pat[0] = *(*pData)++;
900 pat[1] = *(*pData)++;
901 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
903 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
907 *pFrame -= (8*g_width - 8);
913 if ( (*pData)[0] <= (*pData)[1])
921 pat[0] = *(*pData)++;
922 pat[1] = *(*pData)++;
923 pat[2] = *(*pData)++;
924 pat[3] = *(*pData)++;
926 patternQuadrant4Pixels(*pFrame, pat[0], pat[1], pat[2], pat[3], p);
929 *pFrame -= (4*g_width - 4);
931 *pFrame += 4*g_width;
936 if ( (*pData)[12] <= (*pData)[13])
948 pat[0] = *(*pData)++;
949 pat[1] = *(*pData)++;
950 pat[2] = *(*pData)++;
951 pat[3] = *(*pData)++;
953 patternQuadrant4Pixels(*pFrame, pat[0], pat[1], pat[2], pat[3], p);
956 *pFrame -= (4*g_width - 4);
958 *pFrame += 4*g_width;
973 pat[0] = *(*pData)++;
974 pat[1] = *(*pData)++;
975 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
979 *pFrame -= (8*g_width - 8);
987 memcpy(*pFrame, *pData, 8);
992 *pFrame -= (8*g_width - 8);
1002 (*pFrame)[j+2*k] = (*pData)[k];
1003 (*pFrame)[g_width+j+2*k] = (*pData)[k];
1010 *pFrame -= (8*g_width - 8);
1020 (*pFrame)[k*g_width+j] = (*pData)[0];
1021 (*pFrame)[k*g_width+j+4] = (*pData)[1];
1024 *pFrame += 4*g_width;
1028 *pFrame -= (8*g_width - 8);
1034 memset(*pFrame, **pData, 8);
1039 *pFrame -= (8*g_width - 8);
1047 (*pFrame)[j] = (*pData)[(i+j)&1];
1053 *pFrame -= (8*g_width - 8);