GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/vp9block.c Lines: 791 814 97.2 %
Date: 2020-10-23 17:01:47 Branches: 872 930 93.8 %

Line Branch Exec Source
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 FFmpeg.
8
 *
9
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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
#include "vp9dec.h"
33
34
835210
static av_always_inline void setctx_2d(uint8_t *ptr, int w, int h,
35
                                       ptrdiff_t stride, int v)
36
{
37

835210
    switch (w) {
38
599401
    case 1:
39
        do {
40
599401
            *ptr = v;
41
599401
            ptr += stride;
42
599401
        } while (--h);
43
558711
        break;
44
217183
    case 2: {
45
217183
        int v16 = v * 0x0101;
46
        do {
47
399361
            AV_WN16A(ptr, v16);
48
399361
            ptr += stride;
49
399361
        } while (--h);
50
217183
        break;
51
    }
52
47486
    case 4: {
53
47486
        uint32_t v32 = v * 0x01010101;
54
        do {
55
174012
            AV_WN32A(ptr, v32);
56
174012
            ptr += stride;
57
174012
        } while (--h);
58
47486
        break;
59
    }
60
11830
    case 8: {
61
#if HAVE_FAST_64BIT
62
11830
        uint64_t v64 = v * 0x0101010101010101ULL;
63
        do {
64
87928
            AV_WN64A(ptr, v64);
65
87928
            ptr += stride;
66
87928
        } while (--h);
67
#else
68
        uint32_t v32 = v * 0x01010101;
69
        do {
70
            AV_WN32A(ptr,     v32);
71
            AV_WN32A(ptr + 4, v32);
72
            ptr += stride;
73
        } while (--h);
74
#endif
75
11830
        break;
76
    }
77
    }
78
835210
}
79
80
792690
static void decode_mode(VP9TileData *td)
81
{
82
    static const uint8_t left_ctx[N_BS_SIZES] = {
83
        0x0, 0x8, 0x0, 0x8, 0xc, 0x8, 0xc, 0xe, 0xc, 0xe, 0xf, 0xe, 0xf
84
    };
85
    static const uint8_t above_ctx[N_BS_SIZES] = {
86
        0x0, 0x0, 0x8, 0x8, 0x8, 0xc, 0xc, 0xc, 0xe, 0xe, 0xe, 0xf, 0xf
87
    };
88
    static const uint8_t max_tx_for_bl_bp[N_BS_SIZES] = {
89
        TX_32X32, TX_32X32, TX_32X32, TX_32X32, TX_16X16, TX_16X16,
90
        TX_16X16, TX_8X8,   TX_8X8,   TX_8X8,   TX_4X4,   TX_4X4,  TX_4X4
91
    };
92
792690
    VP9Context *s = td->s;
93
792690
    VP9Block *b = td->b;
94
792690
    int row = td->row, col = td->col, row7 = td->row7;
95
792690
    enum TxfmMode max_tx = max_tx_for_bl_bp[b->bs];
96
792690
    int bw4 = ff_vp9_bwh_tab[1][b->bs][0], w4 = FFMIN(s->cols - col, bw4);
97
792690
    int bh4 = ff_vp9_bwh_tab[1][b->bs][1], h4 = FFMIN(s->rows - row, bh4), y;
98
792690
    int have_a = row > 0, have_l = col > td->tile_col_start;
99
    int vref, filter_id;
100
101
792690
    if (!s->s.h.segmentation.enabled) {
102
549032
        b->seg_id = 0;
103

243658
    } else if (s->s.h.keyframe || s->s.h.intraonly) {
104
18592
        b->seg_id = !s->s.h.segmentation.update_map ? 0 :
105
9296
                    vp8_rac_get_tree(td->c, ff_vp9_segmentation_tree, s->s.h.segmentation.prob);
106
234362
    } else if (!s->s.h.segmentation.update_map ||
107

126550
               (s->s.h.segmentation.temporal &&
108
63275
                vp56_rac_get_prob_branchy(td->c,
109
63275
                    s->s.h.segmentation.pred_prob[s->above_segpred_ctx[col] +
110
63275
                                    td->left_segpred_ctx[row7]]))) {
111

227441
        if (!s->s.h.errorres && s->s.frames[REF_FRAME_SEGMAP].segmentation_map) {
112
220485
            int pred = 8, x;
113
220485
            uint8_t *refsegmap = s->s.frames[REF_FRAME_SEGMAP].segmentation_map;
114
115
220485
            if (!s->s.frames[REF_FRAME_SEGMAP].uses_2pass)
116
220485
                ff_thread_await_progress(&s->s.frames[REF_FRAME_SEGMAP].tf, row >> 3, 0);
117
608923
            for (y = 0; y < h4; y++) {
118
388438
                int idx_base = (y + row) * 8 * s->sb_cols + col;
119
1566550
                for (x = 0; x < w4; x++)
120
1178112
                    pred = FFMIN(pred, refsegmap[idx_base + x]);
121
            }
122
            av_assert1(pred < 8);
123
220485
            b->seg_id = pred;
124
        } else {
125
6956
            b->seg_id = 0;
126
        }
127
128
227441
        memset(&s->above_segpred_ctx[col], 1, w4);
129
227441
        memset(&td->left_segpred_ctx[row7], 1, h4);
130
    } else {
131
13842
        b->seg_id = vp8_rac_get_tree(td->c, ff_vp9_segmentation_tree,
132
6921
                                     s->s.h.segmentation.prob);
133
134
6921
        memset(&s->above_segpred_ctx[col], 0, w4);
135
6921
        memset(&td->left_segpred_ctx[row7], 0, h4);
136
    }
137
792690
    if (s->s.h.segmentation.enabled &&
138

243658
        (s->s.h.segmentation.update_map || s->s.h.keyframe || s->s.h.intraonly)) {
139
72571
        setctx_2d(&s->s.frames[CUR_FRAME].segmentation_map[row * 8 * s->sb_cols + col],
140
72571
                  bw4, bh4, 8 * s->sb_cols, b->seg_id);
141
    }
142
143
1036348
    b->skip = s->s.h.segmentation.enabled &&
144
243658
        s->s.h.segmentation.feat[b->seg_id].skip_enabled;
145
792690
    if (!b->skip) {
146
791889
        int c = td->left_skip_ctx[row7] + s->above_skip_ctx[col];
147
791889
        b->skip = vp56_rac_get_prob(td->c, s->prob.p.skip[c]);
148
791889
        td->counts.skip[c][b->skip]++;
149
    }
150
151

792690
    if (s->s.h.keyframe || s->s.h.intraonly) {
152
231621
        b->intra = 1;
153

561069
    } else if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[b->seg_id].ref_enabled) {
154
801
        b->intra = !s->s.h.segmentation.feat[b->seg_id].ref_val;
155
    } else {
156
        int c, bit;
157
158

560268
        if (have_a && have_l) {
159
516971
            c = s->above_intra_ctx[col] + td->left_intra_ctx[row7];
160
516971
            c += (c == 2);
161
        } else {
162
70513
            c = have_a ? 2 * s->above_intra_ctx[col] :
163
27216
                have_l ? 2 * td->left_intra_ctx[row7] : 0;
164
        }
165
560268
        bit = vp56_rac_get_prob(td->c, s->prob.p.intra[c]);
166
560268
        td->counts.intra[c][bit]++;
167
560268
        b->intra = !bit;
168
    }
169
170

792690
    if ((b->intra || !b->skip) && s->s.h.txfmmode == TX_SWITCHABLE) {
171
        int c;
172
375130
        if (have_a) {
173
351872
            if (have_l) {
174
341168
                c = (s->above_skip_ctx[col] ? max_tx :
175
277699
                     s->above_txfm_ctx[col]) +
176
341168
                    (td->left_skip_ctx[row7] ? max_tx :
177
341168
                     td->left_txfm_ctx[row7]) > max_tx;
178
            } else {
179
19764
                c = s->above_skip_ctx[col] ? 1 :
180
9060
                    (s->above_txfm_ctx[col] * 2 > max_tx);
181
            }
182
23258
        } else if (have_l) {
183
41401
            c = td->left_skip_ctx[row7] ? 1 :
184
19419
                (td->left_txfm_ctx[row7] * 2 > max_tx);
185
        } else {
186
1276
            c = 1;
187
        }
188

375130
        switch (max_tx) {
189
10027
        case TX_32X32:
190
10027
            b->tx = vp56_rac_get_prob(td->c, s->prob.p.tx32p[c][0]);
191
10027
            if (b->tx) {
192
9857
                b->tx += vp56_rac_get_prob(td->c, s->prob.p.tx32p[c][1]);
193
9857
                if (b->tx == 2)
194
8802
                    b->tx += vp56_rac_get_prob(td->c, s->prob.p.tx32p[c][2]);
195
            }
196
10027
            td->counts.tx32p[c][b->tx]++;
197
10027
            break;
198
49242
        case TX_16X16:
199
49242
            b->tx = vp56_rac_get_prob(td->c, s->prob.p.tx16p[c][0]);
200
49242
            if (b->tx)
201
46295
                b->tx += vp56_rac_get_prob(td->c, s->prob.p.tx16p[c][1]);
202
49242
            td->counts.tx16p[c][b->tx]++;
203
49242
            break;
204
198915
        case TX_8X8:
205
198915
            b->tx = vp56_rac_get_prob(td->c, s->prob.p.tx8p[c]);
206
198915
            td->counts.tx8p[c][b->tx]++;
207
198915
            break;
208
116946
        case TX_4X4:
209
116946
            b->tx = TX_4X4;
210
116946
            break;
211
        }
212
375130
    } else {
213
417560
        b->tx = FFMIN(max_tx, s->s.h.txfmmode);
214
    }
