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