/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 | | |
14 | | #include "aom_ports/mem.h" |
15 | | |
16 | | #include "av1/encoder/aq_variance.h" |
17 | | #include "av1/common/seg_common.h" |
18 | | #include "av1/encoder/encodeframe.h" |
19 | | #include "av1/encoder/ratectrl.h" |
20 | | #include "av1/encoder/rd.h" |
21 | | #include "av1/encoder/segmentation.h" |
22 | | #include "av1/encoder/dwt.h" |
23 | | |
24 | | static const double rate_ratio[MAX_SEGMENTS] = { 2.2, 1.7, 1.3, 1.0, |
25 | | 0.9, .8, .7, .6 }; |
26 | | |
27 | | static const double deltaq_rate_ratio[MAX_SEGMENTS] = { 2.5, 2.0, 1.5, 1.0, |
28 | | 0.75, 1.0, 1.0, 1.0 }; |
29 | 0 | #define ENERGY_MIN (-4) |
30 | 0 | #define ENERGY_MAX (1) |
31 | | #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1) |
32 | | #define ENERGY_IN_BOUNDS(energy) \ |
33 | 0 | assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX) |
34 | | |
35 | | DECLARE_ALIGNED(16, static const uint8_t, av1_all_zeros[MAX_SB_SIZE]) = { 0 }; |
36 | | |
37 | | DECLARE_ALIGNED(16, static const uint16_t, |
38 | | av1_highbd_all_zeros[MAX_SB_SIZE]) = { 0 }; |
39 | | |
40 | | static const int segment_id[ENERGY_SPAN] = { 0, 1, 1, 2, 3, 4 }; |
41 | | |
42 | 0 | #define SEGMENT_ID(i) segment_id[(i)-ENERGY_MIN] |
43 | | |
44 | 0 | void av1_vaq_frame_setup(AV1_COMP *cpi) { |
45 | 0 | AV1_COMMON *cm = &cpi->common; |
46 | 0 | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
47 | 0 | const int base_qindex = cm->quant_params.base_qindex; |
48 | 0 | struct segmentation *seg = &cm->seg; |
49 | 0 | int i; |
50 | |
|
51 | 0 | int resolution_change = |
52 | 0 | cm->prev_frame && (cm->width != cm->prev_frame->width || |
53 | 0 | cm->height != cm->prev_frame->height); |
54 | 0 | int avg_energy = (int)(cpi->twopass_frame.mb_av_energy - 2); |
55 | 0 | double avg_ratio; |
56 | 0 | if (avg_energy > 7) avg_energy = 7; |
57 | 0 | if (avg_energy < 0) avg_energy = 0; |
58 | 0 | avg_ratio = rate_ratio[avg_energy]; |
59 | |
|
60 | 0 | if (resolution_change) { |
61 | 0 | memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols); |
62 | 0 | av1_clearall_segfeatures(seg); |
63 | 0 | av1_disable_segmentation(seg); |
64 | 0 | return; |
65 | 0 | } |
66 | 0 | if (frame_is_intra_only(cm) || cm->features.error_resilient_mode || |
67 | 0 | refresh_frame->alt_ref_frame || |
68 | 0 | (refresh_frame->golden_frame && !cpi->rc.is_src_frame_alt_ref)) { |
69 | 0 | cpi->vaq_refresh = 1; |
70 | |
|
71 | 0 | av1_enable_segmentation(seg); |
72 | 0 | av1_clearall_segfeatures(seg); |
73 | |
|
74 | 0 | for (i = 0; i < MAX_SEGMENTS; ++i) { |
75 | | // Set up avg segment id to be 1.0 and adjust the other segments around |
76 | | // it. |
77 | 0 | int qindex_delta = av1_compute_qdelta_by_rate( |
78 | 0 | &cpi->rc, cm->current_frame.frame_type, base_qindex, |
79 | 0 | rate_ratio[i] / avg_ratio, cpi->is_screen_content_type, |
80 | 0 | cm->seq_params->bit_depth); |
81 | | |
82 | | // We don't allow qindex 0 in a segment if the base value is not 0. |
83 | | // Q index 0 (lossless) implies 4x4 encoding only and in AQ mode a segment |
84 | | // Q delta is sometimes applied without going back around the rd loop. |
85 | | // This could lead to an illegal combination of partition size and q. |
86 | 0 | if ((base_qindex != 0) && ((base_qindex + qindex_delta) == 0)) { |
87 | 0 | qindex_delta = -base_qindex + 1; |
88 | 0 | } |
89 | |
|
90 | 0 | av1_set_segdata(seg, i, SEG_LVL_ALT_Q, qindex_delta); |
91 | 0 | av1_enable_segfeature(seg, i, SEG_LVL_ALT_Q); |
92 | 0 | } |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | 0 | int av1_log_block_var(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) { |
97 | | // This functions returns a score for the blocks local variance as calculated |
98 | | // by: sum of the log of the (4x4 variances) of each subblock to the current |
99 | | // block (x,bs) |
100 | | // * 32 / number of pixels in the block_size. |
101 | | // This is used for segmentation because to avoid situations in which a large |
102 | | // block with a gentle gradient gets marked high variance even though each |
103 | | // subblock has a low variance. This allows us to assign the same segment |
104 | | // number for the same sorts of area regardless of how the partitioning goes. |
105 | |
|
106 | 0 | MACROBLOCKD *xd = &x->e_mbd; |
107 | 0 | double var = 0; |
108 | 0 | unsigned int sse; |
109 | 0 | int i, j; |
110 | |
|
111 | 0 | int right_overflow = |
112 | 0 | (xd->mb_to_right_edge < 0) ? ((-xd->mb_to_right_edge) >> 3) : 0; |
113 | 0 | int bottom_overflow = |
114 | 0 | (xd->mb_to_bottom_edge < 0) ? ((-xd->mb_to_bottom_edge) >> 3) : 0; |
115 | |
|
116 | 0 | const int bw = MI_SIZE * mi_size_wide[bs] - right_overflow; |
117 | 0 | const int bh = MI_SIZE * mi_size_high[bs] - bottom_overflow; |
118 | |
|
119 | 0 | for (i = 0; i < bh; i += 4) { |
120 | 0 | for (j = 0; j < bw; j += 4) { |
121 | 0 | if (is_cur_buf_hbd(xd)) { |
122 | 0 | var += |
123 | 0 | log(1.0 + cpi->ppi->fn_ptr[BLOCK_4X4].vf( |
124 | 0 | x->plane[0].src.buf + i * x->plane[0].src.stride + j, |
125 | 0 | x->plane[0].src.stride, |
126 | 0 | CONVERT_TO_BYTEPTR(av1_highbd_all_zeros), 0, &sse) / |
127 | 0 | 16.0); |
128 | 0 | } else { |
129 | 0 | var += |
130 | 0 | log(1.0 + cpi->ppi->fn_ptr[BLOCK_4X4].vf( |
131 | 0 | x->plane[0].src.buf + i * x->plane[0].src.stride + j, |
132 | 0 | x->plane[0].src.stride, av1_all_zeros, 0, &sse) / |
133 | 0 | 16.0); |
134 | 0 | } |
135 | 0 | } |
136 | 0 | } |
137 | | // Use average of 4x4 log variance. The range for 8 bit 0 - 9.704121561. |
138 | 0 | var /= (bw / 4 * bh / 4); |
139 | 0 | if (var > 7) var = 7; |
140 | |
|
141 | 0 | return (int)(var); |
142 | 0 | } |
143 | | |
144 | | int av1_log_block_avg(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs, |
145 | 0 | int mi_row, int mi_col) { |
146 | | // This functions returns the block average of luma block |
147 | 0 | unsigned int sum, avg, num_pix; |
148 | 0 | int r, c; |
149 | 0 | const int pic_w = cpi->common.width; |
150 | 0 | const int pic_h = cpi->common.height; |
151 | 0 | const int bw = MI_SIZE * mi_size_wide[bs]; |
152 | 0 | const int bh = MI_SIZE * mi_size_high[bs]; |
153 | 0 | const uint16_t *x16 = CONVERT_TO_SHORTPTR(x->plane[0].src.buf); |
154 | |
|
155 | 0 | sum = 0; |
156 | 0 | num_pix = 0; |
157 | 0 | avg = 0; |
158 | 0 | int row = mi_row << MI_SIZE_LOG2; |
159 | 0 | int col = mi_col << MI_SIZE_LOG2; |
160 | 0 | for (r = row; (r < (row + bh)) && (r < pic_h); r++) { |
161 | 0 | for (c = col; (c < (col + bw)) && (c < pic_w); c++) { |
162 | 0 | sum += *(x16 + r * x->plane[0].src.stride + c); |
163 | 0 | num_pix++; |
164 | 0 | } |
165 | 0 | } |
166 | 0 | if (num_pix != 0) { |
167 | 0 | avg = sum / num_pix; |
168 | 0 | } |
169 | 0 | return avg; |
170 | 0 | } |
171 | | |
172 | 0 | #define DEFAULT_E_MIDPOINT 10.0 |
173 | | |
174 | 0 | static unsigned int haar_ac_energy(MACROBLOCK *x, BLOCK_SIZE bs) { |
175 | 0 | MACROBLOCKD *xd = &x->e_mbd; |
176 | 0 | int stride = x->plane[0].src.stride; |
177 | 0 | uint8_t *buf = x->plane[0].src.buf; |
178 | 0 | const int num_8x8_cols = block_size_wide[bs] / 8; |
179 | 0 | const int num_8x8_rows = block_size_high[bs] / 8; |
180 | 0 | const int hbd = is_cur_buf_hbd(xd); |
181 | |
|
182 | 0 | int64_t var = av1_haar_ac_sad_mxn_uint8_input(buf, stride, hbd, num_8x8_rows, |
183 | 0 | num_8x8_cols); |
184 | |
|
185 | 0 | return (unsigned int)((uint64_t)var * 256) >> num_pels_log2_lookup[bs]; |
186 | 0 | } |
187 | | |
188 | 0 | double av1_log_block_wavelet_energy(MACROBLOCK *x, BLOCK_SIZE bs) { |
189 | 0 | unsigned int haar_sad = haar_ac_energy(x, bs); |
190 | 0 | return log(haar_sad + 1.0); |
191 | 0 | } |
192 | | |
193 | | int av1_block_wavelet_energy_level(const AV1_COMP *cpi, MACROBLOCK *x, |
194 | 0 | BLOCK_SIZE bs) { |
195 | 0 | double energy, energy_midpoint; |
196 | 0 | energy_midpoint = (is_stat_consumption_stage_twopass(cpi)) |
197 | 0 | ? cpi->twopass_frame.frame_avg_haar_energy |
198 | 0 | : DEFAULT_E_MIDPOINT; |
199 | 0 | energy = av1_log_block_wavelet_energy(x, bs) - energy_midpoint; |
200 | 0 | return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX); |
201 | 0 | } |
202 | | |
203 | | int av1_compute_q_from_energy_level_deltaq_mode(const AV1_COMP *const cpi, |
204 | 0 | int block_var_level) { |
205 | 0 | int rate_level; |
206 | 0 | const AV1_COMMON *const cm = &cpi->common; |
207 | |
|
208 | 0 | if (DELTA_Q_PERCEPTUAL_MODULATION == 1) { |
209 | 0 | ENERGY_IN_BOUNDS(block_var_level); |
210 | 0 | rate_level = SEGMENT_ID(block_var_level); |
211 | 0 | } else { |
212 | 0 | rate_level = block_var_level; |
213 | 0 | } |
214 | 0 | const int base_qindex = cm->quant_params.base_qindex; |
215 | 0 | int qindex_delta = av1_compute_qdelta_by_rate( |
216 | 0 | &cpi->rc, cm->current_frame.frame_type, base_qindex, |
217 | 0 | deltaq_rate_ratio[rate_level], cpi->is_screen_content_type, |
218 | 0 | cm->seq_params->bit_depth); |
219 | |
|
220 | 0 | if ((base_qindex != 0) && ((base_qindex + qindex_delta) == 0)) { |
221 | 0 | qindex_delta = -base_qindex + 1; |
222 | 0 | } |
223 | 0 | return base_qindex + qindex_delta; |
224 | 0 | } |