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

11
#include "./vpx_config.h"
12
13
#include "vp9/common/vp9_loopfilter.h"
#include "vp9/common/vp9_onyxc_int.h"
14
#include "vp9/common/vp9_reconinter.h"
Attila Nagy's avatar
Attila Nagy committed
15
#include "vpx_mem/vpx_mem.h"
John Koleszar's avatar
John Koleszar committed
16

17
#include "vp9/common/vp9_seg_common.h"
18

Jim Bankoski's avatar
Jim Bankoski committed
19
20
21
22
23
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
// 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.
static const uint64_t left_64x64_txform_mask[TX_SIZES]= {
    0xffffffffffffffff,  // TX_4X4
    0xffffffffffffffff,  // TX_8x8
    0x5555555555555555,  // TX_16x16
    0x1111111111111111,  // 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.
static const uint64_t above_64x64_txform_mask[TX_SIZES]= {
    0xffffffffffffffff,  // TX_4X4
    0xffffffffffffffff,  // TX_8x8
    0x00ff00ff00ff00ff,  // TX_16x16
    0x000000ff000000ff,  // 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] = {
    0x0000000000000001,  // BLOCK_4X4,
    0x0000000000000001,  // BLOCK_4X8,
    0x0000000000000001,  // BLOCK_8X4,
    0x0000000000000001,  // BLOCK_8X8,
    0x0000000000000101,  // BLOCK_8X16,
    0x0000000000000001,  // BLOCK_16X8,
    0x0000000000000101,  // BLOCK_16X16,
    0x0000000001010101,  // BLOCK_16X32,
    0x0000000000000101,  // BLOCK_32X16,
    0x0000000001010101,  // BLOCK_32X32,
    0x0101010101010101,  // BLOCK_32X64,
    0x0000000001010101,  // BLOCK_64X32,
    0x0101010101010101,  // BLOCK_64X64
};

// 64 bit mask to shift and set for each prediction size.
static const uint64_t above_prediction_mask[BLOCK_SIZES] = {
    0x0000000000000001,  // BLOCK_4X4
    0x0000000000000001,  // BLOCK_4X8
    0x0000000000000001,  // BLOCK_8X4
    0x0000000000000001,  // BLOCK_8X8
    0x0000000000000001,  // BLOCK_8X16,
    0x0000000000000003,  // BLOCK_16X8
    0x0000000000000003,  // BLOCK_16X16
    0x0000000000000003,  // BLOCK_16X32,
    0x000000000000000f,  // BLOCK_32X16,
    0x000000000000000f,  // BLOCK_32X32,
    0x000000000000000f,  // BLOCK_32X64,
    0x00000000000000ff,  // BLOCK_64X32,
    0x00000000000000ff,  // 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] = {
    0x0000000000000001,  // BLOCK_4X4
    0x0000000000000001,  // BLOCK_4X8
    0x0000000000000001,  // BLOCK_8X4
    0x0000000000000001,  // BLOCK_8X8
    0x0000000000000101,  // BLOCK_8X16,
    0x0000000000000003,  // BLOCK_16X8
    0x0000000000000303,  // BLOCK_16X16
    0x0000000003030303,  // BLOCK_16X32,
    0x0000000000000f0f,  // BLOCK_32X16,
    0x000000000f0f0f0f,  // BLOCK_32X32,
    0x0f0f0f0f0f0f0f0f,  // BLOCK_32X64,
    0x00000000ffffffff,  // BLOCK_64X32,
    0xffffffffffffffff,  // BLOCK_64X64
};

// These are used for masking the left and above borders.
static const uint64_t left_border =  0x1111111111111111;
static const uint64_t above_border = 0x000000ff000000ff;

// 16 bit masks for uv transform sizes.
static const uint16_t left_64x64_txform_mask_uv[TX_SIZES]= {
    0xffff,  // TX_4X4
    0xffff,  // TX_8x8
    0x5555,  // TX_16x16
    0x1111,  // TX_32x32
};

static const uint16_t above_64x64_txform_mask_uv[TX_SIZES]= {
    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
};
static const uint16_t left_border_uv =  0x1111;
static const uint16_t above_border_uv = 0x000f;

204
205
206
207
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)
};
Attila Nagy's avatar
Attila Nagy committed
208

Frank Galligan's avatar
Frank Galligan committed
209
static void update_sharpness(loop_filter_info_n *lfi, int sharpness_lvl) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
210
  int lvl;
John Koleszar's avatar
John Koleszar committed
211

Dmitry Kovalev's avatar
Dmitry Kovalev committed
212
213
214
215
  // For each possible value for the loop filter fill out limits
  for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++) {
    // Set loop filter paramaeters that control sharpness.
    int block_inside_limit = lvl >> ((sharpness_lvl > 0) + (sharpness_lvl > 4));
John Koleszar's avatar
John Koleszar committed
216

John Koleszar's avatar
John Koleszar committed
217
218
219
    if (sharpness_lvl > 0) {
      if (block_inside_limit > (9 - sharpness_lvl))
        block_inside_limit = (9 - sharpness_lvl);
John Koleszar's avatar
John Koleszar committed
220
    }
John Koleszar's avatar
John Koleszar committed
221
222
223
224

    if (block_inside_limit < 1)
      block_inside_limit = 1;

225
226
    vpx_memset(lfi->lfthr[lvl].lim, block_inside_limit, SIMD_WIDTH);
    vpx_memset(lfi->lfthr[lvl].mblim, (2 * (lvl + 2) + block_inside_limit),
John Koleszar's avatar
John Koleszar committed
227
228
               SIMD_WIDTH);
  }
