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;
72 int *tokens[TM2_NUM_STREAMS];
73 int tok_lens[TM2_NUM_STREAMS];
74 int tok_ptrs[TM2_NUM_STREAMS];
75 int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
76 /* for blocks decoding */
82 /* data for current and previous frame */
83 int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
84 int *Y1, *U1, *V1, *Y2, *U2, *V2;
85 int y_stride, uv_stride;
90 * Huffman codes for each of streams
92 typedef struct TM2Codes {
93 VLC vlc; ///< table for FFmpeg bitstream reader
95 int *recode; ///< table for converting from code indexes to values
100 * structure for gathering Huffman codes information
102 typedef struct TM2Huff {
103 int val_bits; ///< length of literal
104 int max_bits; ///< maximum length of code
105 int min_bits; ///< minimum length of code
106 int nodes; ///< total number of nodes in tree
107 int num; ///< current number filled
108 int max_num; ///< total number of codes
109 int *nums; ///< literals
110 uint32_t *bits; ///< codes
111 int *lens; ///< codelengths
114 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
117 if (length > huff->max_bits) {
118 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
120 return AVERROR_INVALIDDATA;
123 if (!get_bits1(&ctx->gb)) { /* literal */
127 if (huff->num >= huff->max_num) {
128 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
129 return AVERROR_INVALIDDATA;
131 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
132 huff->bits[huff->num] = prefix;
133 huff->lens[huff->num] = length;
136 } else { /* non-terminal node */
137 if ((ret = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
139 if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
145 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
150 huff.val_bits = get_bits(&ctx->gb, 5);
151 huff.max_bits = get_bits(&ctx->gb, 5);
152 huff.min_bits = get_bits(&ctx->gb, 5);
153 huff.nodes = get_bits_long(&ctx->gb, 17);
156 /* check for correct codes parameters */
157 if ((huff.val_bits < 1) || (huff.val_bits > 32) ||
158 (huff.max_bits < 0) || (huff.max_bits > 25)) {
159 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal "
160 "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits);
161 return AVERROR_INVALIDDATA;
163 if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
164 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree "
165 "nodes: %i\n", huff.nodes);
166 return AVERROR_INVALIDDATA;
169 if (huff.max_bits == 0)
172 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
173 huff.max_num = (huff.nodes + 1) >> 1;
174 huff.nums = av_calloc(huff.max_num, sizeof(int));
175 huff.bits = av_calloc(huff.max_num, sizeof(uint32_t));
176 huff.lens = av_calloc(huff.max_num, sizeof(int));
178 if (!huff.nums || !huff.bits || !huff.lens) {
179 res = AVERROR(ENOMEM);
183 res = tm2_read_tree(ctx, 0, 0, &huff);
185 if (huff.num != huff.max_num) {
186 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
187 huff.num, huff.max_num);
188 res = AVERROR_INVALIDDATA;
191 /* convert codes to vlc_table */
195 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
196 huff.lens, sizeof(int), sizeof(int),
197 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
199 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
201 code->bits = huff.max_bits;
202 code->length = huff.max_num;
203 code->recode = av_malloc_array(code->length, sizeof(int));
205 res = AVERROR(ENOMEM);
208 for (i = 0; i < code->length; i++)
209 code->recode[i] = huff.nums[i];
214 /* free allocated memory */
222 static void tm2_free_codes(TM2Codes *code)
224 av_free(code->recode);
226 ff_free_vlc(&code->vlc);
229 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
232 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
235 return code->recode[val];
238 #define TM2_OLD_HEADER_MAGIC 0x00000100
239 #define TM2_NEW_HEADER_MAGIC 0x00000101
241 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
243 uint32_t magic = AV_RL32(buf);
246 case TM2_OLD_HEADER_MAGIC:
247 avpriv_request_sample(ctx->avctx, "Old TM2 header");
249 case TM2_NEW_HEADER_MAGIC:
252 av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n",
254 return AVERROR_INVALIDDATA;
258 static int tm2_read_deltas(TM2Context *ctx, int stream_id)
263 d = get_bits(&ctx->gb, 9);
264 mb = get_bits(&ctx->gb, 5);
267 if ((d < 1) || (d > TM2_DELTAS) || (mb < 1)) {
268 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
269 return AVERROR_INVALIDDATA;
272 for (i = 0; i < d; i++) {
273 v = get_bits_long(&ctx->gb, mb);
274 if (v & (1 << (mb - 1)))
275 ctx->deltas[stream_id][i] = v - (1 << mb);
277 ctx->deltas[stream_id][i] = v;
279 for (; i < TM2_DELTAS; i++)
280 ctx->deltas[stream_id][i] = 0;
285 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
294 av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n");
295 return AVERROR_INVALIDDATA;
298 /* get stream length in dwords */
299 bytestream2_init(&gb, buf, buf_size);
300 len = bytestream2_get_be32(&gb);
306 if (len >= INT_MAX / 4 - 1 || len < 0 || skip > buf_size) {
307 av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
308 return AVERROR_INVALIDDATA;
311 toks = bytestream2_get_be32(&gb);
313 len = bytestream2_get_be32(&gb);
314 if (len == TM2_ESCAPE) {
315 len = bytestream2_get_be32(&gb);
318 pos = bytestream2_tell(&gb);
320 return AVERROR_INVALIDDATA;
321 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
322 if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
324 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
327 /* skip unused fields */
328 len = bytestream2_get_be32(&gb);
329 if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
330 bytestream2_skip(&gb, 8); /* unused by decoder */
332 bytestream2_skip(&gb, 4); /* unused by decoder */
335 pos = bytestream2_tell(&gb);
337 return AVERROR_INVALIDDATA;
338 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
339 if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
341 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
344 /* check if we have sane number of tokens */
345 if ((toks < 0) || (toks > 0xFFFFFF)) {
346 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
347 ret = AVERROR_INVALIDDATA;
350 ret = av_reallocp_array(&ctx->tokens[stream_id], toks, sizeof(int));
352 ctx->tok_lens[stream_id] = 0;
355 ctx->tok_lens[stream_id] = toks;
356 len = bytestream2_get_be32(&gb);
358 pos = bytestream2_tell(&gb);
360 ret = AVERROR_INVALIDDATA;
363 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
364 for (i = 0; i < toks; i++) {
365 if (get_bits_left(&ctx->gb) <= 0) {
366 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
367 ret = AVERROR_INVALIDDATA;
370 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
371 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS || ctx->tokens[stream_id][i]<0) {
372 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
373 ctx->tokens[stream_id][i], stream_id, i);
374 ret = AVERROR_INVALIDDATA;
379 for (i = 0; i < toks; i++) {
380 ctx->tokens[stream_id][i] = codes.recode[0];
381 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
382 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
383 ctx->tokens[stream_id][i], stream_id, i);
384 ret = AVERROR_INVALIDDATA;
393 tm2_free_codes(&codes);
397 static inline int GET_TOK(TM2Context *ctx,int type)
399 if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
400 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]);
403 if (type <= TM2_MOT) {
404 if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) {
405 av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]);
408 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
410 return ctx->tokens[type][ctx->tok_ptrs[type]++];
413 /* blocks decoding routines */
415 /* common Y, U, V pointers initialisation */
416 #define TM2_INIT_POINTERS() \
419 int Ystride, Ustride, Vstride;\
421 Ystride = ctx->y_stride;\
422 Vstride = ctx->uv_stride;\
423 Ustride = ctx->uv_stride;\
424 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
425 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
426 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
427 last = ctx->last + bx * 4;\
428 clast = ctx->clast + bx * 4;
430 #define TM2_INIT_POINTERS_2() \
432 int oYstride, oUstride, oVstride;\
434 TM2_INIT_POINTERS();\
438 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
439 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
440 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
442 /* recalculate last and delta values for next blocks */
443 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
444 CD[0] = CHR[1] - last[1];\
445 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
446 last[0] = (int)CHR[stride + 0];\
447 last[1] = (int)CHR[stride + 1];}
449 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
450 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
455 for (j = 0; j < 4; j++){
457 for (i = 0; i < 4; i++){
458 d = deltas[i + j * 4];
461 Y[i] = av_clip_uint8(last[i]);
468 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
471 for (j = 0; j < 2; j++) {
472 for (i = 0; i < 2; i++) {
473 CD[j] += deltas[i + j * 2];
481 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
491 t = (CD[0] + CD[1]) >> 1;
492 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
493 CD[1] = CD[0] + CD[1] - t;
497 tm2_high_chroma(data, stride, clast, CD, deltas);
500 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
507 for (i = 0; i < 4; i++) {
508 deltas[i] = GET_TOK(ctx, TM2_C_HI);
509 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
511 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
512 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
515 for (i = 0; i < 16; i++)
516 deltas[i] = GET_TOK(ctx, TM2_L_HI);
518 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
521 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
528 deltas[0] = GET_TOK(ctx, TM2_C_LO);
529 deltas[1] = deltas[2] = deltas[3] = 0;
530 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
532 deltas[0] = GET_TOK(ctx, TM2_C_LO);
533 deltas[1] = deltas[2] = deltas[3] = 0;
534 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
537 for (i = 0; i < 16; i++)
538 deltas[i] = GET_TOK(ctx, TM2_L_HI);
540 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
543 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
551 deltas[0] = GET_TOK(ctx, TM2_C_LO);
552 deltas[1] = deltas[2] = deltas[3] = 0;
553 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
555 deltas[0] = GET_TOK(ctx, TM2_C_LO);
556 deltas[1] = deltas[2] = deltas[3] = 0;
557 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
560 for (i = 0; i < 16; i++)
563 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
564 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
565 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
566 deltas[10] = GET_TOK(ctx, TM2_L_LO);
569 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
571 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
572 last[2] = (last[1] + last[3]) >> 1;
574 t1 = ctx->D[0] + ctx->D[1];
576 ctx->D[1] = t1 - (t1 >> 1);
577 t2 = ctx->D[2] + ctx->D[3];
579 ctx->D[3] = t2 - (t2 >> 1);
581 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
584 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
588 int left, right, diff;
593 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
594 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
596 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
597 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
600 for (i = 0; i < 16; i++)
603 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
606 left = last[-1] - ct;
612 last[0] = left + (diff >> 2);
613 last[1] = left + (diff >> 1);
614 last[2] = right - (diff >> 2);
619 ctx->D[0] = (tp + (ct >> 2)) - left;
621 ctx->D[1] = (tp + (ct >> 1)) - left;
623 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
625 ctx->D[3] = (tp + ct) - left;
627 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
630 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
633 TM2_INIT_POINTERS_2();
636 for (j = 0; j < 2; j++) {
637 for (i = 0; i < 2; i++){
641 U += Ustride; V += Vstride;
642 Uo += oUstride; Vo += oVstride;
646 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
647 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
650 ctx->D[0] = Yo[3] - last[3];
651 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
652 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
653 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
655 for (j = 0; j < 4; j++) {
656 for (i = 0; i < 4; i++) {
665 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
669 TM2_INIT_POINTERS_2();
672 for (j = 0; j < 2; j++) {
673 for (i = 0; i < 2; i++) {
674 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
675 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
684 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
685 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
688 ctx->D[0] = Yo[3] - last[3];
689 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
690 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
691 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
693 for (j = 0; j < 4; j++) {
695 for (i = 0; i < 4; i++) {
696 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
699 ctx->D[j] = last[3] - d;
705 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
709 TM2_INIT_POINTERS_2();
711 mx = GET_TOK(ctx, TM2_MOT);
712 my = GET_TOK(ctx, TM2_MOT);
713 mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
714 my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
716 if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) {
717 av_log(ctx->avctx, AV_LOG_ERROR, "MV out of picture\n");
721 Yo += my * oYstride + mx;
722 Uo += (my >> 1) * oUstride + (mx >> 1);
723 Vo += (my >> 1) * oVstride + (mx >> 1);
726 for (j = 0; j < 2; j++) {
727 for (i = 0; i < 2; i++) {
738 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
739 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
742 for (j = 0; j < 4; j++) {
743 for (i = 0; i < 4; i++) {
749 /* calculate deltas */
751 ctx->D[0] = Y[3] - last[3];
752 ctx->D[1] = Y[3 + Ystride] - Y[3];
753 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
754 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
755 for (i = 0; i < 4; i++)
756 last[i] = Y[i + Ystride * 3];
759 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
762 int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
768 for (i = 0; i < TM2_NUM_STREAMS; i++)
769 ctx->tok_ptrs[i] = 0;
771 if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
772 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
773 return AVERROR_INVALIDDATA;
776 memset(ctx->last, 0, 4 * bw * sizeof(int));
777 memset(ctx->clast, 0, 4 * bw * sizeof(int));
779 for (j = 0; j < bh; j++) {
780 memset(ctx->D, 0, 4 * sizeof(int));
781 memset(ctx->CD, 0, 4 * sizeof(int));
782 for (i = 0; i < bw; i++) {
783 type = GET_TOK(ctx, TM2_TYPE);
786 tm2_hi_res_block(ctx, p, i, j);
789 tm2_med_res_block(ctx, p, i, j);
792 tm2_low_res_block(ctx, p, i, j);
795 tm2_null_res_block(ctx, p, i, j);
798 tm2_update_block(ctx, p, i, j);
802 tm2_still_block(ctx, p, i, j);
806 tm2_motion_block(ctx, p, i, j);
810 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
815 /* copy data from our buffer to AVFrame */
816 Y = (ctx->cur?ctx->Y2:ctx->Y1);
817 U = (ctx->cur?ctx->U2:ctx->U1);
818 V = (ctx->cur?ctx->V2:ctx->V1);
820 for (j = 0; j < h; j++) {
821 for (i = 0; i < w; i++) {
822 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
823 dst[3*i+0] = av_clip_uint8(y + v);
824 dst[3*i+1] = av_clip_uint8(y);
825 dst[3*i+2] = av_clip_uint8(y + u);
828 /* horizontal edge extension */
829 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
830 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
832 /* vertical edge extension */
834 memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
835 memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
836 memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
837 memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
838 } else if (j == h - 1) {
839 memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
840 memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
841 memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
842 memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
847 /* horizontal edge extension */
848 U[-2] = U[-1] = U[0];
849 V[-2] = V[-1] = V[0];
850 U[cw + 1] = U[cw] = U[cw - 1];
851 V[cw + 1] = V[cw] = V[cw - 1];
853 /* vertical edge extension */
855 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
856 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
857 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
858 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
859 } else if (j == h - 1) {
860 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
861 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
862 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
863 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
869 dst += p->linesize[0];
875 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
876 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
879 #define TM2_HEADER_SIZE 40
881 static int decode_frame(AVCodecContext *avctx,
882 void *data, int *got_frame,
885 TM2Context * const l = avctx->priv_data;
886 const uint8_t *buf = avpkt->data;
887 int buf_size = avpkt->size & ~3;
888 AVFrame * const p = l->pic;
889 int offset = TM2_HEADER_SIZE;
892 av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size);
894 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
895 return AVERROR(ENOMEM);
898 if ((ret = ff_reget_buffer(avctx, p)) < 0)
901 l->bdsp.bswap_buf((uint32_t *) l->buffer, (const uint32_t *) buf,
904 if ((ret = tm2_read_header(l, l->buffer)) < 0) {
908 for (i = 0; i < TM2_NUM_STREAMS; i++) {
909 if (offset >= buf_size) {
910 av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n");
911 return AVERROR_INVALIDDATA;
914 t = tm2_read_stream(l, l->buffer + offset, tm2_stream_order[i],
917 int j = tm2_stream_order[i];
918 memset(l->tokens[j], 0, sizeof(**l->tokens) * l->tok_lens[j]);
923 p->key_frame = tm2_decode_blocks(l, p);
925 p->pict_type = AV_PICTURE_TYPE_I;
927 p->pict_type = AV_PICTURE_TYPE_P;
931 ret = av_frame_ref(data, l->pic);
933 return (ret < 0) ? ret : buf_size;
936 static av_cold int decode_init(AVCodecContext *avctx)
938 TM2Context * const l = avctx->priv_data;
939 int i, w = avctx->width, h = avctx->height;
941 if ((avctx->width & 3) || (avctx->height & 3)) {
942 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
943 return AVERROR(EINVAL);
947 avctx->pix_fmt = AV_PIX_FMT_BGR24;
949 l->pic = av_frame_alloc();
951 return AVERROR(ENOMEM);
953 ff_bswapdsp_init(&l->bdsp);
955 l->last = av_malloc_array(w >> 2, 4 * sizeof(*l->last) );
956 l->clast = av_malloc_array(w >> 2, 4 * sizeof(*l->clast));
958 for (i = 0; i < TM2_NUM_STREAMS; i++) {
965 l->Y1_base = av_calloc(w * h, sizeof(*l->Y1_base));
966 l->Y2_base = av_calloc(w * h, sizeof(*l->Y2_base));
970 l->U1_base = av_calloc(w * h, sizeof(*l->U1_base));
971 l->V1_base = av_calloc(w * h, sizeof(*l->V1_base));
972 l->U2_base = av_calloc(w * h, sizeof(*l->U2_base));
973 l->V2_base = av_calloc(w * h, sizeof(*l->V1_base));
976 if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
977 !l->V1_base || !l->U2_base || !l->V2_base ||
978 !l->last || !l->clast) {
979 av_freep(&l->Y1_base);
980 av_freep(&l->Y2_base);
981 av_freep(&l->U1_base);
982 av_freep(&l->U2_base);
983 av_freep(&l->V1_base);
984 av_freep(&l->V2_base);
987 av_frame_free(&l->pic);
988 return AVERROR(ENOMEM);
990 l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
991 l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
992 l->U1 = l->U1_base + l->uv_stride * 2 + 2;
993 l->U2 = l->U2_base + l->uv_stride * 2 + 2;
994 l->V1 = l->V1_base + l->uv_stride * 2 + 2;
995 l->V2 = l->V2_base + l->uv_stride * 2 + 2;
1000 static av_cold int decode_end(AVCodecContext *avctx)
1002 TM2Context * const l = avctx->priv_data;
1007 for (i = 0; i < TM2_NUM_STREAMS; i++)
1008 av_freep(&l->tokens[i]);
1010 av_freep(&l->Y1_base);
1011 av_freep(&l->U1_base);
1012 av_freep(&l->V1_base);
1013 av_freep(&l->Y2_base);
1014 av_freep(&l->U2_base);
1015 av_freep(&l->V2_base);
1017 av_freep(&l->buffer);
1020 av_frame_free(&l->pic);
1025 AVCodec ff_truemotion2_decoder = {
1026 .name = "truemotion2",
1027 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
1028 .type = AVMEDIA_TYPE_VIDEO,
1029 .id = AV_CODEC_ID_TRUEMOTION2,
1030 .priv_data_size = sizeof(TM2Context),
1031 .init = decode_init,
1032 .close = decode_end,
1033 .decode = decode_frame,
1034 .capabilities = AV_CODEC_CAP_DR1,