From 8b9c01ded97e752790430fa86e113c25665daafa Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Sat, 1 Feb 2020 11:28:57 +0800 Subject: [PATCH] ffmpeg: Correctly handle sample format We previously assumed that the first preferred sample format is planar, but that may not be true for all codecs. Instead we should find a supported sample format that is planar. --- src/core/dumping/ffmpeg_backend.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/dumping/ffmpeg_backend.cpp b/src/core/dumping/ffmpeg_backend.cpp index f3c3e236a9..ef7843ffca 100644 --- a/src/core/dumping/ffmpeg_backend.cpp +++ b/src/core/dumping/ffmpeg_backend.cpp @@ -224,7 +224,25 @@ bool FFmpegAudioStream::Init(AVFormatContext* format_context) { // Configure audio codec context codec_context->codec_type = AVMEDIA_TYPE_AUDIO; codec_context->bit_rate = Settings::values.audio_bitrate; - codec_context->sample_fmt = codec->sample_fmts[0]; + if (codec->sample_fmts) { + codec_context->sample_fmt = AV_SAMPLE_FMT_NONE; + // Use any planar format + const AVSampleFormat* ptr = codec->sample_fmts; + while ((*ptr) != -1) { + if (av_sample_fmt_is_planar((*ptr))) { + codec_context->sample_fmt = (*ptr); + break; + } + ptr++; + } + if (codec_context->sample_fmt == AV_SAMPLE_FMT_NONE) { + LOG_ERROR(Render, "Specified audio encoder does not support any planar format"); + return false; + } + } else { + codec_context->sample_fmt = AV_SAMPLE_FMT_S16P; + } + codec_context->sample_rate = AudioCore::native_sample_rate; codec_context->channel_layout = AV_CH_LAYOUT_STEREO; codec_context->channels = 2;