Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/truemotion1.c
Line
Count
Source
1
/*
2
 * Duck TrueMotion 1.0 Decoder
3
 * Copyright (C) 2003 Alex Beregszaszi & Mike Melanson
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 TrueMotion v1 Video Decoder by
25
 * Alex Beregszaszi and
26
 * Mike Melanson (melanson@pcisys.net)
27
 *
28
 * The TrueMotion v1 decoder presently only decodes 16-bit TM1 data and
29
 * outputs RGB555 (or RGB565) data. 24-bit TM1 data is not supported yet.
30
 */
31
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "avcodec.h"
37
#include "codec_internal.h"
38
#include "decode.h"
39
#include "libavutil/imgutils.h"
40
#include "libavutil/internal.h"
41
#include "libavutil/intreadwrite.h"
42
#include "libavutil/mem.h"
43
44
#include "truemotion1data.h"
45
46
typedef struct TrueMotion1Context {
47
    AVCodecContext *avctx;
48
    AVFrame *frame;
49
50
    const uint8_t *buf;
51
    int size;
52
53
    const uint8_t *mb_change_bits;
54
    int mb_change_bits_row_size;
55
    const uint8_t *index_stream;
56
    int index_stream_size;
57
58
    int flags;
59
    int x, y, w, h;
60
61
    uint32_t y_predictor_table[1024];
62
    uint32_t c_predictor_table[1024];
63
    uint32_t fat_y_predictor_table[1024];
64
    uint32_t fat_c_predictor_table[1024];
65
66
    int compression;
67
    int block_type;
68
    int block_width;
69
    int block_height;
70
71
    int16_t ydt[8];
72
    int16_t cdt[8];
73
    int16_t fat_ydt[8];
74
    int16_t fat_cdt[8];
75
76
    int last_deltaset, last_vectable;
77
78
    unsigned int *vert_pred;
79
    int vert_pred_size;
80
81
} TrueMotion1Context;
82
83
213k
#define FLAG_SPRITE         32
84
604k
#define FLAG_KEYFRAME       16
85
7.75k
#define FLAG_INTERFRAME      8
86
191k
#define FLAG_INTERPOLATED    4
87
88
struct frame_header {
89
    uint8_t header_size;
90
    uint8_t compression;
91
    uint8_t deltaset;
92
    uint8_t vectable;
93
    uint16_t ysize;
94
    uint16_t xsize;
95
    uint16_t checksum;
96
    uint8_t version;
97
    uint8_t header_type;
98
    uint8_t flags;
99
    uint8_t control;
100
    uint16_t xoffset;
101
    uint16_t yoffset;
102
    uint16_t width;
103
    uint16_t height;
104
};
105
106
172k
#define ALGO_NOP        0
107
#define ALGO_RGB16V     1
108
#define ALGO_RGB16H     2
109
410k
#define ALGO_RGB24H     3
110
111
/* these are the various block sizes that can occupy a 4x4 block */
112
13.0M
#define BLOCK_2x2  0
113
#define BLOCK_2x4  1
114
7.75M
#define BLOCK_4x2  2
115
#define BLOCK_4x4  3
116
117
typedef struct comp_types {
118
    int algorithm;
119
    int block_width; // vres
120
    int block_height; // hres
121
    int block_type;
122
} comp_types;
123
124
/* { valid for metatype }, algorithm, num of deltas, vert res, horiz res */
125
static const comp_types compression_types[17] = {
126
    { ALGO_NOP,    0, 0, 0 },
127
128
    { ALGO_RGB16V, 4, 4, BLOCK_4x4 },
129
    { ALGO_RGB16H, 4, 4, BLOCK_4x4 },
130
    { ALGO_RGB16V, 4, 2, BLOCK_4x2 },
131
    { ALGO_RGB16H, 4, 2, BLOCK_4x2 },
132
133
    { ALGO_RGB16V, 2, 4, BLOCK_2x4 },
134
    { ALGO_RGB16H, 2, 4, BLOCK_2x4 },
135
    { ALGO_RGB16V, 2, 2, BLOCK_2x2 },
136
    { ALGO_RGB16H, 2, 2, BLOCK_2x2 },
137
138
    { ALGO_NOP,    4, 4, BLOCK_4x4 },
139
    { ALGO_RGB24H, 4, 4, BLOCK_4x4 },
140
    { ALGO_NOP,    4, 2, BLOCK_4x2 },
141
    { ALGO_RGB24H, 4, 2, BLOCK_4x2 },
142
143
    { ALGO_NOP,    2, 4, BLOCK_2x4 },
144
    { ALGO_RGB24H, 2, 4, BLOCK_2x4 },
145
    { ALGO_NOP,    2, 2, BLOCK_2x2 },
146
    { ALGO_RGB24H, 2, 2, BLOCK_2x2 }
147
};
148
149
static void select_delta_tables(TrueMotion1Context *s, int delta_table_index)
150
17.5k
{
151
17.5k
    int i;
152
153
17.5k
    if (delta_table_index > 3)
154
10.1k
        return;
155
156
7.45k
    memcpy(s->ydt, ydts[delta_table_index], 8 * sizeof(int16_t));
157
7.45k
    memcpy(s->cdt, cdts[delta_table_index], 8 * sizeof(int16_t));
158
7.45k
    memcpy(s->fat_ydt, fat_ydts[delta_table_index], 8 * sizeof(int16_t));
159
7.45k
    memcpy(s->fat_cdt, fat_cdts[delta_table_index], 8 * sizeof(int16_t));
160
161
    /* Y skinny deltas need to be halved for some reason; maybe the
162
     * skinny Y deltas should be modified */
163
67.0k
    for (i = 0; i < 8; i++)
164
59.6k
    {
165
        /* drop the lsb before dividing by 2-- net effect: round down
166
         * when dividing a negative number (e.g., -3/2 = -2, not -1) */
167
59.6k
        s->ydt[i] &= 0xFFFE;
168
59.6k
        s->ydt[i] /= 2;
169
59.6k
    }
170
7.45k
}
171
172
#if HAVE_BIGENDIAN
173
static int make_ydt15_entry(int p2, int p1, int16_t *ydt)
174
#else
175
static int make_ydt15_entry(int p1, int p2, int16_t *ydt)
176
#endif
177
2.92M
{
178
2.92M
    int lo, hi;
179
180
2.92M
    lo = ydt[p1];
181
2.92M
    lo += (lo * 32) + (lo * 1024);
182
2.92M
    hi = ydt[p2];
183
2.92M
    hi += (hi * 32) + (hi * 1024);
184
2.92M
    return (lo + (hi * (1U << 16))) * 2;
185
2.92M
}
186
187
static int make_cdt15_entry(int p1, int p2, int16_t *cdt)
188
2.92M
{
189
2.92M
    int r, b, lo;
190
191
2.92M
    b = cdt[p2];
192
2.92M
    r = cdt[p1] * 1024;
193
2.92M
    lo = b + r;
194
2.92M
    return (lo + (lo * (1U << 16))) * 2;
195
2.92M
}
196
197
#if HAVE_BIGENDIAN
198
static int make_ydt16_entry(int p2, int p1, int16_t *ydt)
199
#else
200
static int make_ydt16_entry(int p1, int p2, int16_t *ydt)
201
#endif
202
0
{
203
0
    int lo, hi;
204
205
0
    lo = ydt[p1];
206
0
    lo += (lo << 6) + (lo << 11);
207
0
    hi = ydt[p2];
208
0
    hi += (hi << 6) + (hi << 11);
209
0
    return (lo + (hi << 16)) << 1;
210
0
}
211
212
static int make_cdt16_entry(int p1, int p2, int16_t *cdt)
213
0
{
214
0
    int r, b, lo;
215
216
0
    b = cdt[p2];
217
0
    r = cdt[p1] << 11;
218
0
    lo = b + r;
219
0
    return (lo + (lo * (1 << 16))) * 2;
220
0
}
221
222
static int make_ydt24_entry(int p1, int p2, int16_t *ydt)
223
3.83M
{
224
3.83M
    int lo, hi;
225
226
3.83M
    lo = ydt[p1];
227
3.83M
    hi = ydt[p2];
228
3.83M
    return (lo + (hi * (1 << 8)) + (hi * (1 << 16))) * 2;
229
3.83M
}
230
231
static int make_cdt24_entry(int p1, int p2, int16_t *cdt)
232
3.83M
{
233
3.83M
    int r, b;
234
235
3.83M
    b = cdt[p2];
236
3.83M
    r = cdt[p1] * (1 << 16);
237
3.83M
    return (b+r) * 2;
238
3.83M
}
239
240
static void gen_vector_table15(TrueMotion1Context *s, const uint8_t *sel_vector_table)
241
5.35k
{
242
5.35k
    int len, i, j;
243
5.35k
    unsigned char delta_pair;
244
245
1.37M
    for (i = 0; i < 1024; i += 4)
246
1.37M
    {
247
1.37M
        len = *sel_vector_table++ / 2;
248
4.29M
        for (j = 0; j < len; j++)
249
2.92M
        {
250
2.92M
            delta_pair = *sel_vector_table++;
251
2.92M
            s->y_predictor_table[i+j] = 0xfffffffe &
252
2.92M
                make_ydt15_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
253
2.92M
            s->c_predictor_table[i+j] = 0xfffffffe &
254
2.92M
                make_cdt15_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
255
2.92M
        }
256
1.37M
        s->y_predictor_table[i+(j-1)] |= 1;
257
1.37M
        s->c_predictor_table[i+(j-1)] |= 1;
258
1.37M
    }
259
5.35k
}
260
261
static void gen_vector_table16(TrueMotion1Context *s, const uint8_t *sel_vector_table)
262
0
{
263
0
    int len, i, j;
264
0
    unsigned char delta_pair;
265
266
0
    for (i = 0; i < 1024; i += 4)
267
0
    {
268
0
        len = *sel_vector_table++ / 2;
269
0
        for (j = 0; j < len; j++)
270
0
        {
271
0
            delta_pair = *sel_vector_table++;
272
0
            s->y_predictor_table[i+j] = 0xfffffffe &
273
0
                make_ydt16_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
274
0
            s->c_predictor_table[i+j] = 0xfffffffe &
275
0
                make_cdt16_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
276
0
        }
277
0
        s->y_predictor_table[i+(j-1)] |= 1;
278
0
        s->c_predictor_table[i+(j-1)] |= 1;
279
0
    }
280
0
}
281
282
static void gen_vector_table24(TrueMotion1Context *s, const uint8_t *sel_vector_table)
283
3.48k
{
284
3.48k
    int len, i, j;
285
3.48k
    unsigned char delta_pair;
286
287
894k
    for (i = 0; i < 1024; i += 4)
288
890k
    {
289
890k
        len = *sel_vector_table++ / 2;
290
2.81M
        for (j = 0; j < len; j++)
291
1.91M
        {
292
1.91M
            delta_pair = *sel_vector_table++;
293
1.91M
            s->y_predictor_table[i+j] = 0xfffffffe &
294
1.91M
                make_ydt24_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
295
1.91M
            s->c_predictor_table[i+j] = 0xfffffffe &
296
1.91M
                make_cdt24_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
297
1.91M
            s->fat_y_predictor_table[i+j] = 0xfffffffe &
298
1.91M
                make_ydt24_entry(delta_pair >> 4, delta_pair & 0xf, s->fat_ydt);
299
1.91M
            s->fat_c_predictor_table[i+j] = 0xfffffffe &
300
1.91M
                make_cdt24_entry(delta_pair >> 4, delta_pair & 0xf, s->fat_cdt);
301
1.91M
        }
302
890k
        s->y_predictor_table[i+(j-1)] |= 1;
303
890k
        s->c_predictor_table[i+(j-1)] |= 1;
304
890k
        s->fat_y_predictor_table[i+(j-1)] |= 1;
305
890k
        s->fat_c_predictor_table[i+(j-1)] |= 1;
306
890k
    }
307
3.48k
}
308
309
/* Returns the number of bytes consumed from the bytestream. Returns -1 if
310
 * there was an error while decoding the header */
