svq3.c 47.2 KB
Newer Older
M
Michael Niedermayer 已提交
1
/*
2
 * Copyright (c) 2003 The Libav Project
M
Michael Niedermayer 已提交
3
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav is free software; you can redistribute it and/or
M
Michael Niedermayer 已提交
7 8
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
M
Michael Niedermayer 已提交
10
 *
11
 * Libav is distributed in the hope that it will be useful,
M
Michael Niedermayer 已提交
12 13 14 15 16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with Libav; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 20 21
 */

/*
M
Michael Niedermayer 已提交
22 23
 * How to use this decoder:
 * SVQ3 data is transported within Apple Quicktime files. Quicktime files
24 25 26 27 28 29
 * have stsd atoms to describe media trak properties. A stsd atom for a
 * video trak contains 1 or more ImageDescription atoms. These atoms begin
 * with the 4-byte length of the atom followed by the codec fourcc. Some
 * decoders need information in this atom to operate correctly. Such
 * is the case with SVQ3. In order to get the best use out of this decoder,
 * the calling app must make the SVQ3 ImageDescription atom available
M
Michael Niedermayer 已提交
30 31
 * via the AVCodecContext's extradata[_size] field:
 *
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 35
 * AVCodecContext.extradata_size = size of ImageDescription atom memory
 * buffer (which will be the same as the ImageDescription atom size field
36 37 38 39
 * from the QT file, minus 4 bytes since the length is missing)
 *
 * You will know you have these parameters passed correctly when the decoder
 * correctly decodes this file:
40
 *  http://samples.libav.org/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
M
Michael Niedermayer 已提交
41
 */
42 43

#include "libavutil/attributes.h"
44 45 46 47 48
#include "internal.h"
#include "avcodec.h"
#include "mpegvideo.h"
#include "h264.h"

D
Diego Biurrun 已提交
49
#include "h264data.h" // FIXME FIXME FIXME
50

51
#include "h264_mvpred.h"
52
#include "golomb.h"
53
#include "hpeldsp.h"
54 55
#include "rectangle.h"

56
#if CONFIG_ZLIB
57 58 59
#include <zlib.h>
#endif

60
#include "svq1.h"
61
#include "svq3.h"
62

M
Michael Niedermayer 已提交
63
/**
64
 * @file
M
Michael Niedermayer 已提交
65 66 67
 * svq3 decoder.
 */

68 69
typedef struct {
    H264Context h;
70
    HpelDSPContext hdsp;
A
Anton Khirnov 已提交
71 72 73
    Picture *cur_pic;
    Picture *next_pic;
    Picture *last_pic;
74 75 76 77 78
    int halfpel_flag;
    int thirdpel_flag;
    int unknown_flag;
    int next_slice_index;
    uint32_t watermark_key;
A
Anton Khirnov 已提交
79 80 81 82 83
    int adaptive_quant;
    int next_p_frame_damaged;
    int h_edge_pos;
    int v_edge_pos;
    int last_frame_output;
84 85
} SVQ3Context;

86 87
#define FULLPEL_MODE  1
#define HALFPEL_MODE  2
M
Michael Niedermayer 已提交
88
#define THIRDPEL_MODE 3
89
#define PREDICT_MODE  4
90

M
Michael Niedermayer 已提交
91
/* dual scan (from some older h264 draft)
D
Diego Biurrun 已提交
92 93 94 95 96 97 98 99
 * o-->o-->o   o
 *         |  /|
 * o   o   o / o
 * | / |   |/  |
 * o   o   o   o
 *   /
 * o-->o-->o-->o
 */
100
static const uint8_t svq3_scan[16] = {
D
Diego Biurrun 已提交
101 102 103 104
    0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
    2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
    0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
    0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
M
Michael Niedermayer 已提交
105 106
};

107 108 109 110 111 112 113
static const uint8_t luma_dc_zigzag_scan[16] = {
    0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
    3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
    1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
    3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
};

