diff mbox series

[FFmpeg-devel,2/7] avcodec/sonic: Don't allocate a temporary buffer for every frame

Message ID 20201103175735.170080-2-andreas.rheinhardt@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,1/7] avcodec/sonic: Allocate several buffers together | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Nov. 3, 2020, 5:57 p.m. UTC
Instead allocate it together with the buffer that it mirrors.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/sonic.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

Comments

Andreas Rheinhardt Nov. 6, 2020, 10:10 a.m. UTC | #1
Andreas Rheinhardt:
> Instead allocate it together with the buffer that it mirrors.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/sonic.c | 16 ++++------------
>  1 file changed, 4 insertions(+), 12 deletions(-)
> 
> diff --git a/libavcodec/sonic.c b/libavcodec/sonic.c
> index bca1eca27a..db2c4339a3 100644
> --- a/libavcodec/sonic.c
> +++ b/libavcodec/sonic.c
> @@ -500,14 +500,11 @@ static int predictor_calc_error(int *k, int *state, int order, int error)
>  // copes better with quantization, and calculates the
>  // actual whitened result as it goes.
>  
> -static int modified_levinson_durbin(int *window, int window_entries,
> +static void modified_levinson_durbin(int *window, int window_entries,
>          int *out, int out_entries, int channels, int *tap_quant)
>  {
>      int i;
> -    int *state = av_calloc(window_entries, sizeof(*state));
> -
> -    if (!state)
> -        return AVERROR(ENOMEM);
> +    int *state = window + window_entries;
>  
>      memcpy(state, window, 4* window_entries);
>  
> @@ -571,9 +568,6 @@ static int modified_levinson_durbin(int *window, int window_entries,
>          }
>  #endif
>      }
> -
> -    av_free(state);
> -    return 0;
>  }
>  
>  static inline int code_samplerate(int samplerate)
> @@ -665,7 +659,7 @@ static av_cold int sonic_encode_init(AVCodecContext *avctx)
>      s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
>  
>      s->window_size = ((2*s->tail_size)+s->frame_size);
> -    s->window = av_calloc(s->window_size, sizeof(*s->window));
> +    s->window = av_calloc(s->window_size, 2 * sizeof(*s->window));
>      if (!s->window || !s->int_samples)
>          return AVERROR(ENOMEM);
>  
> @@ -776,10 +770,8 @@ static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
>          s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
>  
>      // generate taps
> -    ret = modified_levinson_durbin(s->window, s->window_size,
> +    modified_levinson_durbin(s->window, s->window_size,
>                  s->predictor_k, s->num_taps, s->channels, s->tap_quant);
> -    if (ret < 0)
> -        return ret;
>  
>      if ((ret = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)
>          return ret;
> 
Will apply the rest of this patchset later today unless there are
objections.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/sonic.c b/libavcodec/sonic.c
index bca1eca27a..db2c4339a3 100644
--- a/libavcodec/sonic.c
+++ b/libavcodec/sonic.c
@@ -500,14 +500,11 @@  static int predictor_calc_error(int *k, int *state, int order, int error)
 // copes better with quantization, and calculates the
 // actual whitened result as it goes.
 
-static int modified_levinson_durbin(int *window, int window_entries,
+static void modified_levinson_durbin(int *window, int window_entries,
         int *out, int out_entries, int channels, int *tap_quant)
 {
     int i;
-    int *state = av_calloc(window_entries, sizeof(*state));
-
-    if (!state)
-        return AVERROR(ENOMEM);
+    int *state = window + window_entries;
 
     memcpy(state, window, 4* window_entries);
 
@@ -571,9 +568,6 @@  static int modified_levinson_durbin(int *window, int window_entries,
         }
 #endif
     }
-
-    av_free(state);
-    return 0;
 }
 
 static inline int code_samplerate(int samplerate)
@@ -665,7 +659,7 @@  static av_cold int sonic_encode_init(AVCodecContext *avctx)
     s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
 
     s->window_size = ((2*s->tail_size)+s->frame_size);
-    s->window = av_calloc(s->window_size, sizeof(*s->window));
+    s->window = av_calloc(s->window_size, 2 * sizeof(*s->window));
     if (!s->window || !s->int_samples)
         return AVERROR(ENOMEM);
 
@@ -776,10 +770,8 @@  static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
 
     // generate taps
-    ret = modified_levinson_durbin(s->window, s->window_size,
+    modified_levinson_durbin(s->window, s->window_size,
                 s->predictor_k, s->num_taps, s->channels, s->tap_quant);
-    if (ret < 0)
-        return ret;
 
     if ((ret = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)
         return ret;