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