GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/svq3.c Lines: 687 839 81.9 %
Date: 2021-04-14 23:45:22 Branches: 437 567 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
    unsigned slice_buf_size;
96
    int halfpel_flag;
97
    int thirdpel_flag;
98
    int has_watermark;
99
    uint32_t watermark_key;
100
    int adaptive_quant;
101
    int next_p_frame_damaged;
102
    int h_edge_pos;
103
    int v_edge_pos;
104
    int last_frame_output;
105
    int slice_num;
106
    int qscale;
107
    int cbp;
108
    int frame_num;
109
    int frame_num_offset;
110
    int prev_frame_num_offset;
111
    int prev_frame_num;
112
113
    enum AVPictureType pict_type;
114
    enum AVPictureType slice_type;
115
    int low_delay;
116
117
    int mb_x, mb_y;
118
    int mb_xy;
119
    int mb_width, mb_height;
120
    int mb_stride, mb_num;
121
    int b_stride;
122
123
    uint32_t *mb2br_xy;
124
125
    int chroma_pred_mode;
126
    int intra16x16_pred_mode;
127
128
    int8_t   intra4x4_pred_mode_cache[5 * 8];
129
    int8_t (*intra4x4_pred_mode);
130
131
    unsigned int top_samples_available;
132
    unsigned int topright_samples_available;
133
    unsigned int left_samples_available;
134
135
    uint8_t *edge_emu_buffer;
136
137
    DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
138
    DECLARE_ALIGNED(8,  int8_t, ref_cache)[2][5 * 8];
139
    DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
140
    DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
141
    DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
142
    uint32_t dequant4_coeff[QP_MAX_NUM + 1][16];
143
    int block_offset[2 * (16 * 3)];
144
    SVQ3Frame frames[3];
145
} SVQ3Context;
146
147
#define FULLPEL_MODE  1
148
#define HALFPEL_MODE  2
149
#define THIRDPEL_MODE 3
150
#define PREDICT_MODE  4
151
152
/* dual scan (from some older H.264 draft)
153
 * o-->o-->o   o
154
 *         |  /|
155
 * o   o   o / o
156
 * | / |   |/  |
157
 * o   o   o   o
158
 *   /
159
 * o-->o-->o-->o
160
 */
