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.
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;
69 int *tokens[TM2_NUM_STREAMS];
70 int tok_lens[TM2_NUM_STREAMS];
71 int tok_ptrs[TM2_NUM_STREAMS];
72 int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
73 /* for blocks decoding */
79 /* data for current and previous frame */
80 int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
81 int *Y1, *U1, *V1, *Y2, *U2, *V2;
82 int y_stride, uv_stride;
87 * Huffman codes for each of streams
89 typedef struct TM2Codes {
90 VLC vlc; ///< table for Libav bitstream reader
92 int *recode; ///< table for converting from code indexes to values
97 * structure for gathering Huffman codes information
99 typedef struct TM2Huff {
100 int val_bits; ///< length of literal
101 int max_bits; ///< maximum length of code
102 int min_bits; ///< minimum length of code
103 int nodes; ///< total number of nodes in tree
104 int num; ///< current number filled
105 int max_num; ///< total number of codes
106 int *nums; ///< literals
107 uint32_t *bits; ///< codes
108 int *lens; ///< codelengths
111 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
114 if (length > huff->max_bits) {
115 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
117 return AVERROR_INVALIDDATA;
120 if (!get_bits1(&ctx->gb)) { /* literal */
124 if (huff->num >= huff->max_num) {
125 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
126 return AVERROR_INVALIDDATA;
128 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
129 huff->bits[huff->num] = prefix;
130 huff->lens[huff->num] = length;
133 } else { /* non-terminal node */
134 if ((ret = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
136 if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
142 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
147 huff.val_bits = get_bits(&ctx->gb, 5);
148 huff.max_bits = get_bits(&ctx->gb, 5);
149 huff.min_bits = get_bits(&ctx->gb, 5);
150 huff.nodes = get_bits_long(&ctx->gb, 17);
153 /* check for correct codes parameters */
154 if ((huff.val_bits < 1) || (huff.val_bits > 32) ||
155 (huff.max_bits < 0) || (huff.max_bits > 25)) {
156 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal "
157 "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits);
158 return AVERROR_INVALIDDATA;
160 if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
161 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree "
162 "nodes: %i\n", huff.nodes);
163 return AVERROR_INVALIDDATA;
166 if (huff.max_bits == 0)
169 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
170 huff.max_num = (huff.nodes + 1) >> 1;
171 huff.nums = av_mallocz(huff.max_num * sizeof(int));
172 huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
173 huff.lens = av_mallocz(huff.max_num * sizeof(int));
175 res = tm2_read_tree(ctx, 0, 0, &huff);
177 if (huff.num != huff.max_num) {
178 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
179 huff.num, huff.max_num);
180 res = AVERROR_INVALIDDATA;
183 /* convert codes to vlc_table */
187 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
188 huff.lens, sizeof(int), sizeof(int),
189 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
191 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
193 code->bits = huff.max_bits;
194 code->length = huff.max_num;
195 code->recode = av_malloc(code->length * sizeof(int));
196 for (i = 0; i < code->length; i++)
197 code->recode[i] = huff.nums[i];
200 /* free allocated memory */
208 static void tm2_free_codes(TM2Codes *code)
210 av_free(code->recode);
212 ff_free_vlc(&code->vlc);
215 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
218 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
219 return code->recode[val];
222 #define TM2_OLD_HEADER_MAGIC 0x00000100
223 #define TM2_NEW_HEADER_MAGIC 0x00000101
225 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
227 uint32_t magic = AV_RL32(buf);
230 case TM2_OLD_HEADER_MAGIC:
231 avpriv_request_sample(ctx->avctx, "Old TM2 header");
233 case TM2_NEW_HEADER_MAGIC:
236 av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n",
238 return AVERROR_INVALIDDATA;
242 static int tm2_read_deltas(TM2Context *ctx, int stream_id)
247 d = get_bits(&ctx->gb, 9);
248 mb = get_bits(&ctx->gb, 5);
250 if ((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
251 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
252 return AVERROR_INVALIDDATA;
255 for (i = 0; i < d; i++) {
256 v = get_bits_long(&ctx->gb, mb);
257 if (v & (1 << (mb - 1)))
258 ctx->deltas[stream_id][i] = v - (1 << mb);
260 ctx->deltas[stream_id][i] = v;
262 for (; i < TM2_DELTAS; i++)
263 ctx->deltas[stream_id][i] = 0;
268 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
276 /* get stream length in dwords */
277 bytestream2_init(&gb, buf, buf_size);
278 len = bytestream2_get_be32(&gb);
284 if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
285 av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
286 return AVERROR_INVALIDDATA;
289 toks = bytestream2_get_be32(&gb);
291 len = bytestream2_get_be32(&gb);
292 if (len == TM2_ESCAPE) {
293 len = bytestream2_get_be32(&gb);
296 pos = bytestream2_tell(&gb);
298 return AVERROR_INVALIDDATA;
299 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
300 if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
302 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
305 /* skip unused fields */
306 len = bytestream2_get_be32(&gb);
307 if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
308 bytestream2_skip(&gb, 8); /* unused by decoder */
310 bytestream2_skip(&gb, 4); /* unused by decoder */
313 pos = bytestream2_tell(&gb);
315 return AVERROR_INVALIDDATA;
316 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
317 if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
319 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
322 /* check if we have sane number of tokens */
323 if ((toks < 0) || (toks > 0xFFFFFF)) {
324 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
325 tm2_free_codes(&codes);
326 return AVERROR_INVALIDDATA;
328 ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
329 ctx->tok_lens[stream_id] = toks;
330 len = bytestream2_get_be32(&gb);
332 pos = bytestream2_tell(&gb);
334 return AVERROR_INVALIDDATA;
335 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
336 for (i = 0; i < toks; i++) {
337 if (get_bits_left(&ctx->gb) <= 0) {
338 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
339 return AVERROR_INVALIDDATA;
341 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
342 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
343 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
344 ctx->tokens[stream_id][i], stream_id, i);
345 return AVERROR_INVALIDDATA;
349 for (i = 0; i < toks; i++) {
350 ctx->tokens[stream_id][i] = codes.recode[0];
351 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
352 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
353 ctx->tokens[stream_id][i], stream_id, i);
354 return AVERROR_INVALIDDATA;
358 tm2_free_codes(&codes);
363 static inline int GET_TOK(TM2Context *ctx,int type)
365 if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
366 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]);
370 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
371 return ctx->tokens[type][ctx->tok_ptrs[type]++];
374 /* blocks decoding routines */
376 /* common Y, U, V pointers initialisation */
377 #define TM2_INIT_POINTERS() \
380 int Ystride, Ustride, Vstride;\
382 Ystride = ctx->y_stride;\
383 Vstride = ctx->uv_stride;\
384 Ustride = ctx->uv_stride;\
385 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
386 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
387 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
388 last = ctx->last + bx * 4;\
389 clast = ctx->clast + bx * 4;
391 #define TM2_INIT_POINTERS_2() \
393 int oYstride, oUstride, oVstride;\
395 TM2_INIT_POINTERS();\
399 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
400 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
401 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
403 /* recalculate last and delta values for next blocks */
404 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
405 CD[0] = CHR[1] - last[1];\
406 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
407 last[0] = (int)CHR[stride + 0];\
408 last[1] = (int)CHR[stride + 1];}
410 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
411 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
416 for (j = 0; j < 4; j++){
418 for (i = 0; i < 4; i++){
419 d = deltas[i + j * 4];
422 Y[i] = av_clip_uint8(last[i]);
429 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
432 for (j = 0; j < 2; j++) {
433 for (i = 0; i < 2; i++) {
434 CD[j] += deltas[i + j * 2];
442 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
452 t = (CD[0] + CD[1]) >> 1;
453 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
454 CD[1] = CD[0] + CD[1] - t;
458 tm2_high_chroma(data, stride, clast, CD, deltas);
461 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
468 for (i = 0; i < 4; i++) {
469 deltas[i] = GET_TOK(ctx, TM2_C_HI);
470 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
472 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
473 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
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_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
489 deltas[0] = GET_TOK(ctx, TM2_C_LO);
490 deltas[1] = deltas[2] = deltas[3] = 0;
491 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
493 deltas[0] = GET_TOK(ctx, TM2_C_LO);
494 deltas[1] = deltas[2] = deltas[3] = 0;
495 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
498 for (i = 0; i < 16; i++)
499 deltas[i] = GET_TOK(ctx, TM2_L_HI);
501 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
504 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
512 deltas[0] = GET_TOK(ctx, TM2_C_LO);
513 deltas[1] = deltas[2] = deltas[3] = 0;
514 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
516 deltas[0] = GET_TOK(ctx, TM2_C_LO);
517 deltas[1] = deltas[2] = deltas[3] = 0;
518 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
521 for (i = 0; i < 16; i++)
524 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
525 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
526 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
527 deltas[10] = GET_TOK(ctx, TM2_L_LO);
530 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
532 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
533 last[2] = (last[1] + last[3]) >> 1;
535 t1 = ctx->D[0] + ctx->D[1];
537 ctx->D[1] = t1 - (t1 >> 1);
538 t2 = ctx->D[2] + ctx->D[3];
540 ctx->D[3] = t2 - (t2 >> 1);
542 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
545 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
549 int left, right, diff;
554 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
555 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
557 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
558 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
561 for (i = 0; i < 16; i++)
564 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
567 left = last[-1] - ct;
573 last[0] = left + (diff >> 2);
574 last[1] = left + (diff >> 1);
575 last[2] = right - (diff >> 2);
580 ctx->D[0] = (tp + (ct >> 2)) - left;
582 ctx->D[1] = (tp + (ct >> 1)) - left;
584 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
586 ctx->D[3] = (tp + ct) - left;
588 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
591 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
594 TM2_INIT_POINTERS_2();
597 for (j = 0; j < 2; j++) {
598 for (i = 0; i < 2; i++){
602 U += Ustride; V += Vstride;
603 Uo += oUstride; Vo += oVstride;
607 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
608 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
611 ctx->D[0] = Yo[3] - last[3];
612 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
613 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
614 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
616 for (j = 0; j < 4; j++) {
617 for (i = 0; i < 4; i++) {
626 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
630 TM2_INIT_POINTERS_2();
633 for (j = 0; j < 2; j++) {
634 for (i = 0; i < 2; i++) {
635 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
636 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
645 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
646 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
649 ctx->D[0] = Yo[3] - last[3];
650 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
651 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
652 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
654 for (j = 0; j < 4; j++) {
656 for (i = 0; i < 4; i++) {
657 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
660 ctx->D[j] = last[3] - d;
666 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
670 TM2_INIT_POINTERS_2();
672 mx = GET_TOK(ctx, TM2_MOT);
673 my = GET_TOK(ctx, TM2_MOT);
674 mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
675 my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
677 Yo += my * oYstride + mx;
678 Uo += (my >> 1) * oUstride + (mx >> 1);
679 Vo += (my >> 1) * oVstride + (mx >> 1);
682 for (j = 0; j < 2; j++) {
683 for (i = 0; i < 2; i++) {
694 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
695 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
698 for (j = 0; j < 4; j++) {
699 for (i = 0; i < 4; i++) {
705 /* calculate deltas */
707 ctx->D[0] = Y[3] - last[3];
708 ctx->D[1] = Y[3 + Ystride] - Y[3];
709 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
710 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
711 for (i = 0; i < 4; i++)
712 last[i] = Y[i + Ystride * 3];
715 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
718 int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
724 for (i = 0; i < TM2_NUM_STREAMS; i++)
725 ctx->tok_ptrs[i] = 0;
727 if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
728 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
729 return AVERROR_INVALIDDATA;
732 memset(ctx->last, 0, 4 * bw * sizeof(int));
733 memset(ctx->clast, 0, 4 * bw * sizeof(int));
735 for (j = 0; j < bh; j++) {
736 memset(ctx->D, 0, 4 * sizeof(int));
737 memset(ctx->CD, 0, 4 * sizeof(int));
738 for (i = 0; i < bw; i++) {
739 type = GET_TOK(ctx, TM2_TYPE);
742 tm2_hi_res_block(ctx, p, i, j);
745 tm2_med_res_block(ctx, p, i, j);
748 tm2_low_res_block(ctx, p, i, j);
751 tm2_null_res_block(ctx, p, i, j);
754 tm2_update_block(ctx, p, i, j);
758 tm2_still_block(ctx, p, i, j);
762 tm2_motion_block(ctx, p, i, j);
766 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
771 /* copy data from our buffer to AVFrame */
772 Y = (ctx->cur?ctx->Y2:ctx->Y1);
773 U = (ctx->cur?ctx->U2:ctx->U1);
774 V = (ctx->cur?ctx->V2:ctx->V1);
776 for (j = 0; j < h; j++) {
777 for (i = 0; i < w; i++) {
778 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
779 dst[3*i+0] = av_clip_uint8(y + v);
780 dst[3*i+1] = av_clip_uint8(y);
781 dst[3*i+2] = av_clip_uint8(y + u);
784 /* horizontal edge extension */
785 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
786 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
788 /* vertical edge extension */
790 memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
791 memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
792 memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
793 memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
794 } else if (j == h - 1) {
795 memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
796 memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
797 memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
798 memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
803 /* horizontal edge extension */
804 U[-2] = U[-1] = U[0];
805 V[-2] = V[-1] = V[0];
806 U[cw + 1] = U[cw] = U[cw - 1];
807 V[cw + 1] = V[cw] = V[cw - 1];
809 /* vertical edge extension */
811 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
812 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
813 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
814 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
815 } else if (j == h - 1) {
816 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
817 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
818 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
819 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
825 dst += p->linesize[0];
831 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
832 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
835 #define TM2_HEADER_SIZE 40
837 static int decode_frame(AVCodecContext *avctx,
838 void *data, int *got_frame,
841 TM2Context * const l = avctx->priv_data;
842 const uint8_t *buf = avpkt->data;
843 int buf_size = avpkt->size & ~3;
844 AVFrame * const p = l->pic;
845 int offset = TM2_HEADER_SIZE;
849 swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
851 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
852 return AVERROR(ENOMEM);
855 if ((ret = ff_reget_buffer(avctx, p)) < 0) {
856 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
861 l->bdsp.bswap_buf((uint32_t *) swbuf, (const uint32_t *) buf,
864 if ((ret = tm2_read_header(l, swbuf)) < 0) {
869 for (i = 0; i < TM2_NUM_STREAMS; i++) {
870 if (offset >= buf_size) {
872 return AVERROR_INVALIDDATA;
874 t = tm2_read_stream(l, swbuf + offset, tm2_stream_order[i],
882 p->key_frame = tm2_decode_blocks(l, p);
884 p->pict_type = AV_PICTURE_TYPE_I;
886 p->pict_type = AV_PICTURE_TYPE_P;
890 ret = av_frame_ref(data, l->pic);
893 return (ret < 0) ? ret : buf_size;
896 static av_cold int decode_init(AVCodecContext *avctx)
898 TM2Context * const l = avctx->priv_data;
899 int i, w = avctx->width, h = avctx->height;
901 if ((avctx->width & 3) || (avctx->height & 3)) {
902 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
903 return AVERROR(EINVAL);
907 avctx->pix_fmt = AV_PIX_FMT_BGR24;
909 l->pic = av_frame_alloc();
911 return AVERROR(ENOMEM);
913 ff_bswapdsp_init(&l->bdsp);
915 l->last = av_malloc(4 * sizeof(*l->last) * (w >> 2));
916 l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
918 for (i = 0; i < TM2_NUM_STREAMS; i++) {
925 l->Y1_base = av_malloc(sizeof(*l->Y1_base) * w * h);
926 l->Y2_base = av_malloc(sizeof(*l->Y2_base) * w * h);
930 l->U1_base = av_malloc(sizeof(*l->U1_base) * w * h);
931 l->V1_base = av_malloc(sizeof(*l->V1_base) * w * h);
932 l->U2_base = av_malloc(sizeof(*l->U2_base) * w * h);
933 l->V2_base = av_malloc(sizeof(*l->V1_base) * w * h);
936 if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
937 !l->V1_base || !l->U2_base || !l->V2_base ||
938 !l->last || !l->clast) {
939 av_freep(&l->Y1_base);
940 av_freep(&l->Y2_base);
941 av_freep(&l->U1_base);
942 av_freep(&l->U2_base);
943 av_freep(&l->V1_base);
944 av_freep(&l->V2_base);
947 return AVERROR(ENOMEM);
949 l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
950 l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
951 l->U1 = l->U1_base + l->uv_stride * 2 + 2;
952 l->U2 = l->U2_base + l->uv_stride * 2 + 2;
953 l->V1 = l->V1_base + l->uv_stride * 2 + 2;
954 l->V2 = l->V2_base + l->uv_stride * 2 + 2;
959 static av_cold int decode_end(AVCodecContext *avctx)
961 TM2Context * const l = avctx->priv_data;
966 for (i = 0; i < TM2_NUM_STREAMS; i++)
967 av_free(l->tokens[i]);
977 av_frame_free(&l->pic);
982 AVCodec ff_truemotion2_decoder = {
983 .name = "truemotion2",
984 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
985 .type = AVMEDIA_TYPE_VIDEO,
986 .id = AV_CODEC_ID_TRUEMOTION2,
987 .priv_data_size = sizeof(TM2Context),
990 .decode = decode_frame,
991 .capabilities = CODEC_CAP_DR1,