GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/truemotion2.c Lines: 481 573 83.9 %
Date: 2020-08-14 10:39:37 Branches: 253 327 77.4 %

Line Branch Exec Source
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 <inttypes.h>
28
29
#include "avcodec.h"
30
#include "bswapdsp.h"
31
#include "bytestream.h"
32
#include "get_bits.h"
33
#include "internal.h"
34
35
#define TM2_ESCAPE 0x80000000
36
#define TM2_DELTAS 64
37
38
/* Huffman-coded streams of different types of blocks */
39
enum TM2_STREAMS {
40
    TM2_C_HI = 0,
41
    TM2_C_LO,
42
    TM2_L_HI,
43
    TM2_L_LO,
44
    TM2_UPD,
45
    TM2_MOT,
46
    TM2_TYPE,
47
    TM2_NUM_STREAMS
48
};
49
50
/* Block types */
51
enum TM2_BLOCKS {
52
    TM2_HI_RES = 0,
53
    TM2_MED_RES,
54
    TM2_LOW_RES,
55
    TM2_NULL_RES,
56
    TM2_UPDATE,
57
    TM2_STILL,
58
    TM2_MOTION
59
};
60
61
typedef struct TM2Context {
62
    AVCodecContext *avctx;
63
    AVFrame *pic;
64
65
    GetBitContext gb;
66
    int error;
67
    BswapDSPContext bdsp;
68
69
    uint8_t *buffer;
70
    int buffer_size;
71
72
    /* TM2 streams */
73
    int *tokens[TM2_NUM_STREAMS];
74
    int tok_lens[TM2_NUM_STREAMS];
75
    int tok_ptrs[TM2_NUM_STREAMS];
76
    int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
77
    /* for blocks decoding */
78
    int D[4];
79
    int CD[4];
80
    int *last;
81
    int *clast;
82
83
    /* data for current and previous frame */
84
    int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
85
    int *Y1, *U1, *V1, *Y2, *U2, *V2;
86
    int y_stride, uv_stride;
87
    int cur;
88
} TM2Context;
89
90
/**
91
* Huffman codes for each of streams
92
*/
93
typedef struct TM2Codes {
94
    VLC vlc; ///< table for FFmpeg bitstream reader
95
    int bits;
96
    int *recode; ///< table for converting from code indexes to values
97
    int length;
98
} TM2Codes;
99
100
/**
101
* structure for gathering Huffman codes information
102
*/
103
typedef struct TM2Huff {
104
    int val_bits; ///< length of literal
105
    int max_bits; ///< maximum length of code
106
    int min_bits; ///< minimum length of code
107
    int nodes; ///< total number of nodes in tree
108
    int num; ///< current number filled
109
    int max_num; ///< total number of codes
110
    int *nums; ///< literals
111
    uint32_t *bits; ///< codes
112
    int *lens; ///< codelengths
113
} TM2Huff;
114
115
/**
116
 *
117
 * @returns the length of the longest code or an AVERROR code
118
 */
119
3148
static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
120
{
121
    int ret, ret2;
122
3148
    if (length > huff->max_bits) {
123
        av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
124
               huff->max_bits);
125
        return AVERROR_INVALIDDATA;
126
    }
127
128
3148
    if (!get_bits1(&ctx->gb)) { /* literal */
129
1670
        if (length == 0) {
130
3
            length = 1;
131
        }
132
1670
        if (huff->num >= huff->max_num) {
133
            av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
134
            return AVERROR_INVALIDDATA;
135
        }
136
1670
        huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
137
1670
        huff->bits[huff->num] = prefix;
138
1670
        huff->lens[huff->num] = length;
139
1670
        huff->num++;
140
1670
        return length;
141
    } else { /* non-terminal node */
142
1478
        if ((ret2 = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
143
            return ret2;
144
1478
        if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
145
            return ret;
146
    }
147
1478
    return FFMAX(ret, ret2);
148
}
149
150
192
static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
151
{
152
    TM2Huff huff;
153
192
    int res = 0;
154
155
192
    huff.val_bits = get_bits(&ctx->gb, 5);
156
192
    huff.max_bits = get_bits(&ctx->gb, 5);
157
192
    huff.min_bits = get_bits(&ctx->gb, 5);
158
192
    huff.nodes    = get_bits(&ctx->gb, 17);
159
192
    huff.num      = 0;
160
161
    /* check for correct codes parameters */
162

192
    if ((huff.val_bits < 1) || (huff.val_bits > 32) ||
163

192
        (huff.max_bits < 0) || (huff.max_bits > 25)) {
164
        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal "
165
               "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits);
166
        return AVERROR_INVALIDDATA;
167
    }
168

192
    if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
169
        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree "
170
               "nodes: %i\n", huff.nodes);
171
        return AVERROR_INVALIDDATA;
172
    }