Attila Nagy's avatar
Attila Nagy committed
229
}
John Koleszar's avatar
John Koleszar committed
230

231
void vp9_loop_filter_init(VP9_COMMON *cm) {
John Koleszar's avatar
John Koleszar committed
232
  loop_filter_info_n *lfi = &cm->lf_info;
233
  struct loopfilter *lf = &cm->lf;
234
  int lvl;
Attila Nagy's avatar
Attila Nagy committed
235

236
  // init limits for given sharpness
237
238
  update_sharpness(lfi, lf->sharpness_level);
  lf->last_sharpness_level = lf->sharpness_level;
Attila Nagy's avatar
Attila Nagy committed
239

240
  // init hev threshold const vectors
241
242
  for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++)
    vpx_memset(lfi->lfthr[lvl].hev_thr, (lvl >> 4), SIMD_WIDTH);
John Koleszar's avatar
John Koleszar committed
243
244
}

Frank Galligan's avatar
Frank Galligan committed
245
void vp9_loop_filter_frame_init(VP9_COMMON *cm, int default_filt_lvl) {
246
  int seg_id;
247
248
249
  // n_shift is the a 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
250
  const int scale = 1 << (default_filt_lvl >> 5);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
251
  loop_filter_info_n *const lfi = &cm->lf_info;
252
  struct loopfilter *const lf = &cm->lf;
253
  const struct segmentation *const seg = &cm->seg;
John Koleszar's avatar
John Koleszar committed
254

Dmitry Kovalev's avatar
Dmitry Kovalev committed
255
  // update limits if sharpness has changed
256
257
258
  if (lf->last_sharpness_level != lf->sharpness_level) {
    update_sharpness(lfi, lf->sharpness_level);
    lf->last_sharpness_level = lf->sharpness_level;
John Koleszar's avatar
John Koleszar committed
259
260
  }

261
  for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) {
262
    int lvl_seg = default_filt_lvl;
263
    if (vp9_segfeature_active(seg, seg_id, SEG_LVL_ALT_LF)) {
264
265
      const int data = vp9_get_segdata(seg, seg_id, SEG_LVL_ALT_LF);
      lvl_seg = seg->abs_delta == SEGMENT_ABSDATA
Dmitry Kovalev's avatar
Dmitry Kovalev committed
266
267
                  ? data
                  : clamp(default_filt_lvl + data, 0, MAX_LOOP_FILTER);
John Koleszar's avatar
John Koleszar committed
268
    }
John Koleszar's avatar
John Koleszar committed
269

270
    if (!lf->mode_ref_delta_enabled) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
271
272
      // we could get rid of this if we assume that deltas are set to
      // zero when not in use; encoder always uses deltas
273
      vpx_memset(lfi->lvl[seg_id], lvl_seg, sizeof(lfi->lvl[seg_id]));
274
275
276
277
278
279
280
281
282
283
284
    } 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);

      for (ref = LAST_FRAME; ref < MAX_REF_FRAMES; ++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][ref][mode] = clamp(inter_lvl, 0, MAX_LOOP_FILTER);
        }
John Koleszar's avatar
John Koleszar committed
285
      }
286
    }
John Koleszar's avatar
John Koleszar committed
287
  }
John Koleszar's avatar
John Koleszar committed
288
289
}

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
static void filter_selectively_vert_row2(PLANE_TYPE plane_type,
                                         uint8_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) {
  const int mask_shift = plane_type ? 4 : 8;
  const int mask_cutoff = plane_type ? 0xf : 0xff;
  const int lfl_forward = plane_type ? 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;
310
311
  unsigned int mask;

312
313
314
315
316
  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);
317

318
    // TODO(yunqingwang): count in loopfilter functions should be removed.
319
    if (mask & 1) {
320
321
      if ((mask_16x16_0 | mask_16x16_1) & 1) {
        if ((mask_16x16_0 & mask_16x16_1) & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
322
323
          vp9_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
                                   lfi0->hev_thr);
324
        } else if (mask_16x16_0 & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
325
326
          vp9_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim,
                              lfi0->hev_thr);
327
        } else {
Jim Bankoski's avatar
Jim Bankoski committed
328
329
          vp9_lpf_vertical_16(s + 8 *pitch, pitch, lfi1->mblim,
                              lfi1->lim, lfi1->hev_thr);
330
        }
331
332
333
334
      }

      if ((mask_8x8_0 | mask_8x8_1) & 1) {
        if ((mask_8x8_0 & mask_8x8_1) & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
335
336
337
          vp9_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
                                  lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                  lfi1->hev_thr);
338
        } else if (mask_8x8_0 & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
339
340
          vp9_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr,
                             1);
341
        } else {
Jim Bankoski's avatar
Jim Bankoski committed
342
343
          vp9_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
                             lfi1->hev_thr, 1);
344
345
346
347
348
        }
      }

      if ((mask_4x4_0 | mask_4x4_1) & 1) {
        if ((mask_4x4_0 & mask_4x4_1) & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
349
350
351
          vp9_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
                                  lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                  lfi1->hev_thr);
352
        } else if (mask_4x4_0 & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
353
354
          vp9_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr,
                             1);
355
        } else {
Jim Bankoski's avatar
Jim Bankoski committed
356
357
          vp9_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
                             lfi1->hev_thr, 1);
358
359
360
361
362
        }
      }

      if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
        if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
