[FFmpeg-devel] [PATCH 179/279] lavc: switch to the new channel layout API

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


From: Vittorio Giovara <vittorio.giovara at gmail.com>

Since the request_channel_layout is used only by a handful of codecs,
move the option to codec private contexts.

Signed-off-by: Anton Khirnov <anton at khirnov.net>
Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/avcodec.c       | 80 ++++++++++++++++++++++++++++++----
 libavcodec/avcodec.h       | 23 +++++++++-
 libavcodec/codec.h         | 11 +++++
 libavcodec/codec_par.c     | 52 +++++++++++++++++-----
 libavcodec/decode.c        | 89 ++++++++++++++++++++++++++++----------
 libavcodec/encode.c        | 36 ++++++++++++---
 libavcodec/options_table.h |  3 ++
 libavcodec/pthread_frame.c | 10 ++++-
 libavcodec/utils.c         | 12 ++++-
 libavformat/demux.c        |  2 +-
 10 files changed, 263 insertions(+), 55 deletions(-)

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index c00a9b2af8..4c576a848c 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -119,7 +119,7 @@ static int64_t get_bit_rate(AVCodecContext *ctx)
     case AVMEDIA_TYPE_AUDIO:
         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
         if (bits_per_sample) {
-            bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
+            bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
             if (bit_rate > INT64_MAX / bits_per_sample) {
                 bit_rate = 0;
             } else
@@ -137,6 +137,8 @@ static int64_t get_bit_rate(AVCodecContext *ctx)
 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
 {
     int ret = 0;
+    int orig_channels;
+    uint64_t orig_channel_layout;
     AVCodecInternal *avci;
 
     if (avcodec_is_open(avctx))
@@ -247,12 +249,6 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
         }
     }
 
-    if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
-        ret = AVERROR(EINVAL);
-        goto free_and_end;
-    }
-
     if (avctx->sample_rate < 0) {
         av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
         ret = AVERROR(EINVAL);
@@ -264,6 +260,38 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
         goto free_and_end;
     }
 
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+    /* compat wrapper for old-style callers */
+    if (avctx->channel_layout && !avctx->channels)
+        avctx->channels = av_popcount64(avctx->channel_layout);
+
+    if ((avctx->channels && avctx->channels != avctx->ch_layout.nb_channels) ||
+        (avctx->channel_layout && avctx->ch_layout.order != AV_CHANNEL_ORDER_NATIVE)) {
+        if (avctx->channel_layout) {
+            av_channel_layout_from_mask(&avctx->ch_layout, avctx->channel_layout);
+        } else {
+            avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+            avctx->ch_layout.nb_channels = avctx->channels;
+        }
+    }
+
+    /* temporary compat wrapper for new-style callers and old-style codecs;
+     * to be removed once all the codecs have been converted */
+    if (avctx->ch_layout.nb_channels) {
+        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
+
+    if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
+        av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels);
+        ret = AVERROR(EINVAL);
+        goto free_and_end;
+    }
+
     avctx->frame_number = 0;
     avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
 