311
static int truemotion1_decode_header(TrueMotion1Context *s)
312
426k
{
313
426k
    int i, ret;
314
426k
    int width_shift = 0;
315
426k
    int new_pix_fmt;
316
426k
    struct frame_header header;
317
426k
    uint8_t header_buffer[128] = { 0 };  /* logical maximum size of the header */
318
426k
    const uint8_t *sel_vector_table;
319
320
426k
    header.header_size = ((s->buf[0] >> 5) | (s->buf[0] << 3)) & 0x7f;
321
426k
    if (s->buf[0] < 0x10)
322
29.1k
    {
323
29.1k
        av_log(s->avctx, AV_LOG_ERROR, "invalid header size (%d)\n", s->buf[0]);
324
29.1k
        return AVERROR_INVALIDDATA;
325
29.1k
    }
326
327
396k
    if (header.header_size + 1 > s->size) {
328
182k
        av_log(s->avctx, AV_LOG_ERROR, "Input packet too small.\n");
329
182k
        return AVERROR_INVALIDDATA;
330
182k
    }
331
332
    /* unscramble the header bytes with a XOR operation */
333
3.11M
    for (i = 1; i < header.header_size; i++)
334
2.89M
        header_buffer[i - 1] = s->buf[i] ^ s->buf[i + 1];
335
336
214k
    header.compression = header_buffer[0];
337
214k
    header.deltaset = header_buffer[1];
338
214k
    header.vectable = header_buffer[2];
339
214k
    header.ysize = AV_RL16(&header_buffer[3]);
340
214k
    header.xsize = AV_RL16(&header_buffer[5]);
341
214k
    header.checksum = AV_RL16(&header_buffer[7]);
342
214k
    header.version = header_buffer[9];
343
214k
    header.header_type = header_buffer[10];
344
214k
    header.flags = header_buffer[11];
345
214k
    header.control = header_buffer[12];
346
347
    /* Version 2 */
348
214k
    if (header.version >= 2)
349
35.3k
    {
350
35.3k
        if (header.header_type > 3)
351
818
        {
352
818
            av_log(s->avctx, AV_LOG_ERROR, "invalid header type (%d)\n", header.header_type);
353
818
            return AVERROR_INVALIDDATA;
354
34.5k
        } else if ((header.header_type == 2) || (header.header_type == 3)) {
355
7.75k
            s->flags = header.flags;
356
7.75k
            if (!(s->flags & FLAG_INTERFRAME))
357
4.25k
                s->flags |= FLAG_KEYFRAME;
358
7.75k
        } else
359
26.7k
            s->flags = FLAG_KEYFRAME;
360
35.3k
    } else /* Version 1 */
361
178k
        s->flags = FLAG_KEYFRAME;
362
363
213k
    if (s->flags & FLAG_SPRITE) {
364
655
        avpriv_request_sample(s->avctx, "Frame with sprite");
365
        /* FIXME header.width, height, xoffset and yoffset aren't initialized */
366
655
        return AVERROR_PATCHWELCOME;
367
212k
    } else {
368
212k
        s->w = header.xsize;
369
212k
        s->h = header.ysize;
370
212k
        if (header.header_type < 2) {
371
203k
            if ((s->w < 213) && (s->h >= 176))
372
191k
            {
373
191k
                s->flags |= FLAG_INTERPOLATED;
374
191k
                avpriv_request_sample(s->avctx, "Interpolated frame");
375
191k
            }
376
203k
        }
377
212k
    }
378
379
212k
    if (header.compression >= 17) {
380
2.47k
        av_log(s->avctx, AV_LOG_ERROR, "invalid compression type (%d)\n", header.compression);
381
2.47k
        return AVERROR_INVALIDDATA;
382
2.47k
    }
383
384
210k
    if ((header.deltaset != s->last_deltaset) ||
385
194k
        (header.vectable != s->last_vectable))
386
17.5k
        select_delta_tables(s, header.deltaset);
387
388
210k
    if ((header.compression & 1) && header.header_type)
389
7.94k
        sel_vector_table = pc_tbl2;
390
202k
    else {
391
202k
        if (header.vectable > 0 && header.vectable < 4)
392
198k
            sel_vector_table = tables[header.vectable - 1];
393
3.86k
        else {
394
3.86k
            av_log(s->avctx, AV_LOG_ERROR, "invalid vector table id (%d)\n", header.vectable);
395
3.86k
            return AVERROR_INVALIDDATA;
396
3.86k
        }
397
202k
    }
398
399
206k
    if (compression_types[header.compression].algorithm == ALGO_RGB24H) {
400
29.9k
        new_pix_fmt = AV_PIX_FMT_0RGB32;
401
29.9k
        width_shift = 1;
402
29.9k
    } else
403
176k
        new_pix_fmt = AV_PIX_FMT_RGB555; // RGB565 is supported as well
404
405
206k
    s->w >>= width_shift;
406
206k
    if (s->w & 1) {
407
1.05k
        avpriv_request_sample(s->avctx, "Frame with odd width");
408
1.05k
        return AVERROR_PATCHWELCOME;
409
1.05k
    }
410
411
205k
    if (s->h & 3) {
412
2.16k
        avpriv_request_sample(s->avctx, "Frame with height not being a multiple of 4");
413
2.16k
        return AVERROR_PATCHWELCOME;
414
2.16k
    }
415
416
203k
    if (s->w != s->avctx->width || s->h != s->avctx->height ||
417
187k
        new_pix_fmt != s->avctx->pix_fmt) {
418
16.0k
        av_frame_unref(s->frame);
419
16.0k
        s->avctx->sample_aspect_ratio = (AVRational){ 1 << width_shift, 1 };
420
16.0k
        s->avctx->pix_fmt = new_pix_fmt;
421
422
16.0k
        if ((ret = ff_set_dimensions(s->avctx, s->w, s->h)) < 0)
423
3.65k
            return ret;
424
425
12.4k
        ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
426
427
12.4k
        av_fast_malloc(&s->vert_pred, &s->vert_pred_size, s->avctx->width * sizeof(unsigned int));
428
12.4k
        if (!s->vert_pred)
429
0
            return AVERROR(ENOMEM);
430
12.4k
    }
431
432
    /* There is 1 change bit per 4 pixels, so each change byte represents
433
     * 32 pixels; divide width by 4 to obtain the number of change bits and
434
     * then round up to the nearest byte. */
435
199k
    s->mb_change_bits_row_size = ((s->avctx->width >> (2 - width_shift)) + 7) >> 3;
436
437
199k
    if ((header.deltaset != s->last_deltaset) || (header.vectable != s->last_vectable))
438
8.83k
    {
439
8.83k
        if (compression_types[header.compression].algorithm == ALGO_RGB24H)
440
3.48k
            gen_vector_table24(s, sel_vector_table);
441
5.35k
        else
442
5.35k
        if (s->avctx->pix_fmt == AV_PIX_FMT_RGB555)
443
5.35k
            gen_vector_table15(s, sel_vector_table);
444
0
        else
445
0
            gen_vector_table16(s, sel_vector_table);
446
8.83k
    }
447
448
    /* set up pointers to the other key data chunks */
449
199k
    s->mb_change_bits = s->buf + header.header_size;
450
199k
    if (s->flags & FLAG_KEYFRAME) {
451
        /* no change bits specified for a keyframe; only index bytes */
452
196k
        s->index_stream = s->mb_change_bits;
453
196k
        if (s->avctx->width * s->avctx->height / 2048 + header.header_size > s->size)
454
4.37k
            return AVERROR_INVALIDDATA;
455
196k
    } else {
456
        /* one change bit per 4x4 block */
457
2.79k
        s->index_stream = s->mb_change_bits +
458
2.79k
            (s->mb_change_bits_row_size * (s->avctx->height >> 2));
459
2.79k
    }
460
195k
    s->index_stream_size = s->size - (s->index_stream - s->buf);
461
462
195k
    s->last_deltaset = header.deltaset;
463
195k
    s->last_vectable = header.vectable;
464
195k
    s->compression = header.compression;
465
195k
    s->block_width = compression_types[header.compression].block_width;
466
195k
    s->block_height = compression_types[header.compression].block_height;
467
195k
    s->block_type = compression_types[header.compression].block_type;
468
469
195k
    if (s->avctx->debug & FF_DEBUG_PICT_INFO)
470
0
        av_log(s->avctx, AV_LOG_INFO, "tables: %d / %d c:%d %dx%d t:%d %s%s%s%s\n",
471
0
            s->last_deltaset, s->last_vectable, s->compression, s->block_width,
472
0
            s->block_height, s->block_type,
473
0
            s->flags & FLAG_KEYFRAME ? " KEY" : "",
474
0
            s->flags & FLAG_INTERFRAME ? " INTER" : "",
475
0
            s->flags & FLAG_SPRITE ? " SPRITE" : "",
476
0
            s->flags & FLAG_INTERPOLATED ? " INTERPOL" : "");
477
478
195k
    return header.header_size;
479
199k
}
480
481
static av_cold int truemotion1_decode_init(AVCodecContext *avctx)
482
2.37k
{
483
2.37k
    TrueMotion1Context *s = avctx->priv_data;
484
485
2.37k
    s->avctx = avctx;
486
487
    // FIXME: it may change ?
488
//    if (avctx->bits_per_sample == 24)
489
//        avctx->pix_fmt = AV_PIX_FMT_RGB24;
490
//    else
491
//        avctx->pix_fmt = AV_PIX_FMT_RGB555;
492
493
2.37k
    s->frame = av_frame_alloc();
494
2.37k
    if (!s->frame)
495
0
        return AVERROR(ENOMEM);
496
497
    /* there is a vertical predictor for each pixel in a line; each vertical
498
     * predictor is 0 to start with */
499
2.37k
    av_fast_malloc(&s->vert_pred, &s->vert_pred_size, s->avctx->width * sizeof(unsigned int));
500
2.37k
    if (!s->vert_pred)
501
127
        return AVERROR(ENOMEM);
502
503
2.25k
    return 0;
504
2.37k
}
505
506
/*
507
Block decoding order:
508
509
dxi: Y-Y
510
dxic: Y-C-Y
511
dxic2: Y-C-Y-C
512
513
hres,vres,i,i%vres (0 < i < 4)
514
2x2 0: 0 dxic2
515
2x2 1: 1 dxi
516
2x2 2: 0 dxic2
517
2x2 3: 1 dxi
518
2x4 0: 0 dxic2
519
2x4 1: 1 dxi
520
2x4 2: 2 dxi
521
2x4 3: 3 dxi
522
4x2 0: 0 dxic
523
4x2 1: 1 dxi
524
4x2 2: 0 dxic
525
4x2 3: 1 dxi
526
4x4 0: 0 dxic
527
4x4 1: 1 dxi
528
4x4 2: 2 dxi
529
4x4 3: 3 dxi
530
*/
531
532
91.8M
#define GET_NEXT_INDEX() \
533
91.8M
{\
534
91.8M
    if (index_stream_index >= s->index_stream_size) { \
535
187k
        av_log(s->avctx, AV_LOG_INFO, " help! truemotion1 decoder went out of bounds\n"); \
536
187k
        return; \
537
187k
    } \
538
91.8M
    index = s->index_stream[index_stream_index++] * 4; \
539
91.6M
}
540
541
99.0M
#define INC_INDEX                                                   \
542
99.0M
do {                                                                \
543
99.0M
    if (index >= 1023) {                                            \
544
6.60k
        av_log(s->avctx, AV_LOG_ERROR, "Invalid index value.\n");   \
545
6.60k
        return;                                                     \
546
6.60k
    }                                                               \
547
99.0M
    index++;                                                        \
548
99.0M
} while (0)
549
550
#define APPLY_C_PREDICTOR() \
551
18.9M
    predictor_pair = s->c_predictor_table[index]; \
