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