173
    /* one-node tree */
174
192
    if (huff.max_bits == 0)
175
3
        huff.max_bits = 1;
176
177
    /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
178
192
    huff.max_num = (huff.nodes + 1) >> 1;
179
192
    huff.nums    = av_calloc(huff.max_num, sizeof(int));
180
192
    huff.bits    = av_calloc(huff.max_num, sizeof(uint32_t));
181
192
    huff.lens    = av_calloc(huff.max_num, sizeof(int));
182
183

192
    if (!huff.nums || !huff.bits || !huff.lens) {
184
        res = AVERROR(ENOMEM);
185
        goto out;
186
    }
187
188
192
    res = tm2_read_tree(ctx, 0, 0, &huff);
189
190

192
    if (res >= 0 && res != huff.max_bits) {
191
        av_log(ctx->avctx, AV_LOG_ERROR, "Got less bits than expected: %i of %i\n",
192
               res, huff.max_bits);
193
        res = AVERROR_INVALIDDATA;
194
    }
195
192
    if (huff.num != huff.max_num) {
196
        av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
197
               huff.num, huff.max_num);
198
        res = AVERROR_INVALIDDATA;
199
    }
200
201
    /* convert codes to vlc_table */
202
192
    if (res >= 0) {
203
        int i;
204
205
192
        res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
206
                       huff.lens, sizeof(int), sizeof(int),
207
                       huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
208
192
        if (res < 0)
209
            av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
210
        else {
211
192
            code->bits = huff.max_bits;
212
192
            code->length = huff.max_num;
213
192
            code->recode = av_malloc_array(code->length, sizeof(int));
214
192
            if (!code->recode) {
215
                res = AVERROR(ENOMEM);
216
                goto out;
217
            }
218
1862
            for (i = 0; i < code->length; i++)
219
1670
                code->recode[i] = huff.nums[i];
220
        }
221
    }
222
223
192
out:
224
    /* free allocated memory */
225
192
    av_free(huff.nums);
226
192
    av_free(huff.bits);
227
192
    av_free(huff.lens);
228
229
192
    return res;
230
}
231
232
192
static void tm2_free_codes(TM2Codes *code)
233
{
234
192
    av_free(code->recode);
235
192
    if (code->vlc.table)
236
192
        ff_free_vlc(&code->vlc);
237
192
}
238
239
2353240
static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
240
{
241
    int val;
242
2353240
    val = get_vlc2(gb, code->vlc.table, code->bits, 1);
243
2353240
    if(val<0)
244
        return -1;
245
2353240
    return code->recode[val];
246
}
247
248
#define TM2_OLD_HEADER_MAGIC 0x00000100
249
#define TM2_NEW_HEADER_MAGIC 0x00000101
250
251
30
static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
252
{
253
30
    uint32_t magic = AV_RL32(buf);
254
255
30
    switch (magic) {
256
    case TM2_OLD_HEADER_MAGIC:
257
        avpriv_request_sample(ctx->avctx, "Old TM2 header");
258
        return 0;
259
30
    case TM2_NEW_HEADER_MAGIC:
260
30
        return 0;
261
    default:
262
        av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n",
263
               magic);
264
        return AVERROR_INVALIDDATA;
265
    }
