diff mbox series

[FFmpeg-devel,182/281] aac: convert to new channel layout API

Message ID 20220113020242.661-3-jamrial@gmail.com
State New
Headers show
Series New channel layout API
Related show

Commit Message

James Almer Jan. 13, 2022, 2:02 a.m. UTC
From: Anton Khirnov <anton@khirnov.net>

Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/aac.h              | 11 ++++--
 libavcodec/aac_ac3_parser.c   |  9 +++--
 libavcodec/aaccoder.c         | 10 +++---
 libavcodec/aaccoder_twoloop.h |  4 +--
 libavcodec/aacdec.c           |  6 ++++
 libavcodec/aacdec_fixed.c     |  3 ++
 libavcodec/aacdec_template.c  | 59 ++++++++++++++++++++++++--------
 libavcodec/aacdectab.h        | 20 +++++++++++
 libavcodec/aacenc.c           | 13 ++++---
 libavcodec/aacenc.h           | 64 ++++++++++++++++++-----------------
 libavcodec/aacenctab.h        | 16 ++++-----
 libavcodec/aacpsy.c           |  8 ++---
 libavcodec/psymodel.c         |  8 ++---
 libavcodec/psymodel.h         |  2 +-
 14 files changed, 154 insertions(+), 79 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/aac.h b/libavcodec/aac.h
index 1e82f56ca9..53be546857 100644
--- a/libavcodec/aac.h
+++ b/libavcodec/aac.h
@@ -32,6 +32,7 @@ 
 
 
 #include "aac_defines.h"
+#include "libavutil/channel_layout.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/fixed_dsp.h"
 #include "libavutil/mem_internal.h"
@@ -125,8 +126,7 @@  typedef struct OutputConfiguration {
     MPEG4AudioConfig m4ac;
     uint8_t layout_map[MAX_ELEM_ID*4][3];
     int layout_map_tags;
-    int channels;
-    uint64_t channel_layout;
+    AVChannelLayout ch_layout;
     enum OCStatus status;
 } OutputConfiguration;
 
@@ -288,6 +288,11 @@  typedef struct ChannelElement {
     SpectralBandReplication sbr;
 } ChannelElement;
 
+enum AACOutputChannelOrder {
+    CHANNEL_ORDER_DEFAULT,
+    CHANNEL_ORDER_CODED,
+};
+
 /**
  * main AAC context
  */
@@ -352,6 +357,8 @@  struct AACContext {
     int dmono_mode;      ///< 0->not dmono, 1->use first channel, 2->use second channel
     /** @} */
 
+    enum AACOutputChannelOrder output_channel_order;
+
     DECLARE_ALIGNED(32, INTFLOAT, temp)[128];
 
     OutputConfiguration oc[2];
diff --git a/libavcodec/aac_ac3_parser.c b/libavcodec/aac_ac3_parser.c
index e84d30aea2..cd54a0a3e2 100644
--- a/libavcodec/aac_ac3_parser.c
+++ b/libavcodec/aac_ac3_parser.c
@@ -90,8 +90,13 @@  get_next:
         if (avctx->codec_id != AV_CODEC_ID_AAC) {
             avctx->sample_rate = s->sample_rate;
             if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
-                avctx->channels = s->channels;
-                avctx->channel_layout = s->channel_layout;
+                av_channel_layout_uninit(&avctx->ch_layout);
+                if (s->channel_layout) {
+                    av_channel_layout_from_mask(&avctx->ch_layout, s->channel_layout);
+                } else {
+                    avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+                    avctx->ch_layout.nb_channels = s->channels;
+                }
             }
             s1->duration = s->samples;
             avctx->audio_service_type = s->service_type;
diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c
index 7bbd4d5b2e..f460479498 100644
--- a/libavcodec/aaccoder.c
+++ b/libavcodec/aaccoder.c
@@ -397,7 +397,7 @@  static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
                                        const float lambda)
 {
     int start = 0, i, w, w2, g;
-    int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels * (lambda / 120.f);
+    int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->ch_layout.nb_channels * (lambda / 120.f);
     float dists[128] = { 0 }, uplims[128] = { 0 };
     float maxvals[128];
     int fflag, minscaler;
@@ -556,7 +556,7 @@  static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChanne
     const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
 
     int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
-        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels)
+        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
         * (lambda / 120.f);
 
     /** Keep this in sync with twoloop's cutoff selection */
@@ -564,7 +564,7 @@  static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChanne
     int prev = -1000, prev_sf = -1;
     int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
         ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
