FFmpeg
aaccoder.c
Go to the documentation of this file.
1 /*
2  * AAC coefficients encoder
3  * Copyright (C) 2008-2009 Konstantin Shishkov
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 coefficients encoder
25  */
26 
27 /***********************************
28  * TODOs:
29  * speedup quantizer selection
30  * add sane pulse detection
31  ***********************************/
32 
33 #include "libavutil/libm.h" // brought forward to work around cygwin header breakage
34 
35 #include <float.h>
36 
37 #include "libavutil/mathematics.h"
38 #include "mathops.h"
39 #include "avcodec.h"
40 #include "put_bits.h"
41 #include "aac.h"
42 #include "aacenc.h"
43 #include "aactab.h"
44 #include "aacenctab.h"
45 #include "aacenc_utils.h"
46 #include "aacenc_quantization.h"
47 
48 #include "aacenc_is.h"
49 #include "aacenc_tns.h"
50 
52 
53 /* Parameter of f(x) = a*(lambda/100), defines the maximum fourier spread
54  * beyond which no PNS is used (since the SFBs contain tone rather than noise) */
55 #define NOISE_SPREAD_THRESHOLD 0.9f
56 
57 /* Parameter of f(x) = a*(100/lambda), defines how much PNS is allowed to
58  * replace low energy non zero bands */
59 #define NOISE_LAMBDA_REPLACE 1.948f
60 
63 
65  const float *in, float *quant, const float *scaled,
66  int size, int scale_idx, int cb,
67  const float lambda, const float uplim,
68  int *bits, float *energy);
69 
70 /**
71  * Calculate rate distortion cost for quantizing with given codebook
72  *
73  * @return quantization distortion
74  */
76  struct AACEncContext *s,
77  PutBitContext *pb, const float *in, float *out,
78  const float *scaled, int size, int scale_idx,
79  int cb, const float lambda, const float uplim,
80  int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED,
81  int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO,
82  const float ROUNDING)
83 {
84  const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
85  const float Q = ff_aac_pow2sf_tab [q_idx];
86  const float Q34 = ff_aac_pow34sf_tab[q_idx];
87  const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
88  const float CLIPPED_ESCAPE = 165140.0f*IQ;
89  float cost = 0;
90  float qenergy = 0;
91  const int dim = BT_PAIR ? 2 : 4;
92  int resbits = 0;
93  int off;
94 
95  if (BT_ZERO || BT_NOISE || BT_STEREO) {
96  for (int i = 0; i < size; i++)
97  cost += in[i]*in[i];
98  if (bits)
99  *bits = 0;
100  if (energy)
101  *energy = qenergy;
102  if (out) {
103  for (int i = 0; i < size; i += dim)
104  for (int j = 0; j < dim; j++)
105  out[i+j] = 0.0f;
106  }
107  return cost * lambda;
108  }
109  if (!scaled) {
110  s->aacdsp.abs_pow34(s->scoefs, in, size);
111  scaled = s->scoefs;
112  }
113  s->aacdsp.quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING);
114  if (BT_UNSIGNED) {
115  off = 0;
116  } else {
117  off = aac_cb_maxval[cb];
118  }
119  for (int i = 0; i < size; i += dim) {
120  const float *vec;
121  int *quants = s->qcoefs + i;
122  int curidx = 0;
123  int curbits;
124  float quantized, rd = 0.0f;
125  for (int j = 0; j < dim; j++) {
126  curidx *= aac_cb_range[cb];
127  curidx += quants[j] + off;
128  }
129  curbits = ff_aac_spectral_bits[cb-1][curidx];
130  vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
131  if (BT_UNSIGNED) {
132  for (int j = 0; j < dim; j++) {
133  float t = fabsf(in[i+j]);
134  float di;
135  if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
136  if (t >= CLIPPED_ESCAPE) {
137  quantized = CLIPPED_ESCAPE;
138  curbits += 21;
139  } else {
140  int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
141  quantized = c*cbrtf(c)*IQ;
142  curbits += av_log2(c)*2 - 4 + 1;
143  }
144  } else {
145  quantized = vec[j]*IQ;
146  }
147  di = t - quantized;
148  if (out)
149  out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
150  if (vec[j] != 0.0f)
151  curbits++;
152  qenergy += quantized*quantized;
153  rd += di*di;
154  }
155  } else {
156  for (int j = 0; j < dim; j++) {
157  quantized = vec[j]*IQ;
158  qenergy += quantized*quantized;
159  if (out)
160  out[i+j] = quantized;
161  rd += (in[i+j] - quantized)*(in[i+j] - quantized);
162  }
163  }
164  cost += rd * lambda + curbits;
165  resbits += curbits;
166  if (cost >= uplim)
167  return uplim;
168  if (pb) {
169  put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
170  if (BT_UNSIGNED)
171  for (int j = 0; j < dim; j++)
172  if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
173  put_bits(pb, 1, in[i+j] < 0.0f);
174  if (BT_ESC) {
175  for (int j = 0; j < 2; j++) {
176  if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
177  int coef = av_clip(quant(fabsf(in[i+j]), Q, ROUNDING), 16, (1 << 13) - 1);
178  int len = av_log2(coef);
179 
180  put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
181  put_sbits(pb, len, coef);
182  }
183  }
184  }
185  }
186  }
187 
188  if (bits)
189  *bits = resbits;
190  if (energy)
191  *energy = qenergy;
192  return cost;
193 }
194 
196  const float *in, float *quant, const float *scaled,
197  int size, int scale_idx, int cb,
198  const float lambda, const float uplim,
199  int *bits, float *energy) {
200  av_assert0(0);
201  return 0.0f;
202 }
203 
204 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
205 static float quantize_and_encode_band_cost_ ## NAME( \
206  struct AACEncContext *s, \
207  PutBitContext *pb, const float *in, float *quant, \
208  const float *scaled, int size, int scale_idx, \
209  int cb, const float lambda, const float uplim, \
210  int *bits, float *energy) { \
211  return quantize_and_encode_band_cost_template( \
212  s, pb, in, quant, scaled, size, scale_idx, \
213  BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \
214  BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
215  ROUNDING); \
216 }
217 
219 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
220 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
221 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
222 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
224 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
225 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
227 
229 {
230  quantize_and_encode_band_cost_ZERO,
231  quantize_and_encode_band_cost_SQUAD,
232  quantize_and_encode_band_cost_SQUAD,
233  quantize_and_encode_band_cost_UQUAD,
234  quantize_and_encode_band_cost_UQUAD,
235  quantize_and_encode_band_cost_SPAIR,
236  quantize_and_encode_band_cost_SPAIR,
237  quantize_and_encode_band_cost_UPAIR,
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_ESC,
242  quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
243  quantize_and_encode_band_cost_NOISE,
244  quantize_and_encode_band_cost_STEREO,
245  quantize_and_encode_band_cost_STEREO,
246 };
247 
249 {
250  quantize_and_encode_band_cost_ZERO,
251  quantize_and_encode_band_cost_SQUAD,
252  quantize_and_encode_band_cost_SQUAD,
253  quantize_and_encode_band_cost_UQUAD,
254  quantize_and_encode_band_cost_UQUAD,
255  quantize_and_encode_band_cost_SPAIR,
256  quantize_and_encode_band_cost_SPAIR,
257  quantize_and_encode_band_cost_UPAIR,
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_ESC_RTZ,
262  quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
263  quantize_and_encode_band_cost_NOISE,
264  quantize_and_encode_band_cost_STEREO,
265  quantize_and_encode_band_cost_STEREO,
266 };
267 
269  const float *in, float *quant, const float *scaled,
270  int size, int scale_idx, int cb,
271  const float lambda, const float uplim,
272  int *bits, float *energy)
273 {
274  return quantize_and_encode_band_cost_arr[cb](s, pb, in, quant, scaled, size,
275  scale_idx, cb, lambda, uplim,
276  bits, energy);
277 }
278 
280  const float *in, float *out, int size, int scale_idx,
281  int cb, const float lambda, int rtz)
282 {
284  lambda, INFINITY, NULL, NULL);
285 }
286 
287 /**
288  * structure used in optimal codebook search
289  */
290 typedef struct BandCodingPath {
291  int prev_idx; ///< pointer to the previous path point
292  float cost; ///< path cost
293  int run;
295 
296 typedef struct TrellisPath {
297  float cost;
298  int prev;
299 } TrellisPath;
300 
301 #define TRELLIS_STAGES 121
302 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
303 
305 {
306  int w, g;
307  int prevscaler_n = -255, prevscaler_i = 0;
308  int bands = 0;
309 
310  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
311  for (g = 0; g < sce->ics.num_swb; g++) {
312  if (sce->zeroes[w*16+g])
313  continue;
314  if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
315  sce->sf_idx[w*16+g] = av_clip(roundf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
316  bands++;
317  } else if (sce->band_type[w*16+g] == NOISE_BT) {
318  sce->sf_idx[w*16+g] = av_clip(3+ceilf(log2f(sce->pns_ener[w*16+g])*2), -100, 155);
319  if (prevscaler_n == -255)
320  prevscaler_n = sce->sf_idx[w*16+g];
321  bands++;
322  }
323  }
324  }
325 
326  if (!bands)
327  return;
328 
329  /* Clip the scalefactor indices */
330  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
331  for (g = 0; g < sce->ics.num_swb; g++) {
332  if (sce->zeroes[w*16+g])
333  continue;
334  if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
335  sce->sf_idx[w*16+g] = prevscaler_i = av_clip(sce->sf_idx[w*16+g], prevscaler_i - SCALE_MAX_DIFF, prevscaler_i + SCALE_MAX_DIFF);
336  } else if (sce->band_type[w*16+g] == NOISE_BT) {
337  sce->sf_idx[w*16+g] = prevscaler_n = av_clip(sce->sf_idx[w*16+g], prevscaler_n - SCALE_MAX_DIFF, prevscaler_n + SCALE_MAX_DIFF);
338  }
339  }
340  }
341 }
342 
345  const float lambda)
346 {
347  int start = 0, i, w, w2, g;
348  int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->ch_layout.nb_channels * (lambda / 120.f);
349  float dists[128] = { 0 }, uplims[128] = { 0 };
350  float maxvals[128];
351  int fflag, minscaler;
352  int its = 0;
353  int allz = 0;
354  float minthr = INFINITY;
355 
356  // for values above this the decoder might end up in an endless loop
357  // due to always having more bits than what can be encoded.
358  destbits = FFMIN(destbits, 5800);
359  //some heuristic to determine initial quantizers will reduce search time
360  //determine zero bands and upper limits
361  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
362  start = 0;
363  for (g = 0; g < sce->ics.num_swb; g++) {
364  int nz = 0;
365  float uplim = 0.0f;
366  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
367  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
368  uplim += band->threshold;
369  if (band->energy <= band->threshold || band->threshold == 0.0f) {
370  sce->zeroes[(w+w2)*16+g] = 1;
371  continue;
372  }
373  nz = 1;
374  }
375  uplims[w*16+g] = uplim *512;
376  sce->band_type[w*16+g] = 0;
377  sce->zeroes[w*16+g] = !nz;
378  if (nz)
379  minthr = FFMIN(minthr, uplim);
380  allz |= nz;
381  start += sce->ics.swb_sizes[g];
382  }
383  }
384  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
385  for (g = 0; g < sce->ics.num_swb; g++) {
386  if (sce->zeroes[w*16+g]) {
387  sce->sf_idx[w*16+g] = SCALE_ONE_POS;
388  continue;
389  }
390  sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
391  }
392  }
393 
394  if (!allz)
395  return;
396  s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
398 
399  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
400  start = w*128;
401  for (g = 0; g < sce->ics.num_swb; g++) {
402  const float *scaled = s->scoefs + start;
403  maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
404  start += sce->ics.swb_sizes[g];
405  }
406  }
407 
408  //perform two-loop search
409  //outer loop - improve quality
410  do {
411  int tbits, qstep;
412  minscaler = sce->sf_idx[0];
413  //inner loop - quantize spectrum to fit into given number of bits
414  qstep = its ? 1 : 32;
415  do {
416  int prev = -1;
417  tbits = 0;
418  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
419  start = w*128;
420  for (g = 0; g < sce->ics.num_swb; g++) {
421  const float *coefs = sce->coeffs + start;
422  const float *scaled = s->scoefs + start;
423  int bits = 0;
424  int cb;
425  float dist = 0.0f;
426 
427  if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
428  start += sce->ics.swb_sizes[g];
429  continue;
430  }
431  minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
432  cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
433  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
434  int b;
435  dist += quantize_band_cost_cached(s, w + w2, g,
436  coefs + w2*128,
437  scaled + w2*128,
438  sce->ics.swb_sizes[g],
439  sce->sf_idx[w*16+g],
440  cb, 1.0f, INFINITY,
441  &b, NULL, 0);
442  bits += b;
443  }
444  dists[w*16+g] = dist - bits;
445  if (prev != -1) {
446  bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
447  }
448  tbits += bits;
449  start += sce->ics.swb_sizes[g];
450  prev = sce->sf_idx[w*16+g];
451  }
452  }
453  if (tbits > destbits) {
454  for (i = 0; i < 128; i++)
455  if (sce->sf_idx[i] < 218 - qstep)
456  sce->sf_idx[i] += qstep;
457  } else {
458  for (i = 0; i < 128; i++)
459  if (sce->sf_idx[i] > 60 - qstep)
460  sce->sf_idx[i] -= qstep;
461  }
462  qstep >>= 1;
463  if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
464  qstep = 1;
465  } while (qstep);
466 
467  fflag = 0;
468  minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
469 
470  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
471  for (g = 0; g < sce->ics.num_swb; g++) {
472  int prevsc = sce->sf_idx[w*16+g];
473  if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
474  if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
475  sce->sf_idx[w*16+g]--;
476  else //Try to make sure there is some energy in every band
477  sce->sf_idx[w*16+g]-=2;
478  }
479  sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
480  sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
481  if (sce->sf_idx[w*16+g] != prevsc)
482  fflag = 1;
483  sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
484  }
485  }
486  its++;
487  } while (fflag && its < 10);
488 }
489 
491 {
492  FFPsyBand *band;
493  int w, g, w2, i;
494  int wlen = 1024 / sce->ics.num_windows;
495  int bandwidth, cutoff;
496  float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128];
497  float *NOR34 = &s->scoefs[3*128];
498  uint8_t nextband[128];
499  const float lambda = s->lambda;
500  const float freq_mult = avctx->sample_rate*0.5f/wlen;
501  const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda);
502  const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
503  const float dist_bias = av_clipf(4.f * 120 / lambda, 0.25f, 4.0f);
504  const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
505 
506  int prev = -1000, prev_sf = -1;
507 
508  /* PNS candidacy must use the coder's actual coding bandwidth (s->bandwidth,
509  * fixed at init), not a separate heuristic, or it evaluates a different band
510  * range than the coder later codes. */
511  bandwidth = s->bandwidth;
512  cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
513 
514  memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
515  ff_init_nextband_map(sce, nextband);
516  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
517  int wstart = w*128;
518  for (g = 0; g < sce->ics.num_swb; g++) {
519  int noise_sfi;
520  float dist1 = 0.0f, dist2 = 0.0f, noise_amp;
521  float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh;
522  float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
523  float min_energy = -1.0f, max_energy = 0.0f;
524  const int start = wstart+sce->ics.swb_offset[g];
525  const float freq = (start-wstart)*freq_mult;
526  const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
527  if (freq < NOISE_LOW_LIMIT || (start-wstart) >= cutoff) {
528  if (!sce->zeroes[w*16+g])
529  prev_sf = sce->sf_idx[w*16+g];
530  continue;
531  }
532  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
533  band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
534  sfb_energy += band->energy;
535  spread = FFMIN(spread, band->spread);
536  threshold += band->threshold;
537  if (!w2) {
538  min_energy = max_energy = band->energy;
539  } else {
540  min_energy = FFMIN(min_energy, band->energy);
541  max_energy = FFMAX(max_energy, band->energy);
542  }
543  }
544 
545  /* Ramps down at ~8000Hz and loosens the dist threshold */
546  dist_thresh = av_clipf(2.5f*NOISE_LOW_LIMIT/freq, 0.5f, 2.5f) * dist_bias;
547 
548  /* PNS is acceptable when all of these are true:
549  * 1. high spread energy (noise-like band)
550  * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
551  * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
552  *
553  * At this stage, point 2 is relaxed for zeroed bands near the noise threshold (hole avoidance is more important)
554  */
555  if ((!sce->zeroes[w*16+g] && !ff_sfdelta_can_remove_band(sce, nextband, prev_sf, w*16+g)) ||
556  ((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.0f/freq_boost)) || spread < spread_threshold ||
557  (!sce->zeroes[w*16+g] && sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost) ||
558  min_energy < pns_transient_energy_r * max_energy ) {
559  sce->pns_ener[w*16+g] = sfb_energy;
560  if (!sce->zeroes[w*16+g])
561  prev_sf = sce->sf_idx[w*16+g];
562  continue;
563  }
564 
565  pns_tgt_energy = sfb_energy*FFMIN(1.0f, spread*spread);
566  noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */
567  noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO]; /* Dequantize */
568  if (prev != -1000) {
569  int noise_sfdiff = noise_sfi - prev + SCALE_DIFF_ZERO;
570  if (noise_sfdiff < 0 || noise_sfdiff > 2*SCALE_MAX_DIFF) {
571  if (!sce->zeroes[w*16+g])
572  prev_sf = sce->sf_idx[w*16+g];
573  continue;
574  }
575  }
576  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
577  float band_energy, scale, pns_senergy;
578  const int start_c = (w+w2)*128+sce->ics.swb_offset[g];
579  band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
580  for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
581  s->random_state = lcg_random(s->random_state);
582  PNS[i] = s->random_state;
583  }
584  band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
585  scale = noise_amp/sqrtf(band_energy);
586  s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]);
587  pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
588  pns_energy += pns_senergy;
589  s->aacdsp.abs_pow34(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]);
590  s->aacdsp.abs_pow34(PNS34, PNS, sce->ics.swb_sizes[g]);
591  dist1 += quantize_band_cost(s, &sce->coeffs[start_c],
592  NOR34,
593  sce->ics.swb_sizes[g],
594  sce->sf_idx[(w+w2)*16+g],
595  sce->band_alt[(w+w2)*16+g],
596  lambda/band->threshold, INFINITY, NULL, NULL);
597  /* Estimate rd on average as 5 bits for SF, 4 for the CB, plus spread energy * lambda/thr */
598  dist2 += band->energy/(band->spread*band->spread)*lambda*dist_thresh/band->threshold;
599  }
600  if (g && sce->band_type[w*16+g-1] == NOISE_BT) {
601  dist2 += 5;
602  } else {
603  dist2 += 9;
604  }
605  energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */
606  sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy;
607  if (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) {
608  sce->band_type[w*16+g] = NOISE_BT;
609  sce->zeroes[w*16+g] = 0;
610  prev = noise_sfi;
611  } else {
612  if (!sce->zeroes[w*16+g])
613  prev_sf = sce->sf_idx[w*16+g];
614  }
615  }
616  }
617 }
618 
620 {
621  FFPsyBand *band;
622  int w, g, w2;
623  int wlen = 1024 / sce->ics.num_windows;
624  int bandwidth, cutoff;
625  const float lambda = s->lambda;
626  const float freq_mult = avctx->sample_rate*0.5f/wlen;
627  const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
628  const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
629 
630  /* PNS candidacy must use the coder's actual coding bandwidth (s->bandwidth,
631  * fixed at init), not a separate heuristic, or it evaluates a different band
632  * range than the coder later codes (NMR relies on this output directly). */
633  bandwidth = s->bandwidth;
634  cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
635 
636  memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
637  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
638  for (g = 0; g < sce->ics.num_swb; g++) {
639  float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
640  float min_energy = -1.0f, max_energy = 0.0f;
641  const int start = sce->ics.swb_offset[g];
642  const float freq = start*freq_mult;
643  const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
644  if (freq < NOISE_LOW_LIMIT || start >= cutoff) {
645  sce->can_pns[w*16+g] = 0;
646  continue;
647  }
648  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
649  band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
650  sfb_energy += band->energy;
651  spread = FFMIN(spread, band->spread);
652  threshold += band->threshold;
653  if (!w2) {
654  min_energy = max_energy = band->energy;
655  } else {
656  min_energy = FFMIN(min_energy, band->energy);
657  max_energy = FFMAX(max_energy, band->energy);
658  }
659  }
660 
661  /* PNS is acceptable when all of these are true:
662  * 1. high spread energy (noise-like band)
663  * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
664  * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
665  */
666  sce->pns_ener[w*16+g] = sfb_energy;
667  if (sfb_energy < threshold*sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) {
668  sce->can_pns[w*16+g] = 0;
669  } else {
670  sce->can_pns[w*16+g] = 1;
671  }
672  }
673  }
674 }
675 
677 {
678  int start = 0, i, w, w2, g, sid_sf_boost, prev_mid, prev_side;
679  uint8_t nextband0[128], nextband1[128];
680  float *M = s->scoefs + 128*0, *S = s->scoefs + 128*1;
681  float *L34 = s->scoefs + 128*2, *R34 = s->scoefs + 128*3;
682  float *M34 = s->scoefs + 128*4, *S34 = s->scoefs + 128*5;
683  const float lambda = s->lambda;
684  const float mslambda = FFMIN(1.0f, lambda / 120.f);
685  SingleChannelElement *sce0 = &cpe->ch[0];
686  SingleChannelElement *sce1 = &cpe->ch[1];
687  if (!cpe->common_window)
688  return;
689 
690  /** Scout out next nonzero bands */
691  ff_init_nextband_map(sce0, nextband0);
692  ff_init_nextband_map(sce1, nextband1);
693 
694  prev_mid = sce0->sf_idx[0];
695  prev_side = sce1->sf_idx[0];
696  for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
697  start = 0;
698  for (g = 0; g < sce0->ics.num_swb; g++) {
699  float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f;
700  if (!cpe->is_mask[w*16+g])
701  cpe->ms_mask[w*16+g] = 0;
702  if (!sce0->zeroes[w*16+g] && !sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g]) {
703  float Mmax = 0.0f, Smax = 0.0f;
704 
705  /* Must compute mid/side SF and book for the whole window group */
706  for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
707  for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
708  M[i] = (sce0->coeffs[start+(w+w2)*128+i]
709  + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
710  S[i] = M[i]
711  - sce1->coeffs[start+(w+w2)*128+i];
712  }
713  s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]);
714  s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]);
715  for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) {
716  Mmax = FFMAX(Mmax, M34[i]);
717  Smax = FFMAX(Smax, S34[i]);
718  }
719  }
720 
721  for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) {
722  float dist1 = 0.0f, dist2 = 0.0f;
723  int B0 = 0, B1 = 0;
724  int minidx;
725  int mididx, sididx;
726  int midcb, sidcb;
727 
728  minidx = FFMIN(sce0->sf_idx[w*16+g], sce1->sf_idx[w*16+g]);
729  mididx = av_clip(minidx, 0, SCALE_MAX_POS - SCALE_DIV_512);
730  sididx = av_clip(minidx - sid_sf_boost * 3, 0, SCALE_MAX_POS - SCALE_DIV_512);
731  if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT
732  && ( !ff_sfdelta_can_replace(sce0, nextband0, prev_mid, mididx, w*16+g)
733  || !ff_sfdelta_can_replace(sce1, nextband1, prev_side, sididx, w*16+g))) {
734  /* scalefactor range violation, bad stuff, will decrease quality unacceptably */
735  continue;
736  }
737 
738  midcb = find_min_book(Mmax, mididx);
739  sidcb = find_min_book(Smax, sididx);
740 
741  /* No CB can be zero */
742  midcb = FFMAX(1,midcb);
743  sidcb = FFMAX(1,sidcb);
744 
745  for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
746  FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
747  FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
748  float minthr = FFMIN(band0->threshold, band1->threshold);
749  int b1,b2,b3,b4;
750  for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
751  M[i] = (sce0->coeffs[start+(w+w2)*128+i]
752  + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
753  S[i] = M[i]
754  - sce1->coeffs[start+(w+w2)*128+i];
755  }
756 
757  s->aacdsp.abs_pow34(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
758  s->aacdsp.abs_pow34(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
759  s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]);
760  s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]);
761  dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
762  L34,
763  sce0->ics.swb_sizes[g],
764  sce0->sf_idx[w*16+g],
765  sce0->band_type[w*16+g],
766  lambda / (band0->threshold + FLT_MIN), INFINITY, &b1, NULL);
767  dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
768  R34,
769  sce1->ics.swb_sizes[g],
770  sce1->sf_idx[w*16+g],
771  sce1->band_type[w*16+g],
772  lambda / (band1->threshold + FLT_MIN), INFINITY, &b2, NULL);
773  dist2 += quantize_band_cost(s, M,
774  M34,
775  sce0->ics.swb_sizes[g],
776  mididx,
777  midcb,
778  lambda / (minthr + FLT_MIN), INFINITY, &b3, NULL);
779  dist2 += quantize_band_cost(s, S,
780  S34,
781  sce1->ics.swb_sizes[g],
782  sididx,
783  sidcb,
784  mslambda / (minthr * bmax + FLT_MIN), INFINITY, &b4, NULL);
785  B0 += b1+b2;
786  B1 += b3+b4;
787  dist1 -= b1+b2;
788  dist2 -= b3+b4;
789  }
790  cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0;
791  if (cpe->ms_mask[w*16+g]) {
792  if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT) {
793  sce0->sf_idx[w*16+g] = mididx;
794  sce1->sf_idx[w*16+g] = sididx;
795  sce0->band_type[w*16+g] = midcb;
796  sce1->band_type[w*16+g] = sidcb;
797  } else if ((sce0->band_type[w*16+g] != NOISE_BT) ^ (sce1->band_type[w*16+g] != NOISE_BT)) {
798  /* ms_mask unneeded, and it confuses some decoders */
799  cpe->ms_mask[w*16+g] = 0;
800  }
801  break;
802  } else if (B1 > B0) {
803  /* More boost won't fix this */
804  break;
805  }
806  }
807  }
808  if (!sce0->zeroes[w*16+g] && sce0->band_type[w*16+g] < RESERVED_BT)
809  prev_mid = sce0->sf_idx[w*16+g];
810  if (!sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g] && sce1->band_type[w*16+g] < RESERVED_BT)
811  prev_side = sce1->sf_idx[w*16+g];
812  start += sce0->ics.swb_sizes[g];
813  }
814  }
815 }
816 
818  [AAC_CODER_TWOLOOP] = {
826  mark_pns,
830  },
831  [AAC_CODER_FAST] = {
839  mark_pns,
843  },
844  [AAC_CODER_NMR] = {
851  NULL, /* PNS decided in the trellis (search_for_quantizers_nmr) */
852  mark_pns,
854  NULL,
855  NULL,
856  },
857 };
SingleChannelElement::band_alt
enum BandType band_alt[128]
alternative band type
Definition: aacenc.h:114
INFINITY
#define INFINITY
Definition: mathematics.h:118
av_clip
#define av_clip
Definition: common.h:100
SingleChannelElement::can_pns
uint8_t can_pns[128]
band is allowed to PNS (informative)
Definition: aacenc.h:117
quantize_and_encode_band_cost_template
static av_always_inline float quantize_and_encode_band_cost_template(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED, int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, const float ROUNDING)
Calculate rate distortion cost for quantizing with given codebook.
Definition: aaccoder.c:75
libm.h
out
static FILE * out
Definition: movenc.c:55
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1040
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:247
aacenctab.h
log2f
#define log2f(x)
Definition: libm.h:411
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:124
quantize_and_encode_band_cost_NONE
static float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, const float *in, float *quant, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy)
Definition: aaccoder.c:195
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:291
SingleChannelElement::zeroes
uint8_t zeroes[128]
band is not coded
Definition: aacenc.h:116
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:154
bval2bmax
static av_always_inline float bval2bmax(float b)
approximates exp10f(-3.0f*(0.5f + 0.5f * cosf(FFMIN(b,15.5f) / 15.5f)))
Definition: aacenc_utils.h:164
ff_aac_coders
const AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB]
Definition: aaccoder.c:817
b
#define b
Definition: input.c:43
ROUND_TO_ZERO
#define ROUND_TO_ZERO
Definition: aacenc_utils.h:37
float.h
AAC_CODER_NB
@ AAC_CODER_NB
Definition: aacenc.h:49
STEREO
#define STEREO
Definition: cook.c:65
aac_cb_maxval
static const uint8_t aac_cb_maxval[12]
Definition: aacenctab.h:139
mathematics.h
ff_sfdelta_can_remove_band
static int ff_sfdelta_can_remove_band(const SingleChannelElement *sce, const uint8_t *nextband, int prev_sf, int band)
Definition: aacenc_utils.h:208
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
search_for_ms
static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
Definition: aaccoder.c:676
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
ChannelElement::ch
SingleChannelElement ch[2]
Definition: aacdec.h:302
ceilf
static __device__ float ceilf(float a)
Definition: cuda_runtime.h:175
B1
@ B1
Definition: mvs.c:532
ESC
#define ESC
Definition: iterm2enc.c:32
roundf
static av_always_inline av_const float roundf(float x)
Definition: libm.h:453
SCALE_MAX_POS
#define SCALE_MAX_POS
scalefactor index maximum value
Definition: aac.h:93
AAC_CODER_FAST
@ AAC_CODER_FAST
Definition: aacenc.h:46
av_always_inline
#define av_always_inline
Definition: attributes.h:76
S
#define S(s, c, i)
Definition: flacdsp_template.c:46
IndividualChannelStream::num_swb
int num_swb
number of scalefactor window bands
Definition: aacdec.h:178
SingleChannelElement::coeffs
float coeffs[1024]
coefficients for IMDCT, maybe processed
Definition: aacenc.h:121
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:2034
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1055
SCALE_DIV_512
#define SCALE_DIV_512
scalefactor difference that corresponds to scale difference in 512 times
Definition: aac.h:91
ff_sfdelta_can_replace
static int ff_sfdelta_can_replace(const SingleChannelElement *sce, const uint8_t *nextband, int prev_sf, int new_sf, int band)
Definition: aacenc_utils.h:222
POW_SF2_ZERO
#define POW_SF2_ZERO
ff_aac_pow2sf_tab index corresponding to pow(2, 0);
Definition: aac.h:97
quantize_and_encode_band
static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size, int scale_idx, int cb, const float lambda, int rtz)
Definition: aaccoder.c:279
quantize_and_encode_band_cost_arr
static const quantize_and_encode_band_func quantize_and_encode_band_cost_arr[]
Definition: aaccoder.c:228
fabsf
static __device__ float fabsf(float a)
Definition: cuda_runtime.h:181
SingleChannelElement::ics
IndividualChannelStream ics
Definition: aacdec.h:218
Q
#define Q(q)
quant
static const uint8_t quant[64]
Definition: vmixdec.c:71
aaccoder_nmr.h
QUANTIZE_AND_ENCODE_BAND_COST_FUNC
#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING)
Definition: aaccoder.c:204
float
float
Definition: af_crystalizer.c:122
search_for_pns
static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
Definition: aaccoder.c:490
NOISE_BT
@ NOISE_BT
Spectral data are scaled white noise not coded in the bitstream.
Definition: aac.h:75
b3
static double b3(void *priv, double x, double y)
Definition: vf_xfade.c:2036
ff_quantize_and_encode_band_cost
float ff_quantize_and_encode_band_cost(struct AACEncContext *s, PutBitContext *pb, const float *in, float *quant, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy)
Definition: aaccoder.c:268
IndividualChannelStream::swb_sizes
const uint8_t * swb_sizes
table of scalefactor band sizes for a particular window
Definition: aacenc.h:85
g
const char * g
Definition: vf_curves.c:128
INTENSITY_BT2
@ INTENSITY_BT2
Scalefactor data are intensity stereo positions (out of phase).
Definition: aac.h:76
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
search_for_quantizers_fast
static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
Definition: aaccoder.c:343
mark_pns
static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
Definition: aaccoder.c:619
search_for_quantizers_nmr
static void search_for_quantizers_nmr(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
Definition: aaccoder_nmr.h:201
bands
static const float bands[]
Definition: af_superequalizer.c:56
SCALE_DIFF_ZERO
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
Definition: aac.h:95
PutBitContext
Definition: put_bits.h:50
quantize_band_cost_cached
static float quantize_band_cost_cached(struct AACEncContext *s, int w, int g, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy, int rtz)
Definition: aacenc_quantization_misc.h:31
TrellisPath
Definition: aaccoder.c:296
INTENSITY_BT
@ INTENSITY_BT
Scalefactor data are intensity stereo positions (in phase).
Definition: aac.h:77
ChannelElement::is_mask
uint8_t is_mask[128]
Set if intensity stereo is used.
Definition: aacenc.h:135
NULL
#define NULL
Definition: coverity.c:32
SingleChannelElement::is_ener
float is_ener[128]
Intensity stereo pos.
Definition: aacenc.h:118
aacenc_quantization.h
codebook_trellis_rate
static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce, int win, int group_len, const float lambda)
Definition: aaccoder_trellis.h:59
ff_aac_apply_tns
void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce)
Definition: aacenc_tns.c:111
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:493
aac_cb_range
static const uint8_t aac_cb_range[12]
Definition: aacenctab.h:138
mathops.h
ChannelElement::ms_mask
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
Definition: aacdec.h:300
FFPsyBand
single band psychoacoustic information
Definition: psymodel.h:50
aac.h
aactab.h
ff_aac_encode_tns_info
void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
Encode TNS data.
Definition: aacenc_tns.c:70
sqrtf
static __device__ float sqrtf(float a)
Definition: cuda_runtime.h:184
ff_init_nextband_map
static void ff_init_nextband_map(const SingleChannelElement *sce, uint8_t *nextband)
Definition: aacenc_utils.h:175
av_clipf
av_clipf
Definition: af_crystalizer.c:122
aaccoder_twoloop.h
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
SingleChannelElement::sf_idx
int sf_idx[128]
scalefactor indices
Definition: aacenc.h:115
ff_aac_pow34sf_tab
float ff_aac_pow34sf_tab[428]
ff_aac_scalefactor_bits
const uint8_t ff_aac_scalefactor_bits[121]
Definition: aactab.c:200
f
f
Definition: af_crystalizer.c:122
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
NOISE_SPREAD_THRESHOLD
#define NOISE_SPREAD_THRESHOLD
Definition: aaccoder.c:55
size
int size
Definition: twinvq_data.h:10344
ff_aac_spectral_codes
const uint16_t *const ff_aac_spectral_codes[11]
Definition: aactab.c:525
ROUND_STANDARD
#define ROUND_STANDARD
Definition: aacenc_utils.h:36
AAC_CODER_TWOLOOP
@ AAC_CODER_TWOLOOP
Definition: aacenc.h:45
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:2035
ChannelElement::common_window
int common_window
Set if channels share a common 'IndividualChannelStream' in bitstream.
Definition: aacenc.h:131
search_for_quantizers_twoloop
static void search_for_quantizers_twoloop(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
two-loop quantizers search taken from ISO 13818-7 Appendix C
Definition: aaccoder_twoloop.h:65
quantize_and_encode_band_cost_rtz_arr
static const quantize_and_encode_band_func quantize_and_encode_band_cost_rtz_arr[]
Definition: aaccoder.c:248
SingleChannelElement::band_type
enum BandType band_type[128]
band types
Definition: aacdec.h:221
SCALE_MAX_DIFF
#define SCALE_MAX_DIFF
maximum scalefactor difference allowed by standard
Definition: aac.h:94
SingleChannelElement::pns_ener
float pns_ener[128]
Noise energy values.
Definition: aacenc.h:119
ZERO
#define ZERO
Definition: aap_template.c:32
aacenc_is.h
quantize_and_encode_band_func
float(* quantize_and_encode_band_func)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *quant, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy)
Definition: aaccoder.c:64
SingleChannelElement
Single Channel Element - used for both SCE and LFE elements.
Definition: aacdec.h:217
IndividualChannelStream::num_windows
int num_windows
Definition: aacdec.h:179
SCALE_ONE_POS
#define SCALE_ONE_POS
scalefactor index that corresponds to scale=1.0
Definition: aac.h:92
find_min_book
static int find_min_book(float maxval, int sf)
Definition: aacenc_utils.h:68
FFPsyBand::threshold
float threshold
Definition: psymodel.h:53
ff_aac_search_for_tns
void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
Definition: aacenc_tns.c:174
ChannelElement
channel element - generic struct for SCE/CPE/CCE/LFE
Definition: aacdec.h:296
IndividualChannelStream::swb_offset
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Definition: aacdec.h:177
s
uint8_t s
Definition: llvidencdsp.c:39
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
cbrtf
static av_always_inline float cbrtf(float x)
Definition: libm.h:63
TrellisPath::prev
int prev
Definition: aaccoder.c:298
set_special_band_scalefactors
static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
Definition: aaccoder.c:304
ff_aac_codebook_vectors
const float *const ff_aac_codebook_vectors[]
Definition: aactab.c:1026
len
int len
Definition: vorbis_enc_data.h:426
AAC_CODER_NMR
@ AAC_CODER_NMR
Definition: aacenc.h:47
NOISE_LOW_LIMIT
#define NOISE_LOW_LIMIT
This file contains a template for the twoloop coder function.
Definition: aaccoder_twoloop.h:54
AACCoefficientsEncoder
Definition: aacenc.h:142
ff_aac_search_for_is
void ff_aac_search_for_is(AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe)
Definition: aacenc_is.c:109
B0
@ B0
Definition: mvs.c:531
avcodec.h
BandCodingPath
structure used in optimal codebook search
Definition: aaccoder.c:290
dim
int dim
Definition: vorbis_enc_data.h:425
ff_aac_spectral_bits
const uint8_t *const ff_aac_spectral_bits[11]
Definition: aactab.c:530
RESERVED_BT
@ RESERVED_BT
Band types following are encoded differently from others.
Definition: aac.h:74
AACEncContext
AAC encoder context.
Definition: aacenc.h:204
FFPsyBand::energy
float energy
Definition: psymodel.h:52
AVCodecContext
main external API structure.
Definition: avcodec.h:443
M
#define M(chr)
Definition: exr.c:177
aacenc_tns.h
quantize_band_cost
static float quantize_band_cost(struct AACEncContext *s, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy)
Definition: aacenc_quantization.h:43
find_max_val
static float find_max_val(int group_len, int swb_size, const float *scaled)
Definition: aacenc_utils.h:56
lcg_random
static av_always_inline int lcg_random(unsigned previous_val)
linear congruential pseudorandom number generator
Definition: aacdec_proc_template.c:39
BandCodingPath::prev_idx
int prev_idx
pointer to the previous path point
Definition: aaccoder.c:291
w
uint8_t w
Definition: llvidencdsp.c:39
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:278
AACEncContext::pb
PutBitContext pb
Definition: aacenc.h:207
TrellisPath::cost
float cost
Definition: aaccoder.c:297
FFPsyBand::spread
float spread
Definition: psymodel.h:54
aacenc_utils.h
ff_aac_pow2sf_tab
float ff_aac_pow2sf_tab[428]
AACEncContext::lambda
float lambda
Definition: aacenc.h:230
put_bits.h
IndividualChannelStream::group_len
uint8_t group_len[8]
Definition: aacdec.h:175
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
BandCodingPath::run
int run
Definition: aaccoder.c:293
ff_quantize_band_cost_cache_init
void ff_quantize_band_cost_cache_init(struct AACEncContext *s)
Definition: aacenc.c:373
aaccoder_trellis.h
NOISE_LAMBDA_REPLACE
#define NOISE_LAMBDA_REPLACE
Definition: aaccoder.c:59
aacenc.h
BandCodingPath::cost
float cost
path cost
Definition: aaccoder.c:292