[FFmpeg-devel] [PATCH 238/281] mlp: convert to new channel layout API

James Almer jamrial at gmail.com
Thu Jan 13 04:05:17 EET 2022


From: Anton Khirnov <anton at khirnov.net>

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/mlp.c        |   9 +++
 libavcodec/mlp.h        |   5 ++
 libavcodec/mlp_parser.c |  12 ++--
 libavcodec/mlpdec.c     | 120 +++++++++++++++++++++++++++-------------
 libavcodec/mlpenc.c     | 106 ++++++++++++++++++-----------------
 5 files changed, 153 insertions(+), 99 deletions(-)

diff --git a/libavcodec/mlp.c b/libavcodec/mlp.c
index dcec145eb0..3f54b2e448 100644
--- a/libavcodec/mlp.c
+++ b/libavcodec/mlp.c
@@ -57,12 +57,21 @@ const ChannelInformation ff_mlp_ch_info[21] = {
     { 0x3F, 0x04, 0x02, 0x00 },
 };
 
+#if FF_API_OLD_CHANNEL_LAYOUT
 const uint64_t ff_mlp_channel_layouts[12] = {
     AV_CH_LAYOUT_MONO, AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_2_1,
     AV_CH_LAYOUT_QUAD, AV_CH_LAYOUT_2POINT1, AV_CH_LAYOUT_SURROUND,
     AV_CH_LAYOUT_4POINT0, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_3POINT1,
     AV_CH_LAYOUT_4POINT1, AV_CH_LAYOUT_5POINT1_BACK, 0,
 };
+#endif
+
+const AVChannelLayout ff_mlp_ch_layouts[12] = {
+    AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO, AV_CHANNEL_LAYOUT_2_1,
+    AV_CHANNEL_LAYOUT_QUAD, AV_CHANNEL_LAYOUT_2POINT1, AV_CHANNEL_LAYOUT_SURROUND,
+    AV_CHANNEL_LAYOUT_4POINT0, AV_CHANNEL_LAYOUT_5POINT0_BACK, AV_CHANNEL_LAYOUT_3POINT1,
+    AV_CHANNEL_LAYOUT_4POINT1, AV_CHANNEL_LAYOUT_5POINT1_BACK, { 0 },
+};
 
 #if CONFIG_SMALL
 #define CRC_TABLE_SIZE 257
diff --git a/libavcodec/mlp.h b/libavcodec/mlp.h
index e45eba55ae..8a3e4566a3 100644
--- a/libavcodec/mlp.h
+++ b/libavcodec/mlp.h
@@ -24,6 +24,8 @@
 
 #include <stdint.h>
 
+#include "libavutil/channel_layout.h"
+
 #define SYNC_MLP        0xbb
 #define SYNC_TRUEHD     0xba
 
@@ -135,7 +137,10 @@ typedef struct {
  */
 extern const ChannelInformation ff_mlp_ch_info[21];
 
+#if FF_API_OLD_CHANNEL_LAYOUT
 extern const uint64_t ff_mlp_channel_layouts[12];
+#endif
+extern const AVChannelLayout ff_mlp_ch_layouts[12];
 
 /** MLP uses checksums that seem to be based on the standard CRC algorithm, but
  *  are not (in implementation terms, the table lookup and XOR are reversed).
diff --git a/libavcodec/mlp_parser.c b/libavcodec/mlp_parser.c
index 9fea7db955..d391390dd5 100644
--- a/libavcodec/mlp_parser.c
+++ b/libavcodec/mlp_parser.c
@@ -175,22 +175,18 @@ static int mlp_parse(AVCodecParserContext *s,
         avctx->frame_size =
         s->duration = mh.access_unit_size;
 
-        if(!avctx->channels || !avctx->channel_layout) {
+        av_channel_layout_uninit(&avctx->ch_layout);
         if (mh.stream_type == 0xbb) {
             /* MLP stream */
-            avctx->channels       = mh.channels_mlp;
-            avctx->channel_layout = mh.channel_layout_mlp;
+            av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_mlp);
         } else { /* mh.stream_type == 0xba */
             /* TrueHD stream */
             if (!mh.channels_thd_stream2) {
-                avctx->channels       = mh.channels_thd_stream1;
-                avctx->channel_layout = mh.channel_layout_thd_stream1;
+                av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream1);
             } else {
-                avctx->channels       = mh.channels_thd_stream2;
-                avctx->channel_layout = mh.channel_layout_thd_stream2;
+                av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream2);
             }
         }