-        : (avctx->bit_rate / avctx->channels);
+        : (avctx->bit_rate / avctx->ch_layout.nb_channels);
 
     frame_bit_rate *= 1.15f;
 
@@ -693,14 +693,14 @@  static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelEleme
     const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
 
     int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
-        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels)
+        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
         * (lambda / 120.f);
 
     /** Keep this in sync with twoloop's cutoff selection */
     float rate_bandwidth_multiplier = 1.5f;
     int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
         ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
-        : (avctx->bit_rate / avctx->channels);
+        : (avctx->bit_rate / avctx->ch_layout.nb_channels);
 
     frame_bit_rate *= 1.15f;
 
diff --git a/libavcodec/aaccoder_twoloop.h b/libavcodec/aaccoder_twoloop.h
index 8e1bc88a85..0504a916ad 100644
--- a/libavcodec/aaccoder_twoloop.h
+++ b/libavcodec/aaccoder_twoloop.h
@@ -71,7 +71,7 @@  static void search_for_quantizers_twoloop(AVCodecContext *avctx,
 {
     int start = 0, i, w, w2, g, recomprd;
     int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
-        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels)
+        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
         * (lambda / 120.f);
     int refbits = destbits;
     int toomanybits, toofewbits;
@@ -186,7 +186,7 @@  static void search_for_quantizers_twoloop(AVCodecContext *avctx,
         float rate_bandwidth_multiplier = 1.5f;
         int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
             ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
-            : (avctx->bit_rate / avctx->channels);
+            : (avctx->bit_rate / avctx->ch_layout.nb_channels);
 
         /** Compensate for extensions that increase efficiency */
         if (s->options.pns || s->options.intensity_stereo)
diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c
index 4dfb38ac83..611f9e4be1 100644
--- a/libavcodec/aacdec.c
+++ b/libavcodec/aacdec.c
@@ -566,7 +566,10 @@  const AVCodec ff_aac_decoder = {
     },
     .capabilities    = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
     .caps_internal   = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
+#if FF_API_OLD_CHANNEL_LAYOUT
     .channel_layouts = aac_channel_layout,
+#endif
+    .ch_layouts      = aac_ch_layout,
     .flush = flush,
     .priv_class      = &aac_decoder_class,
     .profiles        = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
@@ -591,7 +594,10 @@  const AVCodec ff_aac_latm_decoder = {
     },
     .capabilities    = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
     .caps_internal   = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
+#if FF_API_OLD_CHANNEL_LAYOUT
     .channel_layouts = aac_channel_layout,
+#endif
+    .ch_layouts      = aac_ch_layout,
     .flush = flush,
     .profiles        = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
 };
diff --git a/libavcodec/aacdec_fixed.c b/libavcodec/aacdec_fixed.c
index cad2fd6adc..8af7e994ae 100644
--- a/libavcodec/aacdec_fixed.c
+++ b/libavcodec/aacdec_fixed.c
@@ -464,7 +464,10 @@  const AVCodec ff_aac_fixed_decoder = {
     },
     .capabilities    = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
     .caps_internal   = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
+#if FF_API_OLD_CHANNEL_LAYOUT
     .channel_layouts = aac_channel_layout,
+#endif
+    .ch_layouts      = aac_ch_layout,
     .profiles        = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
     .flush = flush,
 };
diff --git a/libavcodec/aacdec_template.c b/libavcodec/aacdec_template.c
index 7149b331ae..387a4acfce 100644
--- a/libavcodec/aacdec_template.c
+++ b/libavcodec/aacdec_template.c
@@ -174,7 +174,7 @@  static int frame_configure_elements(AVCodecContext *avctx)
 
     /* get output buffer */
     av_frame_unref(ac->frame);
-    if (!avctx->channels)
+    if (!avctx->ch_layout.nb_channels)
         return 1;
 
     ac->frame->nb_samples = 2048;
@@ -182,7 +182,7 @@  static int frame_configure_elements(AVCodecContext *avctx)
         return ret;
 
     /* map output channel pointers to AVFrame data */
-    for (ch = 0; ch < avctx->channels; ch++) {
+    for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
         if (ac->output_element[ch])
             ac->output_element[ch]->ret = (INTFLOAT *)ac->frame->extended_data[ch];
     }
@@ -517,8 +517,7 @@  static int push_output_configuration(AACContext *ac) {
 static void pop_output_configuration(AACContext *ac) {
     if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
         ac->oc[1] = ac->oc[0];
-        ac->avctx->channels = ac->oc[1].channels;
-        ac->avctx->channel_layout = ac->oc[1].channel_layout;
+        ac->avctx->ch_layout = ac->oc[1].ch_layout;
         output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
                          ac->oc[1].status, 0);
     }