363
364
365
          vp9_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
                                  lfi0->hev_thr, lfi1->mblim, lfi1->lim,
                                  lfi1->hev_thr);
366
        } else if (mask_4x4_int_0 & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
367
368
          vp9_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
                             lfi0->hev_thr, 1);
369
        } else {
Jim Bankoski's avatar
Jim Bankoski committed
370
371
          vp9_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim, lfi1->lim,
                             lfi1->hev_thr, 1);
372
        }
373
374
      }
    }
375

376
    s += 8;
Yunqing Wang's avatar
Yunqing Wang committed
377
    lfl += 1;
378
379
380
381
382
383
384
385
    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;
386
387
388
389
390
391
392
  }
}

static void filter_selectively_horiz(uint8_t *s, int pitch,
                                     unsigned int mask_16x16,
                                     unsigned int mask_8x8,
                                     unsigned int mask_4x4,
393
                                     unsigned int mask_4x4_int,
Yunqing Wang's avatar
Yunqing Wang committed
394
395
                                     const loop_filter_info_n *lfi_n,
                                     const uint8_t *lfl) {
396
  unsigned int mask;
397
  int count;
398

John Koleszar's avatar
John Koleszar committed
399
  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
400
       mask; mask >>= count) {
Yunqing Wang's avatar
Yunqing Wang committed
401
    const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
402

Dmitry Kovalev's avatar
Dmitry Kovalev committed
403
    count = 1;
404
    if (mask & 1) {
405
      if (mask_16x16 & 1) {
406
        if ((mask_16x16 & 3) == 3) {
Jim Bankoski's avatar
Jim Bankoski committed
407
408
          vp9_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
                                lfi->hev_thr, 2);
409
410
          count = 2;
        } else {
Jim Bankoski's avatar
Jim Bankoski committed
411
412
          vp9_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
                                lfi->hev_thr, 1);
413
        }
414
415
416
417
418
      } else if (mask_8x8 & 1) {
        if ((mask_8x8 & 3) == 3) {
          // Next block's thresholds
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);

Jim Bankoski's avatar
Jim Bankoski committed
419
420
421
          vp9_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
                                    lfi->hev_thr, lfin->mblim, lfin->lim,
                                    lfin->hev_thr);
422
423

          if ((mask_4x4_int & 3) == 3) {
Jim Bankoski's avatar
Jim Bankoski committed
424
425
426
            vp9_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
                                      lfi->lim, lfi->hev_thr, lfin->mblim,
                                      lfin->lim, lfin->hev_thr);
427
          } else {
428
            if (mask_4x4_int & 1)
Jim Bankoski's avatar
Jim Bankoski committed
429
430
              vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
                                   lfi->hev_thr, 1);
431
            else if (mask_4x4_int & 2)
Jim Bankoski's avatar
Jim Bankoski committed
432
433
              vp9_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
                                   lfin->lim, lfin->hev_thr, 1);
434
          }
435
436
          count = 2;
        } else {
Jim Bankoski's avatar
Jim Bankoski committed
437
          vp9_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
438
439

          if (mask_4x4_int & 1)
Jim Bankoski's avatar
Jim Bankoski committed
440
441
            vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
                                 lfi->hev_thr, 1);
442
        }
443
444
445
446
      } else if (mask_4x4 & 1) {
        if ((mask_4x4 & 3) == 3) {
          // Next block's thresholds
          const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
447

Jim Bankoski's avatar
Jim Bankoski committed
448
449
450
          vp9_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
                                    lfi->hev_thr, lfin->mblim, lfin->lim,
                                    lfin->hev_thr);
451
          if ((mask_4x4_int & 3) == 3) {
Jim Bankoski's avatar
Jim Bankoski committed
452
453
454
            vp9_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
                                      lfi->lim, lfi->hev_thr, lfin->mblim,
                                      lfin->lim, lfin->hev_thr);
455
456
          } else {
            if (mask_4x4_int & 1)
Jim Bankoski's avatar
Jim Bankoski committed
457
458
              vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
                                   lfi->hev_thr, 1);
459
            else if (mask_4x4_int & 2)
Jim Bankoski's avatar
Jim Bankoski committed
460
461
              vp9_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
                                   lfin->lim, lfin->hev_thr, 1);
462
463
464
          }
          count = 2;
        } else {
Jim Bankoski's avatar
Jim Bankoski committed
465
          vp9_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
466

Jim Bankoski's avatar
Jim Bankoski committed
467
468
469
          if (mask_4x4_int & 1)
            vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
                                 lfi->hev_thr, 1);
470
471
        }
      } else if (mask_4x4_int & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
472
473
        vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
                             lfi->hev_thr, 1);
474
      }
475
    }
476
    s += 8 * count;
Yunqing Wang's avatar
Yunqing Wang committed
477
    lfl += count;
478
479
480
481
    mask_16x16 >>= count;
    mask_8x8 >>= count;
    mask_4x4 >>= count;
    mask_4x4_int >>= count;
482
483
484
  }
}

