]> icculus.org git repositories - btb/d2x.git/blob - libmve/mveplay.c
get midi working with sdl_mixer and the hmp2midi from d2x-rebirth
[btb/d2x.git] / libmve / mveplay.c
1 #ifdef HAVE_CONFIG_H
2 #include <conf.h>
3 #endif
4
5 #ifndef __MSDOS__
6 #define AUDIO
7 #endif
8 //#define DEBUG
9
10 #include <string.h>
11 #ifdef _WIN32
12 # include <windows.h>
13 #else
14 # include <errno.h>
15 # include <time.h>
16 # include <fcntl.h>
17 # ifdef macintosh
18 #  include <types.h>
19 #  include <OSUtils.h>
20 # else
21 #  include <sys/time.h>
22 #  include <sys/types.h>
23 #  include <sys/stat.h>
24 #  include <unistd.h>
25 # endif // macintosh
26 #endif // _WIN32
27
28 #if defined(AUDIO)
29 #include <SDL.h>
30 #include "SDL_mixer.h"
31 #endif
32
33 #include "mvelib.h"
34 #include "mve_audio.h"
35
36 #include "decoders.h"
37
38 #include "libmve.h"
39
40 #define MVE_OPCODE_ENDOFSTREAM          0x00
41 #define MVE_OPCODE_ENDOFCHUNK           0x01
42 #define MVE_OPCODE_CREATETIMER          0x02
43 #define MVE_OPCODE_INITAUDIOBUFFERS     0x03
44 #define MVE_OPCODE_STARTSTOPAUDIO       0x04
45 #define MVE_OPCODE_INITVIDEOBUFFERS     0x05
46
47 #define MVE_OPCODE_DISPLAYVIDEO         0x07
48 #define MVE_OPCODE_AUDIOFRAMEDATA       0x08
49 #define MVE_OPCODE_AUDIOFRAMESILENCE    0x09
50 #define MVE_OPCODE_INITVIDEOMODE        0x0A
51
52 #define MVE_OPCODE_SETPALETTE           0x0C
53 #define MVE_OPCODE_SETPALETTECOMPRESSED 0x0D
54
55 #define MVE_OPCODE_SETDECODINGMAP       0x0F
56
57 #define MVE_OPCODE_VIDEODATA            0x11
58
59 #define MVE_AUDIO_FLAGS_STEREO     1
60 #define MVE_AUDIO_FLAGS_16BIT      2
61 #define MVE_AUDIO_FLAGS_COMPRESSED 4
62
63 int g_spdFactorNum=0;
64 static int g_spdFactorDenom=10;
65 static int g_frameUpdated = 0;
66
67 static short get_short(unsigned char *data)
68 {
69         short value;
70         value = data[0] | (data[1] << 8);
71         return value;
72 }
73
74 static unsigned short get_ushort(unsigned char *data)
75 {
76         unsigned short value;
77         value = data[0] | (data[1] << 8);
78         return value;
79 }
80
81 static int get_int(unsigned char *data)
82 {
83         int value;
84         value = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
85         return value;
86 }
87
88 static unsigned int unhandled_chunks[32*256];
89
90 static int default_seg_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
91 {
92         unhandled_chunks[major<<8|minor]++;
93         //fprintf(stderr, "unknown chunk type %02x/%02x\n", major, minor);
94         return 1;
95 }
96
97
98 /*************************
99  * general handlers
100  *************************/
101 static int end_movie_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
102 {
103         return 0;
104 }
105
106 /*************************
107  * timer handlers
108  *************************/
109
110 #if !defined(HAVE_STRUCT_TIMEVAL) || !HAVE_STRUCT_TIMEVAL // ifdef _WIN32_WCE
111 struct timeval
112 {
113         long tv_sec;
114         long tv_usec;
115 };
116 #endif
117
118 /*
119  * timer variables
120  */
121 static int timer_created = 0;
122 static int micro_frame_delay=0;
123 static int timer_started=0;
124 static struct timeval timer_expire = {0, 0};
125
126 #if !defined(HAVE_STRUCT_TIMESPEC) || !HAVE_STRUCT_TIMESPEC
127 struct timespec
128 {
129         long int tv_sec;            /* Seconds.  */
130         long int tv_nsec;           /* Nanoseconds.  */
131 };
132 #endif
133
134 #if defined(_WIN32) || defined(macintosh)
135 int gettimeofday(struct timeval *tv, void *tz)
136 {
137         static int counter = 0;
138 #ifdef _WIN32
139         DWORD now = GetTickCount();
140 #else
141         long now = TickCount();
142 #endif
143         counter++;
144
145         tv->tv_sec = now / 1000;
146         tv->tv_usec = (now % 1000) * 1000 + counter;
147
148         return 0;
149 }
150 #endif //  defined(_WIN32) || defined(macintosh)
151
152
153 static int create_timer_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
154 {
155
156 #if !defined(_WIN32) && !defined(macintosh) // FIXME
157         __extension__ long long temp;
158 #else
159         long temp;
160 #endif
161
162         if (timer_created)
163                 return 1;
164         else
165                 timer_created = 1;
166
167         micro_frame_delay = get_int(data) * (int)get_short(data+4);
168         if (g_spdFactorNum != 0)
169         {
170                 temp = micro_frame_delay;
171                 temp *= g_spdFactorNum;
172                 temp /= g_spdFactorDenom;
173                 micro_frame_delay = (int)temp;
174         }
175
176         return 1;
177 }
178
179 static void timer_stop(void)
180 {
181         timer_expire.tv_sec = 0;
182         timer_expire.tv_usec = 0;
183         timer_started = 0;
184 }
185
186 static void timer_start(void)
187 {
188         int nsec=0;
189         gettimeofday(&timer_expire, NULL);
190         timer_expire.tv_usec += micro_frame_delay;
191         if (timer_expire.tv_usec > 1000000)
192         {
193                 nsec = timer_expire.tv_usec / 1000000;
194                 timer_expire.tv_sec += nsec;
195                 timer_expire.tv_usec -= nsec*1000000;
196         }
197         timer_started=1;
198 }
199
200 static void do_timer_wait(void)
201 {
202         int nsec=0;
203         struct timespec ts;
204         struct timeval tv;
205         if (! timer_started)
206                 return;
207
208         gettimeofday(&tv, NULL);
209         if (tv.tv_sec > timer_expire.tv_sec)
210                 goto end;
211         else if (tv.tv_sec == timer_expire.tv_sec  &&  tv.tv_usec >= timer_expire.tv_usec)
212                 goto end;
213
214         ts.tv_sec = timer_expire.tv_sec - tv.tv_sec;
215         ts.tv_nsec = 1000 * (timer_expire.tv_usec - tv.tv_usec);
216         if (ts.tv_nsec < 0)
217         {
218                 ts.tv_nsec += 1000000000UL;
219                 --ts.tv_sec;
220         }
221 #ifdef _WIN32
222         Sleep(ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
223 #elif defined(macintosh)
224         Delay(ts.tv_sec * 1000 + ts.tv_nsec / 1000000, NULL);
225 #else
226         if (nanosleep(&ts, NULL) == -1  &&  errno == EINTR)
227                 exit(1);
228 #endif
229
230  end:
231         timer_expire.tv_usec += micro_frame_delay;
232         if (timer_expire.tv_usec > 1000000)
233         {
234                 nsec = timer_expire.tv_usec / 1000000;
235                 timer_expire.tv_sec += nsec;
236                 timer_expire.tv_usec -= nsec*1000000;
237         }
238 }
239
240 /*************************
241  * audio handlers
242  *************************/
243 #ifdef AUDIO
244 #define TOTAL_AUDIO_BUFFERS 64
245
246 static int audiobuf_created = 0;
247 static void mve_audio_callback(void *userdata, unsigned char *stream, int len);
248 static short *mve_audio_buffers[TOTAL_AUDIO_BUFFERS];
249 static int    mve_audio_buflens[TOTAL_AUDIO_BUFFERS];
250 static int    mve_audio_curbuf_curpos=0;
251 static int mve_audio_bufhead=0;
252 static int mve_audio_buftail=0;
253 static int mve_audio_playing=0;
254 static int mve_audio_canplay=0;
255 static int mve_audio_compressed=0;
256 static int mve_audio_enabled = 1;
257
258
259 static void mve_audio_callback(void *userdata, unsigned char *stream, int len)
260 {
261         int total=0;
262         int length;
263         if (mve_audio_bufhead == mve_audio_buftail)
264                 return /* 0 */;
265
266         //fprintf(stderr, "+ <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
267
268         while (mve_audio_bufhead != mve_audio_buftail                                           /* while we have more buffers  */
269                    &&  len > (mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos))        /* and while we need more data */
270         {
271                 length = mve_audio_buflens[mve_audio_bufhead]-mve_audio_curbuf_curpos;
272                 memcpy(stream,                                                                  /* cur output position */
273                        ((unsigned char *)mve_audio_buffers[mve_audio_bufhead])+mve_audio_curbuf_curpos,           /* cur input position  */
274                        length);                                                                 /* cur input length    */
275
276                 total += length;
277                 stream += length;                                                               /* advance output */
278                 len -= length;                                                                  /* decrement avail ospace */
279                 mve_free(mve_audio_buffers[mve_audio_bufhead]);                                 /* free the buffer */
280                 mve_audio_buffers[mve_audio_bufhead]=NULL;                                      /* free the buffer */
281                 mve_audio_buflens[mve_audio_bufhead]=0;                                         /* free the buffer */
282
283                 if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS)                                 /* next buffer */
284                         mve_audio_bufhead = 0;
285                 mve_audio_curbuf_curpos = 0;
286         }
287
288         //fprintf(stderr, "= <%d (%d), %d, %d>: %d\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len, total);
289         /*    return total; */
290
291         if (len != 0                                                                        /* ospace remaining  */
292                 &&  mve_audio_bufhead != mve_audio_buftail)                                     /* buffers remaining */
293         {
294                 memcpy(stream,                                                                  /* dest */
295                            ((unsigned char *)mve_audio_buffers[mve_audio_bufhead]) + mve_audio_curbuf_curpos,         /* src */
296                            len);                                                                    /* length */
297
298                 mve_audio_curbuf_curpos += len;                                                 /* advance input */
299                 stream += len;                                                                  /* advance output (unnecessary) */
300                 len -= len;                                                                     /* advance output (unnecessary) */
301
302                 if (mve_audio_curbuf_curpos >= mve_audio_buflens[mve_audio_bufhead])            /* if this ends the current chunk */
303                 {
304                         mve_free(mve_audio_buffers[mve_audio_bufhead]);                             /* free buffer */
305                         mve_audio_buffers[mve_audio_bufhead]=NULL;
306                         mve_audio_buflens[mve_audio_bufhead]=0;
307
308                         if (++mve_audio_bufhead == TOTAL_AUDIO_BUFFERS)                             /* next buffer */
309                                 mve_audio_bufhead = 0;
310                         mve_audio_curbuf_curpos = 0;
311                 }
312         }
313
314         //fprintf(stderr, "- <%d (%d), %d, %d>\n", mve_audio_bufhead, mve_audio_curbuf_curpos, mve_audio_buftail, len);
315 }
316 #endif
317
318 static int create_audiobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
319 {
320 #ifdef AUDIO
321         int flags;
322         int sample_rate;
323         int desired_buffer;
324
325         int stereo;
326         int bitsize;
327         int compressed;
328
329         int format;
330
331         if (!mve_audio_enabled)
332                 return 1;
333
334         if (audiobuf_created)
335                 return 1;
336         else
337                 audiobuf_created = 1;
338
339         flags = get_ushort(data + 2);
340         sample_rate = get_ushort(data + 4);
341         desired_buffer = get_int(data + 6);
342
343         stereo = (flags & MVE_AUDIO_FLAGS_STEREO) ? 1 : 0;
344         bitsize = (flags & MVE_AUDIO_FLAGS_16BIT) ? 1 : 0;
345
346         if (minor > 0) {
347                 compressed = flags & MVE_AUDIO_FLAGS_COMPRESSED ? 1 : 0;
348         } else {
349                 compressed = 0;
350         }
351
352         mve_audio_compressed = compressed;
353
354         if (bitsize == 1) {
355 #ifdef WORDS_BIGENDIAN
356                 format = AUDIO_S16MSB;
357 #else
358                 format = AUDIO_S16LSB;
359 #endif
360         } else {
361                 format = AUDIO_U8;
362         }
363
364         fprintf(stderr, "creating audio buffers:\n");
365         fprintf(stderr, "sample rate = %d, stereo = %d, bitsize = %d, compressed = %d\n",
366                         sample_rate, stereo, bitsize ? 16 : 8, compressed);
367
368         if (!Mix_OpenAudio(sample_rate, format, stereo ? 2 : 1, 4096) >= 0)
369         {
370                 fprintf(stderr, "   success\n");
371                 mve_audio_canplay = 1;
372         }
373         else
374         {
375                 fprintf(stderr, "   failure : %s\n", Mix_GetError());
376                 mve_audio_canplay = 0;
377         }
378
379         Mix_SetPostMix(mve_audio_callback, NULL);
380         mve_audio_canplay = 1;
381
382         memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
383         memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
384 #endif
385
386         return 1;
387 }
388
389 static int play_audio_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
390 {
391 #ifdef AUDIO
392         if (mve_audio_canplay  &&  !mve_audio_playing  &&  mve_audio_bufhead != mve_audio_buftail)
393         {
394                 Mix_Resume(-1);
395                 mve_audio_playing = 1;
396         }
397 #endif
398         return 1;
399 }
400
401 static int audio_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
402 {
403 #ifdef AUDIO
404         static const int selected_chan=1;
405         int chan;
406         int nsamp;
407         if (mve_audio_canplay)
408         {
409                 chan = get_ushort(data + 2);
410                 nsamp = get_ushort(data + 4);
411                 if (chan & selected_chan)
412                 {
413                         /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
414                         if (major == MVE_OPCODE_AUDIOFRAMEDATA) {
415                                 if (mve_audio_compressed) {
416                                         nsamp += 4;
417
418                                         mve_audio_buflens[mve_audio_buftail] = nsamp;
419                                         mve_audio_buffers[mve_audio_buftail] = (short *)mve_alloc(nsamp);
420                                         mveaudio_uncompress(mve_audio_buffers[mve_audio_buftail], data, -1); /* XXX */
421                                 } else {
422                                         nsamp -= 8;
423                                         data += 8;
424
425                                         mve_audio_buflens[mve_audio_buftail] = nsamp;
426                                         mve_audio_buffers[mve_audio_buftail] = (short *)mve_alloc(nsamp);
427                                         memcpy(mve_audio_buffers[mve_audio_buftail], data, nsamp);
428                                 }
429                         } else {
430                                 mve_audio_buflens[mve_audio_buftail] = nsamp;
431                                 mve_audio_buffers[mve_audio_buftail] = (short *)mve_alloc(nsamp);
432
433                                 memset(mve_audio_buffers[mve_audio_buftail], 0, nsamp); /* XXX */
434                         }
435
436                         if (++mve_audio_buftail == TOTAL_AUDIO_BUFFERS)
437                                 mve_audio_buftail = 0;
438
439                         if (mve_audio_buftail == mve_audio_bufhead)
440                                 fprintf(stderr, "d'oh!  buffer ring overrun (%d)\n", mve_audio_bufhead);
441                 }
442         }
443 #endif
444
445         return 1;
446 }
447
448 /*************************
449  * video handlers
450  *************************/
451
452 static int videobuf_created = 0;
453 static int video_initialized = 0;
454 int g_width, g_height;
455 void *g_vBuffers = NULL, *g_vBackBuf1, *g_vBackBuf2;
456
457 static int g_destX, g_destY;
458 static int g_screenWidth, g_screenHeight;
459 static unsigned char *g_pCurMap=NULL;
460 static int g_nMapLength=0;
461 static int g_truecolor;
462
463 static int create_videobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
464 {
465         short w, h;
466         short count, truecolor;
467
468         if (videobuf_created)
469                 return 1;
470         else
471                 videobuf_created = 1;
472
473         w = get_short(data);
474         h = get_short(data+2);
475
476         if (minor > 0) {
477                 count = get_short(data+4);
478         } else {
479                 count = 1;
480         }
481
482         if (minor > 1) {
483                 truecolor = get_short(data+6);
484         } else {
485                 truecolor = 0;
486         }
487
488         g_width = w << 3;
489         g_height = h << 3;
490
491         /* TODO: * 4 causes crashes on some files */
492         /* only malloc once */
493         if (g_vBuffers == NULL)
494                 g_vBackBuf1 = g_vBuffers = mve_alloc(g_width * g_height * 8);
495         if (truecolor) {
496                 g_vBackBuf2 = (unsigned short *)g_vBackBuf1 + (g_width * g_height);
497         } else {
498                 g_vBackBuf2 = (unsigned char *)g_vBackBuf1 + (g_width * g_height);
499         }
500
501         memset(g_vBackBuf1, 0, g_width * g_height * 4);
502
503 #ifdef DEBUG
504         fprintf(stderr, "DEBUG: w,h=%d,%d count=%d, tc=%d\n", w, h, count, truecolor);
505 #endif
506
507         g_truecolor = truecolor;
508
509         return 1;
510 }
511
512 static int display_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
513 {
514         if (g_destX == -1) // center it
515                 g_destX = (g_screenWidth - g_width) >> 1;
516         if (g_destY == -1) // center it
517                 g_destY = (g_screenHeight - g_height) >> 1;
518
519         mve_showframe(g_vBackBuf1, g_width, g_height, 0, 0,
520                       g_width, g_height, g_destX, g_destY);
521
522         g_frameUpdated = 1;
523
524         return 1;
525 }
526
527 static int init_video_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
528 {
529         short width, height;
530
531         if (video_initialized)
532                 return 1; /* maybe we actually need to change width/height here? */
533         else
534                 video_initialized = 1;
535
536         width = get_short(data);
537         height = get_short(data+2);
538         g_screenWidth = width;
539         g_screenHeight = height;
540
541         return 1;
542 }
543
544 static int video_palette_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
545 {
546         short start, count;
547         unsigned char *p;
548
549         start = get_short(data);
550         count = get_short(data+2);
551
552         p = data + 4;
553
554         mve_setpalette(p - 3*start, start, count);
555
556         return 1;
557 }
558
559 static int video_codemap_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
560 {
561         g_pCurMap = data;
562         g_nMapLength = len;
563         return 1;
564 }
565
566 static int video_data_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
567 {
568         short nFrameHot, nFrameCold;
569         short nXoffset, nYoffset;
570         short nXsize, nYsize;
571         unsigned short nFlags;
572         unsigned char *temp;
573
574         nFrameHot  = get_short(data);
575         nFrameCold = get_short(data+2);
576         nXoffset   = get_short(data+4);
577         nYoffset   = get_short(data+6);
578         nXsize     = get_short(data+8);
579         nYsize     = get_short(data+10);
580         nFlags     = get_ushort(data+12);
581
582         if (nFlags & 1)
583         {
584                 temp = (unsigned char *)g_vBackBuf1;
585                 g_vBackBuf1 = g_vBackBuf2;
586                 g_vBackBuf2 = temp;
587         }
588
589         /* convert the frame */
590         if (g_truecolor) {
591                 decodeFrame16((unsigned char *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
592         } else {
593                 decodeFrame8(g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
594         }
595
596         return 1;
597 }
598
599 static int end_chunk_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
600 {
601         g_pCurMap=NULL;
602         return 1;
603 }
604
605
606 static MVESTREAM *mve = NULL;
607
608 void MVE_ioCallbacks(mve_cb_Read io_read)
609 {
610         mve_read = io_read;
611 }
612
613 void MVE_memCallbacks(mve_cb_Alloc mem_alloc, mve_cb_Free mem_free)
614 {
615         mve_alloc = mem_alloc;
616         mve_free = mem_free;
617 }
618
619 void MVE_sfCallbacks(mve_cb_ShowFrame showframe)
620 {
621         mve_showframe = showframe;
622 }
623
624 void MVE_palCallbacks(mve_cb_SetPalette setpalette)
625 {
626         mve_setpalette = setpalette;
627 }
628
629 int MVE_rmPrepMovie(void *src, int x, int y, int track)
630 {
631         int i;
632
633         if (mve) {
634                 mve_reset(mve);
635                 return 0;
636         }
637
638         mve = mve_open(src);
639
640         if (!mve)
641                 return 1;
642
643         g_destX = x;
644         g_destY = y;
645
646         for (i = 0; i < 32; i++)
647                 mve_set_handler(mve, i, default_seg_handler);
648
649         mve_set_handler(mve, MVE_OPCODE_ENDOFSTREAM,          end_movie_handler);
650         mve_set_handler(mve, MVE_OPCODE_ENDOFCHUNK,           end_chunk_handler);
651         mve_set_handler(mve, MVE_OPCODE_CREATETIMER,          create_timer_handler);
652         mve_set_handler(mve, MVE_OPCODE_INITAUDIOBUFFERS,     create_audiobuf_handler);
653         mve_set_handler(mve, MVE_OPCODE_STARTSTOPAUDIO,       play_audio_handler);
654         mve_set_handler(mve, MVE_OPCODE_INITVIDEOBUFFERS,     create_videobuf_handler);
655
656         mve_set_handler(mve, MVE_OPCODE_DISPLAYVIDEO,         display_video_handler);
657         mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMEDATA,       audio_data_handler);
658         mve_set_handler(mve, MVE_OPCODE_AUDIOFRAMESILENCE,    audio_data_handler);
659         mve_set_handler(mve, MVE_OPCODE_INITVIDEOMODE,        init_video_handler);
660
661         mve_set_handler(mve, MVE_OPCODE_SETPALETTE,           video_palette_handler);
662         mve_set_handler(mve, MVE_OPCODE_SETPALETTECOMPRESSED, default_seg_handler);
663
664         mve_set_handler(mve, MVE_OPCODE_SETDECODINGMAP,       video_codemap_handler);
665
666         mve_set_handler(mve, MVE_OPCODE_VIDEODATA,            video_data_handler);
667
668         mve_play_next_chunk(mve); /* video initialization chunk */
669         if (mve_audio_enabled)
670                 mve_play_next_chunk(mve); /* audio initialization chunk */
671
672         return 0;
673 }
674
675
676 void MVE_getVideoSpec(MVE_videoSpec *vSpec)
677 {
678         vSpec->screenWidth = g_screenWidth;
679         vSpec->screenHeight = g_screenHeight;
680         vSpec->width = g_width;
681         vSpec->height = g_height;
682         vSpec->truecolor = g_truecolor;
683 }
684
685
686 int MVE_rmStepMovie()
687 {
688         static int init_timer=0;
689         int cont=1;
690
691         if (!timer_started)
692                 timer_start();
693
694         while (cont && !g_frameUpdated) // make a "step" be a frame, not a chunk...
695                 cont = mve_play_next_chunk(mve);
696         g_frameUpdated = 0;
697
698         if (!cont)
699                 return MVE_ERR_EOF;
700
701         if (micro_frame_delay  && !init_timer) {
702                 timer_start();
703                 init_timer = 1;
704         }
705
706         do_timer_wait();
707
708         return 0;
709 }
710
711 void MVE_rmEndMovie()
712 {
713 #ifdef AUDIO
714         int i;
715 #endif
716
717         timer_stop();
718         timer_created = 0;
719
720 #ifdef AUDIO
721         if (mve_audio_canplay) {
722                 // only close audio if we opened it
723                 Mix_CloseAudio();
724                 mve_audio_canplay = 0;
725         }
726         for (i = 0; i < TOTAL_AUDIO_BUFFERS; i++)
727                 if (mve_audio_buffers[i] != NULL)
728                         mve_free(mve_audio_buffers[i]);
729         memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
730         memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));
731         mve_audio_curbuf_curpos=0;
732         mve_audio_bufhead=0;
733         mve_audio_buftail=0;
734         mve_audio_playing=0;
735         mve_audio_canplay=0;
736         mve_audio_compressed=0;
737         audiobuf_created = 0;
738 #endif
739
740         mve_free(g_vBuffers);
741         g_vBuffers = NULL;
742         g_pCurMap=NULL;
743         g_nMapLength=0;
744         videobuf_created = 0;
745         video_initialized = 0;
746
747         mve_close(mve);
748         mve = NULL;
749 }
750
751
752 void MVE_rmHoldMovie()
753 {
754         timer_started = 0;
755 }
756
757
758 void MVE_sndInit(int x)
759 {
760 #ifdef AUDIO
761         if (x == -1)
762                 mve_audio_enabled = 0;
763         else
764                 mve_audio_enabled = 1;
765 #endif
766 }