av1_loopfilter.c 134 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10
11
 */

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

Yaowu Xu's avatar
Yaowu Xu committed
14
15
16
17
#include "./aom_config.h"
#include "./aom_dsp_rtcd.h"
#include "aom_dsp/aom_dsp_common.h"
#include "aom_mem/aom_mem.h"
18
#include "aom_ports/mem.h"
19
20
21
#include "av1/common/av1_loopfilter.h"
#include "av1/common/onyxc_int.h"
#include "av1/common/reconinter.h"
22
#include "av1/common/seg_common.h"
Jingning Han's avatar
Jingning Han committed
23

Cheng Chen's avatar
Cheng Chen committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
140
141
142
143
144
145
146
147
148
149
150
151
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#if CONFIG_LPF_DIRECT
static void pick_filter_pixel_left(uint8_t *const src, uint8_t *const line,
                                   int *const orig_pos, int length, int row,
                                   int col, int width, int height, int pitch,
                                   int pivot, int direct) {
  int i;
  int pos = row * pitch + col;

  for (i = 0; i < length; ++i) {
    int dy = 0;
    switch (direct) {
      case VERT_HORZ: dy = 0; break;
      case DEGREE_45: dy = 1; break;
      case DEGREE_135: dy = -1; break;
    }
    col -= 1;
    row += dy;
    if (col >= 0 && col < width && row >= 0 && row < height) {
      pos = row * pitch + col;
      line[pivot - 1 - i] = src[pos];
      orig_pos[pivot - 1 - i] = pos;
    }
  }
}

static void pick_filter_pixel_right(uint8_t *const src, uint8_t *const line,
                                    int *const orig_pos, int length, int row,
                                    int col, int width, int height, int pitch,
                                    int pivot, int direct) {
  int i;
  int pos = row * pitch + col;

  line[pivot] = src[pos];
  orig_pos[pivot] = pos;

  for (i = 1; i < length; ++i) {
    int dy = 0;
    switch (direct) {
      case VERT_HORZ: dy = 0; break;
      case DEGREE_45: dy = -1; break;
      case DEGREE_135: dy = 1; break;
    }
    col += 1;
    row += dy;
    if (col >= 0 && col < width && row >= 0 && row < height) {
      pos = row * pitch + col;
      line[pivot + i] = src[pos];
      orig_pos[pivot + i] = pos;
    }
  }
}

static void pick_filter_pixel_above(uint8_t *const src, uint8_t *const line,
                                    int *const orig_pos, int length, int row,
                                    int col, int width, int height, int pitch,
                                    int pivot, int direct) {
  int i;
  int pos = row * pitch + col;

  for (i = 0; i < length; ++i) {
    int dx = 0;
    switch (direct) {
      case VERT_HORZ: dx = 0; break;
      case DEGREE_45: dx = 1; break;
      case DEGREE_135: dx = -1; break;
    }
    col += dx;
    row -= 1;
    if (col >= 0 && col < width && row >= 0 && row < height) {
      pos = row * pitch + col;
      line[pivot - 1 - i] = src[pos];
      orig_pos[pivot - 1 - i] = pos;
    }
  }
}

static void pick_filter_pixel_bot(uint8_t *const src, uint8_t *const line,
                                  int *const orig_pos, int length, int row,
                                  int col, int width, int height, int pitch,
                                  int pivot, int direct) {
  int i;
  int pos = row * pitch + col;

  line[pivot] = src[pos];
  orig_pos[pivot] = pos;

  for (i = 1; i < length; ++i) {
    int dx = 0;
    switch (direct) {
      case VERT_HORZ: dx = 0; break;
      case DEGREE_45: dx = -1; break;
      case DEGREE_135: dx = 1; break;
    }
    col += dx;
    row += 1;
    if (col >= 0 && col < width && row >= 0 && row < height) {
      pos = row * pitch + col;
      line[pivot + i] = src[pos];
      orig_pos[pivot + i] = pos;
    }
  }
}

static void pick_filter_block_vert(uint8_t *const src, uint8_t *const block,
                                   int *const orig_pos, int length, int row,
                                   int col, int width, int height, int pitch,
                                   int pivot, int line_length, int unit,
                                   int direct) {
  int i;
  for (i = 0; i < 8 * unit; ++i) {
    pick_filter_pixel_left(src, block + i * line_length,
                           orig_pos + i * line_length, length, row + i, col,
                           width, height, pitch, pivot, direct);
    pick_filter_pixel_right(src, block + i * line_length,
                            orig_pos + i * line_length, length, row + i, col,
                            width, height, pitch, pivot, direct);
  }
}

static void pick_filter_block_horz(uint8_t *const src, uint8_t *const block,
                                   int *const orig_pos, int length, int row,
                                   int col, int width, int height, int pitch,
                                   int pivot, int line_length, int unit,
                                   int direct) {
  int i, j;
  int num = 8 * unit;
  for (i = 0; i < num; ++i) {
    pick_filter_pixel_above(src, block + i * line_length,
                            orig_pos + i * line_length, length, row, col + i,
                            width, height, pitch, pivot, direct);
    pick_filter_pixel_bot(src, block + i * line_length,
                          orig_pos + i * line_length, length, row, col + i,
                          width, height, pitch, pivot, direct);
  }

  // rearrange block
  // TODO(chengchen): make it in-place or a stand alone function
  uint8_t tmp_block[256];
  int tmp_pos[256];
  for (i = 0; i < 256; ++i) {
    tmp_block[i] = 0;
    tmp_pos[i] = -1;
  }
  for (i = 0; i < num; ++i) {
    for (j = 0; j < line_length; ++j) {
      tmp_block[j * line_length + i] = block[i * line_length + j];
      tmp_pos[j * line_length + i] = orig_pos[i * line_length + j];
    }
  }
  for (i = 0; i < 256; ++i) {
    block[i] = tmp_block[i];
    orig_pos[i] = tmp_pos[i];
  }
}

