diff mbox series

[FFmpeg-devel,200/281] dca: convert to new channel layout API

Message ID 20220113020242.661-21-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: James Almer <jamrial@gmail.com>
---
 libavcodec/dca_core.c |  6 ++--
 libavcodec/dca_lbr.c  | 20 +++++++-----
 libavcodec/dca_xll.c  |  2 +-
 libavcodec/dcadec.c   | 71 ++++++++++++++++++++++++++++++-------------
 libavcodec/dcadec.h   |  7 +++++
 libavcodec/dcaenc.c   | 39 ++++++++++++++++--------
 6 files changed, 100 insertions(+), 45 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/dca_core.c b/libavcodec/dca_core.c
index 758e3447a6..ebe44c2d15 100644
--- a/libavcodec/dca_core.c
+++ b/libavcodec/dca_core.c
@@ -2148,7 +2148,7 @@  static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
                                        nsamples, s->ch_mask);
     }
 
-    for (i = 0; i < avctx->channels; i++) {
+    for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
         int32_t *samples = s->output_samples[s->ch_remap[i]];
         int32_t *plane = (int32_t *)frame->extended_data[i];
         for (n = 0; n < nsamples; n++)
@@ -2180,11 +2180,11 @@  static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
         return ret;
 
     // Build reverse speaker to channel mapping
-    for (i = 0; i < avctx->channels; i++)
+    for (i = 0; i < avctx->ch_layout.nb_channels; i++)
         output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
 
     // Allocate space for extra channels
-    nchannels = av_popcount(s->ch_mask) - avctx->channels;
+    nchannels = av_popcount(s->ch_mask) - avctx->ch_layout.nb_channels;
     if (nchannels > 0) {
         av_fast_malloc(&s->output_buffer, &s->output_size,
                        nsamples * nchannels * sizeof(float));
diff --git a/libavcodec/dca_lbr.c b/libavcodec/dca_lbr.c
index 481a8df7e6..c11f2f7e18 100644
--- a/libavcodec/dca_lbr.c
+++ b/libavcodec/dca_lbr.c
@@ -107,10 +107,6 @@  static const uint8_t lfe_index[7] = {
     1, 2, 3, 0, 1, 2, 3
 };
 
-static const uint8_t channel_counts[7] = {
-    1, 2, 3, 2, 3, 4, 5
-};
-
 static const uint16_t channel_layouts[7] = {
     AV_CH_LAYOUT_MONO,
     AV_CH_LAYOUT_STEREO,
@@ -1731,9 +1727,8 @@  int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame)
     AVCodecContext *avctx = s->avctx;
     int i, ret, nchannels, ch_conf = (s->ch_mask & 0x7) - 1;
     const int8_t *reorder;
+    uint64_t channel_mask = channel_layouts[ch_conf];
 
-    avctx->channel_layout = channel_layouts[ch_conf];
-    avctx->channels = nchannels = channel_counts[ch_conf];
     avctx->sample_rate = s->sample_rate;
     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
     avctx->bits_per_raw_sample = 0;
@@ -1741,13 +1736,22 @@  int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame)
     avctx->bit_rate = s->bit_rate_scaled;
 
     if (s->flags & LBR_FLAG_LFE_PRESENT) {
-        avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
-        avctx->channels++;
+        channel_mask |= AV_CH_LOW_FREQUENCY;
         reorder = channel_reorder_lfe[ch_conf];
     } else {
         reorder = channel_reorder_nolfe[ch_conf];
     }
 
+    av_channel_layout_uninit(&avctx->ch_layout);
+    av_channel_layout_from_mask(&avctx->ch_layout, channel_mask);
+#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
+
     frame->nb_samples = 1024 << s->freq_range;
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
diff --git a/libavcodec/dca_xll.c b/libavcodec/dca_xll.c
index ab14dbcc88..a4ff3f9aa2 100644
--- a/libavcodec/dca_xll.c
+++ b/libavcodec/dca_xll.c
@@ -1442,7 +1442,7 @@  int ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame)
                                        s->output_mask);
     }
 
