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 "vpx_mem/vpx_mem.h"
16 #include "vp9/common/vp9_seg_common.h"
18 static void lf_init_lut(loop_filter_info_n *lfi) {
19 lfi->mode_lf_lut[DC_PRED] = 1;
20 lfi->mode_lf_lut[D45_PRED] = 1;
21 lfi->mode_lf_lut[D135_PRED] = 1;
22 lfi->mode_lf_lut[D117_PRED] = 1;
23 lfi->mode_lf_lut[D153_PRED] = 1;
24 lfi->mode_lf_lut[D27_PRED] = 1;
25 lfi->mode_lf_lut[D63_PRED] = 1;
26 lfi->mode_lf_lut[V_PRED] = 1;
27 lfi->mode_lf_lut[H_PRED] = 1;
28 lfi->mode_lf_lut[TM_PRED] = 1;
29 lfi->mode_lf_lut[ZEROMV] = 1;
30 lfi->mode_lf_lut[NEARESTMV] = 2;
31 lfi->mode_lf_lut[NEARMV] = 2;
32 lfi->mode_lf_lut[NEWMV] = 2;
35 void vp9_loop_filter_update_sharpness(loop_filter_info_n *lfi,
39 /* For each possible value for the loop filter fill out limits */
40 for (i = 0; i <= MAX_LOOP_FILTER; i++) {
42 int block_inside_limit = 0;
44 /* Set loop filter paramaeters that control sharpness. */
45 block_inside_limit = filt_lvl >> (sharpness_lvl > 0);
46 block_inside_limit = block_inside_limit >> (sharpness_lvl > 4);
48 if (sharpness_lvl > 0) {
49 if (block_inside_limit > (9 - sharpness_lvl))
50 block_inside_limit = (9 - sharpness_lvl);
53 if (block_inside_limit < 1)
54 block_inside_limit = 1;
56 vpx_memset(lfi->lim[i], block_inside_limit, SIMD_WIDTH);
57 vpx_memset(lfi->blim[i], (2 * filt_lvl + block_inside_limit),
59 vpx_memset(lfi->mblim[i], (2 * (filt_lvl + 2) + block_inside_limit),
64 void vp9_loop_filter_init(VP9_COMMON *cm) {
65 loop_filter_info_n *lfi = &cm->lf_info;
68 // init limits for given sharpness
69 vp9_loop_filter_update_sharpness(lfi, cm->sharpness_level);
70 cm->last_sharpness_level = cm->sharpness_level;
72 // init LUT for lvl and hev thr picking
75 // init hev threshold const vectors
76 for (i = 0; i < 4; i++)
77 vpx_memset(lfi->hev_thr[i], i, SIMD_WIDTH);
80 void vp9_loop_filter_frame_init(VP9_COMMON *cm,
82 int default_filt_lvl) {
83 int seg, // segment number
84 ref, // index in ref_lf_deltas
85 mode; // index in mode_lf_deltas
86 // n_shift is the a multiplier for lf_deltas
87 // the multiplier is 1 for when filter_lvl is between 0 and 31;
88 // 2 when filter_lvl is between 32 and 63
89 int n_shift = default_filt_lvl >> 5;
91 loop_filter_info_n *lfi = &cm->lf_info;
93 /* update limits if sharpness has changed */
94 // printf("vp9_loop_filter_frame_init %d\n", default_filt_lvl);
95 // printf("sharpness level: %d [%d]\n",
96 // cm->sharpness_level, cm->last_sharpness_level);
97 if (cm->last_sharpness_level != cm->sharpness_level) {
98 vp9_loop_filter_update_sharpness(lfi, cm->sharpness_level);
99 cm->last_sharpness_level = cm->sharpness_level;
102 for (seg = 0; seg < MAX_MB_SEGMENTS; seg++) {
103 int lvl_seg = default_filt_lvl;
104 int lvl_ref, lvl_mode;
107 // Set the baseline filter values for each segment
108 if (vp9_segfeature_active(xd, seg, SEG_LVL_ALT_LF)) {
110 if (xd->mb_segment_abs_delta == SEGMENT_ABSDATA) {
111 lvl_seg = vp9_get_segdata(xd, seg, SEG_LVL_ALT_LF);
112 } else { /* Delta Value */
113 lvl_seg += vp9_get_segdata(xd, seg, SEG_LVL_ALT_LF);
114 lvl_seg = clamp(lvl_seg, 0, 63);
118 if (!xd->mode_ref_lf_delta_enabled) {
119 /* we could get rid of this if we assume that deltas are set to
120 * zero when not in use; encoder always uses deltas
122 vpx_memset(lfi->lvl[seg][0], lvl_seg, 4 * 4);
131 /* Apply delta for reference frame */
132 lvl_ref += xd->ref_lf_deltas[ref] << n_shift;
134 /* Apply delta for Intra modes */
135 mode = 0; /* I4X4_PRED */
136 /* Only the split mode I4X4_PRED has a further special case */
137 lvl_mode = lvl_ref + (xd->mode_lf_deltas[mode] << n_shift);
138 lfi->lvl[seg][ref][mode] = clamp(lvl_mode, 0, 63);
140 mode = 1; /* all the rest of Intra modes */
142 lfi->lvl[seg][ref][mode] = clamp(lvl_mode, 0, 63);
144 /* LAST, GOLDEN, ALT */
145 for (ref = 1; ref < MAX_REF_FRAMES; ref++) {
146 int lvl_ref = lvl_seg;
148 /* Apply delta for reference frame */
149 lvl_ref += xd->ref_lf_deltas[ref] << n_shift;
151 /* Apply delta for Inter modes */
152 for (mode = 1; mode < 4; mode++) {
153 lvl_mode = lvl_ref + (xd->mode_lf_deltas[mode] << n_shift);
154 lfi->lvl[seg][ref][mode] = clamp(lvl_mode, 0, 63);
160 // Determine if we should skip inner-MB loop filtering within a MB
161 // The current condition is that the loop filtering is skipped only
162 // the MB uses a prediction size of 16x16 and either 16x16 transform
163 // is used or there is no residue at all.
164 static int mb_lf_skip(const MB_MODE_INFO *const mbmi) {
165 const int skip_coef = mbmi->mb_skip_coeff;
166 const int tx_size = mbmi->txfm_size;
167 return mbmi->sb_type >= BLOCK_SIZE_MB16X16 &&
168 (tx_size >= TX_16X16 || skip_coef);
171 // Determine if we should skip MB loop filtering on a MB edge within
172 // a superblock, the current condition is that MB loop filtering is
173 // skipped only when both MBs do not use inner MB loop filtering, and
174 // same motion vector with same reference frame
175 static int sb_mb_lf_skip(const MODE_INFO *const mip0,
176 const MODE_INFO *const mip1) {
177 const MB_MODE_INFO *mbmi0 = &mip0->mbmi;
178 const MB_MODE_INFO *mbmi1 = &mip1->mbmi;
179 return mb_lf_skip(mbmi0) && mb_lf_skip(mbmi1) &&
180 mbmi0->ref_frame != INTRA_FRAME &&
181 mbmi1->ref_frame != INTRA_FRAME;
184 static void lpf_mb(VP9_COMMON *cm, const MODE_INFO *mi,
185 int do_left_mb_v, int do_above_mb_h,
186 int do_left_mbuv_v, int do_above_mbuv_h,
187 uint8_t *y_ptr, uint8_t *u_ptr, uint8_t *v_ptr,
188 int y_stride, int uv_stride) {
189 loop_filter_info_n *lfi_n = &cm->lf_info;
190 struct loop_filter_info lfi;
191 int mode = mi->mbmi.mode;
192 int mode_index = lfi_n->mode_lf_lut[mode];
193 int seg = mi->mbmi.segment_id;
194 int ref_frame = mi->mbmi.ref_frame;
195 int filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
198 const int skip_lf = mb_lf_skip(&mi->mbmi);
199 const int tx_size = mi->mbmi.txfm_size;
200 const int hev_index = filter_level >> 4;
201 lfi.mblim = lfi_n->mblim[filter_level];
202 lfi.blim = lfi_n->blim[filter_level];
203 lfi.lim = lfi_n->lim[filter_level];
204 lfi.hev_thr = lfi_n->hev_thr[hev_index];
207 if (tx_size >= TX_16X16)
209 do_above_mbuv_h ? u_ptr : NULL,
210 do_above_mbuv_h ? v_ptr : NULL,
211 y_stride, uv_stride, &lfi);
213 vp9_loop_filter_mbh(y_ptr, u_ptr, v_ptr, y_stride, uv_stride, &lfi);
217 if (tx_size >= TX_8X8) {
218 if (tx_size == TX_8X8 &&
219 mi->mbmi.sb_type < BLOCK_SIZE_MB16X16)
220 vp9_loop_filter_bh8x8(y_ptr, u_ptr, v_ptr,
221 y_stride, uv_stride, &lfi);
223 vp9_loop_filter_bh8x8(y_ptr, NULL, NULL,
224 y_stride, uv_stride, &lfi);
226 vp9_loop_filter_bh(y_ptr, u_ptr, v_ptr,
227 y_stride, uv_stride, &lfi);
232 if (tx_size >= TX_16X16)
234 do_left_mbuv_v ? u_ptr : NULL,
235 do_left_mbuv_v ? v_ptr : NULL,
236 y_stride, uv_stride, &lfi);
238 vp9_loop_filter_mbv(y_ptr, u_ptr, v_ptr, y_stride, uv_stride, &lfi);
242 if (tx_size >= TX_8X8) {
243 if (tx_size == TX_8X8 &&
244 mi->mbmi.sb_type < BLOCK_SIZE_MB16X16)
245 vp9_loop_filter_bv8x8(y_ptr, u_ptr, v_ptr,
246 y_stride, uv_stride, &lfi);
248 vp9_loop_filter_bv8x8(y_ptr, NULL, NULL,
249 y_stride, uv_stride, &lfi);
251 vp9_loop_filter_bv(y_ptr, u_ptr, v_ptr,
252 y_stride, uv_stride, &lfi);
258 static void lpf_sb32(VP9_COMMON *cm, const MODE_INFO *mode_info_context,
259 int mb_row, int mb_col,
260 uint8_t *y_ptr, uint8_t *u_ptr, uint8_t *v_ptr,
261 int y_stride, int uv_stride,
263 BLOCK_SIZE_TYPE sb_type = mode_info_context->mbmi.sb_type;
264 const int wbl = b_width_log2(sb_type), hbl = b_height_log2(sb_type);
265 TX_SIZE tx_size = mode_info_context->mbmi.txfm_size;
266 int do_left_v, do_above_h;
267 int do_left_v_mbuv, do_above_h_mbuv;
268 int mis = cm->mode_info_stride;
271 // process 1st MB top-left
272 mi = mode_info_context;
273 do_left_v = (mb_col > 0);
274 do_above_h = (mb_row > 0);
275 do_left_v_mbuv = !(sb_type >= BLOCK_SIZE_SB64X64 &&
276 tx_size >= TX_32X32 && (mb_col & 2));
277 do_above_h_mbuv = !(sb_type >= BLOCK_SIZE_SB64X64 &&
278 tx_size >= TX_32X32 && (mb_row & 2));
279 lpf_mb(cm, mi, do_left_v, do_above_h,
280 do_left_v_mbuv, do_above_h_mbuv,
284 y_stride, uv_stride);
285 // process 2nd MB top-right
286 mi = mode_info_context + 2;
287 do_left_v = !(wbl >= 3 /* 32x16 or >=32x32 */ && (tx_size >= TX_32X32 ||
288 sb_mb_lf_skip(mode_info_context, mi)));
289 do_above_h = (mb_row > 0);
290 do_left_v_mbuv = !(wbl >= 3 /* 32x16 or >=32x32 */ && (tx_size >= TX_16X16 ||
291 sb_mb_lf_skip(mode_info_context, mi)));
292 do_above_h_mbuv = !(sb_type >= BLOCK_SIZE_SB64X64 &&
293 tx_size >= TX_32X32 && (mb_row & 2));
294 lpf_mb(cm, mi, do_left_v, do_above_h,
295 do_left_v_mbuv, do_above_h_mbuv,
297 y_only ? 0 : (u_ptr + 8),
298 y_only ? 0 : (v_ptr + 8),
299 y_stride, uv_stride);
301 // process 3rd MB bottom-left
302 mi = mode_info_context + (mis << 1);
303 do_left_v = (mb_col > 0);
304 do_above_h = !(hbl >= 3 /* 16x32 or >=32x32 */ && (tx_size >= TX_32X32 ||
305 sb_mb_lf_skip(mode_info_context, mi)));
306 do_left_v_mbuv = !(sb_type >= BLOCK_SIZE_SB64X64 &&
307 tx_size >= TX_32X32 && (mb_col & 2));
308 do_above_h_mbuv = !(hbl >= 3 /* 16x32 or >=32x32 */ && (tx_size >= TX_16X16 ||
309 sb_mb_lf_skip(mode_info_context, mi)));
310 lpf_mb(cm, mi, do_left_v, do_above_h,
311 do_left_v_mbuv, do_above_h_mbuv,
312 y_ptr + 16 * y_stride,
313 y_only ? 0 : (u_ptr + 8 * uv_stride),
314 y_only ? 0 : (v_ptr + 8 * uv_stride),
315 y_stride, uv_stride);
317 // process 4th MB bottom right
318 mi = mode_info_context + ((mis + 1) << 1);
319 do_left_v = !(wbl >= 3 /* 32x16 or >=32x32 */ && (tx_size >= TX_32X32 ||
320 sb_mb_lf_skip(mi - 2, mi)));
321 do_above_h = !(hbl >= 3 /* 16x32 or >=32x32 */ && (tx_size >= TX_32X32 ||
322 sb_mb_lf_skip(mode_info_context + 2, mi)));
323 do_left_v_mbuv = (wbl >= 3 /* 32x16 or >=32x32 */ && (tx_size >= TX_16X16 ||
324 sb_mb_lf_skip(mi - 2, mi)));
325 do_above_h_mbuv = !(hbl >= 3 /* 16x32 or >=32x32 */ && (tx_size >= TX_16X16 ||
326 sb_mb_lf_skip(mode_info_context + 2, mi)));
327 lpf_mb(cm, mi, do_left_v, do_above_h,
328 do_left_v_mbuv, do_above_h_mbuv,
329 y_ptr + 16 * y_stride + 16,
330 y_only ? 0 : (u_ptr + 8 * uv_stride + 8),
331 y_only ? 0 : (v_ptr + 8 * uv_stride + 8),
332 y_stride, uv_stride);
335 static void lpf_sb64(VP9_COMMON *cm, const MODE_INFO *mode_info_context,
336 int mb_row, int mb_col,
337 uint8_t *y_ptr, uint8_t *u_ptr, uint8_t *v_ptr,
338 int y_stride, int uv_stride,
340 lpf_sb32(cm, mode_info_context, mb_row, mb_col,
342 y_stride, uv_stride, y_only);
343 lpf_sb32(cm, mode_info_context + 4, mb_row, mb_col + 2,
344 y_ptr + 32, u_ptr + 16, v_ptr + 16,
345 y_stride, uv_stride, y_only);
346 lpf_sb32(cm, mode_info_context + cm->mode_info_stride * 4,
348 y_ptr + 32 * y_stride,
349 u_ptr + 16 * uv_stride,
350 v_ptr + 16 * uv_stride,
351 y_stride, uv_stride, y_only);
352 lpf_sb32(cm, mode_info_context + cm->mode_info_stride * 4 + 4,
353 mb_row + 2, mb_col + 2,
354 y_ptr + 32 * y_stride + 32,
355 u_ptr + 16 * uv_stride + 16,
356 v_ptr + 16 * uv_stride + 16,
357 y_stride, uv_stride, y_only);
360 void vp9_loop_filter_frame(VP9_COMMON *cm,
362 int frame_filter_level,
364 YV12_BUFFER_CONFIG *post = cm->frame_to_show;
366 const int sb64_rows = cm->mb_rows / 4;
367 const int sb64_cols = cm->mb_cols / 4;
368 const int extra_sb32_row = (cm->mb_rows & 2) != 0;
369 const int extra_sb32_col = (cm->mb_cols & 2) != 0;
370 const int extra_mb_col = cm->mb_cols & 1;
371 const int extra_mb_row = cm->mb_rows & 1;
372 // Set up the buffer pointers
373 uint8_t *y_ptr = post->y_buffer;
374 uint8_t *u_ptr = y_only ? 0 : post->u_buffer;
375 uint8_t *v_ptr = y_only ? 0 : post->v_buffer;
377 // Point at base of Mb MODE_INFO list
378 const MODE_INFO *mode_info_context = cm->mi;
380 const int mis = cm->mode_info_stride;
381 const int y_stride = post->y_stride;
382 const int uv_stride = post->uv_stride;
383 // These two flags signal if MB left edge and above edge
384 // should be filtered using MB edge filter. Currently, MB
385 // edge filtering is not applied on MB edge internal to a
386 // 32x32 superblock if:
387 // 1) SB32 is using 32x32 prediction and 32x32 transform
388 // 2) SB32 is using 32x32 prediction and 16x16 transform
389 // but all coefficients are zero.
390 // MB edges are on 32x32 superblock boundary are always
391 // filtered except on image frame boundary.
392 int do_left_v, do_above_h;
393 // These two flags signal if MB UV left edge and above edge
394 // should be filtered using MB edge filter. Currently, MB
395 // edge filtering is not applied for MB edges internal to
396 // a 32x32 superblock if:
397 // 1) SB32 is using 32x32 prediction and 32x32 transform
398 // 2) SB32 is using 32x32 prediction and 16x16 transform
399 // but all coefficients are zero.
400 // 3) SB32 UV edges internal to a SB64 and 32x32 transform
401 // is used, i.e. UV is doing 32x32 transform hence no
402 // transform boundary exists inside the SB64 for UV
403 int do_left_v_mbuv, do_above_h_mbuv;
405 // Initialize the loop filter for this frame.
406 vp9_loop_filter_frame_init(cm, xd, frame_filter_level);
408 // vp9_filter each 64x64 SB
409 // For each SB64: the 4 SB32 are filtered in raster scan order
410 // For each SB32: the 4 MBs are filtered in raster scan order
411 // For each MB: the left and above MB edges as well as the
412 // internal block edges are processed together
413 for (mb_row = 0; mb_row < sb64_rows * 4; mb_row += 4) {
414 for (mb_col = 0; mb_col < sb64_cols * 4; mb_col += 4) {
415 lpf_sb64(cm, mode_info_context, mb_row, mb_col,
417 y_stride, uv_stride, y_only);
419 u_ptr = y_only? 0 : u_ptr + 32;
420 v_ptr = y_only? 0 : v_ptr + 32;
421 mode_info_context += 8; // step to next SB64
423 if (extra_sb32_col) {
424 // process 2 SB32s in the extra SB32 col
425 lpf_sb32(cm, mode_info_context, mb_row, mb_col,
427 y_stride, uv_stride, y_only);
428 lpf_sb32(cm, mode_info_context + mis * 4,
430 y_ptr + 32 * y_stride,
431 u_ptr + 16 * uv_stride,
432 v_ptr + 16 * uv_stride,
433 y_stride, uv_stride, y_only);
435 u_ptr = y_only? 0 : u_ptr + 16;
436 v_ptr = y_only? 0 : v_ptr + 16;
437 mode_info_context += 4; // step to next SB32
441 // process 4 MB in the extra MB col
443 for (k = 0; k < 4; ++k) {
444 mi = mode_info_context + (mis << 1) * k;
445 do_left_v = (mb_col > 0);
446 do_above_h = k == 0 ? mb_row > 0 : 1;
449 lpf_mb(cm, mi, do_left_v, do_above_h,
450 do_left_v_mbuv, do_above_h_mbuv,
451 y_ptr + (k * 16) * y_stride,
452 y_only ? 0 : (u_ptr + (k * 8) * uv_stride),
453 y_only ? 0 : (v_ptr + (k * 8) * uv_stride),
454 y_stride, uv_stride);
458 u_ptr = y_only? 0 : u_ptr + 8;
459 v_ptr = y_only? 0 : v_ptr + 8;
460 mode_info_context += 2; // step to next MB
462 // move pointers to the begining of next sb64 row
463 y_ptr += y_stride * 64 - post->y_width;
465 u_ptr += uv_stride * 32 - post->uv_width;
466 v_ptr += uv_stride * 32 - post->uv_width;
468 /* skip to next SB64 row */
469 mode_info_context += mis * 8 - cm->mi_cols;
471 if (extra_sb32_row) {
472 const int sb32_cols = sb64_cols * 2 + extra_sb32_col;
473 for (mb_col = 0; mb_col < sb32_cols * 2; mb_col += 2) {
474 lpf_sb32(cm, mode_info_context, mb_row, mb_col,
476 y_stride, uv_stride, y_only);
478 u_ptr = y_only? 0 : u_ptr + 16;
479 v_ptr = y_only? 0 : v_ptr + 16;
480 mode_info_context += 4; // step to next SB32
484 mi = mode_info_context;
485 do_left_v = (mb_col > 0);
486 do_above_h = (mb_row > 0);
489 lpf_mb(cm, mi, do_left_v, do_above_h,
490 do_left_v_mbuv, do_above_h_mbuv,
492 y_only? NULL : u_ptr,
493 y_only? NULL : v_ptr,
494 y_stride, uv_stride);
496 mi = mode_info_context + (mis << 1);
497 do_left_v = (mb_col > 0);
501 lpf_mb(cm, mi, do_left_v, do_above_h,
502 do_left_v_mbuv, do_above_h_mbuv,
503 y_ptr + 16 * y_stride,
504 y_only ? NULL : (u_ptr + 8 * uv_stride),
505 y_only ? NULL : (v_ptr + 8 * uv_stride),
506 y_stride, uv_stride);
508 u_ptr = y_only? 0 : u_ptr + 8;
509 v_ptr = y_only? 0 : v_ptr + 8;
510 mode_info_context += 2; /* step to next MB */
512 // move pointers to the beginning of next sb64 row
513 y_ptr += y_stride * 32 - post->y_width;
514 u_ptr += y_only? 0 : uv_stride * 16 - post->uv_width;
515 v_ptr += y_only? 0 : uv_stride * 16 - post->uv_width;
516 // skip to next MB row if exist
517 mode_info_context += mis * 4 - cm->mi_cols;
521 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
522 const MODE_INFO *mi = mode_info_context;
523 do_left_v = (mb_col > 0);
524 do_above_h = (mb_row > 0);
527 lpf_mb(cm, mi, do_left_v, do_above_h,
528 do_left_v_mbuv, do_above_h_mbuv,
532 y_stride, uv_stride);
534 u_ptr = y_only? 0 : u_ptr + 8;
535 v_ptr = y_only? 0 : v_ptr + 8;
536 mode_info_context += 2; // step to next MB