M
Michael Niedermayer 已提交
114
static const uint8_t svq3_pred_0[25][2] = {
115 116 117 118 119 120 121 122 123
    { 0, 0 },
    { 1, 0 }, { 0, 1 },
    { 0, 2 }, { 1, 1 }, { 2, 0 },
    { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
    { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
    { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
    { 2, 4 }, { 3, 3 }, { 4, 2 },
    { 4, 3 }, { 3, 4 },
    { 4, 4 }
M
Michael Niedermayer 已提交
124 125 126
};

static const int8_t svq3_pred_1[6][6][5] = {
D
Diego Biurrun 已提交
127 128 129 130 131 132 133 134 135 136 137 138
    { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
      { 2,  1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
    { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  4,  3 }, { 0, 1,  2,  4,  3 },
      { 0,  2,  1,  4,  3 }, { 2, 0,  1,  3,  4 }, { 0, 4,  2,  1,  3 } },
    { { 2,  0, -1, -1, -1 }, { 2, 1,  0,  4,  3 }, { 1, 2,  4,  0,  3 },
      { 2,  1,  0,  4,  3 }, { 2, 1,  4,  3,  0 }, { 1, 2,  4,  0,  3 } },
    { { 2,  0, -1, -1, -1 }, { 2, 0,  1,  4,  3 }, { 1, 2,  0,  4,  3 },
      { 2,  1,  0,  4,  3 }, { 2, 1,  3,  4,  0 }, { 2, 4,  1,  0,  3 } },
    { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  3,  4 }, { 1, 2,  3,  0,  4 },
      { 2,  0,  1,  3,  4 }, { 2, 1,  3,  0,  4 }, { 2, 0,  4,  3,  1 } },
    { { 0,  2, -1, -1, -1 }, { 0, 2,  4,  1,  3 }, { 1, 4,  2,  0,  3 },
      { 4,  2,  0,  1,  3 }, { 2, 0,  1,  4,  3 }, { 4, 2,  1,  0,  3 } },
M
Michael Niedermayer 已提交
139 140
};

D
Diego Biurrun 已提交
141 142 143 144
static const struct {
    uint8_t run;
    uint8_t level;
} svq3_dct_tables[2][16] = {
145 146 147 148
    { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
      { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
    { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
      { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
M
Michael Niedermayer 已提交
149 150 151
};

static const uint32_t svq3_dequant_coeff[32] = {
D
Diego Biurrun 已提交
152 153 154 155
     3881,  4351,  4890,  5481,   6154,   6914,   7761,   8718,
     9781, 10987, 12339, 13828,  15523,  17435,  19561,  21873,
    24552, 27656, 30847, 34870,  38807,  43747,  49103,  54683,
    61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
M
Michael Niedermayer 已提交
156 157
};

D
Diego Biurrun 已提交
158
void ff_svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
D
Diego Biurrun 已提交
159
{
160
    const int qmul = svq3_dequant_coeff[qp];
M
Michael Niedermayer 已提交
161 162 163
#define stride 16
    int i;
    int temp[16];
D
Diego Biurrun 已提交
164 165 166 167 168 169 170 171 172 173 174 175
    static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };

    for (i = 0; i < 4; i++) {
        const int z0 = 13 * (input[4 * i + 0] +      input[4 * i + 2]);
        const int z1 = 13 * (input[4 * i + 0] -      input[4 * i + 2]);
        const int z2 =  7 *  input[4 * i + 1] - 17 * input[4 * i + 3];
        const int z3 = 17 *  input[4 * i + 1] +  7 * input[4 * i + 3];

        temp[4 * i + 0] = z0 + z3;
        temp[4 * i + 1] = z1 + z2;
        temp[4 * i + 2] = z1 - z2;
        temp[4 * i + 3] = z0 - z3;
M
Michael Niedermayer 已提交
176 177
    }

D
Diego Biurrun 已提交
178 179 180 181 182 183 184
    for (i = 0; i < 4; i++) {
        const int offset = x_offset[i];
        const int z0     = 13 * (temp[4 * 0 + i] +      temp[4 * 2 + i]);
        const int z1     = 13 * (temp[4 * 0 + i] -      temp[4 * 2 + i]);
        const int z2     =  7 *  temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
        const int z3     = 17 *  temp[4 * 1 + i] +  7 * temp[4 * 3 + i];

185 186 187 188
        output[stride *  0 + offset] = (z0 + z3) * qmul + 0x80000 >> 20;
        output[stride *  2 + offset] = (z1 + z2) * qmul + 0x80000 >> 20;
        output[stride *  8 + offset] = (z1 - z2) * qmul + 0x80000 >> 20;
        output[stride * 10 + offset] = (z0 - z3) * qmul + 0x80000 >> 20;
M
Michael Niedermayer 已提交
189 190 191 192
    }
}
#undef stride

D
Diego Biurrun 已提交
193
void ff_svq3_add_idct_c(uint8_t *dst, int16_t *block,
D
Diego Biurrun 已提交
194
                        int stride, int qp, int dc)
195
{
196
    const int qmul = svq3_dequant_coeff[qp];
M
Michael Niedermayer 已提交
197 198 199
    int i;

    if (dc) {
200 201
        dc       = 13 * 13 * (dc == 1 ? 1538 * block[0]
                                      : qmul * (block[0] >> 3) / 2);
M
Michael Niedermayer 已提交
202 203 204
        block[0] = 0;
    }

205
    for (i = 0; i < 4; i++) {
D
Diego Biurrun 已提交
206 207 208 209 210 211 212 213 214
        const int z0 = 13 * (block[0 + 4 * i] +      block[2 + 4 * i]);
        const int z1 = 13 * (block[0 + 4 * i] -      block[2 + 4 * i]);
        const int z2 =  7 *  block[1 + 4 * i] - 17 * block[3 + 4 * i];
        const int z3 = 17 *  block[1 + 4 * i] +  7 * block[3 + 4 * i];

        block[0 + 4 * i] = z0 + z3;
        block[1 + 4 * i] = z1 + z2;
        block[2 + 4 * i] = z1 - z2;
        block[3 + 4 * i] = z0 - z3;
M
Michael Niedermayer 已提交
215 216
    }

217
    for (i = 0; i < 4; i++) {
D
Diego Biurrun 已提交
218 219 220 221
        const int z0 = 13 * (block[i + 4 * 0] +      block[i + 4 * 2]);
        const int z1 = 13 * (block[i + 4 * 0] -      block[i + 4 * 2]);
        const int z2 =  7 *  block[i + 4 * 1] - 17 * block[i + 4 * 3];
        const int z3 = 17 *  block[i + 4 * 1] +  7 * block[i + 4 * 3];
222 223
        const int rr = (dc + 0x80000);

224 225 226 227
        dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((z0 + z3) * qmul + rr >> 20));
        dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((z1 + z2) * qmul + rr >> 20));
        dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((z1 - z2) * qmul + rr >> 20));
        dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((z0 - z3) * qmul + rr >> 20));
M
Michael Niedermayer 已提交
228
    }
229 230

    memset(block, 0, 16 * sizeof(int16_t));
M
Michael Niedermayer 已提交
231 232
}

D
Diego Biurrun 已提交
233
static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
234 235
                                    int index, const int type)
{
236 237
    static const uint8_t *const scan_patterns[4] =
    { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
M
Michael Niedermayer 已提交
238

239 240
    int run, level, limit;
    unsigned vlc;
241
    const int intra           = 3 * type >> 2;
242
    const uint8_t *const scan = scan_patterns[type];
M
Michael Niedermayer 已提交
243

244 245
    for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
        for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
246 247
            int sign = (vlc & 1) ? 0 : -1;
            vlc      = vlc + 1 >> 1;
D
Diego Biurrun 已提交
248 249 250 251 252 253 254 255 256

            if (type == 3) {
                if (vlc < 3) {
                    run   = 0;
                    level = vlc;
                } else if (vlc < 4) {
                    run   = 1;
                    level = 1;
                } else {
257 258
                    run   = vlc & 0x3;
                    level = (vlc + 9 >> 2) - run;
D
Diego Biurrun 已提交
259 260 261 262 263 264
                }
            } else {
                if (vlc < 16) {
                    run   = svq3_dct_tables[intra][vlc].run;
                    level = svq3_dct_tables[intra][vlc].level;
                } else if (intra) {
265
                    run   = vlc & 0x7;
D
Diego Biurrun 已提交
266 267 268
                    level = (vlc >> 3) +
                            ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
                } else {
269
                    run   = vlc & 0xF;
D
Diego Biurrun 已提交
270 271 272 273
                    level = (vlc >> 4) +
                            ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
                }
            }
M
Michael Niedermayer 已提交
274

D
Diego Biurrun 已提交
275 276 277 278
            if ((index += run) >= limit)
                return -1;

            block[scan[index]] = (level ^ sign) - sign;
279
        }
M
Michael Niedermayer 已提交
280

281 282 283
        if (type != 2) {
            break;
        }
M
Michael Niedermayer 已提交
284 285
    }

286
    return 0;
M
Michael Niedermayer 已提交
287 288
}

A
Anton Khirnov 已提交
289
static inline void svq3_mc_dir_part(SVQ3Context *s,
290 291 292 293
                                    int x, int y, int width, int height,
                                    int mx, int my, int dxy,
                                    int thirdpel, int dir, int avg)
{
A
Anton Khirnov 已提交
294 295
    H264Context *h     = &s->h;
    const Picture *pic = (dir == 0) ? s->last_pic : s->next_pic;
296 297
    uint8_t *src, *dest;
    int i, emu = 0;
D
Diego Biurrun 已提交
298
    int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
299 300 301

    mx += x;
    my += y;
M
Michael Niedermayer 已提交
302

303 304
    if (mx < 0 || mx >= s->h_edge_pos - width  - 1 ||
        my < 0 || my >= s->v_edge_pos - height - 1) {
305
        emu = 1;
306 307
        mx = av_clip(mx, -16, s->h_edge_pos - width  + 15);
        my = av_clip(my, -16, s->v_edge_pos - height + 15);
M
Michael Niedermayer 已提交
308 309
    }

310
    /* form component predictions */
A
Anton Khirnov 已提交
311 312
    dest = h->cur_pic.f.data[0] + x + y * h->linesize;
    src  = pic->f.data[0] + mx + my * h->linesize;
313 314

    if (emu) {
A
Anton Khirnov 已提交
315
        h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src, h->linesize,
R
Ronald S. Bultje 已提交
316 317
                                 width + 1, height + 1,
                                 mx, my, s->h_edge_pos, s->v_edge_pos);
A
Anton Khirnov 已提交
318
        src = h->edge_emu_buffer;
M
Michael Niedermayer 已提交
319
    }
320
    if (thirdpel)
A
Anton Khirnov 已提交
321 322
        (avg ? h->dsp.avg_tpel_pixels_tab
             : h->dsp.put_tpel_pixels_tab)[dxy](dest, src, h->linesize,
D
Diego Biurrun 已提交
323
                                                width, height);
324
    else
325 326 327
        (avg ? s->hdsp.avg_pixels_tab
             : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, h->linesize,
                                                       height);
328

A
Anton Khirnov 已提交
329
    if (!(h->flags & CODEC_FLAG_GRAY)) {
330 331 332 333
        mx     = mx + (mx < (int) x) >> 1;
        my     = my + (my < (int) y) >> 1;
        width  = width  >> 1;
        height = height >> 1;
334 335 336
        blocksize++;

        for (i = 1; i < 3; i++) {
A
Anton Khirnov 已提交
337 338
            dest = h->cur_pic.f.data[i] + (x >> 1) + (y >> 1) * h->uvlinesize;
            src  = pic->f.data[i] + mx + my * h->uvlinesize;
339 340

            if (emu) {
A
Anton Khirnov 已提交
341
                h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src, h->uvlinesize,
R
Ronald S. Bultje 已提交
342 343 344
                                         width + 1, height + 1,
                                         mx, my, (s->h_edge_pos >> 1),
                                         s->v_edge_pos >> 1);
A
Anton Khirnov 已提交
345
                src = h->edge_emu_buffer;
346 347
            }
            if (thirdpel)
A
Anton Khirnov 已提交
348 349 350
                (avg ? h->dsp.avg_tpel_pixels_tab
                     : h->dsp.put_tpel_pixels_tab)[dxy](dest, src,
                                                        h->uvlinesize,
D
Diego Biurrun 已提交
351
                                                        width, height);
352
            else
353 354 355 356
                (avg ? s->hdsp.avg_pixels_tab
                     : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
                                                               h->uvlinesize,
                                                               height);
357 358
        }
    }
