Switch from FFmpeg av_opt_set_* to swr_alloc_set_opts2 (#18138)

ffmpeg 7 doesn't allow to set in_channel_count and out_channel_count anymore
This commit is contained in:
trautamaki
2026-02-03 09:00:48 +02:00
committed by GitHub
parent 8ba48932d3
commit f1e679d981
+12 -18
View File
@@ -575,34 +575,28 @@ namespace utils
return; return;
} }
// Prepare resampler
av.swr = swr_alloc();
if (!av.swr)
{
media_log.error("audio_decoder: Failed to allocate resampler for stream #%u in file '%s'", stream_index, path);
has_error = true;
return;
}
const int dst_channels = 2; const int dst_channels = 2;
const AVChannelLayout dst_channel_layout = AV_CHANNEL_LAYOUT_STEREO; const AVChannelLayout dst_channel_layout = AV_CHANNEL_LAYOUT_STEREO;
const AVSampleFormat dst_format = AV_SAMPLE_FMT_FLT; const AVSampleFormat dst_format = AV_SAMPLE_FMT_FLT;
int set_err = 0; const int set_err = swr_alloc_set_opts2(&av.swr, &dst_channel_layout, dst_format,
if ((set_err = av_opt_set_int(av.swr, "in_channel_count", stream->codecpar->ch_layout.nb_channels, 0)) || sample_rate, &stream->codecpar->ch_layout,
(set_err = av_opt_set_int(av.swr, "out_channel_count", dst_channels, 0)) || static_cast<AVSampleFormat>(stream->codecpar->format),
(set_err = av_opt_set_chlayout(av.swr, "in_channel_layout", &stream->codecpar->ch_layout, 0)) || stream->codecpar->sample_rate, 0, nullptr);
(set_err = av_opt_set_chlayout(av.swr, "out_channel_layout", &dst_channel_layout, 0)) || if (set_err < 0)
(set_err = av_opt_set_int(av.swr, "in_sample_rate", stream->codecpar->sample_rate, 0)) ||
(set_err = av_opt_set_int(av.swr, "out_sample_rate", sample_rate, 0)) ||
(set_err = av_opt_set_sample_fmt(av.swr, "in_sample_fmt", static_cast<AVSampleFormat>(stream->codecpar->format), 0)) ||
(set_err = av_opt_set_sample_fmt(av.swr, "out_sample_fmt", dst_format, 0)))
{ {
media_log.error("audio_decoder: Failed to set resampler options: Error: %d='%s'", set_err, av_error_to_string(set_err)); media_log.error("audio_decoder: Failed to set resampler options: Error: %d='%s'", set_err, av_error_to_string(set_err));
has_error = true; has_error = true;
return; return;
} }
if (!av.swr)
{
media_log.error("audio_decoder: Failed to allocate resampler for stream #%u in file '%s'", stream_index, path);
has_error = true;
return;
}
if (int err = swr_init(av.swr); err < 0 || !swr_is_initialized(av.swr)) if (int err = swr_init(av.swr); err < 0 || !swr_is_initialized(av.swr))
{ {
media_log.error("audio_decoder: Resampler has not been properly initialized: %d='%s'", err, av_error_to_string(err)); media_log.error("audio_decoder: Resampler has not been properly initialized: %d='%s'", err, av_error_to_string(err));