Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Subject: [FFmpeg-devel] [PATCH 11/17] avcodec/ac3enc: Deduplicate allocating buffers
Date: Sun,  7 Apr 2024 23:09:09 +0200
Message-ID: <GV1P250MB07378020747651950ABE4AFF8F012@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <GV1P250MB073758D71A50C11C0A3C26CE8F012@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM>

These allocations 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          | 16 +++++++++++++++-
 libavcodec/ac3enc.h          |  7 ++-----
 libavcodec/ac3enc_fixed.c    |  1 -
 libavcodec/ac3enc_float.c    |  1 -
 libavcodec/ac3enc_template.c | 32 +++++++-------------------------
 5 files changed, 24 insertions(+), 33 deletions(-)

diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index ef1ac381c1..681b227d3d 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -52,6 +52,9 @@
 #include "ac3enc.h"
 #include "eac3enc.h"
 
+#define SAMPLETYPE_SIZE(ctx) (sizeof(float) == sizeof(int32_t) ? sizeof(float) : \
+                                  (ctx)->fixed_point ? sizeof(int32_t) : sizeof(float))
+
 typedef struct AC3Mant {
     int16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4
     int mant1_cnt, mant2_cnt, mant4_cnt;    ///< mantissa counts for bap=1,2,4
@@ -2429,10 +2432,21 @@ static av_cold int allocate_buffers(AC3EncodeContext *s)
     int channels = s->channels + 1; /* includes coupling channel */
     int channel_blocks = channels * s->num_blocks;
     int total_coefs    = AC3_MAX_COEFS * channel_blocks;
+    const unsigned sampletype_size = SAMPLETYPE_SIZE(s);
+
+    if (!(s->windowed_samples = av_malloc(sampletype_size * AC3_WINDOW_SIZE)))
+        return AVERROR(ENOMEM);
 
-    if (s->allocate_sample_buffers(s))
+    if (!FF_ALLOCZ_TYPED_ARRAY(s->planar_samples,  s->channels))
         return AVERROR(ENOMEM);
 
+    for (int ch = 0; ch < s->channels; ch++) {
+        s->planar_samples[ch] = av_mallocz((AC3_FRAME_SIZE + AC3_BLOCK_SIZE) *
+                                                  sampletype_size);
+        if (!s->planar_samples[ch])
+            return AVERROR(ENOMEM);
+    }
+
     if (!FF_ALLOC_TYPED_ARRAY(s->bap_buffer,         total_coefs)          ||
         !FF_ALLOC_TYPED_ARRAY(s->bap1_buffer,        total_coefs)          ||
         !FF_ALLOCZ_TYPED_ARRAY(s->mdct_coef_buffer,  total_coefs)          ||
diff --git a/libavcodec/ac3enc.h b/libavcodec/ac3enc.h
index 3dc8acfd91..8d6cb3b3de 100644
--- a/libavcodec/ac3enc.h
+++ b/libavcodec/ac3enc.h
@@ -232,8 +232,8 @@ typedef struct AC3EncodeContext {
     int frame_bits;                         ///< all frame bits except exponents and mantissas
     int exponent_bits;                      ///< number of bits used for exponents
 
-    SampleType *windowed_samples;
-    SampleType **planar_samples;
+    void *windowed_samples;
+    uint8_t **planar_samples;
     uint8_t *bap_buffer;
     uint8_t *bap1_buffer;
     CoefType *mdct_coef_buffer;
@@ -260,9 +260,6 @@ typedef struct AC3EncodeContext {
     /* fixed vs. float function pointers */
     int  (*mdct_init)(struct AC3EncodeContext *s);
 
-    /* fixed vs. float templated function pointers */
-    int  (*allocate_sample_buffers)(struct AC3EncodeContext *s);
-
     /* AC-3 vs. E-AC-3 function pointers */
     void (*output_frame_header)(struct AC3EncodeContext *s);
 } AC3EncodeContext;
diff --git a/libavcodec/ac3enc_fixed.c b/libavcodec/ac3enc_fixed.c
index 4a24cce833..b8a4d88a42 100644
--- a/libavcodec/ac3enc_fixed.c
+++ b/libavcodec/ac3enc_fixed.c
@@ -104,7 +104,6 @@ static av_cold int ac3_fixed_encode_init(AVCodecContext *avctx)
     s->fixed_point = 1;
     s->encode_frame            = encode_frame;
     s->mdct_init               = ac3_fixed_mdct_init;
-    s->allocate_sample_buffers = allocate_sample_buffers;
     return ff_ac3_encode_init(avctx);
 }
 
diff --git a/libavcodec/ac3enc_float.c b/libavcodec/ac3enc_float.c
index e0907fed05..77a7725f31 100644
--- a/libavcodec/ac3enc_float.c
+++ b/libavcodec/ac3enc_float.c
@@ -107,7 +107,6 @@ av_cold int ff_ac3_float_encode_init(AVCodecContext *avctx)
 
     s->encode_frame            = encode_frame;
     s->mdct_init               = ac3_float_mdct_init;
-    s->allocate_sample_buffers = allocate_sample_buffers;
     s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
     if (!s->fdsp)
         return AVERROR(ENOMEM);
diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c
index 56ce36c012..3646e1dcaa 100644
--- a/libavcodec/ac3enc_template.c
+++ b/libavcodec/ac3enc_template.c
@@ -31,8 +31,6 @@
 #include <stdint.h>
 
 #include "libavutil/attributes.h"
-#include "libavutil/internal.h"
-#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "audiodsp.h"
@@ -40,23 +38,6 @@
 #include "eac3enc.h"
 
 
-static int allocate_sample_buffers(AC3EncodeContext *s)
-{
-    int ch;
-
-    if (!FF_ALLOC_TYPED_ARRAY(s->windowed_samples, AC3_WINDOW_SIZE) ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->planar_samples,  s->channels))
-        return AVERROR(ENOMEM);
-
-    for (ch = 0; ch < s->channels; ch++) {
-        if (!(s->planar_samples[ch] = av_mallocz((AC3_FRAME_SIZE + AC3_BLOCK_SIZE) *
-                                                  sizeof(**s->planar_samples))))
-            return AVERROR(ENOMEM);
-    }
-    return 0;
-}
-
-
 /*
  * Copy input samples.
  * Channels are reordered from FFmpeg's default order to AC-3 order.
@@ -68,13 +49,14 @@ static void copy_input_samples(AC3EncodeContext *s, SampleType **samples)
     /* 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], &s->planar_samples[ch][AC3_BLOCK_SIZE * s->num_blocks],
-               AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
+        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(&s->planar_samples[ch][AC3_BLOCK_SIZE],
+        memcpy((SampleType*)s->planar_samples[ch] + AC3_BLOCK_SIZE,
                samples[s->channel_map[ch]],
-               AC3_BLOCK_SIZE * s->num_blocks * sizeof(s->planar_samples[0][0]));
+               AC3_BLOCK_SIZE * s->num_blocks * sizeof(SampleType));
     }
 }
 
@@ -91,11 +73,11 @@ static void apply_mdct(AC3EncodeContext *s)
     for (ch = 0; ch < s->channels; ch++) {
         for (blk = 0; blk < s->num_blocks; blk++) {
             AC3Block *block = &s->blocks[blk];
-            const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
+            const SampleType *input_samples = (SampleType*)s->planar_samples[ch] + blk * AC3_BLOCK_SIZE;
 
             s->fdsp->vector_fmul(s->windowed_samples, input_samples,
                                  s->mdct_window, AC3_BLOCK_SIZE);
-            s->fdsp->vector_fmul_reverse(s->windowed_samples + AC3_BLOCK_SIZE,
+            s->fdsp->vector_fmul_reverse((SampleType*)s->windowed_samples + AC3_BLOCK_SIZE,
                                          &input_samples[AC3_BLOCK_SIZE],
                                          s->mdct_window, AC3_BLOCK_SIZE);
 
-- 
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".

  parent reply	other threads:[~2024-04-07 21:10 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-07 20:39 [FFmpeg-devel] [PATCH 01/17] avcodec/ac3enc: Don't presume ch_layout to be AV_CHANNEL_ORDER_NATIVE Andreas Rheinhardt
2024-04-07 20:51 ` James Almer
2024-04-07 21:53   ` Andreas Rheinhardt
2024-04-07 22:21     ` James Almer
2024-04-07 22:26       ` Andreas Rheinhardt
2024-04-07 22:35         ` James Almer
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 02/17] avcodec/ac3enc: Don't overwrite AVCodecContext.ch_layout Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 03/17] avcodec/ac3enc: Remove redundant channel layout checks Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 04/17] avcodec/ac3enc: Use bit-operations for bit-mask Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 05/17] avcodec/ac3enc: Avoid copying strings Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 06/17] avcodec/ac3enc: Remove always-false sample rate check Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 07/17] avcodec/ac3enc: Remove disabled code for RealAudio variant of AC-3 Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 08/17] avcodec/ac3enc: Use common encode_frame function Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 09/17] avcodec/ac3enc: Move ff_ac3_validate_metadate() upwards Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 10/17] avcodec/ac3enc: Share more code between fixed/float encoders Andreas Rheinhardt
2024-04-07 21:09 ` Andreas Rheinhardt [this message]
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 12/17] avcodec/ac3enc: Deduplicate copying input samples Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 13/17] avcodec/ac3enc_float: Remove uninformative error message Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 14/17] avcodec/ac3enc: Avoid function pointers to initialize MDCT Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 15/17] avcodec/ac3enc: Move EAC-3 specific initialization to eac3enc.c Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 16/17] avcodec/flacenc: Avoid shift where possible Andreas Rheinhardt
2024-04-07 21:09 ` [FFmpeg-devel] [PATCH 17/17] avformat/img2: Avoid relocations for ff_img_tags Andreas Rheinhardt
2024-04-08  0:56 ` [FFmpeg-devel] [PATCH 18/18] avcodec/ac3enc: Avoid calculating the CRC multiple times Andreas Rheinhardt
2024-04-10  6:16 ` [FFmpeg-devel] [PATCH 01/17] avcodec/ac3enc: Don't presume ch_layout to be AV_CHANNEL_ORDER_NATIVE 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=GV1P250MB07378020747651950ABE4AFF8F012@GV1P250MB0737.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