Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / common / vp9_loopfilter.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "./vpx_config.h"
12 #include "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
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 paramaeters 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
231 void vp9_loop_filter_init(VP9_COMMON *cm) {
232   loop_filter_info_n *lfi = &cm->lf_info;
233   struct loopfilter *lf = &cm->lf;
234   int lvl;
235
236   // init limits for given sharpness
237   update_sharpness(lfi, lf->sharpness_level);
238   lf->last_sharpness_level = lf->sharpness_level;
239
240   // init hev threshold const vectors
241   for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++)
242     vpx_memset(lfi->lfthr[lvl].hev_thr, (lvl >> 4), SIMD_WIDTH);
243 }
244
245 void vp9_loop_filter_frame_init(VP9_COMMON *cm, int default_filt_lvl) {
246   int seg_id;
247   // n_shift is the a multiplier for lf_deltas
248   // the multiplier is 1 for when filter_lvl is between 0 and 31;
249   // 2 when filter_lvl is between 32 and 63
250   const int scale = 1 << (default_filt_lvl >> 5);
251   loop_filter_info_n *const lfi = &cm->lf_info;
252   struct loopfilter *const lf = &cm->lf;
253   const struct segmentation *const seg = &cm->seg;
254
255   // update limits if sharpness has changed
256   if (lf->last_sharpness_level != lf->sharpness_level) {
257     update_sharpness(lfi, lf->sharpness_level);
258     lf->last_sharpness_level = lf->sharpness_level;
259   }
260
261   for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) {
262     int lvl_seg = default_filt_lvl;
263     if (vp9_segfeature_active(seg, seg_id, SEG_LVL_ALT_LF)) {
264       const int data = vp9_get_segdata(seg, seg_id, SEG_LVL_ALT_LF);
265       lvl_seg = seg->abs_delta == SEGMENT_ABSDATA
266                   ? data
267                   : clamp(default_filt_lvl + data, 0, MAX_LOOP_FILTER);
268     }
269
270     if (!lf->mode_ref_delta_enabled) {
271       // we could get rid of this if we assume that deltas are set to
272       // zero when not in use; encoder always uses deltas
273       vpx_memset(lfi->lvl[seg_id], lvl_seg, sizeof(lfi->lvl[seg_id]));
274     } else {
275       int ref, mode;
276       const int intra_lvl = lvl_seg + lf->ref_deltas[INTRA_FRAME] * scale;
277       lfi->lvl[seg_id][INTRA_FRAME][0] = clamp(intra_lvl, 0, MAX_LOOP_FILTER);
278
279       for (ref = LAST_FRAME; ref < MAX_REF_FRAMES; ++ref) {
280         for (mode = 0; mode < MAX_MODE_LF_DELTAS; ++mode) {
281           const int inter_lvl = lvl_seg + lf->ref_deltas[ref] * scale
282                                         + lf->mode_deltas[mode] * scale;
283           lfi->lvl[seg_id][ref][mode] = clamp(inter_lvl, 0, MAX_LOOP_FILTER);
284         }
285       }
286     }
287   }
288 }
289
290 static void filter_selectively_vert_row2(PLANE_TYPE plane_type,
291                                          uint8_t *s, int pitch,
292                                          unsigned int mask_16x16_l,
293                                          unsigned int mask_8x8_l,
294                                          unsigned int mask_4x4_l,
295                                          unsigned int mask_4x4_int_l,
296                                          const loop_filter_info_n *lfi_n,
297                                          const uint8_t *lfl) {
298   const int mask_shift = plane_type ? 4 : 8;
299   const int mask_cutoff = plane_type ? 0xf : 0xff;
300   const int lfl_forward = plane_type ? 4 : 8;
301
302   unsigned int mask_16x16_0 = mask_16x16_l & mask_cutoff;
303   unsigned int mask_8x8_0 = mask_8x8_l & mask_cutoff;
304   unsigned int mask_4x4_0 = mask_4x4_l & mask_cutoff;
305   unsigned int mask_4x4_int_0 = mask_4x4_int_l & mask_cutoff;
306   unsigned int mask_16x16_1 = (mask_16x16_l >> mask_shift) & mask_cutoff;
307   unsigned int mask_8x8_1 = (mask_8x8_l >> mask_shift) & mask_cutoff;
308   unsigned int mask_4x4_1 = (mask_4x4_l >> mask_shift) & mask_cutoff;
309   unsigned int mask_4x4_int_1 = (mask_4x4_int_l >> mask_shift) & mask_cutoff;
310   unsigned int mask;
311
312   for (mask = mask_16x16_0 | mask_8x8_0 | mask_4x4_0 | mask_4x4_int_0 |
313       mask_16x16_1 | mask_8x8_1 | mask_4x4_1 | mask_4x4_int_1;
314       mask; mask >>= 1) {
315     const loop_filter_thresh *lfi0 = lfi_n->lfthr + *lfl;
316     const loop_filter_thresh *lfi1 = lfi_n->lfthr + *(lfl + lfl_forward);
317
318     // TODO(yunqingwang): count in loopfilter functions should be removed.
319     if (mask & 1) {
320       if ((mask_16x16_0 | mask_16x16_1) & 1) {
321         if ((mask_16x16_0 & mask_16x16_1) & 1) {
322           vp9_lpf_vertical_16_dual(s, pitch, lfi0->mblim, lfi0->lim,
323                                    lfi0->hev_thr);
324         } else if (mask_16x16_0 & 1) {
325           vp9_lpf_vertical_16(s, pitch, lfi0->mblim, lfi0->lim,
326                               lfi0->hev_thr);
327         } else {
328           vp9_lpf_vertical_16(s + 8 *pitch, pitch, lfi1->mblim,
329                               lfi1->lim, lfi1->hev_thr);
330         }
331       }
332
333       if ((mask_8x8_0 | mask_8x8_1) & 1) {
334         if ((mask_8x8_0 & mask_8x8_1) & 1) {
335           vp9_lpf_vertical_8_dual(s, pitch, lfi0->mblim, lfi0->lim,
336                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
337                                   lfi1->hev_thr);
338         } else if (mask_8x8_0 & 1) {
339           vp9_lpf_vertical_8(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr,
340                              1);
341         } else {
342           vp9_lpf_vertical_8(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
343                              lfi1->hev_thr, 1);
344         }
345       }
346
347       if ((mask_4x4_0 | mask_4x4_1) & 1) {
348         if ((mask_4x4_0 & mask_4x4_1) & 1) {
349           vp9_lpf_vertical_4_dual(s, pitch, lfi0->mblim, lfi0->lim,
350                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
351                                   lfi1->hev_thr);
352         } else if (mask_4x4_0 & 1) {
353           vp9_lpf_vertical_4(s, pitch, lfi0->mblim, lfi0->lim, lfi0->hev_thr,
354                              1);
355         } else {
356           vp9_lpf_vertical_4(s + 8 * pitch, pitch, lfi1->mblim, lfi1->lim,
357                              lfi1->hev_thr, 1);
358         }
359       }
360
361       if ((mask_4x4_int_0 | mask_4x4_int_1) & 1) {
362         if ((mask_4x4_int_0 & mask_4x4_int_1) & 1) {
363           vp9_lpf_vertical_4_dual(s + 4, pitch, lfi0->mblim, lfi0->lim,
364                                   lfi0->hev_thr, lfi1->mblim, lfi1->lim,
365                                   lfi1->hev_thr);
366         } else if (mask_4x4_int_0 & 1) {
367           vp9_lpf_vertical_4(s + 4, pitch, lfi0->mblim, lfi0->lim,
368                              lfi0->hev_thr, 1);
369         } else {
370           vp9_lpf_vertical_4(s + 8 * pitch + 4, pitch, lfi1->mblim, lfi1->lim,
371                              lfi1->hev_thr, 1);
372         }
373       }
374     }
375
376     s += 8;
377     lfl += 1;
378     mask_16x16_0 >>= 1;
379     mask_8x8_0 >>= 1;
380     mask_4x4_0 >>= 1;
381     mask_4x4_int_0 >>= 1;
382     mask_16x16_1 >>= 1;
383     mask_8x8_1 >>= 1;
384     mask_4x4_1 >>= 1;
385     mask_4x4_int_1 >>= 1;
386   }
387 }
388
389 static void filter_selectively_horiz(uint8_t *s, int pitch,
390                                      unsigned int mask_16x16,
391                                      unsigned int mask_8x8,
392                                      unsigned int mask_4x4,
393                                      unsigned int mask_4x4_int,
394                                      const loop_filter_info_n *lfi_n,
395                                      const uint8_t *lfl) {
396   unsigned int mask;
397   int count;
398
399   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
400        mask; mask >>= count) {
401     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
402
403     count = 1;
404     if (mask & 1) {
405       if (mask_16x16 & 1) {
406         if ((mask_16x16 & 3) == 3) {
407           vp9_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
408                                 lfi->hev_thr, 2);
409           count = 2;
410         } else {
411           vp9_lpf_horizontal_16(s, pitch, lfi->mblim, lfi->lim,
412                                 lfi->hev_thr, 1);
413         }
414       } else if (mask_8x8 & 1) {
415         if ((mask_8x8 & 3) == 3) {
416           // Next block's thresholds
417           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
418
419           vp9_lpf_horizontal_8_dual(s, pitch, lfi->mblim, lfi->lim,
420                                     lfi->hev_thr, lfin->mblim, lfin->lim,
421                                     lfin->hev_thr);
422
423           if ((mask_4x4_int & 3) == 3) {
424             vp9_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
425                                       lfi->lim, lfi->hev_thr, lfin->mblim,
426                                       lfin->lim, lfin->hev_thr);
427           } else {
428             if (mask_4x4_int & 1)
429               vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
430                                    lfi->hev_thr, 1);
431             else if (mask_4x4_int & 2)
432               vp9_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
433                                    lfin->lim, lfin->hev_thr, 1);
434           }
435           count = 2;
436         } else {
437           vp9_lpf_horizontal_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
438
439           if (mask_4x4_int & 1)
440             vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
441                                  lfi->hev_thr, 1);
442         }
443       } else if (mask_4x4 & 1) {
444         if ((mask_4x4 & 3) == 3) {
445           // Next block's thresholds
446           const loop_filter_thresh *lfin = lfi_n->lfthr + *(lfl + 1);
447
448           vp9_lpf_horizontal_4_dual(s, pitch, lfi->mblim, lfi->lim,
449                                     lfi->hev_thr, lfin->mblim, lfin->lim,
450                                     lfin->hev_thr);
451           if ((mask_4x4_int & 3) == 3) {
452             vp9_lpf_horizontal_4_dual(s + 4 * pitch, pitch, lfi->mblim,
453                                       lfi->lim, lfi->hev_thr, lfin->mblim,
454                                       lfin->lim, lfin->hev_thr);
455           } else {
456             if (mask_4x4_int & 1)
457               vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
458                                    lfi->hev_thr, 1);
459             else if (mask_4x4_int & 2)
460               vp9_lpf_horizontal_4(s + 8 + 4 * pitch, pitch, lfin->mblim,
461                                    lfin->lim, lfin->hev_thr, 1);
462           }
463           count = 2;
464         } else {
465           vp9_lpf_horizontal_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
466
467           if (mask_4x4_int & 1)
468             vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
469                                  lfi->hev_thr, 1);
470         }
471       } else if (mask_4x4_int & 1) {
472         vp9_lpf_horizontal_4(s + 4 * pitch, pitch, lfi->mblim, lfi->lim,
473                              lfi->hev_thr, 1);
474       }
475     }
476     s += 8 * count;
477     lfl += count;
478     mask_16x16 >>= count;
479     mask_8x8 >>= count;
480     mask_4x4 >>= count;
481     mask_4x4_int >>= count;
482   }
483 }
484
485 // This function ors into the current lfm structure, where to do loop
486 // filters for the specific mi we are looking at.   It uses information
487 // including the block_size_type (32x16, 32x32, etc),  the transform size,
488 // whether there were any coefficients encoded, and the loop filter strength
489 // block we are currently looking at. Shift is used to position the
490 // 1's we produce.
491 // TODO(JBB) Need another function for different resolution color..
492 static void build_masks(const loop_filter_info_n *const lfi_n,
493                         const MODE_INFO *mi, const int shift_y,
494                         const int shift_uv,
495                         LOOP_FILTER_MASK *lfm) {
496   const BLOCK_SIZE block_size = mi->mbmi.sb_type;
497   const TX_SIZE tx_size_y = mi->mbmi.tx_size;
498   const TX_SIZE tx_size_uv = get_uv_tx_size(&mi->mbmi);
499   const int skip = mi->mbmi.skip;
500   const int seg = mi->mbmi.segment_id;
501   const int ref = mi->mbmi.ref_frame[0];
502   const int filter_level = lfi_n->lvl[seg][ref][mode_lf_lut[mi->mbmi.mode]];
503   uint64_t *left_y = &lfm->left_y[tx_size_y];
504   uint64_t *above_y = &lfm->above_y[tx_size_y];
505   uint64_t *int_4x4_y = &lfm->int_4x4_y;
506   uint16_t *left_uv = &lfm->left_uv[tx_size_uv];
507   uint16_t *above_uv = &lfm->above_uv[tx_size_uv];
508   uint16_t *int_4x4_uv = &lfm->int_4x4_uv;
509   int i;
510   int w = num_8x8_blocks_wide_lookup[block_size];
511   int h = num_8x8_blocks_high_lookup[block_size];
512
513   // If filter level is 0 we don't loop filter.
514   if (!filter_level) {
515     return;
516   } else {
517     int index = shift_y;
518     for (i = 0; i < h; i++) {
519       vpx_memset(&lfm->lfl_y[index], filter_level, w);
520       index += 8;
521     }
522   }
523
524   // These set 1 in the current block size for the block size edges.
525   // For instance if the block size is 32x16,   we'll set :
526   //    above =   1111
527   //              0000
528   //    and
529   //    left  =   1000
530   //          =   1000
531   // NOTE : In this example the low bit is left most ( 1000 ) is stored as
532   //        1,  not 8...
533   //
534   // U and v set things on a 16 bit scale.
535   //
536   *above_y |= above_prediction_mask[block_size] << shift_y;
537   *above_uv |= above_prediction_mask_uv[block_size] << shift_uv;
538   *left_y |= left_prediction_mask[block_size] << shift_y;
539   *left_uv |= left_prediction_mask_uv[block_size] << shift_uv;
540
541   // If the block has no coefficients and is not intra we skip applying
542   // the loop filter on block edges.
543   if (skip && ref > INTRA_FRAME)
544     return;
545
546   // Here we are adding a mask for the transform size.  The transform
547   // size mask is set to be correct for a 64x64 prediction block size. We
548   // mask to match the size of the block we are working on and then shift it
549   // into place..
550   *above_y |= (size_mask[block_size] &
551                above_64x64_txform_mask[tx_size_y]) << shift_y;
552   *above_uv |= (size_mask_uv[block_size] &
553                 above_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
554
555   *left_y |= (size_mask[block_size] &
556               left_64x64_txform_mask[tx_size_y]) << shift_y;
557   *left_uv |= (size_mask_uv[block_size] &
558                left_64x64_txform_mask_uv[tx_size_uv]) << shift_uv;
559
560   // Here we are trying to determine what to do with the internal 4x4 block
561   // boundaries.  These differ from the 4x4 boundaries on the outside edge of
562   // an 8x8 in that the internal ones can be skipped and don't depend on
563   // the prediction block size.
564   if (tx_size_y == TX_4X4) {
565     *int_4x4_y |= (size_mask[block_size] & 0xffffffffffffffff) << shift_y;
566   }
567   if (tx_size_uv == TX_4X4) {
568     *int_4x4_uv |= (size_mask_uv[block_size] & 0xffff) << shift_uv;
569   }
570 }
571
572 // This function does the same thing as the one above with the exception that
573 // it only affects the y masks.   It exists because for blocks < 16x16 in size,
574 // we only update u and v masks on the first block.
575 static void build_y_mask(const loop_filter_info_n *const lfi_n,
576                          const MODE_INFO *mi, const int shift_y,
577                          LOOP_FILTER_MASK *lfm) {
578   const BLOCK_SIZE block_size = mi->mbmi.sb_type;
579   const TX_SIZE tx_size_y = mi->mbmi.tx_size;
580   const int skip = mi->mbmi.skip;
581   const int seg = mi->mbmi.segment_id;
582   const int ref = mi->mbmi.ref_frame[0];
583   const int filter_level = lfi_n->lvl[seg][ref][mode_lf_lut[mi->mbmi.mode]];
584   uint64_t *left_y = &lfm->left_y[tx_size_y];
585   uint64_t *above_y = &lfm->above_y[tx_size_y];
586   uint64_t *int_4x4_y = &lfm->int_4x4_y;
587   int i;
588   int w = num_8x8_blocks_wide_lookup[block_size];
589   int h = num_8x8_blocks_high_lookup[block_size];
590
591   if (!filter_level) {
592     return;
593   } else {
594     int index = shift_y;
595     for (i = 0; i < h; i++) {
596       vpx_memset(&lfm->lfl_y[index], filter_level, w);
597       index += 8;
598     }
599   }
600
601   *above_y |= above_prediction_mask[block_size] << shift_y;
602   *left_y |= left_prediction_mask[block_size] << shift_y;
603
604   if (skip && ref > INTRA_FRAME)
605     return;
606
607   *above_y |= (size_mask[block_size] &
608                above_64x64_txform_mask[tx_size_y]) << shift_y;
609
610   *left_y |= (size_mask[block_size] &
611               left_64x64_txform_mask[tx_size_y]) << shift_y;
612
613   if (tx_size_y == TX_4X4) {
614     *int_4x4_y |= (size_mask[block_size] & 0xffffffffffffffff) << shift_y;
615   }
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.
621 void vp9_setup_mask(VP9_COMMON *const cm, const int mi_row, const int mi_col,
622                     MODE_INFO **mi_8x8, 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_8x8;
627   MODE_INFO **mip2 = mi_8x8;
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 avoids
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
656   // TODO(jimbankoski): Try moving most of the following code into decode
657   // loop and storing lfm in the mbmi structure so that we don't have to go
658   // through the recursive loop structure multiple times.
659   switch (mip[0]->mbmi.sb_type) {
660     case BLOCK_64X64:
661       build_masks(lfi_n, mip[0] , 0, 0, lfm);
662       break;
663     case BLOCK_64X32:
664       build_masks(lfi_n, mip[0], 0, 0, lfm);
665       mip2 = mip + mode_info_stride * 4;
666       if (4 >= max_rows)
667         break;
668       build_masks(lfi_n, mip2[0], 32, 8, lfm);
669       break;
670     case BLOCK_32X64:
671       build_masks(lfi_n, mip[0], 0, 0, lfm);
672       mip2 = mip + 4;
673       if (4 >= max_cols)
674         break;
675       build_masks(lfi_n, mip2[0], 4, 2, lfm);
676       break;
677     default:
678       for (idx_32 = 0; idx_32 < 4; mip += offset_32[idx_32], ++idx_32) {
679         const int shift_y = shift_32_y[idx_32];
680         const int shift_uv = shift_32_uv[idx_32];
681         const int mi_32_col_offset = ((idx_32 & 1) << 2);
682         const int mi_32_row_offset = ((idx_32 >> 1) << 2);
683         if (mi_32_col_offset >= max_cols || mi_32_row_offset >= max_rows)
684           continue;
685         switch (mip[0]->mbmi.sb_type) {
686           case BLOCK_32X32:
687             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
688             break;
689           case BLOCK_32X16:
690             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
691             if (mi_32_row_offset + 2 >= max_rows)
692               continue;
693             mip2 = mip + mode_info_stride * 2;
694             build_masks(lfi_n, mip2[0], shift_y + 16, shift_uv + 4, lfm);
695             break;
696           case BLOCK_16X32:
697             build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
698             if (mi_32_col_offset + 2 >= max_cols)
699               continue;
700             mip2 = mip + 2;
701             build_masks(lfi_n, mip2[0], shift_y + 2, shift_uv + 1, lfm);
702             break;
703           default:
704             for (idx_16 = 0; idx_16 < 4; mip += offset_16[idx_16], ++idx_16) {
705               const int shift_y = shift_32_y[idx_32] + shift_16_y[idx_16];
706               const int shift_uv = shift_32_uv[idx_32] + shift_16_uv[idx_16];
707               const int mi_16_col_offset = mi_32_col_offset +
708                   ((idx_16 & 1) << 1);
709               const int mi_16_row_offset = mi_32_row_offset +
710                   ((idx_16 >> 1) << 1);
711
712               if (mi_16_col_offset >= max_cols || mi_16_row_offset >= max_rows)
713                 continue;
714
715               switch (mip[0]->mbmi.sb_type) {
716                 case BLOCK_16X16:
717                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
718                   break;
719                 case BLOCK_16X8:
720                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
721                   if (mi_16_row_offset + 1 >= max_rows)
722                     continue;
723                   mip2 = mip + mode_info_stride;
724                   build_y_mask(lfi_n, mip2[0], shift_y+8, lfm);
725                   break;
726                 case BLOCK_8X16:
727                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
728                   if (mi_16_col_offset +1 >= max_cols)
729                     continue;
730                   mip2 = mip + 1;
731                   build_y_mask(lfi_n, mip2[0], shift_y+1, lfm);
732                   break;
733                 default: {
734                   const int shift_y = shift_32_y[idx_32] +
735                                       shift_16_y[idx_16] +
736                                       shift_8_y[0];
737                   build_masks(lfi_n, mip[0], shift_y, shift_uv, lfm);
738                   mip += offset[0];
739                   for (idx_8 = 1; idx_8 < 4; mip += offset[idx_8], ++idx_8) {
740                     const int shift_y = shift_32_y[idx_32] +
741                                         shift_16_y[idx_16] +
742                                         shift_8_y[idx_8];
743                     const int mi_8_col_offset = mi_16_col_offset +
744                         ((idx_8 & 1));
745                     const int mi_8_row_offset = mi_16_row_offset +
746                         ((idx_8 >> 1));
747
748                     if (mi_8_col_offset >= max_cols ||
749                         mi_8_row_offset >= max_rows)
750                       continue;
751                     build_y_mask(lfi_n, mip[0], shift_y, lfm);
752                   }
753                   break;
754                 }
755               }
756             }
757             break;
758         }
759       }
760       break;
761   }
762   // The largest loopfilter we have is 16x16 so we use the 16x16 mask
763   // for 32x32 transforms also also.
764   lfm->left_y[TX_16X16] |= lfm->left_y[TX_32X32];
765   lfm->above_y[TX_16X16] |= lfm->above_y[TX_32X32];
766   lfm->left_uv[TX_16X16] |= lfm->left_uv[TX_32X32];
767   lfm->above_uv[TX_16X16] |= lfm->above_uv[TX_32X32];
768
769   // We do at least 8 tap filter on every 32x32 even if the transform size
770   // is 4x4.  So if the 4x4 is set on a border pixel add it to the 8x8 and
771   // remove it from the 4x4.
772   lfm->left_y[TX_8X8] |= lfm->left_y[TX_4X4] & left_border;
773   lfm->left_y[TX_4X4] &= ~left_border;
774   lfm->above_y[TX_8X8] |= lfm->above_y[TX_4X4] & above_border;
775   lfm->above_y[TX_4X4] &= ~above_border;
776   lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_4X4] & left_border_uv;
777   lfm->left_uv[TX_4X4] &= ~left_border_uv;
778   lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_4X4] & above_border_uv;
779   lfm->above_uv[TX_4X4] &= ~above_border_uv;
780
781   // We do some special edge handling.
782   if (mi_row + MI_BLOCK_SIZE > cm->mi_rows) {
783     const uint64_t rows = cm->mi_rows - mi_row;
784
785     // Each pixel inside the border gets a 1,
786     const uint64_t mask_y = (((uint64_t) 1 << (rows << 3)) - 1);
787     const uint16_t mask_uv = (((uint16_t) 1 << (((rows + 1) >> 1) << 2)) - 1);
788
789     // Remove values completely outside our border.
790     for (i = 0; i < TX_32X32; i++) {
791       lfm->left_y[i] &= mask_y;
792       lfm->above_y[i] &= mask_y;
793       lfm->left_uv[i] &= mask_uv;
794       lfm->above_uv[i] &= mask_uv;
795     }
796     lfm->int_4x4_y &= mask_y;
797     lfm->int_4x4_uv &= mask_uv;
798
799     // We don't apply a wide loop filter on the last uv block row.  If set
800     // apply the shorter one instead.
801     if (rows == 1) {
802       lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16];
803       lfm->above_uv[TX_16X16] = 0;
804     }
805     if (rows == 5) {
806       lfm->above_uv[TX_8X8] |= lfm->above_uv[TX_16X16] & 0xff00;
807       lfm->above_uv[TX_16X16] &= ~(lfm->above_uv[TX_16X16] & 0xff00);
808     }
809   }
810
811   if (mi_col + MI_BLOCK_SIZE > cm->mi_cols) {
812     const uint64_t columns = cm->mi_cols - mi_col;
813
814     // Each pixel inside the border gets a 1, the multiply copies the border
815     // to where we need it.
816     const uint64_t mask_y  = (((1 << columns) - 1)) * 0x0101010101010101;
817     const uint16_t mask_uv = ((1 << ((columns + 1) >> 1)) - 1) * 0x1111;
818
819     // Internal edges are not applied on the last column of the image so
820     // we mask 1 more for the internal edges
821     const uint16_t mask_uv_int = ((1 << (columns >> 1)) - 1) * 0x1111;
822
823     // Remove the bits outside the image edge.
824     for (i = 0; i < TX_32X32; i++) {
825       lfm->left_y[i] &= mask_y;
826       lfm->above_y[i] &= mask_y;
827       lfm->left_uv[i] &= mask_uv;
828       lfm->above_uv[i] &= mask_uv;
829     }
830     lfm->int_4x4_y &= mask_y;
831     lfm->int_4x4_uv &= mask_uv_int;
832
833     // We don't apply a wide loop filter on the last uv column.  If set
834     // apply the shorter one instead.
835     if (columns == 1) {
836       lfm->left_uv[TX_8X8] |= lfm->left_uv[TX_16X16];
837       lfm->left_uv[TX_16X16] = 0;
838     }
839     if (columns == 5) {
840       lfm->left_uv[TX_8X8] |= (lfm->left_uv[TX_16X16] & 0xcccc);
841       lfm->left_uv[TX_16X16] &= ~(lfm->left_uv[TX_16X16] & 0xcccc);
842     }
843   }
844   // We don't a loop filter on the first column in the image.  Mask that out.
845   if (mi_col == 0) {
846     for (i = 0; i < TX_32X32; i++) {
847       lfm->left_y[i] &= 0xfefefefefefefefe;
848       lfm->left_uv[i] &= 0xeeee;
849     }
850   }
851
852   // Assert if we try to apply 2 different loop filters at the same position.
853   assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_8X8]));
854   assert(!(lfm->left_y[TX_16X16] & lfm->left_y[TX_4X4]));
855   assert(!(lfm->left_y[TX_8X8] & lfm->left_y[TX_4X4]));
856   assert(!(lfm->int_4x4_y & lfm->left_y[TX_16X16]));
857   assert(!(lfm->left_uv[TX_16X16]&lfm->left_uv[TX_8X8]));
858   assert(!(lfm->left_uv[TX_16X16] & lfm->left_uv[TX_4X4]));
859   assert(!(lfm->left_uv[TX_8X8] & lfm->left_uv[TX_4X4]));
860   assert(!(lfm->int_4x4_uv & lfm->left_uv[TX_16X16]));
861   assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_8X8]));
862   assert(!(lfm->above_y[TX_16X16] & lfm->above_y[TX_4X4]));
863   assert(!(lfm->above_y[TX_8X8] & lfm->above_y[TX_4X4]));
864   assert(!(lfm->int_4x4_y & lfm->above_y[TX_16X16]));
865   assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_8X8]));
866   assert(!(lfm->above_uv[TX_16X16] & lfm->above_uv[TX_4X4]));
867   assert(!(lfm->above_uv[TX_8X8] & lfm->above_uv[TX_4X4]));
868   assert(!(lfm->int_4x4_uv & lfm->above_uv[TX_16X16]));
869 }
870
871 #if CONFIG_NON420
872 static uint8_t build_lfi(const loop_filter_info_n *lfi_n,
873                      const MB_MODE_INFO *mbmi) {
874   const int seg = mbmi->segment_id;
875   const int ref = mbmi->ref_frame[0];
876   return lfi_n->lvl[seg][ref][mode_lf_lut[mbmi->mode]];
877 }
878
879 static void filter_selectively_vert(uint8_t *s, int pitch,
880                                     unsigned int mask_16x16,
881                                     unsigned int mask_8x8,
882                                     unsigned int mask_4x4,
883                                     unsigned int mask_4x4_int,
884                                     const loop_filter_info_n *lfi_n,
885                                     const uint8_t *lfl) {
886   unsigned int mask;
887
888   for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
889        mask; mask >>= 1) {
890     const loop_filter_thresh *lfi = lfi_n->lfthr + *lfl;
891
892     if (mask & 1) {
893       if (mask_16x16 & 1) {
894         vp9_lpf_vertical_16(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr);
895       } else if (mask_8x8 & 1) {
896         vp9_lpf_vertical_8(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
897       } else if (mask_4x4 & 1) {
898         vp9_lpf_vertical_4(s, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
899       }
900     }
901     if (mask_4x4_int & 1)
902       vp9_lpf_vertical_4(s + 4, pitch, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
903     s += 8;
904     lfl += 1;
905     mask_16x16 >>= 1;
906     mask_8x8 >>= 1;
907     mask_4x4 >>= 1;
908     mask_4x4_int >>= 1;
909   }
910 }
911
912 static void filter_block_plane_non420(VP9_COMMON *cm,
913                                       struct macroblockd_plane *plane,
914                                       MODE_INFO **mi_8x8,
915                                       int mi_row, int mi_col) {
916   const int ss_x = plane->subsampling_x;
917   const int ss_y = plane->subsampling_y;
918   const int row_step = 1 << ss_x;
919   const int col_step = 1 << ss_y;
920   const int row_step_stride = cm->mode_info_stride * row_step;
921   struct buf_2d *const dst = &plane->dst;
922   uint8_t* const dst0 = dst->buf;
923   unsigned int mask_16x16[MI_BLOCK_SIZE] = {0};
924   unsigned int mask_8x8[MI_BLOCK_SIZE] = {0};
925   unsigned int mask_4x4[MI_BLOCK_SIZE] = {0};
926   unsigned int mask_4x4_int[MI_BLOCK_SIZE] = {0};
927   uint8_t lfl[MI_BLOCK_SIZE * MI_BLOCK_SIZE];
928   int r, c;
929
930   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
931     unsigned int mask_16x16_c = 0;
932     unsigned int mask_8x8_c = 0;
933     unsigned int mask_4x4_c = 0;
934     unsigned int border_mask;
935
936     // Determine the vertical edges that need filtering
937     for (c = 0; c < MI_BLOCK_SIZE && mi_col + c < cm->mi_cols; c += col_step) {
938       const MODE_INFO *mi = mi_8x8[c];
939       const BLOCK_SIZE sb_type = mi[0].mbmi.sb_type;
940       const int skip_this = mi[0].mbmi.skip && is_inter_block(&mi[0].mbmi);
941       // left edge of current unit is block/partition edge -> no skip
942       const int block_edge_left = (num_4x4_blocks_wide_lookup[sb_type] > 1) ?
943           !(c & (num_8x8_blocks_wide_lookup[sb_type] - 1)) : 1;
944       const int skip_this_c = skip_this && !block_edge_left;
945       // top edge of current unit is block/partition edge -> no skip
946       const int block_edge_above = (num_4x4_blocks_high_lookup[sb_type] > 1) ?
947           !(r & (num_8x8_blocks_high_lookup[sb_type] - 1)) : 1;
948       const int skip_this_r = skip_this && !block_edge_above;
949       const TX_SIZE tx_size = (plane->plane_type == PLANE_TYPE_UV)
950                             ? get_uv_tx_size(&mi[0].mbmi)
951                             : mi[0].mbmi.tx_size;
952       const int skip_border_4x4_c = ss_x && mi_col + c == cm->mi_cols - 1;
953       const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
954
955       // Filter level can vary per MI
956       if (!(lfl[(r << 3) + (c >> ss_x)] =
957           build_lfi(&cm->lf_info, &mi[0].mbmi)))
958         continue;
959
960       // Build masks based on the transform size of each block
961       if (tx_size == TX_32X32) {
962         if (!skip_this_c && ((c >> ss_x) & 3) == 0) {
963           if (!skip_border_4x4_c)
964             mask_16x16_c |= 1 << (c >> ss_x);
965           else
966             mask_8x8_c |= 1 << (c >> ss_x);
967         }
968         if (!skip_this_r && ((r >> ss_y) & 3) == 0) {
969           if (!skip_border_4x4_r)
970             mask_16x16[r] |= 1 << (c >> ss_x);
971           else
972             mask_8x8[r] |= 1 << (c >> ss_x);
973         }
974       } else if (tx_size == TX_16X16) {
975         if (!skip_this_c && ((c >> ss_x) & 1) == 0) {
976           if (!skip_border_4x4_c)
977             mask_16x16_c |= 1 << (c >> ss_x);
978           else
979             mask_8x8_c |= 1 << (c >> ss_x);
980         }
981         if (!skip_this_r && ((r >> ss_y) & 1) == 0) {
982           if (!skip_border_4x4_r)
983             mask_16x16[r] |= 1 << (c >> ss_x);
984           else
985             mask_8x8[r] |= 1 << (c >> ss_x);
986         }
987       } else {
988         // force 8x8 filtering on 32x32 boundaries
989         if (!skip_this_c) {
990           if (tx_size == TX_8X8 || ((c >> ss_x) & 3) == 0)
991             mask_8x8_c |= 1 << (c >> ss_x);
992           else
993             mask_4x4_c |= 1 << (c >> ss_x);
994         }
995
996         if (!skip_this_r) {
997           if (tx_size == TX_8X8 || ((r >> ss_y) & 3) == 0)
998             mask_8x8[r] |= 1 << (c >> ss_x);
999           else
1000             mask_4x4[r] |= 1 << (c >> ss_x);
1001         }
1002
1003         if (!skip_this && tx_size < TX_8X8 && !skip_border_4x4_c)
1004           mask_4x4_int[r] |= 1 << (c >> ss_x);
1005       }
1006     }
1007
1008     // Disable filtering on the leftmost column
1009     border_mask = ~(mi_col == 0);
1010     filter_selectively_vert(dst->buf, dst->stride,
1011                             mask_16x16_c & border_mask,
1012                             mask_8x8_c & border_mask,
1013                             mask_4x4_c & border_mask,
1014                             mask_4x4_int[r],
1015                             &cm->lf_info, &lfl[r << 3]);
1016     dst->buf += 8 * dst->stride;
1017     mi_8x8 += row_step_stride;
1018   }
1019
1020   // Now do horizontal pass
1021   dst->buf = dst0;
1022   for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
1023     const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
1024     const unsigned int mask_4x4_int_r = skip_border_4x4_r ? 0 : mask_4x4_int[r];
1025
1026     unsigned int mask_16x16_r;
1027     unsigned int mask_8x8_r;
1028     unsigned int mask_4x4_r;
1029
1030     if (mi_row + r == 0) {
1031       mask_16x16_r = 0;
1032       mask_8x8_r = 0;
1033       mask_4x4_r = 0;
1034     } else {
1035       mask_16x16_r = mask_16x16[r];
1036       mask_8x8_r = mask_8x8[r];
1037       mask_4x4_r = mask_4x4[r];
1038     }
1039
1040     filter_selectively_horiz(dst->buf, dst->stride,
1041                              mask_16x16_r,
1042                              mask_8x8_r,
1043                              mask_4x4_r,
1044                              mask_4x4_int_r,
1045                              &cm->lf_info, &lfl[r << 3]);
1046     dst->buf += 8 * dst->stride;
1047   }
1048 }
1049 #endif
1050
1051 void vp9_filter_block_plane(VP9_COMMON *const cm,
1052                             struct macroblockd_plane *const plane,
1053                             int mi_row,
1054                             LOOP_FILTER_MASK *lfm) {
1055   struct buf_2d *const dst = &plane->dst;
1056   uint8_t* const dst0 = dst->buf;
1057   int r, c;
1058
1059   if (!plane->plane_type) {
1060     uint64_t mask_16x16 = lfm->left_y[TX_16X16];
1061     uint64_t mask_8x8 = lfm->left_y[TX_8X8];
1062     uint64_t mask_4x4 = lfm->left_y[TX_4X4];
1063     uint64_t mask_4x4_int = lfm->int_4x4_y;
1064
1065     // Vertical pass: do 2 rows at one time
1066     for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
1067       unsigned int mask_16x16_l = mask_16x16 & 0xffff;
1068       unsigned int mask_8x8_l = mask_8x8 & 0xffff;
1069       unsigned int mask_4x4_l = mask_4x4 & 0xffff;
1070       unsigned int mask_4x4_int_l = mask_4x4_int & 0xffff;
1071
1072       // Disable filtering on the leftmost column
1073       filter_selectively_vert_row2(plane->plane_type,
1074                                    dst->buf, dst->stride,
1075                                    mask_16x16_l,
1076                                    mask_8x8_l,
1077                                    mask_4x4_l,
1078                                    mask_4x4_int_l,
1079                                    &cm->lf_info, &lfm->lfl_y[r << 3]);
1080
1081       dst->buf += 16 * dst->stride;
1082       mask_16x16 >>= 16;
1083       mask_8x8 >>= 16;
1084       mask_4x4 >>= 16;
1085       mask_4x4_int >>= 16;
1086     }
1087
1088     // Horizontal pass
1089     dst->buf = dst0;
1090     mask_16x16 = lfm->above_y[TX_16X16];
1091     mask_8x8 = lfm->above_y[TX_8X8];
1092     mask_4x4 = lfm->above_y[TX_4X4];
1093     mask_4x4_int = lfm->int_4x4_y;
1094
1095     for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r++) {
1096       unsigned int mask_16x16_r;
1097       unsigned int mask_8x8_r;
1098       unsigned int mask_4x4_r;
1099
1100       if (mi_row + r == 0) {
1101         mask_16x16_r = 0;
1102         mask_8x8_r = 0;
1103         mask_4x4_r = 0;
1104       } else {
1105         mask_16x16_r = mask_16x16 & 0xff;
1106         mask_8x8_r = mask_8x8 & 0xff;
1107         mask_4x4_r = mask_4x4 & 0xff;
1108       }
1109
1110       filter_selectively_horiz(dst->buf, dst->stride,
1111                                mask_16x16_r,
1112                                mask_8x8_r,
1113                                mask_4x4_r,
1114                                mask_4x4_int & 0xff,
1115                                &cm->lf_info, &lfm->lfl_y[r << 3]);
1116
1117       dst->buf += 8 * dst->stride;
1118       mask_16x16 >>= 8;
1119       mask_8x8 >>= 8;
1120       mask_4x4 >>= 8;
1121       mask_4x4_int >>= 8;
1122     }
1123   } else {
1124     uint16_t mask_16x16 = lfm->left_uv[TX_16X16];
1125     uint16_t mask_8x8 = lfm->left_uv[TX_8X8];
1126     uint16_t mask_4x4 = lfm->left_uv[TX_4X4];
1127     uint16_t mask_4x4_int = lfm->int_4x4_uv;
1128
1129     // Vertical pass: do 2 rows at one time
1130     for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 4) {
1131       if (plane->plane_type == 1) {
1132         for (c = 0; c < (MI_BLOCK_SIZE >> 1); c++) {
1133           lfm->lfl_uv[(r << 1) + c] = lfm->lfl_y[(r << 3) + (c << 1)];
1134           lfm->lfl_uv[((r + 2) << 1) + c] = lfm->lfl_y[((r + 2) << 3) +
1135                                                        (c << 1)];
1136         }
1137       }
1138
1139       {
1140         unsigned int mask_16x16_l = mask_16x16 & 0xff;
1141         unsigned int mask_8x8_l = mask_8x8 & 0xff;
1142         unsigned int mask_4x4_l = mask_4x4 & 0xff;
1143         unsigned int mask_4x4_int_l = mask_4x4_int & 0xff;
1144
1145         // Disable filtering on the leftmost column
1146         filter_selectively_vert_row2(plane->plane_type,
1147                                      dst->buf, dst->stride,
1148                                      mask_16x16_l,
1149                                      mask_8x8_l,
1150                                      mask_4x4_l,
1151                                      mask_4x4_int_l,
1152                                      &cm->lf_info, &lfm->lfl_uv[r << 1]);
1153
1154         dst->buf += 16 * dst->stride;
1155         mask_16x16 >>= 8;
1156         mask_8x8 >>= 8;
1157         mask_4x4 >>= 8;
1158         mask_4x4_int >>= 8;
1159       }
1160     }
1161
1162     // Horizontal pass
1163     dst->buf = dst0;
1164     mask_16x16 = lfm->above_uv[TX_16X16];
1165     mask_8x8 = lfm->above_uv[TX_8X8];
1166     mask_4x4 = lfm->above_uv[TX_4X4];
1167     mask_4x4_int = lfm->int_4x4_uv;
1168
1169     for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += 2) {
1170       const int skip_border_4x4_r = mi_row + r == cm->mi_rows - 1;
1171       const unsigned int mask_4x4_int_r = skip_border_4x4_r ?
1172           0 : (mask_4x4_int & 0xf);
1173       unsigned int mask_16x16_r;
1174       unsigned int mask_8x8_r;
1175       unsigned int mask_4x4_r;
1176
1177       if (mi_row + r == 0) {
1178         mask_16x16_r = 0;
1179         mask_8x8_r = 0;
1180         mask_4x4_r = 0;
1181       } else {
1182         mask_16x16_r = mask_16x16 & 0xf;
1183         mask_8x8_r = mask_8x8 & 0xf;
1184         mask_4x4_r = mask_4x4 & 0xf;
1185       }
1186
1187       filter_selectively_horiz(dst->buf, dst->stride,
1188                                mask_16x16_r,
1189                                mask_8x8_r,
1190                                mask_4x4_r,
1191                                mask_4x4_int_r,
1192                                &cm->lf_info, &lfm->lfl_uv[r << 1]);
1193
1194       dst->buf += 8 * dst->stride;
1195       mask_16x16 >>= 4;
1196       mask_8x8 >>= 4;
1197       mask_4x4 >>= 4;
1198       mask_4x4_int >>= 4;
1199     }
1200   }
1201 }
1202
1203 void vp9_loop_filter_rows(const YV12_BUFFER_CONFIG *frame_buffer,
1204                           VP9_COMMON *cm, MACROBLOCKD *xd,
1205                           int start, int stop, int y_only) {
1206   const int num_planes = y_only ? 1 : MAX_MB_PLANE;
1207   int mi_row, mi_col;
1208   LOOP_FILTER_MASK lfm;
1209 #if CONFIG_NON420
1210   int use_420 = y_only || (xd->plane[1].subsampling_y == 1 &&
1211       xd->plane[1].subsampling_x == 1);
1212 #endif
1213
1214   for (mi_row = start; mi_row < stop; mi_row += MI_BLOCK_SIZE) {
1215     MODE_INFO **mi_8x8 = cm->mi_grid_visible + mi_row * cm->mode_info_stride;
1216
1217     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) {
1218       int plane;
1219
1220       setup_dst_planes(xd, frame_buffer, mi_row, mi_col);
1221
1222       // TODO(JBB): Make setup_mask work for non 420.
1223 #if CONFIG_NON420
1224       if (use_420)
1225 #endif
1226         vp9_setup_mask(cm, mi_row, mi_col, mi_8x8 + mi_col,
1227                        cm->mode_info_stride, &lfm);
1228
1229       for (plane = 0; plane < num_planes; ++plane) {
1230 #if CONFIG_NON420
1231         if (use_420)
1232 #endif
1233           vp9_filter_block_plane(cm, &xd->plane[plane], mi_row, &lfm);
1234 #if CONFIG_NON420
1235         else
1236           filter_block_plane_non420(cm, &xd->plane[plane], mi_8x8 + mi_col,
1237                                     mi_row, mi_col);
1238 #endif
1239       }
1240     }
1241   }
1242 }
1243
1244 void vp9_loop_filter_frame(VP9_COMMON *cm, MACROBLOCKD *xd,
1245                            int frame_filter_level,
1246                            int y_only, int partial_frame) {
1247   int start_mi_row, end_mi_row, mi_rows_to_filter;
1248   if (!frame_filter_level) return;
1249   start_mi_row = 0;
1250   mi_rows_to_filter = cm->mi_rows;
1251   if (partial_frame && cm->mi_rows > 8) {
1252     start_mi_row = cm->mi_rows >> 1;
1253     start_mi_row &= 0xfffffff8;
1254     mi_rows_to_filter = MAX(cm->mi_rows / 8, 8);
1255   }
1256   end_mi_row = start_mi_row + mi_rows_to_filter;
1257   vp9_loop_filter_frame_init(cm, frame_filter_level);
1258   vp9_loop_filter_rows(cm->frame_to_show, cm, xd,
1259                        start_mi_row, end_mi_row,
1260                        y_only);
1261 }
1262
1263 int vp9_loop_filter_worker(void *arg1, void *arg2) {
1264   LFWorkerData *const lf_data = (LFWorkerData*)arg1;
1265   (void)arg2;
1266   vp9_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, &lf_data->xd,
1267                        lf_data->start, lf_data->stop, lf_data->y_only);
1268   return 1;
1269 }