161
static const uint8_t svq3_scan[16] = {
162
    0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
163
    2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
164
    0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
165
    0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
166
};
167
168
static const uint8_t luma_dc_zigzag_scan[16] = {
169
    0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
170
    3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
171
    1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
172
    3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
173
};
174
175
static const uint8_t svq3_pred_0[25][2] = {
176
    { 0, 0 },
177
    { 1, 0 }, { 0, 1 },
178
    { 0, 2 }, { 1, 1 }, { 2, 0 },
179
    { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
180
    { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
181
    { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
182
    { 2, 4 }, { 3, 3 }, { 4, 2 },
183
    { 4, 3 }, { 3, 4 },
184
    { 4, 4 }
185
};
186
187
static const int8_t svq3_pred_1[6][6][5] = {
188
    { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
189
      { 2,  1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
190
    { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  4,  3 }, { 0, 1,  2,  4,  3 },
191
      { 0,  2,  1,  4,  3 }, { 2, 0,  1,  3,  4 }, { 0, 4,  2,  1,  3 } },
192
    { { 2,  0, -1, -1, -1 }, { 2, 1,  0,  4,  3 }, { 1, 2,  4,  0,  3 },
193
      { 2,  1,  0,  4,  3 }, { 2, 1,  4,  3,  0 }, { 1, 2,  4,  0,  3 } },
194
    { { 2,  0, -1, -1, -1 }, { 2, 0,  1,  4,  3 }, { 1, 2,  0,  4,  3 },
195
      { 2,  1,  0,  4,  3 }, { 2, 1,  3,  4,  0 }, { 2, 4,  1,  0,  3 } },
196
    { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  3,  4 }, { 1, 2,  3,  0,  4 },
197
      { 2,  0,  1,  3,  4 }, { 2, 1,  3,  0,  4 }, { 2, 0,  4,  3,  1 } },
198
    { { 0,  2, -1, -1, -1 }, { 0, 2,  4,  1,  3 }, { 1, 4,  2,  0,  3 },
199
      { 4,  2,  0,  1,  3 }, { 2, 0,  1,  4,  3 }, { 4, 2,  1,  0,  3 } },
200
};
201
202
static const struct {
203
    uint8_t run;
204
    uint8_t level;
205
} svq3_dct_tables[2][16] = {
206
    { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
207
      { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
208
    { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
209
      { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
210
};
211
212
static const uint32_t svq3_dequant_coeff[32] = {
213
     3881,  4351,  4890,  5481,   6154,   6914,   7761,   8718,
214
     9781, 10987, 12339, 13828,  15523,  17435,  19561,  21873,
215
    24552, 27656, 30847, 34870,  38807,  43747,  49103,  54683,
216
    61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
217
};
218
219
2537
static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
220
{
221
2537
    const unsigned qmul = svq3_dequant_coeff[qp];
222
#define stride 16
223
    int i;
224
    int temp[16];
225
    static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
226
227
12685
    for (i = 0; i < 4; i++) {
228
10148
        const int z0 = 13 * (input[4 * i + 0] +      input[4 * i + 2]);
229
10148
        const int z1 = 13 * (input[4 * i + 0] -      input[4 * i + 2]);
230
10148
        const int z2 =  7 *  input[4 * i + 1] - 17 * input[4 * i + 3];
231
10148
        const int z3 = 17 *  input[4 * i + 1] +  7 * input[4 * i + 3];
232
233
10148
        temp[4 * i + 0] = z0 + z3;
234
10148
        temp[4 * i + 1] = z1 + z2;
235
10148
        temp[4 * i + 2] = z1 - z2;
236
10148
        temp[4 * i + 3] = z0 - z3;
237
    }
238
239
12685
    for (i = 0; i < 4; i++) {
240
10148
        const int offset = x_offset[i];
241
10148
        const int z0     = 13 * (temp[4 * 0 + i] +      temp[4 * 2 + i]);
242
10148
        const int z1     = 13 * (temp[4 * 0 + i] -      temp[4 * 2 + i]);
243
10148
        const int z2     =  7 *  temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
244
10148
        const int z3     = 17 *  temp[4 * 1 + i] +  7 * temp[4 * 3 + i];
245
246
10148
        output[stride *  0 + offset] = (int)((z0 + z3) * qmul + 0x80000) >> 20;
247
10148
        output[stride *  2 + offset] = (int)((z1 + z2) * qmul + 0x80000) >> 20;
248
10148
        output[stride *  8 + offset] = (int)((z1 - z2) * qmul + 0x80000) >> 20;
249
10148
        output[stride * 10 + offset] = (int)((z0 - z3) * qmul + 0x80000) >> 20;
250
    }
251
2537
}
252
#undef stride
253
254
4012263
static void svq3_add_idct_c(uint8_t *dst, int16_t *block,
255
                            int stride, int qp, int dc)
256
{
257
4012263
    const int qmul = svq3_dequant_coeff[qp];
258
    int i;
259
260
4012263
    if (dc) {
261
998567
        dc       = 13 * 13 * (dc == 1 ? 1538U* block[0]
262
972473
                                      : qmul * (block[0] >> 3) / 2);
263
998567
        block[0] = 0;
264
    }
265
266
20061315
    for (i = 0; i < 4; i++) {
267
16049052
        const int z0 = 13 * (block[0 + 4 * i] +      block[2 + 4 * i]);
268
16049052
        const int z1 = 13 * (block[0 + 4 * i] -      block[2 + 4 * i]);
269
16049052
        const int z2 =  7 *  block[1 + 4 * i] - 17 * block[3 + 4 * i];
270
16049052
        const int z3 = 17 *  block[1 + 4 * i] +  7 * block[3 + 4 * i];
271
272
16049052
        block[0 + 4 * i] = z0 + z3;
273
16049052
        block[1 + 4 * i] = z1 + z2;
274
16049052
        block[2 + 4 * i] = z1 - z2;
275
16049052
        block[3 + 4 * i] = z0 - z3;
276
    }
277
278
20061315
    for (i = 0; i < 4; i++) {
279
16049052
        const unsigned z0 = 13 * (block[i + 4 * 0] +      block[i + 4 * 2]);
280
16049052
        const unsigned z1 = 13 * (block[i + 4 * 0] -      block[i + 4 * 2]);
281
16049052
        const unsigned z2 =  7 *  block[i + 4 * 1] - 17 * block[i + 4 * 3];
282
16049052
        const unsigned z3 = 17 *  block[i + 4 * 1] +  7 * block[i + 4 * 3];
283
16049052
        const int rr = (dc + 0x80000u);
284
285
16049052
        dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((int)((z0 + z3) * qmul + rr) >> 20));
286
16049052
        dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((int)((z1 + z2) * qmul + rr) >> 20));
287
16049052
        dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((int)((z1 - z2) * qmul + rr) >> 20));
288
16049052
        dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((int)((z0 - z3) * qmul + rr) >> 20));
289
    }
290
291
4012263
    memset(block, 0, 16 * sizeof(int16_t));
292
4012263
}
293
294
3880617
static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
295
                                    int index, const int type)
296
{
297
    static const uint8_t *const scan_patterns[4] = {
298
        luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan
299
    };
300
301
    int run, level, sign, limit;
302
    unsigned vlc;
303
3880617
    const int intra           = 3 * type >> 2;
304
3880617
    const uint8_t *const scan = scan_patterns[type];
305
306
5000433
    for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
307
9817031
        for (; (vlc = get_interleaved_ue_golomb(gb)) != 0; index++) {
308
5376506
            if ((int32_t)vlc < 0)
309
                return -1;
310
311
5376506
            sign     = (vlc & 1) ? 0 : -1;
312
5376506
            vlc      = vlc + 1 >> 1;
313
314
5376506
            if (type == 3) {
315
249567
                if (vlc < 3) {
316
168966
                    run   = 0;
317
168966
                    level = vlc;
318
80601
                } else if (vlc < 4) {
319
34195
                    run   = 1;
320
34195
                    level = 1;
321
                } else {
322
46406
                    run   = vlc & 0x3;
323
46406
                    level = (vlc + 9 >> 2) - run;
324
                }
325
            } else {
326
5126939
                if (vlc < 16U) {
327
5034539
                    run   = svq3_dct_tables[intra][vlc].run;
328
5034539
                    level = svq3_dct_tables[intra][vlc].level;
329
92400
                } else if (intra) {
330
8622
                    run   = vlc & 0x7;
331

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

2744
        if (s->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) {
1448
            av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1449
            av_frame_unref(s->next_pic->f);
1450
            ret = get_buffer(avctx, s->next_pic);
1451
            if (ret < 0)
1452
                return ret;
1453
            memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]);
1454
            memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) *
1455
                   s->next_pic->f->linesize[1]);
1456
            memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) *
1457
                   s->next_pic->f->linesize[2]);
1458
        }
