GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/svq3.c Lines: 695 848 82.0 %
Date: 2021-01-22 05:18:52 Branches: 440 571 77.1 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2003 The FFmpeg Project
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/*
22
 * How to use this decoder:
23
 * SVQ3 data is transported within Apple Quicktime files. Quicktime files
24
 * have stsd atoms to describe media trak properties. A stsd atom for a
25
 * video trak contains 1 or more ImageDescription atoms. These atoms begin
26
 * with the 4-byte length of the atom followed by the codec fourcc. Some
27
 * decoders need information in this atom to operate correctly. Such
28
 * is the case with SVQ3. In order to get the best use out of this decoder,
29
 * the calling app must make the SVQ3 ImageDescription atom available
30
 * via the AVCodecContext's extradata[_size] field:
31
 *
32
 * AVCodecContext.extradata = pointer to ImageDescription, first characters
33
 * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
34
 * AVCodecContext.extradata_size = size of ImageDescription atom memory
35
 * buffer (which will be the same as the ImageDescription atom size field
36
 * from the QT file, minus 4 bytes since the length is missing)
37
 *
38
 * You will know you have these parameters passed correctly when the decoder
39
 * correctly decodes this file:
40
 *  http://samples.mplayerhq.hu/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
41
 */
42
43
#include <inttypes.h>
44
45
#include "libavutil/attributes.h"
46
#include "libavutil/crc.h"
47
#include "libavutil/mem_internal.h"
48
49
#include "internal.h"
50
#include "avcodec.h"
51
#include "mpegutils.h"
52
#include "h264dec.h"
53
#include "h264data.h"
54
#include "golomb.h"
55
#include "hpeldsp.h"
56
#include "mathops.h"
57
#include "rectangle.h"
58
#include "tpeldsp.h"
59
60
#if CONFIG_ZLIB
61
#include <zlib.h>
62
#endif
63
64
#include "svq1.h"
65
66
/**
67
 * @file
68
 * svq3 decoder.
69
 */
70
71
typedef struct SVQ3Frame {
72
    AVFrame *f;
73
74
    int16_t (*motion_val_buf[2])[2];
75
    int16_t (*motion_val[2])[2];
76
77
    uint32_t *mb_type_buf, *mb_type;
78
} SVQ3Frame;
79
80
typedef struct SVQ3Context {
81
    AVCodecContext *avctx;
82
83
    H264DSPContext  h264dsp;
84
    H264PredContext hpc;
85
    HpelDSPContext hdsp;
86
    TpelDSPContext tdsp;
87
    VideoDSPContext vdsp;
88
89
    SVQ3Frame *cur_pic;
90
    SVQ3Frame *next_pic;
91
    SVQ3Frame *last_pic;
92
    GetBitContext gb;
93
    GetBitContext gb_slice;
94
    uint8_t *slice_buf;
95
    int slice_size;
96
    int halfpel_flag;
97
    int thirdpel_flag;
98
    int has_watermark;
99
    uint32_t watermark_key;
100
    uint8_t *buf;
101
    int buf_size;
102
    int adaptive_quant;
103
    int next_p_frame_damaged;
104
    int h_edge_pos;
105
    int v_edge_pos;
106
    int last_frame_output;
107
    int slice_num;
108
    int qscale;
109
    int cbp;
110
    int frame_num;
111
    int frame_num_offset;
112
    int prev_frame_num_offset;
113
    int prev_frame_num;
114
115
    enum AVPictureType pict_type;
116
    enum AVPictureType slice_type;
117
    int low_delay;
118
119
    int mb_x, mb_y;
120
    int mb_xy;
121
    int mb_width, mb_height;
122
    int mb_stride, mb_num;
123
    int b_stride;
124
125
    uint32_t *mb2br_xy;
126
127
    int chroma_pred_mode;
128
    int intra16x16_pred_mode;
129
130
    int8_t   intra4x4_pred_mode_cache[5 * 8];
131
    int8_t (*intra4x4_pred_mode);
132
133
    unsigned int top_samples_available;
134
    unsigned int topright_samples_available;
135
    unsigned int left_samples_available;
136
137
    uint8_t *edge_emu_buffer;
138
139
    DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
140
    DECLARE_ALIGNED(8,  int8_t, ref_cache)[2][5 * 8];
141
    DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
142
    DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
143
    DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
144
    uint32_t dequant4_coeff[QP_MAX_NUM + 1][16];
145
    int block_offset[2 * (16 * 3)];
146
    SVQ3Frame frames[3];
147
} SVQ3Context;
148
149
#define FULLPEL_MODE  1
150
#define HALFPEL_MODE  2
151
#define THIRDPEL_MODE 3
152
#define PREDICT_MODE  4
153
154
/* dual scan (from some older H.264 draft)
155
 * o-->o-->o   o
156
 *         |  /|
157
 * o   o   o / o
158
 * | / |   |/  |
159
 * o   o   o   o
160
 *   /
161
 * o-->o-->o-->o
162
 */