M
Michael Niedermayer 已提交
359 360
}

A
Anton Khirnov 已提交
361
static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
D
Diego Biurrun 已提交
362
                              int dir, int avg)
363
{
364
    int i, j, k, mx, my, dx, dy, x, y;
A
Anton Khirnov 已提交
365
    H264Context *h          = &s->h;
D
Diego Biurrun 已提交
366 367 368 369 370 371 372
    const int part_width    = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
    const int part_height   = 16 >> ((unsigned)(size + 1) / 3);
    const int extra_width   = (mode == PREDICT_MODE) ? -16 * 6 : 0;
    const int h_edge_pos    = 6 * (s->h_edge_pos - part_width)  - extra_width;
    const int v_edge_pos    = 6 * (s->v_edge_pos - part_height) - extra_width;

    for (i = 0; i < 16; i += part_height)
373
        for (j = 0; j < 16; j += part_width) {
A
Anton Khirnov 已提交
374 375
            const int b_xy = (4 * h->mb_x + (j >> 2)) +
                             (4 * h->mb_y + (i >> 2)) * h->b_stride;
376
            int dxy;
A
Anton Khirnov 已提交
377 378
            x = 16 * h->mb_x + j;
            y = 16 * h->mb_y + i;
379 380
            k = (j >> 2 & 1) + (i >> 1 & 2) +
                (j >> 1 & 4) + (i      & 8);
381 382

            if (mode != PREDICT_MODE) {
383
                pred_motion(h, k, part_width >> 2, dir, 1, &mx, &my);
384
            } else {
385 386
                mx = s->next_pic->motion_val[0][b_xy][0] << 1;
                my = s->next_pic->motion_val[0][b_xy][1] << 1;
387 388

                if (dir == 0) {
389 390 391 392
                    mx = mx * h->frame_num_offset /
                         h->prev_frame_num_offset + 1 >> 1;
                    my = my * h->frame_num_offset /
                         h->prev_frame_num_offset + 1 >> 1;
393
                } else {
394 395 396 397
                    mx = mx * (h->frame_num_offset - h->prev_frame_num_offset) /
                         h->prev_frame_num_offset + 1 >> 1;
                    my = my * (h->frame_num_offset - h->prev_frame_num_offset) /
                         h->prev_frame_num_offset + 1 >> 1;
398 399 400 401
                }
            }

            /* clip motion vector prediction to frame border */
D
Diego Biurrun 已提交
402 403
            mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
            my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
404 405 406 407 408

            /* get (optional) motion vector differential */
            if (mode == PREDICT_MODE) {
                dx = dy = 0;
            } else {
A
Anton Khirnov 已提交
409 410
                dy = svq3_get_se_golomb(&h->gb);
                dx = svq3_get_se_golomb(&h->gb);
411 412

                if (dx == INVALID_VLC || dy == INVALID_VLC) {
A
Anton Khirnov 已提交
413
                    av_log(h->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
414 415 416 417 418 419 420
                    return -1;
                }
            }

            /* compute motion vector */
            if (mode == THIRDPEL_MODE) {
                int fx, fy;
421 422 423 424
                mx  = (mx + 1 >> 1) + dx;
                my  = (my + 1 >> 1) + dy;
                fx  = (unsigned)(mx + 0x3000) / 3 - 0x1000;
                fy  = (unsigned)(my + 0x3000) / 3 - 0x1000;
D
Diego Biurrun 已提交
425 426 427 428
                dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);

                svq3_mc_dir_part(s, x, y, part_width, part_height,
                                 fx, fy, dxy, 1, dir, avg);
429 430 431
                mx += mx;
                my += my;
            } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
432 433
                mx  = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000;
                my  = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000;
D
Diego Biurrun 已提交
434
                dxy = (mx & 1) + 2 * (my & 1);
435

D
Diego Biurrun 已提交
436 437
                svq3_mc_dir_part(s, x, y, part_width, part_height,
                                 mx >> 1, my >> 1, dxy, 0, dir, avg);
438 439 440
                mx *= 3;
                my *= 3;
            } else {
441 442
                mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000;
                my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000;
443

D
Diego Biurrun 已提交
444 445
                svq3_mc_dir_part(s, x, y, part_width, part_height,
                                 mx, my, 0, 0, dir, avg);
446 447 448 449 450 451
                mx *= 6;
                my *= 6;
            }

            /* update mv_cache */
            if (mode != PREDICT_MODE) {
D
Diego Biurrun 已提交
452
                int32_t mv = pack16to32(mx, my);
453 454

                if (part_height == 8 && i < 8) {
D
Diego Biurrun 已提交
455
                    AV_WN32A(h->mv_cache[dir][scan8[k] + 1 * 8], mv);
456

D
Diego Biurrun 已提交
457 458
                    if (part_width == 8 && j < 8)
                        AV_WN32A(h->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
459
                }
D
Diego Biurrun 已提交
460
                if (part_width == 8 && j < 8)
461
                    AV_WN32A(h->mv_cache[dir][scan8[k] + 1], mv);
D
Diego Biurrun 已提交
462
                if (part_width == 4 || part_height == 4)
463
                    AV_WN32A(h->mv_cache[dir][scan8[k]], mv);
464 465 466
            }

            /* write back motion vectors */
467
            fill_rectangle(h->cur_pic.motion_val[dir][b_xy],
D
Diego Biurrun 已提交
468 469
                           part_width >> 2, part_height >> 2, h->b_stride,
                           pack16to32(mx, my), 4);
470
        }
471

472
    return 0;
473 474
}

A
Anton Khirnov 已提交
475
static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
476
{
A
Anton Khirnov 已提交
477
    H264Context *h = &s->h;
478 479 480 481
    int i, j, k, m, dir, mode;
    int cbp = 0;
    uint32_t vlc;
    int8_t *top, *left;
D
Diego Biurrun 已提交
482
    const int mb_xy         = h->mb_xy;
A
Anton Khirnov 已提交
483
    const int b_xy          = 4 * h->mb_x + 4 * h->mb_y * h->b_stride;
484

A
Anton Khirnov 已提交
485 486
    h->top_samples_available      = (h->mb_y == 0) ? 0x33FF : 0xFFFF;
    h->left_samples_available     = (h->mb_x == 0) ? 0x5F5F : 0xFFFF;
487 488 489
    h->topright_samples_available = 0xFFFF;

    if (mb_type == 0) {           /* SKIP */
A
Anton Khirnov 已提交
490
        if (h->pict_type == AV_PICTURE_TYPE_P ||
491
            s->next_pic->mb_type[mb_xy] == -1) {
A
Anton Khirnov 已提交
492
            svq3_mc_dir_part(s, 16 * h->mb_x, 16 * h->mb_y, 16, 16,
D
Diego Biurrun 已提交
493
                             0, 0, 0, 0, 0, 0);
494

A
Anton Khirnov 已提交
495 496
            if (h->pict_type == AV_PICTURE_TYPE_B)
                svq3_mc_dir_part(s, 16 * h->mb_x, 16 * h->mb_y, 16, 16,
D
Diego Biurrun 已提交
497
                                 0, 0, 0, 0, 1, 1);
498 499 500

            mb_type = MB_TYPE_SKIP;
        } else {
501
            mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
A
Anton Khirnov 已提交
502
            if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
503
                return -1;
A
Anton Khirnov 已提交
504
            if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
505
                return -1;
M
Michael Niedermayer 已提交
506

507
            mb_type = MB_TYPE_16x16;
508
        }
509
    } else if (mb_type < 8) {     /* INTER */
A
Anton Khirnov 已提交
510
        if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&h->gb))