215
216

792690
    if (s->s.h.keyframe || s->s.h.intraonly) {
217
231621
        uint8_t *a = &s->above_mode_ctx[col * 2];
218
231621
        uint8_t *l = &td->left_mode_ctx[(row7) << 1];
219
220
231621
        b->comp = 0;
221
231621
        if (b->bs > BS_8x8) {
222
            // FIXME the memory storage intermediates here aren't really
223
            // necessary, they're just there to make the code slightly
224
            // simpler for now
225
82000
            b->mode[0] =
226
164000
            a[0]       = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
227
82000
                                          ff_vp9_default_kf_ymode_probs[a[0]][l[0]]);
228
82000
            if (b->bs != BS_8x4) {
229
117524
                b->mode[1] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
230
58762
                                              ff_vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
231
58762
                l[0]       =
232
58762
                a[1]       = b->mode[1];
233
            } else {
234
23238
                l[0]       =
235
23238
                a[1]       =
236
23238
                b->mode[1] = b->mode[0];
237
            }
238
82000
            if (b->bs != BS_4x8) {
239
66690
                b->mode[2] =
240
133380
                a[0]       = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
241
66690
                                              ff_vp9_default_kf_ymode_probs[a[0]][l[1]]);
242
66690
                if (b->bs != BS_8x4) {
243
86904
                    b->mode[3] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
244
43452
                                                  ff_vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
245
43452
                    l[1]       =
246
43452
                    a[1]       = b->mode[3];
247
                } else {
248
23238
                    l[1]       =
249
23238
                    a[1]       =
250
23238
                    b->mode[3] = b->mode[2];
251
                }
252
            } else {
253
15310
                b->mode[2] = b->mode[0];
254
15310
                l[1]       =
255
15310
                a[1]       =
256
15310
                b->mode[3] = b->mode[1];
257
            }
258
        } else {
259
299242
            b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
260
149621
                                          ff_vp9_default_kf_ymode_probs[*a][*l]);
261
149621
            b->mode[3] =
262
149621
            b->mode[2] =
263
149621
            b->mode[1] = b->mode[0];
264
            // FIXME this can probably be optimized
265
149621
            memset(a, b->mode[0], ff_vp9_bwh_tab[0][b->bs][0]);
266
149621
            memset(l, b->mode[0], ff_vp9_bwh_tab[0][b->bs][1]);
267
        }
268
231621
        b->uvmode = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
269
231621
                                     ff_vp9_default_kf_uvmode_probs[b->mode[3]]);
270
561069
    } else if (b->intra) {
271
32127
        b->comp = 0;
272
32127
        if (b->bs > BS_8x8) {
273
3328
            b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
274
1664
                                          s->prob.p.y_mode[0]);
275
1664
            td->counts.y_mode[0][b->mode[0]]++;
276
1664
            if (b->bs != BS_8x4) {
277
2374
                b->mode[1] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
278
1187
                                              s->prob.p.y_mode[0]);
279
1187
                td->counts.y_mode[0][b->mode[1]]++;
280
            } else {
281
477
                b->mode[1] = b->mode[0];
282
            }
283
1664
            if (b->bs != BS_4x8) {
284
2314
                b->mode[2] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
285
1157
                                              s->prob.p.y_mode[0]);
286
1157
                td->counts.y_mode[0][b->mode[2]]++;
287
1157
                if (b->bs != BS_8x4) {
288
1360
                    b->mode[3] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
289
680
                                                  s->prob.p.y_mode[0]);
290
680
                    td->counts.y_mode[0][b->mode[3]]++;
291
                } else {
292
477
                    b->mode[3] = b->mode[2];
293
                }
294
            } else {
295
507
                b->mode[2] = b->mode[0];
296
507
                b->mode[3] = b->mode[1];
297
            }
298
        } else {
299
            static const uint8_t size_group[10] = {
300
                3, 3, 3, 3, 2, 2, 2, 1, 1, 1
301
            };
302
30463
            int sz = size_group[b->bs];
303
304
60926
            b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
305
30463
                                          s->prob.p.y_mode[sz]);
306
30463
            b->mode[1] =
307
30463
            b->mode[2] =
308
30463
            b->mode[3] = b->mode[0];
309
30463
            td->counts.y_mode[sz][b->mode[3]]++;
310
        }
311
64254
        b->uvmode = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
312
32127
                                     s->prob.p.uv_mode[b->mode[3]]);
313
32127
        td->counts.uv_mode[b->mode[3]][b->uvmode]++;
