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"
32 #define TM2_ESCAPE 0x80000000
34 /* Huffman-coded streams of different types of blocks */
35 enum TM2_STREAMS{ TM2_C_HI = 0, TM2_C_LO, TM2_L_HI, TM2_L_LO,
36 TM2_UPD, TM2_MOT, TM2_TYPE, TM2_NUM_STREAMS};
38 enum TM2_BLOCKS{ TM2_HI_RES = 0, TM2_MED_RES, TM2_LOW_RES, TM2_NULL_RES,
39 TM2_UPDATE, TM2_STILL, TM2_MOTION};
41 typedef struct TM2Context{
42 AVCodecContext *avctx;
52 int *tokens[TM2_NUM_STREAMS];
53 int tok_lens[TM2_NUM_STREAMS];
54 int tok_ptrs[TM2_NUM_STREAMS];
55 int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
56 /* for blocks decoding */
62 /* data for current and previous frame */
63 int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
64 int *Y1, *U1, *V1, *Y2, *U2, *V2;
65 int y_stride, uv_stride;
70 * Huffman codes for each of streams
72 typedef struct TM2Codes{
73 VLC vlc; ///< table for FFmpeg bitstream reader
75 int *recode; ///< table for converting from code indexes to values
80 * structure for gathering Huffman codes information
82 typedef struct TM2Huff{
83 int val_bits; ///< length of literal
84 int max_bits; ///< maximum length of code
85 int min_bits; ///< minimum length of code
86 int nodes; ///< total number of nodes in tree
87 int num; ///< current number filled
88 int max_num; ///< total number of codes
89 int *nums; ///< literals
90 uint32_t *bits; ///< codes
91 int *lens; ///< codelengths
94 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
96 if(length > huff->max_bits) {
97 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n", huff->max_bits);
101 if(!get_bits1(&ctx->gb)) { /* literal */
105 if(huff->num >= huff->max_num) {
106 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
109 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
110 huff->bits[huff->num] = prefix;
111 huff->lens[huff->num] = length;
114 } else { /* non-terminal node */
115 if(tm2_read_tree(ctx, prefix << 1, length + 1, huff) == -1)
117 if(tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff) == -1)
123 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
128 huff.val_bits = get_bits(&ctx->gb, 5);
129 huff.max_bits = get_bits(&ctx->gb, 5);
130 huff.min_bits = get_bits(&ctx->gb, 5);
131 huff.nodes = get_bits_long(&ctx->gb, 17);
134 /* check for correct codes parameters */
135 if((huff.val_bits < 1) || (huff.val_bits > 32) ||
136 (huff.max_bits < 0) || (huff.max_bits > 25)) {
137 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n",
138 huff.val_bits, huff.max_bits);
141 if((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
142 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes);
146 if(huff.max_bits == 0)
149 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
150 huff.max_num = (huff.nodes + 1) >> 1;
151 huff.nums = av_mallocz(huff.max_num * sizeof(int));
152 huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
153 huff.lens = av_mallocz(huff.max_num * sizeof(int));
155 if(tm2_read_tree(ctx, 0, 0, &huff) == -1)
158 if(huff.num != huff.max_num) {
159 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
160 huff.num, huff.max_num);
164 /* convert codes to vlc_table */
168 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
169 huff.lens, sizeof(int), sizeof(int),
170 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
172 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
177 code->bits = huff.max_bits;
178 code->length = huff.max_num;
179 code->recode = av_malloc(code->length * sizeof(int));
180 for(i = 0; i < code->length; i++)
181 code->recode[i] = huff.nums[i];
184 /* free allocated memory */
192 static void tm2_free_codes(TM2Codes *code)
194 av_free(code->recode);
196 ff_free_vlc(&code->vlc);
199 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
202 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
203 return code->recode[val];
206 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
213 magic = AV_RL32(buf);
216 if(magic == 0x00000100) { /* old header */
217 /* av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */
219 } else if(magic == 0x00000101) { /* new header */
222 av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
229 static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
233 d = get_bits(&ctx->gb, 9);
234 mb = get_bits(&ctx->gb, 5);
236 if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
237 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
241 for(i = 0; i < d; i++) {
242 v = get_bits_long(&ctx->gb, mb);
243 if(v & (1 << (mb - 1)))
244 ctx->deltas[stream_id][i] = v - (1 << mb);
246 ctx->deltas[stream_id][i] = v;
248 for(; i < TM2_DELTAS; i++)
249 ctx->deltas[stream_id][i] = 0;
254 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
263 av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n");
264 return AVERROR_INVALIDDATA;
267 /* get stream length in dwords */
268 bytestream2_init(&gb, buf, buf_size);
269 len = bytestream2_get_be32(&gb);
275 if (len >= INT_MAX/4-1 || len < 0 || skip > buf_size) {
276 av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
280 toks = bytestream2_get_be32(&gb);
282 len = bytestream2_get_be32(&gb);
283 if(len == TM2_ESCAPE) {
284 len = bytestream2_get_be32(&gb);
287 pos = bytestream2_tell(&gb);
290 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
291 if(tm2_read_deltas(ctx, stream_id) == -1)
293 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
296 /* skip unused fields */
297 len = bytestream2_get_be32(&gb);
298 if(len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
299 bytestream2_skip(&gb, 8); /* unused by decoder */
301 bytestream2_skip(&gb, 4); /* unused by decoder */
304 pos = bytestream2_tell(&gb);
307 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
308 if(tm2_build_huff_table(ctx, &codes) == -1)
310 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
313 /* check if we have sane number of tokens */
314 if((toks < 0) || (toks > 0xFFFFFF)){
315 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
316 tm2_free_codes(&codes);
319 ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
320 ctx->tok_lens[stream_id] = toks;
321 len = bytestream2_get_be32(&gb);
323 pos = bytestream2_tell(&gb);
326 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
327 for(i = 0; i < toks; i++) {
328 if (get_bits_left(&ctx->gb) <= 0) {
329 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
332 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
333 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
334 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
335 ctx->tokens[stream_id][i], stream_id, i);
336 return AVERROR_INVALIDDATA;
340 for(i = 0; i < toks; i++) {
341 ctx->tokens[stream_id][i] = codes.recode[0];
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 tm2_free_codes(&codes);
354 static inline int GET_TOK(TM2Context *ctx,int type) {
355 if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
356 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]);
359 if(type <= TM2_MOT) {
360 if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) {
361 av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]);
364 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
366 return ctx->tokens[type][ctx->tok_ptrs[type]++];
369 /* blocks decoding routines */
371 /* common Y, U, V pointers initialisation */
372 #define TM2_INIT_POINTERS() \
375 int Ystride, Ustride, Vstride;\
377 Ystride = ctx->y_stride;\
378 Vstride = ctx->uv_stride;\
379 Ustride = ctx->uv_stride;\
380 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
381 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
382 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
383 last = ctx->last + bx * 4;\
384 clast = ctx->clast + bx * 4;
386 #define TM2_INIT_POINTERS_2() \
388 int oYstride, oUstride, oVstride;\
390 TM2_INIT_POINTERS();\
394 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
395 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
396 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
398 /* recalculate last and delta values for next blocks */
399 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
400 CD[0] = CHR[1] - last[1];\
401 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
402 last[0] = (int)CHR[stride + 0];\
403 last[1] = (int)CHR[stride + 1];}
405 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
406 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
411 for(j = 0; j < 4; j++){
413 for(i = 0; i < 4; i++){
414 d = deltas[i + j * 4];
417 Y[i] = av_clip_uint8(last[i]);
424 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
427 for(j = 0; j < 2; j++){
428 for(i = 0; i < 2; i++){
429 CD[j] += deltas[i + j * 2];
437 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
447 t = (CD[0] + CD[1]) >> 1;
448 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
449 CD[1] = CD[0] + CD[1] - t;
453 tm2_high_chroma(data, stride, clast, CD, deltas);
456 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
463 for(i = 0; i < 4; i++) {
464 deltas[i] = GET_TOK(ctx, TM2_C_HI);
465 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
467 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
468 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
471 for(i = 0; i < 16; i++)
472 deltas[i] = GET_TOK(ctx, TM2_L_HI);
474 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
477 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
484 deltas[0] = GET_TOK(ctx, TM2_C_LO);
485 deltas[1] = deltas[2] = deltas[3] = 0;
486 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
488 deltas[0] = GET_TOK(ctx, TM2_C_LO);
489 deltas[1] = deltas[2] = deltas[3] = 0;
490 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
493 for(i = 0; i < 16; i++)
494 deltas[i] = GET_TOK(ctx, TM2_L_HI);
496 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
499 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
507 deltas[0] = GET_TOK(ctx, TM2_C_LO);
508 deltas[1] = deltas[2] = deltas[3] = 0;
509 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
511 deltas[0] = GET_TOK(ctx, TM2_C_LO);
512 deltas[1] = deltas[2] = deltas[3] = 0;
513 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
516 for(i = 0; i < 16; i++)
519 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
520 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
521 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
522 deltas[10] = GET_TOK(ctx, TM2_L_LO);
525 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
527 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
528 last[2] = (last[1] + last[3]) >> 1;
530 t1 = ctx->D[0] + ctx->D[1];
532 ctx->D[1] = t1 - (t1 >> 1);
533 t2 = ctx->D[2] + ctx->D[3];
535 ctx->D[3] = t2 - (t2 >> 1);
537 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
540 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
544 int left, right, diff;
549 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
550 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
552 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
553 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
556 for(i = 0; i < 16; i++)
559 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
562 left = last[-1] - ct;
568 last[0] = left + (diff >> 2);
569 last[1] = left + (diff >> 1);
570 last[2] = right - (diff >> 2);
575 ctx->D[0] = (tp + (ct >> 2)) - left;
577 ctx->D[1] = (tp + (ct >> 1)) - left;
579 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
581 ctx->D[3] = (tp + ct) - left;
583 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
586 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
589 TM2_INIT_POINTERS_2();
592 for(j = 0; j < 2; j++){
593 for(i = 0; i < 2; i++){
597 U += Ustride; V += Vstride;
598 Uo += oUstride; Vo += oVstride;
602 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
603 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
606 ctx->D[0] = Yo[3] - last[3];
607 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
608 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
609 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
611 for(j = 0; j < 4; j++){
612 for(i = 0; i < 4; i++){
621 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
625 TM2_INIT_POINTERS_2();
628 for(j = 0; j < 2; j++){
629 for(i = 0; i < 2; i++){
630 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
631 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
633 U += Ustride; V += Vstride;
634 Uo += oUstride; Vo += oVstride;
638 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
639 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
642 ctx->D[0] = Yo[3] - last[3];
643 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
644 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
645 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
647 for(j = 0; j < 4; j++){
649 for(i = 0; i < 4; i++){
650 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
653 ctx->D[j] = last[3] - d;
659 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
663 TM2_INIT_POINTERS_2();
665 mx = GET_TOK(ctx, TM2_MOT);
666 my = GET_TOK(ctx, TM2_MOT);
667 mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
668 my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
670 if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) {
671 av_log(0,0, "MV out of picture\n");
675 Yo += my * oYstride + mx;
676 Uo += (my >> 1) * oUstride + (mx >> 1);
677 Vo += (my >> 1) * oVstride + (mx >> 1);
680 for(j = 0; j < 2; j++){
681 for(i = 0; i < 2; i++){
685 U += Ustride; V += Vstride;
686 Uo += oUstride; Vo += oVstride;
690 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
691 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
694 for(j = 0; j < 4; j++){
695 for(i = 0; i < 4; i++){
701 /* calculate deltas */
703 ctx->D[0] = Y[3] - last[3];
704 ctx->D[1] = Y[3 + Ystride] - Y[3];
705 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
706 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
707 for(i = 0; i < 4; i++)
708 last[i] = Y[i + Ystride * 3];
711 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
714 int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
720 for(i = 0; i < TM2_NUM_STREAMS; i++)
721 ctx->tok_ptrs[i] = 0;
723 if (ctx->tok_lens[TM2_TYPE]<bw*bh){
724 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
728 memset(ctx->last, 0, 4 * bw * sizeof(int));
729 memset(ctx->clast, 0, 4 * bw * sizeof(int));
731 for(j = 0; j < bh; j++) {
732 memset(ctx->D, 0, 4 * sizeof(int));
733 memset(ctx->CD, 0, 4 * sizeof(int));
734 for(i = 0; i < bw; i++) {
735 type = GET_TOK(ctx, TM2_TYPE);
738 tm2_hi_res_block(ctx, p, i, j);
741 tm2_med_res_block(ctx, p, i, j);
744 tm2_low_res_block(ctx, p, i, j);
747 tm2_null_res_block(ctx, p, i, j);
750 tm2_update_block(ctx, p, i, j);
754 tm2_still_block(ctx, p, i, j);
758 tm2_motion_block(ctx, p, i, j);
762 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
767 /* copy data from our buffer to AVFrame */
768 Y = (ctx->cur?ctx->Y2:ctx->Y1);
769 U = (ctx->cur?ctx->U2:ctx->U1);
770 V = (ctx->cur?ctx->V2:ctx->V1);
772 for(j = 0; j < h; j++){
773 for(i = 0; i < w; i++){
774 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
775 dst[3*i+0] = av_clip_uint8(y + v);
776 dst[3*i+1] = av_clip_uint8(y);
777 dst[3*i+2] = av_clip_uint8(y + u);
780 /* horizontal edge extension */
781 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
782 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
784 /* vertical edge extension */
786 memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
787 memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
788 memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
789 memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
790 } else if (j == h - 1) {
791 memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
792 memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
793 memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
794 memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
799 /* horizontal edge extension */
800 U[-2] = U[-1] = U[0];
801 V[-2] = V[-1] = V[0];
802 U[cw + 1] = U[cw] = U[cw - 1];
803 V[cw + 1] = V[cw] = V[cw - 1];
805 /* vertical edge extension */
807 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
808 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
809 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
810 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
811 } else if (j == h - 1) {
812 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
813 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
814 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
815 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
821 dst += p->linesize[0];
827 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
828 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
831 static int decode_frame(AVCodecContext *avctx,
832 void *data, int *data_size,
835 const uint8_t *buf = avpkt->data;
836 int buf_size = avpkt->size & ~3;
837 TM2Context * const l = avctx->priv_data;
838 AVFrame * const p = &l->pic;
841 av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size);
843 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
847 p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
848 if(avctx->reget_buffer(avctx, p) < 0){
849 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
853 l->dsp.bswap_buf((uint32_t*)l->buffer, (const uint32_t*)buf, buf_size >> 2);
854 skip = tm2_read_header(l, l->buffer);
860 for(i = 0; i < TM2_NUM_STREAMS; i++){
861 if (skip >= buf_size) {
862 av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n");
863 return AVERROR_INVALIDDATA;
866 t = tm2_read_stream(l, l->buffer + skip, tm2_stream_order[i], buf_size - skip);
872 p->key_frame = tm2_decode_blocks(l, p);
874 p->pict_type = AV_PICTURE_TYPE_I;
876 p->pict_type = AV_PICTURE_TYPE_P;
879 *data_size = sizeof(AVFrame);
880 *(AVFrame*)data = l->pic;
885 static av_cold int decode_init(AVCodecContext *avctx){
886 TM2Context * const l = avctx->priv_data;
887 int i, w = avctx->width, h = avctx->height;
889 if((avctx->width & 3) || (avctx->height & 3)){
890 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
896 avctx->pix_fmt = PIX_FMT_BGR24;
897 avcodec_get_frame_defaults(&l->pic);
899 ff_dsputil_init(&l->dsp, avctx);
901 l->last = av_malloc(4 * sizeof(*l->last) * (w >> 2));
902 l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
904 for(i = 0; i < TM2_NUM_STREAMS; i++) {
911 l->Y1_base = av_malloc(sizeof(*l->Y1_base) * w * h);
912 l->Y2_base = av_malloc(sizeof(*l->Y2_base) * w * h);
916 l->U1_base = av_malloc(sizeof(*l->U1_base) * w * h);
917 l->V1_base = av_malloc(sizeof(*l->V1_base) * w * h);
918 l->U2_base = av_malloc(sizeof(*l->U2_base) * w * h);
919 l->V2_base = av_malloc(sizeof(*l->V1_base) * w * h);
922 if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
923 !l->V1_base || !l->U2_base || !l->V2_base ||
924 !l->last || !l->clast) {
925 av_freep(l->Y1_base);
926 av_freep(l->Y2_base);
927 av_freep(l->U1_base);
928 av_freep(l->U2_base);
929 av_freep(l->V1_base);
930 av_freep(l->V2_base);
933 return AVERROR(ENOMEM);
935 l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
936 l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
937 l->U1 = l->U1_base + l->uv_stride * 2 + 2;
938 l->U2 = l->U2_base + l->uv_stride * 2 + 2;
939 l->V1 = l->V1_base + l->uv_stride * 2 + 2;
940 l->V2 = l->V2_base + l->uv_stride * 2 + 2;
945 static av_cold int decode_end(AVCodecContext *avctx){
946 TM2Context * const l = avctx->priv_data;
947 AVFrame *pic = &l->pic;
952 for(i = 0; i < TM2_NUM_STREAMS; i++)
953 av_free(l->tokens[i]);
962 av_freep(&l->buffer);
966 avctx->release_buffer(avctx, pic);
971 AVCodec ff_truemotion2_decoder = {
972 .name = "truemotion2",
973 .type = AVMEDIA_TYPE_VIDEO,
974 .id = CODEC_ID_TRUEMOTION2,
975 .priv_data_size = sizeof(TM2Context),
978 .decode = decode_frame,
979 .capabilities = CODEC_CAP_DR1,
980 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),