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;
48 vp8_mc_func put_pixels_tab[3][3][3];
51 uint8_t *edge_emu_buffer;
52 VP56RangeCoder c; ///< header context, includes mb modes and motion vectors
55 int mb_width; /* number of horizontal MB */
56 int mb_height; /* number of vertical MB */
62 int update_last; ///< update VP56_FRAME_PREVIOUS with the current one
63 int update_golden; ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
68 * If this flag is not set, all the probability updates
69 * are discarded after this frame is decoded.
71 int update_probabilities;
74 * All coefficients are contained in separate arith coding contexts.
75 * There can be 1, 2, 4, or 8 of these after the header context.
77 int num_coeff_partitions;
78 VP56RangeCoder coeff_partition[8];
80 VP8Macroblock *macroblocks;
81 VP8Macroblock *macroblocks_base;
84 uint8_t *intra4x4_pred_mode;
85 uint8_t *intra4x4_pred_mode_base;
89 * Cache of the top row needed for intra prediction
90 * 16 for luma, 8 for each chroma plane
92 uint8_t (*top_border)[16+8+8];
95 * For coeff decode, we need to know whether the above block had non-zero
96 * coefficients. This means for each macroblock, we need data for 4 luma
97 * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
98 * per macroblock. We keep the last row in top_nnz.
100 uint8_t (*top_nnz)[9];
101 DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
104 * This is the index plus one of the last non-zero coeff
105 * for each of the blocks in the current macroblock.
107 * 1 -> dc-only (special transform)
108 * 2+-> full transform
110 DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
111 DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
113 int chroma_pred_mode; ///< 8x8c pred mode of the current macroblock
116 int sign_bias[4]; ///< one state [0, 1] per ref frame type
119 * Base parameters for segmentation, i.e. per-macroblock parameters.
120 * These must be kept unchanged even if segmentation is not used for
121 * a frame, since the values persist between interframes.
127 int8_t base_quant[4];
128 int8_t filter_level[4]; ///< base loop filter level
132 * Macroblocks can have one of 4 different quants in a frame when
133 * segmentation is enabled.
134 * If segmentation is disabled, only the first segment's values are used.
137 // [0] - DC qmul [1] - AC qmul
138 int16_t luma_qmul[2];
139 int16_t luma_dc_qmul[2]; ///< luma dc-only block quant
140 int16_t chroma_qmul[2];
150 int enabled; ///< whether each mb can have a different strength based on mode/ref
153 * filter strength adjustment for the following macroblock modes:
156 * [2] - inter modes except for zero or split mv
158 * i16x16 modes never have any adjustment
163 * filter strength adjustment for macroblocks that reference:
164 * [0] - intra / VP56_FRAME_CURRENT
165 * [1] - VP56_FRAME_PREVIOUS
166 * [2] - VP56_FRAME_GOLDEN
167 * [3] - altref / VP56_FRAME_GOLDEN2
173 * These are all of the updatable probabilities for binary decisions.
174 * They are only implictly reset on keyframes, making it quite likely
175 * for an interframe to desync if a prior frame's header was corrupt
176 * or missing outright!
179 uint8_t segmentid[3];
184 uint8_t pred16x16[4];
186 uint8_t token[4][8][3][NUM_DCT_TOKENS-1];
191 #define RL24(p) (AV_RL16(p) + ((p)[2] << 16))
193 static void vp8_decode_flush(AVCodecContext *avctx)
195 VP8Context *s = avctx->priv_data;
198 for (i = 0; i < 4; i++)
199 if (s->frames[i].data[0])
200 avctx->release_buffer(avctx, &s->frames[i]);
201 memset(s->framep, 0, sizeof(s->framep));
203 av_freep(&s->macroblocks_base);
204 av_freep(&s->intra4x4_pred_mode_base);
205 av_freep(&s->top_nnz);
206 av_freep(&s->edge_emu_buffer);
207 av_freep(&s->top_border);
209 s->macroblocks = NULL;
210 s->intra4x4_pred_mode = NULL;
213 static int update_dimensions(VP8Context *s, int width, int height)
217 if (avcodec_check_dimensions(s->avctx, width, height))
218 return AVERROR_INVALIDDATA;
220 vp8_decode_flush(s->avctx);
222 avcodec_set_dimensions(s->avctx, width, height);
224 s->mb_width = (s->avctx->coded_width +15) / 16;
225 s->mb_height = (s->avctx->coded_height+15) / 16;
227 // we allocate a border around the top/left of intra4x4 modes
228 // this is 4 blocks for intra4x4 to keep 4-byte alignment for fill_rectangle
229 s->mb_stride = s->mb_width+1;
230 s->b4_stride = 4*s->mb_stride;
232 s->macroblocks_base = av_mallocz(s->mb_stride*(s->mb_height+1)*sizeof(*s->macroblocks));
233 s->intra4x4_pred_mode_base = av_mallocz(s->b4_stride*(4*s->mb_height+1));
234 s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
235 s->top_border = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
237 if (!s->macroblocks_base || !s->intra4x4_pred_mode_base || !s->top_nnz || !s->top_border)
238 return AVERROR(ENOMEM);
240 s->macroblocks = s->macroblocks_base + 1 + s->mb_stride;
241 s->intra4x4_pred_mode = s->intra4x4_pred_mode_base + 4 + s->b4_stride;
243 memset(s->intra4x4_pred_mode_base, DC_PRED, s->b4_stride);
244 for (i = 0; i < 4*s->mb_height; i++)
245 s->intra4x4_pred_mode[i*s->b4_stride-1] = DC_PRED;
250 static void parse_segment_info(VP8Context *s)
252 VP56RangeCoder *c = &s->c;
255 s->segmentation.update_map = vp8_rac_get(c);
257 if (vp8_rac_get(c)) { // update segment feature data
258 s->segmentation.absolute_vals = vp8_rac_get(c);
260 for (i = 0; i < 4; i++)
261 s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
263 for (i = 0; i < 4; i++)
264 s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
266 if (s->segmentation.update_map)
267 for (i = 0; i < 3; i++)
268 s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
271 static void update_lf_deltas(VP8Context *s)
273 VP56RangeCoder *c = &s->c;
276 for (i = 0; i < 4; i++)
277 s->lf_delta.ref[i] = vp8_rac_get_sint(c, 6);
279 for (i = 0; i < 4; i++)
280 s->lf_delta.mode[i] = vp8_rac_get_sint(c, 6);
283 static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
285 const uint8_t *sizes = buf;
288 s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
290 buf += 3*(s->num_coeff_partitions-1);
291 buf_size -= 3*(s->num_coeff_partitions-1);
295 for (i = 0; i < s->num_coeff_partitions-1; i++) {
296 int size = RL24(sizes + 3*i);
297 if (buf_size - size < 0)
300 vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
304 vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
309 static void get_quants(VP8Context *s)
311 VP56RangeCoder *c = &s->c;
314 int yac_qi = vp8_rac_get_uint(c, 7);
315 int ydc_delta = vp8_rac_get_sint(c, 4);
316 int y2dc_delta = vp8_rac_get_sint(c, 4);
317 int y2ac_delta = vp8_rac_get_sint(c, 4);
318 int uvdc_delta = vp8_rac_get_sint(c, 4);
319 int uvac_delta = vp8_rac_get_sint(c, 4);
321 for (i = 0; i < 4; i++) {
322 if (s->segmentation.enabled) {
323 base_qi = s->segmentation.base_quant[i];
324 if (!s->segmentation.absolute_vals)
329 s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + ydc_delta , 0, 127)];
330 s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi , 0, 127)];
331 s->qmat[i].luma_dc_qmul[0] = 2 * vp8_dc_qlookup[av_clip(base_qi + y2dc_delta, 0, 127)];
332 s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip(base_qi + y2ac_delta, 0, 127)] / 100;
333 s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + uvdc_delta, 0, 127)];
334 s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi + uvac_delta, 0, 127)];
336 s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
337 s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
342 * Determine which buffers golden and altref should be updated with after this frame.
343 * The spec isn't clear here, so I'm going by my understanding of what libvpx does
345 * Intra frames update all 3 references
346 * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
347 * If the update (golden|altref) flag is set, it's updated with the current frame
348 * if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
349 * If the flag is not set, the number read means:
351 * 1: VP56_FRAME_PREVIOUS
352 * 2: update golden with altref, or update altref with golden
354 static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
356 VP56RangeCoder *c = &s->c;
359 return VP56_FRAME_CURRENT;
361 switch (vp8_rac_get_uint(c, 2)) {
363 return VP56_FRAME_PREVIOUS;
365 return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
367 return VP56_FRAME_NONE;
370 static void update_refs(VP8Context *s)
372 VP56RangeCoder *c = &s->c;
374 int update_golden = vp8_rac_get(c);
375 int update_altref = vp8_rac_get(c);
377 s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
378 s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
381 static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
383 VP56RangeCoder *c = &s->c;
384 int header_size, hscale, vscale, i, j, k, l, ret;
385 int width = s->avctx->width;
386 int height = s->avctx->height;
388 s->keyframe = !(buf[0] & 1);
389 s->profile = (buf[0]>>1) & 7;
390 s->invisible = !(buf[0] & 0x10);
391 header_size = RL24(buf) >> 5;
396 av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
399 memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
400 else // profile 1-3 use bilinear, 4+ aren't defined so whatever
401 memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
403 if (header_size > buf_size - 7*s->keyframe) {
404 av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
405 return AVERROR_INVALIDDATA;
409 if (RL24(buf) != 0x2a019d) {
410 av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", RL24(buf));
411 return AVERROR_INVALIDDATA;
413 width = AV_RL16(buf+3) & 0x3fff;
414 height = AV_RL16(buf+5) & 0x3fff;
415 hscale = buf[4] >> 6;
416 vscale = buf[6] >> 6;
420 if (hscale || vscale)
421 av_log_missing_feature(s->avctx, "Upscaling", 1);
423 s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
424 memcpy(s->prob->token , vp8_token_default_probs , sizeof(s->prob->token));
425 memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
426 memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
427 memcpy(s->prob->mvc , vp8_mv_default_prob , sizeof(s->prob->mvc));
428 memset(&s->segmentation, 0, sizeof(s->segmentation));
431 if (!s->macroblocks_base || /* first frame */
432 width != s->avctx->width || height != s->avctx->height) {
433 if ((ret = update_dimensions(s, width, height) < 0))
437 vp56_init_range_decoder(c, buf, header_size);
439 buf_size -= header_size;
443 av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
444 vp8_rac_get(c); // whether we can skip clamping in dsp functions
447 if ((s->segmentation.enabled = vp8_rac_get(c)))
448 parse_segment_info(s);
450 s->segmentation.update_map = 0; // FIXME: move this to some init function?
452 s->filter.simple = vp8_rac_get(c);
453 s->filter.level = vp8_rac_get_uint(c, 6);
454 s->filter.sharpness = vp8_rac_get_uint(c, 3);
456 if ((s->lf_delta.enabled = vp8_rac_get(c)))
460 if (setup_partitions(s, buf, buf_size)) {
461 av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
462 return AVERROR_INVALIDDATA;
469 s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
470 s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
473 // if we aren't saving this frame's probabilities for future frames,
474 // make a copy of the current probabilities
475 if (!(s->update_probabilities = vp8_rac_get(c)))
476 s->prob[1] = s->prob[0];
478 s->update_last = s->keyframe || vp8_rac_get(c);
480 for (i = 0; i < 4; i++)
481 for (j = 0; j < 8; j++)
482 for (k = 0; k < 3; k++)
483 for (l = 0; l < NUM_DCT_TOKENS-1; l++)
484 if (vp56_rac_get_prob(c, vp8_token_update_probs[i][j][k][l]))
485 s->prob->token[i][j][k][l] = vp8_rac_get_uint(c, 8);
487 if ((s->mbskip_enabled = vp8_rac_get(c)))
488 s->prob->mbskip = vp8_rac_get_uint(c, 8);
491 s->prob->intra = vp8_rac_get_uint(c, 8);
492 s->prob->last = vp8_rac_get_uint(c, 8);
493 s->prob->golden = vp8_rac_get_uint(c, 8);
496 for (i = 0; i < 4; i++)
497 s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
499 for (i = 0; i < 3; i++)
500 s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
502 // 17.2 MV probability update
503 for (i = 0; i < 2; i++)
504 for (j = 0; j < 19; j++)
505 if (vp56_rac_get_prob(c, vp8_mv_update_prob[i][j]))
506 s->prob->mvc[i][j] = vp8_rac_get_nn(c);
512 static inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src,
515 #define MARGIN (16 << 2)
516 dst->x = av_clip(src->x, -((mb_x << 6) + MARGIN),
517 ((s->mb_width - 1 - mb_x) << 6) + MARGIN);
518 dst->y = av_clip(src->y, -((mb_y << 6) + MARGIN),
519 ((s->mb_height - 1 - mb_y) << 6) + MARGIN);
522 static void find_near_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
523 VP56mv near[2], VP56mv *best, uint8_t cnt[4])
525 int mb_stride = s->mb_stride;
526 VP8Macroblock *mb_edge[3] = { mb - mb_stride /* top */,
528 mb - mb_stride - 1 /* top-left */ };
529 enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT };
530 VP56mv near_mv[4] = {{ 0 }};
531 enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
533 int best_idx = CNT_ZERO;
534 int cur_sign_bias = s->sign_bias[mb->ref_frame];
535 int *sign_bias = s->sign_bias;
537 /* Process MB on top, left and top-left */
538 #define MV_EDGE_CHECK(n)\
540 VP8Macroblock *edge = mb_edge[n];\
541 int edge_ref = edge->ref_frame;\
542 if (edge_ref != VP56_FRAME_CURRENT) {\
543 uint32_t mv = AV_RN32A(&edge->mv);\
545 if (cur_sign_bias != sign_bias[edge_ref]) {\
546 /* SWAR negate of the values in mv. */\
547 mv = ((mv&0x80008000) + 0x00010001) ^ (mv&0x7fff7fff);\
549 if (!n || mv != AV_RN32A(&near_mv[idx]))\
550 AV_WN32A(&near_mv[++idx], mv);\
551 cnt[idx] += 1 + (n != 2);\
553 cnt[CNT_ZERO] += 1 + (n != 2);\
560 /* If we have three distinct MVs, merge first and last if they're the same */
561 if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1+EDGE_TOP]) == AV_RN32A(&near_mv[1+EDGE_TOPLEFT]))
562 cnt[CNT_NEAREST] += 1;
564 cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
565 (mb_edge[EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
566 (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
568 /* Swap near and nearest if necessary */
569 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
570 FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
571 FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
574 /* Choose the best mv out of 0,0 and the nearest mv */
575 if (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])
576 best_idx = CNT_NEAREST;
578 clamp_mv(s, best, &near_mv[best_idx], mb_x, mb_y);
579 near[0] = near_mv[CNT_NEAREST];
580 near[1] = near_mv[CNT_NEAR];
584 * Motion vector coding, 17.1.
586 static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
590 if (vp56_rac_get_prob(c, p[0])) {
593 for (i = 0; i < 3; i++)
594 x += vp56_rac_get_prob(c, p[9 + i]) << i;
595 for (i = 9; i > 3; i--)
596 x += vp56_rac_get_prob(c, p[9 + i]) << i;
597 if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
600 x = vp8_rac_get_tree(c, vp8_small_mvtree, &p[2]);
602 return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
605 static const uint8_t *get_submv_prob(uint32_t left, uint32_t top)
608 return vp8_submv_prob[4-!!left];
610 return vp8_submv_prob[2];
611 return vp8_submv_prob[1-!!left];
615 * Split motion vector prediction, 16.4.
616 * @returns the number of motion vectors parsed (2, 4 or 16)
618 static int decode_splitmvs(VP8Context *s, VP56RangeCoder *c,
619 VP8Macroblock *mb, VP56mv *base_mv)
621 int part_idx = mb->partitioning =
622 vp8_rac_get_tree(c, vp8_mbsplit_tree, vp8_mbsplit_prob);
623 int n, num = vp8_mbsplit_count[part_idx];
624 VP8Macroblock *top_mb = &mb[-s->mb_stride];
625 VP8Macroblock *left_mb = &mb[-1];
626 const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning],
627 *mbsplits_top = vp8_mbsplits[top_mb->partitioning],
628 *mbsplits_cur = vp8_mbsplits[part_idx],
629 *firstidx = vp8_mbfirstidx[part_idx];
630 VP56mv *top_mv = top_mb->bmv;
631 VP56mv *left_mv = left_mb->bmv;
632 VP56mv *cur_mv = mb->bmv;
634 for (n = 0; n < num; n++) {
636 uint32_t left, above;
637 const uint8_t *submv_prob;
640 left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
642 left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
644 above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
646 above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
648 submv_prob = get_submv_prob(left, above);
650 switch (vp8_rac_get_tree(c, vp8_submv_ref_tree, submv_prob)) {
651 case VP8_SUBMVMODE_NEW4X4:
652 mb->bmv[n].y = base_mv->y + read_mv_component(c, s->prob->mvc[0]);
653 mb->bmv[n].x = base_mv->x + read_mv_component(c, s->prob->mvc[1]);
655 case VP8_SUBMVMODE_ZERO4X4:
656 AV_WN32A(&mb->bmv[n], 0);
658 case VP8_SUBMVMODE_LEFT4X4:
659 AV_WN32A(&mb->bmv[n], left);
661 case VP8_SUBMVMODE_TOP4X4:
662 AV_WN32A(&mb->bmv[n], above);
670 static inline void decode_intra4x4_modes(VP56RangeCoder *c, uint8_t *intra4x4,
671 int stride, int keyframe)
674 const uint8_t *ctx = vp8_pred4x4_prob_inter;
676 for (y = 0; y < 4; y++) {
677 for (x = 0; x < 4; x++) {
679 t = intra4x4[x - stride];
681 ctx = vp8_pred4x4_prob_intra[t][l];
683 intra4x4[x] = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
689 static void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
692 VP56RangeCoder *c = &s->c;
694 if (s->segmentation.update_map)
695 mb->segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
697 mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
700 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
702 if (mb->mode == MODE_I4x4) {
703 decode_intra4x4_modes(c, intra4x4, s->b4_stride, 1);
705 fill_rectangle(intra4x4, 4, 4, s->b4_stride, vp8_pred4x4_mode[mb->mode], 1);
707 s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
708 mb->ref_frame = VP56_FRAME_CURRENT;
709 } else if (vp56_rac_get_prob(c, s->prob->intra)) {
710 VP56mv near[2], best;
711 uint8_t cnt[4] = { 0 };
715 if (vp56_rac_get_prob(c, s->prob->last))
716 mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
717 VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
719 mb->ref_frame = VP56_FRAME_PREVIOUS;
721 // motion vectors, 16.3
722 find_near_mvs(s, mb, mb_x, mb_y, near, &best, cnt);
723 p[0] = vp8_mode_contexts[cnt[0]][0];
724 p[1] = vp8_mode_contexts[cnt[1]][1];
725 p[2] = vp8_mode_contexts[cnt[2]][2];
726 p[3] = vp8_mode_contexts[cnt[3]][3];
727 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_mvinter, p);
729 case VP8_MVMODE_SPLIT:
730 mb->mv = mb->bmv[decode_splitmvs(s, c, mb, &best) - 1];
732 case VP8_MVMODE_ZERO:
736 case VP8_MVMODE_NEAREST:
737 clamp_mv(s, &mb->mv, &near[0], mb_x, mb_y);
739 case VP8_MVMODE_NEAR:
740 clamp_mv(s, &mb->mv, &near[1], mb_x, mb_y);
743 mb->mv.y = best.y + read_mv_component(c, s->prob->mvc[0]);
744 mb->mv.x = best.x + read_mv_component(c, s->prob->mvc[1]);
747 if (mb->mode != VP8_MVMODE_SPLIT) {
748 mb->partitioning = VP8_SPLITMVMODE_NONE;
753 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
755 if (mb->mode == MODE_I4x4) {
756 decode_intra4x4_modes(c, intra4x4, s->b4_stride, 0);
758 fill_rectangle(intra4x4, 4, 4, s->b4_stride, vp8_pred4x4_mode[mb->mode], 1);
760 s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
761 mb->ref_frame = VP56_FRAME_CURRENT;
766 * @param c arithmetic bitstream reader context
767 * @param block destination for block coefficients
768 * @param probs probabilities to use when reading trees from the bitstream
769 * @param i initial coeff index, 0 unless a separate DC block is coded
770 * @param zero_nhood the initial prediction context for number of surrounding
771 * all-zero blocks (only left/top, so 0-2)
772 * @param qmul array holding the dc/ac dequant factor at position 0/1
773 * @return 0 if no coeffs were decoded
774 * otherwise, the index of the last coeff decoded plus one
776 static int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
777 uint8_t probs[8][3][NUM_DCT_TOKENS-1],
778 int i, int zero_nhood, int16_t qmul[2])
780 int token, nonzero = 0;
783 for (; i < 16; i++) {
784 token = vp8_rac_get_tree_with_offset(c, vp8_coeff_tree, probs[vp8_coeff_band[i]][zero_nhood], offset);
786 if (token == DCT_EOB)
788 else if (token >= DCT_CAT1) {
789 int cat = token-DCT_CAT1;
790 token = vp8_rac_get_coeff(c, vp8_dct_cat_prob[cat]);
791 token += vp8_dct_cat_offset[cat];
794 // after the first token, the non-zero prediction context becomes
795 // based on the last decoded coeff
800 } else if (token == 1)
805 // todo: full [16] qmat? load into register?
806 block[zigzag_scan[i]] = (vp8_rac_get(c) ? -token : token) * qmul[!!i];
813 static void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
814 uint8_t t_nnz[9], uint8_t l_nnz[9])
816 LOCAL_ALIGNED_16(DCTELEM, dc,[16]);
817 int i, x, y, luma_start = 0, luma_ctx = 3;
818 int nnz_pred, nnz, nnz_total = 0;
819 int segment = s->segmentation.enabled ? mb->segment : 0;
821 s->dsp.clear_blocks((DCTELEM *)s->block);
823 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
826 nnz_pred = t_nnz[8] + l_nnz[8];
828 // decode DC values and do hadamard
829 nnz = decode_block_coeffs(c, dc, s->prob->token[1], 0, nnz_pred,
830 s->qmat[segment].luma_dc_qmul);
831 l_nnz[8] = t_nnz[8] = !!nnz;
833 s->vp8dsp.vp8_luma_dc_wht(s->block, dc);
839 for (y = 0; y < 4; y++)
840 for (x = 0; x < 4; x++) {
841 nnz_pred = l_nnz[y] + t_nnz[x];
842 nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
843 nnz_pred, s->qmat[segment].luma_qmul);
844 // nnz+luma_start may be one more than the actual last index, but we don't care
845 s->non_zero_count_cache[y][x] = nnz + luma_start;
846 t_nnz[x] = l_nnz[y] = !!nnz;
851 // TODO: what to do about dimensions? 2nd dim for luma is x,
852 // but for chroma it's (y<<1)|x
853 for (i = 4; i < 6; i++)
854 for (y = 0; y < 2; y++)
855 for (x = 0; x < 2; x++) {
856 nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
857 nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
858 nnz_pred, s->qmat[segment].chroma_qmul);
859 s->non_zero_count_cache[i][(y<<1)+x] = nnz;
860 t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
864 // if there were no coded coeffs despite the macroblock not being marked skip,
865 // we MUST not do the inner loop filter and should not do IDCT
866 // Since skip isn't used for bitstream prediction, just manually set it.
871 static av_always_inline
872 void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
873 int linesize, int uvlinesize, int simple)
875 AV_COPY128(top_border, src_y + 15*linesize);
877 AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
878 AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
882 static av_always_inline
883 void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
884 int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
885 int simple, int xchg)
887 uint8_t *top_border_m1 = top_border-32; // for TL prediction
889 src_cb -= uvlinesize;
890 src_cr -= uvlinesize;
892 #define XCHG(a,b,xchg) do { \
893 if (xchg) AV_SWAP64(b,a); \
894 else AV_COPY64(b,a); \
897 XCHG(top_border_m1+8, src_y-8, xchg);
898 XCHG(top_border, src_y, xchg);
899 XCHG(top_border+8, src_y+8, 1);
900 if (mb_x < mb_width-1)
901 XCHG(top_border+32, src_y+16, 1);
903 // only copy chroma for normal loop filter
904 // or to initialize the top row to 127
905 if (!simple || !mb_y) {
906 XCHG(top_border_m1+16, src_cb-8, xchg);
907 XCHG(top_border_m1+24, src_cr-8, xchg);
908 XCHG(top_border+16, src_cb, 1);
909 XCHG(top_border+24, src_cr, 1);
913 static int check_intra_pred_mode(int mode, int mb_x, int mb_y)
915 if (mode == DC_PRED8x8) {
917 mode = DC_128_PRED8x8;
919 mode = LEFT_DC_PRED8x8;
921 mode = TOP_DC_PRED8x8;
926 static void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
927 uint8_t *bmode, int mb_x, int mb_y)
929 int x, y, mode, nnz, tr;
931 // for the first row, we need to run xchg_mb_border to init the top edge to 127
932 // otherwise, skip it if we aren't going to deblock
933 if (s->deblock_filter || !mb_y)
934 xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
935 s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
936 s->filter.simple, 1);
938 if (mb->mode < MODE_I4x4) {
939 mode = check_intra_pred_mode(mb->mode, mb_x, mb_y);
940 s->hpc.pred16x16[mode](dst[0], s->linesize);
942 uint8_t *ptr = dst[0];
944 // all blocks on the right edge of the macroblock use bottom edge
945 // the top macroblock for their topright edge
946 uint8_t *tr_right = ptr - s->linesize + 16;
948 // if we're on the right edge of the frame, said edge is extended
949 // from the top macroblock
950 if (mb_x == s->mb_width-1) {
951 tr = tr_right[-1]*0x01010101;
952 tr_right = (uint8_t *)&tr;
955 for (y = 0; y < 4; y++) {
956 uint8_t *topright = ptr + 4 - s->linesize;
957 for (x = 0; x < 4; x++) {
961 s->hpc.pred4x4[bmode[x]](ptr+4*x, topright, s->linesize);
963 nnz = s->non_zero_count_cache[y][x];
966 s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
968 s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
973 ptr += 4*s->linesize;
974 bmode += s->b4_stride;
978 mode = check_intra_pred_mode(s->chroma_pred_mode, mb_x, mb_y);
979 s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
980 s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
982 if (s->deblock_filter || !mb_y)
983 xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
984 s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
985 s->filter.simple, 0);
989 * Generic MC function.
991 * @param s VP8 decoding context
992 * @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
993 * @param dst target buffer for block data at block position
994 * @param src reference picture buffer at origin (0, 0)
995 * @param mv motion vector (relative to block position) to get pixel data from
996 * @param x_off horizontal position of block from origin (0, 0)
997 * @param y_off vertical position of block from origin (0, 0)
998 * @param block_w width of block (16, 8 or 4)
999 * @param block_h height of block (always same as block_w)
1000 * @param width width of src/dst plane data
1001 * @param height height of src/dst plane data
1002 * @param linesize size of a single line of plane data, including padding
1003 * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1005 static inline void vp8_mc(VP8Context *s, int luma,
1006 uint8_t *dst, uint8_t *src, const VP56mv *mv,
1007 int x_off, int y_off, int block_w, int block_h,
1008 int width, int height, int linesize,
1009 vp8_mc_func mc_func[3][3])
1012 static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };
1013 int mx = (mv->x << luma)&7, mx_idx = idx[mx];
1014 int my = (mv->y << luma)&7, my_idx = idx[my];
1016 x_off += mv->x >> (3 - luma);
1017 y_off += mv->y >> (3 - luma);
1020 src += y_off * linesize + x_off;
1021 if (x_off < 2 || x_off >= width - block_w - 3 ||
1022 y_off < 2 || y_off >= height - block_h - 3) {
1023 ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,
1024 block_w + 5, block_h + 5,
1025 x_off - 2, y_off - 2, width, height);
1026 src = s->edge_emu_buffer + 2 + linesize * 2;
1028 mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
1030 mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
1033 static inline void vp8_mc_part(VP8Context *s, uint8_t *dst[3],
1034 AVFrame *ref_frame, int x_off, int y_off,
1035 int bx_off, int by_off,
1036 int block_w, int block_h,
1037 int width, int height, VP56mv *mv)
1042 vp8_mc(s, 1, dst[0] + by_off * s->linesize + bx_off,
1043 ref_frame->data[0], mv, x_off + bx_off, y_off + by_off,
1044 block_w, block_h, width, height, s->linesize,
1045 s->put_pixels_tab[block_w == 8]);
1048 if (s->profile == 3) {
1052 x_off >>= 1; y_off >>= 1;
1053 bx_off >>= 1; by_off >>= 1;
1054 width >>= 1; height >>= 1;
1055 block_w >>= 1; block_h >>= 1;
1056 vp8_mc(s, 0, dst[1] + by_off * s->uvlinesize + bx_off,
1057 ref_frame->data[1], &uvmv, x_off + bx_off, y_off + by_off,
1058 block_w, block_h, width, height, s->uvlinesize,
1059 s->put_pixels_tab[1 + (block_w == 4)]);
1060 vp8_mc(s, 0, dst[2] + by_off * s->uvlinesize + bx_off,
1061 ref_frame->data[2], &uvmv, x_off + bx_off, y_off + by_off,
1062 block_w, block_h, width, height, s->uvlinesize,
1063 s->put_pixels_tab[1 + (block_w == 4)]);
1066 /* Fetch pixels for estimated mv 4 macroblocks ahead.
1067 * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
1068 static inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int x_off, int y_off, int ref)
1070 if (mb->ref_frame != VP56_FRAME_CURRENT) {
1071 int mx = mb->mv.x + x_off + 8;
1072 int my = mb->mv.y + y_off;
1073 uint8_t **src= s->framep[mb->ref_frame]->data;
1074 int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
1075 s->dsp.prefetch(src[0]+off, s->linesize, 4);
1076 off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
1077 s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
1082 * Apply motion vectors to prediction buffer, chapter 18.
1084 static void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
1087 int x_off = mb_x << 4, y_off = mb_y << 4;
1088 int width = 16*s->mb_width, height = 16*s->mb_height;
1090 prefetch_motion(s, mb, mb_x, mb_y, x_off, y_off, VP56_FRAME_PREVIOUS);
1092 if (mb->mode < VP8_MVMODE_SPLIT) {
1093 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1094 0, 0, 16, 16, width, height, &mb->mv);
1095 } else switch (mb->partitioning) {
1096 case VP8_SPLITMVMODE_4x4: {
1101 for (y = 0; y < 4; y++) {
1102 for (x = 0; x < 4; x++) {
1103 vp8_mc(s, 1, dst[0] + 4*y*s->linesize + x*4,
1104 s->framep[mb->ref_frame]->data[0], &mb->bmv[4*y + x],
1105 4*x + x_off, 4*y + y_off, 4, 4,
1106 width, height, s->linesize,
1107 s->put_pixels_tab[2]);
1112 x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
1113 for (y = 0; y < 2; y++) {
1114 for (x = 0; x < 2; x++) {
1115 uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
1116 mb->bmv[ 2*y * 4 + 2*x+1].x +
1117 mb->bmv[(2*y+1) * 4 + 2*x ].x +
1118 mb->bmv[(2*y+1) * 4 + 2*x+1].x;
1119 uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
1120 mb->bmv[ 2*y * 4 + 2*x+1].y +
1121 mb->bmv[(2*y+1) * 4 + 2*x ].y +
1122 mb->bmv[(2*y+1) * 4 + 2*x+1].y;
1123 uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
1124 uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
1125 if (s->profile == 3) {
1129 vp8_mc(s, 0, dst[1] + 4*y*s->uvlinesize + x*4,
1130 s->framep[mb->ref_frame]->data[1], &uvmv,
1131 4*x + x_off, 4*y + y_off, 4, 4,
1132 width, height, s->uvlinesize,
1133 s->put_pixels_tab[2]);
1134 vp8_mc(s, 0, dst[2] + 4*y*s->uvlinesize + x*4,
1135 s->framep[mb->ref_frame]->data[2], &uvmv,
1136 4*x + x_off, 4*y + y_off, 4, 4,
1137 width, height, s->uvlinesize,
1138 s->put_pixels_tab[2]);
1143 case VP8_SPLITMVMODE_16x8:
1144 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1145 0, 0, 16, 8, width, height, &mb->bmv[0]);
1146 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1147 0, 8, 16, 8, width, height, &mb->bmv[1]);
1149 case VP8_SPLITMVMODE_8x16:
1150 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1151 0, 0, 8, 16, width, height, &mb->bmv[0]);
1152 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1153 8, 0, 8, 16, width, height, &mb->bmv[1]);
1155 case VP8_SPLITMVMODE_8x8:
1156 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1157 0, 0, 8, 8, width, height, &mb->bmv[0]);
1158 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1159 8, 0, 8, 8, width, height, &mb->bmv[1]);
1160 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1161 0, 8, 8, 8, width, height, &mb->bmv[2]);
1162 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1163 8, 8, 8, 8, width, height, &mb->bmv[3]);
1167 prefetch_motion(s, mb, mb_x, mb_y, x_off, y_off, VP56_FRAME_GOLDEN);
1170 static void idct_mb(VP8Context *s, uint8_t *y_dst, uint8_t *u_dst, uint8_t *v_dst,
1175 if (mb->mode != MODE_I4x4)
1176 for (y = 0; y < 4; y++) {
1177 for (x = 0; x < 4; x++) {
1178 nnz = s->non_zero_count_cache[y][x];
1181 s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
1183 s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
1186 y_dst += 4*s->linesize;
1189 for (y = 0; y < 2; y++) {
1190 for (x = 0; x < 2; x++) {
1191 nnz = s->non_zero_count_cache[4][(y<<1)+x];
1194 s->vp8dsp.vp8_idct_dc_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1196 s->vp8dsp.vp8_idct_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1199 nnz = s->non_zero_count_cache[5][(y<<1)+x];
1202 s->vp8dsp.vp8_idct_dc_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1204 s->vp8dsp.vp8_idct_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1207 u_dst += 4*s->uvlinesize;
1208 v_dst += 4*s->uvlinesize;
1212 static void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, int *level, int *inner, int *hev_thresh)
1214 int interior_limit, filter_level;
1216 if (s->segmentation.enabled) {
1217 filter_level = s->segmentation.filter_level[mb->segment];
1218 if (!s->segmentation.absolute_vals)
1219 filter_level += s->filter.level;
1221 filter_level = s->filter.level;
1223 if (s->lf_delta.enabled) {
1224 filter_level += s->lf_delta.ref[mb->ref_frame];
1226 if (mb->ref_frame == VP56_FRAME_CURRENT) {
1227 if (mb->mode == MODE_I4x4)
1228 filter_level += s->lf_delta.mode[0];
1230 if (mb->mode == VP8_MVMODE_ZERO)
1231 filter_level += s->lf_delta.mode[1];
1232 else if (mb->mode == VP8_MVMODE_SPLIT)
1233 filter_level += s->lf_delta.mode[3];
1235 filter_level += s->lf_delta.mode[2];
1238 filter_level = av_clip(filter_level, 0, 63);
1240 interior_limit = filter_level;
1241 if (s->filter.sharpness) {
1242 interior_limit >>= s->filter.sharpness > 4 ? 2 : 1;
1243 interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
1245 interior_limit = FFMAX(interior_limit, 1);
1247 *level = filter_level;
1248 *inner = interior_limit;
1251 *hev_thresh = filter_level >= 15;
1254 if (filter_level >= 40)
1257 if (filter_level >= 40)
1259 else if (filter_level >= 20)
1265 static void filter_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb, int mb_x, int mb_y)
1267 int filter_level, inner_limit, hev_thresh, mbedge_lim, bedge_lim;
1269 filter_level_for_mb(s, mb, &filter_level, &inner_limit, &hev_thresh);
1273 mbedge_lim = 2*(filter_level+2) + inner_limit;
1274 bedge_lim = 2* filter_level + inner_limit;
1277 s->vp8dsp.vp8_h_loop_filter16y(dst[0], s->linesize,
1278 mbedge_lim, inner_limit, hev_thresh);
1279 s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], s->uvlinesize,
1280 mbedge_lim, inner_limit, hev_thresh);
1283 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1284 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, s->linesize, bedge_lim,
1285 inner_limit, hev_thresh);
1286 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, s->linesize, bedge_lim,
1287 inner_limit, hev_thresh);
1288 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, s->linesize, bedge_lim,
1289 inner_limit, hev_thresh);
1290 s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
1291 s->uvlinesize, bedge_lim,
1292 inner_limit, hev_thresh);
1296 s->vp8dsp.vp8_v_loop_filter16y(dst[0], s->linesize,
1297 mbedge_lim, inner_limit, hev_thresh);
1298 s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], s->uvlinesize,
1299 mbedge_lim, inner_limit, hev_thresh);
1302 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1303 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*s->linesize,
1304 s->linesize, bedge_lim,
1305 inner_limit, hev_thresh);
1306 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*s->linesize,
1307 s->linesize, bedge_lim,
1308 inner_limit, hev_thresh);
1309 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*s->linesize,
1310 s->linesize, bedge_lim,
1311 inner_limit, hev_thresh);
1312 s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * s->uvlinesize,
1313 dst[2] + 4 * s->uvlinesize,
1314 s->uvlinesize, bedge_lim,
1315 inner_limit, hev_thresh);
1319 static void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8Macroblock *mb, int mb_x, int mb_y)
1321 int filter_level, inner_limit, mbedge_lim, bedge_lim;
1323 filter_level_for_mb(s, mb, &filter_level, &inner_limit, NULL);
1327 mbedge_lim = 2*(filter_level+2) + inner_limit;
1328 bedge_lim = 2* filter_level + inner_limit;
1331 s->vp8dsp.vp8_h_loop_filter_simple(dst, s->linesize, mbedge_lim);
1332 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1333 s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, s->linesize, bedge_lim);
1334 s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, s->linesize, bedge_lim);
1335 s->vp8dsp.vp8_h_loop_filter_simple(dst+12, s->linesize, bedge_lim);
1339 s->vp8dsp.vp8_v_loop_filter_simple(dst, s->linesize, mbedge_lim);
1340 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1341 s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*s->linesize, s->linesize, bedge_lim);
1342 s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*s->linesize, s->linesize, bedge_lim);
1343 s->vp8dsp.vp8_v_loop_filter_simple(dst+12*s->linesize, s->linesize, bedge_lim);
1347 static void filter_mb_row(VP8Context *s, int mb_y)
1349 VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1351 s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize,
1352 s->framep[VP56_FRAME_CURRENT]->data[1] + 8*mb_y*s->uvlinesize,
1353 s->framep[VP56_FRAME_CURRENT]->data[2] + 8*mb_y*s->uvlinesize
1357 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1358 backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
1359 filter_mb(s, dst, mb++, mb_x, mb_y);
1366 static void filter_mb_row_simple(VP8Context *s, int mb_y)
1368 uint8_t *dst = s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize;
1369 VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1372 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1373 backup_mb_border(s->top_border[mb_x+1], dst, NULL, NULL, s->linesize, 0, 1);
1374 filter_mb_simple(s, dst, mb++, mb_x, mb_y);
1379 static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1382 VP8Context *s = avctx->priv_data;
1383 int ret, mb_x, mb_y, i, y, referenced;
1384 enum AVDiscard skip_thresh;
1387 if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
1390 referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
1391 || s->update_altref == VP56_FRAME_CURRENT;
1393 skip_thresh = !referenced ? AVDISCARD_NONREF :
1394 !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
1396 if (avctx->skip_frame >= skip_thresh) {
1400 s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
1402 for (i = 0; i < 4; i++)
1403 if (&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1404 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1405 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
1406 curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
1409 if (curframe->data[0])
1410 avctx->release_buffer(avctx, curframe);
1412 curframe->key_frame = s->keyframe;
1413 curframe->pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
1414 curframe->reference = referenced ? 3 : 0;
1415 if ((ret = avctx->get_buffer(avctx, curframe))) {
1416 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
1420 // Given that arithmetic probabilities are updated every frame, it's quite likely
1421 // that the values we have on a random interframe are complete junk if we didn't
1422 // start decode on a keyframe. So just don't display anything rather than junk.
1423 if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
1424 !s->framep[VP56_FRAME_GOLDEN] ||
1425 !s->framep[VP56_FRAME_GOLDEN2])) {
1426 av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
1427 return AVERROR_INVALIDDATA;
1430 s->linesize = curframe->linesize[0];
1431 s->uvlinesize = curframe->linesize[1];
1433 if (!s->edge_emu_buffer)
1434 s->edge_emu_buffer = av_malloc(21*s->linesize);
1436 memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
1438 // top edge of 127 for intra prediction
1439 memset(s->top_border, 127, (s->mb_width+1)*sizeof(*s->top_border));
1441 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1442 VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
1443 VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1444 uint8_t *intra4x4 = s->intra4x4_pred_mode + 4*mb_y*s->b4_stride;
1446 curframe->data[0] + 16*mb_y*s->linesize,
1447 curframe->data[1] + 8*mb_y*s->uvlinesize,
1448 curframe->data[2] + 8*mb_y*s->uvlinesize
1451 memset(s->left_nnz, 0, sizeof(s->left_nnz));
1453 // left edge of 129 for intra prediction
1454 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE))
1455 for (i = 0; i < 3; i++)
1456 for (y = 0; y < 16>>!!i; y++)
1457 dst[i][y*curframe->linesize[i]-1] = 129;
1459 memset(s->top_border, 129, sizeof(*s->top_border));
1461 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1462 /* Prefetch the current frame, 4 MBs ahead */
1463 s->dsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
1464 s->dsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
1466 decode_mb_mode(s, mb, mb_x, mb_y, intra4x4 + 4*mb_x);
1469 decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
1471 AV_ZERO128(s->non_zero_count_cache); // luma
1472 AV_ZERO64(s->non_zero_count_cache[4]); // chroma
1475 if (mb->mode <= MODE_I4x4) {
1476 intra_predict(s, dst, mb, intra4x4 + 4*mb_x, mb_x, mb_y);
1477 memset(mb->bmv, 0, sizeof(mb->bmv));
1479 inter_predict(s, dst, mb, mb_x, mb_y);
1483 idct_mb(s, dst[0], dst[1], dst[2], mb);
1485 AV_ZERO64(s->left_nnz);
1486 AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
1488 // Reset DC block predictors if they would exist if the mb had coefficients
1489 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
1491 s->top_nnz[mb_x][8] = 0;
1500 if (s->deblock_filter) {
1501 if (s->filter.simple)
1502 filter_mb_row_simple(s, mb_y);
1504 filter_mb_row(s, mb_y);
1509 // if future frames don't use the updated probabilities,
1510 // reset them to the values we saved
1511 if (!s->update_probabilities)
1512 s->prob[0] = s->prob[1];
1514 // check if golden and altref are swapped
1515 if (s->update_altref == VP56_FRAME_GOLDEN &&
1516 s->update_golden == VP56_FRAME_GOLDEN2)
1517 FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN], s->framep[VP56_FRAME_GOLDEN2]);
1519 if (s->update_altref != VP56_FRAME_NONE)
1520 s->framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
1522 if (s->update_golden != VP56_FRAME_NONE)
1523 s->framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
1526 if (s->update_last) // move cur->prev
1527 s->framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_CURRENT];
1529 // release no longer referenced frames
1530 for (i = 0; i < 4; i++)
1531 if (s->frames[i].data[0] &&
1532 &s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
1533 &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1534 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1535 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
1536 avctx->release_buffer(avctx, &s->frames[i]);
1538 if (!s->invisible) {
1539 *(AVFrame*)data = *s->framep[VP56_FRAME_CURRENT];
1540 *data_size = sizeof(AVFrame);
1546 static av_cold int vp8_decode_init(AVCodecContext *avctx)
1548 VP8Context *s = avctx->priv_data;
1551 avctx->pix_fmt = PIX_FMT_YUV420P;
1553 dsputil_init(&s->dsp, avctx);
1554 ff_h264_pred_init(&s->hpc, CODEC_ID_VP8);
1555 ff_vp8dsp_init(&s->vp8dsp);
1557 // intra pred needs edge emulation among other things
1558 if (avctx->flags&CODEC_FLAG_EMU_EDGE) {
1559 av_log(avctx, AV_LOG_ERROR, "Edge emulation not supported\n");
1560 return AVERROR_PATCHWELCOME;
1566 static av_cold int vp8_decode_free(AVCodecContext *avctx)
1568 vp8_decode_flush(avctx);
1572 AVCodec vp8_decoder = {
1582 .flush = vp8_decode_flush,
1583 .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),