Jim Bankoski's avatar
Jim Bankoski committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// 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,
                        const int shift_uv,
                        LOOP_FILTER_MASK *lfm) {
  const BLOCK_SIZE block_size = mi->mbmi.sb_type;
  const TX_SIZE tx_size_y = mi->mbmi.tx_size;
  const TX_SIZE tx_size_uv = get_uv_tx_size(&mi->mbmi);
  const int skip = mi->mbmi.skip_coeff;
  const int seg = mi->mbmi.segment_id;
  const int ref = mi->mbmi.ref_frame[0];
502
  const int filter_level = lfi_n->lvl[seg][ref][mode_lf_lut[mi->mbmi.mode]];
Jim Bankoski's avatar
Jim Bankoski committed
503
504
505
506
507
508
  uint64_t *left_y = &lfm->left_y[tx_size_y];
  uint64_t *above_y = &lfm->above_y[tx_size_y];
  uint64_t *int_4x4_y = &lfm->int_4x4_y;
  uint16_t *left_uv = &lfm->left_uv[tx_size_uv];
  uint16_t *above_uv = &lfm->above_uv[tx_size_uv];
  uint16_t *int_4x4_uv = &lfm->int_4x4_uv;
Yunqing Wang's avatar
Yunqing Wang committed
509
510
511
512
  int i;
  int w = num_8x8_blocks_wide_lookup[block_size];
  int h = num_8x8_blocks_high_lookup[block_size];

Jim Bankoski's avatar
Jim Bankoski committed
513
  // If filter level is 0 we don't loop filter.
514
  if (!filter_level) {
Jim Bankoski's avatar
Jim Bankoski committed
515
    return;
516
517
518
519
520
521
522
  } else {
    int index = shift_y;
    for (i = 0; i < h; i++) {
      vpx_memset(&lfm->lfl_y[index], filter_level, w);
      index += 8;
    }
  }
Jim Bankoski's avatar
Jim Bankoski committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582

  // 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.
  if (skip && ref > INTRA_FRAME)
    return;

  // 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..
  *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;

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

  // 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] & 0xffffffffffffffff) << 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,
                         LOOP_FILTER_MASK *lfm) {
  const BLOCK_SIZE block_size = mi->mbmi.sb_type;
  const TX_SIZE tx_size_y = mi->mbmi.tx_size;
  const int skip = mi->mbmi.skip_coeff;
  const int seg = mi->mbmi.segment_id;
  const int ref = mi->mbmi.ref_frame[0];
583
  const int filter_level = lfi_n->lvl[seg][ref][mode_lf_lut[mi->mbmi.mode]];
Jim Bankoski's avatar
Jim Bankoski committed
584
585
586
  uint64_t *left_y = &lfm->left_y[tx_size_y];
  uint64_t *above_y = &lfm->above_y[tx_size_y];
  uint64_t *int_4x4_y = &lfm->int_4x4_y;
Yunqing Wang's avatar
Yunqing Wang committed
587
588
589
590
  int i;
  int w = num_8x8_blocks_wide_lookup[block_size];
  int h = num_8x8_blocks_high_lookup[block_size];

591
  if (!filter_level) {
Jim Bankoski's avatar
Jim Bankoski committed
592
    return;
593
594
595
596
597
598
599
  } else {
    int index = shift_y;
    for (i = 0; i < h; i++) {
      vpx_memset(&lfm->lfl_y[index], filter_level, w);
      index += 8;
    }
  }
Jim Bankoski's avatar
Jim Bankoski committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620

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

  if (skip && ref > INTRA_FRAME)
    return;

  *above_y |= (size_mask[block_size] &
               above_64x64_txform_mask[tx_size_y]) << shift_y;

  *left_y |= (size_mask[block_size] &
              left_64x64_txform_mask[tx_size_y]) << shift_y;

  if (tx_size_y == TX_4X4) {
    *int_4x4_y |= (size_mask[block_size] & 0xffffffffffffffff) << 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.
621
622
623
void vp9_setup_mask(VP9_COMMON *const cm, const int mi_row, const int mi_col,
                    MODE_INFO **mi_8x8, const int mode_info_stride,
                    LOOP_FILTER_MASK *lfm) {
Jim Bankoski's avatar
Jim Bankoski committed
624
625
  int idx_32, idx_16, idx_8;
  const loop_filter_info_n *const lfi_n = &cm->lf_info;
626
627
  MODE_INFO **mip = mi_8x8;
  MODE_INFO **mip2 = mi_8x8;
Jim Bankoski's avatar
Jim Bankoski committed
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658

  // 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 avoids
  // setting up special row and column counters for each index.  The last step
  // brings us out back to the starting position.
  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};

  // 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.
  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};
  int i;
  const int max_rows = (mi_row + MI_BLOCK_SIZE > cm->mi_rows ?
                        cm->mi_rows - mi_row : MI_BLOCK_SIZE);
  const int max_cols = (mi_col + MI_BLOCK_SIZE > cm->mi_cols ?
                        cm->mi_cols - mi_col : MI_BLOCK_SIZE);

  vp9_zero(*lfm);

  // 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.
659
  switch (mip[0]->mbmi.sb_type) {
Jim Bankoski's avatar
Jim Bankoski committed
660
    case BLOCK_64X64:
661
      build_masks(lfi_n, mip[0] , 0, 0, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
662
663
      break;
    case BLOCK_64X32:
664
      build_masks(lfi_n, mip[0], 0, 0, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
665
      mip2 = mip + mode_info_stride * 4;
666
667
      if (4 >= max_rows)
        break;
668
      build_masks(lfi_n, mip2[0], 32, 8, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
669
670
      break;
    case BLOCK_32X64:
671
      build_masks(lfi_n, mip[0], 0, 0, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
672
      mip2 = mip + 4;
673
674
      if (4 >= max_cols)
        break;
675
      build_masks(lfi_n, mip2[0], 4, 2, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
676
677
678
679
680
681
682
683
684
      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;
685
        switch (mip[0]->mbmi.sb_type) {
Jim Bankoski's avatar
Jim Bankoski committed
686
          case BLOCK_32X32:
687
            build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
688
689
            break;
          case BLOCK_32X16:
690
            build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
691
692
            if (mi_32_row_offset + 2 >= max_rows)
              continue;
Jim Bankoski's avatar
Jim Bankoski committed
693
            mip2 = mip + mode_info_stride * 2;
694
            build_masks(lfi_n, mip2[0], shift_y + 16, shift_uv + 4, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
695
696
            break;
          case BLOCK_16X32:
697
            build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
698
699
            if (mi_32_col_offset + 2 >= max_cols)
              continue;
Jim Bankoski's avatar
Jim Bankoski committed
700
            mip2 = mip + 2;
701
            build_masks(lfi_n, mip2[0], shift_y + 2, shift_uv + 1, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
702
703
704
705
706
707
708
709
710
711
712
713
714
            break;
          default:
            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];
              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);

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

715
              switch (mip[0]->mbmi.sb_type) {
Jim Bankoski's avatar
Jim Bankoski committed
716
                case BLOCK_16X16:
717
                  build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
718
719
                  break;
                case BLOCK_16X8:
720
                  build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
721
722
                  if (mi_16_row_offset + 1 >= max_rows)
                    continue;
Jim Bankoski's avatar
Jim Bankoski committed
723
                  mip2 = mip + mode_info_stride;
724
                  build_y_mask(lfi_n, mip2[0], shift_y+8, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
725
726
                  break;
                case BLOCK_8X16:
727
                  build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
728
729
                  if (mi_16_col_offset +1 >= max_cols)
                    continue;
Jim Bankoski's avatar
Jim Bankoski committed
730
                  mip2 = mip + 1;
731
                  build_y_mask(lfi_n, mip2[0], shift_y+1, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
732
733
734
735
736
                  break;
                default: {
                  const int shift_y = shift_32_y[idx_32] +
                                      shift_16_y[idx_16] +
                                      shift_8_y[0];
737
                  build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
738
739
740
741
742
743
744
745
746
747
748
749
750
                  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] +
                                        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));

                    if (mi_8_col_offset >= max_cols ||
                        mi_8_row_offset >= max_rows)
                      continue;
751
                    build_y_mask(lfi_n, mip[0], shift_y, lfm);
Jim Bankoski's avatar
Jim Bankoski committed
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
                  }
                  break;
                }
              }
            }
            break;
        }
      }
      break;
  }
  // The largest loopfilter we have is 16x16 so we use the 16x16 mask
  // for 32x32 transforms also 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.
  if (mi_row + MI_BLOCK_SIZE > cm->mi_rows) {
    const uint64_t rows = cm->mi_rows - mi_row;

    // Each pixel inside the border gets a 1,
    const uint64_t mask_y = (((uint64_t) 1 << (rows << 3)) - 1);
    const uint16_t mask_uv = (((uint16_t) 1 << (((rows + 1) >> 1) << 2)) - 1);

    // 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;
    lfm->int_4x4_uv &= mask_uv;

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

  if (mi_col + MI_BLOCK_SIZE > cm->mi_cols) {
    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.
    const uint64_t mask_y  = (((1 << columns) - 1)) * 0x0101010101010101;
    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;
    lfm->int_4x4_uv &= mask_uv_int;

    // 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 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] &= 0xfefefefefefefefe;
      lfm->left_uv[i] &= 0xeeee;
    }
  }
Jim Bankoski's avatar
Jim Bankoski committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868

  // 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]));
  assert(!(lfm->left_uv[TX_16X16]&lfm->left_uv[TX_8X8]));
  assert(!(lfm->left_uv[TX_16X16] & lfm->left_uv[TX_4X4]));
  assert(!(lfm->left_uv[TX_8X8] & lfm->left_uv[TX_4X4]));
  assert(!(lfm->int_4x4_uv & lfm->left_uv[TX_16X16]));
  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]));
  assert(!(lfm->int_4x4_uv & lfm->above_uv[TX_16X16]));
