[FFmpeg-devel] [PATCH 241/281] mpegaudio: convert to new channel layout API

James Almer jamrial at gmail.com
Thu Jan 13 04:06:45 EET 2022


From: Anton Khirnov <anton at khirnov.net>

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/mp3_header_decompress_bsf.c |  2 +-
 libavcodec/mpegaudio_parser.c          |  4 +++-
 libavcodec/mpegaudiodec_template.c     | 22 ++++++++++++----------
 libavcodec/mpegaudioenc_fixed.c        |  5 +++++
 libavcodec/mpegaudioenc_float.c        |  5 +++++
 libavcodec/mpegaudioenc_template.c     |  2 +-
 6 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/libavcodec/mp3_header_decompress_bsf.c b/libavcodec/mp3_header_decompress_bsf.c
index ebf6bde1c2..0fd8b3a454 100644
--- a/libavcodec/mp3_header_decompress_bsf.c
+++ b/libavcodec/mp3_header_decompress_bsf.c
@@ -98,7 +98,7 @@ static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out)
     }
     memcpy(out->data + frame_size - buf_size, buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
 
-    if(ctx->par_in->channels==2){
+    if (ctx->par_in->ch_layout.nb_channels == 2){
         uint8_t *p= out->data + frame_size - buf_size;
         if(lsf){
             FFSWAP(int, p[1], p[2]);
diff --git a/libavcodec/mpegaudio_parser.c b/libavcodec/mpegaudio_parser.c
index 2549503d35..6f1272117b 100644
--- a/libavcodec/mpegaudio_parser.c
+++ b/libavcodec/mpegaudio_parser.c
@@ -84,7 +84,9 @@ static int mpegaudio_parse(AVCodecParserContext *s1,
 
                     if (s->header_count > header_threshold) {
                         avctx->sample_rate= sr;
-                        avctx->channels   = channels;
+                        av_channel_layout_uninit(&avctx->ch_layout);
+                        avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+                        avctx->ch_layout.nb_channels = channels;
                         s1->duration      = frame_size;
                         avctx->codec_id   = codec_id;
                         if (s->no_bitrate || !avctx->bit_rate) {
diff --git a/libavcodec/mpegaudiodec_template.c b/libavcodec/mpegaudiodec_template.c
index bbb6ff1120..30b315c450 100644
--- a/libavcodec/mpegaudiodec_template.c
+++ b/libavcodec/mpegaudiodec_template.c
@@ -1580,8 +1580,9 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
         return AVERROR_INVALIDDATA;
     }
     /* update codec info */
-    avctx->channels       = s->nb_channels;
-    avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
+    av_channel_layout_uninit(&avctx->ch_layout);
+    avctx->ch_layout = s->nb_channels == 1 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
+                                             (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
     if (!avctx->bit_rate)
         avctx->bit_rate = s->bit_rate;
 
@@ -1661,8 +1662,9 @@ static int decode_frame_adu(AVCodecContext *avctx, void *data,
     }
     /* update codec info */
     avctx->sample_rate = s->sample_rate;
-    avctx->channels    = s->nb_channels;
-    avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
+    av_channel_layout_uninit(&avctx->ch_layout);
+    avctx->ch_layout = s->nb_channels == 1 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
+                                             (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
     if (!avctx->bit_rate)
         avctx->bit_rate = s->bit_rate;
 
@@ -1756,8 +1758,8 @@ static av_cold int decode_init_mp3on4(AVCodecContext * avctx)
     }
     s->frames             = mp3Frames[cfg.chan_config];
     s->coff               = chan_offset[cfg.chan_config];
-    avctx->channels       = ff_mpeg4audio_channels[cfg.chan_config];
-    avctx->channel_layout = chan_layout[cfg.chan_config];
+    av_channel_layout_uninit(&avctx->ch_layout);
+    av_channel_layout_from_mask(&avctx->ch_layout, chan_layout[cfg.chan_config]);
 
     if (cfg.sample_rate < 16000)
         s->syncword = 0xffe00000;
@@ -1854,8 +1856,8 @@ static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
             return AVERROR_INVALIDDATA;
         }
 
-        if (ch + m->nb_channels > avctx->channels ||
-            s->coff[fr] + m->nb_channels > avctx->channels) {
+        if (ch + m->nb_channels > avctx->ch_layout.nb_channels ||
+            s->coff[fr] + m->nb_channels > avctx->ch_layout.nb_channels) {
             av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
                                         "channel count\n");
             return AVERROR_INVALIDDATA;
@@ -1880,7 +1882,7 @@ static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
 
         avctx->bit_rate += m->bit_rate;
     }
-    if (ch != avctx->channels) {
+    if (ch != avctx->ch_layout.nb_channels) {
         av_log(avctx, AV_LOG_ERROR, "failed to decode all channels\n");
         return AVERROR_INVALIDDATA;
     }
@@ -1888,7 +1890,7 @@ static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
     /* update codec info */
     avctx->sample_rate = s->mp3decctx[0]->sample_rate;
 
-    frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
+    frame->nb_samples = out_size / (avctx->ch_layout.nb_channels * sizeof(OUT_INT));
     *got_frame_ptr    = 1;
 
     return buf_size;
diff --git a/libavcodec/mpegaudioenc_fixed.c b/libavcodec/mpegaudioenc_fixed.c
index fb1ba4e1bf..23ee61fac5 100644
--- a/libavcodec/mpegaudioenc_fixed.c
+++ b/libavcodec/mpegaudioenc_fixed.c
@@ -35,9 +35,14 @@ const AVCodec ff_mp2fixed_encoder = {
     .supported_samplerates = (const int[]){
         44100, 48000,  32000, 22050, 24000, 16000, 0
     },
+#if FF_API_OLD_CHANNEL_LAYOUT
     .channel_layouts       = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
                                                  AV_CH_LAYOUT_STEREO,
                                                  0 },
+#endif
+    .ch_layouts            = (const AVChannelLayout[]){ AV_CHANNEL_LAYOUT_MONO,
+                                                        AV_CHANNEL_LAYOUT_STEREO,
+                                                        { 0 } },
     .defaults              = mp2_defaults,
     .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE,
 };
diff --git a/libavcodec/mpegaudioenc_float.c b/libavcodec/mpegaudioenc_float.c
index accb909a6f..9f9b885d21 100644
--- a/libavcodec/mpegaudioenc_float.c
+++ b/libavcodec/mpegaudioenc_float.c
@@ -36,9 +36,14 @@ const AVCodec ff_mp2_encoder = {
     .supported_samplerates = (const int[]){
         44100, 48000,  32000, 22050, 24000, 16000, 0
     },
+#if FF_API_OLD_CHANNEL_LAYOUT
     .channel_layouts       = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
                                                  AV_CH_LAYOUT_STEREO,
                                                  0 },
+#endif
+    .ch_layouts            = (const AVChannelLayout[]){ AV_CHANNEL_LAYOUT_MONO,
+                                                        AV_CHANNEL_LAYOUT_STEREO,
+                                                        { 0 } },
     .defaults              = mp2_defaults,
     .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE,
 };
diff --git a/libavcodec/mpegaudioenc_template.c b/libavcodec/mpegaudioenc_template.c
index 1ffd31f7cb..3299360f0c 100644
--- a/libavcodec/mpegaudioenc_template.c
+++ b/libavcodec/mpegaudioenc_template.c
@@ -79,7 +79,7 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx)
     MpegAudioContext *s = avctx->priv_data;
     int freq = avctx->sample_rate;
     int bitrate = avctx->bit_rate;
-    int channels = avctx->channels;
+    int channels = avctx->ch_layout.nb_channels;
     int i, v, table;
     float a;
 
-- 
2.34.1



More information about the ffmpeg-devel mailing list