-    for (i = 0; i < avctx->channels; i++) {
+    for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
         int32_t *samples = s->output_samples[ch_remap[i]];
         if (frame->format == AV_SAMPLE_FMT_S16P) {
             int16_t *plane = (int16_t *)frame->extended_data[i];
diff --git a/libavcodec/dcadec.c b/libavcodec/dcadec.c
index 43694b51f7..239ed9ac91 100644
--- a/libavcodec/dcadec.c
+++ b/libavcodec/dcadec.c
@@ -42,13 +42,17 @@  int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask
         13, 14, 3, 9, 10, 11, 12, 14, 16, 15, 17, 8, 4,  5,
     };
 
+    DCAContext *s = avctx->priv_data;
+
     int dca_ch, wav_ch, nchannels = 0;
 
-    if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
+    av_channel_layout_uninit(&avctx->ch_layout);
+    if (s->output_channel_order == CHANNEL_ORDER_CODED) {
         for (dca_ch = 0; dca_ch < DCA_SPEAKER_COUNT; dca_ch++)
             if (dca_mask & (1U << dca_ch))
                 ch_remap[nchannels++] = dca_ch;
-        avctx->channel_layout = dca_mask;
+        avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+        avctx->ch_layout.nb_channels = nchannels;
     } else {
         int wav_mask = 0;
         int wav_map[18];
@@ -70,10 +74,18 @@  int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask
         for (wav_ch = 0; wav_ch < 18; wav_ch++)
             if (wav_mask & (1 << wav_ch))
                 ch_remap[nchannels++] = wav_map[wav_ch];
-        avctx->channel_layout = wav_mask;
+
+        av_channel_layout_from_mask(&avctx->ch_layout, wav_mask);
     }
 
-    avctx->channels = nchannels;
+#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
+
     return nchannels;
 }
 
@@ -349,23 +361,28 @@  static av_cold int dcadec_init(AVCodecContext *avctx)
 
     s->crctab = av_crc_get_table(AV_CRC_16_CCITT);
 
-    switch (avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE) {
-    case 0:
-        s->request_channel_layout = 0;
-        break;
-    case AV_CH_LAYOUT_STEREO:
-    case AV_CH_LAYOUT_STEREO_DOWNMIX:
-        s->request_channel_layout = DCA_SPEAKER_LAYOUT_STEREO;
-        break;
-    case AV_CH_LAYOUT_5POINT0:
-        s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT0;
-        break;
-    case AV_CH_LAYOUT_5POINT1:
-        s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT1;
-        break;
-    default:
-        av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
-        break;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)
+        s->output_channel_order = CHANNEL_ORDER_CODED;
+
+    if (avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE) {
+        av_channel_layout_uninit(&s->downmix_layout);
+        av_channel_layout_from_mask(&s->downmix_layout, avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE);
+    }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
+    if (s->downmix_layout.nb_channels) {
+        if (!av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO) ||
+            !av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX))
+            s->request_channel_layout = DCA_SPEAKER_LAYOUT_STEREO;
+        else if (!av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0))
+            s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT0;
+        else if (!av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1))
+            s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT1;
+        else
+            av_log(avctx, AV_LOG_WARNING, "Invalid downmix layout\n");
     }
 
     ff_thread_once(&init_static_once, dcadec_init_static);
