[FFmpeg-devel] [PATCH 1/6] avcodec/ac3enc: Avoid allocation for windowed_samples

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Wed Apr 17 18:04:40 EEST 2024


Andreas Rheinhardt:
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
> ---
>  libavcodec/ac3enc.c          |  4 ----
>  libavcodec/ac3enc.h          |  7 ++++++-
>  libavcodec/ac3enc_template.c | 12 +++++++++---
>  3 files changed, 15 insertions(+), 8 deletions(-)
> 
> diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
> index 1ba4ba549e..a31b528597 100644
> --- a/libavcodec/ac3enc.c
> +++ b/libavcodec/ac3enc.c
> @@ -2184,7 +2184,6 @@ av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
>      AC3EncodeContext *s = avctx->priv_data;
>  
>      av_freep(&s->mdct_window);
> -    av_freep(&s->windowed_samples);
>      if (s->planar_samples)
>          for (ch = 0; ch < s->channels; ch++)
>              av_freep(&s->planar_samples[ch]);
> @@ -2459,9 +2458,6 @@ static av_cold int allocate_buffers(AC3EncodeContext *s)
>      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 (!FF_ALLOCZ_TYPED_ARRAY(s->planar_samples,  s->channels))
>          return AVERROR(ENOMEM);
>  
> diff --git a/libavcodec/ac3enc.h b/libavcodec/ac3enc.h
> index 227744d27f..618c952a18 100644
> --- a/libavcodec/ac3enc.h
> +++ b/libavcodec/ac3enc.h
> @@ -30,6 +30,7 @@
>  
>  #include <stdint.h>
>  
> +#include "libavutil/mem_internal.h"
>  #include "libavutil/opt.h"
>  #include "libavutil/tx.h"
>  
> @@ -232,7 +233,6 @@ typedef struct AC3EncodeContext {
>      int frame_bits;                         ///< all frame bits except exponents and mantissas
>      int exponent_bits;                      ///< number of bits used for exponents
>  
> -    void *windowed_samples;
>      uint8_t **planar_samples;
>      uint8_t *bap_buffer;
>      uint8_t *bap1_buffer;
> @@ -259,6 +259,11 @@ typedef struct AC3EncodeContext {
>  
>      /* AC-3 vs. E-AC-3 function pointers */
>      void (*output_frame_header)(struct AC3EncodeContext *s);
> +
> +    union {
> +        DECLARE_ALIGNED(32, float,   windowed_samples_float)[AC3_WINDOW_SIZE];
> +        DECLARE_ALIGNED(32, int32_t, windowed_samples_fixed)[AC3_WINDOW_SIZE];
> +    };
>  } AC3EncodeContext;
>  
>  extern const AVChannelLayout ff_ac3_ch_layouts[19];
> diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c
> index b0f9e69ee8..6070e14961 100644
> --- a/libavcodec/ac3enc_template.c
> +++ b/libavcodec/ac3enc_template.c
> @@ -37,6 +37,11 @@
>  #include "ac3enc.h"
>  #include "eac3enc.h"
>  
> +#if AC3ENC_FLOAT
> +#define RENAME(element) element ## _float
> +#else
> +#define RENAME(element) element ## _fixed
> +#endif
>  
>  /*
>   * Apply the MDCT to input samples to generate frequency coefficients.
> @@ -51,15 +56,16 @@ static void apply_mdct(AC3EncodeContext *s)
>          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(s->windowed_samples, input_samples,
> +            s->fdsp->vector_fmul(windowed_samples, input_samples,
>                                   s->mdct_window, AC3_BLOCK_SIZE);
> -            s->fdsp->vector_fmul_reverse((SampleType*)s->windowed_samples + AC3_BLOCK_SIZE,
> +            s->fdsp->vector_fmul_reverse(windowed_samples + AC3_BLOCK_SIZE,
>                                           &input_samples[AC3_BLOCK_SIZE],
>                                           s->mdct_window, AC3_BLOCK_SIZE);
>  
>              s->tx_fn(s->tx, block->mdct_coef[ch+1],
> -                     s->windowed_samples, sizeof(float));
> +                     windowed_samples, sizeof(*windowed_samples));
>          }
>      }
>  }

Will apply this patchset tomorrow unless there are objections.

- Andreas



More information about the ffmpeg-devel mailing list