2 * Duck/ON2 TrueMotion 2 Decoder
3 * Copyright (c) 2005 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Duck TrueMotion2 decoder.
28 #include "bytestream.h"
32 #define TM2_ESCAPE 0x80000000
34 /* Huffman-coded streams of different types of blocks */
35 enum TM2_STREAMS
{ TM2_C_HI
= 0, TM2_C_LO
, TM2_L_HI
, TM2_L_LO
,
36 TM2_UPD
, TM2_MOT
, TM2_TYPE
, TM2_NUM_STREAMS
};
38 enum TM2_BLOCKS
{ TM2_HI_RES
= 0, TM2_MED_RES
, TM2_LOW_RES
, TM2_NULL_RES
,
39 TM2_UPDATE
, TM2_STILL
, TM2_MOTION
};
41 typedef struct TM2Context
{
42 AVCodecContext
*avctx
;
49 int *tokens
[TM2_NUM_STREAMS
];
50 int tok_lens
[TM2_NUM_STREAMS
];
51 int tok_ptrs
[TM2_NUM_STREAMS
];
52 int deltas
[TM2_NUM_STREAMS
][TM2_DELTAS
];
53 /* for blocks decoding */
59 /* data for current and previous frame */
60 int *Y1_base
, *U1_base
, *V1_base
, *Y2_base
, *U2_base
, *V2_base
;
61 int *Y1
, *U1
, *V1
, *Y2
, *U2
, *V2
;
62 int y_stride
, uv_stride
;
67 * Huffman codes for each of streams
69 typedef struct TM2Codes
{
70 VLC vlc
; ///< table for Libav bitstream reader
72 int *recode
; ///< table for converting from code indexes to values
77 * structure for gathering Huffman codes information
79 typedef struct TM2Huff
{
80 int val_bits
; ///< length of literal
81 int max_bits
; ///< maximum length of code
82 int min_bits
; ///< minimum length of code
83 int nodes
; ///< total number of nodes in tree
84 int num
; ///< current number filled
85 int max_num
; ///< total number of codes
86 int *nums
; ///< literals
87 uint32_t *bits
; ///< codes
88 int *lens
; ///< codelengths
91 static int tm2_read_tree(TM2Context
*ctx
, uint32_t prefix
, int length
, TM2Huff
*huff
)
93 if(length
> huff
->max_bits
) {
94 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Tree exceeded its given depth (%i)\n", huff
->max_bits
);
98 if(!get_bits1(&ctx
->gb
)) { /* literal */
102 if(huff
->num
>= huff
->max_num
) {
103 av_log(ctx
->avctx
, AV_LOG_DEBUG
, "Too many literals\n");
106 huff
->nums
[huff
->num
] = get_bits_long(&ctx
->gb
, huff
->val_bits
);
107 huff
->bits
[huff
->num
] = prefix
;
108 huff
->lens
[huff
->num
] = length
;
111 } else { /* non-terminal node */
112 if(tm2_read_tree(ctx
, prefix
<< 1, length
+ 1, huff
) == -1)
114 if(tm2_read_tree(ctx
, (prefix
<< 1) | 1, length
+ 1, huff
) == -1)
120 static int tm2_build_huff_table(TM2Context
*ctx
, TM2Codes
*code
)
125 huff
.val_bits
= get_bits(&ctx
->gb
, 5);
126 huff
.max_bits
= get_bits(&ctx
->gb
, 5);
127 huff
.min_bits
= get_bits(&ctx
->gb
, 5);
128 huff
.nodes
= get_bits_long(&ctx
->gb
, 17);
131 /* check for correct codes parameters */
132 if((huff
.val_bits
< 1) || (huff
.val_bits
> 32) ||
133 (huff
.max_bits
< 0) || (huff
.max_bits
> 25)) {
134 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect tree parameters - literal length: %i, max code length: %i\n",
135 huff
.val_bits
, huff
.max_bits
);
138 if((huff
.nodes
<= 0) || (huff
.nodes
> 0x10000)) {
139 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect number of Huffman tree nodes: %i\n", huff
.nodes
);
143 if(huff
.max_bits
== 0)
146 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
147 huff
.max_num
= (huff
.nodes
+ 1) >> 1;
148 huff
.nums
= av_mallocz(huff
.max_num
* sizeof(int));
149 huff
.bits
= av_mallocz(huff
.max_num
* sizeof(uint32_t));
150 huff
.lens
= av_mallocz(huff
.max_num
* sizeof(int));
152 if(tm2_read_tree(ctx
, 0, 0, &huff
) == -1)
155 if(huff
.num
!= huff
.max_num
) {
156 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Got less codes than expected: %i of %i\n",
157 huff
.num
, huff
.max_num
);
161 /* convert codes to vlc_table */
165 res
= init_vlc(&code
->vlc
, huff
.max_bits
, huff
.max_num
,
166 huff
.lens
, sizeof(int), sizeof(int),
167 huff
.bits
, sizeof(uint32_t), sizeof(uint32_t), 0);
169 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
174 code
->bits
= huff
.max_bits
;
175 code
->length
= huff
.max_num
;
176 code
->recode
= av_malloc(code
->length
* sizeof(int));
177 for(i
= 0; i
< code
->length
; i
++)
178 code
->recode
[i
] = huff
.nums
[i
];
181 /* free allocated memory */
189 static void tm2_free_codes(TM2Codes
*code
)
191 av_free(code
->recode
);
193 ff_free_vlc(&code
->vlc
);
196 static inline int tm2_get_token(GetBitContext
*gb
, TM2Codes
*code
)
199 val
= get_vlc2(gb
, code
->vlc
.table
, code
->bits
, 1);
200 return code
->recode
[val
];
203 #define TM2_OLD_HEADER_MAGIC 0x00000100
204 #define TM2_NEW_HEADER_MAGIC 0x00000101
206 static inline int tm2_read_header(TM2Context
*ctx
, const uint8_t *buf
)
208 uint32_t magic
= AV_RL32(buf
);
211 case TM2_OLD_HEADER_MAGIC
:
212 av_log_missing_feature(ctx
->avctx
, "TM2 old header", 1);
214 case TM2_NEW_HEADER_MAGIC
:
217 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Not a TM2 header: 0x%08X\n", magic
);
218 return AVERROR_INVALIDDATA
;
222 static int tm2_read_deltas(TM2Context
*ctx
, int stream_id
) {
226 d
= get_bits(&ctx
->gb
, 9);
227 mb
= get_bits(&ctx
->gb
, 5);
229 if((d
< 1) || (d
> TM2_DELTAS
) || (mb
< 1) || (mb
> 32)) {
230 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect delta table: %i deltas x %i bits\n", d
, mb
);
234 for(i
= 0; i
< d
; i
++) {
235 v
= get_bits_long(&ctx
->gb
, mb
);
236 if(v
& (1 << (mb
- 1)))
237 ctx
->deltas
[stream_id
][i
] = v
- (1 << mb
);
239 ctx
->deltas
[stream_id
][i
] = v
;
241 for(; i
< TM2_DELTAS
; i
++)
242 ctx
->deltas
[stream_id
][i
] = 0;
247 static int tm2_read_stream(TM2Context
*ctx
, const uint8_t *buf
, int stream_id
, int buf_size
)
255 /* get stream length in dwords */
256 bytestream2_init(&gb
, buf
, buf_size
);
257 len
= bytestream2_get_be32(&gb
);
263 if (len
>= INT_MAX
/4-1 || len
< 0 || len
> buf_size
) {
264 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Error, invalid stream size.\n");
268 toks
= bytestream2_get_be32(&gb
);
270 len
= bytestream2_get_be32(&gb
);
271 if(len
== TM2_ESCAPE
) {
272 len
= bytestream2_get_be32(&gb
);
275 pos
= bytestream2_tell(&gb
);
278 init_get_bits(&ctx
->gb
, buf
+ pos
, (skip
- pos
) * 8);
279 if(tm2_read_deltas(ctx
, stream_id
) == -1)
281 bytestream2_skip(&gb
, ((get_bits_count(&ctx
->gb
) + 31) >> 5) << 2);
284 /* skip unused fields */
285 len
= bytestream2_get_be32(&gb
);
286 if(len
== TM2_ESCAPE
) { /* some unknown length - could be escaped too */
287 bytestream2_skip(&gb
, 8); /* unused by decoder */
289 bytestream2_skip(&gb
, 4); /* unused by decoder */
292 pos
= bytestream2_tell(&gb
);
295 init_get_bits(&ctx
->gb
, buf
+ pos
, (skip
- pos
) * 8);
296 if(tm2_build_huff_table(ctx
, &codes
) == -1)
298 bytestream2_skip(&gb
, ((get_bits_count(&ctx
->gb
) + 31) >> 5) << 2);
301 /* check if we have sane number of tokens */
302 if((toks
< 0) || (toks
> 0xFFFFFF)){
303 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect number of tokens: %i\n", toks
);
304 tm2_free_codes(&codes
);
307 ctx
->tokens
[stream_id
] = av_realloc(ctx
->tokens
[stream_id
], toks
* sizeof(int));
308 ctx
->tok_lens
[stream_id
] = toks
;
309 len
= bytestream2_get_be32(&gb
);
311 pos
= bytestream2_tell(&gb
);
314 init_get_bits(&ctx
->gb
, buf
+ pos
, (skip
- pos
) * 8);
315 for(i
= 0; i
< toks
; i
++) {
316 if (get_bits_left(&ctx
->gb
) <= 0) {
317 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect number of tokens: %i\n", toks
);
320 ctx
->tokens
[stream_id
][i
] = tm2_get_token(&ctx
->gb
, &codes
);
321 if (stream_id
<= TM2_MOT
&& ctx
->tokens
[stream_id
][i
] >= TM2_DELTAS
) {
322 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Invalid delta token index %d for type %d, n=%d\n",
323 ctx
->tokens
[stream_id
][i
], stream_id
, i
);
324 return AVERROR_INVALIDDATA
;
328 for(i
= 0; i
< toks
; i
++) {
329 ctx
->tokens
[stream_id
][i
] = codes
.recode
[0];
330 if (stream_id
<= TM2_MOT
&& ctx
->tokens
[stream_id
][i
] >= TM2_DELTAS
) {
331 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Invalid delta token index %d for type %d, n=%d\n",
332 ctx
->tokens
[stream_id
][i
], stream_id
, i
);
333 return AVERROR_INVALIDDATA
;
337 tm2_free_codes(&codes
);
342 static inline int GET_TOK(TM2Context
*ctx
,int type
) {
343 if(ctx
->tok_ptrs
[type
] >= ctx
->tok_lens
[type
]) {
344 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
]);
348 return ctx
->deltas
[type
][ctx
->tokens
[type
][ctx
->tok_ptrs
[type
]++]];
349 return ctx
->tokens
[type
][ctx
->tok_ptrs
[type
]++];
352 /* blocks decoding routines */
354 /* common Y, U, V pointers initialisation */
355 #define TM2_INIT_POINTERS() \
358 int Ystride, Ustride, Vstride;\
360 Ystride = ctx->y_stride;\
361 Vstride = ctx->uv_stride;\
362 Ustride = ctx->uv_stride;\
363 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
364 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
365 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
366 last = ctx->last + bx * 4;\
367 clast = ctx->clast + bx * 4;
369 #define TM2_INIT_POINTERS_2() \
371 int oYstride, oUstride, oVstride;\
373 TM2_INIT_POINTERS();\
377 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
378 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
379 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
381 /* recalculate last and delta values for next blocks */
382 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
383 CD[0] = CHR[1] - last[1];\
384 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
385 last[0] = (int)CHR[stride + 0];\
386 last[1] = (int)CHR[stride + 1];}
388 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
389 static inline void tm2_apply_deltas(TM2Context
*ctx
, int* Y
, int stride
, int *deltas
, int *last
)
394 for(j
= 0; j
< 4; j
++){
396 for(i
= 0; i
< 4; i
++){
397 d
= deltas
[i
+ j
* 4];
400 Y
[i
] = av_clip_uint8(last
[i
]);
407 static inline void tm2_high_chroma(int *data
, int stride
, int *last
, int *CD
, int *deltas
)
410 for(j
= 0; j
< 2; j
++){
411 for(i
= 0; i
< 2; i
++){
412 CD
[j
] += deltas
[i
+ j
* 2];
420 static inline void tm2_low_chroma(int *data
, int stride
, int *clast
, int *CD
, int *deltas
, int bx
)
430 t
= (CD
[0] + CD
[1]) >> 1;
431 l
= (prev
- CD
[0] - CD
[1] + clast
[1]) >> 1;
432 CD
[1] = CD
[0] + CD
[1] - t
;
436 tm2_high_chroma(data
, stride
, clast
, CD
, deltas
);
439 static inline void tm2_hi_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
446 for(i
= 0; i
< 4; i
++) {
447 deltas
[i
] = GET_TOK(ctx
, TM2_C_HI
);
448 deltas
[i
+ 4] = GET_TOK(ctx
, TM2_C_HI
);
450 tm2_high_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
);
451 tm2_high_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
+ 4);
454 for(i
= 0; i
< 16; i
++)
455 deltas
[i
] = GET_TOK(ctx
, TM2_L_HI
);
457 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
460 static inline void tm2_med_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
467 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
468 deltas
[1] = deltas
[2] = deltas
[3] = 0;
469 tm2_low_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
, bx
);
471 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
472 deltas
[1] = deltas
[2] = deltas
[3] = 0;
473 tm2_low_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
, bx
);
476 for(i
= 0; i
< 16; i
++)
477 deltas
[i
] = GET_TOK(ctx
, TM2_L_HI
);
479 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
482 static inline void tm2_low_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
490 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
491 deltas
[1] = deltas
[2] = deltas
[3] = 0;
492 tm2_low_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
, bx
);
494 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
495 deltas
[1] = deltas
[2] = deltas
[3] = 0;
496 tm2_low_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
, bx
);
499 for(i
= 0; i
< 16; i
++)
502 deltas
[ 0] = GET_TOK(ctx
, TM2_L_LO
);
503 deltas
[ 2] = GET_TOK(ctx
, TM2_L_LO
);
504 deltas
[ 8] = GET_TOK(ctx
, TM2_L_LO
);
505 deltas
[10] = GET_TOK(ctx
, TM2_L_LO
);
508 last
[0] = (last
[-1] - ctx
->D
[0] - ctx
->D
[1] - ctx
->D
[2] - ctx
->D
[3] + last
[1]) >> 1;
510 last
[0] = (last
[1] - ctx
->D
[0] - ctx
->D
[1] - ctx
->D
[2] - ctx
->D
[3])>> 1;
511 last
[2] = (last
[1] + last
[3]) >> 1;
513 t1
= ctx
->D
[0] + ctx
->D
[1];
515 ctx
->D
[1] = t1
- (t1
>> 1);
516 t2
= ctx
->D
[2] + ctx
->D
[3];
518 ctx
->D
[3] = t2
- (t2
>> 1);
520 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
523 static inline void tm2_null_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
527 int left
, right
, diff
;
532 deltas
[0] = deltas
[1] = deltas
[2] = deltas
[3] = 0;
533 tm2_low_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
, bx
);
535 deltas
[0] = deltas
[1] = deltas
[2] = deltas
[3] = 0;
536 tm2_low_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
, bx
);
539 for(i
= 0; i
< 16; i
++)
542 ct
= ctx
->D
[0] + ctx
->D
[1] + ctx
->D
[2] + ctx
->D
[3];
545 left
= last
[-1] - ct
;
551 last
[0] = left
+ (diff
>> 2);
552 last
[1] = left
+ (diff
>> 1);
553 last
[2] = right
- (diff
>> 2);
558 ctx
->D
[0] = (tp
+ (ct
>> 2)) - left
;
560 ctx
->D
[1] = (tp
+ (ct
>> 1)) - left
;
562 ctx
->D
[2] = ((tp
+ ct
) - (ct
>> 2)) - left
;
564 ctx
->D
[3] = (tp
+ ct
) - left
;
566 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
569 static inline void tm2_still_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
572 TM2_INIT_POINTERS_2();
575 for(j
= 0; j
< 2; j
++){
576 for(i
= 0; i
< 2; i
++){
580 U
+= Ustride
; V
+= Vstride
;
581 Uo
+= oUstride
; Vo
+= oVstride
;
585 TM2_RECALC_BLOCK(U
, Ustride
, clast
, ctx
->CD
);
586 TM2_RECALC_BLOCK(V
, Vstride
, (clast
+ 2), (ctx
->CD
+ 2));
589 ctx
->D
[0] = Yo
[3] - last
[3];
590 ctx
->D
[1] = Yo
[3 + oYstride
] - Yo
[3];
591 ctx
->D
[2] = Yo
[3 + oYstride
* 2] - Yo
[3 + oYstride
];
592 ctx
->D
[3] = Yo
[3 + oYstride
* 3] - Yo
[3 + oYstride
* 2];
594 for(j
= 0; j
< 4; j
++){
595 for(i
= 0; i
< 4; i
++){
604 static inline void tm2_update_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
608 TM2_INIT_POINTERS_2();
611 for(j
= 0; j
< 2; j
++){
612 for(i
= 0; i
< 2; i
++){
613 U
[i
] = Uo
[i
] + GET_TOK(ctx
, TM2_UPD
);
614 V
[i
] = Vo
[i
] + GET_TOK(ctx
, TM2_UPD
);
616 U
+= Ustride
; V
+= Vstride
;
617 Uo
+= oUstride
; Vo
+= oVstride
;
621 TM2_RECALC_BLOCK(U
, Ustride
, clast
, ctx
->CD
);
622 TM2_RECALC_BLOCK(V
, Vstride
, (clast
+ 2), (ctx
->CD
+ 2));
625 ctx
->D
[0] = Yo
[3] - last
[3];
626 ctx
->D
[1] = Yo
[3 + oYstride
] - Yo
[3];
627 ctx
->D
[2] = Yo
[3 + oYstride
* 2] - Yo
[3 + oYstride
];
628 ctx
->D
[3] = Yo
[3 + oYstride
* 3] - Yo
[3 + oYstride
* 2];
630 for(j
= 0; j
< 4; j
++){
632 for(i
= 0; i
< 4; i
++){
633 Y
[i
] = Yo
[i
] + GET_TOK(ctx
, TM2_UPD
);
636 ctx
->D
[j
] = last
[3] - d
;
642 static inline void tm2_motion_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
646 TM2_INIT_POINTERS_2();
648 mx
= GET_TOK(ctx
, TM2_MOT
);
649 my
= GET_TOK(ctx
, TM2_MOT
);
650 mx
= av_clip(mx
, -(bx
* 4 + 4), ctx
->avctx
->width
- bx
* 4);
651 my
= av_clip(my
, -(by
* 4 + 4), ctx
->avctx
->height
- by
* 4);
653 Yo
+= my
* oYstride
+ mx
;
654 Uo
+= (my
>> 1) * oUstride
+ (mx
>> 1);
655 Vo
+= (my
>> 1) * oVstride
+ (mx
>> 1);
658 for(j
= 0; j
< 2; j
++){
659 for(i
= 0; i
< 2; i
++){
663 U
+= Ustride
; V
+= Vstride
;
664 Uo
+= oUstride
; Vo
+= oVstride
;
668 TM2_RECALC_BLOCK(U
, Ustride
, clast
, ctx
->CD
);
669 TM2_RECALC_BLOCK(V
, Vstride
, (clast
+ 2), (ctx
->CD
+ 2));
672 for(j
= 0; j
< 4; j
++){
673 for(i
= 0; i
< 4; i
++){
679 /* calculate deltas */
681 ctx
->D
[0] = Y
[3] - last
[3];
682 ctx
->D
[1] = Y
[3 + Ystride
] - Y
[3];
683 ctx
->D
[2] = Y
[3 + Ystride
* 2] - Y
[3 + Ystride
];
684 ctx
->D
[3] = Y
[3 + Ystride
* 3] - Y
[3 + Ystride
* 2];
685 for(i
= 0; i
< 4; i
++)
686 last
[i
] = Y
[i
+ Ystride
* 3];
689 static int tm2_decode_blocks(TM2Context
*ctx
, AVFrame
*p
)
692 int w
= ctx
->avctx
->width
, h
= ctx
->avctx
->height
, bw
= w
>> 2, bh
= h
>> 2, cw
= w
>> 1;
698 for(i
= 0; i
< TM2_NUM_STREAMS
; i
++)
699 ctx
->tok_ptrs
[i
] = 0;
701 if (ctx
->tok_lens
[TM2_TYPE
]<bw
*bh
){
702 av_log(ctx
->avctx
,AV_LOG_ERROR
,"Got %i tokens for %i blocks\n",ctx
->tok_lens
[TM2_TYPE
],bw
*bh
);
706 memset(ctx
->last
, 0, 4 * bw
* sizeof(int));
707 memset(ctx
->clast
, 0, 4 * bw
* sizeof(int));
709 for(j
= 0; j
< bh
; j
++) {
710 memset(ctx
->D
, 0, 4 * sizeof(int));
711 memset(ctx
->CD
, 0, 4 * sizeof(int));
712 for(i
= 0; i
< bw
; i
++) {
713 type
= GET_TOK(ctx
, TM2_TYPE
);
716 tm2_hi_res_block(ctx
, p
, i
, j
);
719 tm2_med_res_block(ctx
, p
, i
, j
);
722 tm2_low_res_block(ctx
, p
, i
, j
);
725 tm2_null_res_block(ctx
, p
, i
, j
);
728 tm2_update_block(ctx
, p
, i
, j
);
732 tm2_still_block(ctx
, p
, i
, j
);
736 tm2_motion_block(ctx
, p
, i
, j
);
740 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Skipping unknown block type %i\n", type
);
745 /* copy data from our buffer to AVFrame */
746 Y
= (ctx
->cur
?ctx
->Y2
:ctx
->Y1
);
747 U
= (ctx
->cur
?ctx
->U2
:ctx
->U1
);
748 V
= (ctx
->cur
?ctx
->V2
:ctx
->V1
);
750 for(j
= 0; j
< h
; j
++){
751 for(i
= 0; i
< w
; i
++){
752 int y
= Y
[i
], u
= U
[i
>> 1], v
= V
[i
>> 1];
753 dst
[3*i
+0] = av_clip_uint8(y
+ v
);
754 dst
[3*i
+1] = av_clip_uint8(y
);
755 dst
[3*i
+2] = av_clip_uint8(y
+ u
);
758 /* horizontal edge extension */
759 Y
[-4] = Y
[-3] = Y
[-2] = Y
[-1] = Y
[0];
760 Y
[w
+ 3] = Y
[w
+ 2] = Y
[w
+ 1] = Y
[w
] = Y
[w
- 1];
762 /* vertical edge extension */
764 memcpy(Y
- 4 - 1 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
765 memcpy(Y
- 4 - 2 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
766 memcpy(Y
- 4 - 3 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
767 memcpy(Y
- 4 - 4 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
768 } else if (j
== h
- 1) {
769 memcpy(Y
- 4 + 1 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
770 memcpy(Y
- 4 + 2 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
771 memcpy(Y
- 4 + 3 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
772 memcpy(Y
- 4 + 4 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
777 /* horizontal edge extension */
778 U
[-2] = U
[-1] = U
[0];
779 V
[-2] = V
[-1] = V
[0];
780 U
[cw
+ 1] = U
[cw
] = U
[cw
- 1];
781 V
[cw
+ 1] = V
[cw
] = V
[cw
- 1];
783 /* vertical edge extension */
785 memcpy(U
- 2 - 1 * ctx
->uv_stride
, U
- 2, ctx
->uv_stride
);
786 memcpy(V
- 2 - 1 * ctx
->uv_stride
, V
- 2, ctx
->uv_stride
);
787 memcpy(U
- 2 - 2 * ctx
->uv_stride
, U
- 2, ctx
->uv_stride
);
788 memcpy(V
- 2 - 2 * ctx
->uv_stride
, V
- 2, ctx
->uv_stride
);
789 } else if (j
== h
- 1) {
790 memcpy(U
- 2 + 1 * ctx
->uv_stride
, U
- 2, ctx
->uv_stride
);
791 memcpy(V
- 2 + 1 * ctx
->uv_stride
, V
- 2, ctx
->uv_stride
);
792 memcpy(U
- 2 + 2 * ctx
->uv_stride
, U
- 2, ctx
->uv_stride
);
793 memcpy(V
- 2 + 2 * ctx
->uv_stride
, V
- 2, ctx
->uv_stride
);
799 dst
+= p
->linesize
[0];
805 static const int tm2_stream_order
[TM2_NUM_STREAMS
] = {
806 TM2_C_HI
, TM2_C_LO
, TM2_L_HI
, TM2_L_LO
, TM2_UPD
, TM2_MOT
, TM2_TYPE
809 #define TM2_HEADER_SIZE 40
811 static int decode_frame(AVCodecContext
*avctx
,
812 void *data
, int *got_frame
,
815 const uint8_t *buf
= avpkt
->data
;
816 int buf_size
= avpkt
->size
& ~3;
817 TM2Context
* const l
= avctx
->priv_data
;
818 AVFrame
* const p
= &l
->pic
;
819 int i
, offset
= TM2_HEADER_SIZE
, t
, ret
;
822 swbuf
= av_malloc(buf_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
824 av_log(avctx
, AV_LOG_ERROR
, "Cannot allocate temporary buffer\n");
828 p
->buffer_hints
= FF_BUFFER_HINTS_VALID
| FF_BUFFER_HINTS_PRESERVE
| FF_BUFFER_HINTS_REUSABLE
;
829 if(avctx
->reget_buffer(avctx
, p
) < 0){
830 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
835 l
->dsp
.bswap_buf((uint32_t*)swbuf
, (const uint32_t*)buf
, buf_size
>> 2);
837 if ((ret
= tm2_read_header(l
, swbuf
)) < 0) {
842 for(i
= 0; i
< TM2_NUM_STREAMS
; i
++){
843 if (offset
>= buf_size
) {
845 return AVERROR_INVALIDDATA
;
847 t
= tm2_read_stream(l
, swbuf
+ offset
, tm2_stream_order
[i
],
855 p
->key_frame
= tm2_decode_blocks(l
, p
);
857 p
->pict_type
= AV_PICTURE_TYPE_I
;
859 p
->pict_type
= AV_PICTURE_TYPE_P
;
863 *(AVFrame
*)data
= l
->pic
;
869 static av_cold
int decode_init(AVCodecContext
*avctx
){
870 TM2Context
* const l
= avctx
->priv_data
;
871 int i
, w
= avctx
->width
, h
= avctx
->height
;
873 if((avctx
->width
& 3) || (avctx
->height
& 3)){
874 av_log(avctx
, AV_LOG_ERROR
, "Width and height must be multiple of 4\n");
880 avctx
->pix_fmt
= AV_PIX_FMT_BGR24
;
882 ff_dsputil_init(&l
->dsp
, avctx
);
884 l
->last
= av_malloc(4 * sizeof(*l
->last
) * (w
>> 2));
885 l
->clast
= av_malloc(4 * sizeof(*l
->clast
) * (w
>> 2));
887 for(i
= 0; i
< TM2_NUM_STREAMS
; i
++) {
894 l
->Y1_base
= av_malloc(sizeof(*l
->Y1_base
) * w
* h
);
895 l
->Y2_base
= av_malloc(sizeof(*l
->Y2_base
) * w
* h
);
899 l
->U1_base
= av_malloc(sizeof(*l
->U1_base
) * w
* h
);
900 l
->V1_base
= av_malloc(sizeof(*l
->V1_base
) * w
* h
);
901 l
->U2_base
= av_malloc(sizeof(*l
->U2_base
) * w
* h
);
902 l
->V2_base
= av_malloc(sizeof(*l
->V1_base
) * w
* h
);
905 if (!l
->Y1_base
|| !l
->Y2_base
|| !l
->U1_base
||
906 !l
->V1_base
|| !l
->U2_base
|| !l
->V2_base
||
907 !l
->last
|| !l
->clast
) {
908 av_freep(l
->Y1_base
);
909 av_freep(l
->Y2_base
);
910 av_freep(l
->U1_base
);
911 av_freep(l
->U2_base
);
912 av_freep(l
->V1_base
);
913 av_freep(l
->V2_base
);
916 return AVERROR(ENOMEM
);
918 l
->Y1
= l
->Y1_base
+ l
->y_stride
* 4 + 4;
919 l
->Y2
= l
->Y2_base
+ l
->y_stride
* 4 + 4;
920 l
->U1
= l
->U1_base
+ l
->uv_stride
* 2 + 2;
921 l
->U2
= l
->U2_base
+ l
->uv_stride
* 2 + 2;
922 l
->V1
= l
->V1_base
+ l
->uv_stride
* 2 + 2;
923 l
->V2
= l
->V2_base
+ l
->uv_stride
* 2 + 2;
928 static av_cold
int decode_end(AVCodecContext
*avctx
){
929 TM2Context
* const l
= avctx
->priv_data
;
930 AVFrame
*pic
= &l
->pic
;
935 for(i
= 0; i
< TM2_NUM_STREAMS
; i
++)
936 av_free(l
->tokens
[i
]);
947 avctx
->release_buffer(avctx
, pic
);
952 AVCodec ff_truemotion2_decoder
= {
953 .name
= "truemotion2",
954 .type
= AVMEDIA_TYPE_VIDEO
,
955 .id
= AV_CODEC_ID_TRUEMOTION2
,
956 .priv_data_size
= sizeof(TM2Context
),
959 .decode
= decode_frame
,
960 .capabilities
= CODEC_CAP_DR1
,
961 .long_name
= NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),