Skip to content

Commit

Permalink
Fix crash in declick link
Browse files Browse the repository at this point in the history
A crash would occur if there were not enough future frames available.
Fix the crash by returning unprocessed audio.
Also increase the number of future frames.

A 1.5x window size factor was determined emiprically.
  • Loading branch information
bmatherly committed Sep 4, 2023
1 parent 1d3669e commit 54d3499
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/modules/avformat/link_avfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ static int future_frames_needed(mlt_link self)
int future_frames = 0;

if (strcmp(pdata->avfilter->name, "adeclick") == 0) {
int windowms = mlt_properties_get_int(MLT_LINK_PROPERTIES(self), "window");
int windowms = mlt_properties_get_int(MLT_LINK_PROPERTIES(self), "av.window");
if (windowms <= 0) {
// Default value is 100
windowms = 100;
}
double fps = mlt_profile_fps(mlt_service_profile(MLT_LINK_SERVICE(self)));
future_frames = ceil(fps * windowms / 1000);
// Provide future frames for 1.5x window duration (determined empirically)
future_frames = ceil(1.5 * fps * windowms / 1000);
}

return future_frames;
Expand Down Expand Up @@ -730,23 +731,23 @@ static int link_get_audio(mlt_frame frame,
in.samples = mlt_audio_calculate_frame_samples(mlt_producer_get_fps(MLT_LINK_PRODUCER(self)),
in.frequency,
mlt_frame_get_position(src_frame));
int error = mlt_frame_get_audio(src_frame,
&in.data,
&in.format,
&in.frequency,
&in.channels,
&in.samples);
error = mlt_frame_get_audio(src_frame,
&in.data,
&in.format,
&in.frequency,
&in.channels,
&in.samples);
if (error || in.format != *format || in.frequency != *frequency
|| in.channels != *channels) {
// Error situation. Do not attempt to process.
mlt_audio_get_values(&in, buffer, frequency, format, samples, channels);
mlt_log_error(MLT_LINK_SERVICE(self),
"Invalid Return: E: %d %dS - %dHz %dC %s\n",
error,
in.samples,
in.frequency,
in.channels,
mlt_audio_format_name(in.format));
error = 1;
break;
}

Expand Down Expand Up @@ -826,14 +827,21 @@ static int link_get_audio(mlt_frame frame,
memcpy((uint8_t *) *buffer, pdata->avoutframe->extended_data[0], bufsize);
}
mlt_frame_set_audio(frame, *buffer, *format, bufsize, mlt_pool_release);
// Clear the audio stack in case get_audio was not called on this frame.
while (mlt_frame_pop_audio(frame)) {
};
break;
}

av_frame_unref(pdata->avinframe);
av_frame_unref(pdata->avoutframe);

if (error) {
// Return unprocessed audio if an error occurs
error = mlt_frame_get_audio(frame, buffer, format, frequency, channels, samples);
} else {
// Clear the audio stack in case get_audio was not called on this frame.
while (mlt_frame_pop_audio(frame)) {
};
}

mlt_service_unlock(MLT_LINK_SERVICE(self));
return error;
}
Expand Down

0 comments on commit 54d3499

Please sign in to comment.