Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/svq1dec.c
Line
Count
Source
1
/*
2
 * SVQ1 decoder
3
 * ported to MPlayer by Arpi <arpi@thot.banki.hu>
4
 * ported to libavcodec by Nick Kurshev <nickols_k@mail.ru>
5
 *
6
 * Copyright (c) 2002 The Xine project
7
 * Copyright (c) 2002 The FFmpeg project
8
 *
9
 * SVQ1 Encoder (c) 2004 Mike Melanson <melanson@pcisys.net>
10
 *
11
 * This file is part of FFmpeg.
12
 *
13
 * FFmpeg is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU Lesser General Public
15
 * License as published by the Free Software Foundation; either
16
 * version 2.1 of the License, or (at your option) any later version.
17
 *
18
 * FFmpeg is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21
 * Lesser General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public
24
 * License along with FFmpeg; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
 */
27
28
/**
29
 * @file
30
 * Sorenson Vector Quantizer #1 (SVQ1) video codec.
31
 * For more information of the SVQ1 algorithm, visit:
32
 *   http://www.pcisys.net/~melanson/codecs/
33
 */
34
35
#include "libavutil/attributes.h"
36
#include "libavutil/crc.h"
37
#include "libavutil/mem.h"
38
#include "libavutil/thread.h"
39
40
#include "avcodec.h"
41
#include "codec_internal.h"
42
#include "decode.h"
43
#include "get_bits.h"
44
#include "h263data.h"
45
#include "hpeldsp.h"
46
#include "mathops.h"
47
#include "svq1.h"
48
49
46.2k
#define SVQ1_BLOCK_TYPE_VLC_BITS 3
50
static VLCElem svq1_block_type[8];
51
static VLCElem svq1_motion_component[176];
52
static const VLCElem *svq1_intra_multistage[6];
53
static const VLCElem *svq1_inter_multistage[6];
54
static VLCElem svq1_intra_mean[632];
55
static VLCElem svq1_inter_mean[1434];
56
57
/* motion vector (prediction) */
58
typedef struct svq1_pmv_s {
59
    int x;
60
    int y;
61
} svq1_pmv;
62
63
typedef struct SVQ1Context {
64
    HpelDSPContext hdsp;
65
    GetBitContext gb;
66
    AVFrame *prev;
67
68
    uint8_t *pkt_swapped;
69
    int pkt_swapped_allocated;
70
71
    svq1_pmv *pmv;
72
    int pmv_allocated;
73
74
    int width;
75
    int height;
76
    int frame_code;
77
    int nonref;         // 1 if the current frame won't be referenced
78
79
    int last_tempref;
80
} SVQ1Context;
81
82
static const uint8_t string_table[256] = {
83
    0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54,
84
    0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
85
    0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06,
86
    0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
87
    0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0,
88
    0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
89
    0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2,
90
    0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
91
    0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9,
92
    0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
93
    0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B,
94
    0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
95
    0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D,
96
    0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
97
    0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F,
98
    0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
99
    0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB,
100
    0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
101
    0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9,
102
    0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
103
    0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F,
104
    0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
105
    0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D,
106
    0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
107
    0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26,
108
    0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
109
    0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74,
110
    0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
111
    0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82,
112
    0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
113
    0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0,
114
    0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
115
};
116
117
#define SVQ1_PROCESS_VECTOR()                                           \
118
654k
    for (; level > 0; i++) {                                            \
119
492k
        /* process next depth */                                        \
120
492k
        if (i == m) {                                                   \
121
94.9k
            m = n;                                                      \
122
94.9k
            if (--level == 0)                                           \
123
94.9k
                break;                                                  \
124
94.9k
        }                                                               \
125
492k
        /* divide block if next bit set */                              \
126
492k
        if (!get_bits1(bitbuf))                                         \
127
479k
            break;                                                      \
128
479k
        /* add child nodes */                                           \
129
479k
        list[n++] = list[i];                                            \
130
241k
        list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level >> 1) + 1));\
131
241k
    }
132
133
#define SVQ1_ADD_CODEBOOK()                                             \
134
    /* add codebook entries to vector */                                \
135
1.64M
    for (j = 0; j < stages; j++) {                                      \
136
1.01M
        n3  = codebook[entries[j]] ^ 0x80808080;                        \
137
1.01M
        n1 += (n3 & 0xFF00FF00) >> 8;                                   \
138
1.01M
        n2 +=  n3 & 0x00FF00FF;                                         \
139
1.01M
    }                                                                   \
140
631k
                                                                        \
141
631k
    /* clip to [0..255] */                                              \
