/src/aom/av1/encoder/aq_variance.c
Line | Count | Source (jump to first uncovered line) |
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 <math.h> |
13 | | #include <stdlib.h> |
14 | | |
15 | | #include "aom_dsp/aom_dsp_common.h" |
16 | | #include "aom_ports/mem.h" |
17 | | |
18 | | #include "av1/encoder/aq_variance.h" |
19 | | #include "av1/common/seg_common.h" |
20 | | #include "av1/encoder/encodeframe.h" |
21 | | #include "av1/encoder/ratectrl.h" |
22 | | #include "av1/encoder/rd.h" |
23 | | #include "av1/encoder/segmentation.h" |
24 | | #include "av1/encoder/dwt.h" |
25 | | #include "config/aom_config.h" |
26 | | |
27 | | #if !CONFIG_REALTIME_ONLY |
28 | | static const double rate_ratio[MAX_SEGMENTS] = { 2.2, 1.7, 1.3, 1.0, |
29 | | 0.9, .8, .7, .6 }; |
30 | | |
31 | | static const double deltaq_rate_ratio[MAX_SEGMENTS] = { 2.5, 2.0, 1.5, 1.0, |
32 | | 0.75, 1.0, 1.0, 1.0 }; |
33 | 0 | #define ENERGY_MIN (-4) |
34 | 0 | #define ENERGY_MAX (1) |
35 | | #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1) |
36 | | #define ENERGY_IN_BOUNDS(energy) \ |
37 | 0 | assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX) |
38 | | |
39 | | static const int segment_id[ENERGY_SPAN] = { 0, 1, 1, 2, 3, 4 }; |
40 | | |
41 | 0 | #define SEGMENT_ID(i) segment_id[(i)-ENERGY_MIN] |
42 | | |
43 | 0 | void av1_vaq_frame_setup(AV1_COMP *cpi) { |
44 | 0 | AV1_COMMON *cm = &cpi->common; |
45 | 0 | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
46 | 0 | const int base_qindex = cm->quant_params.base_qindex; |
47 | 0 | struct segmentation *seg = &cm->seg; |
48 | 0 | int i; |
49 | |
|
50 | 0 | int resolution_change = |
51 | 0 | cm->prev_frame && (cm->width != cm->prev_frame->width || |
52 | 0 | cm->height != cm->prev_frame->height); |
53 | 0 | int avg_energy = (int)(cpi->twopass_frame.mb_av_energy - 2); |
54 | 0 | double avg_ratio; |
55 | 0 | if (avg_energy > 7) avg_energy = 7; |
56 | 0 | if (avg_energy < 0) avg_energy = 0; |
57 | 0 | avg_ratio = rate_ratio[avg_energy]; |
58 | |
|
59 | 0 | if (resolution_change) { |
60 | 0 | memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols); |
61 | 0 | av1_clearall_segfeatures(seg); |
62 | 0 | av1_disable_segmentation(seg); |
63 | 0 | return; |
64 | 0 | } |
65 | 0 | if (frame_is_intra_only(cm) || cm->features.error_resilient_mode || |
66 | 0 | refresh_frame->alt_ref_frame || |
67 | 0 | (refresh_frame->golden_frame && !cpi->rc.is_src_frame_alt_ref)) { |
68 | 0 | cpi->vaq_refresh = 1; |
69 | |
|
70 | 0 | av1_enable_segmentation(seg); |
71 | 0 | av1_clearall_segfeatures(seg); |
72 | |
|
73 | 0 | for (i = 0; i < MAX_SEGMENTS; ++i) { |
74 | | // Set up avg segment id to be 1.0 and adjust the other segments around |
75 | | // it. |
76 | 0 | int qindex_delta = |
77 | 0 | av1_compute_qdelta_by_rate(cpi, cm->current_frame.frame_type, |
78 | 0 | base_qindex, rate_ratio[i] / avg_ratio); |
79 | | |
80 | | // We don't allow qindex 0 in a segment if the base value is not 0. |
81 | | // Q index 0 (lossless) implies 4x4 encoding only and in AQ mode a segment |
82 | | // Q delta is sometimes applied without going back around the rd loop. |
83 | | // This could lead to an illegal combination of partition size and q. |
84 | 0 | if ((base_qindex != 0) && ((base_qindex + qindex_delta) == 0)) { |
85 | 0 | qindex_delta = -base_qindex + 1; |
86 | 0 | } |
87 | |
|
88 | 0 | av1_set_segdata(seg, i, SEG_LVL_ALT_Q, qindex_delta); |
89 | 0 | av1_enable_segfeature(seg, i, SEG_LVL_ALT_Q); |
90 | 0 | } |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | | int av1_log_block_avg(const AV1_COMP *cpi, const MACROBLOCK *x, BLOCK_SIZE bs, |
95 | 0 | int mi_row, int mi_col) { |
96 | | // This functions returns the block average of luma block |
97 | 0 | unsigned int sum, avg, num_pix; |
98 | 0 | int r, c; |
99 | 0 | const int pic_w = cpi->common.width; |
100 | 0 | const int pic_h = cpi->common.height; |
101 | 0 | const int bw = MI_SIZE * mi_size_wide[bs]; |
102 | 0 | const int bh = MI_SIZE * mi_size_high[bs]; |
103 | 0 | const uint16_t *x16 = CONVERT_TO_SHORTPTR(x->plane[0].src.buf); |
104 | |
|
105 | 0 | sum = 0; |
106 | 0 | num_pix = 0; |
107 | 0 | avg = 0; |
108 | 0 | int row = mi_row << MI_SIZE_LOG2; |
109 | 0 | int col = mi_col << MI_SIZE_LOG2; |
110 | 0 | for (r = row; (r < (row + bh)) && (r < pic_h); r++) { |
111 | 0 | for (c = col; (c < (col + bw)) && (c < pic_w); c++) { |
112 | 0 | sum += *(x16 + r * x->plane[0].src.stride + c); |
113 | 0 | num_pix++; |
114 | 0 | } |
115 | 0 | } |
116 | 0 | if (num_pix != 0) { |
117 | 0 | avg = sum / num_pix; |
118 | 0 | } |
119 | 0 | return avg; |
120 | 0 | } |
121 | | |
122 | 0 | #define DEFAULT_E_MIDPOINT 10.0 |
123 | | |
124 | 0 | static unsigned int haar_ac_energy(const MACROBLOCK *x, BLOCK_SIZE bs) { |
125 | 0 | const MACROBLOCKD *xd = &x->e_mbd; |
126 | 0 | int stride = x->plane[0].src.stride; |
127 | 0 | const uint8_t *buf = x->plane[0].src.buf; |
128 | 0 | const int num_8x8_cols = block_size_wide[bs] / 8; |
129 | 0 | const int num_8x8_rows = block_size_high[bs] / 8; |
130 | 0 | const int hbd = is_cur_buf_hbd(xd); |
131 | |
|
132 | 0 | int64_t var = av1_haar_ac_sad_mxn_uint8_input(buf, stride, hbd, num_8x8_rows, |
133 | 0 | num_8x8_cols); |
134 | |
|
135 | 0 | return (unsigned int)((uint64_t)var * 256) >> num_pels_log2_lookup[bs]; |
136 | 0 | } |
137 | | |
138 | 0 | static double log_block_wavelet_energy(const MACROBLOCK *x, BLOCK_SIZE bs) { |
139 | 0 | unsigned int haar_sad = haar_ac_energy(x, bs); |
140 | 0 | return log1p(haar_sad); |
141 | 0 | } |
142 | | |
143 | | int av1_block_wavelet_energy_level(const AV1_COMP *cpi, const MACROBLOCK *x, |
144 | 0 | BLOCK_SIZE bs) { |
145 | 0 | double energy, energy_midpoint; |
146 | 0 | energy_midpoint = (is_stat_consumption_stage_twopass(cpi)) |
147 | 0 | ? cpi->twopass_frame.frame_avg_haar_energy |
148 | 0 | : DEFAULT_E_MIDPOINT; |
149 | 0 | energy = log_block_wavelet_energy(x, bs) - energy_midpoint; |
150 | 0 | return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX); |
151 | 0 | } |
152 | | |
153 | | int av1_compute_q_from_energy_level_deltaq_mode(const AV1_COMP *const cpi, |
154 | 0 | int block_var_level) { |
155 | 0 | int rate_level; |
156 | 0 | const AV1_COMMON *const cm = &cpi->common; |
157 | |
|
158 | 0 | if (DELTA_Q_PERCEPTUAL_MODULATION == 1) { |
159 | 0 | ENERGY_IN_BOUNDS(block_var_level); |
160 | 0 | rate_level = SEGMENT_ID(block_var_level); |
161 | 0 | } else { |
162 | 0 | rate_level = block_var_level; |
163 | 0 | } |
164 | 0 | const int base_qindex = cm->quant_params.base_qindex; |
165 | 0 | int qindex_delta = |
166 | 0 | av1_compute_qdelta_by_rate(cpi, cm->current_frame.frame_type, base_qindex, |
167 | 0 | deltaq_rate_ratio[rate_level]); |
168 | |
|
169 | 0 | if ((base_qindex != 0) && ((base_qindex + qindex_delta) == 0)) { |
170 | 0 | qindex_delta = -base_qindex + 1; |
171 | 0 | } |
172 | 0 | return base_qindex + qindex_delta; |
173 | 0 | } |
174 | | |
175 | | // Comparer used by qsort() to order an array of unsigned int from smallest to |
176 | | // largest. |
177 | 0 | static int comp_unsigned_int(const void *a, const void *b) { |
178 | 0 | unsigned int arg1 = *(const unsigned int *)a; |
179 | 0 | unsigned int arg2 = *(const unsigned int *)b; |
180 | |
|
181 | 0 | return (arg1 > arg2) - (arg1 < arg2); |
182 | 0 | } |
183 | | |
184 | | unsigned int av1_get_variance_boost_block_variance(const AV1_COMP *cpi, |
185 | 0 | const MACROBLOCK *x) { |
186 | 0 | #define SUPERBLOCK_SIZE 64 |
187 | 0 | #define SUBBLOCK_SIZE 8 |
188 | 0 | #define SUBBLOCKS_IN_SB_DIM (SUPERBLOCK_SIZE / SUBBLOCK_SIZE) |
189 | 0 | #define SUBBLOCKS_IN_SB (SUBBLOCKS_IN_SB_DIM * SUBBLOCKS_IN_SB_DIM) |
190 | 0 | #define SUBBLOCKS_IN_OCTILE (SUBBLOCKS_IN_SB / 8) |
191 | 0 | DECLARE_ALIGNED(16, static const uint16_t, |
192 | 0 | av1_highbd_all_zeros[SUBBLOCK_SIZE]) = { 0 }; |
193 | 0 | DECLARE_ALIGNED(16, static const uint8_t, |
194 | 0 | av1_all_zeros[SUBBLOCK_SIZE]) = { 0 }; |
195 | |
|
196 | 0 | const MACROBLOCKD *xd = &x->e_mbd; |
197 | 0 | unsigned int sse; |
198 | | // Octile is currently hard-coded and optimized for still pictures. In the |
199 | | // future, we might want to expose this as a parameter that can be fine-tuned |
200 | | // by the caller. |
201 | | // An octile of 5 was chosen because it was found to strike the best balance |
202 | | // between quality and consistency. Lower octiles tend to score lower in |
203 | | // SSIMU2, while higher octiles tend to harm subjective quality consistency, |
204 | | // especially in <1 MP images. |
205 | 0 | const int octile = 5; |
206 | 0 | const uint8_t *all_zeros = is_cur_buf_hbd(xd) |
207 | 0 | ? CONVERT_TO_BYTEPTR(av1_highbd_all_zeros) |
208 | 0 | : av1_all_zeros; |
209 | 0 | unsigned int variances[SUBBLOCKS_IN_SB]; |
210 | | |
211 | | // Calculate subblock variances. |
212 | 0 | aom_variance_fn_t vf = cpi->ppi->fn_ptr[BLOCK_8X8].vf; |
213 | 0 | for (int subb_i = 0; subb_i < SUBBLOCKS_IN_SB_DIM; subb_i++) { |
214 | 0 | int i = subb_i * SUBBLOCK_SIZE; |
215 | 0 | for (int subb_j = 0; subb_j < SUBBLOCKS_IN_SB_DIM; subb_j++) { |
216 | 0 | int j = subb_j * SUBBLOCK_SIZE; |
217 | | // Truncating values to integers (i.e. the 64 term) was found to perform |
218 | | // better than rounding, or returning them as doubles. |
219 | 0 | variances[subb_i * SUBBLOCKS_IN_SB_DIM + subb_j] = |
220 | 0 | vf(x->plane[0].src.buf + i * x->plane[0].src.stride + j, |
221 | 0 | x->plane[0].src.stride, all_zeros, 0, &sse) / |
222 | 0 | 64; |
223 | 0 | } |
224 | 0 | } |
225 | | |
226 | | // Order the 8x8 SB values from smallest to largest variance. |
227 | 0 | qsort(variances, SUBBLOCKS_IN_SB, sizeof(unsigned int), comp_unsigned_int); |
228 | | |
229 | | // Sample three 8x8 variance values: at the specified octile, previous octile, |
230 | | // and next octile. Make sure we use the last subblock in each octile as the |
231 | | // representative of the octile. |
232 | 0 | assert(octile >= 1 && octile <= 8); |
233 | 0 | const int middle_index = octile * SUBBLOCKS_IN_OCTILE - 1; |
234 | 0 | const int lower_index = |
235 | 0 | AOMMAX(SUBBLOCKS_IN_OCTILE - 1, middle_index - SUBBLOCKS_IN_OCTILE); |
236 | 0 | const int upper_index = |
237 | 0 | AOMMIN(SUBBLOCKS_IN_SB - 1, middle_index + SUBBLOCKS_IN_OCTILE); |
238 | | |
239 | | // Weigh the three variances in a 1:2:1 ratio, with rounding (the +2 term). |
240 | | // This allows for smoother delta-q transitions among superblocks with |
241 | | // mixed-variance features. |
242 | 0 | const unsigned int variance = |
243 | 0 | (variances[lower_index] + (variances[middle_index] * 2) + |
244 | 0 | variances[upper_index] + 2) / |
245 | 0 | 4; |
246 | |
|
247 | 0 | return variance; |
248 | 0 | } |
249 | | #endif // !CONFIG_REALTIME_ONLY |
250 | | |
251 | 0 | int av1_log_block_var(const AV1_COMP *cpi, const MACROBLOCK *x, BLOCK_SIZE bs) { |
252 | 0 | DECLARE_ALIGNED(16, static const uint16_t, |
253 | 0 | av1_highbd_all_zeros[MAX_SB_SIZE]) = { 0 }; |
254 | 0 | DECLARE_ALIGNED(16, static const uint8_t, av1_all_zeros[MAX_SB_SIZE]) = { 0 }; |
255 | | |
256 | | // This function returns a score for the blocks local variance as calculated |
257 | | // by: sum of the log of the (4x4 variances) of each subblock to the current |
258 | | // block (x,bs) |
259 | | // * 32 / number of pixels in the block_size. |
260 | | // This is used for segmentation because to avoid situations in which a large |
261 | | // block with a gentle gradient gets marked high variance even though each |
262 | | // subblock has a low variance. This allows us to assign the same segment |
263 | | // number for the same sorts of area regardless of how the partitioning goes. |
264 | |
|
265 | 0 | const MACROBLOCKD *xd = &x->e_mbd; |
266 | 0 | double var = 0; |
267 | 0 | unsigned int sse; |
268 | 0 | int i, j; |
269 | |
|
270 | 0 | int right_overflow = |
271 | 0 | (xd->mb_to_right_edge < 0) ? ((-xd->mb_to_right_edge) >> 3) : 0; |
272 | 0 | int bottom_overflow = |
273 | 0 | (xd->mb_to_bottom_edge < 0) ? ((-xd->mb_to_bottom_edge) >> 3) : 0; |
274 | |
|
275 | 0 | const int bw = MI_SIZE * mi_size_wide[bs] - right_overflow; |
276 | 0 | const int bh = MI_SIZE * mi_size_high[bs] - bottom_overflow; |
277 | |
|
278 | 0 | aom_variance_fn_t vf = cpi->ppi->fn_ptr[BLOCK_4X4].vf; |
279 | 0 | for (i = 0; i < bh; i += 4) { |
280 | 0 | for (j = 0; j < bw; j += 4) { |
281 | 0 | if (is_cur_buf_hbd(xd)) { |
282 | 0 | var += log1p(vf(x->plane[0].src.buf + i * x->plane[0].src.stride + j, |
283 | 0 | x->plane[0].src.stride, |
284 | 0 | CONVERT_TO_BYTEPTR(av1_highbd_all_zeros), 0, &sse) / |
285 | 0 | 16.0); |
286 | 0 | } else { |
287 | 0 | var += log1p(vf(x->plane[0].src.buf + i * x->plane[0].src.stride + j, |
288 | 0 | x->plane[0].src.stride, av1_all_zeros, 0, &sse) / |
289 | 0 | 16.0); |
290 | 0 | } |
291 | 0 | } |
292 | 0 | } |
293 | | // Use average of 4x4 log variance. The range for 8 bit 0 - 9.704121561. |
294 | 0 | var /= (bw / 4 * bh / 4); |
295 | 0 | if (var > 7) var = 7; |
296 | |
|
297 | 0 | return (int)(var); |
298 | 0 | } |