Revision 3b636f21

View differences:

doc/general.texi
441 441
    @tab fourcc: VP50
442 442
@item On2 VP6                @tab     @tab  X
443 443
    @tab fourcc: VP60,VP61,VP62
444
@item VP8                    @tab  X  @tab  X
445
    @tab fourcc: VP80, de/encoding supported through external library libvpx
444
@item VP8                    @tab  E  @tab  X
445
    @tab fourcc: VP80, encoding supported through external library libvpx
446 446
@item planar RGB             @tab     @tab  X
447 447
    @tab fourcc: 8BPS
448 448
@item Q-team QPEG            @tab     @tab  X
libavcodec/Makefile
375 375
                                          vp3dsp.o
376 376
OBJS-$(CONFIG_VP6_DECODER)             += vp6.o vp56.o vp56data.o vp56dsp.o \
377 377
                                          vp3dsp.o vp6dsp.o huffman.o
378
OBJS-$(CONFIG_VP8_DECODER)             += vp8.o vp8dsp.o vp56.o vp56data.o \
379
                                          h264pred.o
378 380
OBJS-$(CONFIG_VQA_DECODER)             += vqavideo.o
379 381
OBJS-$(CONFIG_WAVPACK_DECODER)         += wavpack.o
380 382
OBJS-$(CONFIG_WMAPRO_DECODER)          += wmaprodec.o wma.o
libavcodec/allcodecs.c
201 201
    REGISTER_DECODER (VP6, vp6);
202 202
    REGISTER_DECODER (VP6A, vp6a);
203 203
    REGISTER_DECODER (VP6F, vp6f);
204
    REGISTER_DECODER (VP8, vp8);
204 205
    REGISTER_DECODER (VQA, vqa);
205 206
    REGISTER_ENCDEC  (WMV1, wmv1);
206 207
    REGISTER_ENCDEC  (WMV2, wmv2);
libavcodec/avcodec.h
30 30
#include "libavutil/avutil.h"
31 31

  
32 32
#define LIBAVCODEC_VERSION_MAJOR 52
33
#define LIBAVCODEC_VERSION_MINOR 77
34
#define LIBAVCODEC_VERSION_MICRO  1
33
#define LIBAVCODEC_VERSION_MINOR 78
34
#define LIBAVCODEC_VERSION_MICRO  0
35 35

  
36 36
#define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
37 37
                                               LIBAVCODEC_VERSION_MINOR, \
libavcodec/dsputil.c
39 39
#include "ac3dec.h"
40 40
#include "vorbis.h"
41 41
#include "png.h"
42
#include "vp8dsp.h"
42 43

  
43 44
uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP] = {0, };
44 45
uint32_t ff_squareTbl[512] = {0, };
......
2656 2657
}
2657 2658
#endif /* CONFIG_RV40_DECODER */
2658 2659

  
2660
#if CONFIG_VP8_DECODER
2661
void ff_put_vp8_pixels16_c(uint8_t *dst, uint8_t *src, int stride, int h, int x, int y) {
2662
    put_pixels16_c(dst, src, stride, h);
2663
}
2664
void ff_put_vp8_pixels8_c(uint8_t *dst, uint8_t *src, int stride, int h, int x, int y) {
2665
    put_pixels8_c(dst, src, stride, h);
2666
}
2667
void ff_put_vp8_pixels4_c(uint8_t *dst, uint8_t *src, int stride, int h, int x, int y) {
2668
    put_pixels4_c(dst, src, stride, h);
2669
}
2670
#endif
2671

  
2659 2672
static void wmv2_mspel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int w){
2660 2673
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2661 2674
    int i;
libavcodec/vp56.h
237 237
    return bit;
238 238
}
239 239

  
240
// rounding is different than vp56_rac_get, is vp56_rac_get wrong?
241
static inline int vp8_rac_get(VP56RangeCoder *c)
242
{
243
    return vp56_rac_get_prob(c, 128);
244
}
245

  
240 246
static inline int vp56_rac_gets(VP56RangeCoder *c, int bits)
241 247
{
242 248
    int value = 0;
......
248 254
    return value;
249 255
}
250 256

  
257
static inline int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
258
{
259
    int value = 0;
260

  
261
    while (bits--) {
262
        value = (value << 1) | vp8_rac_get(c);
263
    }
264

  
265
    return value;
266
}
267

  
268
// fixme: add 1 bit to all the calls to this?
269
static inline int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
270
{
271
    int v;
272

  
273
    if (!vp8_rac_get(c))
274
        return 0;
275

  
276
    v = vp8_rac_get_uint(c, bits);
277

  
278
    if (vp8_rac_get(c))
279
        v = -v;
280

  
281
    return v;
282
}
283

  
284
// P(7)
251 285
static inline int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
252 286
{
253 287
    int v = vp56_rac_gets(c, 7) << 1;
254 288
    return v + !v;
255 289
}
256 290

  
291
static inline int vp8_rac_get_nn(VP56RangeCoder *c)
292
{
293
    int v = vp8_rac_get_uint(c, 7) << 1;
294
    return v + !v;
295
}
296

  
257 297
static inline int vp56_rac_get_tree(VP56RangeCoder *c,
258 298
                                    const VP56Tree *tree,
259 299
                                    const uint8_t *probs)
......
267 307
    return -tree->val;
268 308
}
269 309

  
310
/**
311
 * This is identical to vp8_rac_get_tree except for the possibility of starting
312
 * on a node other than the root node, needed for coeff decode where this is
313
 * used to save a bit after a 0 token (by disallowing EOB to immediately follow.)
314
 */
315
static inline int vp8_rac_get_tree_with_offset(VP56RangeCoder *c, const int8_t (*tree)[2],
316
                                               const uint8_t *probs, int i)
317
{
318
    do {
319
        i = tree[i][vp56_rac_get_prob(c, probs[i])];
320
    } while (i > 0);
321

  
322
    return -i;
323
}
324

  
325
// how probabilities are associated with decisions is different I think
326
// well, the new scheme fits in the old but this way has one fewer branches per decision
327
static inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
328
                                   const uint8_t *probs)
329
{
330
    return vp8_rac_get_tree_with_offset(c, tree, probs, 0);
331
}
332

  
333
// DCTextra
334
static inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
335
{
336
    int v = 0;
337

  
338
    do {
339
        v = (v<<1) + vp56_rac_get_prob(c, *prob++);
340
    } while (*prob);
341

  
342
    return v;
343
}
344

  
270 345
#endif /* AVCODEC_VP56_H */
libavcodec/vp8.c
1
/**
2
 * VP8 compatible video decoder
3
 *
4
 * Copyright (C) 2010 David Conrad
5
 * Copyright (C) 2010 Ronald S. Bultje
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23

  
24
#include "avcodec.h"
25
#include "vp56.h"
26
#include "vp8data.h"
27
#include "vp8dsp.h"
28
#include "h264pred.h"
29
#include "rectangle.h"
30

  
31
typedef struct {
32
    uint8_t segment;
33
    uint8_t skip;
34
    // todo: make it possible to check for at least (i4x4 or split_mv)
35
    // in one op. are others needed?
36
    uint8_t mode;
37
    uint8_t ref_frame;
38
    uint8_t partitioning;
39
    VP56mv mv;
40
    VP56mv bmv[16];
41
} VP8Macroblock;
42

  
43
typedef struct {
44
    AVCodecContext *avctx;
45
    DSPContext dsp;
46
    VP8DSPContext vp8dsp;
47
    H264PredContext hpc;
48
    AVFrame frames[4];
49
    AVFrame *framep[4];
50
    uint8_t *edge_emu_buffer;
51
    VP56RangeCoder c;   ///< header context, includes mb modes and motion vectors
52
    int profile;
53

  
54
    int mb_width;   /* number of horizontal MB */
