History

avcodec/truemotion2: Allocate buffers together

Reduces the number of allocations and frees.

Andreas Rheinhardt committed 6 years ago

Commit 03027281 parent fa1d1051 file · +19 −27Browse files

Unified · Split

libavcodec/truemotion2.c+19−27
@@ -81,7 +81,7 @@ typedef struct TM2Context { · 80 unmodified lines
8181 int *clast;
8282
8383 /* data for current and previous frame */
84− int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
84+ int *Y_base, *UV_base;
8585 int *Y1, *U1, *V1, *Y2, *U2, *V2;
8686 int y_stride, uv_stride;
8787 int cur;
@@ -966,32 +966,29 @@ static av_cold int decode_init(AVCodecContext *avctx) · 878 unmodified lines
966966
967967 ff_bswapdsp_init(&l->bdsp);
968968
969− l->last = av_malloc_array(w >> 2, 4 * sizeof(*l->last) );
970− l->clast = av_malloc_array(w >> 2, 4 * sizeof(*l->clast));
969+ l->last = av_malloc_array(w, 2 * sizeof(*l->last));
970+ if (!l->last)
971+ return AVERROR(ENOMEM);
972+ l->clast = l->last + w;
971973
972974 w += 8;
973975 h += 8;
974− l->Y1_base = av_calloc(w * h, sizeof(*l->Y1_base));
975− l->Y2_base = av_calloc(w * h, sizeof(*l->Y2_base));
976+ l->Y_base = av_calloc(w * h, 2 * sizeof(*l->Y_base));
977+ if (!l->Y_base)
978+ return AVERROR(ENOMEM);
976979 l->y_stride = w;
980+ l->Y1 = l->Y_base + l->y_stride * 4 + 4;
981+ l->Y2 = l->Y1 + w * h;
977982 w = (w + 1) >> 1;
978983 h = (h + 1) >> 1;
979− l->U1_base = av_calloc(w * h, sizeof(*l->U1_base));
980− l->V1_base = av_calloc(w * h, sizeof(*l->V1_base));
981− l->U2_base = av_calloc(w * h, sizeof(*l->U2_base));
982− l->V2_base = av_calloc(w * h, sizeof(*l->V1_base));
983− l->uv_stride = w;
984− if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
985− !l->V1_base || !l->U2_base || !l->V2_base ||
986− !l->last || !l->clast) {
984+ l->UV_base = av_calloc(w * h, 4 * sizeof(*l->UV_base));
985+ if (!l->UV_base)
987986 return AVERROR(ENOMEM);
988− }
989− l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
990− l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
991− l->U1 = l->U1_base + l->uv_stride * 2 + 2;
992− l->U2 = l->U2_base + l->uv_stride * 2 + 2;
993− l->V1 = l->V1_base + l->uv_stride * 2 + 2;
994− l->V2 = l->V2_base + l->uv_stride * 2 + 2;
987+ l->uv_stride = w;
988+ l->U1 = l->UV_base + l->uv_stride * 2 + 2;
989+ l->U2 = l->U1 + w * h;
990+ l->V1 = l->U2 + w * h;
991+ l->V2 = l->V1 + w * h;
995992
996993 return 0;
997994 }
@@ -1002,16 +999,11 @@ static av_cold int decode_end(AVCodecContext *avctx) · 4 unmodified lines
1002999 int i;
10031000
10041001 av_freep(&l->last);
1005− av_freep(&l->clast);
10061002 for (i = 0; i < TM2_NUM_STREAMS; i++)
10071003 av_freep(&l->tokens[i]);
10081004
1009− av_freep(&l->Y1_base);
1010− av_freep(&l->U1_base);
1011− av_freep(&l->V1_base);
1012− av_freep(&l->Y2_base);
1013− av_freep(&l->U2_base);
1014− av_freep(&l->V2_base);
1005+ av_freep(&l->Y_base);
1006+ av_freep(&l->UV_base);
10151007 av_freep(&l->buffer);
10161008 l->buffer_size = 0;
10171009

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t