]> icculus.org git repositories - btb/d2x.git/blob - libmve/mveplay.c
make MVE_rmStepMovie play a full frame instead of a chunk
[btb/d2x.git] / libmve / mveplay.c
1 /* $Id: mveplay.c,v 1.2 2003-02-18 23:28:47 btb Exp $ */
2 #ifdef HAVE_CONFIG_H
3 #include <conf.h>
4 #endif
5
6 #ifndef __MSDOS__
7 #define AUDIO
8 #endif
9 //#define DEBUG
10
11 #include <string.h>
12 #include <errno.h>
13 #include <time.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19
20 #if defined(STANDALONE) || defined(AUDIO)
21 #include <SDL.h>
22 #endif
23
24 #include "mvelib.h"
25 #include "mve_audio.h"
26
27 #ifndef STANDALONE
28 #include "libmve.h"
29 #include "error.h"
30 #include "u_mem.h"
31 #include "gr.h"
32 #include "palette.h"
33 #endif
34
35 #ifdef STANDALONE
36 #define d_malloc(size)      malloc(size)
37 #define d_free(ptr)         free(ptr)
38 #endif
39
40 #ifndef MIN
41 #define MIN(a,b) ((a)<(b)?(a):(b))
42 #endif
43
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
50
51 #define MVE_OPCODE_DISPLAYVIDEO         0x07
52 #define MVE_OPCODE_AUDIOFRAMEDATA       0x08
53 #define MVE_OPCODE_AUDIOFRAMESILENCE    0x09
54 #define MVE_OPCODE_INITVIDEOMODE        0x0A
55
56 #define MVE_OPCODE_SETPALETTE           0x0C
57 #define MVE_OPCODE_SETPALETTECOMPRESSED 0x0D
58
59 #define MVE_OPCODE_SETDECODINGMAP       0x0F
60
61 #define MVE_OPCODE_VIDEODATA            0x11
62
63 #define MVE_AUDIO_FLAGS_STEREO     1
64 #define MVE_AUDIO_FLAGS_16BIT      2
65 #define MVE_AUDIO_FLAGS_COMPRESSED 4
66
67 int g_spdFactorNum=0;
68 static int g_spdFactorDenom=10;
69 static int g_frameUpdated = 0;
70
71 #ifdef STANDALONE
72 static int playing = 1;
73 int g_sdlVidFlags = SDL_ANYFORMAT | SDL_DOUBLEBUF;
74 int g_loop = 0;
75
76 void initializeMovie(MVESTREAM *mve);
77 void playMovie(MVESTREAM *mve);
78 void shutdownMovie(MVESTREAM *mve);
79 #endif
80
81 static short get_short(unsigned char *data)
82 {
83         short value;
84         value = data[0] | (data[1] << 8);
85         return value;
86 }
87
88 static unsigned short get_ushort(unsigned char *data)
89 {
90         unsigned short value;
91         value = data[0] | (data[1] << 8);
92         return value;
93 }
94
95 static int get_int(unsigned char *data)
96 {
97         int value;
98         value = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
99         return value;
100 }
101
102 static unsigned int unhandled_chunks[32*256];
103
104 static int default_seg_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
105 {
106         unhandled_chunks[major<<8|minor]++;
107         //fprintf(stderr, "unknown chunk type %02x/%02x\n", major, minor);
108         return 1;
109 }
110
111
112 /*************************
113  * general handlers
114  *************************/
115 static int end_movie_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
116 {
117         return 0;
118 }
119
120 /*************************
121  * timer handlers
122  *************************/
123
124 /*
125  * timer variables
126  */
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};
131
132 #ifdef __WIN32
133 #include <sys/timeb.h>
134
135 struct timespec
136 {
137         long int tv_sec;            /* Seconds.  */
138         long int tv_nsec;           /* Nanoseconds.  */
139 };
140
141 int gettimeofday(struct timeval *tv, void *tz)
142 {
143         static int counter = 0;
144         struct timeb tm;
145
146         counter++; /* to avoid collisions */
147         ftime(&tm);
148         tv->tv_sec  = tm.time;
149         tv->tv_usec = (tm.millitm * 1000) + counter;
150
151         return 0;
152 }
153
154 int nanosleep(struct timespec *ts, void *rem)
155 {
156         sleep(ts->tv_sec * 1000 + ts->tv_nsec / 1000000);
157
158         return 0;
159 }
160 #endif
161
162 static int create_timer_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
163 {
164         __extension__ long long temp;
165
166         if (timer_created)
167                 return 1;
168         else
169                 timer_created = 1;
170
171         micro_frame_delay = get_int(data) * (int)get_short(data+4);
172         if (g_spdFactorNum != 0)
173         {
174                 temp = micro_frame_delay;
175                 temp *= g_spdFactorNum;
176                 temp /= g_spdFactorDenom;
177                 micro_frame_delay = (int)temp;
178         }
179
180         return 1;
181 }
182
183 static void timer_stop(void)
184 {
185         timer_expire.tv_sec = 0;
186         timer_expire.tv_usec = 0;
187         timer_started = 0;
188 }
189
190 static void timer_start(void)
191 {
192         int nsec=0;
193         gettimeofday(&timer_expire, NULL);
194         timer_expire.tv_usec += micro_frame_delay;
195         if (timer_expire.tv_usec > 1000000)
196         {
197                 nsec = timer_expire.tv_usec / 1000000;
198                 timer_expire.tv_sec += nsec;
199                 timer_expire.tv_usec -= nsec*1000000;
200         }
201         timer_started=1;
202 }
203
204 static void do_timer_wait(void)
205 {
206         int nsec=0;
207         struct timespec ts;
208         struct timeval tv;
209         if (! timer_started)
210                 return;
211
212         gettimeofday(&tv, NULL);
213         if (tv.tv_sec > timer_expire.tv_sec)
214                 goto end;
215         else if (tv.tv_sec == timer_expire.tv_sec  &&  tv.tv_usec >= timer_expire.tv_usec)
216                 goto end;
217
218         ts.tv_sec = timer_expire.tv_sec - tv.tv_sec;
219         ts.tv_nsec = 1000 * (timer_expire.tv_usec - tv.tv_usec);
220         if (ts.tv_nsec < 0)
221         {
222                 ts.tv_nsec += 1000000000UL;
223                 --ts.tv_sec;
224         }
225 #ifdef __CYGWIN__
226         usleep(ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
227 #else
228         if (nanosleep(&ts, NULL) == -1  &&  errno == EINTR)
229                 exit(1);
230 #endif
231
232  end:
233         timer_expire.tv_usec += micro_frame_delay;
234         if (timer_expire.tv_usec > 1000000)
235         {
236                 nsec = timer_expire.tv_usec / 1000000;
237                 timer_expire.tv_sec += nsec;
238                 timer_expire.tv_usec -= nsec*1000000;
239         }
240 }
241
242 /*************************
243  * audio handlers
244  *************************/
245 #ifdef AUDIO
246 #define TOTAL_AUDIO_BUFFERS 64
247
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;
259
260 static void mve_audio_callback(void *userdata, unsigned char *stream, int len)
261 {
262         int total=0;
263         int length;
264         if (mve_audio_bufhead == mve_audio_buftail)
265                 return /* 0 */;
266
267         //fprintf(stderr, "+ <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
268
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 */
271         {
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    */
276
277                 total += 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 */
283
284                 if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS)                                 /* next buffer */
285                         mve_audio_bufhead = 0;
286                 mve_audio_curbuf_curpos = 0;
287         }
288
289         //fprintf(stderr, "= <%d (%d), %d, %d>: %d\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len, total);
290         /*    return total; */
291
292         if (len != 0                                                                        /* ospace remaining  */
293                 &&  mve_audio_bufhead != mve_audio_buftail)                                     /* buffers remaining */
294         {
295                 memcpy(stream,                                                                  /* dest */
296                            ((unsigned char *)mve_audio_buffers[mve_audio_bufhead]) + mve_audio_curbuf_curpos,         /* src */
297                            len);                                                                    /* length */
298
299                 mve_audio_curbuf_curpos += len;                                                 /* advance input */
300                 stream += len;                                                                  /* advance output (unnecessary) */
301                 len -= len;                                                                     /* advance output (unnecessary) */
302
303                 if (mve_audio_curbuf_curpos >= mve_audio_buflens[mve_audio_bufhead])            /* if this ends the current chunk */
304                 {
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;
308
309                         if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS)                             /* next buffer */
310                                 mve_audio_bufhead = 0;
311                         mve_audio_curbuf_curpos = 0;
312                 }
313         }
314
315         //fprintf(stderr, "- <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
316 }
317 #endif
318
319 static int create_audiobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
320 {
321 #ifdef AUDIO
322         int flags;
323         int sample_rate;
324         int desired_buffer;
325
326         int stereo;
327         int bitsize;
328         int compressed;
329
330         int format;
331
332         if (audiobuf_created)
333                 return 1;
334         else
335                 audiobuf_created = 1;
336
337 #ifndef STANDALONE
338         if (FindArg("-nosound"))
339                 return 1;
340 #endif
341
342         flags = get_ushort(data + 2);
343         sample_rate = get_ushort(data + 4);
344         desired_buffer = get_int(data + 6);
345
346         stereo = (flags & MVE_AUDIO_FLAGS_STEREO) ? 1 : 0;
347         bitsize = (flags & MVE_AUDIO_FLAGS_16BIT) ? 1 : 0;
348
349         if (minor > 0) {
350                 compressed = flags & MVE_AUDIO_FLAGS_COMPRESSED ? 1 : 0;
351         } else {
352                 compressed = 0;
353         }
354
355         mve_audio_compressed = compressed;
356
357         if (bitsize == 1) {
358                 format = AUDIO_S16LSB;
359         } else {
360                 format = AUDIO_U8;
361         }
362
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);
366
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)
375         {
376                 fprintf(stderr, "   success\n");
377                 mve_audio_canplay = 1;
378         }
379         else
380         {
381                 fprintf(stderr, "   failure : %s\n", SDL_GetError());
382                 mve_audio_canplay = 0;
383         }
384
385         memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
386         memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
387 #endif
388
389         return 1;
390 }
391
392 static int play_audio_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
393 {
394 #ifdef AUDIO
395         if (mve_audio_canplay  &&  !mve_audio_playing  &&  mve_audio_bufhead != mve_audio_buftail)
396         {
397                 SDL_PauseAudio(0);
398                 mve_audio_playing = 1;
399         }
400 #endif
401         return 1;
402 }
403
404 static int audio_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
405 {
406 #ifdef AUDIO
407         static const int selected_chan=1;
408         int chan;
409         int nsamp;
410         if (mve_audio_canplay)
411         {
412                 if (mve_audio_playing)
413                         SDL_LockAudio();
414
415                 chan = get_ushort(data + 2);
416                 nsamp = get_ushort(data + 4);
417                 if (chan & selected_chan)
418                 {
419                         /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
420                         if (major == MVE_OPCODE_AUDIOFRAMEDATA) {
421                                 if (mve_audio_compressed) {
422                                         nsamp += 4;
423
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 */
427                                 } else {
428                                         nsamp -= 8;
429                                         data += 8;
430
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);
434                                 }
435                         } else {
436                                 mve_audio_buflens[mve_audio_buftail] = nsamp;
437                                 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
438
439                                 memset(mve_audio_buffers[mve_audio_buftail], 0, nsamp); /* XXX */
440                         }
441
442                         if (++mve_audio_buftail == TOTAL_AUDIO_BUFFERS)
443                                 mve_audio_buftail = 0;
444
445                         if (mve_audio_buftail == mve_audio_bufhead)
446                                 fprintf(stderr, "d'oh!  buffer ring overrun (%d)\n", mve_audio_bufhead);
447                 }
448
449                 if (mve_audio_playing)
450                         SDL_UnlockAudio();
451         }
452 #endif
453
454         return 1;
455 }
456
457 /*************************
458  * video handlers
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;
464
465 #ifdef STANDALONE
466 static SDL_Surface *g_screen;
467 #else
468 static int g_destX, g_destY;
469 #endif
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;
475
476 static int create_videobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
477 {
478         short w, h;
479         short count, truecolor;
480
481         if (videobuf_created)
482                 return 1;
483         else
484                 videobuf_created = 1;
485
486         w = get_short(data);
487         h = get_short(data+2);
488
489         if (minor > 0) {
490                 count = get_short(data+4);
491         } else {
492                 count = 1;
493         }
494
495         if (minor > 1) {
496                 truecolor = get_short(data+6);
497         } else {
498                 truecolor = 0;
499         }
500
501         g_width = w << 3;
502         g_height = h << 3;
503
504         /* TODO: * 4 causes crashes on some files */
505         g_vBackBuf1 = g_vBuffers = d_malloc(g_width * g_height * 8);
506         if (truecolor) {
507                 g_vBackBuf2 = (unsigned short *)g_vBackBuf1 + (g_width * g_height);
508         } else {
509                 g_vBackBuf2 = (unsigned char *)g_vBackBuf1 + (g_width * g_height);
510         }
511
512         memset(g_vBackBuf1, 0, g_width * g_height * 4);
513
514 #ifdef DEBUG
515         fprintf(stderr, "DEBUG: w,h=%d,%d count=%d, tc=%d\n", w, h, count, truecolor);
516 #endif
517
518         g_truecolor = truecolor;
519
520         return 1;
521 }
522
523 #ifdef STANDALONE
524 static int do_sdl_events()
525 {
526         SDL_Event event;
527         int retr = 0;
528         while (SDL_PollEvent(&event)) {
529                 switch(event.type) {
530                 case SDL_QUIT:
531                         playing=0;
532                         break;
533                 case SDL_KEYDOWN:
534                         if (event.key.keysym.sym == SDLK_ESCAPE)
535                                 playing=0;
536                         break;
537                 case SDL_KEYUP:
538                         retr = 1;
539                         break;
540                 case SDL_MOUSEBUTTONDOWN:
541                         /*
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);
546                           }
547                         */
548                         break;
549                 default:
550                         break;
551                 }
552         }
553
554         return retr;
555 }
556
557 static void ConvertAndDraw()
558 {
559         int i;
560         unsigned char *pal = g_palette;
561         unsigned char *pDest;
562         unsigned char *pixels = g_vBackBuf1;
563         SDL_Surface *screenSprite, *initSprite;
564         SDL_Rect renderArea;
565         int x, y;
566
567         initSprite = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, g_truecolor?16:8, 0x7C00, 0x03E0, 0x001F, 0);
568
569         if (!g_truecolor) {
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;
575                 }
576         }
577
578         pDest = initSprite->pixels;
579
580         if (0 /*g_truecolor*/) {
581
582                 unsigned short *pSrcs, *pDests;
583
584                 pSrcs = (unsigned short *)pixels;
585                 pDests = (unsigned short *)pDest;
586
587                 for (y=0; y<g_height; y++) {
588                         for (x = 0; x < g_width; x++) {
589                                 pDests[x] = (1<<15)|*pSrcs;
590                                 pSrcs++;
591                         }
592                         pDests += g_screenWidth;
593                 }
594
595         } else {
596
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;
601                 }
602         }
603
604         screenSprite = SDL_DisplayFormat(initSprite);
605         SDL_FreeSurface(initSprite);
606
607         if (g_screenWidth > screenSprite->w)
608                 x = (g_screenWidth - screenSprite->w) >> 1;
609         else
610                 x=0;
611         if (g_screenHeight > screenSprite->h)
612                 y = (g_screenHeight - screenSprite->h) >> 1;
613         else
614                 y=0;
615         renderArea.x = x;
616         renderArea.y = y;
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);
620
621         SDL_FreeSurface(screenSprite);
622 }
623 #endif
624
625 static int display_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
626 {
627 #ifdef STANDALONE
628         ConvertAndDraw();
629
630         SDL_Flip(g_screen);
631
632         do_sdl_events();
633 #else
634         grs_bitmap *bitmap;
635
636         bitmap = gr_create_bitmap_raw(g_width, g_height, g_vBackBuf1);
637
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;
642
643         gr_bitmap(g_destX, g_destY, bitmap);
644
645         gr_free_sub_bitmap(bitmap);
646
647         gr_palette_load(g_palette);
648 #endif
649         g_frameUpdated = 1;
650
651         return 1;
652 }
653
654 static int init_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
655 {
656         short width, height;
657
658         if (video_initialized)
659                 return 1;
660         else
661                 video_initialized = 1;
662
663         width = get_short(data);
664         height = get_short(data+2);
665 #ifdef STANDALONE
666         g_screen = SDL_SetVideoMode(width, height, 16, g_sdlVidFlags);
667 #endif
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);
673
674         return 1;
675 }
676
677 static int video_palette_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
678 {
679         short start, count;
680         start = get_short(data);
681         count = get_short(data+2);
682         memcpy(g_palette + 3*start, data+4, 3*count);
683
684         return 1;
685 }
686
687 static int video_codemap_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
688 {
689         g_pCurMap = data;
690         g_nMapLength = len;
691         return 1;
692 }
693
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);
696
697 static int video_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
698 {
699         short nFrameHot, nFrameCold;
700         short nXoffset, nYoffset;
701         short nXsize, nYsize;
702         unsigned short nFlags;
703         unsigned char *temp;
704
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);
712
713         if (nFlags & 1)
714         {
715                 temp = (unsigned char *)g_vBackBuf1;
716                 g_vBackBuf1 = g_vBackBuf2;
717                 g_vBackBuf2 = temp;
718         }
719
720         /* convert the frame */
721         if (g_truecolor) {
722                 decodeFrame16((unsigned char *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
723         } else {
724                 decodeFrame8(g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
725         }
726
727         return 1;
728 }
729
730 static int end_chunk_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
731 {
732         g_pCurMap=NULL;
733         return 1;
734 }
735
736
737 #ifdef STANDALONE
738 void initializeMovie(MVESTREAM *mve)
739 {
740         int i;
741
742         for (i = 0; i < 32; i++)
743                 mve_set_handler(mve, i, default_seg_handler);
744
745         memset(unhandled_chunks, 0, 32*256);
746
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);
753
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);
758
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);
762
763         mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
764 }
765
766 void playMovie(MVESTREAM *mve)
767 {
768         int init_timer=0;
769         int cont=1;
770         while (cont && playing)
771         {
772                 cont = mve_play_next_chunk(mve);
773                 if (micro_frame_delay  &&  !init_timer)
774                 {
775                         timer_start();
776                         init_timer = 1;
777                 }
778
779                 do_timer_wait();
780
781                 if (g_loop && !cont) {
782                         mve_reset(mve);
783                         cont = 1;
784                 }
785         }
786 }
787
788 void shutdownMovie(MVESTREAM *mve)
789 {
790 #ifdef DEBUG
791         int i;
792 #endif
793
794         timer_stop();
795
796 #ifdef DEBUG
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]);
800                 }
801         }
802 #endif
803 }
804
805 #else
806 static MVESTREAM *mve = NULL;
807
808 int MVE_rmPrepMovie(int filehandle, int x, int y, int track)
809 {
810         int i;
811
812         if (mve) {
813                 mve_reset(mve);
814                 return 0;
815         }
816
817         mve = mve_open_filehandle(filehandle);
818
819         if (!mve)
820                 return 1;
821
822         g_destX = x;
823         g_destY = y;
824
825         for (i = 0; i < 32; i++)
826                 mve_set_handler(mve, i, default_seg_handler);
827
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);
834
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);
839
840         mve_set_handler(mve, MVE_OPCODE_SETPALETTE,           video_palette_handler);
841         mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, default_seg_handler);
842
843         mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP,       video_codemap_handler);
844
845         mve_set_handler(mve, MVE_OPCODE_VIDEODATA,            video_data_handler);
846
847         return 0;
848 }
849
850 int MVE_rmStepMovie()
851 {
852         static int init_timer=0;
853         int cont=1;
854
855         if (!timer_started)
856                 timer_start();
857
858         while (cont && !g_frameUpdated) // make a "step" be a frame, not a chunk...
859                 cont = mve_play_next_chunk(mve);
860         g_frameUpdated = 0;
861
862         if (micro_frame_delay  && !init_timer) {
863                 timer_start();
864                 init_timer = 1;
865         }
866
867         do_timer_wait();
868
869         if (cont)
870                 return 0;
871         else
872                 return MVE_ERR_EOF;
873 }
874
875 void MVE_rmEndMovie()
876 {
877 #ifdef AUDIO
878         int i;
879 #endif
880
881         timer_stop();
882         timer_created = 0;
883
884 #ifdef AUDIO
885         SDL_CloseAudio();
886         if (mve_audio_canplay) {
887                 // only close audio if we opened it
888                 mve_audio_canplay = 0;
889         }
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;
896         mve_audio_bufhead=0;
897         mve_audio_buftail=0;
898         mve_audio_playing=0;
899         mve_audio_canplay=0;
900         mve_audio_compressed=0;
901         if (mve_audio_spec)
902                 d_free(mve_audio_spec);
903         mve_audio_spec=NULL;
904         audiobuf_created = 0;
905 #endif
906
907         d_free(g_vBuffers);
908         g_vBuffers = NULL;
909         g_pCurMap=NULL;
910         g_nMapLength=0;
911         videobuf_created = 0;
912         video_initialized = 0;
913
914         mve_close_filehandle(mve);
915         mve = NULL;
916 }
917
918
919 void MVE_rmHoldMovie()
920 {
921         timer_started = 0;
922 }
923 #endif