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