Skip to content

Commit 7b52ab3

Browse files
author
dashodanger
committed
Go back to synchronous MPEG decode
1 parent 811316d commit 7b52ab3

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

source_files/edge/i_movie.cc

+34-31
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
extern int sound_device_frequency;
3838

3939
bool playing_movie = false;
40-
static bool need_canvas_update = false;
4140
static bool skip_bar_active;
4241
static GLuint canvas = 0;
4342
static uint8_t *rgb_data = nullptr;
@@ -57,15 +56,42 @@ static float tx2 = 1.0f;
5756
static float ty1 = 0.0f;
5857
static float ty2 = 1.0f;
5958
static double last_time = 0;
60-
static plm_frame_t *movie_frame = nullptr;
61-
static plm_samples_t *movie_samples = nullptr;
6259

6360
static bool MovieSetupAudioStream(int rate)
6461
{
6562
plm_set_audio_lead_time(decoder, (double)1024 / (double)rate);
63+
PauseMusic();
64+
SoundQueueStop();
65+
SoundQueueInitialize();
6666
return true;
6767
}
6868

69+
void MovieAudioCallback(plm_t *mpeg, plm_samples_t *samples, void *user)
70+
{
71+
(void)mpeg;
72+
(void)user;
73+
if (samples)
74+
{
75+
SoundData *movie_buf = SoundQueueGetFreeBuffer(PLM_AUDIO_SAMPLES_PER_FRAME);
76+
if (movie_buf)
77+
{
78+
movie_buf->length_ = PLM_AUDIO_SAMPLES_PER_FRAME;
79+
memcpy(movie_buf->data_, samples->interleaved, PLM_AUDIO_SAMPLES_PER_FRAME * 2 * sizeof(int16_t));
80+
SoundQueueAddBuffer(movie_buf, movie_sample_rate);
81+
}
82+
}
83+
}
84+
85+
void MovieVideoCallback(plm_t *mpeg, plm_frame_t *frame, void *user)
86+
{
87+
(void)mpeg;
88+
(void)user;
89+
90+
plm_frame_to_rgb(frame, rgb_data, frame->width * 3);
91+
glBindTexture(GL_TEXTURE_2D, canvas);
92+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame->width, frame->height, 0, GL_RGB, GL_UNSIGNED_BYTE, rgb_data);
93+
}
94+
6995
void PlayMovie(const std::string &name)
7096
{
7197
MovieDefinition *movie = moviedefs.Lookup(name.c_str());
@@ -191,6 +217,8 @@ void PlayMovie(const std::string &name)
191217
int num_pixels = movie_width * movie_height * 3;
192218
rgb_data = new uint8_t[num_pixels];
193219
memset(rgb_data, 0, num_pixels);
220+
plm_set_video_decode_callback(decoder, MovieVideoCallback, nullptr);
221+
plm_set_audio_decode_callback(decoder, MovieAudioCallback, nullptr);
194222
if (!no_sound)
195223
{
196224
plm_set_audio_enabled(decoder, 1);
@@ -209,8 +237,6 @@ void PlayMovie(const std::string &name)
209237
static void EndMovie()
210238
{
211239
plm_destroy(decoder);
212-
movie_frame = nullptr;
213-
movie_samples = nullptr;
214240
decoder = nullptr;
215241
delete[] movie_bytes;
216242
movie_bytes = nullptr;
@@ -224,23 +250,15 @@ static void EndMovie()
224250
glDeleteTextures(1, &canvas);
225251
canvas = 0;
226252
}
253+
ResumeMusic();
227254
}
228255

229256
void MovieDrawer()
230257
{
231258
if (!playing_movie)
232259
return;
233-
if (!plm_has_ended(decoder) && movie_frame)
260+
if (!plm_has_ended(decoder))
234261
{
235-
if (need_canvas_update)
236-
{
237-
plm_frame_to_rgb(movie_frame, rgb_data, movie_frame->width * 3);
238-
glBindTexture(GL_TEXTURE_2D, canvas);
239-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, movie_frame->width, movie_frame->height, 0, GL_RGB, GL_UNSIGNED_BYTE,
240-
rgb_data);
241-
need_canvas_update = false;
242-
}
243-
244262
SetupMatrices2D();
245263

246264
glEnable(GL_TEXTURE_2D);
@@ -397,22 +415,7 @@ void MovieTicker()
397415
elapsed_time = 1.0 / 30.0;
398416
last_time = current_time;
399417

400-
movie_frame = plm_decode_video(decoder);
401-
if (movie_frame)
402-
need_canvas_update = true;
403-
movie_samples = plm_decode_audio(decoder);
404-
SoundData *movie_buf = SoundQueueGetFreeBuffer(PLM_AUDIO_SAMPLES_PER_FRAME);
405-
if (movie_buf)
406-
{
407-
movie_buf->length_ = PLM_AUDIO_SAMPLES_PER_FRAME;
408-
if (movie_samples)
409-
{
410-
memcpy(movie_buf->data_, movie_samples->interleaved, PLM_AUDIO_SAMPLES_PER_FRAME * 2 * sizeof(int16_t));
411-
SoundQueueAddBuffer(movie_buf, movie_sample_rate);
412-
}
413-
else
414-
SoundQueueReturnBuffer(movie_buf);
415-
}
418+
plm_decode(decoder, elapsed_time);
416419

417420
if (skip_bar_active)
418421
{

0 commit comments

Comments
 (0)