2 * VP8 compatible video decoder
4 * Copyright (C) 2010 David Conrad
5 * Copyright (C) 2010 Ronald S. Bultje
7 * This file is part of FFmpeg.
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.
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.
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
29 #include "rectangle.h"
34 // todo: make it possible to check for at least (i4x4 or split_mv)
35 // in one op. are others needed?
44 AVCodecContext *avctx;
50 uint8_t *edge_emu_buffer;
51 VP56RangeCoder c; ///< header context, includes mb modes and motion vectors
54 int mb_width; /* number of horizontal MB */
55 int mb_height; /* number of vertical MB */
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
66 * If this flag is not set, all the probability updates
67 * are discarded after this frame is decoded.
69 int update_probabilities;
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.
75 int num_coeff_partitions;
76 VP56RangeCoder coeff_partition[8];
78 VP8Macroblock *macroblocks;
79 VP8Macroblock *macroblocks_base;
82 uint8_t *intra4x4_pred_mode;
83 uint8_t *intra4x4_pred_mode_base;
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.
92 uint8_t (*top_nnz)[9];
93 DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
96 * This is the index plus one of the last non-zero coeff
97 * for each of the blocks in the current macroblock.
99 * 1 -> dc-only (special transform)
100 * 2+-> full transform
102 DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
103 DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
105 int chroma_pred_mode; ///< 8x8c pred mode of the current macroblock
108 int sign_bias[4]; ///< one state [0, 1] per ref frame type
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.
119 int8_t base_quant[4];
120 int8_t filter_level[4]; ///< base loop filter level
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.
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];
142 int enabled; ///< whether each mb can have a different strength based on mode/ref
145 * filter strength adjustment for the following macroblock modes:
148 * [2] - inter modes except for zero or split mv
150 * i16x16 modes never have any adjustment
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
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!
171 uint8_t segmentid[3];
176 uint8_t pred16x16[4];
178 uint8_t token[4][8][3][NUM_DCT_TOKENS-1];
183 #define RL24(p) (AV_RL16(p) + ((p)[2] << 16))
185 static void vp8_decode_flush(AVCodecContext *avctx)
187 VP8Context *s = avctx->priv_data;
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));
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);
200 s->macroblocks = NULL;
201 s->intra4x4_pred_mode = NULL;
204 static int update_dimensions(VP8Context *s, int width, int height)
208 if (avcodec_check_dimensions(s->avctx, width, height))
209 return AVERROR_INVALIDDATA;
211 vp8_decode_flush(s->avctx);
213 avcodec_set_dimensions(s->avctx, width, height);
215 s->mb_width = (s->avctx->coded_width +15) / 16;
216 s->mb_height = (s->avctx->coded_height+15) / 16;
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;
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));
227 s->macroblocks = s->macroblocks_base + 1 + s->mb_stride;
228 s->intra4x4_pred_mode = s->intra4x4_pred_mode_base + 4 + s->b4_stride;
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;
237 static void parse_segment_info(VP8Context *s)
239 VP56RangeCoder *c = &s->c;
242 s->segmentation.update_map = vp8_rac_get(c);
244 if (vp8_rac_get(c)) { // update segment feature data
245 s->segmentation.absolute_vals = vp8_rac_get(c);
247 for (i = 0; i < 4; i++)
248 s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
250 for (i = 0; i < 4; i++)
251 s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
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;
258 static void update_lf_deltas(VP8Context *s)
260 VP56RangeCoder *c = &s->c;
263 for (i = 0; i < 4; i++)
264 s->lf_delta.ref[i] = vp8_rac_get_sint(c, 6);
266 for (i = 0; i < 4; i++)
267 s->lf_delta.mode[i] = vp8_rac_get_sint(c, 6);
270 static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
272 const uint8_t *sizes = buf;
275 s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
277 buf += 3*(s->num_coeff_partitions-1);
278 buf_size -= 3*(s->num_coeff_partitions-1);
282 for (i = 0; i < s->num_coeff_partitions-1; i++) {
283 int size = RL24(sizes + 3*i);
284 if (buf_size - size < 0)
287 vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
291 vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
296 static void get_quants(VP8Context *s)
298 VP56RangeCoder *c = &s->c;
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);
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)
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)];
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);
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
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:
338 * 1: VP56_FRAME_PREVIOUS
339 * 2: update golden with altref, or update altref with golden
341 static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
343 VP56RangeCoder *c = &s->c;
346 return VP56_FRAME_CURRENT;
348 switch (vp8_rac_get_uint(c, 2)) {
350 return VP56_FRAME_PREVIOUS;
352 return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
354 return VP56_FRAME_NONE;
357 static void update_refs(VP8Context *s)
359 VP56RangeCoder *c = &s->c;
361 int update_golden = vp8_rac_get(c);
362 int update_altref = vp8_rac_get(c);
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);
368 static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
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;
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;
383 av_log(s->avctx, AV_LOG_WARNING, "Profile %d not fully handled\n", s->profile);
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;
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;
395 width = AV_RL16(buf+3) & 0x3fff;
396 height = AV_RL16(buf+5) & 0x3fff;
397 hscale = buf[4] >> 6;
398 vscale = buf[6] >> 6;
402 if (hscale || vscale)
403 av_log_missing_feature(s->avctx, "Upscaling", 1);
405 s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
406 memcpy(s->prob->token , vp8_token_default_probs , sizeof(s->prob->token));
407 memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
408 memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
409 memcpy(s->prob->mvc , vp8_mv_default_prob , sizeof(s->prob->mvc));
410 memset(&s->segmentation, 0, sizeof(s->segmentation));
413 if (!s->macroblocks_base || /* first frame */
414 width != s->avctx->width || height != s->avctx->height) {
415 if ((ret = update_dimensions(s, width, height) < 0))
419 vp56_init_range_decoder(c, buf, header_size);
421 buf_size -= header_size;
425 av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
426 vp8_rac_get(c); // whether we can skip clamping in dsp functions
429 if ((s->segmentation.enabled = vp8_rac_get(c)))
430 parse_segment_info(s);
432 s->segmentation.update_map = 0; // FIXME: move this to some init function?
434 s->filter.simple = vp8_rac_get(c);
435 s->filter.level = vp8_rac_get_uint(c, 6);
436 s->filter.sharpness = vp8_rac_get_uint(c, 3);
438 if ((s->lf_delta.enabled = vp8_rac_get(c)))
442 if (setup_partitions(s, buf, buf_size)) {
443 av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
444 return AVERROR_INVALIDDATA;
451 s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
452 s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
455 // if we aren't saving this frame's probabilities for future frames,
456 // make a copy of the current probabilities
457 if (!(s->update_probabilities = vp8_rac_get(c)))
458 s->prob[1] = s->prob[0];
460 s->update_last = s->keyframe || vp8_rac_get(c);
462 for (i = 0; i < 4; i++)
463 for (j = 0; j < 8; j++)
464 for (k = 0; k < 3; k++)
465 for (l = 0; l < NUM_DCT_TOKENS-1; l++)
466 if (vp56_rac_get_prob(c, vp8_token_update_probs[i][j][k][l]))
467 s->prob->token[i][j][k][l] = vp8_rac_get_uint(c, 8);
469 if ((s->mbskip_enabled = vp8_rac_get(c)))
470 s->prob->mbskip = vp8_rac_get_uint(c, 8);
473 s->prob->intra = vp8_rac_get_uint(c, 8);
474 s->prob->last = vp8_rac_get_uint(c, 8);
475 s->prob->golden = vp8_rac_get_uint(c, 8);
478 for (i = 0; i < 4; i++)
479 s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
481 for (i = 0; i < 3; i++)
482 s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
484 // 17.2 MV probability update
485 for (i = 0; i < 2; i++)
486 for (j = 0; j < 19; j++)
487 if (vp56_rac_get_prob(c, vp8_mv_update_prob[i][j]))
488 s->prob->mvc[i][j] = vp8_rac_get_nn(c);
494 static inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src,
497 #define MARGIN (16 << 2)
498 dst->x = av_clip(src->x, -((mb_x << 6) + MARGIN),
499 ((s->mb_width - 1 - mb_x) << 6) + MARGIN);
500 dst->y = av_clip(src->y, -((mb_y << 6) + MARGIN),
501 ((s->mb_height - 1 - mb_y) << 6) + MARGIN);
504 static void find_near_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
505 VP56mv near[2], VP56mv *best, int cnt[4])
507 VP8Macroblock *mb_edge[3] = { mb - s->mb_stride /* top */,
509 mb - s->mb_stride - 1 /* top-left */ };
510 enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT };
511 VP56mv near_mv[4] = {{ 0 }};
512 enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
513 int idx = CNT_ZERO, n;
514 int best_idx = CNT_ZERO;
516 /* Process MB on top, left and top-left */
517 for (n = 0; n < 3; n++) {
518 VP8Macroblock *edge = mb_edge[n];
519 if (edge->ref_frame != VP56_FRAME_CURRENT) {
520 if (edge->mv.x | edge->mv.y) {
521 VP56mv tmp = edge->mv;
522 if (s->sign_bias[mb->ref_frame] != s->sign_bias[edge->ref_frame]) {
526 if ((tmp.x ^ near_mv[idx].x) | (tmp.y ^ near_mv[idx].y))
527 near_mv[++idx] = tmp;
528 cnt[idx] += 1 + (n != 2);
530 cnt[CNT_ZERO] += 1 + (n != 2);
534 /* If we have three distinct MV's, merge first and last if they're the same */
535 if (cnt[CNT_SPLITMV] &&
536 !((near_mv[1+EDGE_TOP].x ^ near_mv[1+EDGE_TOPLEFT].x) |
537 (near_mv[1+EDGE_TOP].y ^ near_mv[1+EDGE_TOPLEFT].y)))
538 cnt[CNT_NEAREST] += 1;
540 cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
541 (mb_edge[EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
542 (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
544 /* Swap near and nearest if necessary */
545 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
546 FFSWAP(int, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
547 FFSWAP(VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
550 /* Choose the best mv out of 0,0 and the nearest mv */
551 if (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])
552 best_idx = CNT_NEAREST;
554 clamp_mv(s, best, &near_mv[best_idx], mb_x, mb_y);
555 near[0] = near_mv[CNT_NEAREST];
556 near[1] = near_mv[CNT_NEAR];
560 * Motion vector coding, 17.1.
562 static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
566 if (vp56_rac_get_prob(c, p[0])) {
569 for (i = 0; i < 3; i++)
570 x += vp56_rac_get_prob(c, p[9 + i]) << i;
571 for (i = 9; i > 3; i--)
572 x += vp56_rac_get_prob(c, p[9 + i]) << i;
573 if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
576 x = vp8_rac_get_tree(c, vp8_small_mvtree, &p[2]);
578 return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
581 static const uint8_t *get_submv_prob(const VP56mv *left, const VP56mv *top)
583 int l_is_zero = !(left->x | left->y);
584 int t_is_zero = !(top->x | top->y);
585 int equal = !((left->x ^ top->x) | (left->y ^ top->y));
588 return l_is_zero ? vp8_submv_prob[4] : vp8_submv_prob[3];
590 return vp8_submv_prob[2];
591 return l_is_zero ? vp8_submv_prob[1] : vp8_submv_prob[0];
595 * Split motion vector prediction, 16.4.
597 static void decode_splitmvs(VP8Context *s, VP56RangeCoder *c,
598 VP8Macroblock *mb, VP56mv *base_mv)
600 int part_idx = mb->partitioning =
601 vp8_rac_get_tree(c, vp8_mbsplit_tree, vp8_mbsplit_prob);
602 int n, num = vp8_mbsplit_count[part_idx];
605 for (n = 0; n < num; n++) {
606 int k = vp8_mbfirstidx[part_idx][n];
607 const VP56mv *left = (k & 3) ? &mb->bmv[k - 1] : &mb[-1].bmv[k + 3],
608 *above = (k > 3) ? &mb->bmv[k - 4] : &mb[-s->mb_stride].bmv[k + 12];
609 const uint8_t *submv_prob = get_submv_prob(left, above);
611 switch (vp8_rac_get_tree(c, vp8_submv_ref_tree, submv_prob)) {
612 case VP8_SUBMVMODE_NEW4X4:
613 part_mv[n].y = base_mv->y + read_mv_component(c, s->prob->mvc[0]);
614 part_mv[n].x = base_mv->x + read_mv_component(c, s->prob->mvc[1]);
616 case VP8_SUBMVMODE_ZERO4X4:
620 case VP8_SUBMVMODE_LEFT4X4:
623 case VP8_SUBMVMODE_TOP4X4:
628 /* fill out over the 4x4 blocks in MB */
629 for (k = 0; k < 16; k++)
630 if (vp8_mbsplits[part_idx][k] == n) {
631 mb->bmv[k] = part_mv[n];
636 static inline void decode_intra4x4_modes(VP56RangeCoder *c, uint8_t *intra4x4,
637 int stride, int keyframe)
640 const uint8_t *ctx = vp8_pred4x4_prob_inter;
642 for (y = 0; y < 4; y++) {
643 for (x = 0; x < 4; x++) {
645 t = intra4x4[x - stride];
647 ctx = vp8_pred4x4_prob_intra[t][l];
649 intra4x4[x] = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
655 static void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
658 VP56RangeCoder *c = &s->c;
661 if (s->segmentation.update_map)
662 mb->segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
664 mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
667 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
669 if (mb->mode == MODE_I4x4) {
670 decode_intra4x4_modes(c, intra4x4, s->b4_stride, 1);
672 fill_rectangle(intra4x4, 4, 4, s->b4_stride, vp8_pred4x4_mode[mb->mode], 1);
674 s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
675 mb->ref_frame = VP56_FRAME_CURRENT;
676 } else if (vp56_rac_get_prob(c, s->prob->intra)) {
677 VP56mv near[2], best;
682 if (vp56_rac_get_prob(c, s->prob->last))
683 mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
684 VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
686 mb->ref_frame = VP56_FRAME_PREVIOUS;
688 // motion vectors, 16.3
689 find_near_mvs(s, mb, mb_x, mb_y, near, &best, cnt);
690 for (n = 0; n < 4; n++)
691 p[n] = vp8_mode_contexts[cnt[n]][n];
692 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_mvinter, p);
694 case VP8_MVMODE_SPLIT:
695 decode_splitmvs(s, c, mb, &best);
696 mb->mv = mb->bmv[15];
698 case VP8_MVMODE_ZERO:
702 case VP8_MVMODE_NEAREST:
703 clamp_mv(s, &mb->mv, &near[0], mb_x, mb_y);
705 case VP8_MVMODE_NEAR:
706 clamp_mv(s, &mb->mv, &near[1], mb_x, mb_y);
709 mb->mv.y = best.y + read_mv_component(c, s->prob->mvc[0]);
710 mb->mv.x = best.x + read_mv_component(c, s->prob->mvc[1]);
713 if (mb->mode != VP8_MVMODE_SPLIT) {
714 for (n = 0; n < 16; n++)
719 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
721 if (mb->mode == MODE_I4x4) {
722 decode_intra4x4_modes(c, intra4x4, s->b4_stride, 0);
724 fill_rectangle(intra4x4, 4, 4, s->b4_stride, vp8_pred4x4_mode[mb->mode], 1);
726 s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
727 mb->ref_frame = VP56_FRAME_CURRENT;
732 * @param i initial coeff index, 0 unless a separate DC block is coded
733 * @param zero_nhood the initial prediction context for number of surrounding
734 * all-zero blocks (only left/top, so 0-2)
735 * @param qmul[0] dc dequant factor
736 * @param qmul[1] ac dequant factor
737 * @return 0 if no coeffs were decoded
738 * otherwise, the index of the last coeff decoded plus one
740 static int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
741 uint8_t probs[8][3][NUM_DCT_TOKENS-1],
742 int i, int zero_nhood, int16_t qmul[2])
744 int token, nonzero = 0;
747 for (; i < 16; i++) {
748 token = vp8_rac_get_tree_with_offset(c, vp8_coeff_tree, probs[vp8_coeff_band[i]][zero_nhood], offset);
750 if (token == DCT_EOB)
752 else if (token >= DCT_CAT1) {
753 int cat = token-DCT_CAT1;
754 token = vp8_rac_get_coeff(c, vp8_dct_cat_prob[cat]);
755 token += vp8_dct_cat_offset[cat];
758 // after the first token, the non-zero prediction context becomes
759 // based on the last decoded coeff
764 } else if (token == 1)
769 // todo: full [16] qmat? load into register?
770 block[zigzag_scan[i]] = (vp8_rac_get(c) ? -token : token) * qmul[!!i];
777 static void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
778 uint8_t t_nnz[9], uint8_t l_nnz[9])
780 LOCAL_ALIGNED_16(DCTELEM, dc,[16]);
781 int i, x, y, luma_start = 0, luma_ctx = 3;
782 int nnz_pred, nnz, nnz_total = 0;
783 int segment = s->segmentation.enabled ? mb->segment : 0;
785 s->dsp.clear_blocks((DCTELEM *)s->block);
787 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
790 nnz_pred = t_nnz[8] + l_nnz[8];
792 // decode DC values and do hadamard
793 nnz = decode_block_coeffs(c, dc, s->prob->token[1], 0, nnz_pred,
794 s->qmat[segment].luma_dc_qmul);
795 l_nnz[8] = t_nnz[8] = !!nnz;
797 s->vp8dsp.vp8_luma_dc_wht(s->block, dc);
803 for (y = 0; y < 4; y++)
804 for (x = 0; x < 4; x++) {
805 nnz_pred = l_nnz[y] + t_nnz[x];
806 nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
807 nnz_pred, s->qmat[segment].luma_qmul);
808 // nnz+luma_start may be one more than the actual last index, but we don't care
809 s->non_zero_count_cache[y][x] = nnz + luma_start;
810 t_nnz[x] = l_nnz[y] = !!nnz;
815 // TODO: what to do about dimensions? 2nd dim for luma is x,
816 // but for chroma it's (y<<1)|x
817 for (i = 4; i < 6; i++)
818 for (y = 0; y < 2; y++)
819 for (x = 0; x < 2; x++) {
820 nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
821 nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
822 nnz_pred, s->qmat[segment].chroma_qmul);
823 s->non_zero_count_cache[i][(y<<1)+x] = nnz;
824 t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
828 // if there were no coded coeffs despite the macroblock not being marked skip,
829 // we MUST not do the inner loop filter and should not do IDCT
830 // Since skip isn't used for bitstream prediction, just manually set it.
835 static int check_intra_pred_mode(int mode, int mb_x, int mb_y)
837 if (mode == DC_PRED8x8) {
839 mode = DC_128_PRED8x8;
841 mode = LEFT_DC_PRED8x8;
843 mode = TOP_DC_PRED8x8;
848 static void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
849 uint8_t *bmode, int mb_x, int mb_y)
851 int x, y, mode, nnz, tr;
853 if (mb->mode < MODE_I4x4) {
854 mode = check_intra_pred_mode(mb->mode, mb_x, mb_y);
855 s->hpc.pred16x16[mode](dst[0], s->linesize);
857 uint8_t *ptr = dst[0];
859 // all blocks on the right edge of the macroblock use bottom edge
860 // the top macroblock for their topright edge
861 uint8_t *tr_right = ptr - s->linesize + 16;
863 // if we're on the right edge of the frame, said edge is extended
864 // from the top macroblock
865 if (mb_x == s->mb_width-1) {
866 tr = tr_right[-1]*0x01010101;
867 tr_right = (uint8_t *)&tr;
870 for (y = 0; y < 4; y++) {
871 uint8_t *topright = ptr + 4 - s->linesize;
872 for (x = 0; x < 4; x++) {
876 s->hpc.pred4x4[bmode[x]](ptr+4*x, topright, s->linesize);
878 nnz = s->non_zero_count_cache[y][x];
881 s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
883 s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
888 ptr += 4*s->linesize;
889 bmode += s->b4_stride;
893 mode = check_intra_pred_mode(s->chroma_pred_mode, mb_x, mb_y);
894 s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
895 s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
899 * Generic MC function.
901 * @param s VP8 decoding context
902 * @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
903 * @param dst target buffer for block data at block position
904 * @param src reference picture buffer at origin (0, 0)
905 * @param mv motion vector (relative to block position) to get pixel data from
906 * @param x_off horizontal position of block from origin (0, 0)
907 * @param y_off vertical position of block from origin (0, 0)
908 * @param block_w width of block (16, 8 or 4)
909 * @param block_h height of block (always same as block_w)
910 * @param width width of src/dst plane data
911 * @param height height of src/dst plane data
912 * @param linesize size of a single line of plane data, including padding
914 static inline void vp8_mc(VP8Context *s, int luma,
915 uint8_t *dst, uint8_t *src, const VP56mv *mv,
916 int x_off, int y_off, int block_w, int block_h,
917 int width, int height, int linesize,
918 vp8_mc_func mc_func[3][3])
920 static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };
921 int mx = (mv->x << luma)&7, mx_idx = idx[mx];
922 int my = (mv->y << luma)&7, my_idx = idx[my];
924 x_off += mv->x >> (3 - luma);
925 y_off += mv->y >> (3 - luma);
928 src += y_off * linesize + x_off;
929 if (x_off < 2 || x_off >= width - block_w - 3 ||
930 y_off < 2 || y_off >= height - block_h - 3) {
931 ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,
932 block_w + 5, block_h + 5,
933 x_off - 2, y_off - 2, width, height);
934 src = s->edge_emu_buffer + 2 + linesize * 2;
937 mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
941 * Apply motion vectors to prediction buffer, chapter 18.
943 static void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
946 int x_off = mb_x << 4, y_off = mb_y << 4;
947 int width = 16*s->mb_width, height = 16*s->mb_height;
950 if (mb->mode < VP8_MVMODE_SPLIT) {
952 vp8_mc(s, 1, dst[0], s->framep[mb->ref_frame]->data[0], &mb->mv,
953 x_off, y_off, 16, 16, width, height, s->linesize,
954 s->vp8dsp.put_vp8_epel_pixels_tab[0]);
958 if (s->profile == 3) {
962 x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
963 vp8_mc(s, 0, dst[1], s->framep[mb->ref_frame]->data[1], &uvmv,
964 x_off, y_off, 8, 8, width, height, s->uvlinesize,
965 s->vp8dsp.put_vp8_epel_pixels_tab[1]);
966 vp8_mc(s, 0, dst[2], s->framep[mb->ref_frame]->data[2], &uvmv,
967 x_off, y_off, 8, 8, width, height, s->uvlinesize,
968 s->vp8dsp.put_vp8_epel_pixels_tab[1]);
973 for (y = 0; y < 4; y++) {
974 for (x = 0; x < 4; x++) {
975 vp8_mc(s, 1, dst[0] + 4*y*s->linesize + x*4,
976 s->framep[mb->ref_frame]->data[0], &mb->bmv[4*y + x],
977 4*x + x_off, 4*y + y_off, 4, 4,
978 width, height, s->linesize,
979 s->vp8dsp.put_vp8_epel_pixels_tab[2]);
984 x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
985 for (y = 0; y < 2; y++) {
986 for (x = 0; x < 2; x++) {
987 uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
988 mb->bmv[ 2*y * 4 + 2*x+1].x +
989 mb->bmv[(2*y+1) * 4 + 2*x ].x +
990 mb->bmv[(2*y+1) * 4 + 2*x+1].x;
991 uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
992 mb->bmv[ 2*y * 4 + 2*x+1].y +
993 mb->bmv[(2*y+1) * 4 + 2*x ].y +
994 mb->bmv[(2*y+1) * 4 + 2*x+1].y;
995 uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
996 uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
997 if (s->profile == 3) {
1001 vp8_mc(s, 0, dst[1] + 4*y*s->uvlinesize + x*4,
1002 s->framep[mb->ref_frame]->data[1], &uvmv,
1003 4*x + x_off, 4*y + y_off, 4, 4,
1004 width, height, s->uvlinesize,
1005 s->vp8dsp.put_vp8_epel_pixels_tab[2]);
1006 vp8_mc(s, 0, dst[2] + 4*y*s->uvlinesize + x*4,
1007 s->framep[mb->ref_frame]->data[2], &uvmv,
1008 4*x + x_off, 4*y + y_off, 4, 4,
1009 width, height, s->uvlinesize,
1010 s->vp8dsp.put_vp8_epel_pixels_tab[2]);
1016 static void idct_mb(VP8Context *s, uint8_t *y_dst, uint8_t *u_dst, uint8_t *v_dst,
1021 if (mb->mode != MODE_I4x4)
1022 for (y = 0; y < 4; y++) {
1023 for (x = 0; x < 4; x++) {
1024 nnz = s->non_zero_count_cache[y][x];
1027 s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
1029 s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
1032 y_dst += 4*s->linesize;
1035 for (y = 0; y < 2; y++) {
1036 for (x = 0; x < 2; x++) {
1037 nnz = s->non_zero_count_cache[4][(y<<1)+x];
1040 s->vp8dsp.vp8_idct_dc_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1042 s->vp8dsp.vp8_idct_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1045 nnz = s->non_zero_count_cache[5][(y<<1)+x];
1048 s->vp8dsp.vp8_idct_dc_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1050 s->vp8dsp.vp8_idct_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1053 u_dst += 4*s->uvlinesize;
1054 v_dst += 4*s->uvlinesize;
1058 static void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, int *level, int *inner, int *hev_thresh)
1060 int interior_limit, filter_level;
1062 if (s->segmentation.enabled) {
1063 filter_level = s->segmentation.filter_level[mb->segment];
1064 if (!s->segmentation.absolute_vals)
1065 filter_level += s->filter.level;
1067 filter_level = s->filter.level;
1069 if (s->lf_delta.enabled) {
1070 filter_level += s->lf_delta.ref[mb->ref_frame];
1072 if (mb->ref_frame == VP56_FRAME_CURRENT) {
1073 if (mb->mode == MODE_I4x4)
1074 filter_level += s->lf_delta.mode[0];
1076 if (mb->mode == VP8_MVMODE_ZERO)
1077 filter_level += s->lf_delta.mode[1];
1078 else if (mb->mode == VP8_MVMODE_SPLIT)
1079 filter_level += s->lf_delta.mode[3];
1081 filter_level += s->lf_delta.mode[2];
1084 filter_level = av_clip(filter_level, 0, 63);
1086 interior_limit = filter_level;
1087 if (s->filter.sharpness) {
1088 interior_limit >>= s->filter.sharpness > 4 ? 2 : 1;
1089 interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
1091 interior_limit = FFMAX(interior_limit, 1);
1093 *level = filter_level;
1094 *inner = interior_limit;
1097 *hev_thresh = filter_level >= 15;
1100 if (filter_level >= 40)
1103 if (filter_level >= 40)
1105 else if (filter_level >= 20)
1111 // TODO: look at backup_mb_border / xchg_mb_border in h264.c
1112 static void filter_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb, int mb_x, int mb_y)
1114 int filter_level, inner_limit, hev_thresh;
1116 filter_level_for_mb(s, mb, &filter_level, &inner_limit, &hev_thresh);
1121 s->vp8dsp.vp8_h_loop_filter16(dst[0], s->linesize, filter_level+2, inner_limit, hev_thresh);
1122 s->vp8dsp.vp8_h_loop_filter8 (dst[1], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
1123 s->vp8dsp.vp8_h_loop_filter8 (dst[2], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
1126 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1127 s->vp8dsp.vp8_h_loop_filter16_inner(dst[0]+ 4, s->linesize, filter_level, inner_limit, hev_thresh);
1128 s->vp8dsp.vp8_h_loop_filter16_inner(dst[0]+ 8, s->linesize, filter_level, inner_limit, hev_thresh);
1129 s->vp8dsp.vp8_h_loop_filter16_inner(dst[0]+12, s->linesize, filter_level, inner_limit, hev_thresh);
1130 s->vp8dsp.vp8_h_loop_filter8_inner (dst[1]+ 4, s->uvlinesize, filter_level, inner_limit, hev_thresh);
1131 s->vp8dsp.vp8_h_loop_filter8_inner (dst[2]+ 4, s->uvlinesize, filter_level, inner_limit, hev_thresh);
1135 s->vp8dsp.vp8_v_loop_filter16(dst[0], s->linesize, filter_level+2, inner_limit, hev_thresh);
1136 s->vp8dsp.vp8_v_loop_filter8 (dst[1], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
1137 s->vp8dsp.vp8_v_loop_filter8 (dst[2], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
1140 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1141 s->vp8dsp.vp8_v_loop_filter16_inner(dst[0]+ 4*s->linesize, s->linesize, filter_level, inner_limit, hev_thresh);
1142 s->vp8dsp.vp8_v_loop_filter16_inner(dst[0]+ 8*s->linesize, s->linesize, filter_level, inner_limit, hev_thresh);
1143 s->vp8dsp.vp8_v_loop_filter16_inner(dst[0]+12*s->linesize, s->linesize, filter_level, inner_limit, hev_thresh);
1144 s->vp8dsp.vp8_v_loop_filter8_inner (dst[1]+ 4*s->uvlinesize, s->uvlinesize, filter_level, inner_limit, hev_thresh);
1145 s->vp8dsp.vp8_v_loop_filter8_inner (dst[2]+ 4*s->uvlinesize, s->uvlinesize, filter_level, inner_limit, hev_thresh);
1149 static void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8Macroblock *mb, int mb_x, int mb_y)
1151 int filter_level, inner_limit, mbedge_lim, bedge_lim;
1153 filter_level_for_mb(s, mb, &filter_level, &inner_limit, NULL);
1157 mbedge_lim = 2*(filter_level+2) + inner_limit;
1158 bedge_lim = 2* filter_level + inner_limit;
1161 s->vp8dsp.vp8_h_loop_filter_simple(dst, s->linesize, mbedge_lim);
1162 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1163 s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, s->linesize, bedge_lim);
1164 s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, s->linesize, bedge_lim);
1165 s->vp8dsp.vp8_h_loop_filter_simple(dst+12, s->linesize, bedge_lim);
1169 s->vp8dsp.vp8_v_loop_filter_simple(dst, s->linesize, mbedge_lim);
1170 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1171 s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*s->linesize, s->linesize, bedge_lim);
1172 s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*s->linesize, s->linesize, bedge_lim);
1173 s->vp8dsp.vp8_v_loop_filter_simple(dst+12*s->linesize, s->linesize, bedge_lim);
1177 static void filter_mb_row(VP8Context *s, int mb_y)
1179 VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1181 s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize,
1182 s->framep[VP56_FRAME_CURRENT]->data[1] + 8*mb_y*s->uvlinesize,
1183 s->framep[VP56_FRAME_CURRENT]->data[2] + 8*mb_y*s->uvlinesize
1187 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1188 filter_mb(s, dst, mb++, mb_x, mb_y);
1195 static void filter_mb_row_simple(VP8Context *s, int mb_y)
1197 uint8_t *dst = s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize;
1198 VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1201 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1202 filter_mb_simple(s, dst, mb++, mb_x, mb_y);
1207 static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1210 VP8Context *s = avctx->priv_data;
1211 int ret, mb_x, mb_y, i, y, referenced;
1212 enum AVDiscard skip_thresh;
1215 if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
1218 referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
1219 || s->update_altref == VP56_FRAME_CURRENT;
1221 skip_thresh = !referenced ? AVDISCARD_NONREF :
1222 !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
1224 if (avctx->skip_frame >= skip_thresh) {
1229 for (i = 0; i < 4; i++)
1230 if (&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1231 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1232 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
1233 curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
1236 if (curframe->data[0])
1237 avctx->release_buffer(avctx, curframe);
1239 curframe->key_frame = s->keyframe;
1240 curframe->pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
1241 curframe->reference = referenced ? 3 : 0;
1242 if ((ret = avctx->get_buffer(avctx, curframe))) {
1243 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
1247 // Given that arithmetic probabilities are updated every frame, it's quite likely
1248 // that the values we have on a random interframe are complete junk if we didn't
1249 // start decode on a keyframe. So just don't display anything rather than junk.
1250 if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
1251 !s->framep[VP56_FRAME_GOLDEN] ||
1252 !s->framep[VP56_FRAME_GOLDEN2])) {
1253 av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
1254 return AVERROR_INVALIDDATA;
1257 s->linesize = curframe->linesize[0];
1258 s->uvlinesize = curframe->linesize[1];
1260 if (!s->edge_emu_buffer)
1261 s->edge_emu_buffer = av_malloc(21*s->linesize);
1263 memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
1265 // top edge of 127 for intra prediction
1266 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1267 memset(curframe->data[0] - s->linesize -1, 127, s->linesize +1);
1268 memset(curframe->data[1] - s->uvlinesize-1, 127, s->uvlinesize+1);
1269 memset(curframe->data[2] - s->uvlinesize-1, 127, s->uvlinesize+1);
1272 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1273 VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
1274 VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1275 uint8_t *intra4x4 = s->intra4x4_pred_mode + 4*mb_y*s->b4_stride;
1277 curframe->data[0] + 16*mb_y*s->linesize,
1278 curframe->data[1] + 8*mb_y*s->uvlinesize,
1279 curframe->data[2] + 8*mb_y*s->uvlinesize
1282 memset(s->left_nnz, 0, sizeof(s->left_nnz));
1284 // left edge of 129 for intra prediction
1285 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE))
1286 for (i = 0; i < 3; i++)
1287 for (y = 0; y < 16>>!!i; y++)
1288 dst[i][y*curframe->linesize[i]-1] = 129;
1290 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1291 decode_mb_mode(s, mb, mb_x, mb_y, intra4x4 + 4*mb_x);
1294 decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
1296 AV_ZERO128(s->non_zero_count_cache); // luma
1297 AV_ZERO64(s->non_zero_count_cache[4]); // chroma
1300 if (mb->mode <= MODE_I4x4) {
1301 intra_predict(s, dst, mb, intra4x4 + 4*mb_x, mb_x, mb_y);
1302 memset(mb->bmv, 0, sizeof(mb->bmv));
1304 inter_predict(s, dst, mb, mb_x, mb_y);
1308 idct_mb(s, dst[0], dst[1], dst[2], mb);
1310 AV_ZERO64(s->left_nnz);
1311 AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
1313 // Reset DC block predictors if they would exist if the mb had coefficients
1314 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
1316 s->top_nnz[mb_x][8] = 0;
1325 if (mb_y && s->filter.level && avctx->skip_loop_filter < skip_thresh) {
1326 if (s->filter.simple)
1327 filter_mb_row_simple(s, mb_y-1);
1329 filter_mb_row(s, mb_y-1);
1332 if (s->filter.level && avctx->skip_loop_filter < skip_thresh) {
1333 if (s->filter.simple)
1334 filter_mb_row_simple(s, mb_y-1);
1336 filter_mb_row(s, mb_y-1);
1340 // if future frames don't use the updated probabilities,
1341 // reset them to the values we saved
1342 if (!s->update_probabilities)
1343 s->prob[0] = s->prob[1];
1345 // check if golden and altref are swapped
1346 if (s->update_altref == VP56_FRAME_GOLDEN &&
1347 s->update_golden == VP56_FRAME_GOLDEN2)
1348 FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN], s->framep[VP56_FRAME_GOLDEN2]);
1350 if (s->update_altref != VP56_FRAME_NONE)
1351 s->framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
1353 if (s->update_golden != VP56_FRAME_NONE)
1354 s->framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
1357 if (s->update_last) // move cur->prev
1358 s->framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_CURRENT];
1360 // release no longer referenced frames
1361 for (i = 0; i < 4; i++)
1362 if (s->frames[i].data[0] &&
1363 &s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
1364 &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1365 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1366 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
1367 avctx->release_buffer(avctx, &s->frames[i]);
1369 if (!s->invisible) {
1370 *(AVFrame*)data = *s->framep[VP56_FRAME_CURRENT];
1371 *data_size = sizeof(AVFrame);
1377 static av_cold int vp8_decode_init(AVCodecContext *avctx)
1379 VP8Context *s = avctx->priv_data;
1382 avctx->pix_fmt = PIX_FMT_YUV420P;
1384 dsputil_init(&s->dsp, avctx);
1385 ff_h264_pred_init(&s->hpc, CODEC_ID_VP8);
1386 ff_vp8dsp_init(&s->vp8dsp);
1388 // intra pred needs edge emulation among other things
1389 if (avctx->flags&CODEC_FLAG_EMU_EDGE) {
1390 av_log(avctx, AV_LOG_ERROR, "Edge emulation not supported\n");
1391 return AVERROR_PATCHWELCOME;
1397 static av_cold int vp8_decode_free(AVCodecContext *avctx)
1399 vp8_decode_flush(avctx);
1403 AVCodec vp8_decoder = {
1413 .flush = vp8_decode_flush,
1414 .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),