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 "vp9/common/vp9_loopfilter.h"
13 #include "vp9/common/vp9_onyxc_int.h"
14 #include "vp9/common/vp9_reconinter.h"
15 #include "vpx_mem/vpx_mem.h"
16 
17 #include "vp9/common/vp9_seg_common.h"
18 
19 // 64 bit masks for left transform size. Each 1 represents a position where
20 // we should apply a loop filter across the left border of an 8x8 block
21 // boundary.
22 //
23 // In the case of TX_16X16->  ( in low order byte first we end up with
24 // a mask that looks like this
25 //
26 //    10101010
27 //    10101010
28 //    10101010
29 //    10101010
30 //    10101010
31 //    10101010
32 //    10101010
33 //    10101010
34 //
35 // A loopfilter should be applied to every other 8x8 horizontally.
36 static const uint64_t left_64x64_txform_mask[TX_SIZES]= {
37   0xffffffffffffffff,  // TX_4X4
38   0xffffffffffffffff,  // TX_8x8
39   0x5555555555555555,  // TX_16x16
40   0x1111111111111111,  // TX_32x32
41 };
42 
43 // 64 bit masks for above transform size. Each 1 represents a position where
44 // we should apply a loop filter across the top border of an 8x8 block
45 // boundary.
46 //
47 // In the case of TX_32x32 ->  ( in low order byte first we end up with
48 // a mask that looks like this
49 //
50 //    11111111
51 //    00000000
52 //    00000000
53 //    00000000
54 //    11111111
55 //    00000000
56 //    00000000
57 //    00000000
58 //
59 // A loopfilter should be applied to every other 4 the row vertically.
60 static const uint64_t above_64x64_txform_mask[TX_SIZES]= {
61   0xffffffffffffffff,  // TX_4X4
62   0xffffffffffffffff,  // TX_8x8
63   0x00ff00ff00ff00ff,  // TX_16x16
64   0x000000ff000000ff,  // TX_32x32
65 };
66 
67 // 64 bit masks for prediction sizes (left). Each 1 represents a position
68 // where left border of an 8x8 block. These are aligned to the right most
69 // appropriate bit, and then shifted into place.
70 //
71 // In the case of TX_16x32 ->  ( low order byte first ) we end up with
72 // a mask that looks like this :
73 //
74 //  10000000
75 //  10000000
76 //  10000000
77 //  10000000
78 //  00000000
79 //  00000000
80 //  00000000
81 //  00000000
82 static const uint64_t left_prediction_mask[BLOCK_SIZES] = {
83   0x0000000000000001,  // BLOCK_4X4,
84   0x0000000000000001,  // BLOCK_4X8,
85   0x0000000000000001,  // BLOCK_8X4,
86   0x0000000000000001,  // BLOCK_8X8,
87   0x0000000000000101,  // BLOCK_8X16,
88   0x0000000000000001,  // BLOCK_16X8,
89   0x0000000000000101,  // BLOCK_16X16,
90   0x0000000001010101,  // BLOCK_16X32,
91   0x0000000000000101,  // BLOCK_32X16,
92   0x0000000001010101,  // BLOCK_32X32,
93   0x0101010101010101,  // BLOCK_32X64,
94   0x0000000001010101,  // BLOCK_64X32,
95   0x0101010101010101,  // BLOCK_64X64
96 };
97 
98 // 64 bit mask to shift and set for each prediction size.
99 static const uint64_t above_prediction_mask[BLOCK_SIZES] = {
100   0x0000000000000001,  // BLOCK_4X4
101   0x0000000000000001,  // BLOCK_4X8
102   0x0000000000000001,  // BLOCK_8X4
103   0x0000000000000001,  // BLOCK_8X8
104   0x0000000000000001,  // BLOCK_8X16,
105   0x0000000000000003,  // BLOCK_16X8
106   0x0000000000000003,  // BLOCK_16X16
107   0x0000000000000003,  // BLOCK_16X32,
108   0x000000000000000f,  // BLOCK_32X16,
109   0x000000000000000f,  // BLOCK_32X32,
110   0x000000000000000f,  // BLOCK_32X64,
111   0x00000000000000ff,  // BLOCK_64X32,
112   0x00000000000000ff,  // BLOCK_64X64
113 };
114 // 64 bit mask to shift and set for each prediction size. A bit is set for
115 // each 8x8 block that would be in the left most block of the given block
116 // size in the 64x64 block.
117 static const uint64_t size_mask[BLOCK_SIZES] = {
118   0x0000000000000001,  // BLOCK_4X4
119   0x0000000000000001,  // BLOCK_4X8
120   0x0000000000000001,  // BLOCK_8X4
121   0x0000000000000001,  // BLOCK_8X8
122   0x0000000000000101,  // BLOCK_8X16,
123   0x0000000000000003,  // BLOCK_16X8
124   0x0000000000000303,  // BLOCK_16X16
125   0x0000000003030303,  // BLOCK_16X32,
126   0x0000000000000f0f,  // BLOCK_32X16,
127   0x000000000f0f0f0f,  // BLOCK_32X32,
128   0x0f0f0f0f0f0f0f0f,  // BLOCK_32X64,
129   0x00000000ffffffff,  // BLOCK_64X32,
130   0xffffffffffffffff,  // BLOCK_64X64
131 };
132 
133 // These are used for masking the left and above borders.
134 static const uint64_t left_border =  0x1111111111111111;
135 static const uint64_t above_border = 0x000000ff000000ff;
136 
137 // 16 bit masks for uv transform sizes.
138 static const uint16_t left_64x64_txform_mask_uv[TX_SIZES]= {
139   0xffff,  // TX_4X4
140   0xffff,  // TX_8x8
141   0x5555,  // TX_16x16
142   0x1111,  // TX_32x32
143 };
144 
145 static const uint16_t above_64x64_txform_mask_uv[TX_SIZES]= {
146   0xffff,  // TX_4X4
147   0xffff,  // TX_8x8
148   0x0f0f,  // TX_16x16
149   0x000f,  // TX_32x32
150 };
151 
152 // 16 bit left mask to shift and set for each uv prediction size.
153 static const uint16_t left_prediction_mask_uv[BLOCK_SIZES] = {
154   0x0001,  // BLOCK_4X4,
155   0x0001,  // BLOCK_4X8,
156   0x0001,  // BLOCK_8X4,
157   0x0001,  // BLOCK_8X8,
158   0x0001,  // BLOCK_8X16,
159   0x0001,  // BLOCK_16X8,
160   0x0001,  // BLOCK_16X16,
161   0x0011,  // BLOCK_16X32,
162   0x0001,  // BLOCK_32X16,
163   0x0011,  // BLOCK_32X32,
164   0x1111,  // BLOCK_32X64
165   0x0011,  // BLOCK_64X32,
166   0x1111,  // BLOCK_64X64
167 };
168 // 16 bit above mask to shift and set for uv each prediction size.
169 static const uint16_t above_prediction_mask_uv[BLOCK_SIZES] = {
170   0x0001,  // BLOCK_4X4
171   0x0001,  // BLOCK_4X8
172   0x0001,  // BLOCK_8X4
173   0x0001,  // BLOCK_8X8
174   0x0001,  // BLOCK_8X16,
175   0x0001,  // BLOCK_16X8
176   0x0001,  // BLOCK_16X16
177   0x0001,  // BLOCK_16X32,
178   0x0003,  // BLOCK_32X16,
179   0x0003,  // BLOCK_32X32,
180   0x0003,  // BLOCK_32X64,
181   0x000f,  // BLOCK_64X32,
182   0x000f,  // BLOCK_64X64
183 };
184 
185 // 64 bit mask to shift and set for each uv prediction size
186 static const uint16_t size_mask_uv[BLOCK_SIZES] = {
187   0x0001,  // BLOCK_4X4
188   0x0001,  // BLOCK_4X8
189   0x0001,  // BLOCK_8X4
190   0x0001,  // BLOCK_8X8
191   0x0001,  // BLOCK_8X16,
192   0x0001,  // BLOCK_16X8
193   0x0001,  // BLOCK_16X16
194   0x0011,  // BLOCK_16X32,
195   0x0003,  // BLOCK_32X16,
196   0x0033,  // BLOCK_32X32,
197   0x3333,  // BLOCK_32X64,
198   0x00ff,  // BLOCK_64X32,
199   0xffff,  // BLOCK_64X64
200 };
201 static const uint16_t left_border_uv =  0x1111;
202 static const uint16_t above_border_uv = 0x000f;
203 
204 static const int mode_lf_lut[MB_MODE_COUNT] = {
205   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // INTRA_MODES
206   1, 1, 0, 1                     // INTER_MODES (ZEROMV == 0)
207 };
208 
update_sharpness(loop_filter_info_n * lfi,int sharpness_lvl)209 static void update_sharpness(loop_filter_info_n *lfi, int sharpness_lvl) {
210   int lvl;
211 
212   // For each possible value for the loop filter fill out limits
213   for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++) {
214     // Set loop filter parameters that control sharpness.
215     int block_inside_limit = lvl >> ((sharpness_lvl > 0) + (sharpness_lvl > 4));
216 
217     if (sharpness_lvl > 0) {
218       if (block_inside_limit > (9 - sharpness_lvl))
219         block_inside_limit = (9 - sharpness_lvl);
220     }
221 
222     if (block_inside_limit < 1)
223       block_inside_limit = 1;
224 
225     vpx_memset(lfi->lfthr[lvl].lim, block_inside_limit, SIMD_WIDTH);
226     vpx_memset(lfi->lfthr[lvl].mblim, (2 * (lvl + 2) + block_inside_limit),
227                SIMD_WIDTH);
228   }
229 }
230 
get_filter_level(const loop_filter_info_n * lfi_n,const MB_MODE_INFO * mbmi)231 static uint8_t get_filter_level(const loop_filter_info_n *lfi_n,
232                                 const MB_MODE_INFO *mbmi) {
233   return lfi_n->lvl[mbmi->segment_id][mbmi->ref_frame[0]]
234                    [mode_lf_lut[mbmi->mode]];
235 }
236 
vp9_loop_filter_init(VP9_COMMON * cm)237 void vp9_loop_filter_init(VP9_COMMON *cm) {
238   loop_filter_info_n *lfi = &cm->lf_info;
239   struct loopfilter *lf = &cm->lf;
240   int lvl;
241 
242   // init limits for given sharpness
243   update_sharpness(lfi, lf->sharpness_level);
244   lf->last_sharpness_level = lf->sharpness_level;
245 
246   // init hev threshold const vectors
247   for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++)
248     vpx_memset(lfi->lfthr[lvl].hev_thr, (lvl >> 4), SIMD_WIDTH);
249 }
250 
vp9_loop_filter_frame_init(VP9_COMMON * cm,int default_filt_lvl)251 void vp9_loop_filter_frame_init(VP9_COMMON *cm, int default_filt_lvl) {
252   int seg_id;
253   // n_shift is the multiplier for lf_deltas
254   // the multiplier is 1 for when filter_lvl is between 0 and 31;
255   // 2 when filter_lvl is between 32 and 63
256   const int scale = 1 << (default_filt_lvl >> 5);
257   loop_filter_info_n *const lfi = &cm->lf_info;
258   struct loopfilter *const lf = &cm->lf;
259   const struct segmentation *const seg = &cm->seg;
260 
261   // update limits if sharpness has changed
262   if (lf->last_sharpness_level != lf->sharpness_level) {
263     update_sharpness(lfi, lf->sharpness_level);
264     lf->last_sharpness_level = lf->sharpness_level;
265   }
266 
267   for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) {
268     int lvl_seg = default_filt_lvl;
269     if (vp9_segfeature_active(seg, seg_id, SEG_LVL_ALT_LF)) {
270       const int data = vp9_get_segdata(seg, seg_id, SEG_LVL_ALT_LF);
271       lvl_seg = clamp(seg->abs_delta == SEGMENT_ABSDATA ?
272                       data : default_filt_lvl + data,
273                       0, MAX_LOOP_FILTER);
274     }
275 
276     if (!lf->mode_ref_delta_enabled) {
277       // we could get rid of this if we assume that deltas are set to
278       // zero when not in use; encoder always uses deltas
279       vpx_memset(lfi->lvl[seg_id], lvl_seg, sizeof(lfi->lvl[seg_id]));
280     } else {
281       int ref, mode;
282       const int intra_lvl = lvl_seg + lf->ref_deltas[INTRA_FRAME] * scale;
283       lfi->lvl[seg_id][INTRA_FRAME][0] = clamp(intra_lvl, 0, MAX_LOOP_FILTER);
284 
285       for (ref = LAST_FRAME; ref < MAX_REF_FRAMES; ++ref) {
286         for (mode = 0; mode < MAX_MODE_LF_DELTAS; ++mode) {
287           const int inter_lvl = lvl_seg + lf->ref_deltas[ref] * scale
288                                         + lf->mode_deltas[mode] * scale;
289           lfi->lvl[seg_id][ref][mode] = clamp(inter_lvl, 0, MAX_LOOP_FILTER);
290         }
291       }
292     }
293   }
294 }
295 
filter_selectively_vert_row2(PLANE_TYPE plane_type,uint8_t * s,int pitch,unsigned int mask_16x16_l,unsigned int mask_8x8_l,unsigned int mask_4x4_l,unsigned int mask_4x4_int_l,const loop_filter_info_n * lfi_n,const uint8_t * lfl)296 static void filter_selectively_vert_row2(PLANE_TYPE plane_type,
297                                          uint8_t *s, int pitch,
298                                          unsigned int mask_16x16_l,
299                                          unsigned int mask_8x8_l,
300                                          unsigned int mask_4x4_l,
301                                          unsigned int mask_4x4_int_l,
302                                          const loop_filter_info_n *lfi_n,
303                                          const uint8_t *lfl) {
304   const int mask_shift = plane_type ? 4 : 8;
305   const int mask_cutoff = plane_type ? 0xf : 0xff;
306   const int lfl_forward = plane_type ? 4 : 8;
307 
308   unsigned int mask_16x16_0 = mask_16x16_l & mask_cutoff;
309   unsigned int mask_8x8_0 = mask_8x8_l & mask_cutoff;
310   unsigned int mask_4x4_0 = mask_4x4_l & mask_cutoff;
311   unsigned int mask_4x4_int_0 = mask_4x4_int_l & mask_cutoff;
312   unsigned int mask_16x16_1 = (mask_16x16_l >> mask_shift) & mask_cutoff;
313   unsigned int mask_8x8_1 = (mask_8x8_l >> mask_shift) & mask_cutoff;
314   unsigned int mask_4x4_1 = (mask_4x4_l >> mask_shift) & mask_cutoff;
315   unsigned int mask_4x4_int_1 = (mask_4x4_int_l >> mask_shift) & mask_cutoff;
316   unsigned int mask;
317 
318   for (mask = mask_16x16_0 | mask_8x8_0 | mask_4x4_0 | mask_4x4_int_0 |
319               mask_16x16_1 | mask_8x8_1 | mask_4x4_1 | mask_4x4_int_1;
320        mask; mask >>= 1) {
321     const loop_filter_thresh *lfi0 = lfi_n->lfthr + *lfl;
322     const loop_filter_thresh *lfi1 = lfi_n->lfthr + *(lfl + lfl_forward);
323 
324     // TODO(yunqingwang): count in loopfilter functions should be removed.
325     if (mask & 1) {
326       if ((mask_16x16_0 | mask_16x16_1) & 1) {
327         if ((mask_16x16_0 & mask_16x16_1) & 1) {
328           vp9_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
329                                    lfi0->hev_thr);
330         } else if (mask_16x16_0 & 1) {
331           vp9_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim,
332                               lfi0->hev_thr);
333         } else {
334           vp9_lpf_vertical_16(s + 8 *pitch, pitch, lfi1->mblim,
335                               lfi1->lim, lfi1->hev_thr);
336         }
337       }
338 
339       if ((mask_8x8_0 | mask_8x8_1) & 1) {
340         if ((mask_8x8_0 & mask_8x8_1) & 1) {
341           vp9_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
342                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
343                                   lfi1->hev_thr);
344         } else if (mask_8x8_0 & 1) {
345           vp9_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr,
346                              1);
347         } else {
348           vp9_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
349                              lfi1->hev_thr, 1);
350         }
351       }
352 
353       if ((mask_4x4_0 | mask_4x4_1) & 1) {
354         if ((mask_4x4_0 & mask_4x4_1) & 1) {
355           vp9_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
356                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
357                                   lfi1->hev_thr);
358         } else if (mask_4x4_0 & 1) {
359           vp9_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr,
360                              1);
361         } else {
362           vp9_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
363                              lfi1->hev_thr, 1);
364         }
365       }
366 
367       if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
368         if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
369           vp9_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
370                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
371                                   lfi1->hev_thr);
372         } else if (mask_4x4_int_0 & 1) {
373           vp9_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
374                              lfi0->hev_thr, 1);
375         } else {
376           vp9_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim, lfi1->lim,
377                              lfi1->hev_thr, 1);
378         }
379       }
380     }
381 
382     s += 8;
383     lfl += 1;
384     mask_16x16_0 >>= 1;
385     mask_8x8_0 >>= 1;
386     mask_4x4_0 >>= 1;
387     mask_4x4_int_0 >>= 1;
388     mask_16x16_1 >>= 1;
389     mask_8x8_1 >>= 1;
390     mask_4x4_1 >>= 1;
391     mask_4x4_int_1 >>= 1;
392   }
393 }
394 
filter_selectively_horiz(uint8_t * s,int pitch,unsigned int mask_16x16,unsigned int mask_8x8,unsigned int mask_4x4,unsigned int mask_4x4_int,const loop_filter_info_n * lfi_n,const uint8_t * lfl)395 static void filter_selectively_horiz(uint8_t *s, int pitch,
396                                      unsigned int mask_16x16,
397                                      unsigned int mask_8x8,
398                                      unsigned int mask_4x4,
399                                      unsigned int mask_4x4_int,
400                                      const loop_filter_info_n *lfi_n,
401                                      const uint8_t *lfl) {
402   unsigned int mask;
403   int count;
404 
405   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
406        mask; mask >>= count) {
407     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
408 
409     count = 1;
410     if (mask & 1) {
411       if (mask_16x16 & 1) {
412         if ((mask_16x16 & 3) == 3) {
413           vp9_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
414                                 lfi->hev_thr, 2);
415           count = 2;
416         } else {
417           vp9_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
418                                 lfi->hev_thr, 1);
419         }
420       } else if (mask_8x8 & 1) {
421         if ((mask_8x8 & 3) == 3) {
422           // Next block's thresholds
423           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
424 
425           vp9_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
426                                     lfi->hev_thr, lfin->mblim, lfin->lim,
427                                     lfin->hev_thr);
428 
429           if ((mask_4x4_int & 3) == 3) {
430             vp9_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
431                                       lfi->lim, lfi->hev_thr, lfin->mblim,
432                                       lfin->lim, lfin->hev_thr);
433           } else {
434             if (mask_4x4_int & 1)
435               vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
436                                    lfi->hev_thr, 1);
437             else if (mask_4x4_int & 2)
438               vp9_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
439                                    lfin->lim, lfin->hev_thr, 1);
440           }
441           count = 2;
442         } else {
443           vp9_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
444 
445           if (mask_4x4_int & 1)
446             vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
447                                  lfi->hev_thr, 1);
448         }
449       } else if (mask_4x4 & 1) {
450         if ((mask_4x4 & 3) == 3) {
451           // Next block's thresholds
452           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
453 
454           vp9_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
455                                     lfi->hev_thr, lfin->mblim, lfin->lim,
456                                     lfin->hev_thr);
457           if ((mask_4x4_int & 3) == 3) {
458             vp9_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
459                                       lfi->lim, lfi->hev_thr, lfin->mblim,
460                                       lfin->lim, lfin->hev_thr);
461           } else {
462             if (mask_4x4_int & 1)
463               vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
464                                    lfi->hev_thr, 1);
465             else if (mask_4x4_int & 2)
466               vp9_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
467                                    lfin->lim, lfin->hev_thr, 1);
468           }
469           count = 2;
470         } else {
471           vp9_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
472 
473           if (mask_4x4_int & 1)
474             vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
475                                  lfi->hev_thr, 1);
476         }
477       } else if (mask_4x4_int & 1) {
478         vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
479                              lfi->hev_thr, 1);
480       }
481     }
482     s += 8 * count;
483     lfl += count;
484     mask_16x16 >>= count;
485     mask_8x8 >>= count;
486     mask_4x4 >>= count;
487     mask_4x4_int >>= count;
488   }
489 }
490 
491 // This function ors into the current lfm structure, where to do loop
492 // filters for the specific mi we are looking at. It uses information
493 // including the block_size_type (32x16, 32x32, etc.), the transform size,
494 // whether there were any coefficients encoded, and the loop filter strength
495 // block we are currently looking at. Shift is used to position the
496 // 1's we produce.
497 // TODO(JBB) Need another function for different resolution color..
build_masks(const loop_filter_info_n * const lfi_n,const MODE_INFO * mi,const int shift_y,const int shift_uv,LOOP_FILTER_MASK * lfm)498 static void build_masks(const loop_filter_info_n *const lfi_n,
499                         const MODE_INFO *mi, const int shift_y,
500                         const int shift_uv,
501                         LOOP_FILTER_MASK *lfm) {
502   const MB_MODE_INFO *mbmi = &mi->mbmi;
503   const BLOCK_SIZE block_size = mbmi->sb_type;
504   const TX_SIZE tx_size_y = mbmi->tx_size;
505   const TX_SIZE tx_size_uv = get_uv_tx_size_impl(tx_size_y, block_size, 1, 1);
506   const int filter_level = get_filter_level(lfi_n, mbmi);
507   uint64_t *const left_y = &lfm->left_y[tx_size_y];
508   uint64_t *const above_y = &lfm->above_y[tx_size_y];
509   uint64_t *const int_4x4_y = &lfm->int_4x4_y;
510   uint16_t *const left_uv = &lfm->left_uv[tx_size_uv];
511   uint16_t *const above_uv = &lfm->above_uv[tx_size_uv];
512   uint16_t *const int_4x4_uv = &lfm->int_4x4_uv;
513   int i;
514 
515   // If filter level is 0 we don't loop filter.
516   if (!filter_level) {
517     return;
518   } else {
519     const int w = num_8x8_blocks_wide_lookup[block_size];
520     const int h = num_8x8_blocks_high_lookup[block_size];
521     int index = shift_y;
522     for (i = 0; i < h; i++) {
523       vpx_memset(&lfm->lfl_y[index], filter_level, w);
524       index += 8;
525     }
526   }
527 
528   // These set 1 in the current block size for the block size edges.
529   // For instance if the block size is 32x16, we'll set:
530   //    above =   1111
531   //              0000
532   //    and
533   //    left  =   1000
534   //          =   1000
535   // NOTE : In this example the low bit is left most ( 1000 ) is stored as
536   //        1,  not 8...
537   //
538   // U and V set things on a 16 bit scale.
539   //
540   *above_y |= above_prediction_mask[block_size] << shift_y;
541   *above_uv |= above_prediction_mask_uv[block_size] << shift_uv;
542   *left_y |= left_prediction_mask[block_size] << shift_y;
543   *left_uv |= left_prediction_mask_uv[block_size] << shift_uv;
544 
545   // If the block has no coefficients and is not intra we skip applying
546   // the loop filter on block edges.
547   if (mbmi->skip && is_inter_block(mbmi))
548     return;
549 
550   // Here we are adding a mask for the transform size. The transform
551   // size mask is set to be correct for a 64x64 prediction block size. We
552   // mask to match the size of the block we are working on and then shift it
553   // into place..
554   *above_y |= (size_mask[block_size] &
555                above_64x64_txform_mask[tx_size_y]) << shift_y;
556   *above_uv |= (size_mask_uv[block_size] &
557                 above_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
558 
559   *left_y |= (size_mask[block_size] &
560               left_64x64_txform_mask[tx_size_y]) << shift_y;
561   *left_uv |= (size_mask_uv[block_size] &
562                left_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
563 
564   // Here we are trying to determine what to do with the internal 4x4 block
565   // boundaries.  These differ from the 4x4 boundaries on the outside edge of
566   // an 8x8 in that the internal ones can be skipped and don't depend on
567   // the prediction block size.
568   if (tx_size_y == TX_4X4)
569     *int_4x4_y |= (size_mask[block_size] & 0xffffffffffffffff) << shift_y;
570 
571   if (tx_size_uv == TX_4X4)
572     *int_4x4_uv |= (size_mask_uv[block_size] & 0xffff) << shift_uv;
573 }
574 
575 // This function does the same thing as the one above with the exception that
576 // it only affects the y masks. It exists because for blocks < 16x16 in size,
577 // we only update u and v masks on the first block.
build_y_mask(const loop_filter_info_n * const lfi_n,const MODE_INFO * mi,const int shift_y,LOOP_FILTER_MASK * lfm)578 static void build_y_mask(const loop_filter_info_n *const lfi_n,
579                          const MODE_INFO *mi, const int shift_y,
580                          LOOP_FILTER_MASK *lfm) {
581   const MB_MODE_INFO *mbmi = &mi->mbmi;
582   const BLOCK_SIZE block_size = mbmi->sb_type;
583   const TX_SIZE tx_size_y = mbmi->tx_size;
584   const int filter_level = get_filter_level(lfi_n, mbmi);
585   uint64_t *const left_y = &lfm->left_y[tx_size_y];
586   uint64_t *const above_y = &lfm->above_y[tx_size_y];
587   uint64_t *const int_4x4_y = &lfm->int_4x4_y;
588   int i;
589 
590   if (!filter_level) {
591     return;
592   } else {
593     const int w = num_8x8_blocks_wide_lookup[block_size];
594     const int h = num_8x8_blocks_high_lookup[block_size];
595     int index = shift_y;
596     for (i = 0; i < h; i++) {
597       vpx_memset(&lfm->lfl_y[index], filter_level, w);
598       index += 8;
599     }
600   }
601 
602   *above_y |= above_prediction_mask[block_size] << shift_y;
603   *left_y |= left_prediction_mask[block_size] << shift_y;
604 
605   if (mbmi->skip && is_inter_block(mbmi))
606     return;
607 
608   *above_y |= (size_mask[block_size] &
609                above_64x64_txform_mask[tx_size_y]) << shift_y;
610 
611   *left_y |= (size_mask[block_size] &
612               left_64x64_txform_mask[tx_size_y]) << shift_y;
613 
614   if (tx_size_y == TX_4X4)
615     *int_4x4_y |= (size_mask[block_size] & 0xffffffffffffffff) << shift_y;
616 }
617 
618 // This function sets up the bit masks for the entire 64x64 region represented
619 // by mi_row, mi_col.
620 // TODO(JBB): This function only works for yv12.
vp9_setup_mask(VP9_COMMON * const cm,const int mi_row,const int mi_col,MODE_INFO ** mi,const int mode_info_stride,LOOP_FILTER_MASK * lfm)621 void vp9_setup_mask(VP9_COMMON *const cm, const int mi_row, const int mi_col,
622                     MODE_INFO **mi, const int mode_info_stride,
623                     LOOP_FILTER_MASK *lfm) {
624   int idx_32, idx_16, idx_8;
625   const loop_filter_info_n *const lfi_n = &cm->lf_info;
626   MODE_INFO **mip = mi;
627   MODE_INFO **mip2 = mi;
628 
629   // These are offsets to the next mi in the 64x64 block. It is what gets
630   // added to the mi ptr as we go through each loop. It helps us to avoid
631   // setting up special row and column counters for each index. The last step
632   // brings us out back to the starting position.
633   const int offset_32[] = {4, (mode_info_stride << 2) - 4, 4,
634                            -(mode_info_stride << 2) - 4};
635   const int offset_16[] = {2, (mode_info_stride << 1) - 2, 2,
636                            -(mode_info_stride << 1) - 2};
637   const int offset[] = {1, mode_info_stride - 1, 1, -mode_info_stride - 1};
638 
639   // Following variables represent shifts to position the current block
640   // mask over the appropriate block. A shift of 36 to the left will move
641   // the bits for the final 32 by 32 block in the 64x64 up 4 rows and left
642   // 4 rows to the appropriate spot.
643   const int shift_32_y[] = {0, 4, 32, 36};
644   const int shift_16_y[] = {0, 2, 16, 18};
645   const int shift_8_y[] = {0, 1, 8, 9};
646   const int shift_32_uv[] = {0, 2, 8, 10};
647   const int shift_16_uv[] = {0, 1, 4, 5};
648   int i;
649   const int max_rows = (mi_row + MI_BLOCK_SIZE > cm->mi_rows ?
650                         cm->mi_rows - mi_row : MI_BLOCK_SIZE);
651   const int max_cols = (mi_col + MI_BLOCK_SIZE > cm->mi_cols ?
652                         cm->mi_cols - mi_col : MI_BLOCK_SIZE);
653 
654   vp9_zero(*lfm);
655   assert(mip[0] != NULL);
656 
657   // TODO(jimbankoski): Try moving most of the following code into decode
658   // loop and storing lfm in the mbmi structure so that we don't have to go
659   // through the recursive loop structure multiple times.
660   switch (mip[0]->mbmi.sb_type) {
661     case BLOCK_64X64:
662       build_masks(lfi_n, mip[0] , 0, 0, lfm);
663       break;
664     case BLOCK_64X32:
665       build_masks(lfi_n, mip[0], 0, 0, lfm);
666       mip2 = mip + mode_info_stride * 4;
667       if (4 >= max_rows)
668         break;
669       build_masks(lfi_n, mip2[0], 32, 8, lfm);
670       break;
671     case BLOCK_32X64:
672       build_masks(lfi_n, mip[0], 0, 0, lfm);
673       mip2 = mip + 4;
674       if (4 >= max_cols)
675         break;
676       build_masks(lfi_n, mip2[0], 4, 2, lfm);
677       break;
678     default:
679       for (idx_32 = 0; idx_32 < 4; mip += offset_32[idx_32], ++idx_32) {
680         const int shift_y = shift_32_y[idx_32];
681         const int shift_uv = shift_32_uv[idx_32];
682         const int mi_32_col_offset = ((idx_32 & 1) << 2);
683         const int mi_32_row_offset = ((idx_32 >> 1) << 2);
684         if (mi_32_col_offset >= max_cols || mi_32_row_offset >= max_rows)
685           continue;
686         switch (mip[0]->mbmi.sb_type) {
687           case BLOCK_32X32:
688             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
689             break;
690           case BLOCK_32X16:
691             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
692             if (mi_32_row_offset + 2 >= max_rows)
693               continue;
694             mip2 = mip + mode_info_stride * 2;
695             build_masks(lfi_n, mip2[0], shift_y + 16, shift_uv + 4, lfm);
696             break;
697           case BLOCK_16X32:
698             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
699             if (mi_32_col_offset + 2 >= max_cols)
700               continue;
701             mip2 = mip + 2;
702             build_masks(lfi_n, mip2[0], shift_y + 2, shift_uv + 1, lfm);
703             break;
704           default:
705             for (idx_16 = 0; idx_16 < 4; mip += offset_16[idx_16], ++idx_16) {
706               const int shift_y = shift_32_y[idx_32] + shift_16_y[idx_16];
707               const int shift_uv = shift_32_uv[idx_32] + shift_16_uv[idx_16];
708               const int mi_16_col_offset = mi_32_col_offset +
709                   ((idx_16 & 1) << 1);
710               const int mi_16_row_offset = mi_32_row_offset +
711                   ((idx_16 >> 1) << 1);
712 
713               if (mi_16_col_offset >= max_cols || mi_16_row_offset >= max_rows)
714                 continue;
715 
716               switch (mip[0]->mbmi.sb_type) {
717                 case BLOCK_16X16:
718                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
719                   break;
720                 case BLOCK_16X8:
721                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
722                   if (mi_16_row_offset + 1 >= max_rows)
723                     continue;
724                   mip2 = mip + mode_info_stride;
725                   build_y_mask(lfi_n, mip2[0], shift_y+8, lfm);
726                   break;
727                 case BLOCK_8X16:
728                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
729                   if (mi_16_col_offset +1 >= max_cols)
730                     continue;
731                   mip2 = mip + 1;
732                   build_y_mask(lfi_n, mip2[0], shift_y+1, lfm);
733                   break;
734                 default: {
735                   const int shift_y = shift_32_y[idx_32] +
736                                       shift_16_y[idx_16] +
737                                       shift_8_y[0];
738                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
739                   mip += offset[0];
740                   for (idx_8 = 1; idx_8 < 4; mip += offset[idx_8], ++idx_8) {
741                     const int shift_y = shift_32_y[idx_32] +
742                                         shift_16_y[idx_16] +
743                                         shift_8_y[idx_8];
744                     const int mi_8_col_offset = mi_16_col_offset +
745                         ((idx_8 & 1));
746                     const int mi_8_row_offset = mi_16_row_offset +
747                         ((idx_8 >> 1));
748 
749                     if (mi_8_col_offset >= max_cols ||
750                         mi_8_row_offset >= max_rows)
751                       continue;
752                     build_y_mask(lfi_n, mip[0], shift_y, lfm);
753                   }
754                   break;
755                 }
756               }
757             }
758             break;
759         }
760       }
761       break;
762   }
763   // The largest loopfilter we have is 16x16 so we use the 16x16 mask
764   // for 32x32 transforms also also.
765   lfm->left_y[TX_16X16] |= lfm->left_y[TX_32X32];
766   lfm->above_y[TX_16X16] |= lfm->above_y[TX_32X32];
767   lfm->left_uv[TX_16X16] |= lfm->left_uv[TX_32X32];
768   lfm->above_uv[TX_16X16] |= lfm->above_uv[TX_32X32];
769 
770   // We do at least 8 tap filter on every 32x32 even if the transform size
771   // is 4x4. So if the 4x4 is set on a border pixel add it to the 8x8 and
772   // remove it from the 4x4.
773   lfm->left_y[TX_8X8] |= lfm->left_y[TX_4X4] & left_border;
774   lfm->left_y[TX_4X4] &= ~left_border;
775   lfm->above_y[TX_8X8] |= lfm->above_y[TX_4X4] & above_border;
776   lfm->above_y[TX_4X4] &= ~above_border;
777   lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_4X4] & left_border_uv;
778   lfm->left_uv[TX_4X4] &= ~left_border_uv;
779   lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_4X4] & above_border_uv;
780   lfm->above_uv[TX_4X4] &= ~above_border_uv;
781 
782   // We do some special edge handling.
783   if (mi_row + MI_BLOCK_SIZE > cm->mi_rows) {
784     const uint64_t rows = cm->mi_rows - mi_row;
785 
786     // Each pixel inside the border gets a 1,
787     const uint64_t mask_y = (((uint64_t) 1 << (rows << 3)) - 1);
788     const uint16_t mask_uv = (((uint16_t) 1 << (((rows + 1) >> 1) << 2)) - 1);
789 
790     // Remove values completely outside our border.
791     for (i = 0; i < TX_32X32; i++) {
792       lfm->left_y[i] &= mask_y;
793       lfm->above_y[i] &= mask_y;
794       lfm->left_uv[i] &= mask_uv;
795       lfm->above_uv[i] &= mask_uv;
796     }
797     lfm->int_4x4_y &= mask_y;
798     lfm->int_4x4_uv &= mask_uv;
799 
800     // We don't apply a wide loop filter on the last uv block row. If set
801     // apply the shorter one instead.
802     if (rows == 1) {
803       lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16];
804       lfm->above_uv[TX_16X16] = 0;
805     }
806     if (rows == 5) {
807       lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16] & 0xff00;
808       lfm->above_uv[TX_16X16] &= ~(lfm->above_uv[TX_16X16] & 0xff00);
809     }
810   }
811 
812   if (mi_col + MI_BLOCK_SIZE > cm->mi_cols) {
813     const uint64_t columns = cm->mi_cols - mi_col;
814 
815     // Each pixel inside the border gets a 1, the multiply copies the border
816     // to where we need it.
817     const uint64_t mask_y  = (((1 << columns) - 1)) * 0x0101010101010101;
818     const uint16_t mask_uv = ((1 << ((columns + 1) >> 1)) - 1) * 0x1111;
819 
820     // Internal edges are not applied on the last column of the image so
821     // we mask 1 more for the internal edges
822     const uint16_t mask_uv_int = ((1 << (columns >> 1)) - 1) * 0x1111;
823 
824     // Remove the bits outside the image edge.
825     for (i = 0; i < TX_32X32; i++) {
826       lfm->left_y[i] &= mask_y;
827       lfm->above_y[i] &= mask_y;
828       lfm->left_uv[i] &= mask_uv;
829       lfm->above_uv[i] &= mask_uv;
830     }
831     lfm->int_4x4_y &= mask_y;
832     lfm->int_4x4_uv &= mask_uv_int;
833 
834     // We don't apply a wide loop filter on the last uv column. If set
835     // apply the shorter one instead.
836     if (columns == 1) {
837       lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_16X16];
838       lfm->left_uv[TX_16X16] = 0;
839     }
840     if (columns == 5) {
841       lfm->left_uv[TX_8X8] |= (lfm->left_uv[TX_16X16] & 0xcccc);
842       lfm->left_uv[TX_16X16] &= ~(lfm->left_uv[TX_16X16] & 0xcccc);
843     }
844   }
845   // We don't apply a loop filter on the first column in the image, mask that
846   // out.
847   if (mi_col == 0) {
848     for (i = 0; i < TX_32X32; i++) {
849       lfm->left_y[i] &= 0xfefefefefefefefe;
850       lfm->left_uv[i] &= 0xeeee;
851     }
852   }
853 
854   // Assert if we try to apply 2 different loop filters at the same position.
855   assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_8X8]));
856   assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_4X4]));
857   assert(!(lfm->left_y[TX_8X8] & lfm->left_y[TX_4X4]));
858   assert(!(lfm->int_4x4_y & lfm->left_y[TX_16X16]));
859   assert(!(lfm->left_uv[TX_16X16]&lfm->left_uv[TX_8X8]));
860   assert(!(lfm->left_uv[TX_16X16] & lfm->left_uv[TX_4X4]));
861   assert(!(lfm->left_uv[TX_8X8] & lfm->left_uv[TX_4X4]));
862   assert(!(lfm->int_4x4_uv & lfm->left_uv[TX_16X16]));
863   assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_8X8]));
864   assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_4X4]));
865   assert(!(lfm->above_y[TX_8X8] & lfm->above_y[TX_4X4]));
866   assert(!(lfm->int_4x4_y & lfm->above_y[TX_16X16]));
867   assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_8X8]));
868   assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_4X4]));
869   assert(!(lfm->above_uv[TX_8X8] & lfm->above_uv[TX_4X4]));
870   assert(!(lfm->int_4x4_uv & lfm->above_uv[TX_16X16]));
871 }
872 
filter_selectively_vert(uint8_t * s,int pitch,unsigned int mask_16x16,unsigned int mask_8x8,unsigned int mask_4x4,unsigned int mask_4x4_int,const loop_filter_info_n * lfi_n,const uint8_t * lfl)873 static void filter_selectively_vert(uint8_t *s, int pitch,
874                                     unsigned int mask_16x16,
875                                     unsigned int mask_8x8,
876                                     unsigned int mask_4x4,
877                                     unsigned int mask_4x4_int,
878                                     const loop_filter_info_n *lfi_n,
879                                     const uint8_t *lfl) {
880   unsigned int mask;
881 
882   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
883        mask; mask >>= 1) {
884     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
885 
886     if (mask & 1) {
887       if (mask_16x16 & 1) {
888         vp9_lpf_vertical_16(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
889       } else if (mask_8x8 & 1) {
890         vp9_lpf_vertical_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
891       } else if (mask_4x4 & 1) {
892         vp9_lpf_vertical_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
893       }
894     }
895     if (mask_4x4_int & 1)
896       vp9_lpf_vertical_4(s + 4, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
897     s += 8;
898     lfl += 1;
899     mask_16x16 >>= 1;
900     mask_8x8 >>= 1;
901     mask_4x4 >>= 1;
902     mask_4x4_int >>= 1;
903   }
904 }
905 
filter_block_plane_non420(VP9_COMMON * cm,struct macroblockd_plane * plane,MODE_INFO ** mi_8x8,int mi_row,int mi_col)906 static void filter_block_plane_non420(VP9_COMMON *cm,
907                                       struct macroblockd_plane *plane,
908                                       MODE_INFO **mi_8x8,
909                                       int mi_row, int mi_col) {
910   const int ss_x = plane->subsampling_x;
911   const int ss_y = plane->subsampling_y;
912   const int row_step = 1 << ss_x;
913   const int col_step = 1 << ss_y;
914   const int row_step_stride = cm->mi_stride * row_step;
915   struct buf_2d *const dst = &plane->dst;
916   uint8_t* const dst0 = dst->buf;
917   unsigned int mask_16x16[MI_BLOCK_SIZE] = {0};
918   unsigned int mask_8x8[MI_BLOCK_SIZE] = {0};
919   unsigned int mask_4x4[MI_BLOCK_SIZE] = {0};
920   unsigned int mask_4x4_int[MI_BLOCK_SIZE] = {0};
921   uint8_t lfl[MI_BLOCK_SIZE * MI_BLOCK_SIZE];
922   int r, c;
923 
924   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
925     unsigned int mask_16x16_c = 0;
926     unsigned int mask_8x8_c = 0;
927     unsigned int mask_4x4_c = 0;
928     unsigned int border_mask;
929 
930     // Determine the vertical edges that need filtering
931     for (c = 0; c < MI_BLOCK_SIZE && mi_col + c < cm->mi_cols; c += col_step) {
932       const MODE_INFO *mi = mi_8x8[c];
933       const BLOCK_SIZE sb_type = mi[0].mbmi.sb_type;
934       const int skip_this = mi[0].mbmi.skip && is_inter_block(&mi[0].mbmi);
935       // left edge of current unit is block/partition edge -> no skip
936       const int block_edge_left = (num_4x4_blocks_wide_lookup[sb_type] > 1) ?
937           !(c & (num_8x8_blocks_wide_lookup[sb_type] - 1)) : 1;
938       const int skip_this_c = skip_this && !block_edge_left;
939       // top edge of current unit is block/partition edge -> no skip
940       const int block_edge_above = (num_4x4_blocks_high_lookup[sb_type] > 1) ?
941           !(r & (num_8x8_blocks_high_lookup[sb_type] - 1)) : 1;
942       const int skip_this_r = skip_this && !block_edge_above;
943       const TX_SIZE tx_size = (plane->plane_type == PLANE_TYPE_UV)
944                             ? get_uv_tx_size(&mi[0].mbmi, plane)
945                             : mi[0].mbmi.tx_size;
946       const int skip_border_4x4_c = ss_x && mi_col + c == cm->mi_cols - 1;
947       const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
948 
949       // Filter level can vary per MI
950       if (!(lfl[(r << 3) + (c >> ss_x)] =
951             get_filter_level(&cm->lf_info, &mi[0].mbmi)))
952         continue;
953 
954       // Build masks based on the transform size of each block
955       if (tx_size == TX_32X32) {
956         if (!skip_this_c && ((c >> ss_x) & 3) == 0) {
957           if (!skip_border_4x4_c)
958             mask_16x16_c |= 1 << (c >> ss_x);
959           else
960             mask_8x8_c |= 1 << (c >> ss_x);
961         }
962         if (!skip_this_r && ((r >> ss_y) & 3) == 0) {
963           if (!skip_border_4x4_r)
964             mask_16x16[r] |= 1 << (c >> ss_x);
965           else
966             mask_8x8[r] |= 1 << (c >> ss_x);
967         }
968       } else if (tx_size == TX_16X16) {
969         if (!skip_this_c && ((c >> ss_x) & 1) == 0) {
970           if (!skip_border_4x4_c)
971             mask_16x16_c |= 1 << (c >> ss_x);
972           else
973             mask_8x8_c |= 1 << (c >> ss_x);
974         }
975         if (!skip_this_r && ((r >> ss_y) & 1) == 0) {
976           if (!skip_border_4x4_r)
977             mask_16x16[r] |= 1 << (c >> ss_x);
978           else
979             mask_8x8[r] |= 1 << (c >> ss_x);
980         }
981       } else {
982         // force 8x8 filtering on 32x32 boundaries
983         if (!skip_this_c) {
984           if (tx_size == TX_8X8 || ((c >> ss_x) & 3) == 0)
985             mask_8x8_c |= 1 << (c >> ss_x);
986           else
987             mask_4x4_c |= 1 << (c >> ss_x);
988         }
989 
990         if (!skip_this_r) {
991           if (tx_size == TX_8X8 || ((r >> ss_y) & 3) == 0)
992             mask_8x8[r] |= 1 << (c >> ss_x);
993           else
994             mask_4x4[r] |= 1 << (c >> ss_x);
995         }
996 
997         if (!skip_this && tx_size < TX_8X8 && !skip_border_4x4_c)
998           mask_4x4_int[r] |= 1 << (c >> ss_x);
999       }
1000     }
1001 
1002     // Disable filtering on the leftmost column
1003     border_mask = ~(mi_col == 0);
1004     filter_selectively_vert(dst->buf, dst->stride,
1005                             mask_16x16_c & border_mask,
1006                             mask_8x8_c & border_mask,
1007                             mask_4x4_c & border_mask,
1008                             mask_4x4_int[r],
1009                             &cm->lf_info, &lfl[r << 3]);
1010     dst->buf += 8 * dst->stride;
1011     mi_8x8 += row_step_stride;
1012   }
1013 
1014   // Now do horizontal pass
1015   dst->buf = dst0;
1016   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
1017     const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
1018     const unsigned int mask_4x4_int_r = skip_border_4x4_r ? 0 : mask_4x4_int[r];
1019 
1020     unsigned int mask_16x16_r;
1021     unsigned int mask_8x8_r;
1022     unsigned int mask_4x4_r;
1023 
1024     if (mi_row + r == 0) {
1025       mask_16x16_r = 0;
1026       mask_8x8_r = 0;
1027       mask_4x4_r = 0;
1028     } else {
1029       mask_16x16_r = mask_16x16[r];
1030       mask_8x8_r = mask_8x8[r];
1031       mask_4x4_r = mask_4x4[r];
1032     }
1033 
1034     filter_selectively_horiz(dst->buf, dst->stride,
1035                              mask_16x16_r,
1036                              mask_8x8_r,
1037                              mask_4x4_r,
1038                              mask_4x4_int_r,
1039                              &cm->lf_info, &lfl[r << 3]);
1040     dst->buf += 8 * dst->stride;
1041   }
1042 }
1043 
vp9_filter_block_plane(VP9_COMMON * const cm,struct macroblockd_plane * const plane,int mi_row,LOOP_FILTER_MASK * lfm)1044 void vp9_filter_block_plane(VP9_COMMON *const cm,
1045                             struct macroblockd_plane *const plane,
1046                             int mi_row,
1047                             LOOP_FILTER_MASK *lfm) {
1048   struct buf_2d *const dst = &plane->dst;
1049   uint8_t* const dst0 = dst->buf;
1050   int r, c;
1051 
1052   if (!plane->plane_type) {
1053     uint64_t mask_16x16 = lfm->left_y[TX_16X16];
1054     uint64_t mask_8x8 = lfm->left_y[TX_8X8];
1055     uint64_t mask_4x4 = lfm->left_y[TX_4X4];
1056     uint64_t mask_4x4_int = lfm->int_4x4_y;
1057 
1058     // Vertical pass: do 2 rows at one time
1059     for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
1060       unsigned int mask_16x16_l = mask_16x16 & 0xffff;
1061       unsigned int mask_8x8_l = mask_8x8 & 0xffff;
1062       unsigned int mask_4x4_l = mask_4x4 & 0xffff;
1063       unsigned int mask_4x4_int_l = mask_4x4_int & 0xffff;
1064 
1065       // Disable filtering on the leftmost column
1066       filter_selectively_vert_row2(plane->plane_type,
1067                                    dst->buf, dst->stride,
1068                                    mask_16x16_l,
1069                                    mask_8x8_l,
1070                                    mask_4x4_l,
1071                                    mask_4x4_int_l,
1072                                    &cm->lf_info, &lfm->lfl_y[r << 3]);
1073 
1074       dst->buf += 16 * dst->stride;
1075       mask_16x16 >>= 16;
1076       mask_8x8 >>= 16;
1077       mask_4x4 >>= 16;
1078       mask_4x4_int >>= 16;
1079     }
1080 
1081     // Horizontal pass
1082     dst->buf = dst0;
1083     mask_16x16 = lfm->above_y[TX_16X16];
1084     mask_8x8 = lfm->above_y[TX_8X8];
1085     mask_4x4 = lfm->above_y[TX_4X4];
1086     mask_4x4_int = lfm->int_4x4_y;
1087 
1088     for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r++) {
1089       unsigned int mask_16x16_r;
1090       unsigned int mask_8x8_r;
1091       unsigned int mask_4x4_r;
1092 
1093       if (mi_row + r == 0) {
1094         mask_16x16_r = 0;
1095         mask_8x8_r = 0;
1096         mask_4x4_r = 0;
1097       } else {
1098         mask_16x16_r = mask_16x16 & 0xff;
1099         mask_8x8_r = mask_8x8 & 0xff;
1100         mask_4x4_r = mask_4x4 & 0xff;
1101       }
1102 
1103       filter_selectively_horiz(dst->buf, dst->stride,
1104                                mask_16x16_r,
1105                                mask_8x8_r,
1106                                mask_4x4_r,
1107                                mask_4x4_int & 0xff,
1108                                &cm->lf_info, &lfm->lfl_y[r << 3]);
1109 
1110       dst->buf += 8 * dst->stride;
1111       mask_16x16 >>= 8;
1112       mask_8x8 >>= 8;
1113       mask_4x4 >>= 8;
1114       mask_4x4_int >>= 8;
1115     }
1116   } else {
1117     uint16_t mask_16x16 = lfm->left_uv[TX_16X16];
1118     uint16_t mask_8x8 = lfm->left_uv[TX_8X8];
1119     uint16_t mask_4x4 = lfm->left_uv[TX_4X4];
1120     uint16_t mask_4x4_int = lfm->int_4x4_uv;
1121 
1122     // Vertical pass: do 2 rows at one time
1123     for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 4) {
1124       if (plane->plane_type == 1) {
1125         for (c = 0; c < (MI_BLOCK_SIZE >> 1); c++) {
1126           lfm->lfl_uv[(r << 1) + c] = lfm->lfl_y[(r << 3) + (c << 1)];
1127           lfm->lfl_uv[((r + 2) << 1) + c] = lfm->lfl_y[((r + 2) << 3) +
1128                                                        (c << 1)];
1129         }
1130       }
1131 
1132       {
1133         unsigned int mask_16x16_l = mask_16x16 & 0xff;
1134         unsigned int mask_8x8_l = mask_8x8 & 0xff;
1135         unsigned int mask_4x4_l = mask_4x4 & 0xff;
1136         unsigned int mask_4x4_int_l = mask_4x4_int & 0xff;
1137 
1138         // Disable filtering on the leftmost column
1139         filter_selectively_vert_row2(plane->plane_type,
1140                                      dst->buf, dst->stride,
1141                                      mask_16x16_l,
1142                                      mask_8x8_l,
1143                                      mask_4x4_l,
1144                                      mask_4x4_int_l,
1145                                      &cm->lf_info, &lfm->lfl_uv[r << 1]);
1146 
1147         dst->buf += 16 * dst->stride;
1148         mask_16x16 >>= 8;
1149         mask_8x8 >>= 8;
1150         mask_4x4 >>= 8;
1151         mask_4x4_int >>= 8;
1152       }
1153     }
1154 
1155     // Horizontal pass
1156     dst->buf = dst0;
1157     mask_16x16 = lfm->above_uv[TX_16X16];
1158     mask_8x8 = lfm->above_uv[TX_8X8];
1159     mask_4x4 = lfm->above_uv[TX_4X4];
1160     mask_4x4_int = lfm->int_4x4_uv;
1161 
1162     for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
1163       const int skip_border_4x4_r = mi_row + r == cm->mi_rows - 1;
1164       const unsigned int mask_4x4_int_r = skip_border_4x4_r ?
1165           0 : (mask_4x4_int & 0xf);
1166       unsigned int mask_16x16_r;
1167       unsigned int mask_8x8_r;
1168       unsigned int mask_4x4_r;
1169 
1170       if (mi_row + r == 0) {
1171         mask_16x16_r = 0;
1172         mask_8x8_r = 0;
1173         mask_4x4_r = 0;
1174       } else {
1175         mask_16x16_r = mask_16x16 & 0xf;
1176         mask_8x8_r = mask_8x8 & 0xf;
1177         mask_4x4_r = mask_4x4 & 0xf;
1178       }
1179 
1180       filter_selectively_horiz(dst->buf, dst->stride,
1181                                mask_16x16_r,
1182                                mask_8x8_r,
1183                                mask_4x4_r,
1184                                mask_4x4_int_r,
1185                                &cm->lf_info, &lfm->lfl_uv[r << 1]);
1186 
1187       dst->buf += 8 * dst->stride;
1188       mask_16x16 >>= 4;
1189       mask_8x8 >>= 4;
1190       mask_4x4 >>= 4;
1191       mask_4x4_int >>= 4;
1192     }
1193   }
1194 }
1195 
vp9_loop_filter_rows(const YV12_BUFFER_CONFIG * frame_buffer,VP9_COMMON * cm,struct macroblockd_plane planes[MAX_MB_PLANE],int start,int stop,int y_only)1196 void vp9_loop_filter_rows(const YV12_BUFFER_CONFIG *frame_buffer,
1197                           VP9_COMMON *cm,
1198                           struct macroblockd_plane planes[MAX_MB_PLANE],
1199                           int start, int stop, int y_only) {
1200   const int num_planes = y_only ? 1 : MAX_MB_PLANE;
1201   const int use_420 = y_only || (planes[1].subsampling_y == 1 &&
1202                                  planes[1].subsampling_x == 1);
1203   LOOP_FILTER_MASK lfm;
1204   int mi_row, mi_col;
1205 
1206   for (mi_row = start; mi_row < stop; mi_row += MI_BLOCK_SIZE) {
1207     MODE_INFO **mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
1208 
1209     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) {
1210       int plane;
1211 
1212       vp9_setup_dst_planes(planes, frame_buffer, mi_row, mi_col);
1213 
1214       // TODO(JBB): Make setup_mask work for non 420.
1215       if (use_420)
1216         vp9_setup_mask(cm, mi_row, mi_col, mi + mi_col, cm->mi_stride,
1217                        &lfm);
1218 
1219       for (plane = 0; plane < num_planes; ++plane) {
1220         if (use_420)
1221           vp9_filter_block_plane(cm, &planes[plane], mi_row, &lfm);
1222         else
1223           filter_block_plane_non420(cm, &planes[plane], mi + mi_col,
1224                                     mi_row, mi_col);
1225       }
1226     }
1227   }
1228 }
1229 
vp9_loop_filter_frame(YV12_BUFFER_CONFIG * frame,VP9_COMMON * cm,MACROBLOCKD * xd,int frame_filter_level,int y_only,int partial_frame)1230 void vp9_loop_filter_frame(YV12_BUFFER_CONFIG *frame,
1231                            VP9_COMMON *cm, MACROBLOCKD *xd,
1232                            int frame_filter_level,
1233                            int y_only, int partial_frame) {
1234   int start_mi_row, end_mi_row, mi_rows_to_filter;
1235   if (!frame_filter_level) return;
1236   start_mi_row = 0;
1237   mi_rows_to_filter = cm->mi_rows;
1238   if (partial_frame && cm->mi_rows > 8) {
1239     start_mi_row = cm->mi_rows >> 1;
1240     start_mi_row &= 0xfffffff8;
1241     mi_rows_to_filter = MAX(cm->mi_rows / 8, 8);
1242   }
1243   end_mi_row = start_mi_row + mi_rows_to_filter;
1244   vp9_loop_filter_frame_init(cm, frame_filter_level);
1245   vp9_loop_filter_rows(frame, cm, xd->plane,
1246                        start_mi_row, end_mi_row,
1247                        y_only);
1248 }
1249 
vp9_loop_filter_worker(void * arg1,void * arg2)1250 int vp9_loop_filter_worker(void *arg1, void *arg2) {
1251   LFWorkerData *const lf_data = (LFWorkerData*)arg1;
1252   (void)arg2;
1253   vp9_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
1254                        lf_data->start, lf_data->stop, lf_data->y_only);
1255   return 1;
1256 }
1257