55
    int mb_height;  /* number of vertical MB */
56
    int linesize;
57
    int uvlinesize;
58

  
59
    int keyframe;
60
    int invisible;
61
    int update_last;    ///< update VP56_FRAME_PREVIOUS with the current one
62
    int update_golden;  ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
63
    int update_altref;
64

  
65
    /**
66
     * If this flag is not set, all the probability updates
67
     * are discarded after this frame is decoded.
68
     */
69
    int update_probabilities;
70

  
71
    /**
72
     * All coefficients are contained in separate arith coding contexts.
73
     * There can be 1, 2, 4, or 8 of these after the header context.
74
     */
75
    int num_coeff_partitions;
76
    VP56RangeCoder coeff_partition[8];
77

  
78
    VP8Macroblock *macroblocks;
79
    VP8Macroblock *macroblocks_base;
80
    int mb_stride;
81

  
82
    uint8_t *intra4x4_pred_mode;
83
    uint8_t *intra4x4_pred_mode_base;
84
    int b4_stride;
85

  
86
    /**
87
     * For coeff decode, we need to know whether the above block had non-zero
88
     * coefficients. This means for each macroblock, we need data for 4 luma
89
     * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
90
     * per macroblock. We keep the last row in top_nnz.
91
     */
92
    uint8_t (*top_nnz)[9];
93
    DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
94

  
95
    /**
96
     * This is the index plus one of the last non-zero coeff
97
     * for each of the blocks in the current macroblock.
98
     * So, 0 -> no coeffs
99
     *     1 -> dc-only (special transform)
100
     *     2+-> full transform
101
     */
102
    DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
103
    DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
104

  
105
    int chroma_pred_mode;    ///< 8x8c pred mode of the current macroblock
106

  
107
    int mbskip_enabled;
108
    int sign_bias[4]; ///< one state [0, 1] per ref frame type
109

  
110
    /**
111
     * Base parameters for segmentation, i.e. per-macroblock parameters.
112
     * These must be kept unchanged even if segmentation is not used for
113
     * a frame, since the values persist between interframes.
114
     */
115
    struct {
116
        int enabled;
117
        int absolute_vals;
118
        int update_map;
119
        int8_t base_quant[4];
120
        int8_t filter_level[4];     ///< base loop filter level
121
    } segmentation;
122

  
123
    /**
124
     * Macroblocks can have one of 4 different quants in a frame when
125
     * segmentation is enabled.
126
     * If segmentation is disabled, only the first segment's values are used.
127
     */
128
    struct {
129
        // [0] - DC qmul  [1] - AC qmul
130
        int16_t luma_qmul[2];
131
        int16_t luma_dc_qmul[2];    ///< luma dc-only block quant
132
        int16_t chroma_qmul[2];
133
    } qmat[4];
134

  
135
    struct {
136
        int simple;
137
        int level;
138
        int sharpness;
139
    } filter;
140

  
141
    struct {
142
        int enabled;    ///< whether each mb can have a different strength based on mode/ref
143

  
144
        /**
145
         * filter strength adjustment for the following macroblock modes:
146
         * [0] - i4x4
147
         * [1] - zero mv
148
         * [2] - inter modes except for zero or split mv
149
         * [3] - split mv
150
         *  i16x16 modes never have any adjustment
151
         */
152
        int8_t mode[4];
153

  
154
        /**
155
         * filter strength adjustment for macroblocks that reference:
156
         * [0] - intra / VP56_FRAME_CURRENT
157
         * [1] - VP56_FRAME_PREVIOUS
158
         * [2] - VP56_FRAME_GOLDEN
159
         * [3] - altref / VP56_FRAME_GOLDEN2
160
         */
161
        int8_t ref[4];
162
    } lf_delta;
163

  
164
    /**
165
     * These are all of the updatable probabilities for binary decisions.
166
     * They are only implictly reset on keyframes, making it quite likely
167
     * for an interframe to desync if a prior frame's header was corrupt
168
     * or missing outright!
169
     */
170
    struct {
171
        uint8_t segmentid[3];
172
        uint8_t mbskip;
173
        uint8_t intra;
174
        uint8_t last;
175
        uint8_t golden;
176
        uint8_t pred16x16[4];
177
        uint8_t pred8x8c[3];
178
        uint8_t token[4][8][3][NUM_DCT_TOKENS-1];
179
        uint8_t mvc[2][19];
180
    } prob[2];
