GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/truemotion2.c Lines: 478 569 84.0 %
Date: 2020-10-23 17:01:47 Branches: 250 323 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
192
        res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
204
                       huff.lens, sizeof(int), sizeof(int),
205
                       huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
206
192
        if (res < 0)
207
            av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
208
        else {
209
192
            code->bits = huff.max_bits;
210
192
            code->length = huff.max_num;
211
192
            code->recode = huff.nums;
212
192
            huff.nums = NULL;
213
        }
214
    }
215
216
out:
217
    /* free allocated memory */
218
192
    av_free(huff.nums);
219
192
    av_free(huff.bits);
220
192
    av_free(huff.lens);
221
222
192
    return res;
223
}
224
225
192
static void tm2_free_codes(TM2Codes *code)
226
{
227
192
    av_free(code->recode);
228
192
    if (code->vlc.table)
229
192
        ff_free_vlc(&code->vlc);
230
192
}
231
232
2353240
static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
233
{
234
    int val;
235
2353240
    val = get_vlc2(gb, code->vlc.table, code->bits, 1);
236
2353240
    if(val<0)
237
        return -1;
238
2353240
    return code->recode[val];
239
}
240
241
#define TM2_OLD_HEADER_MAGIC 0x00000100
242
#define TM2_NEW_HEADER_MAGIC 0x00000101
243
244
30
static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
245
{
246
30
    uint32_t magic = AV_RL32(buf);
247
248
30
    switch (magic) {
249
    case TM2_OLD_HEADER_MAGIC:
250
        avpriv_request_sample(ctx->avctx, "Old TM2 header");
251
        return 0;
252
30
    case TM2_NEW_HEADER_MAGIC:
253
30
        return 0;
254
    default:
255
        av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n",
256
               magic);
257
        return AVERROR_INVALIDDATA;
258
    }
259
}
260
261
29
static int tm2_read_deltas(TM2Context *ctx, int stream_id)
262
{
263
    int d, mb;
264
    int i, v;
265
266
29
    d  = get_bits(&ctx->gb, 9);
267
29
    mb = get_bits(&ctx->gb, 5);
268
269
    av_assert2(mb < 32);
270

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

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

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

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

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

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

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

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

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



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



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



998
    TM2_INIT_POINTERS_2();
719
720
998
    mx = GET_TOK(ctx, TM2_MOT);
721
998
    my = GET_TOK(ctx, TM2_MOT);
722
998
    mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width  - bx * 4);
723
998
    my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
724
725


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


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

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

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

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

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