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