static int compute_block_grad(uint8_t *const src, int length, int row, int col,
                              int width, int height, int pitch, int unit,
                              int vert_or_horz, int direct) {
  int i, j;
  int r0, c0, pos0, r1 = 0, c1 = 0, pos1;
  int sum_grad = 0;
  for (i = 0; i < 8 * unit; ++i) {
    // vert_or_horz: 0 vertical edge, 1 horizontal edge
    r0 = vert_or_horz ? row : row + i;
    c0 = vert_or_horz ? col + i : col;
    pos0 = r0 * pitch + c0;

    for (j = 0; j < length; ++j) {
      if (vert_or_horz == 0) {
        switch (direct) {
          case VERT_HORZ: r1 = r0; break;
          case DEGREE_45: r1 = r0 + 1; break;
          case DEGREE_135: r1 = r0 - 1; break;
        }
        c1 = c0 - 1;
      } else {
        r1 = r0 - 1;
        switch (direct) {
          case VERT_HORZ: c1 = c0; break;
          case DEGREE_45: c1 = c0 + 1; break;
          case DEGREE_135: c1 = c0 - 1; break;
        }
      }
      pos1 = r1 * pitch + c1;

      if (r0 >= 0 && r0 < height && c0 >= 0 && c0 < width && r1 >= 0 &&
          r1 < height && c1 >= 0 && c1 < width) {
        sum_grad += abs(src[pos1] - src[pos0]);
      } else {
        sum_grad += 255;  // penalize unreachable boundary
      }
      r0 = r1;
      c0 = c1;
      pos0 = pos1;
    }

    r0 = vert_or_horz ? row : row + i;
    c0 = vert_or_horz ? col + i : col;
    pos0 = r0 * pitch + c0;

    for (j = 0; j < length - 1; ++j) {
      if (vert_or_horz == 0) {
        switch (direct) {
          case VERT_HORZ: r1 = r0; break;
          case DEGREE_45: r1 = r0 - 1; break;
          case DEGREE_135: r1 = r0 + 1; break;
        }
        c1 = c0 + 1;
      } else {
        r1 = r0 + 1;
        switch (direct) {
          case VERT_HORZ: c1 = c0; break;
          case DEGREE_45: c1 = c0 - 1; break;
          case DEGREE_135: c1 = c0 + 1; break;
        }
      }
      pos1 = r1 * pitch + c1;

      if (r0 >= 0 && r0 < height && c0 >= 0 && c0 < width && r1 >= 0 &&
          r1 < height && c1 >= 0 && c1 < width) {
        sum_grad += abs(src[pos1] - src[pos0]);
      } else {
        sum_grad += 255;  // penalize unreachable boundary
      }
      r0 = r1;
      c0 = c1;
      pos0 = pos1;
    }
  }

  return sum_grad;
}

static int pick_min_grad_direct(uint8_t *const src, int length, int row,
                                int col, int width, int height, int pitch,
                                int unit, int vert_or_horz) {
  int direct = VERT_HORZ;
  int min_grad = INT_MAX, sum_grad = 0;

  int degree;
  for (degree = 0; degree < FILTER_DEGREES; ++degree) {
    // compute abs gradient along each line for the filter block
    sum_grad = compute_block_grad(src, length, row, col, width, height, pitch,
                                  unit, vert_or_horz, degree);
    if (sum_grad < min_grad) {
      min_grad = sum_grad;
      direct = degree;
    }
  }

  return direct;
}
#endif  // CONFIG_LPF_DIRECT

278
#define PARALLEL_DEBLOCKING_15TAPLUMAONLY 1
279
#define PARALLEL_DEBLOCKING_DISABLE_15TAP 0
280

Jingning Han's avatar
Jingning Han committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// 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
298
static const uint64_t left_64x64_txform_mask[TX_SIZES] = {
299
#if CONFIG_CHROMA_2X2
300
301
  0xffffffffffffffffULL,  // TX_2X2
#endif
Jingning Han's avatar
Jingning Han committed
302
303
304
305
  0xffffffffffffffffULL,  // TX_4X4
  0xffffffffffffffffULL,  // TX_8x8
  0x5555555555555555ULL,  // TX_16x16
  0x1111111111111111ULL,  // TX_32x32
306
307
308
#if CONFIG_TX64X64
  0x0101010101010101ULL,  // TX_64x64
#endif                    // CONFIG_TX64X64
Jingning Han's avatar
Jingning Han committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
};

// 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
328
static const uint64_t above_64x64_txform_mask[TX_SIZES] = {
329
#if CONFIG_CHROMA_2X2
330
331
  0xffffffffffffffffULL,  // TX_4X4
#endif
Jingning Han's avatar
Jingning Han committed
332
333
334
335
  0xffffffffffffffffULL,  // TX_4X4
  0xffffffffffffffffULL,  // TX_8x8
  0x00ff00ff00ff00ffULL,  // TX_16x16
  0x000000ff000000ffULL,  // TX_32x32
336
337
338
#if CONFIG_TX64X64
  0x00000000000000ffULL,  // TX_64x64
#endif                    // CONFIG_TX64X64
Jingning Han's avatar
Jingning Han committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
};

// 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
356
static const uint64_t left_prediction_mask[BLOCK_SIZES_ALL] = {
357
#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Han's avatar
Jingning Han committed
358
359
360
361
  0x0000000000000001ULL,  // BLOCK_2X2,
  0x0000000000000001ULL,  // BLOCK_2X4,
  0x0000000000000001ULL,  // BLOCK_4X2,
#endif
Jingning Han's avatar
Jingning Han committed
362
363
364
365
366
367
368
369
370
371
372
373
  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,
374
375
376
377
  0x0101010101010101ULL,  // BLOCK_64X64,
  0x0000000000000101ULL,  // BLOCK_4X16,
  0x0000000000000001ULL,  // BLOCK_16X4,
  0x0000000001010101ULL,  // BLOCK_8X32,
378
379
380
  0x0000000000000001ULL,  // BLOCK_32X8,
  0x0101010101010101ULL,  // BLOCK_16X64,
  0x0000000000000101ULL,  // BLOCK_64X16
Jingning Han's avatar
Jingning Han committed
381
382
383
};