552
18.9M
    horiz_pred += (predictor_pair >> 1); \
553
18.9M
    if (predictor_pair & 1) { \
554
8.30M
        GET_NEXT_INDEX() \
555
8.24M
        if (!index) { \
556
3.36M
            GET_NEXT_INDEX() \
557
3.36M
            predictor_pair = s->c_predictor_table[index]; \
558
3.36M
            horiz_pred += ((predictor_pair >> 1) * 5); \
559
3.36M
            if (predictor_pair & 1) \
560
3.36M
                GET_NEXT_INDEX() \
561
3.36M
            else \
562
3.36M
                INC_INDEX; \
563
3.36M
        } \
564
8.24M
    } else \
565
18.9M
        INC_INDEX;
566
567
#define APPLY_C_PREDICTOR_24() \
568
16.9M
    predictor_pair = s->c_predictor_table[index]; \
569
16.9M
    horiz_pred += (predictor_pair >> 1); \
570
16.9M
    if (predictor_pair & 1) { \
571
7.52M
        GET_NEXT_INDEX() \
572
7.51M
        if (!index) { \
573
3.02M
            GET_NEXT_INDEX() \
574
3.02M
            predictor_pair = s->fat_c_predictor_table[index]; \
575
3.02M
            horiz_pred += (predictor_pair >> 1); \
576
3.02M
            if (predictor_pair & 1) \
577
3.02M
                GET_NEXT_INDEX() \
578
3.02M
            else \
579
3.02M
                INC_INDEX; \
580
3.02M
        } \
581
7.51M
    } else \
