Merge "VP9: Eliminate up_available and left_available"
[aom.git] / vp8 / common / vp8_loopfilter.c
blob756ad488f95c8b27a17ed238e7f69c4ee6b8cec6
1 /*
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.
9 */
12 #include "vpx_config.h"
13 #include "vp8_rtcd.h"
14 #include "loopfilter.h"
15 #include "onyxc_int.h"
16 #include "vpx_mem/vpx_mem.h"
19 static void lf_init_lut(loop_filter_info_n *lfi)
21 int filt_lvl;
23 for (filt_lvl = 0; filt_lvl <= MAX_LOOP_FILTER; filt_lvl++)
25 if (filt_lvl >= 40)
27 lfi->hev_thr_lut[KEY_FRAME][filt_lvl] = 2;
28 lfi->hev_thr_lut[INTER_FRAME][filt_lvl] = 3;
30 else if (filt_lvl >= 20)
32 lfi->hev_thr_lut[KEY_FRAME][filt_lvl] = 1;
33 lfi->hev_thr_lut[INTER_FRAME][filt_lvl] = 2;
35 else if (filt_lvl >= 15)
37 lfi->hev_thr_lut[KEY_FRAME][filt_lvl] = 1;
38 lfi->hev_thr_lut[INTER_FRAME][filt_lvl] = 1;
40 else
42 lfi->hev_thr_lut[KEY_FRAME][filt_lvl] = 0;
43 lfi->hev_thr_lut[INTER_FRAME][filt_lvl] = 0;
47 lfi->mode_lf_lut[DC_PRED] = 1;
48 lfi->mode_lf_lut[V_PRED] = 1;
49 lfi->mode_lf_lut[H_PRED] = 1;
50 lfi->mode_lf_lut[TM_PRED] = 1;
51 lfi->mode_lf_lut[B_PRED] = 0;
53 lfi->mode_lf_lut[ZEROMV] = 1;
54 lfi->mode_lf_lut[NEARESTMV] = 2;
55 lfi->mode_lf_lut[NEARMV] = 2;
56 lfi->mode_lf_lut[NEWMV] = 2;
57 lfi->mode_lf_lut[SPLITMV] = 3;
61 void vp8_loop_filter_update_sharpness(loop_filter_info_n *lfi,
62 int sharpness_lvl)
64 int i;
66 /* For each possible value for the loop filter fill out limits */
67 for (i = 0; i <= MAX_LOOP_FILTER; i++)
69 int filt_lvl = i;
70 int block_inside_limit = 0;
72 /* Set loop filter paramaeters that control sharpness. */
73 block_inside_limit = filt_lvl >> (sharpness_lvl > 0);
74 block_inside_limit = block_inside_limit >> (sharpness_lvl > 4);
76 if (sharpness_lvl > 0)
78 if (block_inside_limit > (9 - sharpness_lvl))
79 block_inside_limit = (9 - sharpness_lvl);
82 if (block_inside_limit < 1)
83 block_inside_limit = 1;
85 memset(lfi->lim[i], block_inside_limit, SIMD_WIDTH);
86 memset(lfi->blim[i], (2 * filt_lvl + block_inside_limit), SIMD_WIDTH);
87 memset(lfi->mblim[i], (2 * (filt_lvl + 2) + block_inside_limit),
88 SIMD_WIDTH);
92 void vp8_loop_filter_init(VP8_COMMON *cm)
94 loop_filter_info_n *lfi = &cm->lf_info;
95 int i;
97 /* init limits for given sharpness*/
98 vp8_loop_filter_update_sharpness(lfi, cm->sharpness_level);
99 cm->last_sharpness_level = cm->sharpness_level;
101 /* init LUT for lvl and hev thr picking */
102 lf_init_lut(lfi);
104 /* init hev threshold const vectors */
105 for(i = 0; i < 4 ; i++)
107 memset(lfi->hev_thr[i], i, SIMD_WIDTH);
111 void vp8_loop_filter_frame_init(VP8_COMMON *cm,
112 MACROBLOCKD *mbd,
113 int default_filt_lvl)
115 int seg, /* segment number */
116 ref, /* index in ref_lf_deltas */
117 mode; /* index in mode_lf_deltas */
119 loop_filter_info_n *lfi = &cm->lf_info;
121 /* update limits if sharpness has changed */
122 if(cm->last_sharpness_level != cm->sharpness_level)
124 vp8_loop_filter_update_sharpness(lfi, cm->sharpness_level);
125 cm->last_sharpness_level = cm->sharpness_level;
128 for(seg = 0; seg < MAX_MB_SEGMENTS; seg++)
130 int lvl_seg = default_filt_lvl;
131 int lvl_ref, lvl_mode;
133 /* Note the baseline filter values for each segment */
134 if (mbd->segmentation_enabled)
136 /* Abs value */
137 if (mbd->mb_segement_abs_delta == SEGMENT_ABSDATA)
139 lvl_seg = mbd->segment_feature_data[MB_LVL_ALT_LF][seg];
141 else /* Delta Value */
143 lvl_seg += mbd->segment_feature_data[MB_LVL_ALT_LF][seg];
145 lvl_seg = (lvl_seg > 0) ? ((lvl_seg > 63) ? 63: lvl_seg) : 0;
148 if (!mbd->mode_ref_lf_delta_enabled)
150 /* we could get rid of this if we assume that deltas are set to
151 * zero when not in use; encoder always uses deltas
153 memset(lfi->lvl[seg][0], lvl_seg, 4 * 4 );
154 continue;
157 /* INTRA_FRAME */
158 ref = INTRA_FRAME;
160 /* Apply delta for reference frame */
161 lvl_ref = lvl_seg + mbd->ref_lf_deltas[ref];
163 /* Apply delta for Intra modes */
164 mode = 0; /* B_PRED */
165 /* Only the split mode BPRED has a further special case */
166 lvl_mode = lvl_ref + mbd->mode_lf_deltas[mode];
167 /* clamp */
168 lvl_mode = (lvl_mode > 0) ? (lvl_mode > 63 ? 63 : lvl_mode) : 0;
170 lfi->lvl[seg][ref][mode] = lvl_mode;
172 mode = 1; /* all the rest of Intra modes */
173 /* clamp */
174 lvl_mode = (lvl_ref > 0) ? (lvl_ref > 63 ? 63 : lvl_ref) : 0;
175 lfi->lvl[seg][ref][mode] = lvl_mode;
177 /* LAST, GOLDEN, ALT */
178 for(ref = 1; ref < MAX_REF_FRAMES; ref++)
180 /* Apply delta for reference frame */
181 lvl_ref = lvl_seg + mbd->ref_lf_deltas[ref];
183 /* Apply delta for Inter modes */
184 for (mode = 1; mode < 4; mode++)
186 lvl_mode = lvl_ref + mbd->mode_lf_deltas[mode];
187 /* clamp */
188 lvl_mode = (lvl_mode > 0) ? (lvl_mode > 63 ? 63 : lvl_mode) : 0;
190 lfi->lvl[seg][ref][mode] = lvl_mode;
197 void vp8_loop_filter_row_normal(VP8_COMMON *cm, MODE_INFO *mode_info_context,
198 int mb_row, int post_ystride, int post_uvstride,
199 unsigned char *y_ptr, unsigned char *u_ptr,
200 unsigned char *v_ptr)
202 int mb_col;
203 int filter_level;
204 loop_filter_info_n *lfi_n = &cm->lf_info;
205 loop_filter_info lfi;
206 FRAME_TYPE frame_type = cm->frame_type;
208 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
210 int skip_lf = (mode_info_context->mbmi.mode != B_PRED &&
211 mode_info_context->mbmi.mode != SPLITMV &&
212 mode_info_context->mbmi.mb_skip_coeff);
214 const int mode_index = lfi_n->mode_lf_lut[mode_info_context->mbmi.mode];
215 const int seg = mode_info_context->mbmi.segment_id;
216 const int ref_frame = mode_info_context->mbmi.ref_frame;
218 filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
220 if (filter_level)
222 const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level];
223 lfi.mblim = lfi_n->mblim[filter_level];
224 lfi.blim = lfi_n->blim[filter_level];
225 lfi.lim = lfi_n->lim[filter_level];
226 lfi.hev_thr = lfi_n->hev_thr[hev_index];
228 if (mb_col > 0)
229 vp8_loop_filter_mbv
230 (y_ptr, u_ptr, v_ptr, post_ystride, post_uvstride, &lfi);
232 if (!skip_lf)
233 vp8_loop_filter_bv
234 (y_ptr, u_ptr, v_ptr, post_ystride, post_uvstride, &lfi);
236 /* don't apply across umv border */
237 if (mb_row > 0)
238 vp8_loop_filter_mbh
239 (y_ptr, u_ptr, v_ptr, post_ystride, post_uvstride, &lfi);
241 if (!skip_lf)
242 vp8_loop_filter_bh
243 (y_ptr, u_ptr, v_ptr, post_ystride, post_uvstride, &lfi);
246 y_ptr += 16;
247 u_ptr += 8;
248 v_ptr += 8;
250 mode_info_context++; /* step to next MB */
255 void vp8_loop_filter_row_simple(VP8_COMMON *cm, MODE_INFO *mode_info_context,
256 int mb_row, int post_ystride, int post_uvstride,
257 unsigned char *y_ptr, unsigned char *u_ptr,
258 unsigned char *v_ptr)
260 int mb_col;
261 int filter_level;
262 loop_filter_info_n *lfi_n = &cm->lf_info;
263 (void)post_uvstride;
265 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
267 int skip_lf = (mode_info_context->mbmi.mode != B_PRED &&
268 mode_info_context->mbmi.mode != SPLITMV &&
269 mode_info_context->mbmi.mb_skip_coeff);
271 const int mode_index = lfi_n->mode_lf_lut[mode_info_context->mbmi.mode];
272 const int seg = mode_info_context->mbmi.segment_id;
273 const int ref_frame = mode_info_context->mbmi.ref_frame;
275 filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
277 if (filter_level)
279 if (mb_col > 0)
280 vp8_loop_filter_simple_mbv
281 (y_ptr, post_ystride, lfi_n->mblim[filter_level]);
283 if (!skip_lf)
284 vp8_loop_filter_simple_bv
285 (y_ptr, post_ystride, lfi_n->blim[filter_level]);
287 /* don't apply across umv border */
288 if (mb_row > 0)
289 vp8_loop_filter_simple_mbh
290 (y_ptr, post_ystride, lfi_n->mblim[filter_level]);
292 if (!skip_lf)
293 vp8_loop_filter_simple_bh
294 (y_ptr, post_ystride, lfi_n->blim[filter_level]);
297 y_ptr += 16;
298 u_ptr += 8;
299 v_ptr += 8;
301 mode_info_context++; /* step to next MB */
305 void vp8_loop_filter_frame(VP8_COMMON *cm,
306 MACROBLOCKD *mbd,
307 int frame_type)
309 YV12_BUFFER_CONFIG *post = cm->frame_to_show;
310 loop_filter_info_n *lfi_n = &cm->lf_info;
311 loop_filter_info lfi;
313 int mb_row;
314 int mb_col;
315 int mb_rows = cm->mb_rows;
316 int mb_cols = cm->mb_cols;
318 int filter_level;
320 unsigned char *y_ptr, *u_ptr, *v_ptr;
322 /* Point at base of Mb MODE_INFO list */
323 const MODE_INFO *mode_info_context = cm->mi;
324 int post_y_stride = post->y_stride;
325 int post_uv_stride = post->uv_stride;
327 /* Initialize the loop filter for this frame. */
328 vp8_loop_filter_frame_init(cm, mbd, cm->filter_level);
330 /* Set up the buffer pointers */
331 y_ptr = post->y_buffer;
332 u_ptr = post->u_buffer;
333 v_ptr = post->v_buffer;
335 /* vp8_filter each macro block */
336 if (cm->filter_type == NORMAL_LOOPFILTER)
338 for (mb_row = 0; mb_row < mb_rows; mb_row++)
340 for (mb_col = 0; mb_col < mb_cols; mb_col++)
342 int skip_lf = (mode_info_context->mbmi.mode != B_PRED &&
343 mode_info_context->mbmi.mode != SPLITMV &&
344 mode_info_context->mbmi.mb_skip_coeff);
346 const int mode_index = lfi_n->mode_lf_lut[mode_info_context->mbmi.mode];
347 const int seg = mode_info_context->mbmi.segment_id;
348 const int ref_frame = mode_info_context->mbmi.ref_frame;
350 filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
352 if (filter_level)
354 const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level];
355 lfi.mblim = lfi_n->mblim[filter_level];
356 lfi.blim = lfi_n->blim[filter_level];
357 lfi.lim = lfi_n->lim[filter_level];
358 lfi.hev_thr = lfi_n->hev_thr[hev_index];
360 if (mb_col > 0)
361 vp8_loop_filter_mbv
362 (y_ptr, u_ptr, v_ptr, post_y_stride, post_uv_stride, &lfi);
364 if (!skip_lf)
365 vp8_loop_filter_bv
366 (y_ptr, u_ptr, v_ptr, post_y_stride, post_uv_stride, &lfi);
368 /* don't apply across umv border */
369 if (mb_row > 0)
370 vp8_loop_filter_mbh
371 (y_ptr, u_ptr, v_ptr, post_y_stride, post_uv_stride, &lfi);
373 if (!skip_lf)
374 vp8_loop_filter_bh
375 (y_ptr, u_ptr, v_ptr, post_y_stride, post_uv_stride, &lfi);
378 y_ptr += 16;
379 u_ptr += 8;
380 v_ptr += 8;
382 mode_info_context++; /* step to next MB */
384 y_ptr += post_y_stride * 16 - post->y_width;
385 u_ptr += post_uv_stride * 8 - post->uv_width;
386 v_ptr += post_uv_stride * 8 - post->uv_width;
388 mode_info_context++; /* Skip border mb */
392 else /* SIMPLE_LOOPFILTER */
394 for (mb_row = 0; mb_row < mb_rows; mb_row++)
396 for (mb_col = 0; mb_col < mb_cols; mb_col++)
398 int skip_lf = (mode_info_context->mbmi.mode != B_PRED &&
399 mode_info_context->mbmi.mode != SPLITMV &&
400 mode_info_context->mbmi.mb_skip_coeff);
402 const int mode_index = lfi_n->mode_lf_lut[mode_info_context->mbmi.mode];
403 const int seg = mode_info_context->mbmi.segment_id;
404 const int ref_frame = mode_info_context->mbmi.ref_frame;
406 filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
407 if (filter_level)
409 const unsigned char * mblim = lfi_n->mblim[filter_level];
410 const unsigned char * blim = lfi_n->blim[filter_level];
412 if (mb_col > 0)
413 vp8_loop_filter_simple_mbv
414 (y_ptr, post_y_stride, mblim);
416 if (!skip_lf)
417 vp8_loop_filter_simple_bv
418 (y_ptr, post_y_stride, blim);
420 /* don't apply across umv border */
421 if (mb_row > 0)
422 vp8_loop_filter_simple_mbh
423 (y_ptr, post_y_stride, mblim);
425 if (!skip_lf)
426 vp8_loop_filter_simple_bh
427 (y_ptr, post_y_stride, blim);
430 y_ptr += 16;
431 u_ptr += 8;
432 v_ptr += 8;
434 mode_info_context++; /* step to next MB */
436 y_ptr += post_y_stride * 16 - post->y_width;
437 u_ptr += post_uv_stride * 8 - post->uv_width;
438 v_ptr += post_uv_stride * 8 - post->uv_width;
440 mode_info_context++; /* Skip border mb */
446 void vp8_loop_filter_frame_yonly
448 VP8_COMMON *cm,
449 MACROBLOCKD *mbd,
450 int default_filt_lvl
453 YV12_BUFFER_CONFIG *post = cm->frame_to_show;
455 unsigned char *y_ptr;
456 int mb_row;
457 int mb_col;
459 loop_filter_info_n *lfi_n = &cm->lf_info;
460 loop_filter_info lfi;
462 int filter_level;
463 FRAME_TYPE frame_type = cm->frame_type;
465 /* Point at base of Mb MODE_INFO list */
466 const MODE_INFO *mode_info_context = cm->mi;
468 #if 0
469 if(default_filt_lvl == 0) /* no filter applied */
470 return;
471 #endif
473 /* Initialize the loop filter for this frame. */
474 vp8_loop_filter_frame_init( cm, mbd, default_filt_lvl);
476 /* Set up the buffer pointers */
477 y_ptr = post->y_buffer;
479 /* vp8_filter each macro block */
480 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
482 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
484 int skip_lf = (mode_info_context->mbmi.mode != B_PRED &&
485 mode_info_context->mbmi.mode != SPLITMV &&
486 mode_info_context->mbmi.mb_skip_coeff);
488 const int mode_index = lfi_n->mode_lf_lut[mode_info_context->mbmi.mode];
489 const int seg = mode_info_context->mbmi.segment_id;
490 const int ref_frame = mode_info_context->mbmi.ref_frame;
492 filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
494 if (filter_level)
496 if (cm->filter_type == NORMAL_LOOPFILTER)
498 const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level];
499 lfi.mblim = lfi_n->mblim[filter_level];
500 lfi.blim = lfi_n->blim[filter_level];
501 lfi.lim = lfi_n->lim[filter_level];
502 lfi.hev_thr = lfi_n->hev_thr[hev_index];
504 if (mb_col > 0)
505 vp8_loop_filter_mbv
506 (y_ptr, 0, 0, post->y_stride, 0, &lfi);
508 if (!skip_lf)
509 vp8_loop_filter_bv
510 (y_ptr, 0, 0, post->y_stride, 0, &lfi);
512 /* don't apply across umv border */
513 if (mb_row > 0)
514 vp8_loop_filter_mbh
515 (y_ptr, 0, 0, post->y_stride, 0, &lfi);
517 if (!skip_lf)
518 vp8_loop_filter_bh
519 (y_ptr, 0, 0, post->y_stride, 0, &lfi);
521 else
523 if (mb_col > 0)
524 vp8_loop_filter_simple_mbv
525 (y_ptr, post->y_stride, lfi_n->mblim[filter_level]);
527 if (!skip_lf)
528 vp8_loop_filter_simple_bv
529 (y_ptr, post->y_stride, lfi_n->blim[filter_level]);
531 /* don't apply across umv border */
532 if (mb_row > 0)
533 vp8_loop_filter_simple_mbh
534 (y_ptr, post->y_stride, lfi_n->mblim[filter_level]);
536 if (!skip_lf)
537 vp8_loop_filter_simple_bh
538 (y_ptr, post->y_stride, lfi_n->blim[filter_level]);
542 y_ptr += 16;
543 mode_info_context ++; /* step to next MB */
547 y_ptr += post->y_stride * 16 - post->y_width;
548 mode_info_context ++; /* Skip border mb */
553 void vp8_loop_filter_partial_frame
555 VP8_COMMON *cm,
556 MACROBLOCKD *mbd,
557 int default_filt_lvl
560 YV12_BUFFER_CONFIG *post = cm->frame_to_show;
562 unsigned char *y_ptr;
563 int mb_row;
564 int mb_col;
565 int mb_cols = post->y_width >> 4;
566 int mb_rows = post->y_height >> 4;
568 int linestocopy;
570 loop_filter_info_n *lfi_n = &cm->lf_info;
571 loop_filter_info lfi;
573 int filter_level;
574 FRAME_TYPE frame_type = cm->frame_type;
576 const MODE_INFO *mode_info_context;
578 #if 0
579 if(default_filt_lvl == 0) /* no filter applied */
580 return;
581 #endif
583 /* Initialize the loop filter for this frame. */
584 vp8_loop_filter_frame_init( cm, mbd, default_filt_lvl);
586 /* number of MB rows to use in partial filtering */
587 linestocopy = mb_rows / PARTIAL_FRAME_FRACTION;
588 linestocopy = linestocopy ? linestocopy << 4 : 16; /* 16 lines per MB */
590 /* Set up the buffer pointers; partial image starts at ~middle of frame */
591 y_ptr = post->y_buffer + ((post->y_height >> 5) * 16) * post->y_stride;
592 mode_info_context = cm->mi + (post->y_height >> 5) * (mb_cols + 1);
594 /* vp8_filter each macro block */
595 for (mb_row = 0; mb_row<(linestocopy >> 4); mb_row++)
597 for (mb_col = 0; mb_col < mb_cols; mb_col++)
599 int skip_lf = (mode_info_context->mbmi.mode != B_PRED &&
600 mode_info_context->mbmi.mode != SPLITMV &&
601 mode_info_context->mbmi.mb_skip_coeff);
603 const int mode_index =
604 lfi_n->mode_lf_lut[mode_info_context->mbmi.mode];
605 const int seg = mode_info_context->mbmi.segment_id;
606 const int ref_frame = mode_info_context->mbmi.ref_frame;
608 filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
610 if (filter_level)
612 if (cm->filter_type == NORMAL_LOOPFILTER)
614 const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level];
615 lfi.mblim = lfi_n->mblim[filter_level];
616 lfi.blim = lfi_n->blim[filter_level];
617 lfi.lim = lfi_n->lim[filter_level];
618 lfi.hev_thr = lfi_n->hev_thr[hev_index];
620 if (mb_col > 0)
621 vp8_loop_filter_mbv
622 (y_ptr, 0, 0, post->y_stride, 0, &lfi);
624 if (!skip_lf)
625 vp8_loop_filter_bv
626 (y_ptr, 0, 0, post->y_stride, 0, &lfi);
628 vp8_loop_filter_mbh
629 (y_ptr, 0, 0, post->y_stride, 0, &lfi);
631 if (!skip_lf)
632 vp8_loop_filter_bh
633 (y_ptr, 0, 0, post->y_stride, 0, &lfi);
635 else
637 if (mb_col > 0)
638 vp8_loop_filter_simple_mbv
639 (y_ptr, post->y_stride, lfi_n->mblim[filter_level]);
641 if (!skip_lf)
642 vp8_loop_filter_simple_bv
643 (y_ptr, post->y_stride, lfi_n->blim[filter_level]);
645 vp8_loop_filter_simple_mbh
646 (y_ptr, post->y_stride, lfi_n->mblim[filter_level]);
648 if (!skip_lf)
649 vp8_loop_filter_simple_bh
650 (y_ptr, post->y_stride, lfi_n->blim[filter_level]);
654 y_ptr += 16;
655 mode_info_context += 1; /* step to next MB */
658 y_ptr += post->y_stride * 16 - post->y_width;
659 mode_info_context += 1; /* Skip border mb */