1459
    }
1460
1461
2808
    if (avctx->debug & FF_DEBUG_PICT_INFO)
1462
        av_log(s->avctx, AV_LOG_DEBUG,
1463
               "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1464
               av_get_picture_type_char(s->pict_type),
1465
               s->halfpel_flag, s->thirdpel_flag,
1466
               s->adaptive_quant, s->qscale, s->slice_num);
1467
1468

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

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

7694
                if (((get_bits_count(&s->gb_slice) & 7) == 0 ||
1517
3847
                    show_bits(&s->gb_slice, get_bits_left(&s->gb_slice) & 7) == 0)) {
1518
1519
                    if (svq3_decode_slice_header(avctx))
1520
                        return -1;
1521
                }
1522
3847
                if (s->slice_type != s->pict_type) {
1523
                    avpriv_request_sample(avctx, "non constant slice type");
1524
                }
1525
                /* TODO: support s->mb_skip_run */
1526
            }
1527
1528
842100
            mb_type = get_interleaved_ue_golomb(&s->gb_slice);
1529
1530
842100
            if (s->pict_type == AV_PICTURE_TYPE_I)
1531
19170
                mb_type += 8;
1532

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

842100
            if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
1535
                av_log(s->avctx, AV_LOG_ERROR,
1536
                       "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
1537
                return -1;
1538
            }
