From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 6/6] avcodec/ac3enc: Avoid copying samples Date: Sun, 14 Apr 2024 20:30:05 +0200 Message-ID: <AS8P250MB07448A705356762FC2B5D3468F0A2@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM> (raw) In-Reply-To: <AS8P250MB0744432E9416A30719F18EDF8F0A2@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM> 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 <andreas.rheinhardt@outlook.com> --- 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); -- 2.40.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2024-04-14 18:31 UTC|newest] Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top 2024-04-14 18:28 [FFmpeg-devel] [PATCH 1/6] avcodec/ac3enc: Avoid allocation for windowed_samples Andreas Rheinhardt 2024-04-14 18:30 ` [FFmpeg-devel] [PATCH 2/6] avcodec/ac3enc: Avoid allocation for mdct_window Andreas Rheinhardt 2024-04-14 18:30 ` [FFmpeg-devel] [PATCH 3/6] avcodec/ac3enc: Avoid indirections, allocations of small arrays Andreas Rheinhardt 2024-04-14 18:30 ` [FFmpeg-devel] [PATCH 4/6] avcodec/ac3enc: Combine loops Andreas Rheinhardt 2024-04-14 18:30 ` [FFmpeg-devel] [PATCH 5/6] avcodec/ac3enc: Combine cpl_coord buffers Andreas Rheinhardt 2024-04-14 18:30 ` Andreas Rheinhardt [this message] 2024-04-17 15:04 ` [FFmpeg-devel] [PATCH 1/6] avcodec/ac3enc: Avoid allocation for windowed_samples Andreas Rheinhardt
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=AS8P250MB07448A705356762FC2B5D3468F0A2@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM \ --to=andreas.rheinhardt@outlook.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git