]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp9block.c
lavc: Add spherical packet side data API
[ffmpeg] / libavcodec / vp9block.c
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] = {
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
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) {
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]]))) {
73         if (!s->errorres) {
74             uint8_t *refsegmap = s->frames[LAST_FRAME].segmentation_map;
75             int pred = MAX_SEGMENT - 1;
76             int x;
77
78             if (!s->last_uses_2pass)
79                 ff_thread_await_progress(&s->frames[LAST_FRAME].tf, row >> 3, 0);
80
81             for (y = 0; y < h4; y++)
82                 for (x = 0; x < w4; x++)
83                     pred = FFMIN(pred,
84                                  refsegmap[(y + row) * 8 * s->sb_cols + x + col]);
85             b->seg_id = pred;
86         } else {
87             b->seg_id = 0;
88         }
89
90         memset(&s->above_segpred_ctx[col], 1, w4);
91         memset(&s->left_segpred_ctx[row7], 1, h4);
92     } else {
93         b->seg_id = vp8_rac_get_tree(&s->c, ff_vp9_segmentation_tree,
94                                      s->prob.seg);
95
96         memset(&s->above_segpred_ctx[col], 0, w4);
97         memset(&s->left_segpred_ctx[row7], 0, h4);
98     }
99     if ((s->segmentation.enabled && s->segmentation.update_map) || s->keyframe) {
100         uint8_t *segmap = s->frames[CUR_FRAME].segmentation_map;
101
102         for (y = 0; y < h4; y++)
103             memset(&segmap[(y + row) * 8 * s->sb_cols + col],
104                    b->seg_id, w4);
105     }
106
107     b->skip = s->segmentation.enabled &&
108               s->segmentation.feat[b->seg_id].skip_enabled;
109     if (!b->skip) {
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]++;
113     }
114
115     if (s->keyframe || s->intraonly) {
116         b->intra = 1;
117     } else if (s->segmentation.feat[b->seg_id].ref_enabled) {
118         b->intra = !s->segmentation.feat[b->seg_id].ref_val;
119     } else {
120         int c, bit;
121
122         if (have_a && have_l) {
123             c  = s->above_intra_ctx[col] + s->left_intra_ctx[row7];
124             c += (c == 2);
125         } else {
126             c = have_a ? 2 * s->above_intra_ctx[col] :
127                 have_l ? 2 * s->left_intra_ctx[row7] : 0;
128         }
129         bit = vp56_rac_get_prob(&s->c, s->prob.p.intra[c]);
130         s->counts.intra[c][bit]++;
131         b->intra = !bit;
132     }
133
134     if ((b->intra || !b->skip) && s->txfmmode == TX_SWITCHABLE) {
135         int c;
136         if (have_a) {
137             if (have_l) {
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;
142             } else {
143                 c = s->above_skip_ctx[col] ? 1 :
144                     (s->above_txfm_ctx[col] * 2 > max_tx);
145             }
146         } else if (have_l) {
147             c = s->left_skip_ctx[row7] ? 1 :
148                 (s->left_txfm_ctx[row7] * 2 > max_tx);
149         } else {
150             c = 1;
151         }
152         switch (max_tx) {
153         case TX_32X32:
154             b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][0]);
155             if (b->tx) {
156                 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][1]);
157                 if (b->tx == 2)
158                     b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][2]);
159             }
160             s->counts.tx32p[c][b->tx]++;
161             break;
162         case TX_16X16:
163             b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][0]);
164             if (b->tx)
165                 b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][1]);
166             s->counts.tx16p[c][b->tx]++;
167             break;
168         case TX_8X8:
169             b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx8p[c]);
170             s->counts.tx8p[c][b->tx]++;
171             break;
172         case TX_4X4:
173             b->tx = TX_4X4;
174             break;
175         }
176     } else {
177         b->tx = FFMIN(max_tx, s->txfmmode);
178     }
179
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];
183
184         b->comp = 0;
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
188             // simpler for now
189             b->mode[0] =
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]]);
195                 l[0]       =
196                 a[1]       = b->mode[1];
197             } else {
198                 l[0]       =
199                 a[1]       =
200                 b->mode[1] = b->mode[0];
201             }
202             if (b->bs != BS_4x8) {
203                 b->mode[2] =
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]]);
209                     l[1]       =
210                     a[1]       = b->mode[3];
211                 } else {
212                     l[1]       =
213                     a[1]       =
214                     b->mode[3] = b->mode[2];
215                 }
216             } else {
217                 b->mode[2] = b->mode[0];
218                 l[1]       =
219                 a[1]       =
220                 b->mode[3] = b->mode[1];
221             }
222         } else {
223             b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
224                                           ff_vp9_default_kf_ymode_probs[*a][*l]);
225             b->mode[3] =
226             b->mode[2] =
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]);
231         }
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) {
235         b->comp = 0;
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]]++;
244             } else {
245                 b->mode[1] = b->mode[0];
246             }
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]]++;
255                 } else {
256                     b->mode[3] = b->mode[2];
257                 }
258             } else {
259                 b->mode[2] = b->mode[0];
260                 b->mode[3] = b->mode[1];
261             }
262         } else {
263             static const uint8_t size_group[10] = {
264                 3, 3, 3, 3, 2, 2, 2, 1, 1, 1
265             };
266             int sz = size_group[b->bs];
267
268             b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
269                                           s->prob.p.y_mode[sz]);
270             b->mode[1] =
271             b->mode[2] =
272             b->mode[3] = b->mode[0];
273             s->counts.y_mode[sz][b->mode[3]]++;
274         }
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]++;
278     } else {
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 },
294         };
295
296         if (s->segmentation.feat[b->seg_id].ref_enabled) {
297             av_assert2(s->segmentation.feat[b->seg_id].ref_val != 0);
298             b->comp   = 0;
299             b->ref[0] = s->segmentation.feat[b->seg_id].ref_val - 1;
300         } else {
301             // read comp_pred flag
302             if (s->comppredmode != PRED_SWITCHABLE) {
303                 b->comp = s->comppredmode == PRED_COMPREF;
304             } else {
305                 int c;
306
307                 // FIXME add intra as ref=0xff (or -1) to make these easier?
308                 if (have_a) {
309                     if (have_l) {
310                         if (s->above_comp_ctx[col] && s->left_comp_ctx[row7]) {
311                             c = 4;
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);
318                         } else {
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);
323                         }
324                     } else {
325                         c = s->above_comp_ctx[col] ? 3 :
326                             (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->fixcompref);
327                     }
328                 } else if (have_l) {
329                     c = s->left_comp_ctx[row7] ? 3 :
330                         (!s->left_intra_ctx[row7] && s->left_ref_ctx[row7] == s->fixcompref);
331                 } else {
332                     c = 1;
333                 }
334                 b->comp = vp56_rac_get_prob(&s->c, s->prob.p.comp[c]);
335                 s->counts.comp[c][b->comp]++;
336             }
337
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;
343
344                 b->ref[fix_idx] = s->fixcompref;
345                 // FIXME can this codeblob be replaced by some sort of LUT?
346                 if (have_a) {
347                     if (have_l) {
348                         if (s->above_intra_ctx[col]) {
349                             if (s->left_intra_ctx[row7]) {
350                                 c = 2;
351                             } else {
352                                 c = 1 + 2 * (s->left_ref_ctx[row7] != s->varcompref[1]);
353                             }
354                         } else if (s->left_intra_ctx[row7]) {
355                             c = 1 + 2 * (s->above_ref_ctx[col] != s->varcompref[1]);
356                         } else {
357                             int refl = s->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
358
359                             if (refl == refa && refa == s->varcompref[1]) {
360                                 c = 0;
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])) {
364                                     c = 4;
365                                 } else {
366                                     c = (refa == refl) ? 3 : 1;
367                                 }
368                             } else if (!s->left_comp_ctx[row7]) {
369                                 if (refa == s->varcompref[1] && refl != s->varcompref[1]) {
370                                     c = 1;
371                                 } else {
372                                     c = (refl == s->varcompref[1] &&
373                                          refa != s->varcompref[1]) ? 2 : 4;
374                                 }
375                             } else if (!s->above_comp_ctx[col]) {
376                                 if (refl == s->varcompref[1] && refa != s->varcompref[1]) {
377                                     c = 1;
378                                 } else {
379                                     c = (refa == s->varcompref[1] &&
380                                          refl != s->varcompref[1]) ? 2 : 4;
381                                 }
382                             } else {
383                                 c = (refl == refa) ? 4 : 2;
384                             }
385                         }
386                     } else {
387                         if (s->above_intra_ctx[col]) {
388                             c = 2;
389                         } else if (s->above_comp_ctx[col]) {
390                             c = 4 * (s->above_ref_ctx[col] != s->varcompref[1]);
391                         } else {
392                             c = 3 * (s->above_ref_ctx[col] != s->varcompref[1]);
393                         }
394                     }
395                 } else if (have_l) {
396                     if (s->left_intra_ctx[row7]) {
397                         c = 2;
398                     } else if (s->left_comp_ctx[row7]) {
399                         c = 4 * (s->left_ref_ctx[row7] != s->varcompref[1]);
400                     } else {
401                         c = 3 * (s->left_ref_ctx[row7] != s->varcompref[1]);
402                     }
403                 } else {
404                     c = 2;
405                 }
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 */
410                 int bit, c;
411
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]);
418                             } else {
419                                 c = (3 * !s->above_ref_ctx[col]) +
420                                     (!s->fixcompref || !s->left_ref_ctx[row7]);
421                             }
422                         } else if (s->above_comp_ctx[col]) {
423                             c = (3 * !s->left_ref_ctx[row7]) +
424                                 (!s->fixcompref || !s->above_ref_ctx[col]);
425                         } else {
426                             c = 2 * !s->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
427                         }
428                     } else if (s->above_intra_ctx[col]) {
429                         c = 2;
430                     } else if (s->above_comp_ctx[col]) {
431                         c = 1 + (!s->fixcompref || !s->above_ref_ctx[col]);
432                     } else {
433                         c = 4 * (!s->above_ref_ctx[col]);
434                     }
435                 } else if (have_l && !s->left_intra_ctx[row7]) {
436                     if (s->left_intra_ctx[row7]) {
437                         c = 2;
438                     } else if (s->left_comp_ctx[row7]) {
439                         c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7]);
440                     } else {
441                         c = 4 * (!s->left_ref_ctx[row7]);
442                     }
443                 } else {
444                     c = 2;
445                 }
446                 bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][0]);
447                 s->counts.single_ref[c][0][bit]++;
448                 if (!bit) {
449                     b->ref[0] = 0;
450                 } else {
451                     // FIXME can this codeblob be replaced by some sort of LUT?
452                     if (have_a) {
453                         if (have_l) {
454                             if (s->left_intra_ctx[row7]) {
455                                 if (s->above_intra_ctx[col]) {
456                                     c = 2;
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]) {
461                                     c = 3;
462                                 } else {
463                                     c = 4 * (s->above_ref_ctx[col] == 1);
464                                 }
465                             } else if (s->above_intra_ctx[col]) {
466                                 if (s->left_intra_ctx[row7]) {
467                                     c = 2;
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]) {
472                                     c = 3;
473                                 } else {
474                                     c = 4 * (s->left_ref_ctx[row7] == 1);
475                                 }
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);
481                                     } else {
482                                         c = 2;
483                                     }
484                                 } else if (!s->left_ref_ctx[row7]) {
485                                     c = 1 + 2 * (s->fixcompref == 1 ||
486                                                  s->above_ref_ctx[col] == 1);
487                                 } else {
488                                     c = 3 * (s->left_ref_ctx[row7] == 1) +
489                                         (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
490                                 }
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);
495                                 } else {
496                                     c = 3 * (s->above_ref_ctx[col] == 1) +
497                                         (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
498                                 }
499                             } else if (!s->above_ref_ctx[col]) {
500                                 if (!s->left_ref_ctx[row7]) {
501                                     c = 3;
502                                 } else {
503                                     c = 4 * (s->left_ref_ctx[row7] == 1);
504                                 }
505                             } else if (!s->left_ref_ctx[row7]) {
506                                 c = 4 * (s->above_ref_ctx[col] == 1);
507                             } else {
508                                 c = 2 * (s->left_ref_ctx[row7] == 1) +
509                                     2 * (s->above_ref_ctx[col] == 1);
510                             }
511                         } else {
512                             if (s->above_intra_ctx[col] ||
513                                 (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
514                                 c = 2;
515                             } else if (s->above_comp_ctx[col]) {
516                                 c = 3 * (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
517                             } else {
518                                 c = 4 * (s->above_ref_ctx[col] == 1);
519                             }
520                         }
521                     } else if (have_l) {
522                         if (s->left_intra_ctx[row7] ||
523                             (!s->left_comp_ctx[row7] && !s->left_ref_ctx[row7])) {
524                             c = 2;
525                         } else if (s->left_comp_ctx[row7]) {
526                             c = 3 * (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
527                         } else {
528                             c = 4 * (s->left_ref_ctx[row7] == 1);
529                         }
530                     } else {
531                         c = 2;
532                     }
533                     bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][1]);
534                     s->counts.single_ref[c][1][bit]++;
535                     b->ref[0] = 1 + bit;
536                 }
537             }
538         }
539
540         if (b->bs <= BS_8x8) {
541             if (s->segmentation.feat[b->seg_id].skip_enabled) {
542                 b->mode[0] =
543                 b->mode[1] =
544                 b->mode[2] =
545                 b->mode[3] = ZEROMV;
546             } else {
547                 static const uint8_t off[10] = {
548                     3, 0, 0, 1, 0, 0, 0, 0, 0, 0
549                 };
550
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]]];
555
556                 b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
557                                               s->prob.p.mv_mode[c]);
558                 b->mode[1] =
559                 b->mode[2] =
560                 b->mode[3] = b->mode[0];
561                 s->counts.mv_mode[c][b->mode[0] - 10]++;
562             }
563         }
564
565         if (s->filtermode == FILTER_SWITCHABLE) {
566             int c;
567
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;
572                 } else {
573                     c = s->above_filter_ctx[col];
574                 }
575             } else if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
576                 c = s->left_filter_ctx[row7];
577             } else {
578                 c = 3;
579             }
580
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]++;
584         } else {
585             b->filter = s->filtermode;
586         }
587
588         if (b->bs > BS_8x8) {
589             int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][s->left_mode_ctx[row7]];
590
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);
595
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);
601             } else {
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]);
605             }
606
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);
612
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);
618                 } else {
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]);
622                 }
623             } else {
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]);
630             }
631         } else {
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]);
639         }
640     }
641
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];
660         }
661         if (b->bs > BS_8x8) {
662             int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
663
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);
672         } else {
673             int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
674
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);
678             }
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);
682             }
683         }
684
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];
688
689             memset(&s->above_ref_ctx[col], vref, w4);
690             memset(&s->left_ref_ctx[row7], vref, h4);
691         }
692     }
693
694     // FIXME kinda ugly
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];
698
699         if (b->intra) {
700             for (x = 0; x < w4; x++) {
701                 mv[x].ref[0] =
702                 mv[x].ref[1] = -1;
703             }
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]);
710             }
711         } else {
712             for (x = 0; x < w4; x++) {
713                 mv[x].ref[0] = b->ref[0];
714                 mv[x].ref[1] = -1;
715                 AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
716             }
717         }
718     }
719 }
720
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)
728 {
729     int i = 0, band = 0, band_left = band_counts[band];
730     uint8_t *tp = p[0][nnz];
731     uint8_t cache[1024];
732
733     do {
734         int val, rc;
735
736         val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
737         eob[band][nnz][val]++;
738         if (!val)
739             break;
740
741 skip_eob:
742         if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
743             cnt[band][nnz][0]++;
744             if (!--band_left)
745                 band_left = band_counts[++band];
746             cache[scan[i]] = 0;
747             nnz            = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
748             tp             = p[band][nnz];
749             if (++i == n_coeffs)
750                 break;  //invalid input; blocks should end with EOB
751             goto skip_eob;
752         }
753
754         rc = scan[i];
755         if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
756             cnt[band][nnz][1]++;
757             val       = 1;
758             cache[rc] = 1;
759         } else {
760             // fill in p[3-10] (model fill) - only once per frame for each pos
761             if (!tp[3])
762                 memcpy(&tp[3], ff_vp9_model_pareto8[tp[2]], 8);
763
764             cnt[band][nnz][2]++;
765             if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
766                 if (!vp56_rac_get_prob_branchy(c, tp[4])) {
767                     cache[rc] = val = 2;
768                 } else {
769                     val       = 3 + vp56_rac_get_prob(c, tp[5]);
770                     cache[rc] = 3;
771                 }
772             } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
773                 cache[rc] = 4;
774                 if (!vp56_rac_get_prob_branchy(c, tp[7])) {
775                     val  =  vp56_rac_get_prob(c, 159) + 5;
776                 } else {
777                     val  = (vp56_rac_get_prob(c, 165) << 1) + 7;
778                     val +=  vp56_rac_get_prob(c, 145);
779                 }
780             } else { // cat 3-6
781                 cache[rc] = 5;
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);
787                     } else {
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);
792                     }
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);
799                 } else {
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);
814                 }
815             }
816         }
817         if (!--band_left)
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;
821         else
822             coef[rc] = (vp8_rac_get(c) ? -val : val) * qmul[!!i];
823         nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
824         tp  = p[band][nnz];
825     } while (++i < n_coeffs);
826
827     return i;
828 }
829
830 static int decode_coeffs(AVCodecContext *avctx)
831 {
832     VP9Context *s = avctx->priv_data;
833     VP9Block *b = s->b;
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 **yscans = ff_vp9_scans[tx];
846     const int16_t (**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 },
856     };
857     const int16_t *y_band_counts  = band_counts[b->tx];
858     const int16_t *uv_band_counts = band_counts[b->uvtx];
859
860     /* y tokens */
861     if (b->tx > TX_4X4) { // FIXME slow
862         for (y = 0; y < end_y; y += step1d)
863             for (x = 1; x < step1d; x++)
864                 l[y] |= l[y + x];
865         for (x = 0; x < end_x; x += step1d)
866             for (y = 1; y < step1d; y++)
867                 a[x] |= a[x + y];
868     }
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 &&
872                                                                 b->bs > BS_8x8 ?
873                                                                 n : 0]];
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,
878                                            qmul[0])) < 0)
879                 return ret;
880             a[x] = l[y] = !!ret;
881             if (b->tx > TX_8X8)
882                 AV_WN16A(&s->eob[n], ret);
883             else
884                 s->eob[n] = ret;
885         }
886     }
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));
892     }
893
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];
897     w4    >>= 1;
898     h4    >>= 1;
899     end_x >>= 1;
900     end_y >>= 1;
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++)
907                     l[y] |= l[y + x];
908             for (x = 0; x < end_x; x += uvstep1d)
909                 for (y = 1; y < uvstep1d; y++)
910                     a[x] |= a[x + y];
911         }
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,
917                                                nnz, uvscan, uvnb,
918                                                uv_band_counts, qmul[1])) < 0)
919                     return ret;
920                 a[x] = l[y] = !!ret;
921                 if (b->uvtx > TX_8X8)
922                     AV_WN16A(&s->uveob[pl][n], ret);
923                 else
924                     s->uveob[pl][n] = ret;
925             }
926         }
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));
932         }
933     }
934
935     return 0;
936 }
937
938 static av_always_inline int check_intra_mode(VP9Context *s, int mode,
939                                              uint8_t **a,
940                                              uint8_t *dst_edge,
941                                              ptrdiff_t stride_edge,
942                                              uint8_t *dst_inner,
943                                              ptrdiff_t stride_inner,
944                                              uint8_t *l, int col, int x, int w,
945                                              int row, int y, enum TxfmMode tx,
946                                              int p)
947 {
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          } },
972     };
973     static const struct {
974         uint8_t needs_left:1;
975         uint8_t needs_top: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 }
998     };
999
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;
1006
1007         if (tx == TX_4X4 && edges[mode].needs_topright && have_right)
1008             n_px_need_tr = 4;
1009
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)
1013         if (have_top) {
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];
1017             if (have_left)
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];
1022         }
1023
1024         if (have_top &&
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) {
1028             *a = top;
1029         } else {
1030             if (have_top) {
1031                 if (n_px_need <= n_px_have) {
1032                     memcpy(*a, top, n_px_need);
1033                 } else {
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);
1037                 }
1038             } else {
1039                 memset(*a, 127, n_px_need);
1040             }
1041             if (edges[mode].needs_topleft) {
1042                 if (have_left && have_top)
1043                     (*a)[-1] = topleft[-1];
1044                 else
1045                     (*a)[-1] = have_top ? 129 : 127;
1046             }
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);
1051                 } else {
1052                     memset(&(*a)[4], (*a)[3], 4);
1053                 }
1054             }
1055         }
1056     }
1057     if (edges[mode].needs_left) {
1058         if (have_left) {
1059             int i;
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;
1064
1065             if (n_px_need <= n_px_have) {
1066                 for (i = 0; i < n_px_need; i++)
1067                     l[i] = dst[i * stride - 1];
1068             } else {
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);
1072             }
1073         } else {
1074             memset(l, 129, 4 << tx);
1075         }
1076     }
1077
1078     return mode;
1079 }
1080
1081 static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off)
1082 {
1083     VP9Context *s = avctx->priv_data;
1084     VP9Block *b = s->b;
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;
1094
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 ?
1100                                y * 2 + x : 0];
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];
1105
1106             mode = check_intra_mode(s, mode, &a, ptr_r,
1107                                     f->linesize[0],
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);
1111             if (eob)
1112                 s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride,
1113                                            s->block + 16 * n, eob);
1114         }
1115         dst_r += 4 * f->linesize[0] * step1d;
1116         dst   += 4 * b->y_stride * step1d;
1117     }
1118
1119     // U/V
1120     h4    >>= 1;
1121     w4    >>= 1;
1122     end_x >>= 1;
1123     end_y >>= 1;
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])
1137                                               : s->uveob[p][n];
1138
1139                 mode = check_intra_mode(s, mode, &a, ptr_r,
1140                                         f->linesize[1],
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);
1144                 if (eob)
1145                     s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1146                                                     s->uvblock[p] + 16 * n,
1147                                                     eob);
1148             }
1149             dst_r += 4 * uvstep1d * f->linesize[1];
1150             dst   += 4 * uvstep1d * b->uv_stride;
1151         }
1152     }
1153 }
1154
1155 static av_always_inline void mc_luma_dir(VP9Context *s, vp9_mc_func(*mc)[2],
1156                                          uint8_t *dst, ptrdiff_t dst_stride,
1157                                          const uint8_t *ref,
1158                                          ptrdiff_t ref_stride,
1159                                          ThreadFrame *ref_frame,
1160                                          ptrdiff_t y, ptrdiff_t x,
1161                                          const VP56mv *mv,
1162                                          int bw, int bh, int w, int h)
1163 {
1164     int mx = mv->x, my = mv->y;
1165     int th;
1166
1167     y   += my >> 3;
1168     x   += mx >> 3;
1169     ref += y * ref_stride + x;
1170     mx  &= 7;
1171     my  &= 7;
1172
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);
1177
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,
1186                                  80,
1187                                  ref_stride,
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;
1191         ref_stride = 80;
1192     }
1193     mc[!!mx][!!my](dst, dst_stride, ref, ref_stride, bh, mx << 1, my << 1);
1194 }
1195
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,
1205                                            const VP56mv *mv,
1206                                            int bw, int bh, int w, int h)
1207 {
1208     int mx = mv->x, my = mv->y;
1209     int th;
1210
1211     y     += my >> 4;
1212     x     += mx >> 4;
1213     ref_u += y * src_stride_u + x;
1214     ref_v += y * src_stride_v + x;
1215     mx    &= 15;
1216     my    &= 15;
1217
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);
1222
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,
1231                                  80,
1232                                  src_stride_u,
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);
1237
1238         s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
1239                                  ref_v - !!my * 3 * src_stride_v - !!mx * 3,
1240                                  80,
1241                                  src_stride_v,
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);
1246     } else {
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);
1249     }
1250 }
1251
1252 static int inter_recon(AVCodecContext *avctx)
1253 {
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 },
1257     };
1258     VP9Context *s = avctx->priv_data;
1259     VP9Block *b = s->b;
1260     int row = b->row, col = b->col;
1261
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;
1266
1267     int w = avctx->width, h = avctx->height;
1268     ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride;
1269
1270     if (!ref1->data[0] || (b->comp && !ref2->data[0]))
1271         return AVERROR_INVALIDDATA;
1272
1273     // y inter pred
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);
1283
1284             if (b->comp) {
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);
1292             }
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);
1300
1301             if (b->comp) {
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);
1308             }
1309         } else {
1310             av_assert2(b->bs == BS_4x4);
1311
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);
1328
1329             if (b->comp) {
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);
1344             }
1345         }
1346     } else {
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;
1350
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);
1354
1355         if (b->comp)
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);
1359     }
1360
1361     // uv inter pred
1362     {
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;
1365         VP56mv mvuv;
1366
1367         w = (w + 1) >> 1;
1368         h = (h + 1) >> 1;
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);
1374         } else {
1375             mvuv = b->mv[0][0];
1376         }
1377
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);
1383
1384         if (b->comp) {
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);
1390             } else {
1391                 mvuv = b->mv[0][1];
1392             }
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);
1398         }
1399     }
1400
1401     if (!b->skip) {
1402         /* mostly copied intra_reconn() */
1403
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];
1411
1412         // y itxfm add
1413         for (n = 0, y = 0; y < end_y; y += step1d) {
1414             uint8_t *ptr = dst;
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];
1417
1418                 if (eob)
1419                     s->dsp.itxfm_add[tx][DCT_DCT](ptr, b->y_stride,
1420                                                   s->block + 16 * n, eob);
1421             }
1422             dst += 4 * b->y_stride * step1d;
1423         }
1424
1425         // uv itxfm add
1426         h4    >>= 1;
1427         w4    >>= 1;
1428         end_x >>= 1;
1429         end_y >>= 1;
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) {
1434                 uint8_t *ptr = dst;
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])
1437                                                : s->uveob[p][n];
1438                     if (eob)
1439                         s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
1440                                                         s->uvblock[p] + 16 * n, eob);
1441                 }
1442                 dst += 4 * uvstep1d * b->uv_stride;
1443             }
1444         }
1445     }
1446     return 0;
1447 }
1448
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)
1453 {
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)
1458
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) {
1465         if (h == 1) {
1466             if (row_and_7 & 1)
1467                 return;
1468             if (!row_end)
1469                 h += 1;
1470         }
1471         if (w == 1) {
1472             if (col_and_7 & 1)
1473                 return;
1474             if (!col_end)
1475                 w += 1;
1476         }
1477     }
1478
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;
1482
1483         // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
1484         if (is_uv) {
1485             int m_row_8 = m_col & 0x01, m_row_4 = m_col - m_row_8;
1486
1487             for (y = row_and_7; y < h + row_and_7; y++) {
1488                 int col_mask_id = 2 - !(y & 7);
1489
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:
1494                 // .---. <-- a
1495                 // |   |
1496                 // |___| <-- b
1497                 // ^   ^
1498                 // c   d
1499                 //
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;
1504                 } else {
1505                     lflvl->mask[is_uv][1][y][col_mask_id] |= m_col;
1506                 }
1507             }
1508         } else {
1509             int m_row_8 = m_col & 0x11, m_row_4 = m_col - m_row_8;
1510
1511             for (y = row_and_7; y < h + row_and_7; y++) {
1512                 int col_mask_id = 2 - !(y & 3);
1513
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;
1519             }
1520         }
1521     } else {
1522         int y, t = 1 << col_and_7, m_col = (t << w) - t;
1523
1524         if (!skip_inter) {
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];
1529
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;
1535
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;
1539                 }
1540             } else {
1541                 for (y = row_and_7; y < h + row_and_7; y++)
1542                     lflvl->mask[is_uv][0][y][mask_id] |= m_row;
1543             }
1544
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;
1550             } else {
1551                 for (y = row_and_7; y < h + row_and_7; y += step1d)
1552                     lflvl->mask[is_uv][1][y][mask_id] |= m_col;
1553             }
1554         } else if (tx != TX_4X4) {
1555             int mask_id;
1556
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;
1562         } else if (is_uv) {
1563             int t8 = t & 0x01, t4 = t - t8;
1564
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;
1568             }
1569             lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 7)] |= m_col;
1570         } else {
1571             int t8 = t & 0x11, t4 = t - t8;
1572
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;
1576             }
1577             lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 3)] |= m_col;
1578         }
1579     }
1580 }
1581
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)
1585 {
1586     VP9Context *s = avctx->priv_data;
1587     VP9Block *b = s->b;
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;
1591     int emu[2];
1592
1593     b->row  = row;
1594     b->row7 = row & 7;
1595     b->col  = col;
1596     b->col7 = col & 7;
1597
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;
1602
1603     if (s->pass < 2) {
1604         b->bs = bs;
1605         b->bl = bl;
1606         b->bp = bp;
1607         decode_mode(s, b);
1608         b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx));
1609
1610         if (!b->skip) {
1611             if ((ret = decode_coeffs(avctx)) < 0)
1612                 return ret;
1613         } else {
1614             int pl;
1615
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);
1621             }
1622         }
1623
1624         if (s->pass == 1) {
1625             s->b++;
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;
1632
1633             return 0;
1634         }
1635     }
1636
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;
1644     if (emu[0]) {
1645         b->dst[0]   = s->tmp_y;
1646         b->y_stride = 64;
1647     } else {
1648         b->dst[0]   = f->data[0] + yoff;
1649         b->y_stride = f->linesize[0];
1650     }
1651     if (emu[1]) {
1652         b->dst[1]    = s->tmp_uv[0];
1653         b->dst[2]    = s->tmp_uv[1];
1654         b->uv_stride = 32;
1655     } else {
1656         b->dst[1]    = f->data[1] + uvoff;
1657         b->dst[2]    = f->data[2] + uvoff;
1658         b->uv_stride = f->linesize[1];
1659     }
1660     if (b->intra) {
1661         intra_recon(avctx, yoff, uvoff);
1662     } else {
1663         if ((ret = inter_recon(avctx)) < 0)
1664             return ret;
1665     }
1666     if (emu[0]) {
1667         int w = FFMIN(s->cols - col, w4) * 8;
1668         int h = FFMIN(s->rows - row, h4) * 8;
1669         int n, o = 0;
1670
1671         for (n = 0; o < w; n++) {
1672             int bw = 64 >> n;
1673
1674             av_assert2(n <= 4);
1675             if (w & bw) {
1676                 s->dsp.mc[n][0][0][0][0](f->data[0] + yoff + o,
1677                                          f->linesize[0],
1678                                          s->tmp_y + o,
1679                                          64, h, 0, 0);
1680                 o += bw;
1681             }
1682         }
1683     }
1684     if (emu[1]) {
1685         int w = FFMIN(s->cols - col, w4) * 4;
1686         int h = FFMIN(s->rows - row, h4) * 4;
1687         int n, o = 0;
1688
1689         for (n = 1; o < w; n++) {
1690             int bw = 64 >> n;
1691
1692             av_assert2(n <= 4);
1693             if (w & bw) {
1694                 s->dsp.mc[n][0][0][0][0](f->data[1] + uvoff + o,
1695                                          f->linesize[1],
1696                                          s->tmp_uv[0] + o,
1697                                          32, h, 0, 0);
1698                 s->dsp.mc[n][0][0][0][0](f->data[2] + uvoff + o,
1699                                          f->linesize[2],
1700                                          s->tmp_uv[1] + o,
1701                                          32, h, 0, 0);
1702                 o += bw;
1703             }
1704         }
1705     }
1706
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;
1714
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);
1722
1723         if (!s->filter.lim_lut[lvl]) {
1724             int sharp = s->filter.sharpness;
1725             int limit = lvl;
1726
1727             if (sharp > 0) {
1728                 limit >>= (sharp + 3) >> 2;
1729                 limit   = FFMIN(limit, 9 - sharp);
1730             }
1731             limit = FFMAX(limit, 1);
1732
1733             s->filter.lim_lut[lvl]   = limit;
1734             s->filter.mblim_lut[lvl] = 2 * (lvl + 2) + limit;
1735         }
1736     }
1737
1738     if (s->pass == 2) {
1739         s->b++;
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;
1746     }
1747
1748     return 0;
1749 }