History

avcodec/aacenc_quantization: Deduplicate quantization functions

Up until now, there were four copies of quantize_and_encode_band_cost_(ZERO|[SU]QUAD|[SU]PAIR|ESC|NONE|NOISE|STEREO) (namely in aaccoder.o, aacenc_is.o, aacenc_ltp.o, aacenc_pred.o). As 43b378a0d321e3d01f196cec95e13acfac80d464 says, this is meant to enable more optimizations. Yet neither GCC nor Clang performed such optimizations: The functions in the aforementioned files are not optimized according to the specifics of the calls in the respective file. Therefore duplicating them is a waste of space; this commit therefore stops doing so. The remaining copy is placed into aaccoder.c (which is the only place where the "round to zero" variant of quantize_and_encode_band() is used, so that this can be completely internal to aaccoder.c).

Andreas Rheinhardt committed 4 years ago

Commit 57d30521 parent e0813583 files · +244 −236Browse files

Showing the changes to libavcodec/aaccoder.c (1 of 3 files). Show all

Unified · Split

libavcodec/aaccoder.c+223−0
@@ -62,6 +62,229 @@ · 61 unmodified lines
6262
6363 #include "libavcodec/aaccoder_trellis.h"
6464
65+typedef float (*quantize_and_encode_band_func)(struct AACEncContext *s, PutBitContext *pb,
66+ const float *in, float *quant, const float *scaled,
67+ int size, int scale_idx, int cb,
68+ const float lambda, const float uplim,
69+ int *bits, float *energy);
70+
71+/**
72+ * Calculate rate distortion cost for quantizing with given codebook
73+ *
74+ * @return quantization distortion
75+ */
76+static av_always_inline float quantize_and_encode_band_cost_template(
77+ struct AACEncContext *s,
78+ PutBitContext *pb, const float *in, float *out,
79+ const float *scaled, int size, int scale_idx,
80+ int cb, const float lambda, const float uplim,
81+ int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED,
82+ int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO,
83+ const float ROUNDING)
84+{
85+ const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
86+ const float Q = ff_aac_pow2sf_tab [q_idx];
87+ const float Q34 = ff_aac_pow34sf_tab[q_idx];
88+ const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
89+ const float CLIPPED_ESCAPE = 165140.0f*IQ;
90+ float cost = 0;
91+ float qenergy = 0;
92+ const int dim = BT_PAIR ? 2 : 4;
93+ int resbits = 0;
94+ int off;
95+
96+ if (BT_ZERO || BT_NOISE || BT_STEREO) {
97+ for (int i = 0; i < size; i++)
98+ cost += in[i]*in[i];
99+ if (bits)
100+ *bits = 0;
101+ if (energy)
102+ *energy = qenergy;
103+ if (out) {
104+ for (int i = 0; i < size; i += dim)
105+ for (int j = 0; j < dim; j++)
106+ out[i+j] = 0.0f;
107+ }
108+ return cost * lambda;
109+ }
110+ if (!scaled) {
111+ s->abs_pow34(s->scoefs, in, size);
112+ scaled = s->scoefs;
113+ }
114+ s->quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING);
115+ if (BT_UNSIGNED) {
116+ off = 0;
117+ } else {
118+ off = aac_cb_maxval[cb];
119+ }
120+ for (int i = 0; i < size; i += dim) {
121+ const float *vec;
122+ int *quants = s->qcoefs + i;
123+ int curidx = 0;
124+ int curbits;
125+ float quantized, rd = 0.0f;
126+ for (int j = 0; j < dim; j++) {
127+ curidx *= aac_cb_range[cb];
128+ curidx += quants[j] + off;
129+ }
130+ curbits = ff_aac_spectral_bits[cb-1][curidx];
131+ vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
132+ if (BT_UNSIGNED) {
133+ for (int j = 0; j < dim; j++) {
134+ float t = fabsf(in[i+j]);
135+ float di;
136+ if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
137+ if (t >= CLIPPED_ESCAPE) {
138+ quantized = CLIPPED_ESCAPE;
139+ curbits += 21;
140+ } else {
141+ int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
142+ quantized = c*cbrtf(c)*IQ;
143+ curbits += av_log2(c)*2 - 4 + 1;
144+ }
145+ } else {
146+ quantized = vec[j]*IQ;
147+ }
148+ di = t - quantized;
149+ if (out)
150+ out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
151+ if (vec[j] != 0.0f)
152+ curbits++;
153+ qenergy += quantized*quantized;
154+ rd += di*di;
155+ }
156+ } else {
157+ for (int j = 0; j < dim; j++) {
158+ quantized = vec[j]*IQ;
159+ qenergy += quantized*quantized;
160+ if (out)
161+ out[i+j] = quantized;
162+ rd += (in[i+j] - quantized)*(in[i+j] - quantized);
163+ }
164+ }
165+ cost += rd * lambda + curbits;
166+ resbits += curbits;
167+ if (cost >= uplim)
168+ return uplim;
169+ if (pb) {
170+ put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
171+ if (BT_UNSIGNED)
172+ for (int j = 0; j < dim; j++)
173+ if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
174+ put_bits(pb, 1, in[i+j] < 0.0f);
175+ if (BT_ESC) {
176+ for (int j = 0; j < 2; j++) {
177+ if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
178+ int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13);
179+ int len = av_log2(coef);
180+
181+ put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
182+ put_sbits(pb, len, coef);
183+ }
184+ }
185+ }
186+ }
187+ }
188+
189+ if (bits)
190+ *bits = resbits;
191+ if (energy)
192+ *energy = qenergy;
193+ return cost;
194+}
195+
196+static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
197+ const float *in, float *quant, const float *scaled,
198+ int size, int scale_idx, int cb,
199+ const float lambda, const float uplim,
200+ int *bits, float *energy) {
201+ av_assert0(0);
202+ return 0.0f;
203+}
204+
205+#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
206+static float quantize_and_encode_band_cost_ ## NAME( \
207+ struct AACEncContext *s, \
208+ PutBitContext *pb, const float *in, float *quant, \
209+ const float *scaled, int size, int scale_idx, \
210+ int cb, const float lambda, const float uplim, \
211+ int *bits, float *energy) { \
212+ return quantize_and_encode_band_cost_template( \
213+ s, pb, in, quant, scaled, size, scale_idx, \
214+ BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \
215+ BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
216+ ROUNDING); \
217+}
218+
219+QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD)
220+QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
221+QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
222+QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
223+QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
224+QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD)
225+QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
226+QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
227+QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD)
228+
229+static quantize_and_encode_band_func quantize_and_encode_band_cost_arr[] =
230+{
231+ quantize_and_encode_band_cost_ZERO,
232+ quantize_and_encode_band_cost_SQUAD,
233+ quantize_and_encode_band_cost_SQUAD,
234+ quantize_and_encode_band_cost_UQUAD,
235+ quantize_and_encode_band_cost_UQUAD,
236+ quantize_and_encode_band_cost_SPAIR,
237+ quantize_and_encode_band_cost_SPAIR,
238+ quantize_and_encode_band_cost_UPAIR,
239+ quantize_and_encode_band_cost_UPAIR,
240+ quantize_and_encode_band_cost_UPAIR,
241+ quantize_and_encode_band_cost_UPAIR,
242+ quantize_and_encode_band_cost_ESC,
243+ quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
244+ quantize_and_encode_band_cost_NOISE,
245+ quantize_and_encode_band_cost_STEREO,
246+ quantize_and_encode_band_cost_STEREO,
247+};
248+
249+static quantize_and_encode_band_func quantize_and_encode_band_cost_rtz_arr[] =
250+{
251+ quantize_and_encode_band_cost_ZERO,
252+ quantize_and_encode_band_cost_SQUAD,
253+ quantize_and_encode_band_cost_SQUAD,
254+ quantize_and_encode_band_cost_UQUAD,
255+ quantize_and_encode_band_cost_UQUAD,
256+ quantize_and_encode_band_cost_SPAIR,
257+ quantize_and_encode_band_cost_SPAIR,
258+ quantize_and_encode_band_cost_UPAIR,
259+ quantize_and_encode_band_cost_UPAIR,
260+ quantize_and_encode_band_cost_UPAIR,
261+ quantize_and_encode_band_cost_UPAIR,
262+ quantize_and_encode_band_cost_ESC_RTZ,
263+ quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
264+ quantize_and_encode_band_cost_NOISE,
265+ quantize_and_encode_band_cost_STEREO,
266+ quantize_and_encode_band_cost_STEREO,
267+};
268+
269+float ff_quantize_and_encode_band_cost(struct AACEncContext *s, PutBitContext *pb,
270+ const float *in, float *quant, const float *scaled,
271+ int size, int scale_idx, int cb,
272+ const float lambda, const float uplim,
273+ int *bits, float *energy)
274+{
275+ return quantize_and_encode_band_cost_arr[cb](s, pb, in, quant, scaled, size,
276+ scale_idx, cb, lambda, uplim,
277+ bits, energy);
278+}
279+
280+static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
281+ const float *in, float *out, int size, int scale_idx,
282+ int cb, const float lambda, int rtz)
283+{
284+ (rtz ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb](s, pb, in, out, NULL, size, scale_idx, cb,
285+ lambda, INFINITY, NULL, NULL);
286+}
287+
65288 /**
66289 * structure used in optimal codebook search
67290 */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t