Jim Bankoski's avatar
Jim Bankoski committed
869
}
Yunqing Wang's avatar
Yunqing Wang committed
870

871
#if CONFIG_NON420
872
873
874
875
static uint8_t build_lfi(const loop_filter_info_n *lfi_n,
                     const MB_MODE_INFO *mbmi) {
  const int seg = mbmi->segment_id;
  const int ref = mbmi->ref_frame[0];
876
  return lfi_n->lvl[seg][ref][mode_lf_lut[mbmi->mode]];
877
878
}

879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
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) {
  unsigned int mask;

  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
       mask; mask >>= 1) {
    const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;

    if (mask & 1) {
      if (mask_16x16 & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
894
        vp9_lpf_vertical_16(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
895
      } else if (mask_8x8 & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
896
        vp9_lpf_vertical_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
897
      } else if (mask_4x4 & 1) {
Jim Bankoski's avatar
Jim Bankoski committed
898
        vp9_lpf_vertical_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
899
900
901
      }
    }
    if (mask_4x4_int & 1)
Jim Bankoski's avatar
Jim Bankoski committed
902
      vp9_lpf_vertical_4(s + 4, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
903
904
905
906
907
908
909
910
911
    s += 8;
    lfl += 1;
    mask_16x16 >>= 1;
    mask_8x8 >>= 1;
    mask_4x4 >>= 1;
    mask_4x4_int >>= 1;
  }
}

Jim Bankoski's avatar
Jim Bankoski committed
912
913
static void filter_block_plane_non420(VP9_COMMON *cm,
                                      struct macroblockd_plane *plane,
914
                                      MODE_INFO **mi_8x8,
Jim Bankoski's avatar
Jim Bankoski committed
915
                                      int mi_row, int mi_col) {
916
917
  const int ss_x = plane->subsampling_x;
  const int ss_y = plane->subsampling_y;
918
919
  const int row_step = 1 << ss_x;
  const int col_step = 1 << ss_y;
920
921
  const int row_step_stride = cm->mode_info_stride * row_step;
  struct buf_2d *const dst = &plane->dst;
922
  uint8_t* const dst0 = dst->buf;
923
924
925
926
  unsigned int mask_16x16[MI_BLOCK_SIZE] = {0};
  unsigned int mask_8x8[MI_BLOCK_SIZE] = {0};
  unsigned int mask_4x4[MI_BLOCK_SIZE] = {0};
  unsigned int mask_4x4_int[MI_BLOCK_SIZE] = {0};
927
  uint8_t lfl[MI_BLOCK_SIZE * MI_BLOCK_SIZE];
928
  int r, c;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
929

930
  for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
931
932
933
934
935
936
    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
937
    for (c = 0; c < MI_BLOCK_SIZE && mi_col + c < cm->mi_cols; c += col_step) {
938
      const MODE_INFO *mi = mi_8x8[c];
939
      const BLOCK_SIZE sb_type = mi[0].mbmi.sb_type;
940
941
      const int skip_this = mi[0].mbmi.skip_coeff
                            && is_inter_block(&mi[0].mbmi);
942
      // left edge of current unit is block/partition edge -> no skip
943
944
      const int block_edge_left = (num_4x4_blocks_wide_lookup[sb_type] > 1) ?
          !(c & (num_8x8_blocks_wide_lookup[sb_type] - 1)) : 1;
945
      const int skip_this_c = skip_this && !block_edge_left;
946
      // top edge of current unit is block/partition edge -> no skip
947
948
      const int block_edge_above = (num_4x4_blocks_high_lookup[sb_type] > 1) ?
          !(r & (num_8x8_blocks_high_lookup[sb_type] - 1)) : 1;
949
      const int skip_this_r = skip_this && !block_edge_above;
950
      const TX_SIZE tx_size = (plane->plane_type == PLANE_TYPE_UV)
951
952
                            ? get_uv_tx_size(&mi[0].mbmi)
                            : mi[0].mbmi.tx_size;
953
      const int skip_border_4x4_c = ss_x && mi_col + c == cm->mi_cols - 1;
954
      const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
955
956

      // Filter level can vary per MI
957
958
      if (!(lfl[(r << 3) + (c >> ss_x)] =
          build_lfi(&cm->lf_info, &mi[0].mbmi)))
959
960
961
962
        continue;

      // Build masks based on the transform size of each block
      if (tx_size == TX_32X32) {
963
964
965
966
967
968
969
970
971
972
973
974
        if (!skip_this_c && ((c >> ss_x) & 3) == 0) {
          if (!skip_border_4x4_c)
            mask_16x16_c |= 1 << (c >> ss_x);
          else
            mask_8x8_c |= 1 << (c >> ss_x);
        }
        if (!skip_this_r && ((r >> ss_y) & 3) == 0) {
          if (!skip_border_4x4_r)
            mask_16x16[r] |= 1 << (c >> ss_x);
          else
            mask_8x8[r] |= 1 << (c >> ss_x);
        }
975
      } else if (tx_size == TX_16X16) {
976
977
978
979
980
981
982
983
984
985
986
987
        if (!skip_this_c && ((c >> ss_x) & 1) == 0) {
          if (!skip_border_4x4_c)
            mask_16x16_c |= 1 << (c >> ss_x);
          else
            mask_8x8_c |= 1 << (c >> ss_x);
        }
        if (!skip_this_r && ((r >> ss_y) & 1) == 0) {
          if (!skip_border_4x4_r)
            mask_16x16[r] |= 1 << (c >> ss_x);
          else
            mask_8x8[r] |= 1 << (c >> ss_x);
        }
988
989
990
      } else {
        // force 8x8 filtering on 32x32 boundaries
        if (!skip_this_c) {
991
          if (tx_size == TX_8X8 || ((c >> ss_x) & 3) == 0)
992
993
994
995
996
997
            mask_8x8_c |= 1 << (c >> ss_x);
          else
            mask_4x4_c |= 1 << (c >> ss_x);
        }

        if (!skip_this_r) {
998
          if (tx_size == TX_8X8 || ((r >> ss_y) & 3) == 0)
999
1000
1001
1002
1003
            mask_8x8[r] |= 1 << (c >> ss_x);
          else
            mask_4x4[r] |= 1 << (c >> ss_x);
        }

1004
1005
        if (!skip_this && tx_size < TX_8X8 && !skip_border_4x4_c)
          mask_4x4_int[r] |= 1 << (c >> ss_x);
1006
1007
1008
1009
1010
1011
1012
1013
1014
      }
    }

    // Disable filtering on the leftmost column
    border_mask = ~(mi_col == 0);
    filter_selectively_vert(dst->buf, dst->stride,
                            mask_16x16_c & border_mask,
                            mask_8x8_c & border_mask,
                            mask_4x4_c & border_mask,
Yunqing Wang's avatar
Yunqing Wang committed
1015
                            mask_4x4_int[r],
1016
                            &cm->lf_info, &lfl[r << 3]);
1017
    dst->buf += 8 * dst->stride;
1018
    mi_8x8 += row_step_stride;
1019
1020
1021
1022
  }

  // Now do horizontal pass
  dst->buf = dst0;
1023
  for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
1024
1025
1026
    const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
    const unsigned int mask_4x4_int_r = skip_border_4x4_r ? 0 : mask_4x4_int[r];

1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
    unsigned int mask_16x16_r;
    unsigned int mask_8x8_r;
    unsigned int mask_4x4_r;

    if (mi_row + r == 0) {
      mask_16x16_r = 0;
      mask_8x8_r = 0;
      mask_4x4_r = 0;
    } else {
      mask_16x16_r = mask_16x16[r];
      mask_8x8_r = mask_8x8[r];
      mask_4x4_r = mask_4x4[r];
    }

1041
    filter_selectively_horiz(dst->buf, dst->stride,
1042
1043
1044
1045
                             mask_16x16_r,
                             mask_8x8_r,
                             mask_4x4_r,
                             mask_4x4_int_r,
1046
                             &cm->lf_info, &lfl[r << 3]);
1047
1048
1049
    dst->buf += 8 * dst->stride;
  }
}
1050
#endif
1051

