2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
11 #include "vpx_config.h"
12 #include "vp9/common/vp9_loopfilter.h"
13 #include "vp9/common/vp9_onyxc_int.h"
14 #include "vp9/common/vp9_reconinter.h"
15 #include "vpx_mem/vpx_mem.h"
17 #include "vp9/common/vp9_seg_common.h"
19 static void lf_init_lut(loop_filter_info_n *lfi) {
20 lfi->mode_lf_lut[DC_PRED] = 0;
21 lfi->mode_lf_lut[D45_PRED] = 0;
22 lfi->mode_lf_lut[D135_PRED] = 0;
23 lfi->mode_lf_lut[D117_PRED] = 0;
24 lfi->mode_lf_lut[D153_PRED] = 0;
25 lfi->mode_lf_lut[D27_PRED] = 0;
26 lfi->mode_lf_lut[D63_PRED] = 0;
27 lfi->mode_lf_lut[V_PRED] = 0;
28 lfi->mode_lf_lut[H_PRED] = 0;
29 lfi->mode_lf_lut[TM_PRED] = 0;
30 lfi->mode_lf_lut[ZEROMV] = 0;
31 lfi->mode_lf_lut[NEARESTMV] = 1;
32 lfi->mode_lf_lut[NEARMV] = 1;
33 lfi->mode_lf_lut[NEWMV] = 1;
36 void vp9_loop_filter_update_sharpness(loop_filter_info_n *lfi,
40 /* For each possible value for the loop filter fill out limits */
41 for (i = 0; i <= MAX_LOOP_FILTER; i++) {
43 int block_inside_limit = 0;
45 /* Set loop filter paramaeters that control sharpness. */
46 block_inside_limit = filt_lvl >> (sharpness_lvl > 0);
47 block_inside_limit = block_inside_limit >> (sharpness_lvl > 4);
49 if (sharpness_lvl > 0) {
50 if (block_inside_limit > (9 - sharpness_lvl))
51 block_inside_limit = (9 - sharpness_lvl);
54 if (block_inside_limit < 1)
55 block_inside_limit = 1;
57 vpx_memset(lfi->lim[i], block_inside_limit, SIMD_WIDTH);
58 vpx_memset(lfi->blim[i], (2 * filt_lvl + block_inside_limit),
60 vpx_memset(lfi->mblim[i], (2 * (filt_lvl + 2) + block_inside_limit),
65 void vp9_loop_filter_init(VP9_COMMON *cm) {
66 loop_filter_info_n *lfi = &cm->lf_info;
69 // init limits for given sharpness
70 vp9_loop_filter_update_sharpness(lfi, cm->sharpness_level);
71 cm->last_sharpness_level = cm->sharpness_level;
73 // init LUT for lvl and hev thr picking
76 // init hev threshold const vectors
77 for (i = 0; i < 4; i++)
78 vpx_memset(lfi->hev_thr[i], i, SIMD_WIDTH);
81 void vp9_loop_filter_frame_init(VP9_COMMON *cm,
83 int default_filt_lvl) {
84 int seg, // segment number
85 ref, // index in ref_lf_deltas
86 mode; // index in mode_lf_deltas
87 // n_shift is the a multiplier for lf_deltas
88 // the multiplier is 1 for when filter_lvl is between 0 and 31;
89 // 2 when filter_lvl is between 32 and 63
90 int n_shift = default_filt_lvl >> 5;
92 loop_filter_info_n *lfi = &cm->lf_info;
94 /* update limits if sharpness has changed */
95 // printf("vp9_loop_filter_frame_init %d\n", default_filt_lvl);
96 // printf("sharpness level: %d [%d]\n",
97 // cm->sharpness_level, cm->last_sharpness_level);
98 if (cm->last_sharpness_level != cm->sharpness_level) {
99 vp9_loop_filter_update_sharpness(lfi, cm->sharpness_level);
100 cm->last_sharpness_level = cm->sharpness_level;
103 for (seg = 0; seg < MAX_MB_SEGMENTS; seg++) {
104 int lvl_seg = default_filt_lvl;
105 int lvl_ref, lvl_mode;
108 // Set the baseline filter values for each segment
109 if (vp9_segfeature_active(xd, seg, SEG_LVL_ALT_LF)) {
111 if (xd->mb_segment_abs_delta == SEGMENT_ABSDATA) {
112 lvl_seg = vp9_get_segdata(xd, seg, SEG_LVL_ALT_LF);
113 } else { /* Delta Value */
114 lvl_seg += vp9_get_segdata(xd, seg, SEG_LVL_ALT_LF);
115 lvl_seg = clamp(lvl_seg, 0, 63);
119 if (!xd->mode_ref_lf_delta_enabled) {
120 /* we could get rid of this if we assume that deltas are set to
121 * zero when not in use; encoder always uses deltas
123 vpx_memset(lfi->lvl[seg][0], lvl_seg, 4 * 4);
132 /* Apply delta for reference frame */
133 lvl_ref += xd->ref_lf_deltas[ref] << n_shift;
135 mode = 0; /* all the rest of Intra modes */
137 lfi->lvl[seg][ref][mode] = clamp(lvl_mode, 0, 63);
139 /* LAST, GOLDEN, ALT */
140 for (ref = 1; ref < MAX_REF_FRAMES; ref++) {
141 int lvl_ref = lvl_seg;
143 /* Apply delta for reference frame */
144 lvl_ref += xd->ref_lf_deltas[ref] << n_shift;
146 /* Apply delta for Inter modes */
147 for (mode = 0; mode < MAX_MODE_LF_DELTAS; mode++) {
148 lvl_mode = lvl_ref + (xd->mode_lf_deltas[mode] << n_shift);
149 lfi->lvl[seg][ref][mode] = clamp(lvl_mode, 0, 63);
155 static int build_lfi(const VP9_COMMON *cm, const MB_MODE_INFO *mbmi,
156 struct loop_filter_info *lfi) {
157 const loop_filter_info_n *lfi_n = &cm->lf_info;
158 int mode = mbmi->mode;
159 int mode_index = lfi_n->mode_lf_lut[mode];
160 int seg = mbmi->segment_id;
161 int ref_frame = mbmi->ref_frame[0];
162 int filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
165 const int hev_index = filter_level >> 4;
166 lfi->mblim = lfi_n->mblim[filter_level];
167 lfi->blim = lfi_n->blim[filter_level];
168 lfi->lim = lfi_n->lim[filter_level];
169 lfi->hev_thr = lfi_n->hev_thr[hev_index];
175 static void filter_selectively_vert(uint8_t *s, int pitch,
176 unsigned int mask_16x16,
177 unsigned int mask_8x8,
178 unsigned int mask_4x4,
179 unsigned int mask_4x4_int,
180 const struct loop_filter_info *lfi) {
183 for (mask = mask_16x16 | mask_8x8 | mask_4x4; mask; mask >>= 1) {
185 if (mask_16x16 & 1) {
186 vp9_mb_lpf_vertical_edge_w(s, pitch, lfi->mblim, lfi->lim,
188 assert(!(mask_8x8 & 1));
189 assert(!(mask_4x4 & 1));
190 assert(!(mask_4x4_int & 1));
191 } else if (mask_8x8 & 1) {
192 vp9_mbloop_filter_vertical_edge(s, pitch, lfi->mblim, lfi->lim,
194 assert(!(mask_16x16 & 1));
195 assert(!(mask_4x4 & 1));
196 } else if (mask_4x4 & 1) {
197 vp9_loop_filter_vertical_edge(s, pitch, lfi->mblim, lfi->lim,
199 assert(!(mask_16x16 & 1));
200 assert(!(mask_8x8 & 1));
205 if (mask_4x4_int & 1)
206 vp9_loop_filter_vertical_edge(s + 4, pitch, lfi->mblim, lfi->lim,
218 static void filter_selectively_horiz(uint8_t *s, int pitch,
219 unsigned int mask_16x16,
220 unsigned int mask_8x8,
221 unsigned int mask_4x4,
222 unsigned int mask_4x4_int,
224 const struct loop_filter_info *lfi) {
227 for (mask = mask_16x16 | mask_8x8 | mask_4x4; mask; mask >>= 1) {
230 if (mask_16x16 & 1) {
231 vp9_mb_lpf_horizontal_edge_w(s, pitch, lfi->mblim, lfi->lim,
233 assert(!(mask_8x8 & 1));
234 assert(!(mask_4x4 & 1));
235 assert(!(mask_4x4_int & 1));
236 } else if (mask_8x8 & 1) {
237 vp9_mbloop_filter_horizontal_edge(s, pitch, lfi->mblim, lfi->lim,
239 assert(!(mask_16x16 & 1));
240 assert(!(mask_4x4 & 1));
241 } else if (mask_4x4 & 1) {
242 vp9_loop_filter_horizontal_edge(s, pitch, lfi->mblim, lfi->lim,
244 assert(!(mask_16x16 & 1));
245 assert(!(mask_8x8 & 1));
251 if (mask_4x4_int & 1)
252 vp9_loop_filter_horizontal_edge(s + 4 * pitch, pitch, lfi->mblim,
253 lfi->lim, lfi->hev_thr, 1);
264 static void filter_block_plane(VP9_COMMON *cm, MACROBLOCKD *xd,
265 int plane, int mi_row, int mi_col) {
266 const int ss_x = xd->plane[plane].subsampling_x;
267 const int ss_y = xd->plane[plane].subsampling_y;
268 const int row_step = 1 << xd->plane[plane].subsampling_y;
269 const int col_step = 1 << xd->plane[plane].subsampling_x;
270 struct buf_2d * const dst = &xd->plane[plane].dst;
271 uint8_t* const dst0 = dst->buf;
272 MODE_INFO* const mi0 = xd->mode_info_context;
273 unsigned int mask_16x16[64 / MI_SIZE] = {0};
274 unsigned int mask_8x8[64 / MI_SIZE] = {0};
275 unsigned int mask_4x4[64 / MI_SIZE] = {0};
276 unsigned int mask_4x4_int[64 / MI_SIZE] = {0};
277 struct loop_filter_info lfi[64 / MI_SIZE][64 / MI_SIZE];
280 for (r = 0; r < 64 / MI_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
281 unsigned int mask_16x16_c = 0;
282 unsigned int mask_8x8_c = 0;
283 unsigned int mask_4x4_c = 0;
284 unsigned int border_mask;
286 // Determine the vertical edges that need filtering
287 for (c = 0; c < 64 / MI_SIZE && mi_col + c < cm->mi_cols; c += col_step) {
288 const MODE_INFO * const mi = xd->mode_info_context;
289 const int skip_this = mi[c].mbmi.mb_skip_coeff
290 && mi[c].mbmi.ref_frame[0] != INTRA_FRAME;
291 // left edge of current unit is block/partition edge -> no skip
292 const int block_edge_left = b_width_log2(mi[c].mbmi.sb_type) ?
293 !(c & ((1 << (b_width_log2(mi[c].mbmi.sb_type)-1)) - 1)) : 1;
294 const int skip_this_c = skip_this && !block_edge_left;
295 // top edge of current unit is block/partition edge -> no skip
296 const int block_edge_above = b_height_log2(mi[c].mbmi.sb_type) ?
297 !(r & ((1 << (b_height_log2(mi[c].mbmi.sb_type)-1)) - 1)) : 1;
298 const int skip_this_r = skip_this && !block_edge_above;
299 const TX_SIZE tx_size = plane ? get_uv_tx_size(&mi[c].mbmi)
300 : mi[c].mbmi.txfm_size;
301 const int skip_border_4x4_c = ss_x && mi_col + c == cm->mi_cols - 1;
302 const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
304 // Filter level can vary per MI
305 if (!build_lfi(cm, &mi[c].mbmi,
306 lfi[r] + (c >> xd->plane[plane].subsampling_x)))
309 // Build masks based on the transform size of each block
310 if (tx_size == TX_32X32) {
311 if (!skip_this_c && ((c >> ss_x) & 3) == 0) {
312 if (!skip_border_4x4_c)
313 mask_16x16_c |= 1 << (c >> ss_x);
315 mask_8x8_c |= 1 << (c >> ss_x);
317 if (!skip_this_r && ((r >> ss_y) & 3) == 0) {
318 if (!skip_border_4x4_r)
319 mask_16x16[r] |= 1 << (c >> ss_x);
321 mask_8x8[r] |= 1 << (c >> ss_x);
323 } else if (tx_size == TX_16X16) {
324 if (!skip_this_c && ((c >> ss_x) & 1) == 0) {
325 if (!skip_border_4x4_c)
326 mask_16x16_c |= 1 << (c >> ss_x);
328 mask_8x8_c |= 1 << (c >> ss_x);
330 if (!skip_this_r && ((r >> ss_y) & 1) == 0) {
331 if (!skip_border_4x4_r)
332 mask_16x16[r] |= 1 << (c >> ss_x);
334 mask_8x8[r] |= 1 << (c >> ss_x);
337 // force 8x8 filtering on 32x32 boundaries
339 if (tx_size == TX_8X8 || ((c >> ss_x) & 3) == 0)
340 mask_8x8_c |= 1 << (c >> ss_x);
342 mask_4x4_c |= 1 << (c >> ss_x);
346 if (tx_size == TX_8X8 || ((r >> ss_y) & 3) == 0)
347 mask_8x8[r] |= 1 << (c >> ss_x);
349 mask_4x4[r] |= 1 << (c >> ss_x);
352 if (!skip_this && tx_size < TX_8X8 && !skip_border_4x4_c)
353 mask_4x4_int[r] |= 1 << (c >> ss_x);
357 // Disable filtering on the leftmost column
358 border_mask = ~(mi_col == 0);
359 filter_selectively_vert(dst->buf, dst->stride,
360 mask_16x16_c & border_mask,
361 mask_8x8_c & border_mask,
362 mask_4x4_c & border_mask,
363 mask_4x4_int[r], lfi[r]);
364 dst->buf += 8 * dst->stride;
365 xd->mode_info_context += cm->mode_info_stride * row_step;
368 // Now do horizontal pass
370 xd->mode_info_context = mi0;
371 for (r = 0; r < 64 / MI_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
372 const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
373 const unsigned int mask_4x4_int_r = skip_border_4x4_r ? 0 : mask_4x4_int[r];
375 filter_selectively_horiz(dst->buf, dst->stride,
379 mask_4x4_int_r, mi_row + r == 0, lfi[r]);
380 dst->buf += 8 * dst->stride;
381 xd->mode_info_context += cm->mode_info_stride * row_step;
385 void vp9_loop_filter_frame(VP9_COMMON *cm,
387 int frame_filter_level,
391 // Initialize the loop filter for this frame.
392 vp9_loop_filter_frame_init(cm, xd, frame_filter_level);
394 for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 64 / MI_SIZE) {
395 MODE_INFO* const mi = cm->mi + mi_row * cm->mode_info_stride;
397 for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 64 / MI_SIZE) {
400 setup_dst_planes(xd, cm->frame_to_show, mi_row, mi_col);
401 for (plane = 0; plane < (y_only ? 1 : MAX_MB_PLANE); plane++) {
402 xd->mode_info_context = mi + mi_col;
403 filter_block_plane(cm, xd, plane, mi_row, mi_col);