// 64 bit mask to shift and set for each prediction size.
384
static const uint64_t above_prediction_mask[BLOCK_SIZES_ALL] = {
385
#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Han's avatar
Jingning Han committed
386
387
388
389
  0x0000000000000001ULL,  // BLOCK_2X2
  0x0000000000000001ULL,  // BLOCK_2X4
  0x0000000000000001ULL,  // BLOCK_4X2
#endif
Jingning Han's avatar
Jingning Han committed
390
391
392
393
394
395
396
397
398
399
400
401
  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,
402
403
404
405
  0x00000000000000ffULL,  // BLOCK_64X64,
  0x0000000000000001ULL,  // BLOCK_4X16,
  0x0000000000000003ULL,  // BLOCK_16X4,
  0x0000000000000001ULL,  // BLOCK_8X32,
406
407
408
  0x000000000000000fULL,  // BLOCK_32X8,
  0x0000000000000003ULL,  // BLOCK_16X64,
  0x00000000000000ffULL,  // BLOCK_64X16
Jingning Han's avatar
Jingning Han committed
409
410
};
// 64 bit mask to shift and set for each prediction size. A bit is set for
411
// each 8x8 block that would be in the top left most block of the given block
Jingning Han's avatar
Jingning Han committed
412
// size in the 64x64 block.
413
static const uint64_t size_mask[BLOCK_SIZES_ALL] = {
414
#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Han's avatar
Jingning Han committed
415
416
417
418
  0x0000000000000001ULL,  // BLOCK_2X2
  0x0000000000000001ULL,  // BLOCK_2X4
  0x0000000000000001ULL,  // BLOCK_4X2
#endif
Jingning Han's avatar
Jingning Han committed
419
420
421
422
423
424
425
426
427
428
429
430
  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,
431
432
433
434
  0xffffffffffffffffULL,  // BLOCK_64X64,
  0x0000000000000101ULL,  // BLOCK_4X16,
  0x0000000000000003ULL,  // BLOCK_16X4,
  0x0000000001010101ULL,  // BLOCK_8X32,
435
436
437
  0x000000000000000fULL,  // BLOCK_32X8,
  0x0303030303030303ULL,  // BLOCK_16X64,
  0x000000000000ffffULL,  // BLOCK_64X16
Jingning Han's avatar
Jingning Han committed
438
439
};

440
// These are used for masking the left and above 32x32 borders.
clang-format's avatar
clang-format committed
441
static const uint64_t left_border = 0x1111111111111111ULL;
Jingning Han's avatar
Jingning Han committed
442
443
444
static const uint64_t above_border = 0x000000ff000000ffULL;

// 16 bit masks for uv transform sizes.
clang-format's avatar
clang-format committed
445
static const uint16_t left_64x64_txform_mask_uv[TX_SIZES] = {
446
#if CONFIG_CHROMA_2X2
447
448
  0xffff,  // TX_2X2
#endif
Jingning Han's avatar
Jingning Han committed
449
450
451
452
  0xffff,  // TX_4X4
  0xffff,  // TX_8x8
  0x5555,  // TX_16x16
  0x1111,  // TX_32x32
453
454
455
#if CONFIG_TX64X64
  0x0101,  // TX_64x64, never used
#endif     // CONFIG_TX64X64
Jingning Han's avatar
Jingning Han committed
456
457
};

clang-format's avatar
clang-format committed
458
static const uint16_t above_64x64_txform_mask_uv[TX_SIZES] = {
459
#if CONFIG_CHROMA_2X2
460
461
  0xffff,  // TX_2X2
#endif
Jingning Han's avatar
Jingning Han committed
462
463
464
465
  0xffff,  // TX_4X4
  0xffff,  // TX_8x8
  0x0f0f,  // TX_16x16
  0x000f,  // TX_32x32
466
467
468
#if CONFIG_TX64X64
  0x0003,  // TX_64x64, never used
#endif     // CONFIG_TX64X64
Jingning Han's avatar
Jingning Han committed
469
470
471
};

// 16 bit left mask to shift and set for each uv prediction size.
472
static const uint16_t left_prediction_mask_uv[BLOCK_SIZES_ALL] = {
473
#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Han's avatar
Jingning Han committed
474
475
476
477
  0x0001,  // BLOCK_2X2,
  0x0001,  // BLOCK_2X4,
  0x0001,  // BLOCK_4X2,
#endif
Jingning Han's avatar
Jingning Han committed
478
479
480
481
482
483
484
485
486
487
488
489
  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,
490
491
492
493
  0x1111,  // BLOCK_64X64,
  0x0001,  // BLOCK_4X16,
  0x0001,  // BLOCK_16X4,
  0x0011,  // BLOCK_8X32,
494
495
496
  0x0001,  // BLOCK_32X8,
  0x1111,  // BLOCK_16X64,
  0x0001,  // BLOCK_64X16,
Jingning Han's avatar
Jingning Han committed
497
};
498

Jingning Han's avatar
Jingning Han committed
499
// 16 bit above mask to shift and set for uv each prediction size.
500
static const uint16_t above_prediction_mask_uv[BLOCK_SIZES_ALL] = {
501
#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Han's avatar
Jingning Han committed
502
503
504
505
  0x0001,  // BLOCK_2X2
  0x0001,  // BLOCK_2X4
  0x0001,  // BLOCK_4X2
#endif
Jingning Han's avatar
Jingning Han committed
506
507
508
509
510
511
512
513
514
515
516
517
  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,
518
519
520
521
  0x000f,  // BLOCK_64X64,
  0x0001,  // BLOCK_4X16,
  0x0001,  // BLOCK_16X4,
  0x0001,  // BLOCK_8X32,
522
523
524
  0x0003,  // BLOCK_32X8,
  0x0001,  // BLOCK_16X64,
  0x000f,  // BLOCK_64X16
Jingning Han's avatar
Jingning Han committed
525
526
527
};

// 64 bit mask to shift and set for each uv prediction size
528
static const uint16_t size_mask_uv[BLOCK_SIZES_ALL] = {
529
#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Han's avatar
Jingning Han committed
530
531
532
533
  0x0001,  // BLOCK_2X2
  0x0001,  // BLOCK_2X4
  0x0001,  // BLOCK_4X2
#endif
Jingning Han's avatar
Jingning Han committed
534
535
536
537
538
539
540
541
542
543
544
545
  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,
546
547
548
549
  0xffff,  // BLOCK_64X64,
  0x0001,  // BLOCK_4X16,
  0x0001,  // BLOCK_16X4,
  0x0011,  // BLOCK_8X32,
550
551
552
  0x0003,  // BLOCK_32X8,
  0x1111,  // BLOCK_16X64,
  0x000f,  // BLOCK_64X16
Jingning Han's avatar
Jingning Han committed
553
};
clang-format's avatar
clang-format committed
554
static const uint16_t left_border_uv = 0x1111;
Jingning Han's avatar
Jingning Han committed
555
556
static const uint16_t above_border_uv = 0x000f;

557
static const int mode_lf_lut[] = {
Jingning Han's avatar
Jingning Han committed
558
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // INTRA_MODES
559
  0,
Urvang Joshi's avatar
Urvang Joshi committed
560
561
562
#if CONFIG_SMOOTH_HV
  0, 0,
#endif         // CONFIG_SMOOTH_HV
563
  1, 1, 0, 1,  // INTER_MODES (ZEROMV == 0)
Yue Chen's avatar
Yue Chen committed
564
#if CONFIG_EXT_INTER
565
566
567
568
569
#if CONFIG_COMPOUND_SINGLEREF
  // 1, 1, 1, 1, 1,       // INTER_SINGLEREF_COMP_MODES
  // NOTE(zoeliu): Remove SR_NEAREST_NEWMV
  1, 1, 1, 1,             // INTER_SINGLEREF_COMP_MODES
#endif                    // CONFIG_COMPOUND_SINGLEREF
570
571
  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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
};

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
587
    if (block_inside_limit < 1) block_inside_limit = 1;
