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.
14 #include "./vpx_scale_rtcd.h"
16 #include "vpx_mem/vpx_mem.h"
18 #include "vp9/common/vp9_loopfilter.h"
19 #include "vp9/common/vp9_onyxc_int.h"
20 #include "vp9/common/vp9_quant_common.h"
22 #include "vp9/encoder/vp9_encoder.h"
23 #include "vp9/encoder/vp9_picklpf.h"
24 #include "vp9/encoder/vp9_quantize.h"
26 static int get_max_filter_level(const VP9_COMP *cpi) {
28 return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
31 return MAX_LOOP_FILTER;
36 static int try_filter_frame(const YV12_BUFFER_CONFIG *sd, VP9_COMP *const cpi,
37 int filt_level, int partial_frame) {
38 VP9_COMMON *const cm = &cpi->common;
41 vp9_loop_filter_frame(cm->frame_to_show, cm, &cpi->mb.e_mbd, filt_level, 1,
43 filt_err = vp9_get_y_sse(sd, cm->frame_to_show);
45 // Re-instate the unfiltered frame
46 vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
51 static int search_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
53 const VP9_COMMON *const cm = &cpi->common;
54 const struct loopfilter *const lf = &cm->lf;
55 const int min_filter_level = 0;
56 const int max_filter_level = get_max_filter_level(cpi);
57 int filt_direction = 0;
58 int best_err, filt_best;
60 // Start the search at the previous frame filter level unless it is now out of
62 int filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
63 int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
64 // Sum squared error at each filter level
65 int ss_err[MAX_LOOP_FILTER + 1];
67 // Set each entry to -1
68 vpx_memset(ss_err, 0xFF, sizeof(ss_err));
70 // Make a copy of the unfiltered / processed recon buffer
71 vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
73 best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame);
75 ss_err[filt_mid] = best_err;
77 while (filter_step > 0) {
78 const int filt_high = MIN(filt_mid + filter_step, max_filter_level);
79 const int filt_low = MAX(filt_mid - filter_step, min_filter_level);
82 // Bias against raising loop filter in favor of lowering it.
83 int bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
85 if ((cpi->pass == 2) && (cpi->twopass.section_intra_rating < 20))
86 bias = (bias * cpi->twopass.section_intra_rating) / 20;
88 // yx, bias less for large block size
89 if (cm->tx_mode != ONLY_4X4)
92 if (filt_direction <= 0 && filt_low != filt_mid) {
93 // Get Low filter error score
94 if (ss_err[filt_low] < 0) {
95 filt_err = try_filter_frame(sd, cpi, filt_low, partial_frame);
96 ss_err[filt_low] = filt_err;
98 filt_err = ss_err[filt_low];
100 // If value is close to the best so far then bias towards a lower loop
102 if ((filt_err - bias) < best_err) {
103 // Was it actually better than the previous best?
104 if (filt_err < best_err)
107 filt_best = filt_low;
111 // Now look at filt_high
112 if (filt_direction >= 0 && filt_high != filt_mid) {
113 if (ss_err[filt_high] < 0) {
114 filt_err = try_filter_frame(sd, cpi, filt_high, partial_frame);
115 ss_err[filt_high] = filt_err;
117 filt_err = ss_err[filt_high];
119 // Was it better than the previous best?
120 if (filt_err < (best_err - bias)) {
122 filt_best = filt_high;
126 // Half the step distance if the best filter value was the same as last time
127 if (filt_best == filt_mid) {
131 filt_direction = (filt_best < filt_mid) ? -1 : 1;
132 filt_mid = filt_best;
139 void vp9_pick_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
140 LPF_PICK_METHOD method) {
141 VP9_COMMON *const cm = &cpi->common;
142 struct loopfilter *const lf = &cm->lf;
144 lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0
145 : cpi->oxcf.sharpness;
147 if (method == LPF_PICK_MINIMAL_LPF && lf->filter_level) {
148 lf->filter_level = 0;
149 } else if (method >= LPF_PICK_FROM_Q) {
150 const int min_filter_level = 0;
151 const int max_filter_level = get_max_filter_level(cpi);
152 const int q = vp9_ac_quant(cm->base_qindex, 0);
153 // These values were determined by linear fitting the result of the
154 // searched level, filt_guess = q * 0.316206 + 3.87252
155 int filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
156 if (cm->frame_type == KEY_FRAME)
158 lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
160 lf->filter_level = search_filter_level(sd, cpi,
161 method == LPF_PICK_FROM_SUBIMAGE);