Statistics
| Branch: | Revision:

ffmpeg / libavcodec / truemotion2.c @ b89f4fb1

History | View | Annotate | Download (25 KB)

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
 * @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 FFmpeg 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
    if(code->recode)
189
        av_free(code->recode);
190
    if(code->vlc.table)
191
        free_vlc(&code->vlc);
192
}
193

    
194
static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
195
{
196
    int val;
197
    val = get_vlc2(gb, code->vlc.table, code->bits, 1);
198
    return code->recode[val];
199
}
200

    
201
static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
202
{
203
    uint32_t magic;
204
    const uint8_t *obuf;
205
    int length;
206

    
207
    obuf = buf;
208

    
209
    magic = AV_RL32(buf);
210
    buf += 4;
211

    
212
    if(magic == 0x00000100) { /* old header */
213
/*      av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */
214
        return 40;
215
    } else if(magic == 0x00000101) { /* new header */
216
        int w, h, size, flags, xr, yr;
217

    
218
        length = AV_RL32(buf);
219
        buf += 4;
220

    
221
        init_get_bits(&ctx->gb, buf, 32 * 8);
222
        size = get_bits_long(&ctx->gb, 31);
223
        h = get_bits(&ctx->gb, 15);
224
        w = get_bits(&ctx->gb, 15);
225
        flags = get_bits_long(&ctx->gb, 31);
226
        yr = get_bits(&ctx->gb, 9);
227
        xr = get_bits(&ctx->gb, 9);
228

    
229
        return 40;
230
    } else {
231
        av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
232
        return -1;
233
    }
234

    
235
    return buf - obuf;
236
}
237

    
238
static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
239
    int d, mb;
240
    int i, v;
241

    
242
    d = get_bits(&ctx->gb, 9);
243
    mb = get_bits(&ctx->gb, 5);
244

    
245
    if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
246
        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
247
        return -1;
248
    }
249

    
250
    for(i = 0; i < d; i++) {
251
        v = get_bits_long(&ctx->gb, mb);
252
        if(v & (1 << (mb - 1)))
253
            ctx->deltas[stream_id][i] = v - (1 << mb);
254
        else
255
            ctx->deltas[stream_id][i] = v;
256
    }
257
    for(; i < TM2_DELTAS; i++)
258
        ctx->deltas[stream_id][i] = 0;
259

    
260
    return 0;
261
}
262

    
263
static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
264
{
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
    if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
279
        av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
280
        return -1;
281
    }
282

    
283
    toks = AV_RB32(buf); buf += 4; cur += 4;
284
    if(toks & 1) {
285
        len = AV_RB32(buf); buf += 4; cur += 4;
286
        if(len == TM2_ESCAPE) {
287
            len = AV_RB32(buf); buf += 4; cur += 4;
288
        }
289
        if(len > 0) {
290
            init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
291
            if(tm2_read_deltas(ctx, stream_id) == -1)
292
                return -1;
293
            buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
294
            cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
295
        }
296
    }
297
    /* skip unused fields */
298
    if(AV_RB32(buf) == TM2_ESCAPE) {
299
        buf += 4; cur += 4; /* some unknown length - could be escaped too */
300
    }
301
    buf += 4; cur += 4;
302
    buf += 4; cur += 4; /* unused by decoder */
303

    
304
    init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
305
    if(tm2_build_huff_table(ctx, &codes) == -1)
306
        return -1;
307
    buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
308
    cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
309

    
310
    toks >>= 1;
311
    /* check if we have sane number of tokens */
312
    if((toks < 0) || (toks > 0xFFFFFF)){
313
        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
314
        tm2_free_codes(&codes);
315
        return -1;
316
    }
317
    ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
318
    ctx->tok_lens[stream_id] = toks;
319
    len = AV_RB32(buf); buf += 4; cur += 4;
320
    if(len > 0) {
321
        init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
322
        for(i = 0; i < toks; i++) {
323
            if (get_bits_left(&ctx->gb) <= 0) {
324
                av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
325
                return -1;
326
            }
327
            ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
328
        }
329
    } else {
330
        for(i = 0; i < toks; i++)
331
            ctx->tokens[stream_id][i] = codes.recode[0];
332
    }
333
    tm2_free_codes(&codes);
