From patchwork Sun Apr 14 18:30:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 48056 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:670b:b0:1a9:af23:56c1 with SMTP id wh11csp1407516pzb; Sun, 14 Apr 2024 11:31:05 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUE5PaQbdWHY1i3kIxZBfmE5aV+QJCL+oqBK1MGvPBODNfEUk8ltwe80cbEfYZtMnc1jqjlaKmXm9PQEfsAn2fLC/sOjZtf8+i1Mw== X-Google-Smtp-Source: AGHT+IF80ud3c9rmCer9wEiOIK0qU9AG+1O5mkTW1YnopdD1cca/R/MyeG0f0S5UEsNX9eF6o2vN X-Received: by 2002:a17:907:970f:b0:a52:3451:8a18 with SMTP id jg15-20020a170907970f00b00a5234518a18mr6680395ejc.29.1713119465363; Sun, 14 Apr 2024 11:31:05 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ho40-20020a1709070ea800b00a51fc4903c7si3722353ejc.1054.2024.04.14.11.31.04; Sun, 14 Apr 2024 11:31:05 -0700 (PDT) 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=@outlook.com header.s=selector1 header.b=lXJZm9pA; arc=fail (body hash mismatch); 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=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0AEC468D23D; Sun, 14 Apr 2024 21:30:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02olkn2035.outbound.protection.outlook.com [40.92.49.35]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8C6FF68D3D2 for ; Sun, 14 Apr 2024 21:30:49 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lp+ifYLYG0xcIkxF7p0pae2O1EXWCxO0GGX2hh/J2tJMEiEjA+nWNz8uF9z2TD9SsmSykD5cIH68hcWzmi3zcbSO1pglfnRA6ygaWC7g7HgIffcl9gvoviJ1I2nY2AMuCNLH8DaM1aojNS1q9CByoADUF76cQXRDn0c/8shn0zJWM97g8Cith3aSvWc8q0rc1nRTa6QBRgwo3xQJDhxGfdz8LJq3ebjKXpO1grR5ncQaDSaK0/ABOGScPsatV+a+eJWOILhgVNHW/nNoBQMe1MubGY6JBOUaqCtYRkadvO3yDniU3GdQjTiZyPNXUlTRNJkyK2KD5yC+cMtNxz7O/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Q7RGcv4XSn09NcFoQ9ZZCM/KYM25C/btg3QnLgxR0w0=; b=jBuBMp7XVY5b9nsAfyYSmufn17h9h3VBBJ65b9vnha4aCXfm/KUlZWkhNLRRxtjwwPaK05x9tCFeyTT5SgPn7uCA4yYL7ewi3nzMGVGdq+iyue+uJpCU0E8ec84CKrCGotEG2S4yJt1IyNaHRsOassipsKdkdCibrMfsGXaPCcEvZk6AKXUccMdy8QTmLZipr1FQ6zM+OmXPGKPwsw9bT+6Lw9WFFsYWGDZiqTVvvQsegdgrjC+0nVXuakDZ4cajpvmtWweAPodCHva42wbTLfr+jIElJ3rhbi4dbkPxgfwFQTUl464biVXJabkr6BDTPX2dwNVULSjNPNwlB4kb3w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Q7RGcv4XSn09NcFoQ9ZZCM/KYM25C/btg3QnLgxR0w0=; b=lXJZm9pAVM+z1vU7kwI8zINrrDmS9eeKf8UErYdhGmgtOp6tV3jHSOh6MYnK6caDUAZmrFStN3VMVb4nDy3hnWe1ugVWJ+VkrWqxf2bfHOa2bH+5L7d8a+nKCR4bv73fuYZMPnEXmgjfArKN6T6UFY4ieqYhA2hETselRkvoqZR6yHRToiqQjRUgmFnJ7km37CZQDAlH9nit3+RhLly7b8wrCtKHdWGMUXtG1La9kXmzxwrnPDH/SSArVwLahaL0TGU6vwHC3HeFigF/RBqgZzV+Wn9YD2m87aJViH/165JUn1kCS1KF8h6V+OEMwzCJ07PE4TXHo6lYjCz4p5MOHQ== Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) by AS4P250MB0413.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:4c0::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.56; Sun, 14 Apr 2024 18:30:47 +0000 Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::1f29:8206:b8c3:45bb]) by AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::1f29:8206:b8c3:45bb%3]) with mapi id 15.20.7409.042; Sun, 14 Apr 2024 18:30:47 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sun, 14 Apr 2024 20:30:05 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [AITMXU51RlieWkc2jnZwfDOY5oKYHr7n] X-ClientProxiedBy: LO2P265CA0374.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::26) To AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) X-Microsoft-Original-Message-ID: <20240414183005.3201646-5-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8P250MB0744:EE_|AS4P250MB0413:EE_ X-MS-Office365-Filtering-Correlation-Id: acc15d10-7927-4706-675f-08dc5cb100cd X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2AGlDnGnwnKVanmTXLdHeKH9eHZBiM3v1ZtUUMTvu+brmMz50mC/Saq0cHrSTgxaHlTTHyr/STf+REtLZaUiH9XWU1lJwoNdCq21wVX36TH6f4uVEwNnh2Z710VltQ96PVA6VX01w7oJd8tPhFaD471qwzWrlEnvb0s4mVCrAlzH37Pe3k+ZSU1qVpBgjdR5LVH8IbhVSc3F0zXuzizg2hihtpQmHOURlfdjLMSggBAQGRd32dbEg0oJxH+3piHyhkhkJyXQqxB7SoUyAno3LWTxF8EU4j9fYwIXZEtQRXZPG3Dgti/F4/zmG7CNFpijfTZmTcEwjIOCQ9cxRMhy+uUaE7XN+Gy3pId+7WDWM0N7cDdUbGm5UuoA6uCJ+TUg8/8APLGvpubGTPWDamGQlRJUGp0m8HpWVv4y3KWb2nQLi5IUBABizhrs9+6Mz8aF3bCMAmHMtUa8W1I5RuqZaYSWwPwG8KQlTLiuGwDljNlEXtRDe5+jBChxJDqcfIegXOT1989BAB/n0I6ysV43VS1YJdLJH4G+LGA+XTe9q0qGsqNWh2Nb7EDen9sDUhwPgz3/nhxc/Q1YWPHu0k6+RR63d/BopI0WRScP5PJUM9F2+ySt8nXDYgvs1ui+ZOxd X-MS-Exchange-AntiSpam-Mess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f+zSl1pUEGcswNM5FkT6tz56RqPSB9hFLM7lVtI/9pcgSZ8N5SOAy0FtcNqANKY2Zp7rqpEV/bGkFNC1O1hh4kiDTPrVnWT/z+yFPPdoOXAKDyv/o3x0XW1gqnPEgrcjkghm5E4r+gRk/BO2hRhb7K8Ml1+sVFzsdnruhKLQ3kh5Dsw/PfWmMYJ6D+OcGiLCoHZKKiofbEW2BGdFoQgvUpGbpve/LD943AjwML3dPlf3c6wSFotIlvMTH9RC235Qg4v09E9CLEedgA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: acc15d10-7927-4706-675f-08dc5cb100cd X-MS-Exchange-CrossTenant-AuthSource: AS8P250MB0744.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Apr 2024 18:30:47.4403 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS4P250MB0413 Subject: [FFmpeg-devel] [PATCH 6/6] avcodec/ac3enc: Avoid copying samples 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ajht8OHzNdtj Only the last 256 samples of each frame are used; the encoder currently uses a buffer for 1536 + 256 samples whose first 256 samples contain are the last 256 samples from the last frame and the next 1536 are the samples of the current frame. Yet since 238b2d4155d9779d770fccb3594076bb32742c82 all the DSP functions only need 256 contiguous samples and this can be achieved by only retaining the last 256 samples of each frame. Doing so saves 6KiB per channel. Signed-off-by: Andreas Rheinhardt --- libavcodec/ac3enc.c | 29 ++--------------------------- libavcodec/ac3enc.h | 2 +- libavcodec/ac3enc_template.c | 20 ++++++++++++++------ 3 files changed, 17 insertions(+), 34 deletions(-) diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index 71d3026d40..1a869ab865 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -503,28 +503,6 @@ static void ac3_adjust_frame_size(AC3EncodeContext *s) s->samples_written += AC3_BLOCK_SIZE * s->num_blocks; } -/* - * Copy input samples. - * Channels are reordered from FFmpeg's default order to AC-3 order. - */ -static void copy_input_samples(AC3EncodeContext *s, uint8_t * const *samples) -{ - const unsigned sampletype_size = SAMPLETYPE_SIZE(s); - - /* copy and remap input samples */ - for (int ch = 0; ch < s->channels; ch++) { - /* copy last 256 samples of previous frame to the start of the current frame */ - memcpy(&s->planar_samples[ch][0], - s->planar_samples[ch] + AC3_BLOCK_SIZE * sampletype_size * s->num_blocks, - AC3_BLOCK_SIZE * sampletype_size); - - /* copy new samples for current frame */ - memcpy(s->planar_samples[ch] + AC3_BLOCK_SIZE * sampletype_size, - samples[s->channel_map[ch]], - sampletype_size * AC3_BLOCK_SIZE * s->num_blocks); - } -} - /** * Set the initial coupling strategy parameters prior to coupling analysis. * @@ -2018,9 +1996,7 @@ int ff_ac3_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, if (s->bit_alloc.sr_code == 1 || s->eac3) ac3_adjust_frame_size(s); - copy_input_samples(s, frame->extended_data); - - s->encode_frame(s); + s->encode_frame(s, frame->extended_data); ac3_apply_rematrixing(s); @@ -2442,8 +2418,7 @@ static av_cold int allocate_buffers(AC3EncodeContext *s) const unsigned sampletype_size = SAMPLETYPE_SIZE(s); for (int ch = 0; ch < s->channels; ch++) { - s->planar_samples[ch] = av_mallocz((AC3_FRAME_SIZE + AC3_BLOCK_SIZE) * - sampletype_size); + s->planar_samples[ch] = av_mallocz(AC3_BLOCK_SIZE * sampletype_size); if (!s->planar_samples[ch]) return AVERROR(ENOMEM); } diff --git a/libavcodec/ac3enc.h b/libavcodec/ac3enc.h index 4241a908a1..30812617cc 100644 --- a/libavcodec/ac3enc.h +++ b/libavcodec/ac3enc.h @@ -253,7 +253,7 @@ typedef struct AC3EncodeContext { int ref_bap_set; ///< indicates if ref_bap pointers have been set /** fixed vs. float function pointers */ - void (*encode_frame)(struct AC3EncodeContext *s); + void (*encode_frame)(struct AC3EncodeContext *s, uint8_t * const *samples); /* AC-3 vs. E-AC-3 function pointers */ void (*output_frame_header)(struct AC3EncodeContext *s); diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c index 698042ae5c..49fc6d7f37 100644 --- a/libavcodec/ac3enc_template.c +++ b/libavcodec/ac3enc_template.c @@ -48,25 +48,33 @@ * This applies the KBD window and normalizes the input to reduce precision * loss due to fixed-point calculations. */ -static void apply_mdct(AC3EncodeContext *s) +static void apply_mdct(AC3EncodeContext *s, uint8_t * const *samples) { int blk, ch; for (ch = 0; ch < s->channels; ch++) { + const SampleType *input_samples0 = (const SampleType*)s->planar_samples[ch]; + /* Reorder channels from native order to AC-3 order. */ + const SampleType *input_samples1 = (const SampleType*)samples[s->channel_map[ch]]; + for (blk = 0; blk < s->num_blocks; blk++) { AC3Block *block = &s->blocks[blk]; - const SampleType *input_samples = (SampleType*)s->planar_samples[ch] + blk * AC3_BLOCK_SIZE; SampleType *windowed_samples = s->RENAME(windowed_samples); - s->fdsp->vector_fmul(windowed_samples, input_samples, + s->fdsp->vector_fmul(windowed_samples, input_samples0, s->RENAME(mdct_window), AC3_BLOCK_SIZE); s->fdsp->vector_fmul_reverse(windowed_samples + AC3_BLOCK_SIZE, - &input_samples[AC3_BLOCK_SIZE], + input_samples1, s->RENAME(mdct_window), AC3_BLOCK_SIZE); s->tx_fn(s->tx, block->mdct_coef[ch+1], windowed_samples, sizeof(*windowed_samples)); + input_samples0 = input_samples1; + input_samples1 += AC3_BLOCK_SIZE; } + /* Store last 256 samples of current frame */ + memcpy(s->planar_samples[ch], input_samples0, + AC3_BLOCK_SIZE * sizeof(*input_samples0)); } } @@ -336,9 +344,9 @@ static void compute_rematrixing_strategy(AC3EncodeContext *s) } -static void encode_frame(AC3EncodeContext *s) +static void encode_frame(AC3EncodeContext *s, uint8_t * const *samples) { - apply_mdct(s); + apply_mdct(s, samples); s->cpl_on = s->cpl_enabled; ff_ac3_compute_coupling_strategy(s);