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
35 /* Huffman-coded streams of different types of blocks */
58 typedef struct TM2Context
{
59 AVCodecContext
*avctx
;
66 int *tokens
[TM2_NUM_STREAMS
];
67 int tok_lens
[TM2_NUM_STREAMS
];
68 int tok_ptrs
[TM2_NUM_STREAMS
];
69 int deltas
[TM2_NUM_STREAMS
][TM2_DELTAS
];
70 /* for blocks decoding */
76 /* data for current and previous frame */
77 int *Y1_base
, *U1_base
, *V1_base
, *Y2_base
, *U2_base
, *V2_base
;
78 int *Y1
, *U1
, *V1
, *Y2
, *U2
, *V2
;
79 int y_stride
, uv_stride
;
84 * Huffman codes for each of streams
86 typedef struct TM2Codes
{
87 VLC vlc
; ///< table for Libav bitstream reader
89 int *recode
; ///< table for converting from code indexes to values
94 * structure for gathering Huffman codes information
96 typedef struct TM2Huff
{
97 int val_bits
; ///< length of literal
98 int max_bits
; ///< maximum length of code
99 int min_bits
; ///< minimum length of code
100 int nodes
; ///< total number of nodes in tree
101 int num
; ///< current number filled
102 int max_num
; ///< total number of codes
103 int *nums
; ///< literals
104 uint32_t *bits
; ///< codes
105 int *lens
; ///< codelengths
108 static int tm2_read_tree(TM2Context
*ctx
, uint32_t prefix
, int length
, TM2Huff
*huff
)
111 if (length
> huff
->max_bits
) {
112 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Tree exceeded its given depth (%i)\n",
114 return AVERROR_INVALIDDATA
;
117 if (!get_bits1(&ctx
->gb
)) { /* literal */
121 if (huff
->num
>= huff
->max_num
) {
122 av_log(ctx
->avctx
, AV_LOG_DEBUG
, "Too many literals\n");
123 return AVERROR_INVALIDDATA
;
125 huff
->nums
[huff
->num
] = get_bits_long(&ctx
->gb
, huff
->val_bits
);
126 huff
->bits
[huff
->num
] = prefix
;
127 huff
->lens
[huff
->num
] = length
;
130 } else { /* non-terminal node */
131 if ((ret
= tm2_read_tree(ctx
, prefix
<< 1, length
+ 1, huff
)) < 0)
133 if ((ret
= tm2_read_tree(ctx
, (prefix
<< 1) | 1, length
+ 1, huff
)) < 0)
139 static int tm2_build_huff_table(TM2Context
*ctx
, TM2Codes
*code
)
144 huff
.val_bits
= get_bits(&ctx
->gb
, 5);
145 huff
.max_bits
= get_bits(&ctx
->gb
, 5);
146 huff
.min_bits
= get_bits(&ctx
->gb
, 5);
147 huff
.nodes
= get_bits_long(&ctx
->gb
, 17);
150 /* check for correct codes parameters */
151 if ((huff
.val_bits
< 1) || (huff
.val_bits
> 32) ||
152 (huff
.max_bits
< 0) || (huff
.max_bits
> 25)) {
153 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect tree parameters - literal "
154 "length: %i, max code length: %i\n", huff
.val_bits
, huff
.max_bits
);
155 return AVERROR_INVALIDDATA
;
157 if ((huff
.nodes
<= 0) || (huff
.nodes
> 0x10000)) {
158 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect number of Huffman tree "
159 "nodes: %i\n", huff
.nodes
);
160 return AVERROR_INVALIDDATA
;
163 if (huff
.max_bits
== 0)
166 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
167 huff
.max_num
= (huff
.nodes
+ 1) >> 1;
168 huff
.nums
= av_mallocz(huff
.max_num
* sizeof(int));
169 huff
.bits
= av_mallocz(huff
.max_num
* sizeof(uint32_t));
170 huff
.lens
= av_mallocz(huff
.max_num
* sizeof(int));
172 res
= tm2_read_tree(ctx
, 0, 0, &huff
);
174 if (huff
.num
!= huff
.max_num
) {
175 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Got less codes than expected: %i of %i\n",
176 huff
.num
, huff
.max_num
);
177 res
= AVERROR_INVALIDDATA
;
180 /* convert codes to vlc_table */
184 res
= init_vlc(&code
->vlc
, huff
.max_bits
, huff
.max_num
,
185 huff
.lens
, sizeof(int), sizeof(int),
186 huff
.bits
, sizeof(uint32_t), sizeof(uint32_t), 0);
188 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
190 code
->bits
= huff
.max_bits
;
191 code
->length
= huff
.max_num
;
192 code
->recode
= av_malloc(code
->length
* sizeof(int));
193 for (i
= 0; i
< code
->length
; i
++)
194 code
->recode
[i
] = huff
.nums
[i
];
197 /* free allocated memory */
205 static void tm2_free_codes(TM2Codes
*code
)
207 av_free(code
->recode
);
209 ff_free_vlc(&code
->vlc
);
212 static inline int tm2_get_token(GetBitContext
*gb
, TM2Codes
*code
)
215 val
= get_vlc2(gb
, code
->vlc
.table
, code
->bits
, 1);
216 return code
->recode
[val
];
219 #define TM2_OLD_HEADER_MAGIC 0x00000100
220 #define TM2_NEW_HEADER_MAGIC 0x00000101
222 static inline int tm2_read_header(TM2Context
*ctx
, const uint8_t *buf
)
224 uint32_t magic
= AV_RL32(buf
);
227 case TM2_OLD_HEADER_MAGIC
:
228 av_log_missing_feature(ctx
->avctx
, "TM2 old header", 1);
230 case TM2_NEW_HEADER_MAGIC
:
233 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Not a TM2 header: 0x%08X\n", magic
);
234 return AVERROR_INVALIDDATA
;
238 static int tm2_read_deltas(TM2Context
*ctx
, int stream_id
)
243 d
= get_bits(&ctx
->gb
, 9);
244 mb
= get_bits(&ctx
->gb
, 5);
246 if ((d
< 1) || (d
> TM2_DELTAS
) || (mb
< 1) || (mb
> 32)) {
247 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect delta table: %i deltas x %i bits\n", d
, mb
);
248 return AVERROR_INVALIDDATA
;
251 for (i
= 0; i
< d
; i
++) {
252 v
= get_bits_long(&ctx
->gb
, mb
);
253 if (v
& (1 << (mb
- 1)))
254 ctx
->deltas
[stream_id
][i
] = v
- (1 << mb
);
256 ctx
->deltas
[stream_id
][i
] = v
;
258 for (; i
< TM2_DELTAS
; i
++)
259 ctx
->deltas
[stream_id
][i
] = 0;
264 static int tm2_read_stream(TM2Context
*ctx
, const uint8_t *buf
, int stream_id
, int buf_size
)
272 /* get stream length in dwords */
273 bytestream2_init(&gb
, buf
, buf_size
);
274 len
= bytestream2_get_be32(&gb
);
280 if (len
>= INT_MAX
/4-1 || len
< 0 || len
> buf_size
) {
281 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Error, invalid stream size.\n");
282 return AVERROR_INVALIDDATA
;
285 toks
= bytestream2_get_be32(&gb
);
287 len
= bytestream2_get_be32(&gb
);
288 if (len
== TM2_ESCAPE
) {
289 len
= bytestream2_get_be32(&gb
);
292 pos
= bytestream2_tell(&gb
);
294 return AVERROR_INVALIDDATA
;
295 init_get_bits(&ctx
->gb
, buf
+ pos
, (skip
- pos
) * 8);
296 if ((ret
= tm2_read_deltas(ctx
, stream_id
)) < 0)
298 bytestream2_skip(&gb
, ((get_bits_count(&ctx
->gb
) + 31) >> 5) << 2);
301 /* skip unused fields */
302 len
= bytestream2_get_be32(&gb
);
303 if (len
== TM2_ESCAPE
) { /* some unknown length - could be escaped too */
304 bytestream2_skip(&gb
, 8); /* unused by decoder */
306 bytestream2_skip(&gb
, 4); /* unused by decoder */
309 pos
= bytestream2_tell(&gb
);
311 return AVERROR_INVALIDDATA
;
312 init_get_bits(&ctx
->gb
, buf
+ pos
, (skip
- pos
) * 8);
313 if ((ret
= tm2_build_huff_table(ctx
, &codes
)) < 0)
315 bytestream2_skip(&gb
, ((get_bits_count(&ctx
->gb
) + 31) >> 5) << 2);
318 /* check if we have sane number of tokens */
319 if ((toks
< 0) || (toks
> 0xFFFFFF)) {
320 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect number of tokens: %i\n", toks
);
321 tm2_free_codes(&codes
);
322 return AVERROR_INVALIDDATA
;
324 ctx
->tokens
[stream_id
] = av_realloc(ctx
->tokens
[stream_id
], toks
* sizeof(int));
325 ctx
->tok_lens
[stream_id
] = toks
;
326 len
= bytestream2_get_be32(&gb
);
328 pos
= bytestream2_tell(&gb
);
330 return AVERROR_INVALIDDATA
;
331 init_get_bits(&ctx
->gb
, buf
+ pos
, (skip
- pos
) * 8);
332 for (i
= 0; i
< toks
; i
++) {
333 if (get_bits_left(&ctx
->gb
) <= 0) {
334 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Incorrect number of tokens: %i\n", toks
);
335 return AVERROR_INVALIDDATA
;
337 ctx
->tokens
[stream_id
][i
] = tm2_get_token(&ctx
->gb
, &codes
);
338 if (stream_id
<= TM2_MOT
&& ctx
->tokens
[stream_id
][i
] >= TM2_DELTAS
) {
339 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Invalid delta token index %d for type %d, n=%d\n",
340 ctx
->tokens
[stream_id
][i
], stream_id
, i
);
341 return AVERROR_INVALIDDATA
;
345 for (i
= 0; i
< toks
; i
++) {
346 ctx
->tokens
[stream_id
][i
] = codes
.recode
[0];
347 if (stream_id
<= TM2_MOT
&& ctx
->tokens
[stream_id
][i
] >= TM2_DELTAS
) {
348 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Invalid delta token index %d for type %d, n=%d\n",
349 ctx
->tokens
[stream_id
][i
], stream_id
, i
);
350 return AVERROR_INVALIDDATA
;
354 tm2_free_codes(&codes
);
359 static inline int GET_TOK(TM2Context
*ctx
,int type
)
361 if (ctx
->tok_ptrs
[type
] >= ctx
->tok_lens
[type
]) {
362 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
]);
366 return ctx
->deltas
[type
][ctx
->tokens
[type
][ctx
->tok_ptrs
[type
]++]];
367 return ctx
->tokens
[type
][ctx
->tok_ptrs
[type
]++];
370 /* blocks decoding routines */
372 /* common Y, U, V pointers initialisation */
373 #define TM2_INIT_POINTERS() \
376 int Ystride, Ustride, Vstride;\
378 Ystride = ctx->y_stride;\
379 Vstride = ctx->uv_stride;\
380 Ustride = ctx->uv_stride;\
381 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
382 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
383 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
384 last = ctx->last + bx * 4;\
385 clast = ctx->clast + bx * 4;
387 #define TM2_INIT_POINTERS_2() \
389 int oYstride, oUstride, oVstride;\
391 TM2_INIT_POINTERS();\
395 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
396 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
397 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
399 /* recalculate last and delta values for next blocks */
400 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
401 CD[0] = CHR[1] - last[1];\
402 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
403 last[0] = (int)CHR[stride + 0];\
404 last[1] = (int)CHR[stride + 1];}
406 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
407 static inline void tm2_apply_deltas(TM2Context
*ctx
, int* Y
, int stride
, int *deltas
, int *last
)
412 for (j
= 0; j
< 4; j
++){
414 for (i
= 0; i
< 4; i
++){
415 d
= deltas
[i
+ j
* 4];
418 Y
[i
] = av_clip_uint8(last
[i
]);
425 static inline void tm2_high_chroma(int *data
, int stride
, int *last
, int *CD
, int *deltas
)
428 for (j
= 0; j
< 2; j
++) {
429 for (i
= 0; i
< 2; i
++) {
430 CD
[j
] += deltas
[i
+ j
* 2];
438 static inline void tm2_low_chroma(int *data
, int stride
, int *clast
, int *CD
, int *deltas
, int bx
)
448 t
= (CD
[0] + CD
[1]) >> 1;
449 l
= (prev
- CD
[0] - CD
[1] + clast
[1]) >> 1;
450 CD
[1] = CD
[0] + CD
[1] - t
;
454 tm2_high_chroma(data
, stride
, clast
, CD
, deltas
);
457 static inline void tm2_hi_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
464 for (i
= 0; i
< 4; i
++) {
465 deltas
[i
] = GET_TOK(ctx
, TM2_C_HI
);
466 deltas
[i
+ 4] = GET_TOK(ctx
, TM2_C_HI
);
468 tm2_high_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
);
469 tm2_high_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
+ 4);
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_med_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
485 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
486 deltas
[1] = deltas
[2] = deltas
[3] = 0;
487 tm2_low_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
, bx
);
489 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
490 deltas
[1] = deltas
[2] = deltas
[3] = 0;
491 tm2_low_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
, bx
);
494 for (i
= 0; i
< 16; i
++)
495 deltas
[i
] = GET_TOK(ctx
, TM2_L_HI
);
497 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
500 static inline void tm2_low_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
508 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
509 deltas
[1] = deltas
[2] = deltas
[3] = 0;
510 tm2_low_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
, bx
);
512 deltas
[0] = GET_TOK(ctx
, TM2_C_LO
);
513 deltas
[1] = deltas
[2] = deltas
[3] = 0;
514 tm2_low_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
, bx
);
517 for (i
= 0; i
< 16; i
++)
520 deltas
[ 0] = GET_TOK(ctx
, TM2_L_LO
);
521 deltas
[ 2] = GET_TOK(ctx
, TM2_L_LO
);
522 deltas
[ 8] = GET_TOK(ctx
, TM2_L_LO
);
523 deltas
[10] = GET_TOK(ctx
, TM2_L_LO
);
526 last
[0] = (last
[-1] - ctx
->D
[0] - ctx
->D
[1] - ctx
->D
[2] - ctx
->D
[3] + last
[1]) >> 1;
528 last
[0] = (last
[1] - ctx
->D
[0] - ctx
->D
[1] - ctx
->D
[2] - ctx
->D
[3])>> 1;
529 last
[2] = (last
[1] + last
[3]) >> 1;
531 t1
= ctx
->D
[0] + ctx
->D
[1];
533 ctx
->D
[1] = t1
- (t1
>> 1);
534 t2
= ctx
->D
[2] + ctx
->D
[3];
536 ctx
->D
[3] = t2
- (t2
>> 1);
538 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
541 static inline void tm2_null_res_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
545 int left
, right
, diff
;
550 deltas
[0] = deltas
[1] = deltas
[2] = deltas
[3] = 0;
551 tm2_low_chroma(U
, Ustride
, clast
, ctx
->CD
, deltas
, bx
);
553 deltas
[0] = deltas
[1] = deltas
[2] = deltas
[3] = 0;
554 tm2_low_chroma(V
, Vstride
, clast
+ 2, ctx
->CD
+ 2, deltas
, bx
);
557 for (i
= 0; i
< 16; i
++)
560 ct
= ctx
->D
[0] + ctx
->D
[1] + ctx
->D
[2] + ctx
->D
[3];
563 left
= last
[-1] - ct
;
569 last
[0] = left
+ (diff
>> 2);
570 last
[1] = left
+ (diff
>> 1);
571 last
[2] = right
- (diff
>> 2);
576 ctx
->D
[0] = (tp
+ (ct
>> 2)) - left
;
578 ctx
->D
[1] = (tp
+ (ct
>> 1)) - left
;
580 ctx
->D
[2] = ((tp
+ ct
) - (ct
>> 2)) - left
;
582 ctx
->D
[3] = (tp
+ ct
) - left
;
584 tm2_apply_deltas(ctx
, Y
, Ystride
, deltas
, last
);
587 static inline void tm2_still_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
590 TM2_INIT_POINTERS_2();
593 for (j
= 0; j
< 2; j
++) {
594 for (i
= 0; i
< 2; i
++){
598 U
+= Ustride
; V
+= Vstride
;
599 Uo
+= oUstride
; Vo
+= oVstride
;
603 TM2_RECALC_BLOCK(U
, Ustride
, clast
, ctx
->CD
);
604 TM2_RECALC_BLOCK(V
, Vstride
, (clast
+ 2), (ctx
->CD
+ 2));
607 ctx
->D
[0] = Yo
[3] - last
[3];
608 ctx
->D
[1] = Yo
[3 + oYstride
] - Yo
[3];
609 ctx
->D
[2] = Yo
[3 + oYstride
* 2] - Yo
[3 + oYstride
];
610 ctx
->D
[3] = Yo
[3 + oYstride
* 3] - Yo
[3 + oYstride
* 2];
612 for (j
= 0; j
< 4; j
++) {
613 for (i
= 0; i
< 4; i
++) {
622 static inline void tm2_update_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
626 TM2_INIT_POINTERS_2();
629 for (j
= 0; j
< 2; j
++) {
630 for (i
= 0; i
< 2; i
++) {
631 U
[i
] = Uo
[i
] + GET_TOK(ctx
, TM2_UPD
);
632 V
[i
] = Vo
[i
] + GET_TOK(ctx
, TM2_UPD
);
641 TM2_RECALC_BLOCK(U
, Ustride
, clast
, ctx
->CD
);
642 TM2_RECALC_BLOCK(V
, Vstride
, (clast
+ 2), (ctx
->CD
+ 2));
645 ctx
->D
[0] = Yo
[3] - last
[3];
646 ctx
->D
[1] = Yo
[3 + oYstride
] - Yo
[3];
647 ctx
->D
[2] = Yo
[3 + oYstride
* 2] - Yo
[3 + oYstride
];
648 ctx
->D
[3] = Yo
[3 + oYstride
* 3] - Yo
[3 + oYstride
* 2];
650 for (j
= 0; j
< 4; j
++) {
652 for (i
= 0; i
< 4; i
++) {
653 Y
[i
] = Yo
[i
] + GET_TOK(ctx
, TM2_UPD
);
656 ctx
->D
[j
] = last
[3] - d
;
662 static inline void tm2_motion_block(TM2Context
*ctx
, AVFrame
*pic
, int bx
, int by
)
666 TM2_INIT_POINTERS_2();
668 mx
= GET_TOK(ctx
, TM2_MOT
);
669 my
= GET_TOK(ctx
, TM2_MOT
);
670 mx
= av_clip(mx
, -(bx
* 4 + 4), ctx
->avctx
->width
- bx
* 4);
671 my
= av_clip(my
, -(by
* 4 + 4), ctx
->avctx
->height
- by
* 4);
673 Yo
+= my
* oYstride
+ mx
;
674 Uo
+= (my
>> 1) * oUstride
+ (mx
>> 1);
675 Vo
+= (my
>> 1) * oVstride
+ (mx
>> 1);
678 for (j
= 0; j
< 2; j
++) {
679 for (i
= 0; i
< 2; i
++) {
690 TM2_RECALC_BLOCK(U
, Ustride
, clast
, ctx
->CD
);
691 TM2_RECALC_BLOCK(V
, Vstride
, (clast
+ 2), (ctx
->CD
+ 2));
694 for (j
= 0; j
< 4; j
++) {
695 for (i
= 0; i
< 4; i
++) {
701 /* calculate deltas */
703 ctx
->D
[0] = Y
[3] - last
[3];
704 ctx
->D
[1] = Y
[3 + Ystride
] - Y
[3];
705 ctx
->D
[2] = Y
[3 + Ystride
* 2] - Y
[3 + Ystride
];
706 ctx
->D
[3] = Y
[3 + Ystride
* 3] - Y
[3 + Ystride
* 2];
707 for (i
= 0; i
< 4; i
++)
708 last
[i
] = Y
[i
+ Ystride
* 3];
711 static int tm2_decode_blocks(TM2Context
*ctx
, AVFrame
*p
)
714 int w
= ctx
->avctx
->width
, h
= ctx
->avctx
->height
, bw
= w
>> 2, bh
= h
>> 2, cw
= w
>> 1;
720 for (i
= 0; i
< TM2_NUM_STREAMS
; i
++)
721 ctx
->tok_ptrs
[i
] = 0;
723 if (ctx
->tok_lens
[TM2_TYPE
]<bw
*bh
) {
724 av_log(ctx
->avctx
,AV_LOG_ERROR
,"Got %i tokens for %i blocks\n",ctx
->tok_lens
[TM2_TYPE
],bw
*bh
);
725 return AVERROR_INVALIDDATA
;
728 memset(ctx
->last
, 0, 4 * bw
* sizeof(int));
729 memset(ctx
->clast
, 0, 4 * bw
* sizeof(int));
731 for (j
= 0; j
< bh
; j
++) {
732 memset(ctx
->D
, 0, 4 * sizeof(int));
733 memset(ctx
->CD
, 0, 4 * sizeof(int));
734 for (i
= 0; i
< bw
; i
++) {
735 type
= GET_TOK(ctx
, TM2_TYPE
);
738 tm2_hi_res_block(ctx
, p
, i
, j
);
741 tm2_med_res_block(ctx
, p
, i
, j
);
744 tm2_low_res_block(ctx
, p
, i
, j
);
747 tm2_null_res_block(ctx
, p
, i
, j
);
750 tm2_update_block(ctx
, p
, i
, j
);
754 tm2_still_block(ctx
, p
, i
, j
);
758 tm2_motion_block(ctx
, p
, i
, j
);
762 av_log(ctx
->avctx
, AV_LOG_ERROR
, "Skipping unknown block type %i\n", type
);
767 /* copy data from our buffer to AVFrame */
768 Y
= (ctx
->cur
?ctx
->Y2
:ctx
->Y1
);
769 U
= (ctx
->cur
?ctx
->U2
:ctx
->U1
);
770 V
= (ctx
->cur
?ctx
->V2
:ctx
->V1
);
772 for (j
= 0; j
< h
; j
++) {
773 for (i
= 0; i
< w
; i
++) {
774 int y
= Y
[i
], u
= U
[i
>> 1], v
= V
[i
>> 1];
775 dst
[3*i
+0] = av_clip_uint8(y
+ v
);
776 dst
[3*i
+1] = av_clip_uint8(y
);
777 dst
[3*i
+2] = av_clip_uint8(y
+ u
);
780 /* horizontal edge extension */
781 Y
[-4] = Y
[-3] = Y
[-2] = Y
[-1] = Y
[0];
782 Y
[w
+ 3] = Y
[w
+ 2] = Y
[w
+ 1] = Y
[w
] = Y
[w
- 1];
784 /* vertical edge extension */
786 memcpy(Y
- 4 - 1 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
787 memcpy(Y
- 4 - 2 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
788 memcpy(Y
- 4 - 3 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
789 memcpy(Y
- 4 - 4 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
790 } else if (j
== h
- 1) {
791 memcpy(Y
- 4 + 1 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
792 memcpy(Y
- 4 + 2 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
793 memcpy(Y
- 4 + 3 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
794 memcpy(Y
- 4 + 4 * ctx
->y_stride
, Y
- 4, ctx
->y_stride
);
799 /* horizontal edge extension */
800 U
[-2] = U
[-1] = U
[0];
801 V
[-2] = V
[-1] = V
[0];
802 U
[cw
+ 1] = U
[cw
] = U
[cw
- 1];
803 V
[cw
+ 1] = V
[cw
] = V
[cw
- 1];
805 /* vertical edge extension */
807 memcpy(U
- 2 - 1 * ctx
->uv_stride
, U
- 2, ctx
->uv_stride
);
808 memcpy(V
- 2 - 1 * ctx
->uv_stride
, V
- 2, ctx
->uv_stride
);
809 memcpy(U
- 2 - 2 * ctx
->uv_stride
, U
- 2, ctx
->uv_stride
);
810 memcpy(V
- 2 - 2 * ctx
->uv_stride
, V
- 2, ctx
->uv_stride
);
811 } else if (j
== h
- 1) {
812 memcpy(U
- 2 + 1 * ctx
->uv_stride
, U
- 2, ctx
->uv_stride
);
813 memcpy(V
- 2 + 1 * ctx
->uv_stride
, V
- 2, ctx
->uv_stride
);
814 memcpy(U
- 2 + 2 * ctx
->uv_stride
, U
- 2, ctx
->uv_stride
);
815 memcpy(V
- 2 + 2 * ctx
->uv_stride
, V
- 2, ctx
->uv_stride
);
821 dst
+= p
->linesize
[0];
827 static const int tm2_stream_order
[TM2_NUM_STREAMS
] = {
828 TM2_C_HI
, TM2_C_LO
, TM2_L_HI
, TM2_L_LO
, TM2_UPD
, TM2_MOT
, TM2_TYPE
831 #define TM2_HEADER_SIZE 40
833 static int decode_frame(AVCodecContext
*avctx
,
834 void *data
, int *got_frame
,
837 TM2Context
* const l
= avctx
->priv_data
;
838 const uint8_t *buf
= avpkt
->data
;
839 int buf_size
= avpkt
->size
& ~3;
840 AVFrame
* const p
= &l
->pic
;
841 int offset
= TM2_HEADER_SIZE
;
845 swbuf
= av_malloc(buf_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
847 av_log(avctx
, AV_LOG_ERROR
, "Cannot allocate temporary buffer\n");
848 return AVERROR(ENOMEM
);
851 p
->buffer_hints
= FF_BUFFER_HINTS_VALID
| FF_BUFFER_HINTS_PRESERVE
| FF_BUFFER_HINTS_REUSABLE
;
852 if ((ret
= avctx
->reget_buffer(avctx
, p
)) < 0) {
853 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
858 l
->dsp
.bswap_buf((uint32_t*)swbuf
, (const uint32_t*)buf
, buf_size
>> 2);
860 if ((ret
= tm2_read_header(l
, swbuf
)) < 0) {
865 for (i
= 0; i
< TM2_NUM_STREAMS
; i
++) {
866 if (offset
>= buf_size
) {
868 return AVERROR_INVALIDDATA
;
870 t
= tm2_read_stream(l
, swbuf
+ offset
, tm2_stream_order
[i
],
878 p
->key_frame
= tm2_decode_blocks(l
, p
);
880 p
->pict_type
= AV_PICTURE_TYPE_I
;
882 p
->pict_type
= AV_PICTURE_TYPE_P
;
886 *(AVFrame
*)data
= l
->pic
;
892 static av_cold
int decode_init(AVCodecContext
*avctx
)
894 TM2Context
* const l
= avctx
->priv_data
;
895 int i
, w
= avctx
->width
, h
= avctx
->height
;
897 if ((avctx
->width
& 3) || (avctx
->height
& 3)) {
898 av_log(avctx
, AV_LOG_ERROR
, "Width and height must be multiple of 4\n");
899 return AVERROR(EINVAL
);
903 l
->pic
.data
[0] = NULL
;
904 avctx
->pix_fmt
= AV_PIX_FMT_BGR24
;
906 ff_dsputil_init(&l
->dsp
, avctx
);
908 l
->last
= av_malloc(4 * sizeof(*l
->last
) * (w
>> 2));
909 l
->clast
= av_malloc(4 * sizeof(*l
->clast
) * (w
>> 2));
911 for (i
= 0; i
< TM2_NUM_STREAMS
; i
++) {
918 l
->Y1_base
= av_malloc(sizeof(*l
->Y1_base
) * w
* h
);
919 l
->Y2_base
= av_malloc(sizeof(*l
->Y2_base
) * w
* h
);
923 l
->U1_base
= av_malloc(sizeof(*l
->U1_base
) * w
* h
);
924 l
->V1_base
= av_malloc(sizeof(*l
->V1_base
) * w
* h
);
925 l
->U2_base
= av_malloc(sizeof(*l
->U2_base
) * w
* h
);
926 l
->V2_base
= av_malloc(sizeof(*l
->V1_base
) * w
* h
);
929 if (!l
->Y1_base
|| !l
->Y2_base
|| !l
->U1_base
||
930 !l
->V1_base
|| !l
->U2_base
|| !l
->V2_base
||
931 !l
->last
|| !l
->clast
) {
932 av_freep(l
->Y1_base
);
933 av_freep(l
->Y2_base
);
934 av_freep(l
->U1_base
);
935 av_freep(l
->U2_base
);
936 av_freep(l
->V1_base
);
937 av_freep(l
->V2_base
);
940 return AVERROR(ENOMEM
);
942 l
->Y1
= l
->Y1_base
+ l
->y_stride
* 4 + 4;
943 l
->Y2
= l
->Y2_base
+ l
->y_stride
* 4 + 4;
944 l
->U1
= l
->U1_base
+ l
->uv_stride
* 2 + 2;
945 l
->U2
= l
->U2_base
+ l
->uv_stride
* 2 + 2;
946 l
->V1
= l
->V1_base
+ l
->uv_stride
* 2 + 2;
947 l
->V2
= l
->V2_base
+ l
->uv_stride
* 2 + 2;
952 static av_cold
int decode_end(AVCodecContext
*avctx
)
954 TM2Context
* const l
= avctx
->priv_data
;
955 AVFrame
*pic
= &l
->pic
;
960 for (i
= 0; i
< TM2_NUM_STREAMS
; i
++)
961 av_free(l
->tokens
[i
]);
972 avctx
->release_buffer(avctx
, pic
);
977 AVCodec ff_truemotion2_decoder
= {
978 .name
= "truemotion2",
979 .type
= AVMEDIA_TYPE_VIDEO
,
980 .id
= AV_CODEC_ID_TRUEMOTION2
,
981 .priv_data_size
= sizeof(TM2Context
),
984 .decode
= decode_frame
,
985 .capabilities
= CODEC_CAP_DR1
,
986 .long_name
= NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),