2 * VP9 compatible video decoder
4 * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5 * Copyright (C) 2013 Clément Bœsch <u pkh me>
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
33 #include "libavutil/avassert.h"
34 #include "libavutil/pixdesc.h"
36 #define VP9_SYNCCODE 0x498342
73 typedef struct VP9Frame {
75 AVBufferRef *extradata;
76 uint8_t *segmentation_map;
77 struct VP9mvrefPair *mv;
83 uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
84 [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
87 typedef struct VP9Block {
88 uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
89 enum FilterMode filter;
90 VP56mv mv[4 /* b_idx */][2 /* ref */];
92 enum TxfmMode tx, uvtx;
94 enum BlockPartition bp;
97 typedef struct VP9Context {
104 VP9Block *b_base, *b;
106 int row, row7, col, col7;
108 ptrdiff_t y_stride, uv_stride;
111 uint8_t keyframe, last_keyframe;
112 uint8_t last_bpp, bpp, bpp_index, bytesperpixel;
114 uint8_t use_last_frame_mvs;
119 uint8_t refreshrefmask;
120 uint8_t highprecisionmvs;
121 enum FilterMode filtermode;
122 uint8_t allowcompinter;
125 uint8_t parallelmode;
129 uint8_t varcompref[2];
130 ThreadFrame refs[8], next_refs[8];
132 #define REF_FRAME_MVPAIR 1
133 #define REF_FRAME_SEGMAP 2
140 uint8_t mblim_lut[64];
148 int8_t ydc_qdelta, uvdc_qdelta, uvac_qdelta;
150 #define MAX_SEGMENT 8
154 uint8_t absolute_vals;
160 uint8_t skip_enabled;
169 unsigned log2_tile_cols, log2_tile_rows;
170 unsigned tile_cols, tile_rows;
171 unsigned tile_row_start, tile_row_end, tile_col_start, tile_col_end;
173 unsigned sb_cols, sb_rows, rows, cols;
176 uint8_t coef[4][2][2][6][6][3];
180 uint8_t coef[4][2][2][6][6][11];
185 unsigned y_mode[4][10];
186 unsigned uv_mode[10][10];
187 unsigned filter[4][3];
188 unsigned mv_mode[7][4];
189 unsigned intra[4][2];
191 unsigned single_ref[5][2][2];
192 unsigned comp_ref[5][2];
193 unsigned tx32p[2][4];
194 unsigned tx16p[2][3];
197 unsigned mv_joint[4];
200 unsigned classes[11];
202 unsigned bits[10][2];
203 unsigned class0_fp[2][4];
205 unsigned class0_hp[2];
208 unsigned partition[4][4][4];
209 unsigned coef[4][2][2][6][6][3];
210 unsigned eob[4][2][2][6][6][2];
212 enum TxfmMode txfmmode;
213 enum CompPredMode comppredmode;
215 // contextual (left/above) cache
216 DECLARE_ALIGNED(16, uint8_t, left_y_nnz_ctx)[16];
217 DECLARE_ALIGNED(16, uint8_t, left_mode_ctx)[16];
218 DECLARE_ALIGNED(16, VP56mv, left_mv_ctx)[16][2];
219 DECLARE_ALIGNED(16, uint8_t, left_uv_nnz_ctx)[2][16];
220 DECLARE_ALIGNED(8, uint8_t, left_partition_ctx)[8];
221 DECLARE_ALIGNED(8, uint8_t, left_skip_ctx)[8];
222 DECLARE_ALIGNED(8, uint8_t, left_txfm_ctx)[8];
223 DECLARE_ALIGNED(8, uint8_t, left_segpred_ctx)[8];
224 DECLARE_ALIGNED(8, uint8_t, left_intra_ctx)[8];
225 DECLARE_ALIGNED(8, uint8_t, left_comp_ctx)[8];
226 DECLARE_ALIGNED(8, uint8_t, left_ref_ctx)[8];
227 DECLARE_ALIGNED(8, uint8_t, left_filter_ctx)[8];
228 uint8_t *above_partition_ctx;
229 uint8_t *above_mode_ctx;
230 // FIXME maybe merge some of the below in a flags field?
231 uint8_t *above_y_nnz_ctx;
232 uint8_t *above_uv_nnz_ctx[2];
233 uint8_t *above_skip_ctx; // 1bit
234 uint8_t *above_txfm_ctx; // 2bit
235 uint8_t *above_segpred_ctx; // 1bit
236 uint8_t *above_intra_ctx; // 1bit
237 uint8_t *above_comp_ctx; // 1bit
238 uint8_t *above_ref_ctx; // 2bit
239 uint8_t *above_filter_ctx;
240 VP56mv (*above_mv_ctx)[2];
243 uint8_t *intra_pred_data[3];
244 struct VP9Filter *lflvl;
245 DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[135 * 144 * 2];
247 // block reconstruction intermediates
248 int block_alloc_using_2pass;
249 int16_t *block_base, *block, *uvblock_base[2], *uvblock[2];
250 uint8_t *eob_base, *uveob_base[2], *eob, *uveob[2];
251 struct { int x, y; } min_mv, max_mv;
252 DECLARE_ALIGNED(32, uint8_t, tmp_y)[64 * 64 * 2];
253 DECLARE_ALIGNED(32, uint8_t, tmp_uv)[2][64 * 64 * 2];
254 uint16_t mvscale[3][2];
255 uint8_t mvstep[3][2];
258 static const uint8_t bwh_tab[2][N_BS_SIZES][2] = {
260 { 16, 16 }, { 16, 8 }, { 8, 16 }, { 8, 8 }, { 8, 4 }, { 4, 8 },
261 { 4, 4 }, { 4, 2 }, { 2, 4 }, { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 },
263 { 8, 8 }, { 8, 4 }, { 4, 8 }, { 4, 4 }, { 4, 2 }, { 2, 4 },
264 { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 },
268 static int vp9_alloc_frame(AVCodecContext *ctx, VP9Frame *f)
270 VP9Context *s = ctx->priv_data;
273 if ((ret = ff_thread_get_buffer(ctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
275 sz = 64 * s->sb_cols * s->sb_rows;
276 if (!(f->extradata = av_buffer_allocz(sz * (1 + sizeof(struct VP9mvrefPair))))) {
277 ff_thread_release_buffer(ctx, &f->tf);
278 return AVERROR(ENOMEM);
281 f->segmentation_map = f->extradata->data;
282 f->mv = (struct VP9mvrefPair *) (f->extradata->data + sz);
287 static void vp9_unref_frame(AVCodecContext *ctx, VP9Frame *f)
289 ff_thread_release_buffer(ctx, &f->tf);
290 av_buffer_unref(&f->extradata);
293 static int vp9_ref_frame(AVCodecContext *ctx, VP9Frame *dst, VP9Frame *src)
297 if ((res = ff_thread_ref_frame(&dst->tf, &src->tf)) < 0) {
299 } else if (!(dst->extradata = av_buffer_ref(src->extradata))) {
300 vp9_unref_frame(ctx, dst);
301 return AVERROR(ENOMEM);
304 dst->segmentation_map = src->segmentation_map;
306 dst->uses_2pass = src->uses_2pass;
311 static int update_size(AVCodecContext *ctx, int w, int h, enum AVPixelFormat fmt)
313 VP9Context *s = ctx->priv_data;
315 int bytesperpixel = s->bytesperpixel;
317 av_assert0(w > 0 && h > 0);
319 if (s->intra_pred_data[0] && w == ctx->width && h == ctx->height && ctx->pix_fmt == fmt)
325 s->sb_cols = (w + 63) >> 6;
326 s->sb_rows = (h + 63) >> 6;
327 s->cols = (w + 7) >> 3;
328 s->rows = (h + 7) >> 3;
330 #define assign(var, type, n) var = (type) p; p += s->sb_cols * (n) * sizeof(*var)
331 av_freep(&s->intra_pred_data[0]);
332 // FIXME we slightly over-allocate here for subsampled chroma, but a little
333 // bit of padding shouldn't affect performance...
334 p = av_malloc(s->sb_cols * (128 + 192 * bytesperpixel +
335 sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx)));
337 return AVERROR(ENOMEM);
338 assign(s->intra_pred_data[0], uint8_t *, 64 * bytesperpixel);
339 assign(s->intra_pred_data[1], uint8_t *, 64 * bytesperpixel);
340 assign(s->intra_pred_data[2], uint8_t *, 64 * bytesperpixel);
341 assign(s->above_y_nnz_ctx, uint8_t *, 16);
342 assign(s->above_mode_ctx, uint8_t *, 16);
343 assign(s->above_mv_ctx, VP56mv(*)[2], 16);
344 assign(s->above_uv_nnz_ctx[0], uint8_t *, 16);
345 assign(s->above_uv_nnz_ctx[1], uint8_t *, 16);
346 assign(s->above_partition_ctx, uint8_t *, 8);
347 assign(s->above_skip_ctx, uint8_t *, 8);
348 assign(s->above_txfm_ctx, uint8_t *, 8);
349 assign(s->above_segpred_ctx, uint8_t *, 8);
350 assign(s->above_intra_ctx, uint8_t *, 8);
351 assign(s->above_comp_ctx, uint8_t *, 8);
352 assign(s->above_ref_ctx, uint8_t *, 8);
353 assign(s->above_filter_ctx, uint8_t *, 8);
354 assign(s->lflvl, struct VP9Filter *, 1);
357 // these will be re-allocated a little later
358 av_freep(&s->b_base);
359 av_freep(&s->block_base);
361 if (s->bpp != s->last_bpp) {
362 ff_vp9dsp_init(&s->dsp, s->bpp);
363 ff_videodsp_init(&s->vdsp, s->bpp);
364 s->last_bpp = s->bpp;
370 static int update_block_buffers(AVCodecContext *ctx)
372 VP9Context *s = ctx->priv_data;
373 int chroma_blocks, chroma_eobs, bytesperpixel = s->bytesperpixel;
375 if (s->b_base && s->block_base && s->block_alloc_using_2pass == s->frames[CUR_FRAME].uses_2pass)
379 av_free(s->block_base);
380 chroma_blocks = 64 * 64 >> (s->ss_h + s->ss_v);
381 chroma_eobs = 16 * 16 >> (s->ss_h + s->ss_v);
382 if (s->frames[CUR_FRAME].uses_2pass) {
383 int sbs = s->sb_cols * s->sb_rows;
385 s->b_base = av_malloc_array(s->cols * s->rows, sizeof(VP9Block));
386 s->block_base = av_mallocz(((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
387 16 * 16 + 2 * chroma_eobs) * sbs);
388 if (!s->b_base || !s->block_base)
389 return AVERROR(ENOMEM);
390 s->uvblock_base[0] = s->block_base + sbs * 64 * 64 * bytesperpixel;
391 s->uvblock_base[1] = s->uvblock_base[0] + sbs * chroma_blocks * bytesperpixel;
392 s->eob_base = (uint8_t *) (s->uvblock_base[1] + sbs * chroma_blocks * bytesperpixel);
393 s->uveob_base[0] = s->eob_base + 16 * 16 * sbs;
394 s->uveob_base[1] = s->uveob_base[0] + chroma_eobs * sbs;
396 s->b_base = av_malloc(sizeof(VP9Block));
397 s->block_base = av_mallocz((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
398 16 * 16 + 2 * chroma_eobs);
399 if (!s->b_base || !s->block_base)
400 return AVERROR(ENOMEM);
401 s->uvblock_base[0] = s->block_base + 64 * 64 * bytesperpixel;
402 s->uvblock_base[1] = s->uvblock_base[0] + chroma_blocks * bytesperpixel;
403 s->eob_base = (uint8_t *) (s->uvblock_base[1] + chroma_blocks * bytesperpixel);
404 s->uveob_base[0] = s->eob_base + 16 * 16;
405 s->uveob_base[1] = s->uveob_base[0] + chroma_eobs;
407 s->block_alloc_using_2pass = s->frames[CUR_FRAME].uses_2pass;
412 // for some reason the sign bit is at the end, not the start, of a bit sequence
413 static av_always_inline int get_sbits_inv(GetBitContext *gb, int n)
415 int v = get_bits(gb, n);
416 return get_bits1(gb) ? -v : v;
419 static av_always_inline int inv_recenter_nonneg(int v, int m)
421 return v > 2 * m ? v : v & 1 ? m - ((v + 1) >> 1) : m + (v >> 1);
424 // differential forward probability updates
425 static int update_prob(VP56RangeCoder *c, int p)
427 static const int inv_map_table[254] = {
428 7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176,
429 189, 202, 215, 228, 241, 254, 1, 2, 3, 4, 5, 6, 8, 9,
430 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24,
431 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39,
432 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54,
433 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
434 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
435 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100,
436 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
437 116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
438 131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
439 146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
440 161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
441 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
442 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
443 207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
444 222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
445 237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
450 /* This code is trying to do a differential probability update. For a
451 * current probability A in the range [1, 255], the difference to a new
452 * probability of any value can be expressed differentially as 1-A,255-A
453 * where some part of this (absolute range) exists both in positive as
454 * well as the negative part, whereas another part only exists in one
455 * half. We're trying to code this shared part differentially, i.e.
456 * times two where the value of the lowest bit specifies the sign, and
457 * the single part is then coded on top of this. This absolute difference
458 * then again has a value of [0,254], but a bigger value in this range
459 * indicates that we're further away from the original value A, so we
460 * can code this as a VLC code, since higher values are increasingly
461 * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
462 * updates vs. the 'fine, exact' updates further down the range, which
463 * adds one extra dimension to this differential update model. */
465 if (!vp8_rac_get(c)) {
466 d = vp8_rac_get_uint(c, 4) + 0;
467 } else if (!vp8_rac_get(c)) {
468 d = vp8_rac_get_uint(c, 4) + 16;
469 } else if (!vp8_rac_get(c)) {
470 d = vp8_rac_get_uint(c, 5) + 32;
472 d = vp8_rac_get_uint(c, 7);
474 d = (d << 1) - 65 + vp8_rac_get(c);
478 return p <= 128 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1) :
479 255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
482 static enum AVPixelFormat read_colorspace_details(AVCodecContext *ctx)
484 static const enum AVColorSpace colorspaces[8] = {
485 AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
486 AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
488 VP9Context *s = ctx->priv_data;
489 enum AVPixelFormat res;
490 int bits = ctx->profile <= 1 ? 0 : 1 + get_bits1(&s->gb); // 0:8, 1:10, 2:12
493 s->bpp = 8 + bits * 2;
494 s->bytesperpixel = (7 + s->bpp) >> 3;
495 ctx->colorspace = colorspaces[get_bits(&s->gb, 3)];
496 if (ctx->colorspace == AVCOL_SPC_RGB) { // RGB = profile 1
497 static const enum AVPixelFormat pix_fmt_rgb[3] = {
498 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12
500 if (ctx->profile & 1) {
501 s->ss_h = s->ss_v = 1;
502 res = pix_fmt_rgb[bits];
503 ctx->color_range = AVCOL_RANGE_JPEG;
505 av_log(ctx, AV_LOG_ERROR, "RGB not supported in profile %d\n",
507 return AVERROR_INVALIDDATA;
510 static const enum AVPixelFormat pix_fmt_for_ss[3][2 /* v */][2 /* h */] = {
511 { { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P },
512 { AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV420P } },
513 { { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10 },
514 { AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV420P10 } },
515 { { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12 },
516 { AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV420P12 } }
518 ctx->color_range = get_bits1(&s->gb) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
519 if (ctx->profile & 1) {
520 s->ss_h = get_bits1(&s->gb);
521 s->ss_v = get_bits1(&s->gb);
522 if ((res = pix_fmt_for_ss[bits][s->ss_v][s->ss_h]) == AV_PIX_FMT_YUV420P) {
523 av_log(ctx, AV_LOG_ERROR, "YUV 4:2:0 not supported in profile %d\n",
525 return AVERROR_INVALIDDATA;
526 } else if (get_bits1(&s->gb)) {
527 av_log(ctx, AV_LOG_ERROR, "Profile %d color details reserved bit set\n",
529 return AVERROR_INVALIDDATA;
532 s->ss_h = s->ss_v = 1;
533 res = pix_fmt_for_ss[bits][1][1];
540 static int decode_frame_header(AVCodecContext *ctx,
541 const uint8_t *data, int size, int *ref)
543 VP9Context *s = ctx->priv_data;
544 int c, i, j, k, l, m, n, w, h, max, size2, res, sharp;
545 enum AVPixelFormat fmt = ctx->pix_fmt;
547 const uint8_t *data2;
550 if ((res = init_get_bits8(&s->gb, data, size)) < 0) {
551 av_log(ctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
554 if (get_bits(&s->gb, 2) != 0x2) { // frame marker
555 av_log(ctx, AV_LOG_ERROR, "Invalid frame marker\n");
556 return AVERROR_INVALIDDATA;
558 ctx->profile = get_bits1(&s->gb);
559 ctx->profile |= get_bits1(&s->gb) << 1;
560 if (ctx->profile == 3) ctx->profile += get_bits1(&s->gb);
561 if (ctx->profile > 3) {
562 av_log(ctx, AV_LOG_ERROR, "Profile %d is not yet supported\n", ctx->profile);
563 return AVERROR_INVALIDDATA;
565 if (get_bits1(&s->gb)) {
566 *ref = get_bits(&s->gb, 3);
569 s->last_keyframe = s->keyframe;
570 s->keyframe = !get_bits1(&s->gb);
571 last_invisible = s->invisible;
572 s->invisible = !get_bits1(&s->gb);
573 s->errorres = get_bits1(&s->gb);
574 s->use_last_frame_mvs = !s->errorres && !last_invisible;
576 if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
577 av_log(ctx, AV_LOG_ERROR, "Invalid sync code\n");
578 return AVERROR_INVALIDDATA;
580 if ((fmt = read_colorspace_details(ctx)) < 0)
582 // for profile 1, here follows the subsampling bits
583 s->refreshrefmask = 0xff;
584 w = get_bits(&s->gb, 16) + 1;
585 h = get_bits(&s->gb, 16) + 1;
586 if (get_bits1(&s->gb)) // display size
587 skip_bits(&s->gb, 32);
589 s->intraonly = s->invisible ? get_bits1(&s->gb) : 0;
590 s->resetctx = s->errorres ? 0 : get_bits(&s->gb, 2);
592 if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
593 av_log(ctx, AV_LOG_ERROR, "Invalid sync code\n");
594 return AVERROR_INVALIDDATA;
596 if (ctx->profile == 1) {
597 if ((fmt = read_colorspace_details(ctx)) < 0)
600 s->ss_h = s->ss_v = 1;
603 s->bytesperpixel = 1;
604 fmt = AV_PIX_FMT_YUV420P;
605 ctx->colorspace = AVCOL_SPC_BT470BG;
606 ctx->color_range = AVCOL_RANGE_JPEG;
608 s->refreshrefmask = get_bits(&s->gb, 8);
609 w = get_bits(&s->gb, 16) + 1;
610 h = get_bits(&s->gb, 16) + 1;
611 if (get_bits1(&s->gb)) // display size
612 skip_bits(&s->gb, 32);
614 s->refreshrefmask = get_bits(&s->gb, 8);
615 s->refidx[0] = get_bits(&s->gb, 3);
616 s->signbias[0] = get_bits1(&s->gb);
617 s->refidx[1] = get_bits(&s->gb, 3);
618 s->signbias[1] = get_bits1(&s->gb);
619 s->refidx[2] = get_bits(&s->gb, 3);
620 s->signbias[2] = get_bits1(&s->gb);
621 if (!s->refs[s->refidx[0]].f->data[0] ||
622 !s->refs[s->refidx[1]].f->data[0] ||
623 !s->refs[s->refidx[2]].f->data[0]) {
624 av_log(ctx, AV_LOG_ERROR, "Not all references are available\n");
625 return AVERROR_INVALIDDATA;
627 if (get_bits1(&s->gb)) {
628 w = s->refs[s->refidx[0]].f->width;
629 h = s->refs[s->refidx[0]].f->height;
630 } else if (get_bits1(&s->gb)) {
631 w = s->refs[s->refidx[1]].f->width;
632 h = s->refs[s->refidx[1]].f->height;
633 } else if (get_bits1(&s->gb)) {
634 w = s->refs[s->refidx[2]].f->width;
635 h = s->refs[s->refidx[2]].f->height;
637 w = get_bits(&s->gb, 16) + 1;
638 h = get_bits(&s->gb, 16) + 1;
640 // Note that in this code, "CUR_FRAME" is actually before we
641 // have formally allocated a frame, and thus actually represents
643 s->use_last_frame_mvs &= s->frames[CUR_FRAME].tf.f->width == w &&
644 s->frames[CUR_FRAME].tf.f->height == h;
645 if (get_bits1(&s->gb)) // display size
646 skip_bits(&s->gb, 32);
647 s->highprecisionmvs = get_bits1(&s->gb);
648 s->filtermode = get_bits1(&s->gb) ? FILTER_SWITCHABLE :
650 s->allowcompinter = s->signbias[0] != s->signbias[1] ||
651 s->signbias[0] != s->signbias[2];
652 if (s->allowcompinter) {
653 if (s->signbias[0] == s->signbias[1]) {
655 s->varcompref[0] = 0;
656 s->varcompref[1] = 1;
657 } else if (s->signbias[0] == s->signbias[2]) {
659 s->varcompref[0] = 0;
660 s->varcompref[1] = 2;
663 s->varcompref[0] = 1;
664 s->varcompref[1] = 2;
668 for (i = 0; i < 3; i++) {
669 AVFrame *ref = s->refs[s->refidx[i]].f;
670 int refw = ref->width, refh = ref->height;
672 if (ref->format != fmt) {
673 av_log(ctx, AV_LOG_ERROR,
674 "Ref pixfmt (%s) did not match current frame (%s)",
675 av_get_pix_fmt_name(ref->format),
676 av_get_pix_fmt_name(fmt));
677 return AVERROR_INVALIDDATA;
678 } else if (refw == w && refh == h) {
679 s->mvscale[i][0] = s->mvscale[i][1] = 0;
681 if (w * 2 < refw || h * 2 < refh || w > 16 * refw || h > 16 * refh) {
682 av_log(ctx, AV_LOG_ERROR,
683 "Invalid ref frame dimensions %dx%d for frame size %dx%d\n",
685 return AVERROR_INVALIDDATA;
687 s->mvscale[i][0] = (refw << 14) / w;
688 s->mvscale[i][1] = (refh << 14) / h;
689 s->mvstep[i][0] = 16 * s->mvscale[i][0] >> 14;
690 s->mvstep[i][1] = 16 * s->mvscale[i][1] >> 14;
695 s->refreshctx = s->errorres ? 0 : get_bits1(&s->gb);
696 s->parallelmode = s->errorres ? 1 : get_bits1(&s->gb);
697 s->framectxid = c = get_bits(&s->gb, 2);
699 /* loopfilter header data */
700 s->filter.level = get_bits(&s->gb, 6);
701 sharp = get_bits(&s->gb, 3);
702 // if sharpness changed, reinit lim/mblim LUTs. if it didn't change, keep
703 // the old cache values since they are still valid
704 if (s->filter.sharpness != sharp)
705 memset(s->filter.lim_lut, 0, sizeof(s->filter.lim_lut));
706 s->filter.sharpness = sharp;
707 if ((s->lf_delta.enabled = get_bits1(&s->gb))) {
708 if (get_bits1(&s->gb)) {
709 for (i = 0; i < 4; i++)
710 if (get_bits1(&s->gb))
711 s->lf_delta.ref[i] = get_sbits_inv(&s->gb, 6);
712 for (i = 0; i < 2; i++)
713 if (get_bits1(&s->gb))
714 s->lf_delta.mode[i] = get_sbits_inv(&s->gb, 6);
718 /* quantization header data */
719 s->yac_qi = get_bits(&s->gb, 8);
720 s->ydc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
721 s->uvdc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
722 s->uvac_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
723 s->lossless = s->yac_qi == 0 && s->ydc_qdelta == 0 &&
724 s->uvdc_qdelta == 0 && s->uvac_qdelta == 0;
726 /* segmentation header info */
727 if ((s->segmentation.enabled = get_bits1(&s->gb))) {
728 if ((s->segmentation.update_map = get_bits1(&s->gb))) {
729 for (i = 0; i < 7; i++)
730 s->prob.seg[i] = get_bits1(&s->gb) ?
731 get_bits(&s->gb, 8) : 255;
732 if ((s->segmentation.temporal = get_bits1(&s->gb))) {
733 for (i = 0; i < 3; i++)
734 s->prob.segpred[i] = get_bits1(&s->gb) ?
735 get_bits(&s->gb, 8) : 255;
738 if ((!s->segmentation.update_map || s->segmentation.temporal) &&
739 (w != s->frames[CUR_FRAME].tf.f->width ||
740 h != s->frames[CUR_FRAME].tf.f->height)) {
741 av_log(ctx, AV_LOG_ERROR,
742 "Reference segmap (temp=%d,update=%d) enabled on size-change!\n",
743 s->segmentation.temporal, s->segmentation.update_map);
744 return AVERROR_INVALIDDATA;
747 if (get_bits1(&s->gb)) {
748 s->segmentation.absolute_vals = get_bits1(&s->gb);
749 for (i = 0; i < 8; i++) {
750 if ((s->segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
751 s->segmentation.feat[i].q_val = get_sbits_inv(&s->gb, 8);
752 if ((s->segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
753 s->segmentation.feat[i].lf_val = get_sbits_inv(&s->gb, 6);
754 if ((s->segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
755 s->segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
756 s->segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
760 s->segmentation.feat[0].q_enabled = 0;
761 s->segmentation.feat[0].lf_enabled = 0;
762 s->segmentation.feat[0].skip_enabled = 0;
763 s->segmentation.feat[0].ref_enabled = 0;
766 // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
767 for (i = 0; i < (s->segmentation.enabled ? 8 : 1); i++) {
768 int qyac, qydc, quvac, quvdc, lflvl, sh;
770 if (s->segmentation.feat[i].q_enabled) {
771 if (s->segmentation.absolute_vals)
772 qyac = s->segmentation.feat[i].q_val;
774 qyac = s->yac_qi + s->segmentation.feat[i].q_val;
778 qydc = av_clip_uintp2(qyac + s->ydc_qdelta, 8);
779 quvdc = av_clip_uintp2(qyac + s->uvdc_qdelta, 8);
780 quvac = av_clip_uintp2(qyac + s->uvac_qdelta, 8);
781 qyac = av_clip_uintp2(qyac, 8);
783 s->segmentation.feat[i].qmul[0][0] = vp9_dc_qlookup[s->bpp_index][qydc];
784 s->segmentation.feat[i].qmul[0][1] = vp9_ac_qlookup[s->bpp_index][qyac];
785 s->segmentation.feat[i].qmul[1][0] = vp9_dc_qlookup[s->bpp_index][quvdc];
786 s->segmentation.feat[i].qmul[1][1] = vp9_ac_qlookup[s->bpp_index][quvac];
788 sh = s->filter.level >= 32;
789 if (s->segmentation.feat[i].lf_enabled) {
790 if (s->segmentation.absolute_vals)
791 lflvl = s->segmentation.feat[i].lf_val;
793 lflvl = s->filter.level + s->segmentation.feat[i].lf_val;
795 lflvl = s->filter.level;
797 if (s->lf_delta.enabled) {
798 s->segmentation.feat[i].lflvl[0][0] =
799 s->segmentation.feat[i].lflvl[0][1] =
800 av_clip_uintp2(lflvl + (s->lf_delta.ref[0] << sh), 6);
801 for (j = 1; j < 4; j++) {
802 s->segmentation.feat[i].lflvl[j][0] =
803 av_clip_uintp2(lflvl + ((s->lf_delta.ref[j] +
804 s->lf_delta.mode[0]) * (1 << sh)), 6);
805 s->segmentation.feat[i].lflvl[j][1] =
806 av_clip_uintp2(lflvl + ((s->lf_delta.ref[j] +
807 s->lf_delta.mode[1]) * (1 << sh)), 6);
810 memset(s->segmentation.feat[i].lflvl, lflvl,
811 sizeof(s->segmentation.feat[i].lflvl));
816 if ((res = update_size(ctx, w, h, fmt)) < 0) {
817 av_log(ctx, AV_LOG_ERROR, "Failed to initialize decoder for %dx%d @ %d\n", w, h, fmt);
820 for (s->tiling.log2_tile_cols = 0;
821 (s->sb_cols >> s->tiling.log2_tile_cols) > 64;
822 s->tiling.log2_tile_cols++) ;
823 for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
824 max = FFMAX(0, max - 1);
825 while (max > s->tiling.log2_tile_cols) {
826 if (get_bits1(&s->gb))
827 s->tiling.log2_tile_cols++;
831 s->tiling.log2_tile_rows = decode012(&s->gb);
832 s->tiling.tile_rows = 1 << s->tiling.log2_tile_rows;
833 if (s->tiling.tile_cols != (1 << s->tiling.log2_tile_cols)) {
834 s->tiling.tile_cols = 1 << s->tiling.log2_tile_cols;
835 s->c_b = av_fast_realloc(s->c_b, &s->c_b_size,
836 sizeof(VP56RangeCoder) * s->tiling.tile_cols);
838 av_log(ctx, AV_LOG_ERROR, "Ran out of memory during range coder init\n");
839 return AVERROR(ENOMEM);
843 if (s->keyframe || s->errorres || s->intraonly) {
844 s->prob_ctx[0].p = s->prob_ctx[1].p = s->prob_ctx[2].p =
845 s->prob_ctx[3].p = vp9_default_probs;
846 memcpy(s->prob_ctx[0].coef, vp9_default_coef_probs,
847 sizeof(vp9_default_coef_probs));
848 memcpy(s->prob_ctx[1].coef, vp9_default_coef_probs,
849 sizeof(vp9_default_coef_probs));
850 memcpy(s->prob_ctx[2].coef, vp9_default_coef_probs,
851 sizeof(vp9_default_coef_probs));
852 memcpy(s->prob_ctx[3].coef, vp9_default_coef_probs,
853 sizeof(vp9_default_coef_probs));
856 // next 16 bits is size of the rest of the header (arith-coded)
857 size2 = get_bits(&s->gb, 16);
858 data2 = align_get_bits(&s->gb);
859 if (size2 > size - (data2 - data)) {
860 av_log(ctx, AV_LOG_ERROR, "Invalid compressed header size\n");
861 return AVERROR_INVALIDDATA;
863 ff_vp56_init_range_decoder(&s->c, data2, size2);
864 if (vp56_rac_get_prob_branchy(&s->c, 128)) { // marker bit
865 av_log(ctx, AV_LOG_ERROR, "Marker bit was set\n");
866 return AVERROR_INVALIDDATA;
869 if (s->keyframe || s->intraonly) {
870 memset(s->counts.coef, 0, sizeof(s->counts.coef));
871 memset(s->counts.eob, 0, sizeof(s->counts.eob));
873 memset(&s->counts, 0, sizeof(s->counts));
875 // FIXME is it faster to not copy here, but do it down in the fw updates
876 // as explicit copies if the fw update is missing (and skip the copy upon
878 s->prob.p = s->prob_ctx[c].p;
882 s->txfmmode = TX_4X4;
884 s->txfmmode = vp8_rac_get_uint(&s->c, 2);
885 if (s->txfmmode == 3)
886 s->txfmmode += vp8_rac_get(&s->c);
888 if (s->txfmmode == TX_SWITCHABLE) {
889 for (i = 0; i < 2; i++)
890 if (vp56_rac_get_prob_branchy(&s->c, 252))
891 s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
892 for (i = 0; i < 2; i++)
893 for (j = 0; j < 2; j++)
894 if (vp56_rac_get_prob_branchy(&s->c, 252))
895 s->prob.p.tx16p[i][j] =
896 update_prob(&s->c, s->prob.p.tx16p[i][j]);
897 for (i = 0; i < 2; i++)
898 for (j = 0; j < 3; j++)
899 if (vp56_rac_get_prob_branchy(&s->c, 252))
900 s->prob.p.tx32p[i][j] =
901 update_prob(&s->c, s->prob.p.tx32p[i][j]);
906 for (i = 0; i < 4; i++) {
907 uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
908 if (vp8_rac_get(&s->c)) {
909 for (j = 0; j < 2; j++)
910 for (k = 0; k < 2; k++)
911 for (l = 0; l < 6; l++)
912 for (m = 0; m < 6; m++) {
913 uint8_t *p = s->prob.coef[i][j][k][l][m];
914 uint8_t *r = ref[j][k][l][m];
915 if (m >= 3 && l == 0) // dc only has 3 pt
917 for (n = 0; n < 3; n++) {
918 if (vp56_rac_get_prob_branchy(&s->c, 252)) {
919 p[n] = update_prob(&s->c, r[n]);
927 for (j = 0; j < 2; j++)
928 for (k = 0; k < 2; k++)
929 for (l = 0; l < 6; l++)
930 for (m = 0; m < 6; m++) {
931 uint8_t *p = s->prob.coef[i][j][k][l][m];
932 uint8_t *r = ref[j][k][l][m];
933 if (m > 3 && l == 0) // dc only has 3 pt
939 if (s->txfmmode == i)
944 for (i = 0; i < 3; i++)
945 if (vp56_rac_get_prob_branchy(&s->c, 252))
946 s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
947 if (!s->keyframe && !s->intraonly) {
948 for (i = 0; i < 7; i++)
949 for (j = 0; j < 3; j++)
950 if (vp56_rac_get_prob_branchy(&s->c, 252))
951 s->prob.p.mv_mode[i][j] =
952 update_prob(&s->c, s->prob.p.mv_mode[i][j]);
954 if (s->filtermode == FILTER_SWITCHABLE)
955 for (i = 0; i < 4; i++)
956 for (j = 0; j < 2; j++)
957 if (vp56_rac_get_prob_branchy(&s->c, 252))
958 s->prob.p.filter[i][j] =
959 update_prob(&s->c, s->prob.p.filter[i][j]);
961 for (i = 0; i < 4; i++)
962 if (vp56_rac_get_prob_branchy(&s->c, 252))
963 s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
965 if (s->allowcompinter) {
966 s->comppredmode = vp8_rac_get(&s->c);
968 s->comppredmode += vp8_rac_get(&s->c);
969 if (s->comppredmode == PRED_SWITCHABLE)
970 for (i = 0; i < 5; i++)
971 if (vp56_rac_get_prob_branchy(&s->c, 252))
973 update_prob(&s->c, s->prob.p.comp[i]);
975 s->comppredmode = PRED_SINGLEREF;
978 if (s->comppredmode != PRED_COMPREF) {
979 for (i = 0; i < 5; i++) {
980 if (vp56_rac_get_prob_branchy(&s->c, 252))
981 s->prob.p.single_ref[i][0] =
982 update_prob(&s->c, s->prob.p.single_ref[i][0]);
983 if (vp56_rac_get_prob_branchy(&s->c, 252))
984 s->prob.p.single_ref[i][1] =
985 update_prob(&s->c, s->prob.p.single_ref[i][1]);
989 if (s->comppredmode != PRED_SINGLEREF) {
990 for (i = 0; i < 5; i++)
991 if (vp56_rac_get_prob_branchy(&s->c, 252))
992 s->prob.p.comp_ref[i] =
993 update_prob(&s->c, s->prob.p.comp_ref[i]);
996 for (i = 0; i < 4; i++)
997 for (j = 0; j < 9; j++)
998 if (vp56_rac_get_prob_branchy(&s->c, 252))
999 s->prob.p.y_mode[i][j] =
1000 update_prob(&s->c, s->prob.p.y_mode[i][j]);
1002 for (i = 0; i < 4; i++)
1003 for (j = 0; j < 4; j++)
1004 for (k = 0; k < 3; k++)
1005 if (vp56_rac_get_prob_branchy(&s->c, 252))
1006 s->prob.p.partition[3 - i][j][k] =
1007 update_prob(&s->c, s->prob.p.partition[3 - i][j][k]);
1009 // mv fields don't use the update_prob subexp model for some reason
1010 for (i = 0; i < 3; i++)
1011 if (vp56_rac_get_prob_branchy(&s->c, 252))
1012 s->prob.p.mv_joint[i] = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1014 for (i = 0; i < 2; i++) {
1015 if (vp56_rac_get_prob_branchy(&s->c, 252))
1016 s->prob.p.mv_comp[i].sign = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1018 for (j = 0; j < 10; j++)
1019 if (vp56_rac_get_prob_branchy(&s->c, 252))
1020 s->prob.p.mv_comp[i].classes[j] =
1021 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1023 if (vp56_rac_get_prob_branchy(&s->c, 252))
1024 s->prob.p.mv_comp[i].class0 = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1026 for (j = 0; j < 10; j++)
1027 if (vp56_rac_get_prob_branchy(&s->c, 252))
1028 s->prob.p.mv_comp[i].bits[j] =
1029 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1032 for (i = 0; i < 2; i++) {
1033 for (j = 0; j < 2; j++)
1034 for (k = 0; k < 3; k++)
1035 if (vp56_rac_get_prob_branchy(&s->c, 252))
1036 s->prob.p.mv_comp[i].class0_fp[j][k] =
1037 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1039 for (j = 0; j < 3; j++)
1040 if (vp56_rac_get_prob_branchy(&s->c, 252))
1041 s->prob.p.mv_comp[i].fp[j] =
1042 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1045 if (s->highprecisionmvs) {
1046 for (i = 0; i < 2; i++) {
1047 if (vp56_rac_get_prob_branchy(&s->c, 252))
1048 s->prob.p.mv_comp[i].class0_hp =
1049 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1051 if (vp56_rac_get_prob_branchy(&s->c, 252))
1052 s->prob.p.mv_comp[i].hp =
1053 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1058 return (data2 - data) + size2;
1061 static av_always_inline void clamp_mv(VP56mv *dst, const VP56mv *src,
1064 dst->x = av_clip(src->x, s->min_mv.x, s->max_mv.x);
1065 dst->y = av_clip(src->y, s->min_mv.y, s->max_mv.y);
1068 static void find_ref_mvs(VP9Context *s,
1069 VP56mv *pmv, int ref, int z, int idx, int sb)
1071 static const int8_t mv_ref_blk_off[N_BS_SIZES][8][2] = {
1072 [BS_64x64] = {{ 3, -1 }, { -1, 3 }, { 4, -1 }, { -1, 4 },
1073 { -1, -1 }, { 0, -1 }, { -1, 0 }, { 6, -1 }},
1074 [BS_64x32] = {{ 0, -1 }, { -1, 0 }, { 4, -1 }, { -1, 2 },
1075 { -1, -1 }, { 0, -3 }, { -3, 0 }, { 2, -1 }},
1076 [BS_32x64] = {{ -1, 0 }, { 0, -1 }, { -1, 4 }, { 2, -1 },
1077 { -1, -1 }, { -3, 0 }, { 0, -3 }, { -1, 2 }},
1078 [BS_32x32] = {{ 1, -1 }, { -1, 1 }, { 2, -1 }, { -1, 2 },
1079 { -1, -1 }, { 0, -3 }, { -3, 0 }, { -3, -3 }},
1080 [BS_32x16] = {{ 0, -1 }, { -1, 0 }, { 2, -1 }, { -1, -1 },
1081 { -1, 1 }, { 0, -3 }, { -3, 0 }, { -3, -3 }},
1082 [BS_16x32] = {{ -1, 0 }, { 0, -1 }, { -1, 2 }, { -1, -1 },
1083 { 1, -1 }, { -3, 0 }, { 0, -3 }, { -3, -3 }},
1084 [BS_16x16] = {{ 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, 1 },
1085 { -1, -1 }, { 0, -3 }, { -3, 0 }, { -3, -3 }},
1086 [BS_16x8] = {{ 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, -1 },
1087 { 0, -2 }, { -2, 0 }, { -2, -1 }, { -1, -2 }},
1088 [BS_8x16] = {{ -1, 0 }, { 0, -1 }, { -1, 1 }, { -1, -1 },
1089 { -2, 0 }, { 0, -2 }, { -1, -2 }, { -2, -1 }},
1090 [BS_8x8] = {{ 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
1091 { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 }},
1092 [BS_8x4] = {{ 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
1093 { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 }},
1094 [BS_4x8] = {{ 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
1095 { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 }},
1096 [BS_4x4] = {{ 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
1097 { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 }},
1100 int row = s->row, col = s->col, row7 = s->row7;
1101 const int8_t (*p)[2] = mv_ref_blk_off[b->bs];
1102 #define INVALID_MV 0x80008000U
1103 uint32_t mem = INVALID_MV;
1106 #define RETURN_DIRECT_MV(mv) \
1108 uint32_t m = AV_RN32A(&mv); \
1112 } else if (mem == INVALID_MV) { \
1114 } else if (m != mem) { \
1121 if (sb == 2 || sb == 1) {
1122 RETURN_DIRECT_MV(b->mv[0][z]);
1123 } else if (sb == 3) {
1124 RETURN_DIRECT_MV(b->mv[2][z]);
1125 RETURN_DIRECT_MV(b->mv[1][z]);
1126 RETURN_DIRECT_MV(b->mv[0][z]);
1129 #define RETURN_MV(mv) \
1134 clamp_mv(&tmp, &mv, s); \
1135 m = AV_RN32A(&tmp); \
1139 } else if (mem == INVALID_MV) { \
1141 } else if (m != mem) { \
1146 uint32_t m = AV_RN32A(&mv); \
1148 clamp_mv(pmv, &mv, s); \
1150 } else if (mem == INVALID_MV) { \
1152 } else if (m != mem) { \
1153 clamp_mv(pmv, &mv, s); \
1160 struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[(row - 1) * s->sb_cols * 8 + col];
1161 if (mv->ref[0] == ref) {
1162 RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][0]);
1163 } else if (mv->ref[1] == ref) {
1164 RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][1]);
1167 if (col > s->tiling.tile_col_start) {
1168 struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[row * s->sb_cols * 8 + col - 1];
1169 if (mv->ref[0] == ref) {
1170 RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][0]);
1171 } else if (mv->ref[1] == ref) {
1172 RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][1]);
1180 // previously coded MVs in this neighbourhood, using same reference frame
1181 for (; i < 8; i++) {
1182 int c = p[i][0] + col, r = p[i][1] + row;
1184 if (c >= s->tiling.tile_col_start && c < s->cols && r >= 0 && r < s->rows) {
1185 struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
1187 if (mv->ref[0] == ref) {
1188 RETURN_MV(mv->mv[0]);
1189 } else if (mv->ref[1] == ref) {
1190 RETURN_MV(mv->mv[1]);
1195 // MV at this position in previous frame, using same reference frame
1196 if (s->use_last_frame_mvs) {
1197 struct VP9mvrefPair *mv = &s->frames[REF_FRAME_MVPAIR].mv[row * s->sb_cols * 8 + col];
1199 if (!s->frames[REF_FRAME_MVPAIR].uses_2pass)
1200 ff_thread_await_progress(&s->frames[REF_FRAME_MVPAIR].tf, row >> 3, 0);
1201 if (mv->ref[0] == ref) {
1202 RETURN_MV(mv->mv[0]);
1203 } else if (mv->ref[1] == ref) {
1204 RETURN_MV(mv->mv[1]);
1208 #define RETURN_SCALE_MV(mv, scale) \
1211 VP56mv mv_temp = { -mv.x, -mv.y }; \
1212 RETURN_MV(mv_temp); \
1218 // previously coded MVs in this neighbourhood, using different reference frame
1219 for (i = 0; i < 8; i++) {
1220 int c = p[i][0] + col, r = p[i][1] + row;
1222 if (c >= s->tiling.tile_col_start && c < s->cols && r >= 0 && r < s->rows) {
1223 struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
1225 if (mv->ref[0] != ref && mv->ref[0] >= 0) {
1226 RETURN_SCALE_MV(mv->mv[0], s->signbias[mv->ref[0]] != s->signbias[ref]);
1228 if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
1229 // BUG - libvpx has this condition regardless of whether
1230 // we used the first ref MV and pre-scaling
1231 AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
1232 RETURN_SCALE_MV(mv->mv[1], s->signbias[mv->ref[1]] != s->signbias[ref]);
1237 // MV at this position in previous frame, using different reference frame
1238 if (s->use_last_frame_mvs) {
1239 struct VP9mvrefPair *mv = &s->frames[REF_FRAME_MVPAIR].mv[row * s->sb_cols * 8 + col];
1241 // no need to await_progress, because we already did that above
1242 if (mv->ref[0] != ref && mv->ref[0] >= 0) {
1243 RETURN_SCALE_MV(mv->mv[0], s->signbias[mv->ref[0]] != s->signbias[ref]);
1245 if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
1246 // BUG - libvpx has this condition regardless of whether
1247 // we used the first ref MV and pre-scaling
1248 AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
1249 RETURN_SCALE_MV(mv->mv[1], s->signbias[mv->ref[1]] != s->signbias[ref]);
1256 #undef RETURN_SCALE_MV
1259 static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
1261 int bit, sign = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].sign);
1262 int n, c = vp8_rac_get_tree(&s->c, vp9_mv_class_tree,
1263 s->prob.p.mv_comp[idx].classes);
1265 s->counts.mv_comp[idx].sign[sign]++;
1266 s->counts.mv_comp[idx].classes[c]++;
1270 for (n = 0, m = 0; m < c; m++) {
1271 bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].bits[m]);
1273 s->counts.mv_comp[idx].bits[m][bit]++;
1276 bit = vp8_rac_get_tree(&s->c, vp9_mv_fp_tree, s->prob.p.mv_comp[idx].fp);
1278 s->counts.mv_comp[idx].fp[bit]++;
1280 bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].hp);
1281 s->counts.mv_comp[idx].hp[bit]++;
1285 // bug in libvpx - we count for bw entropy purposes even if the
1287 s->counts.mv_comp[idx].hp[1]++;
1291 n = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0);
1292 s->counts.mv_comp[idx].class0[n]++;
1293 bit = vp8_rac_get_tree(&s->c, vp9_mv_fp_tree,
1294 s->prob.p.mv_comp[idx].class0_fp[n]);
1295 s->counts.mv_comp[idx].class0_fp[n][bit]++;
1296 n = (n << 3) | (bit << 1);
1298 bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0_hp);
1299 s->counts.mv_comp[idx].class0_hp[bit]++;
1303 // bug in libvpx - we count for bw entropy purposes even if the
1305 s->counts.mv_comp[idx].class0_hp[1]++;
1309 return sign ? -(n + 1) : (n + 1);
1312 static void fill_mv(VP9Context *s,
1313 VP56mv *mv, int mode, int sb)
1317 if (mode == ZEROMV) {
1322 // FIXME cache this value and reuse for other subblocks
1323 find_ref_mvs(s, &mv[0], b->ref[0], 0, mode == NEARMV,
1324 mode == NEWMV ? -1 : sb);
1325 // FIXME maybe move this code into find_ref_mvs()
1326 if ((mode == NEWMV || sb == -1) &&
1327 !(hp = s->highprecisionmvs && abs(mv[0].x) < 64 && abs(mv[0].y) < 64)) {
1341 if (mode == NEWMV) {
1342 enum MVJoint j = vp8_rac_get_tree(&s->c, vp9_mv_joint_tree,
1343 s->prob.p.mv_joint);
1345 s->counts.mv_joint[j]++;
1346 if (j >= MV_JOINT_V)
1347 mv[0].y += read_mv_component(s, 0, hp);
1349 mv[0].x += read_mv_component(s, 1, hp);
1353 // FIXME cache this value and reuse for other subblocks
1354 find_ref_mvs(s, &mv[1], b->ref[1], 1, mode == NEARMV,
1355 mode == NEWMV ? -1 : sb);
1356 if ((mode == NEWMV || sb == -1) &&
1357 !(hp = s->highprecisionmvs && abs(mv[1].x) < 64 && abs(mv[1].y) < 64)) {
1371 if (mode == NEWMV) {
1372 enum MVJoint j = vp8_rac_get_tree(&s->c, vp9_mv_joint_tree,
1373 s->prob.p.mv_joint);
1375 s->counts.mv_joint[j]++;
1376 if (j >= MV_JOINT_V)
1377 mv[1].y += read_mv_component(s, 0, hp);
1379 mv[1].x += read_mv_component(s, 1, hp);
1385 static av_always_inline void setctx_2d(uint8_t *ptr, int w, int h,
1386 ptrdiff_t stride, int v)
1396 int v16 = v * 0x0101;
1404 uint32_t v32 = v * 0x01010101;
1413 uint64_t v64 = v * 0x0101010101010101ULL;
1419 uint32_t v32 = v * 0x01010101;
1422 AV_WN32A(ptr + 4, v32);
1431 static void decode_mode(AVCodecContext *ctx)
1433 static const uint8_t left_ctx[N_BS_SIZES] = {
1434 0x0, 0x8, 0x0, 0x8, 0xc, 0x8, 0xc, 0xe, 0xc, 0xe, 0xf, 0xe, 0xf
1436 static const uint8_t above_ctx[N_BS_SIZES] = {
1437 0x0, 0x0, 0x8, 0x8, 0x8, 0xc, 0xc, 0xc, 0xe, 0xe, 0xe, 0xf, 0xf
1439 static const uint8_t max_tx_for_bl_bp[N_BS_SIZES] = {
1440 TX_32X32, TX_32X32, TX_32X32, TX_32X32, TX_16X16, TX_16X16,
1441 TX_16X16, TX_8X8, TX_8X8, TX_8X8, TX_4X4, TX_4X4, TX_4X4
1443 VP9Context *s = ctx->priv_data;
1445 int row = s->row, col = s->col, row7 = s->row7;
1446 enum TxfmMode max_tx = max_tx_for_bl_bp[b->bs];
1447 int bw4 = bwh_tab[1][b->bs][0], w4 = FFMIN(s->cols - col, bw4);
1448 int bh4 = bwh_tab[1][b->bs][1], h4 = FFMIN(s->rows - row, bh4), y;
1449 int have_a = row > 0, have_l = col > s->tiling.tile_col_start;
1450 int vref, filter_id;
1452 if (!s->segmentation.enabled) {
1454 } else if (s->keyframe || s->intraonly) {
1455 b->seg_id = vp8_rac_get_tree(&s->c, vp9_segmentation_tree, s->prob.seg);
1456 } else if (!s->segmentation.update_map ||
1457 (s->segmentation.temporal &&
1458 vp56_rac_get_prob_branchy(&s->c,
1459 s->prob.segpred[s->above_segpred_ctx[col] +
1460 s->left_segpred_ctx[row7]]))) {
1463 uint8_t *refsegmap = s->frames[REF_FRAME_SEGMAP].segmentation_map;
1465 if (!s->frames[REF_FRAME_SEGMAP].uses_2pass)
1466 ff_thread_await_progress(&s->frames[REF_FRAME_SEGMAP].tf, row >> 3, 0);
1467 for (y = 0; y < h4; y++) {
1468 int idx_base = (y + row) * 8 * s->sb_cols + col;
1469 for (x = 0; x < w4; x++)
1470 pred = FFMIN(pred, refsegmap[idx_base + x]);
1472 av_assert1(pred < 8);
1478 memset(&s->above_segpred_ctx[col], 1, w4);
1479 memset(&s->left_segpred_ctx[row7], 1, h4);
1481 b->seg_id = vp8_rac_get_tree(&s->c, vp9_segmentation_tree,
1484 memset(&s->above_segpred_ctx[col], 0, w4);
1485 memset(&s->left_segpred_ctx[row7], 0, h4);
1487 if (s->segmentation.enabled &&
1488 (s->segmentation.update_map || s->keyframe || s->intraonly)) {
1489 setctx_2d(&s->frames[CUR_FRAME].segmentation_map[row * 8 * s->sb_cols + col],
1490 bw4, bh4, 8 * s->sb_cols, b->seg_id);
1493 b->skip = s->segmentation.enabled &&
1494 s->segmentation.feat[b->seg_id].skip_enabled;
1496 int c = s->left_skip_ctx[row7] + s->above_skip_ctx[col];
1497 b->skip = vp56_rac_get_prob(&s->c, s->prob.p.skip[c]);
1498 s->counts.skip[c][b->skip]++;
1501 if (s->keyframe || s->intraonly) {
1503 } else if (s->segmentation.feat[b->seg_id].ref_enabled) {
1504 b->intra = !s->segmentation.feat[b->seg_id].ref_val;
1508 if (have_a && have_l) {
1509 c = s->above_intra_ctx[col] + s->left_intra_ctx[row7];
1512 c = have_a ? 2 * s->above_intra_ctx[col] :
1513 have_l ? 2 * s->left_intra_ctx[row7] : 0;
1515 bit = vp56_rac_get_prob(&s->c, s->prob.p.intra[c]);
1516 s->counts.intra[c][bit]++;
1520 if ((b->intra || !b->skip) && s->txfmmode == TX_SWITCHABLE) {
1524 c = (s->above_skip_ctx[col] ? max_tx :
1525 s->above_txfm_ctx[col]) +
1526 (s->left_skip_ctx[row7] ? max_tx :
1527 s->left_txfm_ctx[row7]) > max_tx;
1529 c = s->above_skip_ctx[col] ? 1 :
1530 (s->above_txfm_ctx[col] * 2 > max_tx);
1532 } else if (have_l) {
1533 c = s->left_skip_ctx[row7] ? 1 :
1534 (s->left_txfm_ctx[row7] * 2 > max_tx);
1540 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][0]);
1542 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][1]);
1544 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][2]);
1546 s->counts.tx32p[c][b->tx]++;
1549 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][0]);
1551 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][1]);
1552 s->counts.tx16p[c][b->tx]++;
1555 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx8p[c]);
1556 s->counts.tx8p[c][b->tx]++;
1563 b->tx = FFMIN(max_tx, s->txfmmode);
1566 if (s->keyframe || s->intraonly) {
1567 uint8_t *a = &s->above_mode_ctx[col * 2];
1568 uint8_t *l = &s->left_mode_ctx[(row7) << 1];
1571 if (b->bs > BS_8x8) {
1572 // FIXME the memory storage intermediates here aren't really
1573 // necessary, they're just there to make the code slightly
1575 b->mode[0] = a[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1576 vp9_default_kf_ymode_probs[a[0]][l[0]]);
1577 if (b->bs != BS_8x4) {
1578 b->mode[1] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1579 vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
1580 l[0] = a[1] = b->mode[1];
1582 l[0] = a[1] = b->mode[1] = b->mode[0];
1584 if (b->bs != BS_4x8) {
1585 b->mode[2] = a[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1586 vp9_default_kf_ymode_probs[a[0]][l[1]]);
1587 if (b->bs != BS_8x4) {
1588 b->mode[3] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1589 vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
1590 l[1] = a[1] = b->mode[3];
1592 l[1] = a[1] = b->mode[3] = b->mode[2];
1595 b->mode[2] = b->mode[0];
1596 l[1] = a[1] = b->mode[3] = b->mode[1];
1599 b->mode[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1600 vp9_default_kf_ymode_probs[*a][*l]);
1601 b->mode[3] = b->mode[2] = b->mode[1] = b->mode[0];
1602 // FIXME this can probably be optimized
1603 memset(a, b->mode[0], bwh_tab[0][b->bs][0]);
1604 memset(l, b->mode[0], bwh_tab[0][b->bs][1]);
1606 b->uvmode = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1607 vp9_default_kf_uvmode_probs[b->mode[3]]);
1608 } else if (b->intra) {
1610 if (b->bs > BS_8x8) {
1611 b->mode[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1612 s->prob.p.y_mode[0]);
1613 s->counts.y_mode[0][b->mode[0]]++;
1614 if (b->bs != BS_8x4) {
1615 b->mode[1] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1616 s->prob.p.y_mode[0]);
1617 s->counts.y_mode[0][b->mode[1]]++;
1619 b->mode[1] = b->mode[0];
1621 if (b->bs != BS_4x8) {
1622 b->mode[2] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1623 s->prob.p.y_mode[0]);
1624 s->counts.y_mode[0][b->mode[2]]++;
1625 if (b->bs != BS_8x4) {
1626 b->mode[3] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1627 s->prob.p.y_mode[0]);
1628 s->counts.y_mode[0][b->mode[3]]++;
1630 b->mode[3] = b->mode[2];
1633 b->mode[2] = b->mode[0];
1634 b->mode[3] = b->mode[1];
1637 static const uint8_t size_group[10] = {
1638 3, 3, 3, 3, 2, 2, 2, 1, 1, 1
1640 int sz = size_group[b->bs];
1642 b->mode[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1643 s->prob.p.y_mode[sz]);
1644 b->mode[1] = b->mode[2] = b->mode[3] = b->mode[0];
1645 s->counts.y_mode[sz][b->mode[3]]++;
1647 b->uvmode = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
1648 s->prob.p.uv_mode[b->mode[3]]);
1649 s->counts.uv_mode[b->mode[3]][b->uvmode]++;
1651 static const uint8_t inter_mode_ctx_lut[14][14] = {
1652 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1653 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1654 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1655 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1656 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1657 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1658 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1659 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1660 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1661 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
1662 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
1663 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
1664 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 3 },
1665 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
1668 if (s->segmentation.feat[b->seg_id].ref_enabled) {
1669 av_assert2(s->segmentation.feat[b->seg_id].ref_val != 0);
1671 b->ref[0] = s->segmentation.feat[b->seg_id].ref_val - 1;
1673 // read comp_pred flag
1674 if (s->comppredmode != PRED_SWITCHABLE) {
1675 b->comp = s->comppredmode == PRED_COMPREF;
1679 // FIXME add intra as ref=0xff (or -1) to make these easier?
1682 if (s->above_comp_ctx[col] && s->left_comp_ctx[row7]) {
1684 } else if (s->above_comp_ctx[col]) {
1685 c = 2 + (s->left_intra_ctx[row7] ||
1686 s->left_ref_ctx[row7] == s->fixcompref);
1687 } else if (s->left_comp_ctx[row7]) {
1688 c = 2 + (s->above_intra_ctx[col] ||
1689 s->above_ref_ctx[col] == s->fixcompref);
1691 c = (!s->above_intra_ctx[col] &&
1692 s->above_ref_ctx[col] == s->fixcompref) ^
1693 (!s->left_intra_ctx[row7] &&
1694 s->left_ref_ctx[row & 7] == s->fixcompref);
1697 c = s->above_comp_ctx[col] ? 3 :
1698 (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->fixcompref);
1700 } else if (have_l) {
1701 c = s->left_comp_ctx[row7] ? 3 :
1702 (!s->left_intra_ctx[row7] && s->left_ref_ctx[row7] == s->fixcompref);
1706 b->comp = vp56_rac_get_prob(&s->c, s->prob.p.comp[c]);
1707 s->counts.comp[c][b->comp]++;
1710 // read actual references
1711 // FIXME probably cache a few variables here to prevent repetitive
1712 // memory accesses below
1713 if (b->comp) /* two references */ {
1714 int fix_idx = s->signbias[s->fixcompref], var_idx = !fix_idx, c, bit;
1716 b->ref[fix_idx] = s->fixcompref;
1717 // FIXME can this codeblob be replaced by some sort of LUT?
1720 if (s->above_intra_ctx[col]) {
1721 if (s->left_intra_ctx[row7]) {
1724 c = 1 + 2 * (s->left_ref_ctx[row7] != s->varcompref[1]);
1726 } else if (s->left_intra_ctx[row7]) {
1727 c = 1 + 2 * (s->above_ref_ctx[col] != s->varcompref[1]);
1729 int refl = s->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
1731 if (refl == refa && refa == s->varcompref[1]) {
1733 } else if (!s->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
1734 if ((refa == s->fixcompref && refl == s->varcompref[0]) ||
1735 (refl == s->fixcompref && refa == s->varcompref[0])) {
1738 c = (refa == refl) ? 3 : 1;
1740 } else if (!s->left_comp_ctx[row7]) {
1741 if (refa == s->varcompref[1] && refl != s->varcompref[1]) {
1744 c = (refl == s->varcompref[1] &&
1745 refa != s->varcompref[1]) ? 2 : 4;
1747 } else if (!s->above_comp_ctx[col]) {
1748 if (refl == s->varcompref[1] && refa != s->varcompref[1]) {
1751 c = (refa == s->varcompref[1] &&
1752 refl != s->varcompref[1]) ? 2 : 4;
1755 c = (refl == refa) ? 4 : 2;
1759 if (s->above_intra_ctx[col]) {
1761 } else if (s->above_comp_ctx[col]) {
1762 c = 4 * (s->above_ref_ctx[col] != s->varcompref[1]);
1764 c = 3 * (s->above_ref_ctx[col] != s->varcompref[1]);
1767 } else if (have_l) {
1768 if (s->left_intra_ctx[row7]) {
1770 } else if (s->left_comp_ctx[row7]) {
1771 c = 4 * (s->left_ref_ctx[row7] != s->varcompref[1]);
1773 c = 3 * (s->left_ref_ctx[row7] != s->varcompref[1]);
1778 bit = vp56_rac_get_prob(&s->c, s->prob.p.comp_ref[c]);
1779 b->ref[var_idx] = s->varcompref[bit];
1780 s->counts.comp_ref[c][bit]++;
1781 } else /* single reference */ {
1784 if (have_a && !s->above_intra_ctx[col]) {
1785 if (have_l && !s->left_intra_ctx[row7]) {
1786 if (s->left_comp_ctx[row7]) {
1787 if (s->above_comp_ctx[col]) {
1788 c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7] ||
1789 !s->above_ref_ctx[col]);
1791 c = (3 * !s->above_ref_ctx[col]) +
1792 (!s->fixcompref || !s->left_ref_ctx[row7]);
1794 } else if (s->above_comp_ctx[col]) {
1795 c = (3 * !s->left_ref_ctx[row7]) +
1796 (!s->fixcompref || !s->above_ref_ctx[col]);
1798 c = 2 * !s->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
1800 } else if (s->above_intra_ctx[col]) {
1802 } else if (s->above_comp_ctx[col]) {
1803 c = 1 + (!s->fixcompref || !s->above_ref_ctx[col]);
1805 c = 4 * (!s->above_ref_ctx[col]);
1807 } else if (have_l && !s->left_intra_ctx[row7]) {
1808 if (s->left_intra_ctx[row7]) {
1810 } else if (s->left_comp_ctx[row7]) {
1811 c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7]);
1813 c = 4 * (!s->left_ref_ctx[row7]);
1818 bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][0]);
1819 s->counts.single_ref[c][0][bit]++;
1823 // FIXME can this codeblob be replaced by some sort of LUT?
1826 if (s->left_intra_ctx[row7]) {
1827 if (s->above_intra_ctx[col]) {
1829 } else if (s->above_comp_ctx[col]) {
1830 c = 1 + 2 * (s->fixcompref == 1 ||
1831 s->above_ref_ctx[col] == 1);
1832 } else if (!s->above_ref_ctx[col]) {
1835 c = 4 * (s->above_ref_ctx[col] == 1);
1837 } else if (s->above_intra_ctx[col]) {
1838 if (s->left_intra_ctx[row7]) {
1840 } else if (s->left_comp_ctx[row7]) {
1841 c = 1 + 2 * (s->fixcompref == 1 ||
1842 s->left_ref_ctx[row7] == 1);
1843 } else if (!s->left_ref_ctx[row7]) {
1846 c = 4 * (s->left_ref_ctx[row7] == 1);
1848 } else if (s->above_comp_ctx[col]) {
1849 if (s->left_comp_ctx[row7]) {
1850 if (s->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
1851 c = 3 * (s->fixcompref == 1 ||
1852 s->left_ref_ctx[row7] == 1);
1856 } else if (!s->left_ref_ctx[row7]) {
1857 c = 1 + 2 * (s->fixcompref == 1 ||
1858 s->above_ref_ctx[col] == 1);
1860 c = 3 * (s->left_ref_ctx[row7] == 1) +
1861 (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
1863 } else if (s->left_comp_ctx[row7]) {
1864 if (!s->above_ref_ctx[col]) {
1865 c = 1 + 2 * (s->fixcompref == 1 ||
1866 s->left_ref_ctx[row7] == 1);
1868 c = 3 * (s->above_ref_ctx[col] == 1) +
1869 (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
1871 } else if (!s->above_ref_ctx[col]) {
1872 if (!s->left_ref_ctx[row7]) {
1875 c = 4 * (s->left_ref_ctx[row7] == 1);
1877 } else if (!s->left_ref_ctx[row7]) {
1878 c = 4 * (s->above_ref_ctx[col] == 1);
1880 c = 2 * (s->left_ref_ctx[row7] == 1) +
1881 2 * (s->above_ref_ctx[col] == 1);
1884 if (s->above_intra_ctx[col] ||
1885 (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
1887 } else if (s->above_comp_ctx[col]) {
1888 c = 3 * (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
1890 c = 4 * (s->above_ref_ctx[col] == 1);
1893 } else if (have_l) {
1894 if (s->left_intra_ctx[row7] ||
1895 (!s->left_comp_ctx[row7] && !s->left_ref_ctx[row7])) {
1897 } else if (s->left_comp_ctx[row7]) {
1898 c = 3 * (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
1900 c = 4 * (s->left_ref_ctx[row7] == 1);
1905 bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][1]);
1906 s->counts.single_ref[c][1][bit]++;
1907 b->ref[0] = 1 + bit;
1912 if (b->bs <= BS_8x8) {
1913 if (s->segmentation.feat[b->seg_id].skip_enabled) {
1914 b->mode[0] = b->mode[1] = b->mode[2] = b->mode[3] = ZEROMV;
1916 static const uint8_t off[10] = {
1917 3, 0, 0, 1, 0, 0, 0, 0, 0, 0
1920 // FIXME this needs to use the LUT tables from find_ref_mvs
1921 // because not all are -1,0/0,-1
1922 int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
1923 [s->left_mode_ctx[row7 + off[b->bs]]];
1925 b->mode[0] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
1926 s->prob.p.mv_mode[c]);
1927 b->mode[1] = b->mode[2] = b->mode[3] = b->mode[0];
1928 s->counts.mv_mode[c][b->mode[0] - 10]++;
1932 if (s->filtermode == FILTER_SWITCHABLE) {
1935 if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
1936 if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
1937 c = s->above_filter_ctx[col] == s->left_filter_ctx[row7] ?
1938 s->left_filter_ctx[row7] : 3;
1940 c = s->above_filter_ctx[col];
1942 } else if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
1943 c = s->left_filter_ctx[row7];
1948 filter_id = vp8_rac_get_tree(&s->c, vp9_filter_tree,
1949 s->prob.p.filter[c]);
1950 s->counts.filter[c][filter_id]++;
1951 b->filter = vp9_filter_lut[filter_id];
1953 b->filter = s->filtermode;
1956 if (b->bs > BS_8x8) {
1957 int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][s->left_mode_ctx[row7]];
1959 b->mode[0] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
1960 s->prob.p.mv_mode[c]);
1961 s->counts.mv_mode[c][b->mode[0] - 10]++;
1962 fill_mv(s, b->mv[0], b->mode[0], 0);
1964 if (b->bs != BS_8x4) {
1965 b->mode[1] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
1966 s->prob.p.mv_mode[c]);
1967 s->counts.mv_mode[c][b->mode[1] - 10]++;
1968 fill_mv(s, b->mv[1], b->mode[1], 1);
1970 b->mode[1] = b->mode[0];
1971 AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
1972 AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
1975 if (b->bs != BS_4x8) {
1976 b->mode[2] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
1977 s->prob.p.mv_mode[c]);
1978 s->counts.mv_mode[c][b->mode[2] - 10]++;
1979 fill_mv(s, b->mv[2], b->mode[2], 2);
1981 if (b->bs != BS_8x4) {
1982 b->mode[3] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
1983 s->prob.p.mv_mode[c]);
1984 s->counts.mv_mode[c][b->mode[3] - 10]++;
1985 fill_mv(s, b->mv[3], b->mode[3], 3);
1987 b->mode[3] = b->mode[2];
1988 AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
1989 AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
1992 b->mode[2] = b->mode[0];
1993 AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
1994 AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
1995 b->mode[3] = b->mode[1];
1996 AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
1997 AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
2000 fill_mv(s, b->mv[0], b->mode[0], -1);
2001 AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
2002 AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
2003 AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
2004 AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
2005 AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
2006 AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
2009 vref = b->ref[b->comp ? s->signbias[s->varcompref[0]] : 0];
2013 #define SPLAT_CTX(var, val, n) \
2015 case 1: var = val; break; \
2016 case 2: AV_WN16A(&var, val * 0x0101); break; \
2017 case 4: AV_WN32A(&var, val * 0x01010101); break; \
2018 case 8: AV_WN64A(&var, val * 0x0101010101010101ULL); break; \
2020 uint64_t v64 = val * 0x0101010101010101ULL; \
2021 AV_WN64A( &var, v64); \
2022 AV_WN64A(&((uint8_t *) &var)[8], v64); \
2027 #define SPLAT_CTX(var, val, n) \
2029 case 1: var = val; break; \
2030 case 2: AV_WN16A(&var, val * 0x0101); break; \
2031 case 4: AV_WN32A(&var, val * 0x01010101); break; \
2033 uint32_t v32 = val * 0x01010101; \
2034 AV_WN32A( &var, v32); \
2035 AV_WN32A(&((uint8_t *) &var)[4], v32); \
2039 uint32_t v32 = val * 0x01010101; \
2040 AV_WN32A( &var, v32); \
2041 AV_WN32A(&((uint8_t *) &var)[4], v32); \
2042 AV_WN32A(&((uint8_t *) &var)[8], v32); \
2043 AV_WN32A(&((uint8_t *) &var)[12], v32); \
2049 switch (bwh_tab[1][b->bs][0]) {
2050 #define SET_CTXS(dir, off, n) \
2052 SPLAT_CTX(s->dir##_skip_ctx[off], b->skip, n); \
2053 SPLAT_CTX(s->dir##_txfm_ctx[off], b->tx, n); \
2054 SPLAT_CTX(s->dir##_partition_ctx[off], dir##_ctx[b->bs], n); \
2055 if (!s->keyframe && !s->intraonly) { \
2056 SPLAT_CTX(s->dir##_intra_ctx[off], b->intra, n); \
2057 SPLAT_CTX(s->dir##_comp_ctx[off], b->comp, n); \
2058 SPLAT_CTX(s->dir##_mode_ctx[off], b->mode[3], n); \
2060 SPLAT_CTX(s->dir##_ref_ctx[off], vref, n); \
2061 if (s->filtermode == FILTER_SWITCHABLE) { \
2062 SPLAT_CTX(s->dir##_filter_ctx[off], filter_id, n); \
2067 case 1: SET_CTXS(above, col, 1); break;
2068 case 2: SET_CTXS(above, col, 2); break;
2069 case 4: SET_CTXS(above, col, 4); break;
2070 case 8: SET_CTXS(above, col, 8); break;
2072 switch (bwh_tab[1][b->bs][1]) {
2073 case 1: SET_CTXS(left, row7, 1); break;
2074 case 2: SET_CTXS(left, row7, 2); break;
2075 case 4: SET_CTXS(left, row7, 4); break;
2076 case 8: SET_CTXS(left, row7, 8); break;
2081 if (!s->keyframe && !s->intraonly) {
2082 if (b->bs > BS_8x8) {
2083 int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
2085 AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
2086 AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
2087 AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][0], mv0);
2088 AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][1], mv1);
2089 AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
2090 AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
2091 AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
2092 AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
2094 int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
2096 for (n = 0; n < w4 * 2; n++) {
2097 AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
2098 AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
2100 for (n = 0; n < h4 * 2; n++) {
2101 AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][0], mv0);
2102 AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][1], mv1);
2108 for (y = 0; y < h4; y++) {
2109 int x, o = (row + y) * s->sb_cols * 8 + col;
2110 struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[o];
2113 for (x = 0; x < w4; x++) {
2117 } else if (b->comp) {
2118 for (x = 0; x < w4; x++) {
2119 mv[x].ref[0] = b->ref[0];
2120 mv[x].ref[1] = b->ref[1];
2121 AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
2122 AV_COPY32(&mv[x].mv[1], &b->mv[3][1]);
2125 for (x = 0; x < w4; x++) {
2126 mv[x].ref[0] = b->ref[0];
2128 AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
2134 // FIXME merge cnt/eob arguments?
2135 static av_always_inline int
2136 decode_coeffs_b_generic(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
2137 int is_tx32x32, int is8bitsperpixel, int bpp, unsigned (*cnt)[6][3],
2138 unsigned (*eob)[6][2], uint8_t (*p)[6][11],
2139 int nnz, const int16_t *scan, const int16_t (*nb)[2],
2140 const int16_t *band_counts, const int16_t *qmul)
2142 int i = 0, band = 0, band_left = band_counts[band];
2143 uint8_t *tp = p[0][nnz];
2144 uint8_t cache[1024];
2149 val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
2150 eob[band][nnz][val]++;
2155 if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
2156 cnt[band][nnz][0]++;
2158 band_left = band_counts[++band];
2160 nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
2162 if (++i == n_coeffs)
2163 break; //invalid input; blocks should end with EOB
2168 if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
2169 cnt[band][nnz][1]++;
2173 // fill in p[3-10] (model fill) - only once per frame for each pos
2175 memcpy(&tp[3], vp9_model_pareto8[tp[2]], 8);
2177 cnt[band][nnz][2]++;
2178 if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
2179 if (!vp56_rac_get_prob_branchy(c, tp[4])) {
2180 cache[rc] = val = 2;
2182 val = 3 + vp56_rac_get_prob(c, tp[5]);
2185 } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
2187 if (!vp56_rac_get_prob_branchy(c, tp[7])) {
2188 val = 5 + vp56_rac_get_prob(c, 159);
2190 val = 7 + (vp56_rac_get_prob(c, 165) << 1);
2191 val += vp56_rac_get_prob(c, 145);
2195 if (!vp56_rac_get_prob_branchy(c, tp[8])) {
2196 if (!vp56_rac_get_prob_branchy(c, tp[9])) {
2197 val = 11 + (vp56_rac_get_prob(c, 173) << 2);
2198 val += (vp56_rac_get_prob(c, 148) << 1);
2199 val += vp56_rac_get_prob(c, 140);
2201 val = 19 + (vp56_rac_get_prob(c, 176) << 3);
2202 val += (vp56_rac_get_prob(c, 155) << 2);
2203 val += (vp56_rac_get_prob(c, 140) << 1);
2204 val += vp56_rac_get_prob(c, 135);
2206 } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
2207 val = 35 + (vp56_rac_get_prob(c, 180) << 4);
2208 val += (vp56_rac_get_prob(c, 157) << 3);
2209 val += (vp56_rac_get_prob(c, 141) << 2);
2210 val += (vp56_rac_get_prob(c, 134) << 1);
2211 val += vp56_rac_get_prob(c, 130);
2214 if (!is8bitsperpixel) {
2216 val += vp56_rac_get_prob(c, 255) << 17;
2217 val += vp56_rac_get_prob(c, 255) << 16;
2219 val += (vp56_rac_get_prob(c, 255) << 15);
2220 val += (vp56_rac_get_prob(c, 255) << 14);
2222 val += (vp56_rac_get_prob(c, 254) << 13);
2223 val += (vp56_rac_get_prob(c, 254) << 12);
2224 val += (vp56_rac_get_prob(c, 254) << 11);
2225 val += (vp56_rac_get_prob(c, 252) << 10);
2226 val += (vp56_rac_get_prob(c, 249) << 9);
2227 val += (vp56_rac_get_prob(c, 243) << 8);
2228 val += (vp56_rac_get_prob(c, 230) << 7);
2229 val += (vp56_rac_get_prob(c, 196) << 6);
2230 val += (vp56_rac_get_prob(c, 177) << 5);
2231 val += (vp56_rac_get_prob(c, 153) << 4);
2232 val += (vp56_rac_get_prob(c, 140) << 3);
2233 val += (vp56_rac_get_prob(c, 133) << 2);
2234 val += (vp56_rac_get_prob(c, 130) << 1);
2235 val += vp56_rac_get_prob(c, 129);
2239 #define STORE_COEF(c, i, v) do { \
2240 if (is8bitsperpixel) { \
2243 AV_WN32A(&c[i * 2], v); \
2247 band_left = band_counts[++band];
2249 STORE_COEF(coef, rc, ((vp8_rac_get(c) ? -val : val) * qmul[!!i]) / 2);
2251 STORE_COEF(coef, rc, (vp8_rac_get(c) ? -val : val) * qmul[!!i]);
2252 nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
2254 } while (++i < n_coeffs);
2259 static int decode_coeffs_b_8bpp(VP9Context *s, int16_t *coef, int n_coeffs,
2260 unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
2261 uint8_t (*p)[6][11], int nnz, const int16_t *scan,
2262 const int16_t (*nb)[2], const int16_t *band_counts,
2263 const int16_t *qmul)
2265 return decode_coeffs_b_generic(&s->c, coef, n_coeffs, 0, 1, 8, cnt, eob, p,
2266 nnz, scan, nb, band_counts, qmul);
2269 static int decode_coeffs_b32_8bpp(VP9Context *s, int16_t *coef, int n_coeffs,
2270 unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
2271 uint8_t (*p)[6][11], int nnz, const int16_t *scan,
2272 const int16_t (*nb)[2], const int16_t *band_counts,
2273 const int16_t *qmul)
2275 return decode_coeffs_b_generic(&s->c, coef, n_coeffs, 1, 1, 8, cnt, eob, p,
2276 nnz, scan, nb, band_counts, qmul);
2279 static int decode_coeffs_b_16bpp(VP9Context *s, int16_t *coef, int n_coeffs,
2280 unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
2281 uint8_t (*p)[6][11], int nnz, const int16_t *scan,
2282 const int16_t (*nb)[2], const int16_t *band_counts,
2283 const int16_t *qmul)
2285 return decode_coeffs_b_generic(&s->c, coef, n_coeffs, 0, 0, s->bpp, cnt, eob, p,
2286 nnz, scan, nb, band_counts, qmul);
2289 static int decode_coeffs_b32_16bpp(VP9Context *s, int16_t *coef, int n_coeffs,
2290 unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
2291 uint8_t (*p)[6][11], int nnz, const int16_t *scan,
2292 const int16_t (*nb)[2], const int16_t *band_counts,
2293 const int16_t *qmul)
2295 return decode_coeffs_b_generic(&s->c, coef, n_coeffs, 1, 0, s->bpp, cnt, eob, p,
2296 nnz, scan, nb, band_counts, qmul);
2299 static av_always_inline void decode_coeffs(AVCodecContext *ctx, int is8bitsperpixel)
2301 VP9Context *s = ctx->priv_data;
2303 int row = s->row, col = s->col;
2304 uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
2305 unsigned (*c)[6][3] = s->counts.coef[b->tx][0 /* y */][!b->intra];
2306 unsigned (*e)[6][2] = s->counts.eob[b->tx][0 /* y */][!b->intra];
2307 int w4 = bwh_tab[1][b->bs][0] << 1, h4 = bwh_tab[1][b->bs][1] << 1;
2308 int end_x = FFMIN(2 * (s->cols - col), w4);
2309 int end_y = FFMIN(2 * (s->rows - row), h4);
2310 int n, pl, x, y, res;
2311 int16_t (*qmul)[2] = s->segmentation.feat[b->seg_id].qmul;
2312 int tx = 4 * s->lossless + b->tx;
2313 const int16_t * const *yscans = vp9_scans[tx];
2314 const int16_t (* const *ynbs)[2] = vp9_scans_nb[tx];
2315 const int16_t *uvscan = vp9_scans[b->uvtx][DCT_DCT];
2316 const int16_t (*uvnb)[2] = vp9_scans_nb[b->uvtx][DCT_DCT];
2317 uint8_t *a = &s->above_y_nnz_ctx[col * 2];
2318 uint8_t *l = &s->left_y_nnz_ctx[(row & 7) << 1];
2319 static const int16_t band_counts[4][8] = {
2320 { 1, 2, 3, 4, 3, 16 - 13 },
2321 { 1, 2, 3, 4, 11, 64 - 21 },
2322 { 1, 2, 3, 4, 11, 256 - 21 },
2323 { 1, 2, 3, 4, 11, 1024 - 21 },
2325 const int16_t *y_band_counts = band_counts[b->tx];
2326 const int16_t *uv_band_counts = band_counts[b->uvtx];
2327 int bytesperpixel = is8bitsperpixel ? 1 : 2;
2329 #define MERGE(la, end, step, rd) \
2330 for (n = 0; n < end; n += step) \
2331 la[n] = !!rd(&la[n])
2332 #define MERGE_CTX(step, rd) \
2334 MERGE(l, end_y, step, rd); \
2335 MERGE(a, end_x, step, rd); \
2338 #define DECODE_Y_COEF_LOOP(step, mode_index, v) \
2339 for (n = 0, y = 0; y < end_y; y += step) { \
2340 for (x = 0; x < end_x; x += step, n += step * step) { \
2341 enum TxfmType txtp = vp9_intra_txfm_type[b->mode[mode_index]]; \
2342 res = (is8bitsperpixel ? decode_coeffs_b##v##_8bpp : decode_coeffs_b##v##_16bpp) \
2343 (s, s->block + 16 * n * bytesperpixel, 16 * step * step, \
2344 c, e, p, a[x] + l[y], yscans[txtp], \
2345 ynbs[txtp], y_band_counts, qmul[0]); \
2346 a[x] = l[y] = !!res; \
2348 AV_WN16A(&s->eob[n], res); \
2355 #define SPLAT(la, end, step, cond) \
2357 for (n = 1; n < end; n += step) \
2358 la[n] = la[n - 1]; \
2359 } else if (step == 4) { \
2361 for (n = 0; n < end; n += step) \
2362 AV_WN32A(&la[n], la[n] * 0x01010101); \
2364 for (n = 0; n < end; n += step) \
2365 memset(&la[n + 1], la[n], FFMIN(end - n - 1, 3)); \
2367 } else /* step == 8 */ { \
2369 if (HAVE_FAST_64BIT) { \
2370 for (n = 0; n < end; n += step) \
2371 AV_WN64A(&la[n], la[n] * 0x0101010101010101ULL); \
2373 for (n = 0; n < end; n += step) { \
2374 uint32_t v32 = la[n] * 0x01010101; \
2375 AV_WN32A(&la[n], v32); \
2376 AV_WN32A(&la[n + 4], v32); \
2380 for (n = 0; n < end; n += step) \
2381 memset(&la[n + 1], la[n], FFMIN(end - n - 1, 7)); \
2384 #define SPLAT_CTX(step) \
2386 SPLAT(a, end_x, step, end_x == w4); \
2387 SPLAT(l, end_y, step, end_y == h4); \
2393 DECODE_Y_COEF_LOOP(1, b->bs > BS_8x8 ? n : 0,);
2396 MERGE_CTX(2, AV_RN16A);
2397 DECODE_Y_COEF_LOOP(2, 0,);
2401 MERGE_CTX(4, AV_RN32A);
2402 DECODE_Y_COEF_LOOP(4, 0,);
2406 MERGE_CTX(8, AV_RN64A);
2407 DECODE_Y_COEF_LOOP(8, 0, 32);
2412 #define DECODE_UV_COEF_LOOP(step, v) \
2413 for (n = 0, y = 0; y < end_y; y += step) { \
2414 for (x = 0; x < end_x; x += step, n += step * step) { \
2415 res = (is8bitsperpixel ? decode_coeffs_b##v##_8bpp : decode_coeffs_b##v##_16bpp) \
2416 (s, s->uvblock[pl] + 16 * n * bytesperpixel, \
2417 16 * step * step, c, e, p, a[x] + l[y], \
2418 uvscan, uvnb, uv_band_counts, qmul[1]); \
2419 a[x] = l[y] = !!res; \
2421 AV_WN16A(&s->uveob[pl][n], res); \
2423 s->uveob[pl][n] = res; \
2428 p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
2429 c = s->counts.coef[b->uvtx][1 /* uv */][!b->intra];
2430 e = s->counts.eob[b->uvtx][1 /* uv */][!b->intra];
2435 for (pl = 0; pl < 2; pl++) {
2436 a = &s->above_uv_nnz_ctx[pl][col << !s->ss_h];
2437 l = &s->left_uv_nnz_ctx[pl][(row & 7) << !s->ss_v];
2440 DECODE_UV_COEF_LOOP(1,);
2443 MERGE_CTX(2, AV_RN16A);
2444 DECODE_UV_COEF_LOOP(2,);
2448 MERGE_CTX(4, AV_RN32A);
2449 DECODE_UV_COEF_LOOP(4,);
2453 MERGE_CTX(8, AV_RN64A);
2454 DECODE_UV_COEF_LOOP(8, 32);
2461 static void decode_coeffs_8bpp(AVCodecContext *ctx)
2463 decode_coeffs(ctx, 1);
2466 static void decode_coeffs_16bpp(AVCodecContext *ctx)
2468 decode_coeffs(ctx, 0);
2471 static av_always_inline int check_intra_mode(VP9Context *s, int mode, uint8_t **a,
2472 uint8_t *dst_edge, ptrdiff_t stride_edge,
2473 uint8_t *dst_inner, ptrdiff_t stride_inner,
2474 uint8_t *l, int col, int x, int w,
2475 int row, int y, enum TxfmMode tx,
2476 int p, int ss_h, int ss_v, int bytesperpixel)
2478 int have_top = row > 0 || y > 0;
2479 int have_left = col > s->tiling.tile_col_start || x > 0;
2480 int have_right = x < w - 1;
2482 static const uint8_t mode_conv[10][2 /* have_left */][2 /* have_top */] = {
2483 [VERT_PRED] = { { DC_127_PRED, VERT_PRED },
2484 { DC_127_PRED, VERT_PRED } },
2485 [HOR_PRED] = { { DC_129_PRED, DC_129_PRED },
2486 { HOR_PRED, HOR_PRED } },
2487 [DC_PRED] = { { DC_128_PRED, TOP_DC_PRED },
2488 { LEFT_DC_PRED, DC_PRED } },
2489 [DIAG_DOWN_LEFT_PRED] = { { DC_127_PRED, DIAG_DOWN_LEFT_PRED },
2490 { DC_127_PRED, DIAG_DOWN_LEFT_PRED } },
2491 [DIAG_DOWN_RIGHT_PRED] = { { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED },
2492 { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED } },
2493 [VERT_RIGHT_PRED] = { { VERT_RIGHT_PRED, VERT_RIGHT_PRED },
2494 { VERT_RIGHT_PRED, VERT_RIGHT_PRED } },
2495 [HOR_DOWN_PRED] = { { HOR_DOWN_PRED, HOR_DOWN_PRED },
2496 { HOR_DOWN_PRED, HOR_DOWN_PRED } },
2497 [VERT_LEFT_PRED] = { { DC_127_PRED, VERT_LEFT_PRED },
2498 { DC_127_PRED, VERT_LEFT_PRED } },
2499 [HOR_UP_PRED] = { { DC_129_PRED, DC_129_PRED },
2500 { HOR_UP_PRED, HOR_UP_PRED } },
2501 [TM_VP8_PRED] = { { DC_129_PRED, VERT_PRED },
2502 { HOR_PRED, TM_VP8_PRED } },
2504 static const struct {
2505 uint8_t needs_left:1;
2506 uint8_t needs_top:1;
2507 uint8_t needs_topleft:1;
2508 uint8_t needs_topright:1;
2509 uint8_t invert_left:1;
2510 } edges[N_INTRA_PRED_MODES] = {
2511 [VERT_PRED] = { .needs_top = 1 },
2512 [HOR_PRED] = { .needs_left = 1 },
2513 [DC_PRED] = { .needs_top = 1, .needs_left = 1 },
2514 [DIAG_DOWN_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
2515 [DIAG_DOWN_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
2516 [VERT_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
2517 [HOR_DOWN_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
2518 [VERT_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
2519 [HOR_UP_PRED] = { .needs_left = 1, .invert_left = 1 },
2520 [TM_VP8_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
2521 [LEFT_DC_PRED] = { .needs_left = 1 },
2522 [TOP_DC_PRED] = { .needs_top = 1 },
2523 [DC_128_PRED] = { 0 },
2524 [DC_127_PRED] = { 0 },
2525 [DC_129_PRED] = { 0 }
2528 av_assert2(mode >= 0 && mode < 10);
2529 mode = mode_conv[mode][have_left][have_top];
2530 if (edges[mode].needs_top) {
2531 uint8_t *top, *topleft;
2532 int n_px_need = 4 << tx, n_px_have = (((s->cols - col) << !ss_h) - x) * 4;
2533 int n_px_need_tr = 0;
2535 if (tx == TX_4X4 && edges[mode].needs_topright && have_right)
2538 // if top of sb64-row, use s->intra_pred_data[] instead of
2539 // dst[-stride] for intra prediction (it contains pre- instead of
2540 // post-loopfilter data)
2542 top = !(row & 7) && !y ?
2543 s->intra_pred_data[p] + (col * (8 >> ss_h) + x * 4) * bytesperpixel :
2544 y == 0 ? &dst_edge[-stride_edge] : &dst_inner[-stride_inner];
2546 topleft = !(row & 7) && !y ?
2547 s->intra_pred_data[p] + (col * (8 >> ss_h) + x * 4) * bytesperpixel :
2548 y == 0 || x == 0 ? &dst_edge[-stride_edge] :
2549 &dst_inner[-stride_inner];
2553 (!edges[mode].needs_topleft || (have_left && top == topleft)) &&
2554 (tx != TX_4X4 || !edges[mode].needs_topright || have_right) &&
2555 n_px_need + n_px_need_tr <= n_px_have) {
2559 if (n_px_need <= n_px_have) {
2560 memcpy(*a, top, n_px_need * bytesperpixel);
2562 #define memset_bpp(c, i1, v, i2, num) do { \
2563 if (bytesperpixel == 1) { \
2564 memset(&(c)[(i1)], (v)[(i2)], (num)); \
2566 int n, val = AV_RN16A(&(v)[(i2) * 2]); \
2567 for (n = 0; n < (num); n++) { \
2568 AV_WN16A(&(c)[((i1) + n) * 2], val); \
2572 memcpy(*a, top, n_px_have * bytesperpixel);
2573 memset_bpp(*a, n_px_have, (*a), n_px_have - 1, n_px_need - n_px_have);
2576 #define memset_val(c, val, num) do { \
2577 if (bytesperpixel == 1) { \
2578 memset((c), (val), (num)); \
2581 for (n = 0; n < (num); n++) { \
2582 AV_WN16A(&(c)[n * 2], (val)); \
2586 memset_val(*a, (128 << (bpp - 8)) - 1, n_px_need);
2588 if (edges[mode].needs_topleft) {
2589 if (have_left && have_top) {
2590 #define assign_bpp(c, i1, v, i2) do { \
2591 if (bytesperpixel == 1) { \
2592 (c)[(i1)] = (v)[(i2)]; \
2594 AV_COPY16(&(c)[(i1) * 2], &(v)[(i2) * 2]); \
2597 assign_bpp(*a, -1, topleft, -1);
2599 #define assign_val(c, i, v) do { \
2600 if (bytesperpixel == 1) { \
2603 AV_WN16A(&(c)[(i) * 2], (v)); \
2606 assign_val((*a), -1, (128 << (bpp - 8)) + (have_top ? +1 : -1));
2609 if (tx == TX_4X4 && edges[mode].needs_topright) {
2610 if (have_top && have_right &&
2611 n_px_need + n_px_need_tr <= n_px_have) {
2612 memcpy(&(*a)[4 * bytesperpixel], &top[4 * bytesperpixel], 4 * bytesperpixel);
2614 memset_bpp(*a, 4, *a, 3, 4);
2619 if (edges[mode].needs_left) {
2621 int n_px_need = 4 << tx, i, n_px_have = (((s->rows - row) << !ss_v) - y) * 4;
2622 uint8_t *dst = x == 0 ? dst_edge : dst_inner;
2623 ptrdiff_t stride = x == 0 ? stride_edge : stride_inner;
2625 if (edges[mode].invert_left) {
2626 if (n_px_need <= n_px_have) {
2627 for (i = 0; i < n_px_need; i++)
2628 assign_bpp(l, i, &dst[i * stride], -1);
2630 for (i = 0; i < n_px_have; i++)
2631 assign_bpp(l, i, &dst[i * stride], -1);
2632 memset_bpp(l, n_px_have, l, n_px_have - 1, n_px_need - n_px_have);
2635 if (n_px_need <= n_px_have) {
2636 for (i = 0; i < n_px_need; i++)
2637 assign_bpp(l, n_px_need - 1 - i, &dst[i * stride], -1);
2639 for (i = 0; i < n_px_have; i++)
2640 assign_bpp(l, n_px_need - 1 - i, &dst[i * stride], -1);
2641 memset_bpp(l, 0, l, n_px_need - n_px_have, n_px_need - n_px_have);
2645 memset_val(l, (128 << (bpp - 8)) + 1, 4 << tx);
2652 static av_always_inline void intra_recon(AVCodecContext *ctx, ptrdiff_t y_off,
2653 ptrdiff_t uv_off, int bytesperpixel)
2655 VP9Context *s = ctx->priv_data;
2657 int row = s->row, col = s->col;
2658 int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
2659 int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
2660 int end_x = FFMIN(2 * (s->cols - col), w4);
2661 int end_y = FFMIN(2 * (s->rows - row), h4);
2662 int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
2663 int uvstep1d = 1 << b->uvtx, p;
2664 uint8_t *dst = s->dst[0], *dst_r = s->frames[CUR_FRAME].tf.f->data[0] + y_off;
2665 LOCAL_ALIGNED_32(uint8_t, a_buf, [96]);
2666 LOCAL_ALIGNED_32(uint8_t, l, [64]);
2668 for (n = 0, y = 0; y < end_y; y += step1d) {
2669 uint8_t *ptr = dst, *ptr_r = dst_r;
2670 for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d * bytesperpixel,
2671 ptr_r += 4 * step1d * bytesperpixel, n += step) {
2672 int mode = b->mode[b->bs > BS_8x8 && b->tx == TX_4X4 ?
2674 uint8_t *a = &a_buf[32];
2675 enum TxfmType txtp = vp9_intra_txfm_type[mode];
2676 int eob = b->skip ? 0 : b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
2678 mode = check_intra_mode(s, mode, &a, ptr_r,
2679 s->frames[CUR_FRAME].tf.f->linesize[0],
2680 ptr, s->y_stride, l,
2681 col, x, w4, row, y, b->tx, 0, 0, 0, bytesperpixel);
2682 s->dsp.intra_pred[b->tx][mode](ptr, s->y_stride, l, a);
2684 s->dsp.itxfm_add[tx][txtp](ptr, s->y_stride,
2685 s->block + 16 * n * bytesperpixel, eob);
2687 dst_r += 4 * step1d * s->frames[CUR_FRAME].tf.f->linesize[0];
2688 dst += 4 * step1d * s->y_stride;
2695 step = 1 << (b->uvtx * 2);
2696 for (p = 0; p < 2; p++) {
2697 dst = s->dst[1 + p];
2698 dst_r = s->frames[CUR_FRAME].tf.f->data[1 + p] + uv_off;
2699 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
2700 uint8_t *ptr = dst, *ptr_r = dst_r;
2701 for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d * bytesperpixel,
2702 ptr_r += 4 * uvstep1d * bytesperpixel, n += step) {
2703 int mode = b->uvmode;
2704 uint8_t *a = &a_buf[32];
2705 int eob = b->skip ? 0 : b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n]) : s->uveob[p][n];
2707 mode = check_intra_mode(s, mode, &a, ptr_r,
2708 s->frames[CUR_FRAME].tf.f->linesize[1],
2709 ptr, s->uv_stride, l, col, x, w4, row, y,
2710 b->uvtx, p + 1, s->ss_h, s->ss_v, bytesperpixel);
2711 s->dsp.intra_pred[b->uvtx][mode](ptr, s->uv_stride, l, a);
2713 s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, s->uv_stride,
2714 s->uvblock[p] + 16 * n * bytesperpixel, eob);
2716 dst_r += 4 * uvstep1d * s->frames[CUR_FRAME].tf.f->linesize[1];
2717 dst += 4 * uvstep1d * s->uv_stride;
2722 static void intra_recon_8bpp(AVCodecContext *ctx, ptrdiff_t y_off, ptrdiff_t uv_off)
2724 intra_recon(ctx, y_off, uv_off, 1);
2727 static void intra_recon_16bpp(AVCodecContext *ctx, ptrdiff_t y_off, ptrdiff_t uv_off)
2729 intra_recon(ctx, y_off, uv_off, 2);
2732 static av_always_inline void mc_luma_scaled(VP9Context *s, vp9_scaled_mc_func smc,
2733 uint8_t *dst, ptrdiff_t dst_stride,
2734 const uint8_t *ref, ptrdiff_t ref_stride,
2735 ThreadFrame *ref_frame,
2736 ptrdiff_t y, ptrdiff_t x, const VP56mv *mv,
2737 int bw, int bh, int w, int h, int bytesperpixel,
2738 const uint16_t *scale, const uint8_t *step)
2740 #define scale_mv(n, dim) (((int64_t)(n) * scale[dim]) >> 14)
2741 // BUG libvpx seems to scale the two components separately. This introduces
2742 // rounding errors but we have to reproduce them to be exactly compatible
2743 // with the output from libvpx...
2744 int mx = scale_mv(mv->x * 2, 0) + scale_mv(x * 16, 0);
2745 int my = scale_mv(mv->y * 2, 1) + scale_mv(y * 16, 1);
2746 int refbw_m1, refbh_m1;
2751 ref += y * ref_stride + x * bytesperpixel;
2754 refbw_m1 = ((bw - 1) * step[0] + mx) >> 4;
2755 refbh_m1 = ((bh - 1) * step[1] + my) >> 4;
2756 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
2757 // we use +7 because the last 7 pixels of each sbrow can be changed in
2758 // the longest loopfilter of the next sbrow
2759 th = (y + refbh_m1 + 4 + 7) >> 6;
2760 ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0);
2761 if (x < 3 || y < 3 || x + 4 >= w - refbw_m1 || y + 4 >= h - refbh_m1) {
2762 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
2763 ref - 3 * ref_stride - 3 * bytesperpixel,
2765 refbw_m1 + 8, refbh_m1 + 8,
2766 x - 3, y - 3, w, h);
2767 ref = s->edge_emu_buffer + 3 * 288 + 3 * bytesperpixel;
2770 smc(dst, dst_stride, ref, ref_stride, bh, mx, my, step[0], step[1]);
2773 static av_always_inline void mc_chroma_scaled(VP9Context *s, vp9_scaled_mc_func smc,
2774 uint8_t *dst_u, uint8_t *dst_v,
2775 ptrdiff_t dst_stride,
2776 const uint8_t *ref_u, ptrdiff_t src_stride_u,
2777 const uint8_t *ref_v, ptrdiff_t src_stride_v,
2778 ThreadFrame *ref_frame,
2779 ptrdiff_t y, ptrdiff_t x, const VP56mv *mv,
2780 int bw, int bh, int w, int h, int bytesperpixel,
2781 const uint16_t *scale, const uint8_t *step)
2783 // BUG https://code.google.com/p/webm/issues/detail?id=820
2784 int mx = scale_mv(mv->x << !s->ss_h, 0) + (scale_mv(x * 16, 0) & ~15) + (scale_mv(x * 32, 0) & 15);
2785 int my = scale_mv(mv->y << !s->ss_v, 1) + (scale_mv(y * 16, 1) & ~15) + (scale_mv(y * 32, 1) & 15);
2787 int refbw_m1, refbh_m1;
2792 ref_u += y * src_stride_u + x * bytesperpixel;
2793 ref_v += y * src_stride_v + x * bytesperpixel;
2796 refbw_m1 = ((bw - 1) * step[0] + mx) >> 4;
2797 refbh_m1 = ((bh - 1) * step[1] + my) >> 4;
2798 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
2799 // we use +7 because the last 7 pixels of each sbrow can be changed in
2800 // the longest loopfilter of the next sbrow
2801 th = (y + refbh_m1 + 4 + 7) >> (6 - s->ss_v);
2802 ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0);
2803 if (x < 3 || y < 3 || x + 4 >= w - refbw_m1 || y + 4 >= h - refbh_m1) {
2804 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
2805 ref_u - 3 * src_stride_u - 3 * bytesperpixel,
2807 refbw_m1 + 8, refbh_m1 + 8,
2808 x - 3, y - 3, w, h);
2809 ref_u = s->edge_emu_buffer + 3 * 288 + 3 * bytesperpixel;
2810 smc(dst_u, dst_stride, ref_u, 288, bh, mx, my, step[0], step[1]);
2812 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
2813 ref_v - 3 * src_stride_v - 3 * bytesperpixel,
2815 refbw_m1 + 8, refbh_m1 + 8,
2816 x - 3, y - 3, w, h);
2817 ref_v = s->edge_emu_buffer + 3 * 288 + 3 * bytesperpixel;
2818 smc(dst_v, dst_stride, ref_v, 288, bh, mx, my, step[0], step[1]);
2820 smc(dst_u, dst_stride, ref_u, src_stride_u, bh, mx, my, step[0], step[1]);
2821 smc(dst_v, dst_stride, ref_v, src_stride_v, bh, mx, my, step[0], step[1]);
2825 #define mc_luma_dir(s, mc, dst, dst_ls, src, src_ls, tref, row, col, mv, bw, bh, w, h, i) \
2826 mc_luma_scaled(s, s->dsp.s##mc, dst, dst_ls, src, src_ls, tref, row, col, \
2827 mv, bw, bh, w, h, bytesperpixel, \
2828 s->mvscale[b->ref[i]], s->mvstep[b->ref[i]])
2829 #define mc_chroma_dir(s, mc, dstu, dstv, dst_ls, srcu, srcu_ls, srcv, srcv_ls, tref, \
2830 row, col, mv, bw, bh, w, h, i) \
2831 mc_chroma_scaled(s, s->dsp.s##mc, dstu, dstv, dst_ls, srcu, srcu_ls, srcv, srcv_ls, tref, \
2832 row, col, mv, bw, bh, w, h, bytesperpixel, \
2833 s->mvscale[b->ref[i]], s->mvstep[b->ref[i]])
2834 #define FN(x) x##_scaled_8bpp
2835 #define BYTES_PER_PIXEL 1
2836 #include "vp9_mc_template.c"
2838 #undef BYTES_PER_PIXEL
2839 #define FN(x) x##_scaled_16bpp
2840 #define BYTES_PER_PIXEL 2
2841 #include "vp9_mc_template.c"
2843 #undef mc_chroma_dir
2845 #undef BYTES_PER_PIXEL
2847 static av_always_inline void mc_luma_unscaled(VP9Context *s, vp9_mc_func (*mc)[2],
2848 uint8_t *dst, ptrdiff_t dst_stride,
2849 const uint8_t *ref, ptrdiff_t ref_stride,
2850 ThreadFrame *ref_frame,
2851 ptrdiff_t y, ptrdiff_t x, const VP56mv *mv,
2852 int bw, int bh, int w, int h, int bytesperpixel)
2854 int mx = mv->x, my = mv->y, th;
2858 ref += y * ref_stride + x * bytesperpixel;
2861 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
2862 // we use +7 because the last 7 pixels of each sbrow can be changed in
2863 // the longest loopfilter of the next sbrow
2864 th = (y + bh + 4 * !!my + 7) >> 6;
2865 ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0);
2866 if (x < !!mx * 3 || y < !!my * 3 ||
2867 x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
2868 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
2869 ref - !!my * 3 * ref_stride - !!mx * 3 * bytesperpixel,
2871 bw + !!mx * 7, bh + !!my * 7,
2872 x - !!mx * 3, y - !!my * 3, w, h);
2873 ref = s->edge_emu_buffer + !!my * 3 * 160 + !!mx * 3 * bytesperpixel;
2876 mc[!!mx][!!my](dst, dst_stride, ref, ref_stride, bh, mx << 1, my << 1);
2879 static av_always_inline void mc_chroma_unscaled(VP9Context *s, vp9_mc_func (*mc)[2],
2880 uint8_t *dst_u, uint8_t *dst_v,
2881 ptrdiff_t dst_stride,
2882 const uint8_t *ref_u, ptrdiff_t src_stride_u,
2883 const uint8_t *ref_v, ptrdiff_t src_stride_v,
2884 ThreadFrame *ref_frame,
2885 ptrdiff_t y, ptrdiff_t x, const VP56mv *mv,
2886 int bw, int bh, int w, int h, int bytesperpixel)
2888 int mx = mv->x << !s->ss_h, my = mv->y << !s->ss_v, th;
2892 ref_u += y * src_stride_u + x * bytesperpixel;
2893 ref_v += y * src_stride_v + x * bytesperpixel;
2896 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
2897 // we use +7 because the last 7 pixels of each sbrow can be changed in
2898 // the longest loopfilter of the next sbrow
2899 th = (y + bh + 4 * !!my + 7) >> (6 - s->ss_v);
2900 ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0);
2901 if (x < !!mx * 3 || y < !!my * 3 ||
2902 x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
2903 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
2904 ref_u - !!my * 3 * src_stride_u - !!mx * 3 * bytesperpixel,
2906 bw + !!mx * 7, bh + !!my * 7,
2907 x - !!mx * 3, y - !!my * 3, w, h);
2908 ref_u = s->edge_emu_buffer + !!my * 3 * 160 + !!mx * 3 * bytesperpixel;
2909 mc[!!mx][!!my](dst_u, dst_stride, ref_u, 160, bh, mx, my);
2911 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
2912 ref_v - !!my * 3 * src_stride_v - !!mx * 3 * bytesperpixel,
2914 bw + !!mx * 7, bh + !!my * 7,
2915 x - !!mx * 3, y - !!my * 3, w, h);
2916 ref_v = s->edge_emu_buffer + !!my * 3 * 160 + !!mx * 3 * bytesperpixel;
2917 mc[!!mx][!!my](dst_v, dst_stride, ref_v, 160, bh, mx, my);
2919 mc[!!mx][!!my](dst_u, dst_stride, ref_u, src_stride_u, bh, mx, my);
2920 mc[!!mx][!!my](dst_v, dst_stride, ref_v, src_stride_v, bh, mx, my);
2924 #define mc_luma_dir(s, mc, dst, dst_ls, src, src_ls, tref, row, col, mv, bw, bh, w, h, i) \
2925 mc_luma_unscaled(s, s->dsp.mc, dst, dst_ls, src, src_ls, tref, row, col, \
2926 mv, bw, bh, w, h, bytesperpixel)
2927 #define mc_chroma_dir(s, mc, dstu, dstv, dst_ls, srcu, srcu_ls, srcv, srcv_ls, tref, \
2928 row, col, mv, bw, bh, w, h, i) \
2929 mc_chroma_unscaled(s, s->dsp.mc, dstu, dstv, dst_ls, srcu, srcu_ls, srcv, srcv_ls, tref, \
2930 row, col, mv, bw, bh, w, h, bytesperpixel)
2931 #define FN(x) x##_8bpp
2932 #define BYTES_PER_PIXEL 1
2933 #include "vp9_mc_template.c"
2935 #undef BYTES_PER_PIXEL
2936 #define FN(x) x##_16bpp
2937 #define BYTES_PER_PIXEL 2
2938 #include "vp9_mc_template.c"
2939 #undef mc_luma_dir_dir
2940 #undef mc_chroma_dir_dir
2942 #undef BYTES_PER_PIXEL
2944 static av_always_inline void inter_recon(AVCodecContext *ctx, int bytesperpixel)
2946 VP9Context *s = ctx->priv_data;
2948 int row = s->row, col = s->col;
2950 if (s->mvscale[b->ref[0]][0] || (b->comp && s->mvscale[b->ref[1]][0])) {
2951 if (bytesperpixel == 1) {
2952 inter_pred_scaled_8bpp(ctx);
2954 inter_pred_scaled_16bpp(ctx);
2957 if (bytesperpixel == 1) {
2958 inter_pred_8bpp(ctx);
2960 inter_pred_16bpp(ctx);
2964 /* mostly copied intra_recon() */
2966 int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
2967 int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
2968 int end_x = FFMIN(2 * (s->cols - col), w4);
2969 int end_y = FFMIN(2 * (s->rows - row), h4);
2970 int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
2971 int uvstep1d = 1 << b->uvtx, p;
2972 uint8_t *dst = s->dst[0];
2975 for (n = 0, y = 0; y < end_y; y += step1d) {
2977 for (x = 0; x < end_x; x += step1d,
2978 ptr += 4 * step1d * bytesperpixel, n += step) {
2979 int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
2982 s->dsp.itxfm_add[tx][DCT_DCT](ptr, s->y_stride,
2983 s->block + 16 * n * bytesperpixel, eob);
2985 dst += 4 * s->y_stride * step1d;
2991 step = 1 << (b->uvtx * 2);
2992 for (p = 0; p < 2; p++) {
2993 dst = s->dst[p + 1];
2994 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
2996 for (x = 0; x < end_x; x += uvstep1d,
2997 ptr += 4 * uvstep1d * bytesperpixel, n += step) {
2998 int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n]) : s->uveob[p][n];
3001 s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, s->uv_stride,
3002 s->uvblock[p] + 16 * n * bytesperpixel, eob);
3004 dst += 4 * uvstep1d * s->uv_stride;
3010 static void inter_recon_8bpp(AVCodecContext *ctx)
3012 inter_recon(ctx, 1);
3015 static void inter_recon_16bpp(AVCodecContext *ctx)
3017 inter_recon(ctx, 2);
3020 static av_always_inline void mask_edges(uint8_t (*mask)[8][4], int ss_h, int ss_v,
3021 int row_and_7, int col_and_7,
3022 int w, int h, int col_end, int row_end,
3023 enum TxfmMode tx, int skip_inter)
3025 static const unsigned wide_filter_col_mask[2] = { 0x11, 0x01 };
3026 static const unsigned wide_filter_row_mask[2] = { 0x03, 0x07 };
3028 // FIXME I'm pretty sure all loops can be replaced by a single LUT if
3029 // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
3030 // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
3031 // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
3033 // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
3034 // edges. This means that for UV, we work on two subsampled blocks at
3035 // a time, and we only use the topleft block's mode information to set
3036 // things like block strength. Thus, for any block size smaller than
3037 // 16x16, ignore the odd portion of the block.
3038 if (tx == TX_4X4 && (ss_v | ss_h)) {
3053 if (tx == TX_4X4 && !skip_inter) {
3054 int t = 1 << col_and_7, m_col = (t << w) - t, y;
3055 // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
3056 int m_row_8 = m_col & wide_filter_col_mask[ss_h], m_row_4 = m_col - m_row_8;
3058 for (y = row_and_7; y < h + row_and_7; y++) {
3059 int col_mask_id = 2 - !(y & wide_filter_row_mask[ss_v]);
3061 mask[0][y][1] |= m_row_8;
3062 mask[0][y][2] |= m_row_4;
3063 // for odd lines, if the odd col is not being filtered,
3064 // skip odd row also:
3071 // if a/c are even row/col and b/d are odd, and d is skipped,
3072 // e.g. right edge of size-66x66.webm, then skip b also (bug)
3073 if ((ss_h & ss_v) && (col_end & 1) && (y & 1)) {
3074 mask[1][y][col_mask_id] |= (t << (w - 1)) - t;
3076 mask[1][y][col_mask_id] |= m_col;
3079 mask[0][y][3] |= m_col;
3081 mask[1][y][3] |= m_col;
3084 int y, t = 1 << col_and_7, m_col = (t << w) - t;
3087 int mask_id = (tx == TX_8X8);
3088 static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
3089 int l2 = tx + ss_h - 1, step1d;
3090 int m_row = m_col & masks[l2];
3092 // at odd UV col/row edges tx16/tx32 loopfilter edges, force
3093 // 8wd loopfilter to prevent going off the visible edge.
3094 if (ss_h && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
3095 int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
3096 int m_row_8 = m_row - m_row_16;
3098 for (y = row_and_7; y < h + row_and_7; y++) {
3099 mask[0][y][0] |= m_row_16;
3100 mask[0][y][1] |= m_row_8;
3103 for (y = row_and_7; y < h + row_and_7; y++)
3104 mask[0][y][mask_id] |= m_row;
3109 if (ss_v && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
3110 for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
3111 mask[1][y][0] |= m_col;
3112 if (y - row_and_7 == h - 1)
3113 mask[1][y][1] |= m_col;
3115 for (y = row_and_7; y < h + row_and_7; y += step1d)
3116 mask[1][y][mask_id] |= m_col;
3118 } else if (tx != TX_4X4) {
3121 mask_id = (tx == TX_8X8) || (h == ss_v);
3122 mask[1][row_and_7][mask_id] |= m_col;
3123 mask_id = (tx == TX_8X8) || (w == ss_h);
3124 for (y = row_and_7; y < h + row_and_7; y++)
3125 mask[0][y][mask_id] |= t;
3127 int t8 = t & wide_filter_col_mask[ss_h], t4 = t - t8;
3129 for (y = row_and_7; y < h + row_and_7; y++) {
3130 mask[0][y][2] |= t4;
3131 mask[0][y][1] |= t8;
3133 mask[1][row_and_7][2 - !(row_and_7 & wide_filter_row_mask[ss_v])] |= m_col;
3138 static void decode_b(AVCodecContext *ctx, int row, int col,
3139 struct VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
3140 enum BlockLevel bl, enum BlockPartition bp)
3142 VP9Context *s = ctx->priv_data;
3144 enum BlockSize bs = bl * 3 + bp;
3145 int bytesperpixel = s->bytesperpixel;
3146 int w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl;
3148 AVFrame *f = s->frames[CUR_FRAME].tf.f;
3154 s->min_mv.x = -(128 + col * 64);
3155 s->min_mv.y = -(128 + row * 64);
3156 s->max_mv.x = 128 + (s->cols - col - w4) * 64;
3157 s->max_mv.y = 128 + (s->rows - row - h4) * 64;
3163 b->uvtx = b->tx - ((s->ss_h && w4 * 2 == (1 << b->tx)) ||
3164 (s->ss_v && h4 * 2 == (1 << b->tx)));
3167 if (bytesperpixel == 1) {
3168 decode_coeffs_8bpp(ctx);
3170 decode_coeffs_16bpp(ctx);
3175 #define SPLAT_ZERO_CTX(v, n) \
3177 case 1: v = 0; break; \
3178 case 2: AV_ZERO16(&v); break; \
3179 case 4: AV_ZERO32(&v); break; \
3180 case 8: AV_ZERO64(&v); break; \
3181 case 16: AV_ZERO128(&v); break; \
3183 #define SPLAT_ZERO_YUV(dir, var, off, n, dir2) \
3185 SPLAT_ZERO_CTX(s->dir##_y_##var[off * 2], n * 2); \
3186 if (s->ss_##dir2) { \
3187 SPLAT_ZERO_CTX(s->dir##_uv_##var[0][off], n); \
3188 SPLAT_ZERO_CTX(s->dir##_uv_##var[1][off], n); \
3190 SPLAT_ZERO_CTX(s->dir##_uv_##var[0][off * 2], n * 2); \
3191 SPLAT_ZERO_CTX(s->dir##_uv_##var[1][off * 2], n * 2); \
3196 case 1: SPLAT_ZERO_YUV(above, nnz_ctx, col, 1, h); break;
3197 case 2: SPLAT_ZERO_YUV(above, nnz_ctx, col, 2, h); break;
3198 case 4: SPLAT_ZERO_YUV(above, nnz_ctx, col, 4, h); break;
3199 case 8: SPLAT_ZERO_YUV(above, nnz_ctx, col, 8, h); break;
3202 case 1: SPLAT_ZERO_YUV(left, nnz_ctx, row7, 1, v); break;
3203 case 2: SPLAT_ZERO_YUV(left, nnz_ctx, row7, 2, v); break;
3204 case 4: SPLAT_ZERO_YUV(left, nnz_ctx, row7, 4, v); break;
3205 case 8: SPLAT_ZERO_YUV(left, nnz_ctx, row7, 8, v); break;
3210 s->block += w4 * h4 * 64 * bytesperpixel;
3211 s->uvblock[0] += w4 * h4 * 64 * bytesperpixel >> (s->ss_h + s->ss_v);
3212 s->uvblock[1] += w4 * h4 * 64 * bytesperpixel >> (s->ss_h + s->ss_v);
3213 s->eob += 4 * w4 * h4;
3214 s->uveob[0] += 4 * w4 * h4 >> (s->ss_h + s->ss_v);
3215 s->uveob[1] += 4 * w4 * h4 >> (s->ss_h + s->ss_v);
3221 // emulated overhangs if the stride of the target buffer can't hold. This
3222 // allows to support emu-edge and so on even if we have large block
3224 emu[0] = (col + w4) * 8 > f->linesize[0] ||
3225 (row + h4) > s->rows;
3226 emu[1] = (col + w4) * 4 > f->linesize[1] ||
3227 (row + h4) > s->rows;
3229 s->dst[0] = s->tmp_y;
3232 s->dst[0] = f->data[0] + yoff;
3233 s->y_stride = f->linesize[0];
3236 s->dst[1] = s->tmp_uv[0];
3237 s->dst[2] = s->tmp_uv[1];
3240 s->dst[1] = f->data[1] + uvoff;
3241 s->dst[2] = f->data[2] + uvoff;
3242 s->uv_stride = f->linesize[1];
3246 intra_recon_16bpp(ctx, yoff, uvoff);
3248 intra_recon_8bpp(ctx, yoff, uvoff);
3252 inter_recon_16bpp(ctx);
3254 inter_recon_8bpp(ctx);
3258 int w = FFMIN(s->cols - col, w4) * 8, h = FFMIN(s->rows - row, h4) * 8, n, o = 0;
3260 for (n = 0; o < w; n++) {
3265 s->dsp.mc[n][0][0][0][0](f->data[0] + yoff + o, f->linesize[0],
3266 s->tmp_y + o, 128, h, 0, 0);
3267 o += bw * bytesperpixel;
3272 int w = FFMIN(s->cols - col, w4) * 8 >> s->ss_h;
3273 int h = FFMIN(s->rows - row, h4) * 8 >> s->ss_v, n, o = 0;
3275 for (n = 1; o < w; n++) {
3280 s->dsp.mc[n][0][0][0][0](f->data[1] + uvoff + o, f->linesize[1],
3281 s->tmp_uv[0] + o, 128, h, 0, 0);
3282 s->dsp.mc[n][0][0][0][0](f->data[2] + uvoff + o, f->linesize[2],
3283 s->tmp_uv[1] + o, 128, h, 0, 0);
3284 o += bw * bytesperpixel;
3289 // pick filter level and find edges to apply filter to
3290 if (s->filter.level &&
3291 (lvl = s->segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
3292 [b->mode[3] != ZEROMV]) > 0) {
3293 int x_end = FFMIN(s->cols - col, w4), y_end = FFMIN(s->rows - row, h4);
3294 int skip_inter = !b->intra && b->skip, col7 = s->col7, row7 = s->row7;
3296 setctx_2d(&lflvl->level[row7 * 8 + col7], w4, h4, 8, lvl);
3297 mask_edges(lflvl->mask[0], 0, 0, row7, col7, x_end, y_end, 0, 0, b->tx, skip_inter);
3298 if (s->ss_h || s->ss_v)
3299 mask_edges(lflvl->mask[1], s->ss_h, s->ss_v, row7, col7, x_end, y_end,
3300 s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
3301 s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
3302 b->uvtx, skip_inter);
3304 if (!s->filter.lim_lut[lvl]) {
3305 int sharp = s->filter.sharpness;
3309 limit >>= (sharp + 3) >> 2;
3310 limit = FFMIN(limit, 9 - sharp);
3312 limit = FFMAX(limit, 1);
3314 s->filter.lim_lut[lvl] = limit;
3315 s->filter.mblim_lut[lvl] = 2 * (lvl + 2) + limit;
3321 s->block += w4 * h4 * 64 * bytesperpixel;
3322 s->uvblock[0] += w4 * h4 * 64 * bytesperpixel >> (s->ss_v + s->ss_h);
3323 s->uvblock[1] += w4 * h4 * 64 * bytesperpixel >> (s->ss_v + s->ss_h);
3324 s->eob += 4 * w4 * h4;
3325 s->uveob[0] += 4 * w4 * h4 >> (s->ss_v + s->ss_h);
3326 s->uveob[1] += 4 * w4 * h4 >> (s->ss_v + s->ss_h);
3330 static void decode_sb(AVCodecContext *ctx, int row, int col, struct VP9Filter *lflvl,
3331 ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
3333 VP9Context *s = ctx->priv_data;
3334 int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
3335 (((s->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1);
3336 const uint8_t *p = s->keyframe || s->intraonly ? vp9_default_kf_partition_probs[bl][c] :
3337 s->prob.p.partition[bl][c];
3338 enum BlockPartition bp;
3339 ptrdiff_t hbs = 4 >> bl;
3340 AVFrame *f = s->frames[CUR_FRAME].tf.f;
3341 ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
3342 int bytesperpixel = s->bytesperpixel;
3345 bp = vp8_rac_get_tree(&s->c, vp9_partition_tree, p);
3346 decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
3347 } else if (col + hbs < s->cols) { // FIXME why not <=?
3348 if (row + hbs < s->rows) { // FIXME why not <=?
3349 bp = vp8_rac_get_tree(&s->c, vp9_partition_tree, p);
3351 case PARTITION_NONE:
3352 decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
3355 decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
3356 yoff += hbs * 8 * y_stride;
3357 uvoff += hbs * 8 * uv_stride >> s->ss_v;
3358 decode_b(ctx, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
3361 decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
3362 yoff += hbs * 8 * bytesperpixel;
3363 uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
3364 decode_b(ctx, row, col + hbs, lflvl, yoff, uvoff, bl, bp);
3366 case PARTITION_SPLIT:
3367 decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1);
3368 decode_sb(ctx, row, col + hbs, lflvl,
3369 yoff + 8 * hbs * bytesperpixel,
3370 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
3371 yoff += hbs * 8 * y_stride;
3372 uvoff += hbs * 8 * uv_stride >> s->ss_v;
3373 decode_sb(ctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
3374 decode_sb(ctx, row + hbs, col + hbs, lflvl,
3375 yoff + 8 * hbs * bytesperpixel,
3376 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
3381 } else if (vp56_rac_get_prob_branchy(&s->c, p[1])) {
3382 bp = PARTITION_SPLIT;
3383 decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1);
3384 decode_sb(ctx, row, col + hbs, lflvl,
3385 yoff + 8 * hbs * bytesperpixel,
3386 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
3389 decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
3391 } else if (row + hbs < s->rows) { // FIXME why not <=?
3392 if (vp56_rac_get_prob_branchy(&s->c, p[2])) {
3393 bp = PARTITION_SPLIT;
3394 decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1);
3395 yoff += hbs * 8 * y_stride;
3396 uvoff += hbs * 8 * uv_stride >> s->ss_v;
3397 decode_sb(ctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
3400 decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
3403 bp = PARTITION_SPLIT;
3404 decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1);
3406 s->counts.partition[bl][c][bp]++;
3409 static void decode_sb_mem(AVCodecContext *ctx, int row, int col, struct VP9Filter *lflvl,
3410 ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
3412 VP9Context *s = ctx->priv_data;
3414 ptrdiff_t hbs = 4 >> bl;
3415 AVFrame *f = s->frames[CUR_FRAME].tf.f;
3416 ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
3417 int bytesperpixel = s->bytesperpixel;
3420 av_assert2(b->bl == BL_8X8);
3421 decode_b(ctx, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
3422 } else if (s->b->bl == bl) {
3423 decode_b(ctx, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
3424 if (b->bp == PARTITION_H && row + hbs < s->rows) {
3425 yoff += hbs * 8 * y_stride;
3426 uvoff += hbs * 8 * uv_stride >> s->ss_v;
3427 decode_b(ctx, row + hbs, col, lflvl, yoff, uvoff, b->bl, b->bp);
3428 } else if (b->bp == PARTITION_V && col + hbs < s->cols) {
3429 yoff += hbs * 8 * bytesperpixel;
3430 uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
3431 decode_b(ctx, row, col + hbs, lflvl, yoff, uvoff, b->bl, b->bp);
3434 decode_sb_mem(ctx, row, col, lflvl, yoff, uvoff, bl + 1);
3435 if (col + hbs < s->cols) { // FIXME why not <=?
3436 if (row + hbs < s->rows) {
3437 decode_sb_mem(ctx, row, col + hbs, lflvl, yoff + 8 * hbs * bytesperpixel,
3438 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
3439 yoff += hbs * 8 * y_stride;
3440 uvoff += hbs * 8 * uv_stride >> s->ss_v;
3441 decode_sb_mem(ctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
3442 decode_sb_mem(ctx, row + hbs, col + hbs, lflvl,
3443 yoff + 8 * hbs * bytesperpixel,
3444 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
3446 yoff += hbs * 8 * bytesperpixel;
3447 uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
3448 decode_sb_mem(ctx, row, col + hbs, lflvl, yoff, uvoff, bl + 1);
3450 } else if (row + hbs < s->rows) {
3451 yoff += hbs * 8 * y_stride;
3452 uvoff += hbs * 8 * uv_stride >> s->ss_v;
3453 decode_sb_mem(ctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
3458 static av_always_inline void filter_plane_cols(VP9Context *s, int col, int ss_h, int ss_v,
3459 uint8_t *lvl, uint8_t (*mask)[4],
3460 uint8_t *dst, ptrdiff_t ls)
3462 int y, x, bytesperpixel = s->bytesperpixel;
3464 // filter edges between columns (e.g. block1 | block2)
3465 for (y = 0; y < 8; y += 2 << ss_v, dst += 16 * ls, lvl += 16 << ss_v) {
3466 uint8_t *ptr = dst, *l = lvl, *hmask1 = mask[y], *hmask2 = mask[y + 1 + ss_v];
3467 unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2], hm13 = hmask1[3];
3468 unsigned hm2 = hmask2[1] | hmask2[2], hm23 = hmask2[3];
3469 unsigned hm = hm1 | hm2 | hm13 | hm23;
3471 for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 8 * bytesperpixel >> ss_h) {
3474 int L = *l, H = L >> 4;
3475 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
3477 if (hmask1[0] & x) {
3478 if (hmask2[0] & x) {
3479 av_assert2(l[8 << ss_v] == L);
3480 s->dsp.loop_filter_16[0](ptr, ls, E, I, H);
3482 s->dsp.loop_filter_8[2][0](ptr, ls, E, I, H);
3484 } else if (hm2 & x) {
3487 E |= s->filter.mblim_lut[L] << 8;
3488 I |= s->filter.lim_lut[L] << 8;
3489 s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
3491 [0](ptr, ls, E, I, H);
3493 s->dsp.loop_filter_8[!!(hmask1[1] & x)]
3494 [0](ptr, ls, E, I, H);
3496 } else if (hm2 & x) {
3497 int L = l[8 << ss_v], H = L >> 4;
3498 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
3500 s->dsp.loop_filter_8[!!(hmask2[1] & x)]
3501 [0](ptr + 8 * ls, ls, E, I, H);
3509 int L = *l, H = L >> 4;
3510 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
3515 E |= s->filter.mblim_lut[L] << 8;
3516 I |= s->filter.lim_lut[L] << 8;
3517 s->dsp.loop_filter_mix2[0][0][0](ptr + 4 * bytesperpixel, ls, E, I, H);
3519 s->dsp.loop_filter_8[0][0](ptr + 4 * bytesperpixel, ls, E, I, H);
3521 } else if (hm23 & x) {
3522 int L = l[8 << ss_v], H = L >> 4;
3523 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
3525 s->dsp.loop_filter_8[0][0](ptr + 8 * ls + 4 * bytesperpixel, ls, E, I, H);
3533 static av_always_inline void filter_plane_rows(VP9Context *s, int row, int ss_h, int ss_v,
3534 uint8_t *lvl, uint8_t (*mask)[4],
3535 uint8_t *dst, ptrdiff_t ls)
3537 int y, x, bytesperpixel = s->bytesperpixel;
3540 // filter edges between rows (e.g. ------)
3542 for (y = 0; y < 8; y++, dst += 8 * ls >> ss_v) {
3543 uint8_t *ptr = dst, *l = lvl, *vmask = mask[y];
3544 unsigned vm = vmask[0] | vmask[1] | vmask[2], vm3 = vmask[3];
3546 for (x = 1; vm & ~(x - 1); x <<= (2 << ss_h), ptr += 16 * bytesperpixel, l += 2 << ss_h) {
3549 int L = *l, H = L >> 4;
3550 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
3553 if (vmask[0] & (x << (1 + ss_h))) {
3554 av_assert2(l[1 + ss_h] == L);
3555 s->dsp.loop_filter_16[1](ptr, ls, E, I, H);
3557 s->dsp.loop_filter_8[2][1](ptr, ls, E, I, H);
3559 } else if (vm & (x << (1 + ss_h))) {
3562 E |= s->filter.mblim_lut[L] << 8;
3563 I |= s->filter.lim_lut[L] << 8;
3564 s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
3565 [!!(vmask[1] & (x << (1 + ss_h)))]
3566 [1](ptr, ls, E, I, H);
3568 s->dsp.loop_filter_8[!!(vmask[1] & x)]
3569 [1](ptr, ls, E, I, H);
3571 } else if (vm & (x << (1 + ss_h))) {
3572 int L = l[1 + ss_h], H = L >> 4;
3573 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
3575 s->dsp.loop_filter_8[!!(vmask[1] & (x << (1 + ss_h)))]
3576 [1](ptr + 8 * bytesperpixel, ls, E, I, H);
3581 int L = *l, H = L >> 4;
3582 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
3584 if (vm3 & (x << (1 + ss_h))) {
3587 E |= s->filter.mblim_lut[L] << 8;
3588 I |= s->filter.lim_lut[L] << 8;
3589 s->dsp.loop_filter_mix2[0][0][1](ptr + ls * 4, ls, E, I, H);
3591 s->dsp.loop_filter_8[0][1](ptr + ls * 4, ls, E, I, H);
3593 } else if (vm3 & (x << (1 + ss_h))) {
3594 int L = l[1 + ss_h], H = L >> 4;
3595 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
3597 s->dsp.loop_filter_8[0][1](ptr + ls * 4 + 8 * bytesperpixel, ls, E, I, H);
3610 static void loopfilter_sb(AVCodecContext *ctx, struct VP9Filter *lflvl,
3611 int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff)
3613 VP9Context *s = ctx->priv_data;
3614 AVFrame *f = s->frames[CUR_FRAME].tf.f;
3615 uint8_t *dst = f->data[0] + yoff;
3616 ptrdiff_t ls_y = f->linesize[0], ls_uv = f->linesize[1];
3617 uint8_t (*uv_masks)[8][4] = lflvl->mask[s->ss_h | s->ss_v];
3620 // FIXME in how far can we interleave the v/h loopfilter calls? E.g.
3621 // if you think of them as acting on a 8x8 block max, we can interleave
3622 // each v/h within the single x loop, but that only works if we work on
3623 // 8 pixel blocks, and we won't always do that (we want at least 16px
3624 // to use SSE2 optimizations, perhaps 32 for AVX2)
3626 filter_plane_cols(s, col, 0, 0, lflvl->level, lflvl->mask[0][0], dst, ls_y);
3627 filter_plane_rows(s, row, 0, 0, lflvl->level, lflvl->mask[0][1], dst, ls_y);
3629 for (p = 0; p < 2; p++) {
3630 dst = f->data[1 + p] + uvoff;
3631 filter_plane_cols(s, col, s->ss_h, s->ss_v, lflvl->level, uv_masks[0], dst, ls_uv);
3632 filter_plane_rows(s, row, s->ss_h, s->ss_v, lflvl->level, uv_masks[1], dst, ls_uv);
3636 static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
3638 int sb_start = ( idx * n) >> log2_n;
3639 int sb_end = ((idx + 1) * n) >> log2_n;
3640 *start = FFMIN(sb_start, n) << 3;
3641 *end = FFMIN(sb_end, n) << 3;
3644 static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1,
3645 int max_count, int update_factor)
3647 unsigned ct = ct0 + ct1, p2, p1;
3653 p2 = ((ct0 << 8) + (ct >> 1)) / ct;
3654 p2 = av_clip(p2, 1, 255);
3655 ct = FFMIN(ct, max_count);
3656 update_factor = FASTDIV(update_factor * ct, max_count);
3658 // (p1 * (256 - update_factor) + p2 * update_factor + 128) >> 8
3659 *p = p1 + (((p2 - p1) * update_factor + 128) >> 8);
3662 static void adapt_probs(VP9Context *s)
3665 prob_context *p = &s->prob_ctx[s->framectxid].p;
3666 int uf = (s->keyframe || s->intraonly || !s->last_keyframe) ? 112 : 128;
3669 for (i = 0; i < 4; i++)
3670 for (j = 0; j < 2; j++)
3671 for (k = 0; k < 2; k++)
3672 for (l = 0; l < 6; l++)
3673 for (m = 0; m < 6; m++) {
3674 uint8_t *pp = s->prob_ctx[s->framectxid].coef[i][j][k][l][m];
3675 unsigned *e = s->counts.eob[i][j][k][l][m];
3676 unsigned *c = s->counts.coef[i][j][k][l][m];
3678 if (l == 0 && m >= 3) // dc only has 3 pt
3681 adapt_prob(&pp[0], e[0], e[1], 24, uf);
3682 adapt_prob(&pp[1], c[0], c[1] + c[2], 24, uf);
3683 adapt_prob(&pp[2], c[1], c[2], 24, uf);
3686 if (s->keyframe || s->intraonly) {
3687 memcpy(p->skip, s->prob.p.skip, sizeof(p->skip));
3688 memcpy(p->tx32p, s->prob.p.tx32p, sizeof(p->tx32p));
3689 memcpy(p->tx16p, s->prob.p.tx16p, sizeof(p->tx16p));
3690 memcpy(p->tx8p, s->prob.p.tx8p, sizeof(p->tx8p));
3695 for (i = 0; i < 3; i++)
3696 adapt_prob(&p->skip[i], s->counts.skip[i][0], s->counts.skip[i][1], 20, 128);
3699 for (i = 0; i < 4; i++)
3700 adapt_prob(&p->intra[i], s->counts.intra[i][0], s->counts.intra[i][1], 20, 128);
3703 if (s->comppredmode == PRED_SWITCHABLE) {
3704 for (i = 0; i < 5; i++)
3705 adapt_prob(&p->comp[i], s->counts.comp[i][0], s->counts.comp[i][1], 20, 128);
3709 if (s->comppredmode != PRED_SINGLEREF) {
3710 for (i = 0; i < 5; i++)
3711 adapt_prob(&p->comp_ref[i], s->counts.comp_ref[i][0],
3712 s->counts.comp_ref[i][1], 20, 128);
3715 if (s->comppredmode != PRED_COMPREF) {
3716 for (i = 0; i < 5; i++) {
3717 uint8_t *pp = p->single_ref[i];
3718 unsigned (*c)[2] = s->counts.single_ref[i];
3720 adapt_prob(&pp[0], c[0][0], c[0][1], 20, 128);
3721 adapt_prob(&pp[1], c[1][0], c[1][1], 20, 128);
3725 // block partitioning
3726 for (i = 0; i < 4; i++)
3727 for (j = 0; j < 4; j++) {
3728 uint8_t *pp = p->partition[i][j];
3729 unsigned *c = s->counts.partition[i][j];
3731 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
3732 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
3733 adapt_prob(&pp[2], c[2], c[3], 20, 128);
3737 if (s->txfmmode == TX_SWITCHABLE) {
3738 for (i = 0; i < 2; i++) {
3739 unsigned *c16 = s->counts.tx16p[i], *c32 = s->counts.tx32p[i];
3741 adapt_prob(&p->tx8p[i], s->counts.tx8p[i][0], s->counts.tx8p[i][1], 20, 128);
3742 adapt_prob(&p->tx16p[i][0], c16[0], c16[1] + c16[2], 20, 128);
3743 adapt_prob(&p->tx16p[i][1], c16[1], c16[2], 20, 128);
3744 adapt_prob(&p->tx32p[i][0], c32[0], c32[1] + c32[2] + c32[3], 20, 128);
3745 adapt_prob(&p->tx32p[i][1], c32[1], c32[2] + c32[3], 20, 128);
3746 adapt_prob(&p->tx32p[i][2], c32[2], c32[3], 20, 128);
3750 // interpolation filter
3751 if (s->filtermode == FILTER_SWITCHABLE) {
3752 for (i = 0; i < 4; i++) {
3753 uint8_t *pp = p->filter[i];
3754 unsigned *c = s->counts.filter[i];
3756 adapt_prob(&pp[0], c[0], c[1] + c[2], 20, 128);
3757 adapt_prob(&pp[1], c[1], c[2], 20, 128);
3762 for (i = 0; i < 7; i++) {
3763 uint8_t *pp = p->mv_mode[i];
3764 unsigned *c = s->counts.mv_mode[i];
3766 adapt_prob(&pp[0], c[2], c[1] + c[0] + c[3], 20, 128);
3767 adapt_prob(&pp[1], c[0], c[1] + c[3], 20, 128);
3768 adapt_prob(&pp[2], c[1], c[3], 20, 128);
3773 uint8_t *pp = p->mv_joint;
3774 unsigned *c = s->counts.mv_joint;
3776 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
3777 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
3778 adapt_prob(&pp[2], c[2], c[3], 20, 128);
3782 for (i = 0; i < 2; i++) {
3784 unsigned *c, (*c2)[2], sum;
3786 adapt_prob(&p->mv_comp[i].sign, s->counts.mv_comp[i].sign[0],
3787 s->counts.mv_comp[i].sign[1], 20, 128);
3789 pp = p->mv_comp[i].classes;
3790 c = s->counts.mv_comp[i].classes;
3791 sum = c[1] + c[2] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9] + c[10];
3792 adapt_prob(&pp[0], c[0], sum, 20, 128);
3794 adapt_prob(&pp[1], c[1], sum, 20, 128);
3796 adapt_prob(&pp[2], c[2] + c[3], sum, 20, 128);
3797 adapt_prob(&pp[3], c[2], c[3], 20, 128);
3799 adapt_prob(&pp[4], c[4] + c[5], sum, 20, 128);
3800 adapt_prob(&pp[5], c[4], c[5], 20, 128);
3802 adapt_prob(&pp[6], c[6], sum, 20, 128);
3803 adapt_prob(&pp[7], c[7] + c[8], c[9] + c[10], 20, 128);
3804 adapt_prob(&pp[8], c[7], c[8], 20, 128);
3805 adapt_prob(&pp[9], c[9], c[10], 20, 128);
3807 adapt_prob(&p->mv_comp[i].class0, s->counts.mv_comp[i].class0[0],
3808 s->counts.mv_comp[i].class0[1], 20, 128);
3809 pp = p->mv_comp[i].bits;
3810 c2 = s->counts.mv_comp[i].bits;
3811 for (j = 0; j < 10; j++)
3812 adapt_prob(&pp[j], c2[j][0], c2[j][1], 20, 128);
3814 for (j = 0; j < 2; j++) {
3815 pp = p->mv_comp[i].class0_fp[j];
3816 c = s->counts.mv_comp[i].class0_fp[j];
3817 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
3818 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
3819 adapt_prob(&pp[2], c[2], c[3], 20, 128);
3821 pp = p->mv_comp[i].fp;
3822 c = s->counts.mv_comp[i].fp;
3823 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
3824 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
3825 adapt_prob(&pp[2], c[2], c[3], 20, 128);
3827 if (s->highprecisionmvs) {
3828 adapt_prob(&p->mv_comp[i].class0_hp, s->counts.mv_comp[i].class0_hp[0],
3829 s->counts.mv_comp[i].class0_hp[1], 20, 128);
3830 adapt_prob(&p->mv_comp[i].hp, s->counts.mv_comp[i].hp[0],
3831 s->counts.mv_comp[i].hp[1], 20, 128);
3836 for (i = 0; i < 4; i++) {
3837 uint8_t *pp = p->y_mode[i];
3838 unsigned *c = s->counts.y_mode[i], sum, s2;
3840 sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
3841 adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
3842 sum -= c[TM_VP8_PRED];
3843 adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
3844 sum -= c[VERT_PRED];
3845 adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
3846 s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
3848 adapt_prob(&pp[3], s2, sum, 20, 128);
3850 adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
3851 adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED], 20, 128);
3852 sum -= c[DIAG_DOWN_LEFT_PRED];
3853 adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
3854 sum -= c[VERT_LEFT_PRED];
3855 adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
3856 adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
3860 for (i = 0; i < 10; i++) {
3861 uint8_t *pp = p->uv_mode[i];
3862 unsigned *c = s->counts.uv_mode[i], sum, s2;
3864 sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
3865 adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
3866 sum -= c[TM_VP8_PRED];
3867 adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
3868 sum -= c[VERT_PRED];
3869 adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
3870 s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
3872 adapt_prob(&pp[3], s2, sum, 20, 128);
3874 adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
3875 adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED], 20, 128);
3876 sum -= c[DIAG_DOWN_LEFT_PRED];
3877 adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
3878 sum -= c[VERT_LEFT_PRED];
3879 adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
3880 adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
3884 static void free_buffers(VP9Context *s)
3886 av_freep(&s->intra_pred_data[0]);
3887 av_freep(&s->b_base);
3888 av_freep(&s->block_base);
3891 static av_cold int vp9_decode_free(AVCodecContext *ctx)
3893 VP9Context *s = ctx->priv_data;
3896 for (i = 0; i < 3; i++) {
3897 if (s->frames[i].tf.f->data[0])
3898 vp9_unref_frame(ctx, &s->frames[i]);
3899 av_frame_free(&s->frames[i].tf.f);
3901 for (i = 0; i < 8; i++) {
3902 if (s->refs[i].f->data[0])
3903 ff_thread_release_buffer(ctx, &s->refs[i]);
3904 av_frame_free(&s->refs[i].f);
3905 if (s->next_refs[i].f->data[0])
3906 ff_thread_release_buffer(ctx, &s->next_refs[i]);
3907 av_frame_free(&s->next_refs[i].f);
3917 static int vp9_decode_frame(AVCodecContext *ctx, void *frame,
3918 int *got_frame, AVPacket *pkt)
3920 const uint8_t *data = pkt->data;
3921 int size = pkt->size;
3922 VP9Context *s = ctx->priv_data;
3923 int res, tile_row, tile_col, i, ref, row, col;
3924 int retain_segmap_ref = s->segmentation.enabled && !s->segmentation.update_map;
3925 ptrdiff_t yoff, uvoff, ls_y, ls_uv;
3929 if ((res = decode_frame_header(ctx, data, size, &ref)) < 0) {
3931 } else if (res == 0) {
3932 if (!s->refs[ref].f->data[0]) {
3933 av_log(ctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
3934 return AVERROR_INVALIDDATA;
3936 if ((res = av_frame_ref(frame, s->refs[ref].f)) < 0)
3938 ((AVFrame *)frame)->pkt_pts = pkt->pts;
3939 ((AVFrame *)frame)->pkt_dts = pkt->dts;
3940 for (i = 0; i < 8; i++) {
3941 if (s->next_refs[i].f->data[0])
3942 ff_thread_release_buffer(ctx, &s->next_refs[i]);
3943 if (s->refs[i].f->data[0] &&
3944 (res = ff_thread_ref_frame(&s->next_refs[i], &s->refs[i])) < 0)
3953 if (!retain_segmap_ref) {
3954 if (s->frames[REF_FRAME_SEGMAP].tf.f->data[0])
3955 vp9_unref_frame(ctx, &s->frames[REF_FRAME_SEGMAP]);
3956 if (!s->keyframe && !s->intraonly && !s->errorres && s->frames[CUR_FRAME].tf.f->data[0] &&
3957 (res = vp9_ref_frame(ctx, &s->frames[REF_FRAME_SEGMAP], &s->frames[CUR_FRAME])) < 0)
3960 if (s->frames[REF_FRAME_MVPAIR].tf.f->data[0])
3961 vp9_unref_frame(ctx, &s->frames[REF_FRAME_MVPAIR]);
3962 if (!s->intraonly && !s->keyframe && !s->errorres && s->frames[CUR_FRAME].tf.f->data[0] &&
3963 (res = vp9_ref_frame(ctx, &s->frames[REF_FRAME_MVPAIR], &s->frames[CUR_FRAME])) < 0)
3965 if (s->frames[CUR_FRAME].tf.f->data[0])
3966 vp9_unref_frame(ctx, &s->frames[CUR_FRAME]);
3967 if ((res = vp9_alloc_frame(ctx, &s->frames[CUR_FRAME])) < 0)
3969 f = s->frames[CUR_FRAME].tf.f;
3970 f->key_frame = s->keyframe;
3971 f->pict_type = (s->keyframe || s->intraonly) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
3972 ls_y = f->linesize[0];
3973 ls_uv =f->linesize[1];
3976 for (i = 0; i < 8; i++) {
3977 if (s->next_refs[i].f->data[0])
3978 ff_thread_release_buffer(ctx, &s->next_refs[i]);
3979 if (s->refreshrefmask & (1 << i)) {
3980 res = ff_thread_ref_frame(&s->next_refs[i], &s->frames[CUR_FRAME].tf);
3981 } else if (s->refs[i].f->data[0]) {
3982 res = ff_thread_ref_frame(&s->next_refs[i], &s->refs[i]);
3988 // main tile decode loop
3989 bytesperpixel = s->bytesperpixel;
3990 memset(s->above_partition_ctx, 0, s->cols);
3991 memset(s->above_skip_ctx, 0, s->cols);
3992 if (s->keyframe || s->intraonly) {
3993 memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
3995 memset(s->above_mode_ctx, NEARESTMV, s->cols);
3997 memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
3998 memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 16 >> s->ss_h);
3999 memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 16 >> s->ss_h);
4000 memset(s->above_segpred_ctx, 0, s->cols);
4001 s->pass = s->frames[CUR_FRAME].uses_2pass =
4002 ctx->active_thread_type == FF_THREAD_FRAME && s->refreshctx && !s->parallelmode;
4003 if ((res = update_block_buffers(ctx)) < 0) {
4004 av_log(ctx, AV_LOG_ERROR,
4005 "Failed to allocate block buffers\n");
4008 if (s->refreshctx && s->parallelmode) {
4011 for (i = 0; i < 4; i++) {
4012 for (j = 0; j < 2; j++)
4013 for (k = 0; k < 2; k++)
4014 for (l = 0; l < 6; l++)
4015 for (m = 0; m < 6; m++)
4016 memcpy(s->prob_ctx[s->framectxid].coef[i][j][k][l][m],
4017 s->prob.coef[i][j][k][l][m], 3);
4018 if (s->txfmmode == i)
4021 s->prob_ctx[s->framectxid].p = s->prob.p;
4022 ff_thread_finish_setup(ctx);
4023 } else if (!s->refreshctx) {
4024 ff_thread_finish_setup(ctx);
4030 s->block = s->block_base;
4031 s->uvblock[0] = s->uvblock_base[0];
4032 s->uvblock[1] = s->uvblock_base[1];
4033 s->eob = s->eob_base;
4034 s->uveob[0] = s->uveob_base[0];
4035 s->uveob[1] = s->uveob_base[1];
4037 for (tile_row = 0; tile_row < s->tiling.tile_rows; tile_row++) {
4038 set_tile_offset(&s->tiling.tile_row_start, &s->tiling.tile_row_end,
4039 tile_row, s->tiling.log2_tile_rows, s->sb_rows);
4041 for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
4044 if (tile_col == s->tiling.tile_cols - 1 &&
4045 tile_row == s->tiling.tile_rows - 1) {
4048 tile_size = AV_RB32(data);
4052 if (tile_size > size) {
4053 ff_thread_report_progress(&s->frames[CUR_FRAME].tf, INT_MAX, 0);
4054 return AVERROR_INVALIDDATA;
4056 ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size);
4057 if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) { // marker bit
4058 ff_thread_report_progress(&s->frames[CUR_FRAME].tf, INT_MAX, 0);
4059 return AVERROR_INVALIDDATA;
4066 for (row = s->tiling.tile_row_start; row < s->tiling.tile_row_end;
4067 row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
4068 struct VP9Filter *lflvl_ptr = s->lflvl;
4069 ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
4071 for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
4072 set_tile_offset(&s->tiling.tile_col_start, &s->tiling.tile_col_end,
4073 tile_col, s->tiling.log2_tile_cols, s->sb_cols);
4076 memset(s->left_partition_ctx, 0, 8);
4077 memset(s->left_skip_ctx, 0, 8);
4078 if (s->keyframe || s->intraonly) {
4079 memset(s->left_mode_ctx, DC_PRED, 16);
4081 memset(s->left_mode_ctx, NEARESTMV, 8);
4083 memset(s->left_y_nnz_ctx, 0, 16);
4084 memset(s->left_uv_nnz_ctx, 0, 32);
4085 memset(s->left_segpred_ctx, 0, 8);
4087 memcpy(&s->c, &s->c_b[tile_col], sizeof(s->c));
4090 for (col = s->tiling.tile_col_start;
4091 col < s->tiling.tile_col_end;
4092 col += 8, yoff2 += 64 * bytesperpixel,
4093 uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
4094 // FIXME integrate with lf code (i.e. zero after each
4095 // use, similar to invtxfm coefficients, or similar)
4097 memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
4101 decode_sb_mem(ctx, row, col, lflvl_ptr,
4102 yoff2, uvoff2, BL_64X64);
4104 decode_sb(ctx, row, col, lflvl_ptr,
4105 yoff2, uvoff2, BL_64X64);
4109 memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c));
4117 // backup pre-loopfilter reconstruction data for intra
4118 // prediction of next row of sb64s
4119 if (row + 8 < s->rows) {
4120 memcpy(s->intra_pred_data[0],
4121 f->data[0] + yoff + 63 * ls_y,
4122 8 * s->cols * bytesperpixel);
4123 memcpy(s->intra_pred_data[1],
4124 f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
4125 8 * s->cols * bytesperpixel >> s->ss_h);
4126 memcpy(s->intra_pred_data[2],
4127 f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
4128 8 * s->cols * bytesperpixel >> s->ss_h);
4131 // loopfilter one row
4132 if (s->filter.level) {
4135 lflvl_ptr = s->lflvl;
4136 for (col = 0; col < s->cols;
4137 col += 8, yoff2 += 64 * bytesperpixel,
4138 uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
4139 loopfilter_sb(ctx, lflvl_ptr, row, col, yoff2, uvoff2);
4143 // FIXME maybe we can make this more finegrained by running the
4144 // loopfilter per-block instead of after each sbrow
4145 // In fact that would also make intra pred left preparation easier?
4146 ff_thread_report_progress(&s->frames[CUR_FRAME].tf, row >> 3, 0);
4150 if (s->pass < 2 && s->refreshctx && !s->parallelmode) {
4152 ff_thread_finish_setup(ctx);
4154 } while (s->pass++ == 1);
4155 ff_thread_report_progress(&s->frames[CUR_FRAME].tf, INT_MAX, 0);
4158 for (i = 0; i < 8; i++) {
4159 if (s->refs[i].f->data[0])
4160 ff_thread_release_buffer(ctx, &s->refs[i]);
4161 ff_thread_ref_frame(&s->refs[i], &s->next_refs[i]);
4164 if (!s->invisible) {
4165 if ((res = av_frame_ref(frame, s->frames[CUR_FRAME].tf.f)) < 0)
4173 static void vp9_decode_flush(AVCodecContext *ctx)
4175 VP9Context *s = ctx->priv_data;
4178 for (i = 0; i < 3; i++)
4179 vp9_unref_frame(ctx, &s->frames[i]);
4180 for (i = 0; i < 8; i++)
4181 ff_thread_release_buffer(ctx, &s->refs[i]);
4184 static int init_frames(AVCodecContext *ctx)
4186 VP9Context *s = ctx->priv_data;
4189 for (i = 0; i < 3; i++) {
4190 s->frames[i].tf.f = av_frame_alloc();
4191 if (!s->frames[i].tf.f) {
4192 vp9_decode_free(ctx);
4193 av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
4194 return AVERROR(ENOMEM);
4197 for (i = 0; i < 8; i++) {
4198 s->refs[i].f = av_frame_alloc();
4199 s->next_refs[i].f = av_frame_alloc();
4200 if (!s->refs[i].f || !s->next_refs[i].f) {
4201 vp9_decode_free(ctx);
4202 av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
4203 return AVERROR(ENOMEM);
4210 static av_cold int vp9_decode_init(AVCodecContext *ctx)
4212 VP9Context *s = ctx->priv_data;
4214 ctx->internal->allocate_progress = 1;
4216 s->filter.sharpness = -1;
4218 return init_frames(ctx);
4221 static av_cold int vp9_decode_init_thread_copy(AVCodecContext *avctx)
4223 return init_frames(avctx);
4226 static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
4229 VP9Context *s = dst->priv_data, *ssrc = src->priv_data;
4231 // detect size changes in other threads
4232 if (s->intra_pred_data[0] &&
4233 (!ssrc->intra_pred_data[0] || s->cols != ssrc->cols || s->rows != ssrc->rows)) {
4237 for (i = 0; i < 3; i++) {
4238 if (s->frames[i].tf.f->data[0])
4239 vp9_unref_frame(dst, &s->frames[i]);
4240 if (ssrc->frames[i].tf.f->data[0]) {
4241 if ((res = vp9_ref_frame(dst, &s->frames[i], &ssrc->frames[i])) < 0)
4245 for (i = 0; i < 8; i++) {
4246 if (s->refs[i].f->data[0])
4247 ff_thread_release_buffer(dst, &s->refs[i]);
4248 if (ssrc->next_refs[i].f->data[0]) {
4249 if ((res = ff_thread_ref_frame(&s->refs[i], &ssrc->next_refs[i])) < 0)
4254 s->invisible = ssrc->invisible;
4255 s->keyframe = ssrc->keyframe;
4256 s->ss_v = ssrc->ss_v;
4257 s->ss_h = ssrc->ss_h;
4258 s->segmentation.enabled = ssrc->segmentation.enabled;
4259 s->segmentation.update_map = ssrc->segmentation.update_map;
4260 s->bytesperpixel = ssrc->bytesperpixel;
4262 s->bpp_index = ssrc->bpp_index;
4263 memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx));
4264 memcpy(&s->lf_delta, &ssrc->lf_delta, sizeof(s->lf_delta));
4265 if (ssrc->segmentation.enabled) {
4266 memcpy(&s->segmentation.feat, &ssrc->segmentation.feat,
4267 sizeof(s->segmentation.feat));
4273 static const AVProfile profiles[] = {
4274 { FF_PROFILE_VP9_0, "Profile 0" },
4275 { FF_PROFILE_VP9_1, "Profile 1" },
4276 { FF_PROFILE_VP9_2, "Profile 2" },
4277 { FF_PROFILE_VP9_3, "Profile 3" },
4278 { FF_PROFILE_UNKNOWN },
4281 AVCodec ff_vp9_decoder = {
4283 .long_name = NULL_IF_CONFIG_SMALL("Google VP9"),
4284 .type = AVMEDIA_TYPE_VIDEO,
4285 .id = AV_CODEC_ID_VP9,
4286 .priv_data_size = sizeof(VP9Context),
4287 .init = vp9_decode_init,
4288 .close = vp9_decode_free,
4289 .decode = vp9_decode_frame,
4290 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
4291 .flush = vp9_decode_flush,
4292 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp9_decode_init_thread_copy),
4293 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp9_decode_update_thread_context),
4294 .profiles = NULL_IF_CONFIG_SMALL(profiles),