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 Libav.
9 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/avassert.h"
34 static const uint8_t bwh_tab[2][N_BS_SIZES][2] = {
36 { 16, 16 }, { 16, 8 }, { 8, 16 }, { 8, 8 }, { 8, 4 }, { 4, 8 },
37 { 4, 4 }, { 4, 2 }, { 2, 4 }, { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 },
39 { 8, 8 }, { 8, 4 }, { 4, 8 }, { 4, 4 }, { 4, 2 }, { 2, 4 },
40 { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 },
44 // differential forward probability updates
45 static void decode_mode(VP9Context *s, VP9Block *const b)
47 static const uint8_t left_ctx[N_BS_SIZES] = {
48 0x0, 0x8, 0x0, 0x8, 0xc, 0x8, 0xc, 0xe, 0xc, 0xe, 0xf, 0xe, 0xf
50 static const uint8_t above_ctx[N_BS_SIZES] = {
51 0x0, 0x0, 0x8, 0x8, 0x8, 0xc, 0xc, 0xc, 0xe, 0xe, 0xe, 0xf, 0xf
53 static const uint8_t max_tx_for_bl_bp[N_BS_SIZES] = {
54 TX_32X32, TX_32X32, TX_32X32, TX_32X32, TX_16X16, TX_16X16,
55 TX_16X16, TX_8X8, TX_8X8, TX_8X8, TX_4X4, TX_4X4, TX_4X4
57 int row = b->row, col = b->col, row7 = b->row7;
58 enum TxfmMode max_tx = max_tx_for_bl_bp[b->bs];
59 int w4 = FFMIN(s->cols - col, bwh_tab[1][b->bs][0]);
60 int h4 = FFMIN(s->rows - row, bwh_tab[1][b->bs][1]);
61 int have_a = row > 0, have_l = col > s->tiling.tile_col_start;
64 if (!s->segmentation.enabled) {
66 } else if (s->keyframe || s->intraonly) {
67 b->seg_id = s->segmentation.update_map ?
68 vp8_rac_get_tree(&s->c, ff_vp9_segmentation_tree, s->prob.seg) : 0;
69 } else if (!s->segmentation.update_map ||
70 (s->segmentation.temporal &&
71 vp56_rac_get_prob_branchy(&s->c,
72 s->prob.segpred[s->above_segpred_ctx[col] +
73 s->left_segpred_ctx[row7]]))) {
74 int pred = MAX_SEGMENT - 1;
77 for (y = 0; y < h4; y++)
78 for (x = 0; x < w4; x++)
80 s->segmentation_map[(y + row) * 8 * s->sb_cols + x + col]);
83 memset(&s->above_segpred_ctx[col], 1, w4);
84 memset(&s->left_segpred_ctx[row7], 1, h4);
86 b->seg_id = vp8_rac_get_tree(&s->c, ff_vp9_segmentation_tree,
89 memset(&s->above_segpred_ctx[col], 0, w4);
90 memset(&s->left_segpred_ctx[row7], 0, h4);
92 if ((s->segmentation.enabled && s->segmentation.update_map) || s->keyframe) {
93 for (y = 0; y < h4; y++)
94 memset(&s->segmentation_map[(y + row) * 8 * s->sb_cols + col],
98 b->skip = s->segmentation.enabled &&
99 s->segmentation.feat[b->seg_id].skip_enabled;
101 int c = s->left_skip_ctx[row7] + s->above_skip_ctx[col];
102 b->skip = vp56_rac_get_prob(&s->c, s->prob.p.skip[c]);
103 s->counts.skip[c][b->skip]++;
106 if (s->keyframe || s->intraonly) {
108 } else if (s->segmentation.feat[b->seg_id].ref_enabled) {
109 b->intra = !s->segmentation.feat[b->seg_id].ref_val;
113 if (have_a && have_l) {
114 c = s->above_intra_ctx[col] + s->left_intra_ctx[row7];
117 c = have_a ? 2 * s->above_intra_ctx[col] :
118 have_l ? 2 * s->left_intra_ctx[row7] : 0;
120 bit = vp56_rac_get_prob(&s->c, s->prob.p.intra[c]);
121 s->counts.intra[c][bit]++;
125 if ((b->intra || !b->skip) && s->txfmmode == TX_SWITCHABLE) {
129 c = (s->above_skip_ctx[col] ? max_tx :
130 s->above_txfm_ctx[col]) +
131 (s->left_skip_ctx[row7] ? max_tx :
132 s->left_txfm_ctx[row7]) > max_tx;
134 c = s->above_skip_ctx[col] ? 1 :
135 (s->above_txfm_ctx[col] * 2 > max_tx);
138 c = s->left_skip_ctx[row7] ? 1 :
139 (s->left_txfm_ctx[row7] * 2 > max_tx);
145 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][0]);
147 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][1]);
149 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][2]);
151 s->counts.tx32p[c][b->tx]++;
154 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][0]);
156 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][1]);
157 s->counts.tx16p[c][b->tx]++;
160 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx8p[c]);
161 s->counts.tx8p[c][b->tx]++;
168 b->tx = FFMIN(max_tx, s->txfmmode);
171 if (s->keyframe || s->intraonly) {
172 uint8_t *a = &s->above_mode_ctx[col * 2];
173 uint8_t *l = &s->left_mode_ctx[(row7) << 1];
176 if (b->bs > BS_8x8) {
177 // FIXME the memory storage intermediates here aren't really
178 // necessary, they're just there to make the code slightly
181 a[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
182 ff_vp9_default_kf_ymode_probs[a[0]][l[0]]);
183 if (b->bs != BS_8x4) {
184 b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
185 ff_vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
191 b->mode[1] = b->mode[0];
193 if (b->bs != BS_4x8) {
195 a[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
196 ff_vp9_default_kf_ymode_probs[a[0]][l[1]]);
197 if (b->bs != BS_8x4) {
198 b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
199 ff_vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
205 b->mode[3] = b->mode[2];
208 b->mode[2] = b->mode[0];
211 b->mode[3] = b->mode[1];
214 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
215 ff_vp9_default_kf_ymode_probs[*a][*l]);
218 b->mode[1] = b->mode[0];
219 // FIXME this can probably be optimized
220 memset(a, b->mode[0], bwh_tab[0][b->bs][0]);
221 memset(l, b->mode[0], bwh_tab[0][b->bs][1]);
223 b->uvmode = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
224 ff_vp9_default_kf_uvmode_probs[b->mode[3]]);
225 } else if (b->intra) {
227 if (b->bs > BS_8x8) {
228 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
229 s->prob.p.y_mode[0]);
230 s->counts.y_mode[0][b->mode[0]]++;
231 if (b->bs != BS_8x4) {
232 b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
233 s->prob.p.y_mode[0]);
234 s->counts.y_mode[0][b->mode[1]]++;
236 b->mode[1] = b->mode[0];
238 if (b->bs != BS_4x8) {
239 b->mode[2] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
240 s->prob.p.y_mode[0]);
241 s->counts.y_mode[0][b->mode[2]]++;
242 if (b->bs != BS_8x4) {
243 b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
244 s->prob.p.y_mode[0]);
245 s->counts.y_mode[0][b->mode[3]]++;
247 b->mode[3] = b->mode[2];
250 b->mode[2] = b->mode[0];
251 b->mode[3] = b->mode[1];
254 static const uint8_t size_group[10] = {
255 3, 3, 3, 3, 2, 2, 2, 1, 1, 1
257 int sz = size_group[b->bs];
259 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
260 s->prob.p.y_mode[sz]);
263 b->mode[3] = b->mode[0];
264 s->counts.y_mode[sz][b->mode[3]]++;
266 b->uvmode = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
267 s->prob.p.uv_mode[b->mode[3]]);
268 s->counts.uv_mode[b->mode[3]][b->uvmode]++;
270 static const uint8_t inter_mode_ctx_lut[14][14] = {
271 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
272 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
273 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
274 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
275 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
276 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
277 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
278 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
279 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
280 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
281 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
282 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
283 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 3 },
284 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
287 if (s->segmentation.feat[b->seg_id].ref_enabled) {
288 av_assert2(s->segmentation.feat[b->seg_id].ref_val != 0);
290 b->ref[0] = s->segmentation.feat[b->seg_id].ref_val - 1;
292 // read comp_pred flag
293 if (s->comppredmode != PRED_SWITCHABLE) {
294 b->comp = s->comppredmode == PRED_COMPREF;
298 // FIXME add intra as ref=0xff (or -1) to make these easier?
301 if (s->above_comp_ctx[col] && s->left_comp_ctx[row7]) {
303 } else if (s->above_comp_ctx[col]) {
304 c = 2 + (s->left_intra_ctx[row7] ||
305 s->left_ref_ctx[row7] == s->fixcompref);
306 } else if (s->left_comp_ctx[row7]) {
307 c = 2 + (s->above_intra_ctx[col] ||
308 s->above_ref_ctx[col] == s->fixcompref);
310 c = (!s->above_intra_ctx[col] &&
311 s->above_ref_ctx[col] == s->fixcompref) ^
312 (!s->left_intra_ctx[row7] &&
313 s->left_ref_ctx[row & 7] == s->fixcompref);
316 c = s->above_comp_ctx[col] ? 3 :
317 (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->fixcompref);
320 c = s->left_comp_ctx[row7] ? 3 :
321 (!s->left_intra_ctx[row7] && s->left_ref_ctx[row7] == s->fixcompref);
325 b->comp = vp56_rac_get_prob(&s->c, s->prob.p.comp[c]);
326 s->counts.comp[c][b->comp]++;
329 // read actual references
330 // FIXME probably cache a few variables here to prevent repetitive
331 // memory accesses below
332 if (b->comp) { /* two references */
333 int fix_idx = s->signbias[s->fixcompref], var_idx = !fix_idx, c, bit;
335 b->ref[fix_idx] = s->fixcompref;
336 // FIXME can this codeblob be replaced by some sort of LUT?
339 if (s->above_intra_ctx[col]) {
340 if (s->left_intra_ctx[row7]) {
343 c = 1 + 2 * (s->left_ref_ctx[row7] != s->varcompref[1]);
345 } else if (s->left_intra_ctx[row7]) {
346 c = 1 + 2 * (s->above_ref_ctx[col] != s->varcompref[1]);
348 int refl = s->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
350 if (refl == refa && refa == s->varcompref[1]) {
352 } else if (!s->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
353 if ((refa == s->fixcompref && refl == s->varcompref[0]) ||
354 (refl == s->fixcompref && refa == s->varcompref[0])) {
357 c = (refa == refl) ? 3 : 1;
359 } else if (!s->left_comp_ctx[row7]) {
360 if (refa == s->varcompref[1] && refl != s->varcompref[1]) {
363 c = (refl == s->varcompref[1] &&
364 refa != s->varcompref[1]) ? 2 : 4;
366 } else if (!s->above_comp_ctx[col]) {
367 if (refl == s->varcompref[1] && refa != s->varcompref[1]) {
370 c = (refa == s->varcompref[1] &&
371 refl != s->varcompref[1]) ? 2 : 4;
374 c = (refl == refa) ? 4 : 2;
378 if (s->above_intra_ctx[col]) {
380 } else if (s->above_comp_ctx[col]) {
381 c = 4 * (s->above_ref_ctx[col] != s->varcompref[1]);
383 c = 3 * (s->above_ref_ctx[col] != s->varcompref[1]);
387 if (s->left_intra_ctx[row7]) {
389 } else if (s->left_comp_ctx[row7]) {
390 c = 4 * (s->left_ref_ctx[row7] != s->varcompref[1]);
392 c = 3 * (s->left_ref_ctx[row7] != s->varcompref[1]);
397 bit = vp56_rac_get_prob(&s->c, s->prob.p.comp_ref[c]);
398 b->ref[var_idx] = s->varcompref[bit];
399 s->counts.comp_ref[c][bit]++;
400 } else { /* single reference */
403 if (have_a && !s->above_intra_ctx[col]) {
404 if (have_l && !s->left_intra_ctx[row7]) {
405 if (s->left_comp_ctx[row7]) {
406 if (s->above_comp_ctx[col]) {
407 c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7] ||
408 !s->above_ref_ctx[col]);
410 c = (3 * !s->above_ref_ctx[col]) +
411 (!s->fixcompref || !s->left_ref_ctx[row7]);
413 } else if (s->above_comp_ctx[col]) {
414 c = (3 * !s->left_ref_ctx[row7]) +
415 (!s->fixcompref || !s->above_ref_ctx[col]);
417 c = 2 * !s->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
419 } else if (s->above_intra_ctx[col]) {
421 } else if (s->above_comp_ctx[col]) {
422 c = 1 + (!s->fixcompref || !s->above_ref_ctx[col]);
424 c = 4 * (!s->above_ref_ctx[col]);
426 } else if (have_l && !s->left_intra_ctx[row7]) {
427 if (s->left_intra_ctx[row7]) {
429 } else if (s->left_comp_ctx[row7]) {
430 c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7]);
432 c = 4 * (!s->left_ref_ctx[row7]);
437 bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][0]);
438 s->counts.single_ref[c][0][bit]++;
442 // FIXME can this codeblob be replaced by some sort of LUT?
445 if (s->left_intra_ctx[row7]) {
446 if (s->above_intra_ctx[col]) {
448 } else if (s->above_comp_ctx[col]) {
449 c = 1 + 2 * (s->fixcompref == 1 ||
450 s->above_ref_ctx[col] == 1);
451 } else if (!s->above_ref_ctx[col]) {
454 c = 4 * (s->above_ref_ctx[col] == 1);
456 } else if (s->above_intra_ctx[col]) {
457 if (s->left_intra_ctx[row7]) {
459 } else if (s->left_comp_ctx[row7]) {
460 c = 1 + 2 * (s->fixcompref == 1 ||
461 s->left_ref_ctx[row7] == 1);
462 } else if (!s->left_ref_ctx[row7]) {
465 c = 4 * (s->left_ref_ctx[row7] == 1);
467 } else if (s->above_comp_ctx[col]) {
468 if (s->left_comp_ctx[row7]) {
469 if (s->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
470 c = 3 * (s->fixcompref == 1 ||
471 s->left_ref_ctx[row7] == 1);
475 } else if (!s->left_ref_ctx[row7]) {
476 c = 1 + 2 * (s->fixcompref == 1 ||
477 s->above_ref_ctx[col] == 1);
479 c = 3 * (s->left_ref_ctx[row7] == 1) +
480 (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
482 } else if (s->left_comp_ctx[row7]) {
483 if (!s->above_ref_ctx[col]) {
484 c = 1 + 2 * (s->fixcompref == 1 ||
485 s->left_ref_ctx[row7] == 1);
487 c = 3 * (s->above_ref_ctx[col] == 1) +
488 (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
490 } else if (!s->above_ref_ctx[col]) {
491 if (!s->left_ref_ctx[row7]) {
494 c = 4 * (s->left_ref_ctx[row7] == 1);
496 } else if (!s->left_ref_ctx[row7]) {
497 c = 4 * (s->above_ref_ctx[col] == 1);
499 c = 2 * (s->left_ref_ctx[row7] == 1) +
500 2 * (s->above_ref_ctx[col] == 1);
503 if (s->above_intra_ctx[col] ||
504 (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
506 } else if (s->above_comp_ctx[col]) {
507 c = 3 * (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
509 c = 4 * (s->above_ref_ctx[col] == 1);
513 if (s->left_intra_ctx[row7] ||
514 (!s->left_comp_ctx[row7] && !s->left_ref_ctx[row7])) {
516 } else if (s->left_comp_ctx[row7]) {
517 c = 3 * (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
519 c = 4 * (s->left_ref_ctx[row7] == 1);
524 bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][1]);
525 s->counts.single_ref[c][1][bit]++;
531 if (b->bs <= BS_8x8) {
532 if (s->segmentation.feat[b->seg_id].skip_enabled) {
538 static const uint8_t off[10] = {
539 3, 0, 0, 1, 0, 0, 0, 0, 0, 0
542 // FIXME this needs to use the LUT tables from find_ref_mvs
543 // because not all are -1,0/0,-1
544 int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
545 [s->left_mode_ctx[row7 + off[b->bs]]];
547 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
548 s->prob.p.mv_mode[c]);
551 b->mode[3] = b->mode[0];
552 s->counts.mv_mode[c][b->mode[0] - 10]++;
556 if (s->filtermode == FILTER_SWITCHABLE) {
559 if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
560 if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
561 c = s->above_filter_ctx[col] == s->left_filter_ctx[row7] ?
562 s->left_filter_ctx[row7] : 3;
564 c = s->above_filter_ctx[col];
566 } else if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
567 c = s->left_filter_ctx[row7];
572 b->filter = vp8_rac_get_tree(&s->c, ff_vp9_filter_tree,
573 s->prob.p.filter[c]);
574 s->counts.filter[c][b->filter]++;
576 b->filter = s->filtermode;
579 if (b->bs > BS_8x8) {
580 int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][s->left_mode_ctx[row7]];
582 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
583 s->prob.p.mv_mode[c]);
584 s->counts.mv_mode[c][b->mode[0] - 10]++;
585 ff_vp9_fill_mv(s, b->mv[0], b->mode[0], 0);
587 if (b->bs != BS_8x4) {
588 b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
589 s->prob.p.mv_mode[c]);
590 s->counts.mv_mode[c][b->mode[1] - 10]++;
591 ff_vp9_fill_mv(s, b->mv[1], b->mode[1], 1);
593 b->mode[1] = b->mode[0];
594 AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
595 AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
598 if (b->bs != BS_4x8) {
599 b->mode[2] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
600 s->prob.p.mv_mode[c]);
601 s->counts.mv_mode[c][b->mode[2] - 10]++;
602 ff_vp9_fill_mv(s, b->mv[2], b->mode[2], 2);
604 if (b->bs != BS_8x4) {
605 b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
606 s->prob.p.mv_mode[c]);
607 s->counts.mv_mode[c][b->mode[3] - 10]++;
608 ff_vp9_fill_mv(s, b->mv[3], b->mode[3], 3);
610 b->mode[3] = b->mode[2];
611 AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
612 AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
615 b->mode[2] = b->mode[0];
616 AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
617 AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
618 b->mode[3] = b->mode[1];
619 AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
620 AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
623 ff_vp9_fill_mv(s, b->mv[0], b->mode[0], -1);
624 AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
625 AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
626 AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
627 AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
628 AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
629 AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
633 // FIXME this can probably be optimized
634 memset(&s->above_skip_ctx[col], b->skip, w4);
635 memset(&s->left_skip_ctx[row7], b->skip, h4);
636 memset(&s->above_txfm_ctx[col], b->tx, w4);
637 memset(&s->left_txfm_ctx[row7], b->tx, h4);
638 memset(&s->above_partition_ctx[col], above_ctx[b->bs], w4);
639 memset(&s->left_partition_ctx[row7], left_ctx[b->bs], h4);
640 if (!s->keyframe && !s->intraonly) {
641 memset(&s->above_intra_ctx[col], b->intra, w4);
642 memset(&s->left_intra_ctx[row7], b->intra, h4);
643 memset(&s->above_comp_ctx[col], b->comp, w4);
644 memset(&s->left_comp_ctx[row7], b->comp, h4);
645 memset(&s->above_mode_ctx[col], b->mode[3], w4);
646 memset(&s->left_mode_ctx[row7], b->mode[3], h4);
647 if (s->filtermode == FILTER_SWITCHABLE && !b->intra) {
648 memset(&s->above_filter_ctx[col], b->filter, w4);
649 memset(&s->left_filter_ctx[row7], b->filter, h4);
650 b->filter = ff_vp9_filter_lut[b->filter];
652 if (b->bs > BS_8x8) {
653 int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
655 AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
656 AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
657 AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][0], mv0);
658 AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][1], mv1);
659 AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
660 AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
661 AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
662 AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
664 int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
666 for (n = 0; n < w4 * 2; n++) {
667 AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
668 AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
670 for (n = 0; n < h4 * 2; n++) {
671 AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][0], mv0);
672 AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][1], mv1);
676 if (!b->intra) { // FIXME write 0xff or -1 if intra, so we can use this
677 // as a direct check in above branches
678 int vref = b->ref[b->comp ? s->signbias[s->varcompref[0]] : 0];
680 memset(&s->above_ref_ctx[col], vref, w4);
681 memset(&s->left_ref_ctx[row7], vref, h4);
686 for (y = 0; y < h4; y++) {
687 int x, o = (row + y) * s->sb_cols * 8 + col;
690 for (x = 0; x < w4; x++) {
691 s->mv[0][o + x].ref[0] =
692 s->mv[0][o + x].ref[1] = -1;
694 } else if (b->comp) {
695 for (x = 0; x < w4; x++) {
696 s->mv[0][o + x].ref[0] = b->ref[0];
697 s->mv[0][o + x].ref[1] = b->ref[1];
698 AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
699 AV_COPY32(&s->mv[0][o + x].mv[1], &b->mv[3][1]);
702 for (x = 0; x < w4; x++) {
703 s->mv[0][o + x].ref[0] = b->ref[0];
704 s->mv[0][o + x].ref[1] = -1;
705 AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
711 // FIXME remove tx argument, and merge cnt/eob arguments?
712 static int decode_block_coeffs(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
713 enum TxfmMode tx, unsigned (*cnt)[6][3],
714 unsigned (*eob)[6][2], uint8_t(*p)[6][11],
715 int nnz, const int16_t *scan,
716 const int16_t(*nb)[2],
717 const int16_t *band_counts, const int16_t *qmul)
719 int i = 0, band = 0, band_left = band_counts[band];
720 uint8_t *tp = p[0][nnz];
726 val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
727 eob[band][nnz][val]++;
732 if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
735 band_left = band_counts[++band];
737 nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
740 break; //invalid input; blocks should end with EOB
745 if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
750 // fill in p[3-10] (model fill) - only once per frame for each pos
752 memcpy(&tp[3], ff_vp9_model_pareto8[tp[2]], 8);
755 if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
756 if (!vp56_rac_get_prob_branchy(c, tp[4])) {
759 val = 3 + vp56_rac_get_prob(c, tp[5]);
762 } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
764 if (!vp56_rac_get_prob_branchy(c, tp[7])) {
765 val = vp56_rac_get_prob(c, 159) + 5;
767 val = (vp56_rac_get_prob(c, 165) << 1) + 7;
768 val += vp56_rac_get_prob(c, 145);
772 if (!vp56_rac_get_prob_branchy(c, tp[8])) {
773 if (!vp56_rac_get_prob_branchy(c, tp[9])) {
774 val = (vp56_rac_get_prob(c, 173) << 2) + 11;
775 val += (vp56_rac_get_prob(c, 148) << 1);
776 val += vp56_rac_get_prob(c, 140);
778 val = (vp56_rac_get_prob(c, 176) << 3) + 19;
779 val += (vp56_rac_get_prob(c, 155) << 2);
780 val += (vp56_rac_get_prob(c, 140) << 1);
781 val += vp56_rac_get_prob(c, 135);
783 } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
784 val = (vp56_rac_get_prob(c, 180) << 4) + 35;
785 val += (vp56_rac_get_prob(c, 157) << 3);
786 val += (vp56_rac_get_prob(c, 141) << 2);
787 val += (vp56_rac_get_prob(c, 134) << 1);
788 val += vp56_rac_get_prob(c, 130);
790 val = (vp56_rac_get_prob(c, 254) << 13) + 67;
791 val += (vp56_rac_get_prob(c, 254) << 12);
792 val += (vp56_rac_get_prob(c, 254) << 11);
793 val += (vp56_rac_get_prob(c, 252) << 10);
794 val += (vp56_rac_get_prob(c, 249) << 9);
795 val += (vp56_rac_get_prob(c, 243) << 8);
796 val += (vp56_rac_get_prob(c, 230) << 7);
797 val += (vp56_rac_get_prob(c, 196) << 6);
798 val += (vp56_rac_get_prob(c, 177) << 5);
799 val += (vp56_rac_get_prob(c, 153) << 4);
800 val += (vp56_rac_get_prob(c, 140) << 3);
801 val += (vp56_rac_get_prob(c, 133) << 2);
802 val += (vp56_rac_get_prob(c, 130) << 1);
803 val += vp56_rac_get_prob(c, 129);
808 band_left = band_counts[++band];
809 if (tx == TX_32X32) // FIXME slow
810 coef[rc] = ((vp8_rac_get(c) ? -val : val) * qmul[!!i]) / 2;
812 coef[rc] = (vp8_rac_get(c) ? -val : val) * qmul[!!i];
813 nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
815 } while (++i < n_coeffs);
820 static int decode_coeffs(AVCodecContext *avctx)
822 VP9Context *s = avctx->priv_data;
823 VP9Block *const b = &s->b;
824 int row = b->row, col = b->col;
825 uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
826 unsigned (*c)[6][3] = s->counts.coef[b->tx][0 /* y */][!b->intra];
827 unsigned (*e)[6][2] = s->counts.eob[b->tx][0 /* y */][!b->intra];
828 int w4 = bwh_tab[1][b->bs][0] << 1, h4 = bwh_tab[1][b->bs][1] << 1;
829 int end_x = FFMIN(2 * (s->cols - col), w4);
830 int end_y = FFMIN(2 * (s->rows - row), h4);
831 int n, pl, x, y, step1d = 1 << b->tx, step = 1 << (b->tx * 2);
832 int uvstep1d = 1 << b->uvtx, uvstep = 1 << (b->uvtx * 2), ret;
833 int16_t (*qmul)[2] = s->segmentation.feat[b->seg_id].qmul;
834 int tx = 4 * s->lossless + b->tx;
835 const int16_t **yscans = ff_vp9_scans[tx];
836 const int16_t (**ynbs)[2] = ff_vp9_scans_nb[tx];
837 const int16_t *uvscan = ff_vp9_scans[b->uvtx][DCT_DCT];
838 const int16_t (*uvnb)[2] = ff_vp9_scans_nb[b->uvtx][DCT_DCT];
839 uint8_t *a = &s->above_y_nnz_ctx[col * 2];
840 uint8_t *l = &s->left_y_nnz_ctx[(row & 7) << 1];
841 static const int16_t band_counts[4][8] = {
842 { 1, 2, 3, 4, 3, 16 - 13, 0 },
843 { 1, 2, 3, 4, 11, 64 - 21, 0 },
844 { 1, 2, 3, 4, 11, 256 - 21, 0 },
845 { 1, 2, 3, 4, 11, 1024 - 21, 0 },
847 const int16_t *y_band_counts = band_counts[b->tx];
848 const int16_t *uv_band_counts = band_counts[b->uvtx];
851 if (b->tx > TX_4X4) { // FIXME slow
852 for (y = 0; y < end_y; y += step1d)
853 for (x = 1; x < step1d; x++)
855 for (x = 0; x < end_x; x += step1d)
856 for (y = 1; y < step1d; y++)
859 for (n = 0, y = 0; y < end_y; y += step1d) {
860 for (x = 0; x < end_x; x += step1d, n += step) {
861 enum TxfmType txtp = ff_vp9_intra_txfm_type[b->mode[b->tx == TX_4X4 &&
864 int nnz = a[x] + l[y];
865 if ((ret = decode_block_coeffs(&s->c, s->block + 16 * n, 16 * step,
866 b->tx, c, e, p, nnz, yscans[txtp],
867 ynbs[txtp], y_band_counts,
872 AV_WN16A(&s->eob[n], ret);
877 if (b->tx > TX_4X4) { // FIXME slow
878 for (y = 0; y < end_y; y += step1d)
879 memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, step1d - 1));
880 for (x = 0; x < end_x; x += step1d)
881 memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, step1d - 1));
884 p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
885 c = s->counts.coef[b->uvtx][1 /* uv */][!b->intra];
886 e = s->counts.eob[b->uvtx][1 /* uv */][!b->intra];
891 for (pl = 0; pl < 2; pl++) {
892 a = &s->above_uv_nnz_ctx[pl][col];
893 l = &s->left_uv_nnz_ctx[pl][row & 7];
894 if (b->uvtx > TX_4X4) { // FIXME slow
895 for (y = 0; y < end_y; y += uvstep1d)
896 for (x = 1; x < uvstep1d; x++)
898 for (x = 0; x < end_x; x += uvstep1d)
899 for (y = 1; y < uvstep1d; y++)
902 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
903 for (x = 0; x < end_x; x += uvstep1d, n += uvstep) {
904 int nnz = a[x] + l[y];
905 if ((ret = decode_block_coeffs(&s->c, s->uvblock[pl] + 16 * n,
906 16 * uvstep, b->uvtx, c, e, p,
908 uv_band_counts, qmul[1])) < 0)
911 if (b->uvtx > TX_8X8)
912 AV_WN16A(&s->uveob[pl][n], ret);
914 s->uveob[pl][n] = ret;
917 if (b->uvtx > TX_4X4) { // FIXME slow
918 for (y = 0; y < end_y; y += uvstep1d)
919 memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, uvstep1d - 1));
920 for (x = 0; x < end_x; x += uvstep1d)
921 memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, uvstep1d - 1));
928 static av_always_inline int check_intra_mode(VP9Context *s, int mode,
931 ptrdiff_t stride_edge,
933 ptrdiff_t stride_inner,
934 uint8_t *l, int col, int x, int w,
935 int row, int y, enum TxfmMode tx,
938 int have_top = row > 0 || y > 0;
939 int have_left = col > s->tiling.tile_col_start || x > 0;
940 int have_right = x < w - 1;
941 static const uint8_t mode_conv[10][2 /* have_left */][2 /* have_top */] = {
942 [VERT_PRED] = { { DC_127_PRED, VERT_PRED },
943 { DC_127_PRED, VERT_PRED } },
944 [HOR_PRED] = { { DC_129_PRED, DC_129_PRED },
945 { HOR_PRED, HOR_PRED } },
946 [DC_PRED] = { { DC_128_PRED, TOP_DC_PRED },
947 { LEFT_DC_PRED, DC_PRED } },
948 [DIAG_DOWN_LEFT_PRED] = { { DC_127_PRED, DIAG_DOWN_LEFT_PRED },
949 { DC_127_PRED, DIAG_DOWN_LEFT_PRED } },
950 [DIAG_DOWN_RIGHT_PRED] = { { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED },
951 { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED } },
952 [VERT_RIGHT_PRED] = { { VERT_RIGHT_PRED, VERT_RIGHT_PRED },
953 { VERT_RIGHT_PRED, VERT_RIGHT_PRED } },
954 [HOR_DOWN_PRED] = { { HOR_DOWN_PRED, HOR_DOWN_PRED },
955 { HOR_DOWN_PRED, HOR_DOWN_PRED } },
956 [VERT_LEFT_PRED] = { { DC_127_PRED, VERT_LEFT_PRED },
957 { DC_127_PRED, VERT_LEFT_PRED } },
958 [HOR_UP_PRED] = { { DC_129_PRED, DC_129_PRED },
959 { HOR_UP_PRED, HOR_UP_PRED } },
960 [TM_VP8_PRED] = { { DC_129_PRED, VERT_PRED },
961 { HOR_PRED, TM_VP8_PRED } },
963 static const struct {
964 uint8_t needs_left:1;
966 uint8_t needs_topleft:1;
967 uint8_t needs_topright:1;
968 } edges[N_INTRA_PRED_MODES] = {
969 [VERT_PRED] = { .needs_top = 1 },
970 [HOR_PRED] = { .needs_left = 1 },
971 [DC_PRED] = { .needs_top = 1, .needs_left = 1 },
972 [DIAG_DOWN_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
973 [DIAG_DOWN_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
974 .needs_topleft = 1 },
975 [VERT_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
976 .needs_topleft = 1 },
977 [HOR_DOWN_PRED] = { .needs_left = 1, .needs_top = 1,
978 .needs_topleft = 1 },
979 [VERT_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
980 [HOR_UP_PRED] = { .needs_left = 1 },
981 [TM_VP8_PRED] = { .needs_left = 1, .needs_top = 1,
982 .needs_topleft = 1 },
983 [LEFT_DC_PRED] = { .needs_left = 1 },
984 [TOP_DC_PRED] = { .needs_top = 1 },
985 [DC_128_PRED] = { 0 },
986 [DC_127_PRED] = { 0 },
987 [DC_129_PRED] = { 0 }
990 av_assert2(mode >= 0 && mode < 10);
991 mode = mode_conv[mode][have_left][have_top];
992 if (edges[mode].needs_top) {
993 uint8_t *top = NULL, *topleft = NULL;
994 int n_px_need = 4 << tx, n_px_have = (((s->cols - col) << !p) - x) * 4;
995 int n_px_need_tr = 0;
997 if (tx == TX_4X4 && edges[mode].needs_topright && have_right)
1000 // if top of sb64-row, use s->intra_pred_data[] instead of
1001 // dst[-stride] for intra prediction (it contains pre- instead of
1002 // post-loopfilter data)
1004 top = !(row & 7) && !y ?
1005 s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
1006 y == 0 ? &dst_edge[-stride_edge] : &dst_inner[-stride_inner];
1008 topleft = !(row & 7) && !y ?
1009 s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
1010 y == 0 || x == 0 ? &dst_edge[-stride_edge] :
1011 &dst_inner[-stride_inner];
1015 (!edges[mode].needs_topleft || (have_left && top == topleft)) &&
1016 (tx != TX_4X4 || !edges[mode].needs_topright || have_right) &&
1017 n_px_need + n_px_need_tr <= n_px_have) {
1021 if (n_px_need <= n_px_have) {
1022 memcpy(*a, top, n_px_need);
1024 memcpy(*a, top, n_px_have);
1025 memset(&(*a)[n_px_have], (*a)[n_px_have - 1],
1026 n_px_need - n_px_have);
1029 memset(*a, 127, n_px_need);
1031 if (edges[mode].needs_topleft) {
1032 if (have_left && have_top)
1033 (*a)[-1] = topleft[-1];
1035 (*a)[-1] = have_top ? 129 : 127;
1037 if (tx == TX_4X4 && edges[mode].needs_topright) {
1038 if (have_top && have_right &&
1039 n_px_need + n_px_need_tr <= n_px_have) {
1040 memcpy(&(*a)[4], &top[4], 4);
1042 memset(&(*a)[4], (*a)[3], 4);
1047 if (edges[mode].needs_left) {
1050 int n_px_need = 4 << tx;
1051 int n_px_have = (((s->rows - row) << !p) - y) * 4;
1052 uint8_t *dst = x == 0 ? dst_edge : dst_inner;
1053 ptrdiff_t stride = x == 0 ? stride_edge : stride_inner;
1055 if (n_px_need <= n_px_have) {
1056 for (i = 0; i < n_px_need; i++)
1057 l[i] = dst[i * stride - 1];
1059 for (i = 0; i < n_px_have; i++)
1060 l[i] = dst[i * stride - 1];
1061 memset(&l[i], l[i - 1], n_px_need - n_px_have);
1064 memset(l, 129, 4 << tx);
1071 static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off)
1073 VP9Context *s = avctx->priv_data;
1074 VP9Block *const b = &s->b;
1075 int row = b->row, col = b->col;
1076 int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
1077 int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
1078 int end_x = FFMIN(2 * (s->cols - col), w4);
1079 int end_y = FFMIN(2 * (s->rows - row), h4);
1080 int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
1081 int uvstep1d = 1 << b->uvtx, p;
1082 uint8_t *dst = b->dst[0], *dst_r = s->cur_frame->data[0] + y_off;
1084 for (n = 0, y = 0; y < end_y; y += step1d) {
1085 uint8_t *ptr = dst, *ptr_r = dst_r;
1086 for (x = 0; x < end_x;
1087 x += step1d, ptr += 4 * step1d, ptr_r += 4 * step1d, n += step) {
1088 int mode = b->mode[b->bs > BS_8x8 && b->tx == TX_4X4 ?
1090 LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
1091 uint8_t *a = &a_buf[16], l[32];
1092 enum TxfmType txtp = ff_vp9_intra_txfm_type[mode];
1093 int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
1095 mode = check_intra_mode(s, mode, &a, ptr_r,
1096 s->cur_frame->linesize[0],
1097 ptr, b->y_stride, l,
1098 col, x, w4, row, y, b->tx, 0);
1099 s->dsp.intra_pred[b->tx][mode](ptr, b->y_stride, l, a);
1101 s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride,
1102 s->block + 16 * n, eob);
1104 dst_r += 4 * s->cur_frame->linesize[0] * step1d;
1105 dst += 4 * b->y_stride * step1d;
1113 step = 1 << (b->uvtx * 2);
1114 for (p = 0; p < 2; p++) {
1115 dst = b->dst[1 + p];
1116 dst_r = s->cur_frame->data[1 + p] + uv_off;
1117 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
1118 uint8_t *ptr = dst, *ptr_r = dst_r;
1119 for (x = 0; x < end_x;
1120 x += uvstep1d, ptr += 4 * uvstep1d,
1121 ptr_r += 4 * uvstep1d, n += step) {
1122 int mode = b->uvmode;
1123 LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
1124 uint8_t *a = &a_buf[16], l[32];
1125 int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
1128 mode = check_intra_mode(s, mode, &a, ptr_r,
1129 s->cur_frame->linesize[1],
1130 ptr, b->uv_stride, l,
1131 col, x, w4, row, y, b->uvtx, p + 1);
1132 s->dsp.intra_pred[b->uvtx][mode](ptr, b->uv_stride, l, a);
1134 s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1135 s->uvblock[p] + 16 * n,
1138 dst_r += 4 * uvstep1d * s->cur_frame->linesize[1];
1139 dst += 4 * uvstep1d * b->uv_stride;
1144 static av_always_inline void mc_luma_dir(VP9Context *s, vp9_mc_func(*mc)[2],
1145 uint8_t *dst, ptrdiff_t dst_stride,
1147 ptrdiff_t ref_stride,
1148 ptrdiff_t y, ptrdiff_t x,
1150 int bw, int bh, int w, int h)
1152 int mx = mv->x, my = mv->y;
1156 ref += y * ref_stride + x;
1159 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
1160 if (x < !!mx * 3 || y < !!my * 3 ||
1161 x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
1162 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1163 ref - !!my * 3 * ref_stride - !!mx * 3,
1166 bw + !!mx * 7, bh + !!my * 7,
1167 x - !!mx * 3, y - !!my * 3, w, h);
1168 ref = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1171 mc[!!mx][!!my](dst, ref, dst_stride, ref_stride, bh, mx << 1, my << 1);
1174 static av_always_inline void mc_chroma_dir(VP9Context *s, vp9_mc_func(*mc)[2],
1175 uint8_t *dst_u, uint8_t *dst_v,
1176 ptrdiff_t dst_stride,
1177 const uint8_t *ref_u,
1178 ptrdiff_t src_stride_u,
1179 const uint8_t *ref_v,
1180 ptrdiff_t src_stride_v,
1181 ptrdiff_t y, ptrdiff_t x,
1183 int bw, int bh, int w, int h)
1185 int mx = mv->x, my = mv->y;
1189 ref_u += y * src_stride_u + x;
1190 ref_v += y * src_stride_v + x;
1193 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
1194 if (x < !!mx * 3 || y < !!my * 3 ||
1195 x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
1196 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1197 ref_u - !!my * 3 * src_stride_u - !!mx * 3,
1200 bw + !!mx * 7, bh + !!my * 7,
1201 x - !!mx * 3, y - !!my * 3, w, h);
1202 ref_u = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1203 mc[!!mx][!!my](dst_u, ref_u, dst_stride, 80, bh, mx, my);
1205 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1206 ref_v - !!my * 3 * src_stride_v - !!mx * 3,
1209 bw + !!mx * 7, bh + !!my * 7,
1210 x - !!mx * 3, y - !!my * 3, w, h);
1211 ref_v = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1212 mc[!!mx][!!my](dst_v, ref_v, dst_stride, 80, bh, mx, my);
1214 mc[!!mx][!!my](dst_u, ref_u, dst_stride, src_stride_u, bh, mx, my);
1215 mc[!!mx][!!my](dst_v, ref_v, dst_stride, src_stride_v, bh, mx, my);
1219 static int inter_recon(AVCodecContext *avctx)
1221 static const uint8_t bwlog_tab[2][N_BS_SIZES] = {
1222 { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
1223 { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 },
1225 VP9Context *s = avctx->priv_data;
1226 VP9Block *const b = &s->b;
1227 int row = b->row, col = b->col;
1228 AVFrame *ref1 = s->refs[s->refidx[b->ref[0]]];
1229 AVFrame *ref2 = b->comp ? s->refs[s->refidx[b->ref[1]]] : NULL;
1230 int w = avctx->width, h = avctx->height;
1231 ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride;
1233 if (!ref1->data[0] || (b->comp && !ref2->data[0]))
1234 return AVERROR_INVALIDDATA;
1237 if (b->bs > BS_8x8) {
1238 if (b->bs == BS_8x4) {
1239 mc_luma_dir(s, s->dsp.mc[3][b->filter][0], b->dst[0], ls_y,
1240 ref1->data[0], ref1->linesize[0],
1241 row << 3, col << 3, &b->mv[0][0], 8, 4, w, h);
1242 mc_luma_dir(s, s->dsp.mc[3][b->filter][0],
1243 b->dst[0] + 4 * ls_y, ls_y,
1244 ref1->data[0], ref1->linesize[0],
1245 (row << 3) + 4, col << 3, &b->mv[2][0], 8, 4, w, h);
1248 mc_luma_dir(s, s->dsp.mc[3][b->filter][1], b->dst[0], ls_y,
1249 ref2->data[0], ref2->linesize[0],
1250 row << 3, col << 3, &b->mv[0][1], 8, 4, w, h);
1251 mc_luma_dir(s, s->dsp.mc[3][b->filter][1],
1252 b->dst[0] + 4 * ls_y, ls_y,
1253 ref2->data[0], ref2->linesize[0],
1254 (row << 3) + 4, col << 3, &b->mv[2][1], 8, 4, w, h);
1256 } else if (b->bs == BS_4x8) {
1257 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
1258 ref1->data[0], ref1->linesize[0],
1259 row << 3, col << 3, &b->mv[0][0], 4, 8, w, h);
1260 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
1261 ref1->data[0], ref1->linesize[0],
1262 row << 3, (col << 3) + 4, &b->mv[1][0], 4, 8, w, h);
1265 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
1266 ref2->data[0], ref2->linesize[0],
1267 row << 3, col << 3, &b->mv[0][1], 4, 8, w, h);
1268 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
1269 ref2->data[0], ref2->linesize[0],
1270 row << 3, (col << 3) + 4, &b->mv[1][1], 4, 8, w, h);
1273 av_assert2(b->bs == BS_4x4);
1275 // FIXME if two horizontally adjacent blocks have the same MV,
1276 // do a w8 instead of a w4 call
1277 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
1278 ref1->data[0], ref1->linesize[0],
1279 row << 3, col << 3, &b->mv[0][0], 4, 4, w, h);
1280 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
1281 ref1->data[0], ref1->linesize[0],
1282 row << 3, (col << 3) + 4, &b->mv[1][0], 4, 4, w, h);
1283 mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
1284 b->dst[0] + 4 * ls_y, ls_y,
1285 ref1->data[0], ref1->linesize[0],
1286 (row << 3) + 4, col << 3, &b->mv[2][0], 4, 4, w, h);
1287 mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
1288 b->dst[0] + 4 * ls_y + 4, ls_y,
1289 ref1->data[0], ref1->linesize[0],
1290 (row << 3) + 4, (col << 3) + 4, &b->mv[3][0], 4, 4, w, h);
1293 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
1294 ref2->data[0], ref2->linesize[0],
1295 row << 3, col << 3, &b->mv[0][1], 4, 4, w, h);
1296 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
1297 ref2->data[0], ref2->linesize[0],
1298 row << 3, (col << 3) + 4, &b->mv[1][1], 4, 4, w, h);
1299 mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
1300 b->dst[0] + 4 * ls_y, ls_y,
1301 ref2->data[0], ref2->linesize[0],
1302 (row << 3) + 4, col << 3, &b->mv[2][1], 4, 4, w, h);
1303 mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
1304 b->dst[0] + 4 * ls_y + 4, ls_y,
1305 ref2->data[0], ref2->linesize[0],
1306 (row << 3) + 4, (col << 3) + 4, &b->mv[3][1], 4, 4, w, h);
1310 int bwl = bwlog_tab[0][b->bs];
1311 int bw = bwh_tab[0][b->bs][0] * 4;
1312 int bh = bwh_tab[0][b->bs][1] * 4;
1314 mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[0], ls_y,
1315 ref1->data[0], ref1->linesize[0],
1316 row << 3, col << 3, &b->mv[0][0], bw, bh, w, h);
1319 mc_luma_dir(s, s->dsp.mc[bwl][b->filter][1], b->dst[0], ls_y,
1320 ref2->data[0], ref2->linesize[0],
1321 row << 3, col << 3, &b->mv[0][1], bw, bh, w, h);
1326 int bwl = bwlog_tab[1][b->bs];
1327 int bw = bwh_tab[1][b->bs][0] * 4, bh = bwh_tab[1][b->bs][1] * 4;
1332 if (b->bs > BS_8x8) {
1333 mvuv.x = ROUNDED_DIV(b->mv[0][0].x + b->mv[1][0].x +
1334 b->mv[2][0].x + b->mv[3][0].x, 4);
1335 mvuv.y = ROUNDED_DIV(b->mv[0][0].y + b->mv[1][0].y +
1336 b->mv[2][0].y + b->mv[3][0].y, 4);
1341 mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][0],
1342 b->dst[1], b->dst[2], ls_uv,
1343 ref1->data[1], ref1->linesize[1],
1344 ref1->data[2], ref1->linesize[2],
1345 row << 2, col << 2, &mvuv, bw, bh, w, h);
1348 if (b->bs > BS_8x8) {
1349 mvuv.x = ROUNDED_DIV(b->mv[0][1].x + b->mv[1][1].x +
1350 b->mv[2][1].x + b->mv[3][1].x, 4);
1351 mvuv.y = ROUNDED_DIV(b->mv[0][1].y + b->mv[1][1].y +
1352 b->mv[2][1].y + b->mv[3][1].y, 4);
1356 mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][1],
1357 b->dst[1], b->dst[2], ls_uv,
1358 ref2->data[1], ref2->linesize[1],
1359 ref2->data[2], ref2->linesize[2],
1360 row << 2, col << 2, &mvuv, bw, bh, w, h);
1365 /* mostly copied intra_reconn() */
1367 int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
1368 int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
1369 int end_x = FFMIN(2 * (s->cols - col), w4);
1370 int end_y = FFMIN(2 * (s->rows - row), h4);
1371 int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
1372 int uvstep1d = 1 << b->uvtx, p;
1373 uint8_t *dst = b->dst[0];
1376 for (n = 0, y = 0; y < end_y; y += step1d) {
1378 for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d, n += step) {
1379 int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
1382 s->dsp.itxfm_add[tx][DCT_DCT](ptr, b->y_stride,
1383 s->block + 16 * n, eob);
1385 dst += 4 * b->y_stride * step1d;
1393 step = 1 << (b->uvtx * 2);
1394 for (p = 0; p < 2; p++) {
1395 dst = b->dst[p + 1];
1396 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
1398 for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d, n += step) {
1399 int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
1402 s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1403 s->uvblock[p] + 16 * n, eob);
1405 dst += 4 * uvstep1d * b->uv_stride;
1412 static av_always_inline void mask_edges(VP9Filter *lflvl, int is_uv,
1413 int row_and_7, int col_and_7,
1414 int w, int h, int col_end, int row_end,
1415 enum TxfmMode tx, int skip_inter)
1417 // FIXME I'm pretty sure all loops can be replaced by a single LUT if
1418 // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
1419 // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
1420 // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
1422 // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
1423 // edges. This means that for UV, we work on two subsampled blocks at
1424 // a time, and we only use the topleft block's mode information to set
1425 // things like block strength. Thus, for any block size smaller than
1426 // 16x16, ignore the odd portion of the block.
1427 if (tx == TX_4X4 && is_uv) {
1442 if (tx == TX_4X4 && !skip_inter) {
1443 int t = 1 << col_and_7, m_col = (t << w) - t, y;
1444 int m_col_odd = (t << (w - 1)) - t;
1446 // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
1448 int m_row_8 = m_col & 0x01, m_row_4 = m_col - m_row_8;
1450 for (y = row_and_7; y < h + row_and_7; y++) {
1451 int col_mask_id = 2 - !(y & 7);
1453 lflvl->mask[is_uv][0][y][1] |= m_row_8;
1454 lflvl->mask[is_uv][0][y][2] |= m_row_4;
1455 // for odd lines, if the odd col is not being filtered,
1456 // skip odd row also:
1463 // if a/c are even row/col and b/d are odd, and d is skipped,
1464 // e.g. right edge of size-66x66.webm, then skip b also (bug)
1465 if ((col_end & 1) && (y & 1)) {
1466 lflvl->mask[is_uv][1][y][col_mask_id] |= m_col_odd;
1468 lflvl->mask[is_uv][1][y][col_mask_id] |= m_col;
1472 int m_row_8 = m_col & 0x11, m_row_4 = m_col - m_row_8;
1474 for (y = row_and_7; y < h + row_and_7; y++) {
1475 int col_mask_id = 2 - !(y & 3);
1477 lflvl->mask[is_uv][0][y][1] |= m_row_8; // row edge
1478 lflvl->mask[is_uv][0][y][2] |= m_row_4;
1479 lflvl->mask[is_uv][1][y][col_mask_id] |= m_col; // col edge
1480 lflvl->mask[is_uv][0][y][3] |= m_col;
1481 lflvl->mask[is_uv][1][y][3] |= m_col;
1485 int y, t = 1 << col_and_7, m_col = (t << w) - t;
1488 int mask_id = (tx == TX_8X8);
1489 int l2 = tx + is_uv - 1, step1d = 1 << l2;
1490 static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
1491 int m_row = m_col & masks[l2];
1493 // at odd UV col/row edges tx16/tx32 loopfilter edges, force
1494 // 8wd loopfilter to prevent going off the visible edge.
1495 if (is_uv && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
1496 int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
1497 int m_row_8 = m_row - m_row_16;
1499 for (y = row_and_7; y < h + row_and_7; y++) {
1500 lflvl->mask[is_uv][0][y][0] |= m_row_16;
1501 lflvl->mask[is_uv][0][y][1] |= m_row_8;
1504 for (y = row_and_7; y < h + row_and_7; y++)
1505 lflvl->mask[is_uv][0][y][mask_id] |= m_row;
1508 if (is_uv && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
1509 for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
1510 lflvl->mask[is_uv][1][y][0] |= m_col;
1511 if (y - row_and_7 == h - 1)
1512 lflvl->mask[is_uv][1][y][1] |= m_col;
1514 for (y = row_and_7; y < h + row_and_7; y += step1d)
1515 lflvl->mask[is_uv][1][y][mask_id] |= m_col;
1517 } else if (tx != TX_4X4) {
1520 mask_id = (tx == TX_8X8) || (is_uv && h == 1);
1521 lflvl->mask[is_uv][1][row_and_7][mask_id] |= m_col;
1522 mask_id = (tx == TX_8X8) || (is_uv && w == 1);
1523 for (y = row_and_7; y < h + row_and_7; y++)
1524 lflvl->mask[is_uv][0][y][mask_id] |= t;
1526 int t8 = t & 0x01, t4 = t - t8;
1528 for (y = row_and_7; y < h + row_and_7; y++) {
1529 lflvl->mask[is_uv][0][y][2] |= t4;
1530 lflvl->mask[is_uv][0][y][1] |= t8;
1532 lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 7)] |= m_col;
1534 int t8 = t & 0x11, t4 = t - t8;
1536 for (y = row_and_7; y < h + row_and_7; y++) {
1537 lflvl->mask[is_uv][0][y][2] |= t4;
1538 lflvl->mask[is_uv][0][y][1] |= t8;
1540 lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 3)] |= m_col;
1545 int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col,
1546 VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
1547 enum BlockLevel bl, enum BlockPartition bp)
1549 VP9Context *s = avctx->priv_data;
1550 VP9Block *const b = &s->b;
1551 enum BlockSize bs = bl * 3 + bp;
1552 int ret, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl;
1560 s->min_mv.x = -(128 + col * 64);
1561 s->min_mv.y = -(128 + row * 64);
1562 s->max_mv.x = 128 + (s->cols - col - w4) * 64;
1563 s->max_mv.y = 128 + (s->rows - row - h4) * 64;
1567 b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx));
1570 if ((ret = decode_coeffs(avctx)) < 0)
1575 memset(&s->above_y_nnz_ctx[col * 2], 0, w4 * 2);
1576 memset(&s->left_y_nnz_ctx[(row & 7) << 1], 0, h4 * 2);
1577 for (pl = 0; pl < 2; pl++) {
1578 memset(&s->above_uv_nnz_ctx[pl][col], 0, w4);
1579 memset(&s->left_uv_nnz_ctx[pl][row & 7], 0, h4);
1583 /* Emulated overhangs if the stride of the target buffer can't hold.
1584 * This allows to support emu-edge and so on even if we have large
1585 * block overhangs. */
1586 emu[0] = (col + w4) * 8 > s->cur_frame->linesize[0] ||
1587 (row + h4) > s->rows;
1588 emu[1] = (col + w4) * 4 > s->cur_frame->linesize[1] ||
1589 (row + h4) > s->rows;
1591 b->dst[0] = s->tmp_y;
1594 b->dst[0] = s->cur_frame->data[0] + yoff;
1595 b->y_stride = s->cur_frame->linesize[0];
1598 b->dst[1] = s->tmp_uv[0];
1599 b->dst[2] = s->tmp_uv[1];
1602 b->dst[1] = s->cur_frame->data[1] + uvoff;
1603 b->dst[2] = s->cur_frame->data[2] + uvoff;
1604 b->uv_stride = s->cur_frame->linesize[1];
1607 intra_recon(avctx, yoff, uvoff);
1609 if ((ret = inter_recon(avctx)) < 0)
1613 int w = FFMIN(s->cols - col, w4) * 8;
1614 int h = FFMIN(s->rows - row, h4) * 8;
1617 for (n = 0; o < w; n++) {
1622 s->dsp.mc[n][0][0][0][0](s->cur_frame->data[0] + yoff + o,
1624 s->cur_frame->linesize[0],
1631 int w = FFMIN(s->cols - col, w4) * 4;
1632 int h = FFMIN(s->rows - row, h4) * 4;
1635 for (n = 1; o < w; n++) {
1640 s->dsp.mc[n][0][0][0][0](s->cur_frame->data[1] + uvoff + o,
1642 s->cur_frame->linesize[1],
1644 s->dsp.mc[n][0][0][0][0](s->cur_frame->data[2] + uvoff + o,
1646 s->cur_frame->linesize[2],
1653 // pick filter level and find edges to apply filter to
1654 if (s->filter.level &&
1655 (lvl = s->segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
1656 [b->mode[3] != ZEROMV]) > 0) {
1657 int x_end = FFMIN(s->cols - col, w4);
1658 int y_end = FFMIN(s->rows - row, h4);
1659 int skip_inter = !b->intra && b->skip;
1661 for (y = 0; y < h4; y++)
1662 memset(&lflvl->level[((row & 7) + y) * 8 + (col & 7)], lvl, w4);
1663 mask_edges(lflvl, 0, row & 7, col & 7, x_end, y_end, 0, 0, b->tx, skip_inter);
1664 mask_edges(lflvl, 1, row & 7, col & 7, x_end, y_end,
1665 s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
1666 s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
1667 b->uvtx, skip_inter);
1669 if (!s->filter.lim_lut[lvl]) {
1670 int sharp = s->filter.sharpness;
1674 limit >>= (sharp + 3) >> 2;
1675 limit = FFMIN(limit, 9 - sharp);
1677 limit = FFMAX(limit, 1);
1679 s->filter.lim_lut[lvl] = limit;
1680 s->filter.mblim_lut[lvl] = 2 * (lvl + 2) + limit;