]> icculus.org git repositories - btb/d2x.git/blob - libmve/mveplay.c
fixed brightness slider
[btb/d2x.git] / libmve / mveplay.c
1 /* $Id: mveplay.c,v 1.6 2003-02-28 11:27:05 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 __MACOSX__
133 int nanosleep(struct timespec *ts, void *rem);
134 #endif
135
136 #ifdef __WIN32
137 #include <sys/timeb.h>
138
139 struct timespec
140 {
141         long int tv_sec;            /* Seconds.  */
142         long int tv_nsec;           /* Nanoseconds.  */
143 };
144
145 int gettimeofday(struct timeval *tv, void *tz)
146 {
147         static int counter = 0;
148         struct timeb tm;
149
150         counter++; /* to avoid collisions */
151         ftime(&tm);
152         tv->tv_sec  = tm.time;
153         tv->tv_usec = (tm.millitm * 1000) + counter;
154
155         return 0;
156 }
157
158 int nanosleep(struct timespec *ts, void *rem)
159 {
160         sleep(ts->tv_sec * 1000 + ts->tv_nsec / 1000000);
161
162         return 0;
163 }
164 #endif
165
166 static int create_timer_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
167 {
168         __extension__ long long temp;
169
170         if (timer_created)
171                 return 1;
172         else
173                 timer_created = 1;
174
175         micro_frame_delay = get_int(data) * (int)get_short(data+4);
176         if (g_spdFactorNum != 0)
177         {
178                 temp = micro_frame_delay;
179                 temp *= g_spdFactorNum;
180                 temp /= g_spdFactorDenom;
181                 micro_frame_delay = (int)temp;
182         }
183
184         return 1;
185 }
186
187 static void timer_stop(void)
188 {
189         timer_expire.tv_sec = 0;
190         timer_expire.tv_usec = 0;
191         timer_started = 0;
192 }
193
194 static void timer_start(void)
195 {
196         int nsec=0;
197         gettimeofday(&timer_expire, NULL);
198         timer_expire.tv_usec += micro_frame_delay;
199         if (timer_expire.tv_usec > 1000000)
200         {
201                 nsec = timer_expire.tv_usec / 1000000;
202                 timer_expire.tv_sec += nsec;
203                 timer_expire.tv_usec -= nsec*1000000;
204         }
205         timer_started=1;
206 }
207
208 static void do_timer_wait(void)
209 {
210         int nsec=0;
211         struct timespec ts;
212         struct timeval tv;
213         if (! timer_started)
214                 return;
215
216         gettimeofday(&tv, NULL);
217         if (tv.tv_sec > timer_expire.tv_sec)
218                 goto end;
219         else if (tv.tv_sec == timer_expire.tv_sec  &&  tv.tv_usec >= timer_expire.tv_usec)
220                 goto end;
221
222         ts.tv_sec = timer_expire.tv_sec - tv.tv_sec;
223         ts.tv_nsec = 1000 * (timer_expire.tv_usec - tv.tv_usec);
224         if (ts.tv_nsec < 0)
225         {
226                 ts.tv_nsec += 1000000000UL;
227                 --ts.tv_sec;
228         }
229 #ifdef __CYGWIN__
230         usleep(ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
231 #else
232         if (nanosleep(&ts, NULL) == -1  &&  errno == EINTR)
233                 exit(1);
234 #endif
235
236  end:
237         timer_expire.tv_usec += micro_frame_delay;
238         if (timer_expire.tv_usec > 1000000)
239         {
240                 nsec = timer_expire.tv_usec / 1000000;
241                 timer_expire.tv_sec += nsec;
242                 timer_expire.tv_usec -= nsec*1000000;
243         }
244 }
245
246 /*************************
247  * audio handlers
248  *************************/
249 #ifdef AUDIO
250 #define TOTAL_AUDIO_BUFFERS 64
251
252 static int audiobuf_created = 0;
253 static void mve_audio_callback(void *userdata, unsigned char *stream, int len);
254 static short *mve_audio_buffers[TOTAL_AUDIO_BUFFERS];
255 static int    mve_audio_buflens[TOTAL_AUDIO_BUFFERS];
256 static int    mve_audio_curbuf_curpos=0;
257 static int mve_audio_bufhead=0;
258 static int mve_audio_buftail=0;
259 static int mve_audio_playing=0;
260 static int mve_audio_canplay=0;
261 static int mve_audio_compressed=0;
262 static int mve_audio_enabled = 1;
263 static SDL_AudioSpec *mve_audio_spec=NULL;
264
265 static void mve_audio_callback(void *userdata, unsigned char *stream, int len)
266 {
267         int total=0;
268         int length;
269         if (mve_audio_bufhead == mve_audio_buftail)
270                 return /* 0 */;
271
272         //fprintf(stderr, "+ <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
273
274         while (mve_audio_bufhead != mve_audio_buftail                                           /* while we have more buffers  */
275                    &&  len > (mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos))        /* and while we need more data */
276         {
277                 length = mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos;
278                 memcpy(stream,                                                                  /* cur output position */
279                        ((unsigned char *)mve_audio_buffers[mve_audio_bufhead])+mve_audio_curbuf_curpos,           /* cur input position  */
280                        length);                                                                 /* cur input length    */
281
282                 total += length;
283                 stream += length;                                                               /* advance output */
284                 len -= length;                                                                  /* decrement avail ospace */
285                 d_free(mve_audio_buffers[mve_audio_bufhead]);                                   /* free the buffer */
286                 mve_audio_buffers[mve_audio_bufhead]=NULL;                                      /* free the buffer */
287                 mve_audio_buflens[mve_audio_bufhead]=0;                                         /* free the buffer */
288
289                 if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS)                                 /* next buffer */
290                         mve_audio_bufhead = 0;
291                 mve_audio_curbuf_curpos = 0;
292         }
293
294         //fprintf(stderr, "= <%d (%d), %d, %d>: %d\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len, total);
295         /*    return total; */
296
297         if (len != 0                                                                        /* ospace remaining  */
298                 &&  mve_audio_bufhead != mve_audio_buftail)                                     /* buffers remaining */
299         {
300                 memcpy(stream,                                                                  /* dest */
301                            ((unsigned char *)mve_audio_buffers[mve_audio_bufhead]) + mve_audio_curbuf_curpos,         /* src */
302                            len);                                                                    /* length */
303
304                 mve_audio_curbuf_curpos += len;                                                 /* advance input */
305                 stream += len;                                                                  /* advance output (unnecessary) */
306                 len -= len;                                                                     /* advance output (unnecessary) */
307
308                 if (mve_audio_curbuf_curpos >= mve_audio_buflens[mve_audio_bufhead])            /* if this ends the current chunk */
309                 {
310                         d_free(mve_audio_buffers[mve_audio_bufhead]);                               /* free buffer */
311                         mve_audio_buffers[mve_audio_bufhead]=NULL;
312                         mve_audio_buflens[mve_audio_bufhead]=0;
313
314                         if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS)                             /* next buffer */
315                                 mve_audio_bufhead = 0;
316                         mve_audio_curbuf_curpos = 0;
317                 }
318         }
319
320         //fprintf(stderr, "- <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
321 }
322 #endif
323
324 static int create_audiobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
325 {
326 #ifdef AUDIO
327         int flags;
328         int sample_rate;
329         int desired_buffer;
330
331         int stereo;
332         int bitsize;
333         int compressed;
334
335         int format;
336
337         if (!mve_audio_enabled)
338                 return 1;
339
340         if (audiobuf_created)
341                 return 1;
342         else
343                 audiobuf_created = 1;
344
345         flags = get_ushort(data + 2);
346         sample_rate = get_ushort(data + 4);
347         desired_buffer = get_int(data + 6);
348
349         stereo = (flags & MVE_AUDIO_FLAGS_STEREO) ? 1 : 0;
350         bitsize = (flags & MVE_AUDIO_FLAGS_16BIT) ? 1 : 0;
351
352         if (minor > 0) {
353                 compressed = flags & MVE_AUDIO_FLAGS_COMPRESSED ? 1 : 0;
354         } else {
355                 compressed = 0;
356         }
357
358         mve_audio_compressed = compressed;
359
360         if (bitsize == 1) {
361 #ifdef WORDS_BIGENDIAN
362                 format = AUDIO_S16MSB;
363 #else
364                 format = AUDIO_S16LSB;
365 #endif
366         } else {
367                 format = AUDIO_U8;
368         }
369
370         fprintf(stderr, "creating audio buffers:\n");
371         fprintf(stderr, "sample rate = %d, stereo = %d, bitsize = %d, compressed = %d\n",
372                         sample_rate, stereo, bitsize ? 16 : 8, compressed);
373
374         mve_audio_spec = (SDL_AudioSpec *)d_malloc(sizeof(SDL_AudioSpec));
375         mve_audio_spec->freq = sample_rate;
376         mve_audio_spec->format = format;
377         mve_audio_spec->channels = (stereo) ? 2 : 1;
378         mve_audio_spec->samples = 4096;
379         mve_audio_spec->callback = mve_audio_callback;
380         mve_audio_spec->userdata = NULL;
381         if (SDL_OpenAudio(mve_audio_spec, NULL) >= 0)
382         {
383                 fprintf(stderr, "   success\n");
384                 mve_audio_canplay = 1;
385         }
386         else
387         {
388                 fprintf(stderr, "   failure : %s\n", SDL_GetError());
389                 mve_audio_canplay = 0;
390         }
391
392         memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
393         memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
394 #endif
395
396         return 1;
397 }
398
399 static int play_audio_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
400 {
401 #ifdef AUDIO
402         if (mve_audio_canplay  &&  !mve_audio_playing  &&  mve_audio_bufhead != mve_audio_buftail)
403         {
404                 SDL_PauseAudio(0);
405                 mve_audio_playing = 1;
406         }
407 #endif
408         return 1;
409 }
410
411 static int audio_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
412 {
413 #ifdef AUDIO
414         static const int selected_chan=1;
415         int chan;
416         int nsamp;
417         if (mve_audio_canplay)
418         {
419                 if (mve_audio_playing)
420                         SDL_LockAudio();
421
422                 chan = get_ushort(data + 2);
423                 nsamp = get_ushort(data + 4);
424                 if (chan & selected_chan)
425                 {
426                         /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
427                         if (major == MVE_OPCODE_AUDIOFRAMEDATA) {
428                                 if (mve_audio_compressed) {
429                                         nsamp += 4;
430
431                                         mve_audio_buflens[mve_audio_buftail] = nsamp;
432                                         mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
433                                         mveaudio_uncompress(mve_audio_buffers[mve_audio_buftail], data, -1); /* XXX */
434                                 } else {
435                                         nsamp -= 8;
436                                         data += 8;
437
438                                         mve_audio_buflens[mve_audio_buftail] = nsamp;
439                                         mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
440                                         memcpy(mve_audio_buffers[mve_audio_buftail], data, nsamp);
441                                 }
442                         } else {
443                                 mve_audio_buflens[mve_audio_buftail] = nsamp;
444                                 mve_audio_buffers[mve_audio_buftail] = (short *)d_malloc(nsamp);
445
446                                 memset(mve_audio_buffers[mve_audio_buftail], 0, nsamp); /* XXX */
447                         }
448
449                         if (++mve_audio_buftail == TOTAL_AUDIO_BUFFERS)
450                                 mve_audio_buftail = 0;
451
452                         if (mve_audio_buftail == mve_audio_bufhead)
453                                 fprintf(stderr, "d'oh!  buffer ring overrun (%d)\n", mve_audio_bufhead);
454                 }
455
456                 if (mve_audio_playing)
457                         SDL_UnlockAudio();
458         }
459 #endif
460
461         return 1;
462 }
463
464 /*************************
465  * video handlers
466  *************************/
467 static int videobuf_created = 0;
468 static int video_initialized = 0;
469 int g_width, g_height;
470 void *g_vBuffers = NULL, *g_vBackBuf1, *g_vBackBuf2;
471
472 #ifdef STANDALONE
473 static SDL_Surface *g_screen;
474 #else
475 static int g_destX, g_destY;
476 #endif
477 static int g_screenWidth, g_screenHeight;
478 static unsigned char g_palette[768];
479 static unsigned char *g_pCurMap=NULL;
480 static int g_nMapLength=0;
481 static int g_truecolor;
482
483 static int create_videobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
484 {
485         short w, h;
486         short count, truecolor;
487
488         if (videobuf_created)
489                 return 1;
490         else
491                 videobuf_created = 1;
492
493         w = get_short(data);
494         h = get_short(data+2);
495
496         if (minor > 0) {
497                 count = get_short(data+4);
498         } else {
499                 count = 1;
500         }
501
502         if (minor > 1) {
503                 truecolor = get_short(data+6);
504         } else {
505                 truecolor = 0;
506         }
507
508         g_width = w << 3;
509         g_height = h << 3;
510
511         /* TODO: * 4 causes crashes on some files */
512         g_vBackBuf1 = g_vBuffers = d_malloc(g_width * g_height * 8);
513         if (truecolor) {
514                 g_vBackBuf2 = (unsigned short *)g_vBackBuf1 + (g_width * g_height);
515         } else {
516                 g_vBackBuf2 = (unsigned char *)g_vBackBuf1 + (g_width * g_height);
517         }
518
519         memset(g_vBackBuf1, 0, g_width * g_height * 4);
520
521 #ifdef DEBUG
522         fprintf(stderr, "DEBUG: w,h=%d,%d count=%d, tc=%d\n", w, h, count, truecolor);
523 #endif
524
525         g_truecolor = truecolor;
526
527         return 1;
528 }
529
530 #ifdef STANDALONE
531 static int do_sdl_events()
532 {
533         SDL_Event event;
534         int retr = 0;
535         while (SDL_PollEvent(&event)) {
536                 switch(event.type) {
537                 case SDL_QUIT:
538                         playing=0;
539                         break;
540                 case SDL_KEYDOWN:
541                         if (event.key.keysym.sym == SDLK_ESCAPE)
542                                 playing=0;
543                         break;
544                 case SDL_KEYUP:
545                         retr = 1;
546                         break;
547                 case SDL_MOUSEBUTTONDOWN:
548                         /*
549                           if (event.button.button == SDL_BUTTON_LEFT) {
550                           printf("GRID: %d,%d (pix:%d,%d)\n", 
551                           event.button.x / 16, event.button.y / 8,
552                           event.button.x, event.button.y);
553                           }
554                         */
555                         break;
556                 default:
557                         break;
558                 }
559         }
560
561         return retr;
562 }
563
564 static void ConvertAndDraw()
565 {
566         int i;
567         unsigned char *pal = g_palette;
568         unsigned char *pDest;
569         unsigned char *pixels = g_vBackBuf1;
570         SDL_Surface *screenSprite, *initSprite;
571         SDL_Rect renderArea;
572         int x, y;
573
574         initSprite = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, g_truecolor?16:8, 0x7C00, 0x03E0, 0x001F, 0);
575
576         if (!g_truecolor) {
577                 for(i = 0; i < 256; i++) {
578                         initSprite->format->palette->colors[i].r = (*pal++) << 2;
579                         initSprite->format->palette->colors[i].g = (*pal++) << 2;
580                         initSprite->format->palette->colors[i].b = (*pal++) << 2;
581                         initSprite->format->palette->colors[i].unused = 0;
582                 }
583         }
584
585         pDest = initSprite->pixels;
586
587         if (0 /*g_truecolor*/) {
588
589                 unsigned short *pSrcs, *pDests;
590
591                 pSrcs = (unsigned short *)pixels;
592                 pDests = (unsigned short *)pDest;
593
594                 for (y=0; y<g_height; y++) {
595                         for (x = 0; x < g_width; x++) {
596                                 pDests[x] = (1<<15)|*pSrcs;
597                                 pSrcs++;
598                         }
599                         pDests += g_screenWidth;
600                 }
601
602         } else {
603
604                 for (i=0; i<g_height; i++) {
605                         memcpy(pDest, pixels, g_width * (g_truecolor?2:1));
606                         pixels += g_width* (g_truecolor?2:1);
607                         pDest += initSprite->pitch;
608                 }
609         }
610
611         screenSprite = SDL_DisplayFormat(initSprite);
612         SDL_FreeSurface(initSprite);
613
614         if (g_screenWidth > screenSprite->w)
615                 x = (g_screenWidth - screenSprite->w) >> 1;
616         else
617                 x=0;
618         if (g_screenHeight > screenSprite->h)
619                 y = (g_screenHeight - screenSprite->h) >> 1;
620         else
621                 y=0;
622         renderArea.x = x;
623         renderArea.y = y;
624         renderArea.w = MIN(g_screenWidth  - x, screenSprite->w);
625         renderArea.h = MIN(g_screenHeight - y, screenSprite->h);
626         SDL_BlitSurface(screenSprite, NULL, g_screen, &renderArea);
627
628         SDL_FreeSurface(screenSprite);
629 }
630 #endif
631
632 static int display_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
633 {
634 #ifdef STANDALONE
635         ConvertAndDraw();
636
637         SDL_Flip(g_screen);
638
639         do_sdl_events();
640 #else
641         grs_bitmap *bitmap;
642
643         bitmap = gr_create_bitmap_raw(g_width, g_height, g_vBackBuf1);
644
645         if (g_destX == -1) // center it
646                 g_destX = (g_screenWidth - g_width) >> 1;
647         if (g_destY == -1) // center it
648                 g_destY = (g_screenHeight - g_height) >> 1;
649
650         gr_palette_load(g_palette);
651
652         gr_bitmap(g_destX, g_destY, bitmap);
653
654         gr_free_sub_bitmap(bitmap);
655 #endif
656         g_frameUpdated = 1;
657
658         return 1;
659 }
660
661 static int init_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
662 {
663         short width, height;
664
665         if (video_initialized)
666                 return 1;
667         else
668                 video_initialized = 1;
669
670         width = get_short(data);
671         height = get_short(data+2);
672 #ifdef STANDALONE
673         g_screen = SDL_SetVideoMode(width, height, 16, g_sdlVidFlags);
674 #endif
675         g_screenWidth = width;
676         g_screenHeight = height;
677         memset(g_palette, 0, 765);
678         // 255 needs to default to white, for subtitles, etc
679         memset(g_palette + 765, 63, 3);
680
681         return 1;
682 }
683
684 static int video_palette_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
685 {
686         short start, count;
687         start = get_short(data);
688         count = get_short(data+2);
689         memcpy(g_palette + 3*start, data+4, 3*count);
690
691         return 1;
692 }
693
694 static int video_codemap_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
695 {
696         g_pCurMap = data;
697         g_nMapLength = len;
698         return 1;
699 }
700
701 void decodeFrame16(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain);
702 void decodeFrame8(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain);
703
704 static int video_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
705 {
706         short nFrameHot, nFrameCold;
707         short nXoffset, nYoffset;
708         short nXsize, nYsize;
709         unsigned short nFlags;
710         unsigned char *temp;
711
712         nFrameHot  = get_short(data);
713         nFrameCold = get_short(data+2);
714         nXoffset   = get_short(data+4);
715         nYoffset   = get_short(data+6);
716         nXsize     = get_short(data+8);
717         nYsize     = get_short(data+10);
718         nFlags     = get_ushort(data+12);
719
720         if (nFlags & 1)
721         {
722                 temp = (unsigned char *)g_vBackBuf1;
723                 g_vBackBuf1 = g_vBackBuf2;
724                 g_vBackBuf2 = temp;
725         }
726
727         /* convert the frame */
728         if (g_truecolor) {
729                 decodeFrame16((unsigned char *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
730         } else {
731                 decodeFrame8(g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
732         }
733
734         return 1;
735 }
736
737 static int end_chunk_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
738 {
739         g_pCurMap=NULL;
740         return 1;
741 }
742
743
744 #ifdef STANDALONE
745 void initializeMovie(MVESTREAM *mve)
746 {
747         int i;
748
749         for (i = 0; i < 32; i++)
750                 mve_set_handler(mve, i, default_seg_handler);
751
752         memset(unhandled_chunks, 0, 32*256);
753
754         mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM, end_movie_handler);
755         mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK, end_chunk_handler);
756         mve_set_handler(mve, MVE_OPCODE_CREATETIMER, create_timer_handler);
757         mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS, create_audiobuf_handler);
758         mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO, play_audio_handler);
759         mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS, create_videobuf_handler);
760
761         mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO, display_video_handler);
762         mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA, audio_data_handler);
763         mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE, audio_data_handler);
764         mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE, init_video_handler);
765
766         mve_set_handler(mve, MVE_OPCODE_SETPALETTE, video_palette_handler);
767         mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, video_palette_handler);
768         mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP, video_codemap_handler);
769
770         mve_set_handler(mve, MVE_OPCODE_VIDEODATA, video_data_handler);
771 }
772
773 void playMovie(MVESTREAM *mve)
774 {
775         int init_timer=0;
776         int cont=1;
777
778         mve_audio_enabled = 1;
779
780         while (cont && playing)
781         {
782                 cont = mve_play_next_chunk(mve);
783                 if (micro_frame_delay  &&  !init_timer)
784                 {
785                         timer_start();
786                         init_timer = 1;
787                 }
788
789                 do_timer_wait();
790
791                 if (g_loop && !cont) {
792                         mve_reset(mve);
793                         cont = 1;
794                 }
795         }
796 }
797
798 void shutdownMovie(MVESTREAM *mve)
799 {
800 #ifdef DEBUG
801         int i;
802 #endif
803
804         timer_stop();
805
806 #ifdef DEBUG
807         for (i = 0; i < 32*256; i++) {
808                 if (unhandled_chunks[i]) {
809                         fprintf(stderr, "unhandled chunks of type %02x/%02x: %d\n", i>>8, i&0xFF, unhandled_chunks[i]);
810                 }
811         }
812 #endif
813 }
814
815 #else
816 static MVESTREAM *mve = NULL;
817
818 int MVE_rmPrepMovie(int filehandle, int x, int y, int track)
819 {
820         int i;
821
822         if (mve) {
823                 mve_reset(mve);
824                 return 0;
825         }
826
827         mve = mve_open_filehandle(filehandle);
828
829         if (!mve)
830                 return 1;
831
832         g_destX = x;
833         g_destY = y;
834
835         for (i = 0; i < 32; i++)
836                 mve_set_handler(mve, i, default_seg_handler);
837
838         mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM,          end_movie_handler);
839         mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK,           end_chunk_handler);
840         mve_set_handler(mve, MVE_OPCODE_CREATETIMER,          create_timer_handler);
841         mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS,     create_audiobuf_handler);
842         mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO,       play_audio_handler);
843         mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS,     create_videobuf_handler);
844
845         mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO,         display_video_handler);
846         mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA,       audio_data_handler);
847         mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE,    audio_data_handler);
848         mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE,        init_video_handler);
849
850         mve_set_handler(mve, MVE_OPCODE_SETPALETTE,           video_palette_handler);
851         mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, default_seg_handler);
852
853         mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP,       video_codemap_handler);
854
855         mve_set_handler(mve, MVE_OPCODE_VIDEODATA,            video_data_handler);
856
857         return 0;
858 }
859
860 int MVE_rmStepMovie()
861 {
862         static int init_timer=0;
863         int cont=1;
864
865         if (!timer_started)
866                 timer_start();
867
868         while (cont && !g_frameUpdated) // make a "step" be a frame, not a chunk...
869                 cont = mve_play_next_chunk(mve);
870         g_frameUpdated = 0;
871
872         if (micro_frame_delay  && !init_timer) {
873                 timer_start();
874                 init_timer = 1;
875         }
876
877         do_timer_wait();
878
879         if (cont)
880                 return 0;
881         else
882                 return MVE_ERR_EOF;
883 }
884
885 void MVE_rmEndMovie()
886 {
887 #ifdef AUDIO
888         int i;
889 #endif
890
891         timer_stop();
892         timer_created = 0;
893
894 #ifdef AUDIO
895         if (mve_audio_canplay) {
896                 // only close audio if we opened it
897                 SDL_CloseAudio();
898                 mve_audio_canplay = 0;
899         }
900         for (i = 0; i < TOTAL_AUDIO_BUFFERS; i++)
901                 if (mve_audio_buffers[i] != NULL)
902                         d_free(mve_audio_buffers[i]);
903         memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
904         memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
905         mve_audio_curbuf_curpos=0;
906         mve_audio_bufhead=0;
907         mve_audio_buftail=0;
908         mve_audio_playing=0;
909         mve_audio_canplay=0;
910         mve_audio_compressed=0;
911         if (mve_audio_spec)
912                 d_free(mve_audio_spec);
913         mve_audio_spec=NULL;
914         audiobuf_created = 0;
915 #endif
916
917         d_free(g_vBuffers);
918         g_vBuffers = NULL;
919         g_pCurMap=NULL;
920         g_nMapLength=0;
921         videobuf_created = 0;
922         video_initialized = 0;
923
924         mve_close_filehandle(mve);
925         mve = NULL;
926 }
927
928
929 void MVE_rmHoldMovie()
930 {
931         timer_started = 0;
932 }
933
934
935 void MVE_sndInit(int x)
936 {
937 #ifdef AUDIO
938         if (x == -1)
939                 mve_audio_enabled = 0;
940         else
941                 mve_audio_enabled = 1;
942 #endif
943 }
944
945 #endif