@@ -317,6 +345,13 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
         avctx->thread_count = 1;
 
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+    orig_channels = avctx->channels;
+    orig_channel_layout = avctx->channel_layout;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
     if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
         avci->frame_thread_encoder) {
         if (avctx->codec->init) {
@@ -334,6 +369,26 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     if (av_codec_is_decoder(avctx->codec)) {
         if (!avctx->bit_rate)
             avctx->bit_rate = get_bit_rate(avctx);
+
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+        /* decoder setting the old-style fields */
+        if (avctx->channels != orig_channels ||
+            avctx->channel_layout != orig_channel_layout) {
+            av_channel_layout_uninit(&avctx->ch_layout);
+            if (avctx->channel_layout) {
+                av_channel_layout_from_mask(&avctx->ch_layout, avctx->channel_layout);
+            } else {
+                avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+                avctx->ch_layout.nb_channels = avctx->channels;
+            }
+        }
+
+        /* update the deprecated fields for old-style callers */
+        avctx->channels = avctx->ch_layout.nb_channels;
+        avctx->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
+                                avctx->ch_layout.u.mask : 0;
+
         /* validate channel layout from the decoder */
         if (avctx->channel_layout) {
             int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
@@ -358,6 +413,8 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
             ret = AVERROR(EINVAL);
             goto free_and_end;
         }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 
 #if FF_API_AVCTX_TIMEBASE
         if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
@@ -487,6 +544,8 @@ av_cold int avcodec_close(AVCodecContext *avctx)
         av_freep(&avctx->internal);
     }
 
+    av_channel_layout_uninit(&avctx->ch_layout);
+
     for (i = 0; i < avctx->nb_coded_side_data; i++)
         av_freep(&avctx->coded_side_data[i].data);
     av_freep(&avctx->coded_side_data);
@@ -666,7 +725,12 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
         if (enc->sample_rate) {
             av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
         }
-        av_bprint_channel_layout(&bprint, enc->channels, enc->channel_layout);
+        {
+            char buf[512];
+            int ret = av_channel_layout_describe(&enc->ch_layout, buf, sizeof(buf));
+            if (ret >= 0)
+                av_bprintf(&bprint, "%s", buf);
+        }
         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
             (str = av_get_sample_fmt_name(enc->sample_fmt))) {
             av_bprintf(&bprint, ", %s", str);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7ee8bc2b7c..fadcd120cc 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -990,7 +990,14 @@ typedef struct AVCodecContext {
 
     /* audio only */
     int sample_rate; ///< samples per second
-    int channels;    ///< number of audio channels
+#if FF_API_OLD_CHANNEL_LAYOUT
+    /**
+     * number of audio channels
+     * @deprecated use ch_layout.nb_channels
+     */
+    attribute_deprecated
+    int channels;
+#endif
 
     /**
      * audio sample format
@@ -1035,19 +1042,25 @@ typedef struct AVCodecContext {
      */
     int cutoff;
 
+#if FF_API_OLD_CHANNEL_LAYOUT
     /**
      * Audio channel layout.
      * - encoding: set by user.
      * - decoding: set by user, may be overwritten by libavcodec.
+     * @deprecated use ch_layout
      */
+    attribute_deprecated
     uint64_t channel_layout;
 
     /**
      * Request decoder to use this channel layout if it can (0 for default)
      * - encoding: unused
      * - decoding: Set by user.
+     * @deprecated use "downmix" codec private option
      */
+    attribute_deprecated
     uint64_t request_channel_layout;
+#endif
 
     /**
      * Type of service that the audio stream conveys.
@@ -2024,6 +2037,14 @@ typedef struct AVCodecContext {
      * - decoding: unused
      */
     int (*get_encode_buffer)(struct AVCodecContext *s, AVPacket *pkt, int flags);
+
+    /**
+     * Audio channel layout.
+     * - encoding: must be set by the caller, to one of AVCodec.ch_layouts.
+     * - decoding: may be set by the caller if known e.g. from the container.
+     *             The decoder can then override during decoding as needed.
+     */
+    AVChannelLayout ch_layout;
 } AVCodecContext;
 
 struct MpegEncContext;
diff --git a/libavcodec/codec.h b/libavcodec/codec.h
index a8147ec21f..204a558798 100644
--- a/libavcodec/codec.h
+++ b/libavcodec/codec.h
@@ -224,7 +224,13 @@ typedef struct AVCodec {
     const enum AVPixelFormat *pix_fmts;     ///< array of supported pixel formats, or NULL if unknown, array is terminated by -1
     const int *supported_samplerates;       ///< array of supported audio samplerates, or NULL if unknown, array is terminated by 0
     const enum AVSampleFormat *sample_fmts; ///< array of supported sample formats, or NULL if unknown, array is terminated by -1
+#if FF_API_OLD_CHANNEL_LAYOUT
+    /**
+     * @deprecated use ch_layouts instead
+     */
+    attribute_deprecated
     const uint64_t *channel_layouts;         ///< array of support channel layouts, or NULL if unknown. array is terminated by 0
+#endif
     const AVClass *priv_class;              ///< AVClass for the private context
     const AVProfile *profiles;              ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}
 
@@ -240,6 +246,11 @@ typedef struct AVCodec {
      */
     const char *wrapper_name;
 
+    /**
+     * Array of supported channel layouts, terminated with a zeroed layout.
+     */
+    const AVChannelLayout *ch_layouts;
+
     /*****************************************************************
      * No fields below this line are part of the public API. They
      * may not be used outside of libavcodec and can be changed and
diff --git a/libavcodec/codec_par.c b/libavcodec/codec_par.c
index 9d43af1db4..052c64a315 100644
--- a/libavcodec/codec_par.c
+++ b/libavcodec/codec_par.c
@@ -125,11 +125,25 @@ int avcodec_parameters_from_context(AVCodecParameters *par,
         break;
     case AVMEDIA_TYPE_AUDIO:
         par->format           = codec->sample_fmt;
-        if (codec->channel_layout)
-            av_channel_layout_from_mask(&par->ch_layout, codec->channel_layout);
-        else {
-            par->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
-            par->ch_layout.nb_channels = codec->channels;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+        // if the old/new fields are set inconsistently, prefer the old ones
+        if ((codec->channels && codec->channels != codec->ch_layout.nb_channels) ||
+            (codec->channel_layout && (codec->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
+                                       codec->ch_layout.u.mask != codec->channel_layout))) {
+            if (codec->channel_layout)
+                av_channel_layout_from_mask(&par->ch_layout, codec->channel_layout);
+            else {
+                par->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+                par->ch_layout.nb_channels = codec->channels;
+            }
+FF_ENABLE_DEPRECATION_WARNINGS
+        } else
+#endif
+        if (codec->ch_layout.nb_channels) {
+            int ret = av_channel_layout_copy(&par->ch_layout, &codec->ch_layout);
+            if (ret < 0)
+                return ret;
         }
 #if FF_API_OLD_CHANNEL_LAYOUT
 FF_DISABLE_DEPRECATION_WARNINGS
@@ -191,18 +205,32 @@ int avcodec_parameters_to_context(AVCodecContext *codec,
         break;
     case AVMEDIA_TYPE_AUDIO:
         codec->sample_fmt       = par->format;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+        // if the old/new fields are set inconsistently, prefer the old ones
+        if ((par->channels && par->channels != par->ch_layout.nb_channels) ||
+            (par->channel_layout && (par->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
+                                     par->ch_layout.u.mask != par->channel_layout))) {
+            if (par->channel_layout)
+                av_channel_layout_from_mask(&codec->ch_layout, par->channel_layout);
+            else {
+                codec->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+                codec->ch_layout.nb_channels = par->channels;
+            }
+FF_ENABLE_DEPRECATION_WARNINGS
+        } else
+#endif
         if (par->ch_layout.nb_channels) {
-            codec->channel_layout = par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
-                                    par->ch_layout.u.mask : 0;
-            codec->channels       = par->ch_layout.nb_channels;
+            int ret = av_channel_layout_copy(&codec->ch_layout, &par->ch_layout);
+            if (ret < 0)
+                return ret;
         }
 #if FF_API_OLD_CHANNEL_LAYOUT
-        else {
 FF_DISABLE_DEPRECATION_WARNINGS
-            codec->channel_layout = par->channel_layout;
-            codec->channels       = par->channels;
+        codec->channel_layout = codec->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
+                                codec->ch_layout.u.mask : 0;
+        codec->channels       = codec->ch_layout.nb_channels;
 FF_ENABLE_DEPRECATION_WARNINGS
-        }
 #endif
         codec->sample_rate      = par->sample_rate;
         codec->block_align      = par->block_align;
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 01fea93c04..aa74c677ef 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -359,10 +359,14 @@ static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame,
         if (ret >= 0 && got_frame) {
             if (frame->format == AV_SAMPLE_FMT_NONE)
                 frame->format = avctx->sample_fmt;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
             if (!frame->channel_layout)
                 frame->channel_layout = avctx->channel_layout;
             if (!frame->channels)
                 frame->channels = avctx->channels;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
             if (!frame->sample_rate)
                 frame->sample_rate = avctx->sample_rate;
         }
@@ -394,7 +398,7 @@ static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame,
                        avci->skip_samples);
             } else {
                 av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples,
-                                frame->nb_samples - avci->skip_samples, avctx->channels, frame->format);
+                                frame->nb_samples - avci->skip_samples, avctx->ch_layout.nb_channels, frame->format);
                 if(avctx->pkt_timebase.num && avctx->sample_rate) {
                     int64_t diff_ts = av_rescale_q(avci->skip_samples,
                                                    (AVRational){1, avctx->sample_rate},
@@ -683,8 +687,12 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr
             case AVMEDIA_TYPE_AUDIO:
                 avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate :
                                                                  avctx->sample_rate;
-                avci->initial_channels       = frame->channels;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+                avci->initial_channels        = frame->ch_layout.nb_channels;
                 avci->initial_channel_layout = frame->channel_layout;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
                 break;
             }
         }
@@ -698,10 +706,15 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr
                            avci->initial_height != frame->height;
                 break;
             case AVMEDIA_TYPE_AUDIO:
+FF_DISABLE_DEPRECATION_WARNINGS
                 changed |= avci->initial_sample_rate    != frame->sample_rate ||
                            avci->initial_sample_rate    != avctx->sample_rate ||
+#if FF_API_OLD_CHANNEL_LAYOUT
                            avci->initial_channels       != frame->channels ||
-                           avci->initial_channel_layout != frame->channel_layout;
+                           avci->initial_channel_layout != frame->channel_layout
+#endif
+                           ;
+FF_ENABLE_DEPRECATION_WARNINGS
                 break;
             }
 
@@ -1260,7 +1273,13 @@ static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
 
     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
         int planar = av_sample_fmt_is_planar(frame->format);
-        ch     = frame->channels;
+        ch     = frame->ch_layout.nb_channels;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+        if (!ch)
+            ch = frame->channels;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
         planes = planar ? ch : 1;
     }
 
@@ -1568,25 +1587,18 @@ int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
             frame->sample_rate    = avctx->sample_rate;
         if (frame->format < 0)
             frame->format         = avctx->sample_fmt;
-        if (!frame->channel_layout) {
-            if (avctx->channel_layout) {
-                 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
-                     avctx->channels) {
-                     av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
-                            "configuration.\n");
-                     return AVERROR(EINVAL);
-                 }
-
-                frame->channel_layout = avctx->channel_layout;
-            } else {
-                if (avctx->channels > FF_SANE_NB_CHANNELS) {
-                    av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
-                           avctx->channels);
-                    return AVERROR(ENOSYS);
-                }
-            }
+        if (!frame->ch_layout.nb_channels) {
+            int ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
+            if (ret < 0)
+                return ret;
         }
-        frame->channels = avctx->channels;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+        frame->channels = frame->ch_layout.nb_channels;
+        frame->channel_layout = frame->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
+                                frame->ch_layout.u.mask : 0;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
         break;
     }
     return 0;
@@ -1676,7 +1688,36 @@ int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
             goto fail;
         }
     } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
-        if (frame->nb_samples * (int64_t)avctx->channels > avctx->max_samples) {
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+        /* temporary compat layer for decoders setting the old-style channel
+         * layout fields; shall be removed after all the decoders are converted
+         * to the new API */
+        if ((avctx->channels > 0 && avctx->ch_layout.nb_channels != avctx->channels) ||
+            (avctx->channel_layout && (avctx->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
+                                       avctx->ch_layout.u.mask != avctx->channel_layout))) {
+            av_channel_layout_uninit(&avctx->ch_layout);
+            if (avctx->channel_layout) {
+                if (av_popcount64(avctx->channel_layout) != avctx->channels) {
+                    av_log(avctx, AV_LOG_ERROR, "Inconsistent channel layout/channels\n");
+                    ret = AVERROR(EINVAL);
+                    goto fail;
+                }
+                av_channel_layout_from_mask(&avctx->ch_layout, avctx->channel_layout);
+            } else {
+                avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+                avctx->ch_layout.nb_channels = avctx->channels;
+            }
+        }
+
+        /* compat layer for old-style get_buffer() implementations */
+        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
+
+        if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) {
             av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples);
             ret = AVERROR(EINVAL);
             goto fail;
@@ -1786,7 +1827,7 @@ FF_DISABLE_DEPRECATION_WARNINGS
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
 
-    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && avctx->channels == 0 &&
+    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && avctx->ch_layout.nb_channels == 0 &&
         !(avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
         av_log(avctx, AV_LOG_ERROR, "Decoder requires channel count but channels not set\n");
         return AVERROR(EINVAL);
diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index dd25cf999b..0dd5e3fdd1 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -114,9 +114,10 @@ static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
     int ret;
 
     frame->format         = src->format;
-    frame->channel_layout = src->channel_layout;
-    frame->channels       = src->channels;
     frame->nb_samples     = s->frame_size;
+    ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
+    if (ret < 0)
+        goto fail;
     ret = av_frame_get_buffer(frame, 0);
     if (ret < 0)
         goto fail;
@@ -126,11 +127,12 @@ static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
         goto fail;
 
     if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
-                               src->nb_samples, s->channels, s->sample_fmt)) < 0)
+                               src->nb_samples, s->ch_layout.nb_channels,
+                               s->sample_fmt)) < 0)
         goto fail;
     if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
                                       frame->nb_samples - src->nb_samples,
