From 9655d5598c87dcb22c54a83cc7741b77cb47a1b0 Mon Sep 17 00:00:00 2001 From: Thomas A Date: Mon, 1 Jul 2024 09:59:11 -0700 Subject: [PATCH 1/2] Fix Building Against Newer Version Of FFmpeg When building against a newer version: * Use `ch_layout.order` & `ch_layout.nb_channels` instead of `channel_layout` & `channels`. * Workaround `read_probe` no longer being exposed by calling `av_probe_input_format` --- .../AudioFileFormatGeneric.cpp | 12 ++++--- .../AudioToolbox/AudioConverterImpl.cpp | 34 ++++++++++++++++--- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/CoreAudio/AFAVFormatComponent/AudioFileFormatGeneric.cpp b/src/CoreAudio/AFAVFormatComponent/AudioFileFormatGeneric.cpp index 27fe33d86..2ea19f686 100644 --- a/src/CoreAudio/AFAVFormatComponent/AudioFileFormatGeneric.cpp +++ b/src/CoreAudio/AFAVFormatComponent/AudioFileFormatGeneric.cpp @@ -81,10 +81,6 @@ void AudioFileFormatGeneric::GetFileTypeName(CFStringRef *outName) UncertainResult AudioFileFormatGeneric::FileDataIsThisFormat(UInt32 inDataByteSize, const void* inData) { - const AVInputFormat* fmt = av_find_input_format(m_avformatShortName); - if (!fmt) - return false; - std::vector buf; AVProbeData probeData; @@ -96,7 +92,15 @@ UncertainResult AudioFileFormatGeneric::FileDataIsThisFormat(UInt32 inDataByteSi probeData.buf = buf.data(); probeData.buf_size = inDataByteSize; +#warning "TODO: Remove old `read_probe` call once we no longer support older distros" +#if LIBAVCODEC_VERSION_MAJOR >= 61 + return av_probe_input_format(&probeData, false) != nullptr ? kTrue : kFalse; +#else + const AVInputFormat* fmt = av_find_input_format(m_avformatShortName); + if (!fmt) + return false; return fmt->read_probe(&probeData) ? kTrue : kFalse; +#endif } AudioFileObject* AudioFileFormatGeneric::New() diff --git a/src/CoreAudio/AudioToolbox/AudioConverterImpl.cpp b/src/CoreAudio/AudioToolbox/AudioConverterImpl.cpp index fec2bf2b1..5111e8aec 100644 --- a/src/CoreAudio/AudioToolbox/AudioConverterImpl.cpp +++ b/src/CoreAudio/AudioToolbox/AudioConverterImpl.cpp @@ -100,10 +100,19 @@ OSStatus AudioConverter::create(const AudioStreamBasicDescription* inSourceForma if (inSourceFormat->mFormatID == kAudioFormatLinearPCM) { +#warning "TODO: Remove deprecated 'channels' once we no longer support older distros" +#if LIBAVCODEC_VERSION_MAJOR >= 61 + cIn->ch_layout.nb_channels = inSourceFormat->mChannelsPerFrame; +#else cIn->channels = inSourceFormat->mChannelsPerFrame; +#endif cIn->sample_rate = inSourceFormat->mSampleRate; - + +#if LIBAVCODEC_VERSION_MAJOR >= 61 + std::cout << "Converting from PCM with " << cIn->ch_layout.nb_channels << " channels at " << cIn->sample_rate << " Hz\n"; +#else std::cout << "Converting from PCM with " << cIn->channels << " channels at " << cIn->sample_rate << " Hz\n"; +#endif } if (avcodec_open2((*out)->m_decoder, codecIn, nullptr) < 0) @@ -131,9 +140,15 @@ void AudioConverter::initEncoder() m_encoder->codec_type = AVMEDIA_TYPE_AUDIO; m_encoder->bit_rate = m_outBitRate; +#warning "TODO: Remove deprecated 'channels' once we no longer support older distros" +#if LIBAVCODEC_VERSION_MAJOR >= 61 + m_encoder->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + m_encoder->ch_layout.nb_channels = m_destinationFormat.mChannelsPerFrame; +#else m_encoder->channels = m_destinationFormat.mChannelsPerFrame; - m_encoder->sample_rate = m_destinationFormat.mSampleRate; m_encoder->channel_layout = CAChannelCountToLayout(m_destinationFormat.mChannelsPerFrame); +#endif + m_encoder->sample_rate = m_destinationFormat.mSampleRate; m_encoder->sample_fmt = CACodecSampleFormat(&m_destinationFormat); #ifdef DEBUG_AUDIOCONVERTER @@ -161,10 +176,16 @@ void AudioConverter::allocateBuffers() m_audioFrame->nb_samples = ENCODER_FRAME_SAMPLES; m_audioFrame->format = m_encoder->sample_fmt; - m_audioFrame->channel_layout = m_encoder->channel_layout; - +#warning "TODO: Remove deprecated 'channels' once we no longer support older distros" +#if LIBAVCODEC_VERSION_MAJOR >= 61 + m_audioFrame->ch_layout.order = m_encoder->ch_layout.order; + m_audioFrame->ch_layout.nb_channels = m_encoder->ch_layout.nb_channels; + int audioSampleBuffer_size = av_samples_get_buffer_size(nullptr, m_encoder->ch_layout.nb_channels, m_audioFrame->nb_samples, m_encoder->sample_fmt, 0); +#else + m_audioFrame->channel_layout = m_encoder->channel_layout; int audioSampleBuffer_size = av_samples_get_buffer_size(nullptr, m_encoder->channels, m_audioFrame->nb_samples, m_encoder->sample_fmt, 0); +#endif void* audioSampleBuffer = (uint8_t*) av_malloc(audioSampleBuffer_size); if (!audioSampleBuffer) @@ -174,8 +195,13 @@ void AudioConverter::allocateBuffers() } // Setup the data pointers in the AVFrame +#if LIBAVCODEC_VERSION_MAJOR >= 61 + if (int err = avcodec_fill_audio_frame(m_audioFrame, m_encoder->ch_layout.nb_channels, m_encoder->sample_fmt, + (const uint8_t*) audioSampleBuffer, audioSampleBuffer_size, 0 ); err < 0) +#else if (int err = avcodec_fill_audio_frame(m_audioFrame, m_encoder->channels, m_encoder->sample_fmt, (const uint8_t*) audioSampleBuffer, audioSampleBuffer_size, 0 ); err < 0) +#endif { std::cerr << "AudioConverter::allocateBuffers(): Could not set up audio frame\n"; throw std::runtime_error("AudioConverter::allocateBuffers(): Could not set up audio frame"); From 0bcc5815548463929c2b1619ca8bfbbd8d1df5c6 Mon Sep 17 00:00:00 2001 From: Thomas A Date: Sun, 18 Aug 2024 18:12:12 -0700 Subject: [PATCH 2/2] Always Call `av_probe_input_format` Fortunately, the FFmpeg package in Ubuntu 20.04 also has the `av_probe_input_format` method. Therefore, we can remove the old code. --- .../AFAVFormatComponent/AudioFileFormatGeneric.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/CoreAudio/AFAVFormatComponent/AudioFileFormatGeneric.cpp b/src/CoreAudio/AFAVFormatComponent/AudioFileFormatGeneric.cpp index 2ea19f686..8dee3696e 100644 --- a/src/CoreAudio/AFAVFormatComponent/AudioFileFormatGeneric.cpp +++ b/src/CoreAudio/AFAVFormatComponent/AudioFileFormatGeneric.cpp @@ -92,15 +92,7 @@ UncertainResult AudioFileFormatGeneric::FileDataIsThisFormat(UInt32 inDataByteSi probeData.buf = buf.data(); probeData.buf_size = inDataByteSize; -#warning "TODO: Remove old `read_probe` call once we no longer support older distros" -#if LIBAVCODEC_VERSION_MAJOR >= 61 return av_probe_input_format(&probeData, false) != nullptr ? kTrue : kFalse; -#else - const AVInputFormat* fmt = av_find_input_format(m_avformatShortName); - if (!fmt) - return false; - return fmt->read_probe(&probeData) ? kTrue : kFalse; -#endif } AudioFileObject* AudioFileFormatGeneric::New()