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.
45 #include "osregistry.h"
50 static int mve_playing;
54 static int micro_frame_delay = 0;
55 static int timer_started = 0;
56 static int timer_created = 0;
57 static int timer_expire;
58 static Uint64 micro_timer_start = 0;
59 static Uint64 micro_timer_freq = 0;
62 #define MVE_AUDIO_BUFFERS 8 // total buffers to interact with stream
64 static std::vector<ALuint> mve_audio_bufl_free;
65 static ubyte *mve_audio_buf = NULL;
66 static size_t mve_audio_buf_size = 0;
67 static size_t mve_audio_buf_offset = 0;
69 static int mve_audio_playing = 0;
70 static int mve_audio_canplay = 0;
71 static int mve_audio_compressed = 0;
72 static int audiobuf_created;
74 // struct for the audio stream information
82 ALuint buffers[MVE_AUDIO_BUFFERS];
85 mve_audio_t *mas = NULL; // mve_audio_stream
90 int g_width, g_height;
91 void *g_vBuffers = NULL;
92 void *g_vBackBuf1, *g_vBackBuf2;
93 ushort *pixelbuf = NULL;
94 static int g_screenWidth, g_screenHeight;
95 static ubyte g_palette[768];
96 static ubyte *g_pCurMap=NULL;
97 static int g_nMapLength=0;
98 static int videobuf_created, video_inited;
100 static uint mve_video_skiptimer = 0;
101 static int mve_scale_video = 0;
102 static GLuint tex = 0;
105 void decodeFrame16(ubyte *pFrame, ubyte *pMap, int mapRemain, ubyte *pData, int dataRemain);
107 /*************************
109 *************************/
115 /*************************
117 *************************/
119 int mve_timer_create(ubyte *data)
121 micro_frame_delay = mve_get_int(data) * (int)mve_get_short(data+4);
123 micro_timer_start = SDL_GetPerformanceCounter();
124 micro_timer_freq = SDL_GetPerformanceFrequency();
126 if (micro_timer_freq < 1000) {
127 micro_timer_freq = 1000;
135 static int mve_timer_get_microseconds()
138 Uint64 us = SDL_GetPerformanceCounter() - micro_timer_start;
140 if (micro_timer_freq >= 1000000) {
141 us /= (micro_timer_freq / 1000000);
143 us *= (1000000 / micro_timer_freq);
149 static void mve_timer_start(void)
154 timer_expire = mve_timer_get_microseconds();
155 timer_expire += micro_frame_delay;
160 static int mve_do_timer_wait(void)
167 tv = mve_timer_get_microseconds();
169 if (tv > timer_expire)
172 ts = timer_expire - tv;
176 // try and burn off excess in attempt to keep sync
178 for (int i = 0; i < 10; i++) {
183 timer_expire += micro_frame_delay;
188 static void mve_timer_stop()
194 micro_timer_start = 0;
195 micro_timer_freq = 0;
198 /*************************
200 *************************/
202 // setup the audio information from the data stream
203 void mve_audio_createbuf(ubyte minor, ubyte *data)
205 if (audiobuf_created)
208 // if game sound disabled don't try and play movie audio
209 if (!Sound_enabled) {
210 mve_audio_canplay = 0;
211 audiobuf_created = 1;
215 int flags, desired_buffer, sample_rate;
217 mas = (mve_audio_t *) malloc ( sizeof(mve_audio_t) );
220 mve_audio_canplay = 0;
221 audiobuf_created = 1;
225 memset(mas, 0, sizeof(mve_audio_t));
227 mas->format = AL_INVALID;
229 flags = mve_get_ushort(data + 2);
230 sample_rate = mve_get_ushort(data + 4);
231 desired_buffer = mve_get_int(data + 6);
233 if (desired_buffer > 0) {
234 mve_audio_buf = (ubyte*) malloc (desired_buffer);
236 if (mve_audio_buf == NULL) {
237 mve_audio_canplay = 0;
238 audiobuf_created = 1;
242 mve_audio_buf_size = desired_buffer;
243 mve_audio_buf_offset = 0;
245 mve_audio_canplay = 0;
246 audiobuf_created = 1;
250 mas->channels = (flags & 0x0001) ? 2 : 1;
251 mas->bitsize = (flags & 0x0002) ? 16 : 8;
253 mas->sample_rate = sample_rate;
256 mve_audio_compressed = flags & 0x0004 ? 1 : 0;
258 mve_audio_compressed = 0;
261 if (mas->bitsize == 16) {
262 if (mas->channels == 2) {
263 mas->format = AL_FORMAT_STEREO16;
264 } else if (mas->channels == 1) {
265 mas->format = AL_FORMAT_MONO16;
267 } else if (mas->bitsize == 8) {
268 if (mas->channels == 2) {
269 mas->format = AL_FORMAT_STEREO8;
270 } else if (mas->channels == 1) {
271 mas->format = AL_FORMAT_MONO8;
275 // somethings wrong, bail now
276 if (mas->format == AL_INVALID) {
277 mve_audio_canplay = 0;
278 audiobuf_created = 1;
282 alGenSources(1, &mas->source_id);
284 if (mas->source_id == 0) {
285 mve_audio_canplay = 0;
286 audiobuf_created = 1;
290 alSourcef(mas->source_id, AL_GAIN, 1.0f);
291 alSource3f(mas->source_id, AL_POSITION, 0.0f, 0.0f, 0.0f);
292 alSource3f(mas->source_id, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
293 alSource3f(mas->source_id, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
294 alSourcef(mas->source_id, AL_ROLLOFF_FACTOR, 0.0f);
295 alSourcei(mas->source_id, AL_SOURCE_RELATIVE, AL_TRUE);
297 for (int i = 0; i < MVE_AUDIO_BUFFERS; i++) {
298 alGenBuffers(1, &mas->buffers[i]);
301 mve_audio_bufl_free.assign(mas->buffers, mas->buffers+MVE_AUDIO_BUFFERS);
303 audiobuf_created = 1;
304 mve_audio_canplay = 1;
307 // play and stream the audio
308 void mve_audio_play()
310 if (mve_audio_canplay) {
312 ALint status = AL_INVALID;
314 alGetSourcei(mas->source_id, AL_BUFFERS_QUEUED, &queued);
315 alGetSourcei(mas->source_id, AL_SOURCE_STATE, &status);
317 if ( (status != AL_PLAYING) && (queued > 0) ) {
318 alSourcePlay(mas->source_id);
319 mve_audio_playing = 1;
324 // call this in shutdown to stop and close audio
325 static void mve_audio_stop()
327 if (!audiobuf_created)
333 mve_audio_playing = 0;
334 audiobuf_created = 0;
336 mve_audio_bufl_free.clear();
338 if (mas && mas->source_id) {
339 alSourceStop(mas->source_id);
341 alGetSourcei(mas->source_id, AL_BUFFERS_PROCESSED, &processed);
343 while (processed > 0) {
344 alSourceUnqueueBuffers(mas->source_id, 1, &bid);
348 alDeleteBuffers(MVE_AUDIO_BUFFERS, mas->buffers);
349 alDeleteSources(1, &mas->source_id);
357 if (mve_audio_buf != NULL) {
359 mve_audio_buf = NULL;
362 mve_audio_buf_size = 0;
363 mve_audio_buf_offset = 0;
366 int mve_audio_data(ubyte major, ubyte *data)
368 static const int selected_chan = 1;
374 if (mve_audio_canplay) {
375 chan = mve_get_ushort(data + 2);
376 nsamp = mve_get_ushort(data + 4);
378 if (chan & selected_chan) {
379 if ( (mve_audio_buf_offset+nsamp+4) <= mve_audio_buf_size ) {
381 if (mve_audio_compressed) {
382 /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
385 mveaudio_uncompress(mve_audio_buf+mve_audio_buf_offset, data, -1);
390 memcpy(mve_audio_buf+mve_audio_buf_offset, data, nsamp);
394 memset(mve_audio_buf+mve_audio_buf_offset, 0, nsamp);
397 mve_audio_buf_offset += nsamp;
399 mprintf(("MVE audio_buf overrun!!\n"));
402 alGetSourcei(mas->source_id, AL_BUFFERS_PROCESSED, &processed);
405 alSourceUnqueueBuffers(mas->source_id, 1, &bid);
406 mve_audio_bufl_free.push_back(bid);
410 if ( !mve_audio_bufl_free.empty() ) {
411 bid = mve_audio_bufl_free.back();
413 alBufferData(bid, mas->format, mve_audio_buf, mve_audio_buf_offset, mas->sample_rate);
414 alSourceQueueBuffers(mas->source_id, 1, &bid);
416 mve_audio_buf_offset = 0;
417 mve_audio_bufl_free.pop_back();
420 if ( !mve_audio_playing ) {
429 /*************************
431 *************************/
433 int mve_video_createbuf(ubyte minor, ubyte *data)
435 if (videobuf_created)
439 short count, truecolor;
440 w = mve_get_short(data);
441 h = mve_get_short(data+2);
444 count = mve_get_short(data+4);
450 truecolor = mve_get_short(data+6);
458 // with Pierre's decoder16 fix in opcode 0xc, 8 should no longer be needed
459 g_vBackBuf1 = g_vBuffers = malloc(g_width * g_height * 4);
461 if (g_vBackBuf1 == NULL) {
462 mprintf(("MOVIE", "ERROR: Can't allocate video buffer"));
463 videobuf_created = 1;
467 g_vBackBuf2 = (ushort *)g_vBackBuf1 + (g_width * g_height);
469 memset(g_vBackBuf1, 0, g_width * g_height * 4);
471 videobuf_created = 1;
476 static void mve_convert_and_draw()
480 ushort *pixels = (ushort *)g_vBackBuf1;
487 if (g_screenWidth > g_width) {
488 pDests += ((g_screenWidth - g_width) / 2) / 2;
490 if (g_screenHeight > g_height) {
491 pDests += ((g_screenHeight - g_height) / 2) * g_screenWidth;
494 for (y=0; y<g_height; y++) {
495 for (x = 0; x < g_width; x++) {
496 pDests[x] = (1<<15)|*pSrcs;
500 pDests += g_screenWidth;
504 void mve_video_display()
506 fix t1 = timer_get_fixed_seconds();
507 mve_convert_and_draw();
510 int h = g_screenHeight;
511 int w = g_screenWidth;
514 if (mve_scale_video) {
517 // centers on 1024x768, fills on 640x480
518 x = ((gr_screen.max_w - g_screenWidth) / 2);
519 y = ((gr_screen.max_h - g_screenHeight) / 2);
522 // micro_frame_delay is divided by 10 to match mve_video_skiptimer overflow catch
523 if ( mve_video_skiptimer > (uint)(micro_frame_delay/10) ) {
524 // we are running slow so subtract desired time from actual and skip this frame
525 mve_video_skiptimer -= (micro_frame_delay/10);
528 // zero out so we can get a new count
529 mve_video_skiptimer = 0;
532 glBindTexture(GL_TEXTURE_2D, tex);
534 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, pixelbuf);
538 glTexCoord2f(0,0); glVertex2i(x,y);
539 glTexCoord2f(0,i2fl(256)/i2fl(hp2)); glVertex2i(x,y+256);
540 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+256,y+256);
541 glTexCoord2f(i2fl(256)/i2fl(wp2),0); glVertex2i(x+256,y);
546 glTexCoord2f(0,i2fl(256)/i2fl(hp2)); glVertex2i(x,y+256);
547 glTexCoord2f(0,i2fl(h)/i2fl(hp2)); glVertex2i(x,y+h);
548 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+256,y+h);
549 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+256,y+256);
554 glTexCoord2f(i2fl(256)/i2fl(wp2),0); glVertex2i(x+256,y);
555 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+256,y+256);
556 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+512,y+256);
557 glTexCoord2f(i2fl(512)/i2fl(wp2),0); glVertex2i(x+512,y);
562 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+256,y+256);
563 glTexCoord2f(i2fl(256)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+256,y+h);
564 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+512,y+h);
565 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+512,y+256);
570 glTexCoord2f(i2fl(512)/i2fl(wp2),0); glVertex2i(x+512,y);
571 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+512,y+256);
572 glTexCoord2f(i2fl(w)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+w,y+256);
573 glTexCoord2f(i2fl(w)/i2fl(wp2),0); glVertex2i(x+w,y);
578 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+512,y+256);
579 glTexCoord2f(i2fl(512)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+512,y+h);
580 glTexCoord2f(i2fl(w)/i2fl(wp2),i2fl(h)/i2fl(hp2)); glVertex2i(x+w,y+h);
581 glTexCoord2f(i2fl(w)/i2fl(wp2),i2fl(256)/i2fl(hp2)); glVertex2i(x+w,y+256);
584 // centers on 1024x768, fills on 640x480
585 x = ((gr_screen.max_w - g_screenWidth) / 2);
586 y = ((gr_screen.max_h - g_screenHeight) / 2);
588 // DDOI - This is probably really fricking slow
589 int bitmap = bm_create (16, w, h, pixelbuf, 0);
590 gr_set_bitmap (bitmap);
597 os_poll (); /* DDOI - run event loop(s) */
600 fix t2 = timer_get_fixed_seconds();
602 // only get a new count if we are definitely through with old count
603 if ( mve_video_skiptimer == 0 ) {
604 // for a more accurate count convert the frame rate to a float and multiply
605 // by one-hundred-thousand before converting to an uint.
606 mve_video_skiptimer = (uint)(f2fl(t2-t1) * 100000);
610 if ( k == SDLK_ESCAPE ) {
614 // fprintf(stderr, "mve frame took this long: %.6f\n", f2fl(t2-t1));
617 int mve_video_init(ubyte *data)
624 width = mve_get_short(data);
625 height = mve_get_short(data+2);
627 // DDOI - Allocate RGB565 pixel buffer
628 pixelbuf = (ushort *)malloc (width * height * 2);
630 if (pixelbuf == NULL) {
631 mprintf(("MOVIE", "ERROR: Can't allocate memory for pixelbuf"));
636 memset(pixelbuf, 0, width * height * 2);
638 g_screenWidth = width;
639 g_screenHeight = height;
644 tex_w = g_screenWidth;
645 tex_h = g_screenHeight;
647 // set height and width to a power of 2
648 for (i=0; i<16; i++ ) {
649 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
655 for (i=0; i<16; i++ ) {
656 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
662 // try to keep an 8:1 size ratio
671 glGenTextures(1, &tex);
676 mprintf(("MOVIE", "ERROR: Can't create a GL texture"));
681 glBindTexture(GL_TEXTURE_2D, tex);
683 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
684 glDepthFunc(GL_ALWAYS);
685 glDepthMask(GL_FALSE);
686 glDisable(GL_DEPTH_TEST);
687 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
688 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
690 if ( os_config_read_uint(NULL, NOX("ScaleMovies"), 0) == 1 ) {
691 float scale_by = (float)gr_screen.max_w / (float)g_screenWidth;
693 // don't bother setting anything if we aren't going to need it
694 if (scale_by != 1.0f) {
695 glMatrixMode(GL_MODELVIEW);
699 glScalef( scale_by, scale_by, 1.0f );
704 // NOTE: using NULL instead of pixelbuf crashes some drivers, but then so does pixelbuf so less of two evils...
705 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, wp2, hp2, 0, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, NULL);
708 memset(g_palette, 0, 768);
715 void mve_video_palette(ubyte *data)
718 start = mve_get_short(data);
719 count = mve_get_short(data+2);
720 memcpy(g_palette + 3*start, data+4, 3*count);
723 void mve_video_codemap(ubyte *data, int len)
729 void mve_video_data(ubyte *data, int len)
731 short nFrameHot, nFrameCold;
732 short nXoffset, nYoffset;
733 short nXsize, nYsize;
737 nFrameHot = mve_get_short(data);
738 nFrameCold = mve_get_short(data+2);
739 nXoffset = mve_get_short(data+4);
740 nYoffset = mve_get_short(data+6);
741 nXsize = mve_get_short(data+8);
742 nYsize = mve_get_short(data+10);
743 nFlags = mve_get_ushort(data+12);
746 temp = (ubyte *)g_vBackBuf1;
747 g_vBackBuf1 = g_vBackBuf2;
751 decodeFrame16((ubyte *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
759 void mve_init(MVESTREAM *mve)
761 // reset to default values
762 mve_audio_playing = 0;
763 mve_audio_canplay = 0;
764 mve_audio_compressed = 0;
765 mve_audio_buf_offset = 0;
766 audiobuf_created = 0;
768 videobuf_created = 0;
775 void mve_play(MVESTREAM *mve)
777 int init_timer = 0, timer_error = 0;
783 while (cont && mve_playing && !timer_error) {
784 cont = mve_play_next_chunk(mve);
786 if (micro_frame_delay && !init_timer) {
791 timer_error = mve_do_timer_wait();
801 if (pixelbuf != NULL) {
806 if (g_vBuffers != NULL) {
812 if (mve_scale_video) {
813 glMatrixMode(GL_MODELVIEW);
818 glDeleteTextures(1, &tex);
821 glEnable(GL_DEPTH_TEST);