334

    
335
    return skip;
336
}
337

    
338
static inline int GET_TOK(TM2Context *ctx,int type) {
339
    if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
340
        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]);
341
        return 0;
342
    }
343
    if(type <= TM2_MOT)
344
        return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
345
    return ctx->tokens[type][ctx->tok_ptrs[type]++];
346
}
347

    
348
/* blocks decoding routines */
349

    
350
/* common Y, U, V pointers initialisation */
351
#define TM2_INIT_POINTERS() \
352
    int *last, *clast; \
353
    int *Y, *U, *V;\
354
    int Ystride, Ustride, Vstride;\
355
\
356
    Ystride = ctx->avctx->width;\
357
    Vstride = (ctx->avctx->width + 1) >> 1;\
358
    Ustride = (ctx->avctx->width + 1) >> 1;\
359
    Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
360
    V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
361
    U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
362
    last = ctx->last + bx * 4;\
363
    clast = ctx->clast + bx * 4;
364

    
365
#define TM2_INIT_POINTERS_2() \
366
    int *Yo, *Uo, *Vo;\
367
    int oYstride, oUstride, oVstride;\
368
\
369
    TM2_INIT_POINTERS();\
370
    oYstride = Ystride;\
371
    oVstride = Vstride;\
372
    oUstride = Ustride;\
373
    Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
374
    Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
375
    Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
376

    
377
/* recalculate last and delta values for next blocks */
378
#define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
379
    CD[0] = CHR[1] - last[1];\
380
    CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
381
    last[0] = (int)CHR[stride + 0];\
382
    last[1] = (int)CHR[stride + 1];}
383

    
384
/* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
385
static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
386
{
387
    int ct, d;
388
    int i, j;
389

    
390
    for(j = 0; j < 4; j++){
391
        ct = ctx->D[j];
392
        for(i = 0; i < 4; i++){
393
            d = deltas[i + j * 4];
394
            ct += d;
395
            last[i] += ct;
396
            Y[i] = av_clip_uint8(last[i]);
397
        }
398
        Y += stride;
399
        ctx->D[j] = ct;
400
    }
401
}
402

    
403
static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
404
{
405
    int i, j;
406
    for(j = 0; j < 2; j++){
407
        for(i = 0; i < 2; i++){
408
            CD[j] += deltas[i + j * 2];
409
            last[i] += CD[j];
410
            data[i] = last[i];
411
        }
412
        data += stride;
413
    }
414
}
415

    
416
static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
417
{
418
    int t;
419
    int l;
420
    int prev;
421

    
422
    if(bx > 0)
423
        prev = clast[-3];
424
    else
425
        prev = 0;
426
    t = (CD[0] + CD[1]) >> 1;
427
    l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
428
    CD[1] = CD[0] + CD[1] - t;
429
    CD[0] = t;
430
    clast[0] = l;
431

    
432
    tm2_high_chroma(data, stride, clast, CD, deltas);
433
}
434

    
435
static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
436
{
437
    int i;
438
    int deltas[16];
439
    TM2_INIT_POINTERS();
440

    
441
    /* hi-res chroma */
442
    for(i = 0; i < 4; i++) {
443
        deltas[i] = GET_TOK(ctx, TM2_C_HI);
444
        deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
445
    }
446
    tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
447
    tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
448

    
449
    /* hi-res luma */
450
    for(i = 0; i < 16; i++)
451
        deltas[i] = GET_TOK(ctx, TM2_L_HI);
