]> granicus.if.org Git - libvpx/blob - vp9/common/vp9_loopfilter.c
vpx_highbd_lpf_horizontal_4: remove unused count param
[libvpx] / vp9 / common / vp9_loopfilter.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  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.
9  */
10
11 #include "./vpx_config.h"
12 #include "./vpx_dsp_rtcd.h"
13 #include "vp9/common/vp9_loopfilter.h"
14 #include "vp9/common/vp9_onyxc_int.h"
15 #include "vp9/common/vp9_reconinter.h"
16 #include "vpx_dsp/vpx_dsp_common.h"
17 #include "vpx_mem/vpx_mem.h"
18 #include "vpx_ports/mem.h"
19
20 #include "vp9/common/vp9_seg_common.h"
21
22 // 64 bit masks for left transform size. Each 1 represents a position where
23 // we should apply a loop filter across the left border of an 8x8 block
24 // boundary.
25 //
26 // In the case of TX_16X16->  ( in low order byte first we end up with
27 // a mask that looks like this
28 //
29 //    10101010
30 //    10101010
31 //    10101010
32 //    10101010
33 //    10101010
34 //    10101010
35 //    10101010
36 //    10101010
37 //
38 // A loopfilter should be applied to every other 8x8 horizontally.
39 static const uint64_t left_64x64_txform_mask[TX_SIZES]= {
40   0xffffffffffffffffULL,  // TX_4X4
41   0xffffffffffffffffULL,  // TX_8x8
42   0x5555555555555555ULL,  // TX_16x16
43   0x1111111111111111ULL,  // TX_32x32
44 };
45
46 // 64 bit masks for above transform size. Each 1 represents a position where
47 // we should apply a loop filter across the top border of an 8x8 block
48 // boundary.
49 //
50 // In the case of TX_32x32 ->  ( in low order byte first we end up with
51 // a mask that looks like this
52 //
53 //    11111111
54 //    00000000
55 //    00000000
56 //    00000000
57 //    11111111
58 //    00000000
59 //    00000000
60 //    00000000
61 //
62 // A loopfilter should be applied to every other 4 the row vertically.
63 static const uint64_t above_64x64_txform_mask[TX_SIZES]= {
64   0xffffffffffffffffULL,  // TX_4X4
65   0xffffffffffffffffULL,  // TX_8x8
66   0x00ff00ff00ff00ffULL,  // TX_16x16
67   0x000000ff000000ffULL,  // TX_32x32
68 };
69
70 // 64 bit masks for prediction sizes (left). Each 1 represents a position
71 // where left border of an 8x8 block. These are aligned to the right most
72 // appropriate bit, and then shifted into place.
73 //
74 // In the case of TX_16x32 ->  ( low order byte first ) we end up with
75 // a mask that looks like this :
76 //
77 //  10000000
78 //  10000000
79 //  10000000
80 //  10000000
81 //  00000000
82 //  00000000
83 //  00000000
84 //  00000000
85 static const uint64_t left_prediction_mask[BLOCK_SIZES] = {
86   0x0000000000000001ULL,  // BLOCK_4X4,
87   0x0000000000000001ULL,  // BLOCK_4X8,
88   0x0000000000000001ULL,  // BLOCK_8X4,
89   0x0000000000000001ULL,  // BLOCK_8X8,
90   0x0000000000000101ULL,  // BLOCK_8X16,
91   0x0000000000000001ULL,  // BLOCK_16X8,
92   0x0000000000000101ULL,  // BLOCK_16X16,
93   0x0000000001010101ULL,  // BLOCK_16X32,
94   0x0000000000000101ULL,  // BLOCK_32X16,
95   0x0000000001010101ULL,  // BLOCK_32X32,
96   0x0101010101010101ULL,  // BLOCK_32X64,
97   0x0000000001010101ULL,  // BLOCK_64X32,
98   0x0101010101010101ULL,  // BLOCK_64X64
99 };
100
101 // 64 bit mask to shift and set for each prediction size.
102 static const uint64_t above_prediction_mask[BLOCK_SIZES] = {
103   0x0000000000000001ULL,  // BLOCK_4X4
104   0x0000000000000001ULL,  // BLOCK_4X8
105   0x0000000000000001ULL,  // BLOCK_8X4
106   0x0000000000000001ULL,  // BLOCK_8X8
107   0x0000000000000001ULL,  // BLOCK_8X16,
108   0x0000000000000003ULL,  // BLOCK_16X8
109   0x0000000000000003ULL,  // BLOCK_16X16
110   0x0000000000000003ULL,  // BLOCK_16X32,
111   0x000000000000000fULL,  // BLOCK_32X16,
112   0x000000000000000fULL,  // BLOCK_32X32,
113   0x000000000000000fULL,  // BLOCK_32X64,
114   0x00000000000000ffULL,  // BLOCK_64X32,
115   0x00000000000000ffULL,  // BLOCK_64X64
116 };
117 // 64 bit mask to shift and set for each prediction size. A bit is set for
118 // each 8x8 block that would be in the left most block of the given block
119 // size in the 64x64 block.
120 static const uint64_t size_mask[BLOCK_SIZES] = {
121   0x0000000000000001ULL,  // BLOCK_4X4
122   0x0000000000000001ULL,  // BLOCK_4X8
123   0x0000000000000001ULL,  // BLOCK_8X4
124   0x0000000000000001ULL,  // BLOCK_8X8
125   0x0000000000000101ULL,  // BLOCK_8X16,
126   0x0000000000000003ULL,  // BLOCK_16X8
127   0x0000000000000303ULL,  // BLOCK_16X16
128   0x0000000003030303ULL,  // BLOCK_16X32,
129   0x0000000000000f0fULL,  // BLOCK_32X16,
130   0x000000000f0f0f0fULL,  // BLOCK_32X32,
131   0x0f0f0f0f0f0f0f0fULL,  // BLOCK_32X64,
132   0x00000000ffffffffULL,  // BLOCK_64X32,
133   0xffffffffffffffffULL,  // BLOCK_64X64
134 };
135
136 // These are used for masking the left and above borders.
137 static const uint64_t left_border =  0x1111111111111111ULL;
138 static const uint64_t above_border = 0x000000ff000000ffULL;
139
140 // 16 bit masks for uv transform sizes.
141 static const uint16_t left_64x64_txform_mask_uv[TX_SIZES]= {
142   0xffff,  // TX_4X4
143   0xffff,  // TX_8x8
144   0x5555,  // TX_16x16
145   0x1111,  // TX_32x32
146 };
147
148 static const uint16_t above_64x64_txform_mask_uv[TX_SIZES]= {
149   0xffff,  // TX_4X4
150   0xffff,  // TX_8x8
151   0x0f0f,  // TX_16x16
152   0x000f,  // TX_32x32
153 };
154
155 // 16 bit left mask to shift and set for each uv prediction size.
156 static const uint16_t left_prediction_mask_uv[BLOCK_SIZES] = {
157   0x0001,  // BLOCK_4X4,
158   0x0001,  // BLOCK_4X8,
159   0x0001,  // BLOCK_8X4,
160   0x0001,  // BLOCK_8X8,
161   0x0001,  // BLOCK_8X16,
162   0x0001,  // BLOCK_16X8,
163   0x0001,  // BLOCK_16X16,
164   0x0011,  // BLOCK_16X32,
165   0x0001,  // BLOCK_32X16,
166   0x0011,  // BLOCK_32X32,
167   0x1111,  // BLOCK_32X64
168   0x0011,  // BLOCK_64X32,
169   0x1111,  // BLOCK_64X64
170 };
171 // 16 bit above mask to shift and set for uv each prediction size.
172 static const uint16_t above_prediction_mask_uv[BLOCK_SIZES] = {
173   0x0001,  // BLOCK_4X4
174   0x0001,  // BLOCK_4X8
175   0x0001,  // BLOCK_8X4
176   0x0001,  // BLOCK_8X8
177   0x0001,  // BLOCK_8X16,
178   0x0001,  // BLOCK_16X8
179   0x0001,  // BLOCK_16X16
180   0x0001,  // BLOCK_16X32,
181   0x0003,  // BLOCK_32X16,
182   0x0003,  // BLOCK_32X32,
183   0x0003,  // BLOCK_32X64,
184   0x000f,  // BLOCK_64X32,
185   0x000f,  // BLOCK_64X64
186 };
187
188 // 64 bit mask to shift and set for each uv prediction size
189 static const uint16_t size_mask_uv[BLOCK_SIZES] = {
190   0x0001,  // BLOCK_4X4
191   0x0001,  // BLOCK_4X8
192   0x0001,  // BLOCK_8X4
193   0x0001,  // BLOCK_8X8
194   0x0001,  // BLOCK_8X16,
195   0x0001,  // BLOCK_16X8
196   0x0001,  // BLOCK_16X16
197   0x0011,  // BLOCK_16X32,
198   0x0003,  // BLOCK_32X16,
199   0x0033,  // BLOCK_32X32,
200   0x3333,  // BLOCK_32X64,
201   0x00ff,  // BLOCK_64X32,
202   0xffff,  // BLOCK_64X64
203 };
204 static const uint16_t left_border_uv =  0x1111;
205 static const uint16_t above_border_uv = 0x000f;
206
207 static const int mode_lf_lut[MB_MODE_COUNT] = {
208   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // INTRA_MODES
209   1, 1, 0, 1                     // INTER_MODES (ZEROMV == 0)
210 };
211
212 static void update_sharpness(loop_filter_info_n *lfi, int sharpness_lvl) {
213   int lvl;
214
215   // For each possible value for the loop filter fill out limits
216   for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++) {
217     // Set loop filter parameters that control sharpness.
218     int block_inside_limit = lvl >> ((sharpness_lvl > 0) + (sharpness_lvl > 4));
219
220     if (sharpness_lvl > 0) {
221       if (block_inside_limit > (9 - sharpness_lvl))
222         block_inside_limit = (9 - sharpness_lvl);
223     }
224
225     if (block_inside_limit < 1)
226       block_inside_limit = 1;
227
228     memset(lfi->lfthr[lvl].lim, block_inside_limit, SIMD_WIDTH);
229     memset(lfi->lfthr[lvl].mblim, (2 * (lvl + 2) + block_inside_limit),
230            SIMD_WIDTH);
231   }
232 }
233
234 static uint8_t get_filter_level(const loop_filter_info_n *lfi_n,
235                                 const MODE_INFO *mi) {
236   return lfi_n->lvl[mi->segment_id][mi->ref_frame[0]]
237                    [mode_lf_lut[mi->mode]];
238 }
239
240 void vp9_loop_filter_init(VP9_COMMON *cm) {
241   loop_filter_info_n *lfi = &cm->lf_info;
242   struct loopfilter *lf = &cm->lf;
243   int lvl;
244
245   // init limits for given sharpness
246   update_sharpness(lfi, lf->sharpness_level);
247   lf->last_sharpness_level = lf->sharpness_level;
248
249   // init hev threshold const vectors
250   for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++)
251     memset(lfi->lfthr[lvl].hev_thr, (lvl >> 4), SIMD_WIDTH);
252 }
253
254 void vp9_loop_filter_frame_init(VP9_COMMON *cm, int default_filt_lvl) {
255   int seg_id;
256   // n_shift is the multiplier for lf_deltas
257   // the multiplier is 1 for when filter_lvl is between 0 and 31;
258   // 2 when filter_lvl is between 32 and 63
259   const int scale = 1 << (default_filt_lvl >> 5);
260   loop_filter_info_n *const lfi = &cm->lf_info;
261   struct loopfilter *const lf = &cm->lf;
262   const struct segmentation *const seg = &cm->seg;
263
264   // update limits if sharpness has changed
265   if (lf->last_sharpness_level != lf->sharpness_level) {
266     update_sharpness(lfi, lf->sharpness_level);
267     lf->last_sharpness_level = lf->sharpness_level;
268   }
269
270   for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) {
271     int lvl_seg = default_filt_lvl;
272     if (segfeature_active(seg, seg_id, SEG_LVL_ALT_LF)) {
273       const int data = get_segdata(seg, seg_id, SEG_LVL_ALT_LF);
274       lvl_seg = clamp(seg->abs_delta == SEGMENT_ABSDATA ?
275                       data : default_filt_lvl + data,
276                       0, MAX_LOOP_FILTER);
277     }
278
279     if (!lf->mode_ref_delta_enabled) {
280       // we could get rid of this if we assume that deltas are set to
281       // zero when not in use; encoder always uses deltas
282       memset(lfi->lvl[seg_id], lvl_seg, sizeof(lfi->lvl[seg_id]));
283     } else {
284       int ref, mode;
285       const int intra_lvl = lvl_seg + lf->ref_deltas[INTRA_FRAME] * scale;
286       lfi->lvl[seg_id][INTRA_FRAME][0] = clamp(intra_lvl, 0, MAX_LOOP_FILTER);
287
288       for (ref = LAST_FRAME; ref < MAX_REF_FRAMES; ++ref) {
289         for (mode = 0; mode < MAX_MODE_LF_DELTAS; ++mode) {
290           const int inter_lvl = lvl_seg + lf->ref_deltas[ref] * scale
291                                         + lf->mode_deltas[mode] * scale;
292           lfi->lvl[seg_id][ref][mode] = clamp(inter_lvl, 0, MAX_LOOP_FILTER);
293         }
294       }
295     }
296   }
297 }
298
299 static void filter_selectively_vert_row2(int subsampling_factor,
300                                          uint8_t *s, int pitch,
301                                          unsigned int mask_16x16_l,
302                                          unsigned int mask_8x8_l,
303                                          unsigned int mask_4x4_l,
304                                          unsigned int mask_4x4_int_l,
305                                          const loop_filter_info_n *lfi_n,
306                                          const uint8_t *lfl) {
307   const int mask_shift = subsampling_factor ? 4 : 8;
308   const int mask_cutoff = subsampling_factor ? 0xf : 0xff;
309   const int lfl_forward = subsampling_factor ? 4 : 8;
310
311   unsigned int mask_16x16_0 = mask_16x16_l & mask_cutoff;
312   unsigned int mask_8x8_0 = mask_8x8_l & mask_cutoff;
313   unsigned int mask_4x4_0 = mask_4x4_l & mask_cutoff;
314   unsigned int mask_4x4_int_0 = mask_4x4_int_l & mask_cutoff;
315   unsigned int mask_16x16_1 = (mask_16x16_l >> mask_shift) & mask_cutoff;
316   unsigned int mask_8x8_1 = (mask_8x8_l >> mask_shift) & mask_cutoff;
317   unsigned int mask_4x4_1 = (mask_4x4_l >> mask_shift) & mask_cutoff;
318   unsigned int mask_4x4_int_1 = (mask_4x4_int_l >> mask_shift) & mask_cutoff;
319   unsigned int mask;
320
321   for (mask = mask_16x16_0 | mask_8x8_0 | mask_4x4_0 | mask_4x4_int_0 |
322               mask_16x16_1 | mask_8x8_1 | mask_4x4_1 | mask_4x4_int_1;
323        mask; mask >>= 1) {
324     const loop_filter_thresh *lfi0 = lfi_n->lfthr + *lfl;
325     const loop_filter_thresh *lfi1 = lfi_n->lfthr + *(lfl + lfl_forward);
326
327     // TODO(yunqingwang): count in loopfilter functions should be removed.
328     if (mask & 1) {
329       if ((mask_16x16_0 | mask_16x16_1) & 1) {
330         if ((mask_16x16_0 & mask_16x16_1) & 1) {
331           vpx_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
332                                    lfi0->hev_thr);
333         } else if (mask_16x16_0 & 1) {
334           vpx_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim,
335                               lfi0->hev_thr);
336         } else {
337           vpx_lpf_vertical_16(s + 8 *pitch, pitch, lfi1->mblim,
338                               lfi1->lim, lfi1->hev_thr);
339         }
340       }
341
342       if ((mask_8x8_0 | mask_8x8_1) & 1) {
343         if ((mask_8x8_0 & mask_8x8_1) & 1) {
344           vpx_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
345                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
346                                   lfi1->hev_thr);
347         } else if (mask_8x8_0 & 1) {
348           vpx_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr);
349         } else {
350           vpx_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
351                              lfi1->hev_thr);
352         }
353       }
354
355       if ((mask_4x4_0 | mask_4x4_1) & 1) {
356         if ((mask_4x4_0 & mask_4x4_1) & 1) {
357           vpx_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
358                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
359                                   lfi1->hev_thr);
360         } else if (mask_4x4_0 & 1) {
361           vpx_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr);
362         } else {
363           vpx_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
364                              lfi1->hev_thr);
365         }
366       }
367
368       if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
369         if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
370           vpx_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
371                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
372                                   lfi1->hev_thr);
373         } else if (mask_4x4_int_0 & 1) {
374           vpx_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
375                              lfi0->hev_thr);
376         } else {
377           vpx_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim, lfi1->lim,
378                              lfi1->hev_thr);
379         }
380       }
381     }
382
383     s += 8;
384     lfl += 1;
385     mask_16x16_0 >>= 1;
386     mask_8x8_0 >>= 1;
387     mask_4x4_0 >>= 1;
388     mask_4x4_int_0 >>= 1;
389     mask_16x16_1 >>= 1;
390     mask_8x8_1 >>= 1;
391     mask_4x4_1 >>= 1;
392     mask_4x4_int_1 >>= 1;
393   }
394 }
395
396 #if CONFIG_VP9_HIGHBITDEPTH
397 static void highbd_filter_selectively_vert_row2(int subsampling_factor,
398                                                 uint16_t *s, int pitch,
399                                                 unsigned int mask_16x16_l,
400                                                 unsigned int mask_8x8_l,
401                                                 unsigned int mask_4x4_l,
402                                                 unsigned int mask_4x4_int_l,
403                                                 const loop_filter_info_n *lfi_n,
404                                                 const uint8_t *lfl, int bd) {
405   const int mask_shift = subsampling_factor ? 4 : 8;
406   const int mask_cutoff = subsampling_factor ? 0xf : 0xff;
407   const int lfl_forward = subsampling_factor ? 4 : 8;
408
409   unsigned int mask_16x16_0 = mask_16x16_l & mask_cutoff;
410   unsigned int mask_8x8_0 = mask_8x8_l & mask_cutoff;
411   unsigned int mask_4x4_0 = mask_4x4_l & mask_cutoff;
412   unsigned int mask_4x4_int_0 = mask_4x4_int_l & mask_cutoff;
413   unsigned int mask_16x16_1 = (mask_16x16_l >> mask_shift) & mask_cutoff;
414   unsigned int mask_8x8_1 = (mask_8x8_l >> mask_shift) & mask_cutoff;
415   unsigned int mask_4x4_1 = (mask_4x4_l >> mask_shift) & mask_cutoff;
416   unsigned int mask_4x4_int_1 = (mask_4x4_int_l >> mask_shift) & mask_cutoff;
417   unsigned int mask;
418
419   for (mask = mask_16x16_0 | mask_8x8_0 | mask_4x4_0 | mask_4x4_int_0 |
420        mask_16x16_1 | mask_8x8_1 | mask_4x4_1 | mask_4x4_int_1;
421        mask; mask >>= 1) {
422     const loop_filter_thresh *lfi0 = lfi_n->lfthr + *lfl;
423     const loop_filter_thresh *lfi1 = lfi_n->lfthr + *(lfl + lfl_forward);
424
425     // TODO(yunqingwang): count in loopfilter functions should be removed.
426     if (mask & 1) {
427       if ((mask_16x16_0 | mask_16x16_1) & 1) {
428         if ((mask_16x16_0 & mask_16x16_1) & 1) {
429           vpx_highbd_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
430                                           lfi0->hev_thr, bd);
431         } else if (mask_16x16_0 & 1) {
432           vpx_highbd_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim,
433                                      lfi0->hev_thr, bd);
434         } else {
435           vpx_highbd_lpf_vertical_16(s + 8 *pitch, pitch, lfi1->mblim,
436                                      lfi1->lim, lfi1->hev_thr, bd);
437         }
438       }
439
440       if ((mask_8x8_0 | mask_8x8_1) & 1) {
441         if ((mask_8x8_0 & mask_8x8_1) & 1) {
442           vpx_highbd_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
443                                          lfi0->hev_thr, lfi1->mblim, lfi1->lim,
444                                          lfi1->hev_thr, bd);
445         } else if (mask_8x8_0 & 1) {
446           vpx_highbd_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim,
447                                     lfi0->hev_thr, bd);
448         } else {
449           vpx_highbd_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim,
450                                     lfi1->lim, lfi1->hev_thr, bd);
451         }
452       }
453
454       if ((mask_4x4_0 | mask_4x4_1) & 1) {
455         if ((mask_4x4_0 & mask_4x4_1) & 1) {
456           vpx_highbd_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
457                                          lfi0->hev_thr, lfi1->mblim, lfi1->lim,
458                                          lfi1->hev_thr, bd);
459         } else if (mask_4x4_0 & 1) {
460           vpx_highbd_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim,
461                                     lfi0->hev_thr, bd);
462         } else {
463           vpx_highbd_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim,
464                                     lfi1->lim, lfi1->hev_thr, bd);
465         }
466       }
467
468       if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
469         if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
470           vpx_highbd_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
471                                          lfi0->hev_thr, lfi1->mblim, lfi1->lim,
472                                          lfi1->hev_thr, bd);
473         } else if (mask_4x4_int_0 & 1) {
474           vpx_highbd_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
475                                     lfi0->hev_thr, bd);
476         } else {
477           vpx_highbd_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim,
478                                     lfi1->lim, lfi1->hev_thr, bd);
479         }
480       }
481     }
482
483     s += 8;
484     lfl += 1;
485     mask_16x16_0 >>= 1;
486     mask_8x8_0 >>= 1;
487     mask_4x4_0 >>= 1;
488     mask_4x4_int_0 >>= 1;
489     mask_16x16_1 >>= 1;
490     mask_8x8_1 >>= 1;
491     mask_4x4_1 >>= 1;
492     mask_4x4_int_1 >>= 1;
493   }
494 }
495 #endif  // CONFIG_VP9_HIGHBITDEPTH
496
497 static void filter_selectively_horiz(uint8_t *s, int pitch,
498                                      unsigned int mask_16x16,
499                                      unsigned int mask_8x8,
500                                      unsigned int mask_4x4,
501                                      unsigned int mask_4x4_int,
502                                      const loop_filter_info_n *lfi_n,
503                                      const uint8_t *lfl) {
504   unsigned int mask;
505   int count;
506
507   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
508        mask; mask >>= count) {
509     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
510
511     count = 1;
512     if (mask & 1) {
513       if (mask_16x16 & 1) {
514         if ((mask_16x16 & 3) == 3) {
515           vpx_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
516                                 lfi->hev_thr, 2);
517           count = 2;
518         } else {
519           vpx_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
520                                 lfi->hev_thr, 1);
521         }
522       } else if (mask_8x8 & 1) {
523         if ((mask_8x8 & 3) == 3) {
524           // Next block's thresholds.
525           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
526
527           vpx_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
528                                     lfi->hev_thr, lfin->mblim, lfin->lim,
529                                     lfin->hev_thr);
530
531           if ((mask_4x4_int & 3) == 3) {
532             vpx_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
533                                       lfi->lim, lfi->hev_thr, lfin->mblim,
534                                       lfin->lim, lfin->hev_thr);
535           } else {
536             if (mask_4x4_int & 1)
537               vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
538                                    lfi->hev_thr);
539             else if (mask_4x4_int & 2)
540               vpx_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
541                                    lfin->lim, lfin->hev_thr);
542           }
543           count = 2;
544         } else {
545           vpx_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
546
547           if (mask_4x4_int & 1)
548             vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
549                                  lfi->hev_thr);
550         }
551       } else if (mask_4x4 & 1) {
552         if ((mask_4x4 & 3) == 3) {
553           // Next block's thresholds.
554           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
555
556           vpx_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
557                                     lfi->hev_thr, lfin->mblim, lfin->lim,
558                                     lfin->hev_thr);
559           if ((mask_4x4_int & 3) == 3) {
560             vpx_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
561                                       lfi->lim, lfi->hev_thr, lfin->mblim,
562                                       lfin->lim, lfin->hev_thr);
563           } else {
564             if (mask_4x4_int & 1)
565               vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
566                                    lfi->hev_thr);
567             else if (mask_4x4_int & 2)
568               vpx_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
569                                    lfin->lim, lfin->hev_thr);
570           }
571           count = 2;
572         } else {
573           vpx_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
574
575           if (mask_4x4_int & 1)
576             vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
577                                  lfi->hev_thr);
578         }
579       } else if (mask_4x4_int & 1) {
580         vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
581                              lfi->hev_thr);
582       }
583     }
584     s += 8 * count;
585     lfl += count;
586     mask_16x16 >>= count;
587     mask_8x8 >>= count;
588     mask_4x4 >>= count;
589     mask_4x4_int >>= count;
590   }
591 }
592
593 #if CONFIG_VP9_HIGHBITDEPTH
594 static void highbd_filter_selectively_horiz(uint16_t *s, int pitch,
595                                             unsigned int mask_16x16,
596                                             unsigned int mask_8x8,
597                                             unsigned int mask_4x4,
598                                             unsigned int mask_4x4_int,
599                                             const loop_filter_info_n *lfi_n,
600                                             const uint8_t *lfl, int bd) {
601   unsigned int mask;
602   int count;
603
604   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
605        mask; mask >>= count) {
606     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
607
608     count = 1;
609     if (mask & 1) {
610       if (mask_16x16 & 1) {
611         if ((mask_16x16 & 3) == 3) {
612           vpx_highbd_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
613                                        lfi->hev_thr, 2, bd);
614           count = 2;
615         } else {
616           vpx_highbd_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
617                                        lfi->hev_thr, 1, bd);
618         }
619       } else if (mask_8x8 & 1) {
620         if ((mask_8x8 & 3) == 3) {
621           // Next block's thresholds.
622           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
623
624           vpx_highbd_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
625                                            lfi->hev_thr, lfin->mblim, lfin->lim,
626                                            lfin->hev_thr, bd);
627
628           if ((mask_4x4_int & 3) == 3) {
629             vpx_highbd_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
630                                              lfi->lim, lfi->hev_thr,
631                                              lfin->mblim, lfin->lim,
632                                              lfin->hev_thr, bd);
633           } else {
634             if (mask_4x4_int & 1) {
635               vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
636                                           lfi->lim, lfi->hev_thr, bd);
637             } else if (mask_4x4_int & 2) {
638               vpx_highbd_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
639                                           lfin->lim, lfin->hev_thr, bd);
640             }
641           }
642           count = 2;
643         } else {
644           vpx_highbd_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim,
645                                       lfi->hev_thr, bd);
646
647           if (mask_4x4_int & 1) {
648             vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
649                                         lfi->lim, lfi->hev_thr, bd);
650           }
651         }
652       } else if (mask_4x4 & 1) {
653         if ((mask_4x4 & 3) == 3) {
654           // Next block's thresholds.
655           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
656
657           vpx_highbd_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
658                                            lfi->hev_thr, lfin->mblim, lfin->lim,
659                                            lfin->hev_thr, bd);
660           if ((mask_4x4_int & 3) == 3) {
661             vpx_highbd_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
662                                              lfi->lim, lfi->hev_thr,
663                                              lfin->mblim, lfin->lim,
664                                              lfin->hev_thr, bd);
665           } else {
666             if (mask_4x4_int & 1) {
667               vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
668                                           lfi->lim, lfi->hev_thr, bd);
669             } else if (mask_4x4_int & 2) {
670               vpx_highbd_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
671                                           lfin->lim, lfin->hev_thr, bd);
672             }
673           }
674           count = 2;
675         } else {
676           vpx_highbd_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim,
677                                       lfi->hev_thr, bd);
678
679           if (mask_4x4_int & 1) {
680             vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
681                                         lfi->lim, lfi->hev_thr, bd);
682           }
683         }
684       } else if (mask_4x4_int & 1) {
685         vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
686                                     lfi->hev_thr, bd);
687       }
688     }
689     s += 8 * count;
690     lfl += count;
691     mask_16x16 >>= count;
692     mask_8x8 >>= count;
693     mask_4x4 >>= count;
694     mask_4x4_int >>= count;
695   }
696 }
697 #endif  // CONFIG_VP9_HIGHBITDEPTH
698
699 // This function ors into the current lfm structure, where to do loop
700 // filters for the specific mi we are looking at. It uses information
701 // including the block_size_type (32x16, 32x32, etc.), the transform size,
702 // whether there were any coefficients encoded, and the loop filter strength
703 // block we are currently looking at. Shift is used to position the
704 // 1's we produce.
705 // TODO(JBB) Need another function for different resolution color..
706 static void build_masks(const loop_filter_info_n *const lfi_n,
707                         const MODE_INFO *mi, const int shift_y,
708                         const int shift_uv,
709                         LOOP_FILTER_MASK *lfm) {
710   const BLOCK_SIZE block_size = mi->sb_type;
711   const TX_SIZE tx_size_y = mi->tx_size;
712   const TX_SIZE tx_size_uv = get_uv_tx_size_impl(tx_size_y, block_size, 1, 1);
713   const int filter_level = get_filter_level(lfi_n, mi);
714   uint64_t *const left_y = &lfm->left_y[tx_size_y];
715   uint64_t *const above_y = &lfm->above_y[tx_size_y];
716   uint64_t *const int_4x4_y = &lfm->int_4x4_y;
717   uint16_t *const left_uv = &lfm->left_uv[tx_size_uv];
718   uint16_t *const above_uv = &lfm->above_uv[tx_size_uv];
719   uint16_t *const int_4x4_uv = &lfm->int_4x4_uv;
720   int i;
721
722   // If filter level is 0 we don't loop filter.
723   if (!filter_level) {
724     return;
725   } else {
726     const int w = num_8x8_blocks_wide_lookup[block_size];
727     const int h = num_8x8_blocks_high_lookup[block_size];
728     int index = shift_y;
729     for (i = 0; i < h; i++) {
730       memset(&lfm->lfl_y[index], filter_level, w);
731       index += 8;
732     }
733   }
734
735   // These set 1 in the current block size for the block size edges.
736   // For instance if the block size is 32x16, we'll set:
737   //    above =   1111
738   //              0000
739   //    and
740   //    left  =   1000
741   //          =   1000
742   // NOTE : In this example the low bit is left most ( 1000 ) is stored as
743   //        1,  not 8...
744   //
745   // U and V set things on a 16 bit scale.
746   //
747   *above_y |= above_prediction_mask[block_size] << shift_y;
748   *above_uv |= above_prediction_mask_uv[block_size] << shift_uv;
749   *left_y |= left_prediction_mask[block_size] << shift_y;
750   *left_uv |= left_prediction_mask_uv[block_size] << shift_uv;
751
752   // If the block has no coefficients and is not intra we skip applying
753   // the loop filter on block edges.
754   if (mi->skip && is_inter_block(mi))
755     return;
756
757   // Here we are adding a mask for the transform size. The transform
758   // size mask is set to be correct for a 64x64 prediction block size. We
759   // mask to match the size of the block we are working on and then shift it
760   // into place..
761   *above_y |= (size_mask[block_size] &
762                above_64x64_txform_mask[tx_size_y]) << shift_y;
763   *above_uv |= (size_mask_uv[block_size] &
764                 above_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
765
766   *left_y |= (size_mask[block_size] &
767               left_64x64_txform_mask[tx_size_y]) << shift_y;
768   *left_uv |= (size_mask_uv[block_size] &
769                left_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
770
771   // Here we are trying to determine what to do with the internal 4x4 block
772   // boundaries.  These differ from the 4x4 boundaries on the outside edge of
773   // an 8x8 in that the internal ones can be skipped and don't depend on
774   // the prediction block size.
775   if (tx_size_y == TX_4X4)
776     *int_4x4_y |= size_mask[block_size] << shift_y;
777
778   if (tx_size_uv == TX_4X4)
779     *int_4x4_uv |= (size_mask_uv[block_size] & 0xffff) << shift_uv;
780 }
781
782 // This function does the same thing as the one above with the exception that
783 // it only affects the y masks. It exists because for blocks < 16x16 in size,
784 // we only update u and v masks on the first block.
785 static void build_y_mask(const loop_filter_info_n *const lfi_n,
786                          const MODE_INFO *mi, const int shift_y,
787                          LOOP_FILTER_MASK *lfm) {
788   const BLOCK_SIZE block_size = mi->sb_type;
789   const TX_SIZE tx_size_y = mi->tx_size;
790   const int filter_level = get_filter_level(lfi_n, mi);
791   uint64_t *const left_y = &lfm->left_y[tx_size_y];
792   uint64_t *const above_y = &lfm->above_y[tx_size_y];
793   uint64_t *const int_4x4_y = &lfm->int_4x4_y;
794   int i;
795
796   if (!filter_level) {
797     return;
798   } else {
799     const int w = num_8x8_blocks_wide_lookup[block_size];
800     const int h = num_8x8_blocks_high_lookup[block_size];
801     int index = shift_y;
802     for (i = 0; i < h; i++) {
803       memset(&lfm->lfl_y[index], filter_level, w);
804       index += 8;
805     }
806   }
807
808   *above_y |= above_prediction_mask[block_size] << shift_y;
809   *left_y |= left_prediction_mask[block_size] << shift_y;
810
811   if (mi->skip && is_inter_block(mi))
812     return;
813
814   *above_y |= (size_mask[block_size] &
815                above_64x64_txform_mask[tx_size_y]) << shift_y;
816
817   *left_y |= (size_mask[block_size] &
818               left_64x64_txform_mask[tx_size_y]) << shift_y;
819
820   if (tx_size_y == TX_4X4)
821     *int_4x4_y |= size_mask[block_size] << shift_y;
822 }
823
824 void vp9_adjust_mask(VP9_COMMON *const cm, const int mi_row,
825                      const int mi_col, LOOP_FILTER_MASK *lfm) {
826   int i;
827
828   // The largest loopfilter we have is 16x16 so we use the 16x16 mask
829   // for 32x32 transforms also.
830   lfm->left_y[TX_16X16] |= lfm->left_y[TX_32X32];
831   lfm->above_y[TX_16X16] |= lfm->above_y[TX_32X32];
832   lfm->left_uv[TX_16X16] |= lfm->left_uv[TX_32X32];
833   lfm->above_uv[TX_16X16] |= lfm->above_uv[TX_32X32];
834
835   // We do at least 8 tap filter on every 32x32 even if the transform size
836   // is 4x4. So if the 4x4 is set on a border pixel add it to the 8x8 and
837   // remove it from the 4x4.
838   lfm->left_y[TX_8X8] |= lfm->left_y[TX_4X4] & left_border;
839   lfm->left_y[TX_4X4] &= ~left_border;
840   lfm->above_y[TX_8X8] |= lfm->above_y[TX_4X4] & above_border;
841   lfm->above_y[TX_4X4] &= ~above_border;
842   lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_4X4] & left_border_uv;
843   lfm->left_uv[TX_4X4] &= ~left_border_uv;
844   lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_4X4] & above_border_uv;
845   lfm->above_uv[TX_4X4] &= ~above_border_uv;
846
847   // We do some special edge handling.
848   if (mi_row + MI_BLOCK_SIZE > cm->mi_rows) {
849     const uint64_t rows = cm->mi_rows - mi_row;
850
851     // Each pixel inside the border gets a 1,
852     const uint64_t mask_y = (((uint64_t) 1 << (rows << 3)) - 1);
853     const uint16_t mask_uv = (((uint16_t) 1 << (((rows + 1) >> 1) << 2)) - 1);
854
855     // Remove values completely outside our border.
856     for (i = 0; i < TX_32X32; i++) {
857       lfm->left_y[i] &= mask_y;
858       lfm->above_y[i] &= mask_y;
859       lfm->left_uv[i] &= mask_uv;
860       lfm->above_uv[i] &= mask_uv;
861     }
862     lfm->int_4x4_y &= mask_y;
863     lfm->int_4x4_uv &= mask_uv;
864
865     // We don't apply a wide loop filter on the last uv block row. If set
866     // apply the shorter one instead.
867     if (rows == 1) {
868       lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16];
869       lfm->above_uv[TX_16X16] = 0;
870     }
871     if (rows == 5) {
872       lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16] & 0xff00;
873       lfm->above_uv[TX_16X16] &= ~(lfm->above_uv[TX_16X16] & 0xff00);
874     }
875   }
876
877   if (mi_col + MI_BLOCK_SIZE > cm->mi_cols) {
878     const uint64_t columns = cm->mi_cols - mi_col;
879
880     // Each pixel inside the border gets a 1, the multiply copies the border
881     // to where we need it.
882     const uint64_t mask_y  = (((1 << columns) - 1)) * 0x0101010101010101ULL;
883     const uint16_t mask_uv = ((1 << ((columns + 1) >> 1)) - 1) * 0x1111;
884
885     // Internal edges are not applied on the last column of the image so
886     // we mask 1 more for the internal edges
887     const uint16_t mask_uv_int = ((1 << (columns >> 1)) - 1) * 0x1111;
888
889     // Remove the bits outside the image edge.
890     for (i = 0; i < TX_32X32; i++) {
891       lfm->left_y[i] &= mask_y;
892       lfm->above_y[i] &= mask_y;
893       lfm->left_uv[i] &= mask_uv;
894       lfm->above_uv[i] &= mask_uv;
895     }
896     lfm->int_4x4_y &= mask_y;
897     lfm->int_4x4_uv &= mask_uv_int;
898
899     // We don't apply a wide loop filter on the last uv column. If set
900     // apply the shorter one instead.
901     if (columns == 1) {
902       lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_16X16];
903       lfm->left_uv[TX_16X16] = 0;
904     }
905     if (columns == 5) {
906       lfm->left_uv[TX_8X8] |= (lfm->left_uv[TX_16X16] & 0xcccc);
907       lfm->left_uv[TX_16X16] &= ~(lfm->left_uv[TX_16X16] & 0xcccc);
908     }
909   }
910   // We don't apply a loop filter on the first column in the image, mask that
911   // out.
912   if (mi_col == 0) {
913     for (i = 0; i < TX_32X32; i++) {
914       lfm->left_y[i] &= 0xfefefefefefefefeULL;
915       lfm->left_uv[i] &= 0xeeee;
916     }
917   }
918
919   // Assert if we try to apply 2 different loop filters at the same position.
920   assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_8X8]));
921   assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_4X4]));
922   assert(!(lfm->left_y[TX_8X8] & lfm->left_y[TX_4X4]));
923   assert(!(lfm->int_4x4_y & lfm->left_y[TX_16X16]));
924   assert(!(lfm->left_uv[TX_16X16]&lfm->left_uv[TX_8X8]));
925   assert(!(lfm->left_uv[TX_16X16] & lfm->left_uv[TX_4X4]));
926   assert(!(lfm->left_uv[TX_8X8] & lfm->left_uv[TX_4X4]));
927   assert(!(lfm->int_4x4_uv & lfm->left_uv[TX_16X16]));
928   assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_8X8]));
929   assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_4X4]));
930   assert(!(lfm->above_y[TX_8X8] & lfm->above_y[TX_4X4]));
931   assert(!(lfm->int_4x4_y & lfm->above_y[TX_16X16]));
932   assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_8X8]));
933   assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_4X4]));
934   assert(!(lfm->above_uv[TX_8X8] & lfm->above_uv[TX_4X4]));
935   assert(!(lfm->int_4x4_uv & lfm->above_uv[TX_16X16]));
936 }
937
938 // This function sets up the bit masks for the entire 64x64 region represented
939 // by mi_row, mi_col.
940 // TODO(JBB): This function only works for yv12.
941 void vp9_setup_mask(VP9_COMMON *const cm, const int mi_row, const int mi_col,
942                     MODE_INFO **mi, const int mode_info_stride,
943                     LOOP_FILTER_MASK *lfm) {
944   int idx_32, idx_16, idx_8;
945   const loop_filter_info_n *const lfi_n = &cm->lf_info;
946   MODE_INFO **mip = mi;
947   MODE_INFO **mip2 = mi;
948
949   // These are offsets to the next mi in the 64x64 block. It is what gets
950   // added to the mi ptr as we go through each loop. It helps us to avoid
951   // setting up special row and column counters for each index. The last step
952   // brings us out back to the starting position.
953   const int offset_32[] = {4, (mode_info_stride << 2) - 4, 4,
954                            -(mode_info_stride << 2) - 4};
955   const int offset_16[] = {2, (mode_info_stride << 1) - 2, 2,
956                            -(mode_info_stride << 1) - 2};
957   const int offset[] = {1, mode_info_stride - 1, 1, -mode_info_stride - 1};
958
959   // Following variables represent shifts to position the current block
960   // mask over the appropriate block. A shift of 36 to the left will move
961   // the bits for the final 32 by 32 block in the 64x64 up 4 rows and left
962   // 4 rows to the appropriate spot.
963   const int shift_32_y[] = {0, 4, 32, 36};
964   const int shift_16_y[] = {0, 2, 16, 18};
965   const int shift_8_y[] = {0, 1, 8, 9};
966   const int shift_32_uv[] = {0, 2, 8, 10};
967   const int shift_16_uv[] = {0, 1, 4, 5};
968   const int max_rows = (mi_row + MI_BLOCK_SIZE > cm->mi_rows ?
969                         cm->mi_rows - mi_row : MI_BLOCK_SIZE);
970   const int max_cols = (mi_col + MI_BLOCK_SIZE > cm->mi_cols ?
971                         cm->mi_cols - mi_col : MI_BLOCK_SIZE);
972
973   vp9_zero(*lfm);
974   assert(mip[0] != NULL);
975
976   // TODO(jimbankoski): Try moving most of the following code into decode
977   // loop and storing lfm in the mbmi structure so that we don't have to go
978   // through the recursive loop structure multiple times.
979   switch (mip[0]->sb_type) {
980     case BLOCK_64X64:
981       build_masks(lfi_n, mip[0] , 0, 0, lfm);
982       break;
983     case BLOCK_64X32:
984       build_masks(lfi_n, mip[0], 0, 0, lfm);
985       mip2 = mip + mode_info_stride * 4;
986       if (4 >= max_rows)
987         break;
988       build_masks(lfi_n, mip2[0], 32, 8, lfm);
989       break;
990     case BLOCK_32X64:
991       build_masks(lfi_n, mip[0], 0, 0, lfm);
992       mip2 = mip + 4;
993       if (4 >= max_cols)
994         break;
995       build_masks(lfi_n, mip2[0], 4, 2, lfm);
996       break;
997     default:
998       for (idx_32 = 0; idx_32 < 4; mip += offset_32[idx_32], ++idx_32) {
999         const int shift_y = shift_32_y[idx_32];
1000         const int shift_uv = shift_32_uv[idx_32];
1001         const int mi_32_col_offset = ((idx_32 & 1) << 2);
1002         const int mi_32_row_offset = ((idx_32 >> 1) << 2);
1003         if (mi_32_col_offset >= max_cols || mi_32_row_offset >= max_rows)
1004           continue;
1005         switch (mip[0]->sb_type) {
1006           case BLOCK_32X32:
1007             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
1008             break;
1009           case BLOCK_32X16:
1010             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
1011             if (mi_32_row_offset + 2 >= max_rows)
1012               continue;
1013             mip2 = mip + mode_info_stride * 2;
1014             build_masks(lfi_n, mip2[0], shift_y + 16, shift_uv + 4, lfm);
1015             break;
1016           case BLOCK_16X32:
1017             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
1018             if (mi_32_col_offset + 2 >= max_cols)
1019               continue;
1020             mip2 = mip + 2;
1021             build_masks(lfi_n, mip2[0], shift_y + 2, shift_uv + 1, lfm);
1022             break;
1023           default:
1024             for (idx_16 = 0; idx_16 < 4; mip += offset_16[idx_16], ++idx_16) {
1025               const int shift_y = shift_32_y[idx_32] + shift_16_y[idx_16];
1026               const int shift_uv = shift_32_uv[idx_32] + shift_16_uv[idx_16];
1027               const int mi_16_col_offset = mi_32_col_offset +
1028                   ((idx_16 & 1) << 1);
1029               const int mi_16_row_offset = mi_32_row_offset +
1030                   ((idx_16 >> 1) << 1);
1031
1032               if (mi_16_col_offset >= max_cols || mi_16_row_offset >= max_rows)
1033                 continue;
1034
1035               switch (mip[0]->sb_type) {
1036                 case BLOCK_16X16:
1037                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
1038                   break;
1039                 case BLOCK_16X8:
1040                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
1041                   if (mi_16_row_offset + 1 >= max_rows)
1042                     continue;
1043                   mip2 = mip + mode_info_stride;
1044                   build_y_mask(lfi_n, mip2[0], shift_y+8, lfm);
1045                   break;
1046                 case BLOCK_8X16:
1047                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
1048                   if (mi_16_col_offset +1 >= max_cols)
1049                     continue;
1050                   mip2 = mip + 1;
1051                   build_y_mask(lfi_n, mip2[0], shift_y+1, lfm);
1052                   break;
1053                 default: {
1054                   const int shift_y = shift_32_y[idx_32] +
1055                                       shift_16_y[idx_16] +
1056                                       shift_8_y[0];
1057                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
1058                   mip += offset[0];
1059                   for (idx_8 = 1; idx_8 < 4; mip += offset[idx_8], ++idx_8) {
1060                     const int shift_y = shift_32_y[idx_32] +
1061                                         shift_16_y[idx_16] +
1062                                         shift_8_y[idx_8];
1063                     const int mi_8_col_offset = mi_16_col_offset +
1064                         ((idx_8 & 1));
1065                     const int mi_8_row_offset = mi_16_row_offset +
1066                         ((idx_8 >> 1));
1067
1068                     if (mi_8_col_offset >= max_cols ||
1069                         mi_8_row_offset >= max_rows)
1070                       continue;
1071                     build_y_mask(lfi_n, mip[0], shift_y, lfm);
1072                   }
1073                   break;
1074                 }
1075               }
1076             }
1077             break;
1078         }
1079       }
1080       break;
1081   }
1082
1083   vp9_adjust_mask(cm, mi_row, mi_col, lfm);
1084 }
1085
1086 static void filter_selectively_vert(uint8_t *s, int pitch,
1087                                     unsigned int mask_16x16,
1088                                     unsigned int mask_8x8,
1089                                     unsigned int mask_4x4,
1090                                     unsigned int mask_4x4_int,
1091                                     const loop_filter_info_n *lfi_n,
1092                                     const uint8_t *lfl) {
1093   unsigned int mask;
1094
1095   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
1096        mask; mask >>= 1) {
1097     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
1098
1099     if (mask & 1) {
1100       if (mask_16x16 & 1) {
1101         vpx_lpf_vertical_16(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
1102       } else if (mask_8x8 & 1) {
1103         vpx_lpf_vertical_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
1104       } else if (mask_4x4 & 1) {
1105         vpx_lpf_vertical_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
1106       }
1107     }
1108     if (mask_4x4_int & 1)
1109       vpx_lpf_vertical_4(s + 4, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
1110     s += 8;
1111     lfl += 1;
1112     mask_16x16 >>= 1;
1113     mask_8x8 >>= 1;
1114     mask_4x4 >>= 1;
1115     mask_4x4_int >>= 1;
1116   }
1117 }
1118
1119 #if CONFIG_VP9_HIGHBITDEPTH
1120 static void highbd_filter_selectively_vert(uint16_t *s, int pitch,
1121                                            unsigned int mask_16x16,
1122                                            unsigned int mask_8x8,
1123                                            unsigned int mask_4x4,
1124                                            unsigned int mask_4x4_int,
1125                                            const loop_filter_info_n *lfi_n,
1126                                            const uint8_t *lfl, int bd) {
1127   unsigned int mask;
1128
1129   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
1130        mask; mask >>= 1) {
1131     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
1132
1133     if (mask & 1) {
1134       if (mask_16x16 & 1) {
1135         vpx_highbd_lpf_vertical_16(s, pitch, lfi->mblim, lfi->lim,
1136                                    lfi->hev_thr, bd);
1137       } else if (mask_8x8 & 1) {
1138         vpx_highbd_lpf_vertical_8(s, pitch, lfi->mblim, lfi->lim,
1139                                   lfi->hev_thr, bd);
1140       } else if (mask_4x4 & 1) {
1141         vpx_highbd_lpf_vertical_4(s, pitch, lfi->mblim, lfi->lim,
1142                                   lfi->hev_thr, bd);
1143       }
1144     }
1145     if (mask_4x4_int & 1)
1146       vpx_highbd_lpf_vertical_4(s + 4, pitch, lfi->mblim, lfi->lim,
1147                                 lfi->hev_thr, bd);
1148     s += 8;
1149     lfl += 1;
1150     mask_16x16 >>= 1;
1151     mask_8x8 >>= 1;
1152     mask_4x4 >>= 1;
1153     mask_4x4_int >>= 1;
1154   }
1155 }
1156 #endif  // CONFIG_VP9_HIGHBITDEPTH
1157
1158 void vp9_filter_block_plane_non420(VP9_COMMON *cm,
1159                                    struct macroblockd_plane *plane,
1160                                    MODE_INFO **mi_8x8,
1161                                    int mi_row, int mi_col) {
1162   const int ss_x = plane->subsampling_x;
1163   const int ss_y = plane->subsampling_y;
1164   const int row_step = 1 << ss_y;
1165   const int col_step = 1 << ss_x;
1166   const int row_step_stride = cm->mi_stride * row_step;
1167   struct buf_2d *const dst = &plane->dst;
1168   uint8_t* const dst0 = dst->buf;
1169   unsigned int mask_16x16[MI_BLOCK_SIZE] = {0};
1170   unsigned int mask_8x8[MI_BLOCK_SIZE] = {0};
1171   unsigned int mask_4x4[MI_BLOCK_SIZE] = {0};
1172   unsigned int mask_4x4_int[MI_BLOCK_SIZE] = {0};
1173   uint8_t lfl[MI_BLOCK_SIZE * MI_BLOCK_SIZE];
1174   int r, c;
1175
1176   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
1177     unsigned int mask_16x16_c = 0;
1178     unsigned int mask_8x8_c = 0;
1179     unsigned int mask_4x4_c = 0;
1180     unsigned int border_mask;
1181
1182     // Determine the vertical edges that need filtering
1183     for (c = 0; c < MI_BLOCK_SIZE && mi_col + c < cm->mi_cols; c += col_step) {
1184       const MODE_INFO *mi = mi_8x8[c];
1185       const BLOCK_SIZE sb_type = mi[0].sb_type;
1186       const int skip_this = mi[0].skip && is_inter_block(mi);
1187       // left edge of current unit is block/partition edge -> no skip
1188       const int block_edge_left = (num_4x4_blocks_wide_lookup[sb_type] > 1) ?
1189           !(c & (num_8x8_blocks_wide_lookup[sb_type] - 1)) : 1;
1190       const int skip_this_c = skip_this && !block_edge_left;
1191       // top edge of current unit is block/partition edge -> no skip
1192       const int block_edge_above = (num_4x4_blocks_high_lookup[sb_type] > 1) ?
1193           !(r & (num_8x8_blocks_high_lookup[sb_type] - 1)) : 1;
1194       const int skip_this_r = skip_this && !block_edge_above;
1195       const TX_SIZE tx_size = get_uv_tx_size(mi, plane);
1196       const int skip_border_4x4_c = ss_x && mi_col + c == cm->mi_cols - 1;
1197       const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
1198
1199       // Filter level can vary per MI
1200       if (!(lfl[(r << 3) + (c >> ss_x)] =
1201             get_filter_level(&cm->lf_info, mi)))
1202         continue;
1203
1204       // Build masks based on the transform size of each block
1205       if (tx_size == TX_32X32) {
1206         if (!skip_this_c && ((c >> ss_x) & 3) == 0) {
1207           if (!skip_border_4x4_c)
1208             mask_16x16_c |= 1 << (c >> ss_x);
1209           else
1210             mask_8x8_c |= 1 << (c >> ss_x);
1211         }
1212         if (!skip_this_r && ((r >> ss_y) & 3) == 0) {
1213           if (!skip_border_4x4_r)
1214             mask_16x16[r] |= 1 << (c >> ss_x);
1215           else
1216             mask_8x8[r] |= 1 << (c >> ss_x);
1217         }
1218       } else if (tx_size == TX_16X16) {
1219         if (!skip_this_c && ((c >> ss_x) & 1) == 0) {
1220           if (!skip_border_4x4_c)
1221             mask_16x16_c |= 1 << (c >> ss_x);
1222           else
1223             mask_8x8_c |= 1 << (c >> ss_x);
1224         }
1225         if (!skip_this_r && ((r >> ss_y) & 1) == 0) {
1226           if (!skip_border_4x4_r)
1227             mask_16x16[r] |= 1 << (c >> ss_x);
1228           else
1229             mask_8x8[r] |= 1 << (c >> ss_x);
1230         }
1231       } else {
1232         // force 8x8 filtering on 32x32 boundaries
1233         if (!skip_this_c) {
1234           if (tx_size == TX_8X8 || ((c >> ss_x) & 3) == 0)
1235             mask_8x8_c |= 1 << (c >> ss_x);
1236           else
1237             mask_4x4_c |= 1 << (c >> ss_x);
1238         }
1239
1240         if (!skip_this_r) {
1241           if (tx_size == TX_8X8 || ((r >> ss_y) & 3) == 0)
1242             mask_8x8[r] |= 1 << (c >> ss_x);
1243           else
1244             mask_4x4[r] |= 1 << (c >> ss_x);
1245         }
1246
1247         if (!skip_this && tx_size < TX_8X8 && !skip_border_4x4_c)
1248           mask_4x4_int[r] |= 1 << (c >> ss_x);
1249       }
1250     }
1251
1252     // Disable filtering on the leftmost column
1253     border_mask = ~(mi_col == 0);
1254 #if CONFIG_VP9_HIGHBITDEPTH
1255     if (cm->use_highbitdepth) {
1256       highbd_filter_selectively_vert(CONVERT_TO_SHORTPTR(dst->buf),
1257                                      dst->stride,
1258                                      mask_16x16_c & border_mask,
1259                                      mask_8x8_c & border_mask,
1260                                      mask_4x4_c & border_mask,
1261                                      mask_4x4_int[r],
1262                                      &cm->lf_info, &lfl[r << 3],
1263                                      (int)cm->bit_depth);
1264     } else {
1265       filter_selectively_vert(dst->buf, dst->stride,
1266                               mask_16x16_c & border_mask,
1267                               mask_8x8_c & border_mask,
1268                               mask_4x4_c & border_mask,
1269                               mask_4x4_int[r],
1270                               &cm->lf_info, &lfl[r << 3]);
1271     }
1272 #else
1273     filter_selectively_vert(dst->buf, dst->stride,
1274                             mask_16x16_c & border_mask,
1275                             mask_8x8_c & border_mask,
1276                             mask_4x4_c & border_mask,
1277                             mask_4x4_int[r],
1278                             &cm->lf_info, &lfl[r << 3]);
1279 #endif  // CONFIG_VP9_HIGHBITDEPTH
1280     dst->buf += 8 * dst->stride;
1281     mi_8x8 += row_step_stride;
1282   }
1283
1284   // Now do horizontal pass
1285   dst->buf = dst0;
1286   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
1287     const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
1288     const unsigned int mask_4x4_int_r = skip_border_4x4_r ? 0 : mask_4x4_int[r];
1289
1290     unsigned int mask_16x16_r;
1291     unsigned int mask_8x8_r;
1292     unsigned int mask_4x4_r;
1293
1294     if (mi_row + r == 0) {
1295       mask_16x16_r = 0;
1296       mask_8x8_r = 0;
1297       mask_4x4_r = 0;
1298     } else {
1299       mask_16x16_r = mask_16x16[r];
1300       mask_8x8_r = mask_8x8[r];
1301       mask_4x4_r = mask_4x4[r];
1302     }
1303 #if CONFIG_VP9_HIGHBITDEPTH
1304     if (cm->use_highbitdepth) {
1305       highbd_filter_selectively_horiz(CONVERT_TO_SHORTPTR(dst->buf),
1306                                       dst->stride,
1307                                       mask_16x16_r,
1308                                       mask_8x8_r,
1309                                       mask_4x4_r,
1310                                       mask_4x4_int_r,
1311                                       &cm->lf_info, &lfl[r << 3],
1312                                       (int)cm->bit_depth);
1313     } else {
1314       filter_selectively_horiz(dst->buf, dst->stride,
1315                                mask_16x16_r,
1316                                mask_8x8_r,
1317                                mask_4x4_r,
1318                                mask_4x4_int_r,
1319                                &cm->lf_info, &lfl[r << 3]);
1320     }
1321 #else
1322     filter_selectively_horiz(dst->buf, dst->stride,
1323                              mask_16x16_r,
1324                              mask_8x8_r,
1325                              mask_4x4_r,
1326                              mask_4x4_int_r,
1327                              &cm->lf_info, &lfl[r << 3]);
1328 #endif  // CONFIG_VP9_HIGHBITDEPTH
1329     dst->buf += 8 * dst->stride;
1330   }
1331 }
1332
1333 void vp9_filter_block_plane_ss00(VP9_COMMON *const cm,
1334                                  struct macroblockd_plane *const plane,
1335                                  int mi_row,
1336                                  LOOP_FILTER_MASK *lfm) {
1337   struct buf_2d *const dst = &plane->dst;
1338   uint8_t *const dst0 = dst->buf;
1339   int r;
1340   uint64_t mask_16x16 = lfm->left_y[TX_16X16];
1341   uint64_t mask_8x8 = lfm->left_y[TX_8X8];
1342   uint64_t mask_4x4 = lfm->left_y[TX_4X4];
1343   uint64_t mask_4x4_int = lfm->int_4x4_y;
1344
1345   assert(plane->subsampling_x == 0 && plane->subsampling_y == 0);
1346
1347   // Vertical pass: do 2 rows at one time
1348   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
1349     unsigned int mask_16x16_l = mask_16x16 & 0xffff;
1350     unsigned int mask_8x8_l = mask_8x8 & 0xffff;
1351     unsigned int mask_4x4_l = mask_4x4 & 0xffff;
1352     unsigned int mask_4x4_int_l = mask_4x4_int & 0xffff;
1353
1354 // Disable filtering on the leftmost column.
1355 #if CONFIG_VP9_HIGHBITDEPTH
1356     if (cm->use_highbitdepth) {
1357       highbd_filter_selectively_vert_row2(
1358           plane->subsampling_x, CONVERT_TO_SHORTPTR(dst->buf), dst->stride,
1359           mask_16x16_l, mask_8x8_l, mask_4x4_l, mask_4x4_int_l, &cm->lf_info,
1360           &lfm->lfl_y[r << 3], (int)cm->bit_depth);
1361     } else {
1362       filter_selectively_vert_row2(
1363           plane->subsampling_x, dst->buf, dst->stride, mask_16x16_l, mask_8x8_l,
1364           mask_4x4_l, mask_4x4_int_l, &cm->lf_info, &lfm->lfl_y[r << 3]);
1365     }
1366 #else
1367     filter_selectively_vert_row2(
1368         plane->subsampling_x, dst->buf, dst->stride, mask_16x16_l, mask_8x8_l,
1369         mask_4x4_l, mask_4x4_int_l, &cm->lf_info, &lfm->lfl_y[r << 3]);
1370 #endif  // CONFIG_VP9_HIGHBITDEPTH
1371     dst->buf += 16 * dst->stride;
1372     mask_16x16 >>= 16;
1373     mask_8x8 >>= 16;
1374     mask_4x4 >>= 16;
1375     mask_4x4_int >>= 16;
1376   }
1377
1378   // Horizontal pass
1379   dst->buf = dst0;
1380   mask_16x16 = lfm->above_y[TX_16X16];
1381   mask_8x8 = lfm->above_y[TX_8X8];
1382   mask_4x4 = lfm->above_y[TX_4X4];
1383   mask_4x4_int = lfm->int_4x4_y;
1384
1385   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r++) {
1386     unsigned int mask_16x16_r;
1387     unsigned int mask_8x8_r;
1388     unsigned int mask_4x4_r;
1389
1390     if (mi_row + r == 0) {
1391       mask_16x16_r = 0;
1392       mask_8x8_r = 0;
1393       mask_4x4_r = 0;
1394     } else {
1395       mask_16x16_r = mask_16x16 & 0xff;
1396       mask_8x8_r = mask_8x8 & 0xff;
1397       mask_4x4_r = mask_4x4 & 0xff;
1398     }
1399
1400 #if CONFIG_VP9_HIGHBITDEPTH
1401     if (cm->use_highbitdepth) {
1402       highbd_filter_selectively_horiz(
1403           CONVERT_TO_SHORTPTR(dst->buf), dst->stride, mask_16x16_r, mask_8x8_r,
1404           mask_4x4_r, mask_4x4_int & 0xff, &cm->lf_info, &lfm->lfl_y[r << 3],
1405           (int)cm->bit_depth);
1406     } else {
1407       filter_selectively_horiz(dst->buf, dst->stride, mask_16x16_r, mask_8x8_r,
1408                                mask_4x4_r, mask_4x4_int & 0xff, &cm->lf_info,
1409                                &lfm->lfl_y[r << 3]);
1410     }
1411 #else
1412     filter_selectively_horiz(dst->buf, dst->stride, mask_16x16_r, mask_8x8_r,
1413                              mask_4x4_r, mask_4x4_int & 0xff, &cm->lf_info,
1414                              &lfm->lfl_y[r << 3]);
1415 #endif  // CONFIG_VP9_HIGHBITDEPTH
1416
1417     dst->buf += 8 * dst->stride;
1418     mask_16x16 >>= 8;
1419     mask_8x8 >>= 8;
1420     mask_4x4 >>= 8;
1421     mask_4x4_int >>= 8;
1422   }
1423 }
1424
1425 void vp9_filter_block_plane_ss11(VP9_COMMON *const cm,
1426                                  struct macroblockd_plane *const plane,
1427                                  int mi_row,
1428                                  LOOP_FILTER_MASK *lfm) {
1429   struct buf_2d *const dst = &plane->dst;
1430   uint8_t *const dst0 = dst->buf;
1431   int r, c;
1432   uint8_t lfl_uv[16];
1433
1434   uint16_t mask_16x16 = lfm->left_uv[TX_16X16];
1435   uint16_t mask_8x8 = lfm->left_uv[TX_8X8];
1436   uint16_t mask_4x4 = lfm->left_uv[TX_4X4];
1437   uint16_t mask_4x4_int = lfm->int_4x4_uv;
1438
1439   assert(plane->subsampling_x == 1 && plane->subsampling_y == 1);
1440
1441   // Vertical pass: do 2 rows at one time
1442   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 4) {
1443     for (c = 0; c < (MI_BLOCK_SIZE >> 1); c++) {
1444       lfl_uv[(r << 1) + c] = lfm->lfl_y[(r << 3) + (c << 1)];
1445       lfl_uv[((r + 2) << 1) + c] = lfm->lfl_y[((r + 2) << 3) + (c << 1)];
1446     }
1447
1448     {
1449       unsigned int mask_16x16_l = mask_16x16 & 0xff;
1450       unsigned int mask_8x8_l = mask_8x8 & 0xff;
1451       unsigned int mask_4x4_l = mask_4x4 & 0xff;
1452       unsigned int mask_4x4_int_l = mask_4x4_int & 0xff;
1453
1454 // Disable filtering on the leftmost column.
1455 #if CONFIG_VP9_HIGHBITDEPTH
1456       if (cm->use_highbitdepth) {
1457         highbd_filter_selectively_vert_row2(
1458             plane->subsampling_x, CONVERT_TO_SHORTPTR(dst->buf), dst->stride,
1459             mask_16x16_l, mask_8x8_l, mask_4x4_l, mask_4x4_int_l, &cm->lf_info,
1460             &lfl_uv[r << 1], (int)cm->bit_depth);
1461       } else {
1462         filter_selectively_vert_row2(
1463             plane->subsampling_x, dst->buf, dst->stride,
1464             mask_16x16_l, mask_8x8_l, mask_4x4_l, mask_4x4_int_l, &cm->lf_info,
1465             &lfl_uv[r << 1]);
1466       }
1467 #else
1468       filter_selectively_vert_row2(
1469           plane->subsampling_x, dst->buf, dst->stride,
1470           mask_16x16_l, mask_8x8_l, mask_4x4_l, mask_4x4_int_l, &cm->lf_info,
1471           &lfl_uv[r << 1]);
1472 #endif  // CONFIG_VP9_HIGHBITDEPTH
1473
1474       dst->buf += 16 * dst->stride;
1475       mask_16x16 >>= 8;
1476       mask_8x8 >>= 8;
1477       mask_4x4 >>= 8;
1478       mask_4x4_int >>= 8;
1479     }
1480   }
1481
1482   // Horizontal pass
1483   dst->buf = dst0;
1484   mask_16x16 = lfm->above_uv[TX_16X16];
1485   mask_8x8 = lfm->above_uv[TX_8X8];
1486   mask_4x4 = lfm->above_uv[TX_4X4];
1487   mask_4x4_int = lfm->int_4x4_uv;
1488
1489   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
1490     const int skip_border_4x4_r = mi_row + r == cm->mi_rows - 1;
1491     const unsigned int mask_4x4_int_r =
1492         skip_border_4x4_r ? 0 : (mask_4x4_int & 0xf);
1493     unsigned int mask_16x16_r;
1494     unsigned int mask_8x8_r;
1495     unsigned int mask_4x4_r;
1496
1497     if (mi_row + r == 0) {
1498       mask_16x16_r = 0;
1499       mask_8x8_r = 0;
1500       mask_4x4_r = 0;
1501     } else {
1502       mask_16x16_r = mask_16x16 & 0xf;
1503       mask_8x8_r = mask_8x8 & 0xf;
1504       mask_4x4_r = mask_4x4 & 0xf;
1505     }
1506
1507 #if CONFIG_VP9_HIGHBITDEPTH
1508     if (cm->use_highbitdepth) {
1509       highbd_filter_selectively_horiz(CONVERT_TO_SHORTPTR(dst->buf),
1510                                       dst->stride, mask_16x16_r, mask_8x8_r,
1511                                       mask_4x4_r, mask_4x4_int_r, &cm->lf_info,
1512                                       &lfl_uv[r << 1], (int)cm->bit_depth);
1513     } else {
1514       filter_selectively_horiz(dst->buf, dst->stride, mask_16x16_r, mask_8x8_r,
1515                                mask_4x4_r, mask_4x4_int_r, &cm->lf_info,
1516                                &lfl_uv[r << 1]);
1517     }
1518 #else
1519     filter_selectively_horiz(dst->buf, dst->stride, mask_16x16_r, mask_8x8_r,
1520                              mask_4x4_r, mask_4x4_int_r, &cm->lf_info,
1521                              &lfl_uv[r << 1]);
1522 #endif  // CONFIG_VP9_HIGHBITDEPTH
1523
1524     dst->buf += 8 * dst->stride;
1525     mask_16x16 >>= 4;
1526     mask_8x8 >>= 4;
1527     mask_4x4 >>= 4;
1528     mask_4x4_int >>= 4;
1529   }
1530 }
1531
1532 static void loop_filter_rows(YV12_BUFFER_CONFIG *frame_buffer, VP9_COMMON *cm,
1533                              struct macroblockd_plane planes[MAX_MB_PLANE],
1534                              int start, int stop, int y_only) {
1535   const int num_planes = y_only ? 1 : MAX_MB_PLANE;
1536   enum lf_path path;
1537   int mi_row, mi_col;
1538
1539   if (y_only)
1540     path = LF_PATH_444;
1541   else if (planes[1].subsampling_y == 1 && planes[1].subsampling_x == 1)
1542     path = LF_PATH_420;
1543   else if (planes[1].subsampling_y == 0 && planes[1].subsampling_x == 0)
1544     path = LF_PATH_444;
1545   else
1546     path = LF_PATH_SLOW;
1547
1548   for (mi_row = start; mi_row < stop; mi_row += MI_BLOCK_SIZE) {
1549     MODE_INFO **mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
1550     LOOP_FILTER_MASK *lfm = get_lfm(&cm->lf, mi_row, 0);
1551
1552     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE, ++lfm) {
1553       int plane;
1554
1555       vp9_setup_dst_planes(planes, frame_buffer, mi_row, mi_col);
1556
1557       // TODO(JBB): Make setup_mask work for non 420.
1558       vp9_adjust_mask(cm, mi_row, mi_col, lfm);
1559
1560       vp9_filter_block_plane_ss00(cm, &planes[0], mi_row, lfm);
1561       for (plane = 1; plane < num_planes; ++plane) {
1562         switch (path) {
1563           case LF_PATH_420:
1564             vp9_filter_block_plane_ss11(cm, &planes[plane], mi_row, lfm);
1565             break;
1566           case LF_PATH_444:
1567             vp9_filter_block_plane_ss00(cm, &planes[plane], mi_row, lfm);
1568             break;
1569           case LF_PATH_SLOW:
1570             vp9_filter_block_plane_non420(cm, &planes[plane], mi + mi_col,
1571                                           mi_row, mi_col);
1572             break;
1573         }
1574       }
1575     }
1576   }
1577 }
1578
1579 void vp9_loop_filter_frame(YV12_BUFFER_CONFIG *frame,
1580                            VP9_COMMON *cm, MACROBLOCKD *xd,
1581                            int frame_filter_level,
1582                            int y_only, int partial_frame) {
1583   int start_mi_row, end_mi_row, mi_rows_to_filter;
1584   if (!frame_filter_level) return;
1585   start_mi_row = 0;
1586   mi_rows_to_filter = cm->mi_rows;
1587   if (partial_frame && cm->mi_rows > 8) {
1588     start_mi_row = cm->mi_rows >> 1;
1589     start_mi_row &= 0xfffffff8;
1590     mi_rows_to_filter = VPXMAX(cm->mi_rows / 8, 8);
1591   }
1592   end_mi_row = start_mi_row + mi_rows_to_filter;
1593   loop_filter_rows(frame, cm, xd->plane, start_mi_row, end_mi_row, y_only);
1594 }
1595
1596 // Used by the encoder to build the loopfilter masks.
1597 void vp9_build_mask_frame(VP9_COMMON *cm, int frame_filter_level,
1598                           int partial_frame) {
1599   int start_mi_row, end_mi_row, mi_rows_to_filter;
1600   int mi_col, mi_row;
1601   if (!frame_filter_level) return;
1602   start_mi_row = 0;
1603   mi_rows_to_filter = cm->mi_rows;
1604   if (partial_frame && cm->mi_rows > 8) {
1605     start_mi_row = cm->mi_rows >> 1;
1606     start_mi_row &= 0xfffffff8;
1607     mi_rows_to_filter = VPXMAX(cm->mi_rows / 8, 8);
1608   }
1609   end_mi_row = start_mi_row + mi_rows_to_filter;
1610
1611   vp9_loop_filter_frame_init(cm, frame_filter_level);
1612
1613   for (mi_row = start_mi_row; mi_row < end_mi_row; mi_row += MI_BLOCK_SIZE) {
1614     MODE_INFO **mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
1615     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) {
1616       // vp9_setup_mask() zeros lfm
1617       vp9_setup_mask(cm, mi_row, mi_col, mi + mi_col, cm->mi_stride,
1618                      get_lfm(&cm->lf, mi_row, mi_col));
1619     }
1620   }
1621 }
1622
1623 // 8x8 blocks in a superblock.  A "1" represents the first block in a 16x16
1624 // or greater area.
1625 static const uint8_t first_block_in_16x16[8][8] = {
1626   {1, 0, 1, 0, 1, 0, 1, 0},
1627   {0, 0, 0, 0, 0, 0, 0, 0},
1628   {1, 0, 1, 0, 1, 0, 1, 0},
1629   {0, 0, 0, 0, 0, 0, 0, 0},
1630   {1, 0, 1, 0, 1, 0, 1, 0},
1631   {0, 0, 0, 0, 0, 0, 0, 0},
1632   {1, 0, 1, 0, 1, 0, 1, 0},
1633   {0, 0, 0, 0, 0, 0, 0, 0}
1634 };
1635
1636 // This function sets up the bit masks for a block represented
1637 // by mi_row, mi_col in a 64x64 region.
1638 // TODO(SJL): This function only works for yv12.
1639 void vp9_build_mask(VP9_COMMON *cm, const MODE_INFO *mi, int mi_row,
1640                     int mi_col, int bw, int bh) {
1641   const BLOCK_SIZE block_size = mi->sb_type;
1642   const TX_SIZE tx_size_y = mi->tx_size;
1643   const loop_filter_info_n *const lfi_n = &cm->lf_info;
1644   const int filter_level = get_filter_level(lfi_n, mi);
1645   const TX_SIZE tx_size_uv = get_uv_tx_size_impl(tx_size_y, block_size, 1, 1);
1646   LOOP_FILTER_MASK *const lfm = get_lfm(&cm->lf, mi_row, mi_col);
1647   uint64_t *const left_y = &lfm->left_y[tx_size_y];
1648   uint64_t *const above_y = &lfm->above_y[tx_size_y];
1649   uint64_t *const int_4x4_y = &lfm->int_4x4_y;
1650   uint16_t *const left_uv = &lfm->left_uv[tx_size_uv];
1651   uint16_t *const above_uv = &lfm->above_uv[tx_size_uv];
1652   uint16_t *const int_4x4_uv = &lfm->int_4x4_uv;
1653   const int row_in_sb = (mi_row & 7);
1654   const int col_in_sb = (mi_col & 7);
1655   const int shift_y = col_in_sb + (row_in_sb << 3);
1656   const int shift_uv = (col_in_sb >> 1) + ((row_in_sb >> 1) << 2);
1657   const int build_uv = first_block_in_16x16[row_in_sb][col_in_sb];
1658
1659   if (!filter_level) {
1660     return;
1661   } else {
1662     int index = shift_y;
1663     int i;
1664     for (i = 0; i < bh; i++) {
1665       memset(&lfm->lfl_y[index], filter_level, bw);
1666       index += 8;
1667     }
1668   }
1669
1670   // These set 1 in the current block size for the block size edges.
1671   // For instance if the block size is 32x16, we'll set:
1672   //    above =   1111
1673   //              0000
1674   //    and
1675   //    left  =   1000
1676   //          =   1000
1677   // NOTE : In this example the low bit is left most ( 1000 ) is stored as
1678   //        1,  not 8...
1679   //
1680   // U and V set things on a 16 bit scale.
1681   //
1682   *above_y |= above_prediction_mask[block_size] << shift_y;
1683   *left_y |= left_prediction_mask[block_size] << shift_y;
1684
1685   if (build_uv) {
1686     *above_uv |= above_prediction_mask_uv[block_size] << shift_uv;
1687     *left_uv |= left_prediction_mask_uv[block_size] << shift_uv;
1688   }
1689
1690   // If the block has no coefficients and is not intra we skip applying
1691   // the loop filter on block edges.
1692   if (mi->skip && is_inter_block(mi))
1693     return;
1694
1695   // Add a mask for the transform size. The transform size mask is set to
1696   // be correct for a 64x64 prediction block size. Mask to match the size of
1697   // the block we are working on and then shift it into place.
1698   *above_y |= (size_mask[block_size] &
1699                above_64x64_txform_mask[tx_size_y]) << shift_y;
1700   *left_y |= (size_mask[block_size] &
1701               left_64x64_txform_mask[tx_size_y]) << shift_y;
1702
1703   if (build_uv) {
1704     *above_uv |= (size_mask_uv[block_size] &
1705                   above_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
1706
1707     *left_uv |= (size_mask_uv[block_size] &
1708                  left_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
1709   }
1710
1711   // Try to determine what to do with the internal 4x4 block boundaries.  These
1712   // differ from the 4x4 boundaries on the outside edge of an 8x8 in that the
1713   // internal ones can be skipped and don't depend on the prediction block size.
1714   if (tx_size_y == TX_4X4)
1715     *int_4x4_y |= size_mask[block_size] << shift_y;
1716
1717   if (build_uv && tx_size_uv == TX_4X4)
1718     *int_4x4_uv |= (size_mask_uv[block_size] & 0xffff) << shift_uv;
1719 }
1720
1721 void vp9_loop_filter_data_reset(
1722     LFWorkerData *lf_data, YV12_BUFFER_CONFIG *frame_buffer,
1723     struct VP9Common *cm, const struct macroblockd_plane planes[MAX_MB_PLANE]) {
1724   lf_data->frame_buffer = frame_buffer;
1725   lf_data->cm = cm;
1726   lf_data->start = 0;
1727   lf_data->stop = 0;
1728   lf_data->y_only = 0;
1729   memcpy(lf_data->planes, planes, sizeof(lf_data->planes));
1730 }
1731
1732 void vp9_reset_lfm(VP9_COMMON *const cm) {
1733   if (cm->lf.filter_level) {
1734     memset(cm->lf.lfm, 0,
1735            ((cm->mi_rows + (MI_BLOCK_SIZE - 1)) >> 3) * cm->lf.lfm_stride *
1736             sizeof(*cm->lf.lfm));
1737   }
1738 }
1739
1740 int vp9_loop_filter_worker(LFWorkerData *const lf_data, void *unused) {
1741   (void)unused;
1742   loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
1743                    lf_data->start, lf_data->stop, lf_data->y_only);
1744   return 1;
1745 }