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 *Y_base, *UV_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 uint8_t *lens; ///< codelengths
116 * @returns the length of the longest code or an AVERROR code
118 static int tm2_read_tree(TM2Context *ctx, int length, TM2Huff *huff)
121 if (length > huff->max_bits) {
122 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
124 return AVERROR_INVALIDDATA;
127 if (!get_bits1(&ctx->gb)) { /* literal */
131 if (huff->num >= huff->max_num) {
132 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
133 return AVERROR_INVALIDDATA;
135 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
136 huff->lens[huff->num] = length;
139 } else { /* non-terminal node */
140 if ((ret2 = tm2_read_tree(ctx, length + 1, huff)) < 0)
142 if ((ret = tm2_read_tree(ctx, length + 1, huff)) < 0)
145 return FFMAX(ret, ret2);
148 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
153 huff.val_bits = get_bits(&ctx->gb, 5);
154 huff.max_bits = get_bits(&ctx->gb, 5);
155 huff.min_bits = get_bits(&ctx->gb, 5);
156 huff.nodes = get_bits(&ctx->gb, 17);
159 /* check for correct codes parameters */
160 if ((huff.val_bits < 1) || (huff.val_bits > 32) ||
161 (huff.max_bits < 0) || (huff.max_bits > 25)) {
162 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal "
163 "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits);
164 return AVERROR_INVALIDDATA;
166 if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
167 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree "
168 "nodes: %i\n", huff.nodes);
169 return AVERROR_INVALIDDATA;
172 if (huff.max_bits == 0)
175 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
176 huff.max_num = (huff.nodes + 1) >> 1;
177 huff.nums = av_calloc(huff.max_num, sizeof(int));
178 huff.lens = av_mallocz(huff.max_num);
180 if (!huff.nums || !huff.lens) {
181 res = AVERROR(ENOMEM);
185 res = tm2_read_tree(ctx, 0, &huff);
187 if (res >= 0 && res != huff.max_bits) {
188 av_log(ctx->avctx, AV_LOG_ERROR, "Got less bits than expected: %i of %i\n",
190 res = AVERROR_INVALIDDATA;
192 if (huff.num != huff.max_num) {
193 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
194 huff.num, huff.max_num);
195 res = AVERROR_INVALIDDATA;
198 /* convert codes to vlc_table */
200 res = ff_init_vlc_from_lengths(&code->vlc, huff.max_bits, huff.max_num,
201 huff.lens, sizeof(huff.lens[0]),
202 NULL, 0, 0, 0, 0, ctx->avctx);
204 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
206 code->bits = huff.max_bits;
207 code->length = huff.max_num;
208 code->recode = huff.nums;
214 /* free allocated memory */
221 static void tm2_free_codes(TM2Codes *code)
223 av_free(code->recode);
225 ff_free_vlc(&code->vlc);
228 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
231 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
234 return code->recode[val];
237 #define TM2_OLD_HEADER_MAGIC 0x00000100
238 #define TM2_NEW_HEADER_MAGIC 0x00000101
240 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
242 uint32_t magic = AV_RL32(buf);
245 case TM2_OLD_HEADER_MAGIC:
246 avpriv_request_sample(ctx->avctx, "Old TM2 header");
248 case TM2_NEW_HEADER_MAGIC:
251 av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n",
253 return AVERROR_INVALIDDATA;
257 static int tm2_read_deltas(TM2Context *ctx, int stream_id)
262 d = get_bits(&ctx->gb, 9);
263 mb = get_bits(&ctx->gb, 5);
266 if ((d < 1) || (d > TM2_DELTAS) || (mb < 1)) {
267 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
268 return AVERROR_INVALIDDATA;
271 for (i = 0; i < d; i++) {
272 v = get_bits_long(&ctx->gb, mb);
273 if (v & (1 << (mb - 1)))
274 ctx->deltas[stream_id][i] = v - (1U << mb);
276 ctx->deltas[stream_id][i] = v;
278 for (; i < TM2_DELTAS; i++)
279 ctx->deltas[stream_id][i] = 0;
284 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
293 av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n");
294 return AVERROR_INVALIDDATA;
297 /* get stream length in dwords */
298 bytestream2_init(&gb, buf, buf_size);
299 len = bytestream2_get_be32(&gb);
304 if (len >= INT_MAX / 4 - 1 || len < 0 || len * 4 + 4 > buf_size) {
305 av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
306 return AVERROR_INVALIDDATA;
310 toks = bytestream2_get_be32(&gb);
312 len = bytestream2_get_be32(&gb);
313 if (len == TM2_ESCAPE) {
314 len = bytestream2_get_be32(&gb);
317 pos = bytestream2_tell(&gb);
319 return AVERROR_INVALIDDATA;
320 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
321 if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
323 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
326 /* skip unused fields */
327 len = bytestream2_get_be32(&gb);
328 if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
329 bytestream2_skip(&gb, 8); /* unused by decoder */
331 bytestream2_skip(&gb, 4); /* unused by decoder */
334 pos = bytestream2_tell(&gb);
336 return AVERROR_INVALIDDATA;
337 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
338 if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
340 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
343 /* check if we have sane number of tokens */
344 if ((toks < 0) || (toks > 0xFFFFFF)) {
345 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
346 ret = AVERROR_INVALIDDATA;
349 ret = av_reallocp_array(&ctx->tokens[stream_id], toks, sizeof(int));
351 ctx->tok_lens[stream_id] = 0;
354 ctx->tok_lens[stream_id] = toks;
355 len = bytestream2_get_be32(&gb);
357 pos = bytestream2_tell(&gb);
359 ret = AVERROR_INVALIDDATA;
362 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
363 for (i = 0; i < toks; i++) {
364 if (get_bits_left(&ctx->gb) <= 0) {
365 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
366 ret = AVERROR_INVALIDDATA;
369 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
370 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS || ctx->tokens[stream_id][i]<0) {
371 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
372 ctx->tokens[stream_id][i], stream_id, i);
373 ret = AVERROR_INVALIDDATA;
379 ret = AVERROR_INVALIDDATA;
382 for (i = 0; i < toks; i++) {
383 ctx->tokens[stream_id][i] = codes.recode[0];
384 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
385 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
386 ctx->tokens[stream_id][i], stream_id, i);
387 ret = AVERROR_INVALIDDATA;
396 tm2_free_codes(&codes);
400 static inline int GET_TOK(TM2Context *ctx,int type)
402 if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
403 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]);
407 if (type <= TM2_MOT) {
408 if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) {
409 av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]);
412 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
414 return ctx->tokens[type][ctx->tok_ptrs[type]++];
417 /* blocks decoding routines */
419 /* common Y, U, V pointers initialisation */
420 #define TM2_INIT_POINTERS() \
423 int Ystride, Ustride, Vstride;\
425 Ystride = ctx->y_stride;\
426 Vstride = ctx->uv_stride;\
427 Ustride = ctx->uv_stride;\
428 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
429 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
430 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
431 last = ctx->last + bx * 4;\
432 clast = ctx->clast + bx * 4;
434 #define TM2_INIT_POINTERS_2() \
435 unsigned *Yo, *Uo, *Vo;\
436 int oYstride, oUstride, oVstride;\
438 TM2_INIT_POINTERS();\
442 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
443 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
444 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
446 /* recalculate last and delta values for next blocks */
447 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
448 CD[0] = (unsigned)CHR[ 1] - (unsigned)last[1];\
449 CD[1] = (unsigned)CHR[stride + 1] - (unsigned) CHR[1];\
450 last[0] = (int)CHR[stride + 0];\
451 last[1] = (int)CHR[stride + 1];}
453 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
454 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
459 for (j = 0; j < 4; j++){
461 for (i = 0; i < 4; i++){
462 d = deltas[i + j * 4];
465 Y[i] = av_clip_uint8(last[i]);
472 static inline void tm2_high_chroma(int *data, int stride, int *last, unsigned *CD, int *deltas)
475 for (j = 0; j < 2; j++) {
476 for (i = 0; i < 2; i++) {
477 CD[j] += deltas[i + j * 2];
485 static inline void tm2_low_chroma(int *data, int stride, int *clast, unsigned *CD, int *deltas, int bx)
495 t = (int)(CD[0] + CD[1]) >> 1;
496 l = (int)(prev - CD[0] - CD[1] + clast[1]) >> 1;
497 CD[1] = CD[0] + CD[1] - t;
501 tm2_high_chroma(data, stride, clast, CD, deltas);
504 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
511 for (i = 0; i < 4; i++) {
512 deltas[i] = GET_TOK(ctx, TM2_C_HI);
513 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
515 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
516 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
519 for (i = 0; i < 16; i++)
520 deltas[i] = GET_TOK(ctx, TM2_L_HI);
522 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
525 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
532 deltas[0] = GET_TOK(ctx, TM2_C_LO);
533 deltas[1] = deltas[2] = deltas[3] = 0;
534 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
536 deltas[0] = GET_TOK(ctx, TM2_C_LO);
537 deltas[1] = deltas[2] = deltas[3] = 0;
538 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
541 for (i = 0; i < 16; i++)
542 deltas[i] = GET_TOK(ctx, TM2_L_HI);
544 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
547 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
555 deltas[0] = GET_TOK(ctx, TM2_C_LO);
556 deltas[1] = deltas[2] = deltas[3] = 0;
557 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
559 deltas[0] = GET_TOK(ctx, TM2_C_LO);
560 deltas[1] = deltas[2] = deltas[3] = 0;
561 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
564 for (i = 0; i < 16; i++)
567 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
568 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
569 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
570 deltas[10] = GET_TOK(ctx, TM2_L_LO);
573 last[0] = (int)((unsigned)last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
575 last[0] = (int)((unsigned)last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
576 last[2] = (int)((unsigned)last[1] + last[3]) >> 1;
578 t1 = ctx->D[0] + (unsigned)ctx->D[1];
580 ctx->D[1] = t1 - (t1 >> 1);
581 t2 = ctx->D[2] + (unsigned)ctx->D[3];
583 ctx->D[3] = t2 - (t2 >> 1);
585 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
588 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
592 unsigned left, right;
598 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
599 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
601 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
602 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
605 for (i = 0; i < 16; i++)
608 ct = (unsigned)ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
611 left = last[-1] - (unsigned)ct;
617 last[0] = left + (diff >> 2);
618 last[1] = left + (diff >> 1);
619 last[2] = right - (diff >> 2);
624 ctx->D[0] = (tp + (ct >> 2)) - left;
626 ctx->D[1] = (tp + (ct >> 1)) - left;
628 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
630 ctx->D[3] = (tp + ct) - left;
632 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
635 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
638 TM2_INIT_POINTERS_2();
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 ctx->D[0] = Yo[3] - last[3];
656 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
657 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
658 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
660 for (j = 0; j < 4; j++) {
661 for (i = 0; i < 4; i++) {
670 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
674 TM2_INIT_POINTERS_2();
677 for (j = 0; j < 2; j++) {
678 for (i = 0; i < 2; i++) {
679 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
680 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
689 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
690 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
693 ctx->D[0] = Yo[3] - last[3];
694 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
695 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
696 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
698 for (j = 0; j < 4; j++) {
700 for (i = 0; i < 4; i++) {
701 Y[i] = Yo[i] + (unsigned)GET_TOK(ctx, TM2_UPD);
704 ctx->D[j] = last[3] - d;
710 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
714 TM2_INIT_POINTERS_2();
716 mx = GET_TOK(ctx, TM2_MOT);
717 my = GET_TOK(ctx, TM2_MOT);
718 mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
719 my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
721 if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) {
722 av_log(ctx->avctx, AV_LOG_ERROR, "MV out of picture\n");
726 Yo += my * oYstride + mx;
727 Uo += (my >> 1) * oUstride + (mx >> 1);
728 Vo += (my >> 1) * oVstride + (mx >> 1);
731 for (j = 0; j < 2; j++) {
732 for (i = 0; i < 2; i++) {
743 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
744 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
747 for (j = 0; j < 4; j++) {
748 for (i = 0; i < 4; i++) {
754 /* calculate deltas */
756 ctx->D[0] = (unsigned)Y[3] - last[3];
757 ctx->D[1] = (unsigned)Y[3 + Ystride] - Y[3];
758 ctx->D[2] = (unsigned)Y[3 + Ystride * 2] - Y[3 + Ystride];
759 ctx->D[3] = (unsigned)Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
760 for (i = 0; i < 4; i++)
761 last[i] = Y[i + Ystride * 3];
764 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
767 int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
773 for (i = 0; i < TM2_NUM_STREAMS; i++)
774 ctx->tok_ptrs[i] = 0;
776 if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
777 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
778 return AVERROR_INVALIDDATA;
781 memset(ctx->last, 0, 4 * bw * sizeof(int));
782 memset(ctx->clast, 0, 4 * bw * sizeof(int));
784 for (j = 0; j < bh; j++) {
785 memset(ctx->D, 0, 4 * sizeof(int));
786 memset(ctx->CD, 0, 4 * sizeof(int));
787 for (i = 0; i < bw; i++) {
788 type = GET_TOK(ctx, TM2_TYPE);
791 tm2_hi_res_block(ctx, p, i, j);
794 tm2_med_res_block(ctx, p, i, j);
797 tm2_low_res_block(ctx, p, i, j);
800 tm2_null_res_block(ctx, p, i, j);
803 tm2_update_block(ctx, p, i, j);
807 tm2_still_block(ctx, p, i, j);
811 tm2_motion_block(ctx, p, i, j);
815 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
818 return AVERROR_INVALIDDATA;
822 /* copy data from our buffer to AVFrame */
823 Y = (ctx->cur?ctx->Y2:ctx->Y1);
824 U = (ctx->cur?ctx->U2:ctx->U1);
825 V = (ctx->cur?ctx->V2:ctx->V1);
827 for (j = 0; j < h; j++) {
828 for (i = 0; i < w; i++) {
829 unsigned y = Y[i], u = U[i >> 1], v = V[i >> 1];
830 dst[3*i+0] = av_clip_uint8(y + v);
831 dst[3*i+1] = av_clip_uint8(y);
832 dst[3*i+2] = av_clip_uint8(y + u);
835 /* horizontal edge extension */
836 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
837 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
839 /* vertical edge extension */
841 memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
842 memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
843 memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
844 memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
845 } else if (j == h - 1) {
846 memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
847 memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
848 memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
849 memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
854 /* horizontal edge extension */
855 U[-2] = U[-1] = U[0];
856 V[-2] = V[-1] = V[0];
857 U[cw + 1] = U[cw] = U[cw - 1];
858 V[cw + 1] = V[cw] = V[cw - 1];
860 /* vertical edge extension */
862 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
863 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
864 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
865 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
866 } else if (j == h - 1) {
867 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
868 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
869 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
870 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
876 dst += p->linesize[0];
882 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
883 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
886 #define TM2_HEADER_SIZE 40
888 static int decode_frame(AVCodecContext *avctx,
889 void *data, int *got_frame,
892 TM2Context * const l = avctx->priv_data;
893 const uint8_t *buf = avpkt->data;
894 int buf_size = avpkt->size & ~3;
895 AVFrame * const p = l->pic;
896 int offset = TM2_HEADER_SIZE;
901 av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size);
903 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
904 return AVERROR(ENOMEM);
907 if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
910 l->bdsp.bswap_buf((uint32_t *) l->buffer, (const uint32_t *) buf,
913 if ((ret = tm2_read_header(l, l->buffer)) < 0) {
917 for (i = 0; i < TM2_NUM_STREAMS; i++) {
918 if (offset >= buf_size) {
919 av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n");
920 return AVERROR_INVALIDDATA;
923 t = tm2_read_stream(l, l->buffer + offset, tm2_stream_order[i],
926 int j = tm2_stream_order[i];
928 memset(l->tokens[j], 0, sizeof(**l->tokens) * l->tok_lens[j]);
933 p->key_frame = tm2_decode_blocks(l, p);
935 p->pict_type = AV_PICTURE_TYPE_I;
937 p->pict_type = AV_PICTURE_TYPE_P;
941 ret = av_frame_ref(data, l->pic);
943 return (ret < 0) ? ret : buf_size;
946 static av_cold int decode_init(AVCodecContext *avctx)
948 TM2Context * const l = avctx->priv_data;
949 int w = avctx->width, h = avctx->height;
951 if ((avctx->width & 3) || (avctx->height & 3)) {
952 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
953 return AVERROR(EINVAL);
957 avctx->pix_fmt = AV_PIX_FMT_BGR24;
959 l->pic = av_frame_alloc();
961 return AVERROR(ENOMEM);
963 ff_bswapdsp_init(&l->bdsp);
965 l->last = av_malloc_array(w, 2 * sizeof(*l->last));
967 return AVERROR(ENOMEM);
968 l->clast = l->last + w;
972 l->Y_base = av_calloc(w * h, 2 * sizeof(*l->Y_base));
974 return AVERROR(ENOMEM);
976 l->Y1 = l->Y_base + l->y_stride * 4 + 4;
977 l->Y2 = l->Y1 + w * h;
980 l->UV_base = av_calloc(w * h, 4 * sizeof(*l->UV_base));
982 return AVERROR(ENOMEM);
984 l->U1 = l->UV_base + l->uv_stride * 2 + 2;
985 l->U2 = l->U1 + w * h;
986 l->V1 = l->U2 + w * h;
987 l->V2 = l->V1 + w * h;
992 static av_cold int decode_end(AVCodecContext *avctx)
994 TM2Context * const l = avctx->priv_data;
998 for (i = 0; i < TM2_NUM_STREAMS; i++)
999 av_freep(&l->tokens[i]);
1001 av_freep(&l->Y_base);
1002 av_freep(&l->UV_base);
1003 av_freep(&l->buffer);
1006 av_frame_free(&l->pic);
1011 const AVCodec ff_truemotion2_decoder = {
1012 .name = "truemotion2",
1013 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
1014 .type = AVMEDIA_TYPE_VIDEO,
1015 .id = AV_CODEC_ID_TRUEMOTION2,
1016 .priv_data_size = sizeof(TM2Context),
1017 .init = decode_init,
1018 .close = decode_end,
1019 .decode = decode_frame,
1020 .capabilities = AV_CODEC_CAP_DR1,
1021 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,