266
}
267
268
29
static int tm2_read_deltas(TM2Context *ctx, int stream_id)
269
{
270
    int d, mb;
271
    int i, v;
272
273
29
    d  = get_bits(&ctx->gb, 9);
274
29
    mb = get_bits(&ctx->gb, 5);
275
276
    av_assert2(mb < 32);
277

29
    if ((d < 1) || (d > TM2_DELTAS) || (mb < 1)) {
278
        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
279
        return AVERROR_INVALIDDATA;
280
    }
281
282
594
    for (i = 0; i < d; i++) {
283
565
        v = get_bits_long(&ctx->gb, mb);
284
565
        if (v & (1 << (mb - 1)))
285
270
            ctx->deltas[stream_id][i] = v - (1U << mb);
286
        else
287
295
            ctx->deltas[stream_id][i] = v;
288
    }
289
1320
    for (; i < TM2_DELTAS; i++)
290
1291
        ctx->deltas[stream_id][i] = 0;
291
292
29
    return 0;
293
}
294
295
210
static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
296
{
297
    int i, ret;
298
210
    int skip = 0;
299
    int len, toks, pos;
300
    TM2Codes codes;
301
    GetByteContext gb;
302
303
210
    if (buf_size < 4) {
304
        av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n");
305
        return AVERROR_INVALIDDATA;
306
    }
307
308
    /* get stream length in dwords */
309
210
    bytestream2_init(&gb, buf, buf_size);
310
210
    len  = bytestream2_get_be32(&gb);
311
312
210
    if (len == 0)
313
18
        return 4;
314
315

192
    if (len >= INT_MAX / 4 - 1 || len < 0 || len * 4 + 4 > buf_size) {
316
        av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
317
        return AVERROR_INVALIDDATA;
318
    }
319
192
    skip = len * 4 + 4;
320
321
192
    toks = bytestream2_get_be32(&gb);
322
192
    if (toks & 1) {
323
162
        len = bytestream2_get_be32(&gb);
324
162
        if (len == TM2_ESCAPE) {
325
5
            len = bytestream2_get_be32(&gb);
326
        }
327
162
        if (len > 0) {
328
29
            pos = bytestream2_tell(&gb);
329
29
            if (skip <= pos)
330
                return AVERROR_INVALIDDATA;
331
29
            init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
332
29
            if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
333
                return ret;
334
29
            bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
335
        }
336
    }
337
    /* skip unused fields */
338
192
    len = bytestream2_get_be32(&gb);
339
192
    if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
340
        bytestream2_skip(&gb, 8); /* unused by decoder */
341
    } else {
342
192
        bytestream2_skip(&gb, 4); /* unused by decoder */
343
    }
344
345
192
    pos = bytestream2_tell(&gb);
346
192
    if (skip <= pos)
347
        return AVERROR_INVALIDDATA;
348
192
    init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
349
192
    if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
350
        return ret;
351
192
    bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
352
353
192
    toks >>= 1;
354
    /* check if we have sane number of tokens */
355

192
    if ((toks < 0) || (toks > 0xFFFFFF)) {
356
        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
357
        ret = AVERROR_INVALIDDATA;
358
        goto end;
359
    }
360
192
    ret = av_reallocp_array(&ctx->tokens[stream_id], toks, sizeof(int));
361
192
    if (ret < 0) {
362
        ctx->tok_lens[stream_id] = 0;
363
        goto end;
364
    }
365
192
    ctx->tok_lens[stream_id] = toks;
366
192
    len = bytestream2_get_be32(&gb);
367
192
    if (len > 0) {
368
189
        pos = bytestream2_tell(&gb);
369
189
        if (skip <= pos) {
370
            ret = AVERROR_INVALIDDATA;
371
            goto end;
372
        }
373
189
        init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
374
2353429
        for (i = 0; i < toks; i++) {
375
2353240
            if (get_bits_left(&ctx->gb) <= 0) {
376
                av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
377
                ret = AVERROR_INVALIDDATA;
378
                goto end;
379
            }
380
2353240
            ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
381

2353240
            if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS || ctx->tokens[stream_id][i]<0) {
382
                av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
383
                       ctx->tokens[stream_id][i], stream_id, i);
384
                ret = AVERROR_INVALIDDATA;
385
                goto end;
386
            }
387
        }
388
    } else {
389
3
        if (len < 0) {
390
            ret = AVERROR_INVALIDDATA;
391
            goto end;
392
        }
393
21
        for (i = 0; i < toks; i++) {
394
18
            ctx->tokens[stream_id][i] = codes.recode[0];
395

18
            if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
396
                av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
397
                       ctx->tokens[stream_id][i], stream_id, i);
398
                ret = AVERROR_INVALIDDATA;
399
                goto end;
400
            }
401
        }
402
    }
403
404
192
    ret = skip;
405
406
192
end:
407
192
    tm2_free_codes(&codes);
408
192
    return ret;
409
}
410
411
2353258
static inline int GET_TOK(TM2Context *ctx,int type)
412
{
413
2353258
    if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
414
        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]);
415
        ctx->error = 1;
416
        return 0;
417
    }
418
2353258
    if (type <= TM2_MOT) {
419
2209258
        if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) {
420
            av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]);
421
            return 0;
422
        }
423
2209258
        return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
424
    }
425
144000
    return ctx->tokens[type][ctx->tok_ptrs[type]++];
426
}
427
428
/* blocks decoding routines */
429
430
/* common Y, U, V pointers initialisation */
431
#define TM2_INIT_POINTERS() \
432
    int *last, *clast; \
433
    int *Y, *U, *V;\
434
    int Ystride, Ustride, Vstride;\
435
\
436
    Ystride = ctx->y_stride;\
437
    Vstride = ctx->uv_stride;\
