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 = 0;
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_pCurMap=NULL;
96 static int g_nMapLength=0;
97 static int videobuf_created, video_inited;
98 static int mve_scale_video = 0;
99 static GLuint tex = 0;
106 static g_coords_t g_coords[4];
110 void decodeFrame16(ubyte *pFrame, ubyte *pMap, int mapRemain, ubyte *pData, int dataRemain);
112 /*************************
114 *************************/
120 /*************************
122 *************************/
124 int mve_timer_create(ubyte *data)
126 micro_frame_delay = mve_get_int(data) * (int)mve_get_short(data+4);
128 micro_timer_start = SDL_GetPerformanceCounter();
129 micro_timer_freq = SDL_GetPerformanceFrequency();
131 if (micro_timer_freq < 1000) {
132 micro_timer_freq = 1000;
140 static int mve_timer_get_microseconds()
143 Uint64 us = SDL_GetPerformanceCounter() - micro_timer_start;
145 if (micro_timer_freq >= 1000000) {
146 us /= (micro_timer_freq / 1000000);
148 us *= (1000000 / micro_timer_freq);
154 static void mve_timer_start(void)
159 timer_expire = mve_timer_get_microseconds();
160 timer_expire += micro_frame_delay;
165 static int mve_do_timer_wait(void)
172 tv = mve_timer_get_microseconds();
174 if (tv > timer_expire)
177 ts = timer_expire - tv;
179 SDL_Delay(ts / 1000);
181 // try and burn off excess in attempt to keep sync
183 for (int i = 0; i < 10; i++) {
188 timer_expire += micro_frame_delay;
193 static void mve_timer_stop()
199 micro_frame_delay = 0;
201 micro_timer_start = 0;
202 micro_timer_freq = 0;
205 /*************************
207 *************************/
209 // setup the audio information from the data stream
210 void mve_audio_createbuf(ubyte minor, ubyte *data)
212 if (audiobuf_created)
215 // if game sound disabled don't try and play movie audio
216 if ( !Sound_enabled ) {
217 mve_audio_canplay = 0;
218 audiobuf_created = 1;
222 int flags, desired_buffer, sample_rate;
224 mas = (mve_audio_t *) malloc ( sizeof(mve_audio_t) );
227 mve_audio_canplay = 0;
228 audiobuf_created = 1;
232 memset(mas, 0, sizeof(mve_audio_t));
234 mas->format = AL_INVALID;
236 flags = mve_get_ushort(data + 2);
237 sample_rate = mve_get_ushort(data + 4);
238 desired_buffer = mve_get_int(data + 6);
240 if (desired_buffer > 0) {
241 mve_audio_buf = (ubyte*) malloc (desired_buffer);
243 if (mve_audio_buf == NULL) {
244 mve_audio_canplay = 0;
245 audiobuf_created = 1;
249 mve_audio_buf_size = desired_buffer;
250 mve_audio_buf_offset = 0;
252 mve_audio_canplay = 0;
253 audiobuf_created = 1;
257 mas->channels = (flags & 0x0001) ? 2 : 1;
258 mas->bitsize = (flags & 0x0002) ? 16 : 8;
260 mas->sample_rate = sample_rate;
263 mve_audio_compressed = flags & 0x0004 ? 1 : 0;
265 mve_audio_compressed = 0;
268 if (mas->bitsize == 16) {
269 if (mas->channels == 2) {
270 mas->format = AL_FORMAT_STEREO16;
271 } else if (mas->channels == 1) {
272 mas->format = AL_FORMAT_MONO16;
274 } else if (mas->bitsize == 8) {
275 if (mas->channels == 2) {
276 mas->format = AL_FORMAT_STEREO8;
277 } else if (mas->channels == 1) {
278 mas->format = AL_FORMAT_MONO8;
282 // somethings wrong, bail now
283 if (mas->format == AL_INVALID) {
284 mve_audio_canplay = 0;
285 audiobuf_created = 1;
289 oal_check_for_errors("mve_audio_createbuf() begin");
291 for (int i = 0; i < MVE_AUDIO_BUFFERS; i++) {
292 alGenBuffers(1, &mas->buffers[i]);
294 if ( !mas->buffers[i] ) {
295 mve_audio_canplay = 0;
296 audiobuf_created = 1;
301 mve_audio_bufl_free.assign(mas->buffers, mas->buffers+MVE_AUDIO_BUFFERS);
303 mas->chan = oal_get_free_channel(1.0f, -1, SND_PRIORITY_MUST_PLAY);
305 if (mas->chan == NULL) {
306 mve_audio_canplay = 0;
307 audiobuf_created = 1;
311 alSourcef(mas->chan->source_id, AL_GAIN, 1.0f);
312 alSource3f(mas->chan->source_id, AL_POSITION, 0.0f, 0.0f, 0.0f);
313 alSource3f(mas->chan->source_id, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
314 alSource3f(mas->chan->source_id, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
315 alSourcef(mas->chan->source_id, AL_ROLLOFF_FACTOR, 0.0f);
316 alSourcei(mas->chan->source_id, AL_SOURCE_RELATIVE, AL_TRUE);
318 oal_check_for_errors("mve_audio_createbuf() end");
320 audiobuf_created = 1;
321 mve_audio_canplay = 1;
324 // play and stream the audio
325 void mve_audio_play()
327 if (mve_audio_canplay) {
329 ALint status = AL_INVALID;
331 oal_check_for_errors("mve_audio_play() begin");
333 alGetSourcei(mas->chan->source_id, AL_BUFFERS_QUEUED, &queued);
334 alGetSourcei(mas->chan->source_id, AL_SOURCE_STATE, &status);
336 if ( (status != AL_PLAYING) && (queued > 0) ) {
337 alSourcePlay(mas->chan->source_id);
338 mve_audio_playing = 1;
341 oal_check_for_errors("mve_audio_play() end");
345 // call this in shutdown to stop and close audio
346 static void mve_audio_stop()
348 if (!audiobuf_created)
351 oal_check_for_errors("mve_audio_stop() begin");
353 mve_audio_playing = 0;
354 mve_audio_canplay = 0;
355 mve_audio_compressed = 0;
357 audiobuf_created = 0;
359 mve_audio_bufl_free.clear();
363 alSourceStop(mas->chan->source_id);
365 // detach buffers from source so that we can delete them
366 alSourcei(mas->chan->source_id, AL_BUFFER, 0);
369 for (int i = 0; i < MVE_AUDIO_BUFFERS; i++) {
370 if ( alIsBuffer(mas->buffers[i]) ) {
371 alDeleteBuffers(1, &mas->buffers[i]);
381 if (mve_audio_buf != NULL) {
383 mve_audio_buf = NULL;
386 mve_audio_buf_size = 0;
387 mve_audio_buf_offset = 0;
389 oal_check_for_errors("mve_audio_stop() end");
392 int mve_audio_data(ubyte major, ubyte *data)
394 static const int selected_chan = 1;
400 if (mve_audio_canplay) {
401 chan = mve_get_ushort(data + 2);
402 nsamp = mve_get_ushort(data + 4);
404 if (chan & selected_chan) {
405 oal_check_for_errors("mve_audio_data() begin");
407 if ( (mve_audio_buf_offset+nsamp+4) <= mve_audio_buf_size ) {
409 if (mve_audio_compressed) {
410 /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
413 mveaudio_uncompress(mve_audio_buf+mve_audio_buf_offset, data, -1);
418 memcpy(mve_audio_buf+mve_audio_buf_offset, data, nsamp);
422 memset(mve_audio_buf+mve_audio_buf_offset, 0, nsamp);
425 mve_audio_buf_offset += nsamp;
427 mprintf(("MVE audio_buf overrun!!\n"));
430 alGetSourcei(mas->chan->source_id, AL_BUFFERS_PROCESSED, &processed);
433 alSourceUnqueueBuffers(mas->chan->source_id, 1, &bid);
435 mve_audio_bufl_free.push_back(bid);
439 if ( !mve_audio_bufl_free.empty() ) {
440 bid = mve_audio_bufl_free.back();
442 alBufferData(bid, mas->format, mve_audio_buf, mve_audio_buf_offset, mas->sample_rate);
443 alSourceQueueBuffers(mas->chan->source_id, 1, &bid);
445 mve_audio_buf_offset = 0;
446 mve_audio_bufl_free.pop_back();
449 if ( !mve_audio_playing ) {
453 oal_check_for_errors("mve_audio_data() end");
460 /*************************
462 *************************/
464 int mve_video_createbuf(ubyte minor, ubyte *data)
466 if (videobuf_created)
469 if ( (gr_screen.mode != GR_OPENGL) || (gr_screen.use_sections == 1) ) {
470 videobuf_created = 1;
476 w = mve_get_short(data);
477 h = mve_get_short(data+2);
482 // with Pierre's decoder16 fix in opcode 0xc, 8 should no longer be needed
483 g_vBackBuf1 = g_vBuffers = malloc(g_width * g_height * 4);
485 if (g_vBackBuf1 == NULL) {
486 mprintf(("MVE-ERROR: Can't allocate video buffer"));
487 videobuf_created = 1;
491 g_vBackBuf2 = (ushort *)g_vBackBuf1 + (g_width * g_height);
493 memset(g_vBackBuf1, 0, g_width * g_height * 4);
495 videobuf_created = 1;
500 static void mve_convert_and_draw()
504 ushort *pixels = (ushort *)g_vBackBuf1;
511 if (g_screenWidth > g_width) {
512 pDests += ((g_screenWidth - g_width) / 2) / 2;
514 if (g_screenHeight > g_height) {
515 pDests += ((g_screenHeight - g_height) / 2) * g_screenWidth;
518 for (y=0; y<g_height; y++) {
519 for (x = 0; x < g_width; x++) {
520 pDests[x] = (1<<15)|*pSrcs;
524 pDests += g_screenWidth;
528 void mve_video_display()
530 static uint mve_video_skiptimer = 0;
532 fix t1 = timer_get_fixed_seconds();
534 mve_convert_and_draw();
536 // micro_frame_delay is divided by 10 to match mve_video_skiptimer overflow catch
537 if ( mve_video_skiptimer > (uint)(micro_frame_delay/10) ) {
538 // we are running slow so subtract desired time from actual and skip this frame
539 mve_video_skiptimer -= (micro_frame_delay/10);
542 // zero out so we can get a new count
543 mve_video_skiptimer = 0;
546 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_screenWidth, g_screenHeight, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, pixelbuf);
548 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
552 fix t2 = timer_get_fixed_seconds();
554 // only get a new count if we are definitely through with old count
555 if ( mve_video_skiptimer == 0 ) {
556 // for a more accurate count convert the frame rate to a float and multiply
557 // by one-hundred-thousand before converting to an uint.
558 mve_video_skiptimer = (uint)(f2fl(t2-t1) * 100000);
562 int mve_video_init(ubyte *data)
570 if ( (gr_screen.mode != GR_OPENGL) || (gr_screen.use_sections == 1) ) {
575 width = mve_get_short(data);
576 height = mve_get_short(data+2);
578 // DDOI - Allocate RGB565 pixel buffer
579 pixelbuf = (ushort *)malloc (width * height * 2);
581 if (pixelbuf == NULL) {
582 mprintf(("MVE-ERROR: Can't allocate memory for pixelbuf"));
587 memset(pixelbuf, 0, width * height * 2);
589 g_screenWidth = width;
590 g_screenHeight = height;
592 // set height and width to a power of 2
593 tex_w = next_pow2(g_screenWidth);
594 tex_h = next_pow2(g_screenHeight);
596 SDL_assert(tex_w > 0);
597 SDL_assert(tex_h > 0);
599 glGenTextures(1, &tex);
602 mprintf(("MVE-ERROR: Can't create a GL texture"));
607 glBindTexture(GL_TEXTURE_2D, tex);
609 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
610 glDepthFunc(GL_ALWAYS);
611 glDepthMask(GL_FALSE);
612 glDisable(GL_DEPTH_TEST);
613 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
614 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
616 if ( os_config_read_uint(NULL, NOX("ScaleMovies"), 0) == 1 ) {
617 float scale_by = (float)gr_screen.max_w / (float)g_screenWidth;
619 // don't bother setting anything if we aren't going to need it
620 if (scale_by != 1.0f) {
621 glMatrixMode(GL_MODELVIEW);
625 glScalef( scale_by, scale_by, 1.0f );
632 if (mve_scale_video) {
635 // centers on 1024x768, fills on 640x480
636 x = ((gr_screen.max_w - g_screenWidth) / 2);
637 y = ((gr_screen.max_h - g_screenHeight) / 2);
642 g_coords[0].u = 0.0f;
643 g_coords[0].v = 0.0f;
646 g_coords[1].y = y + g_screenHeight;
647 g_coords[1].u = 0.0f;
648 g_coords[1].v = i2fl(g_screenHeight) / i2fl(tex_h);
650 g_coords[2].x = x + g_screenWidth;
652 g_coords[2].u = i2fl(g_screenWidth) / i2fl(tex_w);
653 g_coords[2].v = 0.0f;
655 g_coords[3].x = x + g_screenWidth;
656 g_coords[3].y = y + g_screenHeight;
657 g_coords[3].u = i2fl(g_screenWidth) / i2fl(tex_w);
658 g_coords[3].v = i2fl(g_screenHeight) / i2fl(tex_h);
660 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
661 glEnableClientState(GL_VERTEX_ARRAY);
663 glTexCoordPointer(2, GL_FLOAT, sizeof(g_coords_t), &g_coords[0].u);
664 glVertexPointer(2, GL_INT, sizeof(g_coords_t), &g_coords[0].x);
666 // NOTE: using NULL instead of pixelbuf crashes some drivers, but then so does pixelbuf so less of two evils...
667 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, NULL);
674 void mve_video_codemap(ubyte *data, int len)
680 void mve_video_data(ubyte *data, int len)
685 nFlags = mve_get_ushort(data+12);
688 temp = (ubyte *)g_vBackBuf1;
689 g_vBackBuf1 = g_vBackBuf2;
693 decodeFrame16((ubyte *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
701 void mve_init(MVESTREAM *mve)
703 // reset to default values
704 mve_audio_playing = 0;
705 mve_audio_canplay = 0;
706 mve_audio_compressed = 0;
707 mve_audio_buf_offset = 0;
708 audiobuf_created = 0;
710 videobuf_created = 0;
717 void mve_play(MVESTREAM *mve)
719 int init_timer = 0, timer_error = 0;
725 while (cont && mve_playing && !timer_error) {
726 cont = mve_play_next_chunk(mve);
728 if (micro_frame_delay && !init_timer) {
733 timer_error = mve_do_timer_wait();
737 if (key_inkey() == SDLK_ESCAPE) {
749 if (pixelbuf != NULL) {
754 if (g_vBuffers != NULL) {
759 if (gr_screen.mode == GR_OPENGL) {
761 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
762 glDisableClientState(GL_VERTEX_ARRAY);
764 glBindTexture(GL_TEXTURE_2D, 0);
765 glDeleteTextures(1, &tex);
769 if (mve_scale_video) {
770 glMatrixMode(GL_MODELVIEW);
775 glEnable(GL_DEPTH_TEST);