181
} VP8Context;
182

  
183
#define RL24(p) (AV_RL16(p) + ((p)[2] << 16))
184

  
185
static void vp8_decode_flush(AVCodecContext *avctx)
186
{
187
    VP8Context *s = avctx->priv_data;
188
    int i;
189

  
190
    for (i = 0; i < 4; i++)
191
        if (s->frames[i].data[0])
192
            avctx->release_buffer(avctx, &s->frames[i]);
193
    memset(s->framep, 0, sizeof(s->framep));
194

  
195
    av_freep(&s->macroblocks_base);
196
    av_freep(&s->intra4x4_pred_mode_base);
197
    av_freep(&s->top_nnz);
198
    av_freep(&s->edge_emu_buffer);
199

  
200
    s->macroblocks        = NULL;
201
    s->intra4x4_pred_mode = NULL;
202
}
203

  
204
static int update_dimensions(VP8Context *s, int width, int height)
205
{
206
    int i;
207

  
208
    if (avcodec_check_dimensions(s->avctx, width, height))
209
        return AVERROR_INVALIDDATA;
210

  
211
    vp8_decode_flush(s->avctx);
212

  
213
    avcodec_set_dimensions(s->avctx, width, height);
214

  
215
    s->mb_width  = (s->avctx->coded_width +15) / 16;
216
    s->mb_height = (s->avctx->coded_height+15) / 16;
217

  
218
    // we allocate a border around the top/left of intra4x4 modes
219
    // this is 4 blocks for intra4x4 to keep 4-byte alignment for fill_rectangle
220
    s->mb_stride = s->mb_width+1;
221
    s->b4_stride = 4*s->mb_stride;
222

  
223
    s->macroblocks_base        = av_mallocz(s->mb_stride*(s->mb_height+1)*sizeof(*s->macroblocks));
224
    s->intra4x4_pred_mode_base = av_mallocz(s->b4_stride*(4*s->mb_height+1));
225
    s->top_nnz                 = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
226

  
227
    s->macroblocks        = s->macroblocks_base        + 1 + s->mb_stride;
228
    s->intra4x4_pred_mode = s->intra4x4_pred_mode_base + 4 + s->b4_stride;
229

  
230
    memset(s->intra4x4_pred_mode_base, DC_PRED, s->b4_stride);
231
    for (i = 0; i < 4*s->mb_height; i++)
232
        s->intra4x4_pred_mode[i*s->b4_stride-1] = DC_PRED;
233

  
234
    return 0;
235
}
236

  
237
static void parse_segment_info(VP8Context *s)
238
{
239
    VP56RangeCoder *c = &s->c;
240
    int i;
241

  
242
    s->segmentation.update_map = vp8_rac_get(c);
243

  
244
    if (vp8_rac_get(c)) { // update segment feature data
245
        s->segmentation.absolute_vals = vp8_rac_get(c);
246

  
247
        for (i = 0; i < 4; i++)
248
            s->segmentation.base_quant[i]   = vp8_rac_get_sint(c, 7);
249

  
250
        for (i = 0; i < 4; i++)
251
            s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
252
    }
253
    if (s->segmentation.update_map)
254
        for (i = 0; i < 3; i++)
255
            s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
256
}
257

  
258
static void update_lf_deltas(VP8Context *s)
259
{
260
    VP56RangeCoder *c = &s->c;
261
    int i;
262

  
263
    for (i = 0; i < 4; i++)
264
        s->lf_delta.ref[i]  = vp8_rac_get_sint(c, 6);
265

  
266
    for (i = 0; i < 4; i++)
267
        s->lf_delta.mode[i] = vp8_rac_get_sint(c, 6);
268
}
269

  
270
static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
271
{
272
    const uint8_t *sizes = buf;
273
    int i;
274

  
275
    s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
276

  
277
    buf      += 3*(s->num_coeff_partitions-1);
278
    buf_size -= 3*(s->num_coeff_partitions-1);
279
    if (buf_size < 0)
280
        return -1;
281

  
282
    for (i = 0; i < s->num_coeff_partitions-1; i++) {
283
        int size = RL24(sizes + 3*i);
284
        if (buf_size - size < 0)
285
            return -1;
286

  
287
        vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
288
        buf      += size;
289
        buf_size -= size;
290
    }
291
    vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
292

  
293
    return 0;
294
}
295

  
296
static void get_quants(VP8Context *s)
297
{
298
    VP56RangeCoder *c = &s->c;
299
    int i, base_qi;
300

  
301
    int yac_qi     = vp8_rac_get_uint(c, 7);
302
    int ydc_delta  = vp8_rac_get_sint(c, 4);
303
    int y2dc_delta = vp8_rac_get_sint(c, 4);
304
    int y2ac_delta = vp8_rac_get_sint(c, 4);
305
    int uvdc_delta = vp8_rac_get_sint(c, 4);
306
    int uvac_delta = vp8_rac_get_sint(c, 4);
307

  
308
    for (i = 0; i < 4; i++) {
309
        if (s->segmentation.enabled) {
310
            base_qi = s->segmentation.base_quant[i];
311
            if (!s->segmentation.absolute_vals)
312
                base_qi += yac_qi;
313
        } else
314
            base_qi = yac_qi;
315

  
316
        s->qmat[i].luma_qmul[0]    =       vp8_dc_qlookup[av_clip(base_qi + ydc_delta , 0, 127)];
317
        s->qmat[i].luma_qmul[1]    =       vp8_ac_qlookup[av_clip(base_qi             , 0, 127)];
318
        s->qmat[i].luma_dc_qmul[0] =   2 * vp8_dc_qlookup[av_clip(base_qi + y2dc_delta, 0, 127)];
319
        s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip(base_qi + y2ac_delta, 0, 127)] / 100;
320
        s->qmat[i].chroma_qmul[0]  =       vp8_dc_qlookup[av_clip(base_qi + uvdc_delta, 0, 127)];
321
        s->qmat[i].chroma_qmul[1]  =       vp8_ac_qlookup[av_clip(base_qi + uvac_delta, 0, 127)];
322

  
323
        s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
324
        s->qmat[i].chroma_qmul[0]  = FFMIN(s->qmat[i].chroma_qmul[0], 132);
325
    }
326
}
327

  
328
/**
329
 * Determine which buffers golden and altref should be updated with after this frame.
330
 * The spec isn't clear here, so I'm going by my understanding of what libvpx does
331
 *
332
 * Intra frames update all 3 references
333
 * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
334
 * If the update (golden|altref) flag is set, it's updated with the current frame
335
 *      if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
336
 * If the flag is not set, the number read means:
337
 *      0: no update
338
 *      1: VP56_FRAME_PREVIOUS
339
 *      2: update golden with altref, or update altref with golden
340
 */
341
static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
342
{
343
    VP56RangeCoder *c = &s->c;
344

  
345
    if (update)
346
        return VP56_FRAME_CURRENT;
347

  
348
    switch (vp8_rac_get_uint(c, 2)) {
349
    case 1:
350
        return VP56_FRAME_PREVIOUS;
351
    case 2:
352
        return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
353
    }
354
    return VP56_FRAME_NONE;
355
}
356

  
357
static void update_refs(VP8Context *s)
358
{
359
    VP56RangeCoder *c = &s->c;
360

  
361
    int update_golden = vp8_rac_get(c);
362
    int update_altref = vp8_rac_get(c);
363

  
364
    s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
365
    s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
366
}
367

  
368
static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
369
{
370
    VP56RangeCoder *c = &s->c;
371
    int header_size, hscale, vscale, i, j, k, l, ret;
372
    int width  = s->avctx->width;
373
    int height = s->avctx->height;
374

  
375
    s->keyframe  = !(buf[0] & 1);
376
    s->profile   =  (buf[0]>>1) & 7;
377
    s->invisible = !(buf[0] & 0x10);
378
    header_size  = RL24(buf) >> 5;
379
    buf      += 3;
380
    buf_size -= 3;
381

  
382
    if (s->profile)
383
        av_log(s->avctx, AV_LOG_WARNING, "Profile %d not fully handled\n", s->profile);
384

  
385
    if (header_size > buf_size - 7*s->keyframe) {
386
        av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
387
        return AVERROR_INVALIDDATA;
388
    }
389

  
390
    if (s->keyframe) {
391
        if (RL24(buf) != 0x2a019d) {
392
            av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", RL24(buf));
393
            return AVERROR_INVALIDDATA;
394
        }
395
        width  = AV_RL16(buf+3) & 0x3fff;
396
        height = AV_RL16(buf+5) & 0x3fff;
397
        hscale = buf[4] >> 6;
398
        vscale = buf[6] >> 6;
399
        buf      += 7;
400
        buf_size -= 7;
401

  
402
        s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
403
        memcpy(s->prob->token    , vp8_token_default_probs , sizeof(s->prob->token));
404
        memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
405
        memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
406
        memcpy(s->prob->mvc      , vp8_mv_default_prob     , sizeof(s->prob->mvc));
407
        memset(&s->segmentation, 0, sizeof(s->segmentation));
408
    }
409

  
410
    if (!s->macroblocks_base || /* first frame */
411
        width != s->avctx->width || height != s->avctx->height) {
412
        if ((ret = update_dimensions(s, width, height) < 0))
413
            return ret;
414
    }
415

  
416
    vp56_init_range_decoder(c, buf, header_size);
417
    buf      += header_size;
418
    buf_size -= header_size;
419

  
420
    if (s->keyframe) {
421
        if (vp8_rac_get(c))
422
            av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
423
        vp8_rac_get(c); // whether we can skip clamping in dsp functions
424
    }
425

  
426
    if ((s->segmentation.enabled = vp8_rac_get(c)))
427
        parse_segment_info(s);
428
    else
429
        s->segmentation.update_map = 0; // FIXME: move this to some init function?
430

  
431
    s->filter.simple    = vp8_rac_get(c);
432
    s->filter.level     = vp8_rac_get_uint(c, 6);
433
    s->filter.sharpness = vp8_rac_get_uint(c, 3);
434

  
435
    if ((s->lf_delta.enabled = vp8_rac_get(c)))
436
        if (vp8_rac_get(c))
437
            update_lf_deltas(s);
438

  
439
    if (setup_partitions(s, buf, buf_size)) {
440
        av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
441
        return AVERROR_INVALIDDATA;
442
    }
443

  
444
    get_quants(s);
445

  
446
    if (!s->keyframe) {
447
        update_refs(s);
448
        s->sign_bias[VP56_FRAME_GOLDEN]               = vp8_rac_get(c);
449
        s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
450
    }
451

  
452
    // if we aren't saving this frame's probabilities for future frames,
453
    // make a copy of the current probabilities
454
    if (!(s->update_probabilities = vp8_rac_get(c)))
455
        s->prob[1] = s->prob[0];
456

  
457
    s->update_last = s->keyframe || vp8_rac_get(c);
458

  
459
    for (i = 0; i < 4; i++)
460
        for (j = 0; j < 8; j++)
461
            for (k = 0; k < 3; k++)
462
                for (l = 0; l < NUM_DCT_TOKENS-1; l++)
463
                    if (vp56_rac_get_prob(c, vp8_token_update_probs[i][j][k][l]))
464
                        s->prob->token[i][j][k][l] = vp8_rac_get_uint(c, 8);
465

  
466
    if ((s->mbskip_enabled = vp8_rac_get(c)))
467
        s->prob->mbskip = vp8_rac_get_uint(c, 8);
468

  
469
    if (!s->keyframe) {
470
        s->prob->intra  = vp8_rac_get_uint(c, 8);
471
        s->prob->last   = vp8_rac_get_uint(c, 8);
472
        s->prob->golden = vp8_rac_get_uint(c, 8);
473

  
474
        if (vp8_rac_get(c))
475
            for (i = 0; i < 4; i++)
476
                s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
477
        if (vp8_rac_get(c))
478
            for (i = 0; i < 3; i++)
479
                s->prob->pred8x8c[i]  = vp8_rac_get_uint(c, 8);
480

  
481
        // 17.2 MV probability update
482
        for (i = 0; i < 2; i++)
483
            for (j = 0; j < 19; j++)
484
                if (vp56_rac_get_prob(c, vp8_mv_update_prob[i][j]))
485
                    s->prob->mvc[i][j] = vp8_rac_get_nn(c);
486
    }
487

  
488
    return 0;
489
}
490

  
491
static inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src,
492
                            int mb_x, int mb_y)