1052
1053
1054
1055
void vp9_filter_block_plane(VP9_COMMON *const cm,
                            struct macroblockd_plane *const plane,
                            int mi_row,
                            LOOP_FILTER_MASK *lfm) {
Jim Bankoski's avatar
Jim Bankoski committed
1056
1057
1058
1059
  struct buf_2d *const dst = &plane->dst;
  uint8_t* const dst0 = dst->buf;
  int r, c;

Yunqing Wang's avatar
Yunqing Wang committed
1060
1061
1062
1063
1064
  if (!plane->plane_type) {
    uint64_t mask_16x16 = lfm->left_y[TX_16X16];
    uint64_t mask_8x8 = lfm->left_y[TX_8X8];
    uint64_t mask_4x4 = lfm->left_y[TX_4X4];
    uint64_t mask_4x4_int = lfm->int_4x4_y;
Jim Bankoski's avatar
Jim Bankoski committed
1065

1066
1067
1068
1069
1070
1071
    // Vertical pass: do 2 rows at one time
    for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
      unsigned int mask_16x16_l = mask_16x16 & 0xffff;
      unsigned int mask_8x8_l = mask_8x8 & 0xffff;
      unsigned int mask_4x4_l = mask_4x4 & 0xffff;
      unsigned int mask_4x4_int_l = mask_4x4_int & 0xffff;
1072

Jim Bankoski's avatar
Jim Bankoski committed
1073
      // Disable filtering on the leftmost column
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
      filter_selectively_vert_row2(plane->plane_type,
                                   dst->buf, dst->stride,
                                   mask_16x16_l,
                                   mask_8x8_l,
                                   mask_4x4_l,
                                   mask_4x4_int_l,
                                   &cm->lf_info, &lfm->lfl_y[r << 3]);

      dst->buf += 16 * dst->stride;
      mask_16x16 >>= 16;
      mask_8x8 >>= 16;
      mask_4x4 >>= 16;
      mask_4x4_int >>= 16;
Yunqing Wang's avatar
Yunqing Wang committed
1087
1088
1089
1090
1091
1092
1093
    }

    // Horizontal pass
    dst->buf = dst0;
    mask_16x16 = lfm->above_y[TX_16X16];
    mask_8x8 = lfm->above_y[TX_8X8];
    mask_4x4 = lfm->above_y[TX_4X4];
1094
    mask_4x4_int = lfm->int_4x4_y;
Yunqing Wang's avatar
Yunqing Wang committed
1095
1096

    for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r++) {
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
      unsigned int mask_16x16_r;
      unsigned int mask_8x8_r;
      unsigned int mask_4x4_r;

      if (mi_row + r == 0) {
        mask_16x16_r = 0;
        mask_8x8_r = 0;
        mask_4x4_r = 0;
      } else {
        mask_16x16_r = mask_16x16 & 0xff;
        mask_8x8_r = mask_8x8 & 0xff;
        mask_4x4_r = mask_4x4 & 0xff;
      }

Yunqing Wang's avatar
Yunqing Wang committed
1111
      filter_selectively_horiz(dst->buf, dst->stride,
1112
1113
1114
                               mask_16x16_r,
                               mask_8x8_r,
                               mask_4x4_r,
1115
                               mask_4x4_int & 0xff,
1116
                               &cm->lf_info, &lfm->lfl_y[r << 3]);
Yunqing Wang's avatar
Yunqing Wang committed
1117
1118
1119
1120
1121

      dst->buf += 8 * dst->stride;
      mask_16x16 >>= 8;
      mask_8x8 >>= 8;
      mask_4x4 >>= 8;
1122
      mask_4x4_int >>= 8;
Yunqing Wang's avatar
Yunqing Wang committed
1123
1124
1125
1126
1127
1128
1129
    }
  } else {
    uint16_t mask_16x16 = lfm->left_uv[TX_16X16];
    uint16_t mask_8x8 = lfm->left_uv[TX_8X8];
    uint16_t mask_4x4 = lfm->left_uv[TX_4X4];
    uint16_t mask_4x4_int = lfm->int_4x4_uv;

1130
1131
    // Vertical pass: do 2 rows at one time
    for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 4) {
Yunqing Wang's avatar
Yunqing Wang committed
1132
      if (plane->plane_type == 1) {
1133
        for (c = 0; c < (MI_BLOCK_SIZE >> 1); c++) {
1134
          lfm->lfl_uv[(r << 1) + c] = lfm->lfl_y[(r << 3) + (c << 1)];
1135
1136
1137
          lfm->lfl_uv[((r + 2) << 1) + c] = lfm->lfl_y[((r + 2) << 3) +
                                                       (c << 1)];
        }
Yunqing Wang's avatar
Yunqing Wang committed
1138
1139
      }

1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
      {
        unsigned int mask_16x16_l = mask_16x16 & 0xff;
        unsigned int mask_8x8_l = mask_8x8 & 0xff;
        unsigned int mask_4x4_l = mask_4x4 & 0xff;
        unsigned int mask_4x4_int_l = mask_4x4_int & 0xff;

        // Disable filtering on the leftmost column
        filter_selectively_vert_row2(plane->plane_type,
                                     dst->buf, dst->stride,
                                     mask_16x16_l,
                                     mask_8x8_l,
                                     mask_4x4_l,
                                     mask_4x4_int_l,
                                     &cm->lf_info, &lfm->lfl_uv[r << 1]);

        dst->buf += 16 * dst->stride;
        mask_16x16 >>= 8;
        mask_8x8 >>= 8;
        mask_4x4 >>= 8;
        mask_4x4_int >>= 8;
      }
Jim Bankoski's avatar
Jim Bankoski committed
1161
1162
    }

