1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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 */
10
11 #include <assert.h>
12 #include <limits.h>
13
14 #include "./vpx_scale_rtcd.h"
15
16 #include "vpx_mem/vpx_mem.h"
17 #include "vpx_ports/mem.h"
18
19 #include "vp9/common/vp9_loopfilter.h"
20 #include "vp9/common/vp9_onyxc_int.h"
21 #include "vp9/common/vp9_quant_common.h"
22
23 #include "vp9/encoder/vp9_encoder.h"
24 #include "vp9/encoder/vp9_picklpf.h"
25 #include "vp9/encoder/vp9_quantize.h"
26
get_max_filter_level(const VP9_COMP * cpi)27 static int get_max_filter_level(const VP9_COMP *cpi) {
28 if (cpi->oxcf.pass == 2) {
29 return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
30 : MAX_LOOP_FILTER;
31 } else {
32 return MAX_LOOP_FILTER;
33 }
34 }
35
36
try_filter_frame(const YV12_BUFFER_CONFIG * sd,VP9_COMP * const cpi,int filt_level,int partial_frame)37 static int64_t try_filter_frame(const YV12_BUFFER_CONFIG *sd,
38 VP9_COMP *const cpi,
39 int filt_level, int partial_frame) {
40 VP9_COMMON *const cm = &cpi->common;
41 int64_t filt_err;
42
43 vp9_build_mask_frame(cm, filt_level, partial_frame);
44
45 if (cpi->num_workers > 1)
46 vp9_loop_filter_frame_mt(cm->frame_to_show, cm, cpi->td.mb.e_mbd.plane,
47 filt_level, 1, partial_frame,
48 cpi->workers, cpi->num_workers, &cpi->lf_row_sync);
49 else
50 vp9_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd, filt_level,
51 1, partial_frame);
52
53 #if CONFIG_VP9_HIGHBITDEPTH
54 if (cm->use_highbitdepth) {
55 filt_err = vp9_highbd_get_y_sse(sd, cm->frame_to_show);
56 } else {
57 filt_err = vp9_get_y_sse(sd, cm->frame_to_show);
58 }
59 #else
60 filt_err = vp9_get_y_sse(sd, cm->frame_to_show);
61 #endif // CONFIG_VP9_HIGHBITDEPTH
62
63 // Re-instate the unfiltered frame
64 vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
65
66 return filt_err;
67 }
68
search_filter_level(const YV12_BUFFER_CONFIG * sd,VP9_COMP * cpi,int partial_frame)69 static int search_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
70 int partial_frame) {
71 const VP9_COMMON *const cm = &cpi->common;
72 const struct loopfilter *const lf = &cm->lf;
73 const int min_filter_level = 0;
74 const int max_filter_level = get_max_filter_level(cpi);
75 int filt_direction = 0;
76 int64_t best_err;
77 int filt_best;
78
79 // Start the search at the previous frame filter level unless it is now out of
80 // range.
81 int filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
82 int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
83 // Sum squared error at each filter level
84 int64_t ss_err[MAX_LOOP_FILTER + 1];
85
86 // Set each entry to -1
87 memset(ss_err, 0xFF, sizeof(ss_err));
88
89 // Make a copy of the unfiltered / processed recon buffer
90 vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
91
92 best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame);
93 filt_best = filt_mid;
94 ss_err[filt_mid] = best_err;
95
96 while (filter_step > 0) {
97 const int filt_high = VPXMIN(filt_mid + filter_step, max_filter_level);
98 const int filt_low = VPXMAX(filt_mid - filter_step, min_filter_level);
99
100 // Bias against raising loop filter in favor of lowering it.
101 int64_t bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
102
103 if ((cpi->oxcf.pass == 2) && (cpi->twopass.section_intra_rating < 20))
104 bias = (bias * cpi->twopass.section_intra_rating) / 20;
105
106 // yx, bias less for large block size
107 if (cm->tx_mode != ONLY_4X4)
108 bias >>= 1;
109
110 if (filt_direction <= 0 && filt_low != filt_mid) {
111 // Get Low filter error score
112 if (ss_err[filt_low] < 0) {
113 ss_err[filt_low] = try_filter_frame(sd, cpi, filt_low, partial_frame);
114 }
115 // If value is close to the best so far then bias towards a lower loop
116 // filter value.
117 if ((ss_err[filt_low] - bias) < best_err) {
118 // Was it actually better than the previous best?
119 if (ss_err[filt_low] < best_err)
120 best_err = ss_err[filt_low];
121
122 filt_best = filt_low;
123 }
124 }
125
126 // Now look at filt_high
127 if (filt_direction >= 0 && filt_high != filt_mid) {
128 if (ss_err[filt_high] < 0) {
129 ss_err[filt_high] = try_filter_frame(sd, cpi, filt_high, partial_frame);
130 }
131 // Was it better than the previous best?
132 if (ss_err[filt_high] < (best_err - bias)) {
133 best_err = ss_err[filt_high];
134 filt_best = filt_high;
135 }
136 }
137
138 // Half the step distance if the best filter value was the same as last time
139 if (filt_best == filt_mid) {
140 filter_step /= 2;
141 filt_direction = 0;
142 } else {
143 filt_direction = (filt_best < filt_mid) ? -1 : 1;
144 filt_mid = filt_best;
145 }
146 }
147
148 return filt_best;
149 }
150
vp9_pick_filter_level(const YV12_BUFFER_CONFIG * sd,VP9_COMP * cpi,LPF_PICK_METHOD method)151 void vp9_pick_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
152 LPF_PICK_METHOD method) {
153 VP9_COMMON *const cm = &cpi->common;
154 struct loopfilter *const lf = &cm->lf;
155
156 lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0
157 : cpi->oxcf.sharpness;
158
159 if (method == LPF_PICK_MINIMAL_LPF && lf->filter_level) {
160 lf->filter_level = 0;
161 } else if (method >= LPF_PICK_FROM_Q) {
162 const int min_filter_level = 0;
163 const int max_filter_level = get_max_filter_level(cpi);
164 const int q = vp9_ac_quant(cm->base_qindex, 0, cm->bit_depth);
165 // These values were determined by linear fitting the result of the
166 // searched level, filt_guess = q * 0.316206 + 3.87252
167 #if CONFIG_VP9_HIGHBITDEPTH
168 int filt_guess;
169 switch (cm->bit_depth) {
170 case VPX_BITS_8:
171 filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
172 break;
173 case VPX_BITS_10:
174 filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 4060632, 20);
175 break;
176 case VPX_BITS_12:
177 filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 16242526, 22);
178 break;
179 default:
180 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 "
181 "or VPX_BITS_12");
182 return;
183 }
184 #else
185 int filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
186 #endif // CONFIG_VP9_HIGHBITDEPTH
187 if (cm->frame_type == KEY_FRAME)
188 filt_guess -= 4;
189 lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
190 } else {
191 lf->filter_level = search_filter_level(sd, cpi,
192 method == LPF_PICK_FROM_SUBIMAGE);
193 }
194 }
195