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"
33 static const uint8_t bwh_tab[2][N_BS_SIZES][2] = {
35 { 16, 16 }, { 16, 8 }, { 8, 16 }, { 8, 8 }, { 8, 4 }, { 4, 8 },
36 { 4, 4 }, { 4, 2 }, { 2, 4 }, { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 },
38 { 8, 8 }, { 8, 4 }, { 4, 8 }, { 4, 4 }, { 4, 2 }, { 2, 4 },
39 { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 },
43 // differential forward probability updates
44 static void decode_mode(VP9Context *s, VP9Block *const b)
46 static const uint8_t left_ctx[N_BS_SIZES] = {
47 0x0, 0x8, 0x0, 0x8, 0xc, 0x8, 0xc, 0xe, 0xc, 0xe, 0xf, 0xe, 0xf
49 static const uint8_t above_ctx[N_BS_SIZES] = {
50 0x0, 0x0, 0x8, 0x8, 0x8, 0xc, 0xc, 0xc, 0xe, 0xe, 0xe, 0xf, 0xf
52 static const uint8_t max_tx_for_bl_bp[N_BS_SIZES] = {
53 TX_32X32, TX_32X32, TX_32X32, TX_32X32, TX_16X16, TX_16X16,
54 TX_16X16, TX_8X8, TX_8X8, TX_8X8, TX_4X4, TX_4X4, TX_4X4
56 int row = b->row, col = b->col, row7 = b->row7;
57 enum TxfmMode max_tx = max_tx_for_bl_bp[b->bs];
58 int w4 = FFMIN(s->cols - col, bwh_tab[1][b->bs][0]);
59 int h4 = FFMIN(s->rows - row, bwh_tab[1][b->bs][1]);
60 int have_a = row > 0, have_l = col > s->tiling.tile_col_start;
63 if (!s->segmentation.enabled) {
65 } else if (s->keyframe || s->intraonly) {
66 b->seg_id = s->segmentation.update_map ?
67 vp8_rac_get_tree(&s->c, ff_vp9_segmentation_tree, s->prob.seg) : 0;
68 } else if (!s->segmentation.update_map ||
69 (s->segmentation.temporal &&
70 vp56_rac_get_prob_branchy(&s->c,
71 s->prob.segpred[s->above_segpred_ctx[col] +
72 s->left_segpred_ctx[row7]]))) {
74 uint8_t *refsegmap = s->frames[LAST_FRAME].segmentation_map;
75 int pred = MAX_SEGMENT - 1;
78 if (!s->last_uses_2pass)
79 ff_thread_await_progress(&s->frames[LAST_FRAME].tf, row >> 3, 0);
81 for (y = 0; y < h4; y++)
82 for (x = 0; x < w4; x++)
84 refsegmap[(y + row) * 8 * s->sb_cols + x + col]);
90 memset(&s->above_segpred_ctx[col], 1, w4);
91 memset(&s->left_segpred_ctx[row7], 1, h4);
93 b->seg_id = vp8_rac_get_tree(&s->c, ff_vp9_segmentation_tree,
96 memset(&s->above_segpred_ctx[col], 0, w4);
97 memset(&s->left_segpred_ctx[row7], 0, h4);
99 if ((s->segmentation.enabled && s->segmentation.update_map) || s->keyframe) {
100 uint8_t *segmap = s->frames[CUR_FRAME].segmentation_map;
102 for (y = 0; y < h4; y++)
103 memset(&segmap[(y + row) * 8 * s->sb_cols + col],
107 b->skip = s->segmentation.enabled &&
108 s->segmentation.feat[b->seg_id].skip_enabled;
110 int c = s->left_skip_ctx[row7] + s->above_skip_ctx[col];
111 b->skip = vp56_rac_get_prob(&s->c, s->prob.p.skip[c]);
112 s->counts.skip[c][b->skip]++;
115 if (s->keyframe || s->intraonly) {
117 } else if (s->segmentation.feat[b->seg_id].ref_enabled) {
118 b->intra = !s->segmentation.feat[b->seg_id].ref_val;
122 if (have_a && have_l) {
123 c = s->above_intra_ctx[col] + s->left_intra_ctx[row7];
126 c = have_a ? 2 * s->above_intra_ctx[col] :
127 have_l ? 2 * s->left_intra_ctx[row7] : 0;
129 bit = vp56_rac_get_prob(&s->c, s->prob.p.intra[c]);
130 s->counts.intra[c][bit]++;
134 if ((b->intra || !b->skip) && s->txfmmode == TX_SWITCHABLE) {
138 c = (s->above_skip_ctx[col] ? max_tx :
139 s->above_txfm_ctx[col]) +
140 (s->left_skip_ctx[row7] ? max_tx :
141 s->left_txfm_ctx[row7]) > max_tx;
143 c = s->above_skip_ctx[col] ? 1 :
144 (s->above_txfm_ctx[col] * 2 > max_tx);
147 c = s->left_skip_ctx[row7] ? 1 :
148 (s->left_txfm_ctx[row7] * 2 > max_tx);
154 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][0]);
156 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][1]);
158 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][2]);
160 s->counts.tx32p[c][b->tx]++;
163 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][0]);
165 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][1]);
166 s->counts.tx16p[c][b->tx]++;
169 b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx8p[c]);
170 s->counts.tx8p[c][b->tx]++;
177 b->tx = FFMIN(max_tx, s->txfmmode);
180 if (s->keyframe || s->intraonly) {
181 uint8_t *a = &s->above_mode_ctx[col * 2];
182 uint8_t *l = &s->left_mode_ctx[(row7) << 1];
185 if (b->bs > BS_8x8) {
186 // FIXME the memory storage intermediates here aren't really
187 // necessary, they're just there to make the code slightly
190 a[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
191 ff_vp9_default_kf_ymode_probs[a[0]][l[0]]);
192 if (b->bs != BS_8x4) {
193 b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
194 ff_vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
200 b->mode[1] = b->mode[0];
202 if (b->bs != BS_4x8) {
204 a[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
205 ff_vp9_default_kf_ymode_probs[a[0]][l[1]]);
206 if (b->bs != BS_8x4) {
207 b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
208 ff_vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
214 b->mode[3] = b->mode[2];
217 b->mode[2] = b->mode[0];
220 b->mode[3] = b->mode[1];
223 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
224 ff_vp9_default_kf_ymode_probs[*a][*l]);
227 b->mode[1] = b->mode[0];
228 // FIXME this can probably be optimized
229 memset(a, b->mode[0], bwh_tab[0][b->bs][0]);
230 memset(l, b->mode[0], bwh_tab[0][b->bs][1]);
232 b->uvmode = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
233 ff_vp9_default_kf_uvmode_probs[b->mode[3]]);
234 } else if (b->intra) {
236 if (b->bs > BS_8x8) {
237 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
238 s->prob.p.y_mode[0]);
239 s->counts.y_mode[0][b->mode[0]]++;
240 if (b->bs != BS_8x4) {
241 b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
242 s->prob.p.y_mode[0]);
243 s->counts.y_mode[0][b->mode[1]]++;
245 b->mode[1] = b->mode[0];
247 if (b->bs != BS_4x8) {
248 b->mode[2] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
249 s->prob.p.y_mode[0]);
250 s->counts.y_mode[0][b->mode[2]]++;
251 if (b->bs != BS_8x4) {
252 b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
253 s->prob.p.y_mode[0]);
254 s->counts.y_mode[0][b->mode[3]]++;
256 b->mode[3] = b->mode[2];
259 b->mode[2] = b->mode[0];
260 b->mode[3] = b->mode[1];
263 static const uint8_t size_group[10] = {
264 3, 3, 3, 3, 2, 2, 2, 1, 1, 1
266 int sz = size_group[b->bs];
268 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
269 s->prob.p.y_mode[sz]);
272 b->mode[3] = b->mode[0];
273 s->counts.y_mode[sz][b->mode[3]]++;
275 b->uvmode = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
276 s->prob.p.uv_mode[b->mode[3]]);
277 s->counts.uv_mode[b->mode[3]][b->uvmode]++;
279 static const uint8_t inter_mode_ctx_lut[14][14] = {
280 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
281 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
282 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
283 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
284 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
285 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
286 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
287 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
288 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
289 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
290 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
291 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
292 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 3 },
293 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
296 if (s->segmentation.feat[b->seg_id].ref_enabled) {
297 av_assert2(s->segmentation.feat[b->seg_id].ref_val != 0);
299 b->ref[0] = s->segmentation.feat[b->seg_id].ref_val - 1;
301 // read comp_pred flag
302 if (s->comppredmode != PRED_SWITCHABLE) {
303 b->comp = s->comppredmode == PRED_COMPREF;
307 // FIXME add intra as ref=0xff (or -1) to make these easier?
310 if (s->above_comp_ctx[col] && s->left_comp_ctx[row7]) {
312 } else if (s->above_comp_ctx[col]) {
313 c = 2 + (s->left_intra_ctx[row7] ||
314 s->left_ref_ctx[row7] == s->fixcompref);
315 } else if (s->left_comp_ctx[row7]) {
316 c = 2 + (s->above_intra_ctx[col] ||
317 s->above_ref_ctx[col] == s->fixcompref);
319 c = (!s->above_intra_ctx[col] &&
320 s->above_ref_ctx[col] == s->fixcompref) ^
321 (!s->left_intra_ctx[row7] &&
322 s->left_ref_ctx[row & 7] == s->fixcompref);
325 c = s->above_comp_ctx[col] ? 3 :
326 (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->fixcompref);
329 c = s->left_comp_ctx[row7] ? 3 :
330 (!s->left_intra_ctx[row7] && s->left_ref_ctx[row7] == s->fixcompref);
334 b->comp = vp56_rac_get_prob(&s->c, s->prob.p.comp[c]);
335 s->counts.comp[c][b->comp]++;
338 // read actual references
339 // FIXME probably cache a few variables here to prevent repetitive
340 // memory accesses below
341 if (b->comp) { /* two references */
342 int fix_idx = s->signbias[s->fixcompref], var_idx = !fix_idx, c, bit;
344 b->ref[fix_idx] = s->fixcompref;
345 // FIXME can this codeblob be replaced by some sort of LUT?
348 if (s->above_intra_ctx[col]) {
349 if (s->left_intra_ctx[row7]) {
352 c = 1 + 2 * (s->left_ref_ctx[row7] != s->varcompref[1]);
354 } else if (s->left_intra_ctx[row7]) {
355 c = 1 + 2 * (s->above_ref_ctx[col] != s->varcompref[1]);
357 int refl = s->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
359 if (refl == refa && refa == s->varcompref[1]) {
361 } else if (!s->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
362 if ((refa == s->fixcompref && refl == s->varcompref[0]) ||
363 (refl == s->fixcompref && refa == s->varcompref[0])) {
366 c = (refa == refl) ? 3 : 1;
368 } else if (!s->left_comp_ctx[row7]) {
369 if (refa == s->varcompref[1] && refl != s->varcompref[1]) {
372 c = (refl == s->varcompref[1] &&
373 refa != s->varcompref[1]) ? 2 : 4;
375 } else if (!s->above_comp_ctx[col]) {
376 if (refl == s->varcompref[1] && refa != s->varcompref[1]) {
379 c = (refa == s->varcompref[1] &&
380 refl != s->varcompref[1]) ? 2 : 4;
383 c = (refl == refa) ? 4 : 2;
387 if (s->above_intra_ctx[col]) {
389 } else if (s->above_comp_ctx[col]) {
390 c = 4 * (s->above_ref_ctx[col] != s->varcompref[1]);
392 c = 3 * (s->above_ref_ctx[col] != s->varcompref[1]);
396 if (s->left_intra_ctx[row7]) {
398 } else if (s->left_comp_ctx[row7]) {
399 c = 4 * (s->left_ref_ctx[row7] != s->varcompref[1]);
401 c = 3 * (s->left_ref_ctx[row7] != s->varcompref[1]);
406 bit = vp56_rac_get_prob(&s->c, s->prob.p.comp_ref[c]);
407 b->ref[var_idx] = s->varcompref[bit];
408 s->counts.comp_ref[c][bit]++;
409 } else { /* single reference */
412 if (have_a && !s->above_intra_ctx[col]) {
413 if (have_l && !s->left_intra_ctx[row7]) {
414 if (s->left_comp_ctx[row7]) {
415 if (s->above_comp_ctx[col]) {
416 c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7] ||
417 !s->above_ref_ctx[col]);
419 c = (3 * !s->above_ref_ctx[col]) +
420 (!s->fixcompref || !s->left_ref_ctx[row7]);
422 } else if (s->above_comp_ctx[col]) {
423 c = (3 * !s->left_ref_ctx[row7]) +
424 (!s->fixcompref || !s->above_ref_ctx[col]);
426 c = 2 * !s->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
428 } else if (s->above_intra_ctx[col]) {
430 } else if (s->above_comp_ctx[col]) {
431 c = 1 + (!s->fixcompref || !s->above_ref_ctx[col]);
433 c = 4 * (!s->above_ref_ctx[col]);
435 } else if (have_l && !s->left_intra_ctx[row7]) {
436 if (s->left_intra_ctx[row7]) {
438 } else if (s->left_comp_ctx[row7]) {
439 c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7]);
441 c = 4 * (!s->left_ref_ctx[row7]);
446 bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][0]);
447 s->counts.single_ref[c][0][bit]++;
451 // FIXME can this codeblob be replaced by some sort of LUT?
454 if (s->left_intra_ctx[row7]) {
455 if (s->above_intra_ctx[col]) {
457 } else if (s->above_comp_ctx[col]) {
458 c = 1 + 2 * (s->fixcompref == 1 ||
459 s->above_ref_ctx[col] == 1);
460 } else if (!s->above_ref_ctx[col]) {
463 c = 4 * (s->above_ref_ctx[col] == 1);
465 } else if (s->above_intra_ctx[col]) {
466 if (s->left_intra_ctx[row7]) {
468 } else if (s->left_comp_ctx[row7]) {
469 c = 1 + 2 * (s->fixcompref == 1 ||
470 s->left_ref_ctx[row7] == 1);
471 } else if (!s->left_ref_ctx[row7]) {
474 c = 4 * (s->left_ref_ctx[row7] == 1);
476 } else if (s->above_comp_ctx[col]) {
477 if (s->left_comp_ctx[row7]) {
478 if (s->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
479 c = 3 * (s->fixcompref == 1 ||
480 s->left_ref_ctx[row7] == 1);
484 } else if (!s->left_ref_ctx[row7]) {
485 c = 1 + 2 * (s->fixcompref == 1 ||
486 s->above_ref_ctx[col] == 1);
488 c = 3 * (s->left_ref_ctx[row7] == 1) +
489 (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
491 } else if (s->left_comp_ctx[row7]) {
492 if (!s->above_ref_ctx[col]) {
493 c = 1 + 2 * (s->fixcompref == 1 ||
494 s->left_ref_ctx[row7] == 1);
496 c = 3 * (s->above_ref_ctx[col] == 1) +
497 (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
499 } else if (!s->above_ref_ctx[col]) {
500 if (!s->left_ref_ctx[row7]) {
503 c = 4 * (s->left_ref_ctx[row7] == 1);
505 } else if (!s->left_ref_ctx[row7]) {
506 c = 4 * (s->above_ref_ctx[col] == 1);
508 c = 2 * (s->left_ref_ctx[row7] == 1) +
509 2 * (s->above_ref_ctx[col] == 1);
512 if (s->above_intra_ctx[col] ||
513 (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
515 } else if (s->above_comp_ctx[col]) {
516 c = 3 * (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
518 c = 4 * (s->above_ref_ctx[col] == 1);
522 if (s->left_intra_ctx[row7] ||
523 (!s->left_comp_ctx[row7] && !s->left_ref_ctx[row7])) {
525 } else if (s->left_comp_ctx[row7]) {
526 c = 3 * (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
528 c = 4 * (s->left_ref_ctx[row7] == 1);
533 bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][1]);
534 s->counts.single_ref[c][1][bit]++;
540 if (b->bs <= BS_8x8) {
541 if (s->segmentation.feat[b->seg_id].skip_enabled) {
547 static const uint8_t off[10] = {
548 3, 0, 0, 1, 0, 0, 0, 0, 0, 0
551 // FIXME this needs to use the LUT tables from find_ref_mvs
552 // because not all are -1,0/0,-1
553 int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
554 [s->left_mode_ctx[row7 + off[b->bs]]];
556 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
557 s->prob.p.mv_mode[c]);
560 b->mode[3] = b->mode[0];
561 s->counts.mv_mode[c][b->mode[0] - 10]++;
565 if (s->filtermode == FILTER_SWITCHABLE) {
568 if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
569 if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
570 c = s->above_filter_ctx[col] == s->left_filter_ctx[row7] ?
571 s->left_filter_ctx[row7] : 3;
573 c = s->above_filter_ctx[col];
575 } else if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
576 c = s->left_filter_ctx[row7];
581 b->filter = vp8_rac_get_tree(&s->c, ff_vp9_filter_tree,
582 s->prob.p.filter[c]);
583 s->counts.filter[c][b->filter]++;
585 b->filter = s->filtermode;
588 if (b->bs > BS_8x8) {
589 int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][s->left_mode_ctx[row7]];
591 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
592 s->prob.p.mv_mode[c]);
593 s->counts.mv_mode[c][b->mode[0] - 10]++;
594 ff_vp9_fill_mv(s, b->mv[0], b->mode[0], 0);
596 if (b->bs != BS_8x4) {
597 b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
598 s->prob.p.mv_mode[c]);
599 s->counts.mv_mode[c][b->mode[1] - 10]++;
600 ff_vp9_fill_mv(s, b->mv[1], b->mode[1], 1);
602 b->mode[1] = b->mode[0];
603 AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
604 AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
607 if (b->bs != BS_4x8) {
608 b->mode[2] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
609 s->prob.p.mv_mode[c]);
610 s->counts.mv_mode[c][b->mode[2] - 10]++;
611 ff_vp9_fill_mv(s, b->mv[2], b->mode[2], 2);
613 if (b->bs != BS_8x4) {
614 b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
615 s->prob.p.mv_mode[c]);
616 s->counts.mv_mode[c][b->mode[3] - 10]++;
617 ff_vp9_fill_mv(s, b->mv[3], b->mode[3], 3);
619 b->mode[3] = b->mode[2];
620 AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
621 AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
624 b->mode[2] = b->mode[0];
625 AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
626 AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
627 b->mode[3] = b->mode[1];
628 AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
629 AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
632 ff_vp9_fill_mv(s, b->mv[0], b->mode[0], -1);
633 AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
634 AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
635 AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
636 AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
637 AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
638 AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
642 // FIXME this can probably be optimized
643 memset(&s->above_skip_ctx[col], b->skip, w4);
644 memset(&s->left_skip_ctx[row7], b->skip, h4);
645 memset(&s->above_txfm_ctx[col], b->tx, w4);
646 memset(&s->left_txfm_ctx[row7], b->tx, h4);
647 memset(&s->above_partition_ctx[col], above_ctx[b->bs], w4);
648 memset(&s->left_partition_ctx[row7], left_ctx[b->bs], h4);
649 if (!s->keyframe && !s->intraonly) {
650 memset(&s->above_intra_ctx[col], b->intra, w4);
651 memset(&s->left_intra_ctx[row7], b->intra, h4);
652 memset(&s->above_comp_ctx[col], b->comp, w4);
653 memset(&s->left_comp_ctx[row7], b->comp, h4);
654 memset(&s->above_mode_ctx[col], b->mode[3], w4);
655 memset(&s->left_mode_ctx[row7], b->mode[3], h4);
656 if (s->filtermode == FILTER_SWITCHABLE && !b->intra) {
657 memset(&s->above_filter_ctx[col], b->filter, w4);
658 memset(&s->left_filter_ctx[row7], b->filter, h4);
659 b->filter = ff_vp9_filter_lut[b->filter];
661 if (b->bs > BS_8x8) {
662 int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
664 AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
665 AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
666 AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][0], mv0);
667 AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][1], mv1);
668 AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
669 AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
670 AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
671 AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
673 int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
675 for (n = 0; n < w4 * 2; n++) {
676 AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
677 AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
679 for (n = 0; n < h4 * 2; n++) {
680 AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][0], mv0);
681 AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][1], mv1);
685 if (!b->intra) { // FIXME write 0xff or -1 if intra, so we can use this
686 // as a direct check in above branches
687 int vref = b->ref[b->comp ? s->signbias[s->varcompref[0]] : 0];
689 memset(&s->above_ref_ctx[col], vref, w4);
690 memset(&s->left_ref_ctx[row7], vref, h4);
695 for (y = 0; y < h4; y++) {
696 int x, o = (row + y) * s->sb_cols * 8 + col;
697 VP9MVRefPair *mv = &s->frames[CUR_FRAME].mv[o];
700 for (x = 0; x < w4; x++) {
704 } else if (b->comp) {
705 for (x = 0; x < w4; x++) {
706 mv[x].ref[0] = b->ref[0];
707 mv[x].ref[1] = b->ref[1];
708 AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
709 AV_COPY32(&mv[x].mv[1], &b->mv[3][1]);
712 for (x = 0; x < w4; x++) {
713 mv[x].ref[0] = b->ref[0];
715 AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
721 // FIXME remove tx argument, and merge cnt/eob arguments?
722 static int decode_block_coeffs(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
723 enum TxfmMode tx, unsigned (*cnt)[6][3],
724 unsigned (*eob)[6][2], uint8_t(*p)[6][11],
725 int nnz, const int16_t *scan,
726 const int16_t(*nb)[2],
727 const int16_t *band_counts, const int16_t *qmul)
729 int i = 0, band = 0, band_left = band_counts[band];
730 uint8_t *tp = p[0][nnz];
736 val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
737 eob[band][nnz][val]++;
742 if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
745 band_left = band_counts[++band];
747 nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
750 break; //invalid input; blocks should end with EOB
755 if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
760 // fill in p[3-10] (model fill) - only once per frame for each pos
762 memcpy(&tp[3], ff_vp9_model_pareto8[tp[2]], 8);
765 if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
766 if (!vp56_rac_get_prob_branchy(c, tp[4])) {
769 val = 3 + vp56_rac_get_prob(c, tp[5]);
772 } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
774 if (!vp56_rac_get_prob_branchy(c, tp[7])) {
775 val = vp56_rac_get_prob(c, 159) + 5;
777 val = (vp56_rac_get_prob(c, 165) << 1) + 7;
778 val += vp56_rac_get_prob(c, 145);
782 if (!vp56_rac_get_prob_branchy(c, tp[8])) {
783 if (!vp56_rac_get_prob_branchy(c, tp[9])) {
784 val = (vp56_rac_get_prob(c, 173) << 2) + 11;
785 val += (vp56_rac_get_prob(c, 148) << 1);
786 val += vp56_rac_get_prob(c, 140);
788 val = (vp56_rac_get_prob(c, 176) << 3) + 19;
789 val += (vp56_rac_get_prob(c, 155) << 2);
790 val += (vp56_rac_get_prob(c, 140) << 1);
791 val += vp56_rac_get_prob(c, 135);
793 } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
794 val = (vp56_rac_get_prob(c, 180) << 4) + 35;
795 val += (vp56_rac_get_prob(c, 157) << 3);
796 val += (vp56_rac_get_prob(c, 141) << 2);
797 val += (vp56_rac_get_prob(c, 134) << 1);
798 val += vp56_rac_get_prob(c, 130);
800 val = (vp56_rac_get_prob(c, 254) << 13) + 67;
801 val += (vp56_rac_get_prob(c, 254) << 12);
802 val += (vp56_rac_get_prob(c, 254) << 11);
803 val += (vp56_rac_get_prob(c, 252) << 10);
804 val += (vp56_rac_get_prob(c, 249) << 9);
805 val += (vp56_rac_get_prob(c, 243) << 8);
806 val += (vp56_rac_get_prob(c, 230) << 7);
807 val += (vp56_rac_get_prob(c, 196) << 6);
808 val += (vp56_rac_get_prob(c, 177) << 5);
809 val += (vp56_rac_get_prob(c, 153) << 4);
810 val += (vp56_rac_get_prob(c, 140) << 3);
811 val += (vp56_rac_get_prob(c, 133) << 2);
812 val += (vp56_rac_get_prob(c, 130) << 1);
813 val += vp56_rac_get_prob(c, 129);
818 band_left = band_counts[++band];
819 if (tx == TX_32X32) // FIXME slow
820 coef[rc] = ((vp8_rac_get(c) ? -val : val) * qmul[!!i]) / 2;
822 coef[rc] = (vp8_rac_get(c) ? -val : val) * qmul[!!i];
823 nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
825 } while (++i < n_coeffs);
830 static int decode_coeffs(AVCodecContext *avctx)
832 VP9Context *s = avctx->priv_data;
834 int row = b->row, col = b->col;
835 uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
836 unsigned (*c)[6][3] = s->counts.coef[b->tx][0 /* y */][!b->intra];
837 unsigned (*e)[6][2] = s->counts.eob[b->tx][0 /* y */][!b->intra];
838 int w4 = bwh_tab[1][b->bs][0] << 1, h4 = bwh_tab[1][b->bs][1] << 1;
839 int end_x = FFMIN(2 * (s->cols - col), w4);
840 int end_y = FFMIN(2 * (s->rows - row), h4);
841 int n, pl, x, y, step1d = 1 << b->tx, step = 1 << (b->tx * 2);
842 int uvstep1d = 1 << b->uvtx, uvstep = 1 << (b->uvtx * 2), ret;
843 int16_t (*qmul)[2] = s->segmentation.feat[b->seg_id].qmul;
844 int tx = 4 * s->lossless + b->tx;
845 const int16_t * const *yscans = ff_vp9_scans[tx];
846 const int16_t (* const * ynbs)[2] = ff_vp9_scans_nb[tx];
847 const int16_t *uvscan = ff_vp9_scans[b->uvtx][DCT_DCT];
848 const int16_t (*uvnb)[2] = ff_vp9_scans_nb[b->uvtx][DCT_DCT];
849 uint8_t *a = &s->above_y_nnz_ctx[col * 2];
850 uint8_t *l = &s->left_y_nnz_ctx[(row & 7) << 1];
851 static const int16_t band_counts[4][8] = {
852 { 1, 2, 3, 4, 3, 16 - 13, 0 },
853 { 1, 2, 3, 4, 11, 64 - 21, 0 },
854 { 1, 2, 3, 4, 11, 256 - 21, 0 },
855 { 1, 2, 3, 4, 11, 1024 - 21, 0 },
857 const int16_t *y_band_counts = band_counts[b->tx];
858 const int16_t *uv_band_counts = band_counts[b->uvtx];
861 if (b->tx > TX_4X4) { // FIXME slow
862 for (y = 0; y < end_y; y += step1d)
863 for (x = 1; x < step1d; x++)
865 for (x = 0; x < end_x; x += step1d)
866 for (y = 1; y < step1d; y++)
869 for (n = 0, y = 0; y < end_y; y += step1d) {
870 for (x = 0; x < end_x; x += step1d, n += step) {
871 enum TxfmType txtp = ff_vp9_intra_txfm_type[b->mode[b->tx == TX_4X4 &&
874 int nnz = a[x] + l[y];
875 if ((ret = decode_block_coeffs(&s->c, s->block + 16 * n, 16 * step,
876 b->tx, c, e, p, nnz, yscans[txtp],
877 ynbs[txtp], y_band_counts,
882 AV_WN16A(&s->eob[n], ret);
887 if (b->tx > TX_4X4) { // FIXME slow
888 for (y = 0; y < end_y; y += step1d)
889 memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, step1d - 1));
890 for (x = 0; x < end_x; x += step1d)
891 memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, step1d - 1));
894 p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
895 c = s->counts.coef[b->uvtx][1 /* uv */][!b->intra];
896 e = s->counts.eob[b->uvtx][1 /* uv */][!b->intra];
901 for (pl = 0; pl < 2; pl++) {
902 a = &s->above_uv_nnz_ctx[pl][col];
903 l = &s->left_uv_nnz_ctx[pl][row & 7];
904 if (b->uvtx > TX_4X4) { // FIXME slow
905 for (y = 0; y < end_y; y += uvstep1d)
906 for (x = 1; x < uvstep1d; x++)
908 for (x = 0; x < end_x; x += uvstep1d)
909 for (y = 1; y < uvstep1d; y++)
912 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
913 for (x = 0; x < end_x; x += uvstep1d, n += uvstep) {
914 int nnz = a[x] + l[y];
915 if ((ret = decode_block_coeffs(&s->c, s->uvblock[pl] + 16 * n,
916 16 * uvstep, b->uvtx, c, e, p,
918 uv_band_counts, qmul[1])) < 0)
921 if (b->uvtx > TX_8X8)
922 AV_WN16A(&s->uveob[pl][n], ret);
924 s->uveob[pl][n] = ret;
927 if (b->uvtx > TX_4X4) { // FIXME slow
928 for (y = 0; y < end_y; y += uvstep1d)
929 memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, uvstep1d - 1));
930 for (x = 0; x < end_x; x += uvstep1d)
931 memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, uvstep1d - 1));
938 static av_always_inline int check_intra_mode(VP9Context *s, int mode,
941 ptrdiff_t stride_edge,
943 ptrdiff_t stride_inner,
944 uint8_t *l, int col, int x, int w,
945 int row, int y, enum TxfmMode tx,
948 int have_top = row > 0 || y > 0;
949 int have_left = col > s->tiling.tile_col_start || x > 0;
950 int have_right = x < w - 1;
951 static const uint8_t mode_conv[10][2 /* have_left */][2 /* have_top */] = {
952 [VERT_PRED] = { { DC_127_PRED, VERT_PRED },
953 { DC_127_PRED, VERT_PRED } },
954 [HOR_PRED] = { { DC_129_PRED, DC_129_PRED },
955 { HOR_PRED, HOR_PRED } },
956 [DC_PRED] = { { DC_128_PRED, TOP_DC_PRED },
957 { LEFT_DC_PRED, DC_PRED } },
958 [DIAG_DOWN_LEFT_PRED] = { { DC_127_PRED, DIAG_DOWN_LEFT_PRED },
959 { DC_127_PRED, DIAG_DOWN_LEFT_PRED } },
960 [DIAG_DOWN_RIGHT_PRED] = { { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED },
961 { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED } },
962 [VERT_RIGHT_PRED] = { { VERT_RIGHT_PRED, VERT_RIGHT_PRED },
963 { VERT_RIGHT_PRED, VERT_RIGHT_PRED } },
964 [HOR_DOWN_PRED] = { { HOR_DOWN_PRED, HOR_DOWN_PRED },
965 { HOR_DOWN_PRED, HOR_DOWN_PRED } },
966 [VERT_LEFT_PRED] = { { DC_127_PRED, VERT_LEFT_PRED },
967 { DC_127_PRED, VERT_LEFT_PRED } },
968 [HOR_UP_PRED] = { { DC_129_PRED, DC_129_PRED },
969 { HOR_UP_PRED, HOR_UP_PRED } },
970 [TM_VP8_PRED] = { { DC_129_PRED, VERT_PRED },
971 { HOR_PRED, TM_VP8_PRED } },
973 static const struct {
974 uint8_t needs_left:1;
976 uint8_t needs_topleft:1;
977 uint8_t needs_topright:1;
978 } edges[N_INTRA_PRED_MODES] = {
979 [VERT_PRED] = { .needs_top = 1 },
980 [HOR_PRED] = { .needs_left = 1 },
981 [DC_PRED] = { .needs_top = 1, .needs_left = 1 },
982 [DIAG_DOWN_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
983 [DIAG_DOWN_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
984 .needs_topleft = 1 },
985 [VERT_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
986 .needs_topleft = 1 },
987 [HOR_DOWN_PRED] = { .needs_left = 1, .needs_top = 1,
988 .needs_topleft = 1 },
989 [VERT_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
990 [HOR_UP_PRED] = { .needs_left = 1 },
991 [TM_VP8_PRED] = { .needs_left = 1, .needs_top = 1,
992 .needs_topleft = 1 },
993 [LEFT_DC_PRED] = { .needs_left = 1 },
994 [TOP_DC_PRED] = { .needs_top = 1 },
995 [DC_128_PRED] = { 0 },
996 [DC_127_PRED] = { 0 },
997 [DC_129_PRED] = { 0 }
1000 av_assert2(mode >= 0 && mode < 10);
1001 mode = mode_conv[mode][have_left][have_top];
1002 if (edges[mode].needs_top) {
1003 uint8_t *top = NULL, *topleft = NULL;
1004 int n_px_need = 4 << tx, n_px_have = (((s->cols - col) << !p) - x) * 4;
1005 int n_px_need_tr = 0;
1007 if (tx == TX_4X4 && edges[mode].needs_topright && have_right)
1010 // if top of sb64-row, use s->intra_pred_data[] instead of
1011 // dst[-stride] for intra prediction (it contains pre- instead of
1012 // post-loopfilter data)
1014 top = !(row & 7) && !y ?
1015 s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
1016 y == 0 ? &dst_edge[-stride_edge] : &dst_inner[-stride_inner];
1018 topleft = !(row & 7) && !y ?
1019 s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
1020 y == 0 || x == 0 ? &dst_edge[-stride_edge] :
1021 &dst_inner[-stride_inner];
1025 (!edges[mode].needs_topleft || (have_left && top == topleft)) &&
1026 (tx != TX_4X4 || !edges[mode].needs_topright || have_right) &&
1027 n_px_need + n_px_need_tr <= n_px_have) {
1031 if (n_px_need <= n_px_have) {
1032 memcpy(*a, top, n_px_need);
1034 memcpy(*a, top, n_px_have);
1035 memset(&(*a)[n_px_have], (*a)[n_px_have - 1],
1036 n_px_need - n_px_have);
1039 memset(*a, 127, n_px_need);
1041 if (edges[mode].needs_topleft) {
1042 if (have_left && have_top)
1043 (*a)[-1] = topleft[-1];
1045 (*a)[-1] = have_top ? 129 : 127;
1047 if (tx == TX_4X4 && edges[mode].needs_topright) {
1048 if (have_top && have_right &&
1049 n_px_need + n_px_need_tr <= n_px_have) {
1050 memcpy(&(*a)[4], &top[4], 4);
1052 memset(&(*a)[4], (*a)[3], 4);
1057 if (edges[mode].needs_left) {
1060 int n_px_need = 4 << tx;
1061 int n_px_have = (((s->rows - row) << !p) - y) * 4;
1062 uint8_t *dst = x == 0 ? dst_edge : dst_inner;
1063 ptrdiff_t stride = x == 0 ? stride_edge : stride_inner;
1065 if (n_px_need <= n_px_have) {
1066 for (i = 0; i < n_px_need; i++)
1067 l[i] = dst[i * stride - 1];
1069 for (i = 0; i < n_px_have; i++)
1070 l[i] = dst[i * stride - 1];
1071 memset(&l[i], l[i - 1], n_px_need - n_px_have);
1074 memset(l, 129, 4 << tx);
1081 static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off)
1083 VP9Context *s = avctx->priv_data;
1085 AVFrame *f = s->frames[CUR_FRAME].tf.f;
1086 int row = b->row, col = b->col;
1087 int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
1088 int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
1089 int end_x = FFMIN(2 * (s->cols - col), w4);
1090 int end_y = FFMIN(2 * (s->rows - row), h4);
1091 int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
1092 int uvstep1d = 1 << b->uvtx, p;
1093 uint8_t *dst = b->dst[0], *dst_r = f->data[0] + y_off;
1095 for (n = 0, y = 0; y < end_y; y += step1d) {
1096 uint8_t *ptr = dst, *ptr_r = dst_r;
1097 for (x = 0; x < end_x;
1098 x += step1d, ptr += 4 * step1d, ptr_r += 4 * step1d, n += step) {
1099 int mode = b->mode[b->bs > BS_8x8 && b->tx == TX_4X4 ?
1101 LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
1102 uint8_t *a = &a_buf[16], l[32];
1103 enum TxfmType txtp = ff_vp9_intra_txfm_type[mode];
1104 int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
1106 mode = check_intra_mode(s, mode, &a, ptr_r,
1108 ptr, b->y_stride, l,
1109 col, x, w4, row, y, b->tx, 0);
1110 s->dsp.intra_pred[b->tx][mode](ptr, b->y_stride, l, a);
1112 s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride,
1113 s->block + 16 * n, eob);
1115 dst_r += 4 * f->linesize[0] * step1d;
1116 dst += 4 * b->y_stride * step1d;
1124 step = 1 << (b->uvtx * 2);
1125 for (p = 0; p < 2; p++) {
1126 dst = b->dst[1 + p];
1127 dst_r = f->data[1 + p] + uv_off;
1128 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
1129 uint8_t *ptr = dst, *ptr_r = dst_r;
1130 for (x = 0; x < end_x;
1131 x += uvstep1d, ptr += 4 * uvstep1d,
1132 ptr_r += 4 * uvstep1d, n += step) {
1133 int mode = b->uvmode;
1134 LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
1135 uint8_t *a = &a_buf[16], l[32];
1136 int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
1139 mode = check_intra_mode(s, mode, &a, ptr_r,
1141 ptr, b->uv_stride, l,
1142 col, x, w4, row, y, b->uvtx, p + 1);
1143 s->dsp.intra_pred[b->uvtx][mode](ptr, b->uv_stride, l, a);
1145 s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1146 s->uvblock[p] + 16 * n,
1149 dst_r += 4 * uvstep1d * f->linesize[1];
1150 dst += 4 * uvstep1d * b->uv_stride;
1155 static av_always_inline void mc_luma_dir(VP9Context *s, vp9_mc_func(*mc)[2],
1156 uint8_t *dst, ptrdiff_t dst_stride,
1158 ptrdiff_t ref_stride,
1159 ThreadFrame *ref_frame,
1160 ptrdiff_t y, ptrdiff_t x,
1162 int bw, int bh, int w, int h)
1164 int mx = mv->x, my = mv->y;
1169 ref += y * ref_stride + x;
1173 // we use +7 because the last 7 pixels of each sbrow can be changed in
1174 // the longest loopfilter of the next sbrow
1175 th = (y + bh + 4 * !!my + 7) >> 6;
1176 ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0);
1178 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
1179 // The arm/aarch64 _hv filters read one more row than what actually is
1180 // needed, so switch to emulated edge one pixel sooner vertically
1181 // (!!my * 5) than horizontally (!!mx * 4).
1182 if (x < !!mx * 3 || y < !!my * 3 ||
1183 x + !!mx * 4 > w - bw || y + !!my * 5 > h - bh) {
1184 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1185 ref - !!my * 3 * ref_stride - !!mx * 3,
1188 bw + !!mx * 7, bh + !!my * 7,
1189 x - !!mx * 3, y - !!my * 3, w, h);
1190 ref = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1193 mc[!!mx][!!my](dst, dst_stride, ref, ref_stride, bh, mx << 1, my << 1);
1196 static av_always_inline void mc_chroma_dir(VP9Context *s, vp9_mc_func(*mc)[2],
1197 uint8_t *dst_u, uint8_t *dst_v,
1198 ptrdiff_t dst_stride,
1199 const uint8_t *ref_u,
1200 ptrdiff_t src_stride_u,
1201 const uint8_t *ref_v,
1202 ptrdiff_t src_stride_v,
1203 ThreadFrame *ref_frame,
1204 ptrdiff_t y, ptrdiff_t x,
1206 int bw, int bh, int w, int h)
1208 int mx = mv->x, my = mv->y;
1213 ref_u += y * src_stride_u + x;
1214 ref_v += y * src_stride_v + x;
1218 // we use +7 because the last 7 pixels of each sbrow can be changed in
1219 // the longest loopfilter of the next sbrow
1220 th = (y + bh + 4 * !!my + 7) >> 5;
1221 ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0);
1223 // FIXME bilinear filter only needs 0/1 pixels, not 3/4
1224 // The arm/aarch64 _hv filters read one more row than what actually is
1225 // needed, so switch to emulated edge one pixel sooner vertically
1226 // (!!my * 5) than horizontally (!!mx * 4).
1227 if (x < !!mx * 3 || y < !!my * 3 ||
1228 x + !!mx * 4 > w - bw || y + !!my * 5 > h - bh) {
1229 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1230 ref_u - !!my * 3 * src_stride_u - !!mx * 3,
1233 bw + !!mx * 7, bh + !!my * 7,
1234 x - !!mx * 3, y - !!my * 3, w, h);
1235 ref_u = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1236 mc[!!mx][!!my](dst_u, dst_stride, ref_u, 80, bh, mx, my);
1238 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1239 ref_v - !!my * 3 * src_stride_v - !!mx * 3,
1242 bw + !!mx * 7, bh + !!my * 7,
1243 x - !!mx * 3, y - !!my * 3, w, h);
1244 ref_v = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1245 mc[!!mx][!!my](dst_v, dst_stride, ref_v, 80, bh, mx, my);
1247 mc[!!mx][!!my](dst_u, dst_stride, ref_u, src_stride_u, bh, mx, my);
1248 mc[!!mx][!!my](dst_v, dst_stride, ref_v, src_stride_v, bh, mx, my);
1252 static int inter_recon(AVCodecContext *avctx)
1254 static const uint8_t bwlog_tab[2][N_BS_SIZES] = {
1255 { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
1256 { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 },
1258 VP9Context *s = avctx->priv_data;
1260 int row = b->row, col = b->col;
1262 ThreadFrame *tref1 = &s->refs[s->refidx[b->ref[0]]];
1263 ThreadFrame *tref2 = b->comp ? &s->refs[s->refidx[b->ref[1]]] : NULL;
1264 AVFrame *ref1 = tref1->f;
1265 AVFrame *ref2 = tref2 ? tref2->f : NULL;
1267 int w = avctx->width, h = avctx->height;
1268 ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride;
1270 if (!ref1->data[0] || (b->comp && !ref2->data[0]))
1271 return AVERROR_INVALIDDATA;
1274 if (b->bs > BS_8x8) {
1275 if (b->bs == BS_8x4) {
1276 mc_luma_dir(s, s->dsp.mc[3][b->filter][0], b->dst[0], ls_y,
1277 ref1->data[0], ref1->linesize[0], tref1,
1278 row << 3, col << 3, &b->mv[0][0], 8, 4, w, h);
1279 mc_luma_dir(s, s->dsp.mc[3][b->filter][0],
1280 b->dst[0] + 4 * ls_y, ls_y,
1281 ref1->data[0], ref1->linesize[0], tref1,
1282 (row << 3) + 4, col << 3, &b->mv[2][0], 8, 4, w, h);
1285 mc_luma_dir(s, s->dsp.mc[3][b->filter][1], b->dst[0], ls_y,
1286 ref2->data[0], ref2->linesize[0], tref2,
1287 row << 3, col << 3, &b->mv[0][1], 8, 4, w, h);
1288 mc_luma_dir(s, s->dsp.mc[3][b->filter][1],
1289 b->dst[0] + 4 * ls_y, ls_y,
1290 ref2->data[0], ref2->linesize[0], tref2,
1291 (row << 3) + 4, col << 3, &b->mv[2][1], 8, 4, w, h);
1293 } else if (b->bs == BS_4x8) {
1294 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
1295 ref1->data[0], ref1->linesize[0], tref1,
1296 row << 3, col << 3, &b->mv[0][0], 4, 8, w, h);
1297 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
1298 ref1->data[0], ref1->linesize[0], tref1,
1299 row << 3, (col << 3) + 4, &b->mv[1][0], 4, 8, w, h);
1302 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
1303 ref2->data[0], ref2->linesize[0], tref2,
1304 row << 3, col << 3, &b->mv[0][1], 4, 8, w, h);
1305 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
1306 ref2->data[0], ref2->linesize[0], tref2,
1307 row << 3, (col << 3) + 4, &b->mv[1][1], 4, 8, w, h);
1310 av_assert2(b->bs == BS_4x4);
1312 // FIXME if two horizontally adjacent blocks have the same MV,
1313 // do a w8 instead of a w4 call
1314 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
1315 ref1->data[0], ref1->linesize[0], tref1,
1316 row << 3, col << 3, &b->mv[0][0], 4, 4, w, h);
1317 mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
1318 ref1->data[0], ref1->linesize[0], tref1,
1319 row << 3, (col << 3) + 4, &b->mv[1][0], 4, 4, w, h);
1320 mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
1321 b->dst[0] + 4 * ls_y, ls_y,
1322 ref1->data[0], ref1->linesize[0], tref1,
1323 (row << 3) + 4, col << 3, &b->mv[2][0], 4, 4, w, h);
1324 mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
1325 b->dst[0] + 4 * ls_y + 4, ls_y,
1326 ref1->data[0], ref1->linesize[0], tref1,
1327 (row << 3) + 4, (col << 3) + 4, &b->mv[3][0], 4, 4, w, h);
1330 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
1331 ref2->data[0], ref2->linesize[0], tref2,
1332 row << 3, col << 3, &b->mv[0][1], 4, 4, w, h);
1333 mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
1334 ref2->data[0], ref2->linesize[0], tref2,
1335 row << 3, (col << 3) + 4, &b->mv[1][1], 4, 4, w, h);
1336 mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
1337 b->dst[0] + 4 * ls_y, ls_y,
1338 ref2->data[0], ref2->linesize[0], tref2,
1339 (row << 3) + 4, col << 3, &b->mv[2][1], 4, 4, w, h);
1340 mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
1341 b->dst[0] + 4 * ls_y + 4, ls_y,
1342 ref2->data[0], ref2->linesize[0], tref2,
1343 (row << 3) + 4, (col << 3) + 4, &b->mv[3][1], 4, 4, w, h);
1347 int bwl = bwlog_tab[0][b->bs];
1348 int bw = bwh_tab[0][b->bs][0] * 4;
1349 int bh = bwh_tab[0][b->bs][1] * 4;
1351 mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[0], ls_y,
1352 ref1->data[0], ref1->linesize[0], tref1,
1353 row << 3, col << 3, &b->mv[0][0], bw, bh, w, h);
1356 mc_luma_dir(s, s->dsp.mc[bwl][b->filter][1], b->dst[0], ls_y,
1357 ref2->data[0], ref2->linesize[0], tref2,
1358 row << 3, col << 3, &b->mv[0][1], bw, bh, w, h);
1363 int bwl = bwlog_tab[1][b->bs];
1364 int bw = bwh_tab[1][b->bs][0] * 4, bh = bwh_tab[1][b->bs][1] * 4;
1369 if (b->bs > BS_8x8) {
1370 mvuv.x = ROUNDED_DIV(b->mv[0][0].x + b->mv[1][0].x +
1371 b->mv[2][0].x + b->mv[3][0].x, 4);
1372 mvuv.y = ROUNDED_DIV(b->mv[0][0].y + b->mv[1][0].y +
1373 b->mv[2][0].y + b->mv[3][0].y, 4);
1378 mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][0],
1379 b->dst[1], b->dst[2], ls_uv,
1380 ref1->data[1], ref1->linesize[1],
1381 ref1->data[2], ref1->linesize[2], tref1,
1382 row << 2, col << 2, &mvuv, bw, bh, w, h);
1385 if (b->bs > BS_8x8) {
1386 mvuv.x = ROUNDED_DIV(b->mv[0][1].x + b->mv[1][1].x +
1387 b->mv[2][1].x + b->mv[3][1].x, 4);
1388 mvuv.y = ROUNDED_DIV(b->mv[0][1].y + b->mv[1][1].y +
1389 b->mv[2][1].y + b->mv[3][1].y, 4);
1393 mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][1],
1394 b->dst[1], b->dst[2], ls_uv,
1395 ref2->data[1], ref2->linesize[1],
1396 ref2->data[2], ref2->linesize[2], tref2,
1397 row << 2, col << 2, &mvuv, bw, bh, w, h);
1402 /* mostly copied intra_reconn() */
1404 int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
1405 int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
1406 int end_x = FFMIN(2 * (s->cols - col), w4);
1407 int end_y = FFMIN(2 * (s->rows - row), h4);
1408 int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
1409 int uvstep1d = 1 << b->uvtx, p;
1410 uint8_t *dst = b->dst[0];
1413 for (n = 0, y = 0; y < end_y; y += step1d) {
1415 for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d, n += step) {
1416 int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
1419 s->dsp.itxfm_add[tx][DCT_DCT](ptr, b->y_stride,
1420 s->block + 16 * n, eob);
1422 dst += 4 * b->y_stride * step1d;
1430 step = 1 << (b->uvtx * 2);
1431 for (p = 0; p < 2; p++) {
1432 dst = b->dst[p + 1];
1433 for (n = 0, y = 0; y < end_y; y += uvstep1d) {
1435 for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d, n += step) {
1436 int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
1439 s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1440 s->uvblock[p] + 16 * n, eob);
1442 dst += 4 * uvstep1d * b->uv_stride;
1449 static av_always_inline void mask_edges(VP9Filter *lflvl, int is_uv,
1450 int row_and_7, int col_and_7,
1451 int w, int h, int col_end, int row_end,
1452 enum TxfmMode tx, int skip_inter)
1454 // FIXME I'm pretty sure all loops can be replaced by a single LUT if
1455 // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
1456 // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
1457 // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
1459 // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
1460 // edges. This means that for UV, we work on two subsampled blocks at
1461 // a time, and we only use the topleft block's mode information to set
1462 // things like block strength. Thus, for any block size smaller than
1463 // 16x16, ignore the odd portion of the block.
1464 if (tx == TX_4X4 && is_uv) {
1479 if (tx == TX_4X4 && !skip_inter) {
1480 int t = 1 << col_and_7, m_col = (t << w) - t, y;
1481 int m_col_odd = (t << (w - 1)) - t;
1483 // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
1485 int m_row_8 = m_col & 0x01, m_row_4 = m_col - m_row_8;
1487 for (y = row_and_7; y < h + row_and_7; y++) {
1488 int col_mask_id = 2 - !(y & 7);
1490 lflvl->mask[is_uv][0][y][1] |= m_row_8;
1491 lflvl->mask[is_uv][0][y][2] |= m_row_4;
1492 // for odd lines, if the odd col is not being filtered,
1493 // skip odd row also:
1500 // if a/c are even row/col and b/d are odd, and d is skipped,
1501 // e.g. right edge of size-66x66.webm, then skip b also (bug)
1502 if ((col_end & 1) && (y & 1)) {
1503 lflvl->mask[is_uv][1][y][col_mask_id] |= m_col_odd;
1505 lflvl->mask[is_uv][1][y][col_mask_id] |= m_col;
1509 int m_row_8 = m_col & 0x11, m_row_4 = m_col - m_row_8;
1511 for (y = row_and_7; y < h + row_and_7; y++) {
1512 int col_mask_id = 2 - !(y & 3);
1514 lflvl->mask[is_uv][0][y][1] |= m_row_8; // row edge
1515 lflvl->mask[is_uv][0][y][2] |= m_row_4;
1516 lflvl->mask[is_uv][1][y][col_mask_id] |= m_col; // col edge
1517 lflvl->mask[is_uv][0][y][3] |= m_col;
1518 lflvl->mask[is_uv][1][y][3] |= m_col;
1522 int y, t = 1 << col_and_7, m_col = (t << w) - t;
1525 int mask_id = (tx == TX_8X8);
1526 int l2 = tx + is_uv - 1, step1d = 1 << l2;
1527 static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
1528 int m_row = m_col & masks[l2];
1530 // at odd UV col/row edges tx16/tx32 loopfilter edges, force
1531 // 8wd loopfilter to prevent going off the visible edge.
1532 if (is_uv && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
1533 int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
1534 int m_row_8 = m_row - m_row_16;
1536 for (y = row_and_7; y < h + row_and_7; y++) {
1537 lflvl->mask[is_uv][0][y][0] |= m_row_16;
1538 lflvl->mask[is_uv][0][y][1] |= m_row_8;
1541 for (y = row_and_7; y < h + row_and_7; y++)
1542 lflvl->mask[is_uv][0][y][mask_id] |= m_row;
1545 if (is_uv && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
1546 for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
1547 lflvl->mask[is_uv][1][y][0] |= m_col;
1548 if (y - row_and_7 == h - 1)
1549 lflvl->mask[is_uv][1][y][1] |= m_col;
1551 for (y = row_and_7; y < h + row_and_7; y += step1d)
1552 lflvl->mask[is_uv][1][y][mask_id] |= m_col;
1554 } else if (tx != TX_4X4) {
1557 mask_id = (tx == TX_8X8) || (is_uv && h == 1);
1558 lflvl->mask[is_uv][1][row_and_7][mask_id] |= m_col;
1559 mask_id = (tx == TX_8X8) || (is_uv && w == 1);
1560 for (y = row_and_7; y < h + row_and_7; y++)
1561 lflvl->mask[is_uv][0][y][mask_id] |= t;
1563 int t8 = t & 0x01, t4 = t - t8;
1565 for (y = row_and_7; y < h + row_and_7; y++) {
1566 lflvl->mask[is_uv][0][y][2] |= t4;
1567 lflvl->mask[is_uv][0][y][1] |= t8;
1569 lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 7)] |= m_col;
1571 int t8 = t & 0x11, t4 = t - t8;
1573 for (y = row_and_7; y < h + row_and_7; y++) {
1574 lflvl->mask[is_uv][0][y][2] |= t4;
1575 lflvl->mask[is_uv][0][y][1] |= t8;
1577 lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 3)] |= m_col;
1582 int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col,
1583 VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
1584 enum BlockLevel bl, enum BlockPartition bp)
1586 VP9Context *s = avctx->priv_data;
1588 AVFrame *f = s->frames[CUR_FRAME].tf.f;
1589 enum BlockSize bs = bl * 3 + bp;
1590 int ret, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl;
1598 s->min_mv.x = -(128 + col * 64);
1599 s->min_mv.y = -(128 + row * 64);
1600 s->max_mv.x = 128 + (s->cols - col - w4) * 64;
1601 s->max_mv.y = 128 + (s->rows - row - h4) * 64;
1608 b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx));
1611 if ((ret = decode_coeffs(avctx)) < 0)
1616 memset(&s->above_y_nnz_ctx[col * 2], 0, w4 * 2);
1617 memset(&s->left_y_nnz_ctx[(row & 7) << 1], 0, h4 * 2);
1618 for (pl = 0; pl < 2; pl++) {
1619 memset(&s->above_uv_nnz_ctx[pl][col], 0, w4);
1620 memset(&s->left_uv_nnz_ctx[pl][row & 7], 0, h4);
1626 s->block += w4 * h4 * 64;
1627 s->uvblock[0] += w4 * h4 * 16;
1628 s->uvblock[1] += w4 * h4 * 16;
1629 s->eob += w4 * h4 * 4;
1630 s->uveob[0] += w4 * h4;
1631 s->uveob[1] += w4 * h4;
1637 /* Emulated overhangs if the stride of the target buffer can't hold.
1638 * This allows to support emu-edge and so on even if we have large
1639 * block overhangs. */
1640 emu[0] = (col + w4) * 8 > f->linesize[0] ||
1641 (row + h4) > s->rows;
1642 emu[1] = (col + w4) * 4 > f->linesize[1] ||
1643 (row + h4) > s->rows;
1645 b->dst[0] = s->tmp_y;
1648 b->dst[0] = f->data[0] + yoff;
1649 b->y_stride = f->linesize[0];
1652 b->dst[1] = s->tmp_uv[0];
1653 b->dst[2] = s->tmp_uv[1];
1656 b->dst[1] = f->data[1] + uvoff;
1657 b->dst[2] = f->data[2] + uvoff;
1658 b->uv_stride = f->linesize[1];
1661 intra_recon(avctx, yoff, uvoff);
1663 if ((ret = inter_recon(avctx)) < 0)
1667 int w = FFMIN(s->cols - col, w4) * 8;
1668 int h = FFMIN(s->rows - row, h4) * 8;
1671 for (n = 0; o < w; n++) {
1676 s->dsp.mc[n][0][0][0][0](f->data[0] + yoff + o,
1685 int w = FFMIN(s->cols - col, w4) * 4;
1686 int h = FFMIN(s->rows - row, h4) * 4;
1689 for (n = 1; o < w; n++) {
1694 s->dsp.mc[n][0][0][0][0](f->data[1] + uvoff + o,
1698 s->dsp.mc[n][0][0][0][0](f->data[2] + uvoff + o,
1707 // pick filter level and find edges to apply filter to
1708 if (s->filter.level &&
1709 (lvl = s->segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
1710 [b->mode[3] != ZEROMV]) > 0) {
1711 int x_end = FFMIN(s->cols - col, w4);
1712 int y_end = FFMIN(s->rows - row, h4);
1713 int skip_inter = !b->intra && b->skip;
1715 for (y = 0; y < h4; y++)
1716 memset(&lflvl->level[((row & 7) + y) * 8 + (col & 7)], lvl, w4);
1717 mask_edges(lflvl, 0, row & 7, col & 7, x_end, y_end, 0, 0, b->tx, skip_inter);
1718 mask_edges(lflvl, 1, row & 7, col & 7, x_end, y_end,
1719 s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
1720 s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
1721 b->uvtx, skip_inter);
1723 if (!s->filter.lim_lut[lvl]) {
1724 int sharp = s->filter.sharpness;
1728 limit >>= (sharp + 3) >> 2;
1729 limit = FFMIN(limit, 9 - sharp);
1731 limit = FFMAX(limit, 1);
1733 s->filter.lim_lut[lvl] = limit;
1734 s->filter.mblim_lut[lvl] = 2 * (lvl + 2) + limit;
1740 s->block += w4 * h4 * 64;
1741 s->uvblock[0] += w4 * h4 * 16;
1742 s->uvblock[1] += w4 * h4 * 16;
1743 s->eob += w4 * h4 * 4;
1744 s->uveob[0] += w4 * h4;
1745 s->uveob[1] += w4 * h4;