40
40
#include "aacenc.h"
41
41
#include "aactab.h"
42
#include "aacenctab.h"
43
#include "aacenc_utils.h"
44
#include "aacenc_quantization.h"
45
#include "aac_tablegen_decl.h"
47
#include "aacenc_is.h"
48
#include "aacenc_tns.h"
49
#include "aacenc_pred.h"
43
51
/** Frequency in Hz for lower limit of noise substitution **/
44
#define NOISE_LOW_LIMIT 4000
46
/** Total number of usable codebooks **/
49
/** bits needed to code codebook run value for long windows */
50
static const uint8_t run_value_bits_long[64] = {
51
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
52
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
53
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
54
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
57
/** bits needed to code codebook run value for short windows */
58
static const uint8_t run_value_bits_short[16] = {
59
3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
62
static const uint8_t * const run_value_bits[2] = {
63
run_value_bits_long, run_value_bits_short
66
/** Map to convert values from BandCodingPath index to a codebook index **/
67
static const uint8_t aac_cb_out_map[CB_TOT] = {0,1,2,3,4,5,6,7,8,9,10,11,13};
68
/** Inverse map to convert from codebooks to BandCodingPath indices **/
69
static const uint8_t aac_cb_in_map[CB_TOT+1] = {0,1,2,3,4,5,6,7,8,9,10,11,0,12};
72
* Quantize one coefficient.
73
* @return absolute value of the quantized coefficient
74
* @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
76
static av_always_inline int quant(float coef, const float Q)
79
return sqrtf(a * sqrtf(a)) + 0.4054;
82
static void quantize_bands(int *out, const float *in, const float *scaled,
83
int size, float Q34, int is_signed, int maxval)
87
for (i = 0; i < size; i++) {
89
out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
90
if (is_signed && in[i] < 0.0f) {
96
static void abs_pow34_v(float *out, const float *in, const int size)
98
#ifndef USE_REALLY_FULL_SEARCH
100
for (i = 0; i < size; i++) {
101
float a = fabsf(in[i]);
102
out[i] = sqrtf(a * sqrtf(a));
104
#endif /* USE_REALLY_FULL_SEARCH */
107
static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
108
static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
111
* Calculate rate distortion cost for quantizing with given codebook
113
* @return quantization distortion
115
static av_always_inline float quantize_and_encode_band_cost_template(
116
struct AACEncContext *s,
117
PutBitContext *pb, const float *in,
118
const float *scaled, int size, int scale_idx,
119
int cb, const float lambda, const float uplim,
120
int *bits, int BT_ZERO, int BT_UNSIGNED,
121
int BT_PAIR, int BT_ESC, int BT_NOISE)
123
const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
124
const float Q = ff_aac_pow2sf_tab [q_idx];
125
const float Q34 = ff_aac_pow34sf_tab[q_idx];
126
const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
127
const float CLIPPED_ESCAPE = 165140.0f*IQ;
130
const int dim = BT_PAIR ? 2 : 4;
135
for (i = 0; i < size; i++)
139
return cost * lambda;
142
for (i = 0; i < size; i++)
146
return cost * lambda;
149
abs_pow34_v(s->scoefs, in, size);
152
quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, aac_cb_maxval[cb]);
156
off = aac_cb_maxval[cb];
158
for (i = 0; i < size; i += dim) {
160
int *quants = s->qcoefs + i;
164
for (j = 0; j < dim; j++) {
165
curidx *= aac_cb_range[cb];
166
curidx += quants[j] + off;
168
curbits = ff_aac_spectral_bits[cb-1][curidx];
169
vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
171
for (j = 0; j < dim; j++) {
172
float t = fabsf(in[i+j]);
174
if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
175
if (t >= CLIPPED_ESCAPE) {
176
di = t - CLIPPED_ESCAPE;
179
int c = av_clip_uintp2(quant(t, Q), 13);
180
di = t - c*cbrtf(c)*IQ;
181
curbits += av_log2(c)*2 - 4 + 1;
191
for (j = 0; j < dim; j++) {
192
float di = in[i+j] - vec[j]*IQ;
196
cost += rd * lambda + curbits;
201
put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
203
for (j = 0; j < dim; j++)
204
if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
205
put_bits(pb, 1, in[i+j] < 0.0f);
207
for (j = 0; j < 2; j++) {
208
if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
209
int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q), 13);
210
int len = av_log2(coef);
212
put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
213
put_sbits(pb, len, coef);
225
static float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
226
const float *in, const float *scaled,
227
int size, int scale_idx, int cb,
228
const float lambda, const float uplim,
234
#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE) \
235
static float quantize_and_encode_band_cost_ ## NAME( \
236
struct AACEncContext *s, \
237
PutBitContext *pb, const float *in, \
238
const float *scaled, int size, int scale_idx, \
239
int cb, const float lambda, const float uplim, \
241
return quantize_and_encode_band_cost_template( \
242
s, pb, in, scaled, size, scale_idx, \
243
BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
244
BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE); \
247
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0)
248
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0)
249
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0)
250
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0)
251
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0)
252
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0)
253
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1)
255
static float (*const quantize_and_encode_band_cost_arr[])(
256
struct AACEncContext *s,
257
PutBitContext *pb, const float *in,
258
const float *scaled, int size, int scale_idx,
259
int cb, const float lambda, const float uplim,
261
quantize_and_encode_band_cost_ZERO,
262
quantize_and_encode_band_cost_SQUAD,
263
quantize_and_encode_band_cost_SQUAD,
264
quantize_and_encode_band_cost_UQUAD,
265
quantize_and_encode_band_cost_UQUAD,
266
quantize_and_encode_band_cost_SPAIR,
267
quantize_and_encode_band_cost_SPAIR,
268
quantize_and_encode_band_cost_UPAIR,
269
quantize_and_encode_band_cost_UPAIR,
270
quantize_and_encode_band_cost_UPAIR,
271
quantize_and_encode_band_cost_UPAIR,
272
quantize_and_encode_band_cost_ESC,
273
quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
274
quantize_and_encode_band_cost_NOISE,
277
#define quantize_and_encode_band_cost( \
278
s, pb, in, scaled, size, scale_idx, cb, \
279
lambda, uplim, bits) \
280
quantize_and_encode_band_cost_arr[cb]( \
281
s, pb, in, scaled, size, scale_idx, cb, \
284
static float quantize_band_cost(struct AACEncContext *s, const float *in,
285
const float *scaled, int size, int scale_idx,
286
int cb, const float lambda, const float uplim,
289
return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
290
cb, lambda, uplim, bits);
293
static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
294
const float *in, int size, int scale_idx,
295
int cb, const float lambda)
297
quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
301
static float find_max_val(int group_len, int swb_size, const float *scaled) {
304
for (w2 = 0; w2 < group_len; w2++) {
305
for (i = 0; i < swb_size; i++) {
306
maxval = FFMAX(maxval, scaled[w2*128+i]);
312
static int find_min_book(float maxval, int sf) {
313
float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
314
float Q34 = sqrtf(Q * sqrtf(Q));
316
qmaxval = maxval * Q34 + 0.4054f;
317
if (qmaxval == 0) cb = 0;
318
else if (qmaxval == 1) cb = 1;
319
else if (qmaxval == 2) cb = 3;
320
else if (qmaxval <= 4) cb = 5;
321
else if (qmaxval <= 7) cb = 7;
322
else if (qmaxval <= 12) cb = 9;
52
#define NOISE_LOW_LIMIT 4500
54
/* Energy spread threshold value below which no PNS is used, this corresponds to
55
* typically around 17Khz, after which PNS usage decays ending at 19Khz */
56
#define NOISE_SPREAD_THRESHOLD 0.5f
58
/* This constant gets divided by lambda to return ~1.65 which when multiplied
59
* by the band->threshold and compared to band->energy is the boundary between
60
* excessive PNS and little PNS usage. */
61
#define NOISE_LAMBDA_NUMERATOR 252.1f
328
64
* structure used in optimal codebook search
1131
914
float minthr = FFMIN(band0->threshold, band1->threshold);
1132
915
float maxthr = FFMAX(band0->threshold, band1->threshold);
1133
916
for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1134
M[i] = (sce0->pcoeffs[start+w2*128+i]
1135
+ sce1->pcoeffs[start+w2*128+i]) * 0.5;
917
M[i] = (sce0->coeffs[start+(w+w2)*128+i]
918
+ sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
1137
- sce1->pcoeffs[start+w2*128+i];
920
- sce1->coeffs[start+(w+w2)*128+i];
1139
abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1140
abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
922
abs_pow34_v(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
923
abs_pow34_v(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
1141
924
abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
1142
925
abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
1143
dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
926
dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
1145
928
sce0->ics.swb_sizes[g],
1146
929
sce0->sf_idx[(w+w2)*16+g],
1147
930
sce0->band_type[(w+w2)*16+g],
1148
lambda / band0->threshold, INFINITY, NULL);
1149
dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
931
lambda / band0->threshold, INFINITY, NULL, 0);
932
dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
1151
934
sce1->ics.swb_sizes[g],
1152
935
sce1->sf_idx[(w+w2)*16+g],
1153
936
sce1->band_type[(w+w2)*16+g],
1154
lambda / band1->threshold, INFINITY, NULL);
937
lambda / band1->threshold, INFINITY, NULL, 0);
1155
938
dist2 += quantize_band_cost(s, M,
1157
940
sce0->ics.swb_sizes[g],
1158
941
sce0->sf_idx[(w+w2)*16+g],
1159
942
sce0->band_type[(w+w2)*16+g],
1160
lambda / maxthr, INFINITY, NULL);
943
lambda / maxthr, INFINITY, NULL, 0);
1161
944
dist2 += quantize_band_cost(s, S,
1163
946
sce1->ics.swb_sizes[g],
1164
947
sce1->sf_idx[(w+w2)*16+g],
1165
948
sce1->band_type[(w+w2)*16+g],
1166
lambda / minthr, INFINITY, NULL);
949
lambda / minthr, INFINITY, NULL, 0);
1168
951
cpe->ms_mask[w*16+g] = dist2 < dist1;