582
16.9M
        INC_INDEX;
583
584
585
#define APPLY_Y_PREDICTOR() \
586
54.3M
    predictor_pair = s->y_predictor_table[index]; \
587
54.3M
    horiz_pred += (predictor_pair >> 1); \
588
54.3M
    if (predictor_pair & 1) { \
589
23.7M
        GET_NEXT_INDEX() \
590
23.6M
        if (!index) { \
591
9.72M
            GET_NEXT_INDEX() \
592
9.72M
            predictor_pair = s->y_predictor_table[index]; \
593
9.72M
            horiz_pred += ((predictor_pair >> 1) * 5); \
594
9.72M
            if (predictor_pair & 1) \
595
9.72M
                GET_NEXT_INDEX() \
596
9.72M
            else \
597
9.72M
                INC_INDEX; \
598
9.72M
        } \
599
23.6M
    } else \
600
54.3M
        INC_INDEX;
601
602
#define APPLY_Y_PREDICTOR_24() \
603
50.0M
    predictor_pair = s->y_predictor_table[index]; \
604
50.0M
    horiz_pred += (predictor_pair >> 1); \
605
50.0M
    if (predictor_pair & 1) { \
606
22.0M
        GET_NEXT_INDEX() \
607
22.0M
        if (!index) { \
608
9.07M
            GET_NEXT_INDEX() \
609
9.07M
            predictor_pair = s->fat_y_predictor_table[index]; \
610
9.07M
            horiz_pred += (predictor_pair >> 1); \
611
9.07M
            if (predictor_pair & 1) \
612
9.07M
                GET_NEXT_INDEX() \
613
9.07M
            else \
614
9.07M
                INC_INDEX; \
615
9.07M
        } \
616
22.0M
    } else \
