ffmpeg: channels and channel_layout are deprecated

Use ch_layout instead

Change-Id: I6a07870eae42836d3fd993c253613bc0f33b1598
Reviewed-on: https://review.haiku-os.org/c/haiku/+/7252
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
This commit is contained in:
PulkoMandy 2023-12-29 13:43:53 +01:00 committed by Adrien Destugues
parent eb2154fd47
commit 1774dd5ee9
4 changed files with 133 additions and 24 deletions

View File

@ -345,6 +345,28 @@ AVCodecDecoder::_ResetTempPacket()
}
static int
get_channel_count(AVCodecContext* context)
{
#if LIBAVCODEC_VERSION_MAJOR >= 60
return context->ch_layout.nb_channels;
#else
return context->channels;
#endif
}
static void
set_channel_count(AVCodecContext* context, int count)
{
#if LIBAVCODEC_VERSION_MAJOR >= 60
context->ch_layout.nb_channels = count;
#else
context->channels = count;
#endif
}
status_t
AVCodecDecoder::_NegotiateAudioOutputFormat(media_format* inOutFormat)
{
@ -383,7 +405,7 @@ AVCodecDecoder::_NegotiateAudioOutputFormat(media_format* inOutFormat)
outputAudioFormat = media_raw_audio_format::wildcard;
outputAudioFormat.byte_order = B_MEDIA_HOST_ENDIAN;
outputAudioFormat.frame_rate = fCodecContext->sample_rate;
outputAudioFormat.channel_count = fCodecContext->channels;
outputAudioFormat.channel_count = get_channel_count(fCodecContext);
ConvertAVSampleFormatToRawAudioFormat(fCodecContext->sample_fmt,
outputAudioFormat.format);
// Check that format is not still a wild card!
@ -421,6 +443,17 @@ AVCodecDecoder::_NegotiateAudioOutputFormat(media_format* inOutFormat)
return B_NO_MEMORY;
if (av_sample_fmt_is_planar(fCodecContext->sample_fmt)) {
fResampleContext = NULL;
#if LIBAVCODEC_VERSION_MAJOR >= 60
swr_alloc_set_opts2(&fResampleContext,
&fCodecContext->ch_layout,
fCodecContext->request_sample_fmt,
fCodecContext->sample_rate,
&fCodecContext->ch_layout,
fCodecContext->sample_fmt,
fCodecContext->sample_rate,
0, NULL);
#else
fResampleContext = swr_alloc_set_opts(NULL,
fCodecContext->channel_layout,
fCodecContext->request_sample_fmt,
@ -429,6 +462,7 @@ AVCodecDecoder::_NegotiateAudioOutputFormat(media_format* inOutFormat)
fCodecContext->sample_fmt,
fCodecContext->sample_rate,
0, NULL);
#endif
swr_init(fResampleContext);
}
@ -768,13 +802,13 @@ AVCodecDecoder::_ApplyEssentialAudioContainerPropertiesToContext()
containerProperties.output.format, fCodecContext->request_sample_fmt);
fCodecContext->sample_rate
= static_cast<int>(containerProperties.output.frame_rate);
fCodecContext->channels
= static_cast<int>(containerProperties.output.channel_count);
int channel_count = static_cast<int>(containerProperties.output.channel_count);
// Check that channel count is not still a wild card!
if (fCodecContext->channels == 0) {
if (channel_count == 0) {
TRACE(" channel_count still a wild-card, assuming stereo.\n");
fCodecContext->channels = 2;
}
set_channel_count(fCodecContext, 2);
} else
set_channel_count(fCodecContext, channel_count);
fCodecContext->block_align = fBlockAlign;
fCodecContext->extradata = reinterpret_cast<uint8_t*>(fExtraData);
@ -941,7 +975,7 @@ AVCodecDecoder::_MoveAudioFramesToRawDecodedAudioAndUpdateStartTimes()
uintptr_t out = (uintptr_t)fRawDecodedAudio->data[0];
int32 offset = fDecodedDataBufferOffset;
for (int i = 0; i < frames; i++) {
for (int j = 0; j < fCodecContext->channels; j++) {
for (int j = 0; j < get_channel_count(fCodecContext); j++) {
memcpy((void*)out, fDecodedDataBuffer->data[j]
+ offset, fInputFrameSize);
out += fInputFrameSize;
@ -970,7 +1004,7 @@ AVCodecDecoder::_MoveAudioFramesToRawDecodedAudioAndUpdateStartTimes()
avformat_codec_context* codecContext
= static_cast<avformat_codec_context*>(fRawDecodedAudio->opaque);
codecContext->channels = fCodecContext->channels;
codecContext->channels = get_channel_count(fCodecContext);
codecContext->sample_rate = fCodecContext->sample_rate;
}

View File

@ -238,6 +238,28 @@ AVCodecEncoder::Encode(const void* buffer, int64 frameCount,
// #pragma mark -
static int
get_channel_count(AVCodecContext* context)
{
#if LIBAVCODEC_VERSION_MAJOR >= 60
return context->ch_layout.nb_channels;
#else
return context->channels;
#endif
}
static void
set_channel_count(AVCodecContext* context, int count)
{
#if LIBAVCODEC_VERSION_MAJOR >= 60
context->ch_layout.nb_channels = count;
#else
context->channels = count;
#endif
}
status_t
AVCodecEncoder::_Setup()
{
@ -322,11 +344,10 @@ AVCodecEncoder::_Setup()
// frame rate
fCodecContext->sample_rate = (int)fInputFormat.u.raw_audio.frame_rate;
// channels
fCodecContext->channels = fInputFormat.u.raw_audio.channel_count;
set_channel_count(fCodecContext, fInputFormat.u.raw_audio.channel_count);
// raw bitrate
rawBitRate = fCodecContext->sample_rate * fCodecContext->channels
* (fInputFormat.u.raw_audio.format
& media_raw_audio_format::B_AUDIO_SIZE_MASK) * 8;
rawBitRate = fCodecContext->sample_rate * get_channel_count(fCodecContext)
* (fInputFormat.u.raw_audio.format & media_raw_audio_format::B_AUDIO_SIZE_MASK) * 8;
// sample format
switch (fInputFormat.u.raw_audio.format) {
case media_raw_audio_format::B_AUDIO_FLOAT:
@ -350,6 +371,17 @@ AVCodecEncoder::_Setup()
return B_MEDIA_BAD_FORMAT;
break;
}
#if LIBAVCODEC_VERSION_MAJOR >= 60
if (fInputFormat.u.raw_audio.channel_mask == 0) {
// guess the channel mask...
av_channel_layout_default(&fCodecContext->ch_layout,
fInputFormat.u.raw_audio.channel_count);
} else {
// The bits match 1:1 for media_multi_channels and FFmpeg defines.
av_channel_layout_from_mask(&fCodecContext->ch_layout,
fInputFormat.u.raw_audio.channel_mask);
}
#else
if (fInputFormat.u.raw_audio.channel_mask == 0) {
// guess the channel mask...
switch (fInputFormat.u.raw_audio.channel_count) {
@ -383,6 +415,7 @@ AVCodecEncoder::_Setup()
// The bits match 1:1 for media_multi_channels and FFmpeg defines.
fCodecContext->channel_layout = fInputFormat.u.raw_audio.channel_mask;
}
#endif
} else {
TRACE(" UNSUPPORTED MEDIA TYPE!\n");
return B_NOT_SUPPORTED;
@ -390,8 +423,7 @@ AVCodecEncoder::_Setup()
// TODO: Support letting the user overwrite this via
// SetEncodeParameters(). See comments there...
int wantedBitRate = (int)(rawBitRate / fBitRateScale
* fEncodeParameters.quality);
int wantedBitRate = (int)(rawBitRate / fBitRateScale * fEncodeParameters.quality);
if (wantedBitRate == 0)
wantedBitRate = (int)(rawBitRate / fBitRateScale);
@ -546,8 +578,8 @@ AVCodecEncoder::_EncodeAudio(const uint8* buffer, size_t bufferSize,
av_frame_unref(fFrame);
fFrame->nb_samples = frameCount;
int count = avcodec_fill_audio_frame(fFrame, fCodecContext->channels,
fCodecContext->sample_fmt, (const uint8_t *) buffer, bufferSize, 1);
int count = avcodec_fill_audio_frame(fFrame, get_channel_count(fCodecContext),
fCodecContext->sample_fmt, (const uint8_t*)buffer, bufferSize, 1);
if (count < 0) {
TRACE(" avcodec_encode_audio() failed filling data: %d\n", count);

View File

@ -943,6 +943,28 @@ AVFormatReader::Stream::~Stream()
}
static int
get_channel_count(AVCodecParameters* context)
{
#if LIBAVCODEC_VERSION_MAJOR >= 60
return context->ch_layout.nb_channels;
#else
return context->channels;
#endif
}
static int
get_channel_mask(AVCodecParameters* context)
{
#if LIBAVCODEC_VERSION_MAJOR >= 60
return context->ch_layout.u.mask;
#else
return context->channel_layout;
#endif
}
status_t
AVFormatReader::Stream::Init(int32 virtualIndex)
{
@ -1052,8 +1074,8 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
switch (format->type) {
case B_MEDIA_RAW_AUDIO:
format->u.raw_audio.frame_rate = (float)codecParams->sample_rate;
format->u.raw_audio.channel_count = codecParams->channels;
format->u.raw_audio.channel_mask = codecParams->channel_layout;
format->u.raw_audio.channel_count = get_channel_count(codecParams);
format->u.raw_audio.channel_mask = get_channel_mask(codecParams);
ConvertAVSampleFormatToRawAudioFormat(
(AVSampleFormat)codecParams->format,
format->u.raw_audio.format);
@ -1077,10 +1099,8 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
format->u.encoded_audio.output.frame_rate
= (float)codecParams->sample_rate;
// Channel layout bits match in Be API and FFmpeg.
format->u.encoded_audio.output.channel_count
= codecParams->channels;
format->u.encoded_audio.multi_info.channel_mask
= codecParams->channel_layout;
format->u.encoded_audio.output.channel_count = get_channel_count(codecParams);
format->u.encoded_audio.multi_info.channel_mask = get_channel_mask(codecParams);
format->u.encoded_audio.output.byte_order
= avformat_to_beos_byte_order(
(AVSampleFormat)codecParams->format);
@ -1094,7 +1114,7 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
= codecParams->block_align;
} else {
format->u.encoded_audio.output.buffer_size
= codecParams->frame_size * codecParams->channels
= codecParams->frame_size * get_channel_count(codecParams)
* (format->u.encoded_audio.output.format
& media_raw_audio_format::B_AUDIO_SIZE_MASK);
}

View File

@ -98,6 +98,17 @@ AVFormatWriter::StreamCookie::~StreamCookie()
}
static void
set_channel_count(AVCodecParameters* context, int count)
{
#if LIBAVCODEC_VERSION_MAJOR >= 60
context->ch_layout.nb_channels = count;
#else
context->channels = count;
#endif
}
status_t
AVFormatWriter::StreamCookie::Init(media_format* format,
const media_codec_info* codecInfo)
@ -161,7 +172,7 @@ AVFormatWriter::StreamCookie::Init(media_format* format,
fStream->codecpar->sample_rate = (int)format->u.raw_audio.frame_rate;
// channels
fStream->codecpar->channels = format->u.raw_audio.channel_count;
set_channel_count(fStream->codecpar, format->u.raw_audio.channel_count);
// set fStream to the audio format we want to use. This is only a hint
// (each encoder has a different set of accepted formats)
@ -234,6 +245,17 @@ AVFormatWriter::StreamCookie::Init(media_format* format,
}
}
#if LIBAVCODEC_VERSION_MAJOR >= 60
if (format->u.raw_audio.channel_mask == 0) {
// guess the channel mask...
av_channel_layout_default(&fStream->codecpar->ch_layout,
format->u.raw_audio.channel_count);
} else {
// The bits match 1:1 for media_multi_channels and FFmpeg defines.
av_channel_layout_from_mask(&fStream->codecpar->ch_layout,
format->u.raw_audio.channel_mask);
}
#else
if (format->u.raw_audio.channel_mask == 0) {
// guess the channel mask...
switch (format->u.raw_audio.channel_count) {
@ -267,6 +289,7 @@ AVFormatWriter::StreamCookie::Init(media_format* format,
// The bits match 1:1 for media_multi_channels and FFmpeg defines.
fStream->codecpar->channel_layout = format->u.raw_audio.channel_mask;
}
#endif
}
TRACE(" stream->time_base: (%d/%d)\n",