1539
1540

842100
            if (mb_type != 0 || s->cbp)
1541
488398
                hl_decode_mb(s);
1542
1543

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

841020
                    (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
1546
        }
1547
1548
42120
        ff_draw_horiz_band(avctx, s->cur_pic->f,
1549
42060
                           s->last_pic->f->data[0] ? s->last_pic->f : NULL,
1550
42120
                           16 * s->mb_y, 16, PICT_FRAME, 0,
1551
                           s->low_delay);
1552
    }
1553
1554
2808
    left = buf_size*8 - get_bits_count(&s->gb_slice);
1555
1556

2808
    if (s->mb_y != s->mb_height || s->mb_x != s->mb_width) {
1557
        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);
1558
        //av_hex_dump(stderr, buf+buf_size-8, 8);
1559
    }
1560
1561
2808
    if (left < 0) {
1562
        av_log(avctx, AV_LOG_ERROR, "frame num %d left %d\n", avctx->frame_number, left);
1563
        return -1;
1564
    }
1565
1566

2808
    if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
1567
4
        ret = av_frame_ref(data, s->cur_pic->f);
1568
2804
    else if (s->last_pic->f->data[0])
1569
2800
        ret = av_frame_ref(data, s->last_pic->f);
1570
2808
    if (ret < 0)
1571
        return ret;
1572
1573
    /* Do not output the last pic after seeking. */
1574

2808
    if (s->last_pic->f->data[0] || s->low_delay)
1575
2804
        *got_frame = 1;
1576
1577
2808
    if (s->pict_type != AV_PICTURE_TYPE_B) {
1578
2804
        FFSWAP(SVQ3Frame*, s->cur_pic, s->next_pic);
1579
    } else {
1580
4
        av_frame_unref(s->cur_pic->f);
1581
    }
1582
1583
2808
    return buf_size;
1584
}
1585
1586
8
static av_cold int svq3_decode_end(AVCodecContext *avctx)
1587
{
1588
8
    SVQ3Context *s = avctx->priv_data;
1589
1590
8
    free_picture(avctx, s->cur_pic);
1591
8
    free_picture(avctx, s->next_pic);
1592
8
    free_picture(avctx, s->last_pic);
1593
8
    av_frame_free(&s->cur_pic->f);
1594
8
    av_frame_free(&s->next_pic->f);
1595
8
    av_frame_free(&s->last_pic->f);
1596
8
    av_freep(&s->slice_buf);
1597
8
    av_freep(&s->intra4x4_pred_mode);
1598
8
    av_freep(&s->edge_emu_buffer);
1599
8
    av_freep(&s->mb2br_xy);
1600
1601
8
    return 0;
1602
}
1603
1604
AVCodec ff_svq3_decoder = {
1605
    .name           = "svq3",
1606
    .long_name      = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
1607
    .type           = AVMEDIA_TYPE_VIDEO,
1608
    .id             = AV_CODEC_ID_SVQ3,
1609
    .priv_data_size = sizeof(SVQ3Context),
1610
    .init           = svq3_decode_init,
1611
    .close          = svq3_decode_end,
1612
    .decode         = svq3_decode_frame,
1613
    .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND |
1614
                      AV_CODEC_CAP_DR1             |
1615
                      AV_CODEC_CAP_DELAY,
1616
    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
1617
                                                     AV_PIX_FMT_NONE},
1618
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
1619
};