617
50.0M
        INC_INDEX;
618
619
#define OUTPUT_PIXEL_PAIR() \
620
104M
    *current_pixel_pair = *vert_pred + horiz_pred; \
621
104M
    *vert_pred++ = *current_pixel_pair++;
622
623
static void truemotion1_decode_16bit(TrueMotion1Context *s)
624
172k
{
625
172k
    int y;
626
172k
    int pixels_left;  /* remaining pixels on this line */
627
172k
    unsigned int predictor_pair;
628
172k
    unsigned int horiz_pred;
629
172k
    unsigned int *vert_pred;
630
172k
    unsigned int *current_pixel_pair;
631
172k
    unsigned char *current_line = s->frame->data[0];
632
172k
    int keyframe = s->flags & FLAG_KEYFRAME;
633
634
    /* these variables are for managing the stream of macroblock change bits */
635
172k
    const unsigned char *mb_change_bits = s->mb_change_bits;
636
172k
    unsigned char mb_change_byte;
637
172k
    unsigned char mb_change_byte_mask;
638
172k
    int mb_change_index;
639
640
    /* these variables are for managing the main index stream */
641
172k
    int index_stream_index = 0;  /* yes, the index into the index stream */
642
172k
    int index;
643
644
    /* clean out the line buffer */
645
172k
    memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned int));
