[FFmpeg-devel] [PATCH] avcodec/atrac3plus: reorder channels to match the output layout

James Almer jamrial at gmail.com
Thu Oct 27 19:49:40 EEST 2022


The order in which the channels are coded in the bitstream do not always follow
the native, bitmask-based order of channels both signaled by the WAV container
and forced by this same decoder. This is the case with layouts containing an
LFE channel, as it's always coded last.

Fixes ticket #9964.

Signed-off-by: James Almer <jamrial at gmail.com>
---
6.1 and 7.1 untested, but much like 5.1 they were wrong before this change.

 libavcodec/atrac3plusdec.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/libavcodec/atrac3plusdec.c b/libavcodec/atrac3plusdec.c
index ee71645a3c..8c3d20af76 100644
--- a/libavcodec/atrac3plusdec.c
+++ b/libavcodec/atrac3plusdec.c
@@ -65,6 +65,7 @@ typedef struct ATRAC3PContext {
 
     int num_channel_blocks;     ///< number of channel blocks
     uint8_t channel_blocks[5];  ///< channel configuration descriptor
+    AVChannelLayout coded_ch_layout; ///< order of channels as coded in the bitstream
 } ATRAC3PContext;
 
 static av_cold int atrac3p_decode_close(AVCodecContext *avctx)
@@ -74,6 +75,8 @@ static av_cold int atrac3p_decode_close(AVCodecContext *avctx)
     av_freep(&ctx->ch_units);
     av_freep(&ctx->fdsp);
 
+    av_channel_layout_uninit(&ctx->coded_ch_layout);
+
     ff_mdct_end(&ctx->mdct_ctx);
     ff_mdct_end(&ctx->ipqf_dct_ctx);
 
@@ -84,6 +87,7 @@ static av_cold int set_channel_params(ATRAC3PContext *ctx,
                                       AVCodecContext *avctx)
 {
     int channels = avctx->ch_layout.nb_channels;
+    int ret;
     memset(ctx->channel_blocks, 0, sizeof(ctx->channel_blocks));
 
     av_channel_layout_uninit(&avctx->ch_layout);
@@ -92,17 +96,20 @@ static av_cold int set_channel_params(ATRAC3PContext *ctx,
         avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
         ctx->num_channel_blocks = 1;
         ctx->channel_blocks[0]  = CH_UNIT_MONO;
+        ctx->coded_ch_layout    = avctx->ch_layout;
         break;
     case 2:
         avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
         ctx->num_channel_blocks = 1;
         ctx->channel_blocks[0]  = CH_UNIT_STEREO;
+        ctx->coded_ch_layout    = avctx->ch_layout;
         break;
     case 3:
         avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND;
         ctx->num_channel_blocks = 2;
         ctx->channel_blocks[0]  = CH_UNIT_STEREO;
         ctx->channel_blocks[1]  = CH_UNIT_MONO;
+        ctx->coded_ch_layout    = avctx->ch_layout;
         break;
     case 4:
         avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT0;
@@ -110,6 +117,7 @@ static av_cold int set_channel_params(ATRAC3PContext *ctx,
         ctx->channel_blocks[0]  = CH_UNIT_STEREO;
         ctx->channel_blocks[1]  = CH_UNIT_MONO;
         ctx->channel_blocks[2]  = CH_UNIT_MONO;
+        ctx->coded_ch_layout    = avctx->ch_layout;
         break;
     case 6:
         avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK;
@@ -118,6 +126,9 @@ static av_cold int set_channel_params(ATRAC3PContext *ctx,
         ctx->channel_blocks[1]  = CH_UNIT_MONO;
         ctx->channel_blocks[2]  = CH_UNIT_STEREO;
         ctx->channel_blocks[3]  = CH_UNIT_MONO;
+        ret = av_channel_layout_from_string(&ctx->coded_ch_layout, "FL+FR+FC+BL+BR+LFE");
+        if (ret < 0)
+            return ret;
         break;
     case 7:
         avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_6POINT1_BACK;
@@ -127,6 +138,9 @@ static av_cold int set_channel_params(ATRAC3PContext *ctx,
         ctx->channel_blocks[2]  = CH_UNIT_STEREO;
         ctx->channel_blocks[3]  = CH_UNIT_MONO;
         ctx->channel_blocks[4]  = CH_UNIT_MONO;
+        ret = av_channel_layout_from_string(&ctx->coded_ch_layout, "FL+FR+FC+BL+BR+BC+LFE");
+        if (ret < 0)
+            return ret;
         break;
     case 8:
         avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1;
@@ -136,6 +150,9 @@ static av_cold int set_channel_params(ATRAC3PContext *ctx,
         ctx->channel_blocks[2]  = CH_UNIT_STEREO;
         ctx->channel_blocks[3]  = CH_UNIT_STEREO;
         ctx->channel_blocks[4]  = CH_UNIT_MONO;
+        ret = av_channel_layout_from_string(&ctx->coded_ch_layout, "FL+FR+FC+BL+BR+SL+SR+LFE");
+        if (ret < 0)
+            return ret;
         break;
     default:
         av_log(avctx, AV_LOG_ERROR,
@@ -377,10 +394,12 @@ static int atrac3p_decode_frame(AVCodecContext *avctx, AVFrame *frame,
         reconstruct_frame(ctx, &ctx->ch_units[ch_block],
                           channels_to_process, avctx);
 
-        for (i = 0; i < channels_to_process; i++)
-            memcpy(samples_p[out_ch_index + i], ctx->outp_buf[i],
+        for (i = 0; i < channels_to_process; i++) {
+            enum AVChannel ch = av_channel_layout_channel_from_index(&ctx->coded_ch_layout, out_ch_index + i);
+            int idx = av_channel_layout_index_from_channel(&frame->ch_layout, ch);
+            memcpy(samples_p[idx], ctx->outp_buf[i],
                    ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p));
-
+        }
         ch_block++;
         out_ch_index += channels_to_process;
     }
-- 
2.38.1



More information about the ffmpeg-devel mailing list