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_mallocz(huff.max_num * sizeof(int));
173 huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
174 huff.lens = av_mallocz(huff.max_num * sizeof(int));
176 res = tm2_read_tree(ctx, 0, 0, &huff);
178 if (huff.num != huff.max_num) {
179 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
180 huff.num, huff.max_num);
181 res = AVERROR_INVALIDDATA;
184 /* convert codes to vlc_table */
188 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
189 huff.lens, sizeof(int), sizeof(int),
190 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
192 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
194 code->bits = huff.max_bits;
195 code->length = huff.max_num;
196 code->recode = av_malloc(code->length * sizeof(int));
197 for (i = 0; i < code->length; i++)
198 code->recode[i] = huff.nums[i];
201 /* free allocated memory */
209 static void tm2_free_codes(TM2Codes *code)
211 av_free(code->recode);
213 ff_free_vlc(&code->vlc);
216 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
219 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
222 return code->recode[val];
225 #define TM2_OLD_HEADER_MAGIC 0x00000100
226 #define TM2_NEW_HEADER_MAGIC 0x00000101
228 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
230 uint32_t magic = AV_RL32(buf);
233 case TM2_OLD_HEADER_MAGIC:
234 avpriv_request_sample(ctx->avctx, "Old TM2 header");
236 case TM2_NEW_HEADER_MAGIC:
239 av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
240 return AVERROR_INVALIDDATA;
244 static int tm2_read_deltas(TM2Context *ctx, int stream_id)
249 d = get_bits(&ctx->gb, 9);
250 mb = get_bits(&ctx->gb, 5);
252 if ((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
253 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
254 return AVERROR_INVALIDDATA;
257 for (i = 0; i < d; i++) {
258 v = get_bits_long(&ctx->gb, mb);
259 if (v & (1 << (mb - 1)))
260 ctx->deltas[stream_id][i] = v - (1 << mb);
262 ctx->deltas[stream_id][i] = v;
264 for (; i < TM2_DELTAS; i++)
265 ctx->deltas[stream_id][i] = 0;
270 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
279 av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n");
280 return AVERROR_INVALIDDATA;
283 /* get stream length in dwords */
284 bytestream2_init(&gb, buf, buf_size);
285 len = bytestream2_get_be32(&gb);
291 if (len >= INT_MAX/4-1 || len < 0 || skip > buf_size) {
292 av_log(ctx->avctx, AV_LOG_ERROR, "invalid stream size\n");
293 return AVERROR_INVALIDDATA;
296 toks = bytestream2_get_be32(&gb);
298 len = bytestream2_get_be32(&gb);
299 if (len == TM2_ESCAPE) {
300 len = bytestream2_get_be32(&gb);
303 pos = bytestream2_tell(&gb);
305 return AVERROR_INVALIDDATA;
306 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
307 if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
309 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
312 /* skip unused fields */
313 len = bytestream2_get_be32(&gb);
314 if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
315 bytestream2_skip(&gb, 8); /* unused by decoder */
317 bytestream2_skip(&gb, 4); /* unused by decoder */
320 pos = bytestream2_tell(&gb);
322 return AVERROR_INVALIDDATA;
323 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
324 if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
326 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
329 /* check if we have sane number of tokens */
330 if ((toks < 0) || (toks > 0xFFFFFF)) {
331 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
332 tm2_free_codes(&codes);
333 return AVERROR_INVALIDDATA;
335 ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
336 ctx->tok_lens[stream_id] = toks;
337 len = bytestream2_get_be32(&gb);
339 pos = bytestream2_tell(&gb);
341 return AVERROR_INVALIDDATA;
342 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
343 for (i = 0; i < toks; i++) {
344 if (get_bits_left(&ctx->gb) <= 0) {
345 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
346 return AVERROR_INVALIDDATA;
348 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
349 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS || ctx->tokens[stream_id][i]<0) {
350 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
351 ctx->tokens[stream_id][i], stream_id, i);
352 return AVERROR_INVALIDDATA;
356 for (i = 0; i < toks; i++) {
357 ctx->tokens[stream_id][i] = codes.recode[0];
358 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
359 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
360 ctx->tokens[stream_id][i], stream_id, i);
361 return AVERROR_INVALIDDATA;
365 tm2_free_codes(&codes);
370 static inline int GET_TOK(TM2Context *ctx,int type)
372 if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
373 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]);
376 if (type <= TM2_MOT) {
377 if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) {
378 av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]);
381 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
383 return ctx->tokens[type][ctx->tok_ptrs[type]++];
386 /* blocks decoding routines */
388 /* common Y, U, V pointers initialisation */
389 #define TM2_INIT_POINTERS() \
392 int Ystride, Ustride, Vstride;\
394 Ystride = ctx->y_stride;\
395 Vstride = ctx->uv_stride;\
396 Ustride = ctx->uv_stride;\
397 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
398 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
399 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
400 last = ctx->last + bx * 4;\
401 clast = ctx->clast + bx * 4;
403 #define TM2_INIT_POINTERS_2() \
405 int oYstride, oUstride, oVstride;\
407 TM2_INIT_POINTERS();\
411 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
412 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
413 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
415 /* recalculate last and delta values for next blocks */
416 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
417 CD[0] = CHR[1] - last[1];\
418 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
419 last[0] = (int)CHR[stride + 0];\
420 last[1] = (int)CHR[stride + 1];}
422 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
423 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
428 for (j = 0; j < 4; j++){
430 for (i = 0; i < 4; i++){
431 d = deltas[i + j * 4];
434 Y[i] = av_clip_uint8(last[i]);
441 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
444 for (j = 0; j < 2; j++) {
445 for (i = 0; i < 2; i++) {
446 CD[j] += deltas[i + j * 2];
454 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
464 t = (CD[0] + CD[1]) >> 1;
465 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
466 CD[1] = CD[0] + CD[1] - t;
470 tm2_high_chroma(data, stride, clast, CD, deltas);
473 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
480 for (i = 0; i < 4; i++) {
481 deltas[i] = GET_TOK(ctx, TM2_C_HI);
482 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
484 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
485 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
488 for (i = 0; i < 16; i++)
489 deltas[i] = GET_TOK(ctx, TM2_L_HI);
491 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
494 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
501 deltas[0] = GET_TOK(ctx, TM2_C_LO);
502 deltas[1] = deltas[2] = deltas[3] = 0;
503 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
505 deltas[0] = GET_TOK(ctx, TM2_C_LO);
506 deltas[1] = deltas[2] = deltas[3] = 0;
507 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
510 for (i = 0; i < 16; i++)
511 deltas[i] = GET_TOK(ctx, TM2_L_HI);
513 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
516 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
524 deltas[0] = GET_TOK(ctx, TM2_C_LO);
525 deltas[1] = deltas[2] = deltas[3] = 0;
526 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
528 deltas[0] = GET_TOK(ctx, TM2_C_LO);
529 deltas[1] = deltas[2] = deltas[3] = 0;
530 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
533 for (i = 0; i < 16; i++)
536 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
537 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
538 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
539 deltas[10] = GET_TOK(ctx, TM2_L_LO);
542 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
544 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
545 last[2] = (last[1] + last[3]) >> 1;
547 t1 = ctx->D[0] + ctx->D[1];
549 ctx->D[1] = t1 - (t1 >> 1);
550 t2 = ctx->D[2] + ctx->D[3];
552 ctx->D[3] = t2 - (t2 >> 1);
554 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
557 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
561 int left, right, diff;
566 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
567 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
569 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
570 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
573 for (i = 0; i < 16; i++)
576 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
579 left = last[-1] - ct;
585 last[0] = left + (diff >> 2);
586 last[1] = left + (diff >> 1);
587 last[2] = right - (diff >> 2);
592 ctx->D[0] = (tp + (ct >> 2)) - left;
594 ctx->D[1] = (tp + (ct >> 1)) - left;
596 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
598 ctx->D[3] = (tp + ct) - left;
600 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
603 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
606 TM2_INIT_POINTERS_2();
609 for (j = 0; j < 2; j++) {
610 for (i = 0; i < 2; i++){
614 U += Ustride; V += Vstride;
615 Uo += oUstride; Vo += oVstride;
619 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
620 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
623 ctx->D[0] = Yo[3] - last[3];
624 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
625 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
626 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
628 for (j = 0; j < 4; j++) {
629 for (i = 0; i < 4; i++) {
638 static inline void tm2_update_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++) {
647 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
648 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
657 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
658 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
661 ctx->D[0] = Yo[3] - last[3];
662 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
663 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
664 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
666 for (j = 0; j < 4; j++) {
668 for (i = 0; i < 4; i++) {
669 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
672 ctx->D[j] = last[3] - d;
678 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
682 TM2_INIT_POINTERS_2();
684 mx = GET_TOK(ctx, TM2_MOT);
685 my = GET_TOK(ctx, TM2_MOT);
686 mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
687 my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
689 if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) {
690 av_log(ctx->avctx, AV_LOG_ERROR, "MV out of picture\n");
694 Yo += my * oYstride + mx;
695 Uo += (my >> 1) * oUstride + (mx >> 1);
696 Vo += (my >> 1) * oVstride + (mx >> 1);
699 for (j = 0; j < 2; j++) {
700 for (i = 0; i < 2; i++) {
711 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
712 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
715 for (j = 0; j < 4; j++) {
716 for (i = 0; i < 4; i++) {
722 /* calculate deltas */
724 ctx->D[0] = Y[3] - last[3];
725 ctx->D[1] = Y[3 + Ystride] - Y[3];
726 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
727 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
728 for (i = 0; i < 4; i++)
729 last[i] = Y[i + Ystride * 3];
732 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
735 int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
741 for (i = 0; i < TM2_NUM_STREAMS; i++)
742 ctx->tok_ptrs[i] = 0;
744 if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
745 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
746 return AVERROR_INVALIDDATA;
749 memset(ctx->last, 0, 4 * bw * sizeof(int));
750 memset(ctx->clast, 0, 4 * bw * sizeof(int));
752 for (j = 0; j < bh; j++) {
753 memset(ctx->D, 0, 4 * sizeof(int));
754 memset(ctx->CD, 0, 4 * sizeof(int));
755 for (i = 0; i < bw; i++) {
756 type = GET_TOK(ctx, TM2_TYPE);
759 tm2_hi_res_block(ctx, p, i, j);
762 tm2_med_res_block(ctx, p, i, j);
765 tm2_low_res_block(ctx, p, i, j);
768 tm2_null_res_block(ctx, p, i, j);
771 tm2_update_block(ctx, p, i, j);
775 tm2_still_block(ctx, p, i, j);
779 tm2_motion_block(ctx, p, i, j);
783 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
788 /* copy data from our buffer to AVFrame */
789 Y = (ctx->cur?ctx->Y2:ctx->Y1);
790 U = (ctx->cur?ctx->U2:ctx->U1);
791 V = (ctx->cur?ctx->V2:ctx->V1);
793 for (j = 0; j < h; j++) {
794 for (i = 0; i < w; i++) {
795 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
796 dst[3*i+0] = av_clip_uint8(y + v);
797 dst[3*i+1] = av_clip_uint8(y);
798 dst[3*i+2] = av_clip_uint8(y + u);
801 /* horizontal edge extension */
802 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
803 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
805 /* vertical edge extension */
807 memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
808 memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
809 memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
810 memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
811 } else if (j == h - 1) {
812 memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
813 memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
814 memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
815 memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
820 /* horizontal edge extension */
821 U[-2] = U[-1] = U[0];
822 V[-2] = V[-1] = V[0];
823 U[cw + 1] = U[cw] = U[cw - 1];
824 V[cw + 1] = V[cw] = V[cw - 1];
826 /* vertical edge extension */
828 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
829 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
830 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
831 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
832 } else if (j == h - 1) {
833 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
834 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
835 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
836 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
842 dst += p->linesize[0];
848 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
849 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
852 #define TM2_HEADER_SIZE 40
854 static int decode_frame(AVCodecContext *avctx,
855 void *data, int *got_frame,
858 TM2Context * const l = avctx->priv_data;
859 const uint8_t *buf = avpkt->data;
860 int buf_size = avpkt->size & ~3;
861 AVFrame * const p = &l->pic;
862 int offset = TM2_HEADER_SIZE;
865 av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size);
867 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
868 return AVERROR(ENOMEM);
871 if ((ret = ff_reget_buffer(avctx, p)) < 0)
874 l->dsp.bswap_buf((uint32_t*)l->buffer, (const uint32_t*)buf, buf_size >> 2);
876 if ((ret = tm2_read_header(l, l->buffer)) < 0) {
880 for (i = 0; i < TM2_NUM_STREAMS; i++) {
881 if (offset >= buf_size) {
882 av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n");
883 return AVERROR_INVALIDDATA;
886 t = tm2_read_stream(l, l->buffer + offset, tm2_stream_order[i],
889 int j = tm2_stream_order[i];
890 memset(l->tokens[j], 0, sizeof(**l->tokens) * l->tok_lens[j]);
895 p->key_frame = tm2_decode_blocks(l, p);
897 p->pict_type = AV_PICTURE_TYPE_I;
899 p->pict_type = AV_PICTURE_TYPE_P;
903 ret = av_frame_ref(data, &l->pic);
905 return (ret < 0) ? ret : buf_size;
908 static av_cold int decode_init(AVCodecContext *avctx)
910 TM2Context * const l = avctx->priv_data;
911 int i, w = avctx->width, h = avctx->height;
913 if ((avctx->width & 3) || (avctx->height & 3)) {
914 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
915 return AVERROR(EINVAL);
919 avcodec_get_frame_defaults(&l->pic);
920 avctx->pix_fmt = AV_PIX_FMT_BGR24;
922 ff_dsputil_init(&l->dsp, avctx);
924 l->last = av_malloc(4 * sizeof(*l->last) * (w >> 2));
925 l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
927 for (i = 0; i < TM2_NUM_STREAMS; i++) {
934 l->Y1_base = av_mallocz(sizeof(*l->Y1_base) * w * h);
935 l->Y2_base = av_mallocz(sizeof(*l->Y2_base) * w * h);
939 l->U1_base = av_mallocz(sizeof(*l->U1_base) * w * h);
940 l->V1_base = av_mallocz(sizeof(*l->V1_base) * w * h);
941 l->U2_base = av_mallocz(sizeof(*l->U2_base) * w * h);
942 l->V2_base = av_mallocz(sizeof(*l->V1_base) * w * h);
945 if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
946 !l->V1_base || !l->U2_base || !l->V2_base ||
947 !l->last || !l->clast) {
948 av_freep(l->Y1_base);
949 av_freep(l->Y2_base);
950 av_freep(l->U1_base);
951 av_freep(l->U2_base);
952 av_freep(l->V1_base);
953 av_freep(l->V2_base);
956 return AVERROR(ENOMEM);
958 l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
959 l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
960 l->U1 = l->U1_base + l->uv_stride * 2 + 2;
961 l->U2 = l->U2_base + l->uv_stride * 2 + 2;
962 l->V1 = l->V1_base + l->uv_stride * 2 + 2;
963 l->V2 = l->V2_base + l->uv_stride * 2 + 2;
968 static av_cold int decode_end(AVCodecContext *avctx)
970 TM2Context * const l = avctx->priv_data;
971 AVFrame *pic = &l->pic;
976 for (i = 0; i < TM2_NUM_STREAMS; i++)
977 av_free(l->tokens[i]);
986 av_freep(&l->buffer);
994 AVCodec ff_truemotion2_decoder = {
995 .name = "truemotion2",
996 .type = AVMEDIA_TYPE_VIDEO,
997 .id = AV_CODEC_ID_TRUEMOTION2,
998 .priv_data_size = sizeof(TM2Context),
1000 .close = decode_end,
1001 .decode = decode_frame,
1002 .capabilities = CODEC_CAP_DR1,
1003 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),