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