Jingning Han's avatar
Jingning Han committed
588
589
590
591
592
593

    memset(lfi->lfthr[lvl].lim, block_inside_limit, SIMD_WIDTH);
    memset(lfi->lfthr[lvl].mblim, (2 * (lvl + 2) + block_inside_limit),
           SIMD_WIDTH);
  }
}
Fangwen Fu's avatar
Fangwen Fu committed
594
595
596
#if CONFIG_EXT_DELTA_Q
static uint8_t get_filter_level(const AV1_COMMON *cm,
                                const loop_filter_info_n *lfi_n,
Cheng Chen's avatar
Cheng Chen committed
597
#if CONFIG_LOOPFILTER_LEVEL
598
                                const int dir_idx,
Cheng Chen's avatar
Cheng Chen committed
599
600
601
#endif
#if CONFIG_LPF_SB
                                int mi_row, int mi_col,
602
#endif
Fangwen Fu's avatar
Fangwen Fu committed
603
                                const MB_MODE_INFO *mbmi) {
604
#if CONFIG_LPF_SB
Cheng Chen's avatar
Cheng Chen committed
605
  return cm->mi[mi_row * cm->mi_stride + mi_col].mbmi.filt_lvl;
606
607
#endif

Fangwen Fu's avatar
Fangwen Fu committed
608
609
610
611
612
613
614
615
616
617
#if CONFIG_SUPERTX
  const int segment_id = AOMMIN(mbmi->segment_id, mbmi->segment_id_supertx);
  assert(
      IMPLIES(supertx_enabled(mbmi), mbmi->segment_id_supertx != MAX_SEGMENTS));
  assert(IMPLIES(supertx_enabled(mbmi),
                 mbmi->segment_id_supertx <= mbmi->segment_id));
#else
  const int segment_id = mbmi->segment_id;
#endif  // CONFIG_SUPERTX
  if (cm->delta_lf_present_flag) {
Cheng Chen's avatar
Cheng Chen committed
618
#if CONFIG_LOOPFILTER_LEVEL
619
620
621
622
    int lvl_seg =
        clamp(mbmi->current_delta_lf_from_base + cm->lf.filter_level[dir_idx],
              0, MAX_LOOP_FILTER);
#else
Fangwen Fu's avatar
Fangwen Fu committed
623
624
    int lvl_seg = clamp(mbmi->current_delta_lf_from_base + cm->lf.filter_level,
                        0, MAX_LOOP_FILTER);
625
#endif
Fangwen Fu's avatar
Fangwen Fu committed
626
627
628
629
630
631
632
    const int scale = 1 << (lvl_seg >> 5);
    if (segfeature_active(&cm->seg, segment_id, SEG_LVL_ALT_LF)) {
      const int data = get_segdata(&cm->seg, segment_id, SEG_LVL_ALT_LF);
      lvl_seg =
          clamp(cm->seg.abs_delta == SEGMENT_ABSDATA ? data : lvl_seg + data, 0,
                MAX_LOOP_FILTER);
    }
Jingning Han's avatar
Jingning Han committed
633

Fangwen Fu's avatar
Fangwen Fu committed
634
635
636
637
638
639
640
641
    if (cm->lf.mode_ref_delta_enabled) {
      lvl_seg += cm->lf.ref_deltas[mbmi->ref_frame[0]] * scale;
      if (mbmi->ref_frame[0] > INTRA_FRAME)
        lvl_seg += cm->lf.mode_deltas[mode_lf_lut[mbmi->mode]] * scale;
      lvl_seg = clamp(lvl_seg, 0, MAX_LOOP_FILTER);
    }
    return lvl_seg;
  } else {
Cheng Chen's avatar
Cheng Chen committed
642
#if CONFIG_LOOPFILTER_LEVEL
643
644
645
    return lfi_n
        ->lvl[segment_id][dir_idx][mbmi->ref_frame[0]][mode_lf_lut[mbmi->mode]];
#else
Fangwen Fu's avatar
Fangwen Fu committed
646
    return lfi_n->lvl[segment_id][mbmi->ref_frame[0]][mode_lf_lut[mbmi->mode]];
647
#endif
Fangwen Fu's avatar
Fangwen Fu committed
648
649
650
  }
}
#else
Jingning Han's avatar
Jingning Han committed
651
652
static uint8_t get_filter_level(const loop_filter_info_n *lfi_n,
                                const MB_MODE_INFO *mbmi) {
653
#if CONFIG_SUPERTX
Yaowu Xu's avatar
Yaowu Xu committed
654
  const int segment_id = AOMMIN(mbmi->segment_id, mbmi->segment_id_supertx);
clang-format's avatar
clang-format committed
655
656
  assert(
      IMPLIES(supertx_enabled(mbmi), mbmi->segment_id_supertx != MAX_SEGMENTS));
657
658
659
660
661
662
  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
663
}
Fangwen Fu's avatar
Fangwen Fu committed
664
#endif
Jingning Han's avatar
Jingning Han committed
665

Yaowu Xu's avatar
Yaowu Xu committed
666
void av1_loop_filter_init(AV1_COMMON *cm) {
667
  assert(MB_MODE_COUNT == NELEMENTS(mode_lf_lut));
Jingning Han's avatar
Jingning Han committed
668
669
670
671
672
673
674
675
676
677
678
679
680
  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);
}

681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#if CONFIG_LPF_SB
void av1_loop_filter_sb_level_init(AV1_COMMON *cm, int mi_row, int mi_col,
                                   int lvl) {
  const int mi_row_start = AOMMAX(0, mi_row - FILT_BOUNDARY_MI_OFFSET);
  const int mi_col_start = AOMMAX(0, mi_col - FILT_BOUNDARY_MI_OFFSET);
  const int mi_row_range = mi_row - FILT_BOUNDARY_MI_OFFSET + MAX_MIB_SIZE;
  const int mi_col_range = mi_col - FILT_BOUNDARY_MI_OFFSET + MAX_MIB_SIZE;
  const int mi_row_end = AOMMIN(mi_row_range, cm->mi_rows);
  const int mi_col_end = AOMMIN(mi_col_range, cm->mi_cols);

  int row, col;
  for (row = mi_row_start; row < mi_row_end; ++row) {
    for (col = mi_col_start; col < mi_col_end; ++col) {
      // Note: can't use cm->mi_grid_visible. Because for each partition,
      // all visible pointers will point to the first of the partition.
      cm->mi[row * cm->mi_stride + col].mbmi.filt_lvl = lvl;
    }
  }
}
#endif  // CONFIG_LPF_SB