646
647
172k
    GET_NEXT_INDEX();
648
649
2.26M
    for (y = 0; y < s->avctx->height; y++) {
650
651
        /* re-init variables for the next line iteration */
652
2.25M
        horiz_pred = 0;
653
2.25M
        current_pixel_pair = (unsigned int *)current_line;
654
2.25M
        vert_pred = s->vert_pred;
655
2.25M
        mb_change_index = 0;
656
2.25M
        if (!keyframe)
657
676k
            mb_change_byte = mb_change_bits[mb_change_index++];
658
2.25M
        mb_change_byte_mask = 0x01;
659
2.25M
        pixels_left = s->avctx->width;
660
661
29.9M
        while (pixels_left > 0) {
662
663
27.8M
            if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
664
665
27.2M
                switch (y & 3) {
666
6.91M
                case 0:
667
                    /* if macroblock width is 2, apply C-Y-C-Y; else
668
                     * apply C-Y-Y */
669
6.91M
                    if (s->block_width == 2) {
670
3.42M
                        APPLY_C_PREDICTOR();
671
3.42M
                        APPLY_Y_PREDICTOR();
672
3.41M
                        OUTPUT_PIXEL_PAIR();
673
3.41M
                        APPLY_C_PREDICTOR();
674
3.41M
                        APPLY_Y_PREDICTOR();
675
3.41M
                        OUTPUT_PIXEL_PAIR();
676
3.49M
                    } else {
677
3.49M
                        APPLY_C_PREDICTOR();
678
3.49M
                        APPLY_Y_PREDICTOR();
679
3.49M
                        OUTPUT_PIXEL_PAIR();
680
3.49M
                        APPLY_Y_PREDICTOR();
681
3.46M
                        OUTPUT_PIXEL_PAIR();
682
3.46M
                    }
683
6.88M
                    break;
684
685
6.88M
                case 1:
686
13.5M
                case 3:
687
                    /* always apply 2 Y predictors on these iterations */
688
13.5M
                    APPLY_Y_PREDICTOR();
689
13.5M
                    OUTPUT_PIXEL_PAIR();
690
13.5M
                    APPLY_Y_PREDICTOR();
691
13.4M
                    OUTPUT_PIXEL_PAIR();
692
13.4M
                    break;
693
694
6.77M
                case 2:
695
                    /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
696
                     * depending on the macroblock type */
697
6.77M
                    if (s->block_type == BLOCK_2x2) {
698
3.00M
                        APPLY_C_PREDICTOR();
699
3.00M
                        APPLY_Y_PREDICTOR();
700
3.00M
                        OUTPUT_PIXEL_PAIR();
701
3.00M
                        APPLY_C_PREDICTOR();
702
3.00M
                        APPLY_Y_PREDICTOR();
703
2.99M
                        OUTPUT_PIXEL_PAIR();
704
3.77M
                    } else if (s->block_type == BLOCK_4x2) {
705
2.60M
                        APPLY_C_PREDICTOR();
706
2.54M
                        APPLY_Y_PREDICTOR();
707
2.53M
                        OUTPUT_PIXEL_PAIR();
708
2.53M
                        APPLY_Y_PREDICTOR();
709
2.53M
                        OUTPUT_PIXEL_PAIR();
710
2.53M
                    } else {
711
1.17M
                        APPLY_Y_PREDICTOR();
712
1.16M
                        OUTPUT_PIXEL_PAIR();
713
1.16M
                        APPLY_Y_PREDICTOR();
714
1.16M
                        OUTPUT_PIXEL_PAIR();
715
1.16M
                    }
716
6.70M
                    break;
717
27.2M
                }
718
719
27.2M
            } else {
720
721
                /* skip (copy) four pixels, but reassign the horizontal
722
                 * predictor */
723
608k
                *vert_pred++ = *current_pixel_pair++;
724
608k
                horiz_pred = *current_pixel_pair - *vert_pred;
725
608k
                *vert_pred++ = *current_pixel_pair++;
726
727
608k
            }
728
729
27.6M
            if (!keyframe) {
730
684k
                mb_change_byte_mask <<= 1;
731
732
                /* next byte */
733
684k
                if (!mb_change_byte_mask) {
734
1.06k
                    mb_change_byte = mb_change_bits[mb_change_index++];
735
1.06k
                    mb_change_byte_mask = 0x01;
736
1.06k
                }
737
684k
            }
738
739
27.6M
            pixels_left -= 4;
740
27.6M
        }
