Libav
vp9block.c
Go to the documentation of this file.
1 /*
2  * VP9 compatible video decoder
3  *
4  * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5  * Copyright (C) 2013 Clément Bœsch <u pkh me>
6  *
7  * This file is part of Libav.
8  *
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.
13  *
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.
18  *
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
22  */
23 
24 #include "libavutil/avassert.h"
25 
26 #include "avcodec.h"
27 #include "internal.h"
28 #include "videodsp.h"
29 #include "vp56.h"
30 #include "vp9.h"
31 #include "vp9data.h"
32 
33 static const uint8_t bwh_tab[2][N_BS_SIZES][2] = {
34  {
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 },
37  }, {
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 },
40  }
41 };
42 
43 // differential forward probability updates
44 static void decode_mode(VP9Context *s, VP9Block *const b)
45 {
46  static const uint8_t left_ctx[N_BS_SIZES] = {
47  0x0, 0x8, 0x0, 0x8, 0xc, 0x8, 0xc, 0xe, 0xc, 0xe, 0xf, 0xe, 0xf
48  };
49  static const uint8_t above_ctx[N_BS_SIZES] = {
50  0x0, 0x0, 0x8, 0x8, 0x8, 0xc, 0xc, 0xc, 0xe, 0xe, 0xe, 0xf, 0xf
51  };
52  static const uint8_t max_tx_for_bl_bp[N_BS_SIZES] = {
55  };
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;
61  int y;
62 
63  if (!s->segmentation.enabled) {
64  b->seg_id = 0;
65  } else if (s->keyframe || s->intraonly) {
68  } else if (!s->segmentation.update_map ||
69  (s->segmentation.temporal &&
71  s->prob.segpred[s->above_segpred_ctx[col] +
72  s->left_segpred_ctx[row7]]))) {
73  uint8_t *refsegmap = s->frames[LAST_FRAME].segmentation_map;
74  int pred = MAX_SEGMENT - 1;
75  int x;
76 
77  if (!s->last_uses_2pass)
78  ff_thread_await_progress(&s->frames[LAST_FRAME].tf, row >> 3, 0);
79 
80  for (y = 0; y < h4; y++)
81  for (x = 0; x < w4; x++)
82  pred = FFMIN(pred,
83  refsegmap[(y + row) * 8 * s->sb_cols + x + col]);
84  b->seg_id = pred;
85 
86  memset(&s->above_segpred_ctx[col], 1, w4);
87  memset(&s->left_segpred_ctx[row7], 1, h4);
88  } else {
90  s->prob.seg);
91 
92  memset(&s->above_segpred_ctx[col], 0, w4);
93  memset(&s->left_segpred_ctx[row7], 0, h4);
94  }
95  if ((s->segmentation.enabled && s->segmentation.update_map) || s->keyframe) {
97 
98  for (y = 0; y < h4; y++)
99  memset(&segmap[(y + row) * 8 * s->sb_cols + col],
100  b->seg_id, w4);
101  }
102 
103  b->skip = s->segmentation.enabled &&
104  s->segmentation.feat[b->seg_id].skip_enabled;
105  if (!b->skip) {
106  int c = s->left_skip_ctx[row7] + s->above_skip_ctx[col];
107  b->skip = vp56_rac_get_prob(&s->c, s->prob.p.skip[c]);
108  s->counts.skip[c][b->skip]++;
109  }
110 
111  if (s->keyframe || s->intraonly) {
112  b->intra = 1;
113  } else if (s->segmentation.feat[b->seg_id].ref_enabled) {
114  b->intra = !s->segmentation.feat[b->seg_id].ref_val;
115  } else {
116  int c, bit;
117 
118  if (have_a && have_l) {
119  c = s->above_intra_ctx[col] + s->left_intra_ctx[row7];
120  c += (c == 2);
121  } else {
122  c = have_a ? 2 * s->above_intra_ctx[col] :
123  have_l ? 2 * s->left_intra_ctx[row7] : 0;
124  }
125  bit = vp56_rac_get_prob(&s->c, s->prob.p.intra[c]);
126  s->counts.intra[c][bit]++;
127  b->intra = !bit;
128  }
129 
130  if ((b->intra || !b->skip) && s->txfmmode == TX_SWITCHABLE) {
131  int c;
132  if (have_a) {
133  if (have_l) {
134  c = (s->above_skip_ctx[col] ? max_tx :
135  s->above_txfm_ctx[col]) +
136  (s->left_skip_ctx[row7] ? max_tx :
137  s->left_txfm_ctx[row7]) > max_tx;
138  } else {
139  c = s->above_skip_ctx[col] ? 1 :
140  (s->above_txfm_ctx[col] * 2 > max_tx);
141  }
142  } else if (have_l) {
143  c = s->left_skip_ctx[row7] ? 1 :
144  (s->left_txfm_ctx[row7] * 2 > max_tx);
145  } else {
146  c = 1;
147  }
148  switch (max_tx) {
149  case TX_32X32:
150  b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][0]);
151  if (b->tx) {
152  b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][1]);
153  if (b->tx == 2)
154  b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][2]);
155  }
156  s->counts.tx32p[c][b->tx]++;
157  break;
158  case TX_16X16:
159  b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][0]);
160  if (b->tx)
161  b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][1]);
162  s->counts.tx16p[c][b->tx]++;
163  break;
164  case TX_8X8:
165  b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx8p[c]);
166  s->counts.tx8p[c][b->tx]++;
167  break;
168  case TX_4X4:
169  b->tx = TX_4X4;
170  break;
171  }
172  } else {
173  b->tx = FFMIN(max_tx, s->txfmmode);
174  }
175 
176  if (s->keyframe || s->intraonly) {
177  uint8_t *a = &s->above_mode_ctx[col * 2];
178  uint8_t *l = &s->left_mode_ctx[(row7) << 1];
179 
180  b->comp = 0;
181  if (b->bs > BS_8x8) {
182  // FIXME the memory storage intermediates here aren't really
183  // necessary, they're just there to make the code slightly
184  // simpler for now
185  b->mode[0] =
187  ff_vp9_default_kf_ymode_probs[a[0]][l[0]]);
188  if (b->bs != BS_8x4) {
190  ff_vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
191  l[0] =
192  a[1] = b->mode[1];
193  } else {
194  l[0] =
195  a[1] =
196  b->mode[1] = b->mode[0];
197  }
198  if (b->bs != BS_4x8) {
199  b->mode[2] =
201  ff_vp9_default_kf_ymode_probs[a[0]][l[1]]);
202  if (b->bs != BS_8x4) {
204  ff_vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
205  l[1] =
206  a[1] = b->mode[3];
207  } else {
208  l[1] =
209  a[1] =
210  b->mode[3] = b->mode[2];
211  }
212  } else {
213  b->mode[2] = b->mode[0];
214  l[1] =
215  a[1] =
216  b->mode[3] = b->mode[1];
217  }
218  } else {
221  b->mode[3] =
222  b->mode[2] =
223  b->mode[1] = b->mode[0];
224  // FIXME this can probably be optimized
225  memset(a, b->mode[0], bwh_tab[0][b->bs][0]);
226  memset(l, b->mode[0], bwh_tab[0][b->bs][1]);
227  }
230  } else if (b->intra) {
231  b->comp = 0;
232  if (b->bs > BS_8x8) {
234  s->prob.p.y_mode[0]);
235  s->counts.y_mode[0][b->mode[0]]++;
236  if (b->bs != BS_8x4) {
238  s->prob.p.y_mode[0]);
239  s->counts.y_mode[0][b->mode[1]]++;
240  } else {
241  b->mode[1] = b->mode[0];
242  }
243  if (b->bs != BS_4x8) {
245  s->prob.p.y_mode[0]);
246  s->counts.y_mode[0][b->mode[2]]++;
247  if (b->bs != BS_8x4) {
249  s->prob.p.y_mode[0]);
250  s->counts.y_mode[0][b->mode[3]]++;
251  } else {
252  b->mode[3] = b->mode[2];
253  }
254  } else {
255  b->mode[2] = b->mode[0];
256  b->mode[3] = b->mode[1];
257  }
258  } else {
259  static const uint8_t size_group[10] = {
260  3, 3, 3, 3, 2, 2, 2, 1, 1, 1
261  };
262  int sz = size_group[b->bs];
263 
265  s->prob.p.y_mode[sz]);
266  b->mode[1] =
267  b->mode[2] =
268  b->mode[3] = b->mode[0];
269  s->counts.y_mode[sz][b->mode[3]]++;
270  }
272  s->prob.p.uv_mode[b->mode[3]]);
273  s->counts.uv_mode[b->mode[3]][b->uvmode]++;
274  } else {
275  static const uint8_t inter_mode_ctx_lut[14][14] = {
276  { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
277  { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
278  { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
279  { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
280  { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
281  { 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  { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
287  { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
288  { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 3 },
289  { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
290  };
291 
292  if (s->segmentation.feat[b->seg_id].ref_enabled) {
293  av_assert2(s->segmentation.feat[b->seg_id].ref_val != 0);
294  b->comp = 0;
295  b->ref[0] = s->segmentation.feat[b->seg_id].ref_val - 1;
296  } else {
297  // read comp_pred flag
298  if (s->comppredmode != PRED_SWITCHABLE) {
299  b->comp = s->comppredmode == PRED_COMPREF;
300  } else {
301  int c;
302 
303  // FIXME add intra as ref=0xff (or -1) to make these easier?
304  if (have_a) {
305  if (have_l) {
306  if (s->above_comp_ctx[col] && s->left_comp_ctx[row7]) {
307  c = 4;
308  } else if (s->above_comp_ctx[col]) {
309  c = 2 + (s->left_intra_ctx[row7] ||
310  s->left_ref_ctx[row7] == s->fixcompref);
311  } else if (s->left_comp_ctx[row7]) {
312  c = 2 + (s->above_intra_ctx[col] ||
313  s->above_ref_ctx[col] == s->fixcompref);
314  } else {
315  c = (!s->above_intra_ctx[col] &&
316  s->above_ref_ctx[col] == s->fixcompref) ^
317  (!s->left_intra_ctx[row7] &&
318  s->left_ref_ctx[row & 7] == s->fixcompref);
319  }
320  } else {
321  c = s->above_comp_ctx[col] ? 3 :
322  (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->fixcompref);
323  }
324  } else if (have_l) {
325  c = s->left_comp_ctx[row7] ? 3 :
326  (!s->left_intra_ctx[row7] && s->left_ref_ctx[row7] == s->fixcompref);
327  } else {
328  c = 1;
329  }
330  b->comp = vp56_rac_get_prob(&s->c, s->prob.p.comp[c]);
331  s->counts.comp[c][b->comp]++;
332  }
333 
334  // read actual references
335  // FIXME probably cache a few variables here to prevent repetitive
336  // memory accesses below
337  if (b->comp) { /* two references */
338  int fix_idx = s->signbias[s->fixcompref], var_idx = !fix_idx, c, bit;
339 
340  b->ref[fix_idx] = s->fixcompref;
341  // FIXME can this codeblob be replaced by some sort of LUT?
342  if (have_a) {
343  if (have_l) {
344  if (s->above_intra_ctx[col]) {
345  if (s->left_intra_ctx[row7]) {
346  c = 2;
347  } else {
348  c = 1 + 2 * (s->left_ref_ctx[row7] != s->varcompref[1]);
349  }
350  } else if (s->left_intra_ctx[row7]) {
351  c = 1 + 2 * (s->above_ref_ctx[col] != s->varcompref[1]);
352  } else {
353  int refl = s->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
354 
355  if (refl == refa && refa == s->varcompref[1]) {
356  c = 0;
357  } else if (!s->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
358  if ((refa == s->fixcompref && refl == s->varcompref[0]) ||
359  (refl == s->fixcompref && refa == s->varcompref[0])) {
360  c = 4;
361  } else {
362  c = (refa == refl) ? 3 : 1;
363  }
364  } else if (!s->left_comp_ctx[row7]) {
365  if (refa == s->varcompref[1] && refl != s->varcompref[1]) {
366  c = 1;
367  } else {
368  c = (refl == s->varcompref[1] &&
369  refa != s->varcompref[1]) ? 2 : 4;
370  }
371  } else if (!s->above_comp_ctx[col]) {
372  if (refl == s->varcompref[1] && refa != s->varcompref[1]) {
373  c = 1;
374  } else {
375  c = (refa == s->varcompref[1] &&
376  refl != s->varcompref[1]) ? 2 : 4;
377  }
378  } else {
379  c = (refl == refa) ? 4 : 2;
380  }
381  }
382  } else {
383  if (s->above_intra_ctx[col]) {
384  c = 2;
385  } else if (s->above_comp_ctx[col]) {
386  c = 4 * (s->above_ref_ctx[col] != s->varcompref[1]);
387  } else {
388  c = 3 * (s->above_ref_ctx[col] != s->varcompref[1]);
389  }
390  }
391  } else if (have_l) {
392  if (s->left_intra_ctx[row7]) {
393  c = 2;
394  } else if (s->left_comp_ctx[row7]) {
395  c = 4 * (s->left_ref_ctx[row7] != s->varcompref[1]);
396  } else {
397  c = 3 * (s->left_ref_ctx[row7] != s->varcompref[1]);
398  }
399  } else {
400  c = 2;
401  }
402  bit = vp56_rac_get_prob(&s->c, s->prob.p.comp_ref[c]);
403  b->ref[var_idx] = s->varcompref[bit];
404  s->counts.comp_ref[c][bit]++;
405  } else { /* single reference */
406  int bit, c;
407 
408  if (have_a && !s->above_intra_ctx[col]) {
409  if (have_l && !s->left_intra_ctx[row7]) {
410  if (s->left_comp_ctx[row7]) {
411  if (s->above_comp_ctx[col]) {
412  c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7] ||
413  !s->above_ref_ctx[col]);
414  } else {
415  c = (3 * !s->above_ref_ctx[col]) +
416  (!s->fixcompref || !s->left_ref_ctx[row7]);
417  }
418  } else if (s->above_comp_ctx[col]) {
419  c = (3 * !s->left_ref_ctx[row7]) +
420  (!s->fixcompref || !s->above_ref_ctx[col]);
421  } else {
422  c = 2 * !s->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
423  }
424  } else if (s->above_intra_ctx[col]) {
425  c = 2;
426  } else if (s->above_comp_ctx[col]) {
427  c = 1 + (!s->fixcompref || !s->above_ref_ctx[col]);
428  } else {
429  c = 4 * (!s->above_ref_ctx[col]);
430  }
431  } else if (have_l && !s->left_intra_ctx[row7]) {
432  if (s->left_intra_ctx[row7]) {
433  c = 2;
434  } else if (s->left_comp_ctx[row7]) {
435  c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7]);
436  } else {
437  c = 4 * (!s->left_ref_ctx[row7]);
438  }
439  } else {
440  c = 2;
441  }
442  bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][0]);
443  s->counts.single_ref[c][0][bit]++;
444  if (!bit) {
445  b->ref[0] = 0;
446  } else {
447  // FIXME can this codeblob be replaced by some sort of LUT?
448  if (have_a) {
449  if (have_l) {
450  if (s->left_intra_ctx[row7]) {
451  if (s->above_intra_ctx[col]) {
452  c = 2;
453  } else if (s->above_comp_ctx[col]) {
454  c = 1 + 2 * (s->fixcompref == 1 ||
455  s->above_ref_ctx[col] == 1);
456  } else if (!s->above_ref_ctx[col]) {
457  c = 3;
458  } else {
459  c = 4 * (s->above_ref_ctx[col] == 1);
460  }
461  } else if (s->above_intra_ctx[col]) {
462  if (s->left_intra_ctx[row7]) {
463  c = 2;
464  } else if (s->left_comp_ctx[row7]) {
465  c = 1 + 2 * (s->fixcompref == 1 ||
466  s->left_ref_ctx[row7] == 1);
467  } else if (!s->left_ref_ctx[row7]) {
468  c = 3;
469  } else {
470  c = 4 * (s->left_ref_ctx[row7] == 1);
471  }
472  } else if (s->above_comp_ctx[col]) {
473  if (s->left_comp_ctx[row7]) {
474  if (s->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
475  c = 3 * (s->fixcompref == 1 ||
476  s->left_ref_ctx[row7] == 1);
477  } else {
478  c = 2;
479  }
480  } else if (!s->left_ref_ctx[row7]) {
481  c = 1 + 2 * (s->fixcompref == 1 ||
482  s->above_ref_ctx[col] == 1);
483  } else {
484  c = 3 * (s->left_ref_ctx[row7] == 1) +
485  (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
486  }
487  } else if (s->left_comp_ctx[row7]) {
488  if (!s->above_ref_ctx[col]) {
489  c = 1 + 2 * (s->fixcompref == 1 ||
490  s->left_ref_ctx[row7] == 1);
491  } else {
492  c = 3 * (s->above_ref_ctx[col] == 1) +
493  (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
494  }
495  } else if (!s->above_ref_ctx[col]) {
496  if (!s->left_ref_ctx[row7]) {
497  c = 3;
498  } else {
499  c = 4 * (s->left_ref_ctx[row7] == 1);
500  }
501  } else if (!s->left_ref_ctx[row7]) {
502  c = 4 * (s->above_ref_ctx[col] == 1);
503  } else {
504  c = 2 * (s->left_ref_ctx[row7] == 1) +
505  2 * (s->above_ref_ctx[col] == 1);
506  }
507  } else {
508  if (s->above_intra_ctx[col] ||
509  (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
510  c = 2;
511  } else if (s->above_comp_ctx[col]) {
512  c = 3 * (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
513  } else {
514  c = 4 * (s->above_ref_ctx[col] == 1);
515  }
516  }
517  } else if (have_l) {
518  if (s->left_intra_ctx[row7] ||
519  (!s->left_comp_ctx[row7] && !s->left_ref_ctx[row7])) {
520  c = 2;
521  } else if (s->left_comp_ctx[row7]) {
522  c = 3 * (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
523  } else {
524  c = 4 * (s->left_ref_ctx[row7] == 1);
525  }
526  } else {
527  c = 2;
528  }
529  bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][1]);
530  s->counts.single_ref[c][1][bit]++;
531  b->ref[0] = 1 + bit;
532  }
533  }
534  }
535 
536  if (b->bs <= BS_8x8) {
537  if (s->segmentation.feat[b->seg_id].skip_enabled) {
538  b->mode[0] =
539  b->mode[1] =
540  b->mode[2] =
541  b->mode[3] = ZEROMV;
542  } else {
543  static const uint8_t off[10] = {
544  3, 0, 0, 1, 0, 0, 0, 0, 0, 0
545  };
546 
547  // FIXME this needs to use the LUT tables from find_ref_mvs
548  // because not all are -1,0/0,-1
549  int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
550  [s->left_mode_ctx[row7 + off[b->bs]]];
551 
553  s->prob.p.mv_mode[c]);
554  b->mode[1] =
555  b->mode[2] =
556  b->mode[3] = b->mode[0];
557  s->counts.mv_mode[c][b->mode[0] - 10]++;
558  }
559  }
560 
561  if (s->filtermode == FILTER_SWITCHABLE) {
562  int c;
563 
564  if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
565  if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
566  c = s->above_filter_ctx[col] == s->left_filter_ctx[row7] ?
567  s->left_filter_ctx[row7] : 3;
568  } else {
569  c = s->above_filter_ctx[col];
570  }
571  } else if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
572  c = s->left_filter_ctx[row7];
573  } else {
574  c = 3;
575  }
576 
578  s->prob.p.filter[c]);
579  s->counts.filter[c][b->filter]++;
580  } else {
581  b->filter = s->filtermode;
582  }
583 
584  if (b->bs > BS_8x8) {
585  int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][s->left_mode_ctx[row7]];
586 
588  s->prob.p.mv_mode[c]);
589  s->counts.mv_mode[c][b->mode[0] - 10]++;
590  ff_vp9_fill_mv(s, b->mv[0], b->mode[0], 0);
591 
592  if (b->bs != BS_8x4) {
594  s->prob.p.mv_mode[c]);
595  s->counts.mv_mode[c][b->mode[1] - 10]++;
596  ff_vp9_fill_mv(s, b->mv[1], b->mode[1], 1);
597  } else {
598  b->mode[1] = b->mode[0];
599  AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
600  AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
601  }
602 
603  if (b->bs != BS_4x8) {
605  s->prob.p.mv_mode[c]);
606  s->counts.mv_mode[c][b->mode[2] - 10]++;
607  ff_vp9_fill_mv(s, b->mv[2], b->mode[2], 2);
608 
609  if (b->bs != BS_8x4) {
611  s->prob.p.mv_mode[c]);
612  s->counts.mv_mode[c][b->mode[3] - 10]++;
613  ff_vp9_fill_mv(s, b->mv[3], b->mode[3], 3);
614  } else {
615  b->mode[3] = b->mode[2];
616  AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
617  AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
618  }
619  } else {
620  b->mode[2] = b->mode[0];
621  AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
622  AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
623  b->mode[3] = b->mode[1];
624  AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
625  AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
626  }
627  } else {
628  ff_vp9_fill_mv(s, b->mv[0], b->mode[0], -1);
629  AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
630  AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
631  AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
632  AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
633  AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
634  AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
635  }
636  }
637 
638  // FIXME this can probably be optimized
639  memset(&s->above_skip_ctx[col], b->skip, w4);
640  memset(&s->left_skip_ctx[row7], b->skip, h4);
641  memset(&s->above_txfm_ctx[col], b->tx, w4);
642  memset(&s->left_txfm_ctx[row7], b->tx, h4);
643  memset(&s->above_partition_ctx[col], above_ctx[b->bs], w4);
644  memset(&s->left_partition_ctx[row7], left_ctx[b->bs], h4);
645  if (!s->keyframe && !s->intraonly) {
646  memset(&s->above_intra_ctx[col], b->intra, w4);
647  memset(&s->left_intra_ctx[row7], b->intra, h4);
648  memset(&s->above_comp_ctx[col], b->comp, w4);
649  memset(&s->left_comp_ctx[row7], b->comp, h4);
650  memset(&s->above_mode_ctx[col], b->mode[3], w4);
651  memset(&s->left_mode_ctx[row7], b->mode[3], h4);
652  if (s->filtermode == FILTER_SWITCHABLE && !b->intra) {
653  memset(&s->above_filter_ctx[col], b->filter, w4);
654  memset(&s->left_filter_ctx[row7], b->filter, h4);
656  }
657  if (b->bs > BS_8x8) {
658  int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
659 
660  AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
661  AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
662  AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][0], mv0);
663  AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][1], mv1);
664  AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
665  AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
666  AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
667  AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
668  } else {
669  int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
670 
671  for (n = 0; n < w4 * 2; n++) {
672  AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
673  AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
674  }
675  for (n = 0; n < h4 * 2; n++) {
676  AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][0], mv0);
677  AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][1], mv1);
678  }
679  }
680 
681  if (!b->intra) { // FIXME write 0xff or -1 if intra, so we can use this
682  // as a direct check in above branches
683  int vref = b->ref[b->comp ? s->signbias[s->varcompref[0]] : 0];
684 
685  memset(&s->above_ref_ctx[col], vref, w4);
686  memset(&s->left_ref_ctx[row7], vref, h4);
687  }
688  }
689 
690  // FIXME kinda ugly
691  for (y = 0; y < h4; y++) {
692  int x, o = (row + y) * s->sb_cols * 8 + col;
693  VP9MVRefPair *mv = &s->frames[CUR_FRAME].mv[o];
694 
695  if (b->intra) {
696  for (x = 0; x < w4; x++) {
697  mv[x].ref[0] =
698  mv[x].ref[1] = -1;
699  }
700  } else if (b->comp) {
701  for (x = 0; x < w4; x++) {
702  mv[x].ref[0] = b->ref[0];
703  mv[x].ref[1] = b->ref[1];
704  AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
705  AV_COPY32(&mv[x].mv[1], &b->mv[3][1]);
706  }
707  } else {
708  for (x = 0; x < w4; x++) {
709  mv[x].ref[0] = b->ref[0];
710  mv[x].ref[1] = -1;
711  AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
712  }
713  }
714  }
715 }
716 
717 // FIXME remove tx argument, and merge cnt/eob arguments?
718 static int decode_block_coeffs(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
719  enum TxfmMode tx, unsigned (*cnt)[6][3],
720  unsigned (*eob)[6][2], uint8_t(*p)[6][11],
721  int nnz, const int16_t *scan,
722  const int16_t(*nb)[2],
723  const int16_t *band_counts, const int16_t *qmul)
724 {
725  int i = 0, band = 0, band_left = band_counts[band];
726  uint8_t *tp = p[0][nnz];
727  uint8_t cache[1024];
728 
729  do {
730  int val, rc;
731 
732  val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
733  eob[band][nnz][val]++;
734  if (!val)
735  break;
736 
737 skip_eob:
738  if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
739  cnt[band][nnz][0]++;
740  if (!--band_left)
741  band_left = band_counts[++band];
742  cache[scan[i]] = 0;
743  nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
744  tp = p[band][nnz];
745  if (++i == n_coeffs)
746  break; //invalid input; blocks should end with EOB
747  goto skip_eob;
748  }
749 
750  rc = scan[i];
751  if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
752  cnt[band][nnz][1]++;
753  val = 1;
754  cache[rc] = 1;
755  } else {
756  // fill in p[3-10] (model fill) - only once per frame for each pos
757  if (!tp[3])
758  memcpy(&tp[3], ff_vp9_model_pareto8[tp[2]], 8);
759 
760  cnt[band][nnz][2]++;
761  if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
762  if (!vp56_rac_get_prob_branchy(c, tp[4])) {
763  cache[rc] = val = 2;
764  } else {
765  val = 3 + vp56_rac_get_prob(c, tp[5]);
766  cache[rc] = 3;
767  }
768  } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
769  cache[rc] = 4;
770  if (!vp56_rac_get_prob_branchy(c, tp[7])) {
771  val = vp56_rac_get_prob(c, 159) + 5;
772  } else {
773  val = (vp56_rac_get_prob(c, 165) << 1) + 7;
774  val += vp56_rac_get_prob(c, 145);
775  }
776  } else { // cat 3-6
777  cache[rc] = 5;
778  if (!vp56_rac_get_prob_branchy(c, tp[8])) {
779  if (!vp56_rac_get_prob_branchy(c, tp[9])) {
780  val = (vp56_rac_get_prob(c, 173) << 2) + 11;
781  val += (vp56_rac_get_prob(c, 148) << 1);
782  val += vp56_rac_get_prob(c, 140);
783  } else {
784  val = (vp56_rac_get_prob(c, 176) << 3) + 19;
785  val += (vp56_rac_get_prob(c, 155) << 2);
786  val += (vp56_rac_get_prob(c, 140) << 1);
787  val += vp56_rac_get_prob(c, 135);
788  }
789  } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
790  val = (vp56_rac_get_prob(c, 180) << 4) + 35;
791  val += (vp56_rac_get_prob(c, 157) << 3);
792  val += (vp56_rac_get_prob(c, 141) << 2);
793  val += (vp56_rac_get_prob(c, 134) << 1);
794  val += vp56_rac_get_prob(c, 130);
795  } else {
796  val = (vp56_rac_get_prob(c, 254) << 13) + 67;
797  val += (vp56_rac_get_prob(c, 254) << 12);
798  val += (vp56_rac_get_prob(c, 254) << 11);
799  val += (vp56_rac_get_prob(c, 252) << 10);
800  val += (vp56_rac_get_prob(c, 249) << 9);
801  val += (vp56_rac_get_prob(c, 243) << 8);
802  val += (vp56_rac_get_prob(c, 230) << 7);
803  val += (vp56_rac_get_prob(c, 196) << 6);
804  val += (vp56_rac_get_prob(c, 177) << 5);
805  val += (vp56_rac_get_prob(c, 153) << 4);
806  val += (vp56_rac_get_prob(c, 140) << 3);
807  val += (vp56_rac_get_prob(c, 133) << 2);
808  val += (vp56_rac_get_prob(c, 130) << 1);
809  val += vp56_rac_get_prob(c, 129);
810  }
811  }
812  }
813  if (!--band_left)
814  band_left = band_counts[++band];
815  if (tx == TX_32X32) // FIXME slow
816  coef[rc] = ((vp8_rac_get(c) ? -val : val) * qmul[!!i]) / 2;
817  else
818  coef[rc] = (vp8_rac_get(c) ? -val : val) * qmul[!!i];
819  nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
820  tp = p[band][nnz];
821  } while (++i < n_coeffs);
822 
823  return i;
824 }
825 
826 static int decode_coeffs(AVCodecContext *avctx)
827 {
828  VP9Context *s = avctx->priv_data;
829  VP9Block *b = s->b;
830  int row = b->row, col = b->col;
831  uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
832  unsigned (*c)[6][3] = s->counts.coef[b->tx][0 /* y */][!b->intra];
833  unsigned (*e)[6][2] = s->counts.eob[b->tx][0 /* y */][!b->intra];
834  int w4 = bwh_tab[1][b->bs][0] << 1, h4 = bwh_tab[1][b->bs][1] << 1;
835  int end_x = FFMIN(2 * (s->cols - col), w4);
836  int end_y = FFMIN(2 * (s->rows - row), h4);
837  int n, pl, x, y, step1d = 1 << b->tx, step = 1 << (b->tx * 2);
838  int uvstep1d = 1 << b->uvtx, uvstep = 1 << (b->uvtx * 2), ret;
839  int16_t (*qmul)[2] = s->segmentation.feat[b->seg_id].qmul;
840  int tx = 4 * s->lossless + b->tx;
841  const int16_t **yscans = ff_vp9_scans[tx];
842  const int16_t (**ynbs)[2] = ff_vp9_scans_nb[tx];
843  const int16_t *uvscan = ff_vp9_scans[b->uvtx][DCT_DCT];
844  const int16_t (*uvnb)[2] = ff_vp9_scans_nb[b->uvtx][DCT_DCT];
845  uint8_t *a = &s->above_y_nnz_ctx[col * 2];
846  uint8_t *l = &s->left_y_nnz_ctx[(row & 7) << 1];
847  static const int16_t band_counts[4][8] = {
848  { 1, 2, 3, 4, 3, 16 - 13, 0 },
849  { 1, 2, 3, 4, 11, 64 - 21, 0 },
850  { 1, 2, 3, 4, 11, 256 - 21, 0 },
851  { 1, 2, 3, 4, 11, 1024 - 21, 0 },
852  };
853  const int16_t *y_band_counts = band_counts[b->tx];
854  const int16_t *uv_band_counts = band_counts[b->uvtx];
855 
856  /* y tokens */
857  if (b->tx > TX_4X4) { // FIXME slow
858  for (y = 0; y < end_y; y += step1d)
859  for (x = 1; x < step1d; x++)
860  l[y] |= l[y + x];
861  for (x = 0; x < end_x; x += step1d)
862  for (y = 1; y < step1d; y++)
863  a[x] |= a[x + y];
864  }
865  for (n = 0, y = 0; y < end_y; y += step1d) {
866  for (x = 0; x < end_x; x += step1d, n += step) {
867  enum TxfmType txtp = ff_vp9_intra_txfm_type[b->mode[b->tx == TX_4X4 &&
868  b->bs > BS_8x8 ?
869  n : 0]];
870  int nnz = a[x] + l[y];
871  if ((ret = decode_block_coeffs(&s->c, s->block + 16 * n, 16 * step,
872  b->tx, c, e, p, nnz, yscans[txtp],
873  ynbs[txtp], y_band_counts,
874  qmul[0])) < 0)
875  return ret;
876  a[x] = l[y] = !!ret;
877  if (b->tx > TX_8X8)
878  AV_WN16A(&s->eob[n], ret);
879  else
880  s->eob[n] = ret;
881  }
882  }
883  if (b->tx > TX_4X4) { // FIXME slow
884  for (y = 0; y < end_y; y += step1d)
885  memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, step1d - 1));
886  for (x = 0; x < end_x; x += step1d)
887  memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, step1d - 1));
888  }
889 
890  p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
891  c = s->counts.coef[b->uvtx][1 /* uv */][!b->intra];
892  e = s->counts.eob[b->uvtx][1 /* uv */][!b->intra];
893  w4 >>= 1;
894  h4 >>= 1;
895  end_x >>= 1;
896  end_y >>= 1;
897  for (pl = 0; pl < 2; pl++) {
898  a = &s->above_uv_nnz_ctx[pl][col];
899  l = &s->left_uv_nnz_ctx[pl][row & 7];
900  if (b->uvtx > TX_4X4) { // FIXME slow
901  for (y = 0; y < end_y; y += uvstep1d)
902  for (x = 1; x < uvstep1d; x++)
903  l[y] |= l[y + x];
904  for (x = 0; x < end_x; x += uvstep1d)
905  for (y = 1; y < uvstep1d; y++)
906  a[x] |= a[x + y];
907  }
908  for (n = 0, y = 0; y < end_y; y += uvstep1d) {
909  for (x = 0; x < end_x; x += uvstep1d, n += uvstep) {
910  int nnz = a[x] + l[y];
911  if ((ret = decode_block_coeffs(&s->c, s->uvblock[pl] + 16 * n,
912  16 * uvstep, b->uvtx, c, e, p,
913  nnz, uvscan, uvnb,
914  uv_band_counts, qmul[1])) < 0)
915  return ret;
916  a[x] = l[y] = !!ret;
917  if (b->uvtx > TX_8X8)
918  AV_WN16A(&s->uveob[pl][n], ret);
919  else
920  s->uveob[pl][n] = ret;
921  }
922  }
923  if (b->uvtx > TX_4X4) { // FIXME slow
924  for (y = 0; y < end_y; y += uvstep1d)
925  memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, uvstep1d - 1));
926  for (x = 0; x < end_x; x += uvstep1d)
927  memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, uvstep1d - 1));
928  }
929  }
930 
931  return 0;
932 }
933 
935  uint8_t **a,
936  uint8_t *dst_edge,
937  ptrdiff_t stride_edge,
938  uint8_t *dst_inner,
939  ptrdiff_t stride_inner,
940  uint8_t *l, int col, int x, int w,
941  int row, int y, enum TxfmMode tx,
942  int p)
943 {
944  int have_top = row > 0 || y > 0;
945  int have_left = col > s->tiling.tile_col_start || x > 0;
946  int have_right = x < w - 1;
947  static const uint8_t mode_conv[10][2 /* have_left */][2 /* have_top */] = {
948  [VERT_PRED] = { { DC_127_PRED, VERT_PRED },
949  { DC_127_PRED, VERT_PRED } },
950  [HOR_PRED] = { { DC_129_PRED, DC_129_PRED },
951  { HOR_PRED, HOR_PRED } },
952  [DC_PRED] = { { DC_128_PRED, TOP_DC_PRED },
953  { LEFT_DC_PRED, DC_PRED } },
955  { DC_127_PRED, DIAG_DOWN_LEFT_PRED } },
957  { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED } },
959  { VERT_RIGHT_PRED, VERT_RIGHT_PRED } },
961  { HOR_DOWN_PRED, HOR_DOWN_PRED } },
963  { DC_127_PRED, VERT_LEFT_PRED } },
965  { HOR_UP_PRED, HOR_UP_PRED } },
966  [TM_VP8_PRED] = { { DC_129_PRED, VERT_PRED },
967  { HOR_PRED, TM_VP8_PRED } },
968  };
969  static const struct {
970  uint8_t needs_left:1;
971  uint8_t needs_top:1;
972  uint8_t needs_topleft:1;
973  uint8_t needs_topright:1;
974  } edges[N_INTRA_PRED_MODES] = {
975  [VERT_PRED] = { .needs_top = 1 },
976  [HOR_PRED] = { .needs_left = 1 },
977  [DC_PRED] = { .needs_top = 1, .needs_left = 1 },
978  [DIAG_DOWN_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
979  [DIAG_DOWN_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
980  .needs_topleft = 1 },
981  [VERT_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
982  .needs_topleft = 1 },
983  [HOR_DOWN_PRED] = { .needs_left = 1, .needs_top = 1,
984  .needs_topleft = 1 },
985  [VERT_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
986  [HOR_UP_PRED] = { .needs_left = 1 },
987  [TM_VP8_PRED] = { .needs_left = 1, .needs_top = 1,
988  .needs_topleft = 1 },
989  [LEFT_DC_PRED] = { .needs_left = 1 },
990  [TOP_DC_PRED] = { .needs_top = 1 },
991  [DC_128_PRED] = { 0 },
992  [DC_127_PRED] = { 0 },
993  [DC_129_PRED] = { 0 }
994  };
995 
996  av_assert2(mode >= 0 && mode < 10);
997  mode = mode_conv[mode][have_left][have_top];
998  if (edges[mode].needs_top) {
999  uint8_t *top = NULL, *topleft = NULL;
1000  int n_px_need = 4 << tx, n_px_have = (((s->cols - col) << !p) - x) * 4;
1001  int n_px_need_tr = 0;
1002 
1003  if (tx == TX_4X4 && edges[mode].needs_topright && have_right)
1004  n_px_need_tr = 4;
1005 
1006  // if top of sb64-row, use s->intra_pred_data[] instead of
1007  // dst[-stride] for intra prediction (it contains pre- instead of
1008  // post-loopfilter data)
1009  if (have_top) {
1010  top = !(row & 7) && !y ?
1011  s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
1012  y == 0 ? &dst_edge[-stride_edge] : &dst_inner[-stride_inner];
1013  if (have_left)
1014  topleft = !(row & 7) && !y ?
1015  s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
1016  y == 0 || x == 0 ? &dst_edge[-stride_edge] :
1017  &dst_inner[-stride_inner];
1018  }
1019 
1020  if (have_top &&
1021  (!edges[mode].needs_topleft || (have_left && top == topleft)) &&
1022  (tx != TX_4X4 || !edges[mode].needs_topright || have_right) &&
1023  n_px_need + n_px_need_tr <= n_px_have) {
1024  *a = top;
1025  } else {
1026  if (have_top) {
1027  if (n_px_need <= n_px_have) {
1028  memcpy(*a, top, n_px_need);
1029  } else {
1030  memcpy(*a, top, n_px_have);
1031  memset(&(*a)[n_px_have], (*a)[n_px_have - 1],
1032  n_px_need - n_px_have);
1033  }
1034  } else {
1035  memset(*a, 127, n_px_need);
1036  }
1037  if (edges[mode].needs_topleft) {
1038  if (have_left && have_top)
1039  (*a)[-1] = topleft[-1];
1040  else
1041  (*a)[-1] = have_top ? 129 : 127;
1042  }
1043  if (tx == TX_4X4 && edges[mode].needs_topright) {
1044  if (have_top && have_right &&
1045  n_px_need + n_px_need_tr <= n_px_have) {
1046  memcpy(&(*a)[4], &top[4], 4);
1047  } else {
1048  memset(&(*a)[4], (*a)[3], 4);
1049  }
1050  }
1051  }
1052  }
1053  if (edges[mode].needs_left) {
1054  if (have_left) {
1055  int i;
1056  int n_px_need = 4 << tx;
1057  int n_px_have = (((s->rows - row) << !p) - y) * 4;
1058  uint8_t *dst = x == 0 ? dst_edge : dst_inner;
1059  ptrdiff_t stride = x == 0 ? stride_edge : stride_inner;
1060 
1061  if (n_px_need <= n_px_have) {
1062  for (i = 0; i < n_px_need; i++)
1063  l[i] = dst[i * stride - 1];
1064  } else {
1065  for (i = 0; i < n_px_have; i++)
1066  l[i] = dst[i * stride - 1];
1067  memset(&l[i], l[i - 1], n_px_need - n_px_have);
1068  }
1069  } else {
1070  memset(l, 129, 4 << tx);
1071  }
1072  }
1073 
1074  return mode;
1075 }
1076 
1077 static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off)
1078 {
1079  VP9Context *s = avctx->priv_data;
1080  VP9Block *b = s->b;
1081  AVFrame *f = s->frames[CUR_FRAME].tf.f;
1082  int row = b->row, col = b->col;
1083  int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
1084  int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
1085  int end_x = FFMIN(2 * (s->cols - col), w4);
1086  int end_y = FFMIN(2 * (s->rows - row), h4);
1087  int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
1088  int uvstep1d = 1 << b->uvtx, p;
1089  uint8_t *dst = b->dst[0], *dst_r = f->data[0] + y_off;
1090 
1091  for (n = 0, y = 0; y < end_y; y += step1d) {
1092  uint8_t *ptr = dst, *ptr_r = dst_r;
1093  for (x = 0; x < end_x;
1094  x += step1d, ptr += 4 * step1d, ptr_r += 4 * step1d, n += step) {
1095  int mode = b->mode[b->bs > BS_8x8 && b->tx == TX_4X4 ?
1096  y * 2 + x : 0];
1097  LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
1098  uint8_t *a = &a_buf[16], l[32];
1099  enum TxfmType txtp = ff_vp9_intra_txfm_type[mode];
1100  int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
1101 
1102  mode = check_intra_mode(s, mode, &a, ptr_r,
1103  f->linesize[0],
1104  ptr, b->y_stride, l,
1105  col, x, w4, row, y, b->tx, 0);
1106  s->dsp.intra_pred[b->tx][mode](ptr, b->y_stride, l, a);
1107  if (eob)
1108  s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride,
1109  s->block + 16 * n, eob);
1110  }
1111  dst_r += 4 * f->linesize[0] * step1d;
1112  dst += 4 * b->y_stride * step1d;
1113  }
1114 
1115  // U/V
1116  h4 >>= 1;
1117  w4 >>= 1;
1118  end_x >>= 1;
1119  end_y >>= 1;
1120  step = 1 << (b->uvtx * 2);
1121  for (p = 0; p < 2; p++) {
1122  dst = b->dst[1 + p];
1123  dst_r = f->data[1 + p] + uv_off;
1124  for (n = 0, y = 0; y < end_y; y += uvstep1d) {
1125  uint8_t *ptr = dst, *ptr_r = dst_r;
1126  for (x = 0; x < end_x;
1127  x += uvstep1d, ptr += 4 * uvstep1d,
1128  ptr_r += 4 * uvstep1d, n += step) {
1129  int mode = b->uvmode;
1130  LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
1131  uint8_t *a = &a_buf[16], l[32];
1132  int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
1133  : s->uveob[p][n];
1134 
1135  mode = check_intra_mode(s, mode, &a, ptr_r,
1136  f->linesize[1],
1137  ptr, b->uv_stride, l,
1138  col, x, w4, row, y, b->uvtx, p + 1);
1139  s->dsp.intra_pred[b->uvtx][mode](ptr, b->uv_stride, l, a);
1140  if (eob)
1141  s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1142  s->uvblock[p] + 16 * n,
1143  eob);
1144  }
1145  dst_r += 4 * uvstep1d * f->linesize[1];
1146  dst += 4 * uvstep1d * b->uv_stride;
1147  }
1148  }
1149 }
1150 
1152  uint8_t *dst, ptrdiff_t dst_stride,
1153  const uint8_t *ref,
1154  ptrdiff_t ref_stride,
1156  ptrdiff_t y, ptrdiff_t x,
1157  const VP56mv *mv,
1158  int bw, int bh, int w, int h)
1159 {
1160  int mx = mv->x, my = mv->y;
1161  int th;
1162 
1163  y += my >> 3;
1164  x += mx >> 3;
1165  ref += y * ref_stride + x;
1166  mx &= 7;
1167  my &= 7;
1168 
1169  // we use +7 because the last 7 pixels of each sbrow can be changed in
1170  // the longest loopfilter of the next sbrow
1171  th = (y + bh + 4 * !!my + 7) >> 6;
1172  ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0);
1173 
1174  // FIXME bilinear filter only needs 0/1 pixels, not 3/4
1175  if (x < !!mx * 3 || y < !!my * 3 ||
1176  x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
1178  ref - !!my * 3 * ref_stride - !!mx * 3,
1179  80,
1180  ref_stride,
1181  bw + !!mx * 7, bh + !!my * 7,
1182  x - !!mx * 3, y - !!my * 3, w, h);
1183  ref = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1184  ref_stride = 80;
1185  }
1186  mc[!!mx][!!my](dst, ref, dst_stride, ref_stride, bh, mx << 1, my << 1);
1187 }
1188 
1190  uint8_t *dst_u, uint8_t *dst_v,
1191  ptrdiff_t dst_stride,
1192  const uint8_t *ref_u,
1193  ptrdiff_t src_stride_u,
1194  const uint8_t *ref_v,
1195  ptrdiff_t src_stride_v,
1197  ptrdiff_t y, ptrdiff_t x,
1198  const VP56mv *mv,
1199  int bw, int bh, int w, int h)
1200 {
1201  int mx = mv->x, my = mv->y;
1202  int th;
1203 
1204  y += my >> 4;
1205  x += mx >> 4;
1206  ref_u += y * src_stride_u + x;
1207  ref_v += y * src_stride_v + x;
1208  mx &= 15;
1209  my &= 15;
1210 
1211  // we use +7 because the last 7 pixels of each sbrow can be changed in
1212  // the longest loopfilter of the next sbrow
1213  th = (y + bh + 4 * !!my + 7) >> 5;
1214  ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0);
1215 
1216  // FIXME bilinear filter only needs 0/1 pixels, not 3/4
1217  if (x < !!mx * 3 || y < !!my * 3 ||
1218  x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
1220  ref_u - !!my * 3 * src_stride_u - !!mx * 3,
1221  80,
1222  src_stride_u,
1223  bw + !!mx * 7, bh + !!my * 7,
1224  x - !!mx * 3, y - !!my * 3, w, h);
1225  ref_u = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1226  mc[!!mx][!!my](dst_u, ref_u, dst_stride, 80, bh, mx, my);
1227 
1229  ref_v - !!my * 3 * src_stride_v - !!mx * 3,
1230  80,
1231  src_stride_v,
1232  bw + !!mx * 7, bh + !!my * 7,
1233  x - !!mx * 3, y - !!my * 3, w, h);
1234  ref_v = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
1235  mc[!!mx][!!my](dst_v, ref_v, dst_stride, 80, bh, mx, my);
1236  } else {
1237  mc[!!mx][!!my](dst_u, ref_u, dst_stride, src_stride_u, bh, mx, my);
1238  mc[!!mx][!!my](dst_v, ref_v, dst_stride, src_stride_v, bh, mx, my);
1239  }
1240 }
1241 
1242 static int inter_recon(AVCodecContext *avctx)
1243 {
1244  static const uint8_t bwlog_tab[2][N_BS_SIZES] = {
1245  { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
1246  { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 },
1247  };
1248  VP9Context *s = avctx->priv_data;
1249  VP9Block *b = s->b;
1250  int row = b->row, col = b->col;
1251 
1252  ThreadFrame *tref1 = &s->refs[s->refidx[b->ref[0]]];
1253  ThreadFrame *tref2 = b->comp ? &s->refs[s->refidx[b->ref[1]]] : NULL;
1254  AVFrame *ref1 = tref1->f;
1255  AVFrame *ref2 = tref2 ? tref2->f : NULL;
1256 
1257  int w = avctx->width, h = avctx->height;
1258  ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride;
1259 
1260  if (!ref1->data[0] || (b->comp && !ref2->data[0]))
1261  return AVERROR_INVALIDDATA;
1262 
1263  // y inter pred
1264  if (b->bs > BS_8x8) {
1265  if (b->bs == BS_8x4) {
1266  mc_luma_dir(s, s->dsp.mc[3][b->filter][0], b->dst[0], ls_y,
1267  ref1->data[0], ref1->linesize[0], tref1,
1268  row << 3, col << 3, &b->mv[0][0], 8, 4, w, h);
1269  mc_luma_dir(s, s->dsp.mc[3][b->filter][0],
1270  b->dst[0] + 4 * ls_y, ls_y,
1271  ref1->data[0], ref1->linesize[0], tref1,
1272  (row << 3) + 4, col << 3, &b->mv[2][0], 8, 4, w, h);
1273 
1274  if (b->comp) {
1275  mc_luma_dir(s, s->dsp.mc[3][b->filter][1], b->dst[0], ls_y,
1276  ref2->data[0], ref2->linesize[0], tref2,
1277  row << 3, col << 3, &b->mv[0][1], 8, 4, w, h);
1278  mc_luma_dir(s, s->dsp.mc[3][b->filter][1],
1279  b->dst[0] + 4 * ls_y, ls_y,
1280  ref2->data[0], ref2->linesize[0], tref2,
1281  (row << 3) + 4, col << 3, &b->mv[2][1], 8, 4, w, h);
1282  }
1283  } else if (b->bs == BS_4x8) {
1284  mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
1285  ref1->data[0], ref1->linesize[0], tref1,
1286  row << 3, col << 3, &b->mv[0][0], 4, 8, w, h);
1287  mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
1288  ref1->data[0], ref1->linesize[0], tref1,
1289  row << 3, (col << 3) + 4, &b->mv[1][0], 4, 8, w, h);
1290 
1291  if (b->comp) {
1292  mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
1293  ref2->data[0], ref2->linesize[0], tref2,
1294  row << 3, col << 3, &b->mv[0][1], 4, 8, 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], tref2,
1297  row << 3, (col << 3) + 4, &b->mv[1][1], 4, 8, w, h);
1298  }
1299  } else {
1300  av_assert2(b->bs == BS_4x4);
1301 
1302  // FIXME if two horizontally adjacent blocks have the same MV,
1303  // do a w8 instead of a w4 call
1304  mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
1305  ref1->data[0], ref1->linesize[0], tref1,
1306  row << 3, col << 3, &b->mv[0][0], 4, 4, w, h);
1307  mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
1308  ref1->data[0], ref1->linesize[0], tref1,
1309  row << 3, (col << 3) + 4, &b->mv[1][0], 4, 4, w, h);
1310  mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
1311  b->dst[0] + 4 * ls_y, ls_y,
1312  ref1->data[0], ref1->linesize[0], tref1,
1313  (row << 3) + 4, col << 3, &b->mv[2][0], 4, 4, w, h);
1314  mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
1315  b->dst[0] + 4 * ls_y + 4, ls_y,
1316  ref1->data[0], ref1->linesize[0], tref1,
1317  (row << 3) + 4, (col << 3) + 4, &b->mv[3][0], 4, 4, w, h);
1318 
1319  if (b->comp) {
1320  mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
1321  ref2->data[0], ref2->linesize[0], tref2,
1322  row << 3, col << 3, &b->mv[0][1], 4, 4, w, h);
1323  mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
1324  ref2->data[0], ref2->linesize[0], tref2,
1325  row << 3, (col << 3) + 4, &b->mv[1][1], 4, 4, w, h);
1326  mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
1327  b->dst[0] + 4 * ls_y, ls_y,
1328  ref2->data[0], ref2->linesize[0], tref2,
1329  (row << 3) + 4, col << 3, &b->mv[2][1], 4, 4, w, h);
1330  mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
1331  b->dst[0] + 4 * ls_y + 4, ls_y,
1332  ref2->data[0], ref2->linesize[0], tref2,
1333  (row << 3) + 4, (col << 3) + 4, &b->mv[3][1], 4, 4, w, h);
1334  }
1335  }
1336  } else {
1337  int bwl = bwlog_tab[0][b->bs];
1338  int bw = bwh_tab[0][b->bs][0] * 4;
1339  int bh = bwh_tab[0][b->bs][1] * 4;
1340 
1341  mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[0], ls_y,
1342  ref1->data[0], ref1->linesize[0], tref1,
1343  row << 3, col << 3, &b->mv[0][0], bw, bh, w, h);
1344 
1345  if (b->comp)
1346  mc_luma_dir(s, s->dsp.mc[bwl][b->filter][1], b->dst[0], ls_y,
1347  ref2->data[0], ref2->linesize[0], tref2,
1348  row << 3, col << 3, &b->mv[0][1], bw, bh, w, h);
1349  }
1350 
1351  // uv inter pred
1352  {
1353  int bwl = bwlog_tab[1][b->bs];
1354  int bw = bwh_tab[1][b->bs][0] * 4, bh = bwh_tab[1][b->bs][1] * 4;
1355  VP56mv mvuv;
1356 
1357  w = (w + 1) >> 1;
1358  h = (h + 1) >> 1;
1359  if (b->bs > BS_8x8) {
1360  mvuv.x = ROUNDED_DIV(b->mv[0][0].x + b->mv[1][0].x +
1361  b->mv[2][0].x + b->mv[3][0].x, 4);
1362  mvuv.y = ROUNDED_DIV(b->mv[0][0].y + b->mv[1][0].y +
1363  b->mv[2][0].y + b->mv[3][0].y, 4);
1364  } else {
1365  mvuv = b->mv[0][0];
1366  }
1367 
1368  mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][0],
1369  b->dst[1], b->dst[2], ls_uv,
1370  ref1->data[1], ref1->linesize[1],
1371  ref1->data[2], ref1->linesize[2], tref1,
1372  row << 2, col << 2, &mvuv, bw, bh, w, h);
1373 
1374  if (b->comp) {
1375  if (b->bs > BS_8x8) {
1376  mvuv.x = ROUNDED_DIV(b->mv[0][1].x + b->mv[1][1].x +
1377  b->mv[2][1].x + b->mv[3][1].x, 4);
1378  mvuv.y = ROUNDED_DIV(b->mv[0][1].y + b->mv[1][1].y +
1379  b->mv[2][1].y + b->mv[3][1].y, 4);
1380  } else {
1381  mvuv = b->mv[0][1];
1382  }
1383  mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][1],
1384  b->dst[1], b->dst[2], ls_uv,
1385  ref2->data[1], ref2->linesize[1],
1386  ref2->data[2], ref2->linesize[2], tref2,
1387  row << 2, col << 2, &mvuv, bw, bh, w, h);
1388  }
1389  }
1390 
1391  if (!b->skip) {
1392  /* mostly copied intra_reconn() */
1393 
1394  int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
1395  int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
1396  int end_x = FFMIN(2 * (s->cols - col), w4);
1397  int end_y = FFMIN(2 * (s->rows - row), h4);
1398  int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
1399  int uvstep1d = 1 << b->uvtx, p;
1400  uint8_t *dst = b->dst[0];
1401 
1402  // y itxfm add
1403  for (n = 0, y = 0; y < end_y; y += step1d) {
1404  uint8_t *ptr = dst;
1405  for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d, n += step) {
1406  int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
1407 
1408  if (eob)
1409  s->dsp.itxfm_add[tx][DCT_DCT](ptr, b->y_stride,
1410  s->block + 16 * n, eob);
1411  }
1412  dst += 4 * b->y_stride * step1d;
1413  }
1414 
1415  // uv itxfm add
1416  h4 >>= 1;
1417  w4 >>= 1;
1418  end_x >>= 1;
1419  end_y >>= 1;
1420  step = 1 << (b->uvtx * 2);
1421  for (p = 0; p < 2; p++) {
1422  dst = b->dst[p + 1];
1423  for (n = 0, y = 0; y < end_y; y += uvstep1d) {
1424  uint8_t *ptr = dst;
1425  for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d, n += step) {
1426  int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
1427  : s->uveob[p][n];
1428  if (eob)
1429  s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1430  s->uvblock[p] + 16 * n, eob);
1431  }
1432  dst += 4 * uvstep1d * b->uv_stride;
1433  }
1434  }
1435  }
1436  return 0;
1437 }
1438 
1439 static av_always_inline void mask_edges(VP9Filter *lflvl, int is_uv,
1440  int row_and_7, int col_and_7,
1441  int w, int h, int col_end, int row_end,
1442  enum TxfmMode tx, int skip_inter)
1443 {
1444  // FIXME I'm pretty sure all loops can be replaced by a single LUT if
1445  // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
1446  // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
1447  // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
1448 
1449  // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
1450  // edges. This means that for UV, we work on two subsampled blocks at
1451  // a time, and we only use the topleft block's mode information to set
1452  // things like block strength. Thus, for any block size smaller than
1453  // 16x16, ignore the odd portion of the block.
1454  if (tx == TX_4X4 && is_uv) {
1455  if (h == 1) {
1456  if (row_and_7 & 1)
1457  return;
1458  if (!row_end)
1459  h += 1;
1460  }
1461  if (w == 1) {
1462  if (col_and_7 & 1)
1463  return;
1464  if (!col_end)
1465  w += 1;
1466  }
1467  }
1468 
1469  if (tx == TX_4X4 && !skip_inter) {
1470  int t = 1 << col_and_7, m_col = (t << w) - t, y;
1471  int m_col_odd = (t << (w - 1)) - t;
1472 
1473  // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
1474  if (is_uv) {
1475  int m_row_8 = m_col & 0x01, m_row_4 = m_col - m_row_8;
1476 
1477  for (y = row_and_7; y < h + row_and_7; y++) {
1478  int col_mask_id = 2 - !(y & 7);
1479 
1480  lflvl->mask[is_uv][0][y][1] |= m_row_8;
1481  lflvl->mask[is_uv][0][y][2] |= m_row_4;
1482  // for odd lines, if the odd col is not being filtered,
1483  // skip odd row also:
1484  // .---. <-- a
1485  // | |
1486  // |___| <-- b
1487  // ^ ^
1488  // c d
1489  //
1490  // if a/c are even row/col and b/d are odd, and d is skipped,
1491  // e.g. right edge of size-66x66.webm, then skip b also (bug)
1492  if ((col_end & 1) && (y & 1)) {
1493  lflvl->mask[is_uv][1][y][col_mask_id] |= m_col_odd;
1494  } else {
1495  lflvl->mask[is_uv][1][y][col_mask_id] |= m_col;
1496  }
1497  }
1498  } else {
1499  int m_row_8 = m_col & 0x11, m_row_4 = m_col - m_row_8;
1500 
1501  for (y = row_and_7; y < h + row_and_7; y++) {
1502  int col_mask_id = 2 - !(y & 3);
1503 
1504  lflvl->mask[is_uv][0][y][1] |= m_row_8; // row edge
1505  lflvl->mask[is_uv][0][y][2] |= m_row_4;
1506  lflvl->mask[is_uv][1][y][col_mask_id] |= m_col; // col edge
1507  lflvl->mask[is_uv][0][y][3] |= m_col;
1508  lflvl->mask[is_uv][1][y][3] |= m_col;
1509  }
1510  }
1511  } else {
1512  int y, t = 1 << col_and_7, m_col = (t << w) - t;
1513 
1514  if (!skip_inter) {
1515  int mask_id = (tx == TX_8X8);
1516  int l2 = tx + is_uv - 1, step1d = 1 << l2;
1517  static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
1518  int m_row = m_col & masks[l2];
1519 
1520  // at odd UV col/row edges tx16/tx32 loopfilter edges, force
1521  // 8wd loopfilter to prevent going off the visible edge.
1522  if (is_uv && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
1523  int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
1524  int m_row_8 = m_row - m_row_16;
1525 
1526  for (y = row_and_7; y < h + row_and_7; y++) {
1527  lflvl->mask[is_uv][0][y][0] |= m_row_16;
1528  lflvl->mask[is_uv][0][y][1] |= m_row_8;
1529  }
1530  } else {
1531  for (y = row_and_7; y < h + row_and_7; y++)
1532  lflvl->mask[is_uv][0][y][mask_id] |= m_row;
1533  }
1534 
1535  if (is_uv && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
1536  for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
1537  lflvl->mask[is_uv][1][y][0] |= m_col;
1538  if (y - row_and_7 == h - 1)
1539  lflvl->mask[is_uv][1][y][1] |= m_col;
1540  } else {
1541  for (y = row_and_7; y < h + row_and_7; y += step1d)
1542  lflvl->mask[is_uv][1][y][mask_id] |= m_col;
1543  }
1544  } else if (tx != TX_4X4) {
1545  int mask_id;
1546 
1547  mask_id = (tx == TX_8X8) || (is_uv && h == 1);
1548  lflvl->mask[is_uv][1][row_and_7][mask_id] |= m_col;
1549  mask_id = (tx == TX_8X8) || (is_uv && w == 1);
1550  for (y = row_and_7; y < h + row_and_7; y++)
1551  lflvl->mask[is_uv][0][y][mask_id] |= t;
1552  } else if (is_uv) {
1553  int t8 = t & 0x01, t4 = t - t8;
1554 
1555  for (y = row_and_7; y < h + row_and_7; y++) {
1556  lflvl->mask[is_uv][0][y][2] |= t4;
1557  lflvl->mask[is_uv][0][y][1] |= t8;
1558  }
1559  lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 7)] |= m_col;
1560  } else {
1561  int t8 = t & 0x11, t4 = t - t8;
1562 
1563  for (y = row_and_7; y < h + row_and_7; y++) {
1564  lflvl->mask[is_uv][0][y][2] |= t4;
1565  lflvl->mask[is_uv][0][y][1] |= t8;
1566  }
1567  lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 3)] |= m_col;
1568  }
1569  }
1570 }
1571 
1572 int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col,
1573  VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
1574  enum BlockLevel bl, enum BlockPartition bp)
1575 {
1576  VP9Context *s = avctx->priv_data;
1577  VP9Block *b = s->b;
1578  AVFrame *f = s->frames[CUR_FRAME].tf.f;
1579  enum BlockSize bs = bl * 3 + bp;
1580  int ret, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl;
1581  int emu[2];
1582 
1583  b->row = row;
1584  b->row7 = row & 7;
1585  b->col = col;
1586  b->col7 = col & 7;
1587 
1588  s->min_mv.x = -(128 + col * 64);
1589  s->min_mv.y = -(128 + row * 64);
1590  s->max_mv.x = 128 + (s->cols - col - w4) * 64;
1591  s->max_mv.y = 128 + (s->rows - row - h4) * 64;
1592 
1593  if (s->pass < 2) {
1594  b->bs = bs;
1595  b->bl = bl;
1596  b->bp = bp;
1597  decode_mode(s, b);
1598  b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx));
1599 
1600  if (!b->skip) {
1601  if ((ret = decode_coeffs(avctx)) < 0)
1602  return ret;
1603  } else {
1604  int pl;
1605 
1606  memset(&s->above_y_nnz_ctx[col * 2], 0, w4 * 2);
1607  memset(&s->left_y_nnz_ctx[(row & 7) << 1], 0, h4 * 2);
1608  for (pl = 0; pl < 2; pl++) {
1609  memset(&s->above_uv_nnz_ctx[pl][col], 0, w4);
1610  memset(&s->left_uv_nnz_ctx[pl][row & 7], 0, h4);
1611  }
1612  }
1613 
1614  if (s->pass == 1) {
1615  s->b++;
1616  s->block += w4 * h4 * 64;
1617  s->uvblock[0] += w4 * h4 * 16;
1618  s->uvblock[1] += w4 * h4 * 16;
1619  s->eob += w4 * h4 * 4;
1620  s->uveob[0] += w4 * h4;
1621  s->uveob[1] += w4 * h4;
1622 
1623  return 0;
1624  }
1625  }
1626 
1627  /* Emulated overhangs if the stride of the target buffer can't hold.
1628  * This allows to support emu-edge and so on even if we have large
1629  * block overhangs. */
1630  emu[0] = (col + w4) * 8 > f->linesize[0] ||
1631  (row + h4) > s->rows;
1632  emu[1] = (col + w4) * 4 > f->linesize[1] ||
1633  (row + h4) > s->rows;
1634  if (emu[0]) {
1635  b->dst[0] = s->tmp_y;
1636  b->y_stride = 64;
1637  } else {
1638  b->dst[0] = f->data[0] + yoff;
1639  b->y_stride = f->linesize[0];
1640  }
1641  if (emu[1]) {
1642  b->dst[1] = s->tmp_uv[0];
1643  b->dst[2] = s->tmp_uv[1];
1644  b->uv_stride = 32;
1645  } else {
1646  b->dst[1] = f->data[1] + uvoff;
1647  b->dst[2] = f->data[2] + uvoff;
1648  b->uv_stride = f->linesize[1];
1649  }
1650  if (b->intra) {
1651  intra_recon(avctx, yoff, uvoff);
1652  } else {
1653  if ((ret = inter_recon(avctx)) < 0)
1654  return ret;
1655  }
1656  if (emu[0]) {
1657  int w = FFMIN(s->cols - col, w4) * 8;
1658  int h = FFMIN(s->rows - row, h4) * 8;
1659  int n, o = 0;
1660 
1661  for (n = 0; o < w; n++) {
1662  int bw = 64 >> n;
1663 
1664  av_assert2(n <= 4);
1665  if (w & bw) {
1666  s->dsp.mc[n][0][0][0][0](f->data[0] + yoff + o,
1667  s->tmp_y + o,
1668  f->linesize[0],
1669  64, h, 0, 0);
1670  o += bw;
1671  }
1672  }
1673  }
1674  if (emu[1]) {
1675  int w = FFMIN(s->cols - col, w4) * 4;
1676  int h = FFMIN(s->rows - row, h4) * 4;
1677  int n, o = 0;
1678 
1679  for (n = 1; o < w; n++) {
1680  int bw = 64 >> n;
1681 
1682  av_assert2(n <= 4);
1683  if (w & bw) {
1684  s->dsp.mc[n][0][0][0][0](f->data[1] + uvoff + o,
1685  s->tmp_uv[0] + o,
1686  f->linesize[1],
1687  32, h, 0, 0);
1688  s->dsp.mc[n][0][0][0][0](f->data[2] + uvoff + o,
1689  s->tmp_uv[1] + o,
1690  f->linesize[2],
1691  32, h, 0, 0);
1692  o += bw;
1693  }
1694  }
1695  }
1696 
1697  // pick filter level and find edges to apply filter to
1698  if (s->filter.level &&
1699  (lvl = s->segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
1700  [b->mode[3] != ZEROMV]) > 0) {
1701  int x_end = FFMIN(s->cols - col, w4);
1702  int y_end = FFMIN(s->rows - row, h4);
1703  int skip_inter = !b->intra && b->skip;
1704 
1705  for (y = 0; y < h4; y++)
1706  memset(&lflvl->level[((row & 7) + y) * 8 + (col & 7)], lvl, w4);
1707  mask_edges(lflvl, 0, row & 7, col & 7, x_end, y_end, 0, 0, b->tx, skip_inter);
1708  mask_edges(lflvl, 1, row & 7, col & 7, x_end, y_end,
1709  s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
1710  s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
1711  b->uvtx, skip_inter);
1712 
1713  if (!s->filter.lim_lut[lvl]) {
1714  int sharp = s->filter.sharpness;
1715  int limit = lvl;
1716 
1717  if (sharp > 0) {
1718  limit >>= (sharp + 3) >> 2;
1719  limit = FFMIN(limit, 9 - sharp);
1720  }
1721  limit = FFMAX(limit, 1);
1722 
1723  s->filter.lim_lut[lvl] = limit;
1724  s->filter.mblim_lut[lvl] = 2 * (lvl + 2) + limit;
1725  }
1726  }
1727 
1728  if (s->pass == 2) {
1729  s->b++;
1730  s->block += w4 * h4 * 64;
1731  s->uvblock[0] += w4 * h4 * 16;
1732  s->uvblock[1] += w4 * h4 * 16;
1733  s->eob += w4 * h4 * 4;
1734  s->uveob[0] += w4 * h4;
1735  s->uveob[1] += w4 * h4;
1736  }
1737 
1738  return 0;
1739 }
Definition: vp9.h:90
ThreadFrame tf
Definition: vp9.h:231
int col7
Definition: vp9.h:271
Definition: vp9.h:57
uint8_t lossless
Definition: vp9.h:340
struct VP9Context::@108 min_mv
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
uint8_t * segmentation_map
Definition: vp9.h:233
static void decode_mode(VP9Context *s, VP9Block *const b)
Definition: vp9block.c:44
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
int row7
Definition: vp9.h:271
unsigned comp_ref[5][2]
Definition: vp9.h:383
uint8_t mblim_lut[64]
Definition: vp9.h:331
uint8_t left_segpred_ctx[8]
Definition: vp9.h:414
VP9Block * b
Definition: vp9.h:286
VP5 and VP6 compatible video decoder (common features)
static av_always_inline int check_intra_mode(VP9Context *s, int mode, uint8_t **a, uint8_t *dst_edge, ptrdiff_t stride_edge, uint8_t *dst_inner, ptrdiff_t stride_inner, uint8_t *l, int col, int x, int w, int row, int y, enum TxfmMode tx, int p)
Definition: vp9block.c:934
uint8_t * above_y_nnz_ctx
Definition: vp9.h:410
AVFrame * f
Definition: thread.h:36
static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t(*tree)[2], const uint8_t *probs)
Definition: vp56.h:387
VP9MVRefPair * mv
Definition: vp9.h:234
VideoDSPContext vdsp
Definition: vp9.h:281
BlockPartition
Definition: vp9.h:82
ProbContext p
Definition: vp9.h:366
uint8_t tx32p[2][3]
Definition: vp9.h:112
uint8_t left_uv_nnz_ctx[2][8]
Definition: vp9.h:411
struct VP9Context::@106 prob
unsigned skip[3][2]
Definition: vp9.h:387
BlockLevel
Definition: vp9.h:240
int row
Definition: vp9.h:271
ptrdiff_t y_stride
Definition: vp9.h:273
void(* intra_pred[N_TXFM_SIZES][N_INTRA_PRED_MODES])(uint8_t *dst, ptrdiff_t stride, const uint8_t *left, const uint8_t *top)
Definition: vp9.h:147
Definition: vp9.h:264
uint8_t * above_partition_ctx
Definition: vp9.h:407
#define MAX_SEGMENT
Definition: vp9.h:346
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
unsigned cols
Definition: vp9.h:364
uint8_t ref[2]
Definition: vp9.h:265
int stride
Definition: mace.c:144
uint8_t comp_ref[5]
Definition: vp9.h:111
const uint8_t ff_vp9_default_kf_ymode_probs[10][10][9]
Definition: vp9data.c:77
#define AV_WN32A(p, v)
Definition: intreadwrite.h:469
#define AV_COPY32(d, s)
Definition: intreadwrite.h:517
uint8_t * above_ref_ctx
Definition: vp9.h:417
uint8_t update_map
Definition: vp9.h:345
Definition: vp9.h:39
uint8_t * intra_pred_data[3]
Definition: vp9.h:422
const uint8_t ff_vp9_default_kf_uvmode_probs[10][9]
Definition: vp9data.c:191
int y
Definition: vp9.h:429
uint8_t * above_comp_ctx
Definition: vp9.h:416
uint8_t varcompref[2]
Definition: vp9.h:319
#define AV_RN32A(p)
Definition: intreadwrite.h:457
unsigned uv_mode[10][10]
Definition: vp9.h:377
vp9_mc_func mc[5][4][2][2][2]
Definition: vp9.h:210
int16_t y
Definition: vp56.h:68
uint8_t coef[4][2][2][6][6][3]
Definition: vp9.h:367
uint8_t
VP9Frame frames[2]
Definition: vp9.h:325
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
static int inter_recon(AVCodecContext *avctx)
Definition: vp9block.c:1242
unsigned y_mode[4][10]
Definition: vp9.h:376
TxfmType
Definition: vp9.h:47
uint8_t * above_mode_ctx
Definition: vp9.h:408
int col
Definition: vp9.h:271
struct VP9Context::@103::@109 feat[MAX_SEGMENT]
#define b
Definition: input.c:52
Definition: vp9.h:56
static av_always_inline void mask_edges(VP9Filter *lflvl, int is_uv, int row_and_7, int col_and_7, int w, int h, int col_end, int row_end, enum TxfmMode tx, int skip_inter)
Definition: vp9block.c:1439
static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off)
Definition: vp9block.c:1077
#define CUR_FRAME
Definition: vp9.h:323
uint8_t * above_segpred_ctx
Definition: vp9.h:414
uint8_t skip[3]
Definition: vp9.h:115
uint8_t tmp_y[64 *64]
Definition: vp9.h:430
VP9DSPContext dsp
Definition: vp9.h:280
uint8_t lim_lut[64]
Definition: vp9.h:330
Definition: vp9.h:48
uint8_t mode[4]
Definition: vp9.h:265
Definition: vp9.h:224
uint8_t left_ref_ctx[8]
Definition: vp9.h:417
int x
Definition: vp9.h:429
Definition: vp9.h:40
int16_t * block
Definition: vp9.h:427
uint8_t uv_mode[10][9]
Definition: vp9.h:105
#define ROUNDED_DIV(a, b)
Definition: common.h:52
uint8_t fixcompref
Definition: vp9.h:313
uint8_t mask[2][2][8][4]
Definition: vp9.h:227
int16_t * uvblock[2]
Definition: vp9.h:427
Definition: vp9.h:38
uint8_t keyframe
Definition: vp9.h:299
static av_always_inline void mc_chroma_dir(VP9Context *s, vp9_mc_func(*mc)[2], uint8_t *dst_u, uint8_t *dst_v, ptrdiff_t dst_stride, const uint8_t *ref_u, ptrdiff_t src_stride_u, const uint8_t *ref_v, ptrdiff_t src_stride_v, ThreadFrame *ref_frame, ptrdiff_t y, ptrdiff_t x, const VP56mv *mv, int bw, int bh, int w, int h)
Definition: vp9block.c:1189
int8_t sharpness
Definition: vp9.h:329
unsigned mv_mode[7][4]
Definition: vp9.h:379
enum CompPredMode comppredmode
Definition: vp9.h:404
uint8_t left_partition_ctx[8]
Definition: vp9.h:407
const int8_t ff_vp9_intramode_tree[9][2]
Definition: vp9data.c:65
uint8_t intra
Definition: vp9.h:265
Definition: vp9.h:259
TxfmMode
Definition: vp9.h:37
simple assert() macros that are a bit more flexible than ISO C assert().
uint8_t refidx[3]
Definition: vp9.h:317
uint8_t * above_txfm_ctx
Definition: vp9.h:413
uint8_t intra[4]
Definition: vp9.h:108
unsigned comp[5][2]
Definition: vp9.h:381
unsigned tx8p[2][2]
Definition: vp9.h:386
const int16_t(*[5][4] ff_vp9_scans_nb)[2]
Definition: vp9data.c:1043
#define FFMAX(a, b)
Definition: common.h:64
struct VP9Context::@101 filter
static int decode_coeffs(AVCodecContext *avctx)
Definition: vp9block.c:826
const uint8_t ff_vp9_model_pareto8[256][8]
Definition: vp9data.c:1062
uint8_t * above_filter_ctx
Definition: vp9.h:418
uint8_t comp[5]
Definition: vp9.h:109
struct VP9Context::@108 max_mv
const int16_t * ff_vp9_scans[5][4]
Definition: vp9data.c:486
#define FFMIN(a, b)
Definition: common.h:66
VP56mv left_mv_ctx[16][2]
Definition: vp9.h:419
uint8_t left_y_nnz_ctx[16]
Definition: vp9.h:410
struct VP9Context::@107 counts
const int8_t ff_vp9_inter_mode_tree[3][2]
Definition: vp9data.c:204
uint8_t level[8 *8]
Definition: vp9.h:225
int width
picture width / height.
Definition: avcodec.h:1580
uint8_t left_mode_ctx[16]
Definition: vp9.h:408
unsigned eob[4][2][2][6][6][2]
Definition: vp9.h:401
uint8_t tx16p[2][2]
Definition: vp9.h:113
void(* emulated_edge_mc)(uint8_t *buf, const uint8_t *src, ptrdiff_t buf_linesize, ptrdiff_t src_linesize, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:52
unsigned tx32p[2][4]
Definition: vp9.h:384
unsigned tx16p[2][3]
Definition: vp9.h:385
enum FilterMode filtermode
Definition: vp9.h:311
const int8_t ff_vp9_segmentation_tree[7][2]
Definition: vp9data.c:55
uint8_t * dst[3]
Definition: vp9.h:272
uint8_t uvmode
Definition: vp9.h:265
enum FilterMode ff_vp9_filter_lut[3]
Definition: vp9data.c:215
uint8_t left_comp_ctx[8]
Definition: vp9.h:416
#define AV_WN16A(p, v)
Definition: intreadwrite.h:465
uint8_t mv_mode[7][3]
Definition: vp9.h:107
#define vp56_rac_get_prob
Definition: vp56.h:244
static const uint8_t bwh_tab[2][N_BS_SIZES][2]
Definition: vp9block.c:33
unsigned tile_col_start
Definition: vp9.h:362
unsigned intra[4][2]
Definition: vp9.h:380
if(ac->has_optimized_func)
static const float pred[4]
Definition: siprdata.h:259
unsigned rows
Definition: vp9.h:364
Definition: vp9.h:92
struct VP9Context::@103 segmentation
unsigned sb_cols
Definition: vp9.h:364
Definition: vp9.h:258
static const int8_t mv[256][2]
Definition: 4xm.c:75
uint8_t enabled
Definition: vp9.h:334
static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
Definition: vp56.h:261
NULL
Definition: eval.c:55
VP56mv(* above_mv_ctx)[2]
Definition: vp9.h:419
Libavcodec external API header.
uint8_t filter[4][2]
Definition: vp9.h:106
uint8_t level
Definition: vp9.h:328
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
int pass
Definition: vp9.h:292
BlockSize
Definition: vp9.h:247
#define LAST_FRAME
Definition: vp9.h:324
uint8_t left_skip_ctx[8]
Definition: vp9.h:412
main external API structure.
Definition: avcodec.h:1409
uint8_t left_txfm_ctx[8]
Definition: vp9.h:413
enum TxfmType ff_vp9_intra_txfm_type[14]
Definition: vp9data.c:291
enum TxfmMode tx uvtx
Definition: vp9.h:269
unsigned single_ref[5][2][2]
Definition: vp9.h:382
ThreadFrame refs[8]
Definition: vp9.h:321
int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col, VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl, enum BlockPartition bp)
Definition: vp9block.c:1572
void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb)
Definition: vp9mvs.c:284
uint8_t temporal
Definition: vp9.h:343
uint8_t tx8p[2]
Definition: vp9.h:114
uint8_t seg_id
Definition: vp9.h:265
uint8_t y_mode[4][9]
Definition: vp9.h:104
uint8_t left_filter_ctx[8]
Definition: vp9.h:418
uint8_t intraonly
Definition: vp9.h:307
static int step
Definition: avplay.c:247
uint8_t signbias[3]
Definition: vp9.h:318
enum BlockSize bs
Definition: vp9.h:268
uint8_t tmp_uv[2][32 *32]
Definition: vp9.h:431
VP56mv mv[4][2]
Definition: vp9.h:267
uint8_t * above_skip_ctx
Definition: vp9.h:412
enum BlockPartition bp
Definition: vp9.h:276
enum TxfmMode txfmmode
Definition: vp9.h:403
uint8_t * uveob[2]
Definition: vp9.h:428
uint8_t single_ref[5][2]
Definition: vp9.h:110
Definition: vp56.h:66
Definition: vp9.h:260
uint8_t comp
Definition: vp9.h:265
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
Definition: vp9.h:58
ptrdiff_t uv_stride
Definition: vp9.h:273
void(* vp9_mc_func)(uint8_t *dst, const uint8_t *ref, ptrdiff_t dst_stride, ptrdiff_t ref_stride, int h, int mx, int my)
Definition: vp9.h:130
void(* itxfm_add[N_TXFM_SIZES+1][N_TXFM_TYPES])(uint8_t *dst, ptrdiff_t stride, int16_t *block, int eob)
Definition: vp9.h:166
uint8_t seg[7]
Definition: vp9.h:372
int16_t x
Definition: vp56.h:67
common internal api header.
static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
Definition: vp3.c:1906
uint8_t segpred[3]
Definition: vp9.h:373
static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
Definition: vp56.h:298
Core video DSP helper functions.
uint8_t edge_emu_buffer[71 *80]
Definition: vp9.h:424
enum BlockLevel bl
Definition: vp9.h:275
void * priv_data
Definition: avcodec.h:1451
enum FilterMode filter
Definition: vp9.h:266
static av_always_inline void mc_luma_dir(VP9Context *s, vp9_mc_func(*mc)[2], uint8_t *dst, ptrdiff_t dst_stride, const uint8_t *ref, ptrdiff_t ref_stride, ThreadFrame *ref_frame, ptrdiff_t y, ptrdiff_t x, const VP56mv *mv, int bw, int bh, int w, int h)
Definition: vp9block.c:1151
Definition: vp9.h:41
const int8_t ff_vp9_filter_tree[2][2]
Definition: vp9data.c:210
#define AV_RN16A(p)
Definition: intreadwrite.h:453
uint8_t * above_intra_ctx
Definition: vp9.h:415
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:111
static int decode_block_coeffs(VP56RangeCoder *c, int16_t *coef, int n_coeffs, enum TxfmMode tx, unsigned(*cnt)[6][3], unsigned(*eob)[6][2], uint8_t(*p)[6][11], int nnz, const int16_t *scan, const int16_t(*nb)[2], const int16_t *band_counts, const int16_t *qmul)
Definition: vp9block.c:718
#define av_always_inline
Definition: attributes.h:40
int last_uses_2pass
Definition: vp9.h:294
Definition: vp9.h:257
struct VP9Context::@104 tiling
uint8_t skip
Definition: vp9.h:265
uint8_t left_intra_ctx[8]
Definition: vp9.h:415
uint8_t * above_uv_nnz_ctx[2]
Definition: vp9.h:411
VP56RangeCoder c
Definition: vp9.h:283