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"
49 static int mve_playing;
53 static int micro_frame_delay = 0;
54 static int timer_started = 0;
55 static int timer_created = 0;
56 static unsigned int timer_expire;
57 static Uint64 micro_timer_start = 0;
58 static Uint64 micro_timer_freq = 0;
61 #define MVE_AUDIO_BUFFERS 8 // total buffers to interact with stream
63 static std::vector<ALuint> mve_audio_bufl_free;
64 static ubyte *mve_audio_buf = NULL;
65 static size_t mve_audio_buf_size = 0;
66 static size_t mve_audio_buf_offset = 0;
68 static int mve_audio_playing = 0;
69 static int mve_audio_canplay = 0;
70 static int mve_audio_compressed = 0;
71 static int audiobuf_created = 0;
73 // struct for the audio stream information
81 ALuint buffers[MVE_AUDIO_BUFFERS];
84 mve_audio_t *mas = NULL; // mve_audio_stream
89 int g_width, g_height;
90 void *g_vBuffers = NULL;
91 void *g_vBackBuf1, *g_vBackBuf2;
92 ushort *pixelbuf = NULL;
93 static ubyte *g_pCurMap=NULL;
94 static int g_nMapLength=0;
95 static int videobuf_created;
99 void decodeFrame16(ubyte *pFrame, ubyte *pMap, int mapRemain, ubyte *pData, int dataRemain);
101 /*************************
103 *************************/
109 /*************************
111 *************************/
113 int mve_timer_create(ubyte *data)
115 micro_frame_delay = mve_get_int(data) * (int)mve_get_short(data+4);
117 micro_timer_start = SDL_GetPerformanceCounter();
118 micro_timer_freq = SDL_GetPerformanceFrequency();
125 static unsigned int mve_timer_get_microseconds()
127 Uint64 us = SDL_GetPerformanceCounter() - micro_timer_start;
130 us /= micro_timer_freq;
132 return (unsigned int)us;
135 static void mve_timer_start(void)
140 timer_expire = mve_timer_get_microseconds();
141 timer_expire += micro_frame_delay;
146 static int mve_do_timer_wait(void)
153 tv = mve_timer_get_microseconds();
155 if (tv > timer_expire)
158 ts = timer_expire - tv;
160 SDL_Delay(ts / 1000);
163 timer_expire += micro_frame_delay;
168 static void mve_timer_stop()
174 micro_frame_delay = 0;
176 micro_timer_start = 0;
177 micro_timer_freq = 0;
180 /*************************
182 *************************/
184 // setup the audio information from the data stream
185 void mve_audio_createbuf(ubyte minor, ubyte *data)
187 if (audiobuf_created)
190 // if game sound disabled don't try and play movie audio
191 if ( !Sound_enabled ) {
192 mve_audio_canplay = 0;
193 audiobuf_created = 1;
197 int flags, desired_buffer, sample_rate;
199 mas = (mve_audio_t *) malloc ( sizeof(mve_audio_t) );
202 mve_audio_canplay = 0;
203 audiobuf_created = 1;
207 memset(mas, 0, sizeof(mve_audio_t));
209 mas->format = AL_INVALID;
211 flags = mve_get_ushort(data + 2);
212 sample_rate = mve_get_ushort(data + 4);
213 desired_buffer = mve_get_int(data + 6);
215 if (desired_buffer > 0) {
216 mve_audio_buf = (ubyte*) malloc (desired_buffer);
218 if (mve_audio_buf == NULL) {
219 mve_audio_canplay = 0;
220 audiobuf_created = 1;
224 mve_audio_buf_size = desired_buffer;
225 mve_audio_buf_offset = 0;
227 mve_audio_canplay = 0;
228 audiobuf_created = 1;
232 mas->channels = (flags & 0x0001) ? 2 : 1;
233 mas->bitsize = (flags & 0x0002) ? 16 : 8;
235 mas->sample_rate = sample_rate;
238 mve_audio_compressed = flags & 0x0004 ? 1 : 0;
240 mve_audio_compressed = 0;
243 if (mas->bitsize == 16) {
244 if (mas->channels == 2) {
245 mas->format = AL_FORMAT_STEREO16;
246 } else if (mas->channels == 1) {
247 mas->format = AL_FORMAT_MONO16;
249 } else if (mas->bitsize == 8) {
250 if (mas->channels == 2) {
251 mas->format = AL_FORMAT_STEREO8;
252 } else if (mas->channels == 1) {
253 mas->format = AL_FORMAT_MONO8;
257 // somethings wrong, bail now
258 if (mas->format == AL_INVALID) {
259 mve_audio_canplay = 0;
260 audiobuf_created = 1;
264 oal_check_for_errors("mve_audio_createbuf() begin");
266 for (int i = 0; i < MVE_AUDIO_BUFFERS; i++) {
267 alGenBuffers(1, &mas->buffers[i]);
269 if ( !mas->buffers[i] ) {
270 mve_audio_canplay = 0;
271 audiobuf_created = 1;
276 mve_audio_bufl_free.assign(mas->buffers, mas->buffers+MVE_AUDIO_BUFFERS);
278 mas->chan = oal_get_free_channel(1.0f, -1, SND_PRIORITY_MUST_PLAY);
280 if (mas->chan == NULL) {
281 mve_audio_canplay = 0;
282 audiobuf_created = 1;
286 alSourcef(mas->chan->source_id, AL_GAIN, 1.0f);
287 alSource3f(mas->chan->source_id, AL_POSITION, 0.0f, 0.0f, 0.0f);
288 alSource3f(mas->chan->source_id, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
289 alSource3f(mas->chan->source_id, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
290 alSourcef(mas->chan->source_id, AL_ROLLOFF_FACTOR, 0.0f);
291 alSourcei(mas->chan->source_id, AL_SOURCE_RELATIVE, AL_TRUE);
292 alSourcei(mas->chan->source_id, AL_LOOPING, AL_FALSE);
294 oal_check_for_errors("mve_audio_createbuf() end");
296 audiobuf_created = 1;
297 mve_audio_canplay = 1;
300 // play and stream the audio
301 void mve_audio_play()
303 if (mve_audio_canplay) {
305 ALint status = AL_INVALID;
307 oal_check_for_errors("mve_audio_play() begin");
309 alGetSourcei(mas->chan->source_id, AL_BUFFERS_QUEUED, &queued);
310 alGetSourcei(mas->chan->source_id, AL_SOURCE_STATE, &status);
312 if ( (status != AL_PLAYING) && (queued > 0) ) {
313 alSourcePlay(mas->chan->source_id);
314 mve_audio_playing = 1;
317 oal_check_for_errors("mve_audio_play() end");
321 // call this in shutdown to stop and close audio
322 static void mve_audio_stop()
324 if (!audiobuf_created)
327 oal_check_for_errors("mve_audio_stop() begin");
329 mve_audio_playing = 0;
330 mve_audio_canplay = 0;
331 mve_audio_compressed = 0;
333 audiobuf_created = 0;
335 mve_audio_bufl_free.clear();
339 alSourceStop(mas->chan->source_id);
341 // detach buffers from source so that we can delete them
342 alSourcei(mas->chan->source_id, AL_BUFFER, 0);
345 for (int i = 0; i < MVE_AUDIO_BUFFERS; i++) {
346 if ( alIsBuffer(mas->buffers[i]) ) {
347 alDeleteBuffers(1, &mas->buffers[i]);
357 if (mve_audio_buf != NULL) {
359 mve_audio_buf = NULL;
362 mve_audio_buf_size = 0;
363 mve_audio_buf_offset = 0;
365 oal_check_for_errors("mve_audio_stop() end");
368 int mve_audio_data(ubyte major, ubyte *data)
370 static const int selected_chan = 1;
376 if (mve_audio_canplay) {
377 chan = mve_get_ushort(data + 2);
378 nsamp = mve_get_ushort(data + 4);
380 if (chan & selected_chan) {
381 oal_check_for_errors("mve_audio_data() begin");
383 if ( (mve_audio_buf_offset+nsamp+4) <= mve_audio_buf_size ) {
385 if (mve_audio_compressed) {
386 /* HACK: +4 mveaudio_uncompress adds 4 more bytes */
389 mveaudio_uncompress(mve_audio_buf+mve_audio_buf_offset, data, -1);
394 memcpy(mve_audio_buf+mve_audio_buf_offset, data, nsamp);
398 memset(mve_audio_buf+mve_audio_buf_offset, 0, nsamp);
401 mve_audio_buf_offset += nsamp;
403 mprintf(("MVE audio_buf overrun!!\n"));
406 alGetSourcei(mas->chan->source_id, AL_BUFFERS_PROCESSED, &processed);
409 alSourceUnqueueBuffers(mas->chan->source_id, 1, &bid);
411 mve_audio_bufl_free.push_back(bid);
415 if ( !mve_audio_bufl_free.empty() ) {
416 bid = mve_audio_bufl_free.back();
418 alBufferData(bid, mas->format, mve_audio_buf, mve_audio_buf_offset, mas->sample_rate);
419 alSourceQueueBuffers(mas->chan->source_id, 1, &bid);
421 mve_audio_buf_offset = 0;
422 mve_audio_bufl_free.pop_back();
425 if ( !mve_audio_playing ) {
429 oal_check_for_errors("mve_audio_data() end");
436 /*************************
438 *************************/
440 int mve_video_createbuf(ubyte minor, ubyte *data)
442 if (videobuf_created)
447 w = mve_get_short(data);
448 h = mve_get_short(data+2);
453 // with Pierre's decoder16 fix in opcode 0xc, 8 should no longer be needed
454 g_vBackBuf1 = g_vBuffers = malloc(g_width * g_height * 4);
456 if (g_vBackBuf1 == NULL) {
457 mprintf(("MVE-ERROR: Can't allocate video buffer\n"));
458 videobuf_created = 1;
462 g_vBackBuf2 = (ushort *)g_vBackBuf1 + (g_width * g_height);
464 memset(g_vBackBuf1, 0, g_width * g_height * 4);
466 // DDOI - Allocate RGB565 pixel buffer
467 pixelbuf = (ushort *)malloc (g_width * g_height * 2);
469 if (pixelbuf == NULL) {
470 mprintf(("MVE-ERROR: Can't allocate memory for pixelbuf\n"));
471 videobuf_created = 1;
475 memset(pixelbuf, 0, g_width * g_height * 2);
477 gr_stream_start(-1, -1, g_width, g_height);
479 videobuf_created = 1;
484 static void mve_convert_and_draw()
488 ushort *pixels = (ushort *)g_vBackBuf1;
497 for (y=0; y<g_height; y++) {
498 for (x = 0; x < g_width; x++) {
499 // convert from abgr to rgba
502 r = ubyte((px & 0x7C00) >> 10);
503 g = ubyte((px & 0x3E0) >> 5);
504 b = ubyte((px & 0x1F) >> 0);
505 a = ubyte((px & 0x8000) >> 15);
507 pDests[x] = (r << 11) | (g << 6) | (b << 1) | (a << 0);
515 void mve_video_display()
517 static uint mve_video_skiptimer = 0;
519 fix t1 = timer_get_fixed_seconds();
521 // micro_frame_delay is divided by 10 to match mve_video_skiptimer overflow catch
522 if ( mve_video_skiptimer > (uint)(micro_frame_delay/10) ) {
523 // we are running slow so subtract desired time from actual and skip this frame
524 mve_video_skiptimer -= (micro_frame_delay/10);
527 // zero out so we can get a new count
528 mve_video_skiptimer = 0;
531 mve_convert_and_draw();
533 gr_stream_frame( (ubyte*)pixelbuf );
537 fix t2 = timer_get_fixed_seconds();
539 // only get a new count if we are definitely through with old count
540 if ( mve_video_skiptimer == 0 ) {
541 // for a more accurate count convert the frame rate to a float and multiply
542 // by one-hundred-thousand before converting to an uint.
543 mve_video_skiptimer = (uint)(f2fl(t2-t1) * 100000);
547 void mve_video_codemap(ubyte *data, int len)
553 void mve_video_data(ubyte *data, int len)
558 nFlags = mve_get_ushort(data+12);
561 temp = (ubyte *)g_vBackBuf1;
562 g_vBackBuf1 = g_vBackBuf2;
566 decodeFrame16((ubyte *)g_vBackBuf1, g_pCurMap, g_nMapLength, data+14, len-14);
574 void mve_init(MVESTREAM *mve)
576 // reset to default values
577 mve_audio_playing = 0;
578 mve_audio_canplay = 0;
579 mve_audio_compressed = 0;
580 mve_audio_buf_offset = 0;
581 audiobuf_created = 0;
583 videobuf_created = 0;
588 void mve_play(MVESTREAM *mve)
590 int init_timer = 0, timer_error = 0;
596 while (cont && mve_playing && !timer_error) {
597 cont = mve_play_next_chunk(mve);
599 if (micro_frame_delay && !init_timer) {
604 timer_error = mve_do_timer_wait();
608 if (key_inkey() == SDLK_ESCAPE) {
620 if (pixelbuf != NULL) {
625 if (g_vBuffers != NULL) {