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.
This commit is contained in:
zhupengfei 2020-02-01 11:28:57 +08:00
parent 17461b5d11
commit 8b9c01ded9
No known key found for this signature in database
GPG Key ID: DD129E108BD09378

View File

@ -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;