From patchwork Thu Jan 13 02:02:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 33469 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp38542iog; Wed, 12 Jan 2022 18:35:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJyeRjOXRkF1OUVawBn0HRpwhAV00I0jBd/bfNVusaUvM67U6ga2rjyUaZoxEoTCBZeg0Pql X-Received: by 2002:a17:907:98af:: with SMTP id ju15mr954647ejc.264.1642041352148; Wed, 12 Jan 2022 18:35:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642041352; cv=none; d=google.com; s=arc-20160816; b=WgaD0cV1SvhtpQh/1/0fXoh2Lzn59z0iiCSllYiu19fa54vWDNVl/PGnBrxaqVTC5t zeoLxea8yY24dWhgm231cxtvPZ+/ZGTGzsgfabF9j8J0URJCCCFdVxxBgxoSCe8EcdFH xhVU5YKR7f3GkUJGAm7gP7T8A1wJPhMJfvHwK1vJ9XoV2wxtXdRzHQ5lrMbeWOL1WOsU uSZl2UqXOeklshW2BBUrg4ZSCM243G+UZ6W4FvLVqtYEXHO6ePc/XeR94Xrq/Cwz6Hi1 8UK2KEUfdMKVtsAmRGyUzS/bF+FVlHwq814qPCW4Hn6WwAzVVFsmiTr8O+7DG6v66Mkk Lz0w== 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=mWcbROAYd3zhl6XXeBtx7DrvyY0SnGWB5eVZQz8wyBk=; b=lKnrrZTjWhL0XOKDNKfzB8UhtSbcwBgqV4wxghCjw9/OU1YnlfCcH2yZrHiN1zIZ4h yxn0d3ICPkGavPvAvFU+QGinClXMoXleChSK0gOWPu//PfUpw96LuutB/+h8IBQjvENf QdnRAl8iYxWegVkhu1tXCvgDpw3KrT2a3I9rg+DLUmKlo2aQ7h0DDKm7ZlJmqoQ+dHTh Dn5Z3nnEEk3lVaavwg9Gt2C8uicbnASM/Db8O1mDbi6H/Y1vn3rqAixl1RKQyQ7KllCt RQq0TQF7CY9wJ6oVchYdHw7jJYX1N6DpU2uWZeAdB4wexdx0pstC6D64NbYenRwsSsIn Tzzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=m45VQXtP; 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 oz11si742161ejc.728.2022.01.12.18.35.51; Wed, 12 Jan 2022 18:35:52 -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=m45VQXtP; 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 0197B68B65A; Thu, 13 Jan 2022 04:03:22 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E050868B630 for ; Thu, 13 Jan 2022 04:03:16 +0200 (EET) Received: by mail-qv1-f45.google.com with SMTP id jr5so5075002qvb.11 for ; Wed, 12 Jan 2022 18:03:16 -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=kZxnimFZOqahjwi8rlttDG8HxKGX2m4tpBbCAmYx+dQ=; b=m45VQXtPP5GXqnIodK8rxUTjQ14kVu/lOZoZhGJBaePEAZ54KB8SZ2bnlkw//bhKJC TXsXjmETmOPa75Y3ItC4JDNmlMxXefPE9VtVhcpF6uEFojBuvekHDy+vhX382nSnNLuN 9qrrwA1lJZ449AgcinPrj/xrER0JCSp/CxtBRxrCmCkeEMxZXBVpPL0P28T4rTysldsj bz+yI4/L2+N5Y2uEdqabvco/bIBPvk4VfmwiALv3BpQE3xJrwKOMxMqWrd7lssnbuApC 8BFxnO604y04rK+S+X6bzRS0gUz6R/sg445KxDeLnaQTteq6FVqNMyqtrpJZTamEhwNP zG6Q== 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=kZxnimFZOqahjwi8rlttDG8HxKGX2m4tpBbCAmYx+dQ=; b=RkdqsHnehotiTPd3Gh7C123o0LLen1k/JOdQHr4fOvN6TVvX7p4tORgVPmdOa6QmJ2 u1m5gk9+Vz7zdKpkJ47OP69QlOBPT9FTabmoaJy6IdUs8nrW8djyDXEpseX+/YbuUn7g eVXU2RCFCCB6qs7Tj+ApRJAKMfVH6tnE3ch8gg05GSeOTDvtnv572UDo+kMxoTfo7gX4 mT2SJMsfaYgrpvm4kyX9IqE6Db6u6f5PF7c+ymZTDIzr9A70I8pD6Ky2909JHSzUrigf WzHmAvkLEwQFPwONSqp88zVwLzC5tKEzR/2UEBlBbuw9CwYsJybN1Mom6uPdPtPL8QTF 4/Cw== X-Gm-Message-State: AOAM530Or/GuTktFIlQP+4GVUhCKTNA7zlBWH4Kk5qroQB8ybm9Xq2vN ttNhFyaFJLtlS2xtIgETDcN+URBDp9k= X-Received: by 2002:ad4:5dc6:: with SMTP id m6mr2100868qvh.1.1642039395423; Wed, 12 Jan 2022 18:03:15 -0800 (PST) Received: from localhost.localdomain ([186.136.131.95]) by smtp.gmail.com with ESMTPSA id v1sm1045961qtc.95.2022.01.12.18.03.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jan 2022 18:03:15 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 12 Jan 2022 23:02:28 -0300 Message-Id: <20220113020242.661-16-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 195/281] audiotoolbox: 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: nOAk4yR1B4LG From: Anton Khirnov Signed-off-by: James Almer --- libavcodec/audiotoolboxdec.c | 27 +++++----- libavcodec/audiotoolboxenc.c | 95 ++++++++++++++++-------------------- 2 files changed, 57 insertions(+), 65 deletions(-) diff --git a/libavcodec/audiotoolboxdec.c b/libavcodec/audiotoolboxdec.c index 9939fef218..8edf9bd463 100644 --- a/libavcodec/audiotoolboxdec.c +++ b/libavcodec/audiotoolboxdec.c @@ -166,8 +166,8 @@ static int ffat_update_ctx(AVCodecContext *avctx) &size, &format)) { if (format.mSampleRate) avctx->sample_rate = format.mSampleRate; - avctx->channels = format.mChannelsPerFrame; - avctx->channel_layout = av_get_default_channel_layout(avctx->channels); + av_channel_layout_uninit(&avctx->ch_layout); + av_get_default_channel_layout(&avctx->ch_layout, format.mChannelsPerFrame); avctx->frame_size = format.mFramesPerPacket; } @@ -175,7 +175,7 @@ static int ffat_update_ctx(AVCodecContext *avctx) kAudioConverterCurrentOutputStreamDescription, &size, &format)) { format.mSampleRate = avctx->sample_rate; - format.mChannelsPerFrame = avctx->channels; + format.mChannelsPerFrame = avctx->ch_layout.nb_channels; AudioConverterSetProperty(at->converter, kAudioConverterCurrentOutputStreamDescription, size, &format); @@ -201,7 +201,8 @@ static int ffat_update_ctx(AVCodecContext *avctx) layout_mask |= 1 << id; layout->mChannelDescriptions[i].mChannelFlags = i; // Abusing flags as index } - avctx->channel_layout = layout_mask; + av_channel_layout_uninit(&avctx->ch_layout); + av_channel_layout_from_mask(&avctx->ch_layout, layout_mask); qsort(layout->mChannelDescriptions, layout->mNumberChannelDescriptions, sizeof(AudioChannelDescription), &ffat_compare_channel_descriptions); for (i = 0; i < layout->mNumberChannelDescriptions; i++) @@ -364,11 +365,11 @@ static av_cold int ffat_create_decoder(AVCodecContext *avctx, #endif } else { in_format.mSampleRate = avctx->sample_rate ? avctx->sample_rate : 44100; - in_format.mChannelsPerFrame = avctx->channels ? avctx->channels : 1; + in_format.mChannelsPerFrame = avctx->ch_layout.nb_channels ? avctx->ch_layout.nb_channels : 1; } avctx->sample_rate = out_format.mSampleRate = in_format.mSampleRate; - avctx->channels = out_format.mChannelsPerFrame = in_format.mChannelsPerFrame; + avctx->ch_layout.nb_channels = out_format.mChannelsPerFrame = in_format.mChannelsPerFrame; if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT) in_format.mFramesPerPacket = 64; @@ -389,7 +390,7 @@ static av_cold int ffat_create_decoder(AVCodecContext *avctx, ffat_update_ctx(avctx); if(!(at->decoded_data = av_malloc(av_get_bytes_per_sample(avctx->sample_fmt) - * avctx->frame_size * avctx->channels))) + * avctx->frame_size * avctx->ch_layout.nb_channels))) return AVERROR(ENOMEM); at->last_pts = AV_NOPTS_VALUE; @@ -408,7 +409,7 @@ static av_cold int ffat_init_decoder(AVCodecContext *avctx) memcpy(at->extradata, avctx->extradata, avctx->extradata_size); } - if ((avctx->channels && avctx->sample_rate) || ffat_usable_extradata(avctx)) + if ((avctx->ch_layout.nb_channels && avctx->sample_rate) || ffat_usable_extradata(avctx)) return ffat_create_decoder(avctx, NULL); else return 0; @@ -455,11 +456,11 @@ static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_pac #define COPY_SAMPLES(type) \ type *in_ptr = (type*)at->decoded_data; \ - type *end_ptr = in_ptr + frame->nb_samples * avctx->channels; \ + type *end_ptr = in_ptr + frame->nb_samples * avctx->ch_layout.nb_channels; \ type *out_ptr = (type*)frame->data[0]; \ - for (; in_ptr < end_ptr; in_ptr += avctx->channels, out_ptr += avctx->channels) { \ + for (; in_ptr < end_ptr; in_ptr += avctx->ch_layout.nb_channels, out_ptr += avctx->ch_layout.nb_channels) { \ int c; \ - for (c = 0; c < avctx->channels; c++) \ + for (c = 0; c < avctx->ch_layout.nb_channels; c++) \ out_ptr[c] = in_ptr[at->channel_map[c]]; \ } @@ -509,9 +510,9 @@ static int ffat_decode(AVCodecContext *avctx, void *data, .mNumberBuffers = 1, .mBuffers = { { - .mNumberChannels = avctx->channels, + .mNumberChannels = avctx->ch_layout.nb_channels, .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->frame_size - * avctx->channels, + * avctx->ch_layout.nb_channels, } } }; diff --git a/libavcodec/audiotoolboxenc.c b/libavcodec/audiotoolboxenc.c index 9245aa9dc4..aeda1c36fd 100644 --- a/libavcodec/audiotoolboxenc.c +++ b/libavcodec/audiotoolboxenc.c @@ -178,18 +178,17 @@ static av_cold int get_channel_label(int channel) return -1; } -static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count) +static int remap_layout(AudioChannelLayout *layout, const AVChannelLayout *in_layout) { int i; - int c = 0; layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions; - layout->mNumberChannelDescriptions = count; - for (i = 0; i < count; i++) { - int label; - while (!(in_layout & (1 << c)) && c < 64) - c++; - if (c == 64) - return AVERROR(EINVAL); // This should never happen + layout->mNumberChannelDescriptions = in_layout->nb_channels; + for (i = 0; i < in_layout->nb_channels; i++) { + int c, label; + + c = av_channel_layout_get_channel(in_layout, i); + if (c < 0 || c >= 64) + return AVERROR(EINVAL); label = get_channel_label(c); layout->mChannelDescriptions[i].mChannelLabel = label; if (label < 0) @@ -199,38 +198,33 @@ static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int coun return 0; } -static int get_aac_tag(uint64_t in_layout) +static int get_aac_tag(const AVChannelLayout *in_layout) { - switch (in_layout) { - case AV_CH_LAYOUT_MONO: - return kAudioChannelLayoutTag_Mono; - case AV_CH_LAYOUT_STEREO: - return kAudioChannelLayoutTag_Stereo; - case AV_CH_LAYOUT_QUAD: - return kAudioChannelLayoutTag_AAC_Quadraphonic; - case AV_CH_LAYOUT_OCTAGONAL: - return kAudioChannelLayoutTag_AAC_Octagonal; - case AV_CH_LAYOUT_SURROUND: - return kAudioChannelLayoutTag_AAC_3_0; - case AV_CH_LAYOUT_4POINT0: - return kAudioChannelLayoutTag_AAC_4_0; - case AV_CH_LAYOUT_5POINT0: - return kAudioChannelLayoutTag_AAC_5_0; - case AV_CH_LAYOUT_5POINT1: - return kAudioChannelLayoutTag_AAC_5_1; - case AV_CH_LAYOUT_6POINT0: - return kAudioChannelLayoutTag_AAC_6_0; - case AV_CH_LAYOUT_6POINT1: - return kAudioChannelLayoutTag_AAC_6_1; - case AV_CH_LAYOUT_7POINT0: - return kAudioChannelLayoutTag_AAC_7_0; - case AV_CH_LAYOUT_7POINT1_WIDE_BACK: - return kAudioChannelLayoutTag_AAC_7_1; - case AV_CH_LAYOUT_7POINT1: - return kAudioChannelLayoutTag_MPEG_7_1_C; - default: - return 0; - } + static const struct { + AVChannelLayout chl; + int tag; + } map[] = { + { AV_CH_LAYOUT_MONO, kAudioChannelLayoutTag_Mono }, + { AV_CH_LAYOUT_STEREO, kAudioChannelLayoutTag_Stereo }, + { AV_CH_LAYOUT_QUAD, kAudioChannelLayoutTag_AAC_Quadraphonic }, + { AV_CH_LAYOUT_OCTAGONAL, kAudioChannelLayoutTag_AAC_Octagonal }, + { AV_CH_LAYOUT_SURROUND, kAudioChannelLayoutTag_AAC_3_0 }, + { AV_CH_LAYOUT_4POINT0, kAudioChannelLayoutTag_AAC_4_0 }, + { AV_CH_LAYOUT_5POINT0, kAudioChannelLayoutTag_AAC_5_0 }, + { AV_CH_LAYOUT_5POINT1, kAudioChannelLayoutTag_AAC_5_1 }, + { AV_CH_LAYOUT_6POINT0, kAudioChannelLayoutTag_AAC_6_0 }, + { AV_CH_LAYOUT_6POINT1, kAudioChannelLayoutTag_AAC_6_1 }, + { AV_CH_LAYOUT_7POINT0, kAudioChannelLayoutTag_AAC_7_0 }, + { AV_CH_LAYOUT_7POINT1_WIDE_BACK, kAudioChannelLayoutTag_AAC_7_1 }, + { AV_CH_LAYOUT_7POINT1, kAudioChannelLayoutTag_MPEG_7_1_C }, + }; + int i; + + for (i = 0; i < FF_ARRAY_ELEMS; i++) + if (!av_channel_layout_compare(in_layout, &map[i].chl)) + return map[i].tag; + + return 0; } static av_cold int ffat_init_encoder(AVCodecContext *avctx) @@ -246,10 +240,10 @@ static av_cold int ffat_init_encoder(AVCodecContext *avctx) : avctx->sample_fmt == AV_SAMPLE_FMT_U8 ? 0 : kAudioFormatFlagIsSignedInteger) | kAudioFormatFlagIsPacked, - .mBytesPerPacket = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels, + .mBytesPerPacket = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->ch_layout.nb_channels, .mFramesPerPacket = 1, - .mBytesPerFrame = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels, - .mChannelsPerFrame = avctx->channels, + .mBytesPerFrame = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->ch_layout.nb_channels, + .mChannelsPerFrame = avctx->ch_layout.nb_channels, .mBitsPerChannel = av_get_bytes_per_sample(avctx->sample_fmt) * 8, }; AudioStreamBasicDescription out_format = { @@ -258,7 +252,7 @@ static av_cold int ffat_init_encoder(AVCodecContext *avctx) .mChannelsPerFrame = in_format.mChannelsPerFrame, }; UInt32 layout_size = sizeof(AudioChannelLayout) + - sizeof(AudioChannelDescription) * avctx->channels; + sizeof(AudioChannelDescription) * avctx->ch_layout.nb_channels; AudioChannelLayout *channel_layout = av_malloc(layout_size); if (!channel_layout) @@ -278,10 +272,7 @@ static av_cold int ffat_init_encoder(AVCodecContext *avctx) return AVERROR_UNKNOWN; } - if (!avctx->channel_layout) - avctx->channel_layout = av_get_default_channel_layout(avctx->channels); - - if ((status = remap_layout(channel_layout, avctx->channel_layout, avctx->channels)) < 0) { + if ((status = remap_layout(channel_layout, &avctx->ch_layout)) < 0) { av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n"); av_free(channel_layout); return status; @@ -294,7 +285,7 @@ static av_cold int ffat_init_encoder(AVCodecContext *avctx) return AVERROR(EINVAL); } if (avctx->codec_id == AV_CODEC_ID_AAC) { - int tag = get_aac_tag(avctx->channel_layout); + int tag = get_aac_tag(&avctx->ch_layout); if (tag) { channel_layout->mChannelLayoutTag = tag; channel_layout->mNumberChannelDescriptions = 0; @@ -476,10 +467,10 @@ static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_pac frame = ff_bufqueue_get(&at->frame_queue); data->mNumberBuffers = 1; - data->mBuffers[0].mNumberChannels = avctx->channels; + data->mBuffers[0].mNumberChannels = avctx->ch_layout.nb_channels; data->mBuffers[0].mDataByteSize = frame->nb_samples * av_get_bytes_per_sample(avctx->sample_fmt) * - avctx->channels; + avctx->ch_layout.nb_channels; data->mBuffers[0].mData = frame->data[0]; if (*nb_packets > frame->nb_samples) *nb_packets = frame->nb_samples; @@ -506,7 +497,7 @@ static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt, .mNumberBuffers = 1, .mBuffers = { { - .mNumberChannels = avctx->channels, + .mNumberChannels = avctx->ch_layout.nb_channels, .mDataByteSize = at->pkt_size, } }