163
static const uint8_t svq3_scan[16] = {
164
    0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
165
    2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
166
    0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
167
    0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
168
};
169
170
static const uint8_t luma_dc_zigzag_scan[16] = {
171
    0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
172
    3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
173
    1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
174
    3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
175
};
176
177
static const uint8_t svq3_pred_0[25][2] = {
178
    { 0, 0 },
179
    { 1, 0 }, { 0, 1 },
180
    { 0, 2 }, { 1, 1 }, { 2, 0 },
181
    { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
182
    { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
183
    { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
184
    { 2, 4 }, { 3, 3 }, { 4, 2 },
185
    { 4, 3 }, { 3, 4 },
186
    { 4, 4 }
187
};
188
189
static const int8_t svq3_pred_1[6][6][5] = {
190
    { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
191
      { 2,  1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
192
    { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  4,  3 }, { 0, 1,  2,  4,  3 },
193
      { 0,  2,  1,  4,  3 }, { 2, 0,  1,  3,  4 }, { 0, 4,  2,  1,  3 } },
194
    { { 2,  0, -1, -1, -1 }, { 2, 1,  0,  4,  3 }, { 1, 2,  4,  0,  3 },
195
      { 2,  1,  0,  4,  3 }, { 2, 1,  4,  3,  0 }, { 1, 2,  4,  0,  3 } },
196
    { { 2,  0, -1, -1, -1 }, { 2, 0,  1,  4,  3 }, { 1, 2,  0,  4,  3 },
197
      { 2,  1,  0,  4,  3 }, { 2, 1,  3,  4,  0 }, { 2, 4,  1,  0,  3 } },
198
    { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  3,  4 }, { 1, 2,  3,  0,  4 },
199
      { 2,  0,  1,  3,  4 }, { 2, 1,  3,  0,  4 }, { 2, 0,  4,  3,  1 } },
200
    { { 0,  2, -1, -1, -1 }, { 0, 2,  4,  1,  3 }, { 1, 4,  2,  0,  3 },
201
      { 4,  2,  0,  1,  3 }, { 2, 0,  1,  4,  3 }, { 4, 2,  1,  0,  3 } },
202
};
203
204
static const struct {
205
    uint8_t run;
206
    uint8_t level;
207
} svq3_dct_tables[2][16] = {
208
    { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
209
      { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
210
    { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
211
      { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
212
};
213
214
static const uint32_t svq3_dequant_coeff[32] = {
215
     3881,  4351,  4890,  5481,   6154,   6914,   7761,   8718,
216
     9781, 10987, 12339, 13828,  15523,  17435,  19561,  21873,
217
    24552, 27656, 30847, 34870,  38807,  43747,  49103,  54683,
218
    61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
219
};
220
221
2537
static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
222
{
223
2537
    const unsigned qmul = svq3_dequant_coeff[qp];
224
#define stride 16
225
    int i;
226
    int temp[16];
227
    static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
228
229
12685
    for (i = 0; i < 4; i++) {
230
10148
        const int z0 = 13 * (input[4 * i + 0] +      input[4 * i + 2]);
231
10148
        const int z1 = 13 * (input[4 * i + 0] -      input[4 * i + 2]);
232
10148
        const int z2 =  7 *  input[4 * i + 1] - 17 * input[4 * i + 3];
233
10148
        const int z3 = 17 *  input[4 * i + 1] +  7 * input[4 * i + 3];
234
235
10148
        temp[4 * i + 0] = z0 + z3;
236
10148
        temp[4 * i + 1] = z1 + z2;
237
10148
        temp[4 * i + 2] = z1 - z2;
238
10148
        temp[4 * i + 3] = z0 - z3;
239
    }
240
241
12685
    for (i = 0; i < 4; i++) {
242
10148
        const int offset = x_offset[i];
243
10148
        const int z0     = 13 * (temp[4 * 0 + i] +      temp[4 * 2 + i]);
244
10148
        const int z1     = 13 * (temp[4 * 0 + i] -      temp[4 * 2 + i]);
245
10148
        const int z2     =  7 *  temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
246
10148
        const int z3     = 17 *  temp[4 * 1 + i] +  7 * temp[4 * 3 + i];
247
248
10148
        output[stride *  0 + offset] = (int)((z0 + z3) * qmul + 0x80000) >> 20;
249
10148
        output[stride *  2 + offset] = (int)((z1 + z2) * qmul + 0x80000) >> 20;
250
10148
        output[stride *  8 + offset] = (int)((z1 - z2) * qmul + 0x80000) >> 20;
251
10148
        output[stride * 10 + offset] = (int)((z0 - z3) * qmul + 0x80000) >> 20;
252
    }
253
2537
}
254
#undef stride
255
256
4012263
static void svq3_add_idct_c(uint8_t *dst, int16_t *block,
257
                            int stride, int qp, int dc)
258
{
259
4012263
    const int qmul = svq3_dequant_coeff[qp];
260
    int i;
261
262
4012263
    if (dc) {
263
998567
        dc       = 13 * 13 * (dc == 1 ? 1538U* block[0]
264
972473
                                      : qmul * (block[0] >> 3) / 2);
265
998567
        block[0] = 0;
266
    }
267
268
20061315
    for (i = 0; i < 4; i++) {
269
16049052
        const int z0 = 13 * (block[0 + 4 * i] +      block[2 + 4 * i]);
270
16049052
        const int z1 = 13 * (block[0 + 4 * i] -      block[2 + 4 * i]);
271
16049052
        const int z2 =  7 *  block[1 + 4 * i] - 17 * block[3 + 4 * i];
272
16049052
        const int z3 = 17 *  block[1 + 4 * i] +  7 * block[3 + 4 * i];
273
274
16049052
        block[0 + 4 * i] = z0 + z3;
275
16049052
        block[1 + 4 * i] = z1 + z2;
276
16049052
        block[2 + 4 * i] = z1 - z2;
277
16049052
        block[3 + 4 * i] = z0 - z3;
278
    }
279
280
20061315
    for (i = 0; i < 4; i++) {
281
16049052
        const unsigned z0 = 13 * (block[i + 4 * 0] +      block[i + 4 * 2]);
282
16049052
        const unsigned z1 = 13 * (block[i + 4 * 0] -      block[i + 4 * 2]);
283
16049052
        const unsigned z2 =  7 *  block[i + 4 * 1] - 17 * block[i + 4 * 3];
284
16049052
        const unsigned z3 = 17 *  block[i + 4 * 1] +  7 * block[i + 4 * 3];
285
16049052
        const int rr = (dc + 0x80000u);
286
287
16049052
        dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((int)((z0 + z3) * qmul + rr) >> 20));
288
16049052
        dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((int)((z1 + z2) * qmul + rr) >> 20));
289
16049052
        dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((int)((z1 - z2) * qmul + rr) >> 20));
290
16049052
        dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((int)((z0 - z3) * qmul + rr) >> 20));
291
    }
292
293
4012263
    memset(block, 0, 16 * sizeof(int16_t));
294
4012263
}
295
296
3880617
static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
297
                                    int index, const int type)
298
{
299
    static const uint8_t *const scan_patterns[4] = {
300
        luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan
301
    };
302
303
    int run, level, sign, limit;
304
    unsigned vlc;
305
3880617
    const int intra           = 3 * type >> 2;
306
3880617
    const uint8_t *const scan = scan_patterns[type];
307
308
5000433
    for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
309
9817031
        for (; (vlc = get_interleaved_ue_golomb(gb)) != 0; index++) {
310
5376506
            if ((int32_t)vlc < 0)
311
                return -1;
312
313
5376506
            sign     = (vlc & 1) ? 0 : -1;
314
5376506
            vlc      = vlc + 1 >> 1;
315
316
5376506
            if (type == 3) {
317
249567
                if (vlc < 3) {
318
168966
                    run   = 0;
319
168966
                    level = vlc;
320
80601
                } else if (vlc < 4) {
321
34195
                    run   = 1;
322
34195
                    level = 1;
323
                } else {
324
46406
                    run   = vlc & 0x3;
325
46406
                    level = (vlc + 9 >> 2) - run;
326
                }
327
            } else {
328
5126939
                if (vlc < 16U) {
329
5034539
                    run   = svq3_dct_tables[intra][vlc].run;
330
5034539
                    level = svq3_dct_tables[intra][vlc].level;
331
92400
                } else if (intra) {
332
8622
                    run   = vlc & 0x7;
333

8622
                    level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
334
                } else {
335
83778
                    run   = vlc & 0xF;
336

83778
                    level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
337
                }
338
            }
339
340
341
5376506
            if ((index += run) >= limit)
342
                return -1;
343
344
5376506
            block[scan[index]] = (level ^ sign) - sign;
345
        }
346
347
4440525
        if (type != 2) {
348
3320709
            break;
349
        }
350
    }
351
352
3880617
    return 0;
353
}
354
355
static av_always_inline int
356
556200
svq3_fetch_diagonal_mv(const SVQ3Context *s, const int16_t **C,
357
                       int i, int list, int part_width)
358
{
359
556200
    const int topright_ref = s->ref_cache[list][i - 8 + part_width];
360
361
556200
    if (topright_ref != PART_NOT_AVAILABLE) {
362
474763
        *C = s->mv_cache[list][i - 8 + part_width];
363
474763
        return topright_ref;
364
    } else {
365
81437
        *C = s->mv_cache[list][i - 8 - 1];
366
81437
        return s->ref_cache[list][i - 8 - 1];
367
    }
368
}
369
370
/**
371
 * Get the predicted MV.
372
 * @param n the block index
373
 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
374
 * @param mx the x component of the predicted motion vector
375
 * @param my the y component of the predicted motion vector
376
 */
377
556200
static av_always_inline void svq3_pred_motion(const SVQ3Context *s, int n,
378
                                              int part_width, int list,
379
                                              int ref, int *const mx, int *const my)
380
{
381
556200
    const int index8       = scan8[n];
382
556200
    const int top_ref      = s->ref_cache[list][index8 - 8];
383
556200
    const int left_ref     = s->ref_cache[list][index8 - 1];
384
556200
    const int16_t *const A = s->mv_cache[list][index8 - 1];
385
556200
    const int16_t *const B = s->mv_cache[list][index8 - 8];
386
    const int16_t *C;
387
    int diagonal_ref, match_count;
388
389
/* mv_cache
390
 * B . . A T T T T
391
 * U . . L . . , .
392
 * U . . L . . . .
393
 * U . . L . . , .
394
 * . . . L . . . .
395
 */
396
397
556200
    diagonal_ref = svq3_fetch_diagonal_mv(s, &C, index8, list, part_width);
398
556200
    match_count  = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
399
556200
    if (match_count > 1) { //most common
400
532412
        *mx = mid_pred(A[0], B[0], C[0]);
401
532412
        *my = mid_pred(A[1], B[1], C[1]);
402
23788
    } else if (match_count == 1) {
403
23788
        if (left_ref == ref) {
404
23788
            *mx = A[0];
405
23788
            *my = A[1];
406
        } else if (top_ref == ref) {
407
            *mx = B[0];
408
            *my = B[1];
409
        } else {
410
            *mx = C[0];
411
            *my = C[1];
412
        }
413
    } else {
414
        if (top_ref      == PART_NOT_AVAILABLE &&
415
            diagonal_ref == PART_NOT_AVAILABLE &&
416
            left_ref     != PART_NOT_AVAILABLE) {
417
            *mx = A[0];
418
            *my = A[1];
419
        } else {
420
            *mx = mid_pred(A[0], B[0], C[0]);
421
            *my = mid_pred(A[1], B[1], C[1]);
422
        }
423
    }
424
556200
}
425
426
911088
static inline void svq3_mc_dir_part(SVQ3Context *s,
427
                                    int x, int y, int width, int height,
428
                                    int mx, int my, int dxy,
429
                                    int thirdpel, int dir, int avg)
430
{
431
911088
    const SVQ3Frame *pic = (dir == 0) ? s->last_pic : s->next_pic;
432
    uint8_t *src, *dest;
433
911088
    int i, emu = 0;
434
911088
    int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
435
911088
    int linesize   = s->cur_pic->f->linesize[0];
436
911088
    int uvlinesize = s->cur_pic->f->linesize[1];
437
438
911088
    mx += x;
439
911088
    my += y;
440
441

911088
    if (mx < 0 || mx >= s->h_edge_pos - width  - 1 ||
442
869995
        my < 0 || my >= s->v_edge_pos - height - 1) {
443
88647
        emu = 1;
444
88647
        mx = av_clip(mx, -16, s->h_edge_pos - width  + 15);
445
88647
        my = av_clip(my, -16, s->v_edge_pos - height + 15);
446
    }
447
448
    /* form component predictions */
449
911088
    dest = s->cur_pic->f->data[0] + x + y * linesize;
450
911088
    src  = pic->f->data[0] + mx + my * linesize;
451
452
911088
    if (emu) {
453
88647
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
454
                                 linesize, linesize,
455
                                 width + 1, height + 1,
456
                                 mx, my, s->h_edge_pos, s->v_edge_pos);
457
88647
        src = s->edge_emu_buffer;
458
    }
459
911088
    if (thirdpel)
460
        (avg ? s->tdsp.avg_tpel_pixels_tab
461
182
             : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, linesize,
462
                                                 width, height);
463
    else
464
        (avg ? s->hdsp.avg_pixels_tab
465
910906
             : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, linesize,
466
                                                       height);
467
468
911088
    if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
469
911088
        mx     = mx + (mx < (int) x) >> 1;
470
911088
        my     = my + (my < (int) y) >> 1;
471
911088
        width  = width  >> 1;
472
911088
        height = height >> 1;
473
911088
        blocksize++;
474
475
2733264
        for (i = 1; i < 3; i++) {
476
1822176
            dest = s->cur_pic->f->data[i] + (x >> 1) + (y >> 1) * uvlinesize;
477
1822176
            src  = pic->f->data[i] + mx + my * uvlinesize;
478
479
1822176
            if (emu) {
480
177294
                s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
481
                                         uvlinesize, uvlinesize,
482
                                         width + 1, height + 1,
483
177294
                                         mx, my, (s->h_edge_pos >> 1),
484
177294
                                         s->v_edge_pos >> 1);
485
177294
                src = s->edge_emu_buffer;
486
            }
487
1822176
            if (thirdpel)
488
                (avg ? s->tdsp.avg_tpel_pixels_tab
489
364
                     : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src,
490
                                                         uvlinesize,
491
                                                         width, height);
492
            else
493
                (avg ? s->hdsp.avg_pixels_tab
494
1821812
                     : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
495
                                                               uvlinesize,
496
                                                               height);
497
        }