702
703
void av1_loop_filter_frame_init(AV1_COMMON *cm, int default_filt_lvl,
                                int default_filt_lvl_r) {
Jingning Han's avatar
Jingning Han committed
704
705
706
707
  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
708
  int scale = 1 << (default_filt_lvl >> 5);
Jingning Han's avatar
Jingning Han committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
  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
723
724
725
      lvl_seg = clamp(
          seg->abs_delta == SEGMENT_ABSDATA ? data : default_filt_lvl + data, 0,
          MAX_LOOP_FILTER);
Jingning Han's avatar
Jingning Han committed
726
727
728
729
730
731
732
733
    }

    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;
Cheng Chen's avatar
Cheng Chen committed
734
#if CONFIG_LOOPFILTER_LEVEL
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
      for (int dir = 0; dir < 2; ++dir) {
        lvl_seg = (dir == 0) ? default_filt_lvl : default_filt_lvl_r;
        scale = 1 << (lvl_seg >> 5);

        const int intra_lvl = lvl_seg + lf->ref_deltas[INTRA_FRAME] * scale;
        lfi->lvl[seg_id][dir][INTRA_FRAME][0] =
            clamp(intra_lvl, 0, MAX_LOOP_FILTER);

        for (ref = LAST_FRAME; ref < TOTAL_REFS_PER_FRAME; ++ref) {
          for (mode = 0; mode < MAX_MODE_LF_DELTAS; ++mode) {
            const int inter_lvl = lvl_seg + lf->ref_deltas[ref] * scale +
                                  lf->mode_deltas[mode] * scale;
            lfi->lvl[seg_id][dir][ref][mode] =
                clamp(inter_lvl, 0, MAX_LOOP_FILTER);
          }
        }
      }
#else
      (void)default_filt_lvl_r;
Jingning Han's avatar
Jingning Han committed
754
755
756
      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);

757
      for (ref = LAST_FRAME; ref < TOTAL_REFS_PER_FRAME; ++ref) {
Jingning Han's avatar
Jingning Han committed
758
        for (mode = 0; mode < MAX_MODE_LF_DELTAS; ++mode) {
clang-format's avatar
clang-format committed
759
760
          const int inter_lvl = lvl_seg + lf->ref_deltas[ref] * scale +
                                lf->mode_deltas[mode] * scale;
Jingning Han's avatar
Jingning Han committed
761
762
763
          lfi->lvl[seg_id][ref][mode] = clamp(inter_lvl, 0, MAX_LOOP_FILTER);
        }
      }
764
#endif
Jingning Han's avatar
Jingning Han committed
765
766
767
768
    }
  }
}

clang-format's avatar
clang-format committed
769
770
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
                                         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) {
Yaowu Xu's avatar
Yaowu Xu committed
799
          aom_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
Jingning Han's avatar
Jingning Han committed
800
801
                                   lfi0->hev_thr);
        } else if (mask_16x16_0 & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
802
          aom_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr);
Jingning Han's avatar
Jingning Han committed
803
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
804
          aom_lpf_vertical_16(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
clang-format's avatar
clang-format committed
805
                              lfi1->hev_thr);
Jingning Han's avatar
Jingning Han committed
806
807
808
809
810
        }
      }

      if ((mask_8x8_0 | mask_8x8_1) & 1) {
        if ((mask_8x8_0 & mask_8x8_1) & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
811
          aom_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
Jingning Han's avatar
Jingning Han committed
812
813
814
                                  lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                  lfi1->hev_thr);
        } else if (mask_8x8_0 & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
815
          aom_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr);
Jingning Han's avatar
Jingning Han committed
816
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
817
          aom_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
818
                             lfi1->hev_thr);
Jingning Han's avatar
Jingning Han committed
819
820
821
822
823
        }
      }

      if ((mask_4x4_0 | mask_4x4_1) & 1) {
        if ((mask_4x4_0 & mask_4x4_1) & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
824
          aom_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
Jingning Han's avatar
Jingning Han committed
825
826
827
                                  lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                  lfi1->hev_thr);
        } else if (mask_4x4_0 & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
828
          aom_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr);
Jingning Han's avatar
Jingning Han committed
829
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
830
          aom_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
831
                             lfi1->hev_thr);
Jingning Han's avatar
Jingning Han committed
832
833
834
835
836
        }
      }

      if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
        if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
837
          aom_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
Jingning Han's avatar
Jingning Han committed
838
839
840
                                  lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                  lfi1->hev_thr);
        } else if (mask_4x4_int_0 & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
841
          aom_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
842
                             lfi0->hev_thr);
Jingning Han's avatar
Jingning Han committed
843
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
844
          aom_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim, lfi1->lim,
845
                             lfi1->hev_thr);
Jingning Han's avatar
Jingning Han committed
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
        }
      }
    }

    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;
  }
}

863
#if CONFIG_HIGHBITDEPTH
clang-format's avatar
clang-format committed
864
865
866
867
868
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
  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
884
              mask_16x16_1 | mask_8x8_1 | mask_4x4_1 | mask_4x4_int_1;
Jingning Han's avatar
Jingning Han committed
885
886
887
888
889
890
891
       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) {
Yaowu Xu's avatar
Yaowu Xu committed
892
          aom_highbd_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
Jingning Han's avatar
Jingning Han committed
893
894
                                          lfi0->hev_thr, bd);
        } else if (mask_16x16_0 & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
895
          aom_highbd_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim,
Jingning Han's avatar
Jingning Han committed
896
897
                                     lfi0->hev_thr, bd);
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
898
          aom_highbd_lpf_vertical_16(s + 8 * pitch, pitch, lfi1->mblim,
Jingning Han's avatar
Jingning Han committed
899
900
901
902
903
904
                                     lfi1->lim, lfi1->hev_thr, bd);
        }
      }

      if ((mask_8x8_0 | mask_8x8_1) & 1) {
        if ((mask_8x8_0 & mask_8x8_1) & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
905
          aom_highbd_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
Jingning Han's avatar
Jingning Han committed
906
907
908
                                         lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                         lfi1->hev_thr, bd);
        } else if (mask_8x8_0 & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
909
          aom_highbd_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim,
910
                                    lfi0->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
911
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
912
          aom_highbd_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim,
913
                                    lfi1->lim, lfi1->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
914
915
916
917
918
        }
      }

      if ((mask_4x4_0 | mask_4x4_1) & 1) {
        if ((mask_4x4_0 & mask_4x4_1) & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
919
          aom_highbd_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
Jingning Han's avatar
Jingning Han committed
920
921
922
                                         lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                         lfi1->hev_thr, bd);
        } else if (mask_4x4_0 & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
923
          aom_highbd_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim,
924
                                    lfi0->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
925
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
926
          aom_highbd_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim,
927
                                    lfi1->lim, lfi1->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
928
929
930
931
932
        }
      }

      if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
        if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
