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
23 * @file libavcodec/truemotion2.c
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
) {
270 /* get stream length in dwords */
271 len
= AV_RB32(buf
); buf
+= 4; cur
+= 4;
277 toks
= AV_RB32(buf
); buf
+= 4; cur
+= 4;
279 len
= AV_RB32(buf
); buf
+= 4; cur
+= 4;
280 if(len
== TM2_ESCAPE
) {
281 len
= AV_RB32(buf
); buf
+= 4; cur
+= 4;
284 init_get_bits(&ctx
->gb
, buf
, (skip
- cur
) * 8);
285 if(tm2_read_deltas(ctx
, stream_id
) == -1)
287 buf
+= ((get_bits_count(&ctx
->gb
) + 31) >> 5) << 2;
288 cur
+= ((get_bits_count(&ctx
->gb
) + 31) >> 5) << 2;
291 /* skip unused fields */
292 if(AV_RB32(buf
) == TM2_ESCAPE
) {
293 buf
+= 4; cur
+= 4; /* some unknown length - could be escaped too */
296 buf
+= 4; cur
+= 4; /* unused by decoder */
298 init_get_bits(&ctx
->gb
, buf
, (skip
- cur
) * 8);
299 if(tm2_build_huff_table(ctx
, &codes
) == -1)
301 buf
+= ((get_bits_count(&ctx
->gb
) + 31) >> 5) << 2;
302 cur
+= ((get_bits_count(&ctx
->gb
) + 31) >> 5) << 2;
305 /* check if we have sane number of tokens */
306 if((toks
< 0) || (toks
> 0xFFFFFF)){
307 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect number of tokens: %i\n", toks
);
308 tm2_free_codes(&codes
);
311 ctx
->tokens
[stream_id
] = av_realloc(ctx
->tokens
[stream_id
], toks
* sizeof(int));
312 ctx
->tok_lens
[stream_id
] = toks
;
313 len
= AV_RB32(buf
); buf
+= 4; cur
+= 4;
315 init_get_bits(&ctx
->gb
, buf
, (skip
- cur
) * 8);
316 for(i
= 0; i
< toks
; i
++)
317 ctx
->tokens
[stream_id
][i
] = tm2_get_token(&ctx
->gb
, &codes
);
319 for(i
= 0; i
< toks
; i
++)
320 ctx
->tokens
[stream_id
][i
] = codes
.recode
[0];
322 tm2_free_codes(&codes
);
327 static inline int GET_TOK(TM2Context
*ctx
,int type
) {
328 if(ctx
->tok_ptrs
[type
] >= ctx
->tok_lens
[type
]) {
329 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
]);
333 return ctx
->deltas
[type
][ctx
->tokens
[type
][ctx
->tok_ptrs
[type
]++]];
334 return ctx
->tokens
[type
][ctx
->tok_ptrs
[type
]++];
337 /* blocks decoding routines */
339 /* common Y, U, V pointers initialisation */
340 #define TM2_INIT_POINTERS() \
343 int Ystride, Ustride, Vstride;\
345 Ystride = ctx->avctx->width;\
346 Vstride = (ctx->avctx->width + 1) >> 1;\
347 Ustride = (ctx->avctx->width + 1) >> 1;\
348 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
349 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
350 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
351 last = ctx->last + bx * 4;\
352 clast = ctx->clast + bx * 4;
354 #define TM2_INIT_POINTERS_2() \
356 int oYstride, oUstride, oVstride;\
358 TM2_INIT_POINTERS();\
362 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
363 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
364 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
366 /* recalculate last and delta values for next blocks */
367 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
368 CD[0] = CHR[1] - last[1];\
369 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
370 last[0] = (int)CHR[stride + 0];\
371 last[1] = (int)CHR[stride + 1];}
373 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
374 static inline void tm2_apply_deltas(TM2Context
*ctx
, int* Y
, int stride
, int *deltas
, int *last
)
379 for(j
= 0; j
< 4; j
++){
381 for(i
= 0; i
< 4; i
++){
382 d
= deltas
[i
+ j
* 4];
385 Y
[i
] = av_clip_uint8(last
[i
]);
392 static inline void tm2_high_chroma(int *data
, int stride
, int *last
, int *CD
, int *deltas
)
395 for(j
= 0; j
< 2; j
++){
396 for(i
= 0; i
< 2; i
++){
397 CD
[j
] += deltas
[i
+ j
* 2];
405 static inline void tm2_low_chroma(int *data
, int stride
, int *clast
, int *CD
, int *deltas
, int bx
)
415 t
= (CD
[0] + CD
[1]) >> 1;
416 l
= (prev
- CD
[0] - CD
[1] + clast
[1]) >> 1;
417 CD
[1] = CD
[0] + CD
[1] - t
;
421 tm2_high_chroma(data
, stride
, clast
, CD
, deltas
);
424 static inline void tm2_hi_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
431 for(i
= 0; i
< 4; i
++) {
432 deltas
[i
] = GET_TOK(ctx
, TM2_C_HI
);
433 deltas
[i
+ 4] = GET_TOK(ctx
, TM2_C_HI
);
435 tm2_high_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
);
436 tm2_high_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
+ 4);
439 for(i
= 0; i
< 16; i
++)
440 deltas
[i
] = GET_TOK(ctx
, TM2_L_HI
);
442 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
445 static inline void tm2_med_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
452 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
453 deltas
[1] = deltas
[2] = deltas
[3] = 0;
454 tm2_low_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
, bx
);
456 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
457 deltas
[1] = deltas
[2] = deltas
[3] = 0;
458 tm2_low_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
, bx
);
461 for(i
= 0; i
< 16; i
++)
462 deltas
[i
] = GET_TOK(ctx
, TM2_L_HI
);
464 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
467 static inline void tm2_low_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
475 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
476 deltas
[1] = deltas
[2] = deltas
[3] = 0;
477 tm2_low_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
, bx
);
479 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
480 deltas
[1] = deltas
[2] = deltas
[3] = 0;
481 tm2_low_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
, bx
);
484 for(i
= 0; i
< 16; i
++)
487 deltas
[ 0] = GET_TOK(ctx
, TM2_L_LO
);
488 deltas
[ 2] = GET_TOK(ctx
, TM2_L_LO
);
489 deltas
[ 8] = GET_TOK(ctx
, TM2_L_LO
);
490 deltas
[10] = GET_TOK(ctx
, TM2_L_LO
);
493 last
[0] = (last
[-1] - ctx
->D
[0] - ctx
->D
[1] - ctx
->D
[2] - ctx
->D
[3] + last
[1]) >> 1;
495 last
[0] = (last
[1] - ctx
->D
[0] - ctx
->D
[1] - ctx
->D
[2] - ctx
->D
[3])>> 1;
496 last
[2] = (last
[1] + last
[3]) >> 1;
498 t1
= ctx
->D
[0] + ctx
->D
[1];
500 ctx
->D
[1] = t1
- (t1
>> 1);
501 t2
= ctx
->D
[2] + ctx
->D
[3];
503 ctx
->D
[3] = t2
- (t2
>> 1);
505 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
508 static inline void tm2_null_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
512 int left
, right
, diff
;
517 deltas
[0] = deltas
[1] = deltas
[2] = deltas
[3] = 0;
518 tm2_low_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
, bx
);
520 deltas
[0] = deltas
[1] = deltas
[2] = deltas
[3] = 0;
521 tm2_low_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
, bx
);
524 for(i
= 0; i
< 16; i
++)
527 ct
= ctx
->D
[0] + ctx
->D
[1] + ctx
->D
[2] + ctx
->D
[3];
530 left
= last
[-1] - ct
;
536 last
[0] = left
+ (diff
>> 2);
537 last
[1] = left
+ (diff
>> 1);
538 last
[2] = right
- (diff
>> 2);
543 ctx
->D
[0] = (tp
+ (ct
>> 2)) - left
;
545 ctx
->D
[1] = (tp
+ (ct
>> 1)) - left
;
547 ctx
->D
[2] = ((tp
+ ct
) - (ct
>> 2)) - left
;
549 ctx
->D
[3] = (tp
+ ct
) - left
;
551 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
554 static inline void tm2_still_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
557 TM2_INIT_POINTERS_2();
560 for(j
= 0; j
< 2; j
++){
561 for(i
= 0; i
< 2; i
++){
565 U
+= Ustride
; V
+= Vstride
;
566 Uo
+= oUstride
; Vo
+= oVstride
;
570 TM2_RECALC_BLOCK(U
, Ustride
, clast
, ctx
->CD
);
571 TM2_RECALC_BLOCK(V
, Vstride
, (clast
+ 2), (ctx
->CD
+ 2));
574 ctx
->D
[0] = Yo
[3] - last
[3];
575 ctx
->D
[1] = Yo
[3 + oYstride
] - Yo
[3];
576 ctx
->D
[2] = Yo
[3 + oYstride
* 2] - Yo
[3 + oYstride
];
577 ctx
->D
[3] = Yo
[3 + oYstride
* 3] - Yo
[3 + oYstride
* 2];
579 for(j
= 0; j
< 4; j
++){
580 for(i
= 0; i
< 4; i
++){
589 static inline void tm2_update_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
593 TM2_INIT_POINTERS_2();
596 for(j
= 0; j
< 2; j
++){
597 for(i
= 0; i
< 2; i
++){
598 U
[i
] = Uo
[i
] + GET_TOK(ctx
, TM2_UPD
);
599 V
[i
] = Vo
[i
] + GET_TOK(ctx
, TM2_UPD
);
601 U
+= Ustride
; V
+= Vstride
;
602 Uo
+= oUstride
; Vo
+= oVstride
;
606 TM2_RECALC_BLOCK(U
, Ustride
, clast
, ctx
->CD
);
607 TM2_RECALC_BLOCK(V
, Vstride
, (clast
+ 2), (ctx
->CD
+ 2));
610 ctx
->D
[0] = Yo
[3] - last
[3];
611 ctx
->D
[1] = Yo
[3 + oYstride
] - Yo
[3];
612 ctx
->D
[2] = Yo
[3 + oYstride
* 2] - Yo
[3 + oYstride
];
613 ctx
->D
[3] = Yo
[3 + oYstride
* 3] - Yo
[3 + oYstride
* 2];
615 for(j
= 0; j
< 4; j
++){
617 for(i
= 0; i
< 4; i
++){
618 Y
[i
] = Yo
[i
] + GET_TOK(ctx
, TM2_UPD
);
621 ctx
->D
[j
] = last
[3] - d
;
627 static inline void tm2_motion_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
631 TM2_INIT_POINTERS_2();
633 mx
= GET_TOK(ctx
, TM2_MOT
);
634 my
= GET_TOK(ctx
, TM2_MOT
);
636 Yo
+= my
* oYstride
+ mx
;
637 Uo
+= (my
>> 1) * oUstride
+ (mx
>> 1);
638 Vo
+= (my
>> 1) * oVstride
+ (mx
>> 1);
641 for(j
= 0; j
< 2; j
++){
642 for(i
= 0; i
< 2; i
++){
646 U
+= Ustride
; V
+= Vstride
;
647 Uo
+= oUstride
; Vo
+= oVstride
;
651 TM2_RECALC_BLOCK(U
, Ustride
, clast
, ctx
->CD
);
652 TM2_RECALC_BLOCK(V
, Vstride
, (clast
+ 2), (ctx
->CD
+ 2));
655 for(j
= 0; j
< 4; j
++){
656 for(i
= 0; i
< 4; i
++){
662 /* calculate deltas */
664 ctx
->D
[0] = Y
[3] - last
[3];
665 ctx
->D
[1] = Y
[3 + Ystride
] - Y
[3];
666 ctx
->D
[2] = Y
[3 + Ystride
* 2] - Y
[3 + Ystride
];
667 ctx
->D
[3] = Y
[3 + Ystride
* 3] - Y
[3 + Ystride
* 2];
668 for(i
= 0; i
< 4; i
++)
669 last
[i
] = Y
[i
+ Ystride
* 3];
672 static int tm2_decode_blocks(TM2Context
*ctx
, AVFrame
*p
)
681 bw
= ctx
->avctx
->width
>> 2;
682 bh
= ctx
->avctx
->height
>> 2;
684 for(i
= 0; i
< TM2_NUM_STREAMS
; i
++)
685 ctx
->tok_ptrs
[i
] = 0;
687 if (ctx
->tok_lens
[TM2_TYPE
]<bw
*bh
){
688 av_log(ctx
->avctx
,AV_LOG_ERROR
,"Got %i tokens for %i blocks\n",ctx
->tok_lens
[TM2_TYPE
],bw
*bh
);
692 memset(ctx
->last
, 0, 4 * bw
* sizeof(int));
693 memset(ctx
->clast
, 0, 4 * bw
* sizeof(int));
695 for(j
= 0; j
< bh
; j
++) {
696 memset(ctx
->D
, 0, 4 * sizeof(int));
697 memset(ctx
->CD
, 0, 4 * sizeof(int));
698 for(i
= 0; i
< bw
; i
++) {
699 type
= GET_TOK(ctx
, TM2_TYPE
);
702 tm2_hi_res_block(ctx
, p
, i
, j
);
705 tm2_med_res_block(ctx
, p
, i
, j
);
708 tm2_low_res_block(ctx
, p
, i
, j
);
711 tm2_null_res_block(ctx
, p
, i
, j
);
714 tm2_update_block(ctx
, p
, i
, j
);
718 tm2_still_block(ctx
, p
, i
, j
);
722 tm2_motion_block(ctx
, p
, i
, j
);
726 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Skipping unknown block type %i\n", type
);
731 /* copy data from our buffer to AVFrame */
732 Y
= (ctx
->cur
?ctx
->Y2
:ctx
->Y1
);
733 U
= (ctx
->cur
?ctx
->U2
:ctx
->U1
);
734 V
= (ctx
->cur
?ctx
->V2
:ctx
->V1
);
736 for(j
= 0; j
< ctx
->avctx
->height
; j
++){
737 for(i
= 0; i
< ctx
->avctx
->width
; i
++){
738 int y
= Y
[i
], u
= U
[i
>> 1], v
= V
[i
>> 1];
739 dst
[3*i
+0] = av_clip_uint8(y
+ v
);
740 dst
[3*i
+1] = av_clip_uint8(y
);
741 dst
[3*i
+2] = av_clip_uint8(y
+ u
);
743 Y
+= ctx
->avctx
->width
;
745 U
+= ctx
->avctx
->width
>> 1;
746 V
+= ctx
->avctx
->width
>> 1;
748 dst
+= p
->linesize
[0];
754 static const int tm2_stream_order
[TM2_NUM_STREAMS
] = {
755 TM2_C_HI
, TM2_C_LO
, TM2_L_HI
, TM2_L_LO
, TM2_UPD
, TM2_MOT
, TM2_TYPE
758 static int decode_frame(AVCodecContext
*avctx
,
759 void *data
, int *data_size
,
762 const uint8_t *buf
= avpkt
->data
;
763 int buf_size
= avpkt
->size
;
764 TM2Context
* const l
= avctx
->priv_data
;
765 AVFrame
* const p
= (AVFrame
*)&l
->pic
;
769 swbuf
= av_malloc(buf_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
771 av_log(avctx
, AV_LOG_ERROR
, "Cannot allocate temporary buffer\n");
775 p
->buffer_hints
= FF_BUFFER_HINTS_VALID
| FF_BUFFER_HINTS_PRESERVE
| FF_BUFFER_HINTS_REUSABLE
;
776 if(avctx
->reget_buffer(avctx
, p
) < 0){
777 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
782 l
->dsp
.bswap_buf((uint32_t*)swbuf
, (const uint32_t*)buf
, buf_size
>> 2);
783 skip
= tm2_read_header(l
, swbuf
);
790 for(i
= 0; i
< TM2_NUM_STREAMS
; i
++){
791 t
= tm2_read_stream(l
, swbuf
+ skip
, tm2_stream_order
[i
]);
798 p
->key_frame
= tm2_decode_blocks(l
, p
);
800 p
->pict_type
= FF_I_TYPE
;
802 p
->pict_type
= FF_P_TYPE
;
805 *data_size
= sizeof(AVFrame
);
806 *(AVFrame
*)data
= l
->pic
;
812 static av_cold
int decode_init(AVCodecContext
*avctx
){
813 TM2Context
* const l
= avctx
->priv_data
;
816 if (avcodec_check_dimensions(avctx
, avctx
->width
, avctx
->height
) < 0) {
819 if((avctx
->width
& 3) || (avctx
->height
& 3)){
820 av_log(avctx
, AV_LOG_ERROR
, "Width and height must be multiple of 4\n");
826 avctx
->pix_fmt
= PIX_FMT_BGR24
;
828 dsputil_init(&l
->dsp
, avctx
);
830 l
->last
= av_malloc(4 * sizeof(int) * (avctx
->width
>> 2));
831 l
->clast
= av_malloc(4 * sizeof(int) * (avctx
->width
>> 2));
833 for(i
= 0; i
< TM2_NUM_STREAMS
; i
++) {
838 l
->Y1
= av_malloc(sizeof(int) * avctx
->width
* avctx
->height
);
839 l
->U1
= av_malloc(sizeof(int) * ((avctx
->width
+ 1) >> 1) * ((avctx
->height
+ 1) >> 1));
840 l
->V1
= av_malloc(sizeof(int) * ((avctx
->width
+ 1) >> 1) * ((avctx
->height
+ 1) >> 1));
841 l
->Y2
= av_malloc(sizeof(int) * avctx
->width
* avctx
->height
);
842 l
->U2
= av_malloc(sizeof(int) * ((avctx
->width
+ 1) >> 1) * ((avctx
->height
+ 1) >> 1));
843 l
->V2
= av_malloc(sizeof(int) * ((avctx
->width
+ 1) >> 1) * ((avctx
->height
+ 1) >> 1));
849 static av_cold
int decode_end(AVCodecContext
*avctx
){
850 TM2Context
* const l
= avctx
->priv_data
;
857 for(i
= 0; i
< TM2_NUM_STREAMS
; i
++)
859 av_free(l
->tokens
[i
]);
871 AVCodec truemotion2_decoder
= {
874 CODEC_ID_TRUEMOTION2
,
881 .long_name
= NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),