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