498
    }
499
911088
}
500
501
438655
static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
502
                              int dir, int avg)
503
{
504
    int i, j, k, mx, my, dx, dy, x, y;
505
438655
    const int part_width    = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
506
438655
    const int part_height   = 16 >> ((unsigned)(size + 1) / 3);
507
438655
    const int extra_width   = (mode == PREDICT_MODE) ? -16 * 6 : 0;
508
438655
    const int h_edge_pos    = 6 * (s->h_edge_pos - part_width)  - extra_width;
509
438655
    const int v_edge_pos    = 6 * (s->v_edge_pos - part_height) - extra_width;
510
511
916793
    for (i = 0; i < 16; i += part_height)
512
1035376
        for (j = 0; j < 16; j += part_width) {
513
557238
            const int b_xy = (4 * s->mb_x + (j >> 2)) +
514
557238
                             (4 * s->mb_y + (i >> 2)) * s->b_stride;
515
            int dxy;
516
557238
            x = 16 * s->mb_x + j;
517
557238
            y = 16 * s->mb_y + i;
518
557238
            k = (j >> 2 & 1) + (i >> 1 & 2) +
519
557238
                (j >> 1 & 4) + (i      & 8);
520
521
557238
            if (mode != PREDICT_MODE) {
522
556200
                svq3_pred_motion(s, k, part_width >> 2, dir, 1, &mx, &my);
523
            } else {
524
1038
                mx = s->next_pic->motion_val[0][b_xy][0] * 2;
525
1038
                my = s->next_pic->motion_val[0][b_xy][1] * 2;
526
527
1038
                if (dir == 0) {
528
519
                    mx = mx * s->frame_num_offset /
529
519
                         s->prev_frame_num_offset + 1 >> 1;
530
519
                    my = my * s->frame_num_offset /
531
519
                         s->prev_frame_num_offset + 1 >> 1;
532
                } else {
533
519
                    mx = mx * (s->frame_num_offset - s->prev_frame_num_offset) /
534
519
                         s->prev_frame_num_offset + 1 >> 1;
535
519
                    my = my * (s->frame_num_offset - s->prev_frame_num_offset) /
536
519
                         s->prev_frame_num_offset + 1 >> 1;
537
                }
538
            }
539
540
            /* clip motion vector prediction to frame border */
541
557238
            mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
542
557238
            my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
543
544
            /* get (optional) motion vector differential */
545
557238
            if (mode == PREDICT_MODE) {
546
1038
                dx = dy = 0;
547
            } else {
548
556200
                dy = get_interleaved_se_golomb(&s->gb_slice);
549
556200
                dx = get_interleaved_se_golomb(&s->gb_slice);
550
551

556200
                if (dx != (int16_t)dx || dy != (int16_t)dy) {
552
                    av_log(s->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
553
                    return -1;
554
                }
555
            }
556
557
            /* compute motion vector */
558
557238
            if (mode == THIRDPEL_MODE) {
559
                int fx, fy;
560
182
                mx  = (mx + 1 >> 1) + dx;
561
182
                my  = (my + 1 >> 1) + dy;
562
182
                fx  = (unsigned)(mx + 0x30000) / 3 - 0x10000;
563
182
                fy  = (unsigned)(my + 0x30000) / 3 - 0x10000;
564
182
                dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
565
566
182
                svq3_mc_dir_part(s, x, y, part_width, part_height,
567
                                 fx, fy, dxy, 1, dir, avg);
568
182
                mx += mx;
569
182
                my += my;
570

557056
            } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
571
276056
                mx  = (unsigned)(mx + 1 + 0x30000) / 3 + dx - 0x10000;
572
276056
                my  = (unsigned)(my + 1 + 0x30000) / 3 + dy - 0x10000;
573
276056
                dxy = (mx & 1) + 2 * (my & 1);
574
575
276056
                svq3_mc_dir_part(s, x, y, part_width, part_height,
576
                                 mx >> 1, my >> 1, dxy, 0, dir, avg);
577
276056
                mx *= 3;
578
276056
                my *= 3;
579
            } else {
580
281000
                mx = (unsigned)(mx + 3 + 0x60000) / 6 + dx - 0x10000;
581
281000
                my = (unsigned)(my + 3 + 0x60000) / 6 + dy - 0x10000;
582
583
281000
                svq3_mc_dir_part(s, x, y, part_width, part_height,
584
                                 mx, my, 0, 0, dir, avg);
585
281000
                mx *= 6;
586
281000
                my *= 6;
587
            }
588
589
            /* update mv_cache */
590
557238
            if (mode != PREDICT_MODE) {
591
556200
                int32_t mv = pack16to32(mx, my);
592
593

556200
                if (part_height == 8 && i < 8) {
594
78775
                    AV_WN32A(s->mv_cache[dir][scan8[k] + 1 * 8], mv);
595
596

78775
                    if (part_width == 8 && j < 8)
597
39348
                        AV_WN32A(s->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
598
                }
599

556200
                if (part_width == 8 && j < 8)
600
78808
                    AV_WN32A(s->mv_cache[dir][scan8[k] + 1], mv);
601

556200
                if (part_width == 4 || part_height == 4)
602
184
                    AV_WN32A(s->mv_cache[dir][scan8[k]], mv);
603
            }
604
605
            /* write back motion vectors */
606
557238
            fill_rectangle(s->cur_pic->motion_val[dir][b_xy],
607
                           part_width >> 2, part_height >> 2, s->b_stride,
608
                           pack16to32(mx, my), 4);
609
        }
610
611
438655
    return 0;
612
}
613
614
488398
static av_always_inline void hl_decode_mb_idct_luma(SVQ3Context *s,
615
                                                    int mb_type, const int *block_offset,
616
                                                    int linesize, uint8_t *dest_y)
617
{
618
    int i;
619
488398
    if (!IS_INTRA4x4(mb_type)) {
620
7486545
        for (i = 0; i < 16; i++)
621

7046160
            if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
622
2479726
                uint8_t *const ptr = dest_y + block_offset[i];
623
2479726
                svq3_add_idct_c(ptr, s->mb + i * 16, linesize,
624
2479726
                                s->qscale, IS_INTRA(mb_type) ? 1 : 0);
625
            }
626
    }
627
488398
}
628
629
50550
static av_always_inline void hl_decode_mb_predict_luma(SVQ3Context *s,
630
                                                       int mb_type,
631
                                                       const int *block_offset,
632
                                                       int linesize,
633
                                                       uint8_t *dest_y)