-                                      s->channels, s->sample_fmt)) < 0)
+                                      s->ch_layout.nb_channels, s->sample_fmt)) < 0)
         goto fail;
 
     return 0;
@@ -420,7 +422,7 @@ int ff_encode_preinit(AVCodecContext *avctx)
         for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
             if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
                 break;
-            if (avctx->channels == 1 &&
+            if (avctx->ch_layout.nb_channels == 1 &&
                 av_get_planar_sample_fmt(avctx->sample_fmt) ==
                 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
                 avctx->sample_fmt = avctx->codec->sample_fmts[i];
@@ -468,7 +470,27 @@ int ff_encode_preinit(AVCodecContext *avctx)
                 avctx->sample_rate);
         return AVERROR(EINVAL);
     }
-    if (avctx->codec->channel_layouts) {
+    if (avctx->codec->ch_layouts) {
+        if (!av_channel_layout_check(&avctx->ch_layout)) {
+            av_log(avctx, AV_LOG_WARNING, "Channel layout not specified correctly\n");
+            return AVERROR(EINVAL);
+        }
+
+        for (i = 0; avctx->codec->ch_layouts[i].nb_channels; i++) {
+            if (!av_channel_layout_compare(&avctx->ch_layout, &avctx->codec->ch_layouts[i]))
+                break;
+        }
+        if (!avctx->codec->ch_layouts[i].nb_channels) {
+            char buf[512];
+            int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
+            if (ret > 0)
+                av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
+            return AVERROR(EINVAL);
+        }
+FF_DISABLE_DEPRECATION_WARNINGS
+    }
+#if FF_API_OLD_CHANNEL_LAYOUT
+    else if (avctx->codec->channel_layouts) {
         if (!avctx->channel_layout) {
             av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
         } else {
@@ -501,6 +523,8 @@ int ff_encode_preinit(AVCodecContext *avctx)
                 avctx->channels);
         return AVERROR(EINVAL);
     }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
     if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
         const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
         if (    avctx->bits_per_raw_sample < 0
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
index 130341a2ec..c093b626d4 100644
--- a/libavcodec/options_table.h
+++ b/libavcodec/options_table.h
@@ -262,8 +262,11 @@ static const AVOption avcodec_options[] = {
 {"mv0_threshold", NULL, OFFSET(mv0_threshold), AV_OPT_TYPE_INT, {.i64 = 256 }, 0, INT_MAX, V|E},
 {"compression_level", NULL, OFFSET(compression_level), AV_OPT_TYPE_INT, {.i64 = FF_COMPRESSION_DEFAULT }, INT_MIN, INT_MAX, V|A|E},
 {"bits_per_raw_sample", NULL, OFFSET(bits_per_raw_sample), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, INT_MAX},
+{"ch_layout", NULL, OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = NULL }, 0, 0, A|E|D, "ch_layout"},
+#if FF_API_OLD_CHANNEL_LAYOUT
 {"channel_layout", NULL, OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = DEFAULT }, 0, UINT64_MAX, A|E|D, "channel_layout"},
 {"request_channel_layout", NULL, OFFSET(request_channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = DEFAULT }, 0, UINT64_MAX, A|D, "request_channel_layout"},
+#endif
 {"rc_max_vbv_use", NULL, OFFSET(rc_max_available_vbv_use), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, 0.0, FLT_MAX, V|E},
 {"rc_min_vbv_use", NULL, OFFSET(rc_min_vbv_overflow_use),  AV_OPT_TYPE_FLOAT, {.dbl = 3 },     0.0, FLT_MAX, V|E},
 {"ticks_per_frame", NULL, OFFSET(ticks_per_frame), AV_OPT_TYPE_INT, {.i64 = 1 }, 1, INT_MAX, A|V|E|D},
diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index 73b1b7d7d9..e71f0860a7 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -297,10 +297,18 @@ static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src,
         dst->hwaccel = src->hwaccel;
         dst->hwaccel_context = src->hwaccel_context;
 
-        dst->channels       = src->channels;
         dst->sample_rate    = src->sample_rate;
         dst->sample_fmt     = src->sample_fmt;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+        dst->channels       = src->channels;
         dst->channel_layout = src->channel_layout;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+        err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
+        if (err < 0)
+            return err;
+
         dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
 
         if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index dc5156210f..729e19005b 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -816,8 +816,16 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
 
 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
 {
-    int duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
-                                    avctx->channels, avctx->block_align,
+   int channels = avctx->ch_layout.nb_channels;
+   int duration;
+#if FF_API_OLD_CHANNEL_LAYOUT
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (!channels)
+        channels = avctx->channels;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+    duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
+                                    channels, avctx->block_align,
                                     avctx->codec_tag, avctx->bits_per_coded_sample,
                                     avctx->bit_rate, avctx->extradata, avctx->frame_size,
                                     frame_bytes);
diff --git a/libavformat/demux.c b/libavformat/demux.c
index a9670aeb34..8227d10ff1 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -1951,7 +1951,7 @@ static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
             FAIL("unspecified sample format");
         if (!avctx->sample_rate)
             FAIL("unspecified sample rate");
-        if (!avctx->channels)
+        if (!avctx->ch_layout.nb_channels)
             FAIL("unspecified number of channels");
         if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
             FAIL("no decodable DTS frames");
-- 
2.34.1



More information about the ffmpeg-devel mailing list