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 ubyte *g_pCurMap=NULL;
95 static int g_nMapLength=0;
96 static int videobuf_created;
97 static int mve_scale_video = 0;
98 static GLuint tex = 0;
105 static g_coords_t g_coords[4];
109 void decodeFrame16(ubyte *pFrame, ubyte *pMap, int mapRemain, ubyte *pData, int dataRemain);
111 /*************************
113 *************************/
119 /*************************
121 *************************/
123 int mve_timer_create(ubyte *data)
125 micro_frame_delay = mve_get_int(data) * (int)mve_get_short(data+4);
127 micro_timer_start = SDL_GetPerformanceCounter();
128 micro_timer_freq = SDL_GetPerformanceFrequency();
130 if (micro_timer_freq < 1000) {
131 micro_timer_freq = 1000;
139 static int mve_timer_get_microseconds()
142 Uint64 us = SDL_GetPerformanceCounter() - micro_timer_start;
144 if (micro_timer_freq >= 1000000) {
145 us /= (micro_timer_freq / 1000000);
147 us *= (1000000 / micro_timer_freq);
153 static void mve_timer_start(void)
158 timer_expire = mve_timer_get_microseconds();
159 timer_expire += micro_frame_delay;
164 static int mve_do_timer_wait(void)
171 tv = mve_timer_get_microseconds();
173 if (tv > timer_expire)
176 ts = timer_expire - tv;
178 SDL_Delay(ts / 1000);
180 // try and burn off excess in attempt to keep sync
182 for (int i = 0; i < 10; i++) {
187 timer_expire += micro_frame_delay;
192 static void mve_timer_stop()
198 micro_frame_delay = 0;
200 micro_timer_start = 0;
201 micro_timer_freq = 0;
204 /*************************
206 *************************/
208 // setup the audio information from the data stream
209 void mve_audio_createbuf(ubyte minor, ubyte *data)
211 if (audiobuf_created)
214 // if game sound disabled don't try and play movie audio
215 if ( !Sound_enabled ) {
216 mve_audio_canplay = 0;
217 audiobuf_created = 1;
221 int flags, desired_buffer, sample_rate;
223 mas = (mve_audio_t *) malloc ( sizeof(mve_audio_t) );
226 mve_audio_canplay = 0;
227 audiobuf_created = 1;
231 memset(mas, 0, sizeof(mve_audio_t));
233 mas->format = AL_INVALID;
235 flags = mve_get_ushort(data + 2);
236 sample_rate = mve_get_ushort(data + 4);
237 desired_buffer = mve_get_int(data + 6);
239 if (desired_buffer > 0) {
240 mve_audio_buf = (ubyte*) malloc (desired_buffer);
242 if (mve_audio_buf == NULL) {
243 mve_audio_canplay = 0;
244 audiobuf_created = 1;
248 mve_audio_buf_size = desired_buffer;
249 mve_audio_buf_offset = 0;
251 mve_audio_canplay = 0;
252 audiobuf_created = 1;
256 mas->channels = (flags & 0x0001) ? 2 : 1;
257 mas->bitsize = (flags & 0x0002) ? 16 : 8;
259 mas->sample_rate = sample_rate;
262 mve_audio_compressed = flags & 0x0004 ? 1 : 0;
264 mve_audio_compressed = 0;
267 if (mas->bitsize == 16) {
268 if (mas->channels == 2) {
269 mas->format = AL_FORMAT_STEREO16;
270 } else if (mas->channels == 1) {
271 mas->format = AL_FORMAT_MONO16;
273 } else if (mas->bitsize == 8) {
274 if (mas->channels == 2) {
275 mas->format = AL_FORMAT_STEREO8;
276 } else if (mas->channels == 1) {
277 mas->format = AL_FORMAT_MONO8;
281 // somethings wrong, bail now
282 if (mas->format == AL_INVALID) {
283 mve_audio_canplay = 0;
284 audiobuf_created = 1;
288 oal_check_for_errors("mve_audio_createbuf() begin");
290 for (int i = 0; i < MVE_AUDIO_BUFFERS; i++) {
291 alGenBuffers(1, &mas->buffers[i]);
293 if ( !mas->buffers[i] ) {
294 mve_audio_canplay = 0;
295 audiobuf_created = 1;
300 mve_audio_bufl_free.assign(mas->buffers, mas->buffers+MVE_AUDIO_BUFFERS);
302 mas->chan = oal_get_free_channel(1.0f, -1, SND_PRIORITY_MUST_PLAY);
304 if (mas->chan == NULL) {
305 mve_audio_canplay = 0;
306 audiobuf_created = 1;
310 alSourcef(mas->chan->source_id, AL_GAIN, 1.0f);
311 alSource3f(mas->chan->source_id, AL_POSITION, 0.0f, 0.0f, 0.0f);
312 alSource3f(mas->chan->source_id, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
313 alSource3f(mas->chan->source_id, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
314 alSourcef(mas->chan->source_id, AL_ROLLOFF_FACTOR, 0.0f);
315 alSourcei(mas->chan->source_id, AL_SOURCE_RELATIVE, AL_TRUE);
317 oal_check_for_errors("mve_audio_createbuf() end");
319 audiobuf_created = 1;
320 mve_audio_canplay = 1;
323 // play and stream the audio
324 void mve_audio_play()
326 if (mve_audio_canplay) {
328 ALint status = AL_INVALID;
330 oal_check_for_errors("mve_audio_play() begin");
332 alGetSourcei(mas->chan->source_id, AL_BUFFERS_QUEUED, &queued);
333 alGetSourcei(mas->chan->source_id, AL_SOURCE_STATE, &status);
335 if ( (status != AL_PLAYING) && (queued > 0) ) {
336 alSourcePlay(mas->chan->source_id);
337 mve_audio_playing = 1;
340 oal_check_for_errors("mve_audio_play() end");
344 // call this in shutdown to stop and close audio
345 static void mve_audio_stop()
347 if (!audiobuf_created)
350 oal_check_for_errors("mve_audio_stop() begin");
352 mve_audio_playing = 0;
353 mve_audio_canplay = 0;
354 mve_audio_compressed = 0;
356 audiobuf_created = 0;
358 mve_audio_bufl_free.clear();
362 alSourceStop(mas->chan->source_id);
364 // detach buffers from source so that we can delete them
365 alSourcei(mas->chan->source_id, AL_BUFFER, 0);
368 for (int i = 0; i < MVE_AUDIO_BUFFERS; i++) {
369 if ( alIsBuffer(mas->buffers[i]) ) {
370 alDeleteBuffers(1, &mas->buffers[i]);
380 if (mve_audio_buf != NULL) {
382 mve_audio_buf = NULL;
385 mve_audio_buf_size = 0;
386 mve_audio_buf_offset = 0;
388 oal_check_for_errors("mve_audio_stop() end");
391 int mve_audio_data(ubyte major, ubyte *data)
393 static const int selected_chan = 1;
399 if (mve_audio_canplay) {
400 chan = mve_get_ushort(data + 2);
401 nsamp = mve_get_ushort(data + 4);
403 if (chan & selected_chan) {
404 oal_check_for_errors("mve_audio_data() begin");
406 if ( (mve_audio_buf_offset+nsamp+4) <= mve_audio_buf_size ) {
408 if (mve_audio_compressed) {
409 /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
412 mveaudio_uncompress(mve_audio_buf+mve_audio_buf_offset, data, -1);
417 memcpy(mve_audio_buf+mve_audio_buf_offset, data, nsamp);
421 memset(mve_audio_buf+mve_audio_buf_offset, 0, nsamp);
424 mve_audio_buf_offset += nsamp;
426 mprintf(("MVE audio_buf overrun!!\n"));
429 alGetSourcei(mas->chan->source_id, AL_BUFFERS_PROCESSED, &processed);
432 alSourceUnqueueBuffers(mas->chan->source_id, 1, &bid);
434 mve_audio_bufl_free.push_back(bid);
438 if ( !mve_audio_bufl_free.empty() ) {
439 bid = mve_audio_bufl_free.back();
441 alBufferData(bid, mas->format, mve_audio_buf, mve_audio_buf_offset, mas->sample_rate);
442 alSourceQueueBuffers(mas->chan->source_id, 1, &bid);
444 mve_audio_buf_offset = 0;
445 mve_audio_bufl_free.pop_back();
448 if ( !mve_audio_playing ) {
452 oal_check_for_errors("mve_audio_data() end");
459 /*************************
461 *************************/
463 int mve_video_createbuf(ubyte minor, ubyte *data)
465 if (videobuf_created)
468 if (gr_screen.mode != GR_OPENGL) {
469 mprintf(("MVE-ERROR: Movie playback requires OpenGL renderer\n"));
470 videobuf_created = 1;
474 if (gr_screen.use_sections) {
475 mprintf(("MVE-ERROR: Bitmap sections not supported\n"));
476 videobuf_created = 1;
483 w = mve_get_short(data);
484 h = mve_get_short(data+2);
489 // with Pierre's decoder16 fix in opcode 0xc, 8 should no longer be needed
490 g_vBackBuf1 = g_vBuffers = malloc(g_width * g_height * 4);
492 if (g_vBackBuf1 == NULL) {
493 mprintf(("MVE-ERROR: Can't allocate video buffer\n"));
494 videobuf_created = 1;
498 g_vBackBuf2 = (ushort *)g_vBackBuf1 + (g_width * g_height);
500 memset(g_vBackBuf1, 0, g_width * g_height * 4);
502 // DDOI - Allocate RGB565 pixel buffer
503 pixelbuf = (ushort *)malloc (g_width * g_height * 2);
505 if (pixelbuf == NULL) {
506 mprintf(("MVE-ERROR: Can't allocate memory for pixelbuf\n"));
507 videobuf_created = 1;
511 memset(pixelbuf, 0, g_width * g_height * 2);
513 // set height and width to a power of 2
514 tex_w = next_pow2(g_width);
515 tex_h = next_pow2(g_height);
517 SDL_assert(tex_w > 0);
518 SDL_assert(tex_h > 0);
520 glGenTextures(1, &tex);
523 mprintf(("MVE-ERROR: Can't create a GL texture\n"));
524 videobuf_created = 1;
528 glBindTexture(GL_TEXTURE_2D, tex);
530 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
531 glDepthFunc(GL_ALWAYS);
532 glDepthMask(GL_FALSE);
533 glDisable(GL_DEPTH_TEST);
534 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
535 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
540 x = ((gr_screen.max_w - g_width) / 2);
541 y = ((gr_screen.max_h - g_height) / 2);
543 if ( os_config_read_uint(NULL, NOX("ScaleMovies"), 1) == 1 ) {
544 float scale_by = (float)gr_screen.max_w / (float)g_width;
546 // don't bother setting anything if we aren't going to need it
547 if (scale_by != 1.0f) {
548 glMatrixMode(GL_MODELVIEW);
552 glScalef( scale_by, scale_by, 1.0f );
556 y = ((480 - g_height) / 2);
562 g_coords[0].u = 0.0f;
563 g_coords[0].v = 0.0f;
566 g_coords[1].y = y + g_height;
567 g_coords[1].u = 0.0f;
568 g_coords[1].v = i2fl(g_height) / i2fl(tex_h);
570 g_coords[2].x = x + g_width;
572 g_coords[2].u = i2fl(g_width) / i2fl(tex_w);
573 g_coords[2].v = 0.0f;
575 g_coords[3].x = x + g_width;
576 g_coords[3].y = y + g_height;
577 g_coords[3].u = i2fl(g_width) / i2fl(tex_w);
578 g_coords[3].v = i2fl(g_height) / i2fl(tex_h);
580 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
581 glEnableClientState(GL_VERTEX_ARRAY);
583 glTexCoordPointer(2, GL_FLOAT, sizeof(g_coords_t), &g_coords[0].u);
584 glVertexPointer(2, GL_INT, sizeof(g_coords_t), &g_coords[0].x);
586 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, NULL);
588 videobuf_created = 1;
593 static void mve_convert_and_draw()
597 ushort *pixels = (ushort *)g_vBackBuf1;
604 for (y=0; y<g_height; y++) {
605 for (x = 0; x < g_width; x++) {
606 pDests[x] = (1<<15)|*pSrcs;
614 void mve_video_display()
616 static uint mve_video_skiptimer = 0;
618 fix t1 = timer_get_fixed_seconds();
620 // micro_frame_delay is divided by 10 to match mve_video_skiptimer overflow catch
621 if ( mve_video_skiptimer > (uint)(micro_frame_delay/10) ) {
622 // we are running slow so subtract desired time from actual and skip this frame
623 mve_video_skiptimer -= (micro_frame_delay/10);
626 // zero out so we can get a new count
627 mve_video_skiptimer = 0;
630 mve_convert_and_draw();
632 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_width, g_height, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, pixelbuf);
634 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
638 fix t2 = timer_get_fixed_seconds();
640 // only get a new count if we are definitely through with old count
641 if ( mve_video_skiptimer == 0 ) {
642 // for a more accurate count convert the frame rate to a float and multiply
643 // by one-hundred-thousand before converting to an uint.
644 mve_video_skiptimer = (uint)(f2fl(t2-t1) * 100000);
648 void mve_video_codemap(ubyte *data, int len)
654 void mve_video_data(ubyte *data, int len)
659 nFlags = mve_get_ushort(data+12);
662 temp = (ubyte *)g_vBackBuf1;
663 g_vBackBuf1 = g_vBackBuf2;
667 decodeFrame16((ubyte *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
675 void mve_init(MVESTREAM *mve)
677 // reset to default values
678 mve_audio_playing = 0;
679 mve_audio_canplay = 0;
680 mve_audio_compressed = 0;
681 mve_audio_buf_offset = 0;
682 audiobuf_created = 0;
684 videobuf_created = 0;
690 void mve_play(MVESTREAM *mve)
692 int init_timer = 0, timer_error = 0;
698 while (cont && mve_playing && !timer_error) {
699 cont = mve_play_next_chunk(mve);
701 if (micro_frame_delay && !init_timer) {
706 timer_error = mve_do_timer_wait();
710 if (key_inkey() == SDLK_ESCAPE) {
722 if (pixelbuf != NULL) {
727 if (g_vBuffers != NULL) {
732 if (gr_screen.mode == GR_OPENGL) {
734 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
735 glDisableClientState(GL_VERTEX_ARRAY);
737 glBindTexture(GL_TEXTURE_2D, 0);
738 glDeleteTextures(1, &tex);
742 if (mve_scale_video) {
743 glMatrixMode(GL_MODELVIEW);
748 glEnable(GL_DEPTH_TEST);