-        }
 
         if (!mh.is_vbr) /* Stream is CBR */
             avctx->bit_rate = mh.peak_bitrate;
diff --git a/libavcodec/mlpdec.c b/libavcodec/mlpdec.c
index 29fac54542..d4b8226ec6 100644
--- a/libavcodec/mlpdec.c
+++ b/libavcodec/mlpdec.c
@@ -32,6 +32,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
+#include "libavutil/opt.h"
 #include "get_bits.h"
 #include "internal.h"
 #include "libavutil/crc.h"
@@ -132,8 +133,11 @@ typedef struct SubStream {
 } SubStream;
 
 typedef struct MLPDecodeContext {
+    const AVClass  *class;
     AVCodecContext *avctx;
 
+    AVChannelLayout downmix_layout;
+
     /// Current access unit being read has a major sync.
     int         is_major_sync_unit;
 
@@ -169,39 +173,41 @@ typedef struct MLPDecodeContext {
     MLPDSPContext dsp;
 } MLPDecodeContext;
 
-static const uint64_t thd_channel_order[] = {
-    AV_CH_FRONT_LEFT, AV_CH_FRONT_RIGHT,                     // LR
-    AV_CH_FRONT_CENTER,                                      // C
-    AV_CH_LOW_FREQUENCY,                                     // LFE
-    AV_CH_SIDE_LEFT, AV_CH_SIDE_RIGHT,                       // LRs
-    AV_CH_TOP_FRONT_LEFT, AV_CH_TOP_FRONT_RIGHT,             // LRvh
-    AV_CH_FRONT_LEFT_OF_CENTER, AV_CH_FRONT_RIGHT_OF_CENTER, // LRc
-    AV_CH_BACK_LEFT, AV_CH_BACK_RIGHT,                       // LRrs
-    AV_CH_BACK_CENTER,                                       // Cs
-    AV_CH_TOP_CENTER,                                        // Ts
-    AV_CH_SURROUND_DIRECT_LEFT, AV_CH_SURROUND_DIRECT_RIGHT, // LRsd
-    AV_CH_WIDE_LEFT, AV_CH_WIDE_RIGHT,                       // LRw
-    AV_CH_TOP_FRONT_CENTER,                                  // Cvh
-    AV_CH_LOW_FREQUENCY_2,                                   // LFE2
+static const enum AVChannel thd_channel_order[] = {
+    AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT,                     // LR
+    AV_CHAN_FRONT_CENTER,                                        // C
+    AV_CHAN_LOW_FREQUENCY,                                       // LFE
+    AV_CHAN_SIDE_LEFT, AV_CHAN_SIDE_RIGHT,                       // LRs
+    AV_CHAN_TOP_FRONT_LEFT, AV_CHAN_TOP_FRONT_RIGHT,             // LRvh
+    AV_CHAN_FRONT_LEFT_OF_CENTER, AV_CHAN_FRONT_RIGHT_OF_CENTER, // LRc
+    AV_CHAN_BACK_LEFT, AV_CHAN_BACK_RIGHT,                       // LRrs
+    AV_CHAN_BACK_CENTER,                                         // Cs
+    AV_CHAN_TOP_CENTER,                                          // Ts
+    AV_CHAN_SURROUND_DIRECT_LEFT, AV_CHAN_SURROUND_DIRECT_RIGHT, // LRsd
+    AV_CHAN_WIDE_LEFT, AV_CHAN_WIDE_RIGHT,                       // LRw
+    AV_CHAN_TOP_FRONT_CENTER,                                    // Cvh
+    AV_CHAN_LOW_FREQUENCY_2,                                     // LFE2
 };
 
-static int mlp_channel_layout_subset(uint64_t channel_layout, uint64_t mask)
+static int mlp_channel_layout_subset(AVChannelLayout *layout, uint64_t mask)
 {
-    return channel_layout && ((channel_layout & mask) == channel_layout);
+    return av_channel_layout_check(layout) &&
+           av_channel_layout_subset(layout, mask) ==
+           av_channel_layout_subset(layout, UINT64_MAX);
 }
 
-static uint64_t thd_channel_layout_extract_channel(uint64_t channel_layout,
-                                                   int index)
+static enum AVChannel thd_channel_layout_extract_channel(uint64_t channel_layout,
+                                                         int index)
 {
     int i;
 
-    if (av_get_channel_layout_nb_channels(channel_layout) <= index)
-        return 0;
+    if (av_popcount64(channel_layout) <= index)
+        return AV_CHAN_NONE;
 
     for (i = 0; i < FF_ARRAY_ELEMS(thd_channel_order); i++)
-        if (channel_layout & thd_channel_order[i] && !index--)
+        if (channel_layout & (1 << thd_channel_order[i]) && !index--)
             return thd_channel_order[i];
-    return 0;
+    return AV_CHAN_NONE;
 }
 
 static VLC huff_vlc[3];
@@ -290,6 +296,14 @@ static av_cold int mlp_decode_init(AVCodecContext *avctx)
         m->substream[substr].lossless_check_data = 0xffffffff;
     ff_mlpdsp_init(&m->dsp);
 
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (avctx->request_channel_layout) {
+        av_channel_layout_uninit(&m->downmix_layout);
+        av_channel_layout_from_mask(&m->downmix_layout, avctx->request_channel_layout);
+    }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
     ff_thread_once(&init_static_once, init_static);
 
     return 0;
@@ -410,7 +424,7 @@ static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
         }
         if (mh.channels_thd_stream1 == 2 &&
             mh.channels_thd_stream2 == 2 &&
-            m->avctx->channels == 2)
+            m->avctx->ch_layout.nb_channels == 2)
             m->substream[0].mask = AV_CH_LAYOUT_STEREO;
         if ((substr = (mh.num_substreams > 1)))
             m->substream[0].mask = AV_CH_LAYOUT_STEREO;