142
631k
    if (n1 & 0xFF00FF00) {                                              \
143
109k
        n3  = (n1 >> 15  & 0x00010001 | 0x01000100) - 0x00010001;       \
144
109k
        n1 += 0x7F007F00;                                               \
145
109k
        n1 |= (~n1 >> 15 & 0x00010001 | 0x01000100) - 0x00010001;       \
146
109k
        n1 &= n3 & 0x00FF00FF;                                          \
147
109k
    }                                                                   \
148
631k
                                                                        \
149
631k
    if (n2 & 0xFF00FF00) {                                              \
150
123k
        n3  = (n2 >> 15  & 0x00010001 | 0x01000100) - 0x00010001;       \
151
123k
        n2 += 0x7F007F00;                                               \
152
123k
        n2 |= (~n2 >> 15 & 0x00010001 | 0x01000100) - 0x00010001;       \
153
123k
        n2 &= n3 & 0x00FF00FF;                                          \
154
123k
    }
155
156
#define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)                               \
157
84.5k
    codebook = (const uint32_t *)cbook[level];                          \
158
84.5k
    if (stages > 0)                                                     \
159
84.5k
        bit_cache = get_bits(bitbuf, 4 * stages);                       \
160
84.5k
    /* calculate codebook entries for this vector */                    \
161
341k
    for (j = 0; j < stages; j++) {                                      \
162
257k
        entries[j] = (((bit_cache >> (4 * (stages - j - 1))) & 0xF) +   \
163
257k
                      16 * j) << (level + 1);                           \
164
257k
    }                                                                   \
165
84.5k
    mean -= stages * 128;                                               \
166
84.5k
    n4    = (mean << 16) + mean;
167
168
static int svq1_decode_block_intra(GetBitContext *bitbuf, uint8_t *pixels,
169
                                   ptrdiff_t pitch)
170
152k
{
171
152k
    uint32_t bit_cache;
172
152k
    uint8_t *list[63];
173
152k
    uint32_t *dst;
174
152k
    const uint32_t *codebook;
175
152k
    int entries[6];
176
152k
    int i, j, m, n;
177
152k
    int stages;
178
152k
    unsigned mean;
179
152k
    unsigned x, y, width, height, level;
180
152k
    uint32_t n1, n2, n3, n4;
181
182
    /* initialize list for breadth first processing of vectors */
183
152k
    list[0] = pixels;
184
185
    /* recursively process vector */
186
483k
    for (i = 0, m = 1, n = 1, level = 5; i < n; i++) {
187
335k
        SVQ1_PROCESS_VECTOR();
188
189
        /* destination address and vector size */
190
335k
        dst    = (uint32_t *)list[i];
191
335k
        width  = 1 << ((4 + level) / 2);
192
335k
        height = 1 << ((3 + level) / 2);
193
194
        /* get number of stages (-1 skips vector, 0 for mean only) */
195
335k
        stages = get_vlc2(bitbuf, svq1_intra_multistage[level], 4, 2) - 1;
196
197
335k
        if (stages == -1) {
198
53.2k
            for (y = 0; y < height; y++)
199
47.7k
                memset(&dst[y * (pitch / 4)], 0, width);
200
5.42k
            continue;   /* skip vector */
201
5.42k
        }
202
203
329k
        if ((stages > 0 && level >= 4)) {
204
3.43k
            ff_dlog(NULL,
205
3.43k
                    "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",
206
3.43k
                    stages, level);
207
3.43k
            return AVERROR_INVALIDDATA;  /* invalid vector */
208
3.43k
        }
209
326k
        av_assert0(stages >= 0);
210
211
326k
        mean = get_vlc2(bitbuf, svq1_intra_mean, 8, 3);
212
213
326k
        if (stages == 0) {
214
2.82M
            for (y = 0; y < height; y++)
215
2.54M
                memset(&dst[y * (pitch / 4)], mean, width);
216
288k
        } else {
217
37.3k
            SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_intra_codebooks);
218
219
152k
            for (y = 0; y < height; y++) {
220
267k
                for (x = 0; x < width / 4; x++, codebook++) {
221
152k
                    n1 = n4;
222
152k
                    n2 = n4;
223
152k
                    SVQ1_ADD_CODEBOOK()
224
                    /* store result */
225
152k
                    dst[x] = n1 << 8 | n2;
226
152k
                }
227
115k
                dst += pitch / 4;
228
115k
            }
229
37.3k
        }
230
326k
    }
231
232
148k
    return 0;
233
152k
}
234
235
static int svq1_decode_block_non_intra(GetBitContext *bitbuf, uint8_t *pixels,
236
                                       ptrdiff_t pitch, int buggy)