314
    } else {
315
        static const uint8_t inter_mode_ctx_lut[14][14] = {
316
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
317
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
318
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
319
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
320
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
321
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
322
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
323
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
324
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
325
            { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
326
            { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
327
            { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
328
            { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 3 },
329
            { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
330
        };
331
332

528942
        if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[b->seg_id].ref_enabled) {
333
            av_assert2(s->s.h.segmentation.feat[b->seg_id].ref_val != 0);
334
801
            b->comp = 0;
335
801
            b->ref[0] = s->s.h.segmentation.feat[b->seg_id].ref_val - 1;
336
        } else {
337
            // read comp_pred flag
338
528141
            if (s->s.h.comppredmode != PRED_SWITCHABLE) {
339
343909
                b->comp = s->s.h.comppredmode == PRED_COMPREF;
340
            } else {
341
                int c;
342
343
                // FIXME add intra as ref=0xff (or -1) to make these easier?
344
184232
                if (have_a) {
345
179448
                    if (have_l) {
346

176369
                        if (s->above_comp_ctx[col] && td->left_comp_ctx[row7]) {
347
18517
                            c = 4;
348
157852
                        } else if (s->above_comp_ctx[col]) {
349
19316
                            c = 2 + (td->left_intra_ctx[row7] ||
350
9604
                                     td->left_ref_ctx[row7] == s->s.h.fixcompref);
351
148140
                        } else if (td->left_comp_ctx[row7]) {
352
16690
                            c = 2 + (s->above_intra_ctx[col] ||
353
8289
                                     s->above_ref_ctx[col] == s->s.h.fixcompref);
354
                        } else {
355
418349
                            c = (!s->above_intra_ctx[col] &&
356
138871
                                 s->above_ref_ctx[col] == s->s.h.fixcompref) ^
357
277654
                                (!td->left_intra_ctx[row7] &&
358
137915
                                 td->left_ref_ctx[row & 7] == s->s.h.fixcompref);
359
                        }
360
                    } else {
361
5872
                        c = s->above_comp_ctx[col] ? 3 :
362

2793
                        (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->s.h.fixcompref);
363
                    }
364
4784
                } else if (have_l) {
365
8314
                    c = td->left_comp_ctx[row7] ? 3 :
366

3840
                    (!td->left_intra_ctx[row7] && td->left_ref_ctx[row7] == s->s.h.fixcompref);
367
                } else {
368
310
                    c = 1;
369
                }
370
184232
                b->comp = vp56_rac_get_prob(td->c, s->prob.p.comp[c]);
371
184232
                td->counts.comp[c][b->comp]++;
372
            }
373
374
            // read actual references
375
            // FIXME probably cache a few variables here to prevent repetitive
376
            // memory accesses below
377
528141
            if (b->comp) { /* two references */
378
27071
                int fix_idx = s->s.h.signbias[s->s.h.fixcompref], var_idx = !fix_idx, c, bit;
379
380
27071
                b->ref[fix_idx] = s->s.h.fixcompref;
381
                // FIXME can this codeblob be replaced by some sort of LUT?
382
27071
                if (have_a) {
383
26388
                    if (have_l) {
384
26087
                        if (s->above_intra_ctx[col]) {
385
68
                            if (td->left_intra_ctx[row7]) {
386
14
                                c = 2;
387
                            } else {
388
54
                                c = 1 + 2 * (td->left_ref_ctx[row7] != s->s.h.varcompref[1]);
389
                            }
390
26019
                        } else if (td->left_intra_ctx[row7]) {
391
84
                            c = 1 + 2 * (s->above_ref_ctx[col] != s->s.h.varcompref[1]);
392
                        } else {
393
25935
                            int refl = td->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
394
395

25935
                            if (refl == refa && refa == s->s.h.varcompref[1]) {
396
2446
                                c = 0;
397

23489
                            } else if (!td->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
398

2119
                                if ((refa == s->s.h.fixcompref && refl == s->s.h.varcompref[0]) ||
399

1776
                                    (refl == s->s.h.fixcompref && refa == s->s.h.varcompref[0])) {
400
741
                                    c = 4;
401
                                } else {
402
1378
                                    c = (refa == refl) ? 3 : 1;
403
                                }
404
21370
                            } else if (!td->left_comp_ctx[row7]) {
405

2700
                                if (refa == s->s.h.varcompref[1] && refl != s->s.h.varcompref[1]) {
406
443
                                    c = 1;
407
                                } else {
408
2257
                                    c = (refl == s->s.h.varcompref[1] &&
409

2257
                                         refa != s->s.h.varcompref[1]) ? 2 : 4;
410
                                }
411
18670
                            } else if (!s->above_comp_ctx[col]) {
412

3194
                                if (refl == s->s.h.varcompref[1] && refa != s->s.h.varcompref[1]) {
413
442
                                    c = 1;
414
                                } else {
415
2752
                                    c = (refa == s->s.h.varcompref[1] &&
416

2752
                                         refl != s->s.h.varcompref[1]) ? 2 : 4;
417
                                }
418
                            } else {
419
15476
                                c = (refl == refa) ? 4 : 2;
420
                            }
421
                        }
422
                    } else {
423
301
                        if (s->above_intra_ctx[col]) {
424
1
                            c = 2;
425
300
                        } else if (s->above_comp_ctx[col]) {
426
130
                            c = 4 * (s->above_ref_ctx[col] != s->s.h.varcompref[1]);
427
                        } else {
428
170
                            c = 3 * (s->above_ref_ctx[col] != s->s.h.varcompref[1]);
429
                        }
430
                    }
431
683
                } else if (have_l) {
432
665
                    if (td->left_intra_ctx[row7]) {
433
4
                        c = 2;
434
661
                    } else if (td->left_comp_ctx[row7]) {
435
397
                        c = 4 * (td->left_ref_ctx[row7] != s->s.h.varcompref[1]);
436
                    } else {
437
264
                        c = 3 * (td->left_ref_ctx[row7] != s->s.h.varcompref[1]);
438
                    }
439
                } else {
440
18
                    c = 2;
441
                }
442
27071
                bit = vp56_rac_get_prob(td->c, s->prob.p.comp_ref[c]);
443
27071
                b->ref[var_idx] = s->s.h.varcompref[bit];
444
27071
                td->counts.comp_ref[c][bit]++;
445
            } else /* single reference */ {
446
                int bit, c;
447
448

501070
                if (have_a && !s->above_intra_ctx[col]) {
449

464076
                    if (have_l && !td->left_intra_ctx[row7]) {
450
438983
                        if (td->left_comp_ctx[row7]) {
451
6151
                            if (s->above_comp_ctx[col]) {
452

2088
                                c = 1 + (!s->s.h.fixcompref || !td->left_ref_ctx[row7] ||
453
529
                                         !s->above_ref_ctx[col]);
454
                            } else {
455
9184
                                c = (3 * !s->above_ref_ctx[col]) +
456

4592
                                    (!s->s.h.fixcompref || !td->left_ref_ctx[row7]);
457
                            }
458
432832
                        } else if (s->above_comp_ctx[col]) {
459
13284
                            c = (3 * !td->left_ref_ctx[row7]) +
460

6642
                                (!s->s.h.fixcompref || !s->above_ref_ctx[col]);
461
                        } else {
462

426190
                            c = 2 * !td->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
463
                        }
464
25093
                    } else if (s->above_intra_ctx[col]) {
465
                        c = 2;
466
25093
                    } else if (s->above_comp_ctx[col]) {
467

229
                        c = 1 + (!s->s.h.fixcompref || !s->above_ref_ctx[col]);
468
                    } else {
469
24864
                        c = 4 * (!s->above_ref_ctx[col]);
470
                    }
471

36994
                } else if (have_l && !td->left_intra_ctx[row7]) {
472
31970
                    if (td->left_intra_ctx[row7]) {
473
                        c = 2;
474
31970
                    } else if (td->left_comp_ctx[row7]) {
475

327
                        c = 1 + (!s->s.h.fixcompref || !td->left_ref_ctx[row7]);
476
                    } else {
477
31643
                        c = 4 * (!td->left_ref_ctx[row7]);
478
                    }
479
                } else {
480
5024
                    c = 2;
481
                }
482
501070
                bit = vp56_rac_get_prob(td->c, s->prob.p.single_ref[c][0]);
483
501070
                td->counts.single_ref[c][0][bit]++;
484
501070
                if (!bit) {
485
400801
                    b->ref[0] = 0;
486
                } else {
487
                    // FIXME can this codeblob be replaced by some sort of LUT?
488
100269
                    if (have_a) {
489
95202
                        if (have_l) {
490
92066
                            if (td->left_intra_ctx[row7]) {
491
3386
                                if (s->above_intra_ctx[col]) {
492
772
                                    c = 2;
493
2614
                                } else if (s->above_comp_ctx[col]) {
494
98
                                    c = 1 + 2 * (s->s.h.fixcompref == 1 ||
495
49
                                                 s->above_ref_ctx[col] == 1);
496
2565
                                } else if (!s->above_ref_ctx[col]) {
497
704
                                    c = 3;
498
                                } else {
499
1861
                                    c = 4 * (s->above_ref_ctx[col] == 1);
500
                                }
501
88680
                            } else if (s->above_intra_ctx[col]) {
502
1920
                                if (td->left_intra_ctx[row7]) {
503
                                    c = 2;
504
1920
                                } else if (td->left_comp_ctx[row7]) {
505
90
                                    c = 1 + 2 * (s->s.h.fixcompref == 1 ||
506
45
                                                 td->left_ref_ctx[row7] == 1);
507
1875
                                } else if (!td->left_ref_ctx[row7]) {
508
556
                                    c = 3;
509
                                } else {
510
1319
                                    c = 4 * (td->left_ref_ctx[row7] == 1);
511
                                }
512
86760
                            } else if (s->above_comp_ctx[col]) {
513
4360
                                if (td->left_comp_ctx[row7]) {
514
845
                                    if (td->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
515
1448
                                        c = 3 * (s->s.h.fixcompref == 1 ||
516
724
                                                 td->left_ref_ctx[row7] == 1);
517
                                    } else {
518
121
                                        c = 2;
519
                                    }
520
3515
                                } else if (!td->left_ref_ctx[row7]) {
521
658
                                    c = 1 + 2 * (s->s.h.fixcompref == 1 ||
522
329
                                                 s->above_ref_ctx[col] == 1);
523
                                } else {
524
6372
                                    c = 3 * (td->left_ref_ctx[row7] == 1) +
525

3186
                                    (s->s.h.fixcompref == 1 || s->above_ref_ctx[col] == 1);
526
                                }
527
82400
                            } else if (td->left_comp_ctx[row7]) {
528
1733
                                if (!s->above_ref_ctx[col]) {
529
660
                                    c = 1 + 2 * (s->s.h.fixcompref == 1 ||
530
330
                                                 td->left_ref_ctx[row7] == 1);
531
                                } else {
532
2806
                                    c = 3 * (s->above_ref_ctx[col] == 1) +
533

1403
                                    (s->s.h.fixcompref == 1 || td->left_ref_ctx[row7] == 1);
534
                                }
535
80667
                            } else if (!s->above_ref_ctx[col]) {
536
18174
                                if (!td->left_ref_ctx[row7]) {
537
7482
                                    c = 3;
538
                                } else {
539
10692
                                    c = 4 * (td->left_ref_ctx[row7] == 1);
540
                                }
541
62493
                            } else if (!td->left_ref_ctx[row7]) {
542
9216
                                c = 4 * (s->above_ref_ctx[col] == 1);
543
                            } else {
544
106554
                                c = 2 * (td->left_ref_ctx[row7] == 1) +
545
53277
                                    2 * (s->above_ref_ctx[col] == 1);
546
                            }
547
                        } else {
548
3136
                            if (s->above_intra_ctx[col] ||
549

3067
                                (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
550
765
                                c = 2;
551
2371
                            } else if (s->above_comp_ctx[col]) {
552

105
                                c = 3 * (s->s.h.fixcompref == 1 || s->above_ref_ctx[col] == 1);
553
                            } else {
554
2266
                                c = 4 * (s->above_ref_ctx[col] == 1);
555
                            }
556
                        }
557
5067
                    } else if (have_l) {
558
4659
                        if (td->left_intra_ctx[row7] ||
559

4503
                            (!td->left_comp_ctx[row7] && !td->left_ref_ctx[row7])) {
560
979
                            c = 2;
561
3680
                        } else if (td->left_comp_ctx[row7]) {
562

113
                            c = 3 * (s->s.h.fixcompref == 1 || td->left_ref_ctx[row7] == 1);
563
                        } else {
564
3567
                            c = 4 * (td->left_ref_ctx[row7] == 1);
565
                        }
566
                    } else {
567
408
                        c = 2;
568
                    }
569
100269
                    bit = vp56_rac_get_prob(td->c, s->prob.p.single_ref[c][1]);
570
100269
                    td->counts.single_ref[c][1][bit]++;
571
100269
                    b->ref[0] = 1 + bit;
572
                }
573
            }
574
        }
575
576
528942
        if (b->bs <= BS_8x8) {
577

479062
            if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[b->seg_id].skip_enabled) {
578
801
                b->mode[0] =
579
801
                b->mode[1] =
580
801
                b->mode[2] =
581
801
                b->mode[3] = ZEROMV;
582
            } else {
583
                static const uint8_t off[10] = {
584
                    3, 0, 0, 1, 0, 0, 0, 0, 0, 0
585
                };
586
587
                // FIXME this needs to use the LUT tables from find_ref_mvs
588
                // because not all are -1,0/0,-1
589
478261
                int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
590
478261
                                          [td->left_mode_ctx[row7 + off[b->bs]]];
591
592
956522
                b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
593
478261
                                              s->prob.p.mv_mode[c]);
594
478261
                b->mode[1] =
595
478261
                b->mode[2] =
596
478261
                b->mode[3] = b->mode[0];
597
478261
                td->counts.mv_mode[c][b->mode[0] - 10]++;
598
            }
599
        }
600
601
528942
        if (s->s.h.filtermode == FILTER_SWITCHABLE) {
602
            int c;
603
604

427419
            if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
605

393870
                if (have_l && td->left_mode_ctx[row7] >= NEARESTMV) {
606
371577
                    c = s->above_filter_ctx[col] == td->left_filter_ctx[row7] ?
607
371577
                        td->left_filter_ctx[row7] : 3;
608
                } else {
609
22293
                    c = s->above_filter_ctx[col];
610
                }
611

33549
            } else if (have_l && td->left_mode_ctx[row7] >= NEARESTMV) {
612
29150
                c = td->left_filter_ctx[row7];
613
            } else {
614
4399
                c = 3;
615
            }
616
617
427419
            filter_id = vp8_rac_get_tree(td->c, ff_vp9_filter_tree,
618
427419
                                         s->prob.p.filter[c]);
619
427419
            td->counts.filter[c][filter_id]++;
620
427419
            b->filter = ff_vp9_filter_lut[filter_id];
621
        } else {
622
101523
            b->filter = s->s.h.filtermode;
623
        }
624
625
528942
        if (b->bs > BS_8x8) {
626
49880
            int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][td->left_mode_ctx[row7]];
627
628
99760
            b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
629
49880
                                          s->prob.p.mv_mode[c]);
630
49880
            td->counts.mv_mode[c][b->mode[0] - 10]++;
631
49880
            ff_vp9_fill_mv(td, b->mv[0], b->mode[0], 0);
632
633
49880
            if (b->bs != BS_8x4) {
634
65158
                b->mode[1] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
635
32579
                                              s->prob.p.mv_mode[c]);
636
32579
                td->counts.mv_mode[c][b->mode[1] - 10]++;
637
32579
                ff_vp9_fill_mv(td, b->mv[1], b->mode[1], 1);
638
            } else {
639
17301
                b->mode[1] = b->mode[0];
640
17301
                AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
641
17301
                AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
642
            }
