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.
13 #include "vp9/common/vp9_onyxc_int.h"
14 #include "vp9/encoder/vp9_onyx_int.h"
15 #include "vp9/encoder/vp9_picklpf.h"
16 #include "vp9/encoder/vp9_quantize.h"
17 #include "vp9/common/vp9_quant_common.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_scale/vpx_scale.h"
20 #include "vp9/common/vp9_alloccommon.h"
21 #include "vp9/common/vp9_loopfilter.h"
22 #include "./vpx_scale_rtcd.h"
24 static int get_min_filter_level(VP9_COMP *cpi, int base_qindex) {
28 static int get_max_filter_level(VP9_COMP *cpi, int base_qindex) {
29 return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
33 // Stub function for now Alt LF not used
34 void vp9_set_alt_lf_level(VP9_COMP *cpi, int filt_val) {
37 static int try_filter_frame(const YV12_BUFFER_CONFIG *sd, VP9_COMP *const cpi,
38 MACROBLOCKD *const xd, VP9_COMMON *const cm,
39 int filt_level, int partial_frame) {
42 vp9_set_alt_lf_level(cpi, filt_level);
43 vp9_loop_filter_frame(cm, xd, filt_level, 1, partial_frame);
45 filt_err = vp9_calc_ss_err(sd, cm->frame_to_show);
47 // Re-instate the unfiltered frame
48 vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
53 static void search_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
55 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
56 VP9_COMMON *const cm = &cpi->common;
57 struct loopfilter *const lf = &cm->lf;
58 const int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
59 const int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
62 int filt_direction = 0;
63 // Start the search at the previous frame filter level unless it is now out of
65 int filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
66 int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
67 // Sum squared error at each filter level
68 int ss_err[MAX_LOOP_FILTER + 1];
70 // Set each entry to -1
71 vpx_memset(ss_err, 0xFF, sizeof(ss_err));
73 // Make a copy of the unfiltered / processed recon buffer
74 vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
76 best_err = try_filter_frame(sd, cpi, xd, cm, filt_mid, partial_frame);
78 ss_err[filt_mid] = best_err;
80 while (filter_step > 0) {
81 const int filt_high = MIN(filt_mid + filter_step, max_filter_level);
82 const int filt_low = MAX(filt_mid - filter_step, min_filter_level);
85 // Bias against raising loop filter in favor of lowering it.
86 int bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
88 if (cpi->twopass.section_intra_rating < 20)
89 bias = bias * cpi->twopass.section_intra_rating / 20;
91 // yx, bias less for large block size
92 if (cm->tx_mode != ONLY_4X4)
95 if (filt_direction <= 0 && filt_low != filt_mid) {
96 // Get Low filter error score
97 if (ss_err[filt_low] < 0) {
98 filt_err = try_filter_frame(sd, cpi, xd, cm, filt_low, partial_frame);
99 ss_err[filt_low] = filt_err;
101 filt_err = ss_err[filt_low];
103 // If value is close to the best so far then bias towards a lower loop
105 if ((filt_err - bias) < best_err) {
106 // Was it actually better than the previous best?
107 if (filt_err < best_err)
110 filt_best = filt_low;
114 // Now look at filt_high
115 if (filt_direction >= 0 && filt_high != filt_mid) {
116 if (ss_err[filt_high] < 0) {
117 filt_err = try_filter_frame(sd, cpi, xd, cm, filt_high, partial_frame);
118 ss_err[filt_high] = filt_err;
120 filt_err = ss_err[filt_high];
122 // Was it better than the previous best?
123 if (filt_err < (best_err - bias)) {
125 filt_best = filt_high;
129 // Half the step distance if the best filter value was the same as last time
130 if (filt_best == filt_mid) {
131 filter_step = filter_step / 2;
134 filt_direction = (filt_best < filt_mid) ? -1 : 1;
135 filt_mid = filt_best;
139 lf->filter_level = filt_best;
142 void vp9_pick_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
144 VP9_COMMON *const cm = &cpi->common;
145 struct loopfilter *const lf = &cm->lf;
147 lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0
148 : cpi->oxcf.sharpness;
151 const int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
152 const int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
153 const int q = vp9_ac_quant(cm->base_qindex, 0);
154 // These values were determined by linear fitting the result of the
156 // filt_guess = q * 0.316206 + 3.87252
157 int filt_guess = (q * 20723 + 1015158 + (1 << 17)) >> 18;
158 if (cm->frame_type == KEY_FRAME)
160 lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
162 search_filter_level(sd, cpi, method == 1);