438
    Ustride = ctx->uv_stride;\
439
    Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
440
    V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
441
    U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
442
    last = ctx->last + bx * 4;\
443
    clast = ctx->clast + bx * 4;
444
445
#define TM2_INIT_POINTERS_2() \
446
    unsigned *Yo, *Uo, *Vo;\
447
    int oYstride, oUstride, oVstride;\
448
\
449
    TM2_INIT_POINTERS();\
450
    oYstride = Ystride;\
451
    oVstride = Vstride;\
452
    oUstride = Ustride;\
453
    Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
454
    Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
455
    Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
456
457
/* recalculate last and delta values for next blocks */
458
#define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
459
    CD[0] = (unsigned)CHR[         1] - (unsigned)last[1];\
460
    CD[1] = (unsigned)CHR[stride + 1] - (unsigned) CHR[1];\
461
    last[0] = (int)CHR[stride + 0];\
462
    last[1] = (int)CHR[stride + 1];}
463
464
/* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
465
116660
static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
466
{
467
    unsigned ct, d;
468
    int i, j;
469
470
583300
    for (j = 0; j < 4; j++){
471
466640
        ct = ctx->D[j];
472
2333200
        for (i = 0; i < 4; i++){
473
1866560
            d        = deltas[i + j * 4];
474
1866560
            ct      += d;
475
1866560
            last[i] += ct;
476
1866560
            Y[i]     = av_clip_uint8(last[i]);
477
        }
478
466640
        Y        += stride;
479
466640
        ctx->D[j] = ct;
480
    }
481
116660
}
482
483
233320
static inline void tm2_high_chroma(int *data, int stride, int *last, unsigned *CD, int *deltas)
484
{
485
    int i, j;
486
699960
    for (j = 0; j < 2; j++) {
487
1399920
        for (i = 0; i < 2; i++)  {
488
933280
            CD[j]   += deltas[i + j * 2];
489
933280
            last[i] += CD[j];
490
933280
            data[i]  = last[i];
491
        }
492
466640
        data += stride;
493
    }
494
233320
}
495
496
99888
static inline void tm2_low_chroma(int *data, int stride, int *clast, unsigned *CD, int *deltas, int bx)
497
{
498
    int t;
499
    int l;
500
    int prev;
501
502
99888
    if (bx > 0)
503
99060
        prev = clast[-3];
504
    else
505
828
        prev = 0;
506
99888
    t        = (int)(CD[0] + CD[1]) >> 1;
507
99888
    l        = (int)(prev - CD[0] - CD[1] + clast[1]) >> 1;
508
99888
    CD[1]    = CD[0] + CD[1] - t;
509
99888
    CD[0]    = t;
510
99888
    clast[0] = l;
511
512
99888
    tm2_high_chroma(data, stride, clast, CD, deltas);
513
99888
}
514
515
66716
static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
516
{
517
    int i;
518
    int deltas[16];
519

66716
    TM2_INIT_POINTERS();
520
521
    /* hi-res chroma */
522
333580
    for (i = 0; i < 4; i++) {
523
266864
        deltas[i]     = GET_TOK(ctx, TM2_C_HI);
524
266864
        deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
525
    }
526
66716
    tm2_high_chroma(U, Ustride, clast,     ctx->CD,     deltas);
527
66716
    tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
528
529
    /* hi-res luma */
530
1134172
    for (i = 0; i < 16; i++)
531
1067456
        deltas[i] = GET_TOK(ctx, TM2_L_HI);