@@ -419,14 +433,17 @@ static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
                 m->substream[2].mask = mh.channel_layout_thd_stream2;
             else
                 m->substream[2].mask = mh.channel_layout_thd_stream1;
-        if (m->avctx->channels > 2)
+        if (m->avctx->ch_layout.nb_channels > 2)
             m->substream[mh.num_substreams > 1].mask = mh.channel_layout_thd_stream1;
 
-        if (m->avctx->channels<=2 && m->substream[substr].mask == AV_CH_LAYOUT_MONO && m->max_decoded_substream == 1) {
+        if (m->avctx->ch_layout.nb_channels <= 2 &&
+            m->substream[substr].mask == AV_CH_LAYOUT_MONO && m->max_decoded_substream == 1) {
             av_log(m->avctx, AV_LOG_DEBUG, "Mono stream with 2 substreams, ignoring 2nd\n");
             m->max_decoded_substream = 0;
-            if (m->avctx->channels==2)
-                m->avctx->channel_layout = AV_CH_LAYOUT_STEREO;
+            if (m->avctx->ch_layout.nb_channels == 2) {
+                av_channel_layout_uninit(&m->avctx->ch_layout);
+                m->avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
+            }
         }
     }
 
@@ -548,7 +565,7 @@ static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
     s->max_matrix_channel = max_matrix_channel;
     s->noise_type         = noise_type;
 