237
19.9k
{
238
19.9k
    uint32_t bit_cache;
239
19.9k
    uint8_t *list[63];
240
19.9k
    uint32_t *dst;
241
19.9k
    const uint32_t *codebook;
242
19.9k
    int entries[6];
243
19.9k
    int i, j, m, n;
244
19.9k
    int stages;
245
19.9k
    unsigned mean;
246
19.9k
    int x, y, width, height, level;
247
19.9k
    uint32_t n1, n2, n3, n4;
248
249
    /* initialize list for breadth first processing of vectors */
250
19.9k
    list[0] = pixels;
251
252
    /* recursively process vector */
253
96.8k
    for (i = 0, m = 1, n = 1, level = 5; i < n; i++) {
254
77.8k
        SVQ1_PROCESS_VECTOR();
255
256
        /* destination address and vector size */
257
77.8k
        dst    = (uint32_t *)list[i];
258
77.8k
        width  = 1 << ((4 + level) / 2);
259
77.8k
        height = 1 << ((3 + level) / 2);
260
261
        /* get number of stages (-1 skips vector, 0 for mean only) */
262
77.8k
        stages = get_vlc2(bitbuf, svq1_inter_multistage[level], 3, 2) - 1;
263
264
77.8k
        if (stages == -1)
265
29.7k
            continue;           /* skip vector */
266
267
48.0k
        if ((stages > 0 && level >= 4)) {
268
882
            ff_dlog(NULL,
269
882
                    "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",
270
882
                    stages, level);
271
882
            return AVERROR_INVALIDDATA;  /* invalid vector */
272
882
        }
273
47.1k
        av_assert0(stages >= 0);
274
275
47.1k
        mean = get_vlc2(bitbuf, svq1_inter_mean, 9, 3) - 256;
276
277
47.1k
        if (buggy) {
278
5.62k
            if (mean == -128)
279
204
                mean = 128;
280
5.42k
            else if (mean == 128)
281
197
                mean = -128;
282
5.62k
        }
283
284
47.1k
        SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_inter_codebooks);
285
286
255k
        for (y = 0; y < height; y++) {
287
686k
            for (x = 0; x < width / 4; x++) {
288
478k
                n3 = dst[x];
289
                /* add mean value to vector */
290
478k
                n1 = n4 + ((n3 & 0xFF00FF00) >> 8);
291
478k
                n2 = n4 +  (n3 & 0x00FF00FF);
292
478k
                SVQ1_ADD_CODEBOOK()
293
                /* store result */
294
478k
                dst[x] = n1 << 8 | n2;
295
478k
                if (codebook != NULL)
296
198k
                    codebook++;
297
478k
            }
298
208k
            dst += pitch / 4;
299
208k
        }
300
47.1k
    }
301
19.0k
    return 0;
302
19.9k
}
303
304
static int svq1_decode_motion_vector(GetBitContext *bitbuf, svq1_pmv *mv,
305
                                     svq1_pmv **pmv)
306
47.8k
{
307
47.8k
    int diff;
308
47.8k
    int i;
309
310
141k
    for (i = 0; i < 2; i++) {
311
        /* get motion code */
312
95.1k
        diff = get_vlc2(bitbuf, svq1_motion_component, 7, 2);
313
95.1k
        if (diff < 0)
314
1.39k
            return AVERROR_INVALIDDATA;
315
93.7k
        else if (diff) {
316
48.4k
            if (get_bits1(bitbuf))
317
27.0k
                diff = -diff;
318
48.4k
        }
319
320
        /* add median of motion vector predictors and clip result */
321
93.7k
        if (i == 1)
322
46.4k
            mv->y = sign_extend(diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y), 6);
323
47.2k
        else
324
47.2k
            mv->x = sign_extend(diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x), 6);
325
93.7k
    }
326
327
46.4k
    return 0;
328
47.8k
}
329
330
static void svq1_skip_block(uint8_t *current, uint8_t *previous,
331
                            ptrdiff_t pitch, int x, int y)
332
17.4k
{
333
17.4k
    uint8_t *src;
334
17.4k
    uint8_t *dst;
335
17.4k
    int i;
336
337
17.4k
    src = &previous[x + y * pitch];
338
17.4k
    dst = current;
339
340
296k
    for (i = 0; i < 16; i++) {
341
279k
        memcpy(dst, src, 16);
342
279k
        src += pitch;
343
279k
        dst += pitch;
344
279k
    }
345
17.4k
}
346
347
static int svq1_motion_inter_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
348
                                   uint8_t *current, uint8_t *previous,
349
                                   ptrdiff_t pitch, svq1_pmv *motion, int x, int y,
350
                                   int width, int height)