532
533
66716
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
534
66716
}
535
536
22463
static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
537
{
538
    int i;
539
    int deltas[16];
540

22463
    TM2_INIT_POINTERS();
541
542
    /* low-res chroma */
543
22463
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
544
22463
    deltas[1] = deltas[2] = deltas[3] = 0;
545
22463
    tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
546
547
22463
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
548
22463
    deltas[1] = deltas[2] = deltas[3] = 0;
549
22463
    tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
550
551
    /* hi-res luma */
552
381871
    for (i = 0; i < 16; i++)
553
359408
        deltas[i] = GET_TOK(ctx, TM2_L_HI);
554
555
22463
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
556
22463
}
557
558
12092
static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
559
{
560
    int i;
561
    int t1, t2;
562
    int deltas[16];
563

12092
    TM2_INIT_POINTERS();
564
565
    /* low-res chroma */
566
12092
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
567
12092
    deltas[1] = deltas[2] = deltas[3] = 0;
568
12092
    tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
569
570
12092
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
571
12092
    deltas[1] = deltas[2] = deltas[3] = 0;
572
12092
    tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
573
574
    /* low-res luma */
575
205564
    for (i = 0; i < 16; i++)
576
193472
        deltas[i] = 0;
577
578
12092
    deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
579
12092
    deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
580
12092
    deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
581
12092
    deltas[10] = GET_TOK(ctx, TM2_L_LO);
582
583
12092
    if (bx > 0)
584
12025
        last[0] = (int)((unsigned)last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
585
    else
586
67
        last[0] = (int)((unsigned)last[1]  - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
587
12092
    last[2] = (int)((unsigned)last[1] + last[3]) >> 1;
588
589
12092
    t1 = ctx->D[0] + (unsigned)ctx->D[1];
590
12092
    ctx->D[0] = t1 >> 1;
591
12092
    ctx->D[1] = t1 - (t1 >> 1);
592
12092
    t2 = ctx->D[2] + (unsigned)ctx->D[3];
593
12092
    ctx->D[2] = t2 >> 1;
594
12092
    ctx->D[3] = t2 - (t2 >> 1);
595
596
12092
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
597
12092
}
598
599
15389
static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
600
{
601
    int i;
602
    int ct;
603
    unsigned left, right;
604
    int diff;
605
    int deltas[16];
606

15389
    TM2_INIT_POINTERS();
607
608
    /* null chroma */
609
15389
    deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
610
15389
    tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
611
612
15389
    deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
613
15389
    tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
614
615
    /* null luma */
616
261613
    for (i = 0; i < 16; i++)
617
246224
        deltas[i] = 0;
618
619
15389
    ct = (unsigned)ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
620
621
15389
    if (bx > 0)
622
15315
        left = last[-1] - (unsigned)ct;
623
    else
624
74
        left = 0;
625
626
15389
    right   = last[3];
627
15389
    diff    = right - left;
628
15389
    last[0] = left + (diff >> 2);
629
15389
    last[1] = left + (diff >> 1);
630
15389
    last[2] = right - (diff >> 2);
631
15389
    last[3] = right;
632
    {
633
15389
        unsigned tp = left;
634
635
15389
        ctx->D[0] = (tp + (ct >> 2)) - left;
636
15389
        left     += ctx->D[0];
637
15389
        ctx->D[1] = (tp + (ct >> 1)) - left;
638
15389
        left     += ctx->D[1];
639
15389
        ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
640
15389
        left     += ctx->D[2];
641
15389
        ctx->D[3] = (tp + ct) - left;
642
    }
643
15389
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
644
15389
}
645
646
20959
static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
647
{
648
    int i, j;
649



20959
    TM2_INIT_POINTERS_2();
650
651
    /* update chroma */
652
62877
    for (j = 0; j < 2; j++) {
653
125754
        for (i = 0; i < 2; i++){
654
83836
            U[i] = Uo[i];
655
83836
            V[i] = Vo[i];
656
        }
657
41918
        U  += Ustride; V += Vstride;
658
41918
        Uo += oUstride; Vo += oVstride;
659
    }
660
20959
    U -= Ustride * 2;
661
20959
    V -= Vstride * 2;
662
20959
    TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
663
20959
    TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
664
665
    /* update deltas */
666
20959
    ctx->D[0] = Yo[3] - last[3];
667
20959
    ctx->D[1] = Yo[3 + oYstride] - Yo[3];
668
20959
    ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
669
20959
    ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
670
671
104795
    for (j = 0; j < 4; j++) {
672
419180
        for (i = 0; i < 4; i++) {
673
335344
            Y[i]    = Yo[i];
674
335344
            last[i] = Yo[i];
675
        }
676
83836
        Y  += Ystride;
677
83836
        Yo += oYstride;
678
    }
679
20959
}
680
681
5383
static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
682
{
683
    int i, j;
684
    unsigned d;
685



5383
    TM2_INIT_POINTERS_2();
686
687
    /* update chroma */
688
16149
    for (j = 0; j < 2; j++) {
689
32298
        for (i = 0; i < 2; i++) {
690
21532
            U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
691
21532
            V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
692
        }
693
10766
        U  += Ustride;
694
10766
        V  += Vstride;
695
10766
        Uo += oUstride;
696
10766
        Vo += oVstride;
697
    }
698
5383
    U -= Ustride * 2;
699
5383
    V -= Vstride * 2;
700
5383
    TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
701
5383
    TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
702
703
    /* update deltas */
704
5383
    ctx->D[0] = Yo[3] - last[3];
705
5383
    ctx->D[1] = Yo[3 + oYstride] - Yo[3];
706
5383
    ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
707
5383
    ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
708
709
26915
    for (j = 0; j < 4; j++) {
710
21532
        d = last[3];
711
107660
        for (i = 0; i < 4; i++) {
712
86128
            Y[i]    = Yo[i] + (unsigned)GET_TOK(ctx, TM2_UPD);
713
86128
            last[i] = Y[i];
714
        }
715
21532
        ctx->D[j] = last[3] - d;
716
21532
        Y  += Ystride;
717
21532
        Yo += oYstride;
718
    }
719
5383
}
720
721
998
static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
722
{
723
    int i, j;
724
    int mx, my;
725



998
    TM2_INIT_POINTERS_2();
726
727
998
    mx = GET_TOK(ctx, TM2_MOT);
728
998
    my = GET_TOK(ctx, TM2_MOT);
729
998
    mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width  - bx * 4);
730
998
    my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
731
732


998
    if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) {
733
        av_log(ctx->avctx, AV_LOG_ERROR, "MV out of picture\n");
734
        return;
735
    }
736
737
998
    Yo += my * oYstride + mx;
738
998
    Uo += (my >> 1) * oUstride + (mx >> 1);
739
998
    Vo += (my >> 1) * oVstride + (mx >> 1);
740
741
    /* copy chroma */
742
2994
    for (j = 0; j < 2; j++) {
743
5988
        for (i = 0; i < 2; i++) {
744
3992
            U[i] = Uo[i];
745
3992
            V[i] = Vo[i];
746
        }
747
1996
        U  += Ustride;
748
1996
        V  += Vstride;
749
1996
        Uo += oUstride;
750
1996
        Vo += oVstride;
751
    }
752
998
    U -= Ustride * 2;
753
998
    V -= Vstride * 2;
754
998
    TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
755
998
    TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
756
757
    /* copy luma */
758
4990
    for (j = 0; j < 4; j++) {
759
19960
        for (i = 0; i < 4; i++) {
760
15968
            Y[i] = Yo[i];
761
        }
762
3992
        Y  += Ystride;
763
3992
        Yo += oYstride;
764
    }
765
    /* calculate deltas */
766
998
    Y -= Ystride * 4;
767
998
    ctx->D[0] = (unsigned)Y[3] - last[3];
768
998
    ctx->D[1] = (unsigned)Y[3 + Ystride] - Y[3];
769
998
    ctx->D[2] = (unsigned)Y[3 + Ystride * 2] - Y[3 + Ystride];
770
998
    ctx->D[3] = (unsigned)Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
771
4990
    for (i = 0; i < 4; i++)
772
3992
        last[i] = Y[i + Ystride * 3];
773
}
774
775
30
static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
776
{
777
    int i, j;
778
30
    int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
779
    int type;
780
30
    int keyframe = 1;
781
    int *Y, *U, *V;
782
    uint8_t *dst;
783
784
240
    for (i = 0; i < TM2_NUM_STREAMS; i++)
785
210
        ctx->tok_ptrs[i] = 0;
786
787
30
    if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
788
        av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
789
        return AVERROR_INVALIDDATA;
790
    }
