2 * $Logfile: /Freespace2/code/movie/mveplayer.cpp $
7 * MVE movie playing routines
10 * Revision 1.7 2005/10/01 21:48:01 taylor
12 * fix decoder to swap opcode 0xb since it screws up on PPC
13 * the previous opcode 0xc change was wrong since we had already determined that it messes up FS1 movies
15 * Revision 1.6 2005/08/12 08:47:24 taylor
16 * use new audiostr code rather than old windows/unix version
17 * update all OpenAL commands with new error checking macros
18 * fix play_position to properly account for real position, fixes the talking heads and message text cutting out early
19 * movies will now use better filtering when scaled
21 * Revision 1.5 2005/03/31 21:26:02 taylor
22 * s/alGetSourceiv/alGetSourcei/
24 * Revision 1.4 2005/03/31 00:06:20 taylor
25 * go back to more accurate timer and allow video scaling for movies
27 * Revision 1.3 2005/03/29 07:50:34 taylor
28 * Update to newest movie code with much better video support and audio support from
29 * Pierre Willenbrock. Movies are enabled always now (no longer a build option)
30 * and but can be skipped with the "--nomovies" or "-n" cmdline options.
51 #include "osregistry.h"
56 static int mve_playing;
59 static int g_spdFactorNum = 0;
60 static int g_spdFactorDenom = 10;
61 static int micro_frame_delay = 0;
62 static int timer_started = 0;
64 static struct timeval timer_expire = {0, 0};
66 static fix timer_expire;
70 #define MVE_AUDIO_BUFFERS 64 // total buffers to interact with stream
71 static int mve_audio_curbuf_curpos = 0;
72 static int mve_audio_bufhead = 0;
73 static int mve_audio_buftail = 0;
74 static int mve_audio_playing = 0;
75 static int mve_audio_canplay = 0;
76 static int mve_audio_compressed = 0;
77 static int audiobuf_created;
81 // struct for the audio stream information
82 typedef struct MVE_AUDIO_T {
88 ALuint audio_data[MVE_AUDIO_BUFFERS];
90 ALuint audio_buffer[MVE_AUDIO_BUFFERS];
93 mve_audio_t *mas; // mve_audio_stream
99 int g_width, g_height;
100 void *g_vBuffers = NULL;
101 void *g_vBackBuf1, *g_vBackBuf2;
102 ushort *pixelbuf = NULL;
103 static int g_screenWidth, g_screenHeight;
104 static ubyte g_palette[768];
105 static ubyte *g_pCurMap=NULL;
106 static int g_nMapLength=0;
107 static int videobuf_created, video_inited;
109 static uint mve_video_skiptimer = 0;
110 static int mve_scale_video = 0;
112 static GLuint tex = 0;
116 void decodeFrame16(ubyte *pFrame, ubyte *pMap, int mapRemain, ubyte *pData, int dataRemain);
118 /*************************
120 *************************/
126 /*************************
128 *************************/
130 int mve_timer_create(ubyte *data)
134 micro_frame_delay = mve_get_int(data) * (int)mve_get_short(data+4);
136 if (g_spdFactorNum != 0) {
137 temp = micro_frame_delay;
138 temp *= g_spdFactorNum;
139 temp /= g_spdFactorDenom;
140 micro_frame_delay = (int)temp;
146 static void mve_timer_start(void)
151 gettimeofday(&timer_expire, NULL);
153 timer_expire.tv_usec += micro_frame_delay;
155 if (timer_expire.tv_usec > 1000000) {
156 nsec = timer_expire.tv_usec / 1000000;
157 timer_expire.tv_sec += nsec;
158 timer_expire.tv_usec -= nsec * 1000000;
161 timer_expire = timer_get_microseconds();
162 timer_expire += micro_frame_delay;
168 static int mve_do_timer_wait(void)
175 struct timespec ts, tsRem;
178 gettimeofday(&tv, NULL);
180 if (tv.tv_sec > timer_expire.tv_sec)
183 if ( (tv.tv_sec == timer_expire.tv_sec) && (tv.tv_usec >= timer_expire.tv_usec) )
186 ts.tv_sec = timer_expire.tv_sec - tv.tv_sec;
187 ts.tv_nsec = 1000 * (timer_expire.tv_usec - tv.tv_usec);
189 if (ts.tv_nsec < 0) {
190 ts.tv_nsec += 1000000000UL;
194 if ( (nanosleep(&ts, &tsRem) == -1) && (errno == EINTR) ) {
195 mprintf(("MVE: Timer error! Aborting movie playback!\n"));
200 timer_expire.tv_usec += micro_frame_delay;
202 if (timer_expire.tv_usec > 1000000) {
203 nsec = timer_expire.tv_usec / 1000000;
204 timer_expire.tv_sec += nsec;
205 timer_expire.tv_usec -= nsec * 1000000;
210 tv = timer_get_microseconds();
212 if (tv > timer_expire)
215 ts = timer_expire - tv;
222 timer_expire += micro_frame_delay;
228 static void mve_timer_stop()
230 timer_expire.tv_sec = 0;
231 timer_expire.tv_usec = 0;
235 /*************************
237 *************************/
239 // setup the audio information from the data stream
240 void mve_audio_createbuf(ubyte minor, ubyte *data)
242 if (audiobuf_created)
245 // if game sound disabled don't try and play movie audio
246 if (!Sound_enabled) {
247 mve_audio_canplay = 0;
252 int flags, desired_buffer, sample_rate;
254 mas = (mve_audio_t *) malloc ( sizeof(mve_audio_t) );
255 memset(mas, 0, sizeof(mve_audio_t));
257 mas->format = AL_INVALID;
259 flags = mve_get_ushort(data + 2);
260 sample_rate = mve_get_ushort(data + 4);
261 desired_buffer = mve_get_int(data + 6);
263 mas->channels = (flags & 0x0001) ? 2 : 1;
264 mas->bitsize = (flags & 0x0002) ? 16 : 8;
266 mas->sample_rate = sample_rate;
269 mve_audio_compressed = flags & 0x0004 ? 1 : 0;
271 mve_audio_compressed = 0;
274 if (mas->bitsize == 16) {
275 if (mas->channels == 2) {
276 mas->format = AL_FORMAT_STEREO16;
277 } else if (mas->channels == 1) {
278 mas->format = AL_FORMAT_MONO16;
280 } else if (mas->bitsize == 8) {
281 if (mas->channels == 2) {
282 mas->format = AL_FORMAT_STEREO8;
283 } else if (mas->channels == 1) {
284 mas->format = AL_FORMAT_MONO8;
288 // somethings wrong, bail now
289 if (mas->format == AL_INVALID) {
290 mve_audio_canplay = 0;
291 audiobuf_created = 1;
295 OpenAL_ErrorCheck( alGenSources(1, &mas->source_id), { mve_audio_canplay = 0; return; } );
297 mve_audio_canplay = 1;
299 OpenAL_ErrorPrint( alSourcef(mas->source_id, AL_GAIN, 1.0f) );
300 OpenAL_ErrorPrint( alSource3f(mas->source_id, AL_POSITION, 0.0f, 0.0f, 0.0f) );
301 OpenAL_ErrorPrint( alSource3f(mas->source_id, AL_VELOCITY, 0.0f, 0.0f, 0.0f) );
302 OpenAL_ErrorPrint( alSource3f(mas->source_id, AL_DIRECTION, 0.0f, 0.0f, 0.0f) );
303 OpenAL_ErrorPrint( alSourcef(mas->source_id, AL_ROLLOFF_FACTOR, 0.0f ) );
304 OpenAL_ErrorPrint( alSourcei(mas->source_id, AL_SOURCE_RELATIVE, AL_TRUE ) );
306 memset(mas->audio_buffer, 0, MVE_AUDIO_BUFFERS * sizeof(ALuint));
308 mve_audio_bufhead = 0;
309 mve_audio_buftail = 0;
311 audiobuf_created = 1;
315 // play and stream the audio
316 void mve_audio_play()
319 if (mve_audio_canplay) {
320 ALint status, bqueued;
322 OpenAL_ErrorCheck( alGetSourcei(mas->source_id, AL_SOURCE_STATE, &status), return );
324 OpenAL_ErrorCheck( alGetSourcei(mas->source_id, AL_BUFFERS_QUEUED, &bqueued), return );
326 mve_audio_playing = 1;
328 if (status != AL_PLAYING && bqueued > 0) {
329 OpenAL_ErrorPrint( alSourcePlay(mas->source_id) );
335 // call this in shutdown to stop and close audio
336 static void mve_audio_stop()
338 if (!audiobuf_created)
344 mve_audio_playing = 0;
346 OpenAL_ErrorPrint( alSourceStop(mas->source_id) );
347 OpenAL_ErrorPrint( alGetSourcei(mas->source_id, AL_BUFFERS_PROCESSED, &p) );
348 OpenAL_ErrorPrint( alSourceUnqueueBuffers(mas->source_id, p, mas->audio_buffer) );
349 OpenAL_ErrorPrint( alDeleteBuffers(MVE_AUDIO_BUFFERS, mas->audio_buffer) );
350 OpenAL_ErrorPrint( alDeleteSources(1, &mas->source_id) );
359 int mve_audio_data(ubyte major, ubyte *data)
362 static const int selected_chan=1;
366 if (mve_audio_canplay) {
367 chan = mve_get_ushort(data + 2);
368 nsamp = mve_get_ushort(data + 4);
370 if (chan & selected_chan) {
371 ALint bprocessed, bqueued, status;
374 OpenAL_ErrorCheck( alGetSourcei(mas->source_id, AL_BUFFERS_PROCESSED, &bprocessed), return 0 );
376 while (bprocessed-- > 2) {
377 OpenAL_ErrorPrint( alSourceUnqueueBuffers(mas->source_id, 1, &bid) );
378 // fprintf(stderr,"Unqueued buffer %d(%d)\n", mve_audio_bufhead, bid);
380 if (++mve_audio_bufhead == MVE_AUDIO_BUFFERS)
381 mve_audio_bufhead = 0;
384 OpenAL_ErrorCheck( alGetSourcei(mas->source_id, AL_BUFFERS_QUEUED, &bqueued), return 0 );
387 mprintf(("MVE: Buffer underun (First is normal)\n"));
389 OpenAL_ErrorCheck( alGetSourcei(mas->source_id, AL_SOURCE_STATE, &status), return 0 );
391 if (mve_audio_playing && status != AL_PLAYING && bqueued > 0) {
392 OpenAL_ErrorCheck( alSourcePlay(mas->source_id), return 0 );
395 if (bqueued < MVE_AUDIO_BUFFERS) {
398 /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
400 if (mve_audio_compressed) {
403 buf = (short *)malloc(nsamp);
404 mveaudio_uncompress(buf, data, -1); /* XXX */
409 buf = (short *)malloc(nsamp);
410 memcpy(buf, data, nsamp);
413 buf = (short *)malloc(nsamp);
415 memset(buf, 0, nsamp); /* XXX */
419 if (!mas->audio_buffer[mve_audio_buftail]) {
420 OpenAL_ErrorCheck( alGenBuffers(1,&mas->audio_buffer[mve_audio_buftail]), { free(buf); return 0; } );
423 OpenAL_ErrorCheck( alBufferData(mas->audio_buffer[mve_audio_buftail], mas->format, buf, nsamp, mas->sample_rate), { free(buf); return 0; } );
425 OpenAL_ErrorCheck( alSourceQueueBuffers(mas->source_id, 1, &mas->audio_buffer[mve_audio_buftail]), { free(buf); return 0;} );
427 //fprintf(stderr,"Queued buffer %d(%d)\n", mve_audio_buftail, mas->audio_buffer[mve_audio_buftail]);
429 if (++mve_audio_buftail == MVE_AUDIO_BUFFERS)
430 mve_audio_buftail = 0;
435 mprintf(("MVE: Buffer overrun: Queue full\n"));
438 //fprintf(stderr,"Buffers queued: %d\n", bqueued);
446 /*************************
448 *************************/
450 int mve_video_createbuf(ubyte minor, ubyte *data)
452 if (videobuf_created)
456 short count, truecolor;
457 w = mve_get_short(data);
458 h = mve_get_short(data+2);
461 count = mve_get_short(data+4);
467 truecolor = mve_get_short(data+6);
475 // with Pierre's decoder16 fix in opcode 0xc, 8 should no longer be needed
476 g_vBackBuf1 = g_vBuffers = malloc(g_width * g_height * 4);
478 if (g_vBackBuf1 == NULL) {
479 mprintf(("MOVIE", "ERROR: Can't allocate video buffer"));
480 videobuf_created = 1;
484 g_vBackBuf2 = (ushort *)g_vBackBuf1 + (g_width * g_height);
486 memset(g_vBackBuf1, 0, g_width * g_height * 4);
488 videobuf_created = 1;
493 static void mve_convert_and_draw()
497 ushort *pixels = (ushort *)g_vBackBuf1;
504 if (g_screenWidth > g_width) {
505 pDests += ((g_screenWidth - g_width) / 2) / 2;
507 if (g_screenHeight > g_height) {
508 pDests += ((g_screenHeight - g_height) / 2) * g_screenWidth;
511 for (y=0; y<g_height; y++) {
512 for (x = 0; x < g_width; x++) {
513 pDests[x] = (1<<15)|*pSrcs;
517 pDests += g_screenWidth;
521 void mve_video_display()
523 fix t1 = timer_get_fixed_seconds();
524 mve_convert_and_draw();
527 int h = g_screenHeight;
528 int w = g_screenWidth;
531 if (mve_scale_video) {
534 // centers on 1024x768, fills on 640x480
535 x = ((gr_screen.max_w - g_screenWidth) / 2);
536 y = ((gr_screen.max_h - g_screenHeight) / 2);
539 // micro_frame_delay is divided by 10 to match mve_video_skiptimer overflow catch
540 if ( mve_video_skiptimer > (uint)(micro_frame_delay/10) ) {
541 // we are running slow so subtract desired time from actual and skip this frame
542 mve_video_skiptimer -= (micro_frame_delay/10);
545 // zero out so we can get a new count
546 mve_video_skiptimer = 0;
549 glBindTexture(GL_TEXTURE_2D, tex);
551 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, pixelbuf);
555 glTexCoord2f(0,0); glVertex2i(x,y);
556 glTexCoord2f(0,i2fl(256)/i2fl(hp2)); glVertex2i(x,y+256);
557 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+256,y+256);
558 glTexCoord2f(i2fl(256)/i2fl(wp2),0); glVertex2i(x+256,y);
563 glTexCoord2f(0,i2fl(256)/i2fl(hp2)); glVertex2i(x,y+256);
564 glTexCoord2f(0,i2fl(h)/i2fl(hp2)); glVertex2i(x,y+h);
565 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+256,y+h);
566 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+256,y+256);
571 glTexCoord2f(i2fl(256)/i2fl(wp2),0); glVertex2i(x+256,y);
572 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+256,y+256);
573 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+512,y+256);
574 glTexCoord2f(i2fl(512)/i2fl(wp2),0); glVertex2i(x+512,y);
579 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+256,y+256);
580 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+256,y+h);
581 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+512,y+h);
582 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+512,y+256);
587 glTexCoord2f(i2fl(512)/i2fl(wp2),0); glVertex2i(x+512,y);
588 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+512,y+256);
589 glTexCoord2f(i2fl(w)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+w,y+256);
590 glTexCoord2f(i2fl(w)/i2fl(wp2),0); glVertex2i(x+w,y);
595 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+512,y+256);
596 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+512,y+h);
597 glTexCoord2f(i2fl(w)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+w,y+h);
598 glTexCoord2f(i2fl(w)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+w,y+256);
601 // centers on 1024x768, fills on 640x480
602 x = ((gr_screen.max_w - g_screenWidth) / 2);
603 y = ((gr_screen.max_h - g_screenHeight) / 2);
605 // DDOI - This is probably really fricking slow
606 int bitmap = bm_create (16, w, h, pixelbuf, 0);
607 gr_set_bitmap (bitmap);
614 os_poll (); /* DDOI - run event loop(s) */
617 fix t2 = timer_get_fixed_seconds();
619 // only get a new count if we are definitely through with old count
620 if ( mve_video_skiptimer == 0 ) {
621 // for a more accurate count convert the frame rate to a float and multiply
622 // by one-hundred-thousand before converting to an uint.
623 mve_video_skiptimer = (uint)(f2fl(t2-t1) * 100000);
627 if ( k == KEY_ESC ) {
631 // fprintf(stderr, "mve frame took this long: %.6f\n", f2fl(t2-t1));
634 int mve_video_init(ubyte *data)
641 width = mve_get_short(data);
642 height = mve_get_short(data+2);
644 // DDOI - Allocate RGB565 pixel buffer
645 pixelbuf = (ushort *)malloc (width * height * 2);
647 if (pixelbuf == NULL) {
648 mprintf(("MOVIE", "ERROR: Can't allocate memory for pixelbuf"));
653 memset(pixelbuf, 0, width * height * 2);
655 g_screenWidth = width;
656 g_screenHeight = height;
661 tex_w = g_screenWidth;
662 tex_h = g_screenHeight;
664 // set height and width to a power of 2
665 for (i=0; i<16; i++ ) {
666 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
672 for (i=0; i<16; i++ ) {
673 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
679 // try to keep an 8:1 size ratio
688 glGenTextures(1, &tex);
693 mprintf(("MOVIE", "ERROR: Can't create a GL texture"));
698 glBindTexture(GL_TEXTURE_2D, tex);
700 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
701 glDepthFunc(GL_ALWAYS);
702 glDepthMask(GL_FALSE);
703 glDisable(GL_DEPTH_TEST);
704 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
705 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
707 if ( os_config_read_uint(NULL, NOX("ScaleMovies"), 0) == 1 ) {
708 float scale_by = (float)gr_screen.max_w / (float)g_screenWidth;
710 // don't bother setting anything if we aren't going to need it
711 if (scale_by != 1.0f) {
712 glMatrixMode(GL_MODELVIEW);
716 glScalef( scale_by, scale_by, 1.0f );
721 // NOTE: using NULL instead of pixelbuf crashes some drivers, but then so does pixelbuf so less of two evils...
722 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, wp2, hp2, 0, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, NULL);
725 memset(g_palette, 0, 768);
732 void mve_video_palette(ubyte *data)
735 start = mve_get_short(data);
736 count = mve_get_short(data+2);
737 memcpy(g_palette + 3*start, data+4, 3*count);
740 void mve_video_codemap(ubyte *data, int len)
746 void mve_video_data(ubyte *data, int len)
748 short nFrameHot, nFrameCold;
749 short nXoffset, nYoffset;
750 short nXsize, nYsize;
754 nFrameHot = mve_get_short(data);
755 nFrameCold = mve_get_short(data+2);
756 nXoffset = mve_get_short(data+4);
757 nYoffset = mve_get_short(data+6);
758 nXsize = mve_get_short(data+8);
759 nYsize = mve_get_short(data+10);
760 nFlags = mve_get_ushort(data+12);
763 temp = (ubyte *)g_vBackBuf1;
764 g_vBackBuf1 = g_vBackBuf2;
768 decodeFrame16((ubyte *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
776 void mve_init(MVESTREAM *mve)
778 // reset to default values
779 mve_audio_curbuf_curpos = 0;
780 mve_audio_bufhead = 0;
781 mve_audio_buftail = 0;
782 mve_audio_playing = 0;
783 mve_audio_canplay = 0;
784 mve_audio_compressed = 0;
785 audiobuf_created = 0;
787 videobuf_created = 0;
794 void mve_play(MVESTREAM *mve)
796 int init_timer = 0, timer_error = 0;
802 while (cont && mve_playing && !timer_error) {
803 cont = mve_play_next_chunk(mve);
805 if (micro_frame_delay && !init_timer) {
810 timer_error = mve_do_timer_wait();
820 if (pixelbuf != NULL) {
825 if (g_vBuffers != NULL) {
831 if (mve_scale_video) {
832 glMatrixMode(GL_MODELVIEW);
837 glDeleteTextures(1, &tex);
840 glEnable(GL_DEPTH_TEST);