351
11.9k
{
352
11.9k
    uint8_t *src;
353
11.9k
    uint8_t *dst;
354
11.9k
    svq1_pmv mv;
355
11.9k
    svq1_pmv *pmv[3];
356
11.9k
    int result;
357
358
    /* predict and decode motion vector */
359
11.9k
    pmv[0] = &motion[0];
360
11.9k
    if (y == 0) {
361
4.00k
        pmv[1] =
362
4.00k
        pmv[2] = pmv[0];
363
7.96k
    } else {
364
7.96k
        pmv[1] = &motion[x / 8 + 2];
365
7.96k
        pmv[2] = &motion[x / 8 + 4];
366
7.96k
    }
367
368
11.9k
    result = svq1_decode_motion_vector(bitbuf, &mv, pmv);
369
11.9k
    if (result)
370
397
        return result;
371
372
11.5k
    motion[0].x         =
373
11.5k
    motion[x / 8 + 2].x =
374
11.5k
    motion[x / 8 + 3].x = mv.x;
375
11.5k
    motion[0].y         =
376
11.5k
    motion[x / 8 + 2].y =
377
11.5k
    motion[x / 8 + 3].y = mv.y;
378
379
11.5k
    mv.x = av_clip(mv.x, -2 * x, 2 * (width  - x - 16));
380
11.5k
    mv.y = av_clip(mv.y, -2 * y, 2 * (height - y - 16));
381
382
11.5k
    src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1)) * pitch];
383
11.5k
    dst = current;
384
385
11.5k
    hdsp->put_pixels_tab[0][(mv.y & 1) << 1 | (mv.x & 1)](dst, src, pitch, 16);
386
387
11.5k
    return 0;
388
11.9k
}
389
390
static int svq1_motion_inter_4v_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
391
                                      uint8_t *current, uint8_t *previous,
392
                                      ptrdiff_t pitch, svq1_pmv *motion, int x, int y,
393
                                      int width, int height)
394
9.34k
{
395
9.34k
    uint8_t *src;
396
9.34k
    uint8_t *dst;
397
9.34k
    svq1_pmv mv;
398
9.34k
    svq1_pmv *pmv[4];
399
9.34k
    int i, result;
400
401
    /* predict and decode motion vector (0) */
402
9.34k
    pmv[0] = &motion[0];
403
9.34k
    if (y == 0) {
404
4.21k
        pmv[1] =
405
4.21k
        pmv[2] = pmv[0];
406
5.12k
    } else {
407
5.12k
        pmv[1] = &motion[(x / 8) + 2];
408
5.12k
        pmv[2] = &motion[(x / 8) + 4];
409
5.12k
    }
410
411
9.34k
    result = svq1_decode_motion_vector(bitbuf, &mv, pmv);
412
9.34k
    if (result)
413
220
        return result;
414
415
    /* predict and decode motion vector (1) */
416
9.12k
    pmv[0] = &mv;
417
9.12k
    if (y == 0) {
418
4.13k
        pmv[1] =
419
4.13k
        pmv[2] = pmv[0];
420
4.98k
    } else {
421
4.98k
        pmv[1] = &motion[(x / 8) + 3];
422
4.98k
    }
423
9.12k
    result = svq1_decode_motion_vector(bitbuf, &motion[0], pmv);
424
9.12k
    if (result)
425
308
        return result;
426
427
    /* predict and decode motion vector (2) */
428
8.81k
    pmv[1] = &motion[0];
429
8.81k
    pmv[2] = &motion[(x / 8) + 1];
430
431
8.81k
    result = svq1_decode_motion_vector(bitbuf, &motion[(x / 8) + 2], pmv);
432
8.81k
    if (result)
433
233
        return result;
434
435
    /* predict and decode motion vector (3) */
436
8.58k
    pmv[2] = &motion[(x / 8) + 2];
437
8.58k
    pmv[3] = &motion[(x / 8) + 3];
438
439
8.58k
    result = svq1_decode_motion_vector(bitbuf, pmv[3], pmv);
440
8.58k
    if (result)
441
240
        return result;
442
443
    /* form predictions */
444
41.7k
    for (i = 0; i < 4; i++) {
445
33.3k
        int mvx = pmv[i]->x + (i  & 1) * 16;
446
33.3k
        int mvy = pmv[i]->y + (i >> 1) * 16;
447
448
        // FIXME: clipping or padding?
449
33.3k
        mvx = av_clip(mvx, -2 * x, 2 * (width  - x - 8));
450
33.3k
        mvy = av_clip(mvy, -2 * y, 2 * (height - y - 8));
451
452
33.3k
        src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1)) * pitch];
453
33.3k
        dst = current;
454
455
33.3k
        hdsp->put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst, src, pitch, 8);
456
457
        /* select next block */
458
33.3k
        if (i & 1)
459
16.6k
            current += 8 * (pitch - 1);