791
792
30
    memset(ctx->last, 0, 4 * bw * sizeof(int));
793
30
    memset(ctx->clast, 0, 4 * bw * sizeof(int));
794
795
1830
    for (j = 0; j < bh; j++) {
796
1800
        memset(ctx->D, 0, 4 * sizeof(int));
797
1800
        memset(ctx->CD, 0, 4 * sizeof(int));
798
145800
        for (i = 0; i < bw; i++) {
799
144000
            type = GET_TOK(ctx, TM2_TYPE);
800


144000
            switch(type) {
801
66716
            case TM2_HI_RES:
802
66716
                tm2_hi_res_block(ctx, p, i, j);
803
66716
                break;
804
22463
            case TM2_MED_RES:
805
22463
                tm2_med_res_block(ctx, p, i, j);
806
22463
                break;
807
12092
            case TM2_LOW_RES:
808
12092
                tm2_low_res_block(ctx, p, i, j);
809
12092
                break;
810
15389
            case TM2_NULL_RES:
811
15389
                tm2_null_res_block(ctx, p, i, j);
812
15389
                break;
813
5383
            case TM2_UPDATE:
814
5383
                tm2_update_block(ctx, p, i, j);
815
5383
                keyframe = 0;
816
5383
                break;
817
20959
            case TM2_STILL:
818
20959
                tm2_still_block(ctx, p, i, j);
819
20959
                keyframe = 0;
820
20959
                break;
821
998
            case TM2_MOTION:
822
998
                tm2_motion_block(ctx, p, i, j);
823
998
                keyframe = 0;
824
998
                break;
825
            default:
826
                av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
827
            }
828
144000
            if (ctx->error)
829
                return AVERROR_INVALIDDATA;
830
        }
831
    }