452

    
453
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
454
}
455

    
456
static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
457
{
458
    int i;
459
    int deltas[16];
460
    TM2_INIT_POINTERS();
461

    
462
    /* low-res chroma */
463
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
464
    deltas[1] = deltas[2] = deltas[3] = 0;
465
    tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
466

    
467
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
468
    deltas[1] = deltas[2] = deltas[3] = 0;
469
    tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
470

    
471
    /* hi-res luma */
472
    for(i = 0; i < 16; i++)
473
        deltas[i] = GET_TOK(ctx, TM2_L_HI);
474

    
475
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
476
}
477

    
478
static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
479
{
480
    int i;
481
    int t1, t2;
482
    int deltas[16];
483
    TM2_INIT_POINTERS();
484

    
485
    /* low-res chroma */
486
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
487
    deltas[1] = deltas[2] = deltas[3] = 0;
488
    tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
489

    
490
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
491
    deltas[1] = deltas[2] = deltas[3] = 0;
492
    tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
493

    
494
    /* low-res luma */
495
    for(i = 0; i < 16; i++)
496
        deltas[i] = 0;
497

    
498
    deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
499
    deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
500
    deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
501
    deltas[10] = GET_TOK(ctx, TM2_L_LO);
502

    
503
    if(bx > 0)
504
        last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
505
    else
506
        last[0] = (last[1]  - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
507
    last[2] = (last[1] + last[3]) >> 1;
508

    
509
    t1 = ctx->D[0] + ctx->D[1];
510
    ctx->D[0] = t1 >> 1;
511
    ctx->D[1] = t1 - (t1 >> 1);
512
    t2 = ctx->D[2] + ctx->D[3];
513
    ctx->D[2] = t2 >> 1;
514
    ctx->D[3] = t2 - (t2 >> 1);
515

    
516
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
517
}
518

    
519
static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
520
{
521
    int i;
522
    int ct;
523
    int left, right, diff;
524
    int deltas[16];
525
    TM2_INIT_POINTERS();
526

    
527
    /* null chroma */
528
    deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
529
    tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
530

    
531
    deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
532
    tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
533

    
534
    /* null luma */
535
    for(i = 0; i < 16; i++)
536
        deltas[i] = 0;
537

    
538
    ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
539

    
540
    if(bx > 0)
541
        left = last[-1] - ct;
542
    else
543
        left = 0;
544

    
545
    right = last[3];
546
    diff = right - left;
547
    last[0] = left + (diff >> 2);
548
    last[1] = left + (diff >> 1);
549
    last[2] = right - (diff >> 2);
550
    last[3] = right;
551
    {
552
        int tp = left;
553

    
554
        ctx->D[0] = (tp + (ct >> 2)) - left;
555
        left += ctx->D[0];
556
        ctx->D[1] = (tp + (ct >> 1)) - left;
557
        left += ctx->D[1];
558
        ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
559
        left += ctx->D[2];
560
        ctx->D[3] = (tp + ct) - left;
561
    }
562
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
563
}
564

    
565
static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
566
{
567
    int i, j;
568
    TM2_INIT_POINTERS_2();
569

    
570
    /* update chroma */
571
    for(j = 0; j < 2; j++){
572
        for(i = 0; i < 2; i++){
573
            U[i] = Uo[i];
574
            V[i] = Vo[i];
575
        }
576
        U += Ustride; V += Vstride;
577
        Uo += oUstride; Vo += oVstride;
578
    }
579
    U -= Ustride * 2;
580
    V -= Vstride * 2;
581
    TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
582
    TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
583

    
584
    /* update deltas */
585
    ctx->D[0] = Yo[3] - last[3];
586
    ctx->D[1] = Yo[3 + oYstride] - Yo[3];
587
    ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
588
    ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
589

    
590
    for(j = 0; j < 4; j++){
591
        for(i = 0; i < 4; i++){
592
            Y[i] = Yo[i];
593
            last[i] = Yo[i];
594
        }
595
        Y += Ystride;
596
        Yo += oYstride;
597
    }
598
}
599

    
600
static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
601
{
602
    int i, j;
603
    int d;
604
    TM2_INIT_POINTERS_2();
605

    
606
    /* update chroma */
607
    for(j = 0; j < 2; j++){
608
        for(i = 0; i < 2; i++){
609
            U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
610
            V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
611
        }
612
        U += Ustride; V += Vstride;
613
        Uo += oUstride; Vo += oVstride;
614
    }
615
    U -= Ustride * 2;
616
    V -= Vstride * 2;
617
    TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
618
    TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
619

    
620
    /* update deltas */
621
    ctx->D[0] = Yo[3] - last[3];
622
    ctx->D[1] = Yo[3 + oYstride] - Yo[3];
623
    ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
624
    ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
625

    
626
    for(j = 0; j < 4; j++){
627
        d = last[3];
628
        for(i = 0; i < 4; i++){
629
            Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
630
            last[i] = Y[i];
631
        }
632
        ctx->D[j] = last[3] - d;
633
        Y += Ystride;
634
        Yo += oYstride;
635
    }
636
}
637

    
638
static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
639
{
640
    int i, j;
641
    int mx, my;
642
    TM2_INIT_POINTERS_2();
643

    
644
    mx = GET_TOK(ctx, TM2_MOT);
645
    my = GET_TOK(ctx, TM2_MOT);
646

    
647
    Yo += my * oYstride + mx;
648
    Uo += (my >> 1) * oUstride + (mx >> 1);
649
    Vo += (my >> 1) * oVstride + (mx >> 1);
650

    
651
    /* copy chroma */
652
    for(j = 0; j < 2; j++){
653
        for(i = 0; i < 2; i++){
654
            U[i] = Uo[i];
655
            V[i] = Vo[i];
656
        }
657
        U += Ustride; V += Vstride;
658
        Uo += oUstride; Vo += oVstride;
659
    }
660
    U -= Ustride * 2;
661
    V -= Vstride * 2;
662
    TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
663
    TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
664

    
665
    /* copy luma */
666
    for(j = 0; j < 4; j++){
667
        for(i = 0; i < 4; i++){
668
            Y[i] = Yo[i];
669
        }
670
        Y += Ystride;
671
        Yo += oYstride;
672
    }
673
    /* calculate deltas */
674
    Y -= Ystride * 4;
675
    ctx->D[0] = Y[3] - last[3];
676
    ctx->D[1] = Y[3 + Ystride] - Y[3];
677
    ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
678
    ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
679
    for(i = 0; i < 4; i++)
680
        last[i] = Y[i + Ystride * 3];
681
}
682

    
683
static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
684
{
685
    int i, j;
686
    int bw, bh;
687
    int type;
688
    int keyframe = 1;
689
    int *Y, *U, *V;
690
    uint8_t *dst;
691

    
692
    bw = ctx->avctx->width >> 2;
693
    bh = ctx->avctx->height >> 2;
694

    
695
    for(i = 0; i < TM2_NUM_STREAMS; i++)
696
        ctx->tok_ptrs[i] = 0;
697

    
698
    if (ctx->tok_lens[TM2_TYPE]<bw*bh){
699
        av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
700
        return -1;
701
    }
702

    
703
    memset(ctx->last, 0, 4 * bw * sizeof(int));
704
    memset(ctx->clast, 0, 4 * bw * sizeof(int));
705

    
706
    for(j = 0; j < bh; j++) {
707
        memset(ctx->D, 0, 4 * sizeof(int));
708
        memset(ctx->CD, 0, 4 * sizeof(int));
709
        for(i = 0; i < bw; i++) {
710
            type = GET_TOK(ctx, TM2_TYPE);
711
            switch(type) {
712
            case TM2_HI_RES:
713
                tm2_hi_res_block(ctx, p, i, j);
714
                break;
715
            case TM2_MED_RES:
716
                tm2_med_res_block(ctx, p, i, j);
717
                break;
718
            case TM2_LOW_RES:
719
                tm2_low_res_block(ctx, p, i, j);
720
                break;
721
            case TM2_NULL_RES:
722
                tm2_null_res_block(ctx, p, i, j);
723
                break;
724
            case TM2_UPDATE:
725
                tm2_update_block(ctx, p, i, j);
726
                keyframe = 0;
727
                break;
728
            case TM2_STILL:
729
                tm2_still_block(ctx, p, i, j);
730
                keyframe = 0;
731
                break;
732
            case TM2_MOTION:
733
                tm2_motion_block(ctx, p, i, j);
734
                keyframe = 0;
735
                break;
736
            default:
737
                av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
738
            }
739
        }
740
    }
741

    
742
    /* copy data from our buffer to AVFrame */
743
    Y = (ctx->cur?ctx->Y2:ctx->Y1);
744
    U = (ctx->cur?ctx->U2:ctx->U1);
745
    V = (ctx->cur?ctx->V2:ctx->V1);
746
    dst = p->data[0];
747
    for(j = 0; j < ctx->avctx->height; j++){
748
        for(i = 0; i < ctx->avctx->width; i++){
749
            int y = Y[i], u = U[i >> 1], v = V[i >> 1];
750
            dst[3*i+0] = av_clip_uint8(y + v);
751
            dst[3*i+1] = av_clip_uint8(y);
752
            dst[3*i+2] = av_clip_uint8(y + u);
753
        }
754
        Y += ctx->avctx->width;
755
        if (j & 1) {
756
            U += ctx->avctx->width >> 1;
757
            V += ctx->avctx->width >> 1;
758
        }
759
        dst += p->linesize[0];
760
    }
761

    
762
    return keyframe;
763
}
764

    
765
static const int tm2_stream_order[TM2_NUM_STREAMS] = {
766
    TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
767
};
768

    
769
static int decode_frame(AVCodecContext *avctx,
770
                        void *data, int *data_size,
771
                        AVPacket *avpkt)
772
{
773
    const uint8_t *buf = avpkt->data;
774
    int buf_size = avpkt->size;
775
    TM2Context * const l = avctx->priv_data;
776
    AVFrame * const p= (AVFrame*)&l->pic;
777
    int i, skip, t;
778
    uint8_t *swbuf;
779

    
780
    swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
781
    if(!swbuf){
782
        av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
783
        return -1;
784
    }
785
    p->reference = 1;
786
    p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
787
    if(avctx->reget_buffer(avctx, p) < 0){
788
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
789
        av_free(swbuf);
790
        return -1;
791
    }
792

    
793
    l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
794
    skip = tm2_read_header(l, swbuf);
795

    
796
    if(skip == -1){
797
        av_free(swbuf);
798
        return -1;
799
    }
800

    
801
    for(i = 0; i < TM2_NUM_STREAMS; i++){
802
        t = tm2_read_stream(l, swbuf + skip, tm2_stream_order[i], buf_size);
803
        if(t == -1){
804
            av_free(swbuf);
805
            return -1;
806
        }
807
        skip += t;
808
    }
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
    av_free(swbuf);
819

    
820
    return buf_size;
821
}
822

    
823
static av_cold int decode_init(AVCodecContext *avctx){
824
    TM2Context * const l = avctx->priv_data;
825
    int i;
826

    
827
    if((avctx->width & 3) || (avctx->height & 3)){
828
        av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
829
        return -1;
830
    }
831

    
832
    l->avctx = avctx;
833
    l->pic.data[0]=NULL;
834
    avctx->pix_fmt = PIX_FMT_BGR24;
835

    
836
    dsputil_init(&l->dsp, avctx);
837

    
838
    l->last = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
839
    l->clast = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
840

    
841
    for(i = 0; i < TM2_NUM_STREAMS; i++) {
842
        l->tokens[i] = NULL;
843
        l->tok_lens[i] = 0;
844
    }
845

    
846
    l->Y1 = av_malloc(sizeof(int) * avctx->width * avctx->height);
847
    l->U1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
848
    l->V1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
849
    l->Y2 = av_malloc(sizeof(int) * avctx->width * avctx->height);
850
    l->U2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
851
    l->V2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
852
    l->cur = 0;
853

    
854
    return 0;
855
}
856

    
857
static av_cold int decode_end(AVCodecContext *avctx){
858
    TM2Context * const l = avctx->priv_data;
859
    AVFrame *pic = &l->pic;
860
    int i;
861

    
862
    if(l->last)
863
        av_free(l->last);
864
    if(l->clast)
865
        av_free(l->clast);
866
    for(i = 0; i < TM2_NUM_STREAMS; i++)
867
        if(l->tokens[i])
868
            av_free(l->tokens[i]);
869
    if(l->Y1){
870
        av_free(l->Y1);
871
        av_free(l->U1);
872
        av_free(l->V1);
873
        av_free(l->Y2);
874
        av_free(l->U2);
875
        av_free(l->V2);
876
    }
877

    
878
    if (pic->data[0])
879
        avctx->release_buffer(avctx, pic);
880

    
881
    return 0;
882
}
883

    
884
AVCodec truemotion2_decoder = {
885
    "truemotion2",
886
    AVMEDIA_TYPE_VIDEO,
887
    CODEC_ID_TRUEMOTION2,
888
    sizeof(TM2Context),
889
    decode_init,
890
    NULL,
891
    decode_end,
892
    decode_frame,
893
    CODEC_CAP_DR1,
894
    .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
895
};