2 * Duck/ON2 TrueMotion 2 Decoder
3 * Copyright (c) 2005 Konstantin Shishkov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Duck TrueMotion2 decoder.
31 #define TM2_ESCAPE 0x80000000
33 /* Huffman-coded streams of different types of blocks */
34 enum TM2_STREAMS{ TM2_C_HI = 0, TM2_C_LO, TM2_L_HI, TM2_L_LO,
35 TM2_UPD, TM2_MOT, TM2_TYPE, TM2_NUM_STREAMS};
37 enum TM2_BLOCKS{ TM2_HI_RES = 0, TM2_MED_RES, TM2_LOW_RES, TM2_NULL_RES,
38 TM2_UPDATE, TM2_STILL, TM2_MOTION};
40 typedef struct TM2Context{
41 AVCodecContext *avctx;
48 int *tokens[TM2_NUM_STREAMS];
49 int tok_lens[TM2_NUM_STREAMS];
50 int tok_ptrs[TM2_NUM_STREAMS];
51 int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
52 /* for blocks decoding */
58 /* data for current and previous frame */
59 int *Y1, *U1, *V1, *Y2, *U2, *V2;
64 * Huffman codes for each of streams
66 typedef struct TM2Codes{
67 VLC vlc; ///< table for FFmpeg bitstream reader
69 int *recode; ///< table for converting from code indexes to values
74 * structure for gathering Huffman codes information
76 typedef struct TM2Huff{
77 int val_bits; ///< length of literal
78 int max_bits; ///< maximum length of code
79 int min_bits; ///< minimum length of code
80 int nodes; ///< total number of nodes in tree
81 int num; ///< current number filled
82 int max_num; ///< total number of codes
83 int *nums; ///< literals
84 uint32_t *bits; ///< codes
85 int *lens; ///< codelengths
88 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
90 if(length > huff->max_bits) {
91 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n", huff->max_bits);
95 if(!get_bits1(&ctx->gb)) { /* literal */
99 if(huff->num >= huff->max_num) {
100 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
103 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
104 huff->bits[huff->num] = prefix;
105 huff->lens[huff->num] = length;
108 } else { /* non-terminal node */
109 if(tm2_read_tree(ctx, prefix << 1, length + 1, huff) == -1)
111 if(tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff) == -1)
117 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
122 huff.val_bits = get_bits(&ctx->gb, 5);
123 huff.max_bits = get_bits(&ctx->gb, 5);
124 huff.min_bits = get_bits(&ctx->gb, 5);
125 huff.nodes = get_bits_long(&ctx->gb, 17);
128 /* check for correct codes parameters */
129 if((huff.val_bits < 1) || (huff.val_bits > 32) ||
130 (huff.max_bits < 0) || (huff.max_bits > 32)) {
131 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n",
132 huff.val_bits, huff.max_bits);
135 if((huff.nodes < 0) || (huff.nodes > 0x10000)) {
136 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes);
140 if(huff.max_bits == 0)
143 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
144 huff.max_num = (huff.nodes + 1) >> 1;
145 huff.nums = av_mallocz(huff.max_num * sizeof(int));
146 huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
147 huff.lens = av_mallocz(huff.max_num * sizeof(int));
149 if(tm2_read_tree(ctx, 0, 0, &huff) == -1)
152 if(huff.num != huff.max_num) {
153 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
154 huff.num, huff.max_num);
158 /* convert codes to vlc_table */
162 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
163 huff.lens, sizeof(int), sizeof(int),
164 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
166 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
171 code->bits = huff.max_bits;
172 code->length = huff.max_num;
173 code->recode = av_malloc(code->length * sizeof(int));
174 for(i = 0; i < code->length; i++)
175 code->recode[i] = huff.nums[i];
178 /* free allocated memory */
186 static void tm2_free_codes(TM2Codes *code)
189 av_free(code->recode);
191 free_vlc(&code->vlc);
194 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
197 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
198 return code->recode[val];
201 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
209 magic = AV_RL32(buf);
212 if(magic == 0x00000100) { /* old header */
213 /* av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */
215 } else if(magic == 0x00000101) { /* new header */
216 int w, h, size, flags, xr, yr;
218 length = AV_RL32(buf);
221 init_get_bits(&ctx->gb, buf, 32 * 8);
222 size = get_bits_long(&ctx->gb, 31);
223 h = get_bits(&ctx->gb, 15);
224 w = get_bits(&ctx->gb, 15);
225 flags = get_bits_long(&ctx->gb, 31);
226 yr = get_bits(&ctx->gb, 9);
227 xr = get_bits(&ctx->gb, 9);
231 av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
238 static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
242 d = get_bits(&ctx->gb, 9);
243 mb = get_bits(&ctx->gb, 5);
245 if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
246 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
250 for(i = 0; i < d; i++) {
251 v = get_bits_long(&ctx->gb, mb);
252 if(v & (1 << (mb - 1)))
253 ctx->deltas[stream_id][i] = v - (1 << mb);
255 ctx->deltas[stream_id][i] = v;
257 for(; i < TM2_DELTAS; i++)
258 ctx->deltas[stream_id][i] = 0;
263 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
271 /* get stream length in dwords */
272 len = AV_RB32(buf); buf += 4; cur += 4;
278 if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
279 av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
283 toks = AV_RB32(buf); buf += 4; cur += 4;
285 len = AV_RB32(buf); buf += 4; cur += 4;
286 if(len == TM2_ESCAPE) {
287 len = AV_RB32(buf); buf += 4; cur += 4;
290 init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
291 if(tm2_read_deltas(ctx, stream_id) == -1)
293 buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
294 cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
297 /* skip unused fields */
298 if(AV_RB32(buf) == TM2_ESCAPE) {
299 buf += 4; cur += 4; /* some unknown length - could be escaped too */
302 buf += 4; cur += 4; /* unused by decoder */
304 init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
305 if(tm2_build_huff_table(ctx, &codes) == -1)
307 buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
308 cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
311 /* check if we have sane number of tokens */
312 if((toks < 0) || (toks > 0xFFFFFF)){
313 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
314 tm2_free_codes(&codes);
317 ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
318 ctx->tok_lens[stream_id] = toks;
319 len = AV_RB32(buf); buf += 4; cur += 4;
321 init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
322 for(i = 0; i < toks; i++) {
323 if (get_bits_left(&ctx->gb) <= 0) {
324 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
327 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
330 for(i = 0; i < toks; i++)
331 ctx->tokens[stream_id][i] = codes.recode[0];
333 tm2_free_codes(&codes);
338 static inline int GET_TOK(TM2Context *ctx,int type) {
339 if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
340 av_log(ctx->avctx, AV_LOG_ERROR, "Read token from stream %i out of bounds (%i>=%i)\n", type, ctx->tok_ptrs[type], ctx->tok_lens[type]);
344 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
345 return ctx->tokens[type][ctx->tok_ptrs[type]++];
348 /* blocks decoding routines */
350 /* common Y, U, V pointers initialisation */
351 #define TM2_INIT_POINTERS() \
354 int Ystride, Ustride, Vstride;\
356 Ystride = ctx->avctx->width;\
357 Vstride = (ctx->avctx->width + 1) >> 1;\
358 Ustride = (ctx->avctx->width + 1) >> 1;\
359 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
360 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
361 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
362 last = ctx->last + bx * 4;\
363 clast = ctx->clast + bx * 4;
365 #define TM2_INIT_POINTERS_2() \
367 int oYstride, oUstride, oVstride;\
369 TM2_INIT_POINTERS();\
373 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
374 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
375 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
377 /* recalculate last and delta values for next blocks */
378 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
379 CD[0] = CHR[1] - last[1];\
380 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
381 last[0] = (int)CHR[stride + 0];\
382 last[1] = (int)CHR[stride + 1];}
384 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
385 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
390 for(j = 0; j < 4; j++){
392 for(i = 0; i < 4; i++){
393 d = deltas[i + j * 4];
396 Y[i] = av_clip_uint8(last[i]);
403 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
406 for(j = 0; j < 2; j++){
407 for(i = 0; i < 2; i++){
408 CD[j] += deltas[i + j * 2];
416 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
426 t = (CD[0] + CD[1]) >> 1;
427 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
428 CD[1] = CD[0] + CD[1] - t;
432 tm2_high_chroma(data, stride, clast, CD, deltas);
435 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
442 for(i = 0; i < 4; i++) {
443 deltas[i] = GET_TOK(ctx, TM2_C_HI);
444 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
446 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
447 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
450 for(i = 0; i < 16; i++)
451 deltas[i] = GET_TOK(ctx, TM2_L_HI);
453 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
456 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
463 deltas[0] = GET_TOK(ctx, TM2_C_LO);
464 deltas[1] = deltas[2] = deltas[3] = 0;
465 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
467 deltas[0] = GET_TOK(ctx, TM2_C_LO);
468 deltas[1] = deltas[2] = deltas[3] = 0;
469 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
472 for(i = 0; i < 16; i++)
473 deltas[i] = GET_TOK(ctx, TM2_L_HI);
475 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
478 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
486 deltas[0] = GET_TOK(ctx, TM2_C_LO);
487 deltas[1] = deltas[2] = deltas[3] = 0;
488 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
490 deltas[0] = GET_TOK(ctx, TM2_C_LO);
491 deltas[1] = deltas[2] = deltas[3] = 0;
492 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
495 for(i = 0; i < 16; i++)
498 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
499 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
500 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
501 deltas[10] = GET_TOK(ctx, TM2_L_LO);
504 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
506 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
507 last[2] = (last[1] + last[3]) >> 1;
509 t1 = ctx->D[0] + ctx->D[1];
511 ctx->D[1] = t1 - (t1 >> 1);
512 t2 = ctx->D[2] + ctx->D[3];
514 ctx->D[3] = t2 - (t2 >> 1);
516 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
519 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
523 int left, right, diff;
528 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
529 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
531 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
532 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
535 for(i = 0; i < 16; i++)
538 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
541 left = last[-1] - ct;
547 last[0] = left + (diff >> 2);
548 last[1] = left + (diff >> 1);
549 last[2] = right - (diff >> 2);
554 ctx->D[0] = (tp + (ct >> 2)) - left;
556 ctx->D[1] = (tp + (ct >> 1)) - left;
558 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
560 ctx->D[3] = (tp + ct) - left;
562 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
565 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
568 TM2_INIT_POINTERS_2();
571 for(j = 0; j < 2; j++){
572 for(i = 0; i < 2; i++){
576 U += Ustride; V += Vstride;
577 Uo += oUstride; Vo += oVstride;
581 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
582 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
585 ctx->D[0] = Yo[3] - last[3];
586 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
587 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
588 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
590 for(j = 0; j < 4; j++){
591 for(i = 0; i < 4; i++){
600 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
604 TM2_INIT_POINTERS_2();
607 for(j = 0; j < 2; j++){
608 for(i = 0; i < 2; i++){
609 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
610 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
612 U += Ustride; V += Vstride;
613 Uo += oUstride; Vo += oVstride;
617 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
618 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
621 ctx->D[0] = Yo[3] - last[3];
622 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
623 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
624 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
626 for(j = 0; j < 4; j++){
628 for(i = 0; i < 4; i++){
629 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
632 ctx->D[j] = last[3] - d;
638 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
642 TM2_INIT_POINTERS_2();
644 mx = GET_TOK(ctx, TM2_MOT);
645 my = GET_TOK(ctx, TM2_MOT);
647 Yo += my * oYstride + mx;
648 Uo += (my >> 1) * oUstride + (mx >> 1);
649 Vo += (my >> 1) * oVstride + (mx >> 1);
652 for(j = 0; j < 2; j++){
653 for(i = 0; i < 2; i++){
657 U += Ustride; V += Vstride;
658 Uo += oUstride; Vo += oVstride;
662 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
663 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
666 for(j = 0; j < 4; j++){
667 for(i = 0; i < 4; i++){
673 /* calculate deltas */
675 ctx->D[0] = Y[3] - last[3];
676 ctx->D[1] = Y[3 + Ystride] - Y[3];
677 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
678 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
679 for(i = 0; i < 4; i++)
680 last[i] = Y[i + Ystride * 3];
683 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
692 bw = ctx->avctx->width >> 2;
693 bh = ctx->avctx->height >> 2;
695 for(i = 0; i < TM2_NUM_STREAMS; i++)
696 ctx->tok_ptrs[i] = 0;
698 if (ctx->tok_lens[TM2_TYPE]<bw*bh){
699 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
703 memset(ctx->last, 0, 4 * bw * sizeof(int));
704 memset(ctx->clast, 0, 4 * bw * sizeof(int));
706 for(j = 0; j < bh; j++) {
707 memset(ctx->D, 0, 4 * sizeof(int));
708 memset(ctx->CD, 0, 4 * sizeof(int));
709 for(i = 0; i < bw; i++) {
710 type = GET_TOK(ctx, TM2_TYPE);
713 tm2_hi_res_block(ctx, p, i, j);
716 tm2_med_res_block(ctx, p, i, j);
719 tm2_low_res_block(ctx, p, i, j);
722 tm2_null_res_block(ctx, p, i, j);
725 tm2_update_block(ctx, p, i, j);
729 tm2_still_block(ctx, p, i, j);
733 tm2_motion_block(ctx, p, i, j);
737 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
742 /* copy data from our buffer to AVFrame */
743 Y = (ctx->cur?ctx->Y2:ctx->Y1);
744 U = (ctx->cur?ctx->U2:ctx->U1);
745 V = (ctx->cur?ctx->V2:ctx->V1);
747 for(j = 0; j < ctx->avctx->height; j++){
748 for(i = 0; i < ctx->avctx->width; i++){
749 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
750 dst[3*i+0] = av_clip_uint8(y + v);
751 dst[3*i+1] = av_clip_uint8(y);
752 dst[3*i+2] = av_clip_uint8(y + u);
754 Y += ctx->avctx->width;
756 U += ctx->avctx->width >> 1;
757 V += ctx->avctx->width >> 1;
759 dst += p->linesize[0];
765 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
766 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
769 static int decode_frame(AVCodecContext *avctx,
770 void *data, int *data_size,
773 const uint8_t *buf = avpkt->data;
774 int buf_size = avpkt->size;
775 TM2Context * const l = avctx->priv_data;
776 AVFrame * const p= (AVFrame*)&l->pic;
780 swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
782 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
786 p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
787 if(avctx->reget_buffer(avctx, p) < 0){
788 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
793 l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
794 skip = tm2_read_header(l, swbuf);
801 for(i = 0; i < TM2_NUM_STREAMS; i++){
802 t = tm2_read_stream(l, swbuf + skip, tm2_stream_order[i], buf_size);
809 p->key_frame = tm2_decode_blocks(l, p);
811 p->pict_type = FF_I_TYPE;
813 p->pict_type = FF_P_TYPE;
816 *data_size = sizeof(AVFrame);
817 *(AVFrame*)data = l->pic;
823 static av_cold int decode_init(AVCodecContext *avctx){
824 TM2Context * const l = avctx->priv_data;
827 if((avctx->width & 3) || (avctx->height & 3)){
828 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
834 avctx->pix_fmt = PIX_FMT_BGR24;
836 dsputil_init(&l->dsp, avctx);
838 l->last = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
839 l->clast = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
841 for(i = 0; i < TM2_NUM_STREAMS; i++) {
846 l->Y1 = av_malloc(sizeof(int) * avctx->width * avctx->height);
847 l->U1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
848 l->V1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
849 l->Y2 = av_malloc(sizeof(int) * avctx->width * avctx->height);
850 l->U2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
851 l->V2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
857 static av_cold int decode_end(AVCodecContext *avctx){
858 TM2Context * const l = avctx->priv_data;
859 AVFrame *pic = &l->pic;
866 for(i = 0; i < TM2_NUM_STREAMS; i++)
868 av_free(l->tokens[i]);
879 avctx->release_buffer(avctx, pic);
884 AVCodec truemotion2_decoder = {
887 CODEC_ID_TRUEMOTION2,
894 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),