]> granicus.if.org Git - libvpx/blob - vp10/common/loopfilter.c
vp10: split UV int4x4 loopfilter flag in one for each covered edge.
[libvpx] / vp10 / common / 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 "vp10/common/loopfilter.h"
14 #include "vp10/common/onyxc_int.h"
15 #include "vp10/common/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 "vp10/common/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 MB_MODE_INFO *mbmi) {
236   return lfi_n->lvl[mbmi->segment_id][mbmi->ref_frame[0]]
237                    [mode_lf_lut[mbmi->mode]];
238 }
239
240 void vp10_loop_filter_init(VP10_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 vp10_loop_filter_frame_init(VP10_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                              1);
350         } else {
351           vpx_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
352                              lfi1->hev_thr, 1);
353         }
354       }
355
356       if ((mask_4x4_0 | mask_4x4_1) & 1) {
357         if ((mask_4x4_0 & mask_4x4_1) & 1) {
358           vpx_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
359                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
360                                   lfi1->hev_thr);
361         } else if (mask_4x4_0 & 1) {
362           vpx_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr,
363                              1);
364         } else {
365           vpx_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
366                              lfi1->hev_thr, 1);
367         }
368       }
369
370       if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
371         if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
372           vpx_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
373                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
374                                   lfi1->hev_thr);
375         } else if (mask_4x4_int_0 & 1) {
376           vpx_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
377                              lfi0->hev_thr, 1);
378         } else {
379           vpx_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim, lfi1->lim,
380                              lfi1->hev_thr, 1);
381         }
382       }
383     }
384
385     s += 8;
386     lfl += 1;
387     mask_16x16_0 >>= 1;
388     mask_8x8_0 >>= 1;
389     mask_4x4_0 >>= 1;
390     mask_4x4_int_0 >>= 1;
391     mask_16x16_1 >>= 1;
392     mask_8x8_1 >>= 1;
393     mask_4x4_1 >>= 1;
394     mask_4x4_int_1 >>= 1;
395   }
396 }
397
398 #if CONFIG_VP9_HIGHBITDEPTH
399 static void highbd_filter_selectively_vert_row2(int subsampling_factor,
400                                                 uint16_t *s, int pitch,
401                                                 unsigned int mask_16x16_l,
402                                                 unsigned int mask_8x8_l,
403                                                 unsigned int mask_4x4_l,
404                                                 unsigned int mask_4x4_int_l,
405                                                 const loop_filter_info_n *lfi_n,
406                                                 const uint8_t *lfl, int bd) {
407   const int mask_shift = subsampling_factor ? 4 : 8;
408   const int mask_cutoff = subsampling_factor ? 0xf : 0xff;
409   const int lfl_forward = subsampling_factor ? 4 : 8;
410
411   unsigned int mask_16x16_0 = mask_16x16_l & mask_cutoff;
412   unsigned int mask_8x8_0 = mask_8x8_l & mask_cutoff;
413   unsigned int mask_4x4_0 = mask_4x4_l & mask_cutoff;
414   unsigned int mask_4x4_int_0 = mask_4x4_int_l & mask_cutoff;
415   unsigned int mask_16x16_1 = (mask_16x16_l >> mask_shift) & mask_cutoff;
416   unsigned int mask_8x8_1 = (mask_8x8_l >> mask_shift) & mask_cutoff;
417   unsigned int mask_4x4_1 = (mask_4x4_l >> mask_shift) & mask_cutoff;
418   unsigned int mask_4x4_int_1 = (mask_4x4_int_l >> mask_shift) & mask_cutoff;
419   unsigned int mask;
420
421   for (mask = mask_16x16_0 | mask_8x8_0 | mask_4x4_0 | mask_4x4_int_0 |
422        mask_16x16_1 | mask_8x8_1 | mask_4x4_1 | mask_4x4_int_1;
423        mask; mask >>= 1) {
424     const loop_filter_thresh *lfi0 = lfi_n->lfthr + *lfl;
425     const loop_filter_thresh *lfi1 = lfi_n->lfthr + *(lfl + lfl_forward);
426
427     // TODO(yunqingwang): count in loopfilter functions should be removed.
428     if (mask & 1) {
429       if ((mask_16x16_0 | mask_16x16_1) & 1) {
430         if ((mask_16x16_0 & mask_16x16_1) & 1) {
431           vpx_highbd_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
432                                           lfi0->hev_thr, bd);
433         } else if (mask_16x16_0 & 1) {
434           vpx_highbd_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim,
435                                      lfi0->hev_thr, bd);
436         } else {
437           vpx_highbd_lpf_vertical_16(s + 8 *pitch, pitch, lfi1->mblim,
438                                      lfi1->lim, lfi1->hev_thr, bd);
439         }
440       }
441
442       if ((mask_8x8_0 | mask_8x8_1) & 1) {
443         if ((mask_8x8_0 & mask_8x8_1) & 1) {
444           vpx_highbd_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
445                                          lfi0->hev_thr, lfi1->mblim, lfi1->lim,
446                                          lfi1->hev_thr, bd);
447         } else if (mask_8x8_0 & 1) {
448           vpx_highbd_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim,
449                                     lfi0->hev_thr, 1, bd);
450         } else {
451           vpx_highbd_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim,
452                                     lfi1->lim, lfi1->hev_thr, 1, bd);
453         }
454       }
455
456       if ((mask_4x4_0 | mask_4x4_1) & 1) {
457         if ((mask_4x4_0 & mask_4x4_1) & 1) {
458           vpx_highbd_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
459                                          lfi0->hev_thr, lfi1->mblim, lfi1->lim,
460                                          lfi1->hev_thr, bd);
461         } else if (mask_4x4_0 & 1) {
462           vpx_highbd_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim,
463                                     lfi0->hev_thr, 1, bd);
464         } else {
465           vpx_highbd_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim,
466                                     lfi1->lim, lfi1->hev_thr, 1, bd);
467         }
468       }
469
470       if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
471         if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
472           vpx_highbd_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
473                                          lfi0->hev_thr, lfi1->mblim, lfi1->lim,
474                                          lfi1->hev_thr, bd);
475         } else if (mask_4x4_int_0 & 1) {
476           vpx_highbd_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
477                                     lfi0->hev_thr, 1, bd);
478         } else {
479           vpx_highbd_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim,
480                                     lfi1->lim, lfi1->hev_thr, 1, bd);
481         }
482       }
483     }
484
485     s += 8;
486     lfl += 1;
487     mask_16x16_0 >>= 1;
488     mask_8x8_0 >>= 1;
489     mask_4x4_0 >>= 1;
490     mask_4x4_int_0 >>= 1;
491     mask_16x16_1 >>= 1;
492     mask_8x8_1 >>= 1;
493     mask_4x4_1 >>= 1;
494     mask_4x4_int_1 >>= 1;
495   }
496 }
497 #endif  // CONFIG_VP9_HIGHBITDEPTH
498
499 static void filter_selectively_horiz(uint8_t *s, int pitch,
500                                      unsigned int mask_16x16,
501                                      unsigned int mask_8x8,
502                                      unsigned int mask_4x4,
503                                      unsigned int mask_4x4_int,
504                                      const loop_filter_info_n *lfi_n,
505                                      const uint8_t *lfl) {
506   unsigned int mask;
507   int count;
508
509   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
510        mask; mask >>= count) {
511     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
512
513     count = 1;
514     if (mask & 1) {
515       if (mask_16x16 & 1) {
516         if ((mask_16x16 & 3) == 3) {
517           vpx_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
518                                 lfi->hev_thr, 2);
519           count = 2;
520         } else {
521           vpx_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
522                                 lfi->hev_thr, 1);
523         }
524       } else if (mask_8x8 & 1) {
525         if ((mask_8x8 & 3) == 3) {
526           // Next block's thresholds.
527           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
528
529           vpx_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
530                                     lfi->hev_thr, lfin->mblim, lfin->lim,
531                                     lfin->hev_thr);
532
533           if ((mask_4x4_int & 3) == 3) {
534             vpx_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
535                                       lfi->lim, lfi->hev_thr, lfin->mblim,
536                                       lfin->lim, lfin->hev_thr);
537           } else {
538             if (mask_4x4_int & 1)
539               vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
540                                    lfi->hev_thr, 1);
541             else if (mask_4x4_int & 2)
542               vpx_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
543                                    lfin->lim, lfin->hev_thr, 1);
544           }
545           count = 2;
546         } else {
547           vpx_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
548
549           if (mask_4x4_int & 1)
550             vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
551                                  lfi->hev_thr, 1);
552         }
553       } else if (mask_4x4 & 1) {
554         if ((mask_4x4 & 3) == 3) {
555           // Next block's thresholds.
556           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
557
558           vpx_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
559                                     lfi->hev_thr, lfin->mblim, lfin->lim,
560                                     lfin->hev_thr);
561           if ((mask_4x4_int & 3) == 3) {
562             vpx_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
563                                       lfi->lim, lfi->hev_thr, lfin->mblim,
564                                       lfin->lim, lfin->hev_thr);
565           } else {
566             if (mask_4x4_int & 1)
567               vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
568                                    lfi->hev_thr, 1);
569             else if (mask_4x4_int & 2)
570               vpx_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
571                                    lfin->lim, lfin->hev_thr, 1);
572           }
573           count = 2;
574         } else {
575           vpx_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
576
577           if (mask_4x4_int & 1)
578             vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
579                                  lfi->hev_thr, 1);
580         }
581       } else if (mask_4x4_int & 1) {
582         vpx_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
583                              lfi->hev_thr, 1);
584       }
585     }
586     s += 8 * count;
587     lfl += count;
588     mask_16x16 >>= count;
589     mask_8x8 >>= count;
590     mask_4x4 >>= count;
591     mask_4x4_int >>= count;
592   }
593 }
594
595 #if CONFIG_VP9_HIGHBITDEPTH
596 static void highbd_filter_selectively_horiz(uint16_t *s, int pitch,
597                                             unsigned int mask_16x16,
598                                             unsigned int mask_8x8,
599                                             unsigned int mask_4x4,
600                                             unsigned int mask_4x4_int,
601                                             const loop_filter_info_n *lfi_n,
602                                             const uint8_t *lfl, int bd) {
603   unsigned int mask;
604   int count;
605
606   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
607        mask; mask >>= count) {
608     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
609
610     count = 1;
611     if (mask & 1) {
612       if (mask_16x16 & 1) {
613         if ((mask_16x16 & 3) == 3) {
614           vpx_highbd_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
615                                        lfi->hev_thr, 2, bd);
616           count = 2;
617         } else {
618           vpx_highbd_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
619                                        lfi->hev_thr, 1, bd);
620         }
621       } else if (mask_8x8 & 1) {
622         if ((mask_8x8 & 3) == 3) {
623           // Next block's thresholds.
624           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
625
626           vpx_highbd_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
627                                            lfi->hev_thr, lfin->mblim, lfin->lim,
628                                            lfin->hev_thr, bd);
629
630           if ((mask_4x4_int & 3) == 3) {
631             vpx_highbd_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
632                                              lfi->lim, lfi->hev_thr,
633                                              lfin->mblim, lfin->lim,
634                                              lfin->hev_thr, bd);
635           } else {
636             if (mask_4x4_int & 1) {
637               vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
638                                           lfi->lim, lfi->hev_thr, 1, bd);
639             } else if (mask_4x4_int & 2) {
640               vpx_highbd_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
641                                           lfin->lim, lfin->hev_thr, 1, bd);
642             }
643           }
644           count = 2;
645         } else {
646           vpx_highbd_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim,
647                                       lfi->hev_thr, 1, bd);
648
649           if (mask_4x4_int & 1) {
650             vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
651                                         lfi->lim, lfi->hev_thr, 1, bd);
652           }
653         }
654       } else if (mask_4x4 & 1) {
655         if ((mask_4x4 & 3) == 3) {
656           // Next block's thresholds.
657           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
658
659           vpx_highbd_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
660                                            lfi->hev_thr, lfin->mblim, lfin->lim,
661                                            lfin->hev_thr, bd);
662           if ((mask_4x4_int & 3) == 3) {
663             vpx_highbd_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
664                                              lfi->lim, lfi->hev_thr,
665                                              lfin->mblim, lfin->lim,
666                                              lfin->hev_thr, bd);
667           } else {
668             if (mask_4x4_int & 1) {
669               vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
670                                           lfi->lim, lfi->hev_thr, 1, bd);
671             } else if (mask_4x4_int & 2) {
672               vpx_highbd_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
673                                           lfin->lim, lfin->hev_thr, 1, bd);
674             }
675           }
676           count = 2;
677         } else {
678           vpx_highbd_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim,
679                                       lfi->hev_thr, 1, bd);
680
681           if (mask_4x4_int & 1) {
682             vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim,
683                                         lfi->lim, lfi->hev_thr, 1, bd);
684           }
685         }
686       } else if (mask_4x4_int & 1) {
687         vpx_highbd_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
688                                     lfi->hev_thr, 1, bd);
689       }
690     }
691     s += 8 * count;
692     lfl += count;
693     mask_16x16 >>= count;
694     mask_8x8 >>= count;
695     mask_4x4 >>= count;
696     mask_4x4_int >>= count;
697   }
698 }
699 #endif  // CONFIG_VP9_HIGHBITDEPTH
700
701 // This function ors into the current lfm structure, where to do loop
702 // filters for the specific mi we are looking at. It uses information
703 // including the block_size_type (32x16, 32x32, etc.), the transform size,
704 // whether there were any coefficients encoded, and the loop filter strength
705 // block we are currently looking at. Shift is used to position the
706 // 1's we produce.
707 // TODO(JBB) Need another function for different resolution color..
708 static void build_masks(const loop_filter_info_n *const lfi_n,
709                         const MODE_INFO *mi, const int shift_y,
710                         const int shift_uv,
711                         LOOP_FILTER_MASK *lfm) {
712   const MB_MODE_INFO *mbmi = &mi->mbmi;
713   const BLOCK_SIZE block_size = mbmi->sb_type;
714   const TX_SIZE tx_size_y = mbmi->tx_size;
715   const TX_SIZE tx_size_uv = get_uv_tx_size_impl(tx_size_y, block_size, 1, 1);
716   const int filter_level = get_filter_level(lfi_n, mbmi);
717   uint64_t *const left_y = &lfm->left_y[tx_size_y];
718   uint64_t *const above_y = &lfm->above_y[tx_size_y];
719   uint64_t *const int_4x4_y = &lfm->int_4x4_y;
720   uint16_t *const left_uv = &lfm->left_uv[tx_size_uv];
721   uint16_t *const above_uv = &lfm->above_uv[tx_size_uv];
722 #if CONFIG_MISC_FIXES
723   uint16_t *const int_4x4_uv = &lfm->left_int_4x4_uv;
724 #else
725   uint16_t *const int_4x4_uv = &lfm->int_4x4_uv;
726 #endif
727   int i;
728
729   // If filter level is 0 we don't loop filter.
730   if (!filter_level) {
731     return;
732   } else {
733     const int w = num_8x8_blocks_wide_lookup[block_size];
734     const int h = num_8x8_blocks_high_lookup[block_size];
735     int index = shift_y;
736     for (i = 0; i < h; i++) {
737       memset(&lfm->lfl_y[index], filter_level, w);
738       index += 8;
739     }
740   }
741
742   // These set 1 in the current block size for the block size edges.
743   // For instance if the block size is 32x16, we'll set:
744   //    above =   1111
745   //              0000
746   //    and
747   //    left  =   1000
748   //          =   1000
749   // NOTE : In this example the low bit is left most ( 1000 ) is stored as
750   //        1,  not 8...
751   //
752   // U and V set things on a 16 bit scale.
753   //
754   *above_y |= above_prediction_mask[block_size] << shift_y;
755   *above_uv |= above_prediction_mask_uv[block_size] << shift_uv;
756   *left_y |= left_prediction_mask[block_size] << shift_y;
757   *left_uv |= left_prediction_mask_uv[block_size] << shift_uv;
758
759   // If the block has no coefficients and is not intra we skip applying
760   // the loop filter on block edges.
761 #if CONFIG_MISC_FIXES
762   if ((mbmi->skip || mbmi->has_no_coeffs) && is_inter_block(mbmi))
763     return;
764 #else
765   if (mbmi->skip && is_inter_block(mbmi))
766     return;
767 #endif
768
769   // Here we are adding a mask for the transform size. The transform
770   // size mask is set to be correct for a 64x64 prediction block size. We
771   // mask to match the size of the block we are working on and then shift it
772   // into place..
773   *above_y |= (size_mask[block_size] &
774                above_64x64_txform_mask[tx_size_y]) << shift_y;
775   *above_uv |= (size_mask_uv[block_size] &
776                 above_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
777
778   *left_y |= (size_mask[block_size] &
779               left_64x64_txform_mask[tx_size_y]) << shift_y;
780   *left_uv |= (size_mask_uv[block_size] &
781                left_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
782
783   // Here we are trying to determine what to do with the internal 4x4 block
784   // boundaries.  These differ from the 4x4 boundaries on the outside edge of
785   // an 8x8 in that the internal ones can be skipped and don't depend on
786   // the prediction block size.
787   if (tx_size_y == TX_4X4)
788     *int_4x4_y |= (size_mask[block_size] & 0xffffffffffffffffULL) << shift_y;
789
790   if (tx_size_uv == TX_4X4)
791     *int_4x4_uv |= (size_mask_uv[block_size] & 0xffff) << shift_uv;
792 }
793
794 // This function does the same thing as the one above with the exception that
795 // it only affects the y masks. It exists because for blocks < 16x16 in size,
796 // we only update u and v masks on the first block.
797 static void build_y_mask(const loop_filter_info_n *const lfi_n,
798                          const MODE_INFO *mi, const int shift_y,
799                          LOOP_FILTER_MASK *lfm) {
800   const MB_MODE_INFO *mbmi = &mi->mbmi;
801   const BLOCK_SIZE block_size = mbmi->sb_type;
802   const TX_SIZE tx_size_y = mbmi->tx_size;
803   const int filter_level = get_filter_level(lfi_n, mbmi);
804   uint64_t *const left_y = &lfm->left_y[tx_size_y];
805   uint64_t *const above_y = &lfm->above_y[tx_size_y];
806   uint64_t *const int_4x4_y = &lfm->int_4x4_y;
807   int i;
808
809   if (!filter_level) {
810     return;
811   } else {
812     const int w = num_8x8_blocks_wide_lookup[block_size];
813     const int h = num_8x8_blocks_high_lookup[block_size];
814     int index = shift_y;
815     for (i = 0; i < h; i++) {
816       memset(&lfm->lfl_y[index], filter_level, w);
817       index += 8;
818     }
819   }
820
821   *above_y |= above_prediction_mask[block_size] << shift_y;
822   *left_y |= left_prediction_mask[block_size] << shift_y;
823
824 #if CONFIG_MISC_FIXES
825   if ((mbmi->skip || mbmi->has_no_coeffs) && is_inter_block(mbmi))
826     return;
827 #else
828   if (mbmi->skip && is_inter_block(mbmi))
829     return;
830 #endif
831
832   *above_y |= (size_mask[block_size] &
833                above_64x64_txform_mask[tx_size_y]) << shift_y;
834
835   *left_y |= (size_mask[block_size] &
836               left_64x64_txform_mask[tx_size_y]) << shift_y;
837
838   if (tx_size_y == TX_4X4)
839     *int_4x4_y |= (size_mask[block_size] & 0xffffffffffffffffULL) << shift_y;
840 }
841
842 // This function sets up the bit masks for the entire 64x64 region represented
843 // by mi_row, mi_col.
844 // TODO(JBB): This function only works for yv12.
845 void vp10_setup_mask(VP10_COMMON *const cm, const int mi_row, const int mi_col,
846                     MODE_INFO **mi, const int mode_info_stride,
847                     LOOP_FILTER_MASK *lfm) {
848   int idx_32, idx_16, idx_8;
849   const loop_filter_info_n *const lfi_n = &cm->lf_info;
850   MODE_INFO **mip = mi;
851   MODE_INFO **mip2 = mi;
852
853   // These are offsets to the next mi in the 64x64 block. It is what gets
854   // added to the mi ptr as we go through each loop. It helps us to avoid
855   // setting up special row and column counters for each index. The last step
856   // brings us out back to the starting position.
857   const int offset_32[] = {4, (mode_info_stride << 2) - 4, 4,
858                            -(mode_info_stride << 2) - 4};
859   const int offset_16[] = {2, (mode_info_stride << 1) - 2, 2,
860                            -(mode_info_stride << 1) - 2};
861   const int offset[] = {1, mode_info_stride - 1, 1, -mode_info_stride - 1};
862
863   // Following variables represent shifts to position the current block
864   // mask over the appropriate block. A shift of 36 to the left will move
865   // the bits for the final 32 by 32 block in the 64x64 up 4 rows and left
866   // 4 rows to the appropriate spot.
867   const int shift_32_y[] = {0, 4, 32, 36};
868   const int shift_16_y[] = {0, 2, 16, 18};
869   const int shift_8_y[] = {0, 1, 8, 9};
870   const int shift_32_uv[] = {0, 2, 8, 10};
871   const int shift_16_uv[] = {0, 1, 4, 5};
872   int i;
873   const int max_rows = (mi_row + MI_BLOCK_SIZE > cm->mi_rows ?
874                         cm->mi_rows - mi_row : MI_BLOCK_SIZE);
875   const int max_cols = (mi_col + MI_BLOCK_SIZE > cm->mi_cols ?
876                         cm->mi_cols - mi_col : MI_BLOCK_SIZE);
877
878   vp10_zero(*lfm);
879   assert(mip[0] != NULL);
880
881   // TODO(jimbankoski): Try moving most of the following code into decode
882   // loop and storing lfm in the mbmi structure so that we don't have to go
883   // through the recursive loop structure multiple times.
884   switch (mip[0]->mbmi.sb_type) {
885     case BLOCK_64X64:
886       build_masks(lfi_n, mip[0] , 0, 0, lfm);
887       break;
888     case BLOCK_64X32:
889       build_masks(lfi_n, mip[0], 0, 0, lfm);
890       mip2 = mip + mode_info_stride * 4;
891       if (4 >= max_rows)
892         break;
893       build_masks(lfi_n, mip2[0], 32, 8, lfm);
894       break;
895     case BLOCK_32X64:
896       build_masks(lfi_n, mip[0], 0, 0, lfm);
897       mip2 = mip + 4;
898       if (4 >= max_cols)
899         break;
900       build_masks(lfi_n, mip2[0], 4, 2, lfm);
901       break;
902     default:
903       for (idx_32 = 0; idx_32 < 4; mip += offset_32[idx_32], ++idx_32) {
904         const int shift_y = shift_32_y[idx_32];
905         const int shift_uv = shift_32_uv[idx_32];
906         const int mi_32_col_offset = ((idx_32 & 1) << 2);
907         const int mi_32_row_offset = ((idx_32 >> 1) << 2);
908         if (mi_32_col_offset >= max_cols || mi_32_row_offset >= max_rows)
909           continue;
910         switch (mip[0]->mbmi.sb_type) {
911           case BLOCK_32X32:
912             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
913             break;
914           case BLOCK_32X16:
915             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
916             if (mi_32_row_offset + 2 >= max_rows)
917               continue;
918             mip2 = mip + mode_info_stride * 2;
919             build_masks(lfi_n, mip2[0], shift_y + 16, shift_uv + 4, lfm);
920             break;
921           case BLOCK_16X32:
922             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
923             if (mi_32_col_offset + 2 >= max_cols)
924               continue;
925             mip2 = mip + 2;
926             build_masks(lfi_n, mip2[0], shift_y + 2, shift_uv + 1, lfm);
927             break;
928           default:
929             for (idx_16 = 0; idx_16 < 4; mip += offset_16[idx_16], ++idx_16) {
930               const int shift_y = shift_32_y[idx_32] + shift_16_y[idx_16];
931               const int shift_uv = shift_32_uv[idx_32] + shift_16_uv[idx_16];
932               const int mi_16_col_offset = mi_32_col_offset +
933                   ((idx_16 & 1) << 1);
934               const int mi_16_row_offset = mi_32_row_offset +
935                   ((idx_16 >> 1) << 1);
936
937               if (mi_16_col_offset >= max_cols || mi_16_row_offset >= max_rows)
938                 continue;
939
940               switch (mip[0]->mbmi.sb_type) {
941                 case BLOCK_16X16:
942                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
943                   break;
944                 case BLOCK_16X8:
945                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
946                   if (mi_16_row_offset + 1 >= max_rows)
947                     continue;
948                   mip2 = mip + mode_info_stride;
949                   build_y_mask(lfi_n, mip2[0], shift_y+8, lfm);
950                   break;
951                 case BLOCK_8X16:
952                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
953                   if (mi_16_col_offset +1 >= max_cols)
954                     continue;
955                   mip2 = mip + 1;
956                   build_y_mask(lfi_n, mip2[0], shift_y+1, lfm);
957                   break;
958                 default: {
959                   const int shift_y = shift_32_y[idx_32] +
960                                       shift_16_y[idx_16] +
961                                       shift_8_y[0];
962                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
963                   mip += offset[0];
964                   for (idx_8 = 1; idx_8 < 4; mip += offset[idx_8], ++idx_8) {
965                     const int shift_y = shift_32_y[idx_32] +
966                                         shift_16_y[idx_16] +
967                                         shift_8_y[idx_8];
968                     const int mi_8_col_offset = mi_16_col_offset +
969                         ((idx_8 & 1));
970                     const int mi_8_row_offset = mi_16_row_offset +
971                         ((idx_8 >> 1));
972
973                     if (mi_8_col_offset >= max_cols ||
974                         mi_8_row_offset >= max_rows)
975                       continue;
976                     build_y_mask(lfi_n, mip[0], shift_y, lfm);
977                   }
978                   break;
979                 }
980               }
981             }
982             break;
983         }
984       }
985       break;
986   }
987   // The largest loopfilter we have is 16x16 so we use the 16x16 mask
988   // for 32x32 transforms also.
989   lfm->left_y[TX_16X16] |= lfm->left_y[TX_32X32];
990   lfm->above_y[TX_16X16] |= lfm->above_y[TX_32X32];
991   lfm->left_uv[TX_16X16] |= lfm->left_uv[TX_32X32];
992   lfm->above_uv[TX_16X16] |= lfm->above_uv[TX_32X32];
993
994   // We do at least 8 tap filter on every 32x32 even if the transform size
995   // is 4x4. So if the 4x4 is set on a border pixel add it to the 8x8 and
996   // remove it from the 4x4.
997   lfm->left_y[TX_8X8] |= lfm->left_y[TX_4X4] & left_border;
998   lfm->left_y[TX_4X4] &= ~left_border;
999   lfm->above_y[TX_8X8] |= lfm->above_y[TX_4X4] & above_border;
1000   lfm->above_y[TX_4X4] &= ~above_border;
1001   lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_4X4] & left_border_uv;
1002   lfm->left_uv[TX_4X4] &= ~left_border_uv;
1003   lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_4X4] & above_border_uv;
1004   lfm->above_uv[TX_4X4] &= ~above_border_uv;
1005
1006   // We do some special edge handling.
1007   if (mi_row + MI_BLOCK_SIZE > cm->mi_rows) {
1008     const uint64_t rows = cm->mi_rows - mi_row;
1009
1010     // Each pixel inside the border gets a 1,
1011     const uint64_t mask_y = (((uint64_t) 1 << (rows << 3)) - 1);
1012     const uint16_t mask_uv = (((uint16_t) 1 << (((rows + 1) >> 1) << 2)) - 1);
1013
1014     // Remove values completely outside our border.
1015     for (i = 0; i < TX_32X32; i++) {
1016       lfm->left_y[i] &= mask_y;
1017       lfm->above_y[i] &= mask_y;
1018       lfm->left_uv[i] &= mask_uv;
1019       lfm->above_uv[i] &= mask_uv;
1020     }
1021     lfm->int_4x4_y &= mask_y;
1022 #if CONFIG_MISC_FIXES
1023     lfm->above_int_4x4_uv = lfm->left_int_4x4_uv & mask_uv;
1024 #else
1025     lfm->int_4x4_uv &= mask_uv;
1026 #endif
1027
1028     // We don't apply a wide loop filter on the last uv block row. If set
1029     // apply the shorter one instead.
1030     if (rows == 1) {
1031       lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16];
1032       lfm->above_uv[TX_16X16] = 0;
1033     }
1034     if (rows == 5) {
1035       lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16] & 0xff00;
1036       lfm->above_uv[TX_16X16] &= ~(lfm->above_uv[TX_16X16] & 0xff00);
1037     }
1038   }
1039
1040   if (mi_col + MI_BLOCK_SIZE > cm->mi_cols) {
1041     const uint64_t columns = cm->mi_cols - mi_col;
1042
1043     // Each pixel inside the border gets a 1, the multiply copies the border
1044     // to where we need it.
1045     const uint64_t mask_y  = (((1 << columns) - 1)) * 0x0101010101010101ULL;
1046     const uint16_t mask_uv = ((1 << ((columns + 1) >> 1)) - 1) * 0x1111;
1047
1048     // Internal edges are not applied on the last column of the image so
1049     // we mask 1 more for the internal edges
1050     const uint16_t mask_uv_int = ((1 << (columns >> 1)) - 1) * 0x1111;
1051
1052     // Remove the bits outside the image edge.
1053     for (i = 0; i < TX_32X32; i++) {
1054       lfm->left_y[i] &= mask_y;
1055       lfm->above_y[i] &= mask_y;
1056       lfm->left_uv[i] &= mask_uv;
1057       lfm->above_uv[i] &= mask_uv;
1058     }
1059     lfm->int_4x4_y &= mask_y;
1060 #if CONFIG_MISC_FIXES
1061     lfm->left_int_4x4_uv &= mask_uv_int;
1062 #else
1063     lfm->int_4x4_uv &= mask_uv_int;
1064 #endif
1065
1066     // We don't apply a wide loop filter on the last uv column. If set
1067     // apply the shorter one instead.
1068     if (columns == 1) {
1069       lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_16X16];
1070       lfm->left_uv[TX_16X16] = 0;
1071     }
1072     if (columns == 5) {
1073       lfm->left_uv[TX_8X8] |= (lfm->left_uv[TX_16X16] & 0xcccc);
1074       lfm->left_uv[TX_16X16] &= ~(lfm->left_uv[TX_16X16] & 0xcccc);
1075     }
1076   }
1077   // We don't apply a loop filter on the first column in the image, mask that
1078   // out.
1079   if (mi_col == 0) {
1080     for (i = 0; i < TX_32X32; i++) {
1081       lfm->left_y[i] &= 0xfefefefefefefefeULL;
1082       lfm->left_uv[i] &= 0xeeee;
1083     }
1084   }
1085
1086   // Assert if we try to apply 2 different loop filters at the same position.
1087   assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_8X8]));
1088   assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_4X4]));
1089   assert(!(lfm->left_y[TX_8X8] & lfm->left_y[TX_4X4]));
1090   assert(!(lfm->int_4x4_y & lfm->left_y[TX_16X16]));
1091   assert(!(lfm->left_uv[TX_16X16]&lfm->left_uv[TX_8X8]));
1092   assert(!(lfm->left_uv[TX_16X16] & lfm->left_uv[TX_4X4]));
1093   assert(!(lfm->left_uv[TX_8X8] & lfm->left_uv[TX_4X4]));
1094 #if CONFIG_MISC_FIXES
1095   assert(!(lfm->left_int_4x4_uv & lfm->left_uv[TX_16X16]));
1096 #else
1097   assert(!(lfm->int_4x4_uv & lfm->left_uv[TX_16X16]));
1098 #endif
1099   assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_8X8]));
1100   assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_4X4]));
1101   assert(!(lfm->above_y[TX_8X8] & lfm->above_y[TX_4X4]));
1102   assert(!(lfm->int_4x4_y & lfm->above_y[TX_16X16]));
1103   assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_8X8]));
1104   assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_4X4]));
1105   assert(!(lfm->above_uv[TX_8X8] & lfm->above_uv[TX_4X4]));
1106 #if CONFIG_MISC_FIXES
1107   assert(!(lfm->above_int_4x4_uv & lfm->above_uv[TX_16X16]));
1108 #else
1109   assert(!(lfm->int_4x4_uv & lfm->above_uv[TX_16X16]));
1110 #endif
1111 }
1112
1113 static void filter_selectively_vert(uint8_t *s, int pitch,
1114                                     unsigned int mask_16x16,
1115                                     unsigned int mask_8x8,
1116                                     unsigned int mask_4x4,
1117                                     unsigned int mask_4x4_int,
1118                                     const loop_filter_info_n *lfi_n,
1119                                     const uint8_t *lfl) {
1120   unsigned int mask;
1121
1122   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
1123        mask; mask >>= 1) {
1124     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
1125
1126     if (mask & 1) {
1127       if (mask_16x16 & 1) {
1128         vpx_lpf_vertical_16(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
1129       } else if (mask_8x8 & 1) {
1130         vpx_lpf_vertical_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
1131       } else if (mask_4x4 & 1) {
1132         vpx_lpf_vertical_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
1133       }
1134     }
1135     if (mask_4x4_int & 1)
1136       vpx_lpf_vertical_4(s + 4, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
1137     s += 8;
1138     lfl += 1;
1139     mask_16x16 >>= 1;
1140     mask_8x8 >>= 1;
1141     mask_4x4 >>= 1;
1142     mask_4x4_int >>= 1;
1143   }
1144 }
1145
1146 #if CONFIG_VP9_HIGHBITDEPTH
1147 static void highbd_filter_selectively_vert(uint16_t *s, int pitch,
1148                                            unsigned int mask_16x16,
1149                                            unsigned int mask_8x8,
1150                                            unsigned int mask_4x4,
1151                                            unsigned int mask_4x4_int,
1152                                            const loop_filter_info_n *lfi_n,
1153                                            const uint8_t *lfl, int bd) {
1154   unsigned int mask;
1155
1156   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
1157        mask; mask >>= 1) {
1158     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
1159
1160     if (mask & 1) {
1161       if (mask_16x16 & 1) {
1162         vpx_highbd_lpf_vertical_16(s, pitch, lfi->mblim, lfi->lim,
1163                                    lfi->hev_thr, bd);
1164       } else if (mask_8x8 & 1) {
1165         vpx_highbd_lpf_vertical_8(s, pitch, lfi->mblim, lfi->lim,
1166                                   lfi->hev_thr, 1, bd);
1167       } else if (mask_4x4 & 1) {
1168         vpx_highbd_lpf_vertical_4(s, pitch, lfi->mblim, lfi->lim,
1169                                 lfi->hev_thr, 1, bd);
1170       }
1171     }
1172     if (mask_4x4_int & 1)
1173       vpx_highbd_lpf_vertical_4(s + 4, pitch, lfi->mblim, lfi->lim,
1174                                 lfi->hev_thr, 1, bd);
1175     s += 8;
1176     lfl += 1;
1177     mask_16x16 >>= 1;
1178     mask_8x8 >>= 1;
1179     mask_4x4 >>= 1;
1180     mask_4x4_int >>= 1;
1181   }
1182 }
1183 #endif  // CONFIG_VP9_HIGHBITDEPTH
1184
1185 void vp10_filter_block_plane_non420(VP10_COMMON *cm,
1186                                    struct macroblockd_plane *plane,
1187                                    MODE_INFO **mi_8x8,
1188                                    int mi_row, int mi_col) {
1189   const int ss_x = plane->subsampling_x;
1190   const int ss_y = plane->subsampling_y;
1191   const int row_step = 1 << ss_y;
1192   const int col_step = 1 << ss_x;
1193   const int row_step_stride = cm->mi_stride * row_step;
1194   struct buf_2d *const dst = &plane->dst;
1195   uint8_t* const dst0 = dst->buf;
1196   unsigned int mask_16x16[MI_BLOCK_SIZE] = {0};
1197   unsigned int mask_8x8[MI_BLOCK_SIZE] = {0};
1198   unsigned int mask_4x4[MI_BLOCK_SIZE] = {0};
1199   unsigned int mask_4x4_int[MI_BLOCK_SIZE] = {0};
1200   uint8_t lfl[MI_BLOCK_SIZE * MI_BLOCK_SIZE];
1201   int r, c;
1202
1203   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
1204     unsigned int mask_16x16_c = 0;
1205     unsigned int mask_8x8_c = 0;
1206     unsigned int mask_4x4_c = 0;
1207     unsigned int border_mask;
1208
1209     // Determine the vertical edges that need filtering
1210     for (c = 0; c < MI_BLOCK_SIZE && mi_col + c < cm->mi_cols; c += col_step) {
1211       const MODE_INFO *mi = mi_8x8[c];
1212       const BLOCK_SIZE sb_type = mi[0].mbmi.sb_type;
1213       const int skip_this = mi[0].mbmi.skip && is_inter_block(&mi[0].mbmi);
1214       // left edge of current unit is block/partition edge -> no skip
1215       const int block_edge_left = (num_4x4_blocks_wide_lookup[sb_type] > 1) ?
1216           !(c & (num_8x8_blocks_wide_lookup[sb_type] - 1)) : 1;
1217       const int skip_this_c = skip_this && !block_edge_left;
1218       // top edge of current unit is block/partition edge -> no skip
1219       const int block_edge_above = (num_4x4_blocks_high_lookup[sb_type] > 1) ?
1220           !(r & (num_8x8_blocks_high_lookup[sb_type] - 1)) : 1;
1221       const int skip_this_r = skip_this && !block_edge_above;
1222       const TX_SIZE tx_size = (plane->plane_type == PLANE_TYPE_UV)
1223                             ? get_uv_tx_size(&mi[0].mbmi, plane)
1224                             : mi[0].mbmi.tx_size;
1225       const int skip_border_4x4_c = ss_x && mi_col + c == cm->mi_cols - 1;
1226       const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
1227
1228       // Filter level can vary per MI
1229       if (!(lfl[(r << 3) + (c >> ss_x)] =
1230             get_filter_level(&cm->lf_info, &mi[0].mbmi)))
1231         continue;
1232
1233       // Build masks based on the transform size of each block
1234       if (tx_size == TX_32X32) {
1235         if (!skip_this_c && ((c >> ss_x) & 3) == 0) {
1236           if (!skip_border_4x4_c)
1237             mask_16x16_c |= 1 << (c >> ss_x);
1238           else
1239             mask_8x8_c |= 1 << (c >> ss_x);
1240         }
1241         if (!skip_this_r && ((r >> ss_y) & 3) == 0) {
1242           if (!skip_border_4x4_r)
1243             mask_16x16[r] |= 1 << (c >> ss_x);
1244           else
1245             mask_8x8[r] |= 1 << (c >> ss_x);
1246         }
1247       } else if (tx_size == TX_16X16) {
1248         if (!skip_this_c && ((c >> ss_x) & 1) == 0) {
1249           if (!skip_border_4x4_c)
1250             mask_16x16_c |= 1 << (c >> ss_x);
1251           else
1252             mask_8x8_c |= 1 << (c >> ss_x);
1253         }
1254         if (!skip_this_r && ((r >> ss_y) & 1) == 0) {
1255           if (!skip_border_4x4_r)
1256             mask_16x16[r] |= 1 << (c >> ss_x);
1257           else
1258             mask_8x8[r] |= 1 << (c >> ss_x);
1259         }
1260       } else {
1261         // force 8x8 filtering on 32x32 boundaries
1262         if (!skip_this_c) {
1263           if (tx_size == TX_8X8 || ((c >> ss_x) & 3) == 0)
1264             mask_8x8_c |= 1 << (c >> ss_x);
1265           else
1266             mask_4x4_c |= 1 << (c >> ss_x);
1267         }
1268
1269         if (!skip_this_r) {
1270           if (tx_size == TX_8X8 || ((r >> ss_y) & 3) == 0)
1271             mask_8x8[r] |= 1 << (c >> ss_x);
1272           else
1273             mask_4x4[r] |= 1 << (c >> ss_x);
1274         }
1275
1276         if (!skip_this && tx_size < TX_8X8 && !skip_border_4x4_c)
1277           mask_4x4_int[r] |= 1 << (c >> ss_x);
1278       }
1279     }
1280
1281     // Disable filtering on the leftmost column
1282     border_mask = ~(mi_col == 0);
1283 #if CONFIG_VP9_HIGHBITDEPTH
1284     if (cm->use_highbitdepth) {
1285       highbd_filter_selectively_vert(CONVERT_TO_SHORTPTR(dst->buf),
1286                                      dst->stride,
1287                                      mask_16x16_c & border_mask,
1288                                      mask_8x8_c & border_mask,
1289                                      mask_4x4_c & border_mask,
1290                                      mask_4x4_int[r],
1291                                      &cm->lf_info, &lfl[r << 3],
1292                                      (int)cm->bit_depth);
1293     } else {
1294       filter_selectively_vert(dst->buf, dst->stride,
1295                               mask_16x16_c & border_mask,
1296                               mask_8x8_c & border_mask,
1297                               mask_4x4_c & border_mask,
1298                               mask_4x4_int[r],
1299                               &cm->lf_info, &lfl[r << 3]);
1300     }
1301 #else
1302     filter_selectively_vert(dst->buf, dst->stride,
1303                             mask_16x16_c & border_mask,
1304                             mask_8x8_c & border_mask,
1305                             mask_4x4_c & border_mask,
1306                             mask_4x4_int[r],
1307                             &cm->lf_info, &lfl[r << 3]);
1308 #endif  // CONFIG_VP9_HIGHBITDEPTH
1309     dst->buf += 8 * dst->stride;
1310     mi_8x8 += row_step_stride;
1311   }
1312
1313   // Now do horizontal pass
1314   dst->buf = dst0;
1315   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
1316     const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
1317     const unsigned int mask_4x4_int_r = skip_border_4x4_r ? 0 : mask_4x4_int[r];
1318
1319     unsigned int mask_16x16_r;
1320     unsigned int mask_8x8_r;
1321     unsigned int mask_4x4_r;
1322
1323     if (mi_row + r == 0) {
1324       mask_16x16_r = 0;
1325       mask_8x8_r = 0;
1326       mask_4x4_r = 0;
1327     } else {
1328       mask_16x16_r = mask_16x16[r];
1329       mask_8x8_r = mask_8x8[r];
1330       mask_4x4_r = mask_4x4[r];
1331     }
1332 #if CONFIG_VP9_HIGHBITDEPTH
1333     if (cm->use_highbitdepth) {
1334       highbd_filter_selectively_horiz(CONVERT_TO_SHORTPTR(dst->buf),
1335                                       dst->stride,
1336                                       mask_16x16_r,
1337                                       mask_8x8_r,
1338                                       mask_4x4_r,
1339                                       mask_4x4_int_r,
1340                                       &cm->lf_info, &lfl[r << 3],
1341                                       (int)cm->bit_depth);
1342     } else {
1343       filter_selectively_horiz(dst->buf, dst->stride,
1344                                mask_16x16_r,
1345                                mask_8x8_r,
1346                                mask_4x4_r,
1347                                mask_4x4_int_r,
1348                                &cm->lf_info, &lfl[r << 3]);
1349     }
1350 #else
1351     filter_selectively_horiz(dst->buf, dst->stride,
1352                              mask_16x16_r,
1353                              mask_8x8_r,
1354                              mask_4x4_r,
1355                              mask_4x4_int_r,
1356                              &cm->lf_info, &lfl[r << 3]);
1357 #endif  // CONFIG_VP9_HIGHBITDEPTH
1358     dst->buf += 8 * dst->stride;
1359   }
1360 }
1361
1362 void vp10_filter_block_plane_ss00(VP10_COMMON *const cm,
1363                                  struct macroblockd_plane *const plane,
1364                                  int mi_row,
1365                                  LOOP_FILTER_MASK *lfm) {
1366   struct buf_2d *const dst = &plane->dst;
1367   uint8_t *const dst0 = dst->buf;
1368   int r;
1369   uint64_t mask_16x16 = lfm->left_y[TX_16X16];
1370   uint64_t mask_8x8 = lfm->left_y[TX_8X8];
1371   uint64_t mask_4x4 = lfm->left_y[TX_4X4];
1372   uint64_t mask_4x4_int = lfm->int_4x4_y;
1373
1374   assert(plane->subsampling_x == 0 && plane->subsampling_y == 0);
1375
1376   // Vertical pass: do 2 rows at one time
1377   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
1378     unsigned int mask_16x16_l = mask_16x16 & 0xffff;
1379     unsigned int mask_8x8_l = mask_8x8 & 0xffff;
1380     unsigned int mask_4x4_l = mask_4x4 & 0xffff;
1381     unsigned int mask_4x4_int_l = mask_4x4_int & 0xffff;
1382
1383 // Disable filtering on the leftmost column.
1384 #if CONFIG_VP9_HIGHBITDEPTH
1385     if (cm->use_highbitdepth) {
1386       highbd_filter_selectively_vert_row2(
1387           plane->subsampling_x, CONVERT_TO_SHORTPTR(dst->buf), dst->stride,
1388           mask_16x16_l, mask_8x8_l, mask_4x4_l, mask_4x4_int_l, &cm->lf_info,
1389           &lfm->lfl_y[r << 3], (int)cm->bit_depth);
1390     } else {
1391       filter_selectively_vert_row2(
1392           plane->subsampling_x, dst->buf, dst->stride, mask_16x16_l, mask_8x8_l,
1393           mask_4x4_l, mask_4x4_int_l, &cm->lf_info, &lfm->lfl_y[r << 3]);
1394     }
1395 #else
1396     filter_selectively_vert_row2(
1397         plane->subsampling_x, dst->buf, dst->stride, mask_16x16_l, mask_8x8_l,
1398         mask_4x4_l, mask_4x4_int_l, &cm->lf_info, &lfm->lfl_y[r << 3]);
1399 #endif  // CONFIG_VP9_HIGHBITDEPTH
1400     dst->buf += 16 * dst->stride;
1401     mask_16x16 >>= 16;
1402     mask_8x8 >>= 16;
1403     mask_4x4 >>= 16;
1404     mask_4x4_int >>= 16;
1405   }
1406
1407   // Horizontal pass
1408   dst->buf = dst0;
1409   mask_16x16 = lfm->above_y[TX_16X16];
1410   mask_8x8 = lfm->above_y[TX_8X8];
1411   mask_4x4 = lfm->above_y[TX_4X4];
1412   mask_4x4_int = lfm->int_4x4_y;
1413
1414   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r++) {
1415     unsigned int mask_16x16_r;
1416     unsigned int mask_8x8_r;
1417     unsigned int mask_4x4_r;
1418
1419     if (mi_row + r == 0) {
1420       mask_16x16_r = 0;
1421       mask_8x8_r = 0;
1422       mask_4x4_r = 0;
1423     } else {
1424       mask_16x16_r = mask_16x16 & 0xff;
1425       mask_8x8_r = mask_8x8 & 0xff;
1426       mask_4x4_r = mask_4x4 & 0xff;
1427     }
1428
1429 #if CONFIG_VP9_HIGHBITDEPTH
1430     if (cm->use_highbitdepth) {
1431       highbd_filter_selectively_horiz(
1432           CONVERT_TO_SHORTPTR(dst->buf), dst->stride, mask_16x16_r, mask_8x8_r,
1433           mask_4x4_r, mask_4x4_int & 0xff, &cm->lf_info, &lfm->lfl_y[r << 3],
1434           (int)cm->bit_depth);
1435     } else {
1436       filter_selectively_horiz(dst->buf, dst->stride, mask_16x16_r, mask_8x8_r,
1437                                mask_4x4_r, mask_4x4_int & 0xff, &cm->lf_info,
1438                                &lfm->lfl_y[r << 3]);
1439     }
1440 #else
1441     filter_selectively_horiz(dst->buf, dst->stride, mask_16x16_r, mask_8x8_r,
1442                              mask_4x4_r, mask_4x4_int & 0xff, &cm->lf_info,
1443                              &lfm->lfl_y[r << 3]);
1444 #endif  // CONFIG_VP9_HIGHBITDEPTH
1445
1446     dst->buf += 8 * dst->stride;
1447     mask_16x16 >>= 8;
1448     mask_8x8 >>= 8;
1449     mask_4x4 >>= 8;
1450     mask_4x4_int >>= 8;
1451   }
1452 }
1453
1454 void vp10_filter_block_plane_ss11(VP10_COMMON *const cm,
1455                                  struct macroblockd_plane *const plane,
1456                                  int mi_row,
1457                                  LOOP_FILTER_MASK *lfm) {
1458   struct buf_2d *const dst = &plane->dst;
1459   uint8_t *const dst0 = dst->buf;
1460   int r, c;
1461
1462   uint16_t mask_16x16 = lfm->left_uv[TX_16X16];
1463   uint16_t mask_8x8 = lfm->left_uv[TX_8X8];
1464   uint16_t mask_4x4 = lfm->left_uv[TX_4X4];
1465 #if CONFIG_MISC_FIXES
1466   uint16_t mask_4x4_int = lfm->left_int_4x4_uv;
1467 #else
1468   uint16_t mask_4x4_int = lfm->int_4x4_uv;
1469 #endif
1470
1471   assert(plane->subsampling_x == 1 && plane->subsampling_y == 1);
1472
1473   // Vertical pass: do 2 rows at one time
1474   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 4) {
1475     if (plane->plane_type == 1) {
1476       for (c = 0; c < (MI_BLOCK_SIZE >> 1); c++) {
1477         lfm->lfl_uv[(r << 1) + c] = lfm->lfl_y[(r << 3) + (c << 1)];
1478         lfm->lfl_uv[((r + 2) << 1) + c] = lfm->lfl_y[((r + 2) << 3) + (c << 1)];
1479       }
1480     }
1481
1482     {
1483       unsigned int mask_16x16_l = mask_16x16 & 0xff;
1484       unsigned int mask_8x8_l = mask_8x8 & 0xff;
1485       unsigned int mask_4x4_l = mask_4x4 & 0xff;
1486       unsigned int mask_4x4_int_l = mask_4x4_int & 0xff;
1487
1488 // Disable filtering on the leftmost column.
1489 #if CONFIG_VP9_HIGHBITDEPTH
1490       if (cm->use_highbitdepth) {
1491         highbd_filter_selectively_vert_row2(
1492             plane->subsampling_x, CONVERT_TO_SHORTPTR(dst->buf), dst->stride,
1493             mask_16x16_l, mask_8x8_l, mask_4x4_l, mask_4x4_int_l, &cm->lf_info,
1494             &lfm->lfl_uv[r << 1], (int)cm->bit_depth);
1495       } else {
1496         filter_selectively_vert_row2(
1497             plane->subsampling_x, dst->buf, dst->stride,
1498             mask_16x16_l, mask_8x8_l, mask_4x4_l, mask_4x4_int_l, &cm->lf_info,
1499             &lfm->lfl_uv[r << 1]);
1500       }
1501 #else
1502       filter_selectively_vert_row2(
1503           plane->subsampling_x, dst->buf, dst->stride,
1504           mask_16x16_l, mask_8x8_l, mask_4x4_l, mask_4x4_int_l, &cm->lf_info,
1505           &lfm->lfl_uv[r << 1]);
1506 #endif  // CONFIG_VP9_HIGHBITDEPTH
1507
1508       dst->buf += 16 * dst->stride;
1509       mask_16x16 >>= 8;
1510       mask_8x8 >>= 8;
1511       mask_4x4 >>= 8;
1512       mask_4x4_int >>= 8;
1513     }
1514   }
1515
1516   // Horizontal pass
1517   dst->buf = dst0;
1518   mask_16x16 = lfm->above_uv[TX_16X16];
1519   mask_8x8 = lfm->above_uv[TX_8X8];
1520   mask_4x4 = lfm->above_uv[TX_4X4];
1521 #if CONFIG_MISC_FIXES
1522   mask_4x4_int = lfm->above_int_4x4_uv;
1523 #else
1524   mask_4x4_int = lfm->int_4x4_uv;
1525 #endif
1526
1527   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
1528     const int skip_border_4x4_r = mi_row + r == cm->mi_rows - 1;
1529     const unsigned int mask_4x4_int_r =
1530         skip_border_4x4_r ? 0 : (mask_4x4_int & 0xf);
1531     unsigned int mask_16x16_r;
1532     unsigned int mask_8x8_r;
1533     unsigned int mask_4x4_r;
1534
1535     if (mi_row + r == 0) {
1536       mask_16x16_r = 0;
1537       mask_8x8_r = 0;
1538       mask_4x4_r = 0;
1539     } else {
1540       mask_16x16_r = mask_16x16 & 0xf;
1541       mask_8x8_r = mask_8x8 & 0xf;
1542       mask_4x4_r = mask_4x4 & 0xf;
1543     }
1544
1545 #if CONFIG_VP9_HIGHBITDEPTH
1546     if (cm->use_highbitdepth) {
1547       highbd_filter_selectively_horiz(CONVERT_TO_SHORTPTR(dst->buf),
1548                                       dst->stride, mask_16x16_r, mask_8x8_r,
1549                                       mask_4x4_r, mask_4x4_int_r, &cm->lf_info,
1550                                       &lfm->lfl_uv[r << 1], (int)cm->bit_depth);
1551     } else {
1552       filter_selectively_horiz(dst->buf, dst->stride, mask_16x16_r, mask_8x8_r,
1553                                mask_4x4_r, mask_4x4_int_r, &cm->lf_info,
1554                                &lfm->lfl_uv[r << 1]);
1555     }
1556 #else
1557     filter_selectively_horiz(dst->buf, dst->stride, mask_16x16_r, mask_8x8_r,
1558                              mask_4x4_r, mask_4x4_int_r, &cm->lf_info,
1559                              &lfm->lfl_uv[r << 1]);
1560 #endif  // CONFIG_VP9_HIGHBITDEPTH
1561
1562     dst->buf += 8 * dst->stride;
1563     mask_16x16 >>= 4;
1564     mask_8x8 >>= 4;
1565     mask_4x4 >>= 4;
1566     mask_4x4_int >>= 4;
1567   }
1568 }
1569
1570 void vp10_loop_filter_rows(YV12_BUFFER_CONFIG *frame_buffer,
1571                           VP10_COMMON *cm,
1572                           struct macroblockd_plane planes[MAX_MB_PLANE],
1573                           int start, int stop, int y_only) {
1574   const int num_planes = y_only ? 1 : MAX_MB_PLANE;
1575   enum lf_path path;
1576   LOOP_FILTER_MASK lfm;
1577   int mi_row, mi_col;
1578
1579   if (y_only)
1580     path = LF_PATH_444;
1581   else if (planes[1].subsampling_y == 1 && planes[1].subsampling_x == 1)
1582     path = LF_PATH_420;
1583   else if (planes[1].subsampling_y == 0 && planes[1].subsampling_x == 0)
1584     path = LF_PATH_444;
1585   else
1586     path = LF_PATH_SLOW;
1587
1588   for (mi_row = start; mi_row < stop; mi_row += MI_BLOCK_SIZE) {
1589     MODE_INFO **mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
1590
1591     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) {
1592       int plane;
1593
1594       vp10_setup_dst_planes(planes, frame_buffer, mi_row, mi_col);
1595
1596       // TODO(JBB): Make setup_mask work for non 420.
1597       vp10_setup_mask(cm, mi_row, mi_col, mi + mi_col, cm->mi_stride,
1598                      &lfm);
1599
1600       vp10_filter_block_plane_ss00(cm, &planes[0], mi_row, &lfm);
1601       for (plane = 1; plane < num_planes; ++plane) {
1602         switch (path) {
1603           case LF_PATH_420:
1604             vp10_filter_block_plane_ss11(cm, &planes[plane], mi_row, &lfm);
1605             break;
1606           case LF_PATH_444:
1607             vp10_filter_block_plane_ss00(cm, &planes[plane], mi_row, &lfm);
1608             break;
1609           case LF_PATH_SLOW:
1610             vp10_filter_block_plane_non420(cm, &planes[plane], mi + mi_col,
1611                                           mi_row, mi_col);
1612             break;
1613         }
1614       }
1615     }
1616   }
1617 }
1618
1619 void vp10_loop_filter_frame(YV12_BUFFER_CONFIG *frame,
1620                            VP10_COMMON *cm, MACROBLOCKD *xd,
1621                            int frame_filter_level,
1622                            int y_only, int partial_frame) {
1623   int start_mi_row, end_mi_row, mi_rows_to_filter;
1624   if (!frame_filter_level) return;
1625   start_mi_row = 0;
1626   mi_rows_to_filter = cm->mi_rows;
1627   if (partial_frame && cm->mi_rows > 8) {
1628     start_mi_row = cm->mi_rows >> 1;
1629     start_mi_row &= 0xfffffff8;
1630     mi_rows_to_filter = VPXMAX(cm->mi_rows / 8, 8);
1631   }
1632   end_mi_row = start_mi_row + mi_rows_to_filter;
1633   vp10_loop_filter_frame_init(cm, frame_filter_level);
1634   vp10_loop_filter_rows(frame, cm, xd->plane,
1635                        start_mi_row, end_mi_row,
1636                        y_only);
1637 }
1638
1639 void vp10_loop_filter_data_reset(
1640     LFWorkerData *lf_data, YV12_BUFFER_CONFIG *frame_buffer,
1641     struct VP10Common *cm,
1642     const struct macroblockd_plane planes[MAX_MB_PLANE]) {
1643   lf_data->frame_buffer = frame_buffer;
1644   lf_data->cm = cm;
1645   lf_data->start = 0;
1646   lf_data->stop = 0;
1647   lf_data->y_only = 0;
1648   memcpy(lf_data->planes, planes, sizeof(lf_data->planes));
1649 }
1650
1651 int vp10_loop_filter_worker(LFWorkerData *const lf_data, void *unused) {
1652   (void)unused;
1653   vp10_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
1654                        lf_data->start, lf_data->stop, lf_data->y_only);
1655   return 1;
1656 }