634
{
635
    int i;
636
50550
    int qscale = s->qscale;
637
638
50550
    if (IS_INTRA4x4(mb_type)) {
639
816221
        for (i = 0; i < 16; i++) {
640
768208
            uint8_t *const ptr = dest_y + block_offset[i];
641
768208
            const int dir      = s->intra4x4_pred_mode_cache[scan8[i]];
642
643
            uint8_t *topright;
644
            int nnz, tr;
645

821157
            if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
646
52949
                const int topright_avail = (s->topright_samples_available << i) & 0x8000;
647
                av_assert2(s->mb_y || linesize <= block_offset[i]);
648
52949
                if (!topright_avail) {
649
                    tr       = ptr[3 - linesize] * 0x01010101u;
650
                    topright = (uint8_t *)&tr;
651
                } else
652
52949
                    topright = ptr + 4 - linesize;
653
            } else
654
715259
                topright = NULL;
655
656
768208
            s->hpc.pred4x4[dir](ptr, topright, linesize);
657
768208
            nnz = s->non_zero_count_cache[scan8[i]];
658
768208
            if (nnz) {
659
560064
                svq3_add_idct_c(ptr, s->mb + i * 16, linesize, qscale, 0);
660
            }
661
        }
662
    } else {
663
2537
        s->hpc.pred16x16[s->intra16x16_pred_mode](dest_y, linesize);
664
2537
        svq3_luma_dc_dequant_idct_c(s->mb, s->mb_luma_dc[0], qscale);
665
    }
666
50550
}
667
668
488398
static void hl_decode_mb(SVQ3Context *s)
669
{
670
488398
    const int mb_x    = s->mb_x;
671
488398
    const int mb_y    = s->mb_y;
672
488398
    const int mb_xy   = s->mb_xy;
673
488398
    const int mb_type = s->cur_pic->mb_type[mb_xy];
674
    uint8_t *dest_y, *dest_cb, *dest_cr;
675
    int linesize, uvlinesize;
676
    int i, j;
677
488398
    const int *block_offset = &s->block_offset[0];
678
488398
    const int block_h   = 16 >> 1;
679
680
488398
    linesize   = s->cur_pic->f->linesize[0];
681
488398
    uvlinesize = s->cur_pic->f->linesize[1];
682
683
488398
    dest_y  = s->cur_pic->f->data[0] + (mb_x     + mb_y * linesize)  * 16;
684
488398
    dest_cb = s->cur_pic->f->data[1] +  mb_x * 8 + mb_y * uvlinesize * block_h;
685
488398
    dest_cr = s->cur_pic->f->data[2] +  mb_x * 8 + mb_y * uvlinesize * block_h;
686
687
488398
    s->vdsp.prefetch(dest_y  + (s->mb_x & 3) * 4 * linesize   + 64, linesize,      4);
688
488398
    s->vdsp.prefetch(dest_cb + (s->mb_x & 7)     * uvlinesize + 64, dest_cr - dest_cb, 2);
689
690
488398
    if (IS_INTRA(mb_type)) {
691
50550
        s->hpc.pred8x8[s->chroma_pred_mode](dest_cb, uvlinesize);
692
50550
        s->hpc.pred8x8[s->chroma_pred_mode](dest_cr, uvlinesize);
693
694
50550
        hl_decode_mb_predict_luma(s, mb_type, block_offset, linesize, dest_y);
695
    }
696
697
488398
    hl_decode_mb_idct_luma(s, mb_type, block_offset, linesize, dest_y);
698
699
488398
    if (s->cbp & 0x30) {
700
159176
        uint8_t *dest[2] = { dest_cb, dest_cr };
701
159176
        s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 1,
702
159176
                                               s->dequant4_coeff[4][0]);
703
159176
        s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 2,
704
159176
                                               s->dequant4_coeff[4][0]);
705
477528
        for (j = 1; j < 3; j++) {
706
1591760
            for (i = j * 16; i < j * 16 + 4; i++)
707

1273408
                if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
708
972473
                    uint8_t *const ptr = dest[j - 1] + block_offset[i];
709
972473
                    svq3_add_idct_c(ptr, s->mb + i * 16,
710
972473
                                    uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
711
                }
712
        }
713
    }
714
488398
}
715
716
842100
static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
717
{
718
    int i, j, k, m, dir, mode;
719
842100
    int cbp = 0;
720
    uint32_t vlc;
721
    int8_t *top, *left;
722
842100
    const int mb_xy = s->mb_xy;
723
842100
    const int b_xy  = 4 * s->mb_x + 4 * s->mb_y * s->b_stride;
724
725
842100
    s->top_samples_available      = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
726
842100
    s->left_samples_available     = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
727
842100
    s->topright_samples_available = 0xFFFF;
728
729
842100
    if (mb_type == 0) {           /* SKIP */
730
353762
        if (s->pict_type == AV_PICTURE_TYPE_P ||
731
874
            s->next_pic->mb_type[mb_xy] == -1) {
732
353369
            svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
733
                             0, 0, 0, 0, 0, 0);
734
735
353369
            if (s->pict_type == AV_PICTURE_TYPE_B)
736
481
                svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
737
                                 0, 0, 0, 0, 1, 1);
738
739
353369
            mb_type = MB_TYPE_SKIP;
740
        } else {
741
393
            mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
742
393
            if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
743
                return -1;
744
393
            if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
745
                return -1;
746
747
393
            mb_type = MB_TYPE_16x16;
748
        }
749
488338
    } else if (mb_type < 8) {     /* INTER */
750

437788
        if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&s->gb_slice))
751
129
            mode = THIRDPEL_MODE;
752
437659
        else if (s->halfpel_flag &&
753
437659
                 s->thirdpel_flag == !get_bits1(&s->gb_slice))
754
196960
            mode = HALFPEL_MODE;
755
        else
756
240699
            mode = FULLPEL_MODE;
757
758
        /* fill caches */
759
        /* note ref_cache should contain here:
760
         *  ????????
761
         *  ???11111
762
         *  N??11111
763
         *  N??11111
764
         *  N??11111
765
         */
766
767
438156
        for (m = 0; m < 2; m++) {
768

437972
            if (s->mb_x > 0 && s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6] != -1) {
769
2106155
                for (i = 0; i < 4; i++)
770
1684924
                    AV_COPY32(s->mv_cache[m][scan8[0] - 1 + i * 8],
771
                              s->cur_pic->motion_val[m][b_xy - 1 + i * s->b_stride]);
772
            } else {
773
83705
                for (i = 0; i < 4; i++)
774
66964
                    AV_ZERO32(s->mv_cache[m][scan8[0] - 1 + i * 8]);
775
            }
