From patchwork Thu Jan 13 02:02:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 33454 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp36829iog; Wed, 12 Jan 2022 18:32:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJw/DI1mNFBVu8GaXNhy4iedcJsHdAo5DzbnlKAooNygO5QHHMo/G5T+RbDt6TUTwIoCVJF+ X-Received: by 2002:a17:906:1e4f:: with SMTP id i15mr1862558ejj.522.1642041146110; Wed, 12 Jan 2022 18:32:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642041146; cv=none; d=google.com; s=arc-20160816; b=qozH6ITGxRcdwpHkH9A6x83QmrmdYz3L9t3W4SBgJmAnj+fbS47yTTu9XphOIQSoIs 54VsiNtqz/YAkVFiVMruokI546hsv+3RjarNJv+/cvVTeceaDkuV9svFacVUiE4WkLGq hot4k63WzPeZ3fwtOaAoeL/+QNjpY5ynS6bPw+HDVZhHtkWFZowNlXzRodB2s7z3WGEB CLmW9YgwWKncwJRBoblZFlfA3moHPx/fZwjCy4wMrlAW+TuVqGohWjKim8IHFdUAi/eR h1c2EYaB9MlUxl/K+FmrKyu+keD8o8iFPeFxqnsoPfnUZeZccbw1LVPDr0SmfJdElfkl +o2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=afV08xhxg8+1MTzjlNxTEHD5X5rXzRyf0ptAiC6kX+o=; b=vGLZ/olI7nHF2185yRtSsHpk87HG2jHlsgcQ7d9viUIIA68E9WKcYVHulXc8keT91/ AmhTyuRjJGfims8zoctq8HlylEsngTLbj/CN9DCTeV8U+0g2phQe0eKCOec6e1sAmGyZ 1RcPh4LJmbbwtCOtqaWq94E51x3x1DLicBaHw1xd2A9OX9c+uQGd1gMWlCrE2pthIr1X KfoJXyz14zL/3SDvzv6LMdZFmD/0lqBbvBunNufXvvcJ7JN4ISPKts8CNEfkll376kk1 IGECDSGM2Gi8McX0r3HZL8IP/51L+RKGYBewanoI9ahUT8tt2KhTtSHdCjvAL+dOCrLc MXGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Pep7MXN6; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id qb19si930419ejc.308.2022.01.12.18.32.25; Wed, 12 Jan 2022 18:32:26 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Pep7MXN6; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 51F7A68B5A6; Thu, 13 Jan 2022 04:03:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B452168B59F for ; Thu, 13 Jan 2022 04:02:58 +0200 (EET) Received: by mail-qt1-f182.google.com with SMTP id y10so5384163qtw.1 for ; Wed, 12 Jan 2022 18:02:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=omhCX5KV/Uh7HF4neTsA74WRN1+qBeyW+3YcN1KEd68=; b=Pep7MXN69HHv3WkkuIi+DIldWgLCP9c0j40NRt+Y4p+BOOz3n7pNx5vZfNPhCTyoxd M6YQrCTHiMtQCXjv5cMhc/kMViKd0RgqUV8kYmQCgbS6wWsPso1Ipwe631Ww2gH0Bpuf q89pxYN/00J89mdTWu4ktdaiXWPqV5F/HygX4yVIgnB2B04jELiwqCd/uD1FhRSmLHFS zn5eji97F5RT1JsDg8sTOm2WDO8rZ4Wjm4/JgyyYgvAb6F4w7hj4y5UNOCoBBQtjst/j KgjinNXfCtXUEyw6xhG9kRizyO3D3rO/8Zw/UWKB07GTPd1pJ5FPrRvl8/I7Rhvtm/K9 BfWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=omhCX5KV/Uh7HF4neTsA74WRN1+qBeyW+3YcN1KEd68=; b=i7EsLYdtIYM2s0QR/360XgJ20Q5U4bkwVZCe3A8yBFvAzMwx4+8rFAZKsIu1TAd5lC 3iO4HtGy+GzyczqMZ/UlgUnGw9R/k7q0+3uk7Yzgxl2U25B87AJ4R4f+smaqhp+PETMO 6jf8NFkypa4JTMStXDa5cGCoiktyLci+m5fyxobjOdAKEY/Y4zVQDekPYV7OIw0OXdK3 6Q7arbK3BVNUkyuGg4a4I0kqHeH5MfIT1Oe6FZTIFRG1rE7GF9O60iylqzgSlmBx2u4T Vo1IBQTNtNnEKIKN2hde9OlR/XkohFd9f6GW0GQatU+cRccfZRflvOE5JaEnheyNmBUg 0rVQ== X-Gm-Message-State: AOAM5334hhQl0ocE+CpeKlC4oZ4F4N/+99r4bo5AWcEOLP1e7F/Bi3NV kj2AQ5XpKKfulSXbjYWEe0acvXrZMTU= X-Received: by 2002:a05:622a:489:: with SMTP id p9mr1999855qtx.343.1642039376800; Wed, 12 Jan 2022 18:02:56 -0800 (PST) Received: from localhost.localdomain ([186.136.131.95]) by smtp.gmail.com with ESMTPSA id v1sm1045961qtc.95.2022.01.12.18.02.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jan 2022 18:02:56 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 12 Jan 2022 23:02:13 -0300 Message-Id: <20220113020242.661-1-jamrial@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220113015101.4-1-jamrial@gmail.com> References: <20220113015101.4-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 180/281] lavc: switch to the new channel layout API X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: wE7ThbQjZkJr From: Vittorio Giovara 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 Signed-off-by: James Almer --- libavcodec/avcodec.c | 82 +++++++++++++++++++++++++++++---- libavcodec/avcodec.h | 24 +++++++++- libavcodec/codec.h | 11 +++++ libavcodec/codec_par.c | 52 ++++++++++++++++----- libavcodec/decode.c | 94 ++++++++++++++++++++++++++++---------- libavcodec/encode.c | 36 ++++++++++++--- libavcodec/internal.h | 3 ++ libavcodec/options.c | 2 + libavcodec/options_table.h | 5 ++ libavcodec/pthread_frame.c | 10 +++- libavcodec/utils.c | 12 ++++- libavformat/demux.c | 2 +- 12 files changed, 278 insertions(+), 55 deletions(-) diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index c00a9b2af8..0436a6d0bd 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) @@ -484,9 +541,13 @@ av_cold int avcodec_close(AVCodecContext *avctx) av_bsf_free(&avci->bsf); + av_channel_layout_uninit(&avci->initial_ch_layout); + 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 +727,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 ec1a0566a4..8c72c77985 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -990,7 +990,15 @@ 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 +1043,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 +2038,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 60127e95fc..6381ec68e1 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,17 @@ 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 + ret = av_channel_layout_copy(&avci->initial_ch_layout, &frame->ch_layout); + if (ret < 0) { + av_frame_unref(frame); + return ret; + } break; } } @@ -698,10 +711,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 + av_channel_layout_compare(&avci->initial_ch_layout, &frame->ch_layout); +FF_ENABLE_DEPRECATION_WARNINGS break; } @@ -1263,7 +1281,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; } @@ -1571,25 +1595,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; @@ -1679,7 +1696,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; @@ -1789,7 +1835,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 618be0573d..f9f8879b8e 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; @@ -419,7 +421,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]; @@ -467,7 +469,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 { @@ -500,6 +522,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/internal.h b/libavcodec/internal.h index 72ca1553f6..51d827c1fe 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -208,8 +208,11 @@ typedef struct AVCodecInternal { int initial_format; int initial_width, initial_height; int initial_sample_rate; +#if FF_API_OLD_CHANNEL_LAYOUT int initial_channels; uint64_t initial_channel_layout; +#endif + AVChannelLayout initial_ch_layout; } AVCodecInternal; struct AVCodecDefault { diff --git a/libavcodec/options.c b/libavcodec/options.c index bba6078b62..c0b874fa05 100644 --- a/libavcodec/options.c +++ b/libavcodec/options.c @@ -187,7 +187,9 @@ static const AVOption frame_options[]={ {"width", "", FOFFSET(width), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0}, {"height", "", FOFFSET(height), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0}, {"format", "", FOFFSET(format), AV_OPT_TYPE_INT, {.i64 = -1 }, 0, INT_MAX, 0}, +#if FF_API_OLD_CHANNEL_LAYOUT {"channel_layout", "", FOFFSET(channel_layout), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, 0}, +#endif {"sample_rate", "", FOFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0}, {NULL}, }; diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h index 130341a2ec..4dca85747e 100644 --- a/libavcodec/options_table.h +++ b/libavcodec/options_table.h @@ -89,7 +89,9 @@ static const AVOption avcodec_options[] = { {"time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, INT_MAX}, {"g", "set the group of picture (GOP) size", OFFSET(gop_size), AV_OPT_TYPE_INT, {.i64 = 12 }, INT_MIN, INT_MAX, V|E}, {"ar", "set audio sampling rate (in Hz)", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, INT_MAX, A|D|E}, +#if FF_API_OLD_CHANNEL_LAYOUT {"ac", "set number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, INT_MAX, A|D|E}, +#endif {"cutoff", "set cutoff bandwidth", OFFSET(cutoff), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, A|E}, {"frame_size", NULL, OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, INT_MAX, A|E}, {"frame_number", NULL, OFFSET(frame_number), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX}, @@ -262,8 +264,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 85a6bc98c1..c051f43e4a 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 0505ea6ba2..373fb56357 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -805,8 +805,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 a9682a5949..c7710a5a7b 100644 --- a/libavformat/demux.c +++ b/libavformat/demux.c @@ -1946,7 +1946,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");