493
{
494
#define MARGIN (16 << 2)
495
    dst->x = av_clip(src->x, -((mb_x << 6) + MARGIN),
496
                     ((s->mb_width  - 1 - mb_x) << 6) + MARGIN);
497
    dst->y = av_clip(src->y, -((mb_y << 6) + MARGIN),
498
                     ((s->mb_height - 1 - mb_y) << 6) + MARGIN);
499
}
500

  
501
static void find_near_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
502
                          VP56mv near[2], VP56mv *best, int cnt[4])
503
{
504
    VP8Macroblock *mb_edge[3] = { mb - s->mb_stride     /* top */,
505
                                  mb - 1                /* left */,
506
                                  mb - s->mb_stride - 1 /* top-left */ };
507
    enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT };
508
    VP56mv near_mv[4]  = {{ 0 }};
509
    enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
510
    int idx = CNT_ZERO, n;
511
    int best_idx = CNT_ZERO;
512

  
513
    /* Process MB on top, left and top-left */
514
    for (n = 0; n < 3; n++) {
515
        VP8Macroblock *edge = mb_edge[n];
516
        if (edge->ref_frame != VP56_FRAME_CURRENT) {
517
            if (edge->mv.x | edge->mv.y) {
518
                VP56mv tmp = edge->mv;
519
                if (s->sign_bias[mb->ref_frame] != s->sign_bias[edge->ref_frame]) {
520
                    tmp.x *= -1;
521
                    tmp.y *= -1;
522
                }
523
                if ((tmp.x ^ near_mv[idx].x) | (tmp.y ^ near_mv[idx].y))
524
                    near_mv[++idx] = tmp;
525
                cnt[idx]       += 1 + (n != 2);
526
            } else
527
                cnt[CNT_ZERO] += 1 + (n != 2);
528
        }
529
    }
530

  
531
    /* If we have three distinct MV's, merge first and last if they're the same */
532
    if (cnt[CNT_SPLITMV] &&
533
        !((near_mv[1+EDGE_TOP].x ^ near_mv[1+EDGE_TOPLEFT].x) |
534
          (near_mv[1+EDGE_TOP].y ^ near_mv[1+EDGE_TOPLEFT].y)))
535
        cnt[CNT_NEAREST] += 1;
536

  
537
    cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode   == VP8_MVMODE_SPLIT) +
538
                        (mb_edge[EDGE_TOP]->mode    == VP8_MVMODE_SPLIT)) * 2 +
539
                       (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
540

  
541
    /* Swap near and nearest if necessary */
542
    if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
543
        FFSWAP(int,    cnt[CNT_NEAREST],     cnt[CNT_NEAR]);
544
        FFSWAP(VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
545
    }
546

  
547
    /* Choose the best mv out of 0,0 and the nearest mv */
548
    if (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])
549
        best_idx = CNT_NEAREST;
550

  
551
    clamp_mv(s, best, &near_mv[best_idx], mb_x, mb_y);
552
    near[0] = near_mv[CNT_NEAREST];
553
    near[1] = near_mv[CNT_NEAR];
554
}
555

  
556
/**
557
 * Motion vector coding, 17.1.
558
 */
559
static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
560
{
561
    int x = 0;
562

  
563
    if (vp56_rac_get_prob(c, p[0])) {
564
        int i;
565

  
566
        for (i = 0; i < 3; i++)
567
            x += vp56_rac_get_prob(c, p[9 + i]) << i;
568
        for (i = 9; i > 3; i--)
569
            x += vp56_rac_get_prob(c, p[9 + i]) << i;
570
        if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
571
            x += 8;
572
    } else
573
        x = vp8_rac_get_tree(c, vp8_small_mvtree, &p[2]);
574

  
575
    return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
576
}
577

  
578
static const uint8_t *get_submv_prob(const VP56mv *left, const VP56mv *top)
579
{
580
    int l_is_zero = !(left->x | left->y);
581
    int t_is_zero = !(top->x  | top->y);
582
    int equal = !((left->x ^ top->x) | (left->y ^ top->y));
583

  
584
    if (equal)
585
        return l_is_zero ? vp8_submv_prob[4] : vp8_submv_prob[3];
586
    if (t_is_zero)
587
        return vp8_submv_prob[2];
588
    return l_is_zero ? vp8_submv_prob[1] : vp8_submv_prob[0];
589
}
590

  
591
/**
592
 * Split motion vector prediction, 16.4.
593
 */
594
static void decode_splitmvs(VP8Context    *s,  VP56RangeCoder *c,
595
                            VP8Macroblock *mb, VP56mv         *base_mv)
