Line data Source code
1 : /*
2 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 : *
4 : * This source code is subject to the terms of the BSD 2 Clause License and
5 : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 : * was not distributed with this source code in the LICENSE file, you can
7 : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 : * Media Patent License 1.0 was not distributed with this source code in the
9 : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 : */
11 :
12 : #include <assert.h>
13 : #include <limits.h>
14 :
15 : #include "./aom_scale_rtcd.h"
16 :
17 : #include "aom_dsp/psnr.h"
18 : #include "aom_dsp/aom_dsp_common.h"
19 : #include "aom_mem/aom_mem.h"
20 : #include "aom_ports/mem.h"
21 :
22 : #include "av1/common/av1_loopfilter.h"
23 : #include "av1/common/onyxc_int.h"
24 : #include "av1/common/quant_common.h"
25 :
26 : #include "av1/encoder/av1_quantize.h"
27 : #include "av1/encoder/encoder.h"
28 : #include "av1/encoder/picklpf.h"
29 :
30 0 : int av1_get_max_filter_level(const AV1_COMP *cpi) {
31 0 : if (cpi->oxcf.pass == 2) {
32 0 : return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
33 0 : : MAX_LOOP_FILTER;
34 : } else {
35 0 : return MAX_LOOP_FILTER;
36 : }
37 : }
38 :
39 0 : static int64_t try_filter_frame(const YV12_BUFFER_CONFIG *sd,
40 : AV1_COMP *const cpi, int filt_level,
41 : int partial_frame) {
42 0 : AV1_COMMON *const cm = &cpi->common;
43 : int64_t filt_err;
44 :
45 : #if CONFIG_VAR_TX || CONFIG_EXT_PARTITION || CONFIG_CB4X4
46 0 : av1_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd, filt_level, 1,
47 : partial_frame);
48 : #else
49 : if (cpi->num_workers > 1)
50 : av1_loop_filter_frame_mt(cm->frame_to_show, cm, cpi->td.mb.e_mbd.plane,
51 : filt_level, 1, partial_frame, cpi->workers,
52 : cpi->num_workers, &cpi->lf_row_sync);
53 : else
54 : av1_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd, filt_level,
55 : 1, partial_frame);
56 : #endif
57 :
58 : #if CONFIG_HIGHBITDEPTH
59 0 : if (cm->use_highbitdepth) {
60 0 : filt_err = aom_highbd_get_y_sse(sd, cm->frame_to_show);
61 : } else {
62 0 : filt_err = aom_get_y_sse(sd, cm->frame_to_show);
63 : }
64 : #else
65 : filt_err = aom_get_y_sse(sd, cm->frame_to_show);
66 : #endif // CONFIG_HIGHBITDEPTH
67 :
68 : // Re-instate the unfiltered frame
69 0 : aom_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
70 :
71 0 : return filt_err;
72 : }
73 :
74 0 : int av1_search_filter_level(const YV12_BUFFER_CONFIG *sd, AV1_COMP *cpi,
75 : int partial_frame, double *best_cost_ret) {
76 0 : const AV1_COMMON *const cm = &cpi->common;
77 0 : const struct loopfilter *const lf = &cm->lf;
78 0 : const int min_filter_level = 0;
79 0 : const int max_filter_level = av1_get_max_filter_level(cpi);
80 0 : int filt_direction = 0;
81 : int64_t best_err;
82 : int filt_best;
83 0 : MACROBLOCK *x = &cpi->td.mb;
84 :
85 : // Start the search at the previous frame filter level unless it is now out of
86 : // range.
87 0 : int filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
88 0 : int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
89 : // Sum squared error at each filter level
90 : int64_t ss_err[MAX_LOOP_FILTER + 1];
91 :
92 : // Set each entry to -1
93 0 : memset(ss_err, 0xFF, sizeof(ss_err));
94 :
95 : // Make a copy of the unfiltered / processed recon buffer
96 0 : aom_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
97 :
98 0 : best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame);
99 0 : filt_best = filt_mid;
100 0 : ss_err[filt_mid] = best_err;
101 :
102 0 : while (filter_step > 0) {
103 0 : const int filt_high = AOMMIN(filt_mid + filter_step, max_filter_level);
104 0 : const int filt_low = AOMMAX(filt_mid - filter_step, min_filter_level);
105 :
106 : // Bias against raising loop filter in favor of lowering it.
107 0 : int64_t bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
108 :
109 0 : if ((cpi->oxcf.pass == 2) && (cpi->twopass.section_intra_rating < 20))
110 0 : bias = (bias * cpi->twopass.section_intra_rating) / 20;
111 :
112 : // yx, bias less for large block size
113 0 : if (cm->tx_mode != ONLY_4X4) bias >>= 1;
114 :
115 0 : if (filt_direction <= 0 && filt_low != filt_mid) {
116 : // Get Low filter error score
117 0 : if (ss_err[filt_low] < 0) {
118 0 : ss_err[filt_low] = try_filter_frame(sd, cpi, filt_low, partial_frame);
119 : }
120 : // If value is close to the best so far then bias towards a lower loop
121 : // filter value.
122 0 : if (ss_err[filt_low] < (best_err + bias)) {
123 : // Was it actually better than the previous best?
124 0 : if (ss_err[filt_low] < best_err) {
125 0 : best_err = ss_err[filt_low];
126 : }
127 0 : filt_best = filt_low;
128 : }
129 : }
130 :
131 : // Now look at filt_high
132 0 : if (filt_direction >= 0 && filt_high != filt_mid) {
133 0 : if (ss_err[filt_high] < 0) {
134 0 : ss_err[filt_high] = try_filter_frame(sd, cpi, filt_high, partial_frame);
135 : }
136 : // If value is significantly better than previous best, bias added against
137 : // raising filter value
138 0 : if (ss_err[filt_high] < (best_err - bias)) {
139 0 : best_err = ss_err[filt_high];
140 0 : filt_best = filt_high;
141 : }
142 : }
143 :
144 : // Half the step distance if the best filter value was the same as last time
145 0 : if (filt_best == filt_mid) {
146 0 : filter_step /= 2;
147 0 : filt_direction = 0;
148 : } else {
149 0 : filt_direction = (filt_best < filt_mid) ? -1 : 1;
150 0 : filt_mid = filt_best;
151 : }
152 : }
153 :
154 : // Update best error
155 0 : best_err = ss_err[filt_best];
156 :
157 0 : if (best_cost_ret)
158 0 : *best_cost_ret = RDCOST_DBL(x->rdmult, x->rddiv, 0, best_err);
159 0 : return filt_best;
160 : }
161 :
162 0 : void av1_pick_filter_level(const YV12_BUFFER_CONFIG *sd, AV1_COMP *cpi,
163 : LPF_PICK_METHOD method) {
164 0 : AV1_COMMON *const cm = &cpi->common;
165 0 : struct loopfilter *const lf = &cm->lf;
166 :
167 0 : lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0 : cpi->oxcf.sharpness;
168 :
169 0 : if (method == LPF_PICK_MINIMAL_LPF && lf->filter_level) {
170 0 : lf->filter_level = 0;
171 0 : } else if (method >= LPF_PICK_FROM_Q) {
172 0 : const int min_filter_level = 0;
173 0 : const int max_filter_level = av1_get_max_filter_level(cpi);
174 0 : const int q = av1_ac_quant(cm->base_qindex, 0, cm->bit_depth);
175 : // These values were determined by linear fitting the result of the
176 : // searched level, filt_guess = q * 0.316206 + 3.87252
177 : #if CONFIG_HIGHBITDEPTH
178 : int filt_guess;
179 0 : switch (cm->bit_depth) {
180 : case AOM_BITS_8:
181 0 : filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
182 0 : break;
183 : case AOM_BITS_10:
184 0 : filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 4060632, 20);
185 0 : break;
186 : case AOM_BITS_12:
187 0 : filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 16242526, 22);
188 0 : break;
189 : default:
190 0 : assert(0 &&
191 : "bit_depth should be AOM_BITS_8, AOM_BITS_10 "
192 : "or AOM_BITS_12");
193 : return;
194 : }
195 : #else
196 : int filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
197 : #endif // CONFIG_HIGHBITDEPTH
198 0 : if (cm->frame_type == KEY_FRAME) filt_guess -= 4;
199 0 : lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
200 : } else {
201 0 : lf->filter_level = av1_search_filter_level(
202 : sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL);
203 : }
204 :
205 : #if CONFIG_EXT_TILE
206 : // TODO(any): 0 loopfilter level is only necessary if individual tile
207 : // decoding is required. We need to communicate this requirement to this
208 : // code and force loop filter level 0 only if required.
209 : if (cm->tile_encoding_mode) lf->filter_level = 0;
210 : #endif // CONFIG_EXT_TILE
211 0 : }
|