643
644
49880
            if (b->bs != BS_4x8) {
645
68306
                b->mode[2] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
646
34153
                                              s->prob.p.mv_mode[c]);
647
34153
                td->counts.mv_mode[c][b->mode[2] - 10]++;
648
34153
                ff_vp9_fill_mv(td, b->mv[2], b->mode[2], 2);
649
650
34153
                if (b->bs != BS_8x4) {
651
33704
                    b->mode[3] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
652
16852
                                                  s->prob.p.mv_mode[c]);
653
16852
                    td->counts.mv_mode[c][b->mode[3] - 10]++;
654
16852
                    ff_vp9_fill_mv(td, b->mv[3], b->mode[3], 3);
655
                } else {
656
17301
                    b->mode[3] = b->mode[2];
657
17301
                    AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
658
17301
                    AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
659
                }
660
            } else {
661
15727
                b->mode[2] = b->mode[0];
662
15727
                AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
663
15727
                AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
664
15727
                b->mode[3] = b->mode[1];
665
15727
                AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
666
15727
                AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
667
            }
668
        } else {
669
479062
            ff_vp9_fill_mv(td, b->mv[0], b->mode[0], -1);
670
479062
            AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
671
479062
            AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
672
479062
            AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
673
479062
            AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
674
479062
            AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
675
479062
            AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
676
        }
677
678
528942
        vref = b->ref[b->comp ? s->s.h.signbias[s->s.h.varcompref[0]] : 0];
679
    }
680
681
#if HAVE_FAST_64BIT
682
#define SPLAT_CTX(var, val, n) \
683
    switch (n) { \
684
    case 1:  var = val;                                    break; \
685
    case 2:  AV_WN16A(&var, val *             0x0101);     break; \
686
    case 4:  AV_WN32A(&var, val *         0x01010101);     break; \
687
    case 8:  AV_WN64A(&var, val * 0x0101010101010101ULL);  break; \
688
    case 16: { \
689
        uint64_t v64 = val * 0x0101010101010101ULL; \
690
        AV_WN64A(              &var,     v64); \
691
        AV_WN64A(&((uint8_t *) &var)[8], v64); \
692
        break; \
693
    } \
694
    }
695
#else
696
#define SPLAT_CTX(var, val, n) \
697
    switch (n) { \
698
    case 1:  var = val;                         break; \
699
    case 2:  AV_WN16A(&var, val *     0x0101);  break; \
700
    case 4:  AV_WN32A(&var, val * 0x01010101);  break; \
701
    case 8: { \
702
        uint32_t v32 = val * 0x01010101; \
703
        AV_WN32A(              &var,     v32); \
704
        AV_WN32A(&((uint8_t *) &var)[4], v32); \
705
        break; \
706
    } \
707
    case 16: { \
708
        uint32_t v32 = val * 0x01010101; \
709
        AV_WN32A(              &var,      v32); \
710
        AV_WN32A(&((uint8_t *) &var)[4],  v32); \
711
        AV_WN32A(&((uint8_t *) &var)[8],  v32); \
712
        AV_WN32A(&((uint8_t *) &var)[12], v32); \
713
        break; \
714
    } \
715
    }
716
#endif
717
718