933
          aom_highbd_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
Jingning Han's avatar
Jingning Han committed
934
935
936
                                         lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                         lfi1->hev_thr, bd);
        } else if (mask_4x4_int_0 & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
937
          aom_highbd_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
938
                                    lfi0->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
939
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
940
          aom_highbd_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim,
941
                                    lfi1->lim, lfi1->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
        }
      }
    }

    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;
  }
}
958
#endif  // CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
959

clang-format's avatar
clang-format committed
960
961
962
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,
Cheng Chen's avatar
Cheng Chen committed
963
964
965
966
967
968
969
    const loop_filter_info_n *lfi_n, const uint8_t *lfl
#if CONFIG_LPF_DIRECT
    ,
    uint8_t *const src, int mi_row, int mi_col, int idx_r, int col_step,
    int width, int height, int ss_x, int ss_y
#endif
    ) {
Jingning Han's avatar
Jingning Han committed
970
971
  unsigned int mask;
  int count;
Cheng Chen's avatar
Cheng Chen committed
972
#if CONFIG_LPF_DIRECT
973
974
975
  // scale for u, v plane
  width >>= ss_x;
  height >>= ss_y;
Cheng Chen's avatar
Cheng Chen committed
976
977
  int idx_c = 0;
#endif
Jingning Han's avatar
Jingning Han committed
978

clang-format's avatar
clang-format committed
979
980
  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int; mask;
       mask >>= count) {
Jingning Han's avatar
Jingning Han committed
981
982
983
984
    const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;

    count = 1;
    if (mask & 1) {
Cheng Chen's avatar
Cheng Chen committed
985
986
987
988
989
990
991
992
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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
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
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
#if CONFIG_LPF_DIRECT
      int i;
      const int line_length = 16;
      const int pivot = 8;
      const int above_filt_len = mask_16x16 & 1 ? 8 : 4;
      const int bot_filt_len = mask_16x16 & 1 ? 8 : 4;
      uint8_t block[256];  // line_length * size_of(BLOCK_8X8) * two_blocks
      int orig_pos[256];
      int direct;

      assert(above_filt_len == bot_filt_len);
      (void)bot_filt_len;
      for (i = 0; i < 256; ++i) {
        block[i] = 0;
        orig_pos[i] = -1;
      }

      // actual position for current pixel
      const int row = (mi_row + idx_r) * MI_SIZE >> ss_y;
      const int col = (mi_col + idx_c) * MI_SIZE >> ss_x;

      // Next block's thresholds.
      const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

      if (mask_16x16 & 1) {
        if ((mask_16x16 & 3) == 3) {
          // Could use asymmetric length in the future
          direct = pick_min_grad_direct(src, above_filt_len, row, col, width,
                                        height, pitch, 2, 1);

          pick_filter_block_horz(src, block, orig_pos, above_filt_len, row, col,
                                 width, height, pitch, pivot, line_length, 2,
                                 direct);

          aom_lpf_horizontal_edge_16(block + pivot * line_length, line_length,
                                     lfi->mblim, lfi->lim, lfi->hev_thr);
          count = 2;
        } else {
          direct = pick_min_grad_direct(src, above_filt_len, row, col, width,
                                        height, pitch, 1, 1);

          pick_filter_block_horz(src, block, orig_pos, above_filt_len, row, col,
                                 width, height, pitch, pivot, line_length, 1,
                                 direct);

          aom_lpf_horizontal_edge_8(block + pivot * line_length, line_length,
                                    lfi->mblim, lfi->lim, lfi->hev_thr);
        }

        for (i = 0; i < 256; ++i)
          if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];
      } else if (mask_8x8 & 1) {
        if ((mask_8x8 & 3) == 3) {
          count = 2;
          direct = pick_min_grad_direct(src, above_filt_len, row, col, width,
                                        height, pitch, 2, 1);

          pick_filter_block_horz(src, block, orig_pos, above_filt_len, row, col,
                                 width, height, pitch, pivot, line_length, 2,
                                 direct);

          aom_lpf_horizontal_8_dual(block + pivot * line_length, line_length,
                                    lfi->mblim, lfi->lim, lfi->hev_thr,
                                    lfin->mblim, lfin->lim, lfin->hev_thr);

          for (i = 0; i < 256; ++i)
            if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];

          if ((mask_4x4_int & 3) == 3) {
            for (i = 0; i < 256; ++i) {
              block[i] = 0;
              orig_pos[i] = -1;
            }

            direct = pick_min_grad_direct(src, 4, row, col, width, height,
                                          pitch, 2, 1);

            pick_filter_block_horz(src, block, orig_pos, 4, row + 4, col, width,
                                   height, pitch, pivot, line_length, 2,
                                   direct);

            aom_lpf_horizontal_4_dual(block + pivot * line_length, line_length,
                                      lfi->mblim, lfi->lim, lfi->hev_thr,
                                      lfin->mblim, lfin->lim, lfin->hev_thr);

            for (i = 0; i < 256; ++i)
              if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];
          } else {
            for (i = 0; i < 256; ++i) {
              block[i] = 0;
              orig_pos[i] = -1;
            }

            if (mask_4x4_int & 1) {
              direct = pick_min_grad_direct(src, 4, row, col, width, height,
                                            pitch, 1, 1);

              pick_filter_block_horz(src, block, orig_pos, 4, row + 4, col,
                                     width, height, pitch, pivot, line_length,
                                     1, direct);

              aom_lpf_horizontal_4(block + pivot * line_length, line_length,
                                   lfi->mblim, lfi->lim, lfi->hev_thr);
            } else if (mask_4x4_int & 2) {
              direct = pick_min_grad_direct(src, 4, row, col, width, height,
                                            pitch, 1, 1);

              pick_filter_block_horz(src, block, orig_pos, 4, row + 4, col + 8,
                                     width, height, pitch, pivot, line_length,
                                     1, direct);

              aom_lpf_horizontal_4(block + pivot * line_length, line_length,
                                   lfin->mblim, lfin->lim, lfin->hev_thr);
            }

            for (i = 0; i < 256; ++i)
              if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];
          }
        } else {
          direct = pick_min_grad_direct(src, above_filt_len, row, col, width,
                                        height, pitch, 1, 1);

          pick_filter_block_horz(src, block, orig_pos, above_filt_len, row, col,
                                 width, height, pitch, pivot, line_length, 1,
                                 direct);

          aom_lpf_horizontal_8(block + pivot * line_length, line_length,
                               lfi->mblim, lfi->lim, lfi->hev_thr);

          for (i = 0; i < 256; ++i)
            if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];

          if (mask_4x4_int & 1) {
            for (i = 0; i < 256; ++i) {
              block[i] = 0;
              orig_pos[i] = -1;
            }
            direct = pick_min_grad_direct(src, 4, row, col, width, height,
                                          pitch, 1, 1);

            pick_filter_block_horz(src, block, orig_pos, 4, row + 4, col, width,
                                   height, pitch, pivot, line_length, 1,
                                   direct);

            aom_lpf_horizontal_4(block + pivot * line_length, line_length,
                                 lfi->mblim, lfi->lim, lfi->hev_thr);

            for (i = 0; i < 256; ++i)
              if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];
          }
        }
      } else if (mask_4x4 & 1) {
        if ((mask_4x4 & 3) == 3) {
          count = 2;
          direct = pick_min_grad_direct(src, 4, row, col, width, height, pitch,
                                        2, 1);

          pick_filter_block_horz(src, block, orig_pos, 4, row, col, width,
                                 height, pitch, pivot, line_length, 2, direct);

          aom_lpf_horizontal_4_dual(block + pivot * line_length, line_length,
                                    lfi->mblim, lfi->lim, lfi->hev_thr,
                                    lfin->mblim, lfin->lim, lfin->hev_thr);

          for (i = 0; i < 256; ++i)
            if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];

          if ((mask_4x4_int & 3) == 3) {
            for (i = 0; i < 256; ++i) {
              block[i] = 0;
              orig_pos[i] = -1;
            }

            direct = pick_min_grad_direct(src, 4, row, col, width, height,
                                          pitch, 2, 1);

            pick_filter_block_horz(src, block, orig_pos, 4, row + 4, col, width,
                                   height, pitch, pivot, line_length, 2,
                                   direct);

            aom_lpf_horizontal_4_dual(block + pivot * line_length, line_length,
                                      lfi->mblim, lfi->lim, lfi->hev_thr,
                                      lfin->mblim, lfin->lim, lfin->hev_thr);

            for (i = 0; i < 256; ++i)
              if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];
          } else {
            for (i = 0; i < 256; ++i) {
              block[i] = 0;
              orig_pos[i] = -1;
            }

            if (mask_4x4_int & 1) {
              direct = pick_min_grad_direct(src, 4, row, col, width, height,
                                            pitch, 1, 1);

              pick_filter_block_horz(src, block, orig_pos, 4, row + 4, col,
                                     width, height, pitch, pivot, line_length,
                                     1, direct);

              aom_lpf_horizontal_4(block + pivot * line_length, line_length,
                                   lfi->mblim, lfi->lim, lfi->hev_thr);
            } else if (mask_4x4_int & 2) {
              direct = pick_min_grad_direct(src, 4, row, col, width, height,
                                            pitch, 1, 1);

              pick_filter_block_horz(src, block, orig_pos, 4, row + 4, col + 8,
                                     width, height, pitch, pivot, line_length,
                                     1, direct);

              aom_lpf_horizontal_4(block + pivot * line_length, line_length,
                                   lfin->mblim, lfin->lim, lfin->hev_thr);
            }

            for (i = 0; i < 256; ++i)
              if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];
          }
        } else {
          direct = pick_min_grad_direct(src, above_filt_len, row, col, width,
                                        height, pitch, 1, 1);

          pick_filter_block_horz(src, block, orig_pos, above_filt_len, row, col,
                                 width, height, pitch, pivot, line_length, 1,
                                 direct);

          aom_lpf_horizontal_4(block + pivot * line_length, line_length,
                               lfi->mblim, lfi->lim, lfi->hev_thr);

          for (i = 0; i < 256; ++i)
            if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];

          if (mask_4x4_int & 1) {
            for (i = 0; i < 256; ++i) {
              block[i] = 0;
              orig_pos[i] = -1;
            }
            direct = pick_min_grad_direct(src, above_filt_len, row, col, width,
                                          height, pitch, 1, 1);

            pick_filter_block_horz(src, block, orig_pos, 4, row + 4, col, width,
                                   height, pitch, pivot, line_length, 1,
                                   direct);

            aom_lpf_horizontal_4(block + pivot * line_length, line_length,
                                 lfi->mblim, lfi->lim, lfi->hev_thr);

            for (i = 0; i < 256; ++i)
              if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];
          }
        }
      } else if (mask_4x4_int & 1) {
        direct =
            pick_min_grad_direct(src, 4, row, col, width, height, pitch, 1, 1);

        pick_filter_block_horz(src, block, orig_pos, 4, row + 4, col, width,
                               height, pitch, pivot, line_length, 1, direct);

        aom_lpf_horizontal_4(block + pivot * line_length, line_length,
                             lfi->mblim, lfi->lim, lfi->hev_thr);

        for (i = 0; i < 256; ++i)
          if (orig_pos[i] >= 0) src[orig_pos[i]] = block[i];
      }
