FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include "libavutil/mathematics.h"
37 #include "avcodec.h"
38 #include "put_bits.h"
39 #include "aac.h"
40 #include "aacenc.h"
41 #include "aactab.h"
42 
43 /** Frequency in Hz for lower limit of noise substitution **/
44 #define NOISE_LOW_LIMIT 4000
45 
46 /** Total number of usable codebooks **/
47 #define CB_TOT 13
48 
49 /** bits needed to code codebook run value for long windows */
50 static const uint8_t run_value_bits_long[64] = {
51  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
52  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
53  10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
54  10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
55 };
56 
57 /** bits needed to code codebook run value for short windows */
58 static const uint8_t run_value_bits_short[16] = {
59  3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
60 };
61 
62 static const uint8_t * const run_value_bits[2] = {
64 };
65 
66 /** Map to convert values from BandCodingPath index to a codebook index **/
67 static const uint8_t aac_cb_out_map[CB_TOT] = {0,1,2,3,4,5,6,7,8,9,10,11,13};
68 /** Inverse map to convert from codebooks to BandCodingPath indices **/
69 static const uint8_t aac_cb_in_map[CB_TOT+1] = {0,1,2,3,4,5,6,7,8,9,10,11,0,12};
70 
71 /**
72  * Quantize one coefficient.
73  * @return absolute value of the quantized coefficient
74  * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
75  */
76 static av_always_inline int quant(float coef, const float Q)
77 {
78  float a = coef * Q;
79  return sqrtf(a * sqrtf(a)) + 0.4054;
80 }
81 
82 static void quantize_bands(int *out, const float *in, const float *scaled,
83  int size, float Q34, int is_signed, int maxval)
84 {
85  int i;
86  double qc;
87  for (i = 0; i < size; i++) {
88  qc = scaled[i] * Q34;
89  out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
90  if (is_signed && in[i] < 0.0f) {
91  out[i] = -out[i];
92  }
93  }
94 }
95 
96 static void abs_pow34_v(float *out, const float *in, const int size)
97 {
98 #ifndef USE_REALLY_FULL_SEARCH
99  int i;
100  for (i = 0; i < size; i++) {
101  float a = fabsf(in[i]);
102  out[i] = sqrtf(a * sqrtf(a));
103  }
104 #endif /* USE_REALLY_FULL_SEARCH */
105 }
106 
107 static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
108 static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
109 
110 /**
111  * Calculate rate distortion cost for quantizing with given codebook
112  *
113  * @return quantization distortion
114  */
116  struct AACEncContext *s,
117  PutBitContext *pb, const float *in,
118  const float *scaled, int size, int scale_idx,
119  int cb, const float lambda, const float uplim,
120  int *bits, int BT_ZERO, int BT_UNSIGNED,
121  int BT_PAIR, int BT_ESC, int BT_NOISE)
122 {
123  const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
124  const float Q = ff_aac_pow2sf_tab [q_idx];
125  const float Q34 = ff_aac_pow34sf_tab[q_idx];
126  const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
127  const float CLIPPED_ESCAPE = 165140.0f*IQ;
128  int i, j;
129  float cost = 0;
130  const int dim = BT_PAIR ? 2 : 4;
131  int resbits = 0;
132  int off;
133 
134  if (BT_ZERO) {
135  for (i = 0; i < size; i++)
136  cost += in[i]*in[i];
137  if (bits)
138  *bits = 0;
139  return cost * lambda;
140  }
141  if (BT_NOISE) {
142  for (i = 0; i < size; i++)
143  cost += in[i]*in[i];
144  if (bits)
145  *bits = 0;
146  return cost * lambda;
147  }
148  if (!scaled) {
149  abs_pow34_v(s->scoefs, in, size);
150  scaled = s->scoefs;
151  }
152  quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, aac_cb_maxval[cb]);
153  if (BT_UNSIGNED) {
154  off = 0;
155  } else {
156  off = aac_cb_maxval[cb];
157  }
158  for (i = 0; i < size; i += dim) {
159  const float *vec;
160  int *quants = s->qcoefs + i;
161  int curidx = 0;
162  int curbits;
163  float rd = 0.0f;
164  for (j = 0; j < dim; j++) {
165  curidx *= aac_cb_range[cb];
166  curidx += quants[j] + off;
167  }
168  curbits = ff_aac_spectral_bits[cb-1][curidx];
169  vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
170  if (BT_UNSIGNED) {
171  for (j = 0; j < dim; j++) {
172  float t = fabsf(in[i+j]);
173  float di;
174  if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
175  if (t >= CLIPPED_ESCAPE) {
176  di = t - CLIPPED_ESCAPE;
177  curbits += 21;
178  } else {
179  int c = av_clip_uintp2(quant(t, Q), 13);
180  di = t - c*cbrtf(c)*IQ;
181  curbits += av_log2(c)*2 - 4 + 1;
182  }
183  } else {
184  di = t - vec[j]*IQ;
185  }
186  if (vec[j] != 0.0f)
187  curbits++;
188  rd += di*di;
189  }
190  } else {
191  for (j = 0; j < dim; j++) {
192  float di = in[i+j] - vec[j]*IQ;
193  rd += di*di;
194  }
195  }
196  cost += rd * lambda + curbits;
197  resbits += curbits;
198  if (cost >= uplim)
199  return uplim;
200  if (pb) {
201  put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
202  if (BT_UNSIGNED)
203  for (j = 0; j < dim; j++)
204  if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
205  put_bits(pb, 1, in[i+j] < 0.0f);
206  if (BT_ESC) {
207  for (j = 0; j < 2; j++) {
208  if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
209  int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q), 13);
210  int len = av_log2(coef);
211 
212  put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
213  put_sbits(pb, len, coef);
214  }
215  }
216  }
217  }
218  }
219 
220  if (bits)
221  *bits = resbits;
222  return cost;
223 }
224 
226  const float *in, const float *scaled,
227  int size, int scale_idx, int cb,
228  const float lambda, const float uplim,
229  int *bits) {
230  av_assert0(0);
231  return 0.0f;
232 }
233 
234 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE) \
235 static float quantize_and_encode_band_cost_ ## NAME( \
236  struct AACEncContext *s, \
237  PutBitContext *pb, const float *in, \
238  const float *scaled, int size, int scale_idx, \
239  int cb, const float lambda, const float uplim, \
240  int *bits) { \
241  return quantize_and_encode_band_cost_template( \
242  s, pb, in, scaled, size, scale_idx, \
243  BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
244  BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE); \
245 }
246 
247 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0)
248 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0)
249 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0)
250 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0)
251 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0)
252 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0)
253 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1)
254 
255 static float (*const quantize_and_encode_band_cost_arr[])(
256  struct AACEncContext *s,
257  PutBitContext *pb, const float *in,
258  const float *scaled, int size, int scale_idx,
259  int cb, const float lambda, const float uplim,
260  int *bits) = {
261  quantize_and_encode_band_cost_ZERO,
262  quantize_and_encode_band_cost_SQUAD,
263  quantize_and_encode_band_cost_SQUAD,
264  quantize_and_encode_band_cost_UQUAD,
265  quantize_and_encode_band_cost_UQUAD,
266  quantize_and_encode_band_cost_SPAIR,
267  quantize_and_encode_band_cost_SPAIR,
268  quantize_and_encode_band_cost_UPAIR,
269  quantize_and_encode_band_cost_UPAIR,
270  quantize_and_encode_band_cost_UPAIR,
271  quantize_and_encode_band_cost_UPAIR,
272  quantize_and_encode_band_cost_ESC,
273  quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
274  quantize_and_encode_band_cost_NOISE,
275 };
276 
277 #define quantize_and_encode_band_cost( \
278  s, pb, in, scaled, size, scale_idx, cb, \
279  lambda, uplim, bits) \
280  quantize_and_encode_band_cost_arr[cb]( \
281  s, pb, in, scaled, size, scale_idx, cb, \
282  lambda, uplim, bits)
283 
284 static float quantize_band_cost(struct AACEncContext *s, const float *in,
285  const float *scaled, int size, int scale_idx,
286  int cb, const float lambda, const float uplim,
287  int *bits)
288 {
289  return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
290  cb, lambda, uplim, bits);
291 }
292 
294  const float *in, int size, int scale_idx,
295  int cb, const float lambda)
296 {
297  quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
298  INFINITY, NULL);
299 }
300 
301 static float find_max_val(int group_len, int swb_size, const float *scaled) {
302  float maxval = 0.0f;
303  int w2, i;
304  for (w2 = 0; w2 < group_len; w2++) {
305  for (i = 0; i < swb_size; i++) {
306  maxval = FFMAX(maxval, scaled[w2*128+i]);
307  }
308  }
309  return maxval;
310 }
311 
312 static int find_min_book(float maxval, int sf) {
314  float Q34 = sqrtf(Q * sqrtf(Q));
315  int qmaxval, cb;
316  qmaxval = maxval * Q34 + 0.4054f;
317  if (qmaxval == 0) cb = 0;
318  else if (qmaxval == 1) cb = 1;
319  else if (qmaxval == 2) cb = 3;
320  else if (qmaxval <= 4) cb = 5;
321  else if (qmaxval <= 7) cb = 7;
322  else if (qmaxval <= 12) cb = 9;
323  else cb = 11;
324  return cb;
325 }
326 
327 /**
328  * structure used in optimal codebook search
329  */
330 typedef struct BandCodingPath {
331  int prev_idx; ///< pointer to the previous path point
332  float cost; ///< path cost
333  int run;
335 
336 /**
337  * Encode band info for single window group bands.
338  */
340  int win, int group_len, const float lambda)
341 {
342  BandCodingPath path[120][CB_TOT];
343  int w, swb, cb, start, size;
344  int i, j;
345  const int max_sfb = sce->ics.max_sfb;
346  const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
347  const int run_esc = (1 << run_bits) - 1;
348  int idx, ppos, count;
349  int stackrun[120], stackcb[120], stack_len;
350  float next_minrd = INFINITY;
351  int next_mincb = 0;
352 
353  abs_pow34_v(s->scoefs, sce->coeffs, 1024);
354  start = win*128;
355  for (cb = 0; cb < CB_TOT; cb++) {
356  path[0][cb].cost = 0.0f;
357  path[0][cb].prev_idx = -1;
358  path[0][cb].run = 0;
359  }
360  for (swb = 0; swb < max_sfb; swb++) {
361  size = sce->ics.swb_sizes[swb];
362  if (sce->zeroes[win*16 + swb]) {
363  for (cb = 0; cb < CB_TOT; cb++) {
364  path[swb+1][cb].prev_idx = cb;
365  path[swb+1][cb].cost = path[swb][cb].cost;
366  path[swb+1][cb].run = path[swb][cb].run + 1;
367  }
368  } else {
369  float minrd = next_minrd;
370  int mincb = next_mincb;
371  next_minrd = INFINITY;
372  next_mincb = 0;
373  for (cb = 0; cb < CB_TOT; cb++) {
374  float cost_stay_here, cost_get_here;
375  float rd = 0.0f;
376  for (w = 0; w < group_len; w++) {
377  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb];
378  rd += quantize_band_cost(s, sce->coeffs + start + w*128,
379  s->scoefs + start + w*128, size,
380  sce->sf_idx[(win+w)*16+swb], aac_cb_out_map[cb],
381  lambda / band->threshold, INFINITY, NULL);
382  }
383  cost_stay_here = path[swb][cb].cost + rd;
384  cost_get_here = minrd + rd + run_bits + 4;
385  if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
386  != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
387  cost_stay_here += run_bits;
388  if (cost_get_here < cost_stay_here) {
389  path[swb+1][cb].prev_idx = mincb;
390  path[swb+1][cb].cost = cost_get_here;
391  path[swb+1][cb].run = 1;
392  } else {
393  path[swb+1][cb].prev_idx = cb;
394  path[swb+1][cb].cost = cost_stay_here;
395  path[swb+1][cb].run = path[swb][cb].run + 1;
396  }
397  if (path[swb+1][cb].cost < next_minrd) {
398  next_minrd = path[swb+1][cb].cost;
399  next_mincb = cb;
400  }
401  }
402  }
403  start += sce->ics.swb_sizes[swb];
404  }
405 
406  //convert resulting path from backward-linked list
407  stack_len = 0;
408  idx = 0;
409  for (cb = 1; cb < CB_TOT; cb++)
410  if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
411  idx = cb;
412  ppos = max_sfb;
413  while (ppos > 0) {
414  cb = idx;
415  stackrun[stack_len] = path[ppos][cb].run;
416  stackcb [stack_len] = cb;
417  idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
418  ppos -= path[ppos][cb].run;
419  stack_len++;
420  }
421  //perform actual band info encoding
422  start = 0;
423  for (i = stack_len - 1; i >= 0; i--) {
424  cb = aac_cb_out_map[stackcb[i]];
425  put_bits(&s->pb, 4, cb);
426  count = stackrun[i];
427  memset(sce->zeroes + win*16 + start, !cb, count);
428  //XXX: memset when band_type is also uint8_t
429  for (j = 0; j < count; j++) {
430  sce->band_type[win*16 + start] = cb;
431  start++;
432  }
433  while (count >= run_esc) {
434  put_bits(&s->pb, run_bits, run_esc);
435  count -= run_esc;
436  }
437  put_bits(&s->pb, run_bits, count);
438  }
439 }
440 
442  int win, int group_len, const float lambda)
443 {
444  BandCodingPath path[120][CB_TOT];
445  int w, swb, cb, start, size;
446  int i, j;
447  const int max_sfb = sce->ics.max_sfb;
448  const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
449  const int run_esc = (1 << run_bits) - 1;
450  int idx, ppos, count;
451  int stackrun[120], stackcb[120], stack_len;
452  float next_minbits = INFINITY;
453  int next_mincb = 0;
454 
455  abs_pow34_v(s->scoefs, sce->coeffs, 1024);
456  start = win*128;
457  for (cb = 0; cb < CB_TOT; cb++) {
458  path[0][cb].cost = run_bits+4;
459  path[0][cb].prev_idx = -1;
460  path[0][cb].run = 0;
461  }
462  for (swb = 0; swb < max_sfb; swb++) {
463  size = sce->ics.swb_sizes[swb];
464  if (sce->zeroes[win*16 + swb]) {
465  float cost_stay_here = path[swb][0].cost;
466  float cost_get_here = next_minbits + run_bits + 4;
467  if ( run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
468  != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
469  cost_stay_here += run_bits;
470  if (cost_get_here < cost_stay_here) {
471  path[swb+1][0].prev_idx = next_mincb;
472  path[swb+1][0].cost = cost_get_here;
473  path[swb+1][0].run = 1;
474  } else {
475  path[swb+1][0].prev_idx = 0;
476  path[swb+1][0].cost = cost_stay_here;
477  path[swb+1][0].run = path[swb][0].run + 1;
478  }
479  next_minbits = path[swb+1][0].cost;
480  next_mincb = 0;
481  for (cb = 1; cb < CB_TOT; cb++) {
482  path[swb+1][cb].cost = 61450;
483  path[swb+1][cb].prev_idx = -1;
484  path[swb+1][cb].run = 0;
485  }
486  } else {
487  float minbits = next_minbits;
488  int mincb = next_mincb;
489  int startcb = sce->band_type[win*16+swb];
490  startcb = aac_cb_in_map[startcb];
491  next_minbits = INFINITY;
492  next_mincb = 0;
493  for (cb = 0; cb < startcb; cb++) {
494  path[swb+1][cb].cost = 61450;
495  path[swb+1][cb].prev_idx = -1;
496  path[swb+1][cb].run = 0;
497  }
498  for (cb = startcb; cb < CB_TOT; cb++) {
499  float cost_stay_here, cost_get_here;
500  float bits = 0.0f;
501  if (cb == 12 && sce->band_type[win*16+swb] != NOISE_BT) {
502  path[swb+1][cb].cost = 61450;
503  path[swb+1][cb].prev_idx = -1;
504  path[swb+1][cb].run = 0;
505  continue;
506  }
507  for (w = 0; w < group_len; w++) {
508  bits += quantize_band_cost(s, sce->coeffs + start + w*128,
509  s->scoefs + start + w*128, size,
510  sce->sf_idx[(win+w)*16+swb],
511  aac_cb_out_map[cb],
512  0, INFINITY, NULL);
513  }
514  cost_stay_here = path[swb][cb].cost + bits;
515  cost_get_here = minbits + bits + run_bits + 4;
516  if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
517  != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
518  cost_stay_here += run_bits;
519  if (cost_get_here < cost_stay_here) {
520  path[swb+1][cb].prev_idx = mincb;
521  path[swb+1][cb].cost = cost_get_here;
522  path[swb+1][cb].run = 1;
523  } else {
524  path[swb+1][cb].prev_idx = cb;
525  path[swb+1][cb].cost = cost_stay_here;
526  path[swb+1][cb].run = path[swb][cb].run + 1;
527  }
528  if (path[swb+1][cb].cost < next_minbits) {
529  next_minbits = path[swb+1][cb].cost;
530  next_mincb = cb;
531  }
532  }
533  }
534  start += sce->ics.swb_sizes[swb];
535  }
536 
537  //convert resulting path from backward-linked list
538  stack_len = 0;
539  idx = 0;
540  for (cb = 1; cb < CB_TOT; cb++)
541  if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
542  idx = cb;
543  ppos = max_sfb;
544  while (ppos > 0) {
545  av_assert1(idx >= 0);
546  cb = idx;
547  stackrun[stack_len] = path[ppos][cb].run;
548  stackcb [stack_len] = cb;
549  idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
550  ppos -= path[ppos][cb].run;
551  stack_len++;
552  }
553  //perform actual band info encoding
554  start = 0;
555  for (i = stack_len - 1; i >= 0; i--) {
556  cb = aac_cb_out_map[stackcb[i]];
557  put_bits(&s->pb, 4, cb);
558  count = stackrun[i];
559  memset(sce->zeroes + win*16 + start, !cb, count);
560  //XXX: memset when band_type is also uint8_t
561  for (j = 0; j < count; j++) {
562  sce->band_type[win*16 + start] = cb;
563  start++;
564  }
565  while (count >= run_esc) {
566  put_bits(&s->pb, run_bits, run_esc);
567  count -= run_esc;
568  }
569  put_bits(&s->pb, run_bits, count);
570  }
571 }
572 
573 /** Return the minimum scalefactor where the quantized coef does not clip. */
574 static av_always_inline uint8_t coef2minsf(float coef) {
575  return av_clip_uint8(log2f(coef)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
576 }
577 
578 /** Return the maximum scalefactor where the quantized coef is not zero. */
579 static av_always_inline uint8_t coef2maxsf(float coef) {
580  return av_clip_uint8(log2f(coef)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
581 }
582 
583 typedef struct TrellisPath {
584  float cost;
585  int prev;
586 } TrellisPath;
587 
588 #define TRELLIS_STAGES 121
589 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
590 
593  const float lambda)
594 {
595  int q, w, w2, g, start = 0;
596  int i, j;
597  int idx;
599  int bandaddr[TRELLIS_STAGES];
600  int minq;
601  float mincost;
602  float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
603  int q0, q1, qcnt = 0;
604 
605  for (i = 0; i < 1024; i++) {
606  float t = fabsf(sce->coeffs[i]);
607  if (t > 0.0f) {
608  q0f = FFMIN(q0f, t);
609  q1f = FFMAX(q1f, t);
610  qnrgf += t*t;
611  qcnt++;
612  }
613  }
614 
615  if (!qcnt) {
616  memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
617  memset(sce->zeroes, 1, sizeof(sce->zeroes));
618  return;
619  }
620 
621  //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
622  q0 = coef2minsf(q0f);
623  //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
624  q1 = coef2maxsf(q1f);
625  if (q1 - q0 > 60) {
626  int q0low = q0;
627  int q1high = q1;
628  //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
629  int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
630  q1 = qnrg + 30;
631  q0 = qnrg - 30;
632  if (q0 < q0low) {
633  q1 += q0low - q0;
634  q0 = q0low;
635  } else if (q1 > q1high) {
636  q0 -= q1 - q1high;
637  q1 = q1high;
638  }
639  }
640 
641  for (i = 0; i < TRELLIS_STATES; i++) {
642  paths[0][i].cost = 0.0f;
643  paths[0][i].prev = -1;
644  }
645  for (j = 1; j < TRELLIS_STAGES; j++) {
646  for (i = 0; i < TRELLIS_STATES; i++) {
647  paths[j][i].cost = INFINITY;
648  paths[j][i].prev = -2;
649  }
650  }
651  idx = 1;
652  abs_pow34_v(s->scoefs, sce->coeffs, 1024);
653  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
654  start = w*128;
655  for (g = 0; g < sce->ics.num_swb; g++) {
656  const float *coefs = sce->coeffs + start;
657  float qmin, qmax;
658  int nz = 0;
659 
660  bandaddr[idx] = w * 16 + g;
661  qmin = INT_MAX;
662  qmax = 0.0f;
663  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
664  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
665  if (band->energy <= band->threshold || band->threshold == 0.0f) {
666  sce->zeroes[(w+w2)*16+g] = 1;
667  continue;
668  }
669  sce->zeroes[(w+w2)*16+g] = 0;
670  nz = 1;
671  for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
672  float t = fabsf(coefs[w2*128+i]);
673  if (t > 0.0f)
674  qmin = FFMIN(qmin, t);
675  qmax = FFMAX(qmax, t);
676  }
677  }
678  if (nz) {
679  int minscale, maxscale;
680  float minrd = INFINITY;
681  float maxval;
682  //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
683  minscale = coef2minsf(qmin);
684  //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
685  maxscale = coef2maxsf(qmax);
686  minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
687  maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
688  maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
689  for (q = minscale; q < maxscale; q++) {
690  float dist = 0;
691  int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
692  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
693  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
694  dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
695  q + q0, cb, lambda / band->threshold, INFINITY, NULL);
696  }
697  minrd = FFMIN(minrd, dist);
698 
699  for (i = 0; i < q1 - q0; i++) {
700  float cost;
701  cost = paths[idx - 1][i].cost + dist
703  if (cost < paths[idx][q].cost) {
704  paths[idx][q].cost = cost;
705  paths[idx][q].prev = i;
706  }
707  }
708  }
709  } else {
710  for (q = 0; q < q1 - q0; q++) {
711  paths[idx][q].cost = paths[idx - 1][q].cost + 1;
712  paths[idx][q].prev = q;
713  }
714  }
715  sce->zeroes[w*16+g] = !nz;
716  start += sce->ics.swb_sizes[g];
717  idx++;
718  }
719  }
720  idx--;
721  mincost = paths[idx][0].cost;
722  minq = 0;
723  for (i = 1; i < TRELLIS_STATES; i++) {
724  if (paths[idx][i].cost < mincost) {
725  mincost = paths[idx][i].cost;
726  minq = i;
727  }
728  }
729  while (idx) {
730  sce->sf_idx[bandaddr[idx]] = minq + q0;
731  minq = paths[idx][minq].prev;
732  idx--;
733  }
734  //set the same quantizers inside window groups
735  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
736  for (g = 0; g < sce->ics.num_swb; g++)
737  for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
738  sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
739 }
740 
741 /**
742  * two-loop quantizers search taken from ISO 13818-7 Appendix C
743  */
745  AACEncContext *s,
747  const float lambda)
748 {
749  int start = 0, i, w, w2, g;
750  int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels * (lambda / 120.f);
751  const float freq_mult = avctx->sample_rate/(1024.0f/sce->ics.num_windows)/2.0f;
752  float dists[128] = { 0 }, uplims[128] = { 0 };
753  float maxvals[128];
754  int noise_sf[128] = { 0 };
755  int fflag, minscaler, minscaler_n;
756  int its = 0;
757  int allz = 0;
758  float minthr = INFINITY;
759 
760  // for values above this the decoder might end up in an endless loop
761  // due to always having more bits than what can be encoded.
762  destbits = FFMIN(destbits, 5800);
763  //XXX: some heuristic to determine initial quantizers will reduce search time
764  //determine zero bands and upper limits
765  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
766  start = 0;
767  for (g = 0; g < sce->ics.num_swb; g++) {
768  int nz = 0;
769  float uplim = 0.0f, energy = 0.0f;
770  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
771  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
772  uplim += band->threshold;
773  energy += band->energy;
774  if (band->energy <= band->threshold || band->threshold == 0.0f) {
775  sce->zeroes[(w+w2)*16+g] = 1;
776  continue;
777  }
778  nz = 1;
779  }
780  uplims[w*16+g] = uplim *512;
781  if (s->options.pns && start*freq_mult > NOISE_LOW_LIMIT && energy < uplim * 1.2f) {
782  noise_sf[w*16+g] = av_clip(4+FFMIN(log2f(energy)*2,255), -100, 155);
783  sce->band_type[w*16+g] = NOISE_BT;
784  nz= 1;
785  } else { /** Band type will be determined by the twoloop algorithm */
786  sce->band_type[w*16+g] = 0;
787  }
788  sce->zeroes[w*16+g] = !nz;
789  if (nz)
790  minthr = FFMIN(minthr, uplim);
791  allz |= nz;
792  start += sce->ics.swb_sizes[g];
793  }
794  }
795  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
796  for (g = 0; g < sce->ics.num_swb; g++) {
797  if (sce->zeroes[w*16+g]) {
798  sce->sf_idx[w*16+g] = SCALE_ONE_POS;
799  continue;
800  }
801  sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
802  }
803  }
804 
805  if (!allz)
806  return;
807  abs_pow34_v(s->scoefs, sce->coeffs, 1024);
808 
809  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
810  start = w*128;
811  for (g = 0; g < sce->ics.num_swb; g++) {
812  const float *scaled = s->scoefs + start;
813  maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
814  start += sce->ics.swb_sizes[g];
815  }
816  }
817 
818  //perform two-loop search
819  //outer loop - improve quality
820  do {
821  int tbits, qstep;
822  minscaler = sce->sf_idx[0];
823  minscaler_n = sce->sf_idx[0];
824  //inner loop - quantize spectrum to fit into given number of bits
825  qstep = its ? 1 : 32;
826  do {
827  int prev = -1;
828  tbits = 0;
829  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
830  start = w*128;
831  for (g = 0; g < sce->ics.num_swb; g++) {
832  const float *coefs = sce->coeffs + start;
833  const float *scaled = s->scoefs + start;
834  int bits = 0;
835  int cb;
836  float dist = 0.0f;
837 
838  if (sce->band_type[w*16+g] == NOISE_BT) {
839  minscaler_n = FFMIN(minscaler_n, noise_sf[w*16+g]);
840  start += sce->ics.swb_sizes[g];
841  continue;
842  } else if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
843  start += sce->ics.swb_sizes[g];
844  continue;
845  }
846  minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
847  cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
848  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
849  int b;
850  dist += quantize_band_cost(s, coefs + w2*128,
851  scaled + w2*128,
852  sce->ics.swb_sizes[g],
853  sce->sf_idx[w*16+g],
854  cb,
855  1.0f,
856  INFINITY,
857  &b);
858  bits += b;
859  }
860  dists[w*16+g] = dist - bits;
861  if (prev != -1) {
862  bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
863  }
864  tbits += bits;
865  start += sce->ics.swb_sizes[g];
866  prev = sce->sf_idx[w*16+g];
867  }
868  }
869  if (tbits > destbits) {
870  for (i = 0; i < 128; i++)
871  if (sce->sf_idx[i] < 218 - qstep)
872  sce->sf_idx[i] += qstep;
873  } else {
874  for (i = 0; i < 128; i++)
875  if (sce->sf_idx[i] > 60 - qstep)
876  sce->sf_idx[i] -= qstep;
877  }
878  qstep >>= 1;
879  if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
880  qstep = 1;
881  } while (qstep);
882 
883  fflag = 0;
884  minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
885 
886  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
887  for (g = 0; g < sce->ics.num_swb; g++)
888  if (sce->band_type[w*16+g] == NOISE_BT)
889  sce->sf_idx[w*16+g] = av_clip(noise_sf[w*16+g], minscaler_n, minscaler_n + SCALE_MAX_DIFF);
890 
891  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
892  for (g = 0; g < sce->ics.num_swb; g++) {
893  int prevsc = sce->sf_idx[w*16+g];
894  if (sce->band_type[w*16+g] == NOISE_BT)
895  continue;
896  if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
897  if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
898  sce->sf_idx[w*16+g]--;
899  else //Try to make sure there is some energy in every band
900  sce->sf_idx[w*16+g]-=2;
901  }
902  sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
903  sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
904  if (sce->sf_idx[w*16+g] != prevsc)
905  fflag = 1;
906  sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
907  }
908  }
909  its++;
910  } while (fflag && its < 10);
911 }
912 
915  const float lambda)
916 {
917  int start = 0, i, w, w2, g;
918  float uplim[128], maxq[128];
919  int minq, maxsf;
920  float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
921  int last = 0, lastband = 0, curband = 0;
922  float avg_energy = 0.0;
923  if (sce->ics.num_windows == 1) {
924  start = 0;
925  for (i = 0; i < 1024; i++) {
926  if (i - start >= sce->ics.swb_sizes[curband]) {
927  start += sce->ics.swb_sizes[curband];
928  curband++;
929  }
930  if (sce->coeffs[i]) {
931  avg_energy += sce->coeffs[i] * sce->coeffs[i];
932  last = i;
933  lastband = curband;
934  }
935  }
936  } else {
937  for (w = 0; w < 8; w++) {
938  const float *coeffs = sce->coeffs + w*128;
939  curband = start = 0;
940  for (i = 0; i < 128; i++) {
941  if (i - start >= sce->ics.swb_sizes[curband]) {
942  start += sce->ics.swb_sizes[curband];
943  curband++;
944  }
945  if (coeffs[i]) {
946  avg_energy += coeffs[i] * coeffs[i];
947  last = FFMAX(last, i);
948  lastband = FFMAX(lastband, curband);
949  }
950  }
951  }
952  }
953  last++;
954  avg_energy /= last;
955  if (avg_energy == 0.0f) {
956  for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
957  sce->sf_idx[i] = SCALE_ONE_POS;
958  return;
959  }
960  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
961  start = w*128;
962  for (g = 0; g < sce->ics.num_swb; g++) {
963  float *coefs = sce->coeffs + start;
964  const int size = sce->ics.swb_sizes[g];
965  int start2 = start, end2 = start + size, peakpos = start;
966  float maxval = -1, thr = 0.0f, t;
967  maxq[w*16+g] = 0.0f;
968  if (g > lastband) {
969  maxq[w*16+g] = 0.0f;
970  start += size;
971  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
972  memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
973  continue;
974  }
975  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
976  for (i = 0; i < size; i++) {
977  float t = coefs[w2*128+i]*coefs[w2*128+i];
978  maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
979  thr += t;
980  if (sce->ics.num_windows == 1 && maxval < t) {
981  maxval = t;
982  peakpos = start+i;
983  }
984  }
985  }
986  if (sce->ics.num_windows == 1) {
987  start2 = FFMAX(peakpos - 2, start2);
988  end2 = FFMIN(peakpos + 3, end2);
989  } else {
990  start2 -= start;
991  end2 -= start;
992  }
993  start += size;
994  thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
995  t = 1.0 - (1.0 * start2 / last);
996  uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
997  }
998  }
999  memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
1000  abs_pow34_v(s->scoefs, sce->coeffs, 1024);
1001  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
1002  start = w*128;
1003  for (g = 0; g < sce->ics.num_swb; g++) {
1004  const float *coefs = sce->coeffs + start;
1005  const float *scaled = s->scoefs + start;
1006  const int size = sce->ics.swb_sizes[g];
1007  int scf, prev_scf, step;
1008  int min_scf = -1, max_scf = 256;
1009  float curdiff;
1010  if (maxq[w*16+g] < 21.544) {
1011  sce->zeroes[w*16+g] = 1;
1012  start += size;
1013  continue;
1014  }
1015  sce->zeroes[w*16+g] = 0;
1016  scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
1017  for (;;) {
1018  float dist = 0.0f;
1019  int quant_max;
1020 
1021  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
1022  int b;
1023  dist += quantize_band_cost(s, coefs + w2*128,
1024  scaled + w2*128,
1025  sce->ics.swb_sizes[g],
1026  scf,
1027  ESC_BT,
1028  lambda,
1029  INFINITY,
1030  &b);
1031  dist -= b;
1032  }
1033  dist *= 1.0f / 512.0f / lambda;
1034  quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
1035  if (quant_max >= 8191) { // too much, return to the previous quantizer
1036  sce->sf_idx[w*16+g] = prev_scf;
1037  break;
1038  }
1039  prev_scf = scf;
1040  curdiff = fabsf(dist - uplim[w*16+g]);
1041  if (curdiff <= 1.0f)
1042  step = 0;
1043  else
1044  step = log2f(curdiff);
1045  if (dist > uplim[w*16+g])
1046  step = -step;
1047  scf += step;
1048  scf = av_clip_uint8(scf);
1049  step = scf - prev_scf;
1050  if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
1051  sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
1052  break;
1053  }
1054  if (step > 0)
1055  min_scf = prev_scf;
1056  else
1057  max_scf = prev_scf;
1058  }
1059  start += size;
1060  }
1061  }
1062  minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
1063  for (i = 1; i < 128; i++) {
1064  if (!sce->sf_idx[i])
1065  sce->sf_idx[i] = sce->sf_idx[i-1];
1066  else
1067  minq = FFMIN(minq, sce->sf_idx[i]);
1068  }
1069  if (minq == INT_MAX)
1070  minq = 0;
1071  minq = FFMIN(minq, SCALE_MAX_POS);
1072  maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
1073  for (i = 126; i >= 0; i--) {
1074  if (!sce->sf_idx[i])
1075  sce->sf_idx[i] = sce->sf_idx[i+1];
1076  sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
1077  }
1078 }
1079 
1081  SingleChannelElement *sce,
1082  const float lambda)
1083 {
1084  int i, w, w2, g;
1085  int minq = 255;
1086 
1087  memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
1088  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
1089  for (g = 0; g < sce->ics.num_swb; g++) {
1090  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
1091  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
1092  if (band->energy <= band->threshold) {
1093  sce->sf_idx[(w+w2)*16+g] = 218;
1094  sce->zeroes[(w+w2)*16+g] = 1;
1095  } else {
1096  sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
1097  sce->zeroes[(w+w2)*16+g] = 0;
1098  }
1099  minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
1100  }
1101  }
1102  }
1103  for (i = 0; i < 128; i++) {
1104  sce->sf_idx[i] = 140;
1105  //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
1106  }
1107  //set the same quantizers inside window groups
1108  for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
1109  for (g = 0; g < sce->ics.num_swb; g++)
1110  for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
1111  sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
1112 }
1113 
1115  const float lambda)
1116 {
1117  int start = 0, i, w, w2, g;
1118  float M[128], S[128];
1119  float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
1120  SingleChannelElement *sce0 = &cpe->ch[0];
1121  SingleChannelElement *sce1 = &cpe->ch[1];
1122  if (!cpe->common_window)
1123  return;
1124  for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
1125  for (g = 0; g < sce0->ics.num_swb; g++) {
1126  if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
1127  float dist1 = 0.0f, dist2 = 0.0f;
1128  for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
1129  FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
1130  FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
1131  float minthr = FFMIN(band0->threshold, band1->threshold);
1132  float maxthr = FFMAX(band0->threshold, band1->threshold);
1133  for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1134  M[i] = (sce0->pcoeffs[start+w2*128+i]
1135  + sce1->pcoeffs[start+w2*128+i]) * 0.5;
1136  S[i] = M[i]
1137  - sce1->pcoeffs[start+w2*128+i];
1138  }
1139  abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1140  abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1141  abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
1142  abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
1143  dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
1144  L34,
1145  sce0->ics.swb_sizes[g],
1146  sce0->sf_idx[(w+w2)*16+g],
1147  sce0->band_type[(w+w2)*16+g],
1148  lambda / band0->threshold, INFINITY, NULL);
1149  dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
1150  R34,
1151  sce1->ics.swb_sizes[g],
1152  sce1->sf_idx[(w+w2)*16+g],
1153  sce1->band_type[(w+w2)*16+g],
1154  lambda / band1->threshold, INFINITY, NULL);
1155  dist2 += quantize_band_cost(s, M,
1156  M34,
1157  sce0->ics.swb_sizes[g],
1158  sce0->sf_idx[(w+w2)*16+g],
1159  sce0->band_type[(w+w2)*16+g],
1160  lambda / maxthr, INFINITY, NULL);
1161  dist2 += quantize_band_cost(s, S,
1162  S34,
1163  sce1->ics.swb_sizes[g],
1164  sce1->sf_idx[(w+w2)*16+g],
1165  sce1->band_type[(w+w2)*16+g],
1166  lambda / minthr, INFINITY, NULL);
1167  }
1168  cpe->ms_mask[w*16+g] = dist2 < dist1;
1169  }
1170  start += sce0->ics.swb_sizes[g];
1171  }
1172  }
1173 }
1174 
1176  [AAC_CODER_FAAC] = {
1180  search_for_ms,
1181  },
1182  [AAC_CODER_ANMR] = {
1186  search_for_ms,
1187  },
1188  [AAC_CODER_TWOLOOP] = {
1192  search_for_ms,
1193  },
1194  [AAC_CODER_FAST] = {
1198  search_for_ms,
1199  },
1200 };
static const uint8_t run_value_bits_long[64]
bits needed to code codebook run value for long windows
Definition: aaccoder.c:50
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB]
Definition: aaccoder.c:1175
static av_always_inline uint8_t coef2maxsf(float coef)
Return the maximum scalefactor where the quantized coef is not zero.
Definition: aaccoder.c:579
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:339
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:192
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
float pcoeffs[1024]
coefficients for IMDCT, pristine
Definition: aac.h:240
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
Definition: aac.h:142
const char * g
Definition: vf_curves.c:108
FFPsyBand psy_bands[PSY_MAX_BANDS]
channel bands information
Definition: psymodel.h:49
static const uint8_t run_value_bits_short[16]
bits needed to code codebook run value for short windows
Definition: aaccoder.c:58
#define SCALE_MAX_POS
scalefactor index maximum value
Definition: aac.h:140
#define TRELLIS_STATES
Definition: aaccoder.c:589
static int find_min_book(float maxval, int sf)
Definition: aaccoder.c:312
#define SCALE_MAX_DIFF
maximum scalefactor difference allowed by standard
Definition: aac.h:141
const char * b
Definition: vf_curves.c:109
static const uint8_t aac_cb_in_map[CB_TOT+1]
Inverse map to convert from codebooks to BandCodingPath indices.
Definition: aaccoder.c:69
int common_window
Set if channels share a common 'IndividualChannelStream' in bitstream.
Definition: aac.h:255
#define quantize_and_encode_band_cost(s, pb, in, scaled, size, scale_idx, cb, lambda, uplim, bits)
Definition: aaccoder.c:277
float cost
Definition: aaccoder.c:584
int prev_idx
pointer to the previous path point
Definition: aaccoder.c:331
#define FF_ARRAY_ELEMS(a)
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
Definition: aac.h:257
static const uint8_t q1[256]
Definition: twofish.c:96
static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
Definition: aaccoder.c:1080
Spectral data are scaled white noise not coded in the bitstream.
Definition: aac.h:79
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:96
AACEncOptions options
encoding options
Definition: aacenc.h:67
#define M(a, b)
Definition: vp3dsp.c:44
AAC encoder context.
Definition: aacenc.h:65
uint8_t bits
Definition: crc.c:295
uint8_t
static void search_for_ms(AACEncContext *s, ChannelElement *cpe, const float lambda)
Definition: aaccoder.c:1114
SingleChannelElement ch[2]
Definition: aac.h:259
static av_always_inline int quant(float coef, const float Q)
Quantize one coefficient.
Definition: aaccoder.c:76
const uint8_t ff_aac_scalefactor_bits[121]
Definition: aactab.c:78
int qcoefs[96]
quantized coefficients
Definition: aacenc.h:86
static const uint8_t run_bits[7][16]
Definition: h264_cavlc.c:229
single band psychoacoustic information
Definition: psymodel.h:37
ptrdiff_t size
Definition: opengl_enc.c:101
static const uint8_t aac_cb_range[12]
Definition: aaccoder.c:107
float coeffs[1024]
coefficients for IMDCT, maybe processed
Definition: aac.h:241
static av_always_inline uint8_t coef2minsf(float coef)
Return the minimum scalefactor where the quantized coef does not clip.
Definition: aaccoder.c:574
#define S(s, c, i)
uint8_t max_sfb
number of scalefactor bands per group
Definition: aac.h:162
float energy
Definition: psymodel.h:39
GLsizei count
Definition: opengl_enc.c:109
int num_swb
number of scalefactor window bands
Definition: aac.h:170
float cost
path cost
Definition: aaccoder.c:332
#define FFMAX(a, b)
Definition: common.h:64
static void quantize_bands(int *out, const float *in, const float *scaled, int size, float Q34, int is_signed, int maxval)
Definition: aaccoder.c:82
Libavcodec external API header.
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)
Definition: aaccoder.c:284
const float *const ff_aac_codebook_vectors[]
Definition: aactab.c:904
static const uint8_t q0[256]
Definition: twofish.c:77
#define NOISE_LOW_LIMIT
Frequency in Hz for lower limit of noise substitution.
Definition: aaccoder.c:44
#define POW_SF2_ZERO
ff_aac_pow2sf_tab index corresponding to pow(2, 0);
#define SCALE_DIV_512
scalefactor difference that corresponds to scale difference in 512 times
Definition: aac.h:138
static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
Definition: aaccoder.c:591
static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, const float *in, int size, int scale_idx, int cb, const float lambda)
Definition: aaccoder.c:293
int bit_rate
the average bitrate
Definition: avcodec.h:1305
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int cur_channel
Definition: aacenc.h:82
const uint8_t *const ff_aac_spectral_bits[11]
Definition: aactab.c:408
#define FFMIN(a, b)
Definition: common.h:66
#define FFABS(a)
Definition: common.h:61
AAC definitions and structures.
static void abs_pow34_v(float *out, const float *in, const int size)
Definition: aaccoder.c:96
PutBitContext pb
Definition: aacenc.h:68
float ff_aac_pow34sf_tab[428]
Definition: aacenc.c:61
#define TRELLIS_STAGES
Definition: aaccoder.c:588
#define INFINITY
Definition: math.h:27
int sample_rate
samples per second
Definition: avcodec.h:1985
static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce, int win, int group_len, const float lambda)
Definition: aaccoder.c:441
main external API structure.
Definition: avcodec.h:1241
#define CB_TOT
Total number of usable codebooks.
Definition: aaccoder.c:47
static const uint8_t aac_cb_out_map[CB_TOT]
Map to convert values from BandCodingPath index to a codebook index.
Definition: aaccoder.c:67
IndividualChannelStream ics
Definition: aac.h:232
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static av_always_inline float cbrtf(float x)
Definition: libm.h:59
structure used in optimal codebook search
Definition: aaccoder.c:330
uint8_t group_len[8]
Definition: aac.h:166
Replacements for frequently missing libm functions.
Spectral data are coded with an escape sequence.
Definition: aac.h:78
const uint8_t * swb_sizes
table of scalefactor band sizes for a particular window
Definition: aac.h:169
int dim
static float(*const quantize_and_encode_band_cost_arr[])(struct AACEncContext *s, PutBitContext *pb, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits)
Definition: aaccoder.c:255
FFPsyContext psy
Definition: aacenc.h:79
uint8_t zeroes[128]
band is not coded (used by encoder)
Definition: aac.h:239
int sf_idx[128]
scalefactor indices (used by encoder)
Definition: aac.h:238
#define SCALE_ONE_POS
scalefactor index that corresponds to scale=1.0
Definition: aac.h:139
static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
Definition: aaccoder.c:913
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.c:744
Single Channel Element - used for both SCE and LFE elements.
Definition: aac.h:231
static const uint8_t aac_cb_maxval[12]
Definition: aaccoder.c:108
#define log2f(x)
Definition: libm.h:127
static double c[64]
static av_always_inline float quantize_and_encode_band_cost_template(struct AACEncContext *s, PutBitContext *pb, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, int BT_ZERO, int BT_UNSIGNED, int BT_PAIR, int BT_ESC, int BT_NOISE)
Calculate rate distortion cost for quantizing with given codebook.
Definition: aaccoder.c:115
float ff_aac_pow2sf_tab[428]
Definition: aac_tablegen.h:32
static float find_max_val(int group_len, int swb_size, const float *scaled)
Definition: aaccoder.c:301
channel element - generic struct for SCE/CPE/CCE/LFE
Definition: aac.h:252
const uint16_t *const ff_aac_spectral_codes[11]
Definition: aactab.c:403
int len
int channels
number of audio channels
Definition: avcodec.h:1986
#define av_log2
Definition: intmath.h:105
FFPsyChannel * ch
single channel information
Definition: psymodel.h:80
enum BandType band_type[128]
band types
Definition: aac.h:235
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits)
Definition: aaccoder.c:225
static const uint8_t *const run_value_bits[2]
Definition: aaccoder.c:62
void INT64 start
Definition: avisynth_c.h:553
#define av_always_inline
Definition: attributes.h:37
#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE)
Definition: aaccoder.c:234
float threshold
Definition: psymodel.h:40
AAC data declarations.
float scoefs[1024]
scaled coefficients
Definition: aacenc.h:87
bitstream writer API