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.
28 #include "bytestream.h"
33 #define TM2_ESCAPE 0x80000000
36 /* Huffman-coded streams of different types of blocks */
59 typedef struct TM2Context {
60 AVCodecContext *avctx;
70 int *tokens[TM2_NUM_STREAMS];
71 int tok_lens[TM2_NUM_STREAMS];
72 int tok_ptrs[TM2_NUM_STREAMS];
73 int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
74 /* for blocks decoding */
80 /* data for current and previous frame */
81 int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
82 int *Y1, *U1, *V1, *Y2, *U2, *V2;
83 int y_stride, uv_stride;
88 * Huffman codes for each of streams
90 typedef struct TM2Codes {
91 VLC vlc; ///< table for FFmpeg bitstream reader
93 int *recode; ///< table for converting from code indexes to values
98 * structure for gathering Huffman codes information
100 typedef struct TM2Huff {
101 int val_bits; ///< length of literal
102 int max_bits; ///< maximum length of code
103 int min_bits; ///< minimum length of code
104 int nodes; ///< total number of nodes in tree
105 int num; ///< current number filled
106 int max_num; ///< total number of codes
107 int *nums; ///< literals
108 uint32_t *bits; ///< codes
109 int *lens; ///< codelengths
112 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
115 if (length > huff->max_bits) {
116 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
118 return AVERROR_INVALIDDATA;
121 if (!get_bits1(&ctx->gb)) { /* literal */
125 if (huff->num >= huff->max_num) {
126 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
127 return AVERROR_INVALIDDATA;
129 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
130 huff->bits[huff->num] = prefix;
131 huff->lens[huff->num] = length;
134 } else { /* non-terminal node */
135 if ((ret = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
137 if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
143 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
148 huff.val_bits = get_bits(&ctx->gb, 5);
149 huff.max_bits = get_bits(&ctx->gb, 5);
150 huff.min_bits = get_bits(&ctx->gb, 5);
151 huff.nodes = get_bits_long(&ctx->gb, 17);
154 /* check for correct codes parameters */
155 if ((huff.val_bits < 1) || (huff.val_bits > 32) ||
156 (huff.max_bits < 0) || (huff.max_bits > 25)) {
157 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal "
158 "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits);
159 return AVERROR_INVALIDDATA;
161 if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
162 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree "
163 "nodes: %i\n", huff.nodes);
164 return AVERROR_INVALIDDATA;
167 if (huff.max_bits == 0)
170 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
171 huff.max_num = (huff.nodes + 1) >> 1;
172 huff.nums = av_calloc(huff.max_num, sizeof(int));
173 huff.bits = av_calloc(huff.max_num, sizeof(uint32_t));
174 huff.lens = av_calloc(huff.max_num, sizeof(int));
176 if (!huff.nums || !huff.bits || !huff.lens) {
177 res = AVERROR(ENOMEM);
181 res = tm2_read_tree(ctx, 0, 0, &huff);
183 if (huff.num != huff.max_num) {
184 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
185 huff.num, huff.max_num);
186 res = AVERROR_INVALIDDATA;
189 /* convert codes to vlc_table */
193 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
194 huff.lens, sizeof(int), sizeof(int),
195 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
197 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
199 code->bits = huff.max_bits;
200 code->length = huff.max_num;
201 code->recode = av_malloc_array(code->length, sizeof(int));
203 res = AVERROR(ENOMEM);
206 for (i = 0; i < code->length; i++)
207 code->recode[i] = huff.nums[i];
211 /* free allocated memory */
219 static void tm2_free_codes(TM2Codes *code)
221 av_free(code->recode);
223 ff_free_vlc(&code->vlc);
226 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
229 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
232 return code->recode[val];
235 #define TM2_OLD_HEADER_MAGIC 0x00000100
236 #define TM2_NEW_HEADER_MAGIC 0x00000101
238 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
240 uint32_t magic = AV_RL32(buf);
243 case TM2_OLD_HEADER_MAGIC:
244 avpriv_request_sample(ctx->avctx, "Old TM2 header");
246 case TM2_NEW_HEADER_MAGIC:
249 av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
250 return AVERROR_INVALIDDATA;
254 static int tm2_read_deltas(TM2Context *ctx, int stream_id)
259 d = get_bits(&ctx->gb, 9);
260 mb = get_bits(&ctx->gb, 5);
262 if ((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
263 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
264 return AVERROR_INVALIDDATA;
267 for (i = 0; i < d; i++) {
268 v = get_bits_long(&ctx->gb, mb);
269 if (v & (1 << (mb - 1)))
270 ctx->deltas[stream_id][i] = v - (1 << mb);
272 ctx->deltas[stream_id][i] = v;
274 for (; i < TM2_DELTAS; i++)
275 ctx->deltas[stream_id][i] = 0;
280 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
289 av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n");
290 return AVERROR_INVALIDDATA;
293 /* get stream length in dwords */
294 bytestream2_init(&gb, buf, buf_size);
295 len = bytestream2_get_be32(&gb);
301 if (len >= INT_MAX/4-1 || len < 0 || skip > buf_size) {
302 av_log(ctx->avctx, AV_LOG_ERROR, "invalid stream size\n");
303 return AVERROR_INVALIDDATA;
306 toks = bytestream2_get_be32(&gb);
308 len = bytestream2_get_be32(&gb);
309 if (len == TM2_ESCAPE) {
310 len = bytestream2_get_be32(&gb);
313 pos = bytestream2_tell(&gb);
315 return AVERROR_INVALIDDATA;
316 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
317 if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
319 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
322 /* skip unused fields */
323 len = bytestream2_get_be32(&gb);
324 if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
325 bytestream2_skip(&gb, 8); /* unused by decoder */
327 bytestream2_skip(&gb, 4); /* unused by decoder */
330 pos = bytestream2_tell(&gb);
332 return AVERROR_INVALIDDATA;
333 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
334 if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
336 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
339 /* check if we have sane number of tokens */
340 if ((toks < 0) || (toks > 0xFFFFFF)) {
341 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
342 tm2_free_codes(&codes);
343 return AVERROR_INVALIDDATA;
345 ret = av_reallocp_array(&ctx->tokens[stream_id], toks, sizeof(int));
347 ctx->tok_lens[stream_id] = 0;
350 ctx->tok_lens[stream_id] = toks;
351 len = bytestream2_get_be32(&gb);
353 pos = bytestream2_tell(&gb);
355 return AVERROR_INVALIDDATA;
356 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
357 for (i = 0; i < toks; i++) {
358 if (get_bits_left(&ctx->gb) <= 0) {
359 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
360 return AVERROR_INVALIDDATA;
362 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
363 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS || ctx->tokens[stream_id][i]<0) {
364 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
365 ctx->tokens[stream_id][i], stream_id, i);
366 return AVERROR_INVALIDDATA;
370 for (i = 0; i < toks; i++) {
371 ctx->tokens[stream_id][i] = codes.recode[0];
372 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
373 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
374 ctx->tokens[stream_id][i], stream_id, i);
375 return AVERROR_INVALIDDATA;
379 tm2_free_codes(&codes);
384 static inline int GET_TOK(TM2Context *ctx,int type)
386 if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
387 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]);
390 if (type <= TM2_MOT) {
391 if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) {
392 av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]);
395 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
397 return ctx->tokens[type][ctx->tok_ptrs[type]++];
400 /* blocks decoding routines */
402 /* common Y, U, V pointers initialisation */
403 #define TM2_INIT_POINTERS() \
406 int Ystride, Ustride, Vstride;\
408 Ystride = ctx->y_stride;\
409 Vstride = ctx->uv_stride;\
410 Ustride = ctx->uv_stride;\
411 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
412 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
413 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
414 last = ctx->last + bx * 4;\
415 clast = ctx->clast + bx * 4;
417 #define TM2_INIT_POINTERS_2() \
419 int oYstride, oUstride, oVstride;\
421 TM2_INIT_POINTERS();\
425 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
426 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
427 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
429 /* recalculate last and delta values for next blocks */
430 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
431 CD[0] = CHR[1] - last[1];\
432 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
433 last[0] = (int)CHR[stride + 0];\
434 last[1] = (int)CHR[stride + 1];}
436 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
437 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
442 for (j = 0; j < 4; j++){
444 for (i = 0; i < 4; i++){
445 d = deltas[i + j * 4];
448 Y[i] = av_clip_uint8(last[i]);
455 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
458 for (j = 0; j < 2; j++) {
459 for (i = 0; i < 2; i++) {
460 CD[j] += deltas[i + j * 2];
468 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
478 t = (CD[0] + CD[1]) >> 1;
479 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
480 CD[1] = CD[0] + CD[1] - t;
484 tm2_high_chroma(data, stride, clast, CD, deltas);
487 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
494 for (i = 0; i < 4; i++) {
495 deltas[i] = GET_TOK(ctx, TM2_C_HI);
496 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
498 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
499 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
502 for (i = 0; i < 16; i++)
503 deltas[i] = GET_TOK(ctx, TM2_L_HI);
505 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
508 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
515 deltas[0] = GET_TOK(ctx, TM2_C_LO);
516 deltas[1] = deltas[2] = deltas[3] = 0;
517 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
519 deltas[0] = GET_TOK(ctx, TM2_C_LO);
520 deltas[1] = deltas[2] = deltas[3] = 0;
521 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
524 for (i = 0; i < 16; i++)
525 deltas[i] = GET_TOK(ctx, TM2_L_HI);
527 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
530 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
538 deltas[0] = GET_TOK(ctx, TM2_C_LO);
539 deltas[1] = deltas[2] = deltas[3] = 0;
540 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
542 deltas[0] = GET_TOK(ctx, TM2_C_LO);
543 deltas[1] = deltas[2] = deltas[3] = 0;
544 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
547 for (i = 0; i < 16; i++)
550 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
551 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
552 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
553 deltas[10] = GET_TOK(ctx, TM2_L_LO);
556 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
558 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
559 last[2] = (last[1] + last[3]) >> 1;
561 t1 = ctx->D[0] + ctx->D[1];
563 ctx->D[1] = t1 - (t1 >> 1);
564 t2 = ctx->D[2] + ctx->D[3];
566 ctx->D[3] = t2 - (t2 >> 1);
568 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
571 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
575 int left, right, diff;
580 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
581 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
583 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
584 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
587 for (i = 0; i < 16; i++)
590 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
593 left = last[-1] - ct;
599 last[0] = left + (diff >> 2);
600 last[1] = left + (diff >> 1);
601 last[2] = right - (diff >> 2);
606 ctx->D[0] = (tp + (ct >> 2)) - left;
608 ctx->D[1] = (tp + (ct >> 1)) - left;
610 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
612 ctx->D[3] = (tp + ct) - left;
614 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
617 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
620 TM2_INIT_POINTERS_2();
623 for (j = 0; j < 2; j++) {
624 for (i = 0; i < 2; i++){
628 U += Ustride; V += Vstride;
629 Uo += oUstride; Vo += oVstride;
633 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
634 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
637 ctx->D[0] = Yo[3] - last[3];
638 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
639 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
640 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
642 for (j = 0; j < 4; j++) {
643 for (i = 0; i < 4; i++) {
652 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
656 TM2_INIT_POINTERS_2();
659 for (j = 0; j < 2; j++) {
660 for (i = 0; i < 2; i++) {
661 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
662 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
671 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
672 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
675 ctx->D[0] = Yo[3] - last[3];
676 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
677 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
678 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
680 for (j = 0; j < 4; j++) {
682 for (i = 0; i < 4; i++) {
683 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
686 ctx->D[j] = last[3] - d;
692 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
696 TM2_INIT_POINTERS_2();
698 mx = GET_TOK(ctx, TM2_MOT);
699 my = GET_TOK(ctx, TM2_MOT);
700 mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
701 my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
703 if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) {
704 av_log(ctx->avctx, AV_LOG_ERROR, "MV out of picture\n");
708 Yo += my * oYstride + mx;
709 Uo += (my >> 1) * oUstride + (mx >> 1);
710 Vo += (my >> 1) * oVstride + (mx >> 1);
713 for (j = 0; j < 2; j++) {
714 for (i = 0; i < 2; i++) {
725 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
726 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
729 for (j = 0; j < 4; j++) {
730 for (i = 0; i < 4; i++) {
736 /* calculate deltas */
738 ctx->D[0] = Y[3] - last[3];
739 ctx->D[1] = Y[3 + Ystride] - Y[3];
740 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
741 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
742 for (i = 0; i < 4; i++)
743 last[i] = Y[i + Ystride * 3];
746 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
749 int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
755 for (i = 0; i < TM2_NUM_STREAMS; i++)
756 ctx->tok_ptrs[i] = 0;
758 if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
759 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
760 return AVERROR_INVALIDDATA;
763 memset(ctx->last, 0, 4 * bw * sizeof(int));
764 memset(ctx->clast, 0, 4 * bw * sizeof(int));
766 for (j = 0; j < bh; j++) {
767 memset(ctx->D, 0, 4 * sizeof(int));
768 memset(ctx->CD, 0, 4 * sizeof(int));
769 for (i = 0; i < bw; i++) {
770 type = GET_TOK(ctx, TM2_TYPE);
773 tm2_hi_res_block(ctx, p, i, j);
776 tm2_med_res_block(ctx, p, i, j);
779 tm2_low_res_block(ctx, p, i, j);
782 tm2_null_res_block(ctx, p, i, j);
785 tm2_update_block(ctx, p, i, j);
789 tm2_still_block(ctx, p, i, j);
793 tm2_motion_block(ctx, p, i, j);
797 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
802 /* copy data from our buffer to AVFrame */
803 Y = (ctx->cur?ctx->Y2:ctx->Y1);
804 U = (ctx->cur?ctx->U2:ctx->U1);
805 V = (ctx->cur?ctx->V2:ctx->V1);
807 for (j = 0; j < h; j++) {
808 for (i = 0; i < w; i++) {
809 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
810 dst[3*i+0] = av_clip_uint8(y + v);
811 dst[3*i+1] = av_clip_uint8(y);
812 dst[3*i+2] = av_clip_uint8(y + u);
815 /* horizontal edge extension */
816 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
817 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
819 /* vertical edge extension */
821 memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
822 memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
823 memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
824 memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
825 } else if (j == h - 1) {
826 memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
827 memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
828 memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
829 memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
834 /* horizontal edge extension */
835 U[-2] = U[-1] = U[0];
836 V[-2] = V[-1] = V[0];
837 U[cw + 1] = U[cw] = U[cw - 1];
838 V[cw + 1] = V[cw] = V[cw - 1];
840 /* vertical edge extension */
842 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
843 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
844 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
845 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
846 } else if (j == h - 1) {
847 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
848 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
849 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
850 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
856 dst += p->linesize[0];
862 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
863 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
866 #define TM2_HEADER_SIZE 40
868 static int decode_frame(AVCodecContext *avctx,
869 void *data, int *got_frame,
872 TM2Context * const l = avctx->priv_data;
873 const uint8_t *buf = avpkt->data;
874 int buf_size = avpkt->size & ~3;
875 AVFrame * const p = l->pic;
876 int offset = TM2_HEADER_SIZE;
879 av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size);
881 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
882 return AVERROR(ENOMEM);
885 if ((ret = ff_reget_buffer(avctx, p)) < 0)
888 l->dsp.bswap_buf((uint32_t*)l->buffer, (const uint32_t*)buf, buf_size >> 2);
890 if ((ret = tm2_read_header(l, l->buffer)) < 0) {
894 for (i = 0; i < TM2_NUM_STREAMS; i++) {
895 if (offset >= buf_size) {
896 av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n");
897 return AVERROR_INVALIDDATA;
900 t = tm2_read_stream(l, l->buffer + offset, tm2_stream_order[i],
903 int j = tm2_stream_order[i];
904 memset(l->tokens[j], 0, sizeof(**l->tokens) * l->tok_lens[j]);
909 p->key_frame = tm2_decode_blocks(l, p);
911 p->pict_type = AV_PICTURE_TYPE_I;
913 p->pict_type = AV_PICTURE_TYPE_P;
917 ret = av_frame_ref(data, l->pic);
919 return (ret < 0) ? ret : buf_size;
922 static av_cold int decode_init(AVCodecContext *avctx)
924 TM2Context * const l = avctx->priv_data;
925 int i, w = avctx->width, h = avctx->height;
927 if ((avctx->width & 3) || (avctx->height & 3)) {
928 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
929 return AVERROR(EINVAL);
933 avctx->pix_fmt = AV_PIX_FMT_BGR24;
935 l->pic = av_frame_alloc();
937 return AVERROR(ENOMEM);
939 ff_dsputil_init(&l->dsp, avctx);
941 l->last = av_malloc_array(w >> 2, 4 * sizeof(*l->last) );
942 l->clast = av_malloc_array(w >> 2, 4 * sizeof(*l->clast));
944 for (i = 0; i < TM2_NUM_STREAMS; i++) {
951 l->Y1_base = av_calloc(w * h, sizeof(*l->Y1_base));
952 l->Y2_base = av_calloc(w * h, sizeof(*l->Y2_base));
956 l->U1_base = av_calloc(w * h, sizeof(*l->U1_base));
957 l->V1_base = av_calloc(w * h, sizeof(*l->V1_base));
958 l->U2_base = av_calloc(w * h, sizeof(*l->U2_base));
959 l->V2_base = av_calloc(w * h, sizeof(*l->V1_base));
962 if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
963 !l->V1_base || !l->U2_base || !l->V2_base ||
964 !l->last || !l->clast) {
965 av_freep(&l->Y1_base);
966 av_freep(&l->Y2_base);
967 av_freep(&l->U1_base);
968 av_freep(&l->U2_base);
969 av_freep(&l->V1_base);
970 av_freep(&l->V2_base);
973 av_frame_free(&l->pic);
974 return AVERROR(ENOMEM);
976 l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
977 l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
978 l->U1 = l->U1_base + l->uv_stride * 2 + 2;
979 l->U2 = l->U2_base + l->uv_stride * 2 + 2;
980 l->V1 = l->V1_base + l->uv_stride * 2 + 2;
981 l->V2 = l->V2_base + l->uv_stride * 2 + 2;
986 static av_cold int decode_end(AVCodecContext *avctx)
988 TM2Context * const l = avctx->priv_data;
993 for (i = 0; i < TM2_NUM_STREAMS; i++)
994 av_free(l->tokens[i]);
1000 av_free(l->U2_base);
1001 av_free(l->V2_base);
1003 av_freep(&l->buffer);
1006 av_frame_free(&l->pic);
1011 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 = CODEC_CAP_DR1,