460
16.6k
        else
461
16.6k
            current += 8;
462
33.3k
    }
463
464
8.34k
    return 0;
465
8.58k
}
466
467
static int svq1_decode_delta_block(AVCodecContext *avctx, HpelDSPContext *hdsp,
468
                                   GetBitContext *bitbuf,
469
                                   uint8_t *current, uint8_t *previous,
470
                                   ptrdiff_t pitch, svq1_pmv *motion, int x, int y,
471
                                   int width, int height, int buggy)
472
46.2k
{
473
46.2k
    uint32_t block_type;
474
46.2k
    int result = 0;
475
476
    /* get block type */
477
46.2k
    block_type = get_vlc2(bitbuf, svq1_block_type,
478
46.2k
                          SVQ1_BLOCK_TYPE_VLC_BITS, 1);
479
480
    /* reset motion vectors */
481
46.2k
    if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
482
24.8k
        motion[0].x         =
483
24.8k
        motion[0].y         =
484
24.8k
        motion[x / 8 + 2].x =
485
24.8k
        motion[x / 8 + 2].y =
486
24.8k
        motion[x / 8 + 3].x =
487
24.8k
        motion[x / 8 + 3].y = 0;
488
24.8k
    }
489
490
46.2k
    switch (block_type) {
491
17.4k
    case SVQ1_BLOCK_SKIP:
492
17.4k
        svq1_skip_block(current, previous, pitch, x, y);
493
17.4k
        break;
494
495
11.9k
    case SVQ1_BLOCK_INTER:
496
11.9k
        result = svq1_motion_inter_block(hdsp, bitbuf, current, previous,
497
11.9k
                                         pitch, motion, x, y, width, height);
498
499
11.9k
        if (result != 0) {
500
397
            ff_dlog(avctx, "Error in svq1_motion_inter_block %i\n", result);
501
397
            break;
502
397
        }
503
11.5k
        result = svq1_decode_block_non_intra(bitbuf, current, pitch, buggy);
504
11.5k
        break;
505
506
9.34k
    case SVQ1_BLOCK_INTER_4V:
507
9.34k
        result = svq1_motion_inter_4v_block(hdsp, bitbuf, current, previous,
508
9.34k
                                            pitch, motion, x, y, width, height);
509
510
9.34k
        if (result != 0) {
511
1.00k
            ff_dlog(avctx, "Error in svq1_motion_inter_4v_block %i\n", result);
512
1.00k
            break;
513
1.00k
        }
514
8.34k
        result = svq1_decode_block_non_intra(bitbuf, current, pitch, buggy);
515
8.34k
        break;
516
517
7.43k
    case SVQ1_BLOCK_INTRA:
518
7.43k
        result = svq1_decode_block_intra(bitbuf, current, pitch);
519
7.43k
        break;
520
46.2k
    }
521
522
46.2k
    return result;
523
46.2k
}
524
525
static void svq1_parse_string(GetBitContext *bitbuf, uint8_t out[257])
526
1.00k
{
527
1.00k
    uint8_t seed;
528
1.00k
    int i;
529
530
1.00k
    out[0] = get_bits(bitbuf, 8);
531
1.00k
    seed   = string_table[out[0]];
532
533
103k
    for (i = 1; i <= out[0]; i++) {
534
102k
        out[i] = get_bits(bitbuf, 8) ^ seed;
535
102k
        seed   = string_table[out[i] ^ seed];
536
102k
    }
537
1.00k
    out[i] = 0;
538
1.00k
}
539
540
static int svq1_decode_frame_header(AVCodecContext *avctx, AVFrame *frame, int * buggy)
541
122k
{
542
122k
    SVQ1Context *s = avctx->priv_data;
543
122k
    GetBitContext *bitbuf = &s->gb;
544
122k
    int frame_size_code;
545
122k
    int width  = s->width;
546
122k
    int height = s->height;
547
122k
    int tempref;
548
549
122k
    tempref = get_bits(bitbuf, 8); /* temporal_reference */
550
122k
    *buggy = tempref == 0 && s->last_tempref == 0 && avctx->extradata_size == 0;
551
122k
    s->last_tempref = tempref;
552
553
    /* frame type */
554
122k
    s->nonref = 0;
555
122k
    switch (get_bits(bitbuf, 2)) {
556
88.2k
    case 0:
557
88.2k
        frame->pict_type = AV_PICTURE_TYPE_I;
558
88.2k
        break;
559
1.82k
    case 2:
560
1.82k
        s->nonref = 1;
561
33.7k
    case 1:
562
33.7k
        frame->pict_type = AV_PICTURE_TYPE_P;
563
33.7k
        break;
564
354
    default:
565
354
        av_log(avctx, AV_LOG_ERROR, "Invalid frame type.\n");
566
354
        return AVERROR_INVALIDDATA;
567
122k
    }
568
569
122k
    if (frame->pict_type == AV_PICTURE_TYPE_I) {
570
        /* unknown fields */
571
88.2k
        if (s->frame_code == 0x50 || s->frame_code == 0x60) {
572
804
            int csum = get_bits(bitbuf, 16);
573
574
804
            csum = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_CCITT), av_bswap16(csum), bitbuf->buffer, bitbuf->size_in_bits >> 3));