@@ -555,7 +554,14 @@  static int output_configure(AACContext *ac,
     }
     // Try to sniff a reasonable channel order, otherwise output the
     // channels in the order the PCE declared them.
-    if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (avctx->request_channel_layout == AV_CH_LAYOUT_NATIVE)
+        ac->output_channel_order = CHANNEL_ORDER_CODED;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
+    if (ac->output_channel_order == CHANNEL_ORDER_DEFAULT)
         layout = sniff_channel_order(layout_map, tags);
     for (i = 0; i < tags; i++) {
         int type =     layout_map[i][0];
@@ -577,9 +583,22 @@  static int output_configure(AACContext *ac,
         }
     }
 
-    if (layout) avctx->channel_layout = layout;
-                            ac->oc[1].channel_layout = layout;
-    avctx->channels       = ac->oc[1].channels       = channels;
+    av_channel_layout_uninit(&ac->oc[1].ch_layout);
+    if (layout)
+        av_channel_layout_from_mask(&ac->oc[1].ch_layout, layout);
+    else {
+        ac->oc[1].ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+        ac->oc[1].ch_layout.nb_channels = channels;
+    }
+
+    av_channel_layout_copy(&avctx->ch_layout, &ac->oc[1].ch_layout);
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+    avctx->channels = avctx->ch_layout.nb_channels;
+    avctx->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
+                            avctx->ch_layout.u.mask : 0;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
     ac->oc[1].status = oc_type;
 
     if (get_new_frame) {
@@ -1292,12 +1311,12 @@  static av_cold int aac_decode_init(AVCodecContext *avctx)
 
         sr = sample_rate_idx(avctx->sample_rate);
         ac->oc[1].m4ac.sampling_index = sr;
-        ac->oc[1].m4ac.channels = avctx->channels;
+        ac->oc[1].m4ac.channels = avctx->ch_layout.nb_channels;
         ac->oc[1].m4ac.sbr = -1;
         ac->oc[1].m4ac.ps = -1;
 
         for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
-            if (ff_mpeg4audio_channels[i] == avctx->channels)
+            if (ff_mpeg4audio_channels[i] == avctx->ch_layout.nb_channels)
                 break;
         if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
             i = 0;
@@ -1315,7 +1334,7 @@  static av_cold int aac_decode_init(AVCodecContext *avctx)
         }
     }
 
-    if (avctx->channels > MAX_CHANNELS) {
+    if (avctx->ch_layout.nb_channels > MAX_CHANNELS) {
         av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
         return AVERROR_INVALIDDATA;
     }
@@ -2556,7 +2575,8 @@  static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
             av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
             skip_bits_long(gb, 8 * cnt - 4);
             return res;
-        } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
+        } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED &&
+                   ac->avctx->ch_layout.nb_channels == 1) {
             ac->oc[1].m4ac.sbr = 1;
             ac->oc[1].m4ac.ps = 1;
             ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
@@ -3264,7 +3284,7 @@  static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
         if (avctx->debug & FF_DEBUG_STARTCODE)
             av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
 
-        if (!avctx->channels && elem_type != TYPE_PCE) {
+        if (!avctx->ch_layout.nb_channels && elem_type != TYPE_PCE) {
             err = AVERROR_INVALIDDATA;
             goto fail;
         }
@@ -3385,7 +3405,7 @@  static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
         }
     }
 