Yunqing Wang's avatar
Yunqing Wang committed
1163
1164
1165
1166
1167
    // Horizontal pass
    dst->buf = dst0;
    mask_16x16 = lfm->above_uv[TX_16X16];
    mask_8x8 = lfm->above_uv[TX_8X8];
    mask_4x4 = lfm->above_uv[TX_4X4];
1168
    mask_4x4_int = lfm->int_4x4_uv;
Yunqing Wang's avatar
Yunqing Wang committed
1169
1170
1171
1172

    for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
      const int skip_border_4x4_r = mi_row + r == cm->mi_rows - 1;
      const unsigned int mask_4x4_int_r = skip_border_4x4_r ?
1173
          0 : (mask_4x4_int & 0xf);
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
      unsigned int mask_16x16_r;
      unsigned int mask_8x8_r;
      unsigned int mask_4x4_r;

      if (mi_row + r == 0) {
        mask_16x16_r = 0;
        mask_8x8_r = 0;
        mask_4x4_r = 0;
      } else {
        mask_16x16_r = mask_16x16 & 0xf;
        mask_8x8_r = mask_8x8 & 0xf;
        mask_4x4_r = mask_4x4 & 0xf;
      }
Jim Bankoski's avatar
Jim Bankoski committed
1187
1188

      filter_selectively_horiz(dst->buf, dst->stride,
1189
1190
1191
                               mask_16x16_r,
                               mask_8x8_r,
                               mask_4x4_r,
Jim Bankoski's avatar
Jim Bankoski committed
1192
                               mask_4x4_int_r,
1193
                               &cm->lf_info, &lfm->lfl_uv[r << 1]);
