Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_quantize.c

Issue 17451020: libvpx: Pull from upstream (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_onyx_int.h ('k') | source/libvpx/vp9/encoder/vp9_rdopt.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 17 matching lines...) Expand all
28 28
29 static void quantize(int16_t *zbin_boost_orig_ptr, 29 static void quantize(int16_t *zbin_boost_orig_ptr,
30 int16_t *coeff_ptr, int n_coeffs, int skip_block, 30 int16_t *coeff_ptr, int n_coeffs, int skip_block,
31 int16_t *zbin_ptr, int16_t *round_ptr, int16_t *quant_ptr, 31 int16_t *zbin_ptr, int16_t *round_ptr, int16_t *quant_ptr,
32 uint8_t *quant_shift_ptr, 32 uint8_t *quant_shift_ptr,
33 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr, 33 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
34 int16_t *dequant_ptr, int zbin_oq_value, 34 int16_t *dequant_ptr, int zbin_oq_value,
35 uint16_t *eob_ptr, 35 uint16_t *eob_ptr,
36 const int *scan, int mul) { 36 const int *scan, int mul) {
37 int i, rc, eob; 37 int i, rc, eob;
38 int zbins[2], nzbins[2], zbin;
39 int x, y, z, sz;
40 int zero_run = 0;
41 int16_t *zbin_boost_ptr = zbin_boost_orig_ptr;
42 int zero_flag = n_coeffs;
43
44 vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
45 vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
46
47 eob = -1;
48
49 // Base ZBIN
50 zbins[0] = zbin_ptr[0] + zbin_oq_value;
51 zbins[1] = zbin_ptr[1] + zbin_oq_value;
52 nzbins[0] = zbins[0] * -1;
53 nzbins[1] = zbins[1] * -1;
54
55 if (!skip_block) {
56 // Pre-scan pass
57 for (i = n_coeffs - 1; i >= 0; i--) {
58 rc = scan[i];
59 z = coeff_ptr[rc] * mul;
60
61 if (z < zbins[rc != 0] && z > nzbins[rc != 0]) {
62 zero_flag--;
63 } else {
64 break;
65 }
66 }
67
68 // Quantization pass: All coefficients with index >= zero_flag are
69 // skippable. Note: zero_flag can be zero.
70 for (i = 0; i < zero_flag; i++) {
71 rc = scan[i];
72 z = coeff_ptr[rc] * mul;
73
74 zbin = (zbins[rc != 0] + zbin_boost_ptr[zero_run]);
75 zero_run += (zero_run < 15);
76
77 sz = (z >> 31); // sign of z
78 x = (z ^ sz) - sz;
79
80 if (x >= zbin) {
81 x += (round_ptr[rc != 0]);
82 y = ((int)(((int)(x * quant_ptr[rc != 0]) >> 16) + x))
83 >> quant_shift_ptr[rc != 0]; // quantize (x)
84 x = (y ^ sz) - sz; // get the sign back
85 qcoeff_ptr[rc] = x; // write to destination
86 dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0] / mul; // dequantized value
87
88 if (y) {
89 eob = i; // last nonzero coeffs
90 zero_run = 0; // set zero_run
91 }
92 }
93 }
94 }
95 *eob_ptr = eob + 1;
96 }
97
98 // This function works well for large transform size.
99 static void quantize_sparse(int16_t *zbin_boost_orig_ptr,
100 int16_t *coeff_ptr, int n_coeffs, int skip_block,
101 int16_t *zbin_ptr, int16_t *round_ptr,
102 int16_t *quant_ptr, uint8_t *quant_shift_ptr,
103 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
104 int16_t *dequant_ptr, int zbin_oq_value,
105 uint16_t *eob_ptr, const int *scan, int mul,
106 int *idx_arr) {
107 int i, rc, eob;
108 int zbins[2], pzbins[2], nzbins[2], zbin;
109 int x, y, z, sz;
110 int zero_run = 0;
111 int16_t *zbin_boost_ptr = zbin_boost_orig_ptr;
112 int idx = 0;
113 int pre_idx = 0;
114
115 vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
116 vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
117
118 eob = -1;
119
120 // Base ZBIN
121 zbins[0] = zbin_ptr[0] + zbin_oq_value;
122 zbins[1] = zbin_ptr[1] + zbin_oq_value;
123 // Positive and negative ZBIN
124 pzbins[0] = zbins[0]/mul;
125 pzbins[1] = zbins[1]/mul;
126 nzbins[0] = pzbins[0] * -1;
127 nzbins[1] = pzbins[1] * -1;
128
129 if (!skip_block) {
130 // Pre-scan pass
131 for (i = 0; i < n_coeffs; i++) {
132 rc = scan[i];
133 z = coeff_ptr[rc];
134
135 // If the coefficient is out of the base ZBIN range, keep it for
136 // quantization.
137 if (z >= pzbins[rc != 0] || z <= nzbins[rc != 0])
138 idx_arr[idx++] = i;
139 }
140
141 // Quantization pass: only process the coefficients selected in
142 // pre-scan pass. Note: idx can be zero.
143 for (i = 0; i < idx; i++) {
144 rc = scan[idx_arr[i]];
145
146 // Calculate ZBIN
147 zero_run += idx_arr[i] - pre_idx;
148 if(zero_run > 15) zero_run = 15;
149 zbin = (zbins[rc != 0] + zbin_boost_ptr[zero_run]);
150
151 pre_idx = idx_arr[i];
152 z = coeff_ptr[rc] * mul;
153 sz = (z >> 31); // sign of z
154 x = (z ^ sz) - sz; // x = abs(z)
155
156 if (x >= zbin) {
157 x += (round_ptr[rc != 0]);
158 y = ((int)(((int)(x * quant_ptr[rc != 0]) >> 16) + x))
159 >> quant_shift_ptr[rc != 0]; // quantize (x)
160
161 x = (y ^ sz) - sz; // get the sign back
162 qcoeff_ptr[rc] = x; // write to destination
163 dqcoeff_ptr[rc] = x * dequant_ptr[rc != 0] / mul; // dequantized value
164
165 if (y) {
166 eob = idx_arr[i]; // last nonzero coeffs
167 zero_run = -1; // set zero_run
168 }
169 }
170 }
171 }
172 *eob_ptr = eob + 1;
173 }
174 #if 0
175 // Original quantize function
176 static void quantize(int16_t *zbin_boost_orig_ptr,
177 int16_t *coeff_ptr, int n_coeffs, int skip_block,
178 int16_t *zbin_ptr, int16_t *round_ptr, int16_t *quant_ptr,
179 uint8_t *quant_shift_ptr,
180 int16_t *qcoeff_ptr, int16_t *dqcoeff_ptr,
181 int16_t *dequant_ptr, int zbin_oq_value,
182 uint16_t *eob_ptr,
183 const int *scan, int mul) {
184 int i, rc, eob;
38 int zbin; 185 int zbin;
39 int x, y, z, sz; 186 int x, y, z, sz;
40 int zero_run = 0; 187 int zero_run = 0;
41 int16_t *zbin_boost_ptr = zbin_boost_orig_ptr; 188 int16_t *zbin_boost_ptr = zbin_boost_orig_ptr;
42 189
43 vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t)); 190 vpx_memset(qcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
44 vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t)); 191 vpx_memset(dqcoeff_ptr, 0, n_coeffs*sizeof(int16_t));
45 192
46 eob = -1; 193 eob = -1;
47 194
(...skipping 19 matching lines...) Expand all
67 if (y) { 214 if (y) {
68 eob = i; // last nonzero coeffs 215 eob = i; // last nonzero coeffs
69 zero_run = 0; 216 zero_run = 0;
70 } 217 }
71 } 218 }
72 } 219 }
73 } 220 }
74 221
75 *eob_ptr = eob + 1; 222 *eob_ptr = eob + 1;
76 } 223 }
224 #endif
77 225
78 void vp9_quantize(MACROBLOCK *mb, int plane, int block, int n_coeffs, 226 void vp9_quantize(MACROBLOCK *mb, int plane, int block, int n_coeffs,
79 TX_TYPE tx_type) { 227 TX_TYPE tx_type) {
80 MACROBLOCKD *const xd = &mb->e_mbd; 228 MACROBLOCKD *const xd = &mb->e_mbd;
81 const int mul = n_coeffs == 1024 ? 2 : 1; 229 const int mul = n_coeffs == 1024 ? 2 : 1;
82 const int *scan; 230 const int *scan;
83 231
84 // These contexts may be available in the caller 232 // These contexts may be available in the caller
85 switch (n_coeffs) { 233 switch (n_coeffs) {
86 case 4 * 4: 234 case 4 * 4:
87 scan = get_scan_4x4(tx_type); 235 scan = get_scan_4x4(tx_type);
88 break; 236 break;
89 case 8 * 8: 237 case 8 * 8:
90 scan = get_scan_8x8(tx_type); 238 scan = get_scan_8x8(tx_type);
91 break; 239 break;
92 case 16 * 16: 240 case 16 * 16:
93 scan = get_scan_16x16(tx_type); 241 scan = get_scan_16x16(tx_type);
94 break; 242 break;
95 default: 243 default:
96 scan = vp9_default_scan_32x32; 244 scan = vp9_default_scan_32x32;
97 break; 245 break;
98 } 246 }
99 247
100 quantize(mb->plane[plane].zrun_zbin_boost, 248 // Call different quantization for different transform size.
101 BLOCK_OFFSET(mb->plane[plane].coeff, block, 16), 249 if (n_coeffs >= 1024) {
102 n_coeffs, mb->skip_block, 250 // Save index of picked coefficient in pre-scan pass.
103 mb->plane[plane].zbin, 251 int idx_arr[1024];
104 mb->plane[plane].round, 252
105 mb->plane[plane].quant, 253 quantize_sparse(mb->plane[plane].zrun_zbin_boost,
106 mb->plane[plane].quant_shift, 254 BLOCK_OFFSET(mb->plane[plane].coeff, block, 16),
107 BLOCK_OFFSET(xd->plane[plane].qcoeff, block, 16), 255 n_coeffs, mb->skip_block,
108 BLOCK_OFFSET(xd->plane[plane].dqcoeff, block, 16), 256 mb->plane[plane].zbin,
109 xd->plane[plane].dequant, 257 mb->plane[plane].round,
110 mb->plane[plane].zbin_extra, 258 mb->plane[plane].quant,
111 &xd->plane[plane].eobs[block], 259 mb->plane[plane].quant_shift,
112 scan, mul); 260 BLOCK_OFFSET(xd->plane[plane].qcoeff, block, 16),
261 BLOCK_OFFSET(xd->plane[plane].dqcoeff, block, 16),
262 xd->plane[plane].dequant,
263 mb->plane[plane].zbin_extra,
264 &xd->plane[plane].eobs[block],
265 scan, mul, idx_arr);
266 }
267 else {
268 quantize(mb->plane[plane].zrun_zbin_boost,
269 BLOCK_OFFSET(mb->plane[plane].coeff, block, 16),
270 n_coeffs, mb->skip_block,
271 mb->plane[plane].zbin,
272 mb->plane[plane].round,
273 mb->plane[plane].quant,
274 mb->plane[plane].quant_shift,
275 BLOCK_OFFSET(xd->plane[plane].qcoeff, block, 16),
276 BLOCK_OFFSET(xd->plane[plane].dqcoeff, block, 16),
277 xd->plane[plane].dequant,
278 mb->plane[plane].zbin_extra,
279 &xd->plane[plane].eobs[block],
280 scan, mul);
281 }
113 } 282 }
114 283
115 void vp9_regular_quantize_b_4x4(MACROBLOCK *mb, int b_idx, TX_TYPE tx_type, 284 void vp9_regular_quantize_b_4x4(MACROBLOCK *mb, int b_idx, TX_TYPE tx_type,
116 int y_blocks) { 285 int y_blocks) {
117 MACROBLOCKD *const xd = &mb->e_mbd; 286 MACROBLOCKD *const xd = &mb->e_mbd;
118 const struct plane_block_idx pb_idx = plane_block_idx(y_blocks, b_idx); 287 const struct plane_block_idx pb_idx = plane_block_idx(y_blocks, b_idx);
119 const int *pt_scan = get_scan_4x4(tx_type); 288 const int *pt_scan = get_scan_4x4(tx_type);
120 289
121 quantize(mb->plane[pb_idx.plane].zrun_zbin_boost, 290 quantize(mb->plane[pb_idx.plane].zrun_zbin_boost,
122 BLOCK_OFFSET(mb->plane[pb_idx.plane].coeff, pb_idx.block, 16), 291 BLOCK_OFFSET(mb->plane[pb_idx.plane].coeff, pb_idx.block, 16),
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 // have to be set. 472 // have to be set.
304 cm->y_dc_delta_q = 0; 473 cm->y_dc_delta_q = 0;
305 cm->uv_dc_delta_q = 0; 474 cm->uv_dc_delta_q = 0;
306 cm->uv_ac_delta_q = 0; 475 cm->uv_ac_delta_q = 0;
307 476
308 // quantizer has to be reinitialized if any delta_q changes. 477 // quantizer has to be reinitialized if any delta_q changes.
309 // As there are not any here for now this is inactive code. 478 // As there are not any here for now this is inactive code.
310 // if(update) 479 // if(update)
311 // vp9_init_quantizer(cpi); 480 // vp9_init_quantizer(cpi);
312 } 481 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_onyx_int.h ('k') | source/libvpx/vp9/encoder/vp9_rdopt.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698