776
437972
            if (s->mb_y > 0) {
777
415083
                memcpy(s->mv_cache[m][scan8[0] - 1 * 8],
778
415083
                       s->cur_pic->motion_val[m][b_xy - s->b_stride],
779
                       4 * 2 * sizeof(int16_t));
780
415083
                memset(&s->ref_cache[m][scan8[0] - 1 * 8],
781
415083
                       (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
782
783
415083
                if (s->mb_x < s->mb_width - 1) {
784
396855
                    AV_COPY32(s->mv_cache[m][scan8[0] + 4 - 1 * 8],
785
                              s->cur_pic->motion_val[m][b_xy - s->b_stride + 4]);
786
396855
                    s->ref_cache[m][scan8[0] + 4 - 1 * 8] =
787
396855
                        (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 ||
788
396855
                         s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
789
                } else
790
18228
                    s->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
791
415083
                if (s->mb_x > 0) {
792
399258
                    AV_COPY32(s->mv_cache[m][scan8[0] - 1 - 1 * 8],
793
                              s->cur_pic->motion_val[m][b_xy - s->b_stride - 1]);
794
399258
                    s->ref_cache[m][scan8[0] - 1 - 1 * 8] =
795
399258
                        (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
796
                } else
797
15825
                    s->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
798
            } else
799
22889
                memset(&s->ref_cache[m][scan8[0] - 1 * 8 - 1],
800
                       PART_NOT_AVAILABLE, 8);
801
802
437972
            if (s->pict_type != AV_PICTURE_TYPE_B)
803
437604
                break;
804
        }
805
806
        /* decode motion vector(s) and form prediction(s) */
807
437788
        if (s->pict_type == AV_PICTURE_TYPE_P) {
808
437604
            if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
809
                return -1;
810
        } else {        /* AV_PICTURE_TYPE_B */
811
184
            if (mb_type != 2) {
812
134
                if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
813
                    return -1;
814
            } else {
815
250
                for (i = 0; i < 4; i++)
816
200
                    memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
817
                           0, 4 * 2 * sizeof(int16_t));
818
            }
819
184
            if (mb_type != 1) {
820
131
                if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
821
                    return -1;
822
            } else {
823
265
                for (i = 0; i < 4; i++)
824
212
                    memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
825
                           0, 4 * 2 * sizeof(int16_t));
826
            }
827
        }
828
829
437788
        mb_type = MB_TYPE_16x16;
830

50550
    } else if (mb_type == 8 || mb_type == 33) {   /* INTRA4x4 */
831
48013
        int8_t *i4x4       = s->intra4x4_pred_mode + s->mb2br_xy[s->mb_xy];
832
48013
        int8_t *i4x4_cache = s->intra4x4_pred_mode_cache;
833
834
48013
        memset(s->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
835
836
48013
        if (mb_type == 8) {
837
48013
            if (s->mb_x > 0) {
838
220070
                for (i = 0; i < 4; i++)
839
176056
                    s->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6 - i];
840
44014
                if (s->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
841
                    s->left_samples_available = 0x5F5F;
842
            }
843
48013
            if (s->mb_y > 0) {
844
45104
                s->intra4x4_pred_mode_cache[4 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 0];
845
45104
                s->intra4x4_pred_mode_cache[5 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 1];
846
45104
                s->intra4x4_pred_mode_cache[6 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 2];
847
45104
                s->intra4x4_pred_mode_cache[7 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 3];
848
849
45104
                if (s->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
850
                    s->top_samples_available = 0x33FF;
851
            }
852
853
            /* decode prediction codes for luma blocks */
854
432117
            for (i = 0; i < 16; i += 2) {
855
384104
                vlc = get_interleaved_ue_golomb(&s->gb_slice);
856
857
384104
                if (vlc >= 25U) {
858
                    av_log(s->avctx, AV_LOG_ERROR,
859
                           "luma prediction:%"PRIu32"\n", vlc);
860
                    return -1;
861
                }
862
863
384104
                left = &s->intra4x4_pred_mode_cache[scan8[i] - 1];
864
384104
                top  = &s->intra4x4_pred_mode_cache[scan8[i] - 8];
865
866
384104
                left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
867
384104
                left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
868
869

384104
                if (left[1] == -1 || left[2] == -1) {
870
                    av_log(s->avctx, AV_LOG_ERROR, "weird prediction\n");
871
                    return -1;
872
                }
873
            }
874
        } else {    /* mb_type == 33, DC_128_PRED block type */
875
            for (i = 0; i < 4; i++)
876
                memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
877
        }
878
879
48013
        AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
880
48013
        i4x4[4] = i4x4_cache[7 + 8 * 3];
881
48013
        i4x4[5] = i4x4_cache[7 + 8 * 2];
882
48013
        i4x4[6] = i4x4_cache[7 + 8 * 1];
883
884
48013
        if (mb_type == 8) {
885
48013
            ff_h264_check_intra4x4_pred_mode(s->intra4x4_pred_mode_cache,
886
48013
                                             s->avctx, s->top_samples_available,
887
48013
                                             s->left_samples_available);
888
889
48013
            s->top_samples_available  = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
890
48013
            s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
891
        } else {
892
            for (i = 0; i < 4; i++)
893
                memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
894
895
            s->top_samples_available  = 0x33FF;
896
            s->left_samples_available = 0x5F5F;
897
        }
898
899
48013
        mb_type = MB_TYPE_INTRA4x4;
900
    } else {                      /* INTRA16x16 */
901
2537
        dir = ff_h264_i_mb_type_info[mb_type - 8].pred_mode;
902
2537
        dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
903
904
2537
        if ((s->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
905
2537
                                                                     s->left_samples_available, dir, 0)) < 0) {
906
            av_log(s->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
907
            return s->intra16x16_pred_mode;
908
        }
909
910
2537
        cbp     = ff_h264_i_mb_type_info[mb_type - 8].cbp;
911
2537
        mb_type = MB_TYPE_INTRA16x16;
912
    }
913
914

842100
    if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
915
1923745
        for (i = 0; i < 4; i++)
916
1538996
            memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
917
                   0, 4 * 2 * sizeof(int16_t));
918
384749
        if (s->pict_type == AV_PICTURE_TYPE_B) {
919
2515
            for (i = 0; i < 4; i++)
920
2012
                memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
921
                       0, 4 * 2 * sizeof(int16_t));
922
        }
923
    }
924
842100
    if (!IS_INTRA4x4(mb_type)) {
925
794087
        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy], DC_PRED, 8);
926
    }
927

842100
    if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
928
489212
        memset(s->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
929
    }
930
931
842100
    if (!IS_INTRA16x16(mb_type) &&
932

839563
        (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
933
486675
        if ((vlc = get_interleaved_ue_golomb(&s->gb_slice)) >= 48U){
934
            av_log(s->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc);
935
            return -1;
936
        }
937
938
486675
        cbp = IS_INTRA(mb_type) ? ff_h264_golomb_to_intra4x4_cbp[vlc]
939
486675
                                : ff_h264_golomb_to_inter_cbp[vlc];
940
    }
941
842100
    if (IS_INTRA16x16(mb_type) ||
942

839563
        (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
943
3233
        s->qscale += get_interleaved_se_golomb(&s->gb_slice);
944
945
3233
        if (s->qscale > 31u) {
946
            av_log(s->avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
947
            return -1;
948
        }
949
    }
950
842100
    if (IS_INTRA16x16(mb_type)) {
951
2537
        AV_ZERO128(s->mb_luma_dc[0] + 0);
952
2537
        AV_ZERO128(s->mb_luma_dc[0] + 8);
953
2537
        if (svq3_decode_block(&s->gb_slice, s->mb_luma_dc[0], 0, 1)) {
954
            av_log(s->avctx, AV_LOG_ERROR,
955
                   "error while decoding intra luma dc\n");
956
            return -1;
957
        }
958
    }
959
960
842100
    if (cbp) {
961
333418
        const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
962

333418
        const int type  = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
963
964
1667090
        for (i = 0; i < 4; i++)
965
1333672
            if ((cbp & (1 << i))) {
966
3771960
                for (j = 0; j < 4; j++) {
967
3021440
                    k = index ? (1 * (j & 1) + 2 * (i & 1) +
968
3872
                                 2 * (j & 2) + 4 * (i & 2))
969
3017568
                              : (4 * i + j);
970
3017568
                    s->non_zero_count_cache[scan8[k]] = 1;
971
972
3017568
                    if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], index, type)) {
973
                        av_log(s->avctx, AV_LOG_ERROR,
974
                               "error while decoding block\n");
975
                        return -1;
976
                    }
977
                }
978
            }
979
980
333418
        if ((cbp & 0x30)) {
981
477528
            for (i = 1; i < 3; ++i)
982
318352
                if (svq3_decode_block(&s->gb_slice, &s->mb[16 * 16 * i], 0, 3)) {
983
                    av_log(s->avctx, AV_LOG_ERROR,
984
                           "error while decoding chroma dc block\n");
985
                    return -1;
986
                }
987
988
159176
            if ((cbp & 0x20)) {
989
203310
                for (i = 1; i < 3; i++) {
990
677700
                    for (j = 0; j < 4; j++) {
991
542160
                        k                                 = 16 * i + j;
992
542160
                        s->non_zero_count_cache[scan8[k]] = 1;
993
994
542160
                        if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], 1, 1)) {
995
                            av_log(s->avctx, AV_LOG_ERROR,
996
                                   "error while decoding chroma ac block\n");
997
                            return -1;
998
                        }
999
                    }
1000
                }
1001
            }
1002
        }
1003
    }
1004
1005
842100
    s->cbp                     = cbp;
1006
842100
    s->cur_pic->mb_type[mb_xy] = mb_type;
1007
1008
842100
    if (IS_INTRA(mb_type))
1009
50550
        s->chroma_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
1010
50550
                                                            s->left_samples_available, DC_PRED8x8, 1);
1011
1012
842100
    return 0;
