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