From patchwork Thu Jan 13 02:06:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 33506 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp43363iog; Wed, 12 Jan 2022 18:45:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJxVdpX1+G1sqmECXhmTU3i2wTkZ1Gn9MmpdMWEtmcUKHeEkdpkZMdaTrjGGyAqnZJtk0Efl X-Received: by 2002:a17:906:4f17:: with SMTP id t23mr2044879eju.688.1642041914074; Wed, 12 Jan 2022 18:45:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642041914; cv=none; d=google.com; s=arc-20160816; b=oCsKpq0lBdKq+v7poUhY19uhP8/5GE1sq4iCTmmqZxZZlYWjE1C6YJCDBbUAlYjfG3 obPprhSUgSeVLMQ5j6nBMYDPn3uygTf8och8mRCWhmuNrp5UQdPJ+A9ROzruED5TnHv+ Tn+naCFy9ooUz2QTfZtJt9fdLu6DpjsRNPAkTigRMQ03RZOOFY+T/VsivOT4ETJ3U4pA CCmyDC96iCPLx8faJS1HxlEV+mwpiSYsRYkOwj7v5r35qP+Nx8tb1fvUGQhkvYpEF4I5 OOn1mUI2ML9PADqovJEZbaq32q0oaXNy2bb3s5xBAK1Wf8OY2S6kDaJYZmBI3z3glIUz DwFA== 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=TUrhMsIa7/XrU2vuh+AyOEIkdQrQjt7keGeV8tMfevU=; b=svSQrgBQ+QKbWvQV9AecdHRh2NI5SfzAO9svLJZX6i5QBCu3PvjHZ//9woFjVlZFih aL+BwPg595hAVIFRPDUdkLWnb2uToAOaWM8I69AoNAsO3oRt7qbuuHN4S4O+JIr/c9EP TEX3yHfiaoO6UfmBiDhNGXzfRlzwpP5e6+L79d7PwKPZ1anEU0qF28QLUzfH/jWlfhss 1ugs0Qv/+0OrQj/+JEwdRoHF5DC8g6L3CPlwwtFYxKkclAB3m62kocnnzYvBbafpY7BQ apTNLJi4EalMWynw82m6sjGZ+JizGzkPtPDlMkh4ZE2+9ywAbYRRTcSv7dGlp4XChK0k TpBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=QG1qSPsA; 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 qb36si936152ejc.382.2022.01.12.18.45.13; Wed, 12 Jan 2022 18:45:14 -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=QG1qSPsA; 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 D944668B96C; Thu, 13 Jan 2022 04:07:35 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9D8BC68B95D for ; Thu, 13 Jan 2022 04:07:33 +0200 (EET) Received: by mail-qt1-f174.google.com with SMTP id l17so5357956qtk.7 for ; Wed, 12 Jan 2022 18:07:33 -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=3vA/zfP/i8//GHAFmh7CGuwgO1xGB5VITO/DuCRi/UM=; b=QG1qSPsAmBBmy1LSbd1SwWpoj4KFQuJrnUbTcmcXXyQe3xVkvXLKNVieKSTbwxsRIR f6k2FHG1/8pEgofBFuOelTtNLH6CkQrcakzrmLYvCqp3EGCblX2rbnZ8CjuxNG3flg2l z5dvmXMEiKfVNZJzqU4inRJFAj31d5coSh2OKNGgIicgl8KvfRIMBb+bqgoTjHLG/DCw 5rSPUCy2ajymNkH3Iq9TqU5pllDAhfqSDNV5dXWSp5kkL9vyD2Lc+gIj/7jrzqD7IRW0 QWuw1ixmMWzD0QxS+RHHknSrdHc5xVhkjH6VvuJZ4/eMUeG366JIijAsdcwiq3TQYJQo cavA== 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=3vA/zfP/i8//GHAFmh7CGuwgO1xGB5VITO/DuCRi/UM=; b=IOYRHkjRC1D8qKSHPuV4uuTRT0FmSeGA+fKdrEmY1qCj865dMPvo16zeblUpL6OZY6 hkrgGFEIDNw70YbeP48A4KxSo/4xzaiNEvcoKVMvPFrQq4ZOPETTx9cD04FStFspPVzM oj5fmTvcnLvrK5m9Nfe7ct3O2Up+6YwoOLem4AuQDd0btE7go0UtpjhFNwHBYZYmsMIN Z0ZvwBlTdVGfQdTcufeVi9c0FdMF7+hbkUoO4bocCJfbNOR2GAHsboekugUwRD8MnIr9 jrWCCFA532DjjUtCnxBvd4P0WW9jskuWqOkmrkDyjBq8lm5b37iyxMya4GhK5C4w5kMQ i9EA== X-Gm-Message-State: AOAM532aZrnK7wBC6Gg7ppz3sYr0Z9opvxsebLTCSn0mUcw5+keYo85G sdst1f0BSq9V7caEy+hWjYCIaKLRyIs= X-Received: by 2002:a05:622a:11c8:: with SMTP id n8mr2087261qtk.622.1642039651958; Wed, 12 Jan 2022 18:07:31 -0800 (PST) Received: from localhost.localdomain ([186.136.131.95]) by smtp.gmail.com with ESMTPSA id a16sm1085946qta.13.2022.01.12.18.07.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jan 2022 18:07:31 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 12 Jan 2022 23:06:48 -0300 Message-Id: <20220113020713.801-5-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 244/281] opus: convert to 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: HkrrOb111G2s From: Anton Khirnov Signed-off-by: James Almer --- libavcodec/opus.c | 57 ++++++++++++++++++++++++++-------------- libavcodec/opusdec.c | 4 +-- libavcodec/opusenc.c | 15 ++++++++--- libavcodec/opusenc_psy.c | 20 +++++++------- 4 files changed, 61 insertions(+), 35 deletions(-) diff --git a/libavcodec/opus.c b/libavcodec/opus.c index df8ba93fa9..c18ff47f71 100644 --- a/libavcodec/opus.c +++ b/libavcodec/opus.c @@ -296,14 +296,15 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, static const uint8_t default_channel_map[2] = { 0, 1 }; int (*channel_reorder)(int, int) = channel_reorder_unknown; + int channels = avctx->ch_layout.nb_channels; const uint8_t *extradata, *channel_map; int extradata_size; - int version, channels, map_type, streams, stereo_streams, i, j; - uint64_t layout; + int version, map_type, streams, stereo_streams, i, j, ret; + AVChannelLayout layout = { 0 }; if (!avctx->extradata) { - if (avctx->channels > 2) { + if (channels > 2) { av_log(avctx, AV_LOG_ERROR, "Multichannel configuration without extradata.\n"); return AVERROR(EINVAL); @@ -331,7 +332,7 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, if (avctx->internal) avctx->internal->skip_samples = avctx->delay; - channels = avctx->extradata ? extradata[9] : (avctx->channels == 1) ? 1 : 2; + channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2; if (!channels) { av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n"); return AVERROR_INVALIDDATA; @@ -346,9 +347,11 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, if (channels > 2) { av_log(avctx, AV_LOG_ERROR, "Channel mapping 0 is only specified for up to 2 channels\n"); - return AVERROR_INVALIDDATA; + ret = AVERROR_INVALIDDATA; + goto fail; } - layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; + layout = (channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO : + (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; streams = 1; stereo_streams = channels - 1; channel_map = default_channel_map; @@ -356,7 +359,8 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, if (extradata_size < 21 + channels) { av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", extradata_size); - return AVERROR_INVALIDDATA; + ret = AVERROR_INVALIDDATA; + goto fail; } streams = extradata[19]; @@ -365,16 +369,18 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, streams + stereo_streams > 255) { av_log(avctx, AV_LOG_ERROR, "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams); - return AVERROR_INVALIDDATA; + ret = AVERROR_INVALIDDATA; + goto fail; } if (map_type == 1) { if (channels > 8) { av_log(avctx, AV_LOG_ERROR, "Channel mapping 1 is only specified for up to 8 channels\n"); - return AVERROR_INVALIDDATA; + ret = AVERROR_INVALIDDATA; + goto fail; } - layout = ff_vorbis_channel_layouts[channels - 1]; + av_channel_layout_copy(&layout, &ff_vorbis_ch_layouts[channels - 1]); channel_reorder = channel_reorder_vorbis; } else if (map_type == 2) { int ambisonic_order = ff_sqrt(channels) - 1; @@ -384,15 +390,20 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, "Channel mapping 2 is only specified for channel counts" " which can be written as (n + 1)^2 or (n + 1)^2 + 2" " for nonnegative integer n\n"); - return AVERROR_INVALIDDATA; + ret = AVERROR_INVALIDDATA; + goto fail; } if (channels > 227) { av_log(avctx, AV_LOG_ERROR, "Too many channels\n"); - return AVERROR_INVALIDDATA; + ret = AVERROR_INVALIDDATA; + goto fail; } - layout = 0; - } else - layout = 0; + layout.order = AV_CHANNEL_ORDER_UNSPEC; + layout.nb_channels = channels; + } else { + layout.order = AV_CHANNEL_ORDER_UNSPEC; + layout.nb_channels = channels; + } channel_map = extradata + 21; } else { @@ -401,8 +412,10 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, } s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps)); - if (!s->channel_maps) - return AVERROR(ENOMEM); + if (!s->channel_maps) { + ret = AVERROR(ENOMEM); + goto fail; + } for (i = 0; i < channels; i++) { ChannelMap *map = &s->channel_maps[i]; @@ -415,7 +428,8 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, av_log(avctx, AV_LOG_ERROR, "Invalid channel map for output channel %d: %d\n", i, idx); av_freep(&s->channel_maps); - return AVERROR_INVALIDDATA; + ret = AVERROR_INVALIDDATA; + goto fail; } /* check that we did not see this index yet */ @@ -436,12 +450,15 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, } } - avctx->channels = channels; - avctx->channel_layout = layout; + av_channel_layout_uninit(&avctx->ch_layout); + avctx->ch_layout = layout; s->nb_streams = streams; s->nb_stereo_streams = stereo_streams; return 0; +fail: + av_channel_layout_uninit(&layout); + return ret; } void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc) diff --git a/libavcodec/opusdec.c b/libavcodec/opusdec.c index b063e0efeb..2133a4c63b 100644 --- a/libavcodec/opusdec.c +++ b/libavcodec/opusdec.c @@ -458,7 +458,7 @@ static int opus_decode_packet(AVCodecContext *avctx, void *data, return ret; frame->nb_samples = 0; - for (i = 0; i < avctx->channels; i++) { + for (i = 0; i < avctx->ch_layout.nb_channels; i++) { ChannelMap *map = &c->channel_maps[i]; if (!map->copy) c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i]; @@ -541,7 +541,7 @@ static int opus_decode_packet(AVCodecContext *avctx, void *data, } } - for (i = 0; i < avctx->channels; i++) { + for (i = 0; i < avctx->ch_layout.nb_channels; i++) { ChannelMap *map = &c->channel_maps[i]; /* handle copied channels */ diff --git a/libavcodec/opusenc.c b/libavcodec/opusenc.c index b7f4760a25..a36f59bf23 100644 --- a/libavcodec/opusenc.c +++ b/libavcodec/opusenc.c @@ -66,7 +66,7 @@ static void opus_write_extradata(AVCodecContext *avctx) bytestream_put_buffer(&bs, "OpusHead", 8); bytestream_put_byte (&bs, 0x1); - bytestream_put_byte (&bs, avctx->channels); + bytestream_put_byte (&bs, avctx->ch_layout.nb_channels); bytestream_put_le16 (&bs, avctx->initial_padding); bytestream_put_le32 (&bs, avctx->sample_rate); bytestream_put_le16 (&bs, 0x0); @@ -518,11 +518,16 @@ static void opus_packet_assembler(OpusEncContext *s, AVPacket *avpkt) static AVFrame *spawn_empty_frame(OpusEncContext *s) { AVFrame *f = av_frame_alloc(); + int ret; if (!f) return NULL; f->format = s->avctx->sample_fmt; f->nb_samples = s->avctx->frame_size; - f->channel_layout = s->avctx->channel_layout; + ret = av_channel_layout_copy(&f->ch_layout, &s->avctx->ch_layout); + if (ret < 0) { + av_frame_free(&f); + return NULL; + } if (av_frame_get_buffer(f, 4)) { av_frame_free(&f); return NULL; @@ -626,7 +631,7 @@ static av_cold int opus_encode_init(AVCodecContext *avctx) OpusEncContext *s = avctx->priv_data; s->avctx = avctx; - s->channels = avctx->channels; + s->channels = avctx->ch_layout.nb_channels; /* Opus allows us to change the framesize on each packet (and each packet may * have multiple frames in it) but we can't change the codec's frame size on @@ -734,8 +739,12 @@ const AVCodec ff_opus_encoder = { .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP, .capabilities = AV_CODEC_CAP_EXPERIMENTAL | AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY, .supported_samplerates = (const int []){ 48000, 0 }, +#if FF_API_OLD_CHANNEL_LAYOUT .channel_layouts = (const uint64_t []){ AV_CH_LAYOUT_MONO, AV_CH_LAYOUT_STEREO, 0 }, +#endif + .ch_layouts = (const AVChannelLayout []){ AV_CHANNEL_LAYOUT_MONO, + AV_CHANNEL_LAYOUT_STEREO, { 0 } }, .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }, }; diff --git a/libavcodec/opusenc_psy.c b/libavcodec/opusenc_psy.c index 5a50db942f..1c8f69269c 100644 --- a/libavcodec/opusenc_psy.c +++ b/libavcodec/opusenc_psy.c @@ -83,7 +83,7 @@ static void step_collect_psy_metrics(OpusPsyContext *s, int index) st->index = index; - for (ch = 0; ch < s->avctx->channels; ch++) { + for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) { const int lap_size = (1 << s->bsize_analysis); for (i = 1; i <= FFMIN(lap_size, index); i++) { const int offset = i*120; @@ -105,7 +105,7 @@ static void step_collect_psy_metrics(OpusPsyContext *s, int index) st->bands[ch][i] = &st->coeffs[ch][ff_celt_freq_bands[i] << s->bsize_analysis]; } - for (ch = 0; ch < s->avctx->channels; ch++) { + for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) { for (i = 0; i < CELT_MAX_BANDS; i++) { float avg_c_s, energy = 0.0f, dist_dev = 0.0f; const int range = ff_celt_freq_range[i] << s->bsize_analysis; @@ -128,7 +128,7 @@ static void step_collect_psy_metrics(OpusPsyContext *s, int index) st->silence = !silence; - if (s->avctx->channels > 1) { + if (s->avctx->ch_layout.nb_channels > 1) { for (i = 0; i < CELT_MAX_BANDS; i++) { float incompat = 0.0f; const float *coeffs1 = st->bands[0][i]; @@ -140,7 +140,7 @@ static void step_collect_psy_metrics(OpusPsyContext *s, int index) } } - for (ch = 0; ch < s->avctx->channels; ch++) { + for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) { for (i = 0; i < CELT_MAX_BANDS; i++) { OpusBandExcitation *ex = &s->ex[ch][i]; float bp_e = bessel_filter(&s->bfilter_lo[ch][i], st->energy[ch][i]); @@ -259,7 +259,7 @@ void ff_opus_psy_celt_frame_init(OpusPsyContext *s, CeltFrame *f, int index) f->start_band = (s->p.mode == OPUS_MODE_HYBRID) ? 17 : 0; f->end_band = ff_celt_band_end[s->p.bandwidth]; - f->channels = s->avctx->channels; + f->channels = s->avctx->ch_layout.nb_channels; f->size = s->p.framesize; for (i = 0; i < (1 << f->size); i++) @@ -327,7 +327,7 @@ static void celt_gauge_psy_weight(OpusPsyContext *s, OpusPsyStep **start, float tonal_contrib = 0.0f; for (f = 0; f < (1 << s->p.framesize); f++) { weight = start[f]->stereo[i]; - for (ch = 0; ch < s->avctx->channels; ch++) { + for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) { weight += start[f]->change_amp[ch][i] + start[f]->tone[ch][i] + start[f]->energy[ch][i]; tonal_contrib += start[f]->tone[ch][i]; } @@ -384,7 +384,7 @@ static void celt_search_for_dual_stereo(OpusPsyContext *s, CeltFrame *f) float td1, td2; f->dual_stereo = 0; - if (s->avctx->channels < 2) + if (s->avctx->ch_layout.nb_channels < 2) return; bands_dist(s, f, &td1); @@ -402,7 +402,7 @@ static void celt_search_for_intensity(OpusPsyContext *s, CeltFrame *f) /* TODO: fix, make some heuristic up here using the lambda value */ float end_band = 0; - if (s->avctx->channels < 2) + if (s->avctx->ch_layout.nb_channels < 2) return; for (i = f->end_band; i >= end_band; i--) { @@ -436,7 +436,7 @@ static int celt_search_for_tf(OpusPsyContext *s, OpusPsyStep **start, CeltFrame float iscore0 = 0.0f; float iscore1 = 0.0f; for (j = 0; j < (1 << f->size); j++) { - for (k = 0; k < s->avctx->channels; k++) { + for (k = 0; k < s->avctx->ch_layout.nb_channels; k++) { iscore0 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[0]; iscore1 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[1]; } @@ -540,7 +540,7 @@ av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx, goto fail; } - for (ch = 0; ch < s->avctx->channels; ch++) { + for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) { for (i = 0; i < CELT_MAX_BANDS; i++) { bessel_init(&s->bfilter_hi[ch][i], 1.0f, 19.0f, 100.0f, 1); bessel_init(&s->bfilter_lo[ch][i], 1.0f, 20.0f, 100.0f, 0);