blob: 69d67b10c7f0132a1aa20e4172eb0a771f468d3a [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <limits.h>
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070016
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "aom_dsp/aom_dsp_common.h"
Cheng Chenf572cd32017-08-25 18:34:51 -070018#include "aom_dsp/psnr.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020#include "aom_ports/mem.h"
21
Tom Finegan17ce8b12017-02-08 12:46:31 -080022#include "av1/common/av1_loopfilter.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070023#include "av1/common/onyxc_int.h"
24#include "av1/common/quant_common.h"
25
Tom Finegan17ce8b12017-02-08 12:46:31 -080026#include "av1/encoder/av1_quantize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/encoder/encoder.h"
28#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
Cheng Chen9efbaf92017-07-27 14:09:59 -070030static void yv12_copy_plane(const YV12_BUFFER_CONFIG *src_bc,
31 YV12_BUFFER_CONFIG *dst_bc, int plane) {
32 switch (plane) {
33 case 0: aom_yv12_copy_y(src_bc, dst_bc); break;
34 case 1: aom_yv12_copy_u(src_bc, dst_bc); break;
35 case 2: aom_yv12_copy_v(src_bc, dst_bc); break;
36 default: assert(plane >= 0 && plane <= 2); break;
37 }
38}
39
Yaowu Xuf883b422016-08-30 14:01:10 -070040int av1_get_max_filter_level(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070041 if (cpi->oxcf.pass == 2) {
42 return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
43 : MAX_LOOP_FILTER;
44 } else {
45 return MAX_LOOP_FILTER;
46 }
47}
48
49static int64_t try_filter_frame(const YV12_BUFFER_CONFIG *sd,
Yaowu Xuf883b422016-08-30 14:01:10 -070050 AV1_COMP *const cpi, int filt_level,
Cheng Chene94df5c2017-07-19 17:25:33 -070051 int partial_frame
Cheng Chen13fc8192017-08-19 11:49:28 -070052#if CONFIG_LOOPFILTER_LEVEL
Cheng Chene94df5c2017-07-19 17:25:33 -070053 ,
Cheng Chen179479f2017-08-04 10:56:39 -070054 int plane, int dir
Cheng Chene94df5c2017-07-19 17:25:33 -070055#endif
56 ) {
Yaowu Xuf883b422016-08-30 14:01:10 -070057 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -070058 int64_t filt_err;
59
Cheng Chen13fc8192017-08-19 11:49:28 -070060#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen9efbaf92017-07-27 14:09:59 -070061 assert(plane >= 0 && plane <= 2);
Cheng Chen179479f2017-08-04 10:56:39 -070062 int filter_level[2] = { filt_level, filt_level };
63 if (plane == 0 && dir == 0) filter_level[1] = cm->lf.filter_level[1];
64 if (plane == 0 && dir == 1) filter_level[0] = cm->lf.filter_level[0];
65
66 av1_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd,
67 filter_level[0], filter_level[1], plane, partial_frame);
Cheng Chene94df5c2017-07-19 17:25:33 -070068#else
Yaowu Xuf883b422016-08-30 14:01:10 -070069 av1_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd, filt_level, 1,
70 partial_frame);
Cheng Chen13fc8192017-08-19 11:49:28 -070071#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -070072
Cheng Chen9efbaf92017-07-27 14:09:59 -070073 int highbd = 0;
Cheng Chen9efbaf92017-07-27 14:09:59 -070074 highbd = cm->use_highbitdepth;
Cheng Chen9efbaf92017-07-27 14:09:59 -070075
Cheng Chen13fc8192017-08-19 11:49:28 -070076#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen9efbaf92017-07-27 14:09:59 -070077 filt_err = aom_get_sse_plane(sd, cm->frame_to_show, plane, highbd);
Cheng Chene94df5c2017-07-19 17:25:33 -070078
79 // Re-instate the unfiltered frame
Cheng Chen9efbaf92017-07-27 14:09:59 -070080 yv12_copy_plane(&cpi->last_frame_uf, cm->frame_to_show, plane);
Cheng Chene94df5c2017-07-19 17:25:33 -070081#else
Cheng Chen9efbaf92017-07-27 14:09:59 -070082 filt_err = aom_get_sse_plane(sd, cm->frame_to_show, 0, highbd);
Yaowu Xuc27fc142016-08-22 16:08:15 -070083
84 // Re-instate the unfiltered frame
Cheng Chen9efbaf92017-07-27 14:09:59 -070085 yv12_copy_plane(&cpi->last_frame_uf, cm->frame_to_show, 0);
Cheng Chen13fc8192017-08-19 11:49:28 -070086#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -070087
88 return filt_err;
89}
90
Cheng Chen1545bdb2017-09-05 16:32:12 -070091static int search_filter_level(const YV12_BUFFER_CONFIG *sd, AV1_COMP *cpi,
92 int partial_frame, double *best_cost_ret
Cheng Chen13fc8192017-08-19 11:49:28 -070093#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen1545bdb2017-09-05 16:32:12 -070094 ,
95 int plane, int dir
Cheng Chene94df5c2017-07-19 17:25:33 -070096#endif
Cheng Chen1545bdb2017-09-05 16:32:12 -070097 ) {
Yaowu Xuf883b422016-08-30 14:01:10 -070098 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -070099 const struct loopfilter *const lf = &cm->lf;
100 const int min_filter_level = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700101 const int max_filter_level = av1_get_max_filter_level(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102 int filt_direction = 0;
103 int64_t best_err;
104 int filt_best;
105 MACROBLOCK *x = &cpi->td.mb;
106
Cheng Chene94df5c2017-07-19 17:25:33 -0700107// Start the search at the previous frame filter level unless it is now out of
108// range.
Cheng Chen13fc8192017-08-19 11:49:28 -0700109#if CONFIG_LOOPFILTER_LEVEL
Cheng Chene94df5c2017-07-19 17:25:33 -0700110 int lvl;
111 switch (plane) {
Cheng Chen179479f2017-08-04 10:56:39 -0700112 case 0: lvl = (dir == 1) ? lf->filter_level[1] : lf->filter_level[0]; break;
Cheng Chene94df5c2017-07-19 17:25:33 -0700113 case 1: lvl = lf->filter_level_u; break;
114 case 2: lvl = lf->filter_level_v; break;
Cheng Chen9efbaf92017-07-27 14:09:59 -0700115 default: assert(plane >= 0 && plane <= 2); return 0;
Cheng Chene94df5c2017-07-19 17:25:33 -0700116 }
117 int filt_mid = clamp(lvl, min_filter_level, max_filter_level);
118#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119 int filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
Cheng Chen13fc8192017-08-19 11:49:28 -0700120#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700121 int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
122 // Sum squared error at each filter level
123 int64_t ss_err[MAX_LOOP_FILTER + 1];
124
125 // Set each entry to -1
126 memset(ss_err, 0xFF, sizeof(ss_err));
127
Cheng Chen13fc8192017-08-19 11:49:28 -0700128#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen9efbaf92017-07-27 14:09:59 -0700129 yv12_copy_plane(cm->frame_to_show, &cpi->last_frame_uf, plane);
Cheng Chene94df5c2017-07-19 17:25:33 -0700130#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700131 // Make a copy of the unfiltered / processed recon buffer
Yaowu Xuf883b422016-08-30 14:01:10 -0700132 aom_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
Cheng Chen13fc8192017-08-19 11:49:28 -0700133#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134
Cheng Chen13fc8192017-08-19 11:49:28 -0700135#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -0700136 best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame, plane, dir);
Cheng Chene94df5c2017-07-19 17:25:33 -0700137#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138 best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame);
Cheng Chen13fc8192017-08-19 11:49:28 -0700139#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140 filt_best = filt_mid;
141 ss_err[filt_mid] = best_err;
142
143 while (filter_step > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700144 const int filt_high = AOMMIN(filt_mid + filter_step, max_filter_level);
145 const int filt_low = AOMMAX(filt_mid - filter_step, min_filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700146
147 // Bias against raising loop filter in favor of lowering it.
148 int64_t bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
149
150 if ((cpi->oxcf.pass == 2) && (cpi->twopass.section_intra_rating < 20))
151 bias = (bias * cpi->twopass.section_intra_rating) / 20;
152
153 // yx, bias less for large block size
154 if (cm->tx_mode != ONLY_4X4) bias >>= 1;
155
156 if (filt_direction <= 0 && filt_low != filt_mid) {
157 // Get Low filter error score
158 if (ss_err[filt_low] < 0) {
Cheng Chen13fc8192017-08-19 11:49:28 -0700159#if CONFIG_LOOPFILTER_LEVEL
Cheng Chene94df5c2017-07-19 17:25:33 -0700160 ss_err[filt_low] =
Cheng Chen179479f2017-08-04 10:56:39 -0700161 try_filter_frame(sd, cpi, filt_low, partial_frame, plane, dir);
Cheng Chene94df5c2017-07-19 17:25:33 -0700162#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700163 ss_err[filt_low] = try_filter_frame(sd, cpi, filt_low, partial_frame);
Cheng Chen13fc8192017-08-19 11:49:28 -0700164#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 }
166 // If value is close to the best so far then bias towards a lower loop
167 // filter value.
168 if (ss_err[filt_low] < (best_err + bias)) {
169 // Was it actually better than the previous best?
170 if (ss_err[filt_low] < best_err) {
171 best_err = ss_err[filt_low];
172 }
173 filt_best = filt_low;
174 }
175 }
176
177 // Now look at filt_high
178 if (filt_direction >= 0 && filt_high != filt_mid) {
179 if (ss_err[filt_high] < 0) {
Cheng Chen13fc8192017-08-19 11:49:28 -0700180#if CONFIG_LOOPFILTER_LEVEL
Cheng Chene94df5c2017-07-19 17:25:33 -0700181 ss_err[filt_high] =
Cheng Chen179479f2017-08-04 10:56:39 -0700182 try_filter_frame(sd, cpi, filt_high, partial_frame, plane, dir);
Cheng Chene94df5c2017-07-19 17:25:33 -0700183#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 ss_err[filt_high] = try_filter_frame(sd, cpi, filt_high, partial_frame);
Cheng Chen13fc8192017-08-19 11:49:28 -0700185#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 }
187 // If value is significantly better than previous best, bias added against
188 // raising filter value
189 if (ss_err[filt_high] < (best_err - bias)) {
190 best_err = ss_err[filt_high];
191 filt_best = filt_high;
192 }
193 }
194
195 // Half the step distance if the best filter value was the same as last time
196 if (filt_best == filt_mid) {
197 filter_step /= 2;
198 filt_direction = 0;
199 } else {
200 filt_direction = (filt_best < filt_mid) ? -1 : 1;
201 filt_mid = filt_best;
202 }
203 }
204
205 // Update best error
206 best_err = ss_err[filt_best];
207
Urvang Joshi70006e42017-06-14 16:08:55 -0700208 if (best_cost_ret) *best_cost_ret = RDCOST_DBL(x->rdmult, 0, best_err);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209 return filt_best;
210}
211
Yaowu Xuf883b422016-08-30 14:01:10 -0700212void av1_pick_filter_level(const YV12_BUFFER_CONFIG *sd, AV1_COMP *cpi,
213 LPF_PICK_METHOD method) {
214 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000215 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700216 struct loopfilter *const lf = &cm->lf;
Cheng Chen765e34e2017-12-11 11:43:35 -0800217 (void)sd;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218
219 lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0 : cpi->oxcf.sharpness;
220
Cheng Chen179479f2017-08-04 10:56:39 -0700221 if (method == LPF_PICK_MINIMAL_LPF) {
Cheng Chen13fc8192017-08-19 11:49:28 -0700222#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -0700223 lf->filter_level[0] = 0;
224 lf->filter_level[1] = 0;
225#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 lf->filter_level = 0;
Cheng Chen179479f2017-08-04 10:56:39 -0700227#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700228 } else if (method >= LPF_PICK_FROM_Q) {
229 const int min_filter_level = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700230 const int max_filter_level = av1_get_max_filter_level(cpi);
Monty Montgomery60f2a222017-11-01 19:48:38 -0400231 const int q = av1_ac_quant_Q3(cm->base_qindex, 0, cm->bit_depth);
Yaowu Xud3e7c682017-12-21 14:08:25 -0800232 // These values were determined by linear fitting the result of the
233 // searched level for 8 bit depth:
234 // Keyframes: filt_guess = q * 0.06699 - 1.60817
235 // Other frames: filt_guess = q * 0.02295 + 2.48225
236 //
237 // And high bit depth separately:
238 // filt_guess = q * 0.316206 + 3.87252
Yaowu Xuc27fc142016-08-22 16:08:15 -0700239 int filt_guess;
240 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700241 case AOM_BITS_8:
Alexander Bokovac504f72017-10-10 13:15:13 -0700242 filt_guess = (cm->frame_type == KEY_FRAME)
243 ? ROUND_POWER_OF_TWO(q * 17563 - 421574, 18)
244 : ROUND_POWER_OF_TWO(q * 6017 + 650707, 18);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245 break;
Yaowu Xuf883b422016-08-30 14:01:10 -0700246 case AOM_BITS_10:
Yaowu Xuc27fc142016-08-22 16:08:15 -0700247 filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 4060632, 20);
248 break;
Yaowu Xuf883b422016-08-30 14:01:10 -0700249 case AOM_BITS_12:
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250 filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 16242526, 22);
251 break;
252 default:
253 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700254 "bit_depth should be AOM_BITS_8, AOM_BITS_10 "
255 "or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256 return;
257 }
Alexander Bokovac504f72017-10-10 13:15:13 -0700258 if (cm->bit_depth != AOM_BITS_8 && cm->frame_type == KEY_FRAME)
259 filt_guess -= 4;
Cheng Chen13fc8192017-08-19 11:49:28 -0700260#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen765e34e2017-12-11 11:43:35 -0800261 // TODO(chengchen): retrain the model for Y, U, V filter levels
Cheng Chen179479f2017-08-04 10:56:39 -0700262 lf->filter_level[0] = clamp(filt_guess, min_filter_level, max_filter_level);
263 lf->filter_level[1] = clamp(filt_guess, min_filter_level, max_filter_level);
Cheng Chen765e34e2017-12-11 11:43:35 -0800264 lf->filter_level_u = clamp(filt_guess, min_filter_level, max_filter_level);
265 lf->filter_level_v = clamp(filt_guess, min_filter_level, max_filter_level);
Cheng Chen179479f2017-08-04 10:56:39 -0700266#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700267 lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
Cheng Chen179479f2017-08-04 10:56:39 -0700268#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 } else {
Cheng Chen13fc8192017-08-19 11:49:28 -0700270#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen1545bdb2017-09-05 16:32:12 -0700271 lf->filter_level[0] = lf->filter_level[1] = search_filter_level(
Cheng Chen179479f2017-08-04 10:56:39 -0700272 sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL, 0, 2);
Cheng Chen1545bdb2017-09-05 16:32:12 -0700273 lf->filter_level[0] = search_filter_level(
Cheng Chen179479f2017-08-04 10:56:39 -0700274 sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL, 0, 0);
Cheng Chen1545bdb2017-09-05 16:32:12 -0700275 lf->filter_level[1] = search_filter_level(
Cheng Chen179479f2017-08-04 10:56:39 -0700276 sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL, 0, 1);
277
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000278 if (num_planes > 1) {
279 lf->filter_level_u = search_filter_level(
280 sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL, 1, 0);
281 lf->filter_level_v = search_filter_level(
282 sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL, 2, 0);
283 }
Cheng Chene94df5c2017-07-19 17:25:33 -0700284#else
Cheng Chen1545bdb2017-09-05 16:32:12 -0700285 lf->filter_level =
286 search_filter_level(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL);
Cheng Chen13fc8192017-08-19 11:49:28 -0700287#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289}