575
576
804
            ff_dlog(avctx, "%s checksum (%02x) for packet data\n",
577
804
                    (csum == 0) ? "correct" : "incorrect", csum);
578
804
        }
579
580
88.2k
        if ((s->frame_code ^ 0x10) >= 0x50) {
581
1.00k
            uint8_t msg[257];
582
583
1.00k
            svq1_parse_string(bitbuf, msg);
584
585
1.00k
            av_log(avctx, AV_LOG_INFO,
586
1.00k
                   "embedded message:\n%s\n", ((char *)msg) + 1);
587
1.00k
        }
588
589
88.2k
        skip_bits(bitbuf, 2);
590
88.2k
        skip_bits(bitbuf, 2);
591
88.2k
        skip_bits1(bitbuf);
592
593
        /* load frame size */
594
88.2k
        frame_size_code = get_bits(bitbuf, 3);
595
596
88.2k
        if (frame_size_code == 7) {
597
            /* load width, height (12 bits each) */
598
81.6k
            width  = get_bits(bitbuf, 12);
599
81.6k
            height = get_bits(bitbuf, 12);
600
601
81.6k
            if (!width || !height)
602
633
                return AVERROR_INVALIDDATA;
603
81.6k
        } else {
604
            /* get width, height from table */
605
6.61k
            width  = ff_svq1_frame_size_table[frame_size_code][0];
606
6.61k
            height = ff_svq1_frame_size_table[frame_size_code][1];
607
6.61k
        }
608
88.2k
    }
609
610
    /* unknown fields */
611
121k
    if (get_bits1(bitbuf)) {
612
17.9k
        skip_bits1(bitbuf);    /* use packet checksum if (1) */
613
17.9k
        skip_bits1(bitbuf);    /* component checksums after image data if (1) */
614
615
17.9k
        if (get_bits(bitbuf, 2) != 0)
616
697
            return AVERROR_INVALIDDATA;
617
17.9k
    }
618
619
120k
    if (get_bits1(bitbuf)) {
620
8.59k
        skip_bits1(bitbuf);
621
8.59k
        skip_bits(bitbuf, 4);
622
8.59k
        skip_bits1(bitbuf);
623
8.59k
        skip_bits(bitbuf, 2);
624
625
8.59k
        if (skip_1stop_8data_bits(bitbuf) < 0)
626
500
            return AVERROR_INVALIDDATA;
627
8.59k
    }
628
120k
    if (get_bits_left(bitbuf) <= 0)
629
4.75k
        return AVERROR_INVALIDDATA;
630
631
115k
    s->width  = width;
632
115k
    s->height = height;
633
115k
    return 0;
634
120k
}
635
636
static int svq1_decode_frame(AVCodecContext *avctx, AVFrame *cur,
637
                             int *got_frame, AVPacket *avpkt)
