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