596
{
597
    int part_idx = mb->partitioning =
598
        vp8_rac_get_tree(c, vp8_mbsplit_tree, vp8_mbsplit_prob);
599
    int n, num = vp8_mbsplit_count[part_idx];
600
    VP56mv part_mv[16];
601

  
602
    for (n = 0; n < num; n++) {
603
        int k = vp8_mbfirstidx[part_idx][n];
604
        const VP56mv *left  = (k & 3) ? &mb->bmv[k - 1] : &mb[-1].bmv[k + 3],
605
                     *above = (k > 3) ? &mb->bmv[k - 4] : &mb[-s->mb_stride].bmv[k + 12];
606
        const uint8_t *submv_prob = get_submv_prob(left, above);
607

  
608
        switch (vp8_rac_get_tree(c, vp8_submv_ref_tree, submv_prob)) {
609
        case VP8_SUBMVMODE_NEW4X4:
610
            part_mv[n].y = base_mv->y + read_mv_component(c, s->prob->mvc[0]);
611
            part_mv[n].x = base_mv->x + read_mv_component(c, s->prob->mvc[1]);
612
            break;
613
        case VP8_SUBMVMODE_ZERO4X4:
614
            part_mv[n].x = 0;
615
            part_mv[n].y = 0;
616
            break;
617
        case VP8_SUBMVMODE_LEFT4X4:
618
            part_mv[n] = *left;
619
            break;
620
        case VP8_SUBMVMODE_TOP4X4:
621
            part_mv[n] = *above;
622
            break;
623
        }
624

  
625
        /* fill out over the 4x4 blocks in MB */
626
        for (k = 0; k < 16; k++)
627
            if (vp8_mbsplits[part_idx][k] == n) {
628
                mb->bmv[k]      = part_mv[n];
629
            }
630
    }
631
}
632

  
633
static inline void decode_intra4x4_modes(VP56RangeCoder *c, uint8_t *intra4x4,
634
                                         int stride, int keyframe)
635
{
636
    int x, y, t, l;
637
    const uint8_t *ctx = vp8_pred4x4_prob_inter;
638

  
639
    for (y = 0; y < 4; y++) {
640
        for (x = 0; x < 4; x++) {
641
            if (keyframe) {
642
                t = intra4x4[x - stride];
643
                l = intra4x4[x - 1];
644
                ctx = vp8_pred4x4_prob_intra[t][l];
645
            }
646
            intra4x4[x] = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
647
        }
648
        intra4x4 += stride;
649
    }
650
}
651

  
652
static void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
653
                           uint8_t *intra4x4)
654
{
655
    VP56RangeCoder *c = &s->c;
656
    int n;
657

  
658
    if (s->segmentation.update_map)
659
        mb->segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
660

  
661
    mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
662

  
663
    if (s->keyframe) {
664
        mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
665

  
666
        if (mb->mode == MODE_I4x4) {
667
            decode_intra4x4_modes(c, intra4x4, s->b4_stride, 1);
668
        } else
669
            fill_rectangle(intra4x4, 4, 4, s->b4_stride, vp8_pred4x4_mode[mb->mode], 1);
670

  
671
        s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
672
        mb->ref_frame = VP56_FRAME_CURRENT;
673
    } else if (vp56_rac_get_prob(c, s->prob->intra)) {
674
        VP56mv near[2], best;
675
        int cnt[4] = { 0 };
676
        uint8_t p[4];
677

  
678
        // inter MB, 16.2
679
        if (vp56_rac_get_prob(c, s->prob->last))
680
            mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
681
                VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
682
        else
683
            mb->ref_frame = VP56_FRAME_PREVIOUS;
684

  
685
        // motion vectors, 16.3
686
        find_near_mvs(s, mb, mb_x, mb_y, near, &best, cnt);
687
        for (n = 0; n < 4; n++)
688
            p[n] = vp8_mode_contexts[cnt[n]][n];
689
        mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_mvinter, p);
690
        switch (mb->mode) {
691
        case VP8_MVMODE_SPLIT:
692
            decode_splitmvs(s, c, mb, &best);
693
            mb->mv = mb->bmv[15];
694
            break;
695
        case VP8_MVMODE_ZERO:
696
            mb->mv.x = 0;
697
            mb->mv.y = 0;
698
            break;
699
        case VP8_MVMODE_NEAREST:
700
            clamp_mv(s, &mb->mv, &near[0], mb_x, mb_y);
701
            break;
702
        case VP8_MVMODE_NEAR:
703
            clamp_mv(s, &mb->mv, &near[1], mb_x, mb_y);
704
            break;
705
        case VP8_MVMODE_NEW:
706
            mb->mv.y = best.y + read_mv_component(c, s->prob->mvc[0]);
707
            mb->mv.x = best.x + read_mv_component(c, s->prob->mvc[1]);
708
            break;
709
        }
710
        if (mb->mode != VP8_MVMODE_SPLIT) {
711
            for (n = 0; n < 16; n++)
712
                mb->bmv[n] = mb->mv;
713
        }
714
    } else {
715
        // intra MB, 16.1
716
        mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
717

  
718
        if (mb->mode == MODE_I4x4) {
719
            decode_intra4x4_modes(c, intra4x4, s->b4_stride, 0);
720
        } else
721
            fill_rectangle(intra4x4, 4, 4, s->b4_stride, vp8_pred4x4_mode[mb->mode], 1);
722

  
723
        s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
724
        mb->ref_frame = VP56_FRAME_CURRENT;
725
    }
726
}
727

  
728
/**
729
 * @param i initial coeff index, 0 unless a separate DC block is coded
730
 * @param zero_nhood the initial prediction context for number of surrounding
731
 *                   all-zero blocks (only left/top, so 0-2)
732
 * @param qmul[0] dc dequant factor
733
 * @param qmul[1] ac dequant factor
734
 * @return 0 if no coeffs were decoded
735
 *         otherwise, the index of the last coeff decoded plus one
736
 */
737
static int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
738
                               uint8_t probs[8][3][NUM_DCT_TOKENS-1],
739
                               int i, int zero_nhood, int16_t qmul[2])
740
{
741
    int token, nonzero = 0;
742
    int offset = 0;
743

  
744
    for (; i < 16; i++) {
745
        token = vp8_rac_get_tree_with_offset(c, vp8_coeff_tree, probs[vp8_coeff_band[i]][zero_nhood], offset);
746

  
747
        if (token == DCT_EOB)
748
            break;
749
        else if (token >= DCT_CAT1) {
750
            int cat = token-DCT_CAT1;
751
            token = vp8_rac_get_coeff(c, vp8_dct_cat_prob[cat]);
752
            token += vp8_dct_cat_offset[cat];
753
        }
754

  
755
        // after the first token, the non-zero prediction context becomes
756
        // based on the last decoded coeff
757
        if (!token) {
758
            zero_nhood = 0;
759
            offset = 1;
760
            continue;
761
        } else if (token == 1)
762
            zero_nhood = 1;
763
        else
764
            zero_nhood = 2;
765

  
766
        // todo: full [16] qmat? load into register?
767
        block[zigzag_scan[i]] = (vp8_rac_get(c) ? -token : token) * qmul[!!i];
768
        nonzero = i+1;
769
        offset = 0;
770
    }
771
    return nonzero;
772
}
773

  
774
static void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
775
                             uint8_t t_nnz[9], uint8_t l_nnz[9])
776
{
777
    LOCAL_ALIGNED_16(DCTELEM, dc,[16]);
778
    int i, x, y, luma_start = 0, luma_ctx = 3;
779
    int nnz_pred, nnz, nnz_total = 0;
780
    int segment = s->segmentation.enabled ? mb->segment : 0;
781

  
782
    s->dsp.clear_blocks((DCTELEM *)s->block);
783

  
784
    if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
785
        AV_ZERO128(dc);
786
        AV_ZERO128(dc+8);
787
        nnz_pred = t_nnz[8] + l_nnz[8];
788

  
789
        // decode DC values and do hadamard
790
        nnz = decode_block_coeffs(c, dc, s->prob->token[1], 0, nnz_pred,
791
                                  s->qmat[segment].luma_dc_qmul);
792
        l_nnz[8] = t_nnz[8] = !!nnz;
793
        nnz_total += nnz;
794
        s->vp8dsp.vp8_luma_dc_wht(s->block, dc);
795
        luma_start = 1;
796
        luma_ctx = 0;
797
    }