-    if (!avctx->channels) {
+    if (!avctx->ch_layout.nb_channels) {
         *got_frame_ptr = 0;
         return 0;
     }
@@ -3419,7 +3439,8 @@  static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
 
     /* for dual-mono audio (SCE + SCE) */
     is_dmono = ac->dmono_mode && sce_count == 2 &&
-               ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
+               !av_channel_layout_compare(&ac->oc[1].ch_layout,
+                                          &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO);
     if (is_dmono) {
         if (ac->dmono_mode == 1)
             ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
@@ -3553,6 +3574,14 @@  static const AVOption options[] = {
     {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
     {"both", "Select both channels",     0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
 
+    { "channel_order", "Order in which the channels are to be exported",
+        offsetof(AACContext, output_channel_order), AV_OPT_TYPE_INT,
+        { .i64 = CHANNEL_ORDER_DEFAULT }, 0, 1, AACDEC_FLAGS, "channel_order" },
+      { "default", "normal libavcodec channel order", 0, AV_OPT_TYPE_CONST,
+        { .i64 = CHANNEL_ORDER_DEFAULT }, .flags = AACDEC_FLAGS, "channel_order" },
+      { "coded",    "order in which the channels are coded in the bitstream",
+        0, AV_OPT_TYPE_CONST, { .i64 = CHANNEL_ORDER_CODED }, .flags = AACDEC_FLAGS, "channel_order" },
+
     {NULL},
 };
 
diff --git a/libavcodec/aacdectab.h b/libavcodec/aacdectab.h
index c54a3eb943..e03026806d 100644
--- a/libavcodec/aacdectab.h
+++ b/libavcodec/aacdectab.h
@@ -72,6 +72,7 @@  static const uint8_t aac_channel_layout_map[16][16][3] = {
     /* TODO: Add 7+1 TOP configuration */
 };
 
+#if FF_API_OLD_CHANNEL_LAYOUT
 static const uint64_t aac_channel_layout[16] = {
     AV_CH_LAYOUT_MONO,
     AV_CH_LAYOUT_STEREO,
@@ -89,5 +90,24 @@  static const uint64_t aac_channel_layout[16] = {
     0,
     /* AV_CH_LAYOUT_7POINT1_TOP, */
 };
+#endif
+
+static const AVChannelLayout aac_ch_layout[16] = {
+    AV_CHANNEL_LAYOUT_MONO,
+    AV_CHANNEL_LAYOUT_STEREO,
+    AV_CHANNEL_LAYOUT_SURROUND,
+    AV_CHANNEL_LAYOUT_4POINT0,
+    AV_CHANNEL_LAYOUT_5POINT0_BACK,
+    AV_CHANNEL_LAYOUT_5POINT1_BACK,
+    AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK,
+    { 0 },
+    { 0 },
+    { 0 },
+    AV_CHANNEL_LAYOUT_6POINT1,
+    AV_CHANNEL_LAYOUT_7POINT1,
+    AV_CHANNEL_LAYOUT_22POINT2,
+    { 0 },
+    /* AV_CHANNEL_LAYOUT_7POINT1_TOP, */
+};
 
 #endif /* AVCODEC_AACDECTAB_H */
diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index a1004c3e98..be157bb709 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -962,11 +962,11 @@  static av_cold int aac_encode_init(AVCodecContext *avctx)
     s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
 
     /* Channel map and unspecified bitrate guessing */
-    s->channels = avctx->channels;
+    s->channels = avctx->ch_layout.nb_channels;
 
     s->needs_pce = 1;
     for (i = 0; i < FF_ARRAY_ELEMS(aac_normal_chan_layouts); i++) {
-        if (avctx->channel_layout == aac_normal_chan_layouts[i]) {
+        if (!av_channel_layout_compare(&avctx->ch_layout, &aac_normal_chan_layouts[i])) {
             s->needs_pce = s->options.pce;
             break;
         }
@@ -975,10 +975,13 @@  static av_cold int aac_encode_init(AVCodecContext *avctx)
     if (s->needs_pce) {
         char buf[64];
         for (i = 0; i < FF_ARRAY_ELEMS(aac_pce_configs); i++)
-            if (avctx->channel_layout == aac_pce_configs[i].layout)
+            if (!av_channel_layout_compare(&avctx->ch_layout, &aac_pce_configs[i].layout))
                 break;
-        av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
-        ERROR_IF(i == FF_ARRAY_ELEMS(aac_pce_configs), "Unsupported channel layout \"%s\"\n", buf);
+        av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
+        if (i == FF_ARRAY_ELEMS(aac_pce_configs)) {
+            av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout \"%s\"\n", buf);
+            return AVERROR(EINVAL);
+        }
         av_log(avctx, AV_LOG_INFO, "Using a PCE to encode channel layout \"%s\"\n", buf);
         s->pce = aac_pce_configs[i];
         s->reorder_map = s->pce.reorder_map;
diff --git a/libavcodec/aacenc.h b/libavcodec/aacenc.h
index b106910ce0..a001c7ca60 100644
--- a/libavcodec/aacenc.h
+++ b/libavcodec/aacenc.h
@@ -94,7 +94,7 @@  typedef struct AACQuantizeBandCostCacheEntry {
 } AACQuantizeBandCostCacheEntry;
 
 typedef struct AACPCEInfo {
-    int64_t layout;
+    AVChannelLayout layout;
     int num_ele[4];                              ///< front, side, back, lfe
     int pairing[3][8];                           ///< front, side, back
     int index[4][8];                             ///< front, side, back, lfe
@@ -139,7 +139,7 @@  typedef struct AACPCEInfo {
  */
 static const AACPCEInfo aac_pce_configs[] = {
     {
-        .layout = AV_CH_LAYOUT_MONO,
+        .layout = AV_CHANNEL_LAYOUT_MONO,
         .num_ele = { 1, 0, 0, 0 },
         .pairing = { { 0 }, },
         .index = { { 0 }, },
@@ -147,7 +147,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0 },
     },
     {
-        .layout = AV_CH_LAYOUT_STEREO,
+        .layout = AV_CHANNEL_LAYOUT_STEREO,
         .num_ele = { 1, 0, 0, 0 },
         .pairing = { { 1 }, },
         .index = { { 0 }, },
@@ -155,7 +155,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1 },
     },
     {
-        .layout = AV_CH_LAYOUT_2POINT1,
+        .layout = AV_CHANNEL_LAYOUT_2POINT1,
         .num_ele = { 1, 0, 0, 1 },
         .pairing = { { 1 }, },
         .index = { { 0 },{ 0 },{ 0 },{ 0 } },
@@ -163,7 +163,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2 },
     },
     {
-        .layout = AV_CH_LAYOUT_2_1,
+        .layout = AV_CHANNEL_LAYOUT_2_1,
         .num_ele = { 1, 0, 1, 0 },
         .pairing = { { 1 },{ 0 },{ 0 } },
         .index = { { 0 },{ 0 },{ 0 }, },
@@ -171,7 +171,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2 },
     },
     {
-        .layout = AV_CH_LAYOUT_SURROUND,
+        .layout = AV_CHANNEL_LAYOUT_SURROUND,
         .num_ele = { 2, 0, 0, 0 },
         .pairing = { { 1, 0 }, },
         .index = { { 0, 0 }, },
@@ -179,7 +179,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2 },
     },
     {
-        .layout = AV_CH_LAYOUT_3POINT1,
+        .layout = AV_CHANNEL_LAYOUT_3POINT1,
         .num_ele = { 2, 0, 0, 1 },
         .pairing = { { 1, 0 }, },
         .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
@@ -187,7 +187,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3 },
     },
     {
-        .layout = AV_CH_LAYOUT_4POINT0,
+        .layout = AV_CHANNEL_LAYOUT_4POINT0,
         .num_ele = { 2, 0, 1, 0 },
         .pairing = { { 1, 0 }, { 0 }, { 0 }, },
         .index = { { 0, 0 }, { 0 }, { 1 } },
@@ -195,7 +195,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = {  0, 1, 2, 3 },
     },
     {
-        .layout = AV_CH_LAYOUT_4POINT1,
+        .layout = AV_CHANNEL_LAYOUT_4POINT1,
         .num_ele = { 2, 1, 1, 0 },
         .pairing = { { 1, 0 }, { 0 }, { 0 }, },
         .index = { { 0, 0 }, { 1 }, { 2 }, { 0 } },
@@ -203,7 +203,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4 },
     },
     {
-        .layout = AV_CH_LAYOUT_2_2,
+        .layout = AV_CHANNEL_LAYOUT_2_2,
         .num_ele = { 1, 1, 0, 0 },
         .pairing = { { 1 }, { 1 }, },
         .index = { { 0 }, { 1 }, },
@@ -211,7 +211,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3 },
     },
     {
-        .layout = AV_CH_LAYOUT_QUAD,
+        .layout = AV_CHANNEL_LAYOUT_QUAD,
         .num_ele = { 1, 0, 1, 0 },
         .pairing = { { 1 }, { 0 }, { 1 }, },
         .index = { { 0 }, { 0 }, { 1 } },
@@ -219,7 +219,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3 },
     },
     {
-        .layout = AV_CH_LAYOUT_5POINT0,
+        .layout = AV_CHANNEL_LAYOUT_5POINT0,
         .num_ele = { 2, 1, 0, 0 },
         .pairing = { { 1, 0 }, { 1 }, },
         .index = { { 0, 0 }, { 1 } },
@@ -227,7 +227,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4 },
     },
     {
-        .layout = AV_CH_LAYOUT_5POINT1,
+        .layout = AV_CHANNEL_LAYOUT_5POINT1,
         .num_ele = { 2, 1, 1, 0 },
         .pairing = { { 1, 0 }, { 0 }, { 1 }, },
         .index = { { 0, 0 }, { 1 }, { 1 } },
@@ -235,7 +235,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5 },
     },
     {
-        .layout = AV_CH_LAYOUT_5POINT0_BACK,
+        .layout = AV_CHANNEL_LAYOUT_5POINT0_BACK,
         .num_ele = { 2, 0, 1, 0 },
         .pairing = { { 1, 0 }, { 0 }, { 1 } },
         .index = { { 0, 0 }, { 0 }, { 1 } },
@@ -243,7 +243,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4 },
     },
     {
-        .layout = AV_CH_LAYOUT_5POINT1_BACK,
+        .layout = AV_CHANNEL_LAYOUT_5POINT1_BACK,
         .num_ele = { 2, 1, 1, 0 },
         .pairing = { { 1, 0 }, { 0 }, { 1 }, },
         .index = { { 0, 0 }, { 1 }, { 1 } },
@@ -251,7 +251,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5 },
     },
     {
-        .layout = AV_CH_LAYOUT_6POINT0,
+        .layout = AV_CHANNEL_LAYOUT_6POINT0,
         .num_ele = { 2, 1, 1, 0 },
         .pairing = { { 1, 0 }, { 1 }, { 0 }, },
         .index = { { 0, 0 }, { 1 }, { 1 } },
@@ -259,7 +259,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5 },
     },
     {
-        .layout = AV_CH_LAYOUT_6POINT0_FRONT,
+        .layout = AV_CHANNEL_LAYOUT_6POINT0_FRONT,
         .num_ele = { 2, 1, 0, 0 },
         .pairing = { { 1, 1 }, { 1 } },
         .index = { { 1, 0 }, { 2 }, },
@@ -267,7 +267,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5 },
     },
     {
-        .layout = AV_CH_LAYOUT_HEXAGONAL,
+        .layout = AV_CHANNEL_LAYOUT_HEXAGONAL,
         .num_ele = { 2, 0, 2, 0 },
         .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
         .index = { { 0, 0 },{ 0 },{ 1, 1 } },
@@ -275,7 +275,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5 },
     },
     {
-        .layout = AV_CH_LAYOUT_6POINT1,
+        .layout = AV_CHANNEL_LAYOUT_6POINT1,
         .num_ele = { 2, 1, 2, 0 },
         .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
         .index = { { 0, 0 },{ 1 },{ 1, 2 } },
@@ -283,7 +283,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
     },
     {
-        .layout = AV_CH_LAYOUT_6POINT1_BACK,
+        .layout = AV_CHANNEL_LAYOUT_6POINT1_BACK,
         .num_ele = { 2, 1, 2, 0 },
         .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
         .index = { { 0, 0 }, { 1 }, { 1, 2 } },
@@ -291,7 +291,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
     },
     {
-        .layout = AV_CH_LAYOUT_6POINT1_FRONT,
+        .layout = AV_CHANNEL_LAYOUT_6POINT1_FRONT,
         .num_ele = { 2, 1, 2, 0 },
         .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
         .index = { { 0, 0 }, { 1 }, { 1, 2 } },
@@ -299,7 +299,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
     },
     {
-        .layout = AV_CH_LAYOUT_7POINT0,
+        .layout = AV_CHANNEL_LAYOUT_7POINT0,
         .num_ele = { 2, 1, 1, 0 },
         .pairing = { { 1, 0 }, { 1 }, { 1 }, },
         .index = { { 0, 0 }, { 1 }, { 2 }, },
@@ -307,7 +307,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
     },
     {
-        .layout = AV_CH_LAYOUT_7POINT0_FRONT,
+        .layout = AV_CHANNEL_LAYOUT_7POINT0_FRONT,
         .num_ele = { 2, 1, 1, 0 },
         .pairing = { { 1, 0 }, { 1 }, { 1 }, },
         .index = { { 0, 0 }, { 1 }, { 2 }, },
@@ -315,7 +315,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
     },
     {
-        .layout = AV_CH_LAYOUT_7POINT1,
+        .layout = AV_CHANNEL_LAYOUT_7POINT1,
         .num_ele = { 2, 1, 2, 0 },
         .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
         .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
@@ -323,7 +323,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
     },
     {
-        .layout = AV_CH_LAYOUT_7POINT1_WIDE,
+        .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE,
         .num_ele = { 2, 1, 2, 0 },
         .pairing = { { 1, 0 }, { 0 },{  1, 1 }, },
         .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
@@ -331,7 +331,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
     },
     {
-        .layout = AV_CH_LAYOUT_7POINT1_WIDE_BACK,
+        .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK,
         .num_ele = { 2, 1, 2, 0 },
         .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
         .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
@@ -339,7 +339,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
     },
     {
-        .layout = AV_CH_LAYOUT_OCTAGONAL,
+        .layout = AV_CHANNEL_LAYOUT_OCTAGONAL,
         .num_ele = { 2, 1, 2, 0 },
         .pairing = { { 1, 0 }, { 1 }, { 1, 0 }, },
         .index = { { 0, 0 }, { 1 }, { 2, 1 } },
@@ -347,7 +347,8 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
     },
     {   /* Meant for order 2/mixed ambisonics */
-        .layout = AV_CH_LAYOUT_OCTAGONAL | AV_CH_TOP_CENTER,
+        .layout = { .order = AV_CHANNEL_ORDER_NATIVE, .nb_channels = 9,
+                    .u.mask = AV_CH_LAYOUT_OCTAGONAL | AV_CH_TOP_CENTER },
         .num_ele = { 2, 2, 2, 0 },
         .pairing = { { 1, 0 }, { 1, 0 }, { 1, 0 }, },
         .index = { { 0, 0 }, { 1, 1 }, { 2, 2 } },
@@ -355,8 +356,9 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8 },
     },
     {   /* Meant for order 2/mixed ambisonics */
-        .layout = AV_CH_LAYOUT_6POINT0_FRONT | AV_CH_BACK_CENTER |
-                  AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_TOP_CENTER,
+        .layout = { .order = AV_CHANNEL_ORDER_NATIVE, .nb_channels = 10,
+                    .u.mask = AV_CH_LAYOUT_6POINT0_FRONT | AV_CH_BACK_CENTER |
+                              AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_TOP_CENTER },
         .num_ele = { 2, 2, 2, 0 },
         .pairing = { { 1, 1 }, { 1, 0 }, { 1, 0 }, },
         .index = { { 0, 1 }, { 2, 0 }, { 3, 1 } },
@@ -364,7 +366,7 @@  static const AACPCEInfo aac_pce_configs[] = {
         .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
     },
     {
-        .layout = AV_CH_LAYOUT_HEXADECAGONAL,
+        .layout = AV_CHANNEL_LAYOUT_HEXADECAGONAL,
         .num_ele = { 4, 2, 4, 0 },
         .pairing = { { 1, 0, 1, 0 }, { 1, 1 }, { 1, 0, 1, 0 }, },
         .index = { { 0, 0, 1, 1 }, { 2, 3 }, { 4, 2, 5, 3 } },
diff --git a/libavcodec/aacenctab.h b/libavcodec/aacenctab.h
index 33cb7ae95b..2d96621945 100644
--- a/libavcodec/aacenctab.h
+++ b/libavcodec/aacenctab.h
@@ -45,14 +45,14 @@  extern const uint8_t *const ff_aac_swb_size_128[];
 extern const int      ff_aac_swb_size_128_len;
 
 /* Supported layouts without using a PCE */
-static const int64_t aac_normal_chan_layouts[7] = {
-    AV_CH_LAYOUT_MONO,
-    AV_CH_LAYOUT_STEREO,
-    AV_CH_LAYOUT_SURROUND,
-    AV_CH_LAYOUT_4POINT0,
-    AV_CH_LAYOUT_5POINT0_BACK,
-    AV_CH_LAYOUT_5POINT1_BACK,
-    AV_CH_LAYOUT_7POINT1,
+static const AVChannelLayout aac_normal_chan_layouts[7] = {
+    AV_CHANNEL_LAYOUT_MONO,
+    AV_CHANNEL_LAYOUT_STEREO,
+    AV_CHANNEL_LAYOUT_SURROUND,
+    AV_CHANNEL_LAYOUT_4POINT0,
+    AV_CHANNEL_LAYOUT_5POINT0_BACK,
+    AV_CHANNEL_LAYOUT_5POINT1_BACK,
+    AV_CHANNEL_LAYOUT_7POINT1,
 };
 
 /** default channel configurations */
diff --git a/libavcodec/aacpsy.c b/libavcodec/aacpsy.c
index 0c64f2c7b0..4c5ab2c9d5 100644
--- a/libavcodec/aacpsy.c
+++ b/libavcodec/aacpsy.c
@@ -263,13 +263,13 @@  static av_cold void lame_window_init(AacPsyContext *ctx, AVCodecContext *avctx)
 {
     int i, j;
 
-    for (i = 0; i < avctx->channels; i++) {
+    for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
         AacPsyChannel *pch = &ctx->ch[i];
 
         if (avctx->flags & AV_CODEC_FLAG_QSCALE)
             pch->attack_threshold = psy_vbr_map[avctx->global_quality / FF_QP2LAMBDA].st_lrm;
         else
-            pch->attack_threshold = lame_calc_attack_threshold(avctx->bit_rate / avctx->channels / 1000);
+            pch->attack_threshold = lame_calc_attack_threshold(avctx->bit_rate / avctx->ch_layout.nb_channels / 1000);
 
         for (j = 0; j < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; j++)
             pch->prev_energy_subshort[j] = 10.0f;
@@ -303,7 +303,7 @@  static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
     float bark;
     int i, j, g, start;
     float prev, minscale, minath, minsnr, pe_min;
-    int chan_bitrate = ctx->avctx->bit_rate / ((ctx->avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : ctx->avctx->channels);
+    int chan_bitrate = ctx->avctx->bit_rate / ((ctx->avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : ctx->avctx->ch_layout.nb_channels);
 
     const int bandwidth    = ctx->cutoff ? ctx->cutoff : AAC_CUTOFF(ctx->avctx);
     const float num_bark   = calc_bark((float)bandwidth);
@@ -370,7 +370,7 @@  static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
         }
     }
 
-    pctx->ch = av_calloc(ctx->avctx->channels, sizeof(*pctx->ch));
+    pctx->ch = av_calloc(ctx->avctx->ch_layout.nb_channels, sizeof(*pctx->ch));
     if (!pctx->ch) {
         av_freep(&ctx->model_priv_data);
         return AVERROR(ENOMEM);
diff --git a/libavcodec/psymodel.c b/libavcodec/psymodel.c
index 93c8408297..890b13e1c8 100644
--- a/libavcodec/psymodel.c
+++ b/libavcodec/psymodel.c
@@ -35,7 +35,7 @@  av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
     int i, j, k = 0;
 
     ctx->avctx = avctx;
-    ctx->ch        = av_calloc(avctx->channels, 2 * sizeof(ctx->ch[0]));
+    ctx->ch        = av_calloc(avctx->ch_layout.nb_channels, 2 * sizeof(ctx->ch[0]));
     ctx->group     = av_calloc(num_groups, sizeof(ctx->group[0]));
     ctx->bands     = av_malloc_array (sizeof(ctx->bands[0]),      num_lens);
     ctx->num_bands = av_malloc_array (sizeof(ctx->num_bands[0]),  num_lens);
@@ -120,13 +120,13 @@  av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *av
                                                  FF_FILTER_MODE_LOWPASS, FILT_ORDER,
                                                  cutoff_coeff, 0.0, 0.0);
         if (ctx->fcoeffs) {
-            ctx->fstate = av_calloc(avctx->channels, sizeof(ctx->fstate[0]));
+            ctx->fstate = av_calloc(avctx->ch_layout.nb_channels, sizeof(ctx->fstate[0]));
             if (!ctx->fstate) {
                 av_free(ctx->fcoeffs);
                 av_free(ctx);
                 return NULL;
             }
-            for (i = 0; i < avctx->channels; i++)
+            for (i = 0; i < avctx->ch_layout.nb_channels; i++)
                 ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
         }
     }
@@ -154,7 +154,7 @@  av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
     int i;
     ff_iir_filter_free_coeffsp(&ctx->fcoeffs);
     if (ctx->fstate)
-        for (i = 0; i < ctx->avctx->channels; i++)
+        for (i = 0; i < ctx->avctx->ch_layout.nb_channels; i++)
             ff_iir_filter_free_statep(&ctx->fstate[i]);
     av_freep(&ctx->fstate);
     av_free(ctx);
diff --git a/libavcodec/psymodel.h b/libavcodec/psymodel.h
index e5f917d495..22899a38d9 100644
--- a/libavcodec/psymodel.h
+++ b/libavcodec/psymodel.h
@@ -41,7 +41,7 @@ 
 #define AAC_CUTOFF(s) ( \
     (s->flags & AV_CODEC_FLAG_QSCALE) \
     ? s->sample_rate / 2 \
-    : AAC_CUTOFF_FROM_BITRATE(s->bit_rate, s->channels, s->sample_rate) \
+    : AAC_CUTOFF_FROM_BITRATE(s->bit_rate, s->ch_layout.nb_channels, s->sample_rate) \
 )
 
 /**