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]]))) {
76 for (y = 0; y < h4; y++)
77 for (x = 0; x < w4; x++)
79 s->segmentation_map[(y + row) * 8 * s->sb_cols + x + col]);
82 memset(&s->above_segpred_ctx[col], 1, w4);
83 memset(&s->left_segpred_ctx[row7], 1, h4);
85 b->seg_id = vp8_rac_get_tree(&s->c, ff_vp9_segmentation_tree,
88 memset(&s->above_segpred_ctx[col], 0, w4);
89 memset(&s->left_segpred_ctx[row7], 0, h4);
91 if ((s->segmentation.enabled && s->segmentation.update_map) || s->keyframe) {
92 for (y = 0; y < h4; y++)
93 memset(&s->segmentation_map[(y + row) * 8 * s->sb_cols + col],
97 b->skip = s->segmentation.enabled &&
98 s->segmentation.feat[b->seg_id].skip_enabled;
100 int c = s->left_skip_ctx[row7] + s->above_skip_ctx[col];
101 b->skip = vp56_rac_get_prob(&s->c, s->prob.p.skip[c]);
102 s->counts.skip[c][b->skip]++;
105 if (s->keyframe || s->intraonly) {
107 } else if (s->segmentation.feat[b->seg_id].ref_enabled) {
108 b->intra = !s->segmentation.feat[b->seg_id].ref_val;
112 if (have_a && have_l) {
113 c = s->above_intra_ctx[col] + s->left_intra_ctx[row7];
116 c = have_a ? 2 * s->above_intra_ctx[col] :
117 have_l ? 2 * s->left_intra_ctx[row7] : 0;
119 bit = vp56_rac_get_prob(&s->c, s->prob.p.intra[c]);
120 s->counts.intra[c][bit]++;
124 if ((b->intra || !b->skip) && s->txfmmode == TX_SWITCHABLE) {
128 c = (s->above_skip_ctx[col] ? max_tx :
129 s->above_txfm_ctx[col]) +
130 (s->left_skip_ctx[row7] ? max_tx :
131 s->left_txfm_ctx[row7]) > max_tx;
133 c = s->above_skip_ctx[col] ? 1 :
134 (s->above_txfm_ctx[col] * 2 > max_tx);
137 c = s->left_skip_ctx[row7] ? 1 :
138 (s->left_txfm_ctx[row7] * 2 > max_tx);
144 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][0]);
146 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][1]);
148 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][2]);
150 s->counts.tx32p[c][b->tx]++;
153 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][0]);
155 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][1]);
156 s->counts.tx16p[c][b->tx]++;
159 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx8p[c]);
160 s->counts.tx8p[c][b->tx]++;
167 b->tx = FFMIN(max_tx, s->txfmmode);
170 if (s->keyframe || s->intraonly) {
171 uint8_t *a = &s->above_mode_ctx[col * 2];
172 uint8_t *l = &s->left_mode_ctx[(row7) << 1];
175 if (b->bs > BS_8x8) {
176 // FIXME the memory storage intermediates here aren't really
177 // necessary, they're just there to make the code slightly
180 a[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
181 ff_vp9_default_kf_ymode_probs[a[0]][l[0]]);
182 if (b->bs != BS_8x4) {
183 b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
184 ff_vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
190 b->mode[1] = b->mode[0];
192 if (b->bs != BS_4x8) {
194 a[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
195 ff_vp9_default_kf_ymode_probs[a[0]][l[1]]);
196 if (b->bs != BS_8x4) {
197 b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
198 ff_vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
204 b->mode[3] = b->mode[2];
207 b->mode[2] = b->mode[0];
210 b->mode[3] = b->mode[1];
213 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
214 ff_vp9_default_kf_ymode_probs[*a][*l]);
217 b->mode[1] = b->mode[0];
218 // FIXME this can probably be optimized
219 memset(a, b->mode[0], bwh_tab[0][b->bs][0]);
220 memset(l, b->mode[0], bwh_tab[0][b->bs][1]);
222 b->uvmode = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
223 ff_vp9_default_kf_uvmode_probs[b->mode[3]]);
224 } else if (b->intra) {
226 if (b->bs > BS_8x8) {
227 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
228 s->prob.p.y_mode[0]);
229 s->counts.y_mode[0][b->mode[0]]++;
230 if (b->bs != BS_8x4) {
231 b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
232 s->prob.p.y_mode[0]);
233 s->counts.y_mode[0][b->mode[1]]++;
235 b->mode[1] = b->mode[0];
237 if (b->bs != BS_4x8) {
238 b->mode[2] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
239 s->prob.p.y_mode[0]);
240 s->counts.y_mode[0][b->mode[2]]++;
241 if (b->bs != BS_8x4) {
242 b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
243 s->prob.p.y_mode[0]);
244 s->counts.y_mode[0][b->mode[3]]++;
246 b->mode[3] = b->mode[2];
249 b->mode[2] = b->mode[0];
250 b->mode[3] = b->mode[1];
253 static const uint8_t size_group[10] = {
254 3, 3, 3, 3, 2, 2, 2, 1, 1, 1
256 int sz = size_group[b->bs];
258 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
259 s->prob.p.y_mode[sz]);
262 b->mode[3] = b->mode[0];
263 s->counts.y_mode[sz][b->mode[3]]++;
265 b->uvmode = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
266 s->prob.p.uv_mode[b->mode[3]]);
267 s->counts.uv_mode[b->mode[3]][b->uvmode]++;
269 static const uint8_t inter_mode_ctx_lut[14][14] = {
270 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
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 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
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, 1, 1, 0, 3 },
283 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
286 if (s->segmentation.feat[b->seg_id].ref_enabled) {
287 av_assert2(s->segmentation.feat[b->seg_id].ref_val != 0);
289 b->ref[0] = s->segmentation.feat[b->seg_id].ref_val - 1;
291 // read comp_pred flag
292 if (s->comppredmode != PRED_SWITCHABLE) {
293 b->comp = s->comppredmode == PRED_COMPREF;
297 // FIXME add intra as ref=0xff (or -1) to make these easier?
300 if (s->above_comp_ctx[col] && s->left_comp_ctx[row7]) {
302 } else if (s->above_comp_ctx[col]) {
303 c = 2 + (s->left_intra_ctx[row7] ||
304 s->left_ref_ctx[row7] == s->fixcompref);
305 } else if (s->left_comp_ctx[row7]) {
306 c = 2 + (s->above_intra_ctx[col] ||
307 s->above_ref_ctx[col] == s->fixcompref);
309 c = (!s->above_intra_ctx[col] &&
310 s->above_ref_ctx[col] == s->fixcompref) ^
311 (!s->left_intra_ctx[row7] &&
312 s->left_ref_ctx[row & 7] == s->fixcompref);
315 c = s->above_comp_ctx[col] ? 3 :
316 (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->fixcompref);
319 c = s->left_comp_ctx[row7] ? 3 :
320 (!s->left_intra_ctx[row7] && s->left_ref_ctx[row7] == s->fixcompref);
324 b->comp = vp56_rac_get_prob(&s->c, s->prob.p.comp[c]);
325 s->counts.comp[c][b->comp]++;
328 // read actual references
329 // FIXME probably cache a few variables here to prevent repetitive
330 // memory accesses below
331 if (b->comp) { /* two references */
332 int fix_idx = s->signbias[s->fixcompref], var_idx = !fix_idx, c, bit;
334 b->ref[fix_idx] = s->fixcompref;
335 // FIXME can this codeblob be replaced by some sort of LUT?
338 if (s->above_intra_ctx[col]) {
339 if (s->left_intra_ctx[row7]) {
342 c = 1 + 2 * (s->left_ref_ctx[row7] != s->varcompref[1]);
344 } else if (s->left_intra_ctx[row7]) {
345 c = 1 + 2 * (s->above_ref_ctx[col] != s->varcompref[1]);
347 int refl = s->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
349 if (refl == refa && refa == s->varcompref[1]) {
351 } else if (!s->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
352 if ((refa == s->fixcompref && refl == s->varcompref[0]) ||
353 (refl == s->fixcompref && refa == s->varcompref[0])) {
356 c = (refa == refl) ? 3 : 1;
358 } else if (!s->left_comp_ctx[row7]) {
359 if (refa == s->varcompref[1] && refl != s->varcompref[1]) {
362 c = (refl == s->varcompref[1] &&
363 refa != s->varcompref[1]) ? 2 : 4;
365 } else if (!s->above_comp_ctx[col]) {
366 if (refl == s->varcompref[1] && refa != s->varcompref[1]) {
369 c = (refa == s->varcompref[1] &&
370 refl != s->varcompref[1]) ? 2 : 4;
373 c = (refl == refa) ? 4 : 2;
377 if (s->above_intra_ctx[col]) {
379 } else if (s->above_comp_ctx[col]) {
380 c = 4 * (s->above_ref_ctx[col] != s->varcompref[1]);
382 c = 3 * (s->above_ref_ctx[col] != s->varcompref[1]);
386 if (s->left_intra_ctx[row7]) {
388 } else if (s->left_comp_ctx[row7]) {
389 c = 4 * (s->left_ref_ctx[row7] != s->varcompref[1]);
391 c = 3 * (s->left_ref_ctx[row7] != s->varcompref[1]);
396 bit = vp56_rac_get_prob(&s->c, s->prob.p.comp_ref[c]);
397 b->ref[var_idx] = s->varcompref[bit];
398 s->counts.comp_ref[c][bit]++;
399 } else { /* single reference */
402 if (have_a && !s->above_intra_ctx[col]) {
403 if (have_l && !s->left_intra_ctx[row7]) {
404 if (s->left_comp_ctx[row7]) {
405 if (s->above_comp_ctx[col]) {
406 c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7] ||
407 !s->above_ref_ctx[col]);
409 c = (3 * !s->above_ref_ctx[col]) +
410 (!s->fixcompref || !s->left_ref_ctx[row7]);
412 } else if (s->above_comp_ctx[col]) {
413 c = (3 * !s->left_ref_ctx[row7]) +
414 (!s->fixcompref || !s->above_ref_ctx[col]);
416 c = 2 * !s->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
418 } else if (s->above_intra_ctx[col]) {
420 } else if (s->above_comp_ctx[col]) {
421 c = 1 + (!s->fixcompref || !s->above_ref_ctx[col]);
423 c = 4 * (!s->above_ref_ctx[col]);
425 } else if (have_l && !s->left_intra_ctx[row7]) {
426 if (s->left_intra_ctx[row7]) {
428 } else if (s->left_comp_ctx[row7]) {
429 c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7]);
431 c = 4 * (!s->left_ref_ctx[row7]);
436 bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][0]);
437 s->counts.single_ref[c][0][bit]++;
441 // FIXME can this codeblob be replaced by some sort of LUT?
444 if (s->left_intra_ctx[row7]) {
445 if (s->above_intra_ctx[col]) {
447 } else if (s->above_comp_ctx[col]) {
448 c = 1 + 2 * (s->fixcompref == 1 ||
449 s->above_ref_ctx[col] == 1);
450 } else if (!s->above_ref_ctx[col]) {
453 c = 4 * (s->above_ref_ctx[col] == 1);
455 } else if (s->above_intra_ctx[col]) {
456 if (s->left_intra_ctx[row7]) {
458 } else if (s->left_comp_ctx[row7]) {
459 c = 1 + 2 * (s->fixcompref == 1 ||
460 s->left_ref_ctx[row7] == 1);
461 } else if (!s->left_ref_ctx[row7]) {
464 c = 4 * (s->left_ref_ctx[row7] == 1);
466 } else if (s->above_comp_ctx[col]) {
467 if (s->left_comp_ctx[row7]) {
468 if (s->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
469 c = 3 * (s->fixcompref == 1 ||
470 s->left_ref_ctx[row7] == 1);
474 } else if (!s->left_ref_ctx[row7]) {
475 c = 1 + 2 * (s->fixcompref == 1 ||
476 s->above_ref_ctx[col] == 1);
478 c = 3 * (s->left_ref_ctx[row7] == 1) +
479 (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
481 } else if (s->left_comp_ctx[row7]) {
482 if (!s->above_ref_ctx[col]) {
483 c = 1 + 2 * (s->fixcompref == 1 ||
484 s->left_ref_ctx[row7] == 1);
486 c = 3 * (s->above_ref_ctx[col] == 1) +
487 (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
489 } else if (!s->above_ref_ctx[col]) {
490 if (!s->left_ref_ctx[row7]) {
493 c = 4 * (s->left_ref_ctx[row7] == 1);
495 } else if (!s->left_ref_ctx[row7]) {
496 c = 4 * (s->above_ref_ctx[col] == 1);
498 c = 2 * (s->left_ref_ctx[row7] == 1) +
499 2 * (s->above_ref_ctx[col] == 1);
502 if (s->above_intra_ctx[col] ||
503 (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
505 } else if (s->above_comp_ctx[col]) {
506 c = 3 * (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
508 c = 4 * (s->above_ref_ctx[col] == 1);
512 if (s->left_intra_ctx[row7] ||
513 (!s->left_comp_ctx[row7] && !s->left_ref_ctx[row7])) {
515 } else if (s->left_comp_ctx[row7]) {
516 c = 3 * (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
518 c = 4 * (s->left_ref_ctx[row7] == 1);
523 bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][1]);
524 s->counts.single_ref[c][1][bit]++;
530 if (b->bs <= BS_8x8) {
531 if (s->segmentation.feat[b->seg_id].skip_enabled) {
537 static const uint8_t off[10] = {
538 3, 0, 0, 1, 0, 0, 0, 0, 0, 0
541 // FIXME this needs to use the LUT tables from find_ref_mvs
542 // because not all are -1,0/0,-1
543 int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
544 [s->left_mode_ctx[row7 + off[b->bs]]];
546 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
547 s->prob.p.mv_mode[c]);
550 b->mode[3] = b->mode[0];
551 s->counts.mv_mode[c][b->mode[0] - 10]++;
555 if (s->filtermode == FILTER_SWITCHABLE) {
558 if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
559 if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
560 c = s->above_filter_ctx[col] == s->left_filter_ctx[row7] ?
561 s->left_filter_ctx[row7] : 3;
563 c = s->above_filter_ctx[col];
565 } else if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
566 c = s->left_filter_ctx[row7];
571 b->filter = vp8_rac_get_tree(&s->c, ff_vp9_filter_tree,
572 s->prob.p.filter[c]);
573 s->counts.filter[c][b->filter]++;
575 b->filter = s->filtermode;
578 if (b->bs > BS_8x8) {
579 int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][s->left_mode_ctx[row7]];
581 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
582 s->prob.p.mv_mode[c]);
583 s->counts.mv_mode[c][b->mode[0] - 10]++;
584 ff_vp9_fill_mv(s, b->mv[0], b->mode[0], 0);
586 if (b->bs != BS_8x4) {
587 b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
588 s->prob.p.mv_mode[c]);
589 s->counts.mv_mode[c][b->mode[1] - 10]++;
590 ff_vp9_fill_mv(s, b->mv[1], b->mode[1], 1);
592 b->mode[1] = b->mode[0];
593 AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
594 AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
597 if (b->bs != BS_4x8) {
598 b->mode[2] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
599 s->prob.p.mv_mode[c]);
600 s->counts.mv_mode[c][b->mode[2] - 10]++;
601 ff_vp9_fill_mv(s, b->mv[2], b->mode[2], 2);
603 if (b->bs != BS_8x4) {
604 b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
605 s->prob.p.mv_mode[c]);
606 s->counts.mv_mode[c][b->mode[3] - 10]++;
607 ff_vp9_fill_mv(s, b->mv[3], b->mode[3], 3);
609 b->mode[3] = b->mode[2];
610 AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
611 AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
614 b->mode[2] = b->mode[0];
615 AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
616 AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
617 b->mode[3] = b->mode[1];
618 AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
619 AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
622 ff_vp9_fill_mv(s, b->mv[0], b->mode[0], -1);
623 AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
624 AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
625 AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
626 AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
627 AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
628 AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
632 // FIXME this can probably be optimized
633 memset(&s->above_skip_ctx[col], b->skip, w4);
634 memset(&s->left_skip_ctx[row7], b->skip, h4);
635 memset(&s->above_txfm_ctx[col], b->tx, w4);
636 memset(&s->left_txfm_ctx[row7], b->tx, h4);
637 memset(&s->above_partition_ctx[col], above_ctx[b->bs], w4);
638 memset(&s->left_partition_ctx[row7], left_ctx[b->bs], h4);
639 if (!s->keyframe && !s->intraonly) {
640 memset(&s->above_intra_ctx[col], b->intra, w4);
641 memset(&s->left_intra_ctx[row7], b->intra, h4);
642 memset(&s->above_comp_ctx[col], b->comp, w4);
643 memset(&s->left_comp_ctx[row7], b->comp, h4);
644 memset(&s->above_mode_ctx[col], b->mode[3], w4);
645 memset(&s->left_mode_ctx[row7], b->mode[3], h4);
646 if (s->filtermode == FILTER_SWITCHABLE && !b->intra) {
647 memset(&s->above_filter_ctx[col], b->filter, w4);
648 memset(&s->left_filter_ctx[row7], b->filter, h4);
649 b->filter = ff_vp9_filter_lut[b->filter];
651 if (b->bs > BS_8x8) {
652 int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
654 AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
655 AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
656 AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][0], mv0);
657 AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][1], mv1);
658 AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
659 AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
660 AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
661 AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
663 int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
665 for (n = 0; n < w4 * 2; n++) {
666 AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
667 AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
669 for (n = 0; n < h4 * 2; n++) {
670 AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][0], mv0);
671 AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][1], mv1);
675 if (!b->intra) { // FIXME write 0xff or -1 if intra, so we can use this
676 // as a direct check in above branches
677 int vref = b->ref[b->comp ? s->signbias[s->varcompref[0]] : 0];
679 memset(&s->above_ref_ctx[col], vref, w4);
680 memset(&s->left_ref_ctx[row7], vref, h4);
685 for (y = 0; y < h4; y++) {
686 int x, o = (row + y) * s->sb_cols * 8 + col;
689 for (x = 0; x < w4; x++) {
690 s->mv[0][o + x].ref[0] =
691 s->mv[0][o + x].ref[1] = -1;
693 } else if (b->comp) {
694 for (x = 0; x < w4; x++) {
695 s->mv[0][o + x].ref[0] = b->ref[0];
696 s->mv[0][o + x].ref[1] = b->ref[1];
697 AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
698 AV_COPY32(&s->mv[0][o + x].mv[1], &b->mv[3][1]);
701 for (x = 0; x < w4; x++) {
702 s->mv[0][o + x].ref[0] = b->ref[0];
703 s->mv[0][o + x].ref[1] = -1;
704 AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
710 // FIXME remove tx argument, and merge cnt/eob arguments?
711 static int decode_block_coeffs(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
712 enum TxfmMode tx, unsigned (*cnt)[6][3],
713 unsigned (*eob)[6][2], uint8_t(*p)[6][11],
714 int nnz, const int16_t *scan,
715 const int16_t(*nb)[2],
716 const int16_t *band_counts, const int16_t *qmul)
718 int i = 0, band = 0, band_left = band_counts[band];
719 uint8_t *tp = p[0][nnz];
725 val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
726 eob[band][nnz][val]++;
731 if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
734 band_left = band_counts[++band];
736 nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
739 break; //invalid input; blocks should end with EOB
744 if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
749 // fill in p[3-10] (model fill) - only once per frame for each pos
751 memcpy(&tp[3], ff_vp9_model_pareto8[tp[2]], 8);
754 if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
755 if (!vp56_rac_get_prob_branchy(c, tp[4])) {
758 val = 3 + vp56_rac_get_prob(c, tp[5]);
761 } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
763 if (!vp56_rac_get_prob_branchy(c, tp[7])) {
764 val = vp56_rac_get_prob(c, 159) + 5;
766 val = (vp56_rac_get_prob(c, 165) << 1) + 7;
767 val += vp56_rac_get_prob(c, 145);
771 if (!vp56_rac_get_prob_branchy(c, tp[8])) {
772 if (!vp56_rac_get_prob_branchy(c, tp[9])) {
773 val = (vp56_rac_get_prob(c, 173) << 2) + 11;
774 val += (vp56_rac_get_prob(c, 148) << 1);
775 val += vp56_rac_get_prob(c, 140);
777 val = (vp56_rac_get_prob(c, 176) << 3) + 19;
778 val += (vp56_rac_get_prob(c, 155) << 2);
779 val += (vp56_rac_get_prob(c, 140) << 1);
780 val += vp56_rac_get_prob(c, 135);
782 } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
783 val = (vp56_rac_get_prob(c, 180) << 4) + 35;
784 val += (vp56_rac_get_prob(c, 157) << 3);
785 val += (vp56_rac_get_prob(c, 141) << 2);
786 val += (vp56_rac_get_prob(c, 134) << 1);
787 val += vp56_rac_get_prob(c, 130);
789 val = (vp56_rac_get_prob(c, 254) << 13) + 67;
790 val += (vp56_rac_get_prob(c, 254) << 12);
791 val += (vp56_rac_get_prob(c, 254) << 11);
792 val += (vp56_rac_get_prob(c, 252) << 10);
793 val += (vp56_rac_get_prob(c, 249) << 9);
794 val += (vp56_rac_get_prob(c, 243) << 8);
795 val += (vp56_rac_get_prob(c, 230) << 7);
796 val += (vp56_rac_get_prob(c, 196) << 6);
797 val += (vp56_rac_get_prob(c, 177) << 5);
798 val += (vp56_rac_get_prob(c, 153) << 4);
799 val += (vp56_rac_get_prob(c, 140) << 3);
800 val += (vp56_rac_get_prob(c, 133) << 2);
801 val += (vp56_rac_get_prob(c, 130) << 1);
802 val += vp56_rac_get_prob(c, 129);
807 band_left = band_counts[++band];
808 if (tx == TX_32X32) // FIXME slow
809 coef[rc] = ((vp8_rac_get(c) ? -val : val) * qmul[!!i]) / 2;
811 coef[rc] = (vp8_rac_get(c) ? -val : val) * qmul[!!i];
812 nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
814 } while (++i < n_coeffs);
819 static int decode_coeffs(AVCodecContext *avctx)
821 VP9Context *s = avctx->priv_data;
822 VP9Block *const b = &s->b;
823 int row = b->row, col = b->col;
824 uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
825 unsigned (*c)[6][3] = s->counts.coef[b->tx][0 /* y */][!b->intra];
826 unsigned (*e)[6][2] = s->counts.eob[b->tx][0 /* y */][!b->intra];
827 int w4 = bwh_tab[1][b->bs][0] << 1, h4 = bwh_tab[1][b->bs][1] << 1;
828 int end_x = FFMIN(2 * (s->cols - col), w4);
829 int end_y = FFMIN(2 * (s->rows - row), h4);
830 int n, pl, x, y, step1d = 1 << b->tx, step = 1 << (b->tx * 2);
831 int uvstep1d = 1 << b->uvtx, uvstep = 1 << (b->uvtx * 2), ret;
832 int16_t (*qmul)[2] = s->segmentation.feat[b->seg_id].qmul;
833 int tx = 4 * s->lossless + b->tx;
834 const int16_t **yscans = ff_vp9_scans[tx];
835 const int16_t (**ynbs)[2] = ff_vp9_scans_nb[tx];
836 const int16_t *uvscan = ff_vp9_scans[b->uvtx][DCT_DCT];
837 const int16_t (*uvnb)[2] = ff_vp9_scans_nb[b->uvtx][DCT_DCT];
838 uint8_t *a = &s->above_y_nnz_ctx[col * 2];
839 uint8_t *l = &s->left_y_nnz_ctx[(row & 7) << 1];
840 static const int16_t band_counts[4][8] = {
841 { 1, 2, 3, 4, 3, 16 - 13, 0 },
842 { 1, 2, 3, 4, 11, 64 - 21, 0 },
843 { 1, 2, 3, 4, 11, 256 - 21, 0 },
844 { 1, 2, 3, 4, 11, 1024 - 21, 0 },
846 const int16_t *y_band_counts = band_counts[b->tx];
847 const int16_t *uv_band_counts = band_counts[b->uvtx];
850 if (b->tx > TX_4X4) { // FIXME slow
851 for (y = 0; y < end_y; y += step1d)
852 for (x = 1; x < step1d; x++)
854 for (x = 0; x < end_x; x += step1d)
855 for (y = 1; y < step1d; y++)
858 for (n = 0, y = 0; y < end_y; y += step1d) {
859 for (x = 0; x < end_x; x += step1d, n += step) {
860 enum TxfmType txtp = ff_vp9_intra_txfm_type[b->mode[b->tx == TX_4X4 &&
863 int nnz = a[x] + l[y];
864 if ((ret = decode_block_coeffs(&s->c, s->block + 16 * n, 16 * step,
865 b->tx, c, e, p, nnz, yscans[txtp],
866 ynbs[txtp], y_band_counts,
871 AV_WN16A(&s->eob[n], ret);
876 if (b->tx > TX_4X4) { // FIXME slow
877 for (y = 0; y < end_y; y += step1d)
878 memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, step1d - 1));
879 for (x = 0; x < end_x; x += step1d)
880 memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, step1d - 1));
883 p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
884 c = s->counts.coef[b->uvtx][1 /* uv */][!b->intra];
885 e = s->counts.eob[b->uvtx][1 /* uv */][!b->intra];
890 for (pl = 0; pl < 2; pl++) {
891 a = &s->above_uv_nnz_ctx[pl][col];
892 l = &s->left_uv_nnz_ctx[pl][row & 7];
893 if (b->uvtx > TX_4X4) { // FIXME slow
894 for (y = 0; y < end_y; y += uvstep1d)
895 for (x = 1; x < uvstep1d; x++)
897 for (x = 0; x < end_x; x += uvstep1d)
898 for (y = 1; y < uvstep1d; y++)
901 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
902 for (x = 0; x < end_x; x += uvstep1d, n += uvstep) {
903 int nnz = a[x] + l[y];
904 if ((ret = decode_block_coeffs(&s->c, s->uvblock[pl] + 16 * n,
905 16 * uvstep, b->uvtx, c, e, p,
907 uv_band_counts, qmul[1])) < 0)
910 if (b->uvtx > TX_8X8)
911 AV_WN16A(&s->uveob[pl][n], ret);
913 s->uveob[pl][n] = ret;
916 if (b->uvtx > TX_4X4) { // FIXME slow
917 for (y = 0; y < end_y; y += uvstep1d)
918 memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, uvstep1d - 1));
919 for (x = 0; x < end_x; x += uvstep1d)
920 memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, uvstep1d - 1));
927 static av_always_inline int check_intra_mode(VP9Context *s, int mode,
930 ptrdiff_t stride_edge,
932 ptrdiff_t stride_inner,
933 uint8_t *l, int col, int x, int w,
934 int row, int y, enum TxfmMode tx,
937 int have_top = row > 0 || y > 0;
938 int have_left = col > s->tiling.tile_col_start || x > 0;
939 int have_right = x < w - 1;
940 static const uint8_t mode_conv[10][2 /* have_left */][2 /* have_top */] = {
941 [VERT_PRED] = { { DC_127_PRED, VERT_PRED },
942 { DC_127_PRED, VERT_PRED } },
943 [HOR_PRED] = { { DC_129_PRED, DC_129_PRED },
944 { HOR_PRED, HOR_PRED } },
945 [DC_PRED] = { { DC_128_PRED, TOP_DC_PRED },
946 { LEFT_DC_PRED, DC_PRED } },
947 [DIAG_DOWN_LEFT_PRED] = { { DC_127_PRED, DIAG_DOWN_LEFT_PRED },
948 { DC_127_PRED, DIAG_DOWN_LEFT_PRED } },
949 [DIAG_DOWN_RIGHT_PRED] = { { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED },
950 { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED } },
951 [VERT_RIGHT_PRED] = { { VERT_RIGHT_PRED, VERT_RIGHT_PRED },
952 { VERT_RIGHT_PRED, VERT_RIGHT_PRED } },
953 [HOR_DOWN_PRED] = { { HOR_DOWN_PRED, HOR_DOWN_PRED },
954 { HOR_DOWN_PRED, HOR_DOWN_PRED } },
955 [VERT_LEFT_PRED] = { { DC_127_PRED, VERT_LEFT_PRED },
956 { DC_127_PRED, VERT_LEFT_PRED } },
957 [HOR_UP_PRED] = { { DC_129_PRED, DC_129_PRED },
958 { HOR_UP_PRED, HOR_UP_PRED } },
959 [TM_VP8_PRED] = { { DC_129_PRED, VERT_PRED },
960 { HOR_PRED, TM_VP8_PRED } },
962 static const struct {
963 uint8_t needs_left:1;
965 uint8_t needs_topleft:1;
966 uint8_t needs_topright:1;
967 } edges[N_INTRA_PRED_MODES] = {
968 [VERT_PRED] = { .needs_top = 1 },
969 [HOR_PRED] = { .needs_left = 1 },
970 [DC_PRED] = { .needs_top = 1, .needs_left = 1 },
971 [DIAG_DOWN_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
972 [DIAG_DOWN_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
973 .needs_topleft = 1 },
974 [VERT_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
975 .needs_topleft = 1 },
976 [HOR_DOWN_PRED] = { .needs_left = 1, .needs_top = 1,
977 .needs_topleft = 1 },
978 [VERT_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
979 [HOR_UP_PRED] = { .needs_left = 1 },
980 [TM_VP8_PRED] = { .needs_left = 1, .needs_top = 1,
981 .needs_topleft = 1 },
982 [LEFT_DC_PRED] = { .needs_left = 1 },
983 [TOP_DC_PRED] = { .needs_top = 1 },
984 [DC_128_PRED] = { 0 },
985 [DC_127_PRED] = { 0 },
986 [DC_129_PRED] = { 0 }
989 av_assert2(mode >= 0 && mode < 10);
990 mode = mode_conv[mode][have_left][have_top];
991 if (edges[mode].needs_top) {
992 uint8_t *top = NULL, *topleft = NULL;
993 int n_px_need = 4 << tx, n_px_have = (((s->cols - col) << !p) - x) * 4;
994 int n_px_need_tr = 0;
996 if (tx == TX_4X4 && edges[mode].needs_topright && have_right)
999 // if top of sb64-row, use s->intra_pred_data[] instead of
1000 // dst[-stride] for intra prediction (it contains pre- instead of
1001 // post-loopfilter data)
1003 top = !(row & 7) && !y ?
1004 s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
1005 y == 0 ? &dst_edge[-stride_edge] : &dst_inner[-stride_inner];
1007 topleft = !(row & 7) && !y ?
1008 s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
1009 y == 0 || x == 0 ? &dst_edge[-stride_edge] :
1010 &dst_inner[-stride_inner];
1014 (!edges[mode].needs_topleft || (have_left && top == topleft)) &&
1015 (tx != TX_4X4 || !edges[mode].needs_topright || have_right) &&
1016 n_px_need + n_px_need_tr <= n_px_have) {
1020 if (n_px_need <= n_px_have) {
1021 memcpy(*a, top, n_px_need);
1023 memcpy(*a, top, n_px_have);
1024 memset(&(*a)[n_px_have], (*a)[n_px_have - 1],
1025 n_px_need - n_px_have);
1028 memset(*a, 127, n_px_need);
1030 if (edges[mode].needs_topleft) {
1031 if (have_left && have_top)
1032 (*a)[-1] = topleft[-1];
1034 (*a)[-1] = have_top ? 129 : 127;
1036 if (tx == TX_4X4 && edges[mode].needs_topright) {
1037 if (have_top && have_right &&
1038 n_px_need + n_px_need_tr <= n_px_have) {
1039 memcpy(&(*a)[4], &top[4], 4);
1041 memset(&(*a)[4], (*a)[3], 4);
1046 if (edges[mode].needs_left) {
1049 int n_px_need = 4 << tx;
1050 int n_px_have = (((s->rows - row) << !p) - y) * 4;
1051 uint8_t *dst = x == 0 ? dst_edge : dst_inner;
1052 ptrdiff_t stride = x == 0 ? stride_edge : stride_inner;
1054 if (n_px_need <= n_px_have) {
1055 for (i = 0; i < n_px_need; i++)
1056 l[i] = dst[i * stride - 1];
1058 for (i = 0; i < n_px_have; i++)
1059 l[i] = dst[i * stride - 1];
1060 memset(&l[i], l[i - 1], n_px_need - n_px_have);
1063 memset(l, 129, 4 << tx);
1070 static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off)
1072 VP9Context *s = avctx->priv_data;
1073 VP9Block *const b = &s->b;
1074 int row = b->row, col = b->col;
1075 int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
1076 int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
1077 int end_x = FFMIN(2 * (s->cols - col), w4);
1078 int end_y = FFMIN(2 * (s->rows - row), h4);
1079 int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
1080 int uvstep1d = 1 << b->uvtx, p;
1081 uint8_t *dst = b->dst[0], *dst_r = s->cur_frame->data[0] + y_off;
1083 for (n = 0, y = 0; y < end_y; y += step1d) {
1084 uint8_t *ptr = dst, *ptr_r = dst_r;
1085 for (x = 0; x < end_x;
1086 x += step1d, ptr += 4 * step1d, ptr_r += 4 * step1d, n += step) {
1087 int mode = b->mode[b->bs > BS_8x8 && b->tx == TX_4X4 ?
1089 LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
1090 uint8_t *a = &a_buf[16], l[32];
1091 enum TxfmType txtp = ff_vp9_intra_txfm_type[mode];
1092 int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
1094 mode = check_intra_mode(s, mode, &a, ptr_r,
1095 s->cur_frame->linesize[0],
1096 ptr, b->y_stride, l,
1097 col, x, w4, row, y, b->tx, 0);
1098 s->dsp.intra_pred[b->tx][mode](ptr, b->y_stride, l, a);
1100 s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride,
1101 s->block + 16 * n, eob);
1103 dst_r += 4 * s->cur_frame->linesize[0] * step1d;
1104 dst += 4 * b->y_stride * step1d;
1112 step = 1 << (b->uvtx * 2);
1113 for (p = 0; p < 2; p++) {
1114 dst = b->dst[1 + p];
1115 dst_r = s->cur_frame->data[1 + p] + uv_off;
1116 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
1117 uint8_t *ptr = dst, *ptr_r = dst_r;
1118 for (x = 0; x < end_x;
1119 x += uvstep1d, ptr += 4 * uvstep1d,
1120 ptr_r += 4 * uvstep1d, n += step) {
1121 int mode = b->uvmode;
1122 LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
1123 uint8_t *a = &a_buf[16], l[32];
1124 int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
1127 mode = check_intra_mode(s, mode, &a, ptr_r,
1128 s->cur_frame->linesize[1],
1129 ptr, b->uv_stride, l,
1130 col, x, w4, row, y, b->uvtx, p + 1);
1131 s->dsp.intra_pred[b->uvtx][mode](ptr, b->uv_stride, l, a);
1133 s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1134 s->uvblock[p] + 16 * n,
1137 dst_r += 4 * uvstep1d * s->cur_frame->linesize[1];
1138 dst += 4 * uvstep1d * b->uv_stride;
1143 static av_always_inline void mc_luma_dir(VP9Context *s, vp9_mc_func(*mc)[2],
1144 uint8_t *dst, ptrdiff_t dst_stride,
1146 ptrdiff_t ref_stride,
1147 ptrdiff_t y, ptrdiff_t x,
1149 int bw, int bh, int w, int h)
1151 int mx = mv->x, my = mv->y;
1155 ref += y * ref_stride + x;
1158 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
1159 if (x < !!mx * 3 || y < !!my * 3 ||
1160 x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
1161 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1162 ref - !!my * 3 * ref_stride - !!mx * 3,
1165 bw + !!mx * 7, bh + !!my * 7,
1166 x - !!mx * 3, y - !!my * 3, w, h);
1167 ref = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1170 mc[!!mx][!!my](dst, ref, dst_stride, ref_stride, bh, mx << 1, my << 1);
1173 static av_always_inline void mc_chroma_dir(VP9Context *s, vp9_mc_func(*mc)[2],
1174 uint8_t *dst_u, uint8_t *dst_v,
1175 ptrdiff_t dst_stride,
1176 const uint8_t *ref_u,
1177 ptrdiff_t src_stride_u,
1178 const uint8_t *ref_v,
1179 ptrdiff_t src_stride_v,
1180 ptrdiff_t y, ptrdiff_t x,
1182 int bw, int bh, int w, int h)
1184 int mx = mv->x, my = mv->y;
1188 ref_u += y * src_stride_u + x;
1189 ref_v += y * src_stride_v + x;
1192 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
1193 if (x < !!mx * 3 || y < !!my * 3 ||
1194 x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
1195 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1196 ref_u - !!my * 3 * src_stride_u - !!mx * 3,
1199 bw + !!mx * 7, bh + !!my * 7,
1200 x - !!mx * 3, y - !!my * 3, w, h);
1201 ref_u = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1202 mc[!!mx][!!my](dst_u, ref_u, dst_stride, 80, bh, mx, my);
1204 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1205 ref_v - !!my * 3 * src_stride_v - !!mx * 3,
1208 bw + !!mx * 7, bh + !!my * 7,
1209 x - !!mx * 3, y - !!my * 3, w, h);
1210 ref_v = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1211 mc[!!mx][!!my](dst_v, ref_v, dst_stride, 80, bh, mx, my);
1213 mc[!!mx][!!my](dst_u, ref_u, dst_stride, src_stride_u, bh, mx, my);
1214 mc[!!mx][!!my](dst_v, ref_v, dst_stride, src_stride_v, bh, mx, my);
1218 static int inter_recon(AVCodecContext *avctx)
1220 static const uint8_t bwlog_tab[2][N_BS_SIZES] = {
1221 { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
1222 { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 },
1224 VP9Context *s = avctx->priv_data;
1225 VP9Block *const b = &s->b;
1226 int row = b->row, col = b->col;
1227 AVFrame *ref1 = s->refs[s->refidx[b->ref[0]]];
1228 AVFrame *ref2 = b->comp ? s->refs[s->refidx[b->ref[1]]] : NULL;
1229 int w = avctx->width, h = avctx->height;
1230 ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride;
1232 if (!ref1->data[0] || (b->comp && !ref2->data[0]))
1233 return AVERROR_INVALIDDATA;
1236 if (b->bs > BS_8x8) {
1237 if (b->bs == BS_8x4) {
1238 mc_luma_dir(s, s->dsp.mc[3][b->filter][0], b->dst[0], ls_y,
1239 ref1->data[0], ref1->linesize[0],
1240 row << 3, col << 3, &b->mv[0][0], 8, 4, w, h);
1241 mc_luma_dir(s, s->dsp.mc[3][b->filter][0],
1242 b->dst[0] + 4 * ls_y, ls_y,
1243 ref1->data[0], ref1->linesize[0],
1244 (row << 3) + 4, col << 3, &b->mv[2][0], 8, 4, w, h);
1247 mc_luma_dir(s, s->dsp.mc[3][b->filter][1], b->dst[0], ls_y,
1248 ref2->data[0], ref2->linesize[0],
1249 row << 3, col << 3, &b->mv[0][1], 8, 4, w, h);
1250 mc_luma_dir(s, s->dsp.mc[3][b->filter][1],
1251 b->dst[0] + 4 * ls_y, ls_y,
1252 ref2->data[0], ref2->linesize[0],
1253 (row << 3) + 4, col << 3, &b->mv[2][1], 8, 4, w, h);
1255 } else if (b->bs == BS_4x8) {
1256 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
1257 ref1->data[0], ref1->linesize[0],
1258 row << 3, col << 3, &b->mv[0][0], 4, 8, w, h);
1259 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
1260 ref1->data[0], ref1->linesize[0],
1261 row << 3, (col << 3) + 4, &b->mv[1][0], 4, 8, w, h);
1264 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
1265 ref2->data[0], ref2->linesize[0],
1266 row << 3, col << 3, &b->mv[0][1], 4, 8, w, h);
1267 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
1268 ref2->data[0], ref2->linesize[0],
1269 row << 3, (col << 3) + 4, &b->mv[1][1], 4, 8, w, h);
1272 av_assert2(b->bs == BS_4x4);
1274 // FIXME if two horizontally adjacent blocks have the same MV,
1275 // do a w8 instead of a w4 call
1276 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
1277 ref1->data[0], ref1->linesize[0],
1278 row << 3, col << 3, &b->mv[0][0], 4, 4, w, h);
1279 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
1280 ref1->data[0], ref1->linesize[0],
1281 row << 3, (col << 3) + 4, &b->mv[1][0], 4, 4, w, h);
1282 mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
1283 b->dst[0] + 4 * ls_y, ls_y,
1284 ref1->data[0], ref1->linesize[0],
1285 (row << 3) + 4, col << 3, &b->mv[2][0], 4, 4, w, h);
1286 mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
1287 b->dst[0] + 4 * ls_y + 4, ls_y,
1288 ref1->data[0], ref1->linesize[0],
1289 (row << 3) + 4, (col << 3) + 4, &b->mv[3][0], 4, 4, w, h);
1292 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
1293 ref2->data[0], ref2->linesize[0],
1294 row << 3, col << 3, &b->mv[0][1], 4, 4, w, h);
1295 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
1296 ref2->data[0], ref2->linesize[0],
1297 row << 3, (col << 3) + 4, &b->mv[1][1], 4, 4, w, h);
1298 mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
1299 b->dst[0] + 4 * ls_y, ls_y,
1300 ref2->data[0], ref2->linesize[0],
1301 (row << 3) + 4, col << 3, &b->mv[2][1], 4, 4, w, h);
1302 mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
1303 b->dst[0] + 4 * ls_y + 4, ls_y,
1304 ref2->data[0], ref2->linesize[0],
1305 (row << 3) + 4, (col << 3) + 4, &b->mv[3][1], 4, 4, w, h);
1309 int bwl = bwlog_tab[0][b->bs];
1310 int bw = bwh_tab[0][b->bs][0] * 4;
1311 int bh = bwh_tab[0][b->bs][1] * 4;
1313 mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[0], ls_y,
1314 ref1->data[0], ref1->linesize[0],
1315 row << 3, col << 3, &b->mv[0][0], bw, bh, w, h);
1318 mc_luma_dir(s, s->dsp.mc[bwl][b->filter][1], b->dst[0], ls_y,
1319 ref2->data[0], ref2->linesize[0],
1320 row << 3, col << 3, &b->mv[0][1], bw, bh, w, h);
1325 int bwl = bwlog_tab[1][b->bs];
1326 int bw = bwh_tab[1][b->bs][0] * 4, bh = bwh_tab[1][b->bs][1] * 4;
1331 if (b->bs > BS_8x8) {
1332 mvuv.x = ROUNDED_DIV(b->mv[0][0].x + b->mv[1][0].x +
1333 b->mv[2][0].x + b->mv[3][0].x, 4);
1334 mvuv.y = ROUNDED_DIV(b->mv[0][0].y + b->mv[1][0].y +
1335 b->mv[2][0].y + b->mv[3][0].y, 4);
1340 mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][0],
1341 b->dst[1], b->dst[2], ls_uv,
1342 ref1->data[1], ref1->linesize[1],
1343 ref1->data[2], ref1->linesize[2],
1344 row << 2, col << 2, &mvuv, bw, bh, w, h);
1347 if (b->bs > BS_8x8) {
1348 mvuv.x = ROUNDED_DIV(b->mv[0][1].x + b->mv[1][1].x +
1349 b->mv[2][1].x + b->mv[3][1].x, 4);
1350 mvuv.y = ROUNDED_DIV(b->mv[0][1].y + b->mv[1][1].y +
1351 b->mv[2][1].y + b->mv[3][1].y, 4);
1355 mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][1],
1356 b->dst[1], b->dst[2], ls_uv,
1357 ref2->data[1], ref2->linesize[1],
1358 ref2->data[2], ref2->linesize[2],
1359 row << 2, col << 2, &mvuv, bw, bh, w, h);
1364 /* mostly copied intra_reconn() */
1366 int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
1367 int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
1368 int end_x = FFMIN(2 * (s->cols - col), w4);
1369 int end_y = FFMIN(2 * (s->rows - row), h4);
1370 int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
1371 int uvstep1d = 1 << b->uvtx, p;
1372 uint8_t *dst = b->dst[0];
1375 for (n = 0, y = 0; y < end_y; y += step1d) {
1377 for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d, n += step) {
1378 int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
1381 s->dsp.itxfm_add[tx][DCT_DCT](ptr, b->y_stride,
1382 s->block + 16 * n, eob);
1384 dst += 4 * b->y_stride * step1d;
1392 step = 1 << (b->uvtx * 2);
1393 for (p = 0; p < 2; p++) {
1394 dst = b->dst[p + 1];
1395 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
1397 for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d, n += step) {
1398 int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
1401 s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1402 s->uvblock[p] + 16 * n, eob);
1404 dst += 4 * uvstep1d * b->uv_stride;
1411 static av_always_inline void mask_edges(VP9Filter *lflvl, int is_uv,
1412 int row_and_7, int col_and_7,
1413 int w, int h, int col_end, int row_end,
1414 enum TxfmMode tx, int skip_inter)
1416 // FIXME I'm pretty sure all loops can be replaced by a single LUT if
1417 // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
1418 // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
1419 // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
1421 // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
1422 // edges. This means that for UV, we work on two subsampled blocks at
1423 // a time, and we only use the topleft block's mode information to set
1424 // things like block strength. Thus, for any block size smaller than
1425 // 16x16, ignore the odd portion of the block.
1426 if (tx == TX_4X4 && is_uv) {
1441 if (tx == TX_4X4 && !skip_inter) {
1442 int t = 1 << col_and_7, m_col = (t << w) - t, y;
1443 int m_col_odd = (t << (w - 1)) - t;
1445 // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
1447 int m_row_8 = m_col & 0x01, m_row_4 = m_col - m_row_8;
1449 for (y = row_and_7; y < h + row_and_7; y++) {
1450 int col_mask_id = 2 - !(y & 7);
1452 lflvl->mask[is_uv][0][y][1] |= m_row_8;
1453 lflvl->mask[is_uv][0][y][2] |= m_row_4;
1454 // for odd lines, if the odd col is not being filtered,
1455 // skip odd row also:
1462 // if a/c are even row/col and b/d are odd, and d is skipped,
1463 // e.g. right edge of size-66x66.webm, then skip b also (bug)
1464 if ((col_end & 1) && (y & 1)) {
1465 lflvl->mask[is_uv][1][y][col_mask_id] |= m_col_odd;
1467 lflvl->mask[is_uv][1][y][col_mask_id] |= m_col;
1471 int m_row_8 = m_col & 0x11, m_row_4 = m_col - m_row_8;
1473 for (y = row_and_7; y < h + row_and_7; y++) {
1474 int col_mask_id = 2 - !(y & 3);
1476 lflvl->mask[is_uv][0][y][1] |= m_row_8; // row edge
1477 lflvl->mask[is_uv][0][y][2] |= m_row_4;
1478 lflvl->mask[is_uv][1][y][col_mask_id] |= m_col; // col edge
1479 lflvl->mask[is_uv][0][y][3] |= m_col;
1480 lflvl->mask[is_uv][1][y][3] |= m_col;
1484 int y, t = 1 << col_and_7, m_col = (t << w) - t;
1487 int mask_id = (tx == TX_8X8);
1488 int l2 = tx + is_uv - 1, step1d = 1 << l2;
1489 static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
1490 int m_row = m_col & masks[l2];
1492 // at odd UV col/row edges tx16/tx32 loopfilter edges, force
1493 // 8wd loopfilter to prevent going off the visible edge.
1494 if (is_uv && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
1495 int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
1496 int m_row_8 = m_row - m_row_16;
1498 for (y = row_and_7; y < h + row_and_7; y++) {
1499 lflvl->mask[is_uv][0][y][0] |= m_row_16;
1500 lflvl->mask[is_uv][0][y][1] |= m_row_8;
1503 for (y = row_and_7; y < h + row_and_7; y++)
1504 lflvl->mask[is_uv][0][y][mask_id] |= m_row;
1507 if (is_uv && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
1508 for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
1509 lflvl->mask[is_uv][1][y][0] |= m_col;
1510 if (y - row_and_7 == h - 1)
1511 lflvl->mask[is_uv][1][y][1] |= m_col;
1513 for (y = row_and_7; y < h + row_and_7; y += step1d)
1514 lflvl->mask[is_uv][1][y][mask_id] |= m_col;
1516 } else if (tx != TX_4X4) {
1519 mask_id = (tx == TX_8X8) || (is_uv && h == 1);
1520 lflvl->mask[is_uv][1][row_and_7][mask_id] |= m_col;
1521 mask_id = (tx == TX_8X8) || (is_uv && w == 1);
1522 for (y = row_and_7; y < h + row_and_7; y++)
1523 lflvl->mask[is_uv][0][y][mask_id] |= t;
1525 int t8 = t & 0x01, t4 = t - t8;
1527 for (y = row_and_7; y < h + row_and_7; y++) {
1528 lflvl->mask[is_uv][0][y][2] |= t4;
1529 lflvl->mask[is_uv][0][y][1] |= t8;
1531 lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 7)] |= m_col;
1533 int t8 = t & 0x11, t4 = t - t8;
1535 for (y = row_and_7; y < h + row_and_7; y++) {
1536 lflvl->mask[is_uv][0][y][2] |= t4;
1537 lflvl->mask[is_uv][0][y][1] |= t8;
1539 lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 3)] |= m_col;
1544 int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col,
1545 VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
1546 enum BlockLevel bl, enum BlockPartition bp)
1548 VP9Context *s = avctx->priv_data;
1549 VP9Block *const b = &s->b;
1550 enum BlockSize bs = bl * 3 + bp;
1551 int ret, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl;
1559 s->min_mv.x = -(128 + col * 64);
1560 s->min_mv.y = -(128 + row * 64);
1561 s->max_mv.x = 128 + (s->cols - col - w4) * 64;
1562 s->max_mv.y = 128 + (s->rows - row - h4) * 64;
1566 b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx));
1569 if ((ret = decode_coeffs(avctx)) < 0)
1574 memset(&s->above_y_nnz_ctx[col * 2], 0, w4 * 2);
1575 memset(&s->left_y_nnz_ctx[(row & 7) << 1], 0, h4 * 2);
1576 for (pl = 0; pl < 2; pl++) {
1577 memset(&s->above_uv_nnz_ctx[pl][col], 0, w4);
1578 memset(&s->left_uv_nnz_ctx[pl][row & 7], 0, h4);
1582 /* Emulated overhangs if the stride of the target buffer can't hold.
1583 * This allows to support emu-edge and so on even if we have large
1584 * block overhangs. */
1585 emu[0] = (col + w4) * 8 > s->cur_frame->linesize[0] ||
1586 (row + h4) > s->rows + 2 * !(avctx->flags & CODEC_FLAG_EMU_EDGE);
1587 emu[1] = (col + w4) * 4 > s->cur_frame->linesize[1] ||
1588 (row + h4) > s->rows + 2 * !(avctx->flags & CODEC_FLAG_EMU_EDGE);
1590 b->dst[0] = s->tmp_y;
1593 b->dst[0] = s->cur_frame->data[0] + yoff;
1594 b->y_stride = s->cur_frame->linesize[0];
1597 b->dst[1] = s->tmp_uv[0];
1598 b->dst[2] = s->tmp_uv[1];
1601 b->dst[1] = s->cur_frame->data[1] + uvoff;
1602 b->dst[2] = s->cur_frame->data[2] + uvoff;
1603 b->uv_stride = s->cur_frame->linesize[1];
1606 intra_recon(avctx, yoff, uvoff);
1608 if ((ret = inter_recon(avctx)) < 0)
1612 int w = FFMIN(s->cols - col, w4) * 8;
1613 int h = FFMIN(s->rows - row, h4) * 8;
1616 for (n = 0; o < w; n++) {
1621 s->dsp.mc[n][0][0][0][0](s->cur_frame->data[0] + yoff + o,
1623 s->cur_frame->linesize[0],
1630 int w = FFMIN(s->cols - col, w4) * 4;
1631 int h = FFMIN(s->rows - row, h4) * 4;
1634 for (n = 1; o < w; n++) {
1639 s->dsp.mc[n][0][0][0][0](s->cur_frame->data[1] + uvoff + o,
1641 s->cur_frame->linesize[1],
1643 s->dsp.mc[n][0][0][0][0](s->cur_frame->data[2] + uvoff + o,
1645 s->cur_frame->linesize[2],
1652 // pick filter level and find edges to apply filter to
1653 if (s->filter.level &&
1654 (lvl = s->segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
1655 [b->mode[3] != ZEROMV]) > 0) {
1656 int x_end = FFMIN(s->cols - col, w4);
1657 int y_end = FFMIN(s->rows - row, h4);
1658 int skip_inter = !b->intra && b->skip;
1660 for (y = 0; y < h4; y++)
1661 memset(&lflvl->level[((row & 7) + y) * 8 + (col & 7)], lvl, w4);
1662 mask_edges(lflvl, 0, row & 7, col & 7, x_end, y_end, 0, 0, b->tx, skip_inter);
1663 mask_edges(lflvl, 1, row & 7, col & 7, x_end, y_end,
1664 s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
1665 s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
1666 b->uvtx, skip_inter);
1668 if (!s->filter.lim_lut[lvl]) {
1669 int sharp = s->filter.sharpness;
1673 limit >>= (sharp + 3) >> 2;
1674 limit = FFMIN(limit, 9 - sharp);
1676 limit = FFMAX(limit, 1);
1678 s->filter.lim_lut[lvl] = limit;
1679 s->filter.mblim_lut[lvl] = 2 * (lvl + 2) + limit;