798

  
799
    // luma blocks
800
    for (y = 0; y < 4; y++)
801
        for (x = 0; x < 4; x++) {
802
            nnz_pred = l_nnz[y] + t_nnz[x];
803
            nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
804
                                      nnz_pred, s->qmat[segment].luma_qmul);
805
            // nnz+luma_start may be one more than the actual last index, but we don't care
806
            s->non_zero_count_cache[y][x] = nnz + luma_start;
807
            t_nnz[x] = l_nnz[y] = !!nnz;
808
            nnz_total += nnz;
809
        }
810

  
811
    // chroma blocks
812
    // TODO: what to do about dimensions? 2nd dim for luma is x,
813
    // but for chroma it's (y<<1)|x
814
    for (i = 4; i < 6; i++)
815
        for (y = 0; y < 2; y++)
816
            for (x = 0; x < 2; x++) {
817
                nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
818
                nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
819
                                          nnz_pred, s->qmat[segment].chroma_qmul);
820
                s->non_zero_count_cache[i][(y<<1)+x] = nnz;
821
                t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
822
                nnz_total += nnz;
823
            }
824

  
825
    // if there were no coded coeffs despite the macroblock not being marked skip,
826
    // we MUST not do the inner loop filter and should not do IDCT
827
    // Since skip isn't used for bitstream prediction, just manually set it.
828
    if (!nnz_total)
829
        mb->skip = 1;
830
}
831

  
832
static int check_intra_pred_mode(int mode, int mb_x, int mb_y)
833
{
834
    if (mode == DC_PRED8x8) {
835
        if (!(mb_x|mb_y))
836
            mode = DC_128_PRED8x8;
837
        else if (!mb_y)
838
            mode = LEFT_DC_PRED8x8;
839
        else if (!mb_x)
840
            mode = TOP_DC_PRED8x8;
841
    }
842
    return mode;
843
}
844

  
845
static void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
846
                          uint8_t *bmode, int mb_x, int mb_y)
847
{
848
    int x, y, mode, nnz, tr;
849

  
850
    if (mb->mode < MODE_I4x4) {
851
        mode = check_intra_pred_mode(mb->mode, mb_x, mb_y);
852
        s->hpc.pred16x16[mode](dst[0], s->linesize);
853
    } else {
854
        uint8_t *ptr = dst[0];
855

  
856
        // all blocks on the right edge of the macroblock use bottom edge
857
        // the top macroblock for their topright edge
858
        uint8_t *tr_right = ptr - s->linesize + 16;
859

  
860
        // if we're on the right edge of the frame, said edge is extended
861
        // from the top macroblock
862
        if (mb_x == s->mb_width-1) {
863
            tr = tr_right[-1]*0x01010101;
864
            tr_right = (uint8_t *)&tr;
865
        }
866

  
867
        for (y = 0; y < 4; y++) {
868
            uint8_t *topright = ptr + 4 - s->linesize;
869
            for (x = 0; x < 4; x++) {
870
                if (x == 3)
871
                    topright = tr_right;
872

  
873
                s->hpc.pred4x4[bmode[x]](ptr+4*x, topright, s->linesize);
874

  
875
                nnz = s->non_zero_count_cache[y][x];
876
                if (nnz) {
877
                    if (nnz == 1)
878
                        s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
879
                    else
880
                        s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
881
                }
882
                topright += 4;
883
            }
884

  
885
            ptr   += 4*s->linesize;
886
            bmode += s->b4_stride;
887
        }
888
    }
889

  
890
    mode = check_intra_pred_mode(s->chroma_pred_mode, mb_x, mb_y);
891
    s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
892
    s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
893
}
894

  
895
/**
896
 * Generic MC function.
897
 *
898
 * @param s VP8 decoding context
899
 * @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
900
 * @param dst target buffer for block data at block position
901
 * @param src reference picture buffer at origin (0, 0)
902
 * @param mv motion vector (relative to block position) to get pixel data from
903
 * @param x_off horizontal position of block from origin (0, 0)
904
 * @param y_off vertical position of block from origin (0, 0)
905
 * @param block_w width of block (16, 8 or 4)
906
 * @param block_h height of block (always same as block_w)
907
 * @param width width of src/dst plane data
908
 * @param height height of src/dst plane data
909
 * @param linesize size of a single line of plane data, including padding
910
 */
911
static inline void vp8_mc(VP8Context *s, int luma,
912
                          uint8_t *dst, uint8_t *src, const VP56mv *mv,
913
                          int x_off, int y_off, int block_w, int block_h,
914
                          int width, int height, int linesize,
915
                          h264_chroma_mc_func mc_func[3][3])
916
{
917
    static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };
918
    int mx = (mv->x << luma)&7, mx_idx = idx[mx];
919
    int my = (mv->y << luma)&7, my_idx = idx[my];
920

  
921
    x_off += mv->x >> (3 - luma);
922
    y_off += mv->y >> (3 - luma);
923

  
924
    // edge emulation
925
    src += y_off * linesize + x_off;
926
    if (x_off < 2 || x_off >= width  - block_w - 3 ||
927
        y_off < 2 || y_off >= height - block_h - 3) {
928
        ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,
929
                            block_w + 5, block_h + 5,
930
                            x_off - 2, y_off - 2, width, height);
931
        src = s->edge_emu_buffer + 2 + linesize * 2;
932
    }
933

  
934
    mc_func[my_idx][mx_idx](dst, src, linesize, block_h, mx, my);
935
}
936

  
937
/**
938
 * Apply motion vectors to prediction buffer, chapter 18.
939
 */
940
static void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
941
                          int mb_x, int mb_y)
