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