2 * AAC encoder intensity stereo
3 * Copyright (C) 2015 Rostislav Pehlivanov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * AAC encoder quantizer
25 * @author Rostislav Pehlivanov ( atomnuker gmail com )
28 #ifndef AVCODEC_AACENC_QUANTIZATION_H
29 #define AVCODEC_AACENC_QUANTIZATION_H
33 #include "aacenctab.h"
34 #include "aacenc_utils.h"
37 * Calculate rate distortion cost for quantizing with given codebook
39 * @return quantization distortion
41 static av_always_inline float quantize_and_encode_band_cost_template(
42 struct AACEncContext *s,
43 PutBitContext *pb, const float *in, float *out,
44 const float *scaled, int size, int scale_idx,
45 int cb, const float lambda, const float uplim,
46 int *bits, int BT_ZERO, int BT_UNSIGNED,
47 int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO,
50 const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
51 const float Q = ff_aac_pow2sf_tab [q_idx];
52 const float Q34 = ff_aac_pow34sf_tab[q_idx];
53 const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
54 const float CLIPPED_ESCAPE = 165140.0f*IQ;
57 const int dim = BT_PAIR ? 2 : 4;
61 if (BT_ZERO || BT_NOISE || BT_STEREO) {
62 for (i = 0; i < size; i++)
67 for (i = 0; i < size; i += dim)
68 for (j = 0; j < dim; j++)
74 abs_pow34_v(s->scoefs, in, size);
77 quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, aac_cb_maxval[cb], ROUNDING);
81 off = aac_cb_maxval[cb];
83 for (i = 0; i < size; i += dim) {
85 int *quants = s->qcoefs + i;
88 float quantized, rd = 0.0f;
89 for (j = 0; j < dim; j++) {
90 curidx *= aac_cb_range[cb];
91 curidx += quants[j] + off;
93 curbits = ff_aac_spectral_bits[cb-1][curidx];
94 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
96 for (j = 0; j < dim; j++) {
97 float t = fabsf(in[i+j]);
99 if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
100 if (t >= CLIPPED_ESCAPE) {
101 quantized = CLIPPED_ESCAPE;
104 int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
105 quantized = c*cbrtf(c)*IQ;
106 curbits += av_log2(c)*2 - 4 + 1;
109 quantized = vec[j]*IQ;
113 out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
119 for (j = 0; j < dim; j++) {
120 quantized = vec[j]*IQ;
122 out[i+j] = quantized;
123 rd += (in[i+j] - quantized)*(in[i+j] - quantized);
126 cost += rd * lambda + curbits;
131 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
133 for (j = 0; j < dim; j++)
134 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
135 put_bits(pb, 1, in[i+j] < 0.0f);
137 for (j = 0; j < 2; j++) {
138 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
139 int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13);
140 int len = av_log2(coef);
142 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
143 put_sbits(pb, len, coef);
155 static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
156 const float *in, float *quant, const float *scaled,
157 int size, int scale_idx, int cb,
158 const float lambda, const float uplim,
164 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
165 static float quantize_and_encode_band_cost_ ## NAME( \
166 struct AACEncContext *s, \
167 PutBitContext *pb, const float *in, float *quant, \
168 const float *scaled, int size, int scale_idx, \
169 int cb, const float lambda, const float uplim, \
171 return quantize_and_encode_band_cost_template( \
172 s, pb, in, quant, scaled, size, scale_idx, \
173 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
174 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
178 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD)
179 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
180 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
181 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
182 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
183 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD)
184 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
185 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
186 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD)
188 static float (*const quantize_and_encode_band_cost_arr[])(
189 struct AACEncContext *s,
190 PutBitContext *pb, const float *in, float *quant,
191 const float *scaled, int size, int scale_idx,
192 int cb, const float lambda, const float uplim,
194 quantize_and_encode_band_cost_ZERO,
195 quantize_and_encode_band_cost_SQUAD,
196 quantize_and_encode_band_cost_SQUAD,
197 quantize_and_encode_band_cost_UQUAD,
198 quantize_and_encode_band_cost_UQUAD,
199 quantize_and_encode_band_cost_SPAIR,
200 quantize_and_encode_band_cost_SPAIR,
201 quantize_and_encode_band_cost_UPAIR,
202 quantize_and_encode_band_cost_UPAIR,
203 quantize_and_encode_band_cost_UPAIR,
204 quantize_and_encode_band_cost_UPAIR,
205 quantize_and_encode_band_cost_ESC,
206 quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
207 quantize_and_encode_band_cost_NOISE,
208 quantize_and_encode_band_cost_STEREO,
209 quantize_and_encode_band_cost_STEREO,
212 static float (*const quantize_and_encode_band_cost_rtz_arr[])(
213 struct AACEncContext *s,
214 PutBitContext *pb, const float *in, float *quant,
215 const float *scaled, int size, int scale_idx,
216 int cb, const float lambda, const float uplim,
218 quantize_and_encode_band_cost_ZERO,
219 quantize_and_encode_band_cost_SQUAD,
220 quantize_and_encode_band_cost_SQUAD,
221 quantize_and_encode_band_cost_UQUAD,
222 quantize_and_encode_band_cost_UQUAD,
223 quantize_and_encode_band_cost_SPAIR,
224 quantize_and_encode_band_cost_SPAIR,
225 quantize_and_encode_band_cost_UPAIR,
226 quantize_and_encode_band_cost_UPAIR,
227 quantize_and_encode_band_cost_UPAIR,
228 quantize_and_encode_band_cost_UPAIR,
229 quantize_and_encode_band_cost_ESC_RTZ,
230 quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
231 quantize_and_encode_band_cost_NOISE,
232 quantize_and_encode_band_cost_STEREO,
233 quantize_and_encode_band_cost_STEREO,
236 #define quantize_and_encode_band_cost( \
237 s, pb, in, quant, scaled, size, scale_idx, cb, \
238 lambda, uplim, bits, rtz) \
239 ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \
240 s, pb, in, quant, scaled, size, scale_idx, cb, \
243 static inline float quantize_band_cost(struct AACEncContext *s, const float *in,
244 const float *scaled, int size, int scale_idx,
245 int cb, const float lambda, const float uplim,
248 return quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx,
249 cb, lambda, uplim, bits, rtz);
252 static inline int quantize_band_cost_bits(struct AACEncContext *s, const float *in,
253 const float *scaled, int size, int scale_idx,
254 int cb, const float lambda, const float uplim,
258 quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx,
259 cb, 0.0f, uplim, &_bits, rtz);
266 static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
267 const float *in, float *out, int size, int scale_idx,
268 int cb, const float lambda, int rtz)
270 quantize_and_encode_band_cost(s, pb, in, out, NULL, size, scale_idx, cb, lambda,
271 INFINITY, NULL, rtz);
274 #endif /* AVCODEC_AACENC_QUANTIZATION_H */