638
246k
{
639
246k
    const uint8_t *buf = avpkt->data;
640
246k
    int buf_size       = avpkt->size;
641
246k
    SVQ1Context     *s = avctx->priv_data;
642
246k
    uint8_t *current;
643
246k
    int result, i, x, y, width, height, buggy;
644
246k
    int ret;
645
646
    /* initialize bit buffer */
647
246k
    ret = init_get_bits8(&s->gb, buf, buf_size);
648
246k
    if (ret < 0)
649
0
        return ret;
650
651
    /* decode frame header */
652
246k
    s->frame_code = get_bits(&s->gb, 22);
653
654
246k
    if ((s->frame_code & ~0x70) || !(s->frame_code & 0x60))
655
123k
        return AVERROR_INVALIDDATA;
656
657
    /* swap some header bytes (why?) */
658
122k
    if (s->frame_code != 0x20) {
659
3.07k
        uint32_t *src;
660
661
3.07k
        if (buf_size < 9 * 4) {
662
366
            av_log(avctx, AV_LOG_ERROR, "Input packet too small\n");
663
366
            return AVERROR_INVALIDDATA;
664
366
        }
665
666
2.70k
        av_fast_padded_malloc(&s->pkt_swapped,
667
2.70k
                              &s->pkt_swapped_allocated,
668
2.70k
                              buf_size);
669
2.70k
        if (!s->pkt_swapped)
670
0
            return AVERROR(ENOMEM);
671
672
2.70k
        memcpy(s->pkt_swapped, buf, buf_size);
673
2.70k
        buf = s->pkt_swapped;
674
2.70k
        init_get_bits(&s->gb, buf, buf_size * 8);
675
2.70k
        skip_bits(&s->gb, 22);
676
677
2.70k
        src = (uint32_t *)(s->pkt_swapped + 4);
678
679
13.5k
        for (i = 0; i < 4; i++)
680
10.8k
            src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
681
2.70k
    }
682
683
122k
    result = svq1_decode_frame_header(avctx, cur, &buggy);
684
122k
    if (result != 0) {
685
6.94k
        ff_dlog(avctx, "Error in svq1_decode_frame_header %i\n", result);
686
6.94k
        return result;
687
6.94k
    }
688
689
115k
    result = ff_set_dimensions(avctx, s->width, s->height);
690
115k
    if (result < 0)
691
709
        return result;
692
693
114k
    if ((avctx->skip_frame >= AVDISCARD_NONREF && s->nonref) ||
694
114k
        (avctx->skip_frame >= AVDISCARD_NONKEY &&
695
13.3k
         cur->pict_type != AV_PICTURE_TYPE_I) ||
696
101k
        avctx->skip_frame >= AVDISCARD_ALL)
697
13.7k
        return buf_size;
698
699
101k
    result = ff_get_buffer(avctx, cur, s->nonref ? 0 : AV_GET_BUFFER_FLAG_REF);
700
101k
    if (result < 0)
701
384
        return result;
702
703
100k
    av_fast_padded_malloc(&s->pmv, &s->pmv_allocated, (FFALIGN(s->width, 16) / 8 + 3) * sizeof(*s->pmv));
704
100k
    if (!s->pmv)
705
0
        return AVERROR(ENOMEM);
706
707
    /* decode y, u and v components */
708
374k
    for (i = 0; i < 3; i++) {
709
283k
        int linesize = cur->linesize[i];
710
283k
        if (i == 0) {
711
100k
            width    = FFALIGN(s->width,  16);
712
100k
            height   = FFALIGN(s->height, 16);
713
182k
        } else {
714
182k
            if (avctx->flags & AV_CODEC_FLAG_GRAY)
715
0
                break;
716
182k
            width    = FFALIGN(s->width  / 4, 16);
717
182k
            height   = FFALIGN(s->height / 4, 16);
718
182k
        }
719
720
283k
        current = cur->data[i];
721
722
283k
        if (cur->pict_type == AV_PICTURE_TYPE_I) {
723
            /* keyframe */
724
504k
            for (y = 0; y < height; y += 16) {
725
407k
                for (x = 0; x < width; x += 16) {
726
144k
                    result = svq1_decode_block_intra(&s->gb, &current[x],
727
144k
                                                     linesize);
728
144k
                    if (result) {
729
2.40k
                        av_log(avctx, AV_LOG_ERROR,
730
2.40k
                               "Error in svq1_decode_block %i (keyframe)\n",
731
2.40k
                               result);
732
2.40k
                        return result;
733
2.40k
                    }
734
144k
                }
735
262k
                current += 16 * linesize;
736
262k
            }
737
241k
        } else {
738
            /* delta frame */
739
41.6k
            uint8_t *previous = s->prev->data[i];
740
41.6k
            if (!previous ||
741
38.3k
                s->prev->width != s->width || s->prev->height != s->height) {
742
3.93k
                av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
743
3.93k
                return AVERROR_INVALIDDATA;
744
3.93k
            }
745
746
37.7k
            memset(s->pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv));
747
748
73.3k
            for (y = 0; y < height; y += 16) {
749
81.8k
                for (x = 0; x < width; x += 16) {
750
46.2k
                    result = svq1_decode_delta_block(avctx, &s->hdsp,
751
46.2k
                                                     &s->gb, &current[x],
752
46.2k
                                                     previous, linesize,
753
46.2k
                                                     s->pmv, x, y, width, height, buggy);
754
46.2k
                    if (result != 0) {
755
3.31k
                        ff_dlog(avctx,
756
3.31k
                                "Error in svq1_decode_delta_block %i\n",
757
3.31k
                                result);
758
3.31k
                        return result;
759
3.31k
                    }
760
46.2k
                }
761
762
35.6k
                s->pmv[0].x =
763
35.6k
                s->pmv[0].y = 0;
764
765
35.6k
                current += 16 * linesize;
766
35.6k
            }
767
37.7k
        }
768
283k
    }