832
833
    /* copy data from our buffer to AVFrame */
834
30
    Y = (ctx->cur?ctx->Y2:ctx->Y1);
835
30
    U = (ctx->cur?ctx->U2:ctx->U1);
836
30
    V = (ctx->cur?ctx->V2:ctx->V1);
837
30
    dst = p->data[0];
838
7230
    for (j = 0; j < h; j++) {
839
2311200
        for (i = 0; i < w; i++) {
840
2304000
            unsigned y = Y[i], u = U[i >> 1], v = V[i >> 1];
841
2304000
            dst[3*i+0] = av_clip_uint8(y + v);
842
2304000
            dst[3*i+1] = av_clip_uint8(y);
843
2304000
            dst[3*i+2] = av_clip_uint8(y + u);
844
        }
845
846
        /* horizontal edge extension */
847
7200
        Y[-4]    = Y[-3]    = Y[-2]    = Y[-1] = Y[0];
848
7200
        Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w]  = Y[w - 1];
849
850
        /* vertical edge extension */
851
7200
        if (j == 0) {
852
30
            memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
853
30
            memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
854
30
            memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
855
30
            memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
856
7170
        } else if (j == h - 1) {
857
30
            memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
858
30
            memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
859
30
            memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
860
30
            memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
861
        }
862
863
7200
        Y += ctx->y_stride;
864
7200
        if (j & 1) {
865
            /* horizontal edge extension */
866
3600
            U[-2]     = U[-1] = U[0];
867
3600
            V[-2]     = V[-1] = V[0];
868
3600
            U[cw + 1] = U[cw] = U[cw - 1];
869
3600
            V[cw + 1] = V[cw] = V[cw - 1];
870
871
            /* vertical edge extension */
872
3600
            if (j == 1) {
873
30
                memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
874
30
                memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
875
30
                memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
876
30
                memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
877
3570
            } else if (j == h - 1) {
878
30
                memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
879
30
                memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
880
30
                memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
881
30
                memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
882
            }
883
884
3600
            U += ctx->uv_stride;
885
3600
            V += ctx->uv_stride;
886
        }
887
7200
        dst += p->linesize[0];
888
    }
889
890
30
    return keyframe;
891
}
892
893
static const int tm2_stream_order[TM2_NUM_STREAMS] = {
894
    TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
895
};
896
897
#define TM2_HEADER_SIZE 40
898
899
30
static int decode_frame(AVCodecContext *avctx,
900
                        void *data, int *got_frame,
901
                        AVPacket *avpkt)
902
{
903
30
    TM2Context * const l = avctx->priv_data;
904
30
    const uint8_t *buf   = avpkt->data;
905
30
    int buf_size         = avpkt->size & ~3;
906
30
    AVFrame * const p    = l->pic;
907
30
    int offset           = TM2_HEADER_SIZE;
908
    int i, t, ret;
909
910
30
    l->error = 0;
911
912
30
    av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size);
913
30
    if (!l->buffer) {
914
        av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
915
        return AVERROR(ENOMEM);
916
    }
917
918
30
    if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
919
        return ret;
920
921
30
    l->bdsp.bswap_buf((uint32_t *) l->buffer, (const uint32_t *) buf,
922
                      buf_size >> 2);
923
924
30
    if ((ret = tm2_read_header(l, l->buffer)) < 0) {
925
        return ret;
926
    }
927
928
240
    for (i = 0; i < TM2_NUM_STREAMS; i++) {
929
210
        if (offset >= buf_size) {
930
            av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n");
931
            return AVERROR_INVALIDDATA;
932
        }
933
934
210
        t = tm2_read_stream(l, l->buffer + offset, tm2_stream_order[i],
935
                            buf_size - offset);
936
210
        if (t < 0) {
937
            int j = tm2_stream_order[i];
938
            if (l->tok_lens[j])
939
                memset(l->tokens[j], 0, sizeof(**l->tokens) * l->tok_lens[j]);
940
            return t;
941
        }
942
210
        offset += t;
943
    }
944
30
    p->key_frame = tm2_decode_blocks(l, p);
945
30
    if (p->key_frame)
946
5
        p->pict_type = AV_PICTURE_TYPE_I;
947
    else
948
25
        p->pict_type = AV_PICTURE_TYPE_P;
949
950
30
    l->cur = !l->cur;
951
30
    *got_frame      = 1;
952
30
    ret = av_frame_ref(data, l->pic);
953
954
30
    return (ret < 0) ? ret : buf_size;