-    if (mlp_channel_layout_subset(m->avctx->request_channel_layout, s->mask) &&
+    if (mlp_channel_layout_subset(&m->downmix_layout, s->mask) &&
         m->max_decoded_substream > substr) {
         av_log(m->avctx, AV_LOG_DEBUG,
                "Extracting %d-channel downmix (0x%"PRIx64") from substream %d. "
@@ -580,10 +597,11 @@ static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
     for (ch = 0; ch <= s->max_matrix_channel; ch++) {
         int ch_assign = get_bits(gbp, 6);
         if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
-            uint64_t channel = thd_channel_layout_extract_channel(s->mask,
-                                                                  ch_assign);
-            ch_assign = av_get_channel_layout_channel_index(s->mask,
-                                                            channel);
+            AVChannelLayout l;
+            enum AVChannel channel = thd_channel_layout_extract_channel(s->mask, ch_assign);
+
+            av_channel_layout_from_mask(&l, s->mask);
+            ch_assign = av_channel_layout_index_from_channel(&l, channel);
         }
         if (ch_assign < 0 || ch_assign > s->max_matrix_channel) {
             avpriv_request_sample(m->avctx,
@@ -623,21 +641,21 @@ static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
     }
 
     if (substr == m->max_decoded_substream) {
-        m->avctx->channels       = s->max_matrix_channel + 1;
-        m->avctx->channel_layout = s->mask;
+        av_channel_layout_uninit(&m->avctx->ch_layout);
+        av_channel_layout_from_mask(&m->avctx->ch_layout, s->mask);
         m->dsp.mlp_pack_output = m->dsp.mlp_select_pack_output(s->ch_assign,
                                                                s->output_shift,
                                                                s->max_matrix_channel,
                                                                m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
 
         if (m->avctx->codec_id == AV_CODEC_ID_MLP && m->needs_reordering) {
-            if (m->avctx->channel_layout == (AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY) ||
-                m->avctx->channel_layout == AV_CH_LAYOUT_5POINT0_BACK) {
+            if (s->mask == (AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY) ||
+                s->mask == AV_CH_LAYOUT_5POINT0_BACK) {
                 int i = s->ch_assign[4];
                 s->ch_assign[4] = s->ch_assign[3];
                 s->ch_assign[3] = s->ch_assign[2];
                 s->ch_assign[2] = i;
-            } else if (m->avctx->channel_layout == AV_CH_LAYOUT_5POINT1_BACK) {
+            } else if (s->mask == AV_CH_LAYOUT_5POINT1_BACK) {
                 FFSWAP(int, s->ch_assign[2], s->ch_assign[4]);
                 FFSWAP(int, s->ch_assign[3], s->ch_assign[5]);
             }
@@ -1078,7 +1096,7 @@ static int output_data(MLPDecodeContext *m, unsigned int substr,
     int ret;
     int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
 
-    if (m->avctx->channels != s->max_matrix_channel + 1) {
+    if (m->avctx->ch_layout.nb_channels != s->max_matrix_channel + 1) {
         av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
         return AVERROR_INVALIDDATA;
     }
@@ -1255,7 +1273,7 @@ static int read_access_unit(AVCodecContext *avctx, void* data,
 
         if (substr != m->max_decoded_substream &&
             m->substream[m->max_decoded_substream].min_channel == 0 &&
-            m->substream[m->max_decoded_substream].max_channel == avctx->channels - 1)
+            m->substream[m->max_decoded_substream].max_channel == avctx->ch_layout.nb_channels - 1)
             goto skip_substr;
 
         init_get_bits(&gb, buf, substream_data_len[substr] * 8);
@@ -1372,6 +1390,28 @@ static void mlp_decode_flush(AVCodecContext *avctx)
     }
 }
 
+#define OFFSET(x) offsetof(MLPDecodeContext, x)
+#define FLAGS (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
+static const AVOption options[] = {
+    { "downmix", "Request a specific channel layout from the decoder", OFFSET(downmix_layout),
+        AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, .flags = FLAGS },
+    { NULL },
+};
+
+static const AVClass mlp_decoder_class = {
+    .class_name = "MLP decoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVClass truehd_decoder_class = {
+    .class_name = "TrueHD decoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 #if CONFIG_MLP_DECODER
 const AVCodec ff_mlp_decoder = {
     .name           = "mlp",
@@ -1379,6 +1419,7 @@ const AVCodec ff_mlp_decoder = {
     .type           = AVMEDIA_TYPE_AUDIO,
     .id             = AV_CODEC_ID_MLP,
     .priv_data_size = sizeof(MLPDecodeContext),
+    .priv_class     = &mlp_decoder_class,
     .init           = mlp_decode_init,
     .decode         = read_access_unit,
     .flush          = mlp_decode_flush,
@@ -1393,6 +1434,7 @@ const AVCodec ff_truehd_decoder = {
     .type           = AVMEDIA_TYPE_AUDIO,
     .id             = AV_CODEC_ID_TRUEHD,
     .priv_data_size = sizeof(MLPDecodeContext),
+    .priv_class     = &truehd_decoder_class,
     .init           = mlp_decode_init,
     .decode         = read_access_unit,
     .flush          = mlp_decode_flush,
diff --git a/libavcodec/mlpenc.c b/libavcodec/mlpenc.c
index d8783b6f56..80ebb72758 100644
--- a/libavcodec/mlpenc.c
+++ b/libavcodec/mlpenc.c
@@ -381,8 +381,8 @@ static void copy_restart_frame_params(MLPEncodeContext *ctx)
 
         copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
 
-        for (unsigned int channel = 0; channel < ctx->avctx->channels; channel++) {
-            ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
+        for (unsigned int channel = 0; channel < ctx->avctx->ch_layout.nb_channels; channel++) {
+            ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->ch_layout.nb_channels) + channel;
 
             dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
             dp->matrix_params.shift[channel] = ctx->cur_decoding_params->matrix_params.shift[channel];
@@ -528,13 +528,13 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx)
     ctx->coded_peak_bitrate = mlp_peak_bitrate(9600000, avctx->sample_rate);
 
     /* TODO support more channels. */
-    if (avctx->channels > 2) {
+    if (avctx->ch_layout.nb_channels > 2) {
         av_log(avctx, AV_LOG_WARNING,
                "Only mono and stereo are supported at the moment.\n");
     }
 
     ctx->substream_info |= SUBSTREAM_INFO_ALWAYS_SET;
-    if (avctx->channels <= 2) {
+    if (avctx->ch_layout.nb_channels <= 2) {
         ctx->substream_info |= SUBSTREAM_INFO_MAX_2_CHAN;
     }
 
@@ -559,7 +559,7 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx)
 
     ctx->dts = -avctx->frame_size;
 
-    ctx->num_channels = avctx->channels + 2; /* +2 noise channels */
+    ctx->num_channels = avctx->ch_layout.nb_channels + 2; /* +2 noise channels */
     ctx->one_sample_buffer_size = avctx->frame_size
                                 * ctx->num_channels;
     /* TODO Let user pass major header interval as parameter. */
@@ -588,59 +588,48 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx)
     ctx->num_substreams = 1; // TODO: change this after adding multi-channel support for TrueHD
 
     if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
-        /* MLP */
-        switch(avctx->channel_layout) {
-        case AV_CH_LAYOUT_MONO:
-            ctx->channel_arrangement = 0; break;
-        case AV_CH_LAYOUT_STEREO:
-            ctx->channel_arrangement = 1; break;
-        case AV_CH_LAYOUT_2_1:
-            ctx->channel_arrangement = 2; break;
-        case AV_CH_LAYOUT_QUAD:
-            ctx->channel_arrangement = 3; break;
-        case AV_CH_LAYOUT_2POINT1:
-            ctx->channel_arrangement = 4; break;
-        case AV_CH_LAYOUT_SURROUND:
-            ctx->channel_arrangement = 7; break;
-        case AV_CH_LAYOUT_4POINT0:
-            ctx->channel_arrangement = 8; break;
-        case AV_CH_LAYOUT_5POINT0_BACK:
-            ctx->channel_arrangement = 9; break;
-        case AV_CH_LAYOUT_3POINT1:
-            ctx->channel_arrangement = 10; break;
-        case AV_CH_LAYOUT_4POINT1:
-            ctx->channel_arrangement = 11; break;
-        case AV_CH_LAYOUT_5POINT1_BACK:
-            ctx->channel_arrangement = 12; break;
-        default:
+        static const AVChannelLayout layout_arrangement[] = {
+            AV_CHANNEL_LAYOUT_MONO,         AV_CHANNEL_LAYOUT_STEREO,
+            AV_CHANNEL_LAYOUT_2_1,          AV_CHANNEL_LAYOUT_QUAD,
+            AV_CHANNEL_LAYOUT_2POINT1,      { 0 }, { 0 },
+            AV_CHANNEL_LAYOUT_SURROUND,     AV_CHANNEL_LAYOUT_4POINT0,
+            AV_CHANNEL_LAYOUT_5POINT0_BACK, AV_CHANNEL_LAYOUT_3POINT1,
+            AV_CHANNEL_LAYOUT_4POINT1,      AV_CHANNEL_LAYOUT_5POINT1_BACK,
+        };
+        int i;
+
+        for (i = 0; i < FF_ARRAY_ELEMS(layout_arrangement); i++)
+            if (!av_channel_layout_compare(&avctx->ch_layout, &layout_arrangement[i]))
+                break;
+        if (i == FF_ARRAY_ELEMS(layout_arrangement)) {
             av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
             return AVERROR(EINVAL);
         }
+        ctx->channel_arrangement = i;
         ctx->flags = FLAGS_DVDA;
         ctx->channel_occupancy = ff_mlp_ch_info[ctx->channel_arrangement].channel_occupancy;
         ctx->summary_info      = ff_mlp_ch_info[ctx->channel_arrangement].summary_info     ;
     } else {
         /* TrueHD */
-        switch(avctx->channel_layout) {
-        case AV_CH_LAYOUT_STEREO:
+        if (!av_channel_layout_compare(&avctx->ch_layout,
+                                       &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) {
             ctx->ch_modifier_thd0    = 0;
             ctx->ch_modifier_thd1    = 0;
             ctx->ch_modifier_thd2    = 0;
             ctx->channel_arrangement = 1;
-            break;
-        case AV_CH_LAYOUT_5POINT0_BACK:
+        } else if (!av_channel_layout_compare(&avctx->ch_layout,
+                                              &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK)) {
             ctx->ch_modifier_thd0    = 1;
             ctx->ch_modifier_thd1    = 1;
             ctx->ch_modifier_thd2    = 1;
             ctx->channel_arrangement = 11;
-            break;
-        case AV_CH_LAYOUT_5POINT1_BACK:
+        } else if (!av_channel_layout_compare(&avctx->ch_layout,
+                                              &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK)) {
             ctx->ch_modifier_thd0    = 2;
             ctx->ch_modifier_thd1    = 1;
             ctx->ch_modifier_thd2    = 2;
             ctx->channel_arrangement = 15;
-            break;
-        default:
+        } else {
             av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
             return AVERROR(EINVAL);
         }
@@ -665,7 +654,7 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx)
         sum += ctx->seq_size[index];
     }
     ctx->sequence_size = sum;
-    size = ctx->restart_intervals * ctx->sequence_size * ctx->avctx->channels;
+    size = ctx->restart_intervals * ctx->sequence_size * ctx->avctx->ch_layout.nb_channels;
     ctx->channel_params = av_calloc(size, sizeof(*ctx->channel_params));
     if (!ctx->channel_params)
         return AVERROR(ENOMEM);
@@ -679,7 +668,7 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx)
     rh->noisegen_seed      = 0;
 
     rh->min_channel        = 0;
-    rh->max_channel        = avctx->channels - 1;
+    rh->max_channel        = avctx->ch_layout.nb_channels - 1;
     /* FIXME: this works for 1 and 2 channels, but check for more */
     rh->max_matrix_channel = rh->max_channel;
 
@@ -1228,7 +1217,7 @@ static void input_to_sample_buffer(MLPEncodeContext *ctx)
         int32_t *input_buffer = ctx->inout_buffer + cur_index * ctx->one_sample_buffer_size;
 
         for (unsigned int i = 0; i < ctx->avctx->frame_size; i++) {
-            for (unsigned int channel = 0; channel < ctx->avctx->channels; channel++)
+            for (unsigned int channel = 0; channel < ctx->avctx->ch_layout.nb_channels; channel++)
                 *sample_buffer++ = *input_buffer++;
             sample_buffer += 2; /* noise_channels */
             input_buffer += 2; /* noise_channels */
@@ -1947,7 +1936,7 @@ static void set_best_codebook(MLPEncodeContext *ctx)
 
         /* Update context. */
         for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
-            ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
+            ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->ch_layout.nb_channels) + channel;
 
             best_codebook = *best_path++;
             cur_bo = &ctx->best_offset[index][channel][best_codebook];
@@ -1968,17 +1957,18 @@ static void set_major_params(MLPEncodeContext *ctx)
     RestartHeader *rh = ctx->cur_restart_header;
     uint8_t max_huff_lsbs = 0;
     uint8_t max_output_bits = 0;
-    DecodingParams *seq_dp = ctx->decoding_params + ctx->seq_offset[0] * ctx->avctx->channels;
-    ChannelParams *seq_cp = ctx->channel_params + ctx->seq_offset[0] * ctx->avctx->channels;
+    int channels = ctx->avctx->ch_layout.nb_channels;
+    DecodingParams *seq_dp = ctx->decoding_params + ctx->seq_offset[0] * channels;
+    ChannelParams *seq_cp = ctx->channel_params + ctx->seq_offset[0] * channels;
 
     for (unsigned int index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
         memcpy(&ctx->major_decoding_params[index], seq_dp + index, sizeof(DecodingParams));
-        for (unsigned int channel = 0; channel < ctx->avctx->channels; channel++) {
-            uint8_t huff_lsbs = (seq_cp + index*(ctx->avctx->channels) + channel)->huff_lsbs;
+        for (unsigned int channel = 0; channel < channels; channel++) {
+            uint8_t huff_lsbs = (seq_cp + index*(channels) + channel)->huff_lsbs;
             if (max_huff_lsbs < huff_lsbs)
                 max_huff_lsbs = huff_lsbs;
             memcpy(&ctx->major_channel_params[index][channel],
-                   (seq_cp + index*(ctx->avctx->channels) + channel),
+                   (seq_cp + index*(channels) + channel),
                    sizeof(ChannelParams));
         }
     }
@@ -2017,7 +2007,7 @@ static void analyze_sample_buffer(MLPEncodeContext *ctx)
 
     ctx->cur_restart_header = &ctx->restart_header;
     ctx->cur_decoding_params = seq_dp + 1;
-    ctx->cur_channel_params = seq_cp + ctx->avctx->channels;
+    ctx->cur_channel_params = seq_cp + ctx->avctx->ch_layout.nb_channels;
 
     determine_quant_step_size(ctx);
     generate_2_noise_channels(ctx);
@@ -2044,7 +2034,7 @@ static void analyze_sample_buffer(MLPEncodeContext *ctx)
 
     for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
         ctx->cur_decoding_params = seq_dp + index;
-        ctx->cur_channel_params = seq_cp + index*(ctx->avctx->channels);
+        ctx->cur_channel_params = seq_cp + index*(ctx->avctx->ch_layout.nb_channels);
         ctx->cur_best_offset = ctx->best_offset[index];
         determine_bits(ctx);
         ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
@@ -2078,13 +2068,14 @@ static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 {
     MLPEncodeContext *ctx = avctx->priv_data;
     int bytes_written = 0;
+    int channels = avctx->ch_layout.nb_channels;
     int restart_frame, ret;
     uint8_t *data;
 
     if (!frame && !ctx->last_frames--)
         return 0;
 
-    if ((ret = ff_alloc_packet(avctx, avpkt, 87500 * avctx->channels)) < 0)
+    if ((ret = ff_alloc_packet(avctx, avpkt, 87500 * channels)) < 0)
         return ret;
 
     if (frame) {
@@ -2149,7 +2140,7 @@ input_and_return:
             ctx->number_of_frames = ctx->next_major_number_of_frames;
             ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
 
-            ctx->seq_channel_params = ctx->channel_params + ctx->seq_offset[seq_index] * ctx->avctx->channels;
+            ctx->seq_channel_params = ctx->channel_params + ctx->seq_offset[seq_index] * channels;
 
             ctx->seq_decoding_params = ctx->decoding_params + ctx->seq_offset[seq_index];
 
@@ -2157,7 +2148,7 @@ input_and_return:
             ctx->number_of_samples = number_of_samples;
 
             for (unsigned int index = 0; index < ctx->seq_size[seq_index]; index++) {
-                clear_channel_params(ctx->seq_channel_params + index * ctx->avctx->channels, ctx->avctx->channels);
+                clear_channel_params(ctx->seq_channel_params + index * channels, channels);
                 default_decoding_params(ctx, ctx->seq_decoding_params + index);
             }
 
@@ -2225,7 +2216,10 @@ const AVCodec ff_mlp_encoder = {
     .capabilities           = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
     .sample_fmts            = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE},
     .supported_samplerates  = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
+#if FF_API_OLD_CHANNEL_LAYOUT
     .channel_layouts        = ff_mlp_channel_layouts,
+#endif
+    .ch_layouts             = ff_mlp_ch_layouts,
     .caps_internal          = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
 };
 #endif
@@ -2242,7 +2236,15 @@ const AVCodec ff_truehd_encoder = {
     .capabilities           = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
     .sample_fmts            = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE},
     .supported_samplerates  = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
+#if FF_API_OLD_CHANNEL_LAYOUT
     .channel_layouts        = (const uint64_t[]) {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_5POINT1_BACK, 0},
+#endif
+    .ch_layouts             = (const AVChannelLayout[]) {
+                                  AV_CHANNEL_LAYOUT_STEREO,
+                                  AV_CHANNEL_LAYOUT_5POINT0_BACK,
+                                  AV_CHANNEL_LAYOUT_5POINT1_BACK,
+                                  { 0 }
+                              },
     .caps_internal          = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
 };
 #endif
-- 
2.34.1



More information about the ffmpeg-devel mailing list