diff mbox series

[FFmpeg-devel,12/17] avcodec/ac3enc: Deduplicate copying input samples

Message ID GV1P250MB0737643599DB822551243B738F012@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit 3cd136bc120aee7ccba5965b400e121dc1476c10
Headers show
Series [FFmpeg-devel,01/17] avcodec/ac3enc: Don't presume ch_layout to be AV_CHANNEL_ORDER_NATIVE | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt April 7, 2024, 9:09 p.m. UTC
These memcpy operands only depend upon sizeof(SampleType)
(and this size is actually the same for both the fixed-point
and the floating-point encoders for most (all supported?)
systems).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/ac3enc.c          | 25 ++++++++++++++++++++++++-
 libavcodec/ac3enc.h          |  2 +-
 libavcodec/ac3enc_template.c | 27 +--------------------------
 3 files changed, 26 insertions(+), 28 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index 681b227d3d..7adb1c444a 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -503,6 +503,27 @@  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.
@@ -1996,7 +2017,9 @@  int ff_ac3_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     if (s->bit_alloc.sr_code == 1 || s->eac3)
         ac3_adjust_frame_size(s);
 
-    s->encode_frame(s, frame);
+    copy_input_samples(s, frame->extended_data);
+
+    s->encode_frame(s);
 
     ac3_apply_rematrixing(s);
 
diff --git a/libavcodec/ac3enc.h b/libavcodec/ac3enc.h
index 8d6cb3b3de..271f0eb8eb 100644
--- a/libavcodec/ac3enc.h
+++ b/libavcodec/ac3enc.h
@@ -255,7 +255,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, const AVFrame *frame);
+    void (*encode_frame)(struct AC3EncodeContext *s);
 
     /* fixed vs. float function pointers */
     int  (*mdct_init)(struct AC3EncodeContext *s);
diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c
index 3646e1dcaa..b0f9e69ee8 100644
--- a/libavcodec/ac3enc_template.c
+++ b/libavcodec/ac3enc_template.c
@@ -38,29 +38,6 @@ 
 #include "eac3enc.h"
 
 
-/*
- * Copy input samples.
- * Channels are reordered from FFmpeg's default order to AC-3 order.
- */
-static void copy_input_samples(AC3EncodeContext *s, SampleType **samples)
-{
-    int ch;
-
-    /* copy and remap input samples */
-    for (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],
-               (SampleType*)s->planar_samples[ch] + AC3_BLOCK_SIZE * s->num_blocks,
-               AC3_BLOCK_SIZE * sizeof(SampleType));
-
-        /* copy new samples for current frame */
-        memcpy((SampleType*)s->planar_samples[ch] + AC3_BLOCK_SIZE,
-               samples[s->channel_map[ch]],
-               AC3_BLOCK_SIZE * s->num_blocks * sizeof(SampleType));
-    }
-}
-
-
 /*
  * Apply the MDCT to input samples to generate frequency coefficients.
  * This applies the KBD window and normalizes the input to reduce precision
@@ -353,10 +330,8 @@  static void compute_rematrixing_strategy(AC3EncodeContext *s)
 }
 
 
-static void encode_frame(AC3EncodeContext *s, const AVFrame *frame)
+static void encode_frame(AC3EncodeContext *s)
 {
-    copy_input_samples(s, (SampleType **)frame->extended_data);
-
     apply_mdct(s);
 
     s->cpl_on = s->cpl_enabled;