955
}
956
957
2
static av_cold int decode_init(AVCodecContext *avctx)
958
{
959
2
    TM2Context * const l = avctx->priv_data;
960
2
    int i, w = avctx->width, h = avctx->height;
961
962

2
    if ((avctx->width & 3) || (avctx->height & 3)) {
963
        av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
964
        return AVERROR(EINVAL);
965
    }
966
967
2
    l->avctx       = avctx;
968
2
    avctx->pix_fmt = AV_PIX_FMT_BGR24;
969
970
2
    l->pic = av_frame_alloc();
971
2
    if (!l->pic)
972
        return AVERROR(ENOMEM);
973
974
2
    ff_bswapdsp_init(&l->bdsp);
975
976
2
    l->last  = av_malloc_array(w >> 2, 4 * sizeof(*l->last) );
977
2
    l->clast = av_malloc_array(w >> 2, 4 * sizeof(*l->clast));
978
979
16
    for (i = 0; i < TM2_NUM_STREAMS; i++) {
980
14
        l->tokens[i] = NULL;
981
14
        l->tok_lens[i] = 0;
982
    }
983
984
2
    w += 8;
985
2
    h += 8;
986
2
    l->Y1_base = av_calloc(w * h, sizeof(*l->Y1_base));
987
2
    l->Y2_base = av_calloc(w * h, sizeof(*l->Y2_base));
988
2
    l->y_stride = w;
989
2
    w = (w + 1) >> 1;
990
2
    h = (h + 1) >> 1;
991
2
    l->U1_base = av_calloc(w * h, sizeof(*l->U1_base));
992
2
    l->V1_base = av_calloc(w * h, sizeof(*l->V1_base));
993
2
    l->U2_base = av_calloc(w * h, sizeof(*l->U2_base));
994
2
    l->V2_base = av_calloc(w * h, sizeof(*l->V1_base));
995
2
    l->uv_stride = w;
996
2
    l->cur = 0;
997

2
    if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
998

2
        !l->V1_base || !l->U2_base || !l->V2_base ||
999

2
        !l->last    || !l->clast) {
1000
        av_freep(&l->Y1_base);
1001
        av_freep(&l->Y2_base);
1002
        av_freep(&l->U1_base);
1003
        av_freep(&l->U2_base);
1004
        av_freep(&l->V1_base);
1005
        av_freep(&l->V2_base);
1006
        av_freep(&l->last);
1007
        av_freep(&l->clast);
1008
        av_frame_free(&l->pic);
1009
        return AVERROR(ENOMEM);
1010
    }
1011
2
    l->Y1 = l->Y1_base + l->y_stride  * 4 + 4;
1012
2
    l->Y2 = l->Y2_base + l->y_stride  * 4 + 4;
1013
2
    l->U1 = l->U1_base + l->uv_stride * 2 + 2;
1014
2
    l->U2 = l->U2_base + l->uv_stride * 2 + 2;
1015
2
    l->V1 = l->V1_base + l->uv_stride * 2 + 2;
1016
2
    l->V2 = l->V2_base + l->uv_stride * 2 + 2;
1017
1018
2
    return 0;
1019
}
1020
1021
2
static av_cold int decode_end(AVCodecContext *avctx)
1022
{
1023
2
    TM2Context * const l = avctx->priv_data;
1024
    int i;
1025
1026
2
    av_free(l->last);
1027
2
    av_free(l->clast);
1028
16
    for (i = 0; i < TM2_NUM_STREAMS; i++)
1029
14
        av_freep(&l->tokens[i]);
1030
2
    if (l->Y1) {
1031
2
        av_freep(&l->Y1_base);
1032
2
        av_freep(&l->U1_base);
1033
2
        av_freep(&l->V1_base);
1034
2
        av_freep(&l->Y2_base);
1035
2
        av_freep(&l->U2_base);
1036
2
        av_freep(&l->V2_base);
1037
    }
1038
2
    av_freep(&l->buffer);
1039
2
    l->buffer_size = 0;
1040
1041
2
    av_frame_free(&l->pic);
1042
1043
2
    return 0;
1044
}
1045
1046
AVCodec ff_truemotion2_decoder = {
1047
    .name           = "truemotion2",
1048
    .long_name      = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
1049
    .type           = AVMEDIA_TYPE_VIDEO,
1050
    .id             = AV_CODEC_ID_TRUEMOTION2,
1051
    .priv_data_size = sizeof(TM2Context),
1052
    .init           = decode_init,
1053
    .close          = decode_end,
1054
    .decode         = decode_frame,
1055
    .capabilities   = AV_CODEC_CAP_DR1,
1056
};