Native VP8 decoder.
[libav.git] / libavcodec / vp8.c
CommitLineData
3b636f21
DC
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
31typedef 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
43typedef 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
185static 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
204static 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
237static 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
258static 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
270static 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
296static 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 */
341static 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
357static 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
368static 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
491static 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
501static 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 */
559static 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
578static 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 */
594static 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
633static 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
652static 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 */
737static 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
774static 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
832static 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
845static 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 */
911static 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 */
940static 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
1013static 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
1055static 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
1109static 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
1146static 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
1174static 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
1192static 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
1204static 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 }
1296
1297 if (mb->mode <= MODE_I4x4) {
1298 intra_predict(s, dst, mb, intra4x4 + 4*mb_x, mb_x, mb_y);
1299 memset(mb->bmv, 0, sizeof(mb->bmv));
1300 } else {
1301 inter_predict(s, dst, mb, mb_x, mb_y);
1302 }
1303
1304 if (!mb->skip) {
1305 idct_mb(s, dst[0], dst[1], dst[2], mb);
1306 } else {
1307 AV_ZERO64(s->left_nnz);
1308 AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
1309
1310 // Reset DC block predictors if they would exist if the mb had coefficients
1311 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
1312 s->left_nnz[8] = 0;
1313 s->top_nnz[mb_x][8] = 0;
1314 }
1315 }
1316
1317 dst[0] += 16;
1318 dst[1] += 8;
1319 dst[2] += 8;
1320 mb++;
1321 }
1322 if (mb_y && s->filter.level && avctx->skip_loop_filter < skip_thresh) {
1323 if (s->filter.simple)
1324 filter_mb_row_simple(s, mb_y-1);
1325 else
1326 filter_mb_row(s, mb_y-1);
1327 }
1328 }
1329 if (s->filter.level && avctx->skip_loop_filter < skip_thresh) {
1330 if (s->filter.simple)
1331 filter_mb_row_simple(s, mb_y-1);
1332 else
1333 filter_mb_row(s, mb_y-1);
1334 }
1335
1336skip_decode:
1337 // if future frames don't use the updated probabilities,
1338 // reset them to the values we saved
1339 if (!s->update_probabilities)
1340 s->prob[0] = s->prob[1];
1341
1342 // check if golden and altref are swapped
1343 if (s->update_altref == VP56_FRAME_GOLDEN &&
1344 s->update_golden == VP56_FRAME_GOLDEN2)
1345 FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN], s->framep[VP56_FRAME_GOLDEN2]);
1346 else {
1347 if (s->update_altref != VP56_FRAME_NONE)
1348 s->framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
1349
1350 if (s->update_golden != VP56_FRAME_NONE)
1351 s->framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
1352 }
1353
1354 if (s->update_last) // move cur->prev
1355 s->framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_CURRENT];
1356
1357 // release no longer referenced frames
1358 for (i = 0; i < 4; i++)
1359 if (s->frames[i].data[0] &&
1360 &s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
1361 &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1362 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1363 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
1364 avctx->release_buffer(avctx, &s->frames[i]);
1365
1366 if (!s->invisible) {
1367 *(AVFrame*)data = *s->framep[VP56_FRAME_CURRENT];
1368 *data_size = sizeof(AVFrame);
1369 }
1370
1371 return avpkt->size;
1372}
1373
1374static av_cold int vp8_decode_init(AVCodecContext *avctx)
1375{
1376 VP8Context *s = avctx->priv_data;
1377
1378 s->avctx = avctx;
1379 avctx->pix_fmt = PIX_FMT_YUV420P;
1380
1381 dsputil_init(&s->dsp, avctx);
1382 ff_h264_pred_init(&s->hpc, CODEC_ID_VP8);
1383 ff_vp8dsp_init(&s->vp8dsp);
1384
1385 // intra pred needs edge emulation among other things
1386 if (avctx->flags&CODEC_FLAG_EMU_EDGE) {
1387 av_log(avctx, AV_LOG_ERROR, "Edge emulation not supproted\n");
1388 return AVERROR_PATCHWELCOME;
1389 }
1390
1391 return 0;
1392}
1393
1394static av_cold int vp8_decode_free(AVCodecContext *avctx)
1395{
1396 vp8_decode_flush(avctx);
1397 return 0;
1398}
1399
1400AVCodec vp8_decoder = {
1401 "vp8",
1402 AVMEDIA_TYPE_VIDEO,
1403 CODEC_ID_VP8,
1404 sizeof(VP8Context),
1405 vp8_decode_init,
1406 NULL,
1407 vp8_decode_free,
1408 vp8_decode_frame,
1409 CODEC_CAP_DR1,
1410 .flush = vp8_decode_flush,
1411 .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
1412};