FFmpeg
aacsbr_template.c
Go to the documentation of this file.
1 /*
2  * AAC Spectral Band Replication decoding functions
3  * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
4  * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
5  *
6  * Fixed point code
7  * Copyright (c) 2013
8  * MIPS Technologies, Inc., California.
9  *
10  * This file is part of FFmpeg.
11  *
12  * FFmpeg is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * FFmpeg is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with FFmpeg; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 
27 /**
28  * @file
29  * AAC Spectral Band Replication decoding functions
30  * @author Robert Swain ( rob opendot cl )
31  * @author Stanislav Ocovaj ( stanislav.ocovaj@imgtec.com )
32  * @author Zoran Basaric ( zoran.basaric@imgtec.com )
33  */
34 
35 #include "libavutil/qsort.h"
36 
37 static av_cold void aacsbr_tableinit(void)
38 {
39  int n;
40  for (n = 1; n < 320; n++)
41  sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n];
44 
45  for (n = 0; n < 320; n++)
47 }
48 
50 {
51  static const struct {
52  const void *sbr_codes, *sbr_bits;
53  const unsigned int table_size, elem_size;
54  } sbr_tmp[] = {
55  SBR_VLC_ROW(t_huffman_env_1_5dB),
56  SBR_VLC_ROW(f_huffman_env_1_5dB),
57  SBR_VLC_ROW(t_huffman_env_bal_1_5dB),
58  SBR_VLC_ROW(f_huffman_env_bal_1_5dB),
59  SBR_VLC_ROW(t_huffman_env_3_0dB),
60  SBR_VLC_ROW(f_huffman_env_3_0dB),
61  SBR_VLC_ROW(t_huffman_env_bal_3_0dB),
62  SBR_VLC_ROW(f_huffman_env_bal_3_0dB),
63  SBR_VLC_ROW(t_huffman_noise_3_0dB),
64  SBR_VLC_ROW(t_huffman_noise_bal_3_0dB),
65  };
66 
67  // SBR VLC table initialization
68  SBR_INIT_VLC_STATIC(0, 1098);
69  SBR_INIT_VLC_STATIC(1, 1092);
70  SBR_INIT_VLC_STATIC(2, 768);
71  SBR_INIT_VLC_STATIC(3, 1026);
72  SBR_INIT_VLC_STATIC(4, 1058);
73  SBR_INIT_VLC_STATIC(5, 1052);
74  SBR_INIT_VLC_STATIC(6, 544);
75  SBR_INIT_VLC_STATIC(7, 544);
76  SBR_INIT_VLC_STATIC(8, 592);
77  SBR_INIT_VLC_STATIC(9, 512);
78 
80 
82 }
83 
84 /** Places SBR in pure upsampling mode. */
86  sbr->start = 0;
87  sbr->ready_for_dequant = 0;
88  // Init defults used in pure upsampling mode
89  sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
90  sbr->m[1] = 0;
91  // Reset values for first SBR header
92  sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
93  memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
94 }
95 
97 {
98  if(sbr->mdct.mdct_bits)
99  return;
100  sbr->kx[0] = sbr->kx[1];
101  sbr->id_aac = id_aac;
102  sbr_turnoff(sbr);
103  sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
104  sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
105  /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
106  * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
107  * and scale back down at synthesis. */
108  AAC_RENAME_32(ff_mdct_init)(&sbr->mdct, 7, 1, 1.0 / (64 * 32768.0));
109  AAC_RENAME_32(ff_mdct_init)(&sbr->mdct_ana, 7, 1, -2.0 * 32768.0);
110  AAC_RENAME(ff_ps_ctx_init)(&sbr->ps);
111  AAC_RENAME(ff_sbrdsp_init)(&sbr->dsp);
112  aacsbr_func_ptr_init(&sbr->c);
113 }
114 
116 {
117  AAC_RENAME_32(ff_mdct_end)(&sbr->mdct);
118  AAC_RENAME_32(ff_mdct_end)(&sbr->mdct_ana);
119 }
120 
121 static int qsort_comparison_function_int16(const void *a, const void *b)
122 {
123  return *(const int16_t *)a - *(const int16_t *)b;
124 }
125 
126 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
127 {
128  int i;
129  for (i = 0; i <= last_el; i++)
130  if (table[i] == needle)
131  return 1;
132  return 0;
133 }
134 
135 /// Limiter Frequency Band Table (14496-3 sp04 p198)
137 {
138  int k;
139  if (sbr->bs_limiter_bands > 0) {
140  static const INTFLOAT bands_warped[3] = { Q23(1.32715174233856803909f), //2^(0.49/1.2)
141  Q23(1.18509277094158210129f), //2^(0.49/2)
142  Q23(1.11987160404675912501f) }; //2^(0.49/3)
143  const INTFLOAT lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
144  int16_t patch_borders[7];
145  uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
146 
147  patch_borders[0] = sbr->kx[1];
148  for (k = 1; k <= sbr->num_patches; k++)
149  patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
150 
151  memcpy(sbr->f_tablelim, sbr->f_tablelow,
152  (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
153  if (sbr->num_patches > 1)
154  memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
155  (sbr->num_patches - 1) * sizeof(patch_borders[0]));
156 
157  AV_QSORT(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
158  uint16_t,
160 
161  sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
162  while (out < sbr->f_tablelim + sbr->n_lim) {
163 #if USE_FIXED
164  if ((*in << 23) >= *out * lim_bands_per_octave_warped) {
165 #else
166  if (*in >= *out * lim_bands_per_octave_warped) {
167 #endif /* USE_FIXED */
168  *++out = *in++;
169  } else if (*in == *out ||
170  !in_table_int16(patch_borders, sbr->num_patches, *in)) {
171  in++;
172  sbr->n_lim--;
173  } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
174  *out = *in++;
175  sbr->n_lim--;
176  } else {
177  *++out = *in++;
178  }
179  }
180  } else {
181  sbr->f_tablelim[0] = sbr->f_tablelow[0];
182  sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
183  sbr->n_lim = 1;
184  }
185 }
186 
188 {
189  unsigned int cnt = get_bits_count(gb);
190  uint8_t bs_header_extra_1;
191  uint8_t bs_header_extra_2;
192  int old_bs_limiter_bands = sbr->bs_limiter_bands;
193  SpectrumParameters old_spectrum_params;
194 
195  sbr->start = 1;
196  sbr->ready_for_dequant = 0;
197 
198  // Save last spectrum parameters variables to compare to new ones
199  memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
200 
201  sbr->bs_amp_res_header = get_bits1(gb);
202  sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
203  sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
204  sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
205  skip_bits(gb, 2); // bs_reserved
206 
207  bs_header_extra_1 = get_bits1(gb);
208  bs_header_extra_2 = get_bits1(gb);
209 
210  if (bs_header_extra_1) {
211  sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
214  } else {
218  }
219 
220  // Check if spectrum parameters changed
221  if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
222  sbr->reset = 1;
223 
224  if (bs_header_extra_2) {
225  sbr->bs_limiter_bands = get_bits(gb, 2);
226  sbr->bs_limiter_gains = get_bits(gb, 2);
227  sbr->bs_interpol_freq = get_bits1(gb);
228  sbr->bs_smoothing_mode = get_bits1(gb);
229  } else {
230  sbr->bs_limiter_bands = 2;
231  sbr->bs_limiter_gains = 2;
232  sbr->bs_interpol_freq = 1;
233  sbr->bs_smoothing_mode = 1;
234  }
235 
236  if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
237  sbr_make_f_tablelim(sbr);
238 
239  return get_bits_count(gb) - cnt;
240 }
241 
242 static int array_min_int16(const int16_t *array, int nel)
243 {
244  int i, min = array[0];
245  for (i = 1; i < nel; i++)
246  min = FFMIN(array[i], min);
247  return min;
248 }
249 
250 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
251 {
252  // Requirements (14496-3 sp04 p205)
253  if (n_master <= 0) {
254  av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
255  return -1;
256  }
257  if (bs_xover_band >= n_master) {
258  av_log(avctx, AV_LOG_ERROR,
259  "Invalid bitstream, crossover band index beyond array bounds: %d\n",
260  bs_xover_band);
261  return -1;
262  }
263  return 0;
264 }
265 
266 /// Master Frequency Band Table (14496-3 sp04 p194)
268  SpectrumParameters *spectrum)
269 {
270  unsigned int temp, max_qmf_subbands = 0;
271  unsigned int start_min, stop_min;
272  int k;
273  const int8_t *sbr_offset_ptr;
274  int16_t stop_dk[13];
275 
276  switch (sbr->sample_rate) {
277  case 16000:
278  sbr_offset_ptr = sbr_offset[0];
279  break;
280  case 22050:
281  sbr_offset_ptr = sbr_offset[1];
282  break;
283  case 24000:
284  sbr_offset_ptr = sbr_offset[2];
285  break;
286  case 32000:
287  sbr_offset_ptr = sbr_offset[3];
288  break;
289  case 44100: case 48000: case 64000:
290  sbr_offset_ptr = sbr_offset[4];
291  break;
292  case 88200: case 96000: case 128000: case 176400: case 192000:
293  sbr_offset_ptr = sbr_offset[5];
294  break;
295  default:
297  "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
298  return -1;
299  }
300 
301  if (sbr->sample_rate < 32000) {
302  temp = 3000;
303  } else if (sbr->sample_rate < 64000) {
304  temp = 4000;
305  } else
306  temp = 5000;
307 
308  start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
309  stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
310 
311  sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
312 
313  if (spectrum->bs_stop_freq < 14) {
314  sbr->k[2] = stop_min;
315  make_bands(stop_dk, stop_min, 64, 13);
316  AV_QSORT(stop_dk, 13, int16_t, qsort_comparison_function_int16);
317  for (k = 0; k < spectrum->bs_stop_freq; k++)
318  sbr->k[2] += stop_dk[k];
319  } else if (spectrum->bs_stop_freq == 14) {
320  sbr->k[2] = 2*sbr->k[0];
321  } else if (spectrum->bs_stop_freq == 15) {
322  sbr->k[2] = 3*sbr->k[0];
323  } else {
325  "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
326  return -1;
327  }
328  sbr->k[2] = FFMIN(64, sbr->k[2]);
329 
330  // Requirements (14496-3 sp04 p205)
331  if (sbr->sample_rate <= 32000) {
332  max_qmf_subbands = 48;
333  } else if (sbr->sample_rate == 44100) {
334  max_qmf_subbands = 35;
335  } else if (sbr->sample_rate >= 48000)
336  max_qmf_subbands = 32;
337  else
338  av_assert0(0);
339 
340  if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
342  "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
343  return -1;
344  }
345 
346  if (!spectrum->bs_freq_scale) {
347  int dk, k2diff;
348 
349  dk = spectrum->bs_alter_scale + 1;
350  sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
352  return -1;
353 
354  for (k = 1; k <= sbr->n_master; k++)
355  sbr->f_master[k] = dk;
356 
357  k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
358  if (k2diff < 0) {
359  sbr->f_master[1]--;
360  sbr->f_master[2]-= (k2diff < -1);
361  } else if (k2diff) {
362  sbr->f_master[sbr->n_master]++;
363  }
364 
365  sbr->f_master[0] = sbr->k[0];
366  for (k = 1; k <= sbr->n_master; k++)
367  sbr->f_master[k] += sbr->f_master[k - 1];
368 
369  } else {
370  int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3}
371  int two_regions, num_bands_0;
372  int vdk0_max, vdk1_min;
373  int16_t vk0[49];
374 #if USE_FIXED
375  int tmp, nz = 0;
376 #endif /* USE_FIXED */
377 
378  if (49 * sbr->k[2] > 110 * sbr->k[0]) {
379  two_regions = 1;
380  sbr->k[1] = 2 * sbr->k[0];
381  } else {
382  two_regions = 0;
383  sbr->k[1] = sbr->k[2];
384  }
385 
386 #if USE_FIXED
387  tmp = (sbr->k[1] << 23) / sbr->k[0];
388  while (tmp < 0x40000000) {
389  tmp <<= 1;
390  nz++;
391  }
392  tmp = fixed_log(tmp - 0x80000000);
393  tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
394  tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
395  num_bands_0 = ((tmp + 0x400000) >> 23) * 2;
396 #else
397  num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
398 #endif /* USE_FIXED */
399 
400  if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
401  av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
402  return -1;
403  }
404 
405  vk0[0] = 0;
406 
407  make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
408 
409  AV_QSORT(vk0 + 1, num_bands_0, int16_t, qsort_comparison_function_int16);
410  vdk0_max = vk0[num_bands_0];
411 
412  vk0[0] = sbr->k[0];
413  for (k = 1; k <= num_bands_0; k++) {
414  if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
415  av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
416  return -1;
417  }
418  vk0[k] += vk0[k-1];
419  }
420 
421  if (two_regions) {
422  int16_t vk1[49];
423 #if USE_FIXED
424  int num_bands_1;
425 
426  tmp = (sbr->k[2] << 23) / sbr->k[1];
427  nz = 0;
428  while (tmp < 0x40000000) {
429  tmp <<= 1;
430  nz++;
431  }
432  tmp = fixed_log(tmp - 0x80000000);
433  tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
434  tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
435  if (spectrum->bs_alter_scale)
436  tmp = (int)(((int64_t)tmp * CONST_076923 + 0x40000000) >> 31);
437  num_bands_1 = ((tmp + 0x400000) >> 23) * 2;
438 #else
439  float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
440  : 1.0f; // bs_alter_scale = {0,1}
441  int num_bands_1 = lrintf(half_bands * invwarp *
442  log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
443 #endif /* USE_FIXED */
444  make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
445 
446  vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
447 
448  if (vdk1_min < vdk0_max) {
449  int change;
450  AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
451  change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
452  vk1[1] += change;
453  vk1[num_bands_1] -= change;
454  }
455 
456  AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
457 
458  vk1[0] = sbr->k[1];
459  for (k = 1; k <= num_bands_1; k++) {
460  if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
461  av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
462  return -1;
463  }
464  vk1[k] += vk1[k-1];
465  }
466 
467  sbr->n_master = num_bands_0 + num_bands_1;
469  return -1;
470  memcpy(&sbr->f_master[0], vk0,
471  (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
472  memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
473  num_bands_1 * sizeof(sbr->f_master[0]));
474 
475  } else {
476  sbr->n_master = num_bands_0;
478  return -1;
479  memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
480  }
481  }
482 
483  return 0;
484 }
485 
486 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
488 {
489  int i, k, last_k = -1, last_msb = -1, sb = 0;
490  int msb = sbr->k[0];
491  int usb = sbr->kx[1];
492  int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
493 
494  sbr->num_patches = 0;
495 
496  if (goal_sb < sbr->kx[1] + sbr->m[1]) {
497  for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
498  } else
499  k = sbr->n_master;
500 
501  do {
502  int odd = 0;
503  if (k == last_k && msb == last_msb) {
504  av_log(ac->avctx, AV_LOG_ERROR, "patch construction failed\n");
505  return AVERROR_INVALIDDATA;
506  }
507  last_k = k;
508  last_msb = msb;
509  for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
510  sb = sbr->f_master[i];
511  odd = (sb + sbr->k[0]) & 1;
512  }
513 
514  // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
515  // After this check the final number of patches can still be six which is
516  // illegal however the Coding Technologies decoder check stream has a final
517  // count of 6 patches
518  if (sbr->num_patches > 5) {
519  av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
520  return -1;
521  }
522 
523  sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
524  sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
525 
526  if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
527  usb = sb;
528  msb = sb;
529  sbr->num_patches++;
530  } else
531  msb = sbr->kx[1];
532 
533  if (sbr->f_master[k] - sb < 3)
534  k = sbr->n_master;
535  } while (sb != sbr->kx[1] + sbr->m[1]);
536 
537  if (sbr->num_patches > 1 &&
538  sbr->patch_num_subbands[sbr->num_patches - 1] < 3)
539  sbr->num_patches--;
540 
541  return 0;
542 }
543 
544 /// Derived Frequency Band Tables (14496-3 sp04 p197)
546 {
547  int k, temp;
548 #if USE_FIXED
549  int nz = 0;
550 #endif /* USE_FIXED */
551 
552  sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
553  sbr->n[0] = (sbr->n[1] + 1) >> 1;
554 
555  memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
556  (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
557  sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
558  sbr->kx[1] = sbr->f_tablehigh[0];
559 
560  // Requirements (14496-3 sp04 p205)
561  if (sbr->kx[1] + sbr->m[1] > 64) {
563  "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
564  return -1;
565  }
566  if (sbr->kx[1] > 32) {
567  av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
568  return -1;
569  }
570 
571  sbr->f_tablelow[0] = sbr->f_tablehigh[0];
572  temp = sbr->n[1] & 1;
573  for (k = 1; k <= sbr->n[0]; k++)
574  sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
575 #if USE_FIXED
576  temp = (sbr->k[2] << 23) / sbr->kx[1];
577  while (temp < 0x40000000) {
578  temp <<= 1;
579  nz++;
580  }
581  temp = fixed_log(temp - 0x80000000);
582  temp = (int)(((int64_t)temp * CONST_RECIP_LN2 + 0x20000000) >> 30);
583  temp = (((temp + 0x80) >> 8) + ((8 - nz) << 23)) * sbr->spectrum_params.bs_noise_bands;
584 
585  sbr->n_q = (temp + 0x400000) >> 23;
586  if (sbr->n_q < 1)
587  sbr->n_q = 1;
588 #else
590  log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
591 #endif /* USE_FIXED */
592 
593  if (sbr->n_q > 5) {
594  av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
595  return -1;
596  }
597 
598  sbr->f_tablenoise[0] = sbr->f_tablelow[0];
599  temp = 0;
600  for (k = 1; k <= sbr->n_q; k++) {
601  temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
602  sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
603  }
604 
605  if (sbr_hf_calc_npatches(ac, sbr) < 0)
606  return -1;
607 
608  sbr_make_f_tablelim(sbr);
609 
610  sbr->data[0].f_indexnoise = 0;
611  sbr->data[1].f_indexnoise = 0;
612 
613  return 0;
614 }
615 
617  int elements)
618 {
619  int i;
620  for (i = 0; i < elements; i++) {
621  vec[i] = get_bits1(gb);
622  }
623 }
624 
625 /** ceil(log2(index+1)) */
626 static const int8_t ceil_log2[] = {
627  0, 1, 2, 2, 3, 3,
628 };
629 
631  GetBitContext *gb, SBRData *ch_data)
632 {
633  int i;
634  int bs_pointer = 0;
635  // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
636  int abs_bord_trail = 16;
637  int num_rel_lead, num_rel_trail;
638  unsigned bs_num_env_old = ch_data->bs_num_env;
639  int bs_frame_class, bs_num_env;
640 
641  ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
642  ch_data->bs_amp_res = sbr->bs_amp_res_header;
643  ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
644 
645  switch (bs_frame_class = get_bits(gb, 2)) {
646  case FIXFIX:
647  bs_num_env = 1 << get_bits(gb, 2);
648  if (bs_num_env > 4) {
650  "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
651  bs_num_env);
652  return -1;
653  }
654  ch_data->bs_num_env = bs_num_env;
655  num_rel_lead = ch_data->bs_num_env - 1;
656  if (ch_data->bs_num_env == 1)
657  ch_data->bs_amp_res = 0;
658 
659 
660  ch_data->t_env[0] = 0;
661  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
662 
663  abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
664  ch_data->bs_num_env;
665  for (i = 0; i < num_rel_lead; i++)
666  ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
667 
668  ch_data->bs_freq_res[1] = get_bits1(gb);
669  for (i = 1; i < ch_data->bs_num_env; i++)
670  ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
671  break;
672  case FIXVAR:
673  abs_bord_trail += get_bits(gb, 2);
674  num_rel_trail = get_bits(gb, 2);
675  ch_data->bs_num_env = num_rel_trail + 1;
676  ch_data->t_env[0] = 0;
677  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
678 
679  for (i = 0; i < num_rel_trail; i++)
680  ch_data->t_env[ch_data->bs_num_env - 1 - i] =
681  ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
682 
683  bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
684 
685  for (i = 0; i < ch_data->bs_num_env; i++)
686  ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
687  break;
688  case VARFIX:
689  ch_data->t_env[0] = get_bits(gb, 2);
690  num_rel_lead = get_bits(gb, 2);
691  ch_data->bs_num_env = num_rel_lead + 1;
692  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
693 
694  for (i = 0; i < num_rel_lead; i++)
695  ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
696 
697  bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
698 
699  get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
700  break;
701  case VARVAR:
702  ch_data->t_env[0] = get_bits(gb, 2);
703  abs_bord_trail += get_bits(gb, 2);
704  num_rel_lead = get_bits(gb, 2);
705  num_rel_trail = get_bits(gb, 2);
706  bs_num_env = num_rel_lead + num_rel_trail + 1;
707 
708  if (bs_num_env > 5) {
710  "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
711  bs_num_env);
712  return -1;
713  }
714  ch_data->bs_num_env = bs_num_env;
715 
716  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
717 
718  for (i = 0; i < num_rel_lead; i++)
719  ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
720  for (i = 0; i < num_rel_trail; i++)
721  ch_data->t_env[ch_data->bs_num_env - 1 - i] =
722  ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
723 
724  bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
725 
726  get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
727  break;
728  }
729  ch_data->bs_frame_class = bs_frame_class;
730 
731  av_assert0(bs_pointer >= 0);
732  if (bs_pointer > ch_data->bs_num_env + 1) {
734  "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
735  bs_pointer);
736  return -1;
737  }
738 
739  for (i = 1; i <= ch_data->bs_num_env; i++) {
740  if (ch_data->t_env[i-1] >= ch_data->t_env[i]) {
741  av_log(ac->avctx, AV_LOG_ERROR, "Not strictly monotone time borders\n");
742  return -1;
743  }
744  }
745 
746  ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
747 
748  ch_data->t_q[0] = ch_data->t_env[0];
749  ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
750  if (ch_data->bs_num_noise > 1) {
751  int idx;
752  if (ch_data->bs_frame_class == FIXFIX) {
753  idx = ch_data->bs_num_env >> 1;
754  } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
755  idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
756  } else { // VARFIX
757  if (!bs_pointer)
758  idx = 1;
759  else if (bs_pointer == 1)
760  idx = ch_data->bs_num_env - 1;
761  else // bs_pointer > 1
762  idx = bs_pointer - 1;
763  }
764  ch_data->t_q[1] = ch_data->t_env[idx];
765  }
766 
767  ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
768  ch_data->e_a[1] = -1;
769  if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
770  ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
771  } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
772  ch_data->e_a[1] = bs_pointer - 1;
773 
774  return 0;
775 }
776 
777 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
778  //These variables are saved from the previous frame rather than copied
779  dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
780  dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
781  dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
782 
783  //These variables are read from the bitstream and therefore copied
784  memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
785  memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
786  memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
787  dst->bs_num_env = src->bs_num_env;
788  dst->bs_amp_res = src->bs_amp_res;
789  dst->bs_num_noise = src->bs_num_noise;
790  dst->bs_frame_class = src->bs_frame_class;
791  dst->e_a[1] = src->e_a[1];
792 }
793 
794 /// Read how the envelope and noise floor data is delta coded
796  SBRData *ch_data)
797 {
798  get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
799  get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
800 }
801 
802 /// Read inverse filtering data
804  SBRData *ch_data)
805 {
806  int i;
807 
808  memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
809  for (i = 0; i < sbr->n_q; i++)
810  ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
811 }
812 
814  SBRData *ch_data, int ch)
815 {
816  int bits;
817  int i, j, k;
818  VLC_TYPE (*t_huff)[2], (*f_huff)[2];
819  int t_lav, f_lav;
820  const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
821  const int odd = sbr->n[1] & 1;
822 
823  if (sbr->bs_coupling && ch) {
824  if (ch_data->bs_amp_res) {
825  bits = 5;
830  } else {
831  bits = 6;
836  }
837  } else {
838  if (ch_data->bs_amp_res) {
839  bits = 6;
844  } else {
845  bits = 7;
850  }
851  }
852 
853  for (i = 0; i < ch_data->bs_num_env; i++) {
854  if (ch_data->bs_df_env[i]) {
855  // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
856  if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
857  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
858  ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
859  if (ch_data->env_facs_q[i + 1][j] > 127U) {
860  av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
861  return AVERROR_INVALIDDATA;
862  }
863  }
864  } else if (ch_data->bs_freq_res[i + 1]) {
865  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
866  k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
867  ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
868  if (ch_data->env_facs_q[i + 1][j] > 127U) {
869  av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
870  return AVERROR_INVALIDDATA;
871  }
872  }
873  } else {
874  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
875  k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
876  ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
877  if (ch_data->env_facs_q[i + 1][j] > 127U) {
878  av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
879  return AVERROR_INVALIDDATA;
880  }
881  }
882  }
883  } else {
884  ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
885  for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
886  ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
887  if (ch_data->env_facs_q[i + 1][j] > 127U) {
888  av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
889  return AVERROR_INVALIDDATA;
890  }
891  }
892  }
893  }
894 
895  //assign 0th elements of env_facs_q from last elements
896  memcpy(ch_data->env_facs_q[0], ch_data->env_facs_q[ch_data->bs_num_env],
897  sizeof(ch_data->env_facs_q[0]));
898 
899  return 0;
900 }
901 
903  SBRData *ch_data, int ch)
904 {
905  int i, j;
906  VLC_TYPE (*t_huff)[2], (*f_huff)[2];
907  int t_lav, f_lav;
908  int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
909 
910  if (sbr->bs_coupling && ch) {
915  } else {
920  }
921 
922  for (i = 0; i < ch_data->bs_num_noise; i++) {
923  if (ch_data->bs_df_noise[i]) {
924  for (j = 0; j < sbr->n_q; j++) {
925  ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
926  if (ch_data->noise_facs_q[i + 1][j] > 30U) {
927  av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
928  return AVERROR_INVALIDDATA;
929  }
930  }
931  } else {
932  ch_data->noise_facs_q[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
933  for (j = 1; j < sbr->n_q; j++) {
934  ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
935  if (ch_data->noise_facs_q[i + 1][j] > 30U) {
936  av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
937  return AVERROR_INVALIDDATA;
938  }
939  }
940  }
941  }
942 
943  //assign 0th elements of noise_facs_q from last elements
944  memcpy(ch_data->noise_facs_q[0], ch_data->noise_facs_q[ch_data->bs_num_noise],
945  sizeof(ch_data->noise_facs_q[0]));
946  return 0;
947 }
948 
950  GetBitContext *gb,
951  int bs_extension_id, int *num_bits_left)
952 {
953  switch (bs_extension_id) {
954  case EXTENSION_ID_PS:
955  if (!ac->oc[1].m4ac.ps) {
956  av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
957  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
958  *num_bits_left = 0;
959  } else {
960  *num_bits_left -= AAC_RENAME(ff_ps_read_data)(ac->avctx, gb, &sbr->ps, *num_bits_left);
962  }
963  break;
964  default:
965  // some files contain 0-padding
966  if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
967  avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
968  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
969  *num_bits_left = 0;
970  break;
971  }
972 }
973 
976  GetBitContext *gb)
977 {
978  int ret;
979 
980  if (get_bits1(gb)) // bs_data_extra
981  skip_bits(gb, 4); // bs_reserved
982 
983  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
984  return -1;
985  read_sbr_dtdf(sbr, gb, &sbr->data[0]);
986  read_sbr_invf(sbr, gb, &sbr->data[0]);
987  if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
988  return ret;
989  if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
990  return ret;
991 
992  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
993  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
994 
995  return 0;
996 }
997 
1000  GetBitContext *gb)
1001 {
1002  int ret;
1003 
1004  if (get_bits1(gb)) // bs_data_extra
1005  skip_bits(gb, 8); // bs_reserved
1006 
1007  if ((sbr->bs_coupling = get_bits1(gb))) {
1008  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1009  return -1;
1010  copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
1011  read_sbr_dtdf(sbr, gb, &sbr->data[0]);
1012  read_sbr_dtdf(sbr, gb, &sbr->data[1]);
1013  read_sbr_invf(sbr, gb, &sbr->data[0]);
1014  memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
1015  memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
1016  if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1017  return ret;
1018  if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1019  return ret;
1020  if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1021  return ret;
1022  if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1023  return ret;
1024  } else {
1025  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
1026  read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
1027  return -1;
1028  read_sbr_dtdf(sbr, gb, &sbr->data[0]);
1029  read_sbr_dtdf(sbr, gb, &sbr->data[1]);
1030  read_sbr_invf(sbr, gb, &sbr->data[0]);
1031  read_sbr_invf(sbr, gb, &sbr->data[1]);
1032  if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1033  return ret;
1034  if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1035  return ret;
1036  if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1037  return ret;
1038  if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1039  return ret;
1040  }
1041 
1042  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1043  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1044  if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1045  get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1046 
1047  return 0;
1048 }
1049 
1050 static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
1051  GetBitContext *gb, int id_aac)
1052 {
1053  unsigned int cnt = get_bits_count(gb);
1054 
1055  sbr->id_aac = id_aac;
1056  sbr->ready_for_dequant = 1;
1057 
1058  if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
1059  if (read_sbr_single_channel_element(ac, sbr, gb)) {
1060  sbr_turnoff(sbr);
1061  return get_bits_count(gb) - cnt;
1062  }
1063  } else if (id_aac == TYPE_CPE) {
1064  if (read_sbr_channel_pair_element(ac, sbr, gb)) {
1065  sbr_turnoff(sbr);
1066  return get_bits_count(gb) - cnt;
1067  }
1068  } else {
1069  av_log(ac->avctx, AV_LOG_ERROR,
1070  "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1071  sbr_turnoff(sbr);
1072  return get_bits_count(gb) - cnt;
1073  }
1074  if (get_bits1(gb)) { // bs_extended_data
1075  int num_bits_left = get_bits(gb, 4); // bs_extension_size
1076  if (num_bits_left == 15)
1077  num_bits_left += get_bits(gb, 8); // bs_esc_count
1078 
1079  num_bits_left <<= 3;
1080  while (num_bits_left > 7) {
1081  num_bits_left -= 2;
1082  read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
1083  }
1084  if (num_bits_left < 0) {
1085  av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
1086  }
1087  if (num_bits_left > 0)
1088  skip_bits(gb, num_bits_left);
1089  }
1090 
1091  return get_bits_count(gb) - cnt;
1092 }
1093 
1095 {
1096  int err;
1097  err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
1098  if (err >= 0)
1099  err = sbr_make_f_derived(ac, sbr);
1100  if (err < 0) {
1101  av_log(ac->avctx, AV_LOG_ERROR,
1102  "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1103  sbr_turnoff(sbr);
1104  }
1105 }
1106 
1107 /**
1108  * Decode Spectral Band Replication extension data; reference: table 4.55.
1109  *
1110  * @param crc flag indicating the presence of CRC checksum
1111  * @param cnt length of TYPE_FIL syntactic element in bytes
1112  *
1113  * @return Returns number of bytes consumed from the TYPE_FIL element.
1114  */
1116  GetBitContext *gb_host, int crc, int cnt, int id_aac)
1117 {
1118  unsigned int num_sbr_bits = 0, num_align_bits;
1119  unsigned bytes_read;
1120  GetBitContext gbc = *gb_host, *gb = &gbc;
1121  skip_bits_long(gb_host, cnt*8 - 4);
1122 
1123  sbr->reset = 0;
1124 
1125  if (!sbr->sample_rate)
1126  sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
1127  if (!ac->oc[1].m4ac.ext_sample_rate)
1128  ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
1129 
1130  if (crc) {
1131  skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
1132  num_sbr_bits += 10;
1133  }
1134 
1135  //Save some state from the previous frame.
1136  sbr->kx[0] = sbr->kx[1];
1137  sbr->m[0] = sbr->m[1];
1138  sbr->kx_and_m_pushed = 1;
1139 
1140  num_sbr_bits++;
1141  if (get_bits1(gb)) // bs_header_flag
1142  num_sbr_bits += read_sbr_header(sbr, gb);
1143 
1144  if (sbr->reset)
1145  sbr_reset(ac, sbr);
1146 
1147  if (sbr->start)
1148  num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
1149 
1150  num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1151  bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1152 
1153  if (bytes_read > cnt) {
1154  av_log(ac->avctx, AV_LOG_ERROR,
1155  "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1156  sbr_turnoff(sbr);
1157  }
1158  return cnt;
1159 }
1160 
1161 /**
1162  * Analysis QMF Bank (14496-3 sp04 p206)
1163  *
1164  * @param x pointer to the beginning of the first sample window
1165  * @param W array of complex-valued samples split into subbands
1166  */
1167 #ifndef sbr_qmf_analysis
1168 #if USE_FIXED
1169 static void sbr_qmf_analysis(AVFixedDSPContext *dsp, FFTContext *mdct,
1170 #else
1172 #endif /* USE_FIXED */
1173  SBRDSPContext *sbrdsp, const INTFLOAT *in, INTFLOAT *x,
1174  INTFLOAT z[320], INTFLOAT W[2][32][32][2], int buf_idx)
1175 {
1176  int i;
1177 #if USE_FIXED
1178  int j;
1179 #endif
1180  memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1181  memcpy(x+288, in, 1024*sizeof(x[0]));
1182  for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1183  // are not supported
1184  dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1185  sbrdsp->sum64x5(z);
1186  sbrdsp->qmf_pre_shuffle(z);
1187 #if USE_FIXED
1188  for (j = 64; j < 128; j++) {
1189  if (z[j] > 1<<24) {
1191  "sbr_qmf_analysis: value %09d too large, setting to %09d\n",
1192  z[j], 1<<24);
1193  z[j] = 1<<24;
1194  } else if (z[j] < -(1<<24)) {
1196  "sbr_qmf_analysis: value %09d too small, setting to %09d\n",
1197  z[j], -(1<<24));
1198  z[j] = -(1<<24);
1199  }
1200  }
1201 #endif
1202  mdct->imdct_half(mdct, z, z+64);
1203  sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
1204  x += 32;
1205  }
1206 }
1207 #endif
1208 
1209 /**
1210  * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1211  * (14496-3 sp04 p206)
1212  */
1213 #ifndef sbr_qmf_synthesis
1214 static void sbr_qmf_synthesis(FFTContext *mdct,
1215 #if USE_FIXED
1216  SBRDSPContext *sbrdsp, AVFixedDSPContext *dsp,
1217 #else
1218  SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
1219 #endif /* USE_FIXED */
1220  INTFLOAT *out, INTFLOAT X[2][38][64],
1221  INTFLOAT mdct_buf[2][64],
1222  INTFLOAT *v0, int *v_off, const unsigned int div)
1223 {
1224  int i, n;
1225  const INTFLOAT *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
1226  const int step = 128 >> div;
1227  INTFLOAT *v;
1228  for (i = 0; i < 32; i++) {
1229  if (*v_off < step) {
1230  int saved_samples = (1280 - 128) >> div;
1231  memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(INTFLOAT));
1232  *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
1233  } else {
1234  *v_off -= step;
1235  }
1236  v = v0 + *v_off;
1237  if (div) {
1238  for (n = 0; n < 32; n++) {
1239  X[0][i][ n] = -X[0][i][n];
1240  X[0][i][32+n] = X[1][i][31-n];
1241  }
1242  mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1243  sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
1244  } else {
1245  sbrdsp->neg_odd_64(X[1][i]);
1246  mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1247  mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
1248  sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
1249  }
1250  dsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1251  dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1252  dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1253  dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1254  dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1255  dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1256  dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1257  dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1258  dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1259  dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1260  out += 64 >> div;
1261  }
1262 }
1263 #endif
1264 
1265 /// Generate the subband filtered lowband
1267  INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2],
1268  int buf_idx)
1269 {
1270  int i, k;
1271  const int t_HFGen = 8;
1272  const int i_f = 32;
1273  memset(X_low, 0, 32*sizeof(*X_low));
1274  for (k = 0; k < sbr->kx[1]; k++) {
1275  for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1276  X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1277  X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1278  }
1279  }
1280  buf_idx = 1-buf_idx;
1281  for (k = 0; k < sbr->kx[0]; k++) {
1282  for (i = 0; i < t_HFGen; i++) {
1283  X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1284  X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1285  }
1286  }
1287  return 0;
1288 }
1289 
1290 /// High Frequency Generator (14496-3 sp04 p215)
1292  INTFLOAT X_high[64][40][2], const INTFLOAT X_low[32][40][2],
1293  const INTFLOAT (*alpha0)[2], const INTFLOAT (*alpha1)[2],
1294  const INTFLOAT bw_array[5], const uint8_t *t_env,
1295  int bs_num_env)
1296 {
1297  int j, x;
1298  int g = 0;
1299  int k = sbr->kx[1];
1300  for (j = 0; j < sbr->num_patches; j++) {
1301  for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
1302  const int p = sbr->patch_start_subband[j] + x;
1303  while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
1304  g++;
1305  g--;
1306 
1307  if (g < 0) {
1308  av_log(ac->avctx, AV_LOG_ERROR,
1309  "ERROR : no subband found for frequency %d\n", k);
1310  return -1;
1311  }
1312 
1313  sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
1314  X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
1315  alpha0[p], alpha1[p], bw_array[g],
1316  2 * t_env[0], 2 * t_env[bs_num_env]);
1317  }
1318  }
1319  if (k < sbr->m[1] + sbr->kx[1])
1320  memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
1321 
1322  return 0;
1323 }
1324 
1325 /// Generate the subband filtered lowband
1326 static int sbr_x_gen(SpectralBandReplication *sbr, INTFLOAT X[2][38][64],
1327  const INTFLOAT Y0[38][64][2], const INTFLOAT Y1[38][64][2],
1328  const INTFLOAT X_low[32][40][2], int ch)
1329 {
1330  int k, i;
1331  const int i_f = 32;
1332  const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
1333  memset(X, 0, 2*sizeof(*X));
1334  for (k = 0; k < sbr->kx[0]; k++) {
1335  for (i = 0; i < i_Temp; i++) {
1336  X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1337  X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1338  }
1339  }
1340  for (; k < sbr->kx[0] + sbr->m[0]; k++) {
1341  for (i = 0; i < i_Temp; i++) {
1342  X[0][i][k] = Y0[i + i_f][k][0];
1343  X[1][i][k] = Y0[i + i_f][k][1];
1344  }
1345  }
1346 
1347  for (k = 0; k < sbr->kx[1]; k++) {
1348  for (i = i_Temp; i < 38; i++) {
1349  X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1350  X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1351  }
1352  }
1353  for (; k < sbr->kx[1] + sbr->m[1]; k++) {
1354  for (i = i_Temp; i < i_f; i++) {
1355  X[0][i][k] = Y1[i][k][0];
1356  X[1][i][k] = Y1[i][k][1];
1357  }
1358  }
1359  return 0;
1360 }
1361 
1362 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1363  * (14496-3 sp04 p217)
1364  */
1366  SBRData *ch_data, int e_a[2])
1367 {
1368  int e, i, m;
1369 
1370  memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
1371  for (e = 0; e < ch_data->bs_num_env; e++) {
1372  const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
1373  uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1374  int k;
1375 
1376  if (sbr->kx[1] != table[0]) {
1377  av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
1378  "Derived frequency tables were not regenerated.\n");
1379  sbr_turnoff(sbr);
1380  return AVERROR_BUG;
1381  }
1382  for (i = 0; i < ilim; i++)
1383  for (m = table[i]; m < table[i + 1]; m++)
1384  sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
1385 
1386  // ch_data->bs_num_noise > 1 => 2 noise floors
1387  k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
1388  for (i = 0; i < sbr->n_q; i++)
1389  for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
1390  sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
1391 
1392  for (i = 0; i < sbr->n[1]; i++) {
1393  if (ch_data->bs_add_harmonic_flag) {
1394  const unsigned int m_midpoint =
1395  (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
1396 
1397  ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
1398  (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
1399  }
1400  }
1401 
1402  for (i = 0; i < ilim; i++) {
1403  int additional_sinusoid_present = 0;
1404  for (m = table[i]; m < table[i + 1]; m++) {
1405  if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
1406  additional_sinusoid_present = 1;
1407  break;
1408  }
1409  }
1410  memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
1411  (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
1412  }
1413  }
1414 
1415  memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
1416  return 0;
1417 }
1418 
1419 /// Estimation of current envelope (14496-3 sp04 p218)
1420 static void sbr_env_estimate(AAC_FLOAT (*e_curr)[48], INTFLOAT X_high[64][40][2],
1421  SpectralBandReplication *sbr, SBRData *ch_data)
1422 {
1423  int e, m;
1424  int kx1 = sbr->kx[1];
1425 
1426  if (sbr->bs_interpol_freq) {
1427  for (e = 0; e < ch_data->bs_num_env; e++) {
1428 #if USE_FIXED
1429  const SoftFloat recip_env_size = av_int2sf(0x20000000 / (ch_data->t_env[e + 1] - ch_data->t_env[e]), 30);
1430 #else
1431  const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1432 #endif /* USE_FIXED */
1433  int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1434  int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1435 
1436  for (m = 0; m < sbr->m[1]; m++) {
1437  AAC_FLOAT sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
1438 #if USE_FIXED
1439  e_curr[e][m] = av_mul_sf(sum, recip_env_size);
1440 #else
1441  e_curr[e][m] = sum * recip_env_size;
1442 #endif /* USE_FIXED */
1443  }
1444  }
1445  } else {
1446  int k, p;
1447 
1448  for (e = 0; e < ch_data->bs_num_env; e++) {
1449  const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1450  int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1451  int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1452  const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1453 
1454  for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
1455 #if USE_FIXED
1456  SoftFloat sum = FLOAT_0;
1457  const SoftFloat den = av_int2sf(0x20000000 / (env_size * (table[p + 1] - table[p])), 29);
1458  for (k = table[p]; k < table[p + 1]; k++) {
1459  sum = av_add_sf(sum, sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb));
1460  }
1461  sum = av_mul_sf(sum, den);
1462 #else
1463  float sum = 0.0f;
1464  const int den = env_size * (table[p + 1] - table[p]);
1465 
1466  for (k = table[p]; k < table[p + 1]; k++) {
1467  sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
1468  }
1469  sum /= den;
1470 #endif /* USE_FIXED */
1471  for (k = table[p]; k < table[p + 1]; k++) {
1472  e_curr[e][k - kx1] = sum;
1473  }
1474  }
1475  }
1476  }
1477 }
1478 
1480  INTFLOAT* L, INTFLOAT* R)
1481 {
1482  int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
1483  int ch;
1484  int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1485  int err;
1486 
1487  if (id_aac != sbr->id_aac) {
1488  av_log(ac->avctx, id_aac == TYPE_LFE ? AV_LOG_VERBOSE : AV_LOG_WARNING,
1489  "element type mismatch %d != %d\n", id_aac, sbr->id_aac);
1490  sbr_turnoff(sbr);
1491  }
1492 
1493  if (sbr->start && !sbr->ready_for_dequant) {
1494  av_log(ac->avctx, AV_LOG_ERROR,
1495  "No quantized data read for sbr_dequant.\n");
1496  sbr_turnoff(sbr);
1497  }
1498 
1499  if (!sbr->kx_and_m_pushed) {
1500  sbr->kx[0] = sbr->kx[1];
1501  sbr->m[0] = sbr->m[1];
1502  } else {
1503  sbr->kx_and_m_pushed = 0;
1504  }
1505 
1506  if (sbr->start) {
1507  sbr_dequant(sbr, id_aac);
1508  sbr->ready_for_dequant = 0;
1509  }
1510  for (ch = 0; ch < nch; ch++) {
1511  /* decode channel */
1512  sbr_qmf_analysis(ac->fdsp, &sbr->mdct_ana, &sbr->dsp, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1513  (INTFLOAT*)sbr->qmf_filter_scratch,
1514  sbr->data[ch].W, sbr->data[ch].Ypos);
1515  sbr->c.sbr_lf_gen(ac, sbr, sbr->X_low,
1516  (const INTFLOAT (*)[32][32][2]) sbr->data[ch].W,
1517  sbr->data[ch].Ypos);
1518  sbr->data[ch].Ypos ^= 1;
1519  if (sbr->start) {
1520  sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
1521  (const INTFLOAT (*)[40][2]) sbr->X_low, sbr->k[0]);
1522  sbr_chirp(sbr, &sbr->data[ch]);
1523  av_assert0(sbr->data[ch].bs_num_env > 0);
1524  sbr_hf_gen(ac, sbr, sbr->X_high,
1525  (const INTFLOAT (*)[40][2]) sbr->X_low,
1526  (const INTFLOAT (*)[2]) sbr->alpha0,
1527  (const INTFLOAT (*)[2]) sbr->alpha1,
1528  sbr->data[ch].bw_array, sbr->data[ch].t_env,
1529  sbr->data[ch].bs_num_env);
1530 
1531  // hf_adj
1532  err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1533  if (!err) {
1534  sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1535  sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1536  sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
1537  (const INTFLOAT (*)[40][2]) sbr->X_high,
1538  sbr, &sbr->data[ch],
1539  sbr->data[ch].e_a);
1540  }
1541  }
1542 
1543  /* synthesis */
1544  sbr->c.sbr_x_gen(sbr, sbr->X[ch],
1545  (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
1546  (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[ sbr->data[ch].Ypos],
1547  (const INTFLOAT (*)[40][2]) sbr->X_low, ch);
1548  }
1549 
1550  if (ac->oc[1].m4ac.ps == 1) {
1551  if (sbr->ps.start) {
1552  AAC_RENAME(ff_ps_apply)(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1553  } else {
1554  memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1555  }
1556  nch = 2;
1557  }
1558 
1559  sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
1560  L, sbr->X[0], sbr->qmf_filter_scratch,
1561  sbr->data[0].synthesis_filterbank_samples,
1562  &sbr->data[0].synthesis_filterbank_samples_offset,
1563  downsampled);
1564  if (nch == 2)
1565  sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
1566  R, sbr->X[1], sbr->qmf_filter_scratch,
1567  sbr->data[1].synthesis_filterbank_samples,
1568  &sbr->data[1].synthesis_filterbank_samples_offset,
1569  downsampled);
1570 }
1571 
1573 {
1574  c->sbr_lf_gen = sbr_lf_gen;
1575  c->sbr_hf_assemble = sbr_hf_assemble;
1576  c->sbr_x_gen = sbr_x_gen;
1577  c->sbr_hf_inverse_filter = sbr_hf_inverse_filter;
1578 
1579 #if !USE_FIXED
1580  if(ARCH_MIPS)
1582 #endif
1583 }
SBRDSPContext::hf_gen
void(* hf_gen)(INTFLOAT(*X_high)[2], const INTFLOAT(*X_low)[2], const INTFLOAT alpha0[2], const INTFLOAT alpha1[2], INTFLOAT bw, int start, int end)
Definition: sbrdsp.h:37
sbr_qmf_synthesis
static void sbr_qmf_synthesis(FFTContext *mdct, SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp, INTFLOAT *out, INTFLOAT X[2][38][64], INTFLOAT mdct_buf[2][64], INTFLOAT *v0, int *v_off, const unsigned int div)
Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank (14496-3 sp04 p206)
Definition: aacsbr_template.c:1214
sbr_reset
static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
Definition: aacsbr_template.c:1094
SpectralBandReplication::bs_coupling
unsigned bs_coupling
Definition: sbr.h:156
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
ff_aac_sbr_init
av_cold void AAC_RENAME() ff_aac_sbr_init(void)
Initialize SBR.
Definition: aacsbr_template.c:49
SpectralBandReplication::data
SBRData data[2]
Definition: sbr.h:166
SpectralBandReplication::bs_limiter_gains
unsigned bs_limiter_gains
Definition: sbr.h:152
Q23
#define Q23(x)
Definition: aac_defines.h:94
SBRDSPContext
Definition: sbrdsp.h:28
out
FILE * out
Definition: movenc.c:54
elements
static const ElemCat * elements[ELEMENT_COUNT]
Definition: signature.h:566
n
int n
Definition: avisynth_c.h:760
SpectralBandReplication::e_origmapped
AAC_FLOAT e_origmapped[7][48]
Dequantized envelope scalefactors, remapped.
Definition: sbr.h:198
log2f
#define log2f(x)
Definition: libm.h:409
SBRData::env_facs
AAC_FLOAT env_facs[6][48]
Definition: sbr.h:100
sbr_chirp
static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
Chirp Factors (14496-3 sp04 p214)
Definition: aacsbr.c:195
ch
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
sbr_hf_calc_npatches
static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
Definition: aacsbr_template.c:487
ff_aac_sbr_ctx_close
av_cold void AAC_RENAME() ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
Close one SBR context.
Definition: aacsbr_template.c:115
AVFloatDSPContext::vector_fmul_reverse
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats, and store the result in a vector of floats...
Definition: float_dsp.h:154
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
sbr_hf_inverse_filter
static void sbr_hf_inverse_filter(SBRDSPContext *dsp, float(*alpha0)[2], float(*alpha1)[2], const float X_low[32][40][2], int k0)
High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering (14496-3 sp04 p214) Warning: Thi...
Definition: aacsbr.c:140
T_HUFFMAN_NOISE_BAL_3_0DB
@ T_HUFFMAN_NOISE_BAL_3_0DB
Definition: aacsbr.h:52
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
R
#define R
Definition: huffyuvdsp.h:34
SpectralBandReplication::m
AAC_SIGNE m[2]
M' and M respectively, M is the number of QMF subbands that use SBR.
Definition: sbr.h:162
b
#define b
Definition: input.c:41
table
static const uint16_t table[]
Definition: prosumer.c:206
SBRData::t_env_num_env_old
uint8_t t_env_num_env_old
Envelope time border of the last envelope of the previous frame.
Definition: sbr.h:107
SpectrumParameters::bs_alter_scale
uint8_t bs_alter_scale
Definition: sbr.h:52
ff_mdct_init
#define ff_mdct_init
Definition: fft.h:169
copy_sbr_grid
static void copy_sbr_grid(SBRData *dst, const SBRData *src)
Definition: aacsbr_template.c:777
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
SpectrumParameters::bs_start_freq
uint8_t bs_start_freq
Definition: sbr.h:43
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
ff_ps_init
av_cold void AAC_RENAME() ff_ps_init(void)
Definition: aacps.c:1000
AVFixedDSPContext
Definition: fixed_dsp.h:56
SBRData::t_env
uint8_t t_env[8]
Envelope time borders.
Definition: sbr.h:105
qsort_comparison_function_int16
static int qsort_comparison_function_int16(const void *a, const void *b)
Definition: aacsbr_template.c:121
read_sbr_envelope
static int read_sbr_envelope(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data, int ch)
Definition: aacsbr_template.c:813
vlc_sbr
static VLC vlc_sbr[10]
Definition: aacsbr.c:51
SpectralBandReplication::ready_for_dequant
int ready_for_dequant
Definition: sbr.h:142
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
SBR_VLC_ROW
#define SBR_VLC_ROW(name)
Definition: aacsbr.h:78
SBRData::e_a
int e_a[2]
l_APrev and l_A
Definition: sbr.h:87
T_HUFFMAN_NOISE_3_0DB
@ T_HUFFMAN_NOISE_3_0DB
Definition: aacsbr.h:51
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
T_HUFFMAN_ENV_BAL_1_5DB
@ T_HUFFMAN_ENV_BAL_1_5DB
Definition: aacsbr.h:45
SpectralBandReplication::bs_interpol_freq
unsigned bs_interpol_freq
Definition: sbr.h:153
v0
#define v0
Definition: regdef.h:26
VLC_TYPE
#define VLC_TYPE
Definition: vlc.h:24
U
#define U(x)
Definition: vp56_arith.h:37
SpectralBandReplication::n_master
AAC_SIGNE n_master
The number of frequency bands in f_master.
Definition: sbr.h:165
TYPE_CPE
@ TYPE_CPE
Definition: aac.h:57
GetBitContext
Definition: get_bits.h:61
SBRData::bs_add_harmonic_flag
unsigned bs_add_harmonic_flag
Definition: sbr.h:68
SpectralBandReplication::reset
int reset
Definition: sbr.h:144
read_sbr_channel_pair_element
static int read_sbr_channel_pair_element(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb)
Definition: aacsbr_template.c:998
src
#define src
Definition: vp8dsp.c:254
SBRData::noise_facs
AAC_FLOAT noise_facs[3][5]
Definition: sbr.h:103
sbr_hf_assemble
static void sbr_hf_assemble(float Y1[38][64][2], const float X_high[64][40][2], SpectralBandReplication *sbr, SBRData *ch_data, const int e_a[2])
Assembling HF Signals (14496-3 sp04 p220)
Definition: aacsbr.c:276
USE_FIXED
#define USE_FIXED
Definition: aac_defines.h:25
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
SpectrumParameters::bs_stop_freq
uint8_t bs_stop_freq
Definition: sbr.h:44
av_cold
#define av_cold
Definition: attributes.h:84
FF_PROFILE_AAC_HE_V2
#define FF_PROFILE_AAC_HE_V2
Definition: avcodec.h:2907
T_HUFFMAN_ENV_1_5DB
@ T_HUFFMAN_ENV_1_5DB
Definition: aacsbr.h:43
SBRData::bs_df_noise
uint8_t bs_df_noise[2]
Definition: sbr.h:73
W
#define W(a, i, v)
Definition: jpegls.h:124
g
const char * g
Definition: vf_curves.c:115
SBRData::bs_num_noise
AAC_SIGNE bs_num_noise
Definition: sbr.h:71
bits
uint8_t bits
Definition: vp3data.h:202
read_sbr_header
static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
Definition: aacsbr_template.c:187
sbr_make_f_derived
static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
Derived Frequency Band Tables (14496-3 sp04 p197)
Definition: aacsbr_template.c:545
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
sbr_offset
static const int8_t sbr_offset[6][16]
Definition: aacsbrdata.h:261
SpectrumParameters::bs_xover_band
uint8_t bs_xover_band
Definition: sbr.h:45
SpectralBandReplication::f_tablenoise
uint16_t f_tablenoise[6]
Frequency borders for noise floors.
Definition: sbr.h:181
f
#define f(width, name)
Definition: cbs_vp9.c:255
if
if(ret)
Definition: filter_design.txt:179
ff_sbr_apply
void AAC_RENAME() ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac, INTFLOAT *L, INTFLOAT *R)
Apply one SBR element to one AAC element.
Definition: aacsbr_template.c:1479
SpectralBandReplication::n_lim
AAC_SIGNE n_lim
Number of limiter bands.
Definition: sbr.h:173
SBRData::env_facs_q
uint8_t env_facs_q[6][48]
Envelope scalefactors.
Definition: sbr.h:99
NULL
#define NULL
Definition: coverity.c:32
SpectralBandReplication::f_tablelow
uint16_t f_tablelow[25]
Frequency borders for low resolution SBR.
Definition: sbr.h:177
FLOAT_0
static const SoftFloat FLOAT_0
0.0
Definition: softfloat.h:39
SBRData::f_indexnoise
unsigned f_indexnoise
Definition: sbr.h:110
SBRData::bs_df_env
uint8_t bs_df_env[5]
Definition: sbr.h:72
read_sbr_single_channel_element
static int read_sbr_single_channel_element(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb)
Definition: aacsbr_template.c:974
check_n_master
static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
Definition: aacsbr_template.c:250
SpectrumParameters::bs_freq_scale
uint8_t bs_freq_scale
Definition: sbr.h:51
SpectralBandReplication::f_tablelim
uint16_t f_tablelim[30]
Frequency borders for the limiter.
Definition: sbr.h:183
sbr_make_f_master
static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, SpectrumParameters *spectrum)
Master Frequency Band Table (14496-3 sp04 p194)
Definition: aacsbr_template.c:267
SBRData::bs_add_harmonic
uint8_t bs_add_harmonic[48]
Definition: sbr.h:75
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
AACSBRContext
aacsbr functions pointers
Definition: sbr.h:120
fixed_log
static int fixed_log(int x)
Definition: aacsbr_fixed.c:87
sbr_mapping
static int sbr_mapping(AACContext *ac, SpectralBandReplication *sbr, SBRData *ch_data, int e_a[2])
High Frequency Adjustment (14496-3 sp04 p217) and Mapping (14496-3 sp04 p217)
Definition: aacsbr_template.c:1365
sbr_qmf_window_ds
static INTFLOAT sbr_qmf_window_ds[320]
< window coefficients for analysis/synthesis QMF banks
Definition: aacsbr_tablegen_common.h:29
SBR_SYNTHESIS_BUF_SIZE
#define SBR_SYNTHESIS_BUF_SIZE
Definition: sbr.h:57
ff_decode_sbr_extension
int AAC_RENAME() ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb_host, int crc, int cnt, int id_aac)
Decode Spectral Band Replication extension data; reference: table 4.55.
Definition: aacsbr_template.c:1115
make_bands
static void make_bands(int16_t *bands, int start, int stop, int num_bands)
Definition: aacsbr.c:54
SpectralBandReplication::n
AAC_SIGNE n[2]
N_Low and N_High respectively, the number of frequency bands for low and high resolution.
Definition: sbr.h:169
SBRData::bs_num_env
AAC_SIGNE bs_num_env
Definition: sbr.h:69
SBRData::bs_amp_res
unsigned bs_amp_res
Definition: sbr.h:76
SBRData::s_indexmapped
uint8_t s_indexmapped[8][48]
Definition: sbr.h:97
read_sbr_dtdf
static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data)
Read how the envelope and noise floor data is delta coded.
Definition: aacsbr_template.c:795
SpectralBandReplication::bs_smoothing_mode
unsigned bs_smoothing_mode
Definition: sbr.h:154
sbr_x_gen
static int sbr_x_gen(SpectralBandReplication *sbr, INTFLOAT X[2][38][64], const INTFLOAT Y0[38][64][2], const INTFLOAT Y1[38][64][2], const INTFLOAT X_low[32][40][2], int ch)
Generate the subband filtered lowband.
Definition: aacsbr_template.c:1326
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
SpectrumParameters
Spectral Band Replication header - spectrum parameters that invoke a reset if they differ from the pr...
Definition: sbr.h:42
FFTContext::imdct_half
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:108
SpectralBandReplication::f_master
uint16_t f_master[49]
The master QMF frequency grouping.
Definition: sbr.h:175
sbr_qmf_analysis
static void sbr_qmf_analysis(AVFloatDSPContext *dsp, FFTContext *mdct, SBRDSPContext *sbrdsp, const INTFLOAT *in, INTFLOAT *x, INTFLOAT z[320], INTFLOAT W[2][32][32][2], int buf_idx)
Analysis QMF Bank (14496-3 sp04 p206)
Definition: aacsbr_template.c:1171
qsort.h
F_HUFFMAN_ENV_BAL_3_0DB
@ F_HUFFMAN_ENV_BAL_3_0DB
Definition: aacsbr.h:50
AACContext::avctx
AVCodecContext * avctx
Definition: aac.h:295
AVFloatDSPContext::vector_fmul
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats.
Definition: float_dsp.h:38
F_HUFFMAN_ENV_1_5DB
@ F_HUFFMAN_ENV_1_5DB
Definition: aacsbr.h:44
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
EXTENSION_ID_PS
@ EXTENSION_ID_PS
Definition: aacsbr.h:66
SpectralBandReplication::sample_rate
int sample_rate
Definition: sbr.h:140
ff_mdct_end
#define ff_mdct_end
Definition: fft.h:170
SBR_INIT_VLC_STATIC
#define SBR_INIT_VLC_STATIC(num, size)
Definition: aacsbr.h:72
sbr_turnoff
static void sbr_turnoff(SpectralBandReplication *sbr)
Places SBR in pure upsampling mode.
Definition: aacsbr_template.c:85
SpectralBandReplication
Spectral Band Replication.
Definition: sbr.h:139
AVFloatDSPContext
Definition: float_dsp.h:24
SpectralBandReplication::bs_amp_res_header
int bs_amp_res_header
Definition: sbr.h:146
SBRData::bs_invf_mode
uint8_t bs_invf_mode[2][5]
Definition: sbr.h:74
ff_ps_ctx_init
av_cold void AAC_RENAME() ff_ps_ctx_init(PSContext *ps)
Definition: aacps.c:1032
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
ff_aacsbr_func_ptr_init_mips
void ff_aacsbr_func_ptr_init_mips(AACSBRContext *c)
Definition: aacsbr_mips.c:611
SBRData::bs_freq_res
uint8_t bs_freq_res[7]
Definition: sbr.h:70
F_HUFFMAN_ENV_3_0DB
@ F_HUFFMAN_ENV_3_0DB
Definition: aacsbr.h:48
SpectralBandReplication::n_q
AAC_SIGNE n_q
Number of noise floor bands.
Definition: sbr.h:171
SpectralBandReplication::patch_start_subband
uint8_t patch_start_subband[6]
Definition: sbr.h:186
CONST_076923
static const int CONST_076923
Definition: aacsbr_fixed.c:79
read_sbr_extension
static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, int bs_extension_id, int *num_bits_left)
Definition: aacsbr_template.c:949
ff_sbrdsp_init
void AAC_RENAME() ff_sbrdsp_init(SBRDSPContext *s)
Definition: sbrdsp_template.c:76
aacsbr_func_ptr_init
static void aacsbr_func_ptr_init(AACSBRContext *c)
Definition: aacsbr_template.c:1572
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
SpectralBandReplication::id_aac
int id_aac
Definition: sbr.h:143
FFTContext
Definition: fft.h:88
SoftFloat
Definition: softfloat.h:34
CONST_RECIP_LN2
static const int CONST_RECIP_LN2
Definition: aacsbr_fixed.c:78
lrintf
#define lrintf(x)
Definition: libm_mips.h:70
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
ceil_log2
static const int8_t ceil_log2[]
ceil(log2(index+1))
Definition: aacsbr_template.c:626
AV_QSORT
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
Definition: qsort.h:33
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
SBRData
Spectral Band Replication per channel data.
Definition: sbr.h:62
read_sbr_noise
static int read_sbr_noise(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data, int ch)
Definition: aacsbr_template.c:902
SpectralBandReplication::k
AAC_SIGNE k[5]
k0, k1, k2
Definition: sbr.h:157
AVFloatDSPContext::vector_fmul_add
void(* vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len)
Calculate the entry wise product of two vectors of floats, add a third vector of floats and store the...
Definition: float_dsp.h:137
delta
float delta
Definition: vorbis_enc_data.h:457
in_table_int16
static int in_table_int16(const int16_t *table, int last_el, int16_t needle)
Definition: aacsbr_template.c:126
av_always_inline
#define av_always_inline
Definition: attributes.h:43
VARVAR
@ VARVAR
Definition: aacsbr.h:62
av_int2sf
static av_const SoftFloat av_int2sf(int v, int frac_bits)
Converts a mantisse and exponent to a SoftFloat.
Definition: softfloat.h:185
TYPE_LFE
@ TYPE_LFE
Definition: aac.h:59
uint8_t
uint8_t
Definition: audio_convert.c:194
SBRData::bs_frame_class
unsigned bs_frame_class
Definition: sbr.h:67
AAC_RENAME
#define AAC_RENAME(x)
Definition: aac_defines.h:84
sbr_make_f_tablelim
static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
Limiter Frequency Band Table (14496-3 sp04 p198)
Definition: aacsbr_template.c:136
TYPE_SCE
@ TYPE_SCE
Definition: aac.h:56
AACContext::oc
OutputConfiguration oc[2]
Definition: aac.h:356
read_sbr_invf
static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data)
Read inverse filtering data.
Definition: aacsbr_template.c:803
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:106
ret
ret
Definition: filter_design.txt:187
sbr_dequant
static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
Dequantization and stereo decoding (14496-3 sp04 p203)
Definition: aacsbr.c:73
vlc_sbr_lav
static const int8_t vlc_sbr_lav[10]
Definition: aacsbr.h:69
array_min_int16
static int array_min_int16(const int16_t *array, int nel)
Definition: aacsbr_template.c:242
L
#define L(x)
Definition: vp56_arith.h:36
FIXFIX
@ FIXFIX
Definition: aacsbr.h:59
aacsbr_tableinit
static av_cold void aacsbr_tableinit(void)
Definition: aacsbr_template.c:37
T_HUFFMAN_ENV_3_0DB
@ T_HUFFMAN_ENV_3_0DB
Definition: aacsbr.h:47
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
ff_aac_sbr_ctx_init
av_cold void AAC_RENAME() ff_aac_sbr_ctx_init(AACContext *ac, SpectralBandReplication *sbr, int id_aac)
Initialize one SBR context.
Definition: aacsbr_template.c:96
read_sbr_data
static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, int id_aac)
Definition: aacsbr_template.c:1050
MPEG4AudioConfig::ps
int ps
-1 implicit, 1 presence
Definition: mpeg4audio.h:44
sbr_env_estimate
static void sbr_env_estimate(AAC_FLOAT(*e_curr)[48], INTFLOAT X_high[64][40][2], SpectralBandReplication *sbr, SBRData *ch_data)
Estimation of current envelope (14496-3 sp04 p218)
Definition: aacsbr_template.c:1420
SBRData::t_q
uint8_t t_q[3]
Noise time borders.
Definition: sbr.h:109
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:2898
SpectralBandReplication::spectrum_params
SpectrumParameters spectrum_params
Definition: sbr.h:145
av_add_sf
static av_const SoftFloat av_add_sf(SoftFloat a, SoftFloat b)
Definition: softfloat.h:162
sbr_lf_gen
static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr, INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2], int buf_idx)
Generate the subband filtered lowband.
Definition: aacsbr_template.c:1266
temp
else temp
Definition: vf_mcdeint.c:256
SpectralBandReplication::f_tablehigh
uint16_t f_tablehigh[49]
Frequency borders for high resolution SBR.
Definition: sbr.h:179
SBRData::noise_facs_q
uint8_t noise_facs_q[3][5]
Noise scalefactors.
Definition: sbr.h:102
sbr_hf_gen
static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr, INTFLOAT X_high[64][40][2], const INTFLOAT X_low[32][40][2], const INTFLOAT(*alpha0)[2], const INTFLOAT(*alpha1)[2], const INTFLOAT bw_array[5], const uint8_t *t_env, int bs_num_env)
High Frequency Generator (14496-3 sp04 p215)
Definition: aacsbr_template.c:1291
sbr_gain_calc
static void sbr_gain_calc(AACContext *ac, SpectralBandReplication *sbr, SBRData *ch_data, const int e_a[2])
Calculation of levels of additional HF signal components (14496-3 sp04 p219) and Calculation of gain ...
Definition: aacsbr.c:219
AAC_RENAME_32
#define AAC_RENAME_32(x)
Definition: aac_defines.h:85
SpectralBandReplication::bs_limiter_bands
unsigned bs_limiter_bands
Definition: sbr.h:151
SpectralBandReplication::patch_num_subbands
uint8_t patch_num_subbands[6]
Definition: sbr.h:185
OutputConfiguration::m4ac
MPEG4AudioConfig m4ac
Definition: aac.h:124
TYPE_CCE
@ TYPE_CCE
Definition: aac.h:58
sbr_qmf_window_us
static INTFLOAT sbr_qmf_window_us[640]
Definition: aacsbr_tablegen_common.h:30
SpectralBandReplication::start
int start
Definition: sbr.h:141
F_HUFFMAN_ENV_BAL_1_5DB
@ F_HUFFMAN_ENV_BAL_1_5DB
Definition: aacsbr.h:46
FIXVAR
@ FIXVAR
Definition: aacsbr.h:60
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:39
SBRDSPContext::sum_square
AAC_FLOAT(* sum_square)(INTFLOAT(*x)[2], int n)
Definition: sbrdsp.h:30
ENVELOPE_ADJUSTMENT_OFFSET
#define ENVELOPE_ADJUSTMENT_OFFSET
Definition: aacsbr.h:36
AAC_FLOAT
float AAC_FLOAT
Definition: aac_defines.h:90
SpectrumParameters::bs_noise_bands
uint8_t bs_noise_bands
Definition: sbr.h:53
AACContext
main AAC context
Definition: aac.h:293
VARFIX
@ VARFIX
Definition: aacsbr.h:61
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ff_ps_apply
int AAC_RENAME() ff_ps_apply(AVCodecContext *avctx, PSContext *ps, INTFLOAT L[2][38][64], INTFLOAT R[2][38][64], int top)
Definition: aacps.c:970
SpectralBandReplication::q_mapped
AAC_FLOAT q_mapped[7][48]
Dequantized noise scalefactors, remapped.
Definition: sbr.h:200
get_bits1_vector
static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec, int elements)
Definition: aacsbr_template.c:616
int
int
Definition: ffmpeg_filter.c:191
VLC::table
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
SpectralBandReplication::kx
AAC_SIGNE kx[2]
kx', and kx respectively, kx is the first QMF subband where SBR is used.
Definition: sbr.h:160
av_mul_sf
static av_const SoftFloat av_mul_sf(SoftFloat a, SoftFloat b)
Definition: softfloat.h:102
ff_ps_read_data
int AAC_RENAME() ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
Definition: aacps.c:147
INTFLOAT
float INTFLOAT
Definition: aac_defines.h:86
SpectralBandReplication::s_mapped
uint8_t s_mapped[7][48]
Sinusoidal presence, remapped.
Definition: sbr.h:202
read_sbr_grid
static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data)
Definition: aacsbr_template.c:630
T_HUFFMAN_ENV_BAL_3_0DB
@ T_HUFFMAN_ENV_BAL_3_0DB
Definition: aacsbr.h:49
SpectralBandReplication::ps
PSContext ps
Definition: sbr.h:167
min
float min
Definition: vorbis_enc_data.h:456
SpectralBandReplication::num_patches
AAC_SIGNE num_patches
Definition: sbr.h:184
SpectralBandReplication::dsp
SBRDSPContext dsp
Definition: sbr.h:213