1013
}
1014
1015
2808
static int svq3_decode_slice_header(AVCodecContext *avctx)
1016
{
1017
2808
    SVQ3Context *s = avctx->priv_data;
1018
2808
    const int mb_xy   = s->mb_xy;
1019
    int i, header;
1020
    unsigned slice_id;
1021
1022
2808
    header = get_bits(&s->gb, 8);
1023
1024

2808
    if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
1025
        /* TODO: what? */
1026
        av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
1027
        return -1;
1028
    } else {
1029
        int slice_bits, slice_bytes, slice_length;
1030
2808
        int length = header >> 5 & 3;
1031
1032
2808
        slice_length = show_bits(&s->gb, 8 * length);
1033
2808
        slice_bits   = slice_length * 8;
1034
2808
        slice_bytes  = slice_length + length - 1;
1035
1036
2808
        skip_bits(&s->gb, 8);
1037
1038
2808
        av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1039
2808
        if (!s->slice_buf)
1040
            return AVERROR(ENOMEM);
1041
1042
2808
        if (slice_bytes * 8LL > get_bits_left(&s->gb)) {
1043
            av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
1044
            return AVERROR_INVALIDDATA;
1045
        }
1046
2808
        memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
1047
1048
2808
        if (s->watermark_key) {
1049
10
            uint32_t header = AV_RL32(&s->slice_buf[1]);
1050
10
            AV_WL32(&s->slice_buf[1], header ^ s->watermark_key);
1051
        }
1052
2808
        init_get_bits(&s->gb_slice, s->slice_buf, slice_bits);
1053
1054
2808
        if (length > 0) {
1055
2808
            memmove(s->slice_buf, &s->slice_buf[slice_length], length - 1);
1056
        }
1057
2808
        skip_bits_long(&s->gb, slice_bytes * 8);
1058
    }
1059
1060
2808
    if ((slice_id = get_interleaved_ue_golomb(&s->gb_slice)) >= 3) {
1061
        av_log(s->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
1062
        return -1;
1063
    }
1064
1065
2808
    s->slice_type = ff_h264_golomb_to_pict_type[slice_id];
1066
1067
2808
    if ((header & 0x9F) == 2) {
1068
        i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));
1069
        get_bits(&s->gb_slice, i);
1070
2808
    } else if (get_bits1(&s->gb_slice)) {
1071
        avpriv_report_missing_feature(s->avctx, "Media key encryption");
1072
        return AVERROR_PATCHWELCOME;
1073
    }
1074
1075
2808
    s->slice_num      = get_bits(&s->gb_slice, 8);
1076
2808
    s->qscale         = get_bits(&s->gb_slice, 5);
1077
2808
    s->adaptive_quant = get_bits1(&s->gb_slice);
1078
1079
    /* unknown fields */
1080
2808
    skip_bits1(&s->gb_slice);
1081
1082
2808
    if (s->has_watermark)
1083
10
        skip_bits1(&s->gb_slice);
1084
1085
2808
    skip_bits1(&s->gb_slice);
1086
2808
    skip_bits(&s->gb_slice, 2);
1087
1088
2808
    if (skip_1stop_8data_bits(&s->gb_slice) < 0)
1089
        return AVERROR_INVALIDDATA;
1090
1091
    /* reset intra predictors and invalidate motion vector references */
1092
2808
    if (s->mb_x > 0) {
1093
        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,
1094
               -1, 4 * sizeof(int8_t));
1095
        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],
1096
               -1, 8 * sizeof(int8_t) * s->mb_x);
1097
    }
1098
2808
    if (s->mb_y > 0) {
1099
        memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],
1100
               -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));
1101
1102
        if (s->mb_x > 0)
1103
            s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;
1104
    }
1105
1106
2808
    return 0;
1107
}
1108
1109
8
static void init_dequant4_coeff_table(SVQ3Context *s)
1110
{
1111
    int q, x;
1112
8
    const int max_qp = 51;
1113
1114
424
    for (q = 0; q < max_qp + 1; q++) {
1115
416
        int shift = ff_h264_quant_div6[q] + 2;
1116
416
        int idx   = ff_h264_quant_rem6[q];
1117
7072
        for (x = 0; x < 16; x++)
1118
6656
            s->dequant4_coeff[q][(x >> 2) | ((x << 2) & 0xF)] =
1119
6656
                ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * 16) << shift;
1120
    }
1121
8
}
1122
1123
8
static av_cold int svq3_decode_init(AVCodecContext *avctx)
1124
{
1125
8
    SVQ3Context *s = avctx->priv_data;
1126
    int m, x, y;
1127
    unsigned char *extradata;
1128
    unsigned char *extradata_end;
1129
    unsigned int size;
1130
8
    int marker_found = 0;
1131
    int ret;
1132
1133
8
    s->cur_pic  = &s->frames[0];
1134
8
    s->last_pic = &s->frames[1];
1135
8
    s->next_pic = &s->frames[2];
1136
1137
8
    s->cur_pic->f  = av_frame_alloc();
1138
8
    s->last_pic->f = av_frame_alloc();
1139
8
    s->next_pic->f = av_frame_alloc();
1140

8
    if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f)
1141
        return AVERROR(ENOMEM);
1142
1143
8
    ff_h264dsp_init(&s->h264dsp, 8, 1);
1144
8
    ff_h264_pred_init(&s->hpc, AV_CODEC_ID_SVQ3, 8, 1);
1145
8
    ff_videodsp_init(&s->vdsp, 8);
1146
1147
1148
8
    avctx->bits_per_raw_sample = 8;
1149
1150
8
    ff_hpeldsp_init(&s->hdsp, avctx->flags);
1151
8
    ff_tpeldsp_init(&s->tdsp);
1152
1153
8
    avctx->pix_fmt     = AV_PIX_FMT_YUVJ420P;
1154
8
    avctx->color_range = AVCOL_RANGE_JPEG;
1155
1156
8
    s->avctx         = avctx;
1157
8
    s->halfpel_flag  = 1;
1158
8
    s->thirdpel_flag = 1;
1159
8
    s->has_watermark = 0;
1160
1161
    /* prowl for the "SEQH" marker in the extradata */
1162
8
    extradata     = (unsigned char *)avctx->extradata;
1163
8
    extradata_end = avctx->extradata + avctx->extradata_size;
1164
8
    if (extradata) {
1165
72
        for (m = 0; m + 8 < avctx->extradata_size; m++) {
1166
72
            if (!memcmp(extradata, "SEQH", 4)) {
1167
8
                marker_found = 1;
1168
8
                break;
1169
            }
1170
64
            extradata++;
1171
        }
1172
    }
1173
1174
    /* if a match was found, parse the extra data */
1175
8
    if (marker_found) {
1176
        GetBitContext gb;
1177
        int frame_size_code;
1178
        int unk0, unk1, unk2, unk3, unk4;
1179
        int w,h;
1180
1181
8
        size = AV_RB32(&extradata[4]);
1182
8
        if (size > extradata_end - extradata - 8)
1183
            return AVERROR_INVALIDDATA;
1184
8
        init_get_bits(&gb, extradata + 8, size * 8);
1185
1186
        /* 'frame size code' and optional 'width, height' */
1187
8
        frame_size_code = get_bits(&gb, 3);
1188


8
        switch (frame_size_code) {
1189
        case 0:
1190
            w = 160;
1191
            h = 120;
1192
            break;
1193
        case 1:
1194
            w = 128;
1195
            h =  96;
1196
            break;
1197
        case 2:
1198
            w = 176;
1199
            h = 144;
1200
            break;
1201
        case 3:
1202
            w = 352;
1203
            h = 288;
1204
            break;
1205
        case 4:
1206
            w = 704;
1207
            h = 576;
1208
            break;
1209
        case 5:
1210
            w = 240;
1211
            h = 180;
1212
            break;
1213
6
        case 6:
1214
6
            w = 320;
1215
6
            h = 240;
1216
6
            break;
1217
2
        case 7:
1218
2
            w = get_bits(&gb, 12);
1219
2
            h = get_bits(&gb, 12);
1220
2
            break;
1221
        }
1222
8
        ret = ff_set_dimensions(avctx, w, h);
1223
8
        if (ret < 0)
1224
            return ret;
1225
1226
8
        s->halfpel_flag  = get_bits1(&gb);
1227
8
        s->thirdpel_flag = get_bits1(&gb);
1228
1229
        /* unknown fields */
1230
8
        unk0 = get_bits1(&gb);
1231
8
        unk1 = get_bits1(&gb);
1232
8
        unk2 = get_bits1(&gb);
1233
8
        unk3 = get_bits1(&gb);
1234
1235
8
        s->low_delay = get_bits1(&gb);
1236
1237
        /* unknown field */
1238
8
        unk4 = get_bits1(&gb);
1239
1240
8
        av_log(avctx, AV_LOG_DEBUG, "Unknown fields %d %d %d %d %d\n",
1241
               unk0, unk1, unk2, unk3, unk4);
1242
1243
8
        if (skip_1stop_8data_bits(&gb) < 0)
1244
            return AVERROR_INVALIDDATA;
1245
1246
8
        s->has_watermark  = get_bits1(&gb);
1247
8
        avctx->has_b_frames = !s->low_delay;
1248
8
        if (s->has_watermark) {
1249
#if CONFIG_ZLIB
1250
2
            unsigned watermark_width  = get_interleaved_ue_golomb(&gb);
1251
2
            unsigned watermark_height = get_interleaved_ue_golomb(&gb);
1252
2
            int u1                    = get_interleaved_ue_golomb(&gb);
1253
2
            int u2                    = get_bits(&gb, 8);
1254
2
            int u3                    = get_bits(&gb, 2);
1255
2
            int u4                    = get_interleaved_ue_golomb(&gb);
1256
2
            unsigned long buf_len     = watermark_width *
1257
2
                                        watermark_height * 4;
1258
2
            int offset                = get_bits_count(&gb) + 7 >> 3;
1259
            uint8_t *buf;
1260
1261
2
            if (watermark_height <= 0 ||
1262
2
                (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
1263
                return AVERROR_INVALIDDATA;
1264
1265
2
            buf = av_malloc(buf_len);
1266
2
            if (!buf)
1267
                return AVERROR(ENOMEM);
1268
1269
2
            av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n",
1270
                   watermark_width, watermark_height);
1271
2
            av_log(avctx, AV_LOG_DEBUG,
1272
                   "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
1273
                   u1, u2, u3, u4, offset);
1274
2
            if (uncompress(buf, &buf_len, extradata + 8 + offset,
1275
2
                           size - offset) != Z_OK) {
1276
                av_log(avctx, AV_LOG_ERROR,
1277
                       "could not uncompress watermark logo\n");
1278
                av_free(buf);
1279
                return -1;
1280
            }
1281
2
            s->watermark_key = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_CCITT), 0, buf, buf_len));
