Reduces the number of allocations and frees.
Unified · Split
| @@ -81,7 +81,7 @@ typedef struct TM2Context { · 80 unmodified lines | ||
| 81 | 81 | int *clast; |
| 82 | 82 | |
| 83 | 83 | /* 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; | |
| 85 | 85 | int *Y1, *U1, *V1, *Y2, *U2, *V2; |
| 86 | 86 | int y_stride, uv_stride; |
| 87 | 87 | int cur; |
| @@ -966,32 +966,29 @@ static av_cold int decode_init(AVCodecContext *avctx) · 878 unmodified lines | ||
| 966 | 966 | |
| 967 | 967 | ff_bswapdsp_init(&l->bdsp); |
| 968 | 968 | |
| 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; | |
| 971 | 973 | |
| 972 | 974 | w += 8; |
| 973 | 975 | 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); | |
| 976 | 979 | l->y_stride = w; |
| 980 | + l->Y1 = l->Y_base + l->y_stride * 4 + 4; | |
| 981 | + l->Y2 = l->Y1 + w * h; | |
| 977 | 982 | w = (w + 1) >> 1; |
| 978 | 983 | 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) | |
| 987 | 986 | 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; | |
| 995 | 992 | |
| 996 | 993 | return 0; |
| 997 | 994 | } |
| @@ -1002,16 +999,11 @@ static av_cold int decode_end(AVCodecContext *avctx) · 4 unmodified lines | ||
| 1002 | 999 | int i; |
| 1003 | 1000 | |
| 1004 | 1001 | av_freep(&l->last); |
| 1005 | − av_freep(&l->clast); | |
| 1006 | 1002 | for (i = 0; i < TM2_NUM_STREAMS; i++) |
| 1007 | 1003 | av_freep(&l->tokens[i]); |
| 1008 | 1004 | |
| 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); | |
| 1015 | 1007 | av_freep(&l->buffer); |
| 1016 | 1008 | l->buffer_size = 0; |
| 1017 | 1009 | |