Yunqing Wang's avatar
Yunqing Wang committed
1194
1195
1196
1197
1198

      dst->buf += 8 * dst->stride;
      mask_16x16 >>= 4;
      mask_8x8 >>= 4;
      mask_4x4 >>= 4;
1199
      mask_4x4_int >>= 4;
Jim Bankoski's avatar
Jim Bankoski committed
1200
1201
1202
1203
    }
  }
}

1204
1205
1206
void vp9_loop_filter_rows(const YV12_BUFFER_CONFIG *frame_buffer,
                          VP9_COMMON *cm, MACROBLOCKD *xd,
                          int start, int stop, int y_only) {
1207
  const int num_planes = y_only ? 1 : MAX_MB_PLANE;
1208
  int mi_row, mi_col;
Jim Bankoski's avatar
Jim Bankoski committed
1209
  LOOP_FILTER_MASK lfm;
1210
#if CONFIG_NON420
Jim Bankoski's avatar
Jim Bankoski committed
1211
1212
  int use_420 = y_only || (xd->plane[1].subsampling_y == 1 &&
      xd->plane[1].subsampling_x == 1);
1213
#endif
1214

1215
  for (mi_row = start; mi_row < stop; mi_row += MI_BLOCK_SIZE) {
1216
    MODE_INFO **mi_8x8 = cm->mi_grid_visible + mi_row * cm->mode_info_stride;
1217

1218
    for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) {
1219
1220
      int plane;

1221
      setup_dst_planes(xd, frame_buffer, mi_row, mi_col);
Jim Bankoski's avatar
Jim Bankoski committed
1222
1223

      // TODO(JBB): Make setup_mask work for non 420.
1224
#if CONFIG_NON420
Jim Bankoski's avatar
Jim Bankoski committed
1225
      if (use_420)
1226
#endif
1227
1228
        vp9_setup_mask(cm, mi_row, mi_col, mi_8x8 + mi_col,
                       cm->mode_info_stride, &lfm);
Jim Bankoski's avatar
Jim Bankoski committed
1229

1230
      for (plane = 0; plane < num_planes; ++plane) {
1231
#if CONFIG_NON420
Jim Bankoski's avatar
Jim Bankoski committed
1232
        if (use_420)
1233
#endif
1234
          vp9_filter_block_plane(cm, &xd->plane[plane], mi_row, &lfm);
1235
#if CONFIG_NON420