From 97c7c39d2539127094a3c30a1d13653e25b85d02 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Mon, 31 Aug 2015 16:45:55 +0000 Subject: [PATCH] avfilter/vf_waveform: simplify memory allocations Signed-off-by: Paul B Mahol --- libavfilter/vf_waveform.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/libavfilter/vf_waveform.c b/libavfilter/vf_waveform.c index eb49ef39e2..a856ccb877 100644 --- a/libavfilter/vf_waveform.c +++ b/libavfilter/vf_waveform.c @@ -52,6 +52,7 @@ typedef struct WaveformContext { int eend[4]; int *emax[4]; int *emin[4]; + int *peak; int filter; int size; void (*waveform)(struct WaveformContext *s, AVFrame *in, AVFrame *out, @@ -908,19 +909,20 @@ static int config_output(AVFilterLink *outlink) comp++; } - for (p = 0; p < 4; p++) { - av_freep(&s->emax[p]); - av_freep(&s->emin[p]); - } + av_freep(&s->peak); if (s->mode) { outlink->h = s->size * FFMAX(comp * s->display, 1); - size = inlink->w * sizeof(int); + size = inlink->w; } else { outlink->w = s->size * FFMAX(comp * s->display, 1); - size = inlink->h * sizeof(int); + size = inlink->h; } + s->peak = av_malloc_array(size, 8 * sizeof(*s->peak)); + if (!s->peak) + return AVERROR(ENOMEM); + for (p = 0; p < 4; p++) { const int is_chroma = (p == 1 || p == 2); const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0); @@ -933,8 +935,8 @@ static int config_output(AVFilterLink *outlink) shift = s->mode ? shift_h : shift_w; - s->emax[plane] = av_malloc(size); - s->emin[plane] = av_malloc(size); + s->emax[plane] = s->peak + size * (p + 0); + s->emin[plane] = s->peak + size * (p + 4); if (!s->emin[plane] || !s->emax[plane]) return AVERROR(ENOMEM); @@ -942,7 +944,7 @@ static int config_output(AVFilterLink *outlink) offset = j++ * s->size * s->display; s->estart[plane] = offset >> shift; s->eend[plane] = (offset + s->size - 1) >> shift; - for (i = 0; i < size / sizeof(int); i++) { + for (i = 0; i < size; i++) { s->emax[plane][i] = s->estart[plane]; s->emin[plane][i] = s->eend[plane]; } @@ -992,12 +994,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) static av_cold void uninit(AVFilterContext *ctx) { WaveformContext *s = ctx->priv; - int p; - for (p = 0; p < 4; p++) { - av_freep(&s->emax[p]); - av_freep(&s->emin[p]); - } + av_freep(&s->peak); } static const AVFilterPad inputs[] = {