Browse code

Duck TrueMotion 2 video decoder, courtesy of Konstantin Shishkov

Originally committed as revision 4638 to svn://svn.ffmpeg.org/ffmpeg/trunk

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