#else   // CONFIG_LPF_DIRECT
Jingning Han's avatar
Jingning Han committed
1249
1250
      if (mask_16x16 & 1) {
        if ((mask_16x16 & 3) == 3) {
Yaowu Xu's avatar
Yaowu Xu committed
1251
          aom_lpf_horizontal_edge_16(s, pitch, lfi->mblim, lfi->lim,
1252
                                     lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1253
1254
          count = 2;
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
1255
          aom_lpf_horizontal_edge_8(s, pitch, lfi->mblim, lfi->lim,
1256
                                    lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1257
1258
1259
1260
1261
1262
        }
      } else if (mask_8x8 & 1) {
        if ((mask_8x8 & 3) == 3) {
          // Next block's thresholds.
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

Yaowu Xu's avatar
Yaowu Xu committed
1263
          aom_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
Jingning Han's avatar
Jingning Han committed
1264
1265
1266
1267
                                    lfi->hev_thr, lfin->mblim, lfin->lim,
                                    lfin->hev_thr);

          if ((mask_4x4_int & 3) == 3) {
Yaowu Xu's avatar
Yaowu Xu committed
1268
            aom_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
Jingning Han's avatar
Jingning Han committed
1269
1270
1271
1272
                                      lfi->lim, lfi->hev_thr, lfin->mblim,
                                      lfin->lim, lfin->hev_thr);
          } else {
            if (mask_4x4_int & 1)
Yaowu Xu's avatar
Yaowu Xu committed
1273
              aom_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
1274
                                   lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1275
            else if (mask_4x4_int & 2)
Yaowu Xu's avatar
Yaowu Xu committed
1276
              aom_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
1277
                                   lfin->lim, lfin->hev_thr);
Jingning Han's avatar
Jingning Han committed
1278
1279
1280
          }
          count = 2;
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
1281
          aom_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1282
1283

          if (mask_4x4_int & 1)
Yaowu Xu's avatar
Yaowu Xu committed
1284
            aom_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
1285
                                 lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1286
1287
1288
1289
1290
1291
        }
      } else if (mask_4x4 & 1) {
        if ((mask_4x4 & 3) == 3) {
          // Next block's thresholds.
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

Yaowu Xu's avatar
Yaowu Xu committed
1292
          aom_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
Jingning Han's avatar
Jingning Han committed
1293
1294
                                    lfi->hev_thr, lfin->mblim, lfin->lim,
                                    lfin->hev_thr);
Cheng Chen's avatar
Cheng Chen committed
1295

Jingning Han's avatar
Jingning Han committed
1296
          if ((mask_4x4_int & 3) == 3) {
Yaowu Xu's avatar
Yaowu Xu committed
1297
            aom_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
Jingning Han's avatar
Jingning Han committed
1298
1299
1300
1301
                                      lfi->lim, lfi->hev_thr, lfin->mblim,
                                      lfin->lim, lfin->hev_thr);
          } else {
            if (mask_4x4_int & 1)
Yaowu Xu's avatar
Yaowu Xu committed
1302
              aom_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
1303
                                   lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1304
            else if (mask_4x4_int & 2)
Yaowu Xu's avatar
Yaowu Xu committed
1305
              aom_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
1306
                                   lfin->lim, lfin->hev_thr);
