[FFmpeg-devel] [PATCH 185/279] alac: convert to new channel layout API

James Almer jamrial at gmail.com
Wed Dec 8 03:06:40 EET 2021


From: Anton Khirnov <anton at khirnov.net>

Signed-off-by: Vittorio Giovara <vittorio.giovara at gmail.com>
Signed-off-by: Anton Khirnov <anton at khirnov.net>
Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/alac.c      | 16 ++++++----------
 libavcodec/alac_data.c | 20 ++++++++++----------
 libavcodec/alac_data.h |  6 ++++++
 libavcodec/alacenc.c   | 37 +++++++++++++++++++++++++++++--------
 4 files changed, 51 insertions(+), 28 deletions(-)

diff --git a/libavcodec/alac.c b/libavcodec/alac.c
index 67fc2a3e41..db1e725f66 100644
--- a/libavcodec/alac.c
+++ b/libavcodec/alac.c
@@ -576,21 +576,17 @@ static av_cold int alac_decode_init(AVCodecContext * avctx)
     avctx->bits_per_raw_sample = alac->sample_size;
     avctx->sample_rate         = alac->sample_rate;
 
-    if (alac->channels < 1) {
+    if (alac->channels < 1 || alac->channels > ALAC_MAX_CHANNELS) {
         av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
-        alac->channels = avctx->channels;
-    } else {
-        if (alac->channels > ALAC_MAX_CHANNELS)
-            alac->channels = avctx->channels;
-        else
-            avctx->channels = alac->channels;
+        alac->channels = avctx->ch_layout.nb_channels;
     }
-    if (avctx->channels > ALAC_MAX_CHANNELS || avctx->channels <= 0 ) {
+    if (avctx->ch_layout.nb_channels > ALAC_MAX_CHANNELS || avctx->ch_layout.nb_channels <= 0 ) {
         avpriv_report_missing_feature(avctx, "Channel count %d",
-                                      avctx->channels);
+                                      avctx->ch_layout.nb_channels);
         return AVERROR_PATCHWELCOME;
     }
-    avctx->channel_layout = ff_alac_channel_layouts[alac->channels - 1];
+    av_channel_layout_uninit(&avctx->ch_layout);
+    avctx->ch_layout = ff_alac_ch_layouts[alac->channels - 1];
 
     if ((ret = allocate_buffers(alac)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
diff --git a/libavcodec/alac_data.c b/libavcodec/alac_data.c
index 0bcb06c075..d8ed53f444 100644
--- a/libavcodec/alac_data.c
+++ b/libavcodec/alac_data.c
@@ -32,16 +32,16 @@ const uint8_t ff_alac_channel_layout_offsets[ALAC_MAX_CHANNELS][ALAC_MAX_CHANNEL
     { 2, 6, 7, 0, 1, 4, 5, 3 }
 };
 
-const uint64_t ff_alac_channel_layouts[ALAC_MAX_CHANNELS + 1] = {
-    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_6POINT1_BACK,
-    AV_CH_LAYOUT_7POINT1_WIDE_BACK,
-    0
+const AVChannelLayout ff_alac_ch_layouts[ALAC_MAX_CHANNELS + 1] = {
+    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_6POINT1_BACK,
+    AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK,
+    { 0 }
 };
 
 const enum AlacRawDataBlockType ff_alac_channel_elements[ALAC_MAX_CHANNELS][5] = {
diff --git a/libavcodec/alac_data.h b/libavcodec/alac_data.h
index 650d6dcd15..a68a5f2648 100644
--- a/libavcodec/alac_data.h
+++ b/libavcodec/alac_data.h
@@ -23,6 +23,8 @@
 
 #include <stdint.h>
 
+#include "libavutil/channel_layout.h"
+
 enum AlacRawDataBlockType {
     /* At the moment, only SCE, CPE, LFE, and END are recognized. */
     TYPE_SCE,
@@ -39,7 +41,11 @@ enum AlacRawDataBlockType {
 
 extern const uint8_t ff_alac_channel_layout_offsets[ALAC_MAX_CHANNELS][ALAC_MAX_CHANNELS];
 
+extern const AVChannelLayout ff_alac_ch_layouts[ALAC_MAX_CHANNELS + 1];
+
+#if FF_API_OLD_CHANNEL_LAYOUT
 extern const uint64_t ff_alac_channel_layouts[ALAC_MAX_CHANNELS + 1];
+#endif
 
 extern const enum AlacRawDataBlockType ff_alac_channel_elements[ALAC_MAX_CHANNELS][5];
 
diff --git a/libavcodec/alacenc.c b/libavcodec/alacenc.c
index a38aa6e7d7..09af0584bc 100644
--- a/libavcodec/alacenc.c
+++ b/libavcodec/alacenc.c
@@ -462,14 +462,15 @@ static int write_frame(AlacEncodeContext *s, AVPacket *avpkt,
                        uint8_t * const *samples)
 {
     PutBitContext *pb = &s->pbctx;
-    const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
-    const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
+    int channels = s->avctx->ch_layout.nb_channels;
+    const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[channels - 1];
+    const uint8_t *ch_map = ff_alac_channel_layout_offsets[channels - 1];
     int ch, element, sce, cpe;
 
     init_put_bits(pb, avpkt->data, avpkt->size);
 
     ch = element = sce = cpe = 0;
-    while (ch < s->avctx->channels) {
+    while (ch < channels) {
         if (ch_elements[element] == TYPE_CPE) {
             write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
                           samples[ch_map[ch + 1]]);
@@ -532,7 +533,7 @@ static av_cold int alac_encode_init(AVCodecContext *avctx)
     s->rc.rice_modifier   = 4;
 
     s->max_coded_frame_size = get_max_frame_size(avctx->frame_size,
-                                                 avctx->channels,
+                                                 avctx->ch_layout.nb_channels,
                                                  avctx->bits_per_raw_sample);
 
     avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
@@ -545,10 +546,10 @@ static av_cold int alac_encode_init(AVCodecContext *avctx)
     AV_WB32(alac_extradata+4,  MKBETAG('a','l','a','c'));
     AV_WB32(alac_extradata+12, avctx->frame_size);
     AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
-    AV_WB8 (alac_extradata+21, avctx->channels);
+    AV_WB8 (alac_extradata+21, avctx->ch_layout.nb_channels);
     AV_WB32(alac_extradata+24, s->max_coded_frame_size);
     AV_WB32(alac_extradata+28,
-            avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
+            avctx->sample_rate * avctx->ch_layout.nb_channels * avctx->bits_per_raw_sample); // average bitrate
     AV_WB32(alac_extradata+32, avctx->sample_rate);
 
     // Set relevant extradata fields
@@ -585,7 +586,7 @@ static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     s->frame_size = frame->nb_samples;
 
     if (frame->nb_samples < DEFAULT_FRAME_SIZE)
-        max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
+        max_frame_size = get_max_frame_size(s->frame_size, avctx->ch_layout.nb_channels,
                                             avctx->bits_per_raw_sample);
     else
         max_frame_size = s->max_coded_frame_size;
@@ -616,6 +617,21 @@ static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     return 0;
 }
 
+#if FF_API_OLD_CHANNEL_LAYOUT
+const uint64_t alac_channel_layouts[ALAC_MAX_CHANNELS + 1] = {
+    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_6POINT1_BACK,
+    AV_CH_LAYOUT_7POINT1_WIDE_BACK,
+    0
+};
+#endif
+
+
 #define OFFSET(x) offsetof(AlacEncodeContext, x)
 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
@@ -632,6 +648,7 @@ static const AVClass alacenc_class = {
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
+FF_DISABLE_DEPRECATION_WARNINGS
 const AVCodec ff_alac_encoder = {
     .name           = "alac",
     .long_name      = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
@@ -643,9 +660,13 @@ const AVCodec ff_alac_encoder = {
     .encode2        = alac_encode_frame,
     .close          = alac_encode_close,
     .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME,
-    .channel_layouts = ff_alac_channel_layouts,
+#if FF_API_OLD_CHANNEL_LAYOUT
+    .channel_layouts = alac_channel_layouts,
+#endif
+    .ch_layouts     = ff_alac_ch_layouts,
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
                                                      AV_SAMPLE_FMT_S16P,
                                                      AV_SAMPLE_FMT_NONE },
     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
 };
+FF_ENABLE_DEPRECATION_WARNINGS
-- 
2.34.1



More information about the ffmpeg-devel mailing list