History

aaccoder: move the quantization functions to a separate file

This commit moves the quantizer to a separate header file. This allows the quantizer to be used from a separate files outside of aaccoder without having to put another function pointer and will result in a slight speedup as the compiler can do more optimizations. This is required for commits following.

Rostislav Pehlivanov committed 11 years ago

Commit 43b378a1 parent b47a1e55 files · +263 −220Browse files

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

Unified · Split

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

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