avcodec/truemotion2: Allocate buffers together

Reduces the number of allocations and frees.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
This commit is contained in:
Andreas Rheinhardt 2020-09-27 15:55:27 +02:00
parent fa1d105a6c
commit 0302728c9e
1 changed files with 19 additions and 27 deletions

View File

@ -81,7 +81,7 @@ typedef struct TM2Context {
int *clast;
/* data for current and previous frame */
int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
int *Y_base, *UV_base;
int *Y1, *U1, *V1, *Y2, *U2, *V2;
int y_stride, uv_stride;
int cur;
@ -966,32 +966,29 @@ static av_cold int decode_init(AVCodecContext *avctx)
ff_bswapdsp_init(&l->bdsp);
l->last = av_malloc_array(w >> 2, 4 * sizeof(*l->last) );
l->clast = av_malloc_array(w >> 2, 4 * sizeof(*l->clast));
l->last = av_malloc_array(w, 2 * sizeof(*l->last));
if (!l->last)
return AVERROR(ENOMEM);
l->clast = l->last + w;
w += 8;
h += 8;
l->Y1_base = av_calloc(w * h, sizeof(*l->Y1_base));
l->Y2_base = av_calloc(w * h, sizeof(*l->Y2_base));
l->Y_base = av_calloc(w * h, 2 * sizeof(*l->Y_base));
if (!l->Y_base)
return AVERROR(ENOMEM);
l->y_stride = w;
l->Y1 = l->Y_base + l->y_stride * 4 + 4;
l->Y2 = l->Y1 + w * h;
w = (w + 1) >> 1;
h = (h + 1) >> 1;
l->U1_base = av_calloc(w * h, sizeof(*l->U1_base));
l->V1_base = av_calloc(w * h, sizeof(*l->V1_base));
l->U2_base = av_calloc(w * h, sizeof(*l->U2_base));
l->V2_base = av_calloc(w * h, sizeof(*l->V1_base));
l->uv_stride = w;
if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
!l->V1_base || !l->U2_base || !l->V2_base ||
!l->last || !l->clast) {
l->UV_base = av_calloc(w * h, 4 * sizeof(*l->UV_base));
if (!l->UV_base)
return AVERROR(ENOMEM);
}
l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
l->U1 = l->U1_base + l->uv_stride * 2 + 2;
l->U2 = l->U2_base + l->uv_stride * 2 + 2;
l->V1 = l->V1_base + l->uv_stride * 2 + 2;
l->V2 = l->V2_base + l->uv_stride * 2 + 2;
l->uv_stride = w;
l->U1 = l->UV_base + l->uv_stride * 2 + 2;
l->U2 = l->U1 + w * h;
l->V1 = l->U2 + w * h;
l->V2 = l->V1 + w * h;
return 0;
}
@ -1002,16 +999,11 @@ static av_cold int decode_end(AVCodecContext *avctx)
int i;
av_freep(&l->last);
av_freep(&l->clast);
for (i = 0; i < TM2_NUM_STREAMS; i++)
av_freep(&l->tokens[i]);
av_freep(&l->Y1_base);
av_freep(&l->U1_base);
av_freep(&l->V1_base);
av_freep(&l->Y2_base);
av_freep(&l->U2_base);
av_freep(&l->V2_base);
av_freep(&l->Y_base);
av_freep(&l->UV_base);
av_freep(&l->buffer);
l->buffer_size = 0;