1282
1283
2
            s->watermark_key = s->watermark_key << 16 | s->watermark_key;
1284
2
            av_log(avctx, AV_LOG_DEBUG,
1285
                   "watermark key %#"PRIx32"\n", s->watermark_key);
1286
2
            av_free(buf);
1287
#else
1288
            av_log(avctx, AV_LOG_ERROR,
1289
                   "this svq3 file contains watermark which need zlib support compiled in\n");
1290
            return AVERROR(ENOSYS);
1291
#endif
1292
        }
1293
    }
1294
1295
8
    s->mb_width   = (avctx->width + 15) / 16;
1296
8
    s->mb_height  = (avctx->height + 15) / 16;
1297
8
    s->mb_stride  = s->mb_width + 1;
1298
8
    s->mb_num     = s->mb_width * s->mb_height;
1299
8
    s->b_stride   = 4 * s->mb_width;
1300
8
    s->h_edge_pos = s->mb_width * 16;
1301
8
    s->v_edge_pos = s->mb_height * 16;
1302
1303
8
    s->intra4x4_pred_mode = av_mallocz(s->mb_stride * 2 * 8);
1304
8
    if (!s->intra4x4_pred_mode)
1305
        return AVERROR(ENOMEM);
1306
1307
8
    s->mb2br_xy = av_mallocz(s->mb_stride * (s->mb_height + 1) *
1308
                             sizeof(*s->mb2br_xy));
1309
8
    if (!s->mb2br_xy)
1310
        return AVERROR(ENOMEM);
1311
1312
128
    for (y = 0; y < s->mb_height; y++)
1313
2460
        for (x = 0; x < s->mb_width; x++) {
1314
2340
            const int mb_xy = x + y * s->mb_stride;
1315
1316
2340
            s->mb2br_xy[mb_xy] = 8 * (mb_xy % (2 * s->mb_stride));
1317
        }
1318
1319
8
    init_dequant4_coeff_table(s);
1320
1321
8
    return 0;
1322
}
1323
1324
24
static void free_picture(AVCodecContext *avctx, SVQ3Frame *pic)
1325
{
1326
    int i;
1327
72
    for (i = 0; i < 2; i++) {
1328
48
        av_freep(&pic->motion_val_buf[i]);
1329
    }
1330
24
    av_freep(&pic->mb_type_buf);
1331
1332
24
    av_frame_unref(pic->f);
1333
24
}
1334
1335
2808
static int get_buffer(AVCodecContext *avctx, SVQ3Frame *pic)
1336
{
1337
2808
    SVQ3Context *s = avctx->priv_data;
1338
2808
    const int big_mb_num    = s->mb_stride * (s->mb_height + 1) + 1;
1339
2808
    const int b4_stride     = s->mb_width * 4 + 1;
1340
2808
    const int b4_array_size = b4_stride * s->mb_height * 4;
1341
    int ret;
1342
1343
2808
    if (!pic->motion_val_buf[0]) {
1344
        int i;
1345
1346
12
        pic->mb_type_buf = av_calloc(big_mb_num + s->mb_stride, sizeof(uint32_t));
1347
12
        if (!pic->mb_type_buf)
1348
            return AVERROR(ENOMEM);
1349
12
        pic->mb_type = pic->mb_type_buf + 2 * s->mb_stride + 1;
1350
1351
36
        for (i = 0; i < 2; i++) {
1352
24
            pic->motion_val_buf[i] = av_calloc(b4_array_size + 4, 2 * sizeof(int16_t));
1353
24
            if (!pic->motion_val_buf[i]) {
1354
                ret = AVERROR(ENOMEM);
1355
                goto fail;
1356
            }
1357
1358
24
            pic->motion_val[i] = pic->motion_val_buf[i] + 4;
1359
        }
1360
    }
1361
1362
2808
    ret = ff_get_buffer(avctx, pic->f,
1363
2808
                        (s->pict_type != AV_PICTURE_TYPE_B) ?
1364
                         AV_GET_BUFFER_FLAG_REF : 0);
1365
2808
    if (ret < 0)
1366
        goto fail;
1367
1368
2808
    if (!s->edge_emu_buffer) {
1369
4
        s->edge_emu_buffer = av_mallocz_array(pic->f->linesize[0], 17);
1370
4
        if (!s->edge_emu_buffer)
1371
            return AVERROR(ENOMEM);
1372
    }
1373
1374
2808
    return 0;
1375
fail:
1376
    free_picture(avctx, pic);
1377
    return ret;
1378
}
1379
1380
2815
static int svq3_decode_frame(AVCodecContext *avctx, void *data,
1381
                             int *got_frame, AVPacket *avpkt)
1382
{
1383
2815
    SVQ3Context *s     = avctx->priv_data;
1384
2815
    int buf_size       = avpkt->size;
1385
    int left;
1386
    uint8_t *buf;
1387
    int ret, m, i;
1388
1389
    /* special case for last picture */
1390
2815
    if (buf_size == 0) {
1391

7
        if (s->next_pic->f->data[0] && !s->low_delay && !s->last_frame_output) {
1392
4
            ret = av_frame_ref(data, s->next_pic->f);
1393
4
            if (ret < 0)
1394
                return ret;
1395
4
            s->last_frame_output = 1;
1396
4
            *got_frame          = 1;
1397
        }
1398
7
        return 0;
1399
    }
1400
1401
2808
    s->mb_x = s->mb_y = s->mb_xy = 0;
1402
1403
2808
    if (s->watermark_key) {
1404
10
        av_fast_padded_malloc(&s->buf, &s->buf_size, buf_size);
1405
10
        if (!s->buf)
1406
            return AVERROR(ENOMEM);
1407
10
        memcpy(s->buf, avpkt->data, buf_size);
1408
10
        buf = s->buf;
1409
    } else {
1410
2798
        buf = avpkt->data;
1411
    }
1412
1413
2808
    ret = init_get_bits(&s->gb, buf, 8 * buf_size);
1414
2808
    if (ret < 0)
1415
        return ret;
1416
1417
2808
    if (svq3_decode_slice_header(avctx))
1418
        return -1;
1419
1420
2808
    s->pict_type = s->slice_type;
1421
1422
2808
    if (s->pict_type != AV_PICTURE_TYPE_B)
1423
2804
        FFSWAP(SVQ3Frame*, s->next_pic, s->last_pic);
1424
1425
2808
    av_frame_unref(s->cur_pic->f);
1426
1427
    /* for skipping the frame */
1428
2808
    s->cur_pic->f->pict_type = s->pict_type;
1429
2808
    s->cur_pic->f->key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
1430
1431
2808
    ret = get_buffer(avctx, s->cur_pic);
1432
2808
    if (ret < 0)
1433
        return ret;
1434
1435
47736
    for (i = 0; i < 16; i++) {
1436
44928
        s->block_offset[i]           = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
1437
44928
        s->block_offset[48 + i]      = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
1438
    }
1439
47736
    for (i = 0; i < 16; i++) {
1440
44928
        s->block_offset[16 + i]      =
1441
44928
        s->block_offset[32 + i]      = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
1442
44928
        s->block_offset[48 + 16 + i] =
1443
44928
        s->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
1444
    }
1445
1446
2808
    if (s->pict_type != AV_PICTURE_TYPE_I) {
1447
2744
        if (!s->last_pic->f->data[0]) {
1448
            av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1449
            av_frame_unref(s->last_pic->f);
1450
            ret = get_buffer(avctx, s->last_pic);
1451
            if (ret < 0)
1452
                return ret;
1453
            memset(s->last_pic->f->data[0], 0, avctx->height * s->last_pic->f->linesize[0]);
1454
            memset(s->last_pic->f->data[1], 0x80, (avctx->height / 2) *
1455
                   s->last_pic->f->linesize[1]);
1456
            memset(s->last_pic->f->data[2], 0x80, (avctx->height / 2) *
1457
                   s->last_pic->f->linesize[2]);
1458
        }
1459
1460

2744
        if (s->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) {
1461
            av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1462
            av_frame_unref(s->next_pic->f);
1463
            ret = get_buffer(avctx, s->next_pic);
1464
            if (ret < 0)
1465
                return ret;
1466
            memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]);
1467
            memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) *