511
            mode = THIRDPEL_MODE;
A
Anton Khirnov 已提交
512 513
        else if (s->halfpel_flag &&
                 s->thirdpel_flag == !get_bits1(&h->gb))
514
            mode = HALFPEL_MODE;
D
Diego Biurrun 已提交
515
        else
516
            mode = FULLPEL_MODE;
M
Michael Niedermayer 已提交
517

518 519
        /* fill caches */
        /* note ref_cache should contain here:
D
Diego Biurrun 已提交
520 521 522 523 524 525
         *  ????????
         *  ???11111
         *  N??11111
         *  N??11111
         *  N??11111
         */
526 527

        for (m = 0; m < 2; m++) {
A
Anton Khirnov 已提交
528
            if (h->mb_x > 0 && h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6] != -1) {
D
Diego Biurrun 已提交
529 530
                for (i = 0; i < 4; i++)
                    AV_COPY32(h->mv_cache[m][scan8[0] - 1 + i * 8],
531
                              h->cur_pic.motion_val[m][b_xy - 1 + i * h->b_stride]);
532
            } else {
D
Diego Biurrun 已提交
533 534
                for (i = 0; i < 4; i++)
                    AV_ZERO32(h->mv_cache[m][scan8[0] - 1 + i * 8]);
535
            }
A
Anton Khirnov 已提交
536
            if (h->mb_y > 0) {
D
Diego Biurrun 已提交
537
                memcpy(h->mv_cache[m][scan8[0] - 1 * 8],
538
                       h->cur_pic.motion_val[m][b_xy - h->b_stride],
D
Diego Biurrun 已提交
539 540
                       4 * 2 * sizeof(int16_t));
                memset(&h->ref_cache[m][scan8[0] - 1 * 8],
A
Anton Khirnov 已提交
541
                       (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
542

A
Anton Khirnov 已提交
543
                if (h->mb_x < h->mb_width - 1) {
D
Diego Biurrun 已提交
544
                    AV_COPY32(h->mv_cache[m][scan8[0] + 4 - 1 * 8],
545
                              h->cur_pic.motion_val[m][b_xy - h->b_stride + 4]);
D
Diego Biurrun 已提交
546
                    h->ref_cache[m][scan8[0] + 4 - 1 * 8] =
A
Anton Khirnov 已提交
547 548
                        (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride + 1] + 6] == -1 ||
                         h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
D
Diego Biurrun 已提交
549 550
                } else
                    h->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
A
Anton Khirnov 已提交
551
                if (h->mb_x > 0) {
D
Diego Biurrun 已提交
552
                    AV_COPY32(h->mv_cache[m][scan8[0] - 1 - 1 * 8],
553
                              h->cur_pic.motion_val[m][b_xy - h->b_stride - 1]);
D
Diego Biurrun 已提交
554
                    h->ref_cache[m][scan8[0] - 1 - 1 * 8] =
A
Anton Khirnov 已提交
555
                        (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
D
Diego Biurrun 已提交
556 557 558 559 560
                } else
                    h->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
            } else
                memset(&h->ref_cache[m][scan8[0] - 1 * 8 - 1],
                       PART_NOT_AVAILABLE, 8);
561

A
Anton Khirnov 已提交
562
            if (h->pict_type != AV_PICTURE_TYPE_B)
563
                break;
564
        }
M
Michael Niedermayer 已提交
565

566
        /* decode motion vector(s) and form prediction(s) */
A
Anton Khirnov 已提交
567 568
        if (h->pict_type == AV_PICTURE_TYPE_P) {
            if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
569
                return -1;
570
        } else {        /* AV_PICTURE_TYPE_B */
M
Matti Hamalainen 已提交
571
            if (mb_type != 2) {
A
Anton Khirnov 已提交
572
                if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
573
                    return -1;
M
Matti Hamalainen 已提交
574
            } else {
D
Diego Biurrun 已提交
575
                for (i = 0; i < 4; i++)
576
                    memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
D
Diego Biurrun 已提交
577
                           0, 4 * 2 * sizeof(int16_t));
M
Matti Hamalainen 已提交
578 579
            }
            if (mb_type != 1) {
A
Anton Khirnov 已提交
580
                if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
581
                    return -1;
M
Matti Hamalainen 已提交
582
            } else {
D
Diego Biurrun 已提交
583
                for (i = 0; i < 4; i++)
584
                    memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
D
Diego Biurrun 已提交
585
                           0, 4 * 2 * sizeof(int16_t));
M
Matti Hamalainen 已提交
586
            }
587
        }
M
Michael Niedermayer 已提交
588

589 590
        mb_type = MB_TYPE_16x16;
    } else if (mb_type == 8 || mb_type == 33) {   /* INTRA4x4 */
D
Diego Biurrun 已提交
591
        memset(h->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
592 593

        if (mb_type == 8) {
A
Anton Khirnov 已提交
594
            if (h->mb_x > 0) {
D
Diego Biurrun 已提交
595 596 597
                for (i = 0; i < 4; i++)
                    h->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6 - i];
                if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
598 599
                    h->left_samples_available = 0x5F5F;
            }
A
Anton Khirnov 已提交
600 601 602 603 604
            if (h->mb_y > 0) {
                h->intra4x4_pred_mode_cache[4 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 0];
                h->intra4x4_pred_mode_cache[5 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 1];
                h->intra4x4_pred_mode_cache[6 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 2];
                h->intra4x4_pred_mode_cache[7 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 3];
605

D
Diego Biurrun 已提交
606
                if (h->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
607 608 609 610
                    h->top_samples_available = 0x33FF;
            }

            /* decode prediction codes for luma blocks */
D
Diego Biurrun 已提交
611
            for (i = 0; i < 16; i += 2) {
A
Anton Khirnov 已提交
612
                vlc = svq3_get_ue_golomb(&h->gb);
613

D
Diego Biurrun 已提交
614
                if (vlc >= 25) {
A
Anton Khirnov 已提交
615
                    av_log(h->avctx, AV_LOG_ERROR, "luma prediction:%d\n", vlc);
616 617 618
                    return -1;
                }

D
Diego Biurrun 已提交
619 620
                left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
                top  = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
621 622 623 624

                left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
                left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];

D
Diego Biurrun 已提交
625
                if (left[1] == -1 || left[2] == -1) {
A
Anton Khirnov 已提交
626
                    av_log(h->avctx, AV_LOG_ERROR, "weird prediction\n");
627 628 629 630
                    return -1;
                }
            }
        } else {    /* mb_type == 33, DC_128_PRED block type */
D
Diego Biurrun 已提交
631 632
            for (i = 0; i < 4; i++)
                memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
633
        }
M
Michael Niedermayer 已提交
634

635
        write_back_intra_pred_mode(h);
M
Michael Niedermayer 已提交
636

637
        if (mb_type == 8) {
638
            ff_h264_check_intra4x4_pred_mode(h);
639

A
Anton Khirnov 已提交
640 641
            h->top_samples_available  = (h->mb_y == 0) ? 0x33FF : 0xFFFF;
            h->left_samples_available = (h->mb_x == 0) ? 0x5F5F : 0xFFFF;
642
        } else {
D
Diego Biurrun 已提交
643 644
            for (i = 0; i < 4; i++)
                memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
645

646 647 648
            h->top_samples_available  = 0x33FF;
            h->left_samples_available = 0x5F5F;
        }
649

650 651 652
        mb_type = MB_TYPE_INTRA4x4;
    } else {                      /* INTRA16x16 */
        dir = i_mb_type_info[mb_type - 8].pred_mode;
D
Diego Biurrun 已提交
653
        dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
M
Michael Niedermayer 已提交
654

655 656 657
        if ((h->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, dir, 0)) < 0) {
            av_log(h->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
            return h->intra16x16_pred_mode;
658
        }
M
Michael Niedermayer 已提交
659

D
Diego Biurrun 已提交
660
        cbp     = i_mb_type_info[mb_type - 8].cbp;
661
        mb_type = MB_TYPE_INTRA16x16;
662
    }
