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