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 "aac/aacdec.h"
36 #include "aac/aacdec_tab.h"
37 #include "avcodec.h"
38 #include "libavutil/qsort.h"
39 #include "libavutil/mem.h"
40 
41 typedef struct ExtChannelElement {
46 
48 {
49  return &((ExtChannelElement*)ch)->sbr;
50 }
51 
53 {
55 }
56 
57 /** Places SBR in pure upsampling mode. */
59  sbr->start = 0;
60  sbr->usac = 0;
61  sbr->ready_for_dequant = 0;
62  // Init defults used in pure upsampling mode
63  sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
64  sbr->m[1] = 0;
65  // Reset values for first SBR header
66  sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
67  memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
68 }
69 
71  ChannelElement **che, int id_aac)
72 {
74  ExtChannelElement *ext = av_mallocz(sizeof(*ext));
75  int ret;
76  float scale;
77 
78  if (!ext)
79  return AVERROR(ENOMEM);
80  *che = &ext->ch;
81  sbr = &ext->sbr;
82  ext->ch.ch[0].AAC_RENAME(predictor_state) = ext->predictor_state[0];
83  ext->ch.ch[1].AAC_RENAME(predictor_state) = ext->predictor_state[1];
84 
85  sbr->kx[0] = sbr->kx[1];
86  sbr->id_aac = id_aac;
87  sbr_turnoff(sbr);
90  /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
91  * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
92  * and scale back down at synthesis. */
93 
94  scale = USE_FIXED ? 1 : 1.0 / (64 * 32768);
95  ret = av_tx_init(&sbr->mdct, &sbr->mdct_fn,
97  1, 64, &scale, 0);
98  if (ret < 0)
99  return ret;
100 
101  scale = USE_FIXED ? -1.0 : -2.0 * 32768;
102  ret = av_tx_init(&sbr->mdct_ana, &sbr->mdct_ana_fn,
104  1, 64, &scale, 0);
105  if (ret < 0)
106  return ret;
107 
108  AAC_RENAME(ff_ps_ctx_init)(&sbr->ps);
109  AAC_RENAME(ff_sbrdsp_init)(&sbr->dsp);
110  aacsbr_func_ptr_init(&sbr->c);
111 
112  return 0;
113 }
114 
116 {
117  SpectralBandReplication *sbr = get_sbr(che);
118  av_tx_uninit(&sbr->mdct);
119  av_tx_uninit(&sbr->mdct_ana);
120 }
121 
122 static int qsort_comparison_function_int16(const void *a, const void *b)
123 {
124  return *(const int16_t *)a - *(const int16_t *)b;
125 }
126 
127 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
128 {
129  int i;
130  for (i = 0; i <= last_el; i++)
131  if (table[i] == needle)
132  return 1;
133  return 0;
134 }
135 
136 /// Limiter Frequency Band Table (14496-3 sp04 p198)
138 {
139  int k;
140  if (sbr->bs_limiter_bands > 0) {
141  static const INTFLOAT bands_warped[3] = { Q23(1.32715174233856803909f), //2^(0.49/1.2)
142  Q23(1.18509277094158210129f), //2^(0.49/2)
143  Q23(1.11987160404675912501f) }; //2^(0.49/3)
144  const INTFLOAT lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
145  int16_t patch_borders[7];
146  uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
147 
148  patch_borders[0] = sbr->kx[1];
149  for (k = 1; k <= sbr->num_patches; k++)
150  patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
151 
152  memcpy(sbr->f_tablelim, sbr->f_tablelow,
153  (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
154  if (sbr->num_patches > 1)
155  memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
156  (sbr->num_patches - 1) * sizeof(patch_borders[0]));
157 
158  AV_QSORT(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
159  uint16_t,
161 
162  sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
163  while (out < sbr->f_tablelim + sbr->n_lim) {
164 #if USE_FIXED
165  if ((*in << 23) >= *out * lim_bands_per_octave_warped) {
166 #else
167  if (*in >= *out * lim_bands_per_octave_warped) {
168 #endif /* USE_FIXED */
169  *++out = *in++;
170  } else if (*in == *out ||
171  !in_table_int16(patch_borders, sbr->num_patches, *in)) {
172  in++;
173  sbr->n_lim--;
174  } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
175  *out = *in++;
176  sbr->n_lim--;
177  } else {
178  *++out = *in++;
179  }
180  }
181  } else {
182  sbr->f_tablelim[0] = sbr->f_tablelow[0];
183  sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
184  sbr->n_lim = 1;
185  }
186 }
187 
188 static unsigned int read_sbr_header(SpectralBandReplication *sbr,
189  GetBitContext *gb, int is_usac)
190 {
191  unsigned int cnt = get_bits_count(gb);
192  uint8_t bs_header_extra_1;
193  uint8_t bs_header_extra_2;
194  int old_bs_limiter_bands = sbr->bs_limiter_bands;
195  SpectrumParameters old_spectrum_params;
196 
197  sbr->start = 1;
198  sbr->ready_for_dequant = 0;
199  sbr->usac = is_usac;
200 
201  // Save last spectrum parameters variables to compare to new ones
202  memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
203 
204  if (!is_usac)
205  sbr->bs_amp_res_header = get_bits1(gb);
206 
207  sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
208  sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
209 
210  if (!is_usac)
211  sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
212  skip_bits(gb, 2); // bs_reserved
213 
214  bs_header_extra_1 = get_bits1(gb);
215  bs_header_extra_2 = get_bits1(gb);
216 
217  if (bs_header_extra_1) {
218  sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
221  } else {
225  }
226 
227  // Check if spectrum parameters changed
228  if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
229  sbr->reset = 1;
230 
231  if (bs_header_extra_2) {
232  sbr->bs_limiter_bands = get_bits(gb, 2);
233  sbr->bs_limiter_gains = get_bits(gb, 2);
234  sbr->bs_interpol_freq = get_bits1(gb);
235  sbr->bs_smoothing_mode = get_bits1(gb);
236  } else {
237  sbr->bs_limiter_bands = 2;
238  sbr->bs_limiter_gains = 2;
239  sbr->bs_interpol_freq = 1;
240  sbr->bs_smoothing_mode = 1;
241  }
242 
243  if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
244  sbr_make_f_tablelim(sbr);
245 
246  return get_bits_count(gb) - cnt;
247 }
248 
249 static int array_min_int16(const int16_t *array, int nel)
250 {
251  int i, min = array[0];
252  for (i = 1; i < nel; i++)
253  min = FFMIN(array[i], min);
254  return min;
255 }
256 
257 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
258 {
259  // Requirements (14496-3 sp04 p205)
260  if (n_master <= 0) {
261  av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
262  return -1;
263  }
264  if (bs_xover_band >= n_master) {
265  av_log(avctx, AV_LOG_ERROR,
266  "Invalid bitstream, crossover band index beyond array bounds: %d\n",
267  bs_xover_band);
268  return -1;
269  }
270  return 0;
271 }
272 
273 /// Master Frequency Band Table (14496-3 sp04 p194)
275  SpectrumParameters *spectrum)
276 {
277  unsigned int temp, max_qmf_subbands = 0;
278  unsigned int start_min, stop_min;
279  int k;
280  const int8_t *sbr_offset_ptr;
281  int16_t stop_dk[13];
282 
283  switch (sbr->sample_rate) {
284  case 16000:
285  sbr_offset_ptr = sbr_offset[0];
286  break;
287  case 22050:
288  sbr_offset_ptr = sbr_offset[1];
289  break;
290  case 24000:
291  sbr_offset_ptr = sbr_offset[2];
292  break;
293  case 32000:
294  sbr_offset_ptr = sbr_offset[3];
295  break;
296  case 44100: case 48000: case 64000:
297  sbr_offset_ptr = sbr_offset[4];
298  break;
299  case 88200: case 96000: case 128000: case 176400: case 192000:
300  sbr_offset_ptr = sbr_offset[5];
301  break;
302  default:
304  "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
305  return -1;
306  }
307 
308  if (sbr->sample_rate < 32000) {
309  temp = 3000;
310  } else if (sbr->sample_rate < 64000) {
311  temp = 4000;
312  } else
313  temp = 5000;
314 
315  start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
316  stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
317 
318  sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
319 
320  if (spectrum->bs_stop_freq < 14) {
321  sbr->k[2] = stop_min;
322  make_bands(stop_dk, stop_min, 64, 13);
323  AV_QSORT(stop_dk, 13, int16_t, qsort_comparison_function_int16);
324  for (k = 0; k < spectrum->bs_stop_freq; k++)
325  sbr->k[2] += stop_dk[k];
326  } else if (spectrum->bs_stop_freq == 14) {
327  sbr->k[2] = 2*sbr->k[0];
328  } else if (spectrum->bs_stop_freq == 15) {
329  sbr->k[2] = 3*sbr->k[0];
330  } else {
332  "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
333  return -1;
334  }
335  sbr->k[2] = FFMIN(64, sbr->k[2]);
336 
337  // Requirements (14496-3 sp04 p205)
338  if (sbr->sample_rate <= 32000) {
339  max_qmf_subbands = 48;
340  } else if (sbr->sample_rate == 44100) {
341  max_qmf_subbands = 35;
342  } else if (sbr->sample_rate >= 48000)
343  max_qmf_subbands = 32;
344  else
345  av_assert0(0);
346 
347  if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
349  "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
350  return -1;
351  }
352 
353  if (!spectrum->bs_freq_scale) {
354  int dk, k2diff;
355 
356  dk = spectrum->bs_alter_scale + 1;
357  sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
359  return -1;
360 
361  for (k = 1; k <= sbr->n_master; k++)
362  sbr->f_master[k] = dk;
363 
364  k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
365  if (k2diff < 0) {
366  sbr->f_master[1]--;
367  sbr->f_master[2]-= (k2diff < -1);
368  } else if (k2diff) {
369  sbr->f_master[sbr->n_master]++;
370  }
371 
372  sbr->f_master[0] = sbr->k[0];
373  for (k = 1; k <= sbr->n_master; k++)
374  sbr->f_master[k] += sbr->f_master[k - 1];
375 
376  } else {
377  int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3}
378  int two_regions, num_bands_0;
379  int vdk0_max, vdk1_min;
380  int16_t vk0[49];
381 #if USE_FIXED
382  int tmp, nz = 0;
383 #endif /* USE_FIXED */
384 
385  if (49 * sbr->k[2] > 110 * sbr->k[0]) {
386  two_regions = 1;
387  sbr->k[1] = 2 * sbr->k[0];
388  } else {
389  two_regions = 0;
390  sbr->k[1] = sbr->k[2];
391  }
392 
393 #if USE_FIXED
394  tmp = (sbr->k[1] << 23) / sbr->k[0];
395  while (tmp < 0x40000000) {
396  tmp <<= 1;
397  nz++;
398  }
399  tmp = fixed_log(tmp - 0x80000000);
400  tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
401  tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
402  num_bands_0 = ((tmp + 0x400000) >> 23) * 2;
403 #else
404  num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
405 #endif /* USE_FIXED */
406 
407  if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
408  av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
409  return -1;
410  }
411 
412  vk0[0] = 0;
413 
414  make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
415 
416  AV_QSORT(vk0 + 1, num_bands_0, int16_t, qsort_comparison_function_int16);
417  vdk0_max = vk0[num_bands_0];
418 
419  vk0[0] = sbr->k[0];
420  for (k = 1; k <= num_bands_0; k++) {
421  if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
422  av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
423  return -1;
424  }
425  vk0[k] += vk0[k-1];
426  }
427 
428  if (two_regions) {
429  int16_t vk1[49];
430 #if USE_FIXED
431  int num_bands_1;
432 
433  tmp = (sbr->k[2] << 23) / sbr->k[1];
434  nz = 0;
435  while (tmp < 0x40000000) {
436  tmp <<= 1;
437  nz++;
438  }
439  tmp = fixed_log(tmp - 0x80000000);
440  tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
441  tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
442  if (spectrum->bs_alter_scale)
443  tmp = (int)(((int64_t)tmp * CONST_076923 + 0x40000000) >> 31);
444  num_bands_1 = ((tmp + 0x400000) >> 23) * 2;
445 #else
446  float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
447  : 1.0f; // bs_alter_scale = {0,1}
448  int num_bands_1 = lrintf(half_bands * invwarp *
449  log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
450 #endif /* USE_FIXED */
451  make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
452 
453  vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
454 
455  if (vdk1_min < vdk0_max) {
456  int change;
457  AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
458  change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
459  vk1[1] += change;
460  vk1[num_bands_1] -= change;
461  }
462 
463  AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
464 
465  vk1[0] = sbr->k[1];
466  for (k = 1; k <= num_bands_1; k++) {
467  if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
468  av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
469  return -1;
470  }
471  vk1[k] += vk1[k-1];
472  }
473 
474  sbr->n_master = num_bands_0 + num_bands_1;
476  return -1;
477  memcpy(&sbr->f_master[0], vk0,
478  (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
479  memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
480  num_bands_1 * sizeof(sbr->f_master[0]));
481 
482  } else {
483  sbr->n_master = num_bands_0;
485  return -1;
486  memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
487  }
488  }
489 
490  return 0;
491 }
492 
493 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
495 {
496  int i, k, last_k = -1, last_msb = -1, sb = 0;
497  int msb = sbr->k[0];
498  int usb = sbr->kx[1];
499  int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
500 
501  sbr->num_patches = 0;
502 
503  if (goal_sb < sbr->kx[1] + sbr->m[1]) {
504  for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
505  } else
506  k = sbr->n_master;
507 
508  do {
509  int odd = 0;
510  if (k == last_k && msb == last_msb) {
511  av_log(ac->avctx, AV_LOG_ERROR, "patch construction failed\n");
512  return AVERROR_INVALIDDATA;
513  }
514  last_k = k;
515  last_msb = msb;
516  for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
517  sb = sbr->f_master[i];
518  odd = (sb + sbr->k[0]) & 1;
519  }
520 
521  // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
522  // After this check the final number of patches can still be six which is
523  // illegal however the Coding Technologies decoder check stream has a final
524  // count of 6 patches
525  if (sbr->num_patches > 5) {
526  av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
527  return -1;
528  }
529 
530  sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
531  sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
532 
533  if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
534  usb = sb;
535  msb = sb;
536  sbr->num_patches++;
537  } else
538  msb = sbr->kx[1];
539 
540  if (sbr->f_master[k] - sb < 3)
541  k = sbr->n_master;
542  } while (sb != sbr->kx[1] + sbr->m[1]);
543 
544  if (sbr->num_patches > 1 &&
545  sbr->patch_num_subbands[sbr->num_patches - 1] < 3)
546  sbr->num_patches--;
547 
548  return 0;
549 }
550 
551 /// Derived Frequency Band Tables (14496-3 sp04 p197)
553 {
554  int k, temp;
555 #if USE_FIXED
556  int nz = 0;
557 #endif /* USE_FIXED */
558 
559  sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
560  sbr->n[0] = (sbr->n[1] + 1) >> 1;
561 
562  memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
563  (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
564  sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
565  sbr->kx[1] = sbr->f_tablehigh[0];
566 
567  // Requirements (14496-3 sp04 p205)
568  if (sbr->kx[1] + sbr->m[1] > 64) {
570  "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
571  return -1;
572  }
573  if (sbr->kx[1] > 32) {
574  av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
575  return -1;
576  }
577 
578  sbr->f_tablelow[0] = sbr->f_tablehigh[0];
579  temp = sbr->n[1] & 1;
580  for (k = 1; k <= sbr->n[0]; k++)
581  sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
582 #if USE_FIXED
583  temp = (sbr->k[2] << 23) / sbr->kx[1];
584  while (temp < 0x40000000) {
585  temp <<= 1;
586  nz++;
587  }
588  temp = fixed_log(temp - 0x80000000);
589  temp = (int)(((int64_t)temp * CONST_RECIP_LN2 + 0x20000000) >> 30);
590  temp = (((temp + 0x80) >> 8) + ((8 - nz) << 23)) * sbr->spectrum_params.bs_noise_bands;
591 
592  sbr->n_q = (temp + 0x400000) >> 23;
593  if (sbr->n_q < 1)
594  sbr->n_q = 1;
595 #else
597  log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
598 #endif /* USE_FIXED */
599 
600  if (sbr->n_q > 5) {
601  av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
602  return -1;
603  }
604 
605  sbr->f_tablenoise[0] = sbr->f_tablelow[0];
606  temp = 0;
607  for (k = 1; k <= sbr->n_q; k++) {
608  temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
609  sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
610  }
611 
612  if (sbr_hf_calc_npatches(ac, sbr) < 0)
613  return -1;
614 
615  sbr_make_f_tablelim(sbr);
616 
617  sbr->data[0].f_indexnoise = 0;
618  sbr->data[1].f_indexnoise = 0;
619 
620  return 0;
621 }
622 
623 static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
624  int elements)
625 {
626  int i;
627  for (i = 0; i < elements; i++) {
628  vec[i] = get_bits1(gb);
629  }
630 }
631 
632 /** ceil(log2(index+1)) */
633 static const int8_t ceil_log2[] = {
634  0, 1, 2, 2, 3, 3,
635 };
636 
638  GetBitContext *gb, SBRData *ch_data)
639 {
640  int i;
641  int bs_pointer = 0;
642  // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
643  int abs_bord_trail = 16;
644  int num_rel_lead, num_rel_trail;
645  unsigned bs_num_env_old = ch_data->bs_num_env;
646  int bs_frame_class, bs_num_env;
647 
648  ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
649  ch_data->bs_amp_res = sbr->bs_amp_res_header;
650  ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
651 
652  switch (bs_frame_class = get_bits(gb, 2)) {
653  case FIXFIX:
654  bs_num_env = 1 << get_bits(gb, 2);
655  if (bs_num_env > (sbr->usac ? 8 : 5)) {
657  "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
658  bs_num_env);
659  return -1;
660  }
661  ch_data->bs_num_env = bs_num_env;
662  num_rel_lead = ch_data->bs_num_env - 1;
663  if (ch_data->bs_num_env == 1)
664  ch_data->bs_amp_res = 0;
665 
666 
667  ch_data->t_env[0] = 0;
668  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
669 
670  abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
671  ch_data->bs_num_env;
672  for (i = 0; i < num_rel_lead; i++)
673  ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
674 
675  ch_data->bs_freq_res[1] = get_bits1(gb);
676  for (i = 1; i < ch_data->bs_num_env; i++)
677  ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
678  break;
679  case FIXVAR:
680  abs_bord_trail += get_bits(gb, 2);
681  num_rel_trail = get_bits(gb, 2);
682  ch_data->bs_num_env = num_rel_trail + 1;
683  ch_data->t_env[0] = 0;
684  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
685 
686  for (i = 0; i < num_rel_trail; i++)
687  ch_data->t_env[ch_data->bs_num_env - 1 - i] =
688  ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
689 
690  bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
691 
692  for (i = 0; i < ch_data->bs_num_env; i++)
693  ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
694  break;
695  case VARFIX:
696  ch_data->t_env[0] = get_bits(gb, 2);
697  num_rel_lead = get_bits(gb, 2);
698  ch_data->bs_num_env = num_rel_lead + 1;
699  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
700 
701  for (i = 0; i < num_rel_lead; i++)
702  ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
703 
704  bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
705 
706  get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
707  break;
708  case VARVAR:
709  ch_data->t_env[0] = get_bits(gb, 2);
710  abs_bord_trail += get_bits(gb, 2);
711  num_rel_lead = get_bits(gb, 2);
712  num_rel_trail = get_bits(gb, 2);
713  bs_num_env = num_rel_lead + num_rel_trail + 1;
714 
715  if (bs_num_env > 5) {
717  "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
718  bs_num_env);
719  return -1;
720  }
721  ch_data->bs_num_env = bs_num_env;
722 
723  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
724 
725  for (i = 0; i < num_rel_lead; i++)
726  ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
727  for (i = 0; i < num_rel_trail; i++)
728  ch_data->t_env[ch_data->bs_num_env - 1 - i] =
729  ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
730 
731  bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
732 
733  get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
734  break;
735  }
736  ch_data->bs_frame_class = bs_frame_class;
737 
738  av_assert0(bs_pointer >= 0);
739  if (bs_pointer > ch_data->bs_num_env + 1) {
741  "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
742  bs_pointer);
743  return -1;
744  }
745 
746  for (i = 1; i <= ch_data->bs_num_env; i++) {
747  if (ch_data->t_env[i-1] >= ch_data->t_env[i]) {
748  av_log(ac->avctx, AV_LOG_ERROR, "Not strictly monotone time borders\n");
749  return -1;
750  }
751  }
752 
753  ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
754 
755  ch_data->t_q[0] = ch_data->t_env[0];
756  ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
757  if (ch_data->bs_num_noise > 1) {
758  int idx;
759  if (ch_data->bs_frame_class == FIXFIX) {
760  idx = ch_data->bs_num_env >> 1;
761  } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
762  idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
763  } else { // VARFIX
764  if (!bs_pointer)
765  idx = 1;
766  else if (bs_pointer == 1)
767  idx = ch_data->bs_num_env - 1;
768  else // bs_pointer > 1
769  idx = bs_pointer - 1;
770  }
771  ch_data->t_q[1] = ch_data->t_env[idx];
772  }
773 
774  ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
775  ch_data->e_a[1] = -1;
776  if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
777  ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
778  } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
779  ch_data->e_a[1] = bs_pointer - 1;
780 
781  return 0;
782 }
783 
784 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
785  //These variables are saved from the previous frame rather than copied
786  dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
787  dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
788  dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
789 
790  //These variables are read from the bitstream and therefore copied
791  memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
792  memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
793  memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
794  dst->bs_num_env = src->bs_num_env;
795  dst->bs_amp_res = src->bs_amp_res;
796  dst->bs_num_noise = src->bs_num_noise;
797  dst->bs_frame_class = src->bs_frame_class;
798  dst->e_a[1] = src->e_a[1];
799 }
800 
801 /// Read how the envelope and noise floor data is delta coded
803  SBRData *ch_data, int indep_flag)
804 {
805  if (sbr->usac) {
806  if (indep_flag) {
807  ch_data->bs_df_env[0] = 0;
808  get_bits1_vector(gb, &ch_data->bs_df_env[1], ch_data->bs_num_env - 1);
809  } else {
810  get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
811  }
812 
813  if (indep_flag) {
814  ch_data->bs_df_noise[0] = 0;
815  get_bits1_vector(gb, &ch_data->bs_df_noise[1], ch_data->bs_num_noise - 1);
816  } else {
817  get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
818  }
819  } else {
820  get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
821  get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
822  }
823 }
824 
825 /// Read inverse filtering data
827  SBRData *ch_data)
828 {
829  int i;
830 
831  memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
832  for (i = 0; i < sbr->n_q; i++)
833  ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
834 }
835 
837  SBRData *ch_data, int ch)
838 {
839  int bits;
840  int i, j, k;
841  const VLCElem *t_huff, *f_huff;
842  const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
843  const int odd = sbr->n[1] & 1;
844 
845  if (sbr->bs_coupling && ch) {
846  if (ch_data->bs_amp_res) {
847  bits = 5;
850  } else {
851  bits = 6;
854  }
855  } else {
856  if (ch_data->bs_amp_res) {
857  bits = 6;
860  } else {
861  bits = 7;
864  }
865  }
866 
867  for (i = 0; i < ch_data->bs_num_env; i++) {
868  if (ch_data->bs_df_env[i]) {
869  // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
870  if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
871  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
872  ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][j] + delta * get_vlc2(gb, t_huff, 9, 3);
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  } else if (ch_data->bs_freq_res[i + 1]) {
879  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
880  k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
881  ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * get_vlc2(gb, t_huff, 9, 3);
882  if (ch_data->env_facs_q[i + 1][j] > 127U) {
883  av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
884  return AVERROR_INVALIDDATA;
885  }
886  }
887  } else {
888  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
889  k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
890  ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * get_vlc2(gb, t_huff, 9, 3);
891  if (ch_data->env_facs_q[i + 1][j] > 127U) {
892  av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
893  return AVERROR_INVALIDDATA;
894  }
895  }
896  }
897  } else {
898  ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
899  for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
900  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);
901  if (ch_data->env_facs_q[i + 1][j] > 127U) {
902  av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
903  return AVERROR_INVALIDDATA;
904  }
905  }
906  }
907  if (sbr->usac) {
908  if (sbr->inter_tes) {
909  ch_data->temp_shape[i] = get_bits(gb, 1);
910  if (ch_data->temp_shape[i])
911  ch_data->temp_shape_mode[i] = get_bits(gb, 2);
912  }
913  }
914  }
915 
916  //assign 0th elements of env_facs_q from last elements
917  memcpy(ch_data->env_facs_q[0], ch_data->env_facs_q[ch_data->bs_num_env],
918  sizeof(ch_data->env_facs_q[0]));
919 
920  return 0;
921 }
922 
924  SBRData *ch_data, int ch)
925 {
926  int i, j;
927  const VLCElem *t_huff, *f_huff;
928  int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
929 
930  if (sbr->bs_coupling && ch) {
933  } else {
936  }
937 
938  for (i = 0; i < ch_data->bs_num_noise; i++) {
939  if (ch_data->bs_df_noise[i]) {
940  for (j = 0; j < sbr->n_q; j++) {
941  ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i][j] + delta * get_vlc2(gb, t_huff, 9, 2);
942  if (ch_data->noise_facs_q[i + 1][j] > 30U) {
943  av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
944  return AVERROR_INVALIDDATA;
945  }
946  }
947  } else {
948  ch_data->noise_facs_q[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
949  for (j = 1; j < sbr->n_q; j++) {
950  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);
951  if (ch_data->noise_facs_q[i + 1][j] > 30U) {
952  av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
953  return AVERROR_INVALIDDATA;
954  }
955  }
956  }
957  }
958 
959  //assign 0th elements of noise_facs_q from last elements
960  memcpy(ch_data->noise_facs_q[0], ch_data->noise_facs_q[ch_data->bs_num_noise],
961  sizeof(ch_data->noise_facs_q[0]));
962  return 0;
963 }
964 
966  GetBitContext *gb,
967  int bs_extension_id, int *num_bits_left)
968 {
969  switch (bs_extension_id) {
970  case EXTENSION_ID_PS:
971  if (!ac->oc[1].m4ac.ps) {
972  av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
973  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
974  *num_bits_left = 0;
975  } else {
976  *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps.common, *num_bits_left);
978  // ensure the warning is not printed if PS extension is present
979  ac->warned_he_aac_mono = 1;
980  }
981  break;
982  default:
983  // some files contain 0-padding
984  if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
985  avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
986  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
987  *num_bits_left = 0;
988  break;
989  }
990 }
991 
994  GetBitContext *gb)
995 {
996  int ret;
997 
998  if (get_bits1(gb)) // bs_data_extra
999  skip_bits(gb, 4); // bs_reserved
1000 
1001  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1002  return -1;
1003  read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
1004  read_sbr_invf(sbr, gb, &sbr->data[0]);
1005  if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1006  return ret;
1007  if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1008  return ret;
1009 
1010  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1011  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1012 
1013  return 0;
1014 }
1015 
1018  GetBitContext *gb)
1019 {
1020  int ret;
1021 
1022  if (get_bits1(gb)) // bs_data_extra
1023  skip_bits(gb, 8); // bs_reserved
1024 
1025  if ((sbr->bs_coupling = get_bits1(gb))) {
1026  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1027  return -1;
1028  copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
1029  read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
1030  read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
1031  read_sbr_invf(sbr, gb, &sbr->data[0]);
1032  memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
1033  memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
1034  if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 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_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1039  return ret;
1040  if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1041  return ret;
1042  } else {
1043  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
1044  read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
1045  return -1;
1046  read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
1047  read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
1048  read_sbr_invf(sbr, gb, &sbr->data[0]);
1049  read_sbr_invf(sbr, gb, &sbr->data[1]);
1050  if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1051  return ret;
1052  if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1053  return ret;
1054  if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1055  return ret;
1056  if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1057  return ret;
1058  }
1059 
1060  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1061  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1062  if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1063  get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1064 
1065  return 0;
1066 }
1067 
1069  GetBitContext *gb, int id_aac)
1070 {
1071  unsigned int cnt = get_bits_count(gb);
1072 
1073  sbr->id_aac = id_aac;
1074  sbr->ready_for_dequant = 1;
1075 
1076  if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
1077  if (read_sbr_single_channel_element(ac, sbr, gb)) {
1078  sbr_turnoff(sbr);
1079  return get_bits_count(gb) - cnt;
1080  }
1081  } else if (id_aac == TYPE_CPE) {
1082  if (read_sbr_channel_pair_element(ac, sbr, gb)) {
1083  sbr_turnoff(sbr);
1084  return get_bits_count(gb) - cnt;
1085  }
1086  } else {
1087  av_log(ac->avctx, AV_LOG_ERROR,
1088  "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1089  sbr_turnoff(sbr);
1090  return get_bits_count(gb) - cnt;
1091  }
1092  if (get_bits1(gb)) { // bs_extended_data
1093  int num_bits_left = get_bits(gb, 4); // bs_extension_size
1094  if (num_bits_left == 15)
1095  num_bits_left += get_bits(gb, 8); // bs_esc_count
1096 
1097  num_bits_left <<= 3;
1098  while (num_bits_left > 7) {
1099  num_bits_left -= 2;
1100  read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
1101  }
1102  if (num_bits_left < 0) {
1103  av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
1104  }
1105  if (num_bits_left > 0)
1106  skip_bits(gb, num_bits_left);
1107  }
1108 
1109  return get_bits_count(gb) - cnt;
1110 }
1111 
1113 {
1114  int err;
1115  err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
1116  if (err >= 0)
1117  err = sbr_make_f_derived(ac, sbr);
1118  if (err < 0) {
1119  av_log(ac->avctx, AV_LOG_ERROR,
1120  "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1121  sbr_turnoff(sbr);
1122  }
1123 }
1124 
1125 /**
1126  * Decode Spectral Band Replication extension data; reference: table 4.55.
1127  *
1128  * @param crc flag indicating the presence of CRC checksum
1129  * @param cnt length of TYPE_FIL syntactic element in bytes
1130  *
1131  * @return Returns number of bytes consumed from the TYPE_FIL element.
1132  */
1134  GetBitContext *gb_host, int crc,
1135  int cnt, int id_aac)
1136 {
1137  SpectralBandReplication *sbr = get_sbr(che);
1138  unsigned int num_sbr_bits = 0, num_align_bits;
1139  unsigned bytes_read;
1140  GetBitContext gbc = *gb_host, *gb = &gbc;
1141  skip_bits_long(gb_host, cnt*8 - 4);
1142 
1143  sbr->reset = 0;
1144 
1145  if (!sbr->sample_rate)
1146  sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
1147  if (!ac->oc[1].m4ac.ext_sample_rate)
1148  ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
1149 
1150  if (crc) {
1151  skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
1152  num_sbr_bits += 10;
1153  }
1154 
1155  //Save some state from the previous frame.
1156  sbr->kx[0] = sbr->kx[1];
1157  sbr->m[0] = sbr->m[1];
1158  sbr->kx_and_m_pushed = 1;
1159 
1160  num_sbr_bits++;
1161  if (get_bits1(gb)) // bs_header_flag
1162  num_sbr_bits += read_sbr_header(sbr, gb, 0);
1163 
1164  if (sbr->reset)
1165  sbr_reset(ac, sbr);
1166 
1167  if (sbr->start)
1168  num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
1169 
1170  num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1171  bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1172 
1173  if (bytes_read > cnt) {
1174  av_log(ac->avctx, AV_LOG_ERROR,
1175  "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1176  sbr_turnoff(sbr);
1177  }
1178  return cnt;
1179 }
1180 
1181 #if !USE_FIXED
1184 {
1185  sbr->inter_tes = ue->sbr.bs_intertes;
1186 
1187  sbr->spectrum_params.bs_start_freq = ue->sbr.dflt.start_freq;
1188  sbr->spectrum_params.bs_stop_freq = ue->sbr.dflt.stop_freq;
1189 
1190  sbr->spectrum_params.bs_freq_scale = ue->sbr.dflt.freq_scale;
1191  sbr->spectrum_params.bs_alter_scale = ue->sbr.dflt.alter_scale;
1192  sbr->spectrum_params.bs_noise_bands = ue->sbr.dflt.noise_bands;
1193 
1194  sbr->bs_limiter_bands = ue->sbr.dflt.limiter_bands;
1195  sbr->bs_limiter_gains = ue->sbr.dflt.limiter_gains;
1196  sbr->bs_interpol_freq = ue->sbr.dflt.interpol_freq;
1197  sbr->bs_smoothing_mode = ue->sbr.dflt.smoothing_mode;
1198 }
1199 
1202 {
1203  SpectralBandReplication *sbr = get_sbr(che);
1204  sbr_turnoff(sbr);
1205  return 0;
1206 }
1207 
1210  int sbr_ch, int indep_flag)
1211 {
1212  int ret;
1213  SpectralBandReplication *sbr = get_sbr(che);
1214  int info_present = 1;
1215  int header_present = 1;
1216 
1217  sbr->reset = 0;
1218  sbr->usac = 1;
1219 
1220  sbr->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
1221  sbr->id_aac = sbr_ch == 2 ? TYPE_CPE : TYPE_SCE;
1222 
1223  if (!indep_flag) {
1224  info_present = get_bits1(gb);
1225  if (info_present)
1226  header_present = get_bits1(gb);
1227  else
1228  header_present = 0;
1229  }
1230 
1231  if (info_present) {
1232  /* SbrInfo() */
1233  sbr->bs_amp_res_header = get_bits1(gb);
1234  sbr->spectrum_params.bs_xover_band = get_bits(gb, 4);
1235  sbr->bs_sbr_preprocessing = get_bits1(gb);
1236  /* if (bs_pvc) ... */
1237  }
1238 
1239  if (header_present) {
1240  if (get_bits1(gb)) {
1241  int old_bs_limiter_bands = sbr->bs_limiter_bands;
1242  SpectrumParameters old_spectrum_params;
1243  memcpy(&old_spectrum_params, &sbr->spectrum_params,
1244  sizeof(SpectrumParameters));
1245 
1247  // Check if spectrum parameters changed
1248  if (memcmp(&old_spectrum_params, &sbr->spectrum_params,
1249  sizeof(SpectrumParameters)))
1250  sbr->reset = 1;
1251 
1252  if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
1253  sbr_make_f_tablelim(sbr);
1254  } else {
1255  read_sbr_header(sbr, gb, 1);
1256  }
1257 
1258  sbr->start = 1;
1259  }
1260 
1261  //Save some state from the previous frame.
1262  sbr->kx[0] = sbr->kx[1];
1263  sbr->m[0] = sbr->m[1];
1264  sbr->kx_and_m_pushed = 1;
1265 
1266  if (sbr->reset)
1267  sbr_reset(ac, sbr);
1268 
1269  sbr->ready_for_dequant = 1;
1270 
1271  if (sbr_ch == 1) { /* sbr_single_channel_element */
1272  /* if (harmonicSBR) ... */
1273 
1274  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1275  return -1;
1276 
1277  read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
1278  read_sbr_invf(sbr, gb, &sbr->data[0]);
1279 
1280  if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1281  return ret;
1282 
1283  if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1284  return ret;
1285 
1286  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1287  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1288  } else if (get_bits1(gb)) { /* bs_coupling == 1 */
1289  /* if (harmonicSBR) ... */
1290 
1291  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1292  return -1;
1293  copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
1294 
1295  read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
1296  read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
1297 
1298  read_sbr_invf(sbr, gb, &sbr->data[0]);
1299  memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0],
1300  sizeof(sbr->data[1].bs_invf_mode[0]));
1301  memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0],
1302  sizeof(sbr->data[1].bs_invf_mode[0]));
1303 
1304  if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1305  return ret;
1306  if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1307  return ret;
1308 
1309  if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1310  return ret;
1311  if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1312  return ret;
1313 
1314  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1315  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1316  if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1317  get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1318  } else { /* bs_coupling == 0 */
1319  /* if (harmonicSBR) ... */
1320  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1321  return -1;
1322  if (read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
1323  return -1;
1324 
1325  read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
1326  read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
1327 
1328  read_sbr_invf(sbr, gb, &sbr->data[0]);
1329  read_sbr_invf(sbr, gb, &sbr->data[1]);
1330 
1331  if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1332  return ret;
1333  if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1334  return ret;
1335 
1336  if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1337  return ret;
1338  if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1339  return ret;
1340 
1341  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1342  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1343  if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1344  get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1345  }
1346 
1347  return 0;
1348 }
1349 #endif
1350 
1351 /**
1352  * Analysis QMF Bank (14496-3 sp04 p206)
1353  *
1354  * @param x pointer to the beginning of the first sample window
1355  * @param W array of complex-valued samples split into subbands
1356  */
1357 #ifndef sbr_qmf_analysis
1358 #if USE_FIXED
1359 static void sbr_qmf_analysis(AVFixedDSPContext *dsp, AVTXContext *mdct,
1360  av_tx_fn mdct_fn,
1361 #else
1363  av_tx_fn mdct_fn,
1364 #endif /* USE_FIXED */
1365  SBRDSPContext *sbrdsp, const INTFLOAT *in, INTFLOAT *x,
1366  INTFLOAT z[320], INTFLOAT W[2][32][32][2], int buf_idx)
1367 {
1368  int i;
1369 #if USE_FIXED
1370  int j;
1371 #endif
1372  memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1373  memcpy(x+288, in, 1024*sizeof(x[0]));
1374  for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1375  // are not supported
1376  dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1377  sbrdsp->sum64x5(z);
1378  sbrdsp->qmf_pre_shuffle(z);
1379 #if USE_FIXED
1380  for (j = 64; j < 128; j++) {
1381  if (z[j] > 1<<24) {
1383  "sbr_qmf_analysis: value %09d too large, setting to %09d\n",
1384  z[j], 1<<24);
1385  z[j] = 1<<24;
1386  } else if (z[j] < -(1<<24)) {
1388  "sbr_qmf_analysis: value %09d too small, setting to %09d\n",
1389  z[j], -(1<<24));
1390  z[j] = -(1<<24);
1391  }
1392  }
1393 #endif
1394  mdct_fn(mdct, z, z + 64, sizeof(INTFLOAT));
1395  sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
1396  x += 32;
1397  }
1398 }
1399 #endif
1400 
1401 /**
1402  * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1403  * (14496-3 sp04 p206)
1404  */
1405 #ifndef sbr_qmf_synthesis
1406 static void sbr_qmf_synthesis(AVTXContext *mdct, av_tx_fn mdct_fn,
1407 #if USE_FIXED
1408  SBRDSPContext *sbrdsp, AVFixedDSPContext *dsp,
1409 #else
1410  SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
1411 #endif /* USE_FIXED */
1412  INTFLOAT *out, INTFLOAT X[2][38][64],
1413  INTFLOAT mdct_buf[2][64],
1414  INTFLOAT *v0, int *v_off, const unsigned int div)
1415 {
1416  int i, n;
1417  const INTFLOAT *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
1418  const int step = 128 >> div;
1419  INTFLOAT *v;
1420  for (i = 0; i < 32; i++) {
1421  if (*v_off < step) {
1422  int saved_samples = (1280 - 128) >> div;
1423  memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(INTFLOAT));
1424  *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
1425  } else {
1426  *v_off -= step;
1427  }
1428  v = v0 + *v_off;
1429  if (div) {
1430  for (n = 0; n < 32; n++) {
1431  X[0][i][ n] = -X[0][i][n];
1432  X[0][i][32+n] = X[1][i][31-n];
1433  }
1434  mdct_fn(mdct, mdct_buf[0], X[0][i], sizeof(INTFLOAT));
1435  sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
1436  } else {
1437  sbrdsp->neg_odd_64(X[1][i]);
1438  mdct_fn(mdct, mdct_buf[0], X[0][i], sizeof(INTFLOAT));
1439  mdct_fn(mdct, mdct_buf[1], X[1][i], sizeof(INTFLOAT));
1440  sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
1441  }
1442  dsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1443  dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1444  dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1445  dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1446  dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1447  dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1448  dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1449  dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1450  dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1451  dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1452  out += 64 >> div;
1453  }
1454 }
1455 #endif
1456 
1457 /// Generate the subband filtered lowband
1459  INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2],
1460  int buf_idx)
1461 {
1462  int i, k;
1463  const int t_HFGen = 8;
1464  const int i_f = 32;
1465  memset(X_low, 0, 32*sizeof(*X_low));
1466  for (k = 0; k < sbr->kx[1]; k++) {
1467  for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1468  X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1469  X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1470  }
1471  }
1472  buf_idx = 1-buf_idx;
1473  for (k = 0; k < sbr->kx[0]; k++) {
1474  for (i = 0; i < t_HFGen; i++) {
1475  X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1476  X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1477  }
1478  }
1479  return 0;
1480 }
1481 
1482 /// High Frequency Generator (14496-3 sp04 p215)
1484  INTFLOAT X_high[64][40][2], const INTFLOAT X_low[32][40][2],
1485  const INTFLOAT (*alpha0)[2], const INTFLOAT (*alpha1)[2],
1486  const INTFLOAT bw_array[5], const uint8_t *t_env,
1487  int bs_num_env)
1488 {
1489  int j, x;
1490  int g = 0;
1491  int k = sbr->kx[1];
1492  for (j = 0; j < sbr->num_patches; j++) {
1493  for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
1494  const int p = sbr->patch_start_subband[j] + x;
1495  while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
1496  g++;
1497  g--;
1498 
1499  if (g < 0) {
1500  av_log(ac->avctx, AV_LOG_ERROR,
1501  "ERROR : no subband found for frequency %d\n", k);
1502  return -1;
1503  }
1504 
1505  sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
1506  X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
1507  alpha0[p], alpha1[p], bw_array[g],
1508  2 * t_env[0], 2 * t_env[bs_num_env]);
1509  }
1510  }
1511  if (k < sbr->m[1] + sbr->kx[1])
1512  memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
1513 
1514  return 0;
1515 }
1516 
1517 /// Generate the subband filtered lowband
1518 static int sbr_x_gen(SpectralBandReplication *sbr, INTFLOAT X[2][38][64],
1519  const INTFLOAT Y0[38][64][2], const INTFLOAT Y1[38][64][2],
1520  const INTFLOAT X_low[32][40][2], int ch)
1521 {
1522  int k, i;
1523  const int i_f = 32;
1524  const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
1525  memset(X, 0, 2*sizeof(*X));
1526  for (k = 0; k < sbr->kx[0]; k++) {
1527  for (i = 0; i < i_Temp; i++) {
1528  X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1529  X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1530  }
1531  }
1532  for (; k < sbr->kx[0] + sbr->m[0]; k++) {
1533  for (i = 0; i < i_Temp; i++) {
1534  X[0][i][k] = Y0[i + i_f][k][0];
1535  X[1][i][k] = Y0[i + i_f][k][1];
1536  }
1537  }
1538 
1539  for (k = 0; k < sbr->kx[1]; k++) {
1540  for (i = i_Temp; i < 38; i++) {
1541  X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1542  X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1543  }
1544  }
1545  for (; k < sbr->kx[1] + sbr->m[1]; k++) {
1546  for (i = i_Temp; i < i_f; i++) {
1547  X[0][i][k] = Y1[i][k][0];
1548  X[1][i][k] = Y1[i][k][1];
1549  }
1550  }
1551  return 0;
1552 }
1553 
1554 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1555  * (14496-3 sp04 p217)
1556  */
1558  SBRData *ch_data, int e_a[2])
1559 {
1560  int e, i, m;
1561 
1562  memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
1563  for (e = 0; e < ch_data->bs_num_env; e++) {
1564  const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
1565  uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1566  int k;
1567 
1568  if (sbr->kx[1] != table[0]) {
1569  av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
1570  "Derived frequency tables were not regenerated.\n");
1571  sbr_turnoff(sbr);
1572  return AVERROR_BUG;
1573  }
1574  for (i = 0; i < ilim; i++)
1575  for (m = table[i]; m < table[i + 1]; m++)
1576  sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
1577 
1578  // ch_data->bs_num_noise > 1 => 2 noise floors
1579  k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
1580  for (i = 0; i < sbr->n_q; i++)
1581  for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
1582  sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
1583 
1584  for (i = 0; i < sbr->n[1]; i++) {
1585  if (ch_data->bs_add_harmonic_flag) {
1586  const unsigned int m_midpoint =
1587  (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
1588 
1589  ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
1590  (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
1591  }
1592  }
1593 
1594  for (i = 0; i < ilim; i++) {
1595  int additional_sinusoid_present = 0;
1596  for (m = table[i]; m < table[i + 1]; m++) {
1597  if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
1598  additional_sinusoid_present = 1;
1599  break;
1600  }
1601  }
1602  memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
1603  (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
1604  }
1605  }
1606 
1607  memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
1608  return 0;
1609 }
1610 
1611 /// Estimation of current envelope (14496-3 sp04 p218)
1612 static void sbr_env_estimate(AAC_FLOAT (*e_curr)[48], INTFLOAT X_high[64][40][2],
1613  SpectralBandReplication *sbr, SBRData *ch_data)
1614 {
1615  int e, m;
1616  int kx1 = sbr->kx[1];
1617 
1618  if (sbr->bs_interpol_freq) {
1619  for (e = 0; e < ch_data->bs_num_env; e++) {
1620 #if USE_FIXED
1621  const SoftFloat recip_env_size = av_int2sf(0x20000000 / (ch_data->t_env[e + 1] - ch_data->t_env[e]), 30);
1622 #else
1623  const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1624 #endif /* USE_FIXED */
1625  int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1626  int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1627 
1628  for (m = 0; m < sbr->m[1]; m++) {
1629  AAC_FLOAT sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
1630 #if USE_FIXED
1631  e_curr[e][m] = av_mul_sf(sum, recip_env_size);
1632 #else
1633  e_curr[e][m] = sum * recip_env_size;
1634 #endif /* USE_FIXED */
1635  }
1636  }
1637  } else {
1638  int k, p;
1639 
1640  for (e = 0; e < ch_data->bs_num_env; e++) {
1641  const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1642  int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1643  int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1644  const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1645 
1646  for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
1647 #if USE_FIXED
1648  SoftFloat sum = FLOAT_0;
1649  const SoftFloat den = av_int2sf(0x20000000 / (env_size * (table[p + 1] - table[p])), 29);
1650  for (k = table[p]; k < table[p + 1]; k++) {
1651  sum = av_add_sf(sum, sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb));
1652  }
1653  sum = av_mul_sf(sum, den);
1654 #else
1655  float sum = 0.0f;
1656  const int den = env_size * (table[p + 1] - table[p]);
1657 
1658  for (k = table[p]; k < table[p + 1]; k++) {
1659  sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
1660  }
1661  sum /= den;
1662 #endif /* USE_FIXED */
1663  for (k = table[p]; k < table[p + 1]; k++) {
1664  e_curr[e][k - kx1] = sum;
1665  }
1666  }
1667  }
1668  }
1669 }
1670 
1672  int id_aac, void *L_, void *R_)
1673 {
1674  INTFLOAT *L = L_, *R = R_;
1675  SpectralBandReplication *sbr = get_sbr(che);
1676  int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
1677  int ch;
1678  int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1679  int err;
1680 
1681  if (id_aac != sbr->id_aac) {
1682  av_log(ac->avctx, id_aac == TYPE_LFE ? AV_LOG_VERBOSE : AV_LOG_WARNING,
1683  "element type mismatch %d != %d\n", id_aac, sbr->id_aac);
1684  sbr_turnoff(sbr);
1685  }
1686 
1687  if (sbr->start && !sbr->ready_for_dequant) {
1688  av_log(ac->avctx, AV_LOG_ERROR,
1689  "No quantized data read for sbr_dequant.\n");
1690  sbr_turnoff(sbr);
1691  }
1692 
1693  if (!sbr->kx_and_m_pushed) {
1694  sbr->kx[0] = sbr->kx[1];
1695  sbr->m[0] = sbr->m[1];
1696  } else {
1697  sbr->kx_and_m_pushed = 0;
1698  }
1699 
1700  if (sbr->start) {
1701  sbr_dequant(sbr, id_aac);
1702  sbr->ready_for_dequant = 0;
1703  }
1704  for (ch = 0; ch < nch; ch++) {
1705  /* decode channel */
1706  sbr_qmf_analysis(ac->fdsp, sbr->mdct_ana, sbr->mdct_ana_fn, &sbr->dsp,
1707  ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1709  sbr->data[ch].W, sbr->data[ch].Ypos);
1710  sbr->c.sbr_lf_gen(sbr, sbr->X_low,
1711  (const INTFLOAT (*)[32][32][2]) sbr->data[ch].W,
1712  sbr->data[ch].Ypos);
1713  sbr->data[ch].Ypos ^= 1;
1714  if (sbr->start) {
1715  sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
1716  (const INTFLOAT (*)[40][2]) sbr->X_low, sbr->k[0]);
1717  sbr_chirp(sbr, &sbr->data[ch]);
1718  av_assert0(sbr->data[ch].bs_num_env > 0);
1719  sbr_hf_gen(ac, sbr, sbr->X_high,
1720  (const INTFLOAT (*)[40][2]) sbr->X_low,
1721  (const INTFLOAT (*)[2]) sbr->alpha0,
1722  (const INTFLOAT (*)[2]) sbr->alpha1,
1723  sbr->data[ch].bw_array, sbr->data[ch].t_env,
1724  sbr->data[ch].bs_num_env);
1725 
1726  // hf_adj
1727  err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1728  if (!err) {
1729  sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1730  sbr_gain_calc(sbr, &sbr->data[ch], sbr->data[ch].e_a);
1731  sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
1732  (const INTFLOAT (*)[40][2]) sbr->X_high,
1733  sbr, &sbr->data[ch],
1734  sbr->data[ch].e_a);
1735  }
1736  }
1737 
1738  /* synthesis */
1739  sbr->c.sbr_x_gen(sbr, sbr->X[ch],
1740  (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
1741  (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[ sbr->data[ch].Ypos],
1742  (const INTFLOAT (*)[40][2]) sbr->X_low, ch);
1743  }
1744 
1745  if (ac->oc[1].m4ac.ps == 1) {
1746  if (sbr->ps.common.start) {
1747  AAC_RENAME(ff_ps_apply)(&sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1748  } else {
1749  memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1750  }
1751  nch = 2;
1752  }
1753 
1754  sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
1755  L, sbr->X[0], sbr->qmf_filter_scratch,
1758  downsampled);
1759  if (nch == 2)
1760  sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
1761  R, sbr->X[1], sbr->qmf_filter_scratch,
1764  downsampled);
1765 }
1766 
1768 {
1769  c->sbr_lf_gen = sbr_lf_gen;
1770  c->sbr_hf_assemble = sbr_hf_assemble;
1771  c->sbr_x_gen = sbr_x_gen;
1772  c->sbr_hf_inverse_filter = sbr_hf_inverse_filter;
1773 }
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:36
F_HUFFMAN_ENV_1_5DB
@ F_HUFFMAN_ENV_1_5DB
Definition: aacsbr.h:45
ff_aac_sbr_decode_usac_data
int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che, AACUsacElemConfig *ue, GetBitContext *gb, int sbr_ch, int indep_flag)
Decode frame SBR data, USAC.
Definition: aacsbr_template.c:1208
SpectralBandReplication::bs_coupling
unsigned bs_coupling
Definition: sbr.h:163
SBRData::bs_freq_res
uint8_t bs_freq_res[9]
Definition: sbr.h:71
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
sbr_hf_gen
static int sbr_hf_gen(AACDecContext *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:1483
W
@ W
Definition: vf_addroi.c:27
T_HUFFMAN_NOISE_3_0DB
@ T_HUFFMAN_NOISE_3_0DB
Definition: aacsbr.h:52
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_aac_sbr_init
av_cold void AAC_RENAME() ff_aac_sbr_init(void)
Initialize SBR.
Definition: aacsbr_template.c:52
SpectralBandReplication::data
SBRData data[2]
Definition: sbr.h:173
SpectralBandReplication::bs_limiter_gains
unsigned bs_limiter_gains
Definition: sbr.h:159
Q23
#define Q23(x)
Definition: aac_defines.h:109
SBRDSPContext
Definition: sbrdsp.h:27
out
FILE * out
Definition: movenc.c:55
elements
static const ElemCat * elements[ELEMENT_COUNT]
Definition: signature.h:565
AACDecContext::warned_he_aac_mono
int warned_he_aac_mono
Definition: aacdec.h:532
log2f
#define log2f(x)
Definition: libm.h:409
sbr_chirp
static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
Chirp Factors (14496-3 sp04 p214)
Definition: aacsbr.c:208
AVTXContext
Definition: tx_priv.h:235
int64_t
long long int64_t
Definition: coverity.c:34
SpectralBandReplication::X
INTFLOAT X[2][2][38][64]
QMF values of the reconstructed signal.
Definition: sbr.h:199
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:266
ff_ps_apply
int AAC_RENAME() ff_ps_apply(PSContext *ps, INTFLOAT L[2][38][64], INTFLOAT R[2][38][64], int top)
Definition: aacps.c:719
sbr_qmf_window_ds
static const INTFLOAT sbr_qmf_window_ds[320]
Definition: aacsbrdata.h:45
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:153
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
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
ff_ps_read_data
int ff_ps_read_data(void *logctx, GetBitContext *gb, PSCommonContext *ps, int bits_left)
Definition: aacps_common.c:122
SpectralBandReplication::m
AAC_SIGNE m[2]
M' and M respectively, M is the number of QMF subbands that use SBR.
Definition: sbr.h:169
b
#define b
Definition: input.c:41
read_sbr_noise
static int read_sbr_noise(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data, int ch)
Definition: aacsbr_template.c:923
T_HUFFMAN_ENV_1_5DB
@ T_HUFFMAN_ENV_1_5DB
Definition: aacsbr.h:44
SBRData::bs_df_env
uint8_t bs_df_env[9]
Definition: sbr.h:73
table
static const uint16_t table[]
Definition: prosumer.c:203
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:108
SpectrumParameters::bs_alter_scale
uint8_t bs_alter_scale
Definition: sbr.h:53
R
#define R
Definition: huffyuv.h:44
SBRData::env_facs_q
uint8_t env_facs_q[9][48]
Envelope scalefactors.
Definition: sbr.h:100
MAX_PREDICTORS
#define MAX_PREDICTORS
Definition: aac.h:85
PSCommonContext::start
int start
Definition: aacps.h:48
copy_sbr_grid
static void copy_sbr_grid(SBRData *dst, const SBRData *src)
Definition: aacsbr_template.c:784
AACSBRContext::sbr_hf_assemble
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:128
SpectrumParameters::bs_start_freq
uint8_t bs_start_freq
Definition: sbr.h:44
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
ff_ps_init
av_cold void AAC_RENAME() ff_ps_init(void)
Definition: aacps.c:740
AVFixedDSPContext
Definition: fixed_dsp.h:55
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
SpectralBandReplication::X_low
INTFLOAT X_low[32][40][2]
QMF low frequency input to the HF generator.
Definition: sbr.h:195
SBRData::temp_shape_mode
uint8_t temp_shape_mode[6]
Definition: sbr.h:115
qsort_comparison_function_int16
static int qsort_comparison_function_int16(const void *a, const void *b)
Definition: aacsbr_template.c:122
ChannelElement::ch
SingleChannelElement ch[2]
Definition: aacdec.h:266
ExtChannelElement::ch
ChannelElement ch
Definition: aacsbr_template.c:42
copy_usac_default_header
static void copy_usac_default_header(SpectralBandReplication *sbr, AACUsacElemConfig *ue)
Definition: aacsbr_template.c:1182
SpectralBandReplication::s_mapped
uint8_t s_mapped[8][48]
Sinusoidal presence, remapped.
Definition: sbr.h:209
av_tx_init
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition: tx.c:903
sbr_qmf_analysis
static void sbr_qmf_analysis(AVFloatDSPContext *dsp, AVTXContext *mdct, av_tx_fn mdct_fn, 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:1362
read_sbr_dtdf
static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data, int indep_flag)
Read how the envelope and noise floor data is delta coded.
Definition: aacsbr_template.c:802
SpectralBandReplication::ready_for_dequant
int ready_for_dequant
Definition: sbr.h:146
sbr_gain_calc
static void sbr_gain_calc(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:232
ff_aac_sbr_vlc
const VLCElem * ff_aac_sbr_vlc[10]
Definition: aacdec_tab.c:261
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
SBRData::e_a
int e_a[2]
l_APrev and l_A
Definition: sbr.h:88
SBRData::analysis_filterbank_samples
INTFLOAT analysis_filterbank_samples[1312]
Definition: sbr.h:85
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
SBRData::t_env
uint8_t t_env[9]
Envelope time borders.
Definition: sbr.h:106
SpectralBandReplication::bs_interpol_freq
unsigned bs_interpol_freq
Definition: sbr.h:160
sbr_make_f_master
static int sbr_make_f_master(AACDecContext *ac, SpectralBandReplication *sbr, SpectrumParameters *spectrum)
Master Frequency Band Table (14496-3 sp04 p194)
Definition: aacsbr_template.c:274
ExtChannelElement::sbr
SpectralBandReplication sbr
Definition: aacsbr_template.c:44
SpectralBandReplication::n_master
AAC_SIGNE n_master
The number of frequency bands in f_master.
Definition: sbr.h:172
TYPE_CPE
@ TYPE_CPE
Definition: aac.h:41
VARVAR
@ VARVAR
Definition: aacsbr.h:63
GetBitContext
Definition: get_bits.h:108
SBRData::temp_shape
uint8_t temp_shape[6]
Definition: sbr.h:114
SBRData::bs_add_harmonic_flag
unsigned bs_add_harmonic_flag
Definition: sbr.h:69
SpectralBandReplication::mdct_fn
av_tx_fn mdct_fn
Definition: sbr.h:221
ue
#define ue(name, range_min, range_max)
Definition: cbs_h2645.c:254
SpectralBandReplication::reset
int reset
Definition: sbr.h:150
PSContext::common
PSCommonContext common
Definition: aacps.h:72
sbr_lf_gen
static int sbr_lf_gen(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:1458
SBRData::noise_facs
AAC_FLOAT noise_facs[3][5]
Definition: sbr.h:104
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:289
AACUsacElemConfig
Definition: aacdec.h:297
read_sbr_single_channel_element
static int read_sbr_single_channel_element(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb)
Definition: aacsbr_template.c:992
ExtChannelElement::predictor_state
PredictorState predictor_state[2][MAX_PREDICTORS]
Definition: aacsbr_template.c:43
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
SpectrumParameters::bs_stop_freq
uint8_t bs_stop_freq
Definition: sbr.h:45
av_cold
#define av_cold
Definition: attributes.h:90
av_tx_fn
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition: tx.h:151
AV_TX_FLOAT_MDCT
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respecively.
Definition: tx.h:68
SpectralBandReplication::qmf_filter_scratch
INTFLOAT qmf_filter_scratch[5][64]
Definition: sbr.h:217
SBRData::bs_df_noise
uint8_t bs_df_noise[2]
Definition: sbr.h:74
read_sbr_grid
static int read_sbr_grid(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data)
Definition: aacsbr_template.c:637
SBRData::env_facs
AAC_FLOAT env_facs[9][48]
Definition: sbr.h:101
FIXVAR
@ FIXVAR
Definition: aacsbr.h:61
AV_TX_INT32_MDCT
@ AV_TX_INT32_MDCT
Definition: tx.h:70
g
const char * g
Definition: vf_curves.c:128
SBRData::bs_num_noise
AAC_SIGNE bs_num_noise
Definition: sbr.h:72
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
read_sbr_extension
static void read_sbr_extension(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, int bs_extension_id, int *num_bits_left)
Definition: aacsbr_template.c:965
sbr_offset
static const int8_t sbr_offset[6][16]
window coefficients for analysis/synthesis QMF banks
Definition: aacsbrdata.h:35
PredictorState
Predictor State.
Definition: aac_defines.h:130
SpectrumParameters::bs_xover_band
uint8_t bs_xover_band
Definition: sbr.h:46
SpectralBandReplication::f_tablenoise
uint16_t f_tablenoise[6]
Frequency borders for noise floors.
Definition: sbr.h:188
ff_aac_sbr_apply
void AAC_RENAME() ff_aac_sbr_apply(AACDecContext *ac, ChannelElement *che, int id_aac, void *L_, void *R_)
Apply one SBR element to one AAC element.
Definition: aacsbr_template.c:1671
SpectralBandReplication::e_origmapped
AAC_FLOAT e_origmapped[8][48]
Dequantized envelope scalefactors, remapped.
Definition: sbr.h:205
if
if(ret)
Definition: filter_design.txt:179
AACSBRContext::sbr_x_gen
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:132
SpectralBandReplication::n_lim
AAC_SIGNE n_lim
Number of limiter bands.
Definition: sbr.h:180
SBRData::Ypos
int Ypos
QMF output of the HF adjustor.
Definition: sbr.h:94
NULL
#define NULL
Definition: coverity.c:32
SpectralBandReplication::f_tablelow
uint16_t f_tablelow[25]
Frequency borders for low resolution SBR.
Definition: sbr.h:184
SpectralBandReplication::mdct
AVTXContext * mdct
Definition: sbr.h:220
read_sbr_header
static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb, int is_usac)
Definition: aacsbr_template.c:188
FLOAT_0
static const SoftFloat FLOAT_0
0.0
Definition: softfloat.h:39
SBRData::f_indexnoise
unsigned f_indexnoise
Definition: sbr.h:111
check_n_master
static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
Definition: aacsbr_template.c:257
SpectrumParameters::bs_freq_scale
uint8_t bs_freq_scale
Definition: sbr.h:52
SpectralBandReplication::f_tablelim
uint16_t f_tablelim[30]
Frequency borders for the limiter.
Definition: sbr.h:190
SBRData::bs_add_harmonic
uint8_t bs_add_harmonic[48]
Definition: sbr.h:76
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
AACSBRContext
aacsbr functions pointers
Definition: sbr.h:124
fixed_log
static int fixed_log(int x)
Definition: aacsbr_fixed.c:84
SBR_SYNTHESIS_BUF_SIZE
#define SBR_SYNTHESIS_BUF_SIZE
Definition: sbr.h:58
USE_FIXED
#define USE_FIXED
Definition: aacdec.c:34
make_bands
static void make_bands(int16_t *bands, int start, int stop, int num_bands)
Definition: aacsbr.c:67
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:176
SBRData::bs_num_env
AAC_SIGNE bs_num_env
Definition: sbr.h:70
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:652
SBRData::bs_amp_res
unsigned bs_amp_res
Definition: sbr.h:77
SpectralBandReplication::bs_smoothing_mode
unsigned bs_smoothing_mode
Definition: sbr.h:161
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:1518
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
SpectralBandReplication::mdct_ana_fn
av_tx_fn mdct_ana_fn
Definition: sbr.h:219
SpectrumParameters
Spectral Band Replication header - spectrum parameters that invoke a reset if they differ from the pr...
Definition: sbr.h:43
sbr_qmf_synthesis
static void sbr_qmf_synthesis(AVTXContext *mdct, av_tx_fn mdct_fn, 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:1406
SpectralBandReplication::f_master
uint16_t f_master[49]
The master QMF frequency grouping.
Definition: sbr.h:182
qsort.h
f
f
Definition: af_crystalizer.c:122
sbr_mapping
static int sbr_mapping(AACDecContext *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:1557
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
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
SpectralBandReplication::sample_rate
int sample_rate
Definition: sbr.h:144
VLCElem
Definition: vlc.h:32
sbr_turnoff
static void sbr_turnoff(SpectralBandReplication *sbr)
Places SBR in pure upsampling mode.
Definition: aacsbr_template.c:58
SpectralBandReplication
Spectral Band Replication.
Definition: sbr.h:143
EXTENSION_ID_PS
@ EXTENSION_ID_PS
Definition: aacsbr.h:67
SpectralBandReplication::alpha0
INTFLOAT alpha0[64][2]
Zeroth coefficient used to filter the subband signals.
Definition: sbr.h:201
SpectralBandReplication::c
AACSBRContext c
Definition: sbr.h:223
AVFloatDSPContext
Definition: float_dsp.h:24
SpectralBandReplication::bs_amp_res_header
int bs_amp_res_header
Definition: sbr.h:152
SBRData::bs_invf_mode
uint8_t bs_invf_mode[2][5]
Definition: sbr.h:75
read_sbr_envelope
static int read_sbr_envelope(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data, int ch)
Definition: aacsbr_template.c:836
SpectralBandReplication::alpha1
INTFLOAT alpha1[64][2]
First coefficient used to filter the subband signals.
Definition: sbr.h:203
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
SBRData::Y
INTFLOAT Y[2][38][64][2]
Definition: sbr.h:95
SpectralBandReplication::n_q
AAC_SIGNE n_q
Number of noise floor bands.
Definition: sbr.h:178
SpectralBandReplication::patch_start_subband
uint8_t patch_start_subband[6]
Definition: sbr.h:193
CONST_076923
static const int CONST_076923
Definition: aacsbr_fixed.c:76
ff_aac_sbr_config_usac
int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che, AACUsacElemConfig *ue)
Due to channel allocation not being known upon SBR parameter transmission, supply the parameters sepa...
Definition: aacsbr_template.c:1200
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:295
AACSBRContext::sbr_hf_inverse_filter
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:135
ff_sbrdsp_init
void AAC_RENAME() ff_sbrdsp_init(SBRDSPContext *s)
Definition: sbrdsp_template.c:80
aacsbr_func_ptr_init
static void aacsbr_func_ptr_init(AACSBRContext *c)
Definition: aacsbr_template.c:1767
SpectralBandReplication::inter_tes
int inter_tes
Definition: sbr.h:149
F_HUFFMAN_ENV_BAL_1_5DB
@ F_HUFFMAN_ENV_BAL_1_5DB
Definition: aacsbr.h:47
SpectralBandReplication::id_aac
int id_aac
Definition: sbr.h:147
SoftFloat
Definition: softfloat.h:34
CONST_RECIP_LN2
static const int CONST_RECIP_LN2
Definition: aacsbr_fixed.c:75
SpectralBandReplication::q_mapped
AAC_FLOAT q_mapped[8][48]
Dequantized noise scalefactors, remapped.
Definition: sbr.h:207
lrintf
#define lrintf(x)
Definition: libm_mips.h:72
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ceil_log2
static const int8_t ceil_log2[]
ceil(log2(index+1))
Definition: aacsbr_template.c:633
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:371
SBRData
Spectral Band Replication per channel data.
Definition: sbr.h:63
ChannelElement
channel element - generic struct for SCE/CPE/CCE/LFE
Definition: aacdec.h:260
SBRData::bw_array
INTFLOAT bw_array[5]
Chirp factors.
Definition: sbr.h:90
SpectralBandReplication::k
AAC_SIGNE k[5]
k0, k1, k2
Definition: sbr.h:164
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:430
SpectralBandReplication::X_high
INTFLOAT X_high[64][40][2]
QMF output of the HF generator.
Definition: sbr.h:197
in_table_int16
static int in_table_int16(const int16_t *table, int last_el, int16_t needle)
Definition: aacsbr_template.c:127
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
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:43
SBRData::bs_frame_class
unsigned bs_frame_class
Definition: sbr.h:68
AAC_RENAME
#define AAC_RENAME(x)
Definition: aac_defines.h:99
sbr_make_f_tablelim
static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
Limiter Frequency Band Table (14496-3 sp04 p198)
Definition: aacsbr_template.c:137
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
TYPE_SCE
@ TYPE_SCE
Definition: aac.h:40
AACDecContext::oc
OutputConfiguration oc[2]
Definition: aacdec.h:527
MPEG4AudioConfig::ext_sample_rate
int ext_sample_rate
Definition: mpeg4audio.h:37
SpectralBandReplication::kx_and_m_pushed
unsigned kx_and_m_pushed
Definition: sbr.h:170
sbr_make_f_derived
static int sbr_make_f_derived(AACDecContext *ac, SpectralBandReplication *sbr)
Derived Frequency Band Tables (14496-3 sp04 p197)
Definition: aacsbr_template.c:552
avcodec.h
SBRData::W
INTFLOAT W[2][32][32][2]
QMF values of the original signal.
Definition: sbr.h:92
read_sbr_invf
static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data)
Read inverse filtering data.
Definition: aacsbr_template.c:826
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:116
T_HUFFMAN_NOISE_BAL_3_0DB
@ T_HUFFMAN_NOISE_BAL_3_0DB
Definition: aacsbr.h:53
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:86
FIXFIX
@ FIXFIX
Definition: aacsbr.h:60
T_HUFFMAN_ENV_BAL_1_5DB
@ T_HUFFMAN_ENV_BAL_1_5DB
Definition: aacsbr.h:46
T_HUFFMAN_ENV_3_0DB
@ T_HUFFMAN_ENV_3_0DB
Definition: aacsbr.h:48
SpectralBandReplication::usac
int usac
Definition: sbr.h:148
array_min_int16
static int array_min_int16(const int16_t *array, int nel)
Definition: aacsbr_template.c:249
F_HUFFMAN_ENV_BAL_3_0DB
@ F_HUFFMAN_ENV_BAL_3_0DB
Definition: aacsbr.h:51
U
#define U(x)
Definition: vpx_arith.h:37
aacdec.h
AACDecContext
main AAC decoding context
Definition: aacdec.h:448
sbr_hf_calc_npatches
static int sbr_hf_calc_npatches(AACDecContext *ac, SpectralBandReplication *sbr)
High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
Definition: aacsbr_template.c:494
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ff_aac_sbr_ctx_alloc_init
av_cold int AAC_RENAME() ff_aac_sbr_ctx_alloc_init(AACDecContext *ac, ChannelElement **che, int id_aac)
Allocate an ExtChannelElement (if necessary) and initialize the SBR context contained in it.
Definition: aacsbr_template.c:70
AV_PROFILE_AAC_HE_V2
#define AV_PROFILE_AAC_HE_V2
Definition: defs.h:73
AACDecContext::avctx
struct AVCodecContext * avctx
Definition: aacdec.h:450
MPEG4AudioConfig::ps
int ps
-1 implicit, 1 presence
Definition: mpeg4audio.h:40
F_HUFFMAN_ENV_3_0DB
@ F_HUFFMAN_ENV_3_0DB
Definition: aacsbr.h:49
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:1612
aacdec_tab.h
SBRData::t_q
uint8_t t_q[3]
Noise time borders.
Definition: sbr.h:110
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1650
VARFIX
@ VARFIX
Definition: aacsbr.h:62
SpectralBandReplication::spectrum_params
SpectrumParameters spectrum_params
Definition: sbr.h:151
av_add_sf
static av_const SoftFloat av_add_sf(SoftFloat a, SoftFloat b)
Definition: softfloat.h:162
SpectralBandReplication::bs_sbr_preprocessing
int bs_sbr_preprocessing
Definition: sbr.h:153
temp
else temp
Definition: vf_mcdeint.c:263
sbr_qmf_window_us
static const INTFLOAT sbr_qmf_window_us[640]
Definition: aacsbrdata.h:210
L
#define L(x)
Definition: vpx_arith.h:36
SpectralBandReplication::f_tablehigh
uint16_t f_tablehigh[49]
Frequency borders for high resolution SBR.
Definition: sbr.h:186
SBRData::noise_facs_q
uint8_t noise_facs_q[3][5]
Noise scalefactors.
Definition: sbr.h:103
SBRData::synthesis_filterbank_samples
INTFLOAT synthesis_filterbank_samples[SBR_SYNTHESIS_BUF_SIZE]
Definition: sbr.h:84
sbr_reset
static void sbr_reset(AACDecContext *ac, SpectralBandReplication *sbr)
Definition: aacsbr_template.c:1112
SpectralBandReplication::bs_limiter_bands
unsigned bs_limiter_bands
Definition: sbr.h:158
SpectralBandReplication::patch_num_subbands
uint8_t patch_num_subbands[6]
Definition: sbr.h:192
OutputConfiguration::m4ac
MPEG4AudioConfig m4ac
Definition: aacdec.h:368
TYPE_CCE
@ TYPE_CCE
Definition: aac.h:42
X
@ X
Definition: vf_addroi.c:27
SpectralBandReplication::start
int start
Definition: sbr.h:145
ff_aac_sbr_decode_extension
int AAC_RENAME() ff_aac_sbr_decode_extension(AACDecContext *ac, ChannelElement *che, GetBitContext *gb_host, int crc, int cnt, int id_aac)
Decode Spectral Band Replication extension data; reference: table 4.55.
Definition: aacsbr_template.c:1133
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
SpectralBandReplication::mdct_ana
AVTXContext * mdct_ana
Definition: sbr.h:218
SBRDSPContext::sum_square
AAC_FLOAT(* sum_square)(INTFLOAT(*x)[2], int n)
Definition: sbrdsp.h:29
ff_ps_ctx_init
static void AAC_RENAME() ff_ps_ctx_init(PSContext *ps)
Definition: aacps.h:97
get_sbr
static SpectralBandReplication * get_sbr(ChannelElement *ch)
Definition: aacsbr_template.c:47
ENVELOPE_ADJUSTMENT_OFFSET
#define ENVELOPE_ADJUSTMENT_OFFSET
Definition: aacsbr.h:37
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:291
AAC_FLOAT
float AAC_FLOAT
Definition: aac_defines.h:105
read_sbr_channel_pair_element
static int read_sbr_channel_pair_element(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb)
Definition: aacsbr_template.c:1016
SpectrumParameters::bs_noise_bands
uint8_t bs_noise_bands
Definition: sbr.h:54
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
read_sbr_data
static unsigned int read_sbr_data(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, int id_aac)
Definition: aacsbr_template.c:1068
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ExtChannelElement
Definition: aacsbr_template.c:41
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
T_HUFFMAN_ENV_BAL_3_0DB
@ T_HUFFMAN_ENV_BAL_3_0DB
Definition: aacsbr.h:50
AACSBRContext::sbr_lf_gen
int(* sbr_lf_gen)(SpectralBandReplication *sbr, INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2], int buf_idx)
Definition: sbr.h:125
get_bits1_vector
static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec, int elements)
Definition: aacsbr_template.c:623
ff_aac_sbr_ctx_close
av_cold void AAC_RENAME() ff_aac_sbr_ctx_close(ChannelElement *che)
Close the SBR context implicitly contained in a ChannelElement.
Definition: aacsbr_template.c:115
SBRData::synthesis_filterbank_samples_offset
int synthesis_filterbank_samples_offset
Definition: sbr.h:86
SBRData::s_indexmapped
uint8_t s_indexmapped[9][48]
Definition: sbr.h:98
SpectralBandReplication::kx
AAC_SIGNE kx[2]
kx', and kx respectively, kx is the first QMF subband where SBR is used.
Definition: sbr.h:167
av_mul_sf
static av_const SoftFloat av_mul_sf(SoftFloat a, SoftFloat b)
Definition: softfloat.h:102
INTFLOAT
float INTFLOAT
Definition: aac_defines.h:101
SpectralBandReplication::e_curr
AAC_FLOAT e_curr[8][48]
Estimated envelope.
Definition: sbr.h:211
src
#define src
Definition: vp8dsp.c:248
SpectralBandReplication::ps
PSContext ps
Definition: sbr.h:174
min
float min
Definition: vorbis_enc_data.h:429
SpectralBandReplication::num_patches
AAC_SIGNE num_patches
Definition: sbr.h:191
SpectralBandReplication::dsp
SBRDSPContext dsp
Definition: sbr.h:222