@@ -378,6 +395,18 @@  static av_cold int dcadec_init(AVCodecContext *avctx)
 
 static const AVOption dcadec_options[] = {
     { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
+
+    { "channel_order", "Order in which the channels are to be exported",
+        OFFSET(output_channel_order), AV_OPT_TYPE_INT,
+        { .i64 = CHANNEL_ORDER_DEFAULT }, 0, 1, PARAM, "channel_order" },
+      { "default", "normal libavcodec channel order", 0, AV_OPT_TYPE_CONST,
+        { .i64 = CHANNEL_ORDER_DEFAULT }, .flags = PARAM, "channel_order" },
+      { "coded",    "order in which the channels are coded in the bitstream",
+        0, AV_OPT_TYPE_CONST, { .i64 = CHANNEL_ORDER_CODED }, .flags = PARAM, "channel_order" },
+
+    { "downmix", "Request a specific channel layout from the decoder", OFFSET(downmix_layout),
+        AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, .flags = PARAM },
+
     { NULL }
 };
 
diff --git a/libavcodec/dcadec.h b/libavcodec/dcadec.h
index 9da8d3b444..7fdb62d310 100644
--- a/libavcodec/dcadec.h
+++ b/libavcodec/dcadec.h
@@ -43,6 +43,11 @@ 
 #define DCA_PACKET_RECOVERY     0x10    ///< Sync error recovery flag
 #define DCA_PACKET_RESIDUAL     0x20    ///< Core valid for residual decoding
 
+enum DCAOutputChannelOrder {
+    CHANNEL_ORDER_DEFAULT,
+    CHANNEL_ORDER_CODED,
+};
+
 typedef struct DCAContext {
     const AVClass   *class;       ///< class for AVOptions
     AVCodecContext  *avctx;
@@ -63,6 +68,8 @@  typedef struct DCAContext {
 
     int     request_channel_layout; ///< Converted from avctx.request_channel_layout
     int     core_only;              ///< Core only decoding flag
+    int     output_channel_order;
+    AVChannelLayout downmix_layout;
 } DCAContext;
 
 int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask);
diff --git a/libavcodec/dcaenc.c b/libavcodec/dcaenc.c
index 884ac896d3..a3aae93dc9 100644
--- a/libavcodec/dcaenc.c
+++ b/libavcodec/dcaenc.c
@@ -163,15 +163,15 @@  static void subband_bufer_free(DCAEncContext *c)
 static int encode_init(AVCodecContext *avctx)
 {
     DCAEncContext *c = avctx->priv_data;
-    uint64_t layout = avctx->channel_layout;
+    AVChannelLayout layout = avctx->ch_layout;
     int i, j, k, min_frame_bits;
     int ret;
 
     if ((ret = subband_bufer_alloc(c)) < 0)
         return ret;
 
-    c->fullband_channels = c->channels = avctx->channels;
-    c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
+    c->fullband_channels = c->channels = layout.nb_channels;
+    c->lfe_channel = (c->channels == 3 || c->channels == 6);
     c->band_interpolation = c->band_interpolation_tab[1];
     c->band_spectrum = c->band_spectrum_tab[1];
     c->worst_quantization_noise = -2047;
@@ -181,19 +181,24 @@  static int encode_init(AVCodecContext *avctx)
     if (ff_dcaadpcm_init(&c->adpcm_ctx))
         return AVERROR(ENOMEM);
 
-    if (!layout) {
+    if (layout.order == AV_CHANNEL_ORDER_UNSPEC) {
         av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
                                       "encoder will guess the layout, but it "
                                       "might be incorrect.\n");
-        layout = av_get_default_channel_layout(avctx->channels);
+        av_channel_layout_default(&layout, layout.nb_channels);
     }
-    switch (layout) {
-    case AV_CH_LAYOUT_MONO:         c->channel_config = 0; break;
-    case AV_CH_LAYOUT_STEREO:       c->channel_config = 2; break;
-    case AV_CH_LAYOUT_2_2:          c->channel_config = 8; break;
-    case AV_CH_LAYOUT_5POINT0:      c->channel_config = 9; break;
-    case AV_CH_LAYOUT_5POINT1:      c->channel_config = 9; break;
-    default:
+
+    if (!av_channel_layout_compare(&layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO))
+        c->channel_config = 0;
+    else if (!av_channel_layout_compare(&layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO))
+        c->channel_config = 2;
+    else if (!av_channel_layout_compare(&layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2))
+        c->channel_config = 8;
+    else if (!av_channel_layout_compare(&layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0))
+        c->channel_config = 9;
+    else if (!av_channel_layout_compare(&layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1))
+        c->channel_config = 9;
+    else {
         av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout!\n");
         return AVERROR_PATCHWELCOME;
     }
@@ -1248,12 +1253,22 @@  const AVCodec ff_dca_encoder = {
     .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
                                                             AV_SAMPLE_FMT_NONE },
     .supported_samplerates = sample_rates,
+#if FF_API_OLD_CHANNEL_LAYOUT
     .channel_layouts       = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
                                                   AV_CH_LAYOUT_STEREO,
                                                   AV_CH_LAYOUT_2_2,
                                                   AV_CH_LAYOUT_5POINT0,
                                                   AV_CH_LAYOUT_5POINT1,
                                                   0 },
+#endif
+    .ch_layouts       = (const AVChannelLayout[]){
+        AV_CHANNEL_LAYOUT_MONO,
+        AV_CHANNEL_LAYOUT_STEREO,
+        AV_CHANNEL_LAYOUT_2_2,
+        AV_CHANNEL_LAYOUT_5POINT0,
+        AV_CHANNEL_LAYOUT_5POINT1,
+        { 0 },
+    },
     .defaults              = defaults,
     .priv_class            = &dcaenc_class,
 };