942
{
943
    int x_off = mb_x << 4, y_off = mb_y << 4;
944
    int width = 16*s->mb_width, height = 16*s->mb_height;
945
    VP56mv uvmv;
946

  
947
    if (mb->mode < VP8_MVMODE_SPLIT) {
948
        /* Y */
949
        vp8_mc(s, 1, dst[0], s->framep[mb->ref_frame]->data[0], &mb->mv,
950
               x_off, y_off, 16, 16, width, height, s->linesize,
951
               s->vp8dsp.put_vp8_epel_pixels_tab[0]);
952

  
953
        /* U/V */
954
        uvmv = mb->mv;
955
        if (s->profile == 3) {
956
            uvmv.x &= ~7;
957
            uvmv.y &= ~7;
958
        }
959
        x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
960
        vp8_mc(s, 0, dst[1], s->framep[mb->ref_frame]->data[1], &uvmv,
961
               x_off, y_off, 8, 8, width, height, s->uvlinesize,
962
               s->vp8dsp.put_vp8_epel_pixels_tab[1]);
963
        vp8_mc(s, 0, dst[2], s->framep[mb->ref_frame]->data[2], &uvmv,
964
               x_off, y_off, 8, 8, width, height, s->uvlinesize,
965
               s->vp8dsp.put_vp8_epel_pixels_tab[1]);
966
    } else {
967
        int x, y;
968

  
969
        /* Y */
970
        for (y = 0; y < 4; y++) {
971
            for (x = 0; x < 4; x++) {
972
                vp8_mc(s, 1, dst[0] + 4*y*s->linesize + x*4,
973
                       s->framep[mb->ref_frame]->data[0], &mb->bmv[4*y + x],
974
                       4*x + x_off, 4*y + y_off, 4, 4,
975
                       width, height, s->linesize,
976
                       s->vp8dsp.put_vp8_epel_pixels_tab[2]);
977
            }
978
        }
979

  
980
        /* U/V */
981
        x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
982
        for (y = 0; y < 2; y++) {
983
            for (x = 0; x < 2; x++) {
984
                uvmv.x = mb->bmv[ 2*y    * 4 + 2*x  ].x +
985
                         mb->bmv[ 2*y    * 4 + 2*x+1].x +
986
                         mb->bmv[(2*y+1) * 4 + 2*x  ].x +
987
                         mb->bmv[(2*y+1) * 4 + 2*x+1].x;
988
                uvmv.y = mb->bmv[ 2*y    * 4 + 2*x  ].y +
989
                         mb->bmv[ 2*y    * 4 + 2*x+1].y +
990
                         mb->bmv[(2*y+1) * 4 + 2*x  ].y +
991
                         mb->bmv[(2*y+1) * 4 + 2*x+1].y;
992
                uvmv.x = (uvmv.x + (uvmv.x < 0 ? -2 : 2)) / 4;
993
                uvmv.y = (uvmv.y + (uvmv.y < 0 ? -2 : 2)) / 4;
994
                if (s->profile == 3) {
995
                    uvmv.x &= ~7;
996
                    uvmv.y &= ~7;
997
                }
998
                vp8_mc(s, 0, dst[1] + 4*y*s->uvlinesize + x*4,
999
                       s->framep[mb->ref_frame]->data[1], &uvmv,
1000
                       4*x + x_off, 4*y + y_off, 4, 4,
1001
                       width, height, s->uvlinesize,
1002
                       s->vp8dsp.put_vp8_epel_pixels_tab[2]);
1003
                vp8_mc(s, 0, dst[2] + 4*y*s->uvlinesize + x*4,
1004
                       s->framep[mb->ref_frame]->data[2], &uvmv,
1005
                       4*x + x_off, 4*y + y_off, 4, 4,
1006
                       width, height, s->uvlinesize,
1007
                       s->vp8dsp.put_vp8_epel_pixels_tab[2]);
1008
            }
1009
        }
1010
    }
1011
}
1012

  
1013
static void idct_mb(VP8Context *s, uint8_t *y_dst, uint8_t *u_dst, uint8_t *v_dst,
1014
                    VP8Macroblock *mb)
1015
{
1016
    int x, y, nnz;
1017

  
1018
    if (mb->mode != MODE_I4x4)
1019
        for (y = 0; y < 4; y++) {
1020
            for (x = 0; x < 4; x++) {
1021
                nnz = s->non_zero_count_cache[y][x];
1022
                if (nnz) {
1023
                    if (nnz == 1)
1024
                        s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
1025
                    else
1026
                        s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
1027
                }
1028
            }
1029
            y_dst += 4*s->linesize;
1030
        }
1031

  
1032
    for (y = 0; y < 2; y++) {
1033
        for (x = 0; x < 2; x++) {
1034
            nnz = s->non_zero_count_cache[4][(y<<1)+x];
1035
            if (nnz) {
1036
                if (nnz == 1)
1037
                    s->vp8dsp.vp8_idct_dc_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1038
                else
1039
                    s->vp8dsp.vp8_idct_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1040
            }
1041

  
1042
            nnz = s->non_zero_count_cache[5][(y<<1)+x];
1043
            if (nnz) {
1044
                if (nnz == 1)
1045
                    s->vp8dsp.vp8_idct_dc_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1046
                else
1047
                    s->vp8dsp.vp8_idct_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1048
            }
1049
        }
1050
        u_dst += 4*s->uvlinesize;
1051
        v_dst += 4*s->uvlinesize;
1052
    }
1053
}
1054

  
1055
static void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, int *level, int *inner, int *hev_thresh)
1056
{
1057
    int interior_limit, filter_level;
1058

  
1059
    if (s->segmentation.enabled) {
1060
        filter_level = s->segmentation.filter_level[mb->segment];
1061
        if (!s->segmentation.absolute_vals)
1062
            filter_level += s->filter.level;
1063
    } else
1064
        filter_level = s->filter.level;
1065

  
1066
    if (s->lf_delta.enabled) {
1067
        filter_level += s->lf_delta.ref[mb->ref_frame];
1068

  
1069
        if (mb->ref_frame == VP56_FRAME_CURRENT) {
1070
            if (mb->mode == MODE_I4x4)
1071
                filter_level += s->lf_delta.mode[0];
1072
        } else {
1073
            if (mb->mode == VP8_MVMODE_ZERO)
1074
                filter_level += s->lf_delta.mode[1];
1075
            else if (mb->mode == VP8_MVMODE_SPLIT)
1076
                filter_level += s->lf_delta.mode[3];
1077
            else
1078
                filter_level += s->lf_delta.mode[2];
1079
        }
1080
    }
1081
    filter_level = av_clip(filter_level, 0, 63);
1082

  
1083
    interior_limit = filter_level;
1084
    if (s->filter.sharpness) {
1085
        interior_limit >>= s->filter.sharpness > 4 ? 2 : 1;
1086
        interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
1087
    }
1088
    interior_limit = FFMAX(interior_limit, 1);
1089

  
1090
    *level = filter_level;
1091
    *inner = interior_limit;
1092

  
1093
    if (hev_thresh) {
1094
        *hev_thresh = filter_level >= 15;
1095

  
1096
        if (s->keyframe) {
1097
            if (filter_level >= 40)
1098
                *hev_thresh = 2;
1099
        } else {
1100
            if (filter_level >= 40)
1101
                *hev_thresh = 3;
1102
            else if (filter_level >= 20)
1103
                *hev_thresh = 2;
1104
        }
1105
    }
1106
}
1107

  
1108
// TODO: look at backup_mb_border / xchg_mb_border in h264.c
1109
static void filter_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb, int mb_x, int mb_y)
1110
{
1111
    int filter_level, inner_limit, hev_thresh;
1112

  
1113
    filter_level_for_mb(s, mb, &filter_level, &inner_limit, &hev_thresh);
1114
    if (!filter_level)
1115
        return;
1116

  
1117
    if (mb_x) {
1118
        s->vp8dsp.vp8_h_loop_filter16(dst[0], s->linesize,   filter_level+2, inner_limit, hev_thresh);
1119
        s->vp8dsp.vp8_h_loop_filter8 (dst[1], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
1120
        s->vp8dsp.vp8_h_loop_filter8 (dst[2], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
1121
    }
1122

  
1123
    if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1124
        s->vp8dsp.vp8_h_loop_filter16_inner(dst[0]+ 4, s->linesize,   filter_level, inner_limit, hev_thresh);
1125
        s->vp8dsp.vp8_h_loop_filter16_inner(dst[0]+ 8, s->linesize,   filter_level, inner_limit, hev_thresh);
1126
        s->vp8dsp.vp8_h_loop_filter16_inner(dst[0]+12, s->linesize,   filter_level, inner_limit, hev_thresh);
1127
        s->vp8dsp.vp8_h_loop_filter8_inner (dst[1]+ 4, s->uvlinesize, filter_level, inner_limit, hev_thresh);
1128
        s->vp8dsp.vp8_h_loop_filter8_inner (dst[2]+ 4, s->uvlinesize, filter_level, inner_limit, hev_thresh);
1129
    }
1130

  
1131
    if (mb_y) {
1132
        s->vp8dsp.vp8_v_loop_filter16(dst[0], s->linesize,   filter_level+2, inner_limit, hev_thresh);
1133
        s->vp8dsp.vp8_v_loop_filter8 (dst[1], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
1134
        s->vp8dsp.vp8_v_loop_filter8 (dst[2], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
1135
    }
1136

  
1137
    if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1138
        s->vp8dsp.vp8_v_loop_filter16_inner(dst[0]+ 4*s->linesize,   s->linesize,   filter_level, inner_limit, hev_thresh);
1139
        s->vp8dsp.vp8_v_loop_filter16_inner(dst[0]+ 8*s->linesize,   s->linesize,   filter_level, inner_limit, hev_thresh);
1140
        s->vp8dsp.vp8_v_loop_filter16_inner(dst[0]+12*s->linesize,   s->linesize,   filter_level, inner_limit, hev_thresh);
1141
        s->vp8dsp.vp8_v_loop_filter8_inner (dst[1]+ 4*s->uvlinesize, s->uvlinesize, filter_level, inner_limit, hev_thresh);
1142
        s->vp8dsp.vp8_v_loop_filter8_inner (dst[2]+ 4*s->uvlinesize, s->uvlinesize, filter_level, inner_limit, hev_thresh);
1143
    }
1144
}
1145

  
1146
static void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8Macroblock *mb, int mb_x, int mb_y)
1147
{
1148
    int filter_level, inner_limit, mbedge_lim, bedge_lim;
1149

  
1150
    filter_level_for_mb(s, mb, &filter_level, &inner_limit, NULL);
1151
    if (!filter_level)
1152
        return;
1153

  
1154
    mbedge_lim = 2*(filter_level+2) + inner_limit;
1155
     bedge_lim = 2* filter_level    + inner_limit;
1156

  
1157
    if (mb_x)
1158
        s->vp8dsp.vp8_h_loop_filter_simple(dst, s->linesize, mbedge_lim);
1159
    if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1160
        s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, s->linesize, bedge_lim);
1161
        s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, s->linesize, bedge_lim);