741
742
        /* next change row */
743
2.08M
        if (((y + 1) & 3) == 0)
744
467k
            mb_change_bits += s->mb_change_bits_row_size;
745
746
2.08M
        current_line += s->frame->linesize[0];
747
2.08M
    }
748
171k
}
749
750
static void truemotion1_decode_24bit(TrueMotion1Context *s)
751
22.6k
{
752
22.6k
    int y;
753
22.6k
    int pixels_left;  /* remaining pixels on this line */
754
22.6k
    unsigned int predictor_pair;
755
22.6k
    unsigned int horiz_pred;
756
22.6k
    unsigned int *vert_pred;
757
22.6k
    unsigned int *current_pixel_pair;
758
22.6k
    unsigned char *current_line = s->frame->data[0];
759
22.6k
    int keyframe = s->flags & FLAG_KEYFRAME;
760
761
    /* these variables are for managing the stream of macroblock change bits */
762
22.6k
    const unsigned char *mb_change_bits = s->mb_change_bits;
763
22.6k
    unsigned char mb_change_byte;
764
22.6k
    unsigned char mb_change_byte_mask;
765
22.6k
    int mb_change_index;
766
767
    /* these variables are for managing the main index stream */
768
22.6k
    int index_stream_index = 0;  /* yes, the index into the index stream */
769
22.6k
    int index;
770
771
    /* clean out the line buffer */
772
22.6k
    memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned int));
773
774
22.6k
    GET_NEXT_INDEX();
