]> git.sesse.net Git - ffmpeg/blob - libavcodec/truemotion2.c
fft-test: Drop unnecessary pointer indirection for context structs
[ffmpeg] / libavcodec / truemotion2.c
1 /*
2  * Duck/ON2 TrueMotion 2 Decoder
3  * Copyright (c) 2005 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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.
11  *
12  * Libav 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Duck TrueMotion2 decoder.
25  */
26
27 #include <inttypes.h>
28
29 #include "avcodec.h"
30 #include "bswapdsp.h"
31 #include "bytestream.h"
32 #include "get_bits.h"
33 #include "internal.h"
34
35 #define TM2_ESCAPE 0x80000000
36 #define TM2_DELTAS 64
37
38 /* Huffman-coded streams of different types of blocks */
39 enum TM2_STREAMS {
40     TM2_C_HI = 0,
41     TM2_C_LO,
42     TM2_L_HI,
43     TM2_L_LO,
44     TM2_UPD,
45     TM2_MOT,
46     TM2_TYPE,
47     TM2_NUM_STREAMS
48 };
49
50 /* Block types */
51 enum TM2_BLOCKS {
52     TM2_HI_RES = 0,
53     TM2_MED_RES,
54     TM2_LOW_RES,
55     TM2_NULL_RES,
56     TM2_UPDATE,
57     TM2_STILL,
58     TM2_MOTION
59 };
60
61 typedef struct TM2Context {
62     AVCodecContext *avctx;
63     AVFrame *pic;
64
65     GetBitContext gb;
66     BswapDSPContext bdsp;
67
68     /* TM2 streams */
69     int *tokens[TM2_NUM_STREAMS];
70     int tok_lens[TM2_NUM_STREAMS];
71     int tok_ptrs[TM2_NUM_STREAMS];
72     int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
73     /* for blocks decoding */
74     int D[4];
75     int CD[4];
76     int *last;
77     int *clast;
78
79     /* data for current and previous frame */
80     int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
81     int *Y1, *U1, *V1, *Y2, *U2, *V2;
82     int y_stride, uv_stride;
83     int cur;
84 } TM2Context;
85
86 /**
87 * Huffman codes for each of streams
88 */
89 typedef struct TM2Codes {
90     VLC vlc; ///< table for Libav bitstream reader
91     int bits;
92     int *recode; ///< table for converting from code indexes to values
93     int length;
94 } TM2Codes;
95
96 /**
97 * structure for gathering Huffman codes information
98 */
99 typedef struct TM2Huff {
100     int val_bits; ///< length of literal
101     int max_bits; ///< maximum length of code
102     int min_bits; ///< minimum length of code
103     int nodes; ///< total number of nodes in tree
104     int num; ///< current number filled
105     int max_num; ///< total number of codes
106     int *nums; ///< literals
107     uint32_t *bits; ///< codes
108     int *lens; ///< codelengths
109 } TM2Huff;
110
111 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
112 {
113     int ret;
114     if (length > huff->max_bits) {
115         av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
116                huff->max_bits);
117         return AVERROR_INVALIDDATA;
118     }
119
120     if (!get_bits1(&ctx->gb)) { /* literal */
121         if (length == 0) {
122             length = 1;
123         }
124         if (huff->num >= huff->max_num) {
125             av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
126             return AVERROR_INVALIDDATA;
127         }
128         huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
129         huff->bits[huff->num] = prefix;
130         huff->lens[huff->num] = length;
131         huff->num++;
132         return 0;
133     } else { /* non-terminal node */
134         if ((ret = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
135             return ret;
136         if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
137             return ret;
138     }
139     return 0;
140 }
141
142 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
143 {
144     TM2Huff huff;
145     int res = 0;
146
147     huff.val_bits = get_bits(&ctx->gb, 5);
148     huff.max_bits = get_bits(&ctx->gb, 5);
149     huff.min_bits = get_bits(&ctx->gb, 5);
150     huff.nodes    = get_bits_long(&ctx->gb, 17);
151     huff.num      = 0;
152
153     /* check for correct codes parameters */
154     if ((huff.val_bits < 1) || (huff.val_bits > 32) ||
155         (huff.max_bits < 0) || (huff.max_bits > 25)) {
156         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal "
157                "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits);
158         return AVERROR_INVALIDDATA;
159     }
160     if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
161         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree "
162                "nodes: %i\n", huff.nodes);
163         return AVERROR_INVALIDDATA;
164     }
165     /* one-node tree */
166     if (huff.max_bits == 0)
167         huff.max_bits = 1;
168
169     /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
170     huff.max_num = (huff.nodes + 1) >> 1;
171     huff.nums    = av_mallocz(huff.max_num * sizeof(int));
172     huff.bits    = av_mallocz(huff.max_num * sizeof(uint32_t));
173     huff.lens    = av_mallocz(huff.max_num * sizeof(int));
174
175     res = tm2_read_tree(ctx, 0, 0, &huff);
176
177     if (huff.num != huff.max_num) {
178         av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
179                huff.num, huff.max_num);
180         res = AVERROR_INVALIDDATA;
181     }
182
183     /* convert codes to vlc_table */
184     if (res >= 0) {
185         int i;
186
187         res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
188                        huff.lens, sizeof(int), sizeof(int),
189                        huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
190         if (res < 0)
191             av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
192         else {
193             code->bits = huff.max_bits;
194             code->length = huff.max_num;
195             code->recode = av_malloc(code->length * sizeof(int));
196             for (i = 0; i < code->length; i++)
197                 code->recode[i] = huff.nums[i];
198         }
199     }
200     /* free allocated memory */
201     av_free(huff.nums);
202     av_free(huff.bits);
203     av_free(huff.lens);
204
205     return res;
206 }
207
208 static void tm2_free_codes(TM2Codes *code)
209 {
210     av_free(code->recode);
211     if (code->vlc.table)
212         ff_free_vlc(&code->vlc);
213 }
214
215 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
216 {
217     int val;
218     val = get_vlc2(gb, code->vlc.table, code->bits, 1);
219     return code->recode[val];
220 }
221
222 #define TM2_OLD_HEADER_MAGIC 0x00000100
223 #define TM2_NEW_HEADER_MAGIC 0x00000101
224
225 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
226 {
227     uint32_t magic = AV_RL32(buf);
228
229     switch (magic) {
230     case TM2_OLD_HEADER_MAGIC:
231         avpriv_request_sample(ctx->avctx, "Old TM2 header");
232         return 0;
233     case TM2_NEW_HEADER_MAGIC:
234         return 0;
235     default:
236         av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n",
237                magic);
238         return AVERROR_INVALIDDATA;
239     }
240 }
241
242 static int tm2_read_deltas(TM2Context *ctx, int stream_id)
243 {
244     int d, mb;
245     int i, v;
246
247     d  = get_bits(&ctx->gb, 9);
248     mb = get_bits(&ctx->gb, 5);
249
250     if ((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
251         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
252         return AVERROR_INVALIDDATA;
253     }
254
255     for (i = 0; i < d; i++) {
256         v = get_bits_long(&ctx->gb, mb);
257         if (v & (1 << (mb - 1)))
258             ctx->deltas[stream_id][i] = v - (1 << mb);
259         else
260             ctx->deltas[stream_id][i] = v;
261     }
262     for (; i < TM2_DELTAS; i++)
263         ctx->deltas[stream_id][i] = 0;
264
265     return 0;
266 }
267
268 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
269 {
270     int i, ret;
271     int skip = 0;
272     int len, toks, pos;
273     TM2Codes codes;
274     GetByteContext gb;
275
276     /* get stream length in dwords */
277     bytestream2_init(&gb, buf, buf_size);
278     len  = bytestream2_get_be32(&gb);
279     skip = len * 4 + 4;
280
281     if (len == 0)
282         return 4;
283
284     if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
285         av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
286         return AVERROR_INVALIDDATA;
287     }
288
289     toks = bytestream2_get_be32(&gb);
290     if (toks & 1) {
291         len = bytestream2_get_be32(&gb);
292         if (len == TM2_ESCAPE) {
293             len = bytestream2_get_be32(&gb);
294         }
295         if (len > 0) {
296             pos = bytestream2_tell(&gb);
297             if (skip <= pos)
298                 return AVERROR_INVALIDDATA;
299             init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
300             if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
301                 return ret;
302             bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
303         }
304     }
305     /* skip unused fields */
306     len = bytestream2_get_be32(&gb);
307     if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
308         bytestream2_skip(&gb, 8); /* unused by decoder */
309     } else {
310         bytestream2_skip(&gb, 4); /* unused by decoder */
311     }
312
313     pos = bytestream2_tell(&gb);
314     if (skip <= pos)
315         return AVERROR_INVALIDDATA;
316     init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
317     if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
318         return ret;
319     bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
320
321     toks >>= 1;
322     /* check if we have sane number of tokens */
323     if ((toks < 0) || (toks > 0xFFFFFF)) {
324         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
325         tm2_free_codes(&codes);
326         return AVERROR_INVALIDDATA;
327     }
328     ctx->tokens[stream_id]   = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
329     ctx->tok_lens[stream_id] = toks;
330     len = bytestream2_get_be32(&gb);
331     if (len > 0) {
332         pos = bytestream2_tell(&gb);
333         if (skip <= pos)
334             return AVERROR_INVALIDDATA;
335         init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
336         for (i = 0; i < toks; i++) {
337             if (get_bits_left(&ctx->gb) <= 0) {
338                 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
339                 return AVERROR_INVALIDDATA;
340             }
341             ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
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;
346             }
347         }
348     } else {
349         for (i = 0; i < toks; i++) {
350             ctx->tokens[stream_id][i] = codes.recode[0];
351             if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
352                 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
353                        ctx->tokens[stream_id][i], stream_id, i);
354                 return AVERROR_INVALIDDATA;
355             }
356         }
357     }
358     tm2_free_codes(&codes);
359
360     return skip;
361 }
362
363 static inline int GET_TOK(TM2Context *ctx,int type)
364 {
365     if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
366         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]);
367         return 0;
368     }
369     if (type <= TM2_MOT)
370         return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
371     return ctx->tokens[type][ctx->tok_ptrs[type]++];
372 }
373
374 /* blocks decoding routines */
375
376 /* common Y, U, V pointers initialisation */
377 #define TM2_INIT_POINTERS() \
378     int *last, *clast; \
379     int *Y, *U, *V;\
380     int Ystride, Ustride, Vstride;\
381 \
382     Ystride = ctx->y_stride;\
383     Vstride = ctx->uv_stride;\
384     Ustride = ctx->uv_stride;\
385     Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
386     V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
387     U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
388     last = ctx->last + bx * 4;\
389     clast = ctx->clast + bx * 4;
390
391 #define TM2_INIT_POINTERS_2() \
392     int *Yo, *Uo, *Vo;\
393     int oYstride, oUstride, oVstride;\
394 \
395     TM2_INIT_POINTERS();\
396     oYstride = Ystride;\
397     oVstride = Vstride;\
398     oUstride = Ustride;\
399     Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
400     Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
401     Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
402
403 /* recalculate last and delta values for next blocks */
404 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
405     CD[0] = CHR[1] - last[1];\
406     CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
407     last[0] = (int)CHR[stride + 0];\
408     last[1] = (int)CHR[stride + 1];}
409
410 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
411 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
412 {
413     int ct, d;
414     int i, j;
415
416     for (j = 0; j < 4; j++){
417         ct = ctx->D[j];
418         for (i = 0; i < 4; i++){
419             d        = deltas[i + j * 4];
420             ct      += d;
421             last[i] += ct;
422             Y[i]     = av_clip_uint8(last[i]);
423         }
424         Y        += stride;
425         ctx->D[j] = ct;
426     }
427 }
428
429 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
430 {
431     int i, j;
432     for (j = 0; j < 2; j++) {
433         for (i = 0; i < 2; i++)  {
434             CD[j]   += deltas[i + j * 2];
435             last[i] += CD[j];
436             data[i]  = last[i];
437         }
438         data += stride;
439     }
440 }
441
442 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
443 {
444     int t;
445     int l;
446     int prev;
447
448     if (bx > 0)
449         prev = clast[-3];
450     else
451         prev = 0;
452     t        = (CD[0] + CD[1]) >> 1;
453     l        = (prev - CD[0] - CD[1] + clast[1]) >> 1;
454     CD[1]    = CD[0] + CD[1] - t;
455     CD[0]    = t;
456     clast[0] = l;
457
458     tm2_high_chroma(data, stride, clast, CD, deltas);
459 }
460
461 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
462 {
463     int i;
464     int deltas[16];
465     TM2_INIT_POINTERS();
466
467     /* hi-res chroma */
468     for (i = 0; i < 4; i++) {
469         deltas[i]     = GET_TOK(ctx, TM2_C_HI);
470         deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
471     }
472     tm2_high_chroma(U, Ustride, clast,     ctx->CD,     deltas);
473     tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
474
475     /* hi-res luma */
476     for (i = 0; i < 16; i++)
477         deltas[i] = GET_TOK(ctx, TM2_L_HI);
478
479     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
480 }
481
482 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
483 {
484     int i;
485     int deltas[16];
486     TM2_INIT_POINTERS();
487
488     /* low-res chroma */
489     deltas[0] = GET_TOK(ctx, TM2_C_LO);
490     deltas[1] = deltas[2] = deltas[3] = 0;
491     tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
492
493     deltas[0] = GET_TOK(ctx, TM2_C_LO);
494     deltas[1] = deltas[2] = deltas[3] = 0;
495     tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
496
497     /* hi-res luma */
498     for (i = 0; i < 16; i++)
499         deltas[i] = GET_TOK(ctx, TM2_L_HI);
500
501     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
502 }
503
504 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
505 {
506     int i;
507     int t1, t2;
508     int deltas[16];
509     TM2_INIT_POINTERS();
510
511     /* low-res chroma */
512     deltas[0] = GET_TOK(ctx, TM2_C_LO);
513     deltas[1] = deltas[2] = deltas[3] = 0;
514     tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
515
516     deltas[0] = GET_TOK(ctx, TM2_C_LO);
517     deltas[1] = deltas[2] = deltas[3] = 0;
518     tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
519
520     /* low-res luma */
521     for (i = 0; i < 16; i++)
522         deltas[i] = 0;
523
524     deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
525     deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
526     deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
527     deltas[10] = GET_TOK(ctx, TM2_L_LO);
528
529     if (bx > 0)
530         last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
531     else
532         last[0] = (last[1]  - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
533     last[2] = (last[1] + last[3]) >> 1;
534
535     t1 = ctx->D[0] + ctx->D[1];
536     ctx->D[0] = t1 >> 1;
537     ctx->D[1] = t1 - (t1 >> 1);
538     t2 = ctx->D[2] + ctx->D[3];
539     ctx->D[2] = t2 >> 1;
540     ctx->D[3] = t2 - (t2 >> 1);
541
542     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
543 }
544
545 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
546 {
547     int i;
548     int ct;
549     int left, right, diff;
550     int deltas[16];
551     TM2_INIT_POINTERS();
552
553     /* null chroma */
554     deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
555     tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
556
557     deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
558     tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
559
560     /* null luma */
561     for (i = 0; i < 16; i++)
562         deltas[i] = 0;
563
564     ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
565
566     if (bx > 0)
567         left = last[-1] - ct;
568     else
569         left = 0;
570
571     right   = last[3];
572     diff    = right - left;
573     last[0] = left + (diff >> 2);
574     last[1] = left + (diff >> 1);
575     last[2] = right - (diff >> 2);
576     last[3] = right;
577     {
578         int tp = left;
579
580         ctx->D[0] = (tp + (ct >> 2)) - left;
581         left     += ctx->D[0];
582         ctx->D[1] = (tp + (ct >> 1)) - left;
583         left     += ctx->D[1];
584         ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
585         left     += ctx->D[2];
586         ctx->D[3] = (tp + ct) - left;
587     }
588     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
589 }
590
591 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
592 {
593     int i, j;
594     TM2_INIT_POINTERS_2();
595
596     /* update chroma */
597     for (j = 0; j < 2; j++) {
598         for (i = 0; i < 2; i++){
599             U[i] = Uo[i];
600             V[i] = Vo[i];
601         }
602         U  += Ustride; V += Vstride;
603         Uo += oUstride; Vo += oVstride;
604     }
605     U -= Ustride * 2;
606     V -= Vstride * 2;
607     TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
608     TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
609
610     /* update deltas */
611     ctx->D[0] = Yo[3] - last[3];
612     ctx->D[1] = Yo[3 + oYstride] - Yo[3];
613     ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
614     ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
615
616     for (j = 0; j < 4; j++) {
617         for (i = 0; i < 4; i++) {
618             Y[i]    = Yo[i];
619             last[i] = Yo[i];
620         }
621         Y  += Ystride;
622         Yo += oYstride;
623     }
624 }
625
626 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
627 {
628     int i, j;
629     int d;
630     TM2_INIT_POINTERS_2();
631
632     /* update chroma */
633     for (j = 0; j < 2; j++) {
634         for (i = 0; i < 2; i++) {
635             U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
636             V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
637         }
638         U  += Ustride;
639         V  += Vstride;
640         Uo += oUstride;
641         Vo += oVstride;
642     }
643     U -= Ustride * 2;
644     V -= Vstride * 2;
645     TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
646     TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
647
648     /* update deltas */
649     ctx->D[0] = Yo[3] - last[3];
650     ctx->D[1] = Yo[3 + oYstride] - Yo[3];
651     ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
652     ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
653
654     for (j = 0; j < 4; j++) {
655         d = last[3];
656         for (i = 0; i < 4; i++) {
657             Y[i]    = Yo[i] + GET_TOK(ctx, TM2_UPD);
658             last[i] = Y[i];
659         }
660         ctx->D[j] = last[3] - d;
661         Y  += Ystride;
662         Yo += oYstride;
663     }
664 }
665
666 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
667 {
668     int i, j;
669     int mx, my;
670     TM2_INIT_POINTERS_2();
671
672     mx = GET_TOK(ctx, TM2_MOT);
673     my = GET_TOK(ctx, TM2_MOT);
674     mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width  - bx * 4);
675     my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
676
677     Yo += my * oYstride + mx;
678     Uo += (my >> 1) * oUstride + (mx >> 1);
679     Vo += (my >> 1) * oVstride + (mx >> 1);
680
681     /* copy chroma */
682     for (j = 0; j < 2; j++) {
683         for (i = 0; i < 2; i++) {
684             U[i] = Uo[i];
685             V[i] = Vo[i];
686         }
687         U  += Ustride;
688         V  += Vstride;
689         Uo += oUstride;
690         Vo += oVstride;
691     }
692     U -= Ustride * 2;
693     V -= Vstride * 2;
694     TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
695     TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
696
697     /* copy luma */
698     for (j = 0; j < 4; j++) {
699         for (i = 0; i < 4; i++) {
700             Y[i] = Yo[i];
701         }
702         Y  += Ystride;
703         Yo += oYstride;
704     }
705     /* calculate deltas */
706     Y -= Ystride * 4;
707     ctx->D[0] = Y[3] - last[3];
708     ctx->D[1] = Y[3 + Ystride] - Y[3];
709     ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
710     ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
711     for (i = 0; i < 4; i++)
712         last[i] = Y[i + Ystride * 3];
713 }
714
715 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
716 {
717     int i, j;
718     int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
719     int type;
720     int keyframe = 1;
721     int *Y, *U, *V;
722     uint8_t *dst;
723
724     for (i = 0; i < TM2_NUM_STREAMS; i++)
725         ctx->tok_ptrs[i] = 0;
726
727     if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
728         av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
729         return AVERROR_INVALIDDATA;
730     }
731
732     memset(ctx->last, 0, 4 * bw * sizeof(int));
733     memset(ctx->clast, 0, 4 * bw * sizeof(int));
734
735     for (j = 0; j < bh; j++) {
736         memset(ctx->D, 0, 4 * sizeof(int));
737         memset(ctx->CD, 0, 4 * sizeof(int));
738         for (i = 0; i < bw; i++) {
739             type = GET_TOK(ctx, TM2_TYPE);
740             switch(type) {
741             case TM2_HI_RES:
742                 tm2_hi_res_block(ctx, p, i, j);
743                 break;
744             case TM2_MED_RES:
745                 tm2_med_res_block(ctx, p, i, j);
746                 break;
747             case TM2_LOW_RES:
748                 tm2_low_res_block(ctx, p, i, j);
749                 break;
750             case TM2_NULL_RES:
751                 tm2_null_res_block(ctx, p, i, j);
752                 break;
753             case TM2_UPDATE:
754                 tm2_update_block(ctx, p, i, j);
755                 keyframe = 0;
756                 break;
757             case TM2_STILL:
758                 tm2_still_block(ctx, p, i, j);
759                 keyframe = 0;
760                 break;
761             case TM2_MOTION:
762                 tm2_motion_block(ctx, p, i, j);
763                 keyframe = 0;
764                 break;
765             default:
766                 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
767             }
768         }
769     }
770
771     /* copy data from our buffer to AVFrame */
772     Y = (ctx->cur?ctx->Y2:ctx->Y1);
773     U = (ctx->cur?ctx->U2:ctx->U1);
774     V = (ctx->cur?ctx->V2:ctx->V1);
775     dst = p->data[0];
776     for (j = 0; j < h; j++) {
777         for (i = 0; i < w; i++) {
778             int y = Y[i], u = U[i >> 1], v = V[i >> 1];
779             dst[3*i+0] = av_clip_uint8(y + v);
780             dst[3*i+1] = av_clip_uint8(y);
781             dst[3*i+2] = av_clip_uint8(y + u);
782         }
783
784         /* horizontal edge extension */
785         Y[-4]    = Y[-3]    = Y[-2]    = Y[-1] = Y[0];
786         Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w]  = Y[w - 1];
787
788         /* vertical edge extension */
789         if (j == 0) {
790             memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
791             memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
792             memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
793             memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
794         } else if (j == h - 1) {
795             memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
796             memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
797             memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
798             memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
799         }
800
801         Y += ctx->y_stride;
802         if (j & 1) {
803             /* horizontal edge extension */
804             U[-2]     = U[-1] = U[0];
805             V[-2]     = V[-1] = V[0];
806             U[cw + 1] = U[cw] = U[cw - 1];
807             V[cw + 1] = V[cw] = V[cw - 1];
808
809             /* vertical edge extension */
810             if (j == 1) {
811                 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
812                 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
813                 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
814                 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
815             } else if (j == h - 1) {
816                 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
817                 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
818                 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
819                 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
820             }
821
822             U += ctx->uv_stride;
823             V += ctx->uv_stride;
824         }
825         dst += p->linesize[0];
826     }
827
828     return keyframe;
829 }
830
831 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
832     TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
833 };
834
835 #define TM2_HEADER_SIZE 40
836
837 static int decode_frame(AVCodecContext *avctx,
838                         void *data, int *got_frame,
839                         AVPacket *avpkt)
840 {
841     TM2Context * const l = avctx->priv_data;
842     const uint8_t *buf   = avpkt->data;
843     int buf_size         = avpkt->size & ~3;
844     AVFrame * const p    = l->pic;
845     int offset           = TM2_HEADER_SIZE;
846     int i, t, ret;
847     uint8_t *swbuf;
848
849     swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
850     if (!swbuf) {
851         av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
852         return AVERROR(ENOMEM);
853     }
854
855     if ((ret = ff_reget_buffer(avctx, p)) < 0) {
856         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
857         av_free(swbuf);
858         return ret;
859     }
860
861     l->bdsp.bswap_buf((uint32_t *) swbuf, (const uint32_t *) buf,
862                       buf_size >> 2);
863
864     if ((ret = tm2_read_header(l, swbuf)) < 0) {
865         av_free(swbuf);
866         return ret;
867     }
868
869     for (i = 0; i < TM2_NUM_STREAMS; i++) {
870         if (offset >= buf_size) {
871             av_free(swbuf);
872             return AVERROR_INVALIDDATA;
873         }
874         t = tm2_read_stream(l, swbuf + offset, tm2_stream_order[i],
875                             buf_size - offset);
876         if (t < 0) {
877             av_free(swbuf);
878             return t;
879         }
880         offset += t;
881     }
882     p->key_frame = tm2_decode_blocks(l, p);
883     if (p->key_frame)
884         p->pict_type = AV_PICTURE_TYPE_I;
885     else
886         p->pict_type = AV_PICTURE_TYPE_P;
887
888     l->cur = !l->cur;
889     *got_frame      = 1;
890     ret = av_frame_ref(data, l->pic);
891     av_free(swbuf);
892
893     return (ret < 0) ? ret : buf_size;
894 }
895
896 static av_cold int decode_init(AVCodecContext *avctx)
897 {
898     TM2Context * const l = avctx->priv_data;
899     int i, w = avctx->width, h = avctx->height;
900
901     if ((avctx->width & 3) || (avctx->height & 3)) {
902         av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
903         return AVERROR(EINVAL);
904     }
905
906     l->avctx       = avctx;
907     avctx->pix_fmt = AV_PIX_FMT_BGR24;
908
909     l->pic = av_frame_alloc();
910     if (!l->pic)
911         return AVERROR(ENOMEM);
912
913     ff_bswapdsp_init(&l->bdsp);
914
915     l->last  = av_malloc(4 * sizeof(*l->last)  * (w >> 2));
916     l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
917
918     for (i = 0; i < TM2_NUM_STREAMS; i++) {
919         l->tokens[i] = NULL;
920         l->tok_lens[i] = 0;
921     }
922
923     w += 8;
924     h += 8;
925     l->Y1_base = av_malloc(sizeof(*l->Y1_base) * w * h);
926     l->Y2_base = av_malloc(sizeof(*l->Y2_base) * w * h);
927     l->y_stride = w;
928     w = (w + 1) >> 1;
929     h = (h + 1) >> 1;
930     l->U1_base = av_malloc(sizeof(*l->U1_base) * w * h);
931     l->V1_base = av_malloc(sizeof(*l->V1_base) * w * h);
932     l->U2_base = av_malloc(sizeof(*l->U2_base) * w * h);
933     l->V2_base = av_malloc(sizeof(*l->V1_base) * w * h);
934     l->uv_stride = w;
935     l->cur = 0;
936     if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
937         !l->V1_base || !l->U2_base || !l->V2_base ||
938         !l->last    || !l->clast) {
939         av_freep(&l->Y1_base);
940         av_freep(&l->Y2_base);
941         av_freep(&l->U1_base);
942         av_freep(&l->U2_base);
943         av_freep(&l->V1_base);
944         av_freep(&l->V2_base);
945         av_freep(&l->last);
946         av_freep(&l->clast);
947         return AVERROR(ENOMEM);
948     }
949     l->Y1 = l->Y1_base + l->y_stride  * 4 + 4;
950     l->Y2 = l->Y2_base + l->y_stride  * 4 + 4;
951     l->U1 = l->U1_base + l->uv_stride * 2 + 2;
952     l->U2 = l->U2_base + l->uv_stride * 2 + 2;
953     l->V1 = l->V1_base + l->uv_stride * 2 + 2;
954     l->V2 = l->V2_base + l->uv_stride * 2 + 2;
955
956     return 0;
957 }
958
959 static av_cold int decode_end(AVCodecContext *avctx)
960 {
961     TM2Context * const l = avctx->priv_data;
962     int i;
963
964     av_free(l->last);
965     av_free(l->clast);
966     for (i = 0; i < TM2_NUM_STREAMS; i++)
967         av_free(l->tokens[i]);
968     if (l->Y1) {
969         av_free(l->Y1_base);
970         av_free(l->U1_base);
971         av_free(l->V1_base);
972         av_free(l->Y2_base);
973         av_free(l->U2_base);
974         av_free(l->V2_base);
975     }
976
977     av_frame_free(&l->pic);
978
979     return 0;
980 }
981
982 AVCodec ff_truemotion2_decoder = {
983     .name           = "truemotion2",
984     .long_name      = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
985     .type           = AVMEDIA_TYPE_VIDEO,
986     .id             = AV_CODEC_ID_TRUEMOTION2,
987     .priv_data_size = sizeof(TM2Context),
988     .init           = decode_init,
989     .close          = decode_end,
990     .decode         = decode_frame,
991     .capabilities   = CODEC_CAP_DR1,
992 };