From dfb6055ae42ffce9130f74c13719913b8f9bcebb Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Fri, 7 Apr 2023 00:58:28 -0700 Subject: [PATCH] FFmpeg: Force lower sample rate for codecs not supporting high rates (fixes #2869) --- CHANGES | 1 + src/feature/ffmpeg/ffmpeg-encoder.c | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 041f3f4ce..03d763dcc 100644 --- a/CHANGES +++ b/CHANGES @@ -17,6 +17,7 @@ Emulation fixes: - GBA Video: Disable BG target 1 blending when OBJ blending (fixes mgba.io/i/2722) Other fixes: - Core: Allow sending thread requests to a crashed core (fixes mgba.io/i/2784) + - FFmpeg: Force lower sample rate for codecs not supporting high rates (fixes mgba.io/i/2869) - Qt: Fix crash when attempting to use OpenGL 2.1 to 3.1 (fixes mgba.io/i/2794) - Qt: Disable sync while running scripts from main thread (fixes mgba.io/i/2738) - Qt: Fix savestate preview sizes with different scales (fixes mgba.io/i/2560) diff --git a/src/feature/ffmpeg/ffmpeg-encoder.c b/src/feature/ffmpeg/ffmpeg-encoder.c index 88f4f78c2..9d61e5846 100644 --- a/src/feature/ffmpeg/ffmpeg-encoder.c +++ b/src/feature/ffmpeg/ffmpeg-encoder.c @@ -154,19 +154,35 @@ bool FFmpegEncoderSetAudio(struct FFmpegEncoder* encoder, const char* acodec, un } encoder->sampleRate = encoder->isampleRate; if (codec->supported_samplerates) { + bool gotSampleRate = false; + int highestSampleRate = 0; for (i = 0; codec->supported_samplerates[i]; ++i) { + if (codec->supported_samplerates[i] > highestSampleRate) { + highestSampleRate = codec->supported_samplerates[i]; + } if (codec->supported_samplerates[i] < encoder->isampleRate) { continue; } - if (encoder->sampleRate == encoder->isampleRate || encoder->sampleRate > codec->supported_samplerates[i]) { + if (!gotSampleRate || encoder->sampleRate > codec->supported_samplerates[i]) { encoder->sampleRate = codec->supported_samplerates[i]; + gotSampleRate = true; } } + if (!gotSampleRate) { + // There are no available sample rates that are higher than the input sample rate + // Let's use the highest available instead + encoder->sampleRate = highestSampleRate; + } } else if (codec->id == AV_CODEC_ID_FLAC) { // HACK: FLAC doesn't support > 65535Hz unless it's divisible by 10 if (encoder->sampleRate >= 65535) { encoder->sampleRate -= encoder->isampleRate % 10; } + } else if (codec->id == AV_CODEC_ID_VORBIS) { + // HACK: FLAC doesn't support > 48000Hz but doesn't tell us + if (encoder->sampleRate > 48000) { + encoder->sampleRate = 48000; + } } else if (codec->id == AV_CODEC_ID_AAC) { // HACK: AAC doesn't support 32768Hz (it rounds to 32000), but libfaac doesn't tell us that encoder->sampleRate = 48000;