M
Michael Niedermayer 已提交
663

A
Anton Khirnov 已提交
664
    if (!IS_INTER(mb_type) && h->pict_type != AV_PICTURE_TYPE_I) {
D
Diego Biurrun 已提交
665
        for (i = 0; i < 4; i++)
666
            memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
D
Diego Biurrun 已提交
667
                   0, 4 * 2 * sizeof(int16_t));
A
Anton Khirnov 已提交
668
        if (h->pict_type == AV_PICTURE_TYPE_B) {
D
Diego Biurrun 已提交
669
            for (i = 0; i < 4; i++)
670
                memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
D
Diego Biurrun 已提交
671
                       0, 4 * 2 * sizeof(int16_t));
672
        }
M
Michael Niedermayer 已提交
673
    }
674
    if (!IS_INTRA4x4(mb_type)) {
D
Diego Biurrun 已提交
675
        memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy], DC_PRED, 8);
676
    }
A
Anton Khirnov 已提交
677
    if (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B) {
D
Diego Biurrun 已提交
678
        memset(h->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
679
    }
680

D
Diego Biurrun 已提交
681
    if (!IS_INTRA16x16(mb_type) &&
A
Anton Khirnov 已提交
682 683 684
        (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B)) {
        if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48) {
            av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
685 686
            return -1;
        }
M
Michael Niedermayer 已提交
687

D
Diego Biurrun 已提交
688 689
        cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc]
                                : golomb_to_inter_cbp[vlc];
690
    }
D
Diego Biurrun 已提交
691
    if (IS_INTRA16x16(mb_type) ||
A
Anton Khirnov 已提交
692 693
        (h->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
        h->qscale += svq3_get_se_golomb(&h->gb);
M
Michael Niedermayer 已提交
694

A
Anton Khirnov 已提交
695 696
        if (h->qscale > 31u) {
            av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", h->qscale);
697 698
            return -1;
        }
M
Michael Niedermayer 已提交
699
    }
700
    if (IS_INTRA16x16(mb_type)) {
D
Diego Biurrun 已提交
701 702
        AV_ZERO128(h->mb_luma_dc[0] + 0);
        AV_ZERO128(h->mb_luma_dc[0] + 8);
A
Anton Khirnov 已提交
703 704
        if (svq3_decode_block(&h->gb, h->mb_luma_dc[0], 0, 1)) {
            av_log(h->avctx, AV_LOG_ERROR,
D
Diego Biurrun 已提交
705
                   "error while decoding intra luma dc\n");
706
            return -1;
707
        }
708
    }
M
Michael Niedermayer 已提交
709

710 711
    if (cbp) {
        const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
A
Anton Khirnov 已提交
712
        const int type  = ((h->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
713

D
Diego Biurrun 已提交
714
        for (i = 0; i < 4; i++)
715 716
            if ((cbp & (1 << i))) {
                for (j = 0; j < 4; j++) {
D
Diego Biurrun 已提交
717 718 719 720 721
                    k = index ? (1 * (j & 1) + 2 * (i & 1) +
                                 2 * (j & 2) + 4 * (i & 2))
                              : (4 * i + j);
                    h->non_zero_count_cache[scan8[k]] = 1;

A
Anton Khirnov 已提交
722 723
                    if (svq3_decode_block(&h->gb, &h->mb[16 * k], index, type)) {
                        av_log(h->avctx, AV_LOG_ERROR,
D
Diego Biurrun 已提交
724
                               "error while decoding block\n");
725 726 727 728
                        return -1;
                    }
                }
            }
M
Michael Niedermayer 已提交
729

730
        if ((cbp & 0x30)) {
D
Diego Biurrun 已提交
731
            for (i = 1; i < 3; ++i)
A
Anton Khirnov 已提交
732 733
                if (svq3_decode_block(&h->gb, &h->mb[16 * 16 * i], 0, 3)) {
                    av_log(h->avctx, AV_LOG_ERROR,
D
Diego Biurrun 已提交
734 735 736
                           "error while decoding chroma dc block\n");
                    return -1;
                }
737 738

            if ((cbp & 0x20)) {
739 740
                for (i = 1; i < 3; i++) {
                    for (j = 0; j < 4; j++) {
D
Diego Biurrun 已提交
741 742
                        k                                 = 16 * i + j;
                        h->non_zero_count_cache[scan8[k]] = 1;
743

A
Anton Khirnov 已提交
744 745
                        if (svq3_decode_block(&h->gb, &h->mb[16 * k], 1, 1)) {
                            av_log(h->avctx, AV_LOG_ERROR,
D
Diego Biurrun 已提交
746
                                   "error while decoding chroma ac block\n");
747 748
                            return -1;
                        }
749 750 751
                    }
                }
            }
752
        }
M
Michael Niedermayer 已提交
753 754
    }

D
Diego Biurrun 已提交
755
    h->cbp                              = cbp;
756
    h->cur_pic.mb_type[mb_xy] = mb_type;
M
Michael Niedermayer 已提交
757

D
Diego Biurrun 已提交
758
    if (IS_INTRA(mb_type))
759
        h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
M
Michael Niedermayer 已提交
760

761
    return 0;
M
Michael Niedermayer 已提交
762 763
}

764
static int svq3_decode_slice_header(AVCodecContext *avctx)
765
{
A
Anton Khirnov 已提交
766 767
    SVQ3Context *s = avctx->priv_data;
    H264Context *h    = &s->h;
D
Diego Biurrun 已提交
768
    const int mb_xy   = h->mb_xy;
769
    int i, header;
770
    unsigned slice_id;
771

A
Anton Khirnov 已提交
772
    header = get_bits(&h->gb, 8);
773

774 775
    if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
        /* TODO: what? */
776
        av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
777 778
        return -1;
    } else {
779
        int length = header >> 5 & 3;
780

A
Anton Khirnov 已提交
781 782 783
        s->next_slice_index = get_bits_count(&h->gb) +
                              8 * show_bits(&h->gb, 8 * length) +
                              8 * length;
784

A
Anton Khirnov 已提交
785
        if (s->next_slice_index > h->gb.size_in_bits) {
786
            av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
787
            return -1;
D
Diego Biurrun 已提交
788
        }
789

A
Anton Khirnov 已提交
790 791
        h->gb.size_in_bits = s->next_slice_index - 8 * (length - 1);
        skip_bits(&h->gb, 8);
792

A
Anton Khirnov 已提交
793 794 795 796
        if (s->watermark_key) {
            uint32_t header = AV_RL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1]);
            AV_WL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1],
                    header ^ s->watermark_key);
797 798
        }
        if (length > 0) {
A
Anton Khirnov 已提交
799 800
            memcpy((uint8_t *) &h->gb.buffer[get_bits_count(&h->gb) >> 3],
                   &h->gb.buffer[h->gb.size_in_bits >> 3], length - 1);
801
        }
A
Anton Khirnov 已提交
802
        skip_bits_long(&h->gb, 0);
803 804
    }

A
Anton Khirnov 已提交
805 806
    if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) {
        av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %d \n", slice_id);
807 808
        return -1;
    }
809

810
    h->slice_type = golomb_to_pict_type[slice_id];
811

812
    if ((header & 0x9F) == 2) {
A
Anton Khirnov 已提交
813 814 815
        i              = (h->mb_num < 64) ? 6 : (1 + av_log2(h->mb_num - 1));
        h->mb_skip_run = get_bits(&h->gb, i) -
                         (h->mb_y * h->mb_width + h->mb_x);
816
    } else {
A
Anton Khirnov 已提交
817 818
        skip_bits1(&h->gb);
        h->mb_skip_run = 0;
819
    }
