GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/truemotion2.c Lines: 463 547 84.6 %
Date: 2021-04-15 16:04:23 Branches: 241 307 78.5 %

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 *Y_base, *UV_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
    uint8_t *lens; ///< codelengths
112
} TM2Huff;
113
114
/**
115
 *
116
 * @returns the length of the longest code or an AVERROR code
117
 */
118
3148
static int tm2_read_tree(TM2Context *ctx, int length, TM2Huff *huff)
119
{
120
    int ret, ret2;
121
3148
    if (length > huff->max_bits) {
122
        av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
123
               huff->max_bits);
124
        return AVERROR_INVALIDDATA;
125
    }
126
127
3148
    if (!get_bits1(&ctx->gb)) { /* literal */
128
1670
        if (length == 0) {
129
3
            length = 1;
130
        }
131
1670
        if (huff->num >= huff->max_num) {
132
            av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
133
            return AVERROR_INVALIDDATA;
134
        }
135
1670
        huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
136
1670
        huff->lens[huff->num] = length;
137
1670
        huff->num++;
138
1670
        return length;
139
    } else { /* non-terminal node */
140
1478
        if ((ret2 = tm2_read_tree(ctx, length + 1, huff)) < 0)
141
            return ret2;
142
1478
        if ((ret = tm2_read_tree(ctx, length + 1, huff)) < 0)
143
            return ret;
144
    }
145
1478
    return FFMAX(ret, ret2);
146
}
147
148
192
static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
149
{
150
    TM2Huff huff;
151
192
    int res = 0;
152
153
192
    huff.val_bits = get_bits(&ctx->gb, 5);
154
192
    huff.max_bits = get_bits(&ctx->gb, 5);
155
192
    huff.min_bits = get_bits(&ctx->gb, 5);
156
192
    huff.nodes    = get_bits(&ctx->gb, 17);
157
192
    huff.num      = 0;
158
159
    /* check for correct codes parameters */
160

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

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

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

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

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

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

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

192
    if ((toks < 0) || (toks > 0xFFFFFF)) {
345
        av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
346
        ret = AVERROR_INVALIDDATA;
347
        goto end;
348
    }
349
192
    ret = av_reallocp_array(&ctx->tokens[stream_id], toks, sizeof(int));
350
192
    if (ret < 0) {
351
        ctx->tok_lens[stream_id] = 0;
352
        goto end;
353
    }
354
192
    ctx->tok_lens[stream_id] = toks;
355
192
    len = bytestream2_get_be32(&gb);
356
192
    if (len > 0) {
357
189
        pos = bytestream2_tell(&gb);
358
189
        if (skip <= pos) {
359
            ret = AVERROR_INVALIDDATA;
360
            goto end;
361
        }
362
189
        init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
363
2353429
        for (i = 0; i < toks; i++) {
364
2353240
            if (get_bits_left(&ctx->gb) <= 0) {
365
                av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
366
                ret = AVERROR_INVALIDDATA;
367
                goto end;
368
            }
369
2353240
            ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
370

2353240
            if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS || ctx->tokens[stream_id][i]<0) {
371
                av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
372
                       ctx->tokens[stream_id][i], stream_id, i);
373
                ret = AVERROR_INVALIDDATA;
374
                goto end;
375
            }
376
        }