792690
    switch (ff_vp9_bwh_tab[1][b->bs][0]) {
719
#define SET_CTXS(perf, dir, off, n) \
720
    do { \
721
        SPLAT_CTX(perf->dir##_skip_ctx[off],      b->skip,          n); \
722
        SPLAT_CTX(perf->dir##_txfm_ctx[off],      b->tx,            n); \
723
        SPLAT_CTX(perf->dir##_partition_ctx[off], dir##_ctx[b->bs], n); \
724
        if (!s->s.h.keyframe && !s->s.h.intraonly) { \
725
            SPLAT_CTX(perf->dir##_intra_ctx[off], b->intra,   n); \
726
            SPLAT_CTX(perf->dir##_comp_ctx[off],  b->comp,    n); \
727
            SPLAT_CTX(perf->dir##_mode_ctx[off],  b->mode[3], n); \
728
            if (!b->intra) { \
729
                SPLAT_CTX(perf->dir##_ref_ctx[off], vref, n); \
730
                if (s->s.h.filtermode == FILTER_SWITCHABLE) { \
731
                    SPLAT_CTX(perf->dir##_filter_ctx[off], filter_id, n); \
732
                } \
733
            } \
734
        } \
735
    } while (0)
736


532924
    case 1: SET_CTXS(s, above, col, 1); break;
737


202916
    case 2: SET_CTXS(s, above, col, 2); break;
738


44196
    case 4: SET_CTXS(s, above, col, 4); break;
739


12654
    case 8: SET_CTXS(s, above, col, 8); break;
740
    }
741

792690
    switch (ff_vp9_bwh_tab[1][b->bs][1]) {
742


541824
    case 1: SET_CTXS(td, left, row7, 1); break;
743


196533
    case 2: SET_CTXS(td, left, row7, 2); break;
744


42537
    case 4: SET_CTXS(td, left, row7, 4); break;
745


11796
    case 8: SET_CTXS(td, left, row7, 8); break;
746
    }
747
#undef SPLAT_CTX
748
#undef SET_CTXS
749
750

792690
    if (!s->s.h.keyframe && !s->s.h.intraonly) {
751
561069
        if (b->bs > BS_8x8) {
752
51544
            int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
753
754
51544
            AV_COPY32(&td->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
755
51544
            AV_COPY32(&td->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
756
51544
            AV_WN32A(&td->left_mv_ctx[row7 * 2 + 1][0], mv0);
757
51544
            AV_WN32A(&td->left_mv_ctx[row7 * 2 + 1][1], mv1);
758
51544
            AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
759
51544
            AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
760
51544
            AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
761
51544
            AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
762
        } else {
763
509525
            int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
764
765
2214067
            for (n = 0; n < w4 * 2; n++) {
766
1704542
                AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
767
1704542
                AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
768
            }
769
2193979
            for (n = 0; n < h4 * 2; n++) {
770
1684454
                AV_WN32A(&td->left_mv_ctx[row7 * 2 + n][0], mv0);
771
1684454
                AV_WN32A(&td->left_mv_ctx[row7 * 2 + n][1], mv1);
772
            }
773
        }
774
    }
775
776
    // FIXME kinda ugly
777
1989389
    for (y = 0; y < h4; y++) {
778
1196699
        int x, o = (row + y) * s->sb_cols * 8 + col;
779
1196699
        VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[o];
780
781
1196699
        if (b->intra) {
782
942142
            for (x = 0; x < w4; x++) {
783
596488
                mv[x].ref[0] =
784
596488
                mv[x].ref[1] = -1;
785
            }
786
851045
        } else if (b->comp) {
787
206417
            for (x = 0; x < w4; x++) {
788
153674
                mv[x].ref[0] = b->ref[0];
789
153674
                mv[x].ref[1] = b->ref[1];
790
153674
                AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
791
153674
                AV_COPY32(&mv[x].mv[1], &b->mv[3][1]);
792
            }
793
        } else {
794
2746911
            for (x = 0; x < w4; x++) {
795
1948609
                mv[x].ref[0] = b->ref[0];
796
1948609
                mv[x].ref[1] = -1;
797
1948609
                AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
798
            }
799
        }
800
    }
801
792690
}
802
803
// FIXME merge cnt/eob arguments?
804
static av_always_inline int
805
2002121
decode_coeffs_b_generic(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
806
                        int is_tx32x32, int is8bitsperpixel, int bpp, unsigned (*cnt)[6][3],
807
                        unsigned (*eob)[6][2], uint8_t (*p)[6][11],
808
                        int nnz, const int16_t *scan, const int16_t (*nb)[2],
809
                        const int16_t *band_counts, int16_t *qmul)
810
{
811
2002121
    int i = 0, band = 0, band_left = band_counts[band];
812
2002121
    const uint8_t *tp = p[0][nnz];
813
    uint8_t cache[1024];
814
815
    do {
816
        int val, rc;
817
818
10685765
        val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
819
10685765
        eob[band][nnz][val]++;
820
10685765
        if (!val)
821
1943545
            break;
822
823
8742220
skip_eob:
824
17384175
        if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
825
8641955
            cnt[band][nnz][0]++;
826
8641955
            if (!--band_left)
827
1195999
                band_left = band_counts[++band];
828
8641955
            cache[scan[i]] = 0;
829
8641955
            nnz            = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
830
8641955
            tp             = p[band][nnz];
831
8641955
            if (++i == n_coeffs)
832
                break;  //invalid input; blocks should end with EOB
833
8641955
            goto skip_eob;
834
        }
835
836
8742220
        rc = scan[i];
837
8742220
        if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
838
4978876
            cnt[band][nnz][1]++;
839
4978876
            val       = 1;
840
4978876
            cache[rc] = 1;
841
        } else {
842
3763344
            cnt[band][nnz][2]++;
843
3763344
            if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
844
2495217
                if (!vp56_rac_get_prob_branchy(c, tp[4])) {
845
1424026
                    cache[rc] = val = 2;
846
                } else {
847
1071191
                    val       = 3 + vp56_rac_get_prob(c, tp[5]);
848
1071191
                    cache[rc] = 3;
849
                }
850
1268127
            } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
851
798374
                cache[rc] = 4;
852
798374
                if (!vp56_rac_get_prob_branchy(c, tp[7])) {
853
426977
                    val  =  vp56_rac_get_prob(c, 159) + 5;
854
                } else {
855
371397
                    val  = (vp56_rac_get_prob(c, 165) << 1) + 7;
856
371397
                    val +=  vp56_rac_get_prob(c, 145);
857
                }
858
            } else { // cat 3-6
859
469753
                cache[rc] = 5;
860
469753
                if (!vp56_rac_get_prob_branchy(c, tp[8])) {
861
390930
                    if (!vp56_rac_get_prob_branchy(c, tp[9])) {
862
252769
                        val  = 11 + (vp56_rac_get_prob(c, 173) << 2);
863
252769
                        val +=      (vp56_rac_get_prob(c, 148) << 1);
864
252769
                        val +=       vp56_rac_get_prob(c, 140);
865
                    } else {
866
138161
                        val  = 19 + (vp56_rac_get_prob(c, 176) << 3);
867
138161
                        val +=      (vp56_rac_get_prob(c, 155) << 2);
868
138161
                        val +=      (vp56_rac_get_prob(c, 140) << 1);
869
138161
                        val +=       vp56_rac_get_prob(c, 135);
870
                    }
871
78823
                } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
872
57599
                    val  = (vp56_rac_get_prob(c, 180) << 4) + 35;
873
57599
                    val += (vp56_rac_get_prob(c, 157) << 3);
874
57599
                    val += (vp56_rac_get_prob(c, 141) << 2);
875
57599
                    val += (vp56_rac_get_prob(c, 134) << 1);
876
57599
                    val +=  vp56_rac_get_prob(c, 130);
877
                } else {
878
21224
                    val = 67;
879
21224
                    if (!is8bitsperpixel) {
880
81
                        if (bpp == 12) {
881
60
                            val += vp56_rac_get_prob(c, 255) << 17;
882
60
                            val += vp56_rac_get_prob(c, 255) << 16;
883
                        }
884
81
                        val +=  (vp56_rac_get_prob(c, 255) << 15);
885
81
                        val +=  (vp56_rac_get_prob(c, 255) << 14);
886
                    }
887
21224
                    val += (vp56_rac_get_prob(c, 254) << 13);
888
21224
                    val += (vp56_rac_get_prob(c, 254) << 12);
889
21224
                    val += (vp56_rac_get_prob(c, 254) << 11);
890
21224
                    val += (vp56_rac_get_prob(c, 252) << 10);
891
21224
                    val += (vp56_rac_get_prob(c, 249) << 9);
892
21224
                    val += (vp56_rac_get_prob(c, 243) << 8);
893
21224
                    val += (vp56_rac_get_prob(c, 230) << 7);
894
21224
                    val += (vp56_rac_get_prob(c, 196) << 6);
895
21224
                    val += (vp56_rac_get_prob(c, 177) << 5);
896
21224
                    val += (vp56_rac_get_prob(c, 153) << 4);
897
21224
                    val += (vp56_rac_get_prob(c, 140) << 3);
898
21224
                    val += (vp56_rac_get_prob(c, 133) << 2);
899
21224
                    val += (vp56_rac_get_prob(c, 130) << 1);
900
21224
                    val +=  vp56_rac_get_prob(c, 129);
901
                }
902
            }
903
        }
904
#define STORE_COEF(c, i, v) do { \
905
    if (is8bitsperpixel) { \
906
        c[i] = v; \
907
    } else { \
908
        AV_WN32A(&c[i * 2], v); \
909
    } \
910
} while (0)
911
8742220
        if (!--band_left)
912
2174707
            band_left = band_counts[++band];
913
8742220
        if (is_tx32x32)
914


1366404
            STORE_COEF(coef, rc, (int)((vp8_rac_get(c) ? -val : val) * (unsigned)qmul[!!i]) / 2);
915
        else
916


7375816
            STORE_COEF(coef, rc, (vp8_rac_get(c) ? -val : val) * (unsigned)qmul[!!i]);
917
8742220
        nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
918
8742220
        tp = p[band][nnz];
919
8742220
    } while (++i < n_coeffs);
920
921
2002121
    return i;
922
}
923
924
1886053
static int decode_coeffs_b_8bpp(VP9TileData *td, int16_t *coef, int n_coeffs,
925
                                unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
926
                                uint8_t (*p)[6][11], int nnz, const int16_t *scan,
927
                                const int16_t (*nb)[2], const int16_t *band_counts,
928
                                int16_t *qmul)
929
{
930
1886053
    return decode_coeffs_b_generic(td->c, coef, n_coeffs, 0, 1, 8, cnt, eob, p,
931
                                   nnz, scan, nb, band_counts, qmul);
932
}
933
934
13609
static int decode_coeffs_b32_8bpp(VP9TileData *td, int16_t *coef, int n_coeffs,
935
                                  unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
936
                                  uint8_t (*p)[6][11], int nnz, const int16_t *scan,
937
                                  const int16_t (*nb)[2], const int16_t *band_counts,
938
                                  int16_t *qmul)
939
{
940
13609
    return decode_coeffs_b_generic(td->c, coef, n_coeffs, 1, 1, 8, cnt, eob, p,
941
                                   nnz, scan, nb, band_counts, qmul);
942
}
943
944
102344
static int decode_coeffs_b_16bpp(VP9TileData *td, int16_t *coef, int n_coeffs,
945
                                 unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
946
                                 uint8_t (*p)[6][11], int nnz, const int16_t *scan,
947
                                 const int16_t (*nb)[2], const int16_t *band_counts,
948
                                 int16_t *qmul)
949
{
950
102344
    return decode_coeffs_b_generic(td->c, coef, n_coeffs, 0, 0, td->s->s.h.bpp, cnt, eob, p,
951
                                   nnz, scan, nb, band_counts, qmul);
952
}
953
954
115
static int decode_coeffs_b32_16bpp(VP9TileData *td, int16_t *coef, int n_coeffs,
955
                                   unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
956
                                   uint8_t (*p)[6][11], int nnz, const int16_t *scan,
957
                                   const int16_t (*nb)[2], const int16_t *band_counts,
958
                                   int16_t *qmul)
959
{
960
115
    return decode_coeffs_b_generic(td->c, coef, n_coeffs, 1, 0, td->s->s.h.bpp, cnt, eob, p,
961
                                   nnz, scan, nb, band_counts, qmul);
962
}
963
964
368255
static av_always_inline int decode_coeffs(VP9TileData *td, int is8bitsperpixel)
965
{
966
368255
    VP9Context *s = td->s;
967
368255
    VP9Block *b = td->b;
968
368255
    int row = td->row, col = td->col;
969
368255
    uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
970
368255
    unsigned (*c)[6][3] = td->counts.coef[b->tx][0 /* y */][!b->intra];
971
368255
    unsigned (*e)[6][2] = td->counts.eob[b->tx][0 /* y */][!b->intra];
972
368255
    int w4 = ff_vp9_bwh_tab[1][b->bs][0] << 1, h4 = ff_vp9_bwh_tab[1][b->bs][1] << 1;
973
368255
    int end_x = FFMIN(2 * (s->cols - col), w4);
974
368255
    int end_y = FFMIN(2 * (s->rows - row), h4);
975
    int n, pl, x, y, ret;
976
368255
    int16_t (*qmul)[2] = s->s.h.segmentation.feat[b->seg_id].qmul;
977
368255
    int tx = 4 * s->s.h.lossless + b->tx;
978
368255
    const int16_t * const *yscans = ff_vp9_scans[tx];
979
368255
    const int16_t (* const * ynbs)[2] = ff_vp9_scans_nb[tx];
980
368255
    const int16_t *uvscan = ff_vp9_scans[b->uvtx][DCT_DCT];
981
368255
    const int16_t (*uvnb)[2] = ff_vp9_scans_nb[b->uvtx][DCT_DCT];
982
368255
    uint8_t *a = &s->above_y_nnz_ctx[col * 2];
983
368255
    uint8_t *l = &td->left_y_nnz_ctx[(row & 7) << 1];
984
    static const int16_t band_counts[4][8] = {
985
        { 1, 2, 3, 4,  3,   16 - 13 },
986
        { 1, 2, 3, 4, 11,   64 - 21 },
987
        { 1, 2, 3, 4, 11,  256 - 21 },
988
        { 1, 2, 3, 4, 11, 1024 - 21 },
989
    };
990
368255
    const int16_t *y_band_counts = band_counts[b->tx];
991
368255
    const int16_t *uv_band_counts = band_counts[b->uvtx];
992
368255
    int bytesperpixel = is8bitsperpixel ? 1 : 2;
993
368255
    int total_coeff = 0;
994
995
#define MERGE(la, end, step, rd) \
996
    for (n = 0; n < end; n += step) \
997
        la[n] = !!rd(&la[n])
998
#define MERGE_CTX(step, rd) \
999
    do { \
1000
        MERGE(l, end_y, step, rd); \
1001
        MERGE(a, end_x, step, rd); \
1002
    } while (0)
1003
1004
#define DECODE_Y_COEF_LOOP(step, mode_index, v) \
1005
    for (n = 0, y = 0; y < end_y; y += step) { \
1006
        for (x = 0; x < end_x; x += step, n += step * step) { \
1007
            enum TxfmType txtp = ff_vp9_intra_txfm_type[b->mode[mode_index]]; \
1008
            ret = (is8bitsperpixel ? decode_coeffs_b##v##_8bpp : decode_coeffs_b##v##_16bpp) \
1009
                                    (td, td->block + 16 * n * bytesperpixel, 16 * step * step, \
1010
                                     c, e, p, a[x] + l[y], yscans[txtp], \
1011
                                     ynbs[txtp], y_band_counts, qmul[0]); \
1012
            a[x] = l[y] = !!ret; \
1013
            total_coeff |= !!ret; \
1014
            if (step >= 4) { \
1015
                AV_WN16A(&td->eob[n], ret); \
1016
            } else { \
1017
                td->eob[n] = ret; \
1018
            } \
1019
        } \
1020
    }
1021
1022
#define SPLAT(la, end, step, cond) \
1023
    if (step == 2) { \
1024
        for (n = 1; n < end; n += step) \
1025
            la[n] = la[n - 1]; \
1026
    } else if (step == 4) { \
1027
        if (cond) { \
1028
            for (n = 0; n < end; n += step) \
1029
                AV_WN32A(&la[n], la[n] * 0x01010101); \
1030
        } else { \
1031
            for (n = 0; n < end; n += step) \
1032
                memset(&la[n + 1], la[n], FFMIN(end - n - 1, 3)); \
1033
        } \
1034
    } else /* step == 8 */ { \
1035
        if (cond) { \
1036
            if (HAVE_FAST_64BIT) { \
1037
                for (n = 0; n < end; n += step) \
1038
                    AV_WN64A(&la[n], la[n] * 0x0101010101010101ULL); \
1039
            } else { \
1040
                for (n = 0; n < end; n += step) { \
1041
                    uint32_t v32 = la[n] * 0x01010101; \
1042
                    AV_WN32A(&la[n],     v32); \
1043
                    AV_WN32A(&la[n + 4], v32); \
1044
                } \
1045
            } \
1046
        } else { \
1047
            for (n = 0; n < end; n += step) \
1048
                memset(&la[n + 1], la[n], FFMIN(end - n - 1, 7)); \
1049
        } \
1050
    }
1051
#define SPLAT_CTX(step) \
1052
    do { \
1053
        SPLAT(a, end_x, step, end_x == w4); \
1054
        SPLAT(l, end_y, step, end_y == h4); \
1055
    } while (0)
1056
1057
    /* y tokens */
1058

368255
    switch (b->tx) {
1059
180459
    case TX_4X4:
1060


1381657
        DECODE_Y_COEF_LOOP(1, b->bs > BS_8x8 ? n : 0,);
1061
180459
        break;
1062
140564
    case TX_8X8:
1063

478044
        MERGE_CTX(2, AV_RN16A);
1064

542524
        DECODE_Y_COEF_LOOP(2, 0,);
1065

478044
        SPLAT_CTX(2);
1066
140564
        break;
1067
38631
    case TX_16X16:
1068

120500
        MERGE_CTX(4, AV_RN32A);
1069

123703
        DECODE_Y_COEF_LOOP(4, 0,);
1070



120500
        SPLAT_CTX(4);
1071
38631
        break;
1072
8601
    case TX_32X32:
1073

28055
        MERGE_CTX(8, AV_RN64A);
1074

29994
        DECODE_Y_COEF_LOOP(8, 0, 32);
1075



28055
        SPLAT_CTX(8);
1076
8601
        break;
1077
    }
1078
1079
#define DECODE_UV_COEF_LOOP(step, v) \
1080
    for (n = 0, y = 0; y < end_y; y += step) { \
1081
        for (x = 0; x < end_x; x += step, n += step * step) { \
1082
            ret = (is8bitsperpixel ? decode_coeffs_b##v##_8bpp : decode_coeffs_b##v##_16bpp) \
1083
                                    (td, td->uvblock[pl] + 16 * n * bytesperpixel, \
1084
                                     16 * step * step, c, e, p, a[x] + l[y], \
1085
                                     uvscan, uvnb, uv_band_counts, qmul[1]); \
1086
            a[x] = l[y] = !!ret; \
1087
            total_coeff |= !!ret; \
1088
            if (step >= 4) { \
1089
                AV_WN16A(&td->uveob[pl][n], ret); \
1090
            } else { \
1091
                td->uveob[pl][n] = ret; \
1092
            } \
1093
        } \
1094
    }
1095
1096
368255
    p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
1097
368255
    c = td->counts.coef[b->uvtx][1 /* uv */][!b->intra];
1098
368255
    e = td->counts.eob[b->uvtx][1 /* uv */][!b->intra];
1099
368255
    w4 >>= s->ss_h;
1100
368255
    end_x >>= s->ss_h;
1101
368255
    h4 >>= s->ss_v;
1102
368255
    end_y >>= s->ss_v;
1103
1104765
    for (pl = 0; pl < 2; pl++) {
1104
736510
        a = &s->above_uv_nnz_ctx[pl][col << !s->ss_h];
1105
736510
        l = &td->left_uv_nnz_ctx[pl][(row & 7) << !s->ss_v];
1106

736510
        switch (b->uvtx) {
1107
610960
        case TX_4X4:
1108

2007652
            DECODE_UV_COEF_LOOP(1,);
1109
610960
            break;
1110
105352
        case TX_8X8:
1111

333164
            MERGE_CTX(2, AV_RN16A);
1112

349364
            DECODE_UV_COEF_LOOP(2,);
1113

332828
            SPLAT_CTX(2);
1114
105352
            break;
1115
18230
        case TX_16X16:
1116

56046
            MERGE_CTX(4, AV_RN32A);
1117

56548
            DECODE_UV_COEF_LOOP(4,);
1118



56046
            SPLAT_CTX(4);
1119
18230
            break;
1120
1968
        case TX_32X32:
1121

5930
            MERGE_CTX(8, AV_RN64A);
1122

5942
            DECODE_UV_COEF_LOOP(8, 32);
1123



5930
            SPLAT_CTX(8);
1124
1968
            break;
1125
        }
1126
736510
    }
1127
1128
368255
    return total_coeff;
1129
}
1130
1131
354840
static int decode_coeffs_8bpp(VP9TileData *td)
1132
{
1133
354840
    return decode_coeffs(td, 1);
1134
}
1135
1136
13415
static int decode_coeffs_16bpp(VP9TileData *td)
1137
{
1138
13415
    return decode_coeffs(td, 0);
1139
}
1140
1141
1515322
static av_always_inline void mask_edges(uint8_t (*mask)[8][4], int ss_h, int ss_v,
1142
                                        int row_and_7, int col_and_7,
1143
                                        int w, int h, int col_end, int row_end,
1144
                                        enum TxfmMode tx, int skip_inter)
1145
{
1146
    static const unsigned wide_filter_col_mask[2] = { 0x11, 0x01 };
1147
    static const unsigned wide_filter_row_mask[2] = { 0x03, 0x07 };
1148
1149
    // FIXME I'm pretty sure all loops can be replaced by a single LUT if
1150
    // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
1151
    // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
1152
    // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
1153
1154
    // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
1155
    // edges. This means that for UV, we work on two subsampled blocks at
1156
    // a time, and we only use the topleft block's mode information to set
1157
    // things like block strength. Thus, for any block size smaller than
1158
    // 16x16, ignore the odd portion of the block.
1159

1515322
    if (tx == TX_4X4 && (ss_v | ss_h)) {
1160
555188
        if (h == ss_v) {
1161
508703
            if (row_and_7 & 1)
1162
252222
                return;
1163
256481
            if (!row_end)
1164
252221
                h += 1;
1165
        }
1166
302966
        if (w == ss_h) {
1167
271387
            if (col_and_7 & 1)
1168
130597
                return;
1169
140790
            if (!col_end)
1170
130597
                w += 1;
1171
        }
1172
    }
1173
1174

1400164
    if (tx == TX_4X4 && !skip_inter) {
1175
267661
        int t = 1 << col_and_7, m_col = (t << w) - t, y;
1176
        // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
1177
267661
        int m_row_8 = m_col & wide_filter_col_mask[ss_h], m_row_4 = m_col - m_row_8;
1178
1179
638719
        for (y = row_and_7; y < h + row_and_7; y++) {
1180
371058
            int col_mask_id = 2 - !(y & wide_filter_row_mask[ss_v]);
1181
1182
371058
            mask[0][y][1] |= m_row_8;
1183
371058
            mask[0][y][2] |= m_row_4;
1184
            // for odd lines, if the odd col is not being filtered,
1185
            // skip odd row also:
1186
            // .---. <-- a
1187
            // |   |
1188
            // |___| <-- b
1189
            // ^   ^
1190
            // c   d
1191
            //
1192
            // if a/c are even row/col and b/d are odd, and d is skipped,
1193
            // e.g. right edge of size-66x66.webm, then skip b also (bug)
1194

371058
            if ((ss_h & ss_v) && (col_end & 1) && (y & 1)) {
1195
4224
                mask[1][y][col_mask_id] |= (t << (w - 1)) - t;
1196
            } else {
1197
366834
                mask[1][y][col_mask_id] |= m_col;
1198
            }
1199
371058
            if (!ss_h)
1200
190547
                mask[0][y][3] |= m_col;
1201
371058
            if (!ss_v) {
1202

184376
                if (ss_h && (col_end & 1))
1203
                    mask[1][y][3] |= (t << (w - 1)) - t;
1204
                else
1205
184376
                    mask[1][y][3] |= m_col;
1206
            }
1207
        }
1208
    } else {
1209
864842
        int y, t = 1 << col_and_7, m_col = (t << w) - t;
1210
1211
864842
        if (!skip_inter) {
1212
262865
            int mask_id = (tx == TX_8X8);
1213
262865
            int l2 = tx + ss_h - 1, step1d;
1214
            static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
1215
262865
            int m_row = m_col & masks[l2];
1216
1217
            // at odd UV col/row edges tx16/tx32 loopfilter edges, force
1218
            // 8wd loopfilter to prevent going off the visible edge.
1219

262873
            if (ss_h && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
1220
8
                int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
1221
8
                int m_row_8 = m_row - m_row_16;
1222
1223
60
                for (y = row_and_7; y < h + row_and_7; y++) {
1224
52
                    mask[0][y][0] |= m_row_16;
1225
52
                    mask[0][y][1] |= m_row_8;
1226
                }
1227
            } else {
1228
714441
                for (y = row_and_7; y < h + row_and_7; y++)
1229
451584
                    mask[0][y][mask_id] |= m_row;
1230
            }
1231
1232
262865
            l2 = tx + ss_v - 1;
1233
262865
            step1d = 1 << l2;
1234

262865
            if (ss_v && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
1235
156
                for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
1236
77
                    mask[1][y][0] |= m_col;
1237
79
                if (y - row_and_7 == h - 1)
1238
2
                    mask[1][y][1] |= m_col;
1239
            } else {
1240
558195
                for (y = row_and_7; y < h + row_and_7; y += step1d)
1241
295409
                    mask[1][y][mask_id] |= m_col;
1242
            }
1243
601977
        } else if (tx != TX_4X4) {
1244
            int mask_id;
1245
1246

512258
            mask_id = (tx == TX_8X8) || (h == ss_v);
1247
512258
            mask[1][row_and_7][mask_id] |= m_col;
1248

512258
            mask_id = (tx == TX_8X8) || (w == ss_h);
1249
1539809
            for (y = row_and_7; y < h + row_and_7; y++)
1250
1027551
                mask[0][y][mask_id] |= t;
1251
        } else {
1252
89719
            int t8 = t & wide_filter_col_mask[ss_h], t4 = t - t8;
1253
1254
254883
            for (y = row_and_7; y < h + row_and_7; y++) {
1255
165164
                mask[0][y][2] |= t4;
1256
165164
                mask[0][y][1] |= t8;
1257
            }
1258

89719
            mask[1][row_and_7][2 - !(row_and_7 & wide_filter_row_mask[ss_v])] |= m_col;
1259
        }
1260
    }
1261
}
1262
1263
792690
void ff_vp9_decode_block(VP9TileData *td, int row, int col,
1264
                         VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
1265
                         enum BlockLevel bl, enum BlockPartition bp)
1266
{
1267
792690
    VP9Context *s = td->s;
1268
792690
    VP9Block *b = td->b;
1269
792690
    enum BlockSize bs = bl * 3 + bp;
1270
792690
    int bytesperpixel = s->bytesperpixel;
1271
792690
    int w4 = ff_vp9_bwh_tab[1][bs][0], h4 = ff_vp9_bwh_tab[1][bs][1], lvl;
1272
    int emu[2];
1273
792690
    AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
1274
1275
792690
    td->row = row;
1276
792690
    td->row7 = row & 7;
1277
792690
    td->col = col;
1278
792690
    td->col7 = col & 7;
1279
1280
792690
    td->min_mv.x = -(128 + col * 64);
1281
792690
    td->min_mv.y = -(128 + row * 64);
1282
792690
    td->max_mv.x = 128 + (s->cols - col - w4) * 64;
1283
792690
    td->max_mv.y = 128 + (s->rows - row - h4) * 64;
1284
1285
792690
    if (s->pass < 2) {
1286
792690
        b->bs = bs;
1287
792690
        b->bl = bl;
1288
792690
        b->bp = bp;
1289
792690
        decode_mode(td);
1290

1082638
        b->uvtx = b->tx - ((s->ss_h && w4 * 2 == (1 << b->tx)) ||
1291

289948
                           (s->ss_v && h4 * 2 == (1 << b->tx)));
1292
1293
792690
        if (td->block_structure) {
1294
1204
            td->block_structure[td->nb_block_structure].row = row;
1295
1204
            td->block_structure[td->nb_block_structure].col = col;
1296
1204
            td->block_structure[td->nb_block_structure].block_size_idx_x = av_log2(w4);
1297
1204
            td->block_structure[td->nb_block_structure].block_size_idx_y = av_log2(h4);
1298
1204
            td->nb_block_structure++;
1299
        }
1300
1301
792690
        if (!b->skip) {
1302
            int has_coeffs;
1303
1304
368255
            if (bytesperpixel == 1) {
1305
354840
                has_coeffs = decode_coeffs_8bpp(td);
1306
            } else {
1307
13415
                has_coeffs = decode_coeffs_16bpp(td);
1308
            }
1309

368255
            if (!has_coeffs && b->bs <= BS_8x8 && !b->intra) {
1310
                b->skip = 1;
1311
                memset(&s->above_skip_ctx[col], 1, w4);
1312
                memset(&td->left_skip_ctx[td->row7], 1, h4);
1313
            }
1314
        } else {
1315
424435
            int row7 = td->row7;
1316
1317
#define SPLAT_ZERO_CTX(v, n) \
1318
    switch (n) { \
1319
    case 1:  v = 0;          break; \
1320
    case 2:  AV_ZERO16(&v);  break; \
1321
    case 4:  AV_ZERO32(&v);  break; \
1322
    case 8:  AV_ZERO64(&v);  break; \
1323
    case 16: AV_ZERO128(&v); break; \
1324
    }
1325
#define SPLAT_ZERO_YUV(dir, var, off, n, dir2) \
1326
    do { \
1327
        SPLAT_ZERO_CTX(dir##_y_##var[off * 2], n * 2); \
1328
        if (s->ss_##dir2) { \
1329
            SPLAT_ZERO_CTX(dir##_uv_##var[0][off], n); \
1330
            SPLAT_ZERO_CTX(dir##_uv_##var[1][off], n); \
1331
        } else { \
1332
            SPLAT_ZERO_CTX(dir##_uv_##var[0][off * 2], n * 2); \
1333
            SPLAT_ZERO_CTX(dir##_uv_##var[1][off * 2], n * 2); \
1334
        } \
1335
    } while (0)
1336
1337

424435
            switch (w4) {
1338
247172
            case 1: SPLAT_ZERO_YUV(s->above, nnz_ctx, col, 1, h); break;
1339
133369
            case 2: SPLAT_ZERO_YUV(s->above, nnz_ctx, col, 2, h); break;
1340
32871
            case 4: SPLAT_ZERO_YUV(s->above, nnz_ctx, col, 4, h); break;
1341
11023
            case 8: SPLAT_ZERO_YUV(s->above, nnz_ctx, col, 8, h); break;
1342
            }
1343

424435
            switch (h4) {
1344
252401
            case 1: SPLAT_ZERO_YUV(td->left, nnz_ctx, row7, 1, v); break;
1345
130077
            case 2: SPLAT_ZERO_YUV(td->left, nnz_ctx, row7, 2, v); break;
1346
31630
            case 4: SPLAT_ZERO_YUV(td->left, nnz_ctx, row7, 4, v); break;
1347
10327
            case 8: SPLAT_ZERO_YUV(td->left, nnz_ctx, row7, 8, v); break;
1348
            }
1349
792690
        }
1350
1351
792690
        if (s->pass == 1) {
1352
            s->td[0].b++;
1353
            s->td[0].block += w4 * h4 * 64 * bytesperpixel;
1354
            s->td[0].uvblock[0] += w4 * h4 * 64 * bytesperpixel >> (s->ss_h + s->ss_v);
1355
            s->td[0].uvblock[1] += w4 * h4 * 64 * bytesperpixel >> (s->ss_h + s->ss_v);
1356
            s->td[0].eob += 4 * w4 * h4;
1357
            s->td[0].uveob[0] += 4 * w4 * h4 >> (s->ss_h + s->ss_v);
1358
            s->td[0].uveob[1] += 4 * w4 * h4 >> (s->ss_h + s->ss_v);
1359
1360
            return;
1361
        }
1362
    }
1363
1364
    // emulated overhangs if the stride of the target buffer can't hold. This
1365
    // makes it possible to support emu-edge and so on even if we have large block
1366
    // overhangs
1367
1585380
    emu[0] = (col + w4) * 8 * bytesperpixel > f->linesize[0] ||
1368
792690
             (row + h4) > s->rows;
1369
1585380
    emu[1] = ((col + w4) * 8 >> s->ss_h) * bytesperpixel > f->linesize[1] ||
1370
792690
             (row + h4) > s->rows;
1371
792690
    if (emu[0]) {
1372
2238
        td->dst[0] = td->tmp_y;
1373
2238
        td->y_stride = 128;
1374
    } else {
1375
790452
        td->dst[0] = f->data[0] + yoff;
1376
790452
        td->y_stride = f->linesize[0];
1377
    }
1378
792690
    if (emu[1]) {
1379
2238
        td->dst[1] = td->tmp_uv[0];
1380
2238
        td->dst[2] = td->tmp_uv[1];
1381
2238
        td->uv_stride = 128;
1382
    } else {
1383
790452
        td->dst[1] = f->data[1] + uvoff;
1384
790452
        td->dst[2] = f->data[2] + uvoff;
1385
790452
        td->uv_stride = f->linesize[1];
1386
    }
1387
792690
    if (b->intra) {
1388
263748
        if (s->s.h.bpp > 8) {
1389
4809
            ff_vp9_intra_recon_16bpp(td, yoff, uvoff);
1390
        } else {
1391
258939
            ff_vp9_intra_recon_8bpp(td, yoff, uvoff);
1392
        }
1393
    } else {
1394
528942
        if (s->s.h.bpp > 8) {
1395
14448
            ff_vp9_inter_recon_16bpp(td);
1396
        } else {
1397
514494
            ff_vp9_inter_recon_8bpp(td);
1398
        }
1399
    }
1400
792690
    if (emu[0]) {
1401
2238
        int w = FFMIN(s->cols - col, w4) * 8, h = FFMIN(s->rows - row, h4) * 8, n, o = 0;
1402
1403
6465
        for (n = 0; o < w; n++) {
1404
4227
            int bw = 64 >> n;
1405
1406
            av_assert2(n <= 4);
1407
4227
            if (w & bw) {
1408
2253
                s->dsp.mc[n][0][0][0][0](f->data[0] + yoff + o * bytesperpixel, f->linesize[0],
1409
2253
                                         td->tmp_y + o * bytesperpixel, 128, h, 0, 0);
1410
2253
                o += bw;
1411
            }
1412
        }
1413
    }
1414
792690
    if (emu[1]) {
1415
2238
        int w = FFMIN(s->cols - col, w4) * 8 >> s->ss_h;
1416
2238
        int h = FFMIN(s->rows - row, h4) * 8 >> s->ss_v, n, o = 0;
1417
1418
6465
        for (n = s->ss_h; o < w; n++) {
1419
4227
            int bw = 64 >> n;
1420
1421
            av_assert2(n <= 4);
1422
4227
            if (w & bw) {
1423
2253
                s->dsp.mc[n][0][0][0][0](f->data[1] + uvoff + o * bytesperpixel, f->linesize[1],
1424
2253
                                         td->tmp_uv[0] + o * bytesperpixel, 128, h, 0, 0);
1425
2253
                s->dsp.mc[n][0][0][0][0](f->data[2] + uvoff + o * bytesperpixel, f->linesize[2],
1426
2253
                                         td->tmp_uv[1] + o * bytesperpixel, 128, h, 0, 0);
1427
2253
                o += bw;
1428
            }
1429
        }
1430
    }
1431
1432
    // pick filter level and find edges to apply filter to
1433
792690
    if (s->s.h.filter.level &&
1434
762730
        (lvl = s->s.h.segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
1435
762730
                                                      [b->mode[3] != ZEROMV]) > 0) {
1436
762639
        int x_end = FFMIN(s->cols - col, w4), y_end = FFMIN(s->rows - row, h4);
1437

762639
        int skip_inter = !b->intra && b->skip, col7 = td->col7, row7 = td->row7;
1438
1439
762639
        setctx_2d(&lflvl->level[row7 * 8 + col7], w4, h4, 8, lvl);
1440
762639
        mask_edges(lflvl->mask[0], 0, 0, row7, col7, x_end, y_end, 0, 0, b->tx, skip_inter);
1441

762639
        if (s->ss_h || s->ss_v)
1442
1499025
            mask_edges(lflvl->mask[1], s->ss_h, s->ss_v, row7, col7, x_end, y_end,
1443

752683
                       s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
1444

752683
                       s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
1445
                       b->uvtx, skip_inter);
1446
    }
1447
1448
792690
    if (s->pass == 2) {
1449
        s->td[0].b++;
1450
        s->td[0].block += w4 * h4 * 64 * bytesperpixel;
1451
        s->td[0].uvblock[0] += w4 * h4 * 64 * bytesperpixel >> (s->ss_v + s->ss_h);
1452
        s->td[0].uvblock[1] += w4 * h4 * 64 * bytesperpixel >> (s->ss_v + s->ss_h);
1453
        s->td[0].eob += 4 * w4 * h4;
1454
        s->td[0].uveob[0] += 4 * w4 * h4 >> (s->ss_v + s->ss_h);
1455
        s->td[0].uveob[1] += 4 * w4 * h4 >> (s->ss_v + s->ss_h);
1456
    }
1457
}