820

A
Anton Khirnov 已提交
821 822 823
    h->slice_num      = get_bits(&h->gb, 8);
    h->qscale         = get_bits(&h->gb, 5);
    s->adaptive_quant = get_bits1(&h->gb);
824

825
    /* unknown fields */
A
Anton Khirnov 已提交
826
    skip_bits1(&h->gb);
827

A
Anton Khirnov 已提交
828 829
    if (s->unknown_flag)
        skip_bits1(&h->gb);
830

A
Anton Khirnov 已提交
831 832
    skip_bits1(&h->gb);
    skip_bits(&h->gb, 2);
833

A
Anton Khirnov 已提交
834 835
    while (get_bits1(&h->gb))
        skip_bits(&h->gb, 8);
836

837
    /* reset intra predictors and invalidate motion vector references */
A
Anton Khirnov 已提交
838
    if (h->mb_x > 0) {
D
Diego Biurrun 已提交
839 840
        memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - 1] + 3,
               -1, 4 * sizeof(int8_t));
A
Anton Khirnov 已提交
841 842
        memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_x],
               -1, 8 * sizeof(int8_t) * h->mb_x);
843
    }
A
Anton Khirnov 已提交
844 845 846
    if (h->mb_y > 0) {
        memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_stride],
               -1, 8 * sizeof(int8_t) * (h->mb_width - h->mb_x));
847

A
Anton Khirnov 已提交
848 849
        if (h->mb_x > 0)
            h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] = -1;
850 851
    }

852
    return 0;
853 854
}