775
776
1.02M
    for (y = 0; y < s->avctx->height; y++) {
777
778
        /* re-init variables for the next line iteration */
779
1.02M
        horiz_pred = 0;
780
1.02M
        current_pixel_pair = (unsigned int *)current_line;
781
1.02M
        vert_pred = s->vert_pred;
782
1.02M
        mb_change_index = 0;
783
1.02M
        mb_change_byte = mb_change_bits[mb_change_index++];
784
1.02M
        mb_change_byte_mask = 0x01;
785
1.02M
        pixels_left = s->avctx->width;
786
787
26.0M
        while (pixels_left > 0) {
788
789
25.0M
            if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
790
791
25.0M
                switch (y & 3) {
792
6.32M
                case 0:
793
                    /* if macroblock width is 2, apply C-Y-C-Y; else
794
                     * apply C-Y-Y */
795
6.32M
                    if (s->block_width == 2) {
796
3.48M
                        APPLY_C_PREDICTOR_24();
797
3.48M
                        APPLY_Y_PREDICTOR_24();
798
3.48M
                        OUTPUT_PIXEL_PAIR();
799
3.48M
                        APPLY_C_PREDICTOR_24();
800
3.48M
                        APPLY_Y_PREDICTOR_24();
801
3.48M
                        OUTPUT_PIXEL_PAIR();
802
3.48M
                    } else {
803
2.84M
                        APPLY_C_PREDICTOR_24();
804
2.83M
                        APPLY_Y_PREDICTOR_24();
805
2.83M
                        OUTPUT_PIXEL_PAIR();
806
2.83M
                        APPLY_Y_PREDICTOR_24();
807
2.83M
                        OUTPUT_PIXEL_PAIR();
808
2.83M
                    }
809
6.32M
                    break;
810
811
6.32M
                case 1:
812
12.4M
                case 3:
813
                    /* always apply 2 Y predictors on these iterations */
814
12.4M
                    APPLY_Y_PREDICTOR_24();
815
12.4M
                    OUTPUT_PIXEL_PAIR();
816
12.4M
                    APPLY_Y_PREDICTOR_24();
817
12.4M
                    OUTPUT_PIXEL_PAIR();
818
12.4M
                    break;
819
820
6.24M
                case 2:
821
                    /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
822
                     * depending on the macroblock type */
823
6.24M
                    if (s->block_type == BLOCK_2x2) {
824
2.26M
                        APPLY_C_PREDICTOR_24();
825
2.26M
                        APPLY_Y_PREDICTOR_24();
826
2.25M
                        OUTPUT_PIXEL_PAIR();
827
2.25M
                        APPLY_C_PREDICTOR_24();
828
2.25M
                        APPLY_Y_PREDICTOR_24();
829
2.25M
                        OUTPUT_PIXEL_PAIR();
830
3.98M
                    } else if (s->block_type == BLOCK_4x2) {
831
2.62M
                        APPLY_C_PREDICTOR_24();
832
2.62M
                        APPLY_Y_PREDICTOR_24();
833
2.62M
                        OUTPUT_PIXEL_PAIR();
834
2.62M
                        APPLY_Y_PREDICTOR_24();
835
2.62M
                        OUTPUT_PIXEL_PAIR();
836
2.62M
                    } else {
837
1.36M
                        APPLY_Y_PREDICTOR_24();
838
1.36M
                        OUTPUT_PIXEL_PAIR();
839
1.36M
                        APPLY_Y_PREDICTOR_24();
840
1.35M
                        OUTPUT_PIXEL_PAIR();
841
1.35M
                    }
842
6.23M
                    break;
843
25.0M
                }
844
845
25.0M
            } else {
846
847
                /* skip (copy) four pixels, but reassign the horizontal
848
                 * predictor */
849
5.11k
                *vert_pred++ = *current_pixel_pair++;
850
5.11k
                horiz_pred = *current_pixel_pair - *vert_pred;
851
5.11k
                *vert_pred++ = *current_pixel_pair++;
852
853
5.11k
            }
854
855
25.0M
            if (!keyframe) {
856
15.2k
                mb_change_byte_mask <<= 1;
857
858
                /* next byte */
859
15.2k
                if (!mb_change_byte_mask) {
860
1.68k
                    mb_change_byte = mb_change_bits[mb_change_index++];
861
1.68k
                    mb_change_byte_mask = 0x01;
862
1.68k
                }
863
15.2k
            }
864
865
25.0M
            pixels_left -= 2;
866
25.0M
        }
867
868
        /* next change row */
869
1.00M
        if (((y + 1) & 3) == 0)
870
245k
            mb_change_bits += s->mb_change_bits_row_size;
871
872
1.00M
        current_line += s->frame->linesize[0];
873
1.00M
    }
874
20.8k
}
875
876
877
static int truemotion1_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
878
                                    int *got_frame, AVPacket *avpkt)
879
426k
{
880
426k
    const uint8_t *buf = avpkt->data;
881
426k
    int ret, buf_size = avpkt->size;
882
426k
    TrueMotion1Context *s = avctx->priv_data;
883
884
426k
    s->buf = buf;
885
426k
    s->size = buf_size;
886
887
426k
    if ((ret = truemotion1_decode_header(s)) < 0)
888
230k
        return ret;
889
890
195k
    if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
891
226
        return ret;
892
893
195k
    if (compression_types[s->compression].algorithm == ALGO_RGB24H) {
894
22.6k
        truemotion1_decode_24bit(s);
895
172k
    } else if (compression_types[s->compression].algorithm != ALGO_NOP) {
896
172k
        truemotion1_decode_16bit(s);
897
172k
    }
898
899
195k
    if ((ret = av_frame_ref(rframe, s->frame)) < 0)
900
0
        return ret;
901
902
195k
    *got_frame      = 1;
903
904
    /* report that the buffer was completely consumed */
905
195k
    return buf_size;
906
195k
}
907
908
static av_cold int truemotion1_decode_end(AVCodecContext *avctx)
909
2.37k
{
910
2.37k
    TrueMotion1Context *s = avctx->priv_data;
911
912
2.37k
    av_frame_free(&s->frame);
913
2.37k
    av_freep(&s->vert_pred);
914
915
2.37k
    return 0;
916
2.37k
}
917
918
const FFCodec ff_truemotion1_decoder = {
919
    .p.name         = "truemotion1",
920
    CODEC_LONG_NAME("Duck TrueMotion 1.0"),
921
    .p.type         = AVMEDIA_TYPE_VIDEO,
922
    .p.id           = AV_CODEC_ID_TRUEMOTION1,
923
    .priv_data_size = sizeof(TrueMotion1Context),
924
    .init           = truemotion1_decode_init,
925
    .close          = truemotion1_decode_end,
926
    FF_CODEC_DECODE_CB(truemotion1_decode_frame),
927
    .p.capabilities = AV_CODEC_CAP_DR1,
928
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
929
};