377
    } else {
378
3
        if (len < 0) {
379
            ret = AVERROR_INVALIDDATA;
380
            goto end;
381
        }
382
21
        for (i = 0; i < toks; i++) {
383
18
            ctx->tokens[stream_id][i] = codes.recode[0];
384

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

66716
    TM2_INIT_POINTERS();
509
510
    /* hi-res chroma */
511
333580
    for (i = 0; i < 4; i++) {
512
266864
        deltas[i]     = GET_TOK(ctx, TM2_C_HI);
513
266864
        deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
514
    }
515
66716
    tm2_high_chroma(U, Ustride, clast,     ctx->CD,     deltas);
516
66716
    tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
517
518
    /* hi-res luma */
519
1134172
    for (i = 0; i < 16; i++)
520
1067456
        deltas[i] = GET_TOK(ctx, TM2_L_HI);
521
522
66716
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
523
66716
}
524
525
22463
static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
526
{
527
    int i;
528
    int deltas[16];
529

22463
    TM2_INIT_POINTERS();
530
531
    /* low-res chroma */
532
22463
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
533
22463
    deltas[1] = deltas[2] = deltas[3] = 0;
534
22463
    tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
535
536
22463
    deltas[0] = GET_TOK(ctx, TM2_C_LO);
537
22463
    deltas[1] = deltas[2] = deltas[3] = 0;
538
22463
    tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
539
540
    /* hi-res luma */
541
381871
    for (i = 0; i < 16; i++)
542
359408
        deltas[i] = GET_TOK(ctx, TM2_L_HI);
543
544
22463
    tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
545
22463
}
546
547
12092
static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
548
{
549
    int i;
550
    int t1, t2;
551
    int deltas[16];
552

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

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



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



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



998
    TM2_INIT_POINTERS_2();
715
716
998
    mx = GET_TOK(ctx, TM2_MOT);
717
998
    my = GET_TOK(ctx, TM2_MOT);
718
998
    mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width  - bx * 4);
719
998
    my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
720
721


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


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

2
    if ((avctx->width & 3) || (avctx->height & 3)) {
952
        av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
953
        return AVERROR(EINVAL);
954
    }
955
956
2
    l->avctx       = avctx;
957
2
    avctx->pix_fmt = AV_PIX_FMT_BGR24;
958
959
2
    l->pic = av_frame_alloc();
960
2
    if (!l->pic)
961
        return AVERROR(ENOMEM);
962
963
2
    ff_bswapdsp_init(&l->bdsp);
964
965
2
    l->last  = av_malloc_array(w, 2 * sizeof(*l->last));
966
2
    if (!l->last)
967
        return AVERROR(ENOMEM);
968
2
    l->clast = l->last + w;
969
970
2
    w += 8;
971
2
    h += 8;
972
2
    l->Y_base = av_calloc(w * h, 2 * sizeof(*l->Y_base));
973
2
    if (!l->Y_base)
974
        return AVERROR(ENOMEM);
975
2
    l->y_stride = w;
976
2
    l->Y1 = l->Y_base + l->y_stride * 4 + 4;
977
2
    l->Y2 = l->Y1 + w * h;
978
2
    w = (w + 1) >> 1;
979
2
    h = (h + 1) >> 1;
980
2
    l->UV_base = av_calloc(w * h, 4 * sizeof(*l->UV_base));
981
2
    if (!l->UV_base)
982
        return AVERROR(ENOMEM);
983
2
    l->uv_stride = w;
984
2
    l->U1 = l->UV_base + l->uv_stride * 2 + 2;
985
2
    l->U2 = l->U1 + w * h;
986
2
    l->V1 = l->U2 + w * h;
987
2
    l->V2 = l->V1 + w * h;
988
989
2
    return 0;
990
}
991
992
2
static av_cold int decode_end(AVCodecContext *avctx)
993
{
994
2
    TM2Context * const l = avctx->priv_data;
995
    int i;
996
997
2
    av_freep(&l->last);
998
16
    for (i = 0; i < TM2_NUM_STREAMS; i++)
999
14
        av_freep(&l->tokens[i]);
1000
1001
2
    av_freep(&l->Y_base);
1002
2
    av_freep(&l->UV_base);
1003
2
    av_freep(&l->buffer);
1004
2
    l->buffer_size = 0;
1005
1006
2
    av_frame_free(&l->pic);
1007
1008
2
    return 0;
1009
}
1010
1011
AVCodec ff_truemotion2_decoder = {
1012
    .name           = "truemotion2",
1013
    .long_name      = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
1014
    .type           = AVMEDIA_TYPE_VIDEO,
1015
    .id             = AV_CODEC_ID_TRUEMOTION2,
1016
    .priv_data_size = sizeof(TM2Context),
1017
    .init           = decode_init,
1018
    .close          = decode_end,
1019
    .decode         = decode_frame,
1020
    .capabilities   = AV_CODEC_CAP_DR1,
1021
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,
1022
};