1162
        s->vp8dsp.vp8_h_loop_filter_simple(dst+12, s->linesize, bedge_lim);
1163
    }
1164

  
1165
    if (mb_y)
1166
        s->vp8dsp.vp8_v_loop_filter_simple(dst, s->linesize, mbedge_lim);
1167
    if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1168
        s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*s->linesize, s->linesize, bedge_lim);
1169
        s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*s->linesize, s->linesize, bedge_lim);
1170
        s->vp8dsp.vp8_v_loop_filter_simple(dst+12*s->linesize, s->linesize, bedge_lim);
1171
    }
1172
}
1173

  
1174
static void filter_mb_row(VP8Context *s, int mb_y)
1175
{
1176
    VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1177
    uint8_t *dst[3] = {
1178
        s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize,
1179
        s->framep[VP56_FRAME_CURRENT]->data[1] +  8*mb_y*s->uvlinesize,
1180
        s->framep[VP56_FRAME_CURRENT]->data[2] +  8*mb_y*s->uvlinesize
1181
    };
1182
    int mb_x;
1183

  
1184
    for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1185
        filter_mb(s, dst, mb++, mb_x, mb_y);
1186
        dst[0] += 16;
1187
        dst[1] += 8;
1188
        dst[2] += 8;
1189
    }
1190
}
1191

  
1192
static void filter_mb_row_simple(VP8Context *s, int mb_y)
1193
{
1194
    uint8_t *dst = s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize;
1195
    VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1196
    int mb_x;
1197

  
1198
    for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1199
        filter_mb_simple(s, dst, mb++, mb_x, mb_y);
1200
        dst += 16;
1201
    }
1202
}
1203

  
1204
static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1205
                            AVPacket *avpkt)
1206
{
1207
    VP8Context *s = avctx->priv_data;
1208
    int ret, mb_x, mb_y, i, y, referenced;
1209
    enum AVDiscard skip_thresh;
1210
    AVFrame *curframe;
1211

  
1212
    if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
1213
        return ret;
1214

  
1215
    referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
1216
                                || s->update_altref == VP56_FRAME_CURRENT;
1217

  
1218
    skip_thresh = !referenced ? AVDISCARD_NONREF :
1219
                    !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
1220

  
1221
    if (avctx->skip_frame >= skip_thresh) {
1222
        s->invisible = 1;
1223
        goto skip_decode;
1224
    }
1225

  
1226
    for (i = 0; i < 4; i++)
1227
        if (&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1228
            &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1229
            &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
1230
            curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
1231
            break;
1232
        }
1233
    if (curframe->data[0])
1234
        avctx->release_buffer(avctx, curframe);
1235

  
1236
    curframe->key_frame = s->keyframe;
1237
    curframe->pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
1238
    curframe->reference = referenced ? 3 : 0;
1239
    if ((ret = avctx->get_buffer(avctx, curframe))) {
1240
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
1241
        return ret;
1242
    }
1243

  
1244
    // Given that arithmetic probabilities are updated every frame, it's quite likely
1245
    // that the values we have on a random interframe are complete junk if we didn't
1246
    // start decode on a keyframe. So just don't display anything rather than junk.
1247
    if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
1248
                         !s->framep[VP56_FRAME_GOLDEN] ||
1249
                         !s->framep[VP56_FRAME_GOLDEN2])) {
1250
        av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
1251
        return AVERROR_INVALIDDATA;
1252
    }
1253

  
1254
    s->linesize   = curframe->linesize[0];
1255
    s->uvlinesize = curframe->linesize[1];
1256

  
1257
    if (!s->edge_emu_buffer)
1258
        s->edge_emu_buffer = av_malloc(21*s->linesize);
1259

  
1260
    memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
1261

  
1262
    // top edge of 127 for intra prediction
1263
    if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1264
        memset(curframe->data[0] - s->linesize  -1, 127, s->linesize  +1);
1265
        memset(curframe->data[1] - s->uvlinesize-1, 127, s->uvlinesize+1);
1266
        memset(curframe->data[2] - s->uvlinesize-1, 127, s->uvlinesize+1);
1267
    }
1268

  
1269
    for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1270
        VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
1271
        VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1272
        uint8_t *intra4x4 = s->intra4x4_pred_mode + 4*mb_y*s->b4_stride;
1273
        uint8_t *dst[3] = {
1274
            curframe->data[0] + 16*mb_y*s->linesize,
1275
            curframe->data[1] +  8*mb_y*s->uvlinesize,
1276
            curframe->data[2] +  8*mb_y*s->uvlinesize
1277
        };
1278

  
1279
        memset(s->left_nnz, 0, sizeof(s->left_nnz));
1280

  
1281
        // left edge of 129 for intra prediction
1282
        if (!(avctx->flags & CODEC_FLAG_EMU_EDGE))
1283
            for (i = 0; i < 3; i++)
1284
                for (y = 0; y < 16>>!!i; y++)
1285
                    dst[i][y*curframe->linesize[i]-1] = 129;
1286

  
1287
        for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1288
            decode_mb_mode(s, mb, mb_x, mb_y, intra4x4 + 4*mb_x);
1289

  
1290
            if (!mb->skip)
1291
                decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
1292
            else {
1293
                AV_ZERO128(s->non_zero_count_cache);    // luma
1294
                AV_ZERO64(s->non_zero_count_cache[4]);  // chroma
1295
            }
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff