loopfilter.c 63.4 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
2
3
4
5
6
7
8
9
10
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

Debargha Mukherjee's avatar
Debargha Mukherjee committed
11
12
#include <math.h>

Jingning Han's avatar
Jingning Han committed
13
14
#include "./vpx_config.h"
#include "./vpx_dsp_rtcd.h"
15
16
17
#include "vp10/common/loopfilter.h"
#include "vp10/common/onyxc_int.h"
#include "vp10/common/reconinter.h"
18
#include "vp10/common/restoration.h"
19
#include "vpx_dsp/vpx_dsp_common.h"
Jingning Han's avatar
Jingning Han committed
20
21
22
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/mem.h"

23
#include "vp10/common/seg_common.h"
Jingning Han's avatar
Jingning Han committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

// 64 bit masks for left transform size. Each 1 represents a position where
// we should apply a loop filter across the left border of an 8x8 block
// boundary.
//
// In the case of TX_16X16->  ( in low order byte first we end up with
// a mask that looks like this
//
//    10101010
//    10101010
//    10101010
//    10101010
//    10101010
//    10101010
//    10101010
//    10101010
//
// A loopfilter should be applied to every other 8x8 horizontally.
clang-format's avatar
clang-format committed
42
static const uint64_t left_64x64_txform_mask[TX_SIZES] = {
Jingning Han's avatar
Jingning Han committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  0xffffffffffffffffULL,  // TX_4X4
  0xffffffffffffffffULL,  // TX_8x8
  0x5555555555555555ULL,  // TX_16x16
  0x1111111111111111ULL,  // TX_32x32
};

// 64 bit masks for above transform size. Each 1 represents a position where
// we should apply a loop filter across the top border of an 8x8 block
// boundary.
//
// In the case of TX_32x32 ->  ( in low order byte first we end up with
// a mask that looks like this
//
//    11111111
//    00000000
//    00000000
//    00000000
//    11111111
//    00000000
//    00000000
//    00000000
//
// A loopfilter should be applied to every other 4 the row vertically.
clang-format's avatar
clang-format committed
66
static const uint64_t above_64x64_txform_mask[TX_SIZES] = {
Jingning Han's avatar
Jingning Han committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  0xffffffffffffffffULL,  // TX_4X4
  0xffffffffffffffffULL,  // TX_8x8
  0x00ff00ff00ff00ffULL,  // TX_16x16
  0x000000ff000000ffULL,  // TX_32x32
};

// 64 bit masks for prediction sizes (left). Each 1 represents a position
// where left border of an 8x8 block. These are aligned to the right most
// appropriate bit, and then shifted into place.
//
// In the case of TX_16x32 ->  ( low order byte first ) we end up with
// a mask that looks like this :
//
//  10000000
//  10000000
//  10000000
//  10000000
//  00000000
//  00000000
//  00000000
//  00000000
static const uint64_t left_prediction_mask[BLOCK_SIZES] = {
  0x0000000000000001ULL,  // BLOCK_4X4,
  0x0000000000000001ULL,  // BLOCK_4X8,
  0x0000000000000001ULL,  // BLOCK_8X4,
  0x0000000000000001ULL,  // BLOCK_8X8,
  0x0000000000000101ULL,  // BLOCK_8X16,
  0x0000000000000001ULL,  // BLOCK_16X8,
  0x0000000000000101ULL,  // BLOCK_16X16,
  0x0000000001010101ULL,  // BLOCK_16X32,
  0x0000000000000101ULL,  // BLOCK_32X16,
  0x0000000001010101ULL,  // BLOCK_32X32,
  0x0101010101010101ULL,  // BLOCK_32X64,
  0x0000000001010101ULL,  // BLOCK_64X32,
  0x0101010101010101ULL,  // BLOCK_64X64
};

// 64 bit mask to shift and set for each prediction size.
static const uint64_t above_prediction_mask[BLOCK_SIZES] = {
  0x0000000000000001ULL,  // BLOCK_4X4
  0x0000000000000001ULL,  // BLOCK_4X8
  0x0000000000000001ULL,  // BLOCK_8X4
  0x0000000000000001ULL,  // BLOCK_8X8
  0x0000000000000001ULL,  // BLOCK_8X16,
  0x0000000000000003ULL,  // BLOCK_16X8
  0x0000000000000003ULL,  // BLOCK_16X16
  0x0000000000000003ULL,  // BLOCK_16X32,
  0x000000000000000fULL,  // BLOCK_32X16,
  0x000000000000000fULL,  // BLOCK_32X32,
  0x000000000000000fULL,  // BLOCK_32X64,
  0x00000000000000ffULL,  // BLOCK_64X32,
  0x00000000000000ffULL,  // BLOCK_64X64
};
// 64 bit mask to shift and set for each prediction size. A bit is set for
// each 8x8 block that would be in the left most block of the given block
// size in the 64x64 block.
static const uint64_t size_mask[BLOCK_SIZES] = {
  0x0000000000000001ULL,  // BLOCK_4X4
  0x0000000000000001ULL,  // BLOCK_4X8
  0x0000000000000001ULL,  // BLOCK_8X4
  0x0000000000000001ULL,  // BLOCK_8X8
  0x0000000000000101ULL,  // BLOCK_8X16,
  0x0000000000000003ULL,  // BLOCK_16X8
  0x0000000000000303ULL,  // BLOCK_16X16
  0x0000000003030303ULL,  // BLOCK_16X32,
  0x0000000000000f0fULL,  // BLOCK_32X16,
  0x000000000f0f0f0fULL,  // BLOCK_32X32,
  0x0f0f0f0f0f0f0f0fULL,  // BLOCK_32X64,
  0x00000000ffffffffULL,  // BLOCK_64X32,
  0xffffffffffffffffULL,  // BLOCK_64X64
};

// These are used for masking the left and above borders.
clang-format's avatar
clang-format committed
140
static const uint64_t left_border = 0x1111111111111111ULL;
Jingning Han's avatar
Jingning Han committed
141
142
143
static const uint64_t above_border = 0x000000ff000000ffULL;

// 16 bit masks for uv transform sizes.
clang-format's avatar
clang-format committed
144
static const uint16_t left_64x64_txform_mask_uv[TX_SIZES] = {
Jingning Han's avatar
Jingning Han committed
145
146
147
148
149
150
  0xffff,  // TX_4X4
  0xffff,  // TX_8x8
  0x5555,  // TX_16x16
  0x1111,  // TX_32x32
};

clang-format's avatar
clang-format committed
151
static const uint16_t above_64x64_txform_mask_uv[TX_SIZES] = {
Jingning Han's avatar
Jingning Han committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  0xffff,  // TX_4X4
  0xffff,  // TX_8x8
  0x0f0f,  // TX_16x16
  0x000f,  // TX_32x32
};

// 16 bit left mask to shift and set for each uv prediction size.
static const uint16_t left_prediction_mask_uv[BLOCK_SIZES] = {
  0x0001,  // BLOCK_4X4,
  0x0001,  // BLOCK_4X8,
  0x0001,  // BLOCK_8X4,
  0x0001,  // BLOCK_8X8,
  0x0001,  // BLOCK_8X16,
  0x0001,  // BLOCK_16X8,
  0x0001,  // BLOCK_16X16,
  0x0011,  // BLOCK_16X32,
  0x0001,  // BLOCK_32X16,
  0x0011,  // BLOCK_32X32,
  0x1111,  // BLOCK_32X64
  0x0011,  // BLOCK_64X32,
  0x1111,  // BLOCK_64X64
};
// 16 bit above mask to shift and set for uv each prediction size.
static const uint16_t above_prediction_mask_uv[BLOCK_SIZES] = {
  0x0001,  // BLOCK_4X4
  0x0001,  // BLOCK_4X8
  0x0001,  // BLOCK_8X4
  0x0001,  // BLOCK_8X8
  0x0001,  // BLOCK_8X16,
  0x0001,  // BLOCK_16X8
  0x0001,  // BLOCK_16X16
  0x0001,  // BLOCK_16X32,
  0x0003,  // BLOCK_32X16,
  0x0003,  // BLOCK_32X32,
  0x0003,  // BLOCK_32X64,
  0x000f,  // BLOCK_64X32,
  0x000f,  // BLOCK_64X64
};

// 64 bit mask to shift and set for each uv prediction size
static const uint16_t size_mask_uv[BLOCK_SIZES] = {
  0x0001,  // BLOCK_4X4
  0x0001,  // BLOCK_4X8
  0x0001,  // BLOCK_8X4
  0x0001,  // BLOCK_8X8
  0x0001,  // BLOCK_8X16,
  0x0001,  // BLOCK_16X8
  0x0001,  // BLOCK_16X16
  0x0011,  // BLOCK_16X32,
  0x0003,  // BLOCK_32X16,
  0x0033,  // BLOCK_32X32,
  0x3333,  // BLOCK_32X64,
  0x00ff,  // BLOCK_64X32,
  0xffff,  // BLOCK_64X64
};
clang-format's avatar
clang-format committed
207
static const uint16_t left_border_uv = 0x1111;
Jingning Han's avatar
Jingning Han committed
208
209
210
211
212
static const uint16_t above_border_uv = 0x000f;

static const int mode_lf_lut[MB_MODE_COUNT] = {
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // INTRA_MODES
  1, 1, 0, 1                     // INTER_MODES (ZEROMV == 0)
Yue Chen's avatar
Yue Chen committed
213
#if CONFIG_EXT_INTER
clang-format's avatar
clang-format committed
214
215
216
217
  ,
  1,                            // NEWFROMNEARMV mode
  1, 1, 1, 1, 1, 1, 1, 1, 0, 1  // INTER_COMPOUND_MODES (ZERO_ZEROMV == 0)
#endif                          // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
};

static void update_sharpness(loop_filter_info_n *lfi, int sharpness_lvl) {
  int lvl;

  // For each possible value for the loop filter fill out limits
  for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++) {
    // Set loop filter parameters that control sharpness.
    int block_inside_limit = lvl >> ((sharpness_lvl > 0) + (sharpness_lvl > 4));

    if (sharpness_lvl > 0) {
      if (block_inside_limit > (9 - sharpness_lvl))
        block_inside_limit = (9 - sharpness_lvl);
    }

clang-format's avatar
clang-format committed
233
    if (block_inside_limit < 1) block_inside_limit = 1;
Jingning Han's avatar
Jingning Han committed
234
235
236
237
238
239
240
241
242

    memset(lfi->lfthr[lvl].lim, block_inside_limit, SIMD_WIDTH);
    memset(lfi->lfthr[lvl].mblim, (2 * (lvl + 2) + block_inside_limit),
           SIMD_WIDTH);
  }
}

static uint8_t get_filter_level(const loop_filter_info_n *lfi_n,
                                const MB_MODE_INFO *mbmi) {
243
244
#if CONFIG_SUPERTX
  const int segment_id = VPXMIN(mbmi->segment_id, mbmi->segment_id_supertx);
clang-format's avatar
clang-format committed
245
246
  assert(
      IMPLIES(supertx_enabled(mbmi), mbmi->segment_id_supertx != MAX_SEGMENTS));
247
248
249
250
251
252
  assert(IMPLIES(supertx_enabled(mbmi),
                 mbmi->segment_id_supertx <= mbmi->segment_id));
#else
  const int segment_id = mbmi->segment_id;
#endif  // CONFIG_SUPERTX
  return lfi_n->lvl[segment_id][mbmi->ref_frame[0]][mode_lf_lut[mbmi->mode]];
Jingning Han's avatar
Jingning Han committed
253
254
}

Yaowu Xu's avatar
Yaowu Xu committed
255
void vp10_loop_filter_init(VP10_COMMON *cm) {
Jingning Han's avatar
Jingning Han committed
256
257
258
259
260
261
262
263
264
265
266
267
268
  loop_filter_info_n *lfi = &cm->lf_info;
  struct loopfilter *lf = &cm->lf;
  int lvl;

  // init limits for given sharpness
  update_sharpness(lfi, lf->sharpness_level);
  lf->last_sharpness_level = lf->sharpness_level;

  // init hev threshold const vectors
  for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++)
    memset(lfi->lfthr[lvl].hev_thr, (lvl >> 4), SIMD_WIDTH);
}

Yaowu Xu's avatar
Yaowu Xu committed
269
void vp10_loop_filter_frame_init(VP10_COMMON *cm, int default_filt_lvl) {
Jingning Han's avatar
Jingning Han committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  int seg_id;
  // n_shift is the multiplier for lf_deltas
  // the multiplier is 1 for when filter_lvl is between 0 and 31;
  // 2 when filter_lvl is between 32 and 63
  const int scale = 1 << (default_filt_lvl >> 5);
  loop_filter_info_n *const lfi = &cm->lf_info;
  struct loopfilter *const lf = &cm->lf;
  const struct segmentation *const seg = &cm->seg;

  // update limits if sharpness has changed
  if (lf->last_sharpness_level != lf->sharpness_level) {
    update_sharpness(lfi, lf->sharpness_level);
    lf->last_sharpness_level = lf->sharpness_level;
  }

  for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) {
    int lvl_seg = default_filt_lvl;
    if (segfeature_active(seg, seg_id, SEG_LVL_ALT_LF)) {
      const int data = get_segdata(seg, seg_id, SEG_LVL_ALT_LF);
clang-format's avatar
clang-format committed
289
290
291
      lvl_seg = clamp(
          seg->abs_delta == SEGMENT_ABSDATA ? data : default_filt_lvl + data, 0,
          MAX_LOOP_FILTER);
Jingning Han's avatar
Jingning Han committed
292
293
294
295
296
297
298
299
300
301
302
    }

    if (!lf->mode_ref_delta_enabled) {
      // we could get rid of this if we assume that deltas are set to
      // zero when not in use; encoder always uses deltas
      memset(lfi->lvl[seg_id], lvl_seg, sizeof(lfi->lvl[seg_id]));
    } else {
      int ref, mode;
      const int intra_lvl = lvl_seg + lf->ref_deltas[INTRA_FRAME] * scale;
      lfi->lvl[seg_id][INTRA_FRAME][0] = clamp(intra_lvl, 0, MAX_LOOP_FILTER);

303
      for (ref = LAST_FRAME; ref < TOTAL_REFS_PER_FRAME; ++ref) {
Jingning Han's avatar
Jingning Han committed
304
        for (mode = 0; mode < MAX_MODE_LF_DELTAS; ++mode) {
clang-format's avatar
clang-format committed
305
306
          const int inter_lvl = lvl_seg + lf->ref_deltas[ref] * scale +
                                lf->mode_deltas[mode] * scale;
Jingning Han's avatar
Jingning Han committed
307
308
309
310
311
312
313
          lfi->lvl[seg_id][ref][mode] = clamp(inter_lvl, 0, MAX_LOOP_FILTER);
        }
      }
    }
  }
}

clang-format's avatar
clang-format committed
314
315
static void filter_selectively_vert_row2(int subsampling_factor, uint8_t *s,
                                         int pitch, unsigned int mask_16x16_l,
Jingning Han's avatar
Jingning Han committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
                                         unsigned int mask_8x8_l,
                                         unsigned int mask_4x4_l,
                                         unsigned int mask_4x4_int_l,
                                         const loop_filter_info_n *lfi_n,
                                         const uint8_t *lfl) {
  const int mask_shift = subsampling_factor ? 4 : 8;
  const int mask_cutoff = subsampling_factor ? 0xf : 0xff;
  const int lfl_forward = subsampling_factor ? 4 : 8;

  unsigned int mask_16x16_0 = mask_16x16_l & mask_cutoff;
  unsigned int mask_8x8_0 = mask_8x8_l & mask_cutoff;
  unsigned int mask_4x4_0 = mask_4x4_l & mask_cutoff;
  unsigned int mask_4x4_int_0 = mask_4x4_int_l & mask_cutoff;
  unsigned int mask_16x16_1 = (mask_16x16_l >> mask_shift) & mask_cutoff;
  unsigned int mask_8x8_1 = (mask_8x8_l >> mask_shift) & mask_cutoff;
  unsigned int mask_4x4_1 = (mask_4x4_l >> mask_shift) & mask_cutoff;
  unsigned int mask_4x4_int_1 = (mask_4x4_int_l >> mask_shift) & mask_cutoff;
  unsigned int mask;

  for (mask = mask_16x16_0 | mask_8x8_0 | mask_4x4_0 | mask_4x4_int_0 |
              mask_16x16_1 | mask_8x8_1 | mask_4x4_1 | mask_4x4_int_1;
       mask; mask >>= 1) {
    const loop_filter_thresh *lfi0 = lfi_n->lfthr + *lfl;
    const loop_filter_thresh *lfi1 = lfi_n->lfthr + *(lfl + lfl_forward);

    if (mask & 1) {
      if ((mask_16x16_0 | mask_16x16_1) & 1) {
        if ((mask_16x16_0 & mask_16x16_1) & 1) {
          vpx_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
                                   lfi0->hev_thr);
        } else if (mask_16x16_0 & 1) {
clang-format's avatar
clang-format committed
347
          vpx_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr);
Jingning Han's avatar
Jingning Han committed
348
        } else {
clang-format's avatar
clang-format committed
349
350
          vpx_lpf_vertical_16(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
                              lfi1->hev_thr);
Jingning Han's avatar
Jingning Han committed
351
352
353
354
355
356
357
358
359
        }
      }

      if ((mask_8x8_0 | mask_8x8_1) & 1) {
        if ((mask_8x8_0 & mask_8x8_1) & 1) {
          vpx_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
                                  lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                  lfi1->hev_thr);
        } else if (mask_8x8_0 & 1) {
360
          vpx_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr);
Jingning Han's avatar
Jingning Han committed
361
362
        } else {
          vpx_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
363
                             lfi1->hev_thr);
Jingning Han's avatar
Jingning Han committed
364
365
366
367
368
369
370
371
372
        }
      }

      if ((mask_4x4_0 | mask_4x4_1) & 1) {
        if ((mask_4x4_0 & mask_4x4_1) & 1) {
          vpx_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
                                  lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                  lfi1->hev_thr);
        } else if (mask_4x4_0 & 1) {
373
          vpx_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr);
Jingning Han's avatar
Jingning Han committed
374
375
        } else {
          vpx_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
376
                             lfi1->hev_thr);
Jingning Han's avatar
Jingning Han committed
377
378
379
380
381
382
383
384
385
386
        }
      }

      if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
        if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
          vpx_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
                                  lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                  lfi1->hev_thr);
        } else if (mask_4x4_int_0 & 1) {
          vpx_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
387
                             lfi0->hev_thr);
Jingning Han's avatar
Jingning Han committed
388
389
        } else {
          vpx_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim, lfi1->lim,
390
                             lfi1->hev_thr);
Jingning Han's avatar
Jingning Han committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
        }
      }
    }

    s += 8;
    lfl += 1;
    mask_16x16_0 >>= 1;
    mask_8x8_0 >>= 1;
    mask_4x4_0 >>= 1;
    mask_4x4_int_0 >>= 1;
    mask_16x16_1 >>= 1;
    mask_8x8_1 >>= 1;
    mask_4x4_1 >>= 1;
    mask_4x4_int_1 >>= 1;
  }
}

#if CONFIG_VP9_HIGHBITDEPTH
clang-format's avatar
clang-format committed
409
410
411
412
413
static void highbd_filter_selectively_vert_row2(
    int subsampling_factor, uint16_t *s, int pitch, unsigned int mask_16x16_l,
    unsigned int mask_8x8_l, unsigned int mask_4x4_l,
    unsigned int mask_4x4_int_l, const loop_filter_info_n *lfi_n,
    const uint8_t *lfl, int bd) {
Jingning Han's avatar
Jingning Han committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  const int mask_shift = subsampling_factor ? 4 : 8;
  const int mask_cutoff = subsampling_factor ? 0xf : 0xff;
  const int lfl_forward = subsampling_factor ? 4 : 8;

  unsigned int mask_16x16_0 = mask_16x16_l & mask_cutoff;
  unsigned int mask_8x8_0 = mask_8x8_l & mask_cutoff;
  unsigned int mask_4x4_0 = mask_4x4_l & mask_cutoff;
  unsigned int mask_4x4_int_0 = mask_4x4_int_l & mask_cutoff;
  unsigned int mask_16x16_1 = (mask_16x16_l >> mask_shift) & mask_cutoff;
  unsigned int mask_8x8_1 = (mask_8x8_l >> mask_shift) & mask_cutoff;
  unsigned int mask_4x4_1 = (mask_4x4_l >> mask_shift) & mask_cutoff;
  unsigned int mask_4x4_int_1 = (mask_4x4_int_l >> mask_shift) & mask_cutoff;
  unsigned int mask;

  for (mask = mask_16x16_0 | mask_8x8_0 | mask_4x4_0 | mask_4x4_int_0 |
clang-format's avatar
clang-format committed
429
              mask_16x16_1 | mask_8x8_1 | mask_4x4_1 | mask_4x4_int_1;
Jingning Han's avatar
Jingning Han committed
430
431
432
433
434
435
436
437
438
439
440
441
442
       mask; mask >>= 1) {
    const loop_filter_thresh *lfi0 = lfi_n->lfthr + *lfl;
    const loop_filter_thresh *lfi1 = lfi_n->lfthr + *(lfl + lfl_forward);

    if (mask & 1) {
      if ((mask_16x16_0 | mask_16x16_1) & 1) {
        if ((mask_16x16_0 & mask_16x16_1) & 1) {
          vpx_highbd_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
                                          lfi0->hev_thr, bd);
        } else if (mask_16x16_0 & 1) {
          vpx_highbd_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim,
                                     lfi0->hev_thr, bd);
        } else {
clang-format's avatar
clang-format committed
443
          vpx_highbd_lpf_vertical_16(s + 8 * pitch, pitch, lfi1->mblim,
Jingning Han's avatar
Jingning Han committed
444
445
446
447
448
449
450
451
452
453
454
                                     lfi1->lim, lfi1->hev_thr, bd);
        }
      }

      if ((mask_8x8_0 | mask_8x8_1) & 1) {
        if ((mask_8x8_0 & mask_8x8_1) & 1) {
          vpx_highbd_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
                                         lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                         lfi1->hev_thr, bd);
        } else if (mask_8x8_0 & 1) {
          vpx_highbd_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim,
455
                                    lfi0->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
456
457
        } else {
          vpx_highbd_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim,
458
                                    lfi1->lim, lfi1->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
459
460
461
462
463
464
465
466
467
468
        }
      }

      if ((mask_4x4_0 | mask_4x4_1) & 1) {
        if ((mask_4x4_0 & mask_4x4_1) & 1) {
          vpx_highbd_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
                                         lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                         lfi1->hev_thr, bd);
        } else if (mask_4x4_0 & 1) {
          vpx_highbd_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim,
469
                                    lfi0->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
470
471
        } else {
          vpx_highbd_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim,
472
                                    lfi1->lim, lfi1->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
473
474
475
476
477
478
479
480
481
482
        }
      }

      if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
        if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
          vpx_highbd_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
                                         lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                         lfi1->hev_thr, bd);
        } else if (mask_4x4_int_0 & 1) {
          vpx_highbd_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
483
                                    lfi0->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
484
485
        } else {
          vpx_highbd_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim,
486
                                    lfi1->lim, lfi1->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
        }
      }
    }

    s += 8;
    lfl += 1;
    mask_16x16_0 >>= 1;
    mask_8x8_0 >>= 1;
    mask_4x4_0 >>= 1;
    mask_4x4_int_0 >>= 1;
    mask_16x16_1 >>= 1;
    mask_8x8_1 >>= 1;
    mask_4x4_1 >>= 1;
    mask_4x4_int_1 >>= 1;
  }
}
#endif  // CONFIG_VP9_HIGHBITDEPTH

clang-format's avatar
clang-format committed
505
506
507
508
static void filter_selectively_horiz(
    uint8_t *s, int pitch, unsigned int mask_16x16, unsigned int mask_8x8,
    unsigned int mask_4x4, unsigned int mask_4x4_int,
    const loop_filter_info_n *lfi_n, const uint8_t *lfl) {
Jingning Han's avatar
Jingning Han committed
509
510
511
  unsigned int mask;
  int count;

clang-format's avatar
clang-format committed
512
513
  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int; mask;
       mask >>= count) {
Jingning Han's avatar
Jingning Han committed
514
515
516
517
518
519
    const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;

    count = 1;
    if (mask & 1) {
      if (mask_16x16 & 1) {
        if ((mask_16x16 & 3) == 3) {
520
521
          vpx_lpf_horizontal_edge_16(s, pitch, lfi->mblim, lfi->lim,
                                     lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
522
523
          count = 2;
        } else {
524
525
          vpx_lpf_horizontal_edge_8(s, pitch, lfi->mblim, lfi->lim,
                                    lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
        }
      } else if (mask_8x8 & 1) {
        if ((mask_8x8 & 3) == 3) {
          // Next block's thresholds.
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

          vpx_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
                                    lfi->hev_thr, lfin->mblim, lfin->lim,
                                    lfin->hev_thr);

          if ((mask_4x4_int & 3) == 3) {
            vpx_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
                                      lfi->lim, lfi->hev_thr, lfin->mblim,
                                      lfin->lim, lfin->hev_thr);
          } else {
            if (mask_4x4_int & 1)
              vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
543
                                   lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
544
545
            else if (mask_4x4_int & 2)
              vpx_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
546
                                   lfin->lim, lfin->hev_thr);
Jingning Han's avatar
Jingning Han committed
547
548
549
          }
          count = 2;
        } else {
550
          vpx_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
551
552
553

          if (mask_4x4_int & 1)
            vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
554
                                 lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
        }
      } else if (mask_4x4 & 1) {
        if ((mask_4x4 & 3) == 3) {
          // Next block's thresholds.
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

          vpx_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
                                    lfi->hev_thr, lfin->mblim, lfin->lim,
                                    lfin->hev_thr);
          if ((mask_4x4_int & 3) == 3) {
            vpx_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
                                      lfi->lim, lfi->hev_thr, lfin->mblim,
                                      lfin->lim, lfin->hev_thr);
          } else {
            if (mask_4x4_int & 1)
              vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
571
                                   lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
572
573
            else if (mask_4x4_int & 2)
              vpx_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
574
                                   lfin->lim, lfin->hev_thr);
Jingning Han's avatar
Jingning Han committed
575
576
577
          }
          count = 2;
        } else {
578
          vpx_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
579
580
581

          if (mask_4x4_int & 1)
            vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
582
                                 lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
583
584
585
        }
      } else if (mask_4x4_int & 1) {
        vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
586
                             lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
587
588
589
590
591
592
593
594
595
596
597
598
      }
    }
    s += 8 * count;
    lfl += count;
    mask_16x16 >>= count;
    mask_8x8 >>= count;
    mask_4x4 >>= count;
    mask_4x4_int >>= count;
  }
}

#if CONFIG_VP9_HIGHBITDEPTH
clang-format's avatar
clang-format committed
599
600
601
602
static void highbd_filter_selectively_horiz(
    uint16_t *s, int pitch, unsigned int mask_16x16, unsigned int mask_8x8,
    unsigned int mask_4x4, unsigned int mask_4x4_int,
    const loop_filter_info_n *lfi_n, const uint8_t *lfl, int bd) {
Jingning Han's avatar
Jingning Han committed
603
604
605
  unsigned int mask;
  int count;

clang-format's avatar
clang-format committed
606
607
  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int; mask;
       mask >>= count) {
Jingning Han's avatar
Jingning Han committed
608
609
610
611
612
613
    const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;

    count = 1;
    if (mask & 1) {
      if (mask_16x16 & 1) {
        if ((mask_16x16 & 3) == 3) {
614
615
          vpx_highbd_lpf_horizontal_edge_16(s, pitch, lfi->mblim, lfi->lim,
                                            lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
616
617
          count = 2;
        } else {
618
619
          vpx_highbd_lpf_horizontal_edge_8(s, pitch, lfi->mblim, lfi->lim,
                                           lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
620
621
622
623
624
625
626
627
628
629
630
        }
      } else if (mask_8x8 & 1) {
        if ((mask_8x8 & 3) == 3) {
          // Next block's thresholds.
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

          vpx_highbd_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
                                           lfi->hev_thr, lfin->mblim, lfin->lim,
                                           lfin->hev_thr, bd);

          if ((mask_4x4_int & 3) == 3) {
clang-format's avatar
clang-format committed
631
632
633
            vpx_highbd_lpf_horizontal_4_dual(
                s + 4 * pitch, pitch, lfi->mblim, lfi->lim, lfi->hev_thr,
                lfin->mblim, lfin->lim, lfin->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
634
635
636
          } else {
            if (mask_4x4_int & 1) {
              vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
637
                                          lfi->lim, lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
638
639
            } else if (mask_4x4_int & 2) {
              vpx_highbd_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
640
                                          lfin->lim, lfin->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
641
642
643
644
645
            }
          }
          count = 2;
        } else {
          vpx_highbd_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim,
646
                                      lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
647
648
649

          if (mask_4x4_int & 1) {
            vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
650
                                        lfi->lim, lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
651
652
653
654
655
656
657
658
659
660
661
          }
        }
      } else if (mask_4x4 & 1) {
        if ((mask_4x4 & 3) == 3) {
          // Next block's thresholds.
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

          vpx_highbd_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
                                           lfi->hev_thr, lfin->mblim, lfin->lim,
                                           lfin->hev_thr, bd);
          if ((mask_4x4_int & 3) == 3) {
clang-format's avatar
clang-format committed
662
663
664
            vpx_highbd_lpf_horizontal_4_dual(
                s + 4 * pitch, pitch, lfi->mblim, lfi->lim, lfi->hev_thr,
                lfin->mblim, lfin->lim, lfin->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
665
666
667
          } else {
            if (mask_4x4_int & 1) {
              vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
668
                                          lfi->lim, lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
669
670
            } else if (mask_4x4_int & 2) {
              vpx_highbd_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
671
                                          lfin->lim, lfin->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
672
673
674
675
676
            }
          }
          count = 2;
        } else {
          vpx_highbd_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim,
677
                                      lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
678
679
680

          if (mask_4x4_int & 1) {
            vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
681
                                        lfi->lim, lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
682
683
684
685
          }
        }
      } else if (mask_4x4_int & 1) {
        vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
686
                                    lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
      }
    }
    s += 8 * count;
    lfl += count;
    mask_16x16 >>= count;
    mask_8x8 >>= count;
    mask_4x4 >>= count;
    mask_4x4_int >>= count;
  }
}
#endif  // CONFIG_VP9_HIGHBITDEPTH

// This function ors into the current lfm structure, where to do loop
// filters for the specific mi we are looking at. It uses information
// including the block_size_type (32x16, 32x32, etc.), the transform size,
// whether there were any coefficients encoded, and the loop filter strength
// block we are currently looking at. Shift is used to position the
// 1's we produce.
// TODO(JBB) Need another function for different resolution color..
static void build_masks(const loop_filter_info_n *const lfi_n,
                        const MODE_INFO *mi, const int shift_y,
clang-format's avatar
clang-format committed
708
                        const int shift_uv, LOOP_FILTER_MASK *lfm) {
Jingning Han's avatar
Jingning Han committed
709
710
  const MB_MODE_INFO *mbmi = &mi->mbmi;
  const BLOCK_SIZE block_size = mbmi->sb_type;
711
712
  // TODO(debargha): Check if masks can be setup correctly when
  // rectangular transfroms are used with the EXT_TX expt.
Debargha Mukherjee's avatar
Debargha Mukherjee committed
713
  const TX_SIZE tx_size_y = txsize_sqr_up_map[mbmi->tx_size];
714
715
  const TX_SIZE tx_size_uv =
      get_uv_tx_size_impl(mbmi->tx_size, block_size, 1, 1);
Jingning Han's avatar
Jingning Han committed
716
717
718
719
720
721
  const int filter_level = get_filter_level(lfi_n, mbmi);
  uint64_t *const left_y = &lfm->left_y[tx_size_y];
  uint64_t *const above_y = &lfm->above_y[tx_size_y];
  uint64_t *const int_4x4_y = &lfm->int_4x4_y;
  uint16_t *const left_uv = &lfm->left_uv[tx_size_uv];
  uint16_t *const above_uv = &lfm->above_uv[tx_size_uv];
722
  uint16_t *const int_4x4_uv = &lfm->left_int_4x4_uv;
Jingning Han's avatar
Jingning Han committed
723
724
725
726
727
728
729
730
  int i;

  // If filter level is 0 we don't loop filter.
  if (!filter_level) {
    return;
  } else {
    const int w = num_8x8_blocks_wide_lookup[block_size];
    const int h = num_8x8_blocks_high_lookup[block_size];
731
732
733
    const int row = (shift_y >> MAX_MIB_SIZE_LOG2);
    const int col = shift_y - (row << MAX_MIB_SIZE_LOG2);

clang-format's avatar
clang-format committed
734
    for (i = 0; i < h; i++) memset(&lfm->lfl_y[row + i][col], filter_level, w);
Jingning Han's avatar
Jingning Han committed
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
  }

  // These set 1 in the current block size for the block size edges.
  // For instance if the block size is 32x16, we'll set:
  //    above =   1111
  //              0000
  //    and
  //    left  =   1000
  //          =   1000
  // NOTE : In this example the low bit is left most ( 1000 ) is stored as
  //        1,  not 8...
  //
  // U and V set things on a 16 bit scale.
  //
  *above_y |= above_prediction_mask[block_size] << shift_y;
  *above_uv |= above_prediction_mask_uv[block_size] << shift_uv;
  *left_y |= left_prediction_mask[block_size] << shift_y;
  *left_uv |= left_prediction_mask_uv[block_size] << shift_uv;

  // If the block has no coefficients and is not intra we skip applying
  // the loop filter on block edges.
clang-format's avatar
clang-format committed
756
  if ((mbmi->skip || mbmi->has_no_coeffs) && is_inter_block(mbmi)) return;
Jingning Han's avatar
Jingning Han committed
757
758
759
760
761

  // Here we are adding a mask for the transform size. The transform
  // size mask is set to be correct for a 64x64 prediction block size. We
  // mask to match the size of the block we are working on and then shift it
  // into place..
clang-format's avatar
clang-format committed
762
763
764
765
766
  *above_y |= (size_mask[block_size] & above_64x64_txform_mask[tx_size_y])
              << shift_y;
  *above_uv |=
      (size_mask_uv[block_size] & above_64x64_txform_mask_uv[tx_size_uv])
      << shift_uv;
Jingning Han's avatar
Jingning Han committed
767

clang-format's avatar
clang-format committed
768
769
770
771
  *left_y |= (size_mask[block_size] & left_64x64_txform_mask[tx_size_y])
             << shift_y;
  *left_uv |= (size_mask_uv[block_size] & left_64x64_txform_mask_uv[tx_size_uv])
              << shift_uv;
Jingning Han's avatar
Jingning Han committed
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788

  // Here we are trying to determine what to do with the internal 4x4 block
  // boundaries.  These differ from the 4x4 boundaries on the outside edge of
  // an 8x8 in that the internal ones can be skipped and don't depend on
  // the prediction block size.
  if (tx_size_y == TX_4X4)
    *int_4x4_y |= (size_mask[block_size] & 0xffffffffffffffffULL) << shift_y;

  if (tx_size_uv == TX_4X4)
    *int_4x4_uv |= (size_mask_uv[block_size] & 0xffff) << shift_uv;
}

// This function does the same thing as the one above with the exception that
// it only affects the y masks. It exists because for blocks < 16x16 in size,
// we only update u and v masks on the first block.
static void build_y_mask(const loop_filter_info_n *const lfi_n,
                         const MODE_INFO *mi, const int shift_y,
789
790
791
#if CONFIG_SUPERTX
                         int supertx_enabled,
#endif  // CONFIG_SUPERTX
Jingning Han's avatar
Jingning Han committed
792
793
                         LOOP_FILTER_MASK *lfm) {
  const MB_MODE_INFO *mbmi = &mi->mbmi;
Debargha Mukherjee's avatar
Debargha Mukherjee committed
794
  const TX_SIZE tx_size_y = txsize_sqr_up_map[mbmi->tx_size];
795
796
797
798
799
800
#if CONFIG_SUPERTX
  const BLOCK_SIZE block_size =
      supertx_enabled ? (BLOCK_SIZE)(3 * tx_size_y) : mbmi->sb_type;
#else
  const BLOCK_SIZE block_size = mbmi->sb_type;
#endif
Jingning Han's avatar
Jingning Han committed
801
802
803
804
805
806
807
808
809
810
811
  const int filter_level = get_filter_level(lfi_n, mbmi);
  uint64_t *const left_y = &lfm->left_y[tx_size_y];
  uint64_t *const above_y = &lfm->above_y[tx_size_y];
  uint64_t *const int_4x4_y = &lfm->int_4x4_y;
  int i;

  if (!filter_level) {
    return;
  } else {
    const int w = num_8x8_blocks_wide_lookup[block_size];
    const int h = num_8x8_blocks_high_lookup[block_size];
812
813
814
    const int row = (shift_y >> MAX_MIB_SIZE_LOG2);
    const int col = shift_y - (row << MAX_MIB_SIZE_LOG2);

clang-format's avatar
clang-format committed
815
    for (i = 0; i < h; i++) memset(&lfm->lfl_y[row + i][col], filter_level, w);
Jingning Han's avatar
Jingning Han committed
816
817
818
819
820
  }

  *above_y |= above_prediction_mask[block_size] << shift_y;
  *left_y |= left_prediction_mask[block_size] << shift_y;

clang-format's avatar
clang-format committed
821
  if ((mbmi->skip || mbmi->has_no_coeffs) && is_inter_block(mbmi)) return;
Jingning Han's avatar
Jingning Han committed
822

clang-format's avatar
clang-format committed
823
824
  *above_y |= (size_mask[block_size] & above_64x64_txform_mask[tx_size_y])
              << shift_y;
Jingning Han's avatar
Jingning Han committed
825

clang-format's avatar
clang-format committed
826
827
  *left_y |= (size_mask[block_size] & left_64x64_txform_mask[tx_size_y])
             << shift_y;
Jingning Han's avatar
Jingning Han committed
828
829
830
831
832
833
834
835

  if (tx_size_y == TX_4X4)
    *int_4x4_y |= (size_mask[block_size] & 0xffffffffffffffffULL) << shift_y;
}

// This function sets up the bit masks for the entire 64x64 region represented
// by mi_row, mi_col.
// TODO(JBB): This function only works for yv12.
Yaowu Xu's avatar
Yaowu Xu committed
836
void vp10_setup_mask(VP10_COMMON *const cm, const int mi_row, const int mi_col,
clang-format's avatar
clang-format committed
837
838
                     MODE_INFO **mi, const int mode_info_stride,
                     LOOP_FILTER_MASK *lfm) {
Jingning Han's avatar
Jingning Han committed
839
840
841
842
843
844
845
846
847
  int idx_32, idx_16, idx_8;
  const loop_filter_info_n *const lfi_n = &cm->lf_info;
  MODE_INFO **mip = mi;
  MODE_INFO **mip2 = mi;

  // These are offsets to the next mi in the 64x64 block. It is what gets
  // added to the mi ptr as we go through each loop. It helps us to avoid
  // setting up special row and column counters for each index. The last step
  // brings us out back to the starting position.
clang-format's avatar
clang-format committed
848
849
850
851
852
  const int offset_32[] = { 4, (mode_info_stride << 2) - 4, 4,
                            -(mode_info_stride << 2) - 4 };
  const int offset_16[] = { 2, (mode_info_stride << 1) - 2, 2,
                            -(mode_info_stride << 1) - 2 };
  const int offset[] = { 1, mode_info_stride - 1, 1, -mode_info_stride - 1 };
Jingning Han's avatar
Jingning Han committed
853
854
855
856
857

  // Following variables represent shifts to position the current block
  // mask over the appropriate block. A shift of 36 to the left will move
  // the bits for the final 32 by 32 block in the 64x64 up 4 rows and left
  // 4 rows to the appropriate spot.
clang-format's avatar
clang-format committed
858
859
860
861
862
  const int shift_32_y[] = { 0, 4, 32, 36 };
  const int shift_16_y[] = { 0, 2, 16, 18 };
  const int shift_8_y[] = { 0, 1, 8, 9 };
  const int shift_32_uv[] = { 0, 2, 8, 10 };
  const int shift_16_uv[] = { 0, 1, 4, 5 };
Jingning Han's avatar
Jingning Han committed
863
  int i;
864
865
  const int max_rows = VPXMIN(cm->mi_rows - mi_row, MAX_MIB_SIZE);
  const int max_cols = VPXMIN(cm->mi_cols - mi_col, MAX_MIB_SIZE);
866
867
868
#if CONFIG_EXT_PARTITION
  assert(0 && "Not yet updated");
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
869
870
871
872
873
874
875
876

  vp10_zero(*lfm);
  assert(mip[0] != NULL);

  // TODO(jimbankoski): Try moving most of the following code into decode
  // loop and storing lfm in the mbmi structure so that we don't have to go
  // through the recursive loop structure multiple times.
  switch (mip[0]->mbmi.sb_type) {
clang-format's avatar
clang-format committed
877
    case BLOCK_64X64: build_masks(lfi_n, mip[0], 0, 0, lfm); break;
Jingning Han's avatar
Jingning Han committed
878
879
880
    case BLOCK_64X32:
      build_masks(lfi_n, mip[0], 0, 0, lfm);
      mip2 = mip + mode_info_stride * 4;
clang-format's avatar
clang-format committed
881
      if (4 >= max_rows) break;
Jingning Han's avatar
Jingning Han committed
882
883
884
885
886
      build_masks(lfi_n, mip2[0], 32, 8, lfm);
      break;
    case BLOCK_32X64:
      build_masks(lfi_n, mip[0], 0, 0, lfm);
      mip2 = mip + 4;
clang-format's avatar
clang-format committed
887
      if (4 >= max_cols) break;
Jingning Han's avatar
Jingning Han committed
888
889
890
891
892
893
894
895
896
897
898
899
900
901
      build_masks(lfi_n, mip2[0], 4, 2, lfm);
      break;
    default:
      for (idx_32 = 0; idx_32 < 4; mip += offset_32[idx_32], ++idx_32) {
        const int shift_y = shift_32_y[idx_32];
        const int shift_uv = shift_32_uv[idx_32];
        const int mi_32_col_offset = ((idx_32 & 1) << 2);
        const int mi_32_row_offset = ((idx_32 >> 1) << 2);
        if (mi_32_col_offset >= max_cols || mi_32_row_offset >= max_rows)
          continue;
        switch (mip[0]->mbmi.sb_type) {
          case BLOCK_32X32:
            build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
            break;
clang-format's avatar
clang-format committed
902
          case BLOCK_32X16: build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
903
#if CONFIG_SUPERTX
clang-format's avatar
clang-format committed
904
            if (supertx_enabled(&mip[0]->mbmi)) break;
905
#endif
clang-format's avatar
clang-format committed
906
            if (mi_32_row_offset + 2 >= max_rows) continue;
Jingning Han's avatar
Jingning Han committed
907
908
909
            mip2 = mip + mode_info_stride * 2;
            build_masks(lfi_n, mip2[0], shift_y + 16, shift_uv + 4, lfm);
            break;
clang-format's avatar
clang-format committed
910
          case BLOCK_16X32: build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
911
#if CONFIG_SUPERTX
clang-format's avatar
clang-format committed
912
            if (supertx_enabled(&mip[0]->mbmi)) break;
913
#endif
clang-format's avatar
clang-format committed
914
            if (mi_32_col_offset + 2 >= max_cols) continue;
Jingning Han's avatar
Jingning Han committed
915
916
917
918
            mip2 = mip + 2;
            build_masks(lfi_n, mip2[0], shift_y + 2, shift_uv + 1, lfm);
            break;
          default:
919
920
921
922
923
924
#if CONFIG_SUPERTX
            if (mip[0]->mbmi.tx_size == TX_32X32) {
              build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
              break;
            }
#endif
Jingning Han's avatar
Jingning Han committed
925
926
927
            for (idx_16 = 0; idx_16 < 4; mip += offset_16[idx_16], ++idx_16) {
              const int shift_y = shift_32_y[idx_32] + shift_16_y[idx_16];
              const int shift_uv = shift_32_uv[idx_32] + shift_16_uv[idx_16];
clang-format's avatar
clang-format committed
928
929
930
931
              const int mi_16_col_offset =
                  mi_32_col_offset + ((idx_16 & 1) << 1);
              const int mi_16_row_offset =
                  mi_32_row_offset + ((idx_16 >> 1) << 1);
Jingning Han's avatar
Jingning Han committed
932
933
934
935
936
937
938
939
940

              if (mi_16_col_offset >= max_cols || mi_16_row_offset >= max_rows)
                continue;

              switch (mip[0]->mbmi.sb_type) {
                case BLOCK_16X16:
                  build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
                  break;
                case BLOCK_16X8:
941
#if CONFIG_SUPERTX
clang-format's avatar
clang-format committed
942
                  if (supertx_enabled(&mip[0]->mbmi)) break;
943
#endif
Jingning Han's avatar
Jingning Han committed
944
                  build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
clang-format's avatar
clang-format committed
945
                  if (mi_16_row_offset + 1 >= max_rows) continue;
Jingning Han's avatar
Jingning Han committed
946
                  mip2 = mip + mode_info_stride;
clang-format's avatar
clang-format committed
947
                  build_y_mask(lfi_n, mip2[0], shift_y + 8,
948
949
950
951
#if CONFIG_SUPERTX
                               0,
#endif
                               lfm);
Jingning Han's avatar
Jingning Han committed
952
953
                  break;
                case BLOCK_8X16:
954
#if CONFIG_SUPERTX
clang-format's avatar
clang-format committed
955
                  if (supertx_enabled(&mip[0]->mbmi)) break;
956
#endif
Jingning Han's avatar
Jingning Han committed
957
                  build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
clang-format's avatar
clang-format committed
958
                  if (mi_16_col_offset + 1 >= max_cols) continue;
Jingning Han's avatar
Jingning Han committed
959
                  mip2 = mip + 1;
clang-format's avatar
clang-format committed
960
                  build_y_mask(lfi_n, mip2[0], shift_y + 1,
961
962
963
964
#if CONFIG_SUPERTX
                               0,
#endif
                               lfm);
Jingning Han's avatar
Jingning Han committed
965
966
                  break;
                default: {
clang-format's avatar
clang-format committed
967
968
                  const int shift_y =
                      shift_32_y[idx_32] + shift_16_y[idx_16] + shift_8_y[0];
969
970
971
972
973
974
#if CONFIG_SUPERTX
                  if (mip[0]->mbmi.tx_size == TX_16X16) {
                    build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
                    break;
                  }
#endif
Jingning Han's avatar
Jingning Han committed
975
976
977
978
                  build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
                  mip += offset[0];
                  for (idx_8 = 1; idx_8 < 4; mip += offset[idx_8], ++idx_8) {
                    const int shift_y = shift_32_y[idx_32] +
clang-format's avatar
clang-format committed
979
980
981
982
983
                                        shift_16_y[idx_16] + shift_8_y[idx_8];
                    const int mi_8_col_offset =
                        mi_16_col_offset + ((idx_8 & 1));
                    const int mi_8_row_offset =
                        mi_16_row_offset + ((idx_8 >> 1));
Jingning Han's avatar
Jingning Han committed
984
985
986
987

                    if (mi_8_col_offset >= max_cols ||
                        mi_8_row_offset >= max_rows)
                      continue;
988
989
990
991
992
                    build_y_mask(lfi_n, mip[0], shift_y,
#if CONFIG_SUPERTX
                                 supertx_enabled(&mip[0]->mbmi),
#endif
                                 lfm);
Jingning Han's avatar
Jingning Han committed
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
                  }
                  break;
                }
              }
            }
            break;
        }
      }
      break;
  }
  // The largest loopfilter we have is 16x16 so we use the 16x16 mask
  // for 32x32 transforms also.
  lfm->left_y[TX_16X16] |= lfm->left_y[TX_32X32];
  lfm->above_y[TX_16X16] |= lfm->above_y[TX_32X32];
  lfm->left_uv[TX_16X16] |= lfm->left_uv[TX_32X32];
  lfm->above_uv[TX_16X16] |= lfm->above_uv[TX_32X32];

  // We do at least 8 tap filter on every 32x32 even if the transform size
  // is 4x4. So if the 4x4 is set on a border pixel add it to the 8x8 and
  // remove it from the 4x4.
  lfm->left_y[TX_8X8] |= lfm->left_y[TX_4X4] & left_border;
  lfm->left_y[TX_4X4] &= ~left_border;
  lfm->above_y[TX_8X8] |= lfm->above_y[TX_4X4] & above_border;
  lfm->above_y[TX_4X4] &= ~above_border;
  lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_4X4] & left_border_uv;
  lfm->left_uv[TX_4X4] &= ~left_border_uv;
  lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_4X4] & above_border_uv;
  lfm->above_uv[TX_4X4] &= ~above_border_uv;

  // We do some special edge handling.
1023
  if (mi_row + MAX_MIB_SIZE > cm->mi_rows) {
Jingning Han's avatar
Jingning Han committed
1024
1025
1026
    const uint64_t rows = cm->mi_rows - mi_row;

    // Each pixel inside the border gets a 1,
clang-format's avatar
clang-format committed
1027
    const uint64_t mask_y = (((uint64_t)1 << (rows << MAX_MIB_SIZE_LOG2)) - 1);
1028
    const uint16_t mask_uv =
clang-format's avatar
clang-format committed
1029
        (((uint16_t)1 << (((rows + 1) >> 1) << (MAX_MIB_SIZE_LOG2 - 1))) - 1);
Jingning Han's avatar
Jingning Han committed
1030
1031
1032
1033
1034
1035
1036
1037
1038

    // Remove values completely outside our border.
    for (i = 0; i < TX_32X32; i++) {
      lfm->left_y[i] &= mask_y;
      lfm->above_y[i] &= mask_y;
      lfm->left_uv[i] &= mask_uv;
      lfm->above_uv[i] &= mask_uv;
    }
    lfm->int_4x4_y &= mask_y;
1039
    lfm->above_int_4x4_uv = lfm->left_int_4x4_uv & mask_uv;
Jingning Han's avatar
Jingning Han committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052

    // We don't apply a wide loop filter on the last uv block row. If set
    // apply the shorter one instead.
    if (rows == 1) {
      lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16];
      lfm->above_uv[TX_16X16] = 0;
    }
    if (rows == 5) {
      lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16] & 0xff00;
      lfm->above_uv[TX_16X16] &= ~(lfm->above_uv[TX_16X16] & 0xff00);
    }
  }

1053
  if (mi_col + MAX_MIB_SIZE > cm->mi_cols) {
Jingning Han's avatar
Jingning Han committed
1054
1055
1056
1057
    const uint64_t columns = cm->mi_cols - mi_col;

    // Each pixel inside the border gets a 1, the multiply copies the border
    // to where we need it.
clang-format's avatar
clang-format committed
1058
    const uint64_t mask_y = (((1 << columns) - 1)) * 0x0101010101010101ULL;
Jingning Han's avatar
Jingning Han committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
    const uint16_t mask_uv = ((1 << ((columns + 1) >> 1)) - 1) * 0x1111;

    // Internal edges are not applied on the last column of the image so
    // we mask 1 more for the internal edges
    const uint16_t mask_uv_int = ((1 << (columns >> 1)) - 1) * 0x1111;

    // Remove the bits outside the image edge.
    for (i = 0; i < TX_32X32; i++) {
      lfm->left_y[i] &= mask_y;
      lfm->above_y[i] &= mask_y;
      lfm->left_uv[i] &= mask_uv;
      lfm->above_uv[i] &= mask_uv;
    }
    lfm->int_4x4_y &= mask_y;
1073
    lfm->left_int_4x4_uv &= mask_uv_int;
Jingning Han's avatar
Jingning Han committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099

    // We don't apply a wide loop filter on the last uv column. If set
    // apply the shorter one instead.
    if (columns == 1) {
      lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_16X16];
      lfm->left_uv[TX_16X16] = 0;
    }
    if (columns == 5) {
      lfm->left_uv[TX_8X8] |= (lfm->left_uv[TX_16X16] & 0xcccc);
      lfm->left_uv[TX_16X16] &= ~(lfm->left_uv[TX_16X16] & 0xcccc);
    }
  }
  // We don't apply a loop filter on the first column in the image, mask that
  // out.
  if (mi_col == 0) {
    for (i = 0; i < TX_32X32; i++) {
      lfm->left_y[i] &= 0xfefefefefefefefeULL;
      lfm->left_uv[i] &= 0xeeee;
    }
  }

  // Assert if we try to apply 2 different loop filters at the same position.
  assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_8X8]));
  assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_4X4]));
  assert(!(lfm->left_y[TX_8X8] & lfm->left_y[TX_4X4]));
  assert(!(lfm->int_4x4_y & lfm->left_y[TX_16X16]));
clang-format's avatar
clang-format committed
1100
  assert(!(lfm->left_uv[TX_16X16] & lfm->left_uv[TX_8X8]));
Jingning Han's avatar
Jingning Han committed
1101
1102
  assert(!(lfm->left_uv[TX_16X16] & lfm->left_uv[TX_4X4]));
  assert(!(lfm->left_uv[TX_8X8] & lfm->left_uv[TX_4X4]));
1103
  assert(!(lfm->left_int_4x4_uv & lfm->left_uv[TX_16X16]));
Jingning Han's avatar
Jingning Han committed
1104
1105
1106
1107
1108
1109
1110
  assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_8X8]));
  assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_4X4]));
  assert(!(lfm->above_y[TX_8X8] & lfm->above_y[TX_4X4]));
  assert(!(lfm->int_4x4_y & lfm->above_y[TX_16X16]));
  assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_8X8]));
  assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_4X4]));
  assert(!(lfm->above_uv[TX_8X8] & lfm->above_uv[TX_4X4]));
1111
  assert(!(lfm->above_int_4x4_uv & lfm->above_uv[TX_16X16]));
Jingning Han's avatar
Jingning Han committed
1112
1113
}

clang-format's avatar
clang-format committed
1114
1115
1116
1117
static void filter_selectively_vert(
    uint8_t *s, int pitch, unsigned int mask_16x16, unsigned int mask_8x8,
    unsigned int mask_4x4, unsigned int mask_4x4_int,
    const loop_filter_info_n *lfi_n, const uint8_t *lfl) {
Jingning Han's avatar
Jingning Han committed
1118
1119
  unsigned int mask;

clang-format's avatar
clang-format committed
1120
1121
  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int; mask;
       mask >>= 1) {
Jingning Han's avatar
Jingning Han committed
1122
1123
1124
1125
1126
1127
    const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;

    if (mask & 1) {
      if (mask_16x16 & 1) {
        vpx_lpf_vertical_16(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
      } else if (mask_8x8 & 1) {
1128
        vpx_lpf_vertical_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1129
      } else if (mask_4x4 & 1) {
1130
        vpx_lpf_vertical_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1131
1132
1133
      }
    }
    if (mask_4x4_int & 1)
1134
      vpx_lpf_vertical_4(s + 4, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
    s += 8;
    lfl += 1;
    mask_16x16 >>= 1;
    mask_8x8 >>= 1;
    mask_4x4 >>= 1;
    mask_4x4_int >>= 1;
  }
}

#if CONFIG_VP9_HIGHBITDEPTH
clang-format's avatar
clang-format committed
1145
1146
1147
1148
static void highbd_filter_selectively_vert(
    uint16_t *s, int pitch, unsigned int mask_16x16, unsigned int mask_8x8,
    unsigned int mask_4x4, unsigned int mask_4x4_int,
    const loop_filter_info_n *lfi_n, const uint8_t *lfl, int bd) {
Jingning Han's avatar
Jingning Han committed
1149
1150
  unsigned int mask;

clang-format's avatar
clang-format committed
1151
1152
  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int; mask;
       mask >>= 1) {
Jingning Han's avatar
Jingning Han committed
1153
1154
1155
1156
    const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;

    if (mask & 1) {
      if (mask_16x16 & 1) {
clang-format's avatar
clang-format committed
1157
1158
        vpx_highbd_lpf_vertical_16(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr,
                                   bd);
Jingning Han's avatar
Jingning Han committed
1159
      } else if (mask_8x8 & 1) {
clang-format's avatar
clang-format committed
1160
1161
        vpx_highbd_lpf_vertical_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr,
                                  bd);
Jingning Han's avatar
Jingning Han committed
1162
      } else if (mask_4x4 & 1) {
clang-format's avatar
clang-format committed
1163
1164
        vpx_highbd_lpf_vertical_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr,
                                  bd);
Jingning Han's avatar
Jingning Han committed
1165
1166
1167
1168
      }
    }
    if (mask_4x4_int & 1)
      vpx_highbd_lpf_vertical_4(s + 4, pitch, lfi->mblim, lfi->lim,
1169
                                lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
    s += 8;
    lfl += 1;
    mask_16x16 >>= 1;
    mask_8x8 >>= 1;
    mask_4x4 >>= 1;
    mask_4x4_int >>= 1;
  }
}
#endif  // CONFIG_VP9_HIGHBITDEPTH

Yaowu Xu's avatar
Yaowu Xu committed
1180
void vp10_filter_block_plane_non420(VP10_COMMON *cm,
Jingning Han's avatar
Jingning Han committed
1181
                                    struct macroblockd_plane *plane,
clang-format's avatar
clang-format committed
1182
                                    MODE_INFO **mib, int mi_row, int mi_col) {
Jingning Han's avatar
Jingning Han committed
1183
1184
1185
1186
1187
  const int ss_x = plane->subsampling_x;
  const int ss_y = plane->subsampling_y;
  const int row_step = 1 << ss_y;
  const int col_step = 1 << ss_x;
  struct buf_2d *const dst = &plane->dst;
clang-format's avatar
clang-format committed
1188
1189
1190
1191
1192
  uint8_t *const dst0 = dst->buf;
  unsigned int mask_16x16[MAX_MIB_SIZE] = { 0 };
  unsigned int mask_8x8[MAX_MIB_SIZE] = { 0 };
  unsigned int mask_4x4[MAX_MIB_SIZE] = { 0 };
  unsigned int mask_4x4_int[MAX_MIB_SIZE] = { 0 };
1193
  uint8_t lfl[MAX_MIB_SIZE][MAX_MIB_SIZE];
Jingning Han's avatar
Jingning Han committed
1194
1195
  int r, c;

1196
  for (r = 0; r < cm->mib_size && mi_row + r < cm->mi_rows; r += row_step) {
Jingning Han's avatar
Jingning Han committed
1197
1198
1199
1200
1201
1202
    unsigned int mask_16x16_c = 0;
    unsigned int mask_8x8_c = 0;
    unsigned int mask_4x4_c = 0;
    unsigned int border_mask;

    // Determine the vertical edges that need filtering
1203
1204
    for (c = 0; c < cm->mib_size && mi_col + c < cm->mi_cols; c += col_step) {
      const MODE_INFO *mi = mib[c];
Jingning Han's avatar
Jingning Han committed
1205
1206
1207
1208
1209
1210
      const MB_MODE_INFO *mbmi = &mi[0].mbmi;
      const BLOCK_SIZE sb_type = mbmi->sb_type;
      const int skip_this = mbmi->skip && is_inter_block(mbmi);
      const int blk_row = r & (num_8x8_blocks_high_lookup[sb_type] - 1);
      const int blk_col = c & (num_8x8_blocks_wide_lookup[sb_type] - 1);

Jingning Han's avatar
Jingning Han committed
1211
      // left edge of current unit is block/partition edge -> no skip
clang-format's avatar
clang-format committed
1212
1213
      const int block_edge_left =
          (num_4x4_blocks_wide_lookup[sb_type] > 1) ? !blk_col : 1;
Jingning Han's avatar
Jingning Han committed
1214
1215
      const int skip_this_c = skip_this && !block_edge_left;
      // top edge of current unit is block/partition edge -> no skip
clang-format's avatar
clang-format committed
1216
1217
      const int block_edge_above =
          (num_4x4_blocks_high_lookup[sb_type] > 1) ? !blk_row : 1;
Jingning Han's avatar
Jingning Han committed
1218
      const int skip_this_r = skip_this && !block_edge_above;
Jingning Han's avatar
Jingning Han committed
1219

1220
1221
1222
1223
#if CONFIG_VAR_TX
      TX_SIZE tx_size = (plane->plane_type == PLANE_TYPE_UV)
                            ? get_uv_tx_size(mbmi, plane)
                            : mbmi->tx_size;
clang-format's avatar
clang-format committed
1224
1225
1226
1227
#else
      const TX_SIZE tx_size = (plane->plane_type == PLANE_TYPE_UV)
                                  ? get_uv_tx_size(mbmi, plane)
                                  : mbmi->tx_size;
1228
1229
#endif

Jingning Han's avatar
Jingning Han committed
1230
1231
1232
      const int skip_border_4x4_c = ss_x && mi_col + c == cm->mi_cols - 1;
      const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;

1233
1234
      TX_SIZE tx_size_c = num_4x4_blocks_wide_txsize_log2_lookup[tx_size];
      TX_SIZE tx_size_r = num_4x4_blocks_high_txsize_log2_lookup[tx_size];
1235
1236

      int tx_size_mask = 0;
Jingning Han's avatar
Jingning Han committed
1237
      // Filter level can vary per MI
clang-format's avatar
clang-format committed
1238
      if (!(lfl[r][c >> ss_x] = get_filter_level(&cm->lf_info, mbmi))) continue;
Jingning Han's avatar
Jingning Han committed
1239

1240
1241
1242
1243
1244
1245
1246
1247
1248
      if (tx_size == TX_32X32)
        tx_size_mask = 3;
      else if (tx_size == TX_16X16)
        tx_size_mask = 1;
      else
        tx_size_mask = 0;

#if CONFIG_VAR_TX
      if (is_inter_block(mbmi) && !mbmi->skip)
clang-format's avatar
clang-format committed
1249
1250
1251
1252
1253
        tx_size =
            (plane->plane_type == PLANE_TYPE_UV)
                ? get_uv_tx_size_impl(mbmi->inter_tx_size[blk_row][blk_col],
                                      sb_type, ss_x, ss_y)
                : mbmi->inter_tx_size[blk_row][blk_col];
1254

1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
#if CONFIG_EXT_TX && CONFIG_RECT_TX
      tx_size_r =
          VPXMIN(txsize_horz_map[tx_size], cm->above_txfm_context[mi_col + c]);
      tx_size_c = VPXMIN(txsize_vert_map[tx_size],
                         cm->left_txfm_context[(mi_row + r) & MAX_MIB_MASK]);

      cm->above_txfm_context[mi_col + c] = txsize_horz_map[tx_size];
      cm->left_txfm_context[(mi_row + r) & MAX_MIB_MASK] =
          txsize_vert_map[tx_size];
#else
clang-format's avatar
clang-format committed
1265
1266
1267
      tx_size_r = VPXMIN(tx_size, cm->above_txfm_context[mi_col + c]);
      tx_size_c =
          VPXMIN(tx_size, cm->left_txfm_context[(mi_row + r) & MAX_MIB_MASK]);
1268
1269

      cm->above_txfm_context[mi_col + c] = tx_size;
1270
      cm->left_txfm_context[(mi_row + r) & MAX_MIB_MASK] = tx_size;
1271
#endif
1272
1273
#endif

Jingning Han's avatar
Jingning Han committed
1274
      // Build masks based on the transform size of each block
1275
1276
1277
      // handle vertical mask
      if (tx_size_c == TX_32X32) {
        if (!skip_this_c && ((c >> ss_x) & tx_size_mask) == 0) {
Jingning Han's avatar
Jingning Han committed
1278
1279
1280
1281
1282
          if (!skip_border_4x4_c)
            mask_16x16_c |= 1 << (c >> ss_x);
          else
            mask_8x8_c |= 1 << (c >> ss_x);
        }
1283
1284
      } else if (tx_size_c == TX_16X16) {
        if (!skip_this_c && ((c >> ss_x) & tx_size_mask) == 0) {
Jingning Han's avatar
Jingning Han committed
1285
1286
1287
1288
1289
1290
1291
          if (!skip_border_4x4_c)
            mask_16x16_c |= 1 << (c >> ss_x);
          else
            mask_8x8_c |= 1 << (c >> ss_x);
        }
      } else {
        // force 8x8 filtering on 32x32 boundaries
1292
1293
        if (!skip_this_c && ((c >> ss_x) & tx_size_mask) == 0) {
          if (tx_size_c == TX_8X8 || ((c >> ss_x) & 3) == 0)
Jingning Han's avatar
Jingning Han committed
1294
1295
1296
1297
1298
            mask_8x8_c |= 1 << (c >> ss_x);
          else
            mask_4x4_c |= 1 << (c >> ss_x);
        }

1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
        if (!skip_this && tx_size_c < TX_8X8 && !skip_border_4x4_c &&
            ((c >> ss_x) & tx_size_mask) == 0)
          mask_4x4_int[r] |= 1 << (c >> ss_x);
      }

      // set horizontal mask
      if (tx_size_r == TX_32X32) {
        if (!skip_this_r && ((r >> ss_y) & tx_size_mask) == 0) {
          if (!skip_border_4x4_r)
            mask_16x16[r] |= 1 << (c >> ss_x);
          else
            mask_8x8[r] |= 1 << (c >> ss_x);
        }
      } else if (tx_size_r == TX_16X16) {
        if (!skip_this_r && ((r >> ss_y) & tx_size_mask) == 0) {
          if (!skip_border_4x4_r)
            mask_16x16[r] |= 1 << (c >> ss_x);
          else
            mask_8x8[r] |= 1 << (c >> ss_x);
        }
      } else {
        // force 8x8 filtering on 32x32 boundaries
        if (!skip_this_r && ((r >> ss_y) & tx_size_mask) == 0) {
          if (tx_size_r == TX_8X8 || ((r >> ss_y) & 3) == 0)
Jingning Han's avatar
Jingning Han committed
1323
1324
1325
1326
1327
            mask_8x8[r] |= 1 << (c >> ss_x);
          else
            mask_4x4[r] |= 1 << (c >> ss_x);
        }

1328
1329
        if (!skip_this && tx_size_r < TX_8X8 && !skip_border_4x4_c &&
            ((r >> ss_y) & tx_size_mask) == 0)
Jingning Han's avatar
Jingning Han committed
1330
1331
1332
1333
1334
1335
1336
1337
          mask_4x4_int[r] |= 1 << (c >> ss_x);
      }
    }

    // Disable filtering on the leftmost column
    border_mask = ~(mi_col == 0);
#if CONFIG_VP9_HIGHBITDEPTH
    if (cm->use_highbitdepth) {
1338
      highbd_filter_selectively_vert(
clang-format's avatar
clang-format committed
1339
1340
1341
          CONVERT_TO_SHORTPTR(dst->buf), dst->stride,
          mask_16x16_c & border_mask, mask_8x8_c & border_mask,
          mask_4x4_c & border_mask, mask_4x4_int[r], &cm->lf_info, &lfl[r][0],
1342
          (int)cm->bit_depth);
Jingning Han's avatar
Jingning Han committed
1343
    } else {
clang-format's avatar
clang-format committed
1344
      filter_selectively_vert(dst->buf, dst->stride, mask_16x16_c & border_mask,
Jingning Han's avatar
Jingning Han committed
1345
                              mask_8x8_c & border_mask,
clang-format's avatar
clang-format committed
1346
                              mask_4x4_c & border_mask, mask_4x4_int[r],
1347
                              &cm->lf_info, &lfl[r][0]);
Jingning Han's avatar
Jingning Han committed
1348
1349
    }
#else
clang-format's avatar
clang-format committed
1350
1351
1352
    filter_selectively_vert(dst->buf, dst->stride, mask_16x16_c & border_mask,
                            mask_8x8_c & border_mask, mask_4x4_c & border_mask,
                            mask_4x4_int[r], &cm->lf_info, &lfl[r][0]);
Jingning Han's avatar
Jingning Han committed
1353
#endif  // CONFIG_VP9_HIGHBITDEPTH
1354
1355
    dst->buf += MI_SIZE * dst->stride;
    mib += row_step * cm->mi_stride;
Jingning Han's avatar
Jingning Han committed
1356
1357
1358
1359
  }