Jingning Han's avatar
Jingning Han committed
1307
1308
1309
          }
          count = 2;
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
1310
          aom_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1311
1312

          if (mask_4x4_int & 1)
Yaowu Xu's avatar
Yaowu Xu committed
1313
            aom_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
1314
                                 lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1315
1316
        }
      } else if (mask_4x4_int & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
1317
        aom_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
1318
                             lfi->hev_thr);
Jingning Han's avatar
Jingning Han committed
1319
      }
Cheng Chen's avatar
Cheng Chen committed
1320
#endif  // CONFIG_LPF_DIRECT
Jingning Han's avatar
Jingning Han committed
1321
    }
Cheng Chen's avatar
Cheng Chen committed
1322
1323
1324
#if CONFIG_LPF_DIRECT
    idx_c += col_step * count;
#endif
Jingning Han's avatar
Jingning Han committed
1325
1326
1327
1328
1329
1330
1331
1332
1333
    s += 8 * count;
    lfl += count;
    mask_16x16 >>= count;
    mask_8x8 >>= count;
    mask_4x4 >>= count;
    mask_4x4_int >>= count;
  }
}

1334
#if CONFIG_HIGHBITDEPTH
clang-format's avatar
clang-format committed
1335
1336
1337
1338
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
1339
1340
1341
  unsigned int mask;
  int count;

clang-format's avatar
clang-format committed
1342
1343
  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int; mask;
       mask >>= count) {
Jingning Han's avatar
Jingning Han committed
1344
1345
1346
1347
1348
1349
    const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;

    count = 1;
    if (mask & 1) {
      if (mask_16x16 & 1) {
        if ((mask_16x16 & 3) == 3) {
Yaowu Xu's avatar
Yaowu Xu committed
1350
          aom_highbd_lpf_horizontal_edge_16(s, pitch, lfi->mblim, lfi->lim,
1351
                                            lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
1352
1353
          count = 2;
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
1354
          aom_highbd_lpf_horizontal_edge_8(s, pitch, lfi->mblim, lfi->lim,
1355
                                           lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
1356
1357
1358
1359
1360
1361
        }
      } else if (mask_8x8 & 1) {
        if ((mask_8x8 & 3) == 3) {
          // Next block's thresholds.
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

Yaowu Xu's avatar
Yaowu Xu committed
1362
          aom_highbd_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
Jingning Han's avatar
Jingning Han committed
1363
1364
1365
1366
                                           lfi->hev_thr, lfin->mblim, lfin->lim,
                                           lfin->hev_thr, bd);

          if ((mask_4x4_int & 3) == 3) {
Yaowu Xu's avatar
Yaowu Xu committed
1367
            aom_highbd_lpf_horizontal_4_dual(
clang-format's avatar
clang-format committed
1368
1369
                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
1370
1371
          } else {
            if (mask_4x4_int & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
1372
              aom_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
1373
                                          lfi->lim, lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
1374
            } else if (mask_4x4_int & 2) {
Yaowu Xu's avatar
Yaowu Xu committed
1375
              aom_highbd_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
1376
                                          lfin->lim, lfin->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
1377
1378
1379
1380
            }
          }
          count = 2;
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
1381
          aom_highbd_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim,
1382
                                      lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
1383
1384

          if (mask_4x4_int & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
1385
            aom_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
1386
                                        lfi->lim, lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed
1387
1388
1389
1390
1391
1392
1393
          }
        }
      } else if (mask_4x4 & 1) {
        if ((mask_4x4 & 3) == 3) {
          // Next block's thresholds.
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

Yaowu Xu's avatar
Yaowu Xu committed
1394
          aom_highbd_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
Jingning Han's avatar
Jingning Han committed
1395
1396
1397
                                           lfi->hev_thr, lfin->mblim, lfin->lim,
                                           lfin->hev_thr, bd);
          if ((mask_4x4_int & 3) == 3) {
Yaowu Xu's avatar
Yaowu Xu committed
1398
            aom_highbd_lpf_horizontal_4_dual(
clang-format's avatar
clang-format committed
1399
1400
                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
1401
1402
          } else {
            if (mask_4x4_int & 1) {
Yaowu Xu's avatar
Yaowu Xu committed
1403
              aom_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
1404
                                          lfi->lim, lfi->hev_thr, bd);
Jingning Han's avatar
Jingning Han committed