FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aacenc_quantization.h
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 99 105 94.3%
Branches: 68 76 89.5%

Line Branch Exec Source
1 /*
2 * AAC encoder quantizer
3 * Copyright (C) 2015 Rostislav Pehlivanov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * AAC encoder quantizer
25 * @author Rostislav Pehlivanov ( atomnuker gmail com )
26 */
27
28 #ifndef AVCODEC_AACENC_QUANTIZATION_H
29 #define AVCODEC_AACENC_QUANTIZATION_H
30
31 #include "aactab.h"
32 #include "aacenc.h"
33 #include "aacenctab.h"
34 #include "aacenc_utils.h"
35
36 /**
37 * Calculate rate distortion cost for quantizing with given codebook
38 *
39 * @return quantization distortion
40 */
41 9209431 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, float *energy, int BT_ZERO, int BT_UNSIGNED,
47 int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO,
48 const float ROUNDING)
49 {
50 9209431 const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
51 9209431 const float Q = ff_aac_pow2sf_tab [q_idx];
52 9209431 const float Q34 = ff_aac_pow34sf_tab[q_idx];
53 9209431 const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
54 9209431 const float CLIPPED_ESCAPE = 165140.0f*IQ;
55 int i, j;
56 9209431 float cost = 0;
57 9209431 float qenergy = 0;
58
2/2
✓ Branch 0 taken 4543142 times.
✓ Branch 1 taken 4666289 times.
9209431 const int dim = BT_PAIR ? 2 : 4;
59 9209431 int resbits = 0;
60 int off;
61
62
6/6
✓ Branch 0 taken 7587715 times.
✓ Branch 1 taken 1621716 times.
✓ Branch 2 taken 7574415 times.
✓ Branch 3 taken 13300 times.
✓ Branch 4 taken 17350 times.
✓ Branch 5 taken 7557065 times.
9209431 if (BT_ZERO || BT_NOISE || BT_STEREO) {
63
2/2
✓ Branch 0 taken 37189524 times.
✓ Branch 1 taken 1652366 times.
38841890 for (i = 0; i < size; i++)
64 37189524 cost += in[i]*in[i];
65
2/2
✓ Branch 0 taken 1628852 times.
✓ Branch 1 taken 23514 times.
1652366 if (bits)
66 1628852 *bits = 0;
67
2/2
✓ Branch 0 taken 1596318 times.
✓ Branch 1 taken 56048 times.
1652366 if (energy)
68 1596318 *energy = qenergy;
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1652366 times.
1652366 if (out) {
70 for (i = 0; i < size; i += dim)
71 for (j = 0; j < dim; j++)
72 out[i+j] = 0.0f;
73 }
74 1652366 return cost * lambda;
75 }
76
2/2
✓ Branch 0 taken 469295 times.
✓ Branch 1 taken 7087770 times.
7557065 if (!scaled) {
77 469295 s->abs_pow34(s->scoefs, in, size);
78 469295 scaled = s->scoefs;
79 }
80 7557065 s->quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING);
81
2/2
✓ Branch 0 taken 4623855 times.
✓ Branch 1 taken 2933210 times.
7557065 if (BT_UNSIGNED) {
82 4623855 off = 0;
83 } else {
84 2933210 off = aac_cb_maxval[cb];
85 }
86
2/2
✓ Branch 0 taken 59793004 times.
✓ Branch 1 taken 7549665 times.
67342669 for (i = 0; i < size; i += dim) {
87 const float *vec;
88 59793004 int *quants = s->qcoefs + i;
89 59793004 int curidx = 0;
90 int curbits;
91 59793004 float quantized, rd = 0.0f;
92
2/2
✓ Branch 0 taken 154525248 times.
✓ Branch 1 taken 59793004 times.
214318252 for (j = 0; j < dim; j++) {
93 154525248 curidx *= aac_cb_range[cb];
94 154525248 curidx += quants[j] + off;
95 }
96 59793004 curbits = ff_aac_spectral_bits[cb-1][curidx];
97 59793004 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
98
2/2
✓ Branch 0 taken 37505267 times.
✓ Branch 1 taken 22287737 times.
59793004 if (BT_UNSIGNED) {
99
2/2
✓ Branch 0 taken 89600198 times.
✓ Branch 1 taken 37505267 times.
127105465 for (j = 0; j < dim; j++) {
100 89600198 float t = fabsf(in[i+j]);
101 float di;
102
4/4
✓ Branch 0 taken 15712130 times.
✓ Branch 1 taken 73888068 times.
✓ Branch 2 taken 1618136 times.
✓ Branch 3 taken 14093994 times.
89600198 if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
103
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 1618023 times.
1618136 if (t >= CLIPPED_ESCAPE) {
104 113 quantized = CLIPPED_ESCAPE;
105 113 curbits += 21;
106 } else {
107 1618023 int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
108 1618023 quantized = c*cbrtf(c)*IQ;
109 1618023 curbits += av_log2(c)*2 - 4 + 1;
110 }
111 } else {
112 87982062 quantized = vec[j]*IQ;
113 }
114 89600198 di = t - quantized;
115
2/2
✓ Branch 0 taken 55528 times.
✓ Branch 1 taken 89544670 times.
89600198 if (out)
116
2/2
✓ Branch 0 taken 27577 times.
✓ Branch 1 taken 27951 times.
55528 out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
117
2/2
✓ Branch 0 taken 51625338 times.
✓ Branch 1 taken 37974860 times.
89600198 if (vec[j] != 0.0f)
118 51625338 curbits++;
119 89600198 qenergy += quantized*quantized;
120 89600198 rd += di*di;
121 }
122 } else {
123
2/2
✓ Branch 0 taken 64925050 times.
✓ Branch 1 taken 22287737 times.
87212787 for (j = 0; j < dim; j++) {
124 64925050 quantized = vec[j]*IQ;
125 64925050 qenergy += quantized*quantized;
126
2/2
✓ Branch 0 taken 443504 times.
✓ Branch 1 taken 64481546 times.
64925050 if (out)
127 443504 out[i+j] = quantized;
128 64925050 rd += (in[i+j] - quantized)*(in[i+j] - quantized);
129 }
130 }
131 59793004 cost += rd * lambda + curbits;
132 59793004 resbits += curbits;
133
2/2
✓ Branch 0 taken 7400 times.
✓ Branch 1 taken 59785604 times.
59793004 if (cost >= uplim)
134 7400 return uplim;
135
2/2
✓ Branch 0 taken 3365767 times.
✓ Branch 1 taken 56419837 times.
59785604 if (pb) {
136 3365767 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
137
2/2
✓ Branch 0 taken 1758664 times.
✓ Branch 1 taken 1607103 times.
3365767 if (BT_UNSIGNED)
138
2/2
✓ Branch 0 taken 4658984 times.
✓ Branch 1 taken 1758664 times.
6417648 for (j = 0; j < dim; j++)
139
2/2
✓ Branch 0 taken 3229049 times.
✓ Branch 1 taken 1429935 times.
4658984 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
140 3229049 put_bits(pb, 1, in[i+j] < 0.0f);
141
2/2
✓ Branch 0 taken 454666 times.
✓ Branch 1 taken 2911101 times.
3365767 if (BT_ESC) {
142
2/2
✓ Branch 0 taken 909332 times.
✓ Branch 1 taken 454666 times.
1363998 for (j = 0; j < 2; j++) {
143
2/2
✓ Branch 0 taken 132912 times.
✓ Branch 1 taken 776420 times.
909332 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
144 132912 int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13);
145 132912 int len = av_log2(coef);
146
147 132912 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
148 132912 put_sbits(pb, len, coef);
149 }
150 }
151 }
152 }
153 }
154
155
2/2
✓ Branch 0 taken 6981773 times.
✓ Branch 1 taken 567892 times.
7549665 if (bits)
156 6981773 *bits = resbits;
157
2/2
✓ Branch 0 taken 3338013 times.
✓ Branch 1 taken 4211652 times.
7549665 if (energy)
158 3338013 *energy = qenergy;
159 7549665 return cost;
160 }
161
162 static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
163 const float *in, float *quant, const float *scaled,
164 int size, int scale_idx, int cb,
165 const float lambda, const float uplim,
166 int *bits, float *energy) {
167 av_assert0(0);
168 return 0.0f;
169 }
170
171 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
172 static float quantize_and_encode_band_cost_ ## NAME( \
173 struct AACEncContext *s, \
174 PutBitContext *pb, const float *in, float *quant, \
175 const float *scaled, int size, int scale_idx, \
176 int cb, const float lambda, const float uplim, \
177 int *bits, float *energy) { \
178 return quantize_and_encode_band_cost_template( \
179 s, pb, in, quant, scaled, size, scale_idx, \
180 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \
181 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
182 ROUNDING); \
183 }
184
185 1621716 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD)
186 1699236 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
187 1314687 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
188 1233974 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
189 2296227 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
190 1007828 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD)
191 5113 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
192 13300 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
193 17350 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD)
194
195 static float (*const quantize_and_encode_band_cost_arr[])(
196 struct AACEncContext *s,
197 PutBitContext *pb, const float *in, float *quant,
198 const float *scaled, int size, int scale_idx,
199 int cb, const float lambda, const float uplim,
200 int *bits, float *energy) = {
201 quantize_and_encode_band_cost_ZERO,
202 quantize_and_encode_band_cost_SQUAD,
203 quantize_and_encode_band_cost_SQUAD,
204 quantize_and_encode_band_cost_UQUAD,
205 quantize_and_encode_band_cost_UQUAD,
206 quantize_and_encode_band_cost_SPAIR,
207 quantize_and_encode_band_cost_SPAIR,
208 quantize_and_encode_band_cost_UPAIR,
209 quantize_and_encode_band_cost_UPAIR,
210 quantize_and_encode_band_cost_UPAIR,
211 quantize_and_encode_band_cost_UPAIR,
212 quantize_and_encode_band_cost_ESC,
213 quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
214 quantize_and_encode_band_cost_NOISE,
215 quantize_and_encode_band_cost_STEREO,
216 quantize_and_encode_band_cost_STEREO,
217 };
218
219 static float (*const quantize_and_encode_band_cost_rtz_arr[])(
220 struct AACEncContext *s,
221 PutBitContext *pb, const float *in, float *quant,
222 const float *scaled, int size, int scale_idx,
223 int cb, const float lambda, const float uplim,
224 int *bits, float *energy) = {
225 quantize_and_encode_band_cost_ZERO,
226 quantize_and_encode_band_cost_SQUAD,
227 quantize_and_encode_band_cost_SQUAD,
228 quantize_and_encode_band_cost_UQUAD,
229 quantize_and_encode_band_cost_UQUAD,
230 quantize_and_encode_band_cost_SPAIR,
231 quantize_and_encode_band_cost_SPAIR,
232 quantize_and_encode_band_cost_UPAIR,
233 quantize_and_encode_band_cost_UPAIR,
234 quantize_and_encode_band_cost_UPAIR,
235 quantize_and_encode_band_cost_UPAIR,
236 quantize_and_encode_band_cost_ESC_RTZ,
237 quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
238 quantize_and_encode_band_cost_NOISE,
239 quantize_and_encode_band_cost_STEREO,
240 quantize_and_encode_band_cost_STEREO,
241 };
242
243 #define quantize_and_encode_band_cost( \
244 s, pb, in, quant, scaled, size, scale_idx, cb, \
245 lambda, uplim, bits, energy, rtz) \
246 ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \
247 s, pb, in, quant, scaled, size, scale_idx, cb, \
248 lambda, uplim, bits, energy)
249
250 5039063 static inline float quantize_band_cost(struct AACEncContext *s, const float *in,
251 const float *scaled, int size, int scale_idx,
252 int cb, const float lambda, const float uplim,
253 int *bits, float *energy, int rtz)
254 {
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5039063 times.
5039063 return quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx,
256 cb, lambda, uplim, bits, energy, rtz);
257 }
258
259 3614834 static inline int quantize_band_cost_bits(struct AACEncContext *s, const float *in,
260 const float *scaled, int size, int scale_idx,
261 int cb, const float lambda, const float uplim,
262 int *bits, float *energy, int rtz)
263 {
264 int auxbits;
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3614834 times.
3614834 quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx,
266 cb, 0.0f, uplim, &auxbits, energy, rtz);
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3614834 times.
3614834 if (bits) {
268 *bits = auxbits;
269 }
270 3614834 return auxbits;
271 }
272
273 484380 static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
274 const float *in, float *out, int size, int scale_idx,
275 int cb, const float lambda, int rtz)
276 {
277
2/2
✓ Branch 0 taken 16224 times.
✓ Branch 1 taken 468156 times.
484380 quantize_and_encode_band_cost(s, pb, in, out, NULL, size, scale_idx, cb, lambda,
278 INFINITY, NULL, NULL, rtz);
279 484380 }
280
281 #include "aacenc_quantization_misc.h"
282
283 #endif /* AVCODEC_AACENC_QUANTIZATION_H */
284