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