769
770
90.9k
    if (!s->nonref) {
771
90.6k
        result = av_frame_replace(s->prev, cur);
772
90.6k
        if (result < 0)
773
0
            return result;
774
90.6k
    }
775
776
90.9k
    *got_frame = 1;
777
90.9k
    result     = buf_size;
778
779
90.9k
    return result;
780
90.9k
}
781
782
static av_cold void svq1_static_init(void)
783
1
{
784
1
    static VLCElem table[196];
785
1
    VLCInitState state = VLC_INIT_STATE(table);
786
787
1
    VLC_INIT_STATIC_TABLE(svq1_block_type, SVQ1_BLOCK_TYPE_VLC_BITS, 4,
788
1
                          &ff_svq1_block_type_vlc[0][1], 2, 1,
789
1
                          &ff_svq1_block_type_vlc[0][0], 2, 1, 0);
790
791
1
    VLC_INIT_STATIC_TABLE(svq1_motion_component, 7, 33,
792
1
                          &ff_mvtab[0][1], 2, 1,
793
1
                          &ff_mvtab[0][0], 2, 1, 0);
794
795
7
    for (int i = 0; i < 6; i++) {
796
6
        svq1_intra_multistage[i] =
797
6
            ff_vlc_init_tables(&state, 4, 8,
798
6
                               &ff_svq1_intra_multistage_vlc[i][0][1], 2, 1,
799
6
                               &ff_svq1_intra_multistage_vlc[i][0][0], 2, 1, 0);
800
6
        svq1_inter_multistage[i] =
801
6
            ff_vlc_init_tables(&state, 3, 8,
802
6
                               &ff_svq1_inter_multistage_vlc[i][0][1], 2, 1,
803
6
                               &ff_svq1_inter_multistage_vlc[i][0][0], 2, 1, 0);
804
6
    }
805
806
1
    VLC_INIT_STATIC_TABLE(svq1_intra_mean, 8, 256,
807
1
                          &ff_svq1_intra_mean_vlc[0][1], 4, 2,
808
1
                          &ff_svq1_intra_mean_vlc[0][0], 4, 2, 0);
809
810
1
    VLC_INIT_STATIC_TABLE(svq1_inter_mean, 9, 512,
811
1
                          &ff_svq1_inter_mean_vlc[0][1], 4, 2,
812
1
                          &ff_svq1_inter_mean_vlc[0][0], 4, 2, 0);
813
1
}
814
815
static av_cold int svq1_decode_init(AVCodecContext *avctx)
816
2.81k
{
817
2.81k
    static AVOnce init_static_once = AV_ONCE_INIT;
818
2.81k
    SVQ1Context *s = avctx->priv_data;
819
820
2.81k
    s->prev = av_frame_alloc();
821
2.81k
    if (!s->prev)
822
0
        return AVERROR(ENOMEM);
823
824
2.81k
    s->width            = avctx->width  + 3 & ~3;
825
2.81k
    s->height           = avctx->height + 3 & ~3;
826
2.81k
    avctx->pix_fmt      = AV_PIX_FMT_YUV410P;
827
828
2.81k
    ff_hpeldsp_init(&s->hdsp, avctx->flags);
829
830
2.81k
    ff_thread_once(&init_static_once, svq1_static_init);
831
832
2.81k
    s->last_tempref = 0xFF;
833
834
2.81k
    return 0;
835
2.81k
}
836
837
static av_cold int svq1_decode_end(AVCodecContext *avctx)
838
2.81k
{
839
2.81k
    SVQ1Context *s = avctx->priv_data;
840
841
2.81k
    av_frame_free(&s->prev);
842
2.81k
    av_freep(&s->pkt_swapped);
843
2.81k
    s->pkt_swapped_allocated = 0;
844
2.81k
    av_freep(&s->pmv);
845
2.81k
    s->pmv_allocated = 0;
846
847
2.81k
    return 0;
848
2.81k
}
849
850
static av_cold void svq1_flush(AVCodecContext *avctx)
851
48.2k
{
852
48.2k
    SVQ1Context *s = avctx->priv_data;
853
854
48.2k
    av_frame_unref(s->prev);
855
48.2k
}
856
857
const FFCodec ff_svq1_decoder = {
858
    .p.name         = "svq1",
859
    CODEC_LONG_NAME("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
860
    .p.type         = AVMEDIA_TYPE_VIDEO,
861
    .p.id           = AV_CODEC_ID_SVQ1,
862
    .priv_data_size = sizeof(SVQ1Context),
863
    .init           = svq1_decode_init,
864
    .close          = svq1_decode_end,
865
    FF_CODEC_DECODE_CB(svq1_decode_frame),
866
    .p.capabilities = AV_CODEC_CAP_DR1,
867
    .flush          = svq1_flush,
868
};