]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp8.c
947d7c08caf1e69f523081539a1b707bcf411b07
[ffmpeg] / 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 filter_level;
33     uint8_t inner_limit;
34     uint8_t inner_filter;
35 } VP8FilterStrength;
36
37 typedef struct {
38     uint8_t skip;
39     // todo: make it possible to check for at least (i4x4 or split_mv)
40     // in one op. are others needed?
41     uint8_t mode;
42     uint8_t ref_frame;
43     uint8_t partitioning;
44     VP56mv mv;
45     VP56mv bmv[16];
46 } VP8Macroblock;
47
48 typedef struct {
49     AVCodecContext *avctx;
50     DSPContext dsp;
51     VP8DSPContext vp8dsp;
52     H264PredContext hpc;
53     vp8_mc_func put_pixels_tab[3][3][3];
54     AVFrame frames[4];
55     AVFrame *framep[4];
56     uint8_t *edge_emu_buffer;
57     VP56RangeCoder c;   ///< header context, includes mb modes and motion vectors
58     int profile;
59
60     int mb_width;   /* number of horizontal MB */
61     int mb_height;  /* number of vertical MB */
62     int linesize;
63     int uvlinesize;
64
65     int keyframe;
66     int invisible;
67     int update_last;    ///< update VP56_FRAME_PREVIOUS with the current one
68     int update_golden;  ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
69     int update_altref;
70     int deblock_filter;
71
72     /**
73      * If this flag is not set, all the probability updates
74      * are discarded after this frame is decoded.
75      */
76     int update_probabilities;
77
78     /**
79      * All coefficients are contained in separate arith coding contexts.
80      * There can be 1, 2, 4, or 8 of these after the header context.
81      */
82     int num_coeff_partitions;
83     VP56RangeCoder coeff_partition[8];
84
85     VP8Macroblock *macroblocks;
86     VP8Macroblock *macroblocks_base;
87     VP8FilterStrength *filter_strength;
88     int mb_stride;
89
90     uint8_t *intra4x4_pred_mode;
91     uint8_t *intra4x4_pred_mode_base;
92     uint8_t *segmentation_map;
93     int b4_stride;
94
95     /**
96      * Cache of the top row needed for intra prediction
97      * 16 for luma, 8 for each chroma plane
98      */
99     uint8_t (*top_border)[16+8+8];
100
101     /**
102      * For coeff decode, we need to know whether the above block had non-zero
103      * coefficients. This means for each macroblock, we need data for 4 luma
104      * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
105      * per macroblock. We keep the last row in top_nnz.
106      */
107     uint8_t (*top_nnz)[9];
108     DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
109
110     /**
111      * This is the index plus one of the last non-zero coeff
112      * for each of the blocks in the current macroblock.
113      * So, 0 -> no coeffs
114      *     1 -> dc-only (special transform)
115      *     2+-> full transform
116      */
117     DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
118     DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
119     uint8_t intra4x4_pred_mode_mb[16];
120
121     int chroma_pred_mode;    ///< 8x8c pred mode of the current macroblock
122     int segment;             ///< segment of the current macroblock
123
124     int mbskip_enabled;
125     int sign_bias[4]; ///< one state [0, 1] per ref frame type
126
127     /**
128      * Base parameters for segmentation, i.e. per-macroblock parameters.
129      * These must be kept unchanged even if segmentation is not used for
130      * a frame, since the values persist between interframes.
131      */
132     struct {
133         int enabled;
134         int absolute_vals;
135         int update_map;
136         int8_t base_quant[4];
137         int8_t filter_level[4];     ///< base loop filter level
138     } segmentation;
139
140     /**
141      * Macroblocks can have one of 4 different quants in a frame when
142      * segmentation is enabled.
143      * If segmentation is disabled, only the first segment's values are used.
144      */
145     struct {
146         // [0] - DC qmul  [1] - AC qmul
147         int16_t luma_qmul[2];
148         int16_t luma_dc_qmul[2];    ///< luma dc-only block quant
149         int16_t chroma_qmul[2];
150     } qmat[4];
151
152     struct {
153         int simple;
154         int level;
155         int sharpness;
156     } filter;
157
158     struct {
159         int enabled;    ///< whether each mb can have a different strength based on mode/ref
160
161         /**
162          * filter strength adjustment for the following macroblock modes:
163          * [0] - i4x4
164          * [1] - zero mv
165          * [2] - inter modes except for zero or split mv
166          * [3] - split mv
167          *  i16x16 modes never have any adjustment
168          */
169         int8_t mode[4];
170
171         /**
172          * filter strength adjustment for macroblocks that reference:
173          * [0] - intra / VP56_FRAME_CURRENT
174          * [1] - VP56_FRAME_PREVIOUS
175          * [2] - VP56_FRAME_GOLDEN
176          * [3] - altref / VP56_FRAME_GOLDEN2
177          */
178         int8_t ref[4];
179     } lf_delta;
180
181     /**
182      * These are all of the updatable probabilities for binary decisions.
183      * They are only implictly reset on keyframes, making it quite likely
184      * for an interframe to desync if a prior frame's header was corrupt
185      * or missing outright!
186      */
187     struct {
188         uint8_t segmentid[3];
189         uint8_t mbskip;
190         uint8_t intra;
191         uint8_t last;
192         uint8_t golden;
193         uint8_t pred16x16[4];
194         uint8_t pred8x8c[3];
195         uint8_t token[4][8][3][NUM_DCT_TOKENS-1];
196         uint8_t mvc[2][19];
197     } prob[2];
198 } VP8Context;
199
200 #define RL24(p) (AV_RL16(p) + ((p)[2] << 16))
201
202 static void vp8_decode_flush(AVCodecContext *avctx)
203 {
204     VP8Context *s = avctx->priv_data;
205     int i;
206
207     for (i = 0; i < 4; i++)
208         if (s->frames[i].data[0])
209             avctx->release_buffer(avctx, &s->frames[i]);
210     memset(s->framep, 0, sizeof(s->framep));
211
212     av_freep(&s->macroblocks_base);
213     av_freep(&s->intra4x4_pred_mode_base);
214     av_freep(&s->top_nnz);
215     av_freep(&s->edge_emu_buffer);
216     av_freep(&s->top_border);
217     av_freep(&s->segmentation_map);
218
219     s->macroblocks        = NULL;
220     s->intra4x4_pred_mode = NULL;
221 }
222
223 static int update_dimensions(VP8Context *s, int width, int height)
224 {
225     int i;
226
227     if (avcodec_check_dimensions(s->avctx, width, height))
228         return AVERROR_INVALIDDATA;
229
230     vp8_decode_flush(s->avctx);
231
232     avcodec_set_dimensions(s->avctx, width, height);
233
234     s->mb_width  = (s->avctx->coded_width +15) / 16;
235     s->mb_height = (s->avctx->coded_height+15) / 16;
236
237     // we allocate a border around the top/left of intra4x4 modes
238     // this is 4 blocks for intra4x4 to keep 4-byte alignment for fill_rectangle
239     s->mb_stride = s->mb_width+1;
240     s->b4_stride = 4*s->mb_stride;
241
242     s->macroblocks_base        = av_mallocz((s->mb_stride+s->mb_height*2+2)*sizeof(*s->macroblocks));
243     s->filter_strength         = av_mallocz(s->mb_stride*sizeof(*s->filter_strength));
244     s->intra4x4_pred_mode_base = av_mallocz(s->b4_stride*(4*s->mb_height+1));
245     s->top_nnz                 = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
246     s->top_border              = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
247     s->segmentation_map        = av_mallocz(s->mb_stride*s->mb_height);
248
249     if (!s->macroblocks_base || !s->filter_strength || !s->intra4x4_pred_mode_base ||
250         !s->top_nnz || !s->top_border || !s->segmentation_map)
251         return AVERROR(ENOMEM);
252
253     s->macroblocks        = s->macroblocks_base + 1;
254     s->intra4x4_pred_mode = s->intra4x4_pred_mode_base + 4 + s->b4_stride;
255
256     memset(s->intra4x4_pred_mode_base, DC_PRED, s->b4_stride);
257     for (i = 0; i < 4*s->mb_height; i++)
258         s->intra4x4_pred_mode[i*s->b4_stride-1] = DC_PRED;
259
260     return 0;
261 }
262
263 static void parse_segment_info(VP8Context *s)
264 {
265     VP56RangeCoder *c = &s->c;
266     int i;
267
268     s->segmentation.update_map = vp8_rac_get(c);
269
270     if (vp8_rac_get(c)) { // update segment feature data
271         s->segmentation.absolute_vals = vp8_rac_get(c);
272
273         for (i = 0; i < 4; i++)
274             s->segmentation.base_quant[i]   = vp8_rac_get_sint(c, 7);
275
276         for (i = 0; i < 4; i++)
277             s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
278     }
279     if (s->segmentation.update_map)
280         for (i = 0; i < 3; i++)
281             s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
282 }
283
284 static void update_lf_deltas(VP8Context *s)
285 {
286     VP56RangeCoder *c = &s->c;
287     int i;
288
289     for (i = 0; i < 4; i++)
290         s->lf_delta.ref[i]  = vp8_rac_get_sint(c, 6);
291
292     for (i = 0; i < 4; i++)
293         s->lf_delta.mode[i] = vp8_rac_get_sint(c, 6);
294 }
295
296 static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
297 {
298     const uint8_t *sizes = buf;
299     int i;
300
301     s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
302
303     buf      += 3*(s->num_coeff_partitions-1);
304     buf_size -= 3*(s->num_coeff_partitions-1);
305     if (buf_size < 0)
306         return -1;
307
308     for (i = 0; i < s->num_coeff_partitions-1; i++) {
309         int size = RL24(sizes + 3*i);
310         if (buf_size - size < 0)
311             return -1;
312
313         vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
314         buf      += size;
315         buf_size -= size;
316     }
317     vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
318
319     return 0;
320 }
321
322 static void get_quants(VP8Context *s)
323 {
324     VP56RangeCoder *c = &s->c;
325     int i, base_qi;
326
327     int yac_qi     = vp8_rac_get_uint(c, 7);
328     int ydc_delta  = vp8_rac_get_sint(c, 4);
329     int y2dc_delta = vp8_rac_get_sint(c, 4);
330     int y2ac_delta = vp8_rac_get_sint(c, 4);
331     int uvdc_delta = vp8_rac_get_sint(c, 4);
332     int uvac_delta = vp8_rac_get_sint(c, 4);
333
334     for (i = 0; i < 4; i++) {
335         if (s->segmentation.enabled) {
336             base_qi = s->segmentation.base_quant[i];
337             if (!s->segmentation.absolute_vals)
338                 base_qi += yac_qi;
339         } else
340             base_qi = yac_qi;
341
342         s->qmat[i].luma_qmul[0]    =       vp8_dc_qlookup[av_clip(base_qi + ydc_delta , 0, 127)];
343         s->qmat[i].luma_qmul[1]    =       vp8_ac_qlookup[av_clip(base_qi             , 0, 127)];
344         s->qmat[i].luma_dc_qmul[0] =   2 * vp8_dc_qlookup[av_clip(base_qi + y2dc_delta, 0, 127)];
345         s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip(base_qi + y2ac_delta, 0, 127)] / 100;
346         s->qmat[i].chroma_qmul[0]  =       vp8_dc_qlookup[av_clip(base_qi + uvdc_delta, 0, 127)];
347         s->qmat[i].chroma_qmul[1]  =       vp8_ac_qlookup[av_clip(base_qi + uvac_delta, 0, 127)];
348
349         s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
350         s->qmat[i].chroma_qmul[0]  = FFMIN(s->qmat[i].chroma_qmul[0], 132);
351     }
352 }
353
354 /**
355  * Determine which buffers golden and altref should be updated with after this frame.
356  * The spec isn't clear here, so I'm going by my understanding of what libvpx does
357  *
358  * Intra frames update all 3 references
359  * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
360  * If the update (golden|altref) flag is set, it's updated with the current frame
361  *      if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
362  * If the flag is not set, the number read means:
363  *      0: no update
364  *      1: VP56_FRAME_PREVIOUS
365  *      2: update golden with altref, or update altref with golden
366  */
367 static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
368 {
369     VP56RangeCoder *c = &s->c;
370
371     if (update)
372         return VP56_FRAME_CURRENT;
373
374     switch (vp8_rac_get_uint(c, 2)) {
375     case 1:
376         return VP56_FRAME_PREVIOUS;
377     case 2:
378         return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
379     }
380     return VP56_FRAME_NONE;
381 }
382
383 static void update_refs(VP8Context *s)
384 {
385     VP56RangeCoder *c = &s->c;
386
387     int update_golden = vp8_rac_get(c);
388     int update_altref = vp8_rac_get(c);
389
390     s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
391     s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
392 }
393
394 static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
395 {
396     VP56RangeCoder *c = &s->c;
397     int header_size, hscale, vscale, i, j, k, l, ret;
398     int width  = s->avctx->width;
399     int height = s->avctx->height;
400
401     s->keyframe  = !(buf[0] & 1);
402     s->profile   =  (buf[0]>>1) & 7;
403     s->invisible = !(buf[0] & 0x10);
404     header_size  = RL24(buf) >> 5;
405     buf      += 3;
406     buf_size -= 3;
407
408     if (s->profile > 3)
409         av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
410
411     if (!s->profile)
412         memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
413     else    // profile 1-3 use bilinear, 4+ aren't defined so whatever
414         memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
415
416     if (header_size > buf_size - 7*s->keyframe) {
417         av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
418         return AVERROR_INVALIDDATA;
419     }
420
421     if (s->keyframe) {
422         if (RL24(buf) != 0x2a019d) {
423             av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", RL24(buf));
424             return AVERROR_INVALIDDATA;
425         }
426         width  = AV_RL16(buf+3) & 0x3fff;
427         height = AV_RL16(buf+5) & 0x3fff;
428         hscale = buf[4] >> 6;
429         vscale = buf[6] >> 6;
430         buf      += 7;
431         buf_size -= 7;
432
433         if (hscale || vscale)
434             av_log_missing_feature(s->avctx, "Upscaling", 1);
435
436         s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
437         memcpy(s->prob->token    , vp8_token_default_probs , sizeof(s->prob->token));
438         memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
439         memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
440         memcpy(s->prob->mvc      , vp8_mv_default_prob     , sizeof(s->prob->mvc));
441         memset(&s->segmentation, 0, sizeof(s->segmentation));
442     }
443
444     if (!s->macroblocks_base || /* first frame */
445         width != s->avctx->width || height != s->avctx->height) {
446         if ((ret = update_dimensions(s, width, height) < 0))
447             return ret;
448     }
449
450     vp56_init_range_decoder(c, buf, header_size);
451     buf      += header_size;
452     buf_size -= header_size;
453
454     if (s->keyframe) {
455         if (vp8_rac_get(c))
456             av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
457         vp8_rac_get(c); // whether we can skip clamping in dsp functions
458     }
459
460     if ((s->segmentation.enabled = vp8_rac_get(c)))
461         parse_segment_info(s);
462     else
463         s->segmentation.update_map = 0; // FIXME: move this to some init function?
464
465     s->filter.simple    = vp8_rac_get(c);
466     s->filter.level     = vp8_rac_get_uint(c, 6);
467     s->filter.sharpness = vp8_rac_get_uint(c, 3);
468
469     if ((s->lf_delta.enabled = vp8_rac_get(c)))
470         if (vp8_rac_get(c))
471             update_lf_deltas(s);
472
473     if (setup_partitions(s, buf, buf_size)) {
474         av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
475         return AVERROR_INVALIDDATA;
476     }
477
478     get_quants(s);
479
480     if (!s->keyframe) {
481         update_refs(s);
482         s->sign_bias[VP56_FRAME_GOLDEN]               = vp8_rac_get(c);
483         s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
484     }
485
486     // if we aren't saving this frame's probabilities for future frames,
487     // make a copy of the current probabilities
488     if (!(s->update_probabilities = vp8_rac_get(c)))
489         s->prob[1] = s->prob[0];
490
491     s->update_last = s->keyframe || vp8_rac_get(c);
492
493     for (i = 0; i < 4; i++)
494         for (j = 0; j < 8; j++)
495             for (k = 0; k < 3; k++)
496                 for (l = 0; l < NUM_DCT_TOKENS-1; l++)
497                     if (vp56_rac_get_prob(c, vp8_token_update_probs[i][j][k][l]))
498                         s->prob->token[i][j][k][l] = vp8_rac_get_uint(c, 8);
499
500     if ((s->mbskip_enabled = vp8_rac_get(c)))
501         s->prob->mbskip = vp8_rac_get_uint(c, 8);
502
503     if (!s->keyframe) {
504         s->prob->intra  = vp8_rac_get_uint(c, 8);
505         s->prob->last   = vp8_rac_get_uint(c, 8);
506         s->prob->golden = vp8_rac_get_uint(c, 8);
507
508         if (vp8_rac_get(c))
509             for (i = 0; i < 4; i++)
510                 s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
511         if (vp8_rac_get(c))
512             for (i = 0; i < 3; i++)
513                 s->prob->pred8x8c[i]  = vp8_rac_get_uint(c, 8);
514
515         // 17.2 MV probability update
516         for (i = 0; i < 2; i++)
517             for (j = 0; j < 19; j++)
518                 if (vp56_rac_get_prob(c, vp8_mv_update_prob[i][j]))
519                     s->prob->mvc[i][j] = vp8_rac_get_nn(c);
520     }
521
522     return 0;
523 }
524
525 static inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src,
526                             int mb_x, int mb_y)
527 {
528 #define MARGIN (16 << 2)
529     dst->x = av_clip(src->x, -((mb_x << 6) + MARGIN),
530                      ((s->mb_width  - 1 - mb_x) << 6) + MARGIN);
531     dst->y = av_clip(src->y, -((mb_y << 6) + MARGIN),
532                      ((s->mb_height - 1 - mb_y) << 6) + MARGIN);
533 }
534
535 static void find_near_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
536                           VP56mv near[2], VP56mv *best, uint8_t cnt[4])
537 {
538     VP8Macroblock *mb_edge[3] = { mb + 2 /* top */,
539                                   mb - 1 /* left */,
540                                   mb + 1 /* top-left */ };
541     enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT };
542     VP56mv near_mv[4]  = {{ 0 }};
543     enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
544     int idx = CNT_ZERO;
545     int best_idx = CNT_ZERO;
546     int cur_sign_bias = s->sign_bias[mb->ref_frame];
547     int *sign_bias = s->sign_bias;
548
549     /* Process MB on top, left and top-left */
550     #define MV_EDGE_CHECK(n)\
551     {\
552         VP8Macroblock *edge = mb_edge[n];\
553         int edge_ref = edge->ref_frame;\
554         if (edge_ref != VP56_FRAME_CURRENT) {\
555             uint32_t mv = AV_RN32A(&edge->mv);\
556             if (mv) {\
557                 if (cur_sign_bias != sign_bias[edge_ref]) {\
558                     /* SWAR negate of the values in mv. */\
559                     mv = ((mv&0x80008000) + 0x00010001) ^ (mv&0x7fff7fff);\
560                 }\
561                 if (!n || mv != AV_RN32A(&near_mv[idx]))\
562                     AV_WN32A(&near_mv[++idx], mv);\
563                 cnt[idx]      += 1 + (n != 2);\
564             } else\
565                 cnt[CNT_ZERO] += 1 + (n != 2);\
566         }\
567     }
568     MV_EDGE_CHECK(0)
569     MV_EDGE_CHECK(1)
570     MV_EDGE_CHECK(2)
571
572     /* If we have three distinct MVs, merge first and last if they're the same */
573     if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1+EDGE_TOP]) == AV_RN32A(&near_mv[1+EDGE_TOPLEFT]))
574         cnt[CNT_NEAREST] += 1;
575
576     cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode   == VP8_MVMODE_SPLIT) +
577                         (mb_edge[EDGE_TOP]->mode    == VP8_MVMODE_SPLIT)) * 2 +
578                        (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
579
580     /* Swap near and nearest if necessary */
581     if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
582         FFSWAP(uint8_t,     cnt[CNT_NEAREST],     cnt[CNT_NEAR]);
583         FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
584     }
585
586     /* Choose the best mv out of 0,0 and the nearest mv */
587     if (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])
588         best_idx = CNT_NEAREST;
589
590     clamp_mv(s, best, &near_mv[best_idx], mb_x, mb_y);
591     near[0] = near_mv[CNT_NEAREST];
592     near[1] = near_mv[CNT_NEAR];
593 }
594
595 /**
596  * Motion vector coding, 17.1.
597  */
598 static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
599 {
600     int x = 0;
601
602     if (vp56_rac_get_prob(c, p[0])) {
603         int i;
604
605         for (i = 0; i < 3; i++)
606             x += vp56_rac_get_prob(c, p[9 + i]) << i;
607         for (i = 9; i > 3; i--)
608             x += vp56_rac_get_prob(c, p[9 + i]) << i;
609         if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
610             x += 8;
611     } else
612         x = vp8_rac_get_tree(c, vp8_small_mvtree, &p[2]);
613
614     return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
615 }
616
617 static const uint8_t *get_submv_prob(uint32_t left, uint32_t top)
618 {
619     if (left == top)
620         return vp8_submv_prob[4-!!left];
621     if (!top)
622         return vp8_submv_prob[2];
623     return vp8_submv_prob[1-!!left];
624 }
625
626 /**
627  * Split motion vector prediction, 16.4.
628  * @returns the number of motion vectors parsed (2, 4 or 16)
629  */
630 static int decode_splitmvs(VP8Context    *s,  VP56RangeCoder *c,
631                             VP8Macroblock *mb, VP56mv         *base_mv)
632 {
633     int part_idx = mb->partitioning =
634         vp8_rac_get_tree(c, vp8_mbsplit_tree, vp8_mbsplit_prob);
635     int n, num = vp8_mbsplit_count[part_idx];
636     VP8Macroblock *top_mb  = &mb[2];
637     VP8Macroblock *left_mb = &mb[-1];
638     const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning],
639                   *mbsplits_top = vp8_mbsplits[top_mb->partitioning],
640                   *mbsplits_cur = vp8_mbsplits[part_idx],
641                   *firstidx = vp8_mbfirstidx[part_idx];
642     VP56mv *top_mv  = top_mb->bmv;
643     VP56mv *left_mv = left_mb->bmv;
644     VP56mv *cur_mv  = mb->bmv;
645
646     for (n = 0; n < num; n++) {
647         int k = firstidx[n];
648         uint32_t left, above;
649         const uint8_t *submv_prob;
650
651         if (!(k & 3))
652             left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
653         else
654             left  = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
655         if (k <= 3)
656             above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
657         else
658             above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
659
660         submv_prob = get_submv_prob(left, above);
661
662         switch (vp8_rac_get_tree(c, vp8_submv_ref_tree, submv_prob)) {
663         case VP8_SUBMVMODE_NEW4X4:
664             mb->bmv[n].y = base_mv->y + read_mv_component(c, s->prob->mvc[0]);
665             mb->bmv[n].x = base_mv->x + read_mv_component(c, s->prob->mvc[1]);
666             break;
667         case VP8_SUBMVMODE_ZERO4X4:
668             AV_WN32A(&mb->bmv[n], 0);
669             break;
670         case VP8_SUBMVMODE_LEFT4X4:
671             AV_WN32A(&mb->bmv[n], left);
672             break;
673         case VP8_SUBMVMODE_TOP4X4:
674             AV_WN32A(&mb->bmv[n], above);
675             break;
676         }
677     }
678
679     return num;
680 }
681
682 static inline void decode_intra4x4_modes(VP56RangeCoder *c, uint8_t *intra4x4,
683                                          int stride, int keyframe)
684 {
685     int x, y, t, l, i;
686
687     if (keyframe) {
688         const uint8_t *ctx;
689         for (y = 0; y < 4; y++) {
690             for (x = 0; x < 4; x++) {
691                 t = intra4x4[x - stride];
692                 l = intra4x4[x - 1];
693                 ctx = vp8_pred4x4_prob_intra[t][l];
694                 intra4x4[x] = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
695             }
696             intra4x4 += stride;
697         }
698     } else {
699         for (i = 0; i < 16; i++)
700             intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree, vp8_pred4x4_prob_inter);
701     }
702 }
703
704 static void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
705                            uint8_t *intra4x4, uint8_t *segment)
706 {
707     VP56RangeCoder *c = &s->c;
708
709     if (s->segmentation.update_map)
710         *segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
711     s->segment = *segment;
712
713     mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
714
715     if (s->keyframe) {
716         mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
717
718         if (mb->mode == MODE_I4x4) {
719             decode_intra4x4_modes(c, intra4x4, s->b4_stride, 1);
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, vp8_pred8x8c_prob_intra);
724         mb->ref_frame = VP56_FRAME_CURRENT;
725     } else if (vp56_rac_get_prob(c, s->prob->intra)) {
726         VP56mv near[2], best;
727         uint8_t cnt[4] = { 0 };
728         uint8_t p[4];
729
730         // inter MB, 16.2
731         if (vp56_rac_get_prob(c, s->prob->last))
732             mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
733                 VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
734         else
735             mb->ref_frame = VP56_FRAME_PREVIOUS;
736
737         // motion vectors, 16.3
738         find_near_mvs(s, mb, mb_x, mb_y, near, &best, cnt);
739         p[0] = vp8_mode_contexts[cnt[0]][0];
740         p[1] = vp8_mode_contexts[cnt[1]][1];
741         p[2] = vp8_mode_contexts[cnt[2]][2];
742         p[3] = vp8_mode_contexts[cnt[3]][3];
743         mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_mvinter, p);
744         switch (mb->mode) {
745         case VP8_MVMODE_SPLIT:
746             mb->mv = mb->bmv[decode_splitmvs(s, c, mb, &best) - 1];
747             break;
748         case VP8_MVMODE_ZERO:
749             AV_WN32A(&mb->mv, 0);
750             break;
751         case VP8_MVMODE_NEAREST:
752             clamp_mv(s, &mb->mv, &near[0], mb_x, mb_y);
753             break;
754         case VP8_MVMODE_NEAR:
755             clamp_mv(s, &mb->mv, &near[1], mb_x, mb_y);
756             break;
757         case VP8_MVMODE_NEW:
758             mb->mv.y = best.y + read_mv_component(c, s->prob->mvc[0]);
759             mb->mv.x = best.x + read_mv_component(c, s->prob->mvc[1]);
760             break;
761         }
762         if (mb->mode != VP8_MVMODE_SPLIT) {
763             mb->partitioning = VP8_SPLITMVMODE_NONE;
764             mb->bmv[0] = mb->mv;
765         }
766     } else {
767         // intra MB, 16.1
768         mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
769
770         if (mb->mode == MODE_I4x4)
771             decode_intra4x4_modes(c, intra4x4, 4, 0);
772
773         s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
774         mb->ref_frame = VP56_FRAME_CURRENT;
775         mb->partitioning = VP8_SPLITMVMODE_NONE;
776         AV_WN32A(&mb->bmv[0], 0);
777     }
778 }
779
780 /**
781  * @param c arithmetic bitstream reader context
782  * @param block destination for block coefficients
783  * @param probs probabilities to use when reading trees from the bitstream
784  * @param i initial coeff index, 0 unless a separate DC block is coded
785  * @param zero_nhood the initial prediction context for number of surrounding
786  *                   all-zero blocks (only left/top, so 0-2)
787  * @param qmul array holding the dc/ac dequant factor at position 0/1
788  * @return 0 if no coeffs were decoded
789  *         otherwise, the index of the last coeff decoded plus one
790  */
791 static int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
792                                uint8_t probs[8][3][NUM_DCT_TOKENS-1],
793                                int i, int zero_nhood, int16_t qmul[2])
794 {
795     int token, nonzero = 0;
796     int offset = 0;
797
798     for (; i < 16; i++) {
799         token = vp8_rac_get_tree_with_offset(c, vp8_coeff_tree, probs[vp8_coeff_band[i]][zero_nhood], offset);
800
801         if (token == DCT_EOB)
802             break;
803         else if (token >= DCT_CAT1) {
804             int cat = token-DCT_CAT1;
805             token = vp8_rac_get_coeff(c, vp8_dct_cat_prob[cat]);
806             token += 3 + (2<<cat);
807         }
808
809         // after the first token, the non-zero prediction context becomes
810         // based on the last decoded coeff
811         if (!token) {
812             zero_nhood = 0;
813             offset = 1;
814             continue;
815         } else if (token == 1)
816             zero_nhood = 1;
817         else
818             zero_nhood = 2;
819
820         // todo: full [16] qmat? load into register?
821         block[zigzag_scan[i]] = (vp8_rac_get(c) ? -token : token) * qmul[!!i];
822         nonzero = i+1;
823         offset = 0;
824     }
825     return nonzero;
826 }
827
828 static void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
829                              uint8_t t_nnz[9], uint8_t l_nnz[9])
830 {
831     LOCAL_ALIGNED_16(DCTELEM, dc,[16]);
832     int i, x, y, luma_start = 0, luma_ctx = 3;
833     int nnz_pred, nnz, nnz_total = 0;
834     int segment = s->segment;
835
836     s->dsp.clear_blocks((DCTELEM *)s->block);
837
838     if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
839         AV_ZERO128(dc);
840         AV_ZERO128(dc+8);
841         nnz_pred = t_nnz[8] + l_nnz[8];
842
843         // decode DC values and do hadamard
844         nnz = decode_block_coeffs(c, dc, s->prob->token[1], 0, nnz_pred,
845                                   s->qmat[segment].luma_dc_qmul);
846         l_nnz[8] = t_nnz[8] = !!nnz;
847         nnz_total += nnz;
848         s->vp8dsp.vp8_luma_dc_wht(s->block, dc);
849         luma_start = 1;
850         luma_ctx = 0;
851     }
852
853     // luma blocks
854     for (y = 0; y < 4; y++)
855         for (x = 0; x < 4; x++) {
856             nnz_pred = l_nnz[y] + t_nnz[x];
857             nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
858                                       nnz_pred, s->qmat[segment].luma_qmul);
859             // nnz+luma_start may be one more than the actual last index, but we don't care
860             s->non_zero_count_cache[y][x] = nnz + luma_start;
861             t_nnz[x] = l_nnz[y] = !!nnz;
862             nnz_total += nnz;
863         }
864
865     // chroma blocks
866     // TODO: what to do about dimensions? 2nd dim for luma is x,
867     // but for chroma it's (y<<1)|x
868     for (i = 4; i < 6; i++)
869         for (y = 0; y < 2; y++)
870             for (x = 0; x < 2; x++) {
871                 nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
872                 nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
873                                           nnz_pred, s->qmat[segment].chroma_qmul);
874                 s->non_zero_count_cache[i][(y<<1)+x] = nnz;
875                 t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
876                 nnz_total += nnz;
877             }
878
879     // if there were no coded coeffs despite the macroblock not being marked skip,
880     // we MUST not do the inner loop filter and should not do IDCT
881     // Since skip isn't used for bitstream prediction, just manually set it.
882     if (!nnz_total)
883         mb->skip = 1;
884 }
885
886 static av_always_inline
887 void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
888                       int linesize, int uvlinesize, int simple)
889 {
890     AV_COPY128(top_border, src_y + 15*linesize);
891     if (!simple) {
892         AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
893         AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
894     }
895 }
896
897 static av_always_inline
898 void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
899                     int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
900                     int simple, int xchg)
901 {
902     uint8_t *top_border_m1 = top_border-32;     // for TL prediction
903     src_y  -=   linesize;
904     src_cb -= uvlinesize;
905     src_cr -= uvlinesize;
906
907 #define XCHG(a,b,xchg) do {                     \
908         if (xchg) AV_SWAP64(b,a);               \
909         else      AV_COPY64(b,a);               \
910     } while (0)
911
912     XCHG(top_border_m1+8, src_y-8, xchg);
913     XCHG(top_border,      src_y,   xchg);
914     XCHG(top_border+8,    src_y+8, 1);
915     if (mb_x < mb_width-1)
916         XCHG(top_border+32, src_y+16, 1);
917
918     // only copy chroma for normal loop filter
919     // or to initialize the top row to 127
920     if (!simple || !mb_y) {
921         XCHG(top_border_m1+16, src_cb-8, xchg);
922         XCHG(top_border_m1+24, src_cr-8, xchg);
923         XCHG(top_border+16,    src_cb, 1);
924         XCHG(top_border+24,    src_cr, 1);
925     }
926 }
927
928 static int check_intra_pred_mode(int mode, int mb_x, int mb_y)
929 {
930     if (mode == DC_PRED8x8) {
931         if (!(mb_x|mb_y))
932             mode = DC_128_PRED8x8;
933         else if (!mb_y)
934             mode = LEFT_DC_PRED8x8;
935         else if (!mb_x)
936             mode = TOP_DC_PRED8x8;
937     }
938     return mode;
939 }
940
941 static void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
942                           uint8_t *intra4x4, int mb_x, int mb_y)
943 {
944     int x, y, mode, nnz, tr;
945
946     // for the first row, we need to run xchg_mb_border to init the top edge to 127
947     // otherwise, skip it if we aren't going to deblock
948     if (s->deblock_filter || !mb_y)
949         xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
950                        s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
951                        s->filter.simple, 1);
952
953     if (mb->mode < MODE_I4x4) {
954         mode = check_intra_pred_mode(mb->mode, mb_x, mb_y);
955         s->hpc.pred16x16[mode](dst[0], s->linesize);
956     } else {
957         uint8_t *ptr = dst[0];
958         int stride = s->keyframe ? s->b4_stride : 4;
959
960         // all blocks on the right edge of the macroblock use bottom edge
961         // the top macroblock for their topright edge
962         uint8_t *tr_right = ptr - s->linesize + 16;
963
964         // if we're on the right edge of the frame, said edge is extended
965         // from the top macroblock
966         if (mb_x == s->mb_width-1) {
967             tr = tr_right[-1]*0x01010101;
968             tr_right = (uint8_t *)&tr;
969         }
970
971         for (y = 0; y < 4; y++) {
972             uint8_t *topright = ptr + 4 - s->linesize;
973             for (x = 0; x < 4; x++) {
974                 if (x == 3)
975                     topright = tr_right;
976
977                 s->hpc.pred4x4[intra4x4[x]](ptr+4*x, topright, s->linesize);
978
979                 nnz = s->non_zero_count_cache[y][x];
980                 if (nnz) {
981                     if (nnz == 1)
982                         s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
983                     else
984                         s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
985                 }
986                 topright += 4;
987             }
988
989             ptr   += 4*s->linesize;
990             intra4x4 += stride;
991         }
992     }
993
994     mode = check_intra_pred_mode(s->chroma_pred_mode, mb_x, mb_y);
995     s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
996     s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
997
998     if (s->deblock_filter || !mb_y)
999         xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
1000                        s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1001                        s->filter.simple, 0);
1002 }
1003
1004 /**
1005  * Generic MC function.
1006  *
1007  * @param s VP8 decoding context
1008  * @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
1009  * @param dst target buffer for block data at block position
1010  * @param src reference picture buffer at origin (0, 0)
1011  * @param mv motion vector (relative to block position) to get pixel data from
1012  * @param x_off horizontal position of block from origin (0, 0)
1013  * @param y_off vertical position of block from origin (0, 0)
1014  * @param block_w width of block (16, 8 or 4)
1015  * @param block_h height of block (always same as block_w)
1016  * @param width width of src/dst plane data
1017  * @param height height of src/dst plane data
1018  * @param linesize size of a single line of plane data, including padding
1019  * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1020  */
1021 static inline void vp8_mc(VP8Context *s, int luma,
1022                           uint8_t *dst, uint8_t *src, const VP56mv *mv,
1023                           int x_off, int y_off, int block_w, int block_h,
1024                           int width, int height, int linesize,
1025                           vp8_mc_func mc_func[3][3])
1026 {
1027     if (AV_RN32A(mv)) {
1028         static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };
1029         int mx = (mv->x << luma)&7, mx_idx = idx[mx];
1030         int my = (mv->y << luma)&7, my_idx = idx[my];
1031
1032         x_off += mv->x >> (3 - luma);
1033         y_off += mv->y >> (3 - luma);
1034
1035         // edge emulation
1036         src += y_off * linesize + x_off;
1037         if (x_off < 2 || x_off >= width  - block_w - 3 ||
1038             y_off < 2 || y_off >= height - block_h - 3) {
1039             ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,
1040                                 block_w + 5, block_h + 5,
1041                                 x_off - 2, y_off - 2, width, height);
1042             src = s->edge_emu_buffer + 2 + linesize * 2;
1043         }
1044         mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
1045     } else
1046         mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
1047 }
1048
1049 static inline void vp8_mc_part(VP8Context *s, uint8_t *dst[3],
1050                                AVFrame *ref_frame, int x_off, int y_off,
1051                                int bx_off, int by_off,
1052                                int block_w, int block_h,
1053                                int width, int height, VP56mv *mv)
1054 {
1055     VP56mv uvmv = *mv;
1056
1057     /* Y */
1058     vp8_mc(s, 1, dst[0] + by_off * s->linesize + bx_off,
1059            ref_frame->data[0], mv, x_off + bx_off, y_off + by_off,
1060            block_w, block_h, width, height, s->linesize,
1061            s->put_pixels_tab[block_w == 8]);
1062
1063     /* U/V */
1064     if (s->profile == 3) {
1065         uvmv.x &= ~7;
1066         uvmv.y &= ~7;
1067     }
1068     x_off   >>= 1; y_off   >>= 1;
1069     bx_off  >>= 1; by_off  >>= 1;
1070     width   >>= 1; height  >>= 1;
1071     block_w >>= 1; block_h >>= 1;
1072     vp8_mc(s, 0, dst[1] + by_off * s->uvlinesize + bx_off,
1073            ref_frame->data[1], &uvmv, x_off + bx_off, y_off + by_off,
1074            block_w, block_h, width, height, s->uvlinesize,
1075            s->put_pixels_tab[1 + (block_w == 4)]);
1076     vp8_mc(s, 0, dst[2] + by_off * s->uvlinesize + bx_off,
1077            ref_frame->data[2], &uvmv, x_off + bx_off, y_off + by_off,
1078            block_w, block_h, width, height, s->uvlinesize,
1079            s->put_pixels_tab[1 + (block_w == 4)]);
1080 }
1081
1082 /* Fetch pixels for estimated mv 4 macroblocks ahead.
1083  * Optimized for 64-byte cache lines.  Inspired by ffh264 prefetch_motion. */
1084 static inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int x_off, int y_off, int ref)
1085 {
1086     if (mb->ref_frame != VP56_FRAME_CURRENT) {
1087         int mx = mb->mv.x + x_off + 8;
1088         int my = mb->mv.y + y_off;
1089         uint8_t **src= s->framep[mb->ref_frame]->data;
1090         int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
1091         s->dsp.prefetch(src[0]+off, s->linesize, 4);
1092         off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
1093         s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
1094     }
1095 }
1096
1097 /**
1098  * Apply motion vectors to prediction buffer, chapter 18.
1099  */
1100 static void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
1101                           int mb_x, int mb_y)
1102 {
1103     int x_off = mb_x << 4, y_off = mb_y << 4;
1104     int width = 16*s->mb_width, height = 16*s->mb_height;
1105     AVFrame *ref = s->framep[mb->ref_frame];
1106     VP56mv *bmv = mb->bmv;
1107
1108     prefetch_motion(s, mb, mb_x, mb_y, x_off, y_off, VP56_FRAME_PREVIOUS);
1109
1110     if (mb->mode < VP8_MVMODE_SPLIT) {
1111         vp8_mc_part(s, dst, ref, x_off, y_off,
1112                     0, 0, 16, 16, width, height, &mb->mv);
1113     } else switch (mb->partitioning) {
1114     case VP8_SPLITMVMODE_4x4: {
1115         int x, y;
1116         VP56mv uvmv;
1117
1118         /* Y */
1119         for (y = 0; y < 4; y++) {
1120             for (x = 0; x < 4; x++) {
1121                 vp8_mc(s, 1, dst[0] + 4*y*s->linesize + x*4,
1122                        ref->data[0], &bmv[4*y + x],
1123                        4*x + x_off, 4*y + y_off, 4, 4,
1124                        width, height, s->linesize,
1125                        s->put_pixels_tab[2]);
1126             }
1127         }
1128
1129         /* U/V */
1130         x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
1131         for (y = 0; y < 2; y++) {
1132             for (x = 0; x < 2; x++) {
1133                 uvmv.x = mb->bmv[ 2*y    * 4 + 2*x  ].x +
1134                          mb->bmv[ 2*y    * 4 + 2*x+1].x +
1135                          mb->bmv[(2*y+1) * 4 + 2*x  ].x +
1136                          mb->bmv[(2*y+1) * 4 + 2*x+1].x;
1137                 uvmv.y = mb->bmv[ 2*y    * 4 + 2*x  ].y +
1138                          mb->bmv[ 2*y    * 4 + 2*x+1].y +
1139                          mb->bmv[(2*y+1) * 4 + 2*x  ].y +
1140                          mb->bmv[(2*y+1) * 4 + 2*x+1].y;
1141                 uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
1142                 uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
1143                 if (s->profile == 3) {
1144                     uvmv.x &= ~7;
1145                     uvmv.y &= ~7;
1146                 }
1147                 vp8_mc(s, 0, dst[1] + 4*y*s->uvlinesize + x*4,
1148                        ref->data[1], &uvmv,
1149                        4*x + x_off, 4*y + y_off, 4, 4,
1150                        width, height, s->uvlinesize,
1151                        s->put_pixels_tab[2]);
1152                 vp8_mc(s, 0, dst[2] + 4*y*s->uvlinesize + x*4,
1153                        ref->data[2], &uvmv,
1154                        4*x + x_off, 4*y + y_off, 4, 4,
1155                        width, height, s->uvlinesize,
1156                        s->put_pixels_tab[2]);
1157             }
1158         }
1159         break;
1160     }
1161     case VP8_SPLITMVMODE_16x8:
1162         vp8_mc_part(s, dst, ref, x_off, y_off,
1163                     0, 0, 16, 8, width, height, &bmv[0]);
1164         vp8_mc_part(s, dst, ref, x_off, y_off,
1165                     0, 8, 16, 8, width, height, &bmv[1]);
1166         break;
1167     case VP8_SPLITMVMODE_8x16:
1168         vp8_mc_part(s, dst, ref, x_off, y_off,
1169                     0, 0, 8, 16, width, height, &bmv[0]);
1170         vp8_mc_part(s, dst, ref, x_off, y_off,
1171                     8, 0, 8, 16, width, height, &bmv[1]);
1172         break;
1173     case VP8_SPLITMVMODE_8x8:
1174         vp8_mc_part(s, dst, ref, x_off, y_off,
1175                     0, 0, 8, 8, width, height, &bmv[0]);
1176         vp8_mc_part(s, dst, ref, x_off, y_off,
1177                     8, 0, 8, 8, width, height, &bmv[1]);
1178         vp8_mc_part(s, dst, ref, x_off, y_off,
1179                     0, 8, 8, 8, width, height, &bmv[2]);
1180         vp8_mc_part(s, dst, ref, x_off, y_off,
1181                     8, 8, 8, 8, width, height, &bmv[3]);
1182         break;
1183     }
1184
1185     prefetch_motion(s, mb, mb_x, mb_y, x_off, y_off, VP56_FRAME_GOLDEN);
1186 }
1187
1188 static void idct_mb(VP8Context *s, uint8_t *y_dst, uint8_t *u_dst, uint8_t *v_dst,
1189                     VP8Macroblock *mb)
1190 {
1191     int x, y, nnz;
1192
1193     if (mb->mode != MODE_I4x4)
1194         for (y = 0; y < 4; y++) {
1195             for (x = 0; x < 4; x++) {
1196                 nnz = s->non_zero_count_cache[y][x];
1197                 if (nnz) {
1198                     if (nnz == 1)
1199                         s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
1200                     else
1201                         s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
1202                 }
1203             }
1204             y_dst += 4*s->linesize;
1205         }
1206
1207     for (y = 0; y < 2; y++) {
1208         for (x = 0; x < 2; x++) {
1209             nnz = s->non_zero_count_cache[4][(y<<1)+x];
1210             if (nnz) {
1211                 if (nnz == 1)
1212                     s->vp8dsp.vp8_idct_dc_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1213                 else
1214                     s->vp8dsp.vp8_idct_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1215             }
1216
1217             nnz = s->non_zero_count_cache[5][(y<<1)+x];
1218             if (nnz) {
1219                 if (nnz == 1)
1220                     s->vp8dsp.vp8_idct_dc_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1221                 else
1222                     s->vp8dsp.vp8_idct_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1223             }
1224         }
1225         u_dst += 4*s->uvlinesize;
1226         v_dst += 4*s->uvlinesize;
1227     }
1228 }
1229
1230 static void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f )
1231 {
1232     int interior_limit, filter_level;
1233
1234     if (s->segmentation.enabled) {
1235         filter_level = s->segmentation.filter_level[s->segment];
1236         if (!s->segmentation.absolute_vals)
1237             filter_level += s->filter.level;
1238     } else
1239         filter_level = s->filter.level;
1240
1241     if (s->lf_delta.enabled) {
1242         filter_level += s->lf_delta.ref[mb->ref_frame];
1243
1244         if (mb->ref_frame == VP56_FRAME_CURRENT) {
1245             if (mb->mode == MODE_I4x4)
1246                 filter_level += s->lf_delta.mode[0];
1247         } else {
1248             if (mb->mode == VP8_MVMODE_ZERO)
1249                 filter_level += s->lf_delta.mode[1];
1250             else if (mb->mode == VP8_MVMODE_SPLIT)
1251                 filter_level += s->lf_delta.mode[3];
1252             else
1253                 filter_level += s->lf_delta.mode[2];
1254         }
1255     }
1256     filter_level = av_clip(filter_level, 0, 63);
1257
1258     interior_limit = filter_level;
1259     if (s->filter.sharpness) {
1260         interior_limit >>= s->filter.sharpness > 4 ? 2 : 1;
1261         interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
1262     }
1263     interior_limit = FFMAX(interior_limit, 1);
1264
1265     f->filter_level = filter_level;
1266     f->inner_limit = interior_limit;
1267     f->inner_filter = !mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT;
1268 }
1269
1270 static void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f, int mb_x, int mb_y)
1271 {
1272     int mbedge_lim, bedge_lim, hev_thresh;
1273     int filter_level = f->filter_level;
1274     int inner_limit = f->inner_limit;
1275     int inner_filter = f->inner_filter;
1276
1277     if (!filter_level)
1278         return;
1279
1280     mbedge_lim = 2*(filter_level+2) + inner_limit;
1281      bedge_lim = 2* filter_level    + inner_limit;
1282     hev_thresh = filter_level >= 15;
1283
1284     if (s->keyframe) {
1285         if (filter_level >= 40)
1286             hev_thresh = 2;
1287     } else {
1288         if (filter_level >= 40)
1289             hev_thresh = 3;
1290         else if (filter_level >= 20)
1291             hev_thresh = 2;
1292     }
1293
1294     if (mb_x) {
1295         s->vp8dsp.vp8_h_loop_filter16y(dst[0],     s->linesize,
1296                                        mbedge_lim, inner_limit, hev_thresh);
1297         s->vp8dsp.vp8_h_loop_filter8uv(dst[1],     dst[2],      s->uvlinesize,
1298                                        mbedge_lim, inner_limit, hev_thresh);
1299     }
1300
1301     if (inner_filter) {
1302         s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, s->linesize, bedge_lim,
1303                                              inner_limit,   hev_thresh);
1304         s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, s->linesize, bedge_lim,
1305                                              inner_limit,   hev_thresh);
1306         s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, s->linesize, bedge_lim,
1307                                              inner_limit,   hev_thresh);
1308         s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4,    dst[2] + 4,
1309                                              s->uvlinesize, bedge_lim,
1310                                              inner_limit,   hev_thresh);
1311     }
1312
1313     if (mb_y) {
1314         s->vp8dsp.vp8_v_loop_filter16y(dst[0],     s->linesize,
1315                                        mbedge_lim, inner_limit, hev_thresh);
1316         s->vp8dsp.vp8_v_loop_filter8uv(dst[1],     dst[2],      s->uvlinesize,
1317                                        mbedge_lim, inner_limit, hev_thresh);
1318     }
1319
1320     if (inner_filter) {
1321         s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*s->linesize,
1322                                              s->linesize,   bedge_lim,
1323                                              inner_limit,   hev_thresh);
1324         s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*s->linesize,
1325                                              s->linesize,   bedge_lim,
1326                                              inner_limit,   hev_thresh);
1327         s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*s->linesize,
1328                                              s->linesize,   bedge_lim,
1329                                              inner_limit, hev_thresh);
1330         s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * s->uvlinesize,
1331                                              dst[2] + 4 * s->uvlinesize,
1332                                              s->uvlinesize, bedge_lim,
1333                                              inner_limit,   hev_thresh);
1334     }
1335 }
1336
1337 static void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
1338 {
1339     int mbedge_lim, bedge_lim;
1340     int filter_level = f->filter_level;
1341     int inner_limit = f->inner_limit;
1342     int inner_filter = f->inner_filter;
1343
1344     if (!filter_level)
1345         return;
1346
1347     mbedge_lim = 2*(filter_level+2) + inner_limit;
1348      bedge_lim = 2* filter_level    + inner_limit;
1349
1350     if (mb_x)
1351         s->vp8dsp.vp8_h_loop_filter_simple(dst, s->linesize, mbedge_lim);
1352     if (inner_filter) {
1353         s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, s->linesize, bedge_lim);
1354         s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, s->linesize, bedge_lim);
1355         s->vp8dsp.vp8_h_loop_filter_simple(dst+12, s->linesize, bedge_lim);
1356     }
1357
1358     if (mb_y)
1359         s->vp8dsp.vp8_v_loop_filter_simple(dst, s->linesize, mbedge_lim);
1360     if (inner_filter) {
1361         s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*s->linesize, s->linesize, bedge_lim);
1362         s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*s->linesize, s->linesize, bedge_lim);
1363         s->vp8dsp.vp8_v_loop_filter_simple(dst+12*s->linesize, s->linesize, bedge_lim);
1364     }
1365 }
1366
1367 static void filter_mb_row(VP8Context *s, int mb_y)
1368 {
1369     VP8FilterStrength *f = s->filter_strength;
1370     uint8_t *dst[3] = {
1371         s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize,
1372         s->framep[VP56_FRAME_CURRENT]->data[1] +  8*mb_y*s->uvlinesize,
1373         s->framep[VP56_FRAME_CURRENT]->data[2] +  8*mb_y*s->uvlinesize
1374     };
1375     int mb_x;
1376
1377     for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1378         backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
1379         filter_mb(s, dst, f++, mb_x, mb_y);
1380         dst[0] += 16;
1381         dst[1] += 8;
1382         dst[2] += 8;
1383     }
1384 }
1385
1386 static void filter_mb_row_simple(VP8Context *s, int mb_y)
1387 {
1388     VP8FilterStrength *f = s->filter_strength;
1389     uint8_t *dst = s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize;
1390     int mb_x;
1391
1392     for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1393         backup_mb_border(s->top_border[mb_x+1], dst, NULL, NULL, s->linesize, 0, 1);
1394         filter_mb_simple(s, dst, f++, mb_x, mb_y);
1395         dst += 16;
1396     }
1397 }
1398
1399 static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1400                             AVPacket *avpkt)
1401 {
1402     VP8Context *s = avctx->priv_data;
1403     int ret, mb_x, mb_y, i, y, referenced;
1404     enum AVDiscard skip_thresh;
1405     AVFrame *curframe;
1406
1407     if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
1408         return ret;
1409
1410     referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
1411                                 || s->update_altref == VP56_FRAME_CURRENT;
1412
1413     skip_thresh = !referenced ? AVDISCARD_NONREF :
1414                     !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
1415
1416     if (avctx->skip_frame >= skip_thresh) {
1417         s->invisible = 1;
1418         goto skip_decode;
1419     }
1420     s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
1421
1422     for (i = 0; i < 4; i++)
1423         if (&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1424             &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1425             &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
1426             curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
1427             break;
1428         }
1429     if (curframe->data[0])
1430         avctx->release_buffer(avctx, curframe);
1431
1432     curframe->key_frame = s->keyframe;
1433     curframe->pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
1434     curframe->reference = referenced ? 3 : 0;
1435     if ((ret = avctx->get_buffer(avctx, curframe))) {
1436         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
1437         return ret;
1438     }
1439
1440     // Given that arithmetic probabilities are updated every frame, it's quite likely
1441     // that the values we have on a random interframe are complete junk if we didn't
1442     // start decode on a keyframe. So just don't display anything rather than junk.
1443     if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
1444                          !s->framep[VP56_FRAME_GOLDEN] ||
1445                          !s->framep[VP56_FRAME_GOLDEN2])) {
1446         av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
1447         return AVERROR_INVALIDDATA;
1448     }
1449
1450     s->linesize   = curframe->linesize[0];
1451     s->uvlinesize = curframe->linesize[1];
1452
1453     if (!s->edge_emu_buffer)
1454         s->edge_emu_buffer = av_malloc(21*s->linesize);
1455
1456     memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
1457
1458     /* Zero macroblock structures for top/left prediction from outside the frame. */
1459     memset(s->macroblocks, 0, (s->mb_width + s->mb_height*2)*sizeof(*s->macroblocks));
1460
1461     // top edge of 127 for intra prediction
1462     memset(s->top_border, 127, (s->mb_width+1)*sizeof(*s->top_border));
1463
1464     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1465         VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
1466         VP8Macroblock *mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
1467         uint8_t *intra4x4 = s->intra4x4_pred_mode + 4*mb_y*s->b4_stride;
1468         uint8_t *segment_map = s->segmentation_map + mb_y*s->mb_stride;
1469         uint8_t *dst[3] = {
1470             curframe->data[0] + 16*mb_y*s->linesize,
1471             curframe->data[1] +  8*mb_y*s->uvlinesize,
1472             curframe->data[2] +  8*mb_y*s->uvlinesize
1473         };
1474
1475         memset(s->left_nnz, 0, sizeof(s->left_nnz));
1476
1477         // left edge of 129 for intra prediction
1478         if (!(avctx->flags & CODEC_FLAG_EMU_EDGE))
1479             for (i = 0; i < 3; i++)
1480                 for (y = 0; y < 16>>!!i; y++)
1481                     dst[i][y*curframe->linesize[i]-1] = 129;
1482         if (mb_y)
1483             memset(s->top_border, 129, sizeof(*s->top_border));
1484
1485         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1486             uint8_t *intra4x4_mb = s->keyframe ? intra4x4 + 4*mb_x : s->intra4x4_pred_mode_mb;
1487             uint8_t *segment_mb = segment_map+mb_x;
1488
1489             /* Prefetch the current frame, 4 MBs ahead */
1490             s->dsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
1491             s->dsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
1492
1493             decode_mb_mode(s, mb, mb_x, mb_y, intra4x4_mb, segment_mb);
1494
1495             if (!mb->skip)
1496                 decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
1497             else {
1498                 AV_ZERO128(s->non_zero_count_cache);    // luma
1499                 AV_ZERO64(s->non_zero_count_cache[4]);  // chroma
1500             }
1501
1502             if (mb->mode <= MODE_I4x4)
1503                 intra_predict(s, dst, mb, intra4x4_mb, mb_x, mb_y);
1504             else
1505                 inter_predict(s, dst, mb, mb_x, mb_y);
1506
1507             if (!mb->skip) {
1508                 idct_mb(s, dst[0], dst[1], dst[2], mb);
1509             } else {
1510                 AV_ZERO64(s->left_nnz);
1511                 AV_WN64(s->top_nnz[mb_x], 0);   // array of 9, so unaligned
1512
1513                 // Reset DC block predictors if they would exist if the mb had coefficients
1514                 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
1515                     s->left_nnz[8]      = 0;
1516                     s->top_nnz[mb_x][8] = 0;
1517                 }
1518             }
1519
1520             if (s->deblock_filter)
1521                 filter_level_for_mb(s, mb, &s->filter_strength[mb_x]);
1522
1523             dst[0] += 16;
1524             dst[1] += 8;
1525             dst[2] += 8;
1526             mb++;
1527         }
1528         if (s->deblock_filter) {
1529             if (s->filter.simple)
1530                 filter_mb_row_simple(s, mb_y);
1531             else
1532                 filter_mb_row(s, mb_y);
1533         }
1534     }
1535
1536 skip_decode:
1537     // if future frames don't use the updated probabilities,
1538     // reset them to the values we saved
1539     if (!s->update_probabilities)
1540         s->prob[0] = s->prob[1];
1541
1542     // check if golden and altref are swapped
1543     if (s->update_altref == VP56_FRAME_GOLDEN &&
1544         s->update_golden == VP56_FRAME_GOLDEN2)
1545         FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN], s->framep[VP56_FRAME_GOLDEN2]);
1546     else {
1547         if (s->update_altref != VP56_FRAME_NONE)
1548             s->framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
1549
1550         if (s->update_golden != VP56_FRAME_NONE)
1551             s->framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
1552     }
1553
1554     if (s->update_last) // move cur->prev
1555         s->framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_CURRENT];
1556
1557     // release no longer referenced frames
1558     for (i = 0; i < 4; i++)
1559         if (s->frames[i].data[0] &&
1560             &s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
1561             &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1562             &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1563             &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
1564             avctx->release_buffer(avctx, &s->frames[i]);
1565
1566     if (!s->invisible) {
1567         *(AVFrame*)data = *s->framep[VP56_FRAME_CURRENT];
1568         *data_size = sizeof(AVFrame);
1569     }
1570
1571     return avpkt->size;
1572 }
1573
1574 static av_cold int vp8_decode_init(AVCodecContext *avctx)
1575 {
1576     VP8Context *s = avctx->priv_data;
1577
1578     s->avctx = avctx;
1579     avctx->pix_fmt = PIX_FMT_YUV420P;
1580
1581     dsputil_init(&s->dsp, avctx);
1582     ff_h264_pred_init(&s->hpc, CODEC_ID_VP8);
1583     ff_vp8dsp_init(&s->vp8dsp);
1584
1585     // intra pred needs edge emulation among other things
1586     if (avctx->flags&CODEC_FLAG_EMU_EDGE) {
1587         av_log(avctx, AV_LOG_ERROR, "Edge emulation not supported\n");
1588         return AVERROR_PATCHWELCOME;
1589     }
1590
1591     return 0;
1592 }
1593
1594 static av_cold int vp8_decode_free(AVCodecContext *avctx)
1595 {
1596     vp8_decode_flush(avctx);
1597     return 0;
1598 }
1599
1600 AVCodec vp8_decoder = {
1601     "vp8",
1602     AVMEDIA_TYPE_VIDEO,
1603     CODEC_ID_VP8,
1604     sizeof(VP8Context),
1605     vp8_decode_init,
1606     NULL,
1607     vp8_decode_free,
1608     vp8_decode_frame,
1609     CODEC_CAP_DR1,
1610     .flush = vp8_decode_flush,
1611     .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
1612 };