1468
                   s->next_pic->f->linesize[1]);
1469
            memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) *
1470
                   s->next_pic->f->linesize[2]);
1471
        }
1472
    }
1473
1474
2808
    if (avctx->debug & FF_DEBUG_PICT_INFO)
1475
        av_log(s->avctx, AV_LOG_DEBUG,
1476
               "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1477
               av_get_picture_type_char(s->pict_type),
1478
               s->halfpel_flag, s->thirdpel_flag,
1479
               s->adaptive_quant, s->qscale, s->slice_num);
1480
1481

2808
    if (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B ||
1482

2808
        avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I ||
1483
2808
        avctx->skip_frame >= AVDISCARD_ALL)
1484
        return 0;
1485
1486
2808
    if (s->next_p_frame_damaged) {
1487
        if (s->pict_type == AV_PICTURE_TYPE_B)
1488
            return 0;
1489
        else
1490
            s->next_p_frame_damaged = 0;
1491
    }
1492
1493
2808
    if (s->pict_type == AV_PICTURE_TYPE_B) {
1494
4
        s->frame_num_offset = s->slice_num - s->prev_frame_num;
1495
1496
4
        if (s->frame_num_offset < 0)
1497
            s->frame_num_offset += 256;
1498
4
        if (s->frame_num_offset == 0 ||
1499
4
            s->frame_num_offset >= s->prev_frame_num_offset) {
1500
            av_log(s->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1501
            return -1;
1502
        }
1503
    } else {
1504
2804
        s->prev_frame_num        = s->frame_num;
1505
2804
        s->frame_num             = s->slice_num;
1506
2804
        s->prev_frame_num_offset = s->frame_num - s->prev_frame_num;
1507
1508
2804
        if (s->prev_frame_num_offset < 0)
1509
10
            s->prev_frame_num_offset += 256;
1510
    }
1511
1512
8424
    for (m = 0; m < 2; m++) {
1513
        int i;
1514
28080
        for (i = 0; i < 4; i++) {
1515
            int j;
1516
134784
            for (j = -1; j < 4; j++)
1517
112320
                s->ref_cache[m][scan8[0] + 8 * i + j] = 1;
1518
22464
            if (i < 3)
1519
16848
                s->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
1520
        }
1521
    }
1522
1523
44928
    for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
1524
884220
        for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
1525
            unsigned mb_type;
1526
842100
            s->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
1527
1528
842100
            if ((get_bits_left(&s->gb_slice)) <= 7) {
1529

7694
                if (((get_bits_count(&s->gb_slice) & 7) == 0 ||
1530
3847
                    show_bits(&s->gb_slice, get_bits_left(&s->gb_slice) & 7) == 0)) {
1531
1532
                    if (svq3_decode_slice_header(avctx))
1533
                        return -1;
1534
                }
1535
3847
                if (s->slice_type != s->pict_type) {
1536
                    avpriv_request_sample(avctx, "non constant slice type");
1537
                }
1538
                /* TODO: support s->mb_skip_run */
1539
            }
1540
1541
842100
            mb_type = get_interleaved_ue_golomb(&s->gb_slice);
1542
1543
842100
            if (s->pict_type == AV_PICTURE_TYPE_I)
1544
19170
                mb_type += 8;
1545

822930
            else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
1546
22
                mb_type += 4;
1547

842100
            if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
1548
                av_log(s->avctx, AV_LOG_ERROR,
1549
                       "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
1550
                return -1;
1551
            }
1552
1553

842100
            if (mb_type != 0 || s->cbp)
1554
488398
                hl_decode_mb(s);
1555
1556

842100
            if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay)
1557
841020
                s->cur_pic->mb_type[s->mb_x + s->mb_y * s->mb_stride] =
1558

841020
                    (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
1559
        }
1560
1561
42120
        ff_draw_horiz_band(avctx, s->cur_pic->f,
1562
42060
                           s->last_pic->f->data[0] ? s->last_pic->f : NULL,
1563
42120
                           16 * s->mb_y, 16, PICT_FRAME, 0,
1564
                           s->low_delay);
1565
    }
1566
1567
2808
    left = buf_size*8 - get_bits_count(&s->gb_slice);
1568
1569

2808
    if (s->mb_y != s->mb_height || s->mb_x != s->mb_width) {
1570
        av_log(avctx, AV_LOG_INFO, "frame num %d incomplete pic x %d y %d left %d\n", avctx->frame_number, s->mb_y, s->mb_x, left);
1571
        //av_hex_dump(stderr, buf+buf_size-8, 8);
1572
    }
1573
1574
2808
    if (left < 0) {
1575
        av_log(avctx, AV_LOG_ERROR, "frame num %d left %d\n", avctx->frame_number, left);
1576
        return -1;
1577
    }
1578
1579

2808
    if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
1580
4
        ret = av_frame_ref(data, s->cur_pic->f);
1581
2804
    else if (s->last_pic->f->data[0])
1582
2800
        ret = av_frame_ref(data, s->last_pic->f);
1583
2808
    if (ret < 0)
1584
        return ret;
1585
1586
    /* Do not output the last pic after seeking. */
1587

2808
    if (s->last_pic->f->data[0] || s->low_delay)
1588
2804
        *got_frame = 1;
1589
1590
2808
    if (s->pict_type != AV_PICTURE_TYPE_B) {
1591
2804
        FFSWAP(SVQ3Frame*, s->cur_pic, s->next_pic);
1592
    } else {
1593
4
        av_frame_unref(s->cur_pic->f);
1594
    }
1595
1596
2808
    return buf_size;
1597
}
1598
1599
8
static av_cold int svq3_decode_end(AVCodecContext *avctx)
1600
{
1601
8
    SVQ3Context *s = avctx->priv_data;
1602
1603
8
    free_picture(avctx, s->cur_pic);
1604
8
    free_picture(avctx, s->next_pic);
1605
8
    free_picture(avctx, s->last_pic);
1606
8
    av_frame_free(&s->cur_pic->f);
1607
8
    av_frame_free(&s->next_pic->f);
1608
8
    av_frame_free(&s->last_pic->f);
1609
8
    av_freep(&s->slice_buf);
1610
8
    av_freep(&s->intra4x4_pred_mode);
1611
8
    av_freep(&s->edge_emu_buffer);
1612
8
    av_freep(&s->mb2br_xy);
1613
1614
1615
8
    av_freep(&s->buf);
1616
8
    s->buf_size = 0;
1617
1618
8
    return 0;
1619
}
1620
1621
AVCodec ff_svq3_decoder = {
1622
    .name           = "svq3",
1623
    .long_name      = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
1624
    .type           = AVMEDIA_TYPE_VIDEO,
1625
    .id             = AV_CODEC_ID_SVQ3,
1626
    .priv_data_size = sizeof(SVQ3Context),
1627
    .init           = svq3_decode_init,
1628
    .close          = svq3_decode_end,
1629
    .decode         = svq3_decode_frame,
1630
    .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND |
1631
                      AV_CODEC_CAP_DR1             |
1632
                      AV_CODEC_CAP_DELAY,
1633
    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
1634
                                                     AV_PIX_FMT_NONE},
1635
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
1636
};