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 #include "bytestream.h"
35 #define TM2_ESCAPE 0x80000000
38 /* Huffman-coded streams of different types of blocks */
61 typedef struct TM2Context {
62 AVCodecContext *avctx;
73 int *tokens[TM2_NUM_STREAMS];
74 int tok_lens[TM2_NUM_STREAMS];
75 int tok_ptrs[TM2_NUM_STREAMS];
76 int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
77 /* for blocks decoding */
83 /* data for current and previous frame */
84 int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
85 int *Y1, *U1, *V1, *Y2, *U2, *V2;
86 int y_stride, uv_stride;
91 * Huffman codes for each of streams
93 typedef struct TM2Codes {
94 VLC vlc; ///< table for FFmpeg bitstream reader
96 int *recode; ///< table for converting from code indexes to values
101 * structure for gathering Huffman codes information
103 typedef struct TM2Huff {
104 int val_bits; ///< length of literal
105 int max_bits; ///< maximum length of code
106 int min_bits; ///< minimum length of code
107 int nodes; ///< total number of nodes in tree
108 int num; ///< current number filled
109 int max_num; ///< total number of codes
110 int *nums; ///< literals
111 uint32_t *bits; ///< codes
112 int *lens; ///< codelengths
117 * @returns the length of the longest code or an AVERROR code
119 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
122 if (length > huff->max_bits) {
123 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
125 return AVERROR_INVALIDDATA;
128 if (!get_bits1(&ctx->gb)) { /* literal */
132 if (huff->num >= huff->max_num) {
133 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
134 return AVERROR_INVALIDDATA;
136 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
137 huff->bits[huff->num] = prefix;
138 huff->lens[huff->num] = length;
141 } else { /* non-terminal node */
142 if ((ret2 = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
144 if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
147 return FFMAX(ret, ret2);
150 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
155 huff.val_bits = get_bits(&ctx->gb, 5);
156 huff.max_bits = get_bits(&ctx->gb, 5);
157 huff.min_bits = get_bits(&ctx->gb, 5);
158 huff.nodes = get_bits(&ctx->gb, 17);
161 /* check for correct codes parameters */
162 if ((huff.val_bits < 1) || (huff.val_bits > 32) ||
163 (huff.max_bits < 0) || (huff.max_bits > 25)) {
164 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal "
165 "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits);
166 return AVERROR_INVALIDDATA;
168 if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
169 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree "
170 "nodes: %i\n", huff.nodes);
171 return AVERROR_INVALIDDATA;
174 if (huff.max_bits == 0)
177 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
178 huff.max_num = (huff.nodes + 1) >> 1;
179 huff.nums = av_calloc(huff.max_num, sizeof(int));
180 huff.bits = av_calloc(huff.max_num, sizeof(uint32_t));
181 huff.lens = av_calloc(huff.max_num, sizeof(int));
183 if (!huff.nums || !huff.bits || !huff.lens) {
184 res = AVERROR(ENOMEM);
188 res = tm2_read_tree(ctx, 0, 0, &huff);
190 if (res >= 0 && res != huff.max_bits) {
191 av_log(ctx->avctx, AV_LOG_ERROR, "Got less bits than expected: %i of %i\n",
193 res = AVERROR_INVALIDDATA;
195 if (huff.num != huff.max_num) {
196 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
197 huff.num, huff.max_num);
198 res = AVERROR_INVALIDDATA;
201 /* convert codes to vlc_table */
203 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
204 huff.lens, sizeof(int), sizeof(int),
205 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
207 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
209 code->bits = huff.max_bits;
210 code->length = huff.max_num;
211 code->recode = huff.nums;
217 /* free allocated memory */
225 static void tm2_free_codes(TM2Codes *code)
227 av_free(code->recode);
229 ff_free_vlc(&code->vlc);
232 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
235 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
238 return code->recode[val];
241 #define TM2_OLD_HEADER_MAGIC 0x00000100
242 #define TM2_NEW_HEADER_MAGIC 0x00000101
244 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
246 uint32_t magic = AV_RL32(buf);
249 case TM2_OLD_HEADER_MAGIC:
250 avpriv_request_sample(ctx->avctx, "Old TM2 header");
252 case TM2_NEW_HEADER_MAGIC:
255 av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n",
257 return AVERROR_INVALIDDATA;
261 static int tm2_read_deltas(TM2Context *ctx, int stream_id)
266 d = get_bits(&ctx->gb, 9);
267 mb = get_bits(&ctx->gb, 5);
270 if ((d < 1) || (d > TM2_DELTAS) || (mb < 1)) {
271 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
272 return AVERROR_INVALIDDATA;
275 for (i = 0; i < d; i++) {
276 v = get_bits_long(&ctx->gb, mb);
277 if (v & (1 << (mb - 1)))
278 ctx->deltas[stream_id][i] = v - (1U << mb);
280 ctx->deltas[stream_id][i] = v;
282 for (; i < TM2_DELTAS; i++)
283 ctx->deltas[stream_id][i] = 0;
288 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
297 av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n");
298 return AVERROR_INVALIDDATA;
301 /* get stream length in dwords */
302 bytestream2_init(&gb, buf, buf_size);
303 len = bytestream2_get_be32(&gb);
308 if (len >= INT_MAX / 4 - 1 || len < 0 || len * 4 + 4 > buf_size) {
309 av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
310 return AVERROR_INVALIDDATA;
314 toks = bytestream2_get_be32(&gb);
316 len = bytestream2_get_be32(&gb);
317 if (len == TM2_ESCAPE) {
318 len = bytestream2_get_be32(&gb);
321 pos = bytestream2_tell(&gb);
323 return AVERROR_INVALIDDATA;
324 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
325 if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
327 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
330 /* skip unused fields */
331 len = bytestream2_get_be32(&gb);
332 if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
333 bytestream2_skip(&gb, 8); /* unused by decoder */
335 bytestream2_skip(&gb, 4); /* unused by decoder */
338 pos = bytestream2_tell(&gb);
340 return AVERROR_INVALIDDATA;
341 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
342 if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
344 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
347 /* check if we have sane number of tokens */
348 if ((toks < 0) || (toks > 0xFFFFFF)) {
349 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
350 ret = AVERROR_INVALIDDATA;
353 ret = av_reallocp_array(&ctx->tokens[stream_id], toks, sizeof(int));
355 ctx->tok_lens[stream_id] = 0;
358 ctx->tok_lens[stream_id] = toks;
359 len = bytestream2_get_be32(&gb);
361 pos = bytestream2_tell(&gb);
363 ret = AVERROR_INVALIDDATA;
366 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
367 for (i = 0; i < toks; i++) {
368 if (get_bits_left(&ctx->gb) <= 0) {
369 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
370 ret = AVERROR_INVALIDDATA;
373 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
374 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS || ctx->tokens[stream_id][i]<0) {
375 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
376 ctx->tokens[stream_id][i], stream_id, i);
377 ret = AVERROR_INVALIDDATA;
383 ret = AVERROR_INVALIDDATA;
386 for (i = 0; i < toks; i++) {
387 ctx->tokens[stream_id][i] = codes.recode[0];
388 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
389 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
390 ctx->tokens[stream_id][i], stream_id, i);
391 ret = AVERROR_INVALIDDATA;
400 tm2_free_codes(&codes);
404 static inline int GET_TOK(TM2Context *ctx,int type)
406 if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
407 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]);
411 if (type <= TM2_MOT) {
412 if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) {
413 av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]);
416 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
418 return ctx->tokens[type][ctx->tok_ptrs[type]++];
421 /* blocks decoding routines */
423 /* common Y, U, V pointers initialisation */
424 #define TM2_INIT_POINTERS() \
427 int Ystride, Ustride, Vstride;\
429 Ystride = ctx->y_stride;\
430 Vstride = ctx->uv_stride;\
431 Ustride = ctx->uv_stride;\
432 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
433 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
434 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
435 last = ctx->last + bx * 4;\
436 clast = ctx->clast + bx * 4;
438 #define TM2_INIT_POINTERS_2() \
439 unsigned *Yo, *Uo, *Vo;\
440 int oYstride, oUstride, oVstride;\
442 TM2_INIT_POINTERS();\
446 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
447 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
448 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
450 /* recalculate last and delta values for next blocks */
451 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
452 CD[0] = (unsigned)CHR[ 1] - (unsigned)last[1];\
453 CD[1] = (unsigned)CHR[stride + 1] - (unsigned) CHR[1];\
454 last[0] = (int)CHR[stride + 0];\
455 last[1] = (int)CHR[stride + 1];}
457 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
458 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
463 for (j = 0; j < 4; j++){
465 for (i = 0; i < 4; i++){
466 d = deltas[i + j * 4];
469 Y[i] = av_clip_uint8(last[i]);
476 static inline void tm2_high_chroma(int *data, int stride, int *last, unsigned *CD, int *deltas)
479 for (j = 0; j < 2; j++) {
480 for (i = 0; i < 2; i++) {
481 CD[j] += deltas[i + j * 2];
489 static inline void tm2_low_chroma(int *data, int stride, int *clast, unsigned *CD, int *deltas, int bx)
499 t = (int)(CD[0] + CD[1]) >> 1;
500 l = (int)(prev - CD[0] - CD[1] + clast[1]) >> 1;
501 CD[1] = CD[0] + CD[1] - t;
505 tm2_high_chroma(data, stride, clast, CD, deltas);
508 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
515 for (i = 0; i < 4; i++) {
516 deltas[i] = GET_TOK(ctx, TM2_C_HI);
517 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
519 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
520 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
523 for (i = 0; i < 16; i++)
524 deltas[i] = GET_TOK(ctx, TM2_L_HI);
526 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
529 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
536 deltas[0] = GET_TOK(ctx, TM2_C_LO);
537 deltas[1] = deltas[2] = deltas[3] = 0;
538 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
540 deltas[0] = GET_TOK(ctx, TM2_C_LO);
541 deltas[1] = deltas[2] = deltas[3] = 0;
542 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
545 for (i = 0; i < 16; i++)
546 deltas[i] = GET_TOK(ctx, TM2_L_HI);
548 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
551 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
559 deltas[0] = GET_TOK(ctx, TM2_C_LO);
560 deltas[1] = deltas[2] = deltas[3] = 0;
561 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
563 deltas[0] = GET_TOK(ctx, TM2_C_LO);
564 deltas[1] = deltas[2] = deltas[3] = 0;
565 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
568 for (i = 0; i < 16; i++)
571 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
572 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
573 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
574 deltas[10] = GET_TOK(ctx, TM2_L_LO);
577 last[0] = (int)((unsigned)last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
579 last[0] = (int)((unsigned)last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
580 last[2] = (int)((unsigned)last[1] + last[3]) >> 1;
582 t1 = ctx->D[0] + (unsigned)ctx->D[1];
584 ctx->D[1] = t1 - (t1 >> 1);
585 t2 = ctx->D[2] + (unsigned)ctx->D[3];
587 ctx->D[3] = t2 - (t2 >> 1);
589 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
592 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
596 unsigned left, right;
602 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
603 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
605 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
606 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
609 for (i = 0; i < 16; i++)
612 ct = (unsigned)ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
615 left = last[-1] - (unsigned)ct;
621 last[0] = left + (diff >> 2);
622 last[1] = left + (diff >> 1);
623 last[2] = right - (diff >> 2);
628 ctx->D[0] = (tp + (ct >> 2)) - left;
630 ctx->D[1] = (tp + (ct >> 1)) - left;
632 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
634 ctx->D[3] = (tp + ct) - left;
636 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
639 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
642 TM2_INIT_POINTERS_2();
645 for (j = 0; j < 2; j++) {
646 for (i = 0; i < 2; i++){
650 U += Ustride; V += Vstride;
651 Uo += oUstride; Vo += oVstride;
655 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
656 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
659 ctx->D[0] = Yo[3] - last[3];
660 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
661 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
662 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
664 for (j = 0; j < 4; j++) {
665 for (i = 0; i < 4; i++) {
674 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
678 TM2_INIT_POINTERS_2();
681 for (j = 0; j < 2; j++) {
682 for (i = 0; i < 2; i++) {
683 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
684 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
693 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
694 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
697 ctx->D[0] = Yo[3] - last[3];
698 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
699 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
700 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
702 for (j = 0; j < 4; j++) {
704 for (i = 0; i < 4; i++) {
705 Y[i] = Yo[i] + (unsigned)GET_TOK(ctx, TM2_UPD);
708 ctx->D[j] = last[3] - d;
714 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
718 TM2_INIT_POINTERS_2();
720 mx = GET_TOK(ctx, TM2_MOT);
721 my = GET_TOK(ctx, TM2_MOT);
722 mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
723 my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
725 if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) {
726 av_log(ctx->avctx, AV_LOG_ERROR, "MV out of picture\n");
730 Yo += my * oYstride + mx;
731 Uo += (my >> 1) * oUstride + (mx >> 1);
732 Vo += (my >> 1) * oVstride + (mx >> 1);
735 for (j = 0; j < 2; j++) {
736 for (i = 0; i < 2; i++) {
747 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
748 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
751 for (j = 0; j < 4; j++) {
752 for (i = 0; i < 4; i++) {
758 /* calculate deltas */
760 ctx->D[0] = (unsigned)Y[3] - last[3];
761 ctx->D[1] = (unsigned)Y[3 + Ystride] - Y[3];
762 ctx->D[2] = (unsigned)Y[3 + Ystride * 2] - Y[3 + Ystride];
763 ctx->D[3] = (unsigned)Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
764 for (i = 0; i < 4; i++)
765 last[i] = Y[i + Ystride * 3];
768 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
771 int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
777 for (i = 0; i < TM2_NUM_STREAMS; i++)
778 ctx->tok_ptrs[i] = 0;
780 if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
781 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
782 return AVERROR_INVALIDDATA;
785 memset(ctx->last, 0, 4 * bw * sizeof(int));
786 memset(ctx->clast, 0, 4 * bw * sizeof(int));
788 for (j = 0; j < bh; j++) {
789 memset(ctx->D, 0, 4 * sizeof(int));
790 memset(ctx->CD, 0, 4 * sizeof(int));
791 for (i = 0; i < bw; i++) {
792 type = GET_TOK(ctx, TM2_TYPE);
795 tm2_hi_res_block(ctx, p, i, j);
798 tm2_med_res_block(ctx, p, i, j);
801 tm2_low_res_block(ctx, p, i, j);
804 tm2_null_res_block(ctx, p, i, j);
807 tm2_update_block(ctx, p, i, j);
811 tm2_still_block(ctx, p, i, j);
815 tm2_motion_block(ctx, p, i, j);
819 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
822 return AVERROR_INVALIDDATA;
826 /* copy data from our buffer to AVFrame */
827 Y = (ctx->cur?ctx->Y2:ctx->Y1);
828 U = (ctx->cur?ctx->U2:ctx->U1);
829 V = (ctx->cur?ctx->V2:ctx->V1);
831 for (j = 0; j < h; j++) {
832 for (i = 0; i < w; i++) {
833 unsigned y = Y[i], u = U[i >> 1], v = V[i >> 1];
834 dst[3*i+0] = av_clip_uint8(y + v);
835 dst[3*i+1] = av_clip_uint8(y);
836 dst[3*i+2] = av_clip_uint8(y + u);
839 /* horizontal edge extension */
840 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
841 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
843 /* vertical edge extension */
845 memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
846 memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
847 memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
848 memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
849 } else if (j == h - 1) {
850 memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
851 memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
852 memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
853 memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
858 /* horizontal edge extension */
859 U[-2] = U[-1] = U[0];
860 V[-2] = V[-1] = V[0];
861 U[cw + 1] = U[cw] = U[cw - 1];
862 V[cw + 1] = V[cw] = V[cw - 1];
864 /* vertical edge extension */
866 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
867 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
868 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
869 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
870 } else if (j == h - 1) {
871 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
872 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
873 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
874 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
880 dst += p->linesize[0];
886 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
887 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
890 #define TM2_HEADER_SIZE 40
892 static int decode_frame(AVCodecContext *avctx,
893 void *data, int *got_frame,
896 TM2Context * const l = avctx->priv_data;
897 const uint8_t *buf = avpkt->data;
898 int buf_size = avpkt->size & ~3;
899 AVFrame * const p = l->pic;
900 int offset = TM2_HEADER_SIZE;
905 av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size);
907 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
908 return AVERROR(ENOMEM);
911 if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
914 l->bdsp.bswap_buf((uint32_t *) l->buffer, (const uint32_t *) buf,
917 if ((ret = tm2_read_header(l, l->buffer)) < 0) {
921 for (i = 0; i < TM2_NUM_STREAMS; i++) {
922 if (offset >= buf_size) {
923 av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n");
924 return AVERROR_INVALIDDATA;
927 t = tm2_read_stream(l, l->buffer + offset, tm2_stream_order[i],
930 int j = tm2_stream_order[i];
932 memset(l->tokens[j], 0, sizeof(**l->tokens) * l->tok_lens[j]);
937 p->key_frame = tm2_decode_blocks(l, p);
939 p->pict_type = AV_PICTURE_TYPE_I;
941 p->pict_type = AV_PICTURE_TYPE_P;
945 ret = av_frame_ref(data, l->pic);
947 return (ret < 0) ? ret : buf_size;
950 static av_cold int decode_init(AVCodecContext *avctx)
952 TM2Context * const l = avctx->priv_data;
953 int i, w = avctx->width, h = avctx->height;
955 if ((avctx->width & 3) || (avctx->height & 3)) {
956 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
957 return AVERROR(EINVAL);
961 avctx->pix_fmt = AV_PIX_FMT_BGR24;
963 l->pic = av_frame_alloc();
965 return AVERROR(ENOMEM);
967 ff_bswapdsp_init(&l->bdsp);
969 l->last = av_malloc_array(w >> 2, 4 * sizeof(*l->last) );
970 l->clast = av_malloc_array(w >> 2, 4 * sizeof(*l->clast));
972 for (i = 0; i < TM2_NUM_STREAMS; i++) {
979 l->Y1_base = av_calloc(w * h, sizeof(*l->Y1_base));
980 l->Y2_base = av_calloc(w * h, sizeof(*l->Y2_base));
984 l->U1_base = av_calloc(w * h, sizeof(*l->U1_base));
985 l->V1_base = av_calloc(w * h, sizeof(*l->V1_base));
986 l->U2_base = av_calloc(w * h, sizeof(*l->U2_base));
987 l->V2_base = av_calloc(w * h, sizeof(*l->V1_base));
990 if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
991 !l->V1_base || !l->U2_base || !l->V2_base ||
992 !l->last || !l->clast) {
993 av_freep(&l->Y1_base);
994 av_freep(&l->Y2_base);
995 av_freep(&l->U1_base);
996 av_freep(&l->U2_base);
997 av_freep(&l->V1_base);
998 av_freep(&l->V2_base);
1000 av_freep(&l->clast);
1001 av_frame_free(&l->pic);
1002 return AVERROR(ENOMEM);
1004 l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
1005 l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
1006 l->U1 = l->U1_base + l->uv_stride * 2 + 2;
1007 l->U2 = l->U2_base + l->uv_stride * 2 + 2;
1008 l->V1 = l->V1_base + l->uv_stride * 2 + 2;
1009 l->V2 = l->V2_base + l->uv_stride * 2 + 2;
1014 static av_cold int decode_end(AVCodecContext *avctx)
1016 TM2Context * const l = avctx->priv_data;
1021 for (i = 0; i < TM2_NUM_STREAMS; i++)
1022 av_freep(&l->tokens[i]);
1024 av_freep(&l->Y1_base);
1025 av_freep(&l->U1_base);
1026 av_freep(&l->V1_base);
1027 av_freep(&l->Y2_base);
1028 av_freep(&l->U2_base);
1029 av_freep(&l->V2_base);
1031 av_freep(&l->buffer);
1034 av_frame_free(&l->pic);
1039 AVCodec ff_truemotion2_decoder = {
1040 .name = "truemotion2",
1041 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
1042 .type = AVMEDIA_TYPE_VIDEO,
1043 .id = AV_CODEC_ID_TRUEMOTION2,
1044 .priv_data_size = sizeof(TM2Context),
1045 .init = decode_init,
1046 .close = decode_end,
1047 .decode = decode_frame,
1048 .capabilities = AV_CODEC_CAP_DR1,