855
static av_cold int svq3_decode_init(AVCodecContext *avctx)
856
{
A
Anton Khirnov 已提交
857 858
    SVQ3Context *s = avctx->priv_data;
    H264Context *h = &s->h;
859
    int m;
860
    unsigned char *extradata;
861
    unsigned char *extradata_end;
862
    unsigned int size;
863
    int marker_found = 0;
864

A
Anton Khirnov 已提交
865 866 867 868 869 870 871 872 873 874
    s->cur_pic  = av_mallocz(sizeof(*s->cur_pic));
    s->last_pic = av_mallocz(sizeof(*s->last_pic));
    s->next_pic = av_mallocz(sizeof(*s->next_pic));
    if (!s->next_pic || !s->last_pic || !s->cur_pic) {
        av_freep(&s->cur_pic);
        av_freep(&s->last_pic);
        av_freep(&s->next_pic);
        return AVERROR(ENOMEM);
    }

875
    if (ff_h264_decode_init(avctx) < 0)
876 877
        return -1;

878
    ff_hpeldsp_init(&s->hdsp, avctx->flags);
A
Anton Khirnov 已提交
879
    h->flags           = avctx->flags;
D
Diego Biurrun 已提交
880
    h->is_complex      = 1;
A
Anton Khirnov 已提交
881
    h->picture_structure = PICT_FRAME;
D
Diego Biurrun 已提交
882
    avctx->pix_fmt     = avctx->codec->pix_fmts[0];
883

A
Anton Khirnov 已提交
884
    h->chroma_qp[0] = h->chroma_qp[1] = 4;
A
Anton Khirnov 已提交
885
    h->chroma_x_shift = h->chroma_y_shift = 1;
A
Anton Khirnov 已提交
886

A
Anton Khirnov 已提交
887 888 889
    s->halfpel_flag  = 1;
    s->thirdpel_flag = 1;
    s->unknown_flag  = 0;
A
Anton Khirnov 已提交
890 891 892 893 894 895 896 897 898

    /* prowl for the "SEQH" marker in the extradata */
    extradata     = (unsigned char *)avctx->extradata;
    extradata_end = avctx->extradata + avctx->extradata_size;
    if (extradata) {
        for (m = 0; m + 8 < avctx->extradata_size; m++) {
            if (!memcmp(extradata, "SEQH", 4)) {
                marker_found = 1;
                break;
899
            }
A
Anton Khirnov 已提交
900
            extradata++;
901
        }
A
Anton Khirnov 已提交
902
    }
M
Michael Niedermayer 已提交
903

A
Anton Khirnov 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
    /* if a match was found, parse the extra data */
    if (marker_found) {
        GetBitContext gb;
        int frame_size_code;

        size = AV_RB32(&extradata[4]);
        if (size > extradata_end - extradata - 8)
            return AVERROR_INVALIDDATA;
        init_get_bits(&gb, extradata + 8, size * 8);

        /* 'frame size code' and optional 'width, height' */
        frame_size_code = get_bits(&gb, 3);
        switch (frame_size_code) {
        case 0:
            avctx->width  = 160;
            avctx->height = 120;
            break;
        case 1:
            avctx->width  = 128;
            avctx->height =  96;
            break;
        case 2:
            avctx->width  = 176;
            avctx->height = 144;
            break;
        case 3:
            avctx->width  = 352;
            avctx->height = 288;
            break;
        case 4:
            avctx->width  = 704;
            avctx->height = 576;
            break;
        case 5:
            avctx->width  = 240;
            avctx->height = 180;
            break;
        case 6:
            avctx->width  = 320;
            avctx->height = 240;
            break;
        case 7:
            avctx->width  = get_bits(&gb, 12);
            avctx->height = get_bits(&gb, 12);
            break;
        }
M
Michael Niedermayer 已提交
950

A
Anton Khirnov 已提交
951 952
        s->halfpel_flag  = get_bits1(&gb);
        s->thirdpel_flag = get_bits1(&gb);
M
Michael Niedermayer 已提交
953

A
Anton Khirnov 已提交
954 955 956 957 958
        /* unknown fields */
        skip_bits1(&gb);
        skip_bits1(&gb);
        skip_bits1(&gb);
        skip_bits1(&gb);
M
Michael Niedermayer 已提交
959

A
Anton Khirnov 已提交
960
        h->low_delay = get_bits1(&gb);
961

A
Anton Khirnov 已提交
962 963
        /* unknown field */
        skip_bits1(&gb);
964

A
Anton Khirnov 已提交
965 966
        while (get_bits1(&gb))
            skip_bits(&gb, 8);
967

A
Anton Khirnov 已提交
968 969 970
        s->unknown_flag  = get_bits1(&gb);
        avctx->has_b_frames = !h->low_delay;
        if (s->unknown_flag) {
971
#if CONFIG_ZLIB
A
Anton Khirnov 已提交
972 973 974 975 976 977 978 979 980 981 982
            unsigned watermark_width  = svq3_get_ue_golomb(&gb);
            unsigned watermark_height = svq3_get_ue_golomb(&gb);
            int u1                    = svq3_get_ue_golomb(&gb);
            int u2                    = get_bits(&gb, 8);
            int u3                    = get_bits(&gb, 2);
            int u4                    = svq3_get_ue_golomb(&gb);
            unsigned long buf_len     = watermark_width *
                                        watermark_height * 4;
            int offset                = get_bits_count(&gb) + 7 >> 3;
            uint8_t *buf;

M
Martin Storsjö 已提交
983 984
            if (watermark_height > 0 &&
                (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
A
Anton Khirnov 已提交
985
                return -1;
986

A
Anton Khirnov 已提交
987 988 989 990 991 992 993 994
            buf = av_malloc(buf_len);
            av_log(avctx, AV_LOG_DEBUG, "watermark size: %dx%d\n",
                   watermark_width, watermark_height);
            av_log(avctx, AV_LOG_DEBUG,
                   "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
                   u1, u2, u3, u4, offset);
            if (uncompress(buf, &buf_len, extradata + 8 + offset,
                           size - offset) != Z_OK) {
D
Diego Biurrun 已提交
995
                av_log(avctx, AV_LOG_ERROR,
A
Anton Khirnov 已提交
996 997
                       "could not uncompress watermark logo\n");
                av_free(buf);
998 999
                return -1;
            }
A
Anton Khirnov 已提交
1000 1001
            s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
            s->watermark_key = s->watermark_key << 16 | s->watermark_key;
A
Anton Khirnov 已提交
1002
            av_log(avctx, AV_LOG_DEBUG,
A
Anton Khirnov 已提交
1003
                   "watermark key %#x\n", s->watermark_key);
A
Anton Khirnov 已提交
1004 1005 1006 1007 1008 1009
            av_free(buf);
#else
            av_log(avctx, AV_LOG_ERROR,
                   "this svq3 file contains watermark which need zlib support compiled in\n");
            return -1;
#endif
1010
        }
A
Anton Khirnov 已提交
1011
    }
1012

A
Anton Khirnov 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021
    h->width  = avctx->width;
    h->height = avctx->height;
    h->mb_width  = (h->width + 15) / 16;
    h->mb_height = (h->height + 15) / 16;
    h->mb_stride = h->mb_width + 1;
    h->mb_num    = h->mb_width * h->mb_height;
    h->b_stride = 4 * h->mb_width;
    s->h_edge_pos = h->mb_width * 16;
    s->v_edge_pos = h->mb_height * 16;
1022

A
Anton Khirnov 已提交
1023 1024 1025
    if (ff_h264_alloc_tables(h) < 0) {
        av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
        return AVERROR(ENOMEM);
1026
    }
1027

1028 1029 1030
    return 0;
}

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
static void free_picture(AVCodecContext *avctx, Picture *pic)
{
    int i;
    for (i = 0; i < 2; i++) {
        av_buffer_unref(&pic->motion_val_buf[i]);
        av_buffer_unref(&pic->ref_index_buf[i]);
    }
    av_buffer_unref(&pic->mb_type_buf);

    av_frame_unref(&pic->f);
}

A
Anton Khirnov 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
static int get_buffer(AVCodecContext *avctx, Picture *pic)
{
    SVQ3Context *s = avctx->priv_data;
    H264Context *h = &s->h;
    const int big_mb_num    = h->mb_stride * (h->mb_height + 1) + 1;
    const int mb_array_size = h->mb_stride * h->mb_height;
    const int b4_stride     = h->mb_width * 4 + 1;
    const int b4_array_size = b4_stride * h->mb_height * 4;
    int ret;

1053
    if (!pic->motion_val_buf[0]) {
A
Anton Khirnov 已提交
1054 1055
        int i;

1056 1057
        pic->mb_type_buf = av_buffer_allocz((big_mb_num + h->mb_stride) * sizeof(uint32_t));
        if (!pic->mb_type_buf)
A
Anton Khirnov 已提交
1058
            return AVERROR(ENOMEM);
1059
        pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
A
Anton Khirnov 已提交
1060 1061

        for (i = 0; i < 2; i++) {
1062 1063 1064 1065 1066 1067
            pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
            pic->ref_index_buf[i]  = av_buffer_allocz(4 * mb_array_size);
            if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
                ret = AVERROR(ENOMEM);
                goto fail;
            }
A
Anton Khirnov 已提交
1068

1069 1070
            pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
            pic->ref_index[i]  = pic->ref_index_buf[i]->data;
A
Anton Khirnov 已提交
1071 1072
        }
    }
1073 1074 1075 1076 1077 1078
    pic->reference = !(h->pict_type == AV_PICTURE_TYPE_B);

    ret = ff_get_buffer(avctx, &pic->f,
                        pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
    if (ret < 0)
        goto fail;
A
Anton Khirnov 已提交
1079

1080 1081 1082 1083 1084
    if (!h->edge_emu_buffer) {
        h->edge_emu_buffer = av_mallocz(pic->f.linesize[0] * 17);
        if (!h->edge_emu_buffer)
            return AVERROR(ENOMEM);
    }
A
Anton Khirnov 已提交
1085 1086 1087 1088

    h->linesize   = pic->f.linesize[0];
    h->uvlinesize = pic->f.linesize[1];

1089 1090 1091
    return 0;
fail:
    free_picture(avctx, pic);
A
Anton Khirnov 已提交
1092 1093 1094
    return ret;
}

D
Diego Biurrun 已提交
1095
static int svq3_decode_frame(AVCodecContext *avctx, void *data,
1096
                             int *got_frame, AVPacket *avpkt)
1097
{
1098
    const uint8_t *buf = avpkt->data;
A
Anton Khirnov 已提交
1099 1100
    SVQ3Context *s     = avctx->priv_data;
    H264Context *h     = &s->h;
D
Diego Biurrun 已提交
1101
    int buf_size       = avpkt->size;
A
Anton Khirnov 已提交
1102
    int ret, m, i;
1103

1104 1105
    /* special case for last picture */
    if (buf_size == 0) {
A
Anton Khirnov 已提交
1106
        if (s->next_pic->f.data[0] && !h->low_delay && !s->last_frame_output) {
1107 1108 1109
            ret = av_frame_ref(data, &s->next_pic->f);
            if (ret < 0)
                return ret;
A
Anton Khirnov 已提交
1110
            s->last_frame_output = 1;
1111
            *got_frame          = 1;
1112 1113
        }
        return 0;
1114
    }
M
Michael Niedermayer 已提交
1115

A
Anton Khirnov 已提交
1116
    init_get_bits(&h->gb, buf, 8 * buf_size);
M
Michael Niedermayer 已提交
1117

A
Anton Khirnov 已提交
1118
    h->mb_x = h->mb_y = h->mb_xy = 0;
M
Michael Niedermayer 已提交
1119

1120
    if (svq3_decode_slice_header(avctx))
1121
        return -1;
M
Michael Niedermayer 已提交
1122

A
Anton Khirnov 已提交
1123
    h->pict_type = h->slice_type;
M
Michael Niedermayer 已提交
1124

A
Anton Khirnov 已提交
1125 1126 1127
    if (h->pict_type != AV_PICTURE_TYPE_B)
        FFSWAP(Picture*, s->next_pic, s->last_pic);

1128
    av_frame_unref(&s->cur_pic->f);
M
Michael Niedermayer 已提交
1129

1130
    /* for skipping the frame */
A
Anton Khirnov 已提交
1131 1132
    s->cur_pic->f.pict_type = h->pict_type;
    s->cur_pic->f.key_frame = (h->pict_type == AV_PICTURE_TYPE_I);
1133

A
Anton Khirnov 已提交
1134 1135 1136 1137 1138
    ret = get_buffer(avctx, s->cur_pic);
    if (ret < 0)
        return ret;

    h->cur_pic_ptr = s->cur_pic;
1139
    av_frame_unref(&h->cur_pic.f);
A
Anton Khirnov 已提交
1140
    h->cur_pic     = *s->cur_pic;
1141 1142 1143
    ret = av_frame_ref(&h->cur_pic.f, &s->cur_pic->f);
    if (ret < 0)
        return ret;
A
Anton Khirnov 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190

    for (i = 0; i < 16; i++) {
        h->block_offset[i]           = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
        h->block_offset[48 + i]      = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
    }
    for (i = 0; i < 16; i++) {
        h->block_offset[16 + i]      =
        h->block_offset[32 + i]      = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
        h->block_offset[48 + 16 + i] =
        h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
    }

    if (h->pict_type != AV_PICTURE_TYPE_I) {
        if (!s->last_pic->f.data[0]) {
            av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
            ret = get_buffer(avctx, s->last_pic);
            if (ret < 0)
                return ret;
            memset(s->last_pic->f.data[0], 0, avctx->height * s->last_pic->f.linesize[0]);
            memset(s->last_pic->f.data[1], 0x80, (avctx->height / 2) *
                   s->last_pic->f.linesize[1]);
            memset(s->last_pic->f.data[2], 0x80, (avctx->height / 2) *
                   s->last_pic->f.linesize[2]);
        }

        if (h->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f.data[0]) {
            av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
            ret = get_buffer(avctx, s->next_pic);
            if (ret < 0)
                return ret;
            memset(s->next_pic->f.data[0], 0, avctx->height * s->next_pic->f.linesize[0]);
            memset(s->next_pic->f.data[1], 0x80, (avctx->height / 2) *
                   s->next_pic->f.linesize[1]);
            memset(s->next_pic->f.data[2], 0x80, (avctx->height / 2) *
                   s->next_pic->f.linesize[2]);
        }
    }

    if (avctx->debug & FF_DEBUG_PICT_INFO)
        av_log(h->avctx, AV_LOG_DEBUG,
               "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
               av_get_picture_type_char(h->pict_type),
               s->halfpel_flag, s->thirdpel_flag,
               s->adaptive_quant, h->qscale, h->slice_num);

    if (avctx->skip_frame >= AVDISCARD_NONREF && h->pict_type == AV_PICTURE_TYPE_B ||
        avctx->skip_frame >= AVDISCARD_NONKEY && h->pict_type != AV_PICTURE_TYPE_I ||
1191
        avctx->skip_frame >= AVDISCARD_ALL)
1192 1193 1194
        return 0;

    if (s->next_p_frame_damaged) {
A
Anton Khirnov 已提交
1195
        if (h->pict_type == AV_PICTURE_TYPE_B)
1196 1197 1198 1199
            return 0;
        else
            s->next_p_frame_damaged = 0;
    }
1200

A
Anton Khirnov 已提交
1201
    if (h->pict_type == AV_PICTURE_TYPE_B) {
1202
        h->frame_num_offset = h->slice_num - h->prev_frame_num;
M
Michael Niedermayer 已提交
1203

D
Diego Biurrun 已提交
1204
        if (h->frame_num_offset < 0)
1205
            h->frame_num_offset += 256;
D
Diego Biurrun 已提交
1206 1207
        if (h->frame_num_offset == 0 ||
            h->frame_num_offset >= h->prev_frame_num_offset) {
A
Anton Khirnov 已提交
1208
            av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1209 1210 1211
            return -1;
        }
    } else {
D
Diego Biurrun 已提交
1212 1213
        h->prev_frame_num        = h->frame_num;
        h->frame_num             = h->slice_num;
1214
        h->prev_frame_num_offset = h->frame_num - h->prev_frame_num;
1215

D
Diego Biurrun 已提交
1216
        if (h->prev_frame_num_offset < 0)
1217
            h->prev_frame_num_offset += 256;
1218 1219
    }

D
Diego Biurrun 已提交
1220
    for (m = 0; m < 2; m++) {
1221
        int i;
D
Diego Biurrun 已提交
1222
        for (i = 0; i < 4; i++) {
1223 1224
            int j;
            for (j = -1; j < 4; j++)
D
Diego Biurrun 已提交
1225
                h->ref_cache[m][scan8[0] + 8 * i + j] = 1;
1226
            if (i < 3)
D
Diego Biurrun 已提交
1227
                h->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
1228
        }
1229 1230
    }

A
Anton Khirnov 已提交
1231 1232
    for (h->mb_y = 0; h->mb_y < h->mb_height; h->mb_y++) {
        for (h->mb_x = 0; h->mb_x < h->mb_width; h->mb_x++) {
1233
            unsigned mb_type;
A
Anton Khirnov 已提交
1234
            h->mb_xy = h->mb_x + h->mb_y * h->mb_stride;
1235

A
Anton Khirnov 已提交
1236 1237 1238 1239 1240
            if ((get_bits_count(&h->gb) + 7) >= h->gb.size_in_bits &&
                ((get_bits_count(&h->gb) & 7) == 0 ||
                 show_bits(&h->gb, -get_bits_count(&h->gb) & 7) == 0)) {
                skip_bits(&h->gb, s->next_slice_index - get_bits_count(&h->gb));
                h->gb.size_in_bits = 8 * buf_size;
1241

1242
                if (svq3_decode_slice_header(avctx))
1243
                    return -1;
1244

1245 1246
                /* TODO: support s->mb_skip_run */
            }
1247

A
Anton Khirnov 已提交
1248
            mb_type = svq3_get_ue_golomb(&h->gb);
1249

A
Anton Khirnov 已提交
1250
            if (h->pict_type == AV_PICTURE_TYPE_I)
1251
                mb_type += 8;
A
Anton Khirnov 已提交
1252
            else if (h->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
1253
                mb_type += 4;
A
Anton Khirnov 已提交
1254 1255 1256
            if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
                av_log(h->avctx, AV_LOG_ERROR,
                       "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
1257 1258
                return -1;
            }
M
Michael Niedermayer 已提交
1259

D
Diego Biurrun 已提交
1260 1261
            if (mb_type != 0)
                ff_h264_hl_decode_mb(h);
M
Michael Niedermayer 已提交
1262

A
Anton Khirnov 已提交
1263
            if (h->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
1264
                h->cur_pic.mb_type[h->mb_x + h->mb_y * h->mb_stride] =
A
Anton Khirnov 已提交
1265
                    (h->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
1266
        }
1267

1268 1269
        ff_draw_horiz_band(avctx, NULL, s->cur_pic, s->last_pic->f.data[0] ? s->last_pic : NULL,
                           16 * h->mb_y, 16, h->picture_structure, 0, 0,
A
Anton Khirnov 已提交
1270
                           h->low_delay, h->mb_height * 16, h->mb_width * 16);
M
Michael Niedermayer 已提交
1271
    }
M
Michael Niedermayer 已提交
1272

A
Anton Khirnov 已提交
1273
    if (h->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
1274 1275 1276 1277 1278
        ret = av_frame_ref(data, &s->cur_pic->f);
    else if (s->last_pic->f.data[0])
        ret = av_frame_ref(data, &s->last_pic->f);
    if (ret < 0)
        return ret;
1279

1280
    /* Do not output the last pic after seeking. */
A
Anton Khirnov 已提交
1281
    if (s->last_pic->f.data[0] || h->low_delay)
1282
        *got_frame = 1;
1283

A
Anton Khirnov 已提交
1284 1285
    if (h->pict_type != AV_PICTURE_TYPE_B) {
        FFSWAP(Picture*, s->cur_pic, s->next_pic);
1286 1287
    } else {
        av_frame_unref(&s->cur_pic->f);
A
Anton Khirnov 已提交
1288 1289
    }

1290
    return buf_size;
M
Michael Niedermayer 已提交
1291 1292
}

1293
static av_cold int svq3_decode_end(AVCodecContext *avctx)
1294
{
A
Anton Khirnov 已提交
1295 1296
    SVQ3Context *s = avctx->priv_data;
    H264Context *h = &s->h;
1297

A
Anton Khirnov 已提交
1298 1299 1300
    free_picture(avctx, s->cur_pic);
    free_picture(avctx, s->next_pic);
    free_picture(avctx, s->last_pic);
1301 1302 1303 1304 1305
    av_freep(&s->cur_pic);
    av_freep(&s->next_pic);
    av_freep(&s->last_pic);

    av_frame_unref(&h->cur_pic.f);
1306

A
Anton Khirnov 已提交
1307
    ff_h264_free_context(h);
1308 1309 1310

    return 0;
}
M
Michael Niedermayer 已提交
1311

1312
AVCodec ff_svq3_decoder = {
1313 1314
    .name           = "svq3",
    .type           = AVMEDIA_TYPE_VIDEO,
1315
    .id             = AV_CODEC_ID_SVQ3,
1316 1317 1318 1319
    .priv_data_size = sizeof(SVQ3Context),
    .init           = svq3_decode_init,
    .close          = svq3_decode_end,
    .decode         = svq3_decode_frame,
D
Diego Biurrun 已提交
1320 1321
    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND |
                      CODEC_CAP_DR1             |
1322 1323
                      CODEC_CAP_DELAY,
    .long_name      = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
D
Diego Biurrun 已提交
1324 1325
    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
                                                     AV_PIX_FMT_NONE},
M
Michael Niedermayer 已提交
1326
};