Skip to content

Commit

Permalink
Use "ch_layout.nb_channels" When Building On Newer Libavcodec Version
Browse files Browse the repository at this point in the history
  • Loading branch information
CuriousTommy committed Jul 1, 2024
1 parent 203af1f commit bc55338
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/CoreAudio/AudioToolbox/AudioConverterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -131,7 +140,12 @@ 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.nb_channels = m_destinationFormat.mChannelsPerFrame;
#else
m_encoder->channels = m_destinationFormat.mChannelsPerFrame;
#endif
m_encoder->sample_rate = m_destinationFormat.mSampleRate;
m_encoder->channel_layout = CAChannelCountToLayout(m_destinationFormat.mChannelsPerFrame);
m_encoder->sample_fmt = CACodecSampleFormat(&m_destinationFormat);
Expand Down Expand Up @@ -163,8 +177,12 @@ void AudioConverter::allocateBuffers()
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
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
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)
Expand All @@ -174,8 +192,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");
Expand Down

0 comments on commit bc55338

Please sign in to comment.