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 #include "aacenc_ltp.h"
51 #include "aacenc_pred.h"
52 
54 
55 /* Parameter of f(x) = a*(lambda/100), defines the maximum fourier spread
56  * beyond which no PNS is used (since the SFBs contain tone rather than noise) */
57 #define NOISE_SPREAD_THRESHOLD 0.9f
58 
59 /* Parameter of f(x) = a*(100/lambda), defines how much PNS is allowed to
60  * replace low energy non zero bands */
61 #define NOISE_LAMBDA_REPLACE 1.948f
62 
64 
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  */
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->aacdsp.abs_pow34(s->scoefs, in, size);
112  scaled = s->scoefs;
113  }
114  s->aacdsp.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 
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 
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)
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)
228 
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 
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 
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 
281  const float *in, float *out, int size, int scale_idx,
282  int cb, const float lambda, int rtz)
283 {
285  lambda, INFINITY, NULL, NULL);
286 }
287 
288 /**
289  * structure used in optimal codebook search
290  */
291 typedef struct BandCodingPath {
292  int prev_idx; ///< pointer to the previous path point
293  float cost; ///< path cost
294  int run;
296 
297 /**
298  * Encode band info for single window group bands.
299  */
301  int win, int group_len, const float lambda)
302 {
303  BandCodingPath path[120][CB_TOT_ALL];
304  int w, swb, cb, start, size;
305  int i, j;
306  const int max_sfb = sce->ics.max_sfb;
307  const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
308  const int run_esc = (1 << run_bits) - 1;
309  int idx, ppos, count;
310  int stackrun[120], stackcb[120], stack_len;
311  float next_minrd = INFINITY;
312  int next_mincb = 0;
313 
314  s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
315  start = win*128;
316  for (cb = 0; cb < CB_TOT_ALL; cb++) {
317  path[0][cb].cost = 0.0f;
318  path[0][cb].prev_idx = -1;
319  path[0][cb].run = 0;
320  }
321  for (swb = 0; swb < max_sfb; swb++) {
322  size = sce->ics.swb_sizes[swb];
323  if (sce->zeroes[win*16 + swb]) {
324  for (cb = 0; cb < CB_TOT_ALL; cb++) {
325  path[swb+1][cb].prev_idx = cb;
326  path[swb+1][cb].cost = path[swb][cb].cost;
327  path[swb+1][cb].run = path[swb][cb].run + 1;
328  }
329  } else {
330  float minrd = next_minrd;
331  int mincb = next_mincb;
332  next_minrd = INFINITY;
333  next_mincb = 0;
334  for (cb = 0; cb < CB_TOT_ALL; cb++) {
335  float cost_stay_here, cost_get_here;
336  float rd = 0.0f;
337  if (cb >= 12 && sce->band_type[win*16+swb] < aac_cb_out_map[cb] ||
338  cb < aac_cb_in_map[sce->band_type[win*16+swb]] && sce->band_type[win*16+swb] > aac_cb_out_map[cb]) {
339  path[swb+1][cb].prev_idx = -1;
340  path[swb+1][cb].cost = INFINITY;
341  path[swb+1][cb].run = path[swb][cb].run + 1;
342  continue;
343  }
344  for (w = 0; w < group_len; w++) {
345  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb];
346  rd += quantize_band_cost(s, &sce->coeffs[start + w*128],
347  &s->scoefs[start + w*128], size,
348  sce->sf_idx[(win+w)*16+swb], aac_cb_out_map[cb],
349  lambda / band->threshold, INFINITY, NULL, NULL);
350  }
351  cost_stay_here = path[swb][cb].cost + rd;
352  cost_get_here = minrd + rd + run_bits + 4;
353  if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
354  != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
355  cost_stay_here += run_bits;
356  if (cost_get_here < cost_stay_here) {
357  path[swb+1][cb].prev_idx = mincb;
358  path[swb+1][cb].cost = cost_get_here;
359  path[swb+1][cb].run = 1;
360  } else {
361  path[swb+1][cb].prev_idx = cb;
362  path[swb+1][cb].cost = cost_stay_here;
363  path[swb+1][cb].run = path[swb][cb].run + 1;
364  }
365  if (path[swb+1][cb].cost < next_minrd) {
366  next_minrd = path[swb+1][cb].cost;
367  next_mincb = cb;
368  }
369  }
370  }
371  start += sce->ics.swb_sizes[swb];
372  }
373 
374  //convert resulting path from backward-linked list
375  stack_len = 0;
376  idx = 0;
377  for (cb = 1; cb < CB_TOT_ALL; cb++)
378  if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
379  idx = cb;
380  ppos = max_sfb;
381  while (ppos > 0) {
382  av_assert1(idx >= 0);
383  cb = idx;
384  stackrun[stack_len] = path[ppos][cb].run;
385  stackcb [stack_len] = cb;
386  idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
387  ppos -= path[ppos][cb].run;
388  stack_len++;
389  }
390  //perform actual band info encoding
391  start = 0;
392  for (i = stack_len - 1; i >= 0; i--) {
393  cb = aac_cb_out_map[stackcb[i]];
394  put_bits(&s->pb, 4, cb);
395  count = stackrun[i];
396  memset(sce->zeroes + win*16 + start, !cb, count);
397  //XXX: memset when band_type is also uint8_t
398  for (j = 0; j < count; j++) {
399  sce->band_type[win*16 + start] = cb;
400  start++;
401  }
402  while (count >= run_esc) {
403  put_bits(&s->pb, run_bits, run_esc);
404  count -= run_esc;
405  }
406  put_bits(&s->pb, run_bits, count);
407  }
408 }
409 
410 
411 typedef struct TrellisPath {
412  float cost;
413  int prev;
414 } TrellisPath;
415 
416 #define TRELLIS_STAGES 121
417 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
418 
420 {
421  int w, g;
422  int prevscaler_n = -255, prevscaler_i = 0;
423  int bands = 0;
424 
425  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
426  for (g = 0; g < sce->ics.num_swb; g++) {
427  if (sce->zeroes[w*16+g])
428  continue;
429  if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
430  sce->sf_idx[w*16+g] = av_clip(roundf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
431  bands++;
432  } else if (sce->band_type[w*16+g] == NOISE_BT) {
433  sce->sf_idx[w*16+g] = av_clip(3+ceilf(log2f(sce->pns_ener[w*16+g])*2), -100, 155);
434  if (prevscaler_n == -255)
435  prevscaler_n = sce->sf_idx[w*16+g];
436  bands++;
437  }
438  }
439  }
440 
441  if (!bands)
442  return;
443 
444  /* Clip the scalefactor indices */
445  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
446  for (g = 0; g < sce->ics.num_swb; g++) {
447  if (sce->zeroes[w*16+g])
448  continue;
449  if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
450  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);
451  } else if (sce->band_type[w*16+g] == NOISE_BT) {
452  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);
453  }
454  }
455  }
456 }
457 
460  const float lambda)
461 {
462  int q, w, w2, g, start = 0;
463  int i, j;
464  int idx;
466  int bandaddr[TRELLIS_STAGES];
467  int minq;
468  float mincost;
469  float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
470  int q0, q1, qcnt = 0;
471 
472  for (i = 0; i < 1024; i++) {
473  float t = fabsf(sce->coeffs[i]);
474  if (t > 0.0f) {
475  q0f = FFMIN(q0f, t);
476  q1f = FFMAX(q1f, t);
477  qnrgf += t*t;
478  qcnt++;
479  }
480  }
481 
482  if (!qcnt) {
483  memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
484  memset(sce->zeroes, 1, sizeof(sce->zeroes));
485  return;
486  }
487 
488  //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
489  q0 = av_clip(coef2minsf(q0f), 0, SCALE_MAX_POS-1);
490  //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
491  q1 = av_clip(coef2maxsf(q1f), 1, SCALE_MAX_POS);
492  if (q1 - q0 > 60) {
493  int q0low = q0;
494  int q1high = q1;
495  //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
496  int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
497  q1 = qnrg + 30;
498  q0 = qnrg - 30;
499  if (q0 < q0low) {
500  q1 += q0low - q0;
501  q0 = q0low;
502  } else if (q1 > q1high) {
503  q0 -= q1 - q1high;
504  q1 = q1high;
505  }
506  }
507  // q0 == q1 isn't really a legal situation
508  if (q0 == q1) {
509  // the following is indirect but guarantees q1 != q0 && q1 near q0
510  q1 = av_clip(q0+1, 1, SCALE_MAX_POS);
511  q0 = av_clip(q1-1, 0, SCALE_MAX_POS - 1);
512  }
513 
514  for (i = 0; i < TRELLIS_STATES; i++) {
515  paths[0][i].cost = 0.0f;
516  paths[0][i].prev = -1;
517  }
518  for (j = 1; j < TRELLIS_STAGES; j++) {
519  for (i = 0; i < TRELLIS_STATES; i++) {
520  paths[j][i].cost = INFINITY;
521  paths[j][i].prev = -2;
522  }
523  }
524  idx = 1;
525  s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
526  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
527  start = w*128;
528  for (g = 0; g < sce->ics.num_swb; g++) {
529  const float *coefs = &sce->coeffs[start];
530  float qmin, qmax;
531  int nz = 0;
532 
533  bandaddr[idx] = w * 16 + g;
534  qmin = INT_MAX;
535  qmax = 0.0f;
536  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
537  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
538  if (band->energy <= band->threshold || band->threshold == 0.0f) {
539  sce->zeroes[(w+w2)*16+g] = 1;
540  continue;
541  }
542  sce->zeroes[(w+w2)*16+g] = 0;
543  nz = 1;
544  for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
545  float t = fabsf(coefs[w2*128+i]);
546  if (t > 0.0f)
547  qmin = FFMIN(qmin, t);
548  qmax = FFMAX(qmax, t);
549  }
550  }
551  if (nz) {
552  int minscale, maxscale;
553  float minrd = INFINITY;
554  float maxval;
555  //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
556  minscale = coef2minsf(qmin);
557  //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
558  maxscale = coef2maxsf(qmax);
559  minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
560  maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
561  if (minscale == maxscale) {
562  maxscale = av_clip(minscale+1, 1, TRELLIS_STATES);
563  minscale = av_clip(maxscale-1, 0, TRELLIS_STATES - 1);
564  }
565  maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
566  for (q = minscale; q < maxscale; q++) {
567  float dist = 0;
568  int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
569  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
570  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
571  dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
572  q + q0, cb, lambda / band->threshold, INFINITY, NULL, NULL);
573  }
574  minrd = FFMIN(minrd, dist);
575 
576  for (i = 0; i < q1 - q0; i++) {
577  float cost;
578  cost = paths[idx - 1][i].cost + dist
580  if (cost < paths[idx][q].cost) {
581  paths[idx][q].cost = cost;
582  paths[idx][q].prev = i;
583  }
584  }
585  }
586  } else {
587  for (q = 0; q < q1 - q0; q++) {
588  paths[idx][q].cost = paths[idx - 1][q].cost + 1;
589  paths[idx][q].prev = q;
590  }
591  }
592  sce->zeroes[w*16+g] = !nz;
593  start += sce->ics.swb_sizes[g];
594  idx++;
595  }
596  }
597  idx--;
598  mincost = paths[idx][0].cost;
599  minq = 0;
600  for (i = 1; i < TRELLIS_STATES; i++) {
601  if (paths[idx][i].cost < mincost) {
602  mincost = paths[idx][i].cost;
603  minq = i;
604  }
605  }
606  while (idx) {
607  sce->sf_idx[bandaddr[idx]] = minq + q0;
608  minq = FFMAX(paths[idx][minq].prev, 0);
609  idx--;
610  }
611  //set the same quantizers inside window groups
612  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
613  for (g = 0; g < sce->ics.num_swb; g++)
614  for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
615  sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
616 }
617 
620  const float lambda)
621 {
622  int start = 0, i, w, w2, g;
623  int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->ch_layout.nb_channels * (lambda / 120.f);
624  float dists[128] = { 0 }, uplims[128] = { 0 };
625  float maxvals[128];
626  int fflag, minscaler;
627  int its = 0;
628  int allz = 0;
629  float minthr = INFINITY;
630 
631  // for values above this the decoder might end up in an endless loop
632  // due to always having more bits than what can be encoded.
633  destbits = FFMIN(destbits, 5800);
634  //some heuristic to determine initial quantizers will reduce search time
635  //determine zero bands and upper limits
636  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
637  start = 0;
638  for (g = 0; g < sce->ics.num_swb; g++) {
639  int nz = 0;
640  float uplim = 0.0f;
641  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
642  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
643  uplim += band->threshold;
644  if (band->energy <= band->threshold || band->threshold == 0.0f) {
645  sce->zeroes[(w+w2)*16+g] = 1;
646  continue;
647  }
648  nz = 1;
649  }
650  uplims[w*16+g] = uplim *512;
651  sce->band_type[w*16+g] = 0;
652  sce->zeroes[w*16+g] = !nz;
653  if (nz)
654  minthr = FFMIN(minthr, uplim);
655  allz |= nz;
656  start += sce->ics.swb_sizes[g];
657  }
658  }
659  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
660  for (g = 0; g < sce->ics.num_swb; g++) {
661  if (sce->zeroes[w*16+g]) {
662  sce->sf_idx[w*16+g] = SCALE_ONE_POS;
663  continue;
664  }
665  sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
666  }
667  }
668 
669  if (!allz)
670  return;
671  s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
673 
674  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
675  start = w*128;
676  for (g = 0; g < sce->ics.num_swb; g++) {
677  const float *scaled = s->scoefs + start;
678  maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
679  start += sce->ics.swb_sizes[g];
680  }
681  }
682 
683  //perform two-loop search
684  //outer loop - improve quality
685  do {
686  int tbits, qstep;
687  minscaler = sce->sf_idx[0];
688  //inner loop - quantize spectrum to fit into given number of bits
689  qstep = its ? 1 : 32;
690  do {
691  int prev = -1;
692  tbits = 0;
693  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
694  start = w*128;
695  for (g = 0; g < sce->ics.num_swb; g++) {
696  const float *coefs = sce->coeffs + start;
697  const float *scaled = s->scoefs + start;
698  int bits = 0;
699  int cb;
700  float dist = 0.0f;
701 
702  if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
703  start += sce->ics.swb_sizes[g];
704  continue;
705  }
706  minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
707  cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
708  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
709  int b;
710  dist += quantize_band_cost_cached(s, w + w2, g,
711  coefs + w2*128,
712  scaled + w2*128,
713  sce->ics.swb_sizes[g],
714  sce->sf_idx[w*16+g],
715  cb, 1.0f, INFINITY,
716  &b, NULL, 0);
717  bits += b;
718  }
719  dists[w*16+g] = dist - bits;
720  if (prev != -1) {
721  bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
722  }
723  tbits += bits;
724  start += sce->ics.swb_sizes[g];
725  prev = sce->sf_idx[w*16+g];
726  }
727  }
728  if (tbits > destbits) {
729  for (i = 0; i < 128; i++)
730  if (sce->sf_idx[i] < 218 - qstep)
731  sce->sf_idx[i] += qstep;
732  } else {
733  for (i = 0; i < 128; i++)
734  if (sce->sf_idx[i] > 60 - qstep)
735  sce->sf_idx[i] -= qstep;
736  }
737  qstep >>= 1;
738  if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
739  qstep = 1;
740  } while (qstep);
741 
742  fflag = 0;
743  minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
744 
745  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
746  for (g = 0; g < sce->ics.num_swb; g++) {
747  int prevsc = sce->sf_idx[w*16+g];
748  if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
749  if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
750  sce->sf_idx[w*16+g]--;
751  else //Try to make sure there is some energy in every band
752  sce->sf_idx[w*16+g]-=2;
753  }
754  sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
755  sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
756  if (sce->sf_idx[w*16+g] != prevsc)
757  fflag = 1;
758  sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
759  }
760  }
761  its++;
762  } while (fflag && its < 10);
763 }
764 
766 {
767  FFPsyBand *band;
768  int w, g, w2, i;
769  int wlen = 1024 / sce->ics.num_windows;
770  int bandwidth, cutoff;
771  float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128];
772  float *NOR34 = &s->scoefs[3*128];
773  uint8_t nextband[128];
774  const float lambda = s->lambda;
775  const float freq_mult = avctx->sample_rate*0.5f/wlen;
776  const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda);
777  const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
778  const float dist_bias = av_clipf(4.f * 120 / lambda, 0.25f, 4.0f);
779  const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
780 
781  int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
782  / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
783  * (lambda / 120.f);
784 
785  /** Keep this in sync with twoloop's cutoff selection */
786  float rate_bandwidth_multiplier = 1.5f;
787  int prev = -1000, prev_sf = -1;
788  int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
789  ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
790  : (avctx->bit_rate / avctx->ch_layout.nb_channels);
791 
792  frame_bit_rate *= 1.15f;
793 
794  if (avctx->cutoff > 0) {
795  bandwidth = avctx->cutoff;
796  } else {
797  bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate));
798  }
799 
800  cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
801 
802  memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
803  ff_init_nextband_map(sce, nextband);
804  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
805  int wstart = w*128;
806  for (g = 0; g < sce->ics.num_swb; g++) {
807  int noise_sfi;
808  float dist1 = 0.0f, dist2 = 0.0f, noise_amp;
809  float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh;
810  float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
811  float min_energy = -1.0f, max_energy = 0.0f;
812  const int start = wstart+sce->ics.swb_offset[g];
813  const float freq = (start-wstart)*freq_mult;
814  const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
815  if (freq < NOISE_LOW_LIMIT || (start-wstart) >= cutoff) {
816  if (!sce->zeroes[w*16+g])
817  prev_sf = sce->sf_idx[w*16+g];
818  continue;
819  }
820  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
821  band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
822  sfb_energy += band->energy;
823  spread = FFMIN(spread, band->spread);
824  threshold += band->threshold;
825  if (!w2) {
826  min_energy = max_energy = band->energy;
827  } else {
828  min_energy = FFMIN(min_energy, band->energy);
829  max_energy = FFMAX(max_energy, band->energy);
830  }
831  }
832 
833  /* Ramps down at ~8000Hz and loosens the dist threshold */
834  dist_thresh = av_clipf(2.5f*NOISE_LOW_LIMIT/freq, 0.5f, 2.5f) * dist_bias;
835 
836  /* PNS is acceptable when all of these are true:
837  * 1. high spread energy (noise-like band)
838  * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
839  * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
840  *
841  * At this stage, point 2 is relaxed for zeroed bands near the noise threshold (hole avoidance is more important)
842  */
843  if ((!sce->zeroes[w*16+g] && !ff_sfdelta_can_remove_band(sce, nextband, prev_sf, w*16+g)) ||
844  ((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.0f/freq_boost)) || spread < spread_threshold ||
845  (!sce->zeroes[w*16+g] && sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost) ||
846  min_energy < pns_transient_energy_r * max_energy ) {
847  sce->pns_ener[w*16+g] = sfb_energy;
848  if (!sce->zeroes[w*16+g])
849  prev_sf = sce->sf_idx[w*16+g];
850  continue;
851  }
852 
853  pns_tgt_energy = sfb_energy*FFMIN(1.0f, spread*spread);
854  noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */
855  noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO]; /* Dequantize */
856  if (prev != -1000) {
857  int noise_sfdiff = noise_sfi - prev + SCALE_DIFF_ZERO;
858  if (noise_sfdiff < 0 || noise_sfdiff > 2*SCALE_MAX_DIFF) {
859  if (!sce->zeroes[w*16+g])
860  prev_sf = sce->sf_idx[w*16+g];
861  continue;
862  }
863  }
864  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
865  float band_energy, scale, pns_senergy;
866  const int start_c = (w+w2)*128+sce->ics.swb_offset[g];
867  band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
868  for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
869  s->random_state = lcg_random(s->random_state);
870  PNS[i] = s->random_state;
871  }
872  band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
873  scale = noise_amp/sqrtf(band_energy);
874  s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]);
875  pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
876  pns_energy += pns_senergy;
877  s->aacdsp.abs_pow34(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]);
878  s->aacdsp.abs_pow34(PNS34, PNS, sce->ics.swb_sizes[g]);
879  dist1 += quantize_band_cost(s, &sce->coeffs[start_c],
880  NOR34,
881  sce->ics.swb_sizes[g],
882  sce->sf_idx[(w+w2)*16+g],
883  sce->band_alt[(w+w2)*16+g],
884  lambda/band->threshold, INFINITY, NULL, NULL);
885  /* Estimate rd on average as 5 bits for SF, 4 for the CB, plus spread energy * lambda/thr */
886  dist2 += band->energy/(band->spread*band->spread)*lambda*dist_thresh/band->threshold;
887  }
888  if (g && sce->band_type[w*16+g-1] == NOISE_BT) {
889  dist2 += 5;
890  } else {
891  dist2 += 9;
892  }
893  energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */
894  sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy;
895  if (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) {
896  sce->band_type[w*16+g] = NOISE_BT;
897  sce->zeroes[w*16+g] = 0;
898  prev = noise_sfi;
899  } else {
900  if (!sce->zeroes[w*16+g])
901  prev_sf = sce->sf_idx[w*16+g];
902  }
903  }
904  }
905 }
906 
908 {
909  FFPsyBand *band;
910  int w, g, w2;
911  int wlen = 1024 / sce->ics.num_windows;
912  int bandwidth, cutoff;
913  const float lambda = s->lambda;
914  const float freq_mult = avctx->sample_rate*0.5f/wlen;
915  const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
916  const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
917 
918  int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
919  / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
920  * (lambda / 120.f);
921 
922  /** Keep this in sync with twoloop's cutoff selection */
923  float rate_bandwidth_multiplier = 1.5f;
924  int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
925  ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
926  : (avctx->bit_rate / avctx->ch_layout.nb_channels);
927 
928  frame_bit_rate *= 1.15f;
929 
930  if (avctx->cutoff > 0) {
931  bandwidth = avctx->cutoff;
932  } else {
933  bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate));
934  }
935 
936  cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
937 
938  memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
939  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
940  for (g = 0; g < sce->ics.num_swb; g++) {
941  float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
942  float min_energy = -1.0f, max_energy = 0.0f;
943  const int start = sce->ics.swb_offset[g];
944  const float freq = start*freq_mult;
945  const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
946  if (freq < NOISE_LOW_LIMIT || start >= cutoff) {
947  sce->can_pns[w*16+g] = 0;
948  continue;
949  }
950  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
951  band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
952  sfb_energy += band->energy;
953  spread = FFMIN(spread, band->spread);
954  threshold += band->threshold;
955  if (!w2) {
956  min_energy = max_energy = band->energy;
957  } else {
958  min_energy = FFMIN(min_energy, band->energy);
959  max_energy = FFMAX(max_energy, band->energy);
960  }
961  }
962 
963  /* PNS is acceptable when all of these are true:
964  * 1. high spread energy (noise-like band)
965  * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
966  * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
967  */
968  sce->pns_ener[w*16+g] = sfb_energy;
969  if (sfb_energy < threshold*sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) {
970  sce->can_pns[w*16+g] = 0;
971  } else {
972  sce->can_pns[w*16+g] = 1;
973  }
974  }
975  }
976 }
977 
979 {
980  int start = 0, i, w, w2, g, sid_sf_boost, prev_mid, prev_side;
981  uint8_t nextband0[128], nextband1[128];
982  float *M = s->scoefs + 128*0, *S = s->scoefs + 128*1;
983  float *L34 = s->scoefs + 128*2, *R34 = s->scoefs + 128*3;
984  float *M34 = s->scoefs + 128*4, *S34 = s->scoefs + 128*5;
985  const float lambda = s->lambda;
986  const float mslambda = FFMIN(1.0f, lambda / 120.f);
987  SingleChannelElement *sce0 = &cpe->ch[0];
988  SingleChannelElement *sce1 = &cpe->ch[1];
989  if (!cpe->common_window)
990  return;
991 
992  /** Scout out next nonzero bands */
993  ff_init_nextband_map(sce0, nextband0);
994  ff_init_nextband_map(sce1, nextband1);
995 
996  prev_mid = sce0->sf_idx[0];
997  prev_side = sce1->sf_idx[0];
998  for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
999  start = 0;
1000  for (g = 0; g < sce0->ics.num_swb; g++) {
1001  float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f;
1002  if (!cpe->is_mask[w*16+g])
1003  cpe->ms_mask[w*16+g] = 0;
1004  if (!sce0->zeroes[w*16+g] && !sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g]) {
1005  float Mmax = 0.0f, Smax = 0.0f;
1006 
1007  /* Must compute mid/side SF and book for the whole window group */
1008  for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
1009  for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1010  M[i] = (sce0->coeffs[start+(w+w2)*128+i]
1011  + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
1012  S[i] = M[i]
1013  - sce1->coeffs[start+(w+w2)*128+i];
1014  }
1015  s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]);
1016  s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]);
1017  for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) {
1018  Mmax = FFMAX(Mmax, M34[i]);
1019  Smax = FFMAX(Smax, S34[i]);
1020  }
1021  }
1022 
1023  for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) {
1024  float dist1 = 0.0f, dist2 = 0.0f;
1025  int B0 = 0, B1 = 0;
1026  int minidx;
1027  int mididx, sididx;
1028  int midcb, sidcb;
1029 
1030  minidx = FFMIN(sce0->sf_idx[w*16+g], sce1->sf_idx[w*16+g]);
1031  mididx = av_clip(minidx, 0, SCALE_MAX_POS - SCALE_DIV_512);
1032  sididx = av_clip(minidx - sid_sf_boost * 3, 0, SCALE_MAX_POS - SCALE_DIV_512);
1033  if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT
1034  && ( !ff_sfdelta_can_replace(sce0, nextband0, prev_mid, mididx, w*16+g)
1035  || !ff_sfdelta_can_replace(sce1, nextband1, prev_side, sididx, w*16+g))) {
1036  /* scalefactor range violation, bad stuff, will decrease quality unacceptably */
1037  continue;
1038  }
1039 
1040  midcb = find_min_book(Mmax, mididx);
1041  sidcb = find_min_book(Smax, sididx);
1042 
1043  /* No CB can be zero */
1044  midcb = FFMAX(1,midcb);
1045  sidcb = FFMAX(1,sidcb);
1046 
1047  for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
1048  FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
1049  FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
1050  float minthr = FFMIN(band0->threshold, band1->threshold);
1051  int b1,b2,b3,b4;
1052  for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1053  M[i] = (sce0->coeffs[start+(w+w2)*128+i]
1054  + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
1055  S[i] = M[i]
1056  - sce1->coeffs[start+(w+w2)*128+i];
1057  }
1058 
1059  s->aacdsp.abs_pow34(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
1060  s->aacdsp.abs_pow34(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
1061  s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]);
1062  s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]);
1063  dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
1064  L34,
1065  sce0->ics.swb_sizes[g],
1066  sce0->sf_idx[w*16+g],
1067  sce0->band_type[w*16+g],
1068  lambda / (band0->threshold + FLT_MIN), INFINITY, &b1, NULL);
1069  dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
1070  R34,
1071  sce1->ics.swb_sizes[g],
1072  sce1->sf_idx[w*16+g],
1073  sce1->band_type[w*16+g],
1074  lambda / (band1->threshold + FLT_MIN), INFINITY, &b2, NULL);
1075  dist2 += quantize_band_cost(s, M,
1076  M34,
1077  sce0->ics.swb_sizes[g],
1078  mididx,
1079  midcb,
1080  lambda / (minthr + FLT_MIN), INFINITY, &b3, NULL);
1081  dist2 += quantize_band_cost(s, S,
1082  S34,
1083  sce1->ics.swb_sizes[g],
1084  sididx,
1085  sidcb,
1086  mslambda / (minthr * bmax + FLT_MIN), INFINITY, &b4, NULL);
1087  B0 += b1+b2;
1088  B1 += b3+b4;
1089  dist1 -= b1+b2;
1090  dist2 -= b3+b4;
1091  }
1092  cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0;
1093  if (cpe->ms_mask[w*16+g]) {
1094  if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT) {
1095  sce0->sf_idx[w*16+g] = mididx;
1096  sce1->sf_idx[w*16+g] = sididx;
1097  sce0->band_type[w*16+g] = midcb;
1098  sce1->band_type[w*16+g] = sidcb;
1099  } else if ((sce0->band_type[w*16+g] != NOISE_BT) ^ (sce1->band_type[w*16+g] != NOISE_BT)) {
1100  /* ms_mask unneeded, and it confuses some decoders */
1101  cpe->ms_mask[w*16+g] = 0;
1102  }
1103  break;
1104  } else if (B1 > B0) {
1105  /* More boost won't fix this */
1106  break;
1107  }
1108  }
1109  }
1110  if (!sce0->zeroes[w*16+g] && sce0->band_type[w*16+g] < RESERVED_BT)
1111  prev_mid = sce0->sf_idx[w*16+g];
1112  if (!sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g] && sce1->band_type[w*16+g] < RESERVED_BT)
1113  prev_side = sce1->sf_idx[w*16+g];
1114  start += sce0->ics.swb_sizes[g];
1115  }
1116  }
1117 }
1118 
1120  [AAC_CODER_ANMR] = {
1135  mark_pns,
1138  search_for_ms,
1141  },
1142  [AAC_CODER_TWOLOOP] = {
1157  mark_pns,
1160  search_for_ms,
1163  },
1164  [AAC_CODER_FAST] = {
1179  mark_pns,
1182  search_for_ms,
1185  },
1186 };
M
#define M(a, b)
Definition: vp3dsp.c:48
SingleChannelElement::band_alt
enum BandType band_alt[128]
alternative band type
Definition: aacenc.h:132
q1
static const uint8_t q1[256]
Definition: twofish.c:100
av_clip
#define av_clip
Definition: common.h:100
INFINITY
#define INFINITY
Definition: mathematics.h:118
SingleChannelElement::can_pns
uint8_t can_pns[128]
band is allowed to PNS (informative)
Definition: aacenc.h:135
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:76
libm.h
out
FILE * out
Definition: movenc.c:55
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:247
aacenctab.h
log2f
#define log2f(x)
Definition: libm.h:409
CB_TOT_ALL
#define CB_TOT_ALL
Total number of codebooks, including special ones.
Definition: aacenctab.h:39
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:124
ff_aac_update_ltp
void ff_aac_update_ltp(AACEncContext *s, SingleChannelElement *sce)
Process LTP parameters.
Definition: aacenc_ltp.c:117
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:224
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:196
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:281
SingleChannelElement::zeroes
uint8_t zeroes[128]
band is not coded
Definition: aacenc.h:134
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
aacenc_ltp.h
w
uint8_t w
Definition: llviddspenc.c:38
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:1119
b
#define b
Definition: input.c:41
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:121
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:978
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
ChannelElement::ch
SingleChannelElement ch[2]
Definition: aacdec.h:266
ceilf
static __device__ float ceilf(float a)
Definition: cuda_runtime.h:175
B1
@ B1
Definition: mvs.c:527
roundf
static av_always_inline av_const float roundf(float x)
Definition: libm.h:451
coef2maxsf
static uint8_t coef2maxsf(float coef)
Return the maximum scalefactor where the quantized coef is not zero.
Definition: aacenc_utils.h:139
SCALE_MAX_POS
#define SCALE_MAX_POS
scalefactor index maximum value
Definition: aac.h:89
AAC_CODER_FAST
@ AAC_CODER_FAST
Definition: aacenc.h:47
win
static float win(SuperEqualizerContext *s, float n, int N)
Definition: af_superequalizer.c:119
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:171
SingleChannelElement::coeffs
float coeffs[1024]
coefficients for IMDCT, maybe processed
Definition: aacenc.h:139
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:1071
SCALE_DIV_512
#define SCALE_DIV_512
scalefactor difference that corresponds to scale difference in 512 times
Definition: aac.h:87
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
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:508
POW_SF2_ZERO
#define POW_SF2_ZERO
ff_aac_pow2sf_tab index corresponding to pow(2, 0);
Definition: aac.h:93
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:280
quantize_and_encode_band_cost_arr
static const quantize_and_encode_band_func quantize_and_encode_band_cost_arr[]
Definition: aaccoder.c:229
fabsf
static __device__ float fabsf(float a)
Definition: cuda_runtime.h:181
SingleChannelElement::ics
IndividualChannelStream ics
Definition: aacdec.h:211
quant
static const uint8_t quant[64]
Definition: vmixdec.c:71
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:205
float
float
Definition: af_crystalizer.c:122
search_for_pns
static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
Definition: aaccoder.c:765
NOISE_BT
@ NOISE_BT
Spectral data are scaled white noise not coded in the bitstream.
Definition: aac.h:71
b3
static double b3(void *priv, double x, double y)
Definition: vf_xfade.c:2036
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_aac_apply_main_pred
void ff_aac_apply_main_pred(AACEncContext *s, SingleChannelElement *sce)
Definition: aacenc_pred.c:119
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:269
IndividualChannelStream::swb_sizes
const uint8_t * swb_sizes
table of scalefactor band sizes for a particular window
Definition: aacenc.h:98
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:72
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
search_for_quantizers_fast
static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
Definition: aaccoder.c:618
ff_aac_encode_ltp_info
void ff_aac_encode_ltp_info(AACEncContext *s, SingleChannelElement *sce, int common_window)
Encode LTP data.
Definition: aacenc_ltp.c:35
mark_pns
static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
Definition: aaccoder.c:907
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:91
PutBitContext
Definition: put_bits.h:50
q0
static const uint8_t q0[256]
Definition: twofish.c:81
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:411
INTENSITY_BT
@ INTENSITY_BT
Scalefactor data are intensity stereo positions (in phase).
Definition: aac.h:73
ChannelElement::is_mask
uint8_t is_mask[128]
Set if intensity stereo is used.
Definition: aacenc.h:156
NULL
#define NULL
Definition: coverity.c:32
SingleChannelElement::is_ener
float is_ener[128]
Intensity stereo pos.
Definition: aacenc.h:136
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:102
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:501
aac_cb_range
static const uint8_t aac_cb_range[12]
Definition: aacenctab.h:120
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:264
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
run_value_bits
static const uint8_t *const run_value_bits[2]
Definition: aacenctab.h:111
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:133
ff_aac_pow34sf_tab
float ff_aac_pow34sf_tab[428]
ff_aac_ltp_insert_new_frame
void ff_aac_ltp_insert_new_frame(AACEncContext *s)
Definition: aacenc_ltp.c:53
ff_aac_scalefactor_bits
const uint8_t ff_aac_scalefactor_bits[121]
Definition: aactab.c:204
coef2minsf
static uint8_t coef2minsf(float coef)
Return the minimum scalefactor where the quantized coef does not clip.
Definition: aacenc_utils.h:133
aac_cb_out_map
static const uint8_t aac_cb_out_map[CB_TOT_ALL]
Map to convert values from BandCodingPath index to a codebook index.
Definition: aacenctab.h:116
AAC_CODER_ANMR
@ AAC_CODER_ANMR
Definition: aacenc.h:45
f
f
Definition: af_crystalizer.c:122
ff_aac_adjust_common_pred
void ff_aac_adjust_common_pred(AACEncContext *s, ChannelElement *cpe)
Definition: aacenc_pred.c:151
NOISE_SPREAD_THRESHOLD
#define NOISE_SPREAD_THRESHOLD
Definition: aaccoder.c:57
size
int size
Definition: twinvq_data.h:10344
run_bits
static const uint8_t run_bits[7][16]
Definition: h264_cavlc.c:227
ff_aac_spectral_codes
const uint16_t *const ff_aac_spectral_codes[11]
Definition: aactab.c:529
ROUND_STANDARD
#define ROUND_STANDARD
Definition: aacenc_utils.h:36
AAC_CODER_TWOLOOP
@ AAC_CODER_TWOLOOP
Definition: aacenc.h:46
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:152
ff_aac_adjust_common_ltp
void ff_aac_adjust_common_ltp(AACEncContext *s, ChannelElement *cpe)
Definition: aacenc_ltp.c:130
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:249
SingleChannelElement::band_type
enum BandType band_type[128]
band types
Definition: aacdec.h:214
SCALE_MAX_DIFF
#define SCALE_MAX_DIFF
maximum scalefactor difference allowed by standard
Definition: aac.h:90
SingleChannelElement::pns_ener
float pns_ener[128]
Noise energy values.
Definition: aacenc.h:137
ZERO
#define ZERO
Definition: aap_template.c:32
AAC_CUTOFF_FROM_BITRATE
#define AAC_CUTOFF_FROM_BITRATE(bit_rate, channels, sample_rate)
Definition: psymodel.h:35
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:65
search_for_quantizers_anmr
static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
Definition: aaccoder.c:458
SingleChannelElement
Single Channel Element - used for both SCE and LFE elements.
Definition: aacdec.h:210
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
IndividualChannelStream::num_windows
int num_windows
Definition: aacdec.h:172
SCALE_ONE_POS
#define SCALE_ONE_POS
scalefactor index that corresponds to scale=1.0
Definition: aac.h:88
find_min_book
static int find_min_book(float maxval, int sf)
Definition: aacenc_utils.h:68
aac_cb_in_map
static const uint8_t aac_cb_in_map[CB_TOT_ALL+1]
Inverse map to convert from codebooks to BandCodingPath indices.
Definition: aacenctab.h:118
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:161
ChannelElement
channel element - generic struct for SCE/CPE/CCE/LFE
Definition: aacdec.h:260
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:170
AVCodecContext::cutoff
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:1096
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
cbrtf
static av_always_inline float cbrtf(float x)
Definition: libm.h:61
TrellisPath::prev
int prev
Definition: aaccoder.c:413
set_special_band_scalefactors
static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
Definition: aaccoder.c:419
ff_aac_codebook_vectors
const float *const ff_aac_codebook_vectors[]
Definition: aactab.c:1030
len
int len
Definition: vorbis_enc_data.h:426
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:163
ff_aac_search_for_is
void ff_aac_search_for_is(AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe)
Definition: aacenc_is.c:98
B0
@ B0
Definition: mvs.c:526
avcodec.h
TRELLIS_STAGES
#define TRELLIS_STAGES
Definition: aaccoder.c:416
BandCodingPath
structure used in optimal codebook search
Definition: aaccoder.c:291
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:534
RESERVED_BT
@ RESERVED_BT
Band types following are encoded differently from others.
Definition: aac.h:70
AACEncContext
AAC encoder context.
Definition: aacenc.h:212
FFPsyBand::energy
float energy
Definition: psymodel.h:52
AVCodecContext
main external API structure.
Definition: avcodec.h:451
aacenc_pred.h
ff_aac_encode_main_pred
void ff_aac_encode_main_pred(AACEncContext *s, SingleChannelElement *sce)
Encoder predictors data.
Definition: aacenc_pred.c:332
av_clip_uint8
#define av_clip_uint8
Definition: common.h:106
encode_window_bands_info
static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce, int win, int group_len, const float lambda)
Encode band info for single window group bands.
Definition: aaccoder.c:300
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:292
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:291
ff_aac_search_for_pred
void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce)
Definition: aacenc_pred.c:233
AACEncContext::pb
PutBitContext pb
Definition: aacenc.h:215
TRELLIS_STATES
#define TRELLIS_STATES
Definition: aaccoder.c:417
Q
#define Q(x)
Definition: filter_template.c:423
IndividualChannelStream::max_sfb
uint8_t max_sfb
number of scalefactor bands per group
Definition: aacdec.h:163
ff_aac_search_for_ltp
void ff_aac_search_for_ltp(AACEncContext *s, SingleChannelElement *sce, int common_window)
Mark LTP sfb's.
Definition: aacenc_ltp.c:159
TrellisPath::cost
float cost
Definition: aaccoder.c:412
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:238
put_bits.h
IndividualChannelStream::group_len
uint8_t group_len[8]
Definition: aacdec.h:168
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
BandCodingPath::run
int run
Definition: aaccoder.c:294
ff_quantize_band_cost_cache_init
void ff_quantize_band_cost_cache_init(struct AACEncContext *s)
Definition: aacenc.c:401
aaccoder_trellis.h
NOISE_LAMBDA_REPLACE
#define NOISE_LAMBDA_REPLACE
Definition: aaccoder.c:61
aacenc.h
BandCodingPath::cost
float cost
path cost
Definition: aaccoder.c:293