FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aacsbr.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  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * AAC Spectral Band Replication decoding functions
26  * @author Robert Swain ( rob opendot cl )
27  */
28 
29 #include "aac.h"
30 #include "sbr.h"
31 #include "aacsbr.h"
32 #include "aacsbrdata.h"
33 #include "aacsbr_tablegen.h"
34 #include "fft.h"
35 #include "aacps.h"
36 #include "sbrdsp.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/libm.h"
39 #include "libavutil/avassert.h"
40 
41 #include <stdint.h>
42 #include <float.h>
43 #include <math.h>
44 
45 #define ENVELOPE_ADJUSTMENT_OFFSET 2
46 #define NOISE_FLOOR_OFFSET 6.0f
47 
48 #if ARCH_MIPS
49 #include "mips/aacsbr_mips.h"
50 #endif /* ARCH_MIPS */
51 
52 /**
53  * SBR VLC tables
54  */
55 enum {
66 };
67 
68 /**
69  * bs_frame_class - frame class of current SBR frame (14496-3 sp04 p98)
70  */
71 enum {
76 };
77 
78 enum {
80 };
81 
82 static VLC vlc_sbr[10];
83 static const int8_t vlc_sbr_lav[10] =
84  { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
85 
86 #define SBR_INIT_VLC_STATIC(num, size) \
87  INIT_VLC_STATIC(&vlc_sbr[num], 9, sbr_tmp[num].table_size / sbr_tmp[num].elem_size, \
88  sbr_tmp[num].sbr_bits , 1, 1, \
89  sbr_tmp[num].sbr_codes, sbr_tmp[num].elem_size, sbr_tmp[num].elem_size, \
90  size)
91 
92 #define SBR_VLC_ROW(name) \
93  { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
94 
96 
98 {
99  static const struct {
100  const void *sbr_codes, *sbr_bits;
101  const unsigned int table_size, elem_size;
102  } sbr_tmp[] = {
103  SBR_VLC_ROW(t_huffman_env_1_5dB),
104  SBR_VLC_ROW(f_huffman_env_1_5dB),
105  SBR_VLC_ROW(t_huffman_env_bal_1_5dB),
106  SBR_VLC_ROW(f_huffman_env_bal_1_5dB),
107  SBR_VLC_ROW(t_huffman_env_3_0dB),
108  SBR_VLC_ROW(f_huffman_env_3_0dB),
109  SBR_VLC_ROW(t_huffman_env_bal_3_0dB),
110  SBR_VLC_ROW(f_huffman_env_bal_3_0dB),
111  SBR_VLC_ROW(t_huffman_noise_3_0dB),
112  SBR_VLC_ROW(t_huffman_noise_bal_3_0dB),
113  };
114 
115  // SBR VLC table initialization
116  SBR_INIT_VLC_STATIC(0, 1098);
117  SBR_INIT_VLC_STATIC(1, 1092);
118  SBR_INIT_VLC_STATIC(2, 768);
119  SBR_INIT_VLC_STATIC(3, 1026);
120  SBR_INIT_VLC_STATIC(4, 1058);
121  SBR_INIT_VLC_STATIC(5, 1052);
122  SBR_INIT_VLC_STATIC(6, 544);
123  SBR_INIT_VLC_STATIC(7, 544);
124  SBR_INIT_VLC_STATIC(8, 592);
125  SBR_INIT_VLC_STATIC(9, 512);
126 
128 
129  ff_ps_init();
130 }
131 
132 /** Places SBR in pure upsampling mode. */
134  sbr->start = 0;
135  // Init defults used in pure upsampling mode
136  sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
137  sbr->m[1] = 0;
138  // Reset values for first SBR header
139  sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
140  memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
141 }
142 
144 {
145  if(sbr->mdct.mdct_bits)
146  return;
147  sbr->kx[0] = sbr->kx[1];
148  sbr_turnoff(sbr);
151  /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
152  * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
153  * and scale back down at synthesis. */
154  ff_mdct_init(&sbr->mdct, 7, 1, 1.0 / (64 * 32768.0));
155  ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0 * 32768.0);
156  ff_ps_ctx_init(&sbr->ps);
157  ff_sbrdsp_init(&sbr->dsp);
158  aacsbr_func_ptr_init(&sbr->c);
159 }
160 
162 {
163  ff_mdct_end(&sbr->mdct);
164  ff_mdct_end(&sbr->mdct_ana);
165 }
166 
167 static int qsort_comparison_function_int16(const void *a, const void *b)
168 {
169  return *(const int16_t *)a - *(const int16_t *)b;
170 }
171 
172 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
173 {
174  int i;
175  for (i = 0; i <= last_el; i++)
176  if (table[i] == needle)
177  return 1;
178  return 0;
179 }
180 
181 /// Limiter Frequency Band Table (14496-3 sp04 p198)
183 {
184  int k;
185  if (sbr->bs_limiter_bands > 0) {
186  static const float bands_warped[3] = { 1.32715174233856803909f, //2^(0.49/1.2)
187  1.18509277094158210129f, //2^(0.49/2)
188  1.11987160404675912501f }; //2^(0.49/3)
189  const float lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
190  int16_t patch_borders[7];
191  uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
192 
193  patch_borders[0] = sbr->kx[1];
194  for (k = 1; k <= sbr->num_patches; k++)
195  patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
196 
197  memcpy(sbr->f_tablelim, sbr->f_tablelow,
198  (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
199  if (sbr->num_patches > 1)
200  memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
201  (sbr->num_patches - 1) * sizeof(patch_borders[0]));
202 
203  qsort(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
204  sizeof(sbr->f_tablelim[0]),
206 
207  sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
208  while (out < sbr->f_tablelim + sbr->n_lim) {
209  if (*in >= *out * lim_bands_per_octave_warped) {
210  *++out = *in++;
211  } else if (*in == *out ||
212  !in_table_int16(patch_borders, sbr->num_patches, *in)) {
213  in++;
214  sbr->n_lim--;
215  } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
216  *out = *in++;
217  sbr->n_lim--;
218  } else {
219  *++out = *in++;
220  }
221  }
222  } else {
223  sbr->f_tablelim[0] = sbr->f_tablelow[0];
224  sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
225  sbr->n_lim = 1;
226  }
227 }
228 
230 {
231  unsigned int cnt = get_bits_count(gb);
232  uint8_t bs_header_extra_1;
233  uint8_t bs_header_extra_2;
234  int old_bs_limiter_bands = sbr->bs_limiter_bands;
235  SpectrumParameters old_spectrum_params;
236 
237  sbr->start = 1;
238 
239  // Save last spectrum parameters variables to compare to new ones
240  memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
241 
242  sbr->bs_amp_res_header = get_bits1(gb);
243  sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
244  sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
245  sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
246  skip_bits(gb, 2); // bs_reserved
247 
248  bs_header_extra_1 = get_bits1(gb);
249  bs_header_extra_2 = get_bits1(gb);
250 
251  if (bs_header_extra_1) {
252  sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
255  } else {
259  }
260 
261  // Check if spectrum parameters changed
262  if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
263  sbr->reset = 1;
264 
265  if (bs_header_extra_2) {
266  sbr->bs_limiter_bands = get_bits(gb, 2);
267  sbr->bs_limiter_gains = get_bits(gb, 2);
268  sbr->bs_interpol_freq = get_bits1(gb);
269  sbr->bs_smoothing_mode = get_bits1(gb);
270  } else {
271  sbr->bs_limiter_bands = 2;
272  sbr->bs_limiter_gains = 2;
273  sbr->bs_interpol_freq = 1;
274  sbr->bs_smoothing_mode = 1;
275  }
276 
277  if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
278  sbr_make_f_tablelim(sbr);
279 
280  return get_bits_count(gb) - cnt;
281 }
282 
283 static int array_min_int16(const int16_t *array, int nel)
284 {
285  int i, min = array[0];
286  for (i = 1; i < nel; i++)
287  min = FFMIN(array[i], min);
288  return min;
289 }
290 
291 static void make_bands(int16_t* bands, int start, int stop, int num_bands)
292 {
293  int k, previous, present;
294  float base, prod;
295 
296  base = powf((float)stop / start, 1.0f / num_bands);
297  prod = start;
298  previous = start;
299 
300  for (k = 0; k < num_bands-1; k++) {
301  prod *= base;
302  present = lrintf(prod);
303  bands[k] = present - previous;
304  previous = present;
305  }
306  bands[num_bands-1] = stop - previous;
307 }
308 
309 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
310 {
311  // Requirements (14496-3 sp04 p205)
312  if (n_master <= 0) {
313  av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
314  return -1;
315  }
316  if (bs_xover_band >= n_master) {
317  av_log(avctx, AV_LOG_ERROR,
318  "Invalid bitstream, crossover band index beyond array bounds: %d\n",
319  bs_xover_band);
320  return -1;
321  }
322  return 0;
323 }
324 
325 /// Master Frequency Band Table (14496-3 sp04 p194)
327  SpectrumParameters *spectrum)
328 {
329  unsigned int temp, max_qmf_subbands = 0;
330  unsigned int start_min, stop_min;
331  int k;
332  const int8_t *sbr_offset_ptr;
333  int16_t stop_dk[13];
334 
335  if (sbr->sample_rate < 32000) {
336  temp = 3000;
337  } else if (sbr->sample_rate < 64000) {
338  temp = 4000;
339  } else
340  temp = 5000;
341 
342  switch (sbr->sample_rate) {
343  case 16000:
344  sbr_offset_ptr = sbr_offset[0];
345  break;
346  case 22050:
347  sbr_offset_ptr = sbr_offset[1];
348  break;
349  case 24000:
350  sbr_offset_ptr = sbr_offset[2];
351  break;
352  case 32000:
353  sbr_offset_ptr = sbr_offset[3];
354  break;
355  case 44100: case 48000: case 64000:
356  sbr_offset_ptr = sbr_offset[4];
357  break;
358  case 88200: case 96000: case 128000: case 176400: case 192000:
359  sbr_offset_ptr = sbr_offset[5];
360  break;
361  default:
363  "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
364  return -1;
365  }
366 
367  start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
368  stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
369 
370  sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
371 
372  if (spectrum->bs_stop_freq < 14) {
373  sbr->k[2] = stop_min;
374  make_bands(stop_dk, stop_min, 64, 13);
375  qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16);
376  for (k = 0; k < spectrum->bs_stop_freq; k++)
377  sbr->k[2] += stop_dk[k];
378  } else if (spectrum->bs_stop_freq == 14) {
379  sbr->k[2] = 2*sbr->k[0];
380  } else if (spectrum->bs_stop_freq == 15) {
381  sbr->k[2] = 3*sbr->k[0];
382  } else {
384  "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
385  return -1;
386  }
387  sbr->k[2] = FFMIN(64, sbr->k[2]);
388 
389  // Requirements (14496-3 sp04 p205)
390  if (sbr->sample_rate <= 32000) {
391  max_qmf_subbands = 48;
392  } else if (sbr->sample_rate == 44100) {
393  max_qmf_subbands = 35;
394  } else if (sbr->sample_rate >= 48000)
395  max_qmf_subbands = 32;
396  else
397  av_assert0(0);
398 
399  if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
401  "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
402  return -1;
403  }
404 
405  if (!spectrum->bs_freq_scale) {
406  int dk, k2diff;
407 
408  dk = spectrum->bs_alter_scale + 1;
409  sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
411  return -1;
412 
413  for (k = 1; k <= sbr->n_master; k++)
414  sbr->f_master[k] = dk;
415 
416  k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
417  if (k2diff < 0) {
418  sbr->f_master[1]--;
419  sbr->f_master[2]-= (k2diff < -1);
420  } else if (k2diff) {
421  sbr->f_master[sbr->n_master]++;
422  }
423 
424  sbr->f_master[0] = sbr->k[0];
425  for (k = 1; k <= sbr->n_master; k++)
426  sbr->f_master[k] += sbr->f_master[k - 1];
427 
428  } else {
429  int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3}
430  int two_regions, num_bands_0;
431  int vdk0_max, vdk1_min;
432  int16_t vk0[49];
433 
434  if (49 * sbr->k[2] > 110 * sbr->k[0]) {
435  two_regions = 1;
436  sbr->k[1] = 2 * sbr->k[0];
437  } else {
438  two_regions = 0;
439  sbr->k[1] = sbr->k[2];
440  }
441 
442  num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
443 
444  if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
445  av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
446  return -1;
447  }
448 
449  vk0[0] = 0;
450 
451  make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
452 
453  qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16);
454  vdk0_max = vk0[num_bands_0];
455 
456  vk0[0] = sbr->k[0];
457  for (k = 1; k <= num_bands_0; k++) {
458  if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
459  av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
460  return -1;
461  }
462  vk0[k] += vk0[k-1];
463  }
464 
465  if (two_regions) {
466  int16_t vk1[49];
467  float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
468  : 1.0f; // bs_alter_scale = {0,1}
469  int num_bands_1 = lrintf(half_bands * invwarp *
470  log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
471 
472  make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
473 
474  vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
475 
476  if (vdk1_min < vdk0_max) {
477  int change;
478  qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
479  change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
480  vk1[1] += change;
481  vk1[num_bands_1] -= change;
482  }
483 
484  qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
485 
486  vk1[0] = sbr->k[1];
487  for (k = 1; k <= num_bands_1; k++) {
488  if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
489  av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
490  return -1;
491  }
492  vk1[k] += vk1[k-1];
493  }
494 
495  sbr->n_master = num_bands_0 + num_bands_1;
497  return -1;
498  memcpy(&sbr->f_master[0], vk0,
499  (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
500  memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
501  num_bands_1 * sizeof(sbr->f_master[0]));
502 
503  } else {
504  sbr->n_master = num_bands_0;
506  return -1;
507  memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
508  }
509  }
510 
511  return 0;
512 }
513 
514 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
516 {
517  int i, k, last_k = -1, last_msb = -1, sb = 0;
518  int msb = sbr->k[0];
519  int usb = sbr->kx[1];
520  int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
521 
522  sbr->num_patches = 0;
523 
524  if (goal_sb < sbr->kx[1] + sbr->m[1]) {
525  for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
526  } else
527  k = sbr->n_master;
528 
529  do {
530  int odd = 0;
531  if (k == last_k && msb == last_msb) {
532  av_log(ac->avctx, AV_LOG_ERROR, "patch construction failed\n");
533  return AVERROR_INVALIDDATA;
534  }
535  last_k = k;
536  last_msb = msb;
537  for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
538  sb = sbr->f_master[i];
539  odd = (sb + sbr->k[0]) & 1;
540  }
541 
542  // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
543  // After this check the final number of patches can still be six which is
544  // illegal however the Coding Technologies decoder check stream has a final
545  // count of 6 patches
546  if (sbr->num_patches > 5) {
547  av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
548  return -1;
549  }
550 
551  sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
552  sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
553 
554  if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
555  usb = sb;
556  msb = sb;
557  sbr->num_patches++;
558  } else
559  msb = sbr->kx[1];
560 
561  if (sbr->f_master[k] - sb < 3)
562  k = sbr->n_master;
563  } while (sb != sbr->kx[1] + sbr->m[1]);
564 
565  if (sbr->num_patches > 1 &&
566  sbr->patch_num_subbands[sbr->num_patches - 1] < 3)
567  sbr->num_patches--;
568 
569  return 0;
570 }
571 
572 /// Derived Frequency Band Tables (14496-3 sp04 p197)
574 {
575  int k, temp;
576 
577  sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
578  sbr->n[0] = (sbr->n[1] + 1) >> 1;
579 
580  memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
581  (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
582  sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
583  sbr->kx[1] = sbr->f_tablehigh[0];
584 
585  // Requirements (14496-3 sp04 p205)
586  if (sbr->kx[1] + sbr->m[1] > 64) {
588  "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
589  return -1;
590  }
591  if (sbr->kx[1] > 32) {
592  av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
593  return -1;
594  }
595 
596  sbr->f_tablelow[0] = sbr->f_tablehigh[0];
597  temp = sbr->n[1] & 1;
598  for (k = 1; k <= sbr->n[0]; k++)
599  sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
600 
602  log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
603  if (sbr->n_q > 5) {
604  av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
605  return -1;
606  }
607 
608  sbr->f_tablenoise[0] = sbr->f_tablelow[0];
609  temp = 0;
610  for (k = 1; k <= sbr->n_q; k++) {
611  temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
612  sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
613  }
614 
615  if (sbr_hf_calc_npatches(ac, sbr) < 0)
616  return -1;
617 
618  sbr_make_f_tablelim(sbr);
619 
620  sbr->data[0].f_indexnoise = 0;
621  sbr->data[1].f_indexnoise = 0;
622 
623  return 0;
624 }
625 
627  int elements)
628 {
629  int i;
630  for (i = 0; i < elements; i++) {
631  vec[i] = get_bits1(gb);
632  }
633 }
634 
635 /** ceil(log2(index+1)) */
636 static const int8_t ceil_log2[] = {
637  0, 1, 2, 2, 3, 3,
638 };
639 
641  GetBitContext *gb, SBRData *ch_data)
642 {
643  int i;
644  int bs_pointer = 0;
645  // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
646  int abs_bord_trail = 16;
647  int num_rel_lead, num_rel_trail;
648  unsigned bs_num_env_old = ch_data->bs_num_env;
649 
650  ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
651  ch_data->bs_amp_res = sbr->bs_amp_res_header;
652  ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
653 
654  switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
655  case FIXFIX:
656  ch_data->bs_num_env = 1 << get_bits(gb, 2);
657  num_rel_lead = ch_data->bs_num_env - 1;
658  if (ch_data->bs_num_env == 1)
659  ch_data->bs_amp_res = 0;
660 
661  if (ch_data->bs_num_env > 4) {
663  "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
664  ch_data->bs_num_env);
665  return -1;
666  }
667 
668  ch_data->t_env[0] = 0;
669  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
670 
671  abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
672  ch_data->bs_num_env;
673  for (i = 0; i < num_rel_lead; i++)
674  ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
675 
676  ch_data->bs_freq_res[1] = get_bits1(gb);
677  for (i = 1; i < ch_data->bs_num_env; i++)
678  ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
679  break;
680  case FIXVAR:
681  abs_bord_trail += get_bits(gb, 2);
682  num_rel_trail = get_bits(gb, 2);
683  ch_data->bs_num_env = num_rel_trail + 1;
684  ch_data->t_env[0] = 0;
685  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
686 
687  for (i = 0; i < num_rel_trail; i++)
688  ch_data->t_env[ch_data->bs_num_env - 1 - i] =
689  ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
690 
691  bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
692 
693  for (i = 0; i < ch_data->bs_num_env; i++)
694  ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
695  break;
696  case VARFIX:
697  ch_data->t_env[0] = get_bits(gb, 2);
698  num_rel_lead = get_bits(gb, 2);
699  ch_data->bs_num_env = num_rel_lead + 1;
700  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
701 
702  for (i = 0; i < num_rel_lead; i++)
703  ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
704 
705  bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
706 
707  get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
708  break;
709  case VARVAR:
710  ch_data->t_env[0] = get_bits(gb, 2);
711  abs_bord_trail += get_bits(gb, 2);
712  num_rel_lead = get_bits(gb, 2);
713  num_rel_trail = get_bits(gb, 2);
714  ch_data->bs_num_env = num_rel_lead + num_rel_trail + 1;
715 
716  if (ch_data->bs_num_env > 5) {
718  "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
719  ch_data->bs_num_env);
720  return -1;
721  }
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 
737  av_assert0(bs_pointer >= 0);
738  if (bs_pointer > ch_data->bs_num_env + 1) {
740  "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
741  bs_pointer);
742  return -1;
743  }
744 
745  for (i = 1; i <= ch_data->bs_num_env; i++) {
746  if (ch_data->t_env[i-1] > ch_data->t_env[i]) {
747  av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n");
748  return -1;
749  }
750  }
751 
752  ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
753 
754  ch_data->t_q[0] = ch_data->t_env[0];
755  ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
756  if (ch_data->bs_num_noise > 1) {
757  int idx;
758  if (ch_data->bs_frame_class == FIXFIX) {
759  idx = ch_data->bs_num_env >> 1;
760  } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
761  idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
762  } else { // VARFIX
763  if (!bs_pointer)
764  idx = 1;
765  else if (bs_pointer == 1)
766  idx = ch_data->bs_num_env - 1;
767  else // bs_pointer > 1
768  idx = bs_pointer - 1;
769  }
770  ch_data->t_q[1] = ch_data->t_env[idx];
771  }
772 
773  ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
774  ch_data->e_a[1] = -1;
775  if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
776  ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
777  } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
778  ch_data->e_a[1] = bs_pointer - 1;
779 
780  return 0;
781 }
782 
783 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
784  //These variables are saved from the previous frame rather than copied
785  dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
786  dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
787  dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
788 
789  //These variables are read from the bitstream and therefore copied
790  memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
791  memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
792  memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
793  dst->bs_num_env = src->bs_num_env;
794  dst->bs_amp_res = src->bs_amp_res;
795  dst->bs_num_noise = src->bs_num_noise;
796  dst->bs_frame_class = src->bs_frame_class;
797  dst->e_a[1] = src->e_a[1];
798 }
799 
800 /// Read how the envelope and noise floor data is delta coded
802  SBRData *ch_data)
803 {
804  get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
805  get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
806 }
807 
808 /// Read inverse filtering data
810  SBRData *ch_data)
811 {
812  int i;
813 
814  memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
815  for (i = 0; i < sbr->n_q; i++)
816  ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
817 }
818 
820  SBRData *ch_data, int ch)
821 {
822  int bits;
823  int i, j, k;
824  VLC_TYPE (*t_huff)[2], (*f_huff)[2];
825  int t_lav, f_lav;
826  const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
827  const int odd = sbr->n[1] & 1;
828 
829  if (sbr->bs_coupling && ch) {
830  if (ch_data->bs_amp_res) {
831  bits = 5;
832  t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
834  f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
836  } else {
837  bits = 6;
838  t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
840  f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
842  }
843  } else {
844  if (ch_data->bs_amp_res) {
845  bits = 6;
846  t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
848  f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
850  } else {
851  bits = 7;
852  t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
854  f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
856  }
857  }
858 
859  for (i = 0; i < ch_data->bs_num_env; i++) {
860  if (ch_data->bs_df_env[i]) {
861  // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
862  if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
863  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
864  ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
865  } else if (ch_data->bs_freq_res[i + 1]) {
866  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
867  k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
868  ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
869  }
870  } else {
871  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
872  k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
873  ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
874  }
875  }
876  } else {
877  ch_data->env_facs[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
878  for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
879  ch_data->env_facs[i + 1][j] = ch_data->env_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
880  }
881  }
882 
883  //assign 0th elements of env_facs from last elements
884  memcpy(ch_data->env_facs[0], ch_data->env_facs[ch_data->bs_num_env],
885  sizeof(ch_data->env_facs[0]));
886 }
887 
889  SBRData *ch_data, int ch)
890 {
891  int i, j;
892  VLC_TYPE (*t_huff)[2], (*f_huff)[2];
893  int t_lav, f_lav;
894  int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
895 
896  if (sbr->bs_coupling && ch) {
897  t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
899  f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
901  } else {
902  t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
904  f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
906  }
907 
908  for (i = 0; i < ch_data->bs_num_noise; i++) {
909  if (ch_data->bs_df_noise[i]) {
910  for (j = 0; j < sbr->n_q; j++)
911  ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
912  } else {
913  ch_data->noise_facs[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
914  for (j = 1; j < sbr->n_q; j++)
915  ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
916  }
917  }
918 
919  //assign 0th elements of noise_facs from last elements
920  memcpy(ch_data->noise_facs[0], ch_data->noise_facs[ch_data->bs_num_noise],
921  sizeof(ch_data->noise_facs[0]));
922 }
923 
925  GetBitContext *gb,
926  int bs_extension_id, int *num_bits_left)
927 {
928  switch (bs_extension_id) {
929  case EXTENSION_ID_PS:
930  if (!ac->oc[1].m4ac.ps) {
931  av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
932  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
933  *num_bits_left = 0;
934  } else {
935 #if 1
936  *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left);
938 #else
939  avpriv_report_missing_feature(ac->avctx, "Parametric Stereo");
940  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
941  *num_bits_left = 0;
942 #endif
943  }
944  break;
945  default:
946  // some files contain 0-padding
947  if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
948  avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
949  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
950  *num_bits_left = 0;
951  break;
952  }
953 }
954 
957  GetBitContext *gb)
958 {
959  if (get_bits1(gb)) // bs_data_extra
960  skip_bits(gb, 4); // bs_reserved
961 
962  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
963  return -1;
964  read_sbr_dtdf(sbr, gb, &sbr->data[0]);
965  read_sbr_invf(sbr, gb, &sbr->data[0]);
966  read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
967  read_sbr_noise(sbr, gb, &sbr->data[0], 0);
968 
969  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
970  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
971 
972  return 0;
973 }
974 
977  GetBitContext *gb)
978 {
979  if (get_bits1(gb)) // bs_data_extra
980  skip_bits(gb, 8); // bs_reserved
981 
982  if ((sbr->bs_coupling = get_bits1(gb))) {
983  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
984  return -1;
985  copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
986  read_sbr_dtdf(sbr, gb, &sbr->data[0]);
987  read_sbr_dtdf(sbr, gb, &sbr->data[1]);
988  read_sbr_invf(sbr, gb, &sbr->data[0]);
989  memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
990  memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
991  read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
992  read_sbr_noise(sbr, gb, &sbr->data[0], 0);
993  read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
994  read_sbr_noise(sbr, gb, &sbr->data[1], 1);
995  } else {
996  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
997  read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
998  return -1;
999  read_sbr_dtdf(sbr, gb, &sbr->data[0]);
1000  read_sbr_dtdf(sbr, gb, &sbr->data[1]);
1001  read_sbr_invf(sbr, gb, &sbr->data[0]);
1002  read_sbr_invf(sbr, gb, &sbr->data[1]);
1003  read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
1004  read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
1005  read_sbr_noise(sbr, gb, &sbr->data[0], 0);
1006  read_sbr_noise(sbr, gb, &sbr->data[1], 1);
1007  }
1008 
1009  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1010  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1011  if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1012  get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1013 
1014  return 0;
1015 }
1016 
1017 static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
1018  GetBitContext *gb, int id_aac)
1019 {
1020  unsigned int cnt = get_bits_count(gb);
1021 
1022  if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
1023  if (read_sbr_single_channel_element(ac, sbr, gb)) {
1024  sbr_turnoff(sbr);
1025  return get_bits_count(gb) - cnt;
1026  }
1027  } else if (id_aac == TYPE_CPE) {
1028  if (read_sbr_channel_pair_element(ac, sbr, gb)) {
1029  sbr_turnoff(sbr);
1030  return get_bits_count(gb) - cnt;
1031  }
1032  } else {
1033  av_log(ac->avctx, AV_LOG_ERROR,
1034  "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1035  sbr_turnoff(sbr);
1036  return get_bits_count(gb) - cnt;
1037  }
1038  if (get_bits1(gb)) { // bs_extended_data
1039  int num_bits_left = get_bits(gb, 4); // bs_extension_size
1040  if (num_bits_left == 15)
1041  num_bits_left += get_bits(gb, 8); // bs_esc_count
1042 
1043  num_bits_left <<= 3;
1044  while (num_bits_left > 7) {
1045  num_bits_left -= 2;
1046  read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
1047  }
1048  if (num_bits_left < 0) {
1049  av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
1050  }
1051  if (num_bits_left > 0)
1052  skip_bits(gb, num_bits_left);
1053  }
1054 
1055  return get_bits_count(gb) - cnt;
1056 }
1057 
1059 {
1060  int err;
1061  err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
1062  if (err >= 0)
1063  err = sbr_make_f_derived(ac, sbr);
1064  if (err < 0) {
1065  av_log(ac->avctx, AV_LOG_ERROR,
1066  "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1067  sbr_turnoff(sbr);
1068  }
1069 }
1070 
1071 /**
1072  * Decode Spectral Band Replication extension data; reference: table 4.55.
1073  *
1074  * @param crc flag indicating the presence of CRC checksum
1075  * @param cnt length of TYPE_FIL syntactic element in bytes
1076  *
1077  * @return Returns number of bytes consumed from the TYPE_FIL element.
1078  */
1080  GetBitContext *gb_host, int crc, int cnt, int id_aac)
1081 {
1082  unsigned int num_sbr_bits = 0, num_align_bits;
1083  unsigned bytes_read;
1084  GetBitContext gbc = *gb_host, *gb = &gbc;
1085  skip_bits_long(gb_host, cnt*8 - 4);
1086 
1087  sbr->reset = 0;
1088 
1089  if (!sbr->sample_rate)
1090  sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
1091  if (!ac->oc[1].m4ac.ext_sample_rate)
1092  ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
1093 
1094  if (crc) {
1095  skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
1096  num_sbr_bits += 10;
1097  }
1098 
1099  //Save some state from the previous frame.
1100  sbr->kx[0] = sbr->kx[1];
1101  sbr->m[0] = sbr->m[1];
1102  sbr->kx_and_m_pushed = 1;
1103 
1104  num_sbr_bits++;
1105  if (get_bits1(gb)) // bs_header_flag
1106  num_sbr_bits += read_sbr_header(sbr, gb);
1107 
1108  if (sbr->reset)
1109  sbr_reset(ac, sbr);
1110 
1111  if (sbr->start)
1112  num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
1113 
1114  num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1115  bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1116 
1117  if (bytes_read > cnt) {
1118  av_log(ac->avctx, AV_LOG_ERROR,
1119  "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1120  }
1121  return cnt;
1122 }
1123 
1124 /// Dequantization and stereo decoding (14496-3 sp04 p203)
1125 static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
1126 {
1127  int k, e;
1128  int ch;
1129 
1130  if (id_aac == TYPE_CPE && sbr->bs_coupling) {
1131  float alpha = sbr->data[0].bs_amp_res ? 1.0f : 0.5f;
1132  float pan_offset = sbr->data[0].bs_amp_res ? 12.0f : 24.0f;
1133  for (e = 1; e <= sbr->data[0].bs_num_env; e++) {
1134  for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
1135  float temp1 = exp2f(sbr->data[0].env_facs[e][k] * alpha + 7.0f);
1136  float temp2 = exp2f((pan_offset - sbr->data[1].env_facs[e][k]) * alpha);
1137  float fac;
1138  if (temp1 > 1E20) {
1139  av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
1140  temp1 = 1;
1141  }
1142  fac = temp1 / (1.0f + temp2);
1143  sbr->data[0].env_facs[e][k] = fac;
1144  sbr->data[1].env_facs[e][k] = fac * temp2;
1145  }
1146  }
1147  for (e = 1; e <= sbr->data[0].bs_num_noise; e++) {
1148  for (k = 0; k < sbr->n_q; k++) {
1149  float temp1 = exp2f(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs[e][k] + 1);
1150  float temp2 = exp2f(12 - sbr->data[1].noise_facs[e][k]);
1151  float fac;
1152  if (temp1 > 1E20) {
1153  av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
1154  temp1 = 1;
1155  }
1156  fac = temp1 / (1.0f + temp2);
1157  sbr->data[0].noise_facs[e][k] = fac;
1158  sbr->data[1].noise_facs[e][k] = fac * temp2;
1159  }
1160  }
1161  } else { // SCE or one non-coupled CPE
1162  for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
1163  float alpha = sbr->data[ch].bs_amp_res ? 1.0f : 0.5f;
1164  for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
1165  for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++){
1166  sbr->data[ch].env_facs[e][k] =
1167  exp2f(alpha * sbr->data[ch].env_facs[e][k] + 6.0f);
1168  if (sbr->data[ch].env_facs[e][k] > 1E20) {
1169  av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
1170  sbr->data[ch].env_facs[e][k] = 1;
1171  }
1172  }
1173 
1174  for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
1175  for (k = 0; k < sbr->n_q; k++)
1176  sbr->data[ch].noise_facs[e][k] =
1177  exp2f(NOISE_FLOOR_OFFSET - sbr->data[ch].noise_facs[e][k]);
1178  }
1179  }
1180 }
1181 
1182 /**
1183  * Analysis QMF Bank (14496-3 sp04 p206)
1184  *
1185  * @param x pointer to the beginning of the first sample window
1186  * @param W array of complex-valued samples split into subbands
1187  */
1188 #ifndef sbr_qmf_analysis
1190  SBRDSPContext *sbrdsp, const float *in, float *x,
1191  float z[320], float W[2][32][32][2], int buf_idx)
1192 {
1193  int i;
1194  memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1195  memcpy(x+288, in, 1024*sizeof(x[0]));
1196  for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1197  // are not supported
1198  dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1199  sbrdsp->sum64x5(z);
1200  sbrdsp->qmf_pre_shuffle(z);
1201  mdct->imdct_half(mdct, z, z+64);
1202  sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
1203  x += 32;
1204  }
1205 }
1206 #endif
1207 
1208 /**
1209  * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1210  * (14496-3 sp04 p206)
1211  */
1212 #ifndef sbr_qmf_synthesis
1213 static void sbr_qmf_synthesis(FFTContext *mdct,
1214  SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
1215  float *out, float X[2][38][64],
1216  float mdct_buf[2][64],
1217  float *v0, int *v_off, const unsigned int div)
1218 {
1219  int i, n;
1220  const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
1221  const int step = 128 >> div;
1222  float *v;
1223  for (i = 0; i < 32; i++) {
1224  if (*v_off < step) {
1225  int saved_samples = (1280 - 128) >> div;
1226  memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(float));
1227  *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
1228  } else {
1229  *v_off -= step;
1230  }
1231  v = v0 + *v_off;
1232  if (div) {
1233  for (n = 0; n < 32; n++) {
1234  X[0][i][ n] = -X[0][i][n];
1235  X[0][i][32+n] = X[1][i][31-n];
1236  }
1237  mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1238  sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
1239  } else {
1240  sbrdsp->neg_odd_64(X[1][i]);
1241  mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1242  mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
1243  sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
1244  }
1245  dsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1246  dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1247  dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1248  dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1249  dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1250  dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1251  dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1252  dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1253  dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1254  dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1255  out += 64 >> div;
1256  }
1257 }
1258 #endif
1259 
1260 /** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering
1261  * (14496-3 sp04 p214)
1262  * Warning: This routine does not seem numerically stable.
1263  */
1265  float (*alpha0)[2], float (*alpha1)[2],
1266  const float X_low[32][40][2], int k0)
1267 {
1268  int k;
1269  for (k = 0; k < k0; k++) {
1270  LOCAL_ALIGNED_16(float, phi, [3], [2][2]);
1271  float dk;
1272 
1273  dsp->autocorrelate(X_low[k], phi);
1274 
1275  dk = phi[2][1][0] * phi[1][0][0] -
1276  (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
1277 
1278  if (!dk) {
1279  alpha1[k][0] = 0;
1280  alpha1[k][1] = 0;
1281  } else {
1282  float temp_real, temp_im;
1283  temp_real = phi[0][0][0] * phi[1][1][0] -
1284  phi[0][0][1] * phi[1][1][1] -
1285  phi[0][1][0] * phi[1][0][0];
1286  temp_im = phi[0][0][0] * phi[1][1][1] +
1287  phi[0][0][1] * phi[1][1][0] -
1288  phi[0][1][1] * phi[1][0][0];
1289 
1290  alpha1[k][0] = temp_real / dk;
1291  alpha1[k][1] = temp_im / dk;
1292  }
1293 
1294  if (!phi[1][0][0]) {
1295  alpha0[k][0] = 0;
1296  alpha0[k][1] = 0;
1297  } else {
1298  float temp_real, temp_im;
1299  temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
1300  alpha1[k][1] * phi[1][1][1];
1301  temp_im = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
1302  alpha1[k][0] * phi[1][1][1];
1303 
1304  alpha0[k][0] = -temp_real / phi[1][0][0];
1305  alpha0[k][1] = -temp_im / phi[1][0][0];
1306  }
1307 
1308  if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
1309  alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
1310  alpha1[k][0] = 0;
1311  alpha1[k][1] = 0;
1312  alpha0[k][0] = 0;
1313  alpha0[k][1] = 0;
1314  }
1315  }
1316 }
1317 
1318 /// Chirp Factors (14496-3 sp04 p214)
1319 static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
1320 {
1321  int i;
1322  float new_bw;
1323  static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f };
1324 
1325  for (i = 0; i < sbr->n_q; i++) {
1326  if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1) {
1327  new_bw = 0.6f;
1328  } else
1329  new_bw = bw_tab[ch_data->bs_invf_mode[0][i]];
1330 
1331  if (new_bw < ch_data->bw_array[i]) {
1332  new_bw = 0.75f * new_bw + 0.25f * ch_data->bw_array[i];
1333  } else
1334  new_bw = 0.90625f * new_bw + 0.09375f * ch_data->bw_array[i];
1335  ch_data->bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw;
1336  }
1337 }
1338 
1339 /// Generate the subband filtered lowband
1341  float X_low[32][40][2], const float W[2][32][32][2],
1342  int buf_idx)
1343 {
1344  int i, k;
1345  const int t_HFGen = 8;
1346  const int i_f = 32;
1347  memset(X_low, 0, 32*sizeof(*X_low));
1348  for (k = 0; k < sbr->kx[1]; k++) {
1349  for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1350  X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1351  X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1352  }
1353  }
1354  buf_idx = 1-buf_idx;
1355  for (k = 0; k < sbr->kx[0]; k++) {
1356  for (i = 0; i < t_HFGen; i++) {
1357  X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1358  X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1359  }
1360  }
1361  return 0;
1362 }
1363 
1364 /// High Frequency Generator (14496-3 sp04 p215)
1366  float X_high[64][40][2], const float X_low[32][40][2],
1367  const float (*alpha0)[2], const float (*alpha1)[2],
1368  const float bw_array[5], const uint8_t *t_env,
1369  int bs_num_env)
1370 {
1371  int j, x;
1372  int g = 0;
1373  int k = sbr->kx[1];
1374  for (j = 0; j < sbr->num_patches; j++) {
1375  for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
1376  const int p = sbr->patch_start_subband[j] + x;
1377  while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
1378  g++;
1379  g--;
1380 
1381  if (g < 0) {
1382  av_log(ac->avctx, AV_LOG_ERROR,
1383  "ERROR : no subband found for frequency %d\n", k);
1384  return -1;
1385  }
1386 
1387  sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
1388  X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
1389  alpha0[p], alpha1[p], bw_array[g],
1390  2 * t_env[0], 2 * t_env[bs_num_env]);
1391  }
1392  }
1393  if (k < sbr->m[1] + sbr->kx[1])
1394  memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
1395 
1396  return 0;
1397 }
1398 
1399 /// Generate the subband filtered lowband
1400 static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64],
1401  const float Y0[38][64][2], const float Y1[38][64][2],
1402  const float X_low[32][40][2], int ch)
1403 {
1404  int k, i;
1405  const int i_f = 32;
1406  const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
1407  memset(X, 0, 2*sizeof(*X));
1408  for (k = 0; k < sbr->kx[0]; k++) {
1409  for (i = 0; i < i_Temp; i++) {
1410  X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1411  X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1412  }
1413  }
1414  for (; k < sbr->kx[0] + sbr->m[0]; k++) {
1415  for (i = 0; i < i_Temp; i++) {
1416  X[0][i][k] = Y0[i + i_f][k][0];
1417  X[1][i][k] = Y0[i + i_f][k][1];
1418  }
1419  }
1420 
1421  for (k = 0; k < sbr->kx[1]; k++) {
1422  for (i = i_Temp; i < 38; i++) {
1423  X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1424  X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1425  }
1426  }
1427  for (; k < sbr->kx[1] + sbr->m[1]; k++) {
1428  for (i = i_Temp; i < i_f; i++) {
1429  X[0][i][k] = Y1[i][k][0];
1430  X[1][i][k] = Y1[i][k][1];
1431  }
1432  }
1433  return 0;
1434 }
1435 
1436 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1437  * (14496-3 sp04 p217)
1438  */
1440  SBRData *ch_data, int e_a[2])
1441 {
1442  int e, i, m;
1443 
1444  memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
1445  for (e = 0; e < ch_data->bs_num_env; e++) {
1446  const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
1447  uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1448  int k;
1449 
1450  if (sbr->kx[1] != table[0]) {
1451  av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
1452  "Derived frequency tables were not regenerated.\n");
1453  sbr_turnoff(sbr);
1454  return AVERROR_BUG;
1455  }
1456  for (i = 0; i < ilim; i++)
1457  for (m = table[i]; m < table[i + 1]; m++)
1458  sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
1459 
1460  // ch_data->bs_num_noise > 1 => 2 noise floors
1461  k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
1462  for (i = 0; i < sbr->n_q; i++)
1463  for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
1464  sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
1465 
1466  for (i = 0; i < sbr->n[1]; i++) {
1467  if (ch_data->bs_add_harmonic_flag) {
1468  const unsigned int m_midpoint =
1469  (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
1470 
1471  ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
1472  (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
1473  }
1474  }
1475 
1476  for (i = 0; i < ilim; i++) {
1477  int additional_sinusoid_present = 0;
1478  for (m = table[i]; m < table[i + 1]; m++) {
1479  if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
1480  additional_sinusoid_present = 1;
1481  break;
1482  }
1483  }
1484  memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
1485  (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
1486  }
1487  }
1488 
1489  memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
1490  return 0;
1491 }
1492 
1493 /// Estimation of current envelope (14496-3 sp04 p218)
1494 static void sbr_env_estimate(float (*e_curr)[48], float X_high[64][40][2],
1495  SpectralBandReplication *sbr, SBRData *ch_data)
1496 {
1497  int e, m;
1498  int kx1 = sbr->kx[1];
1499 
1500  if (sbr->bs_interpol_freq) {
1501  for (e = 0; e < ch_data->bs_num_env; e++) {
1502  const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1503  int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1504  int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1505 
1506  for (m = 0; m < sbr->m[1]; m++) {
1507  float sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
1508  e_curr[e][m] = sum * recip_env_size;
1509  }
1510  }
1511  } else {
1512  int k, p;
1513 
1514  for (e = 0; e < ch_data->bs_num_env; e++) {
1515  const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1516  int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1517  int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1518  const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1519 
1520  for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
1521  float sum = 0.0f;
1522  const int den = env_size * (table[p + 1] - table[p]);
1523 
1524  for (k = table[p]; k < table[p + 1]; k++) {
1525  sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
1526  }
1527  sum /= den;
1528  for (k = table[p]; k < table[p + 1]; k++) {
1529  e_curr[e][k - kx1] = sum;
1530  }
1531  }
1532  }
1533  }
1534 }
1535 
1536 /**
1537  * Calculation of levels of additional HF signal components (14496-3 sp04 p219)
1538  * and Calculation of gain (14496-3 sp04 p219)
1539  */
1541  SBRData *ch_data, const int e_a[2])
1542 {
1543  int e, k, m;
1544  // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off)
1545  static const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
1546 
1547  for (e = 0; e < ch_data->bs_num_env; e++) {
1548  int delta = !((e == e_a[1]) || (e == e_a[0]));
1549  for (k = 0; k < sbr->n_lim; k++) {
1550  float gain_boost, gain_max;
1551  float sum[2] = { 0.0f, 0.0f };
1552  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1553  const float temp = sbr->e_origmapped[e][m] / (1.0f + sbr->q_mapped[e][m]);
1554  sbr->q_m[e][m] = sqrtf(temp * sbr->q_mapped[e][m]);
1555  sbr->s_m[e][m] = sqrtf(temp * ch_data->s_indexmapped[e + 1][m]);
1556  if (!sbr->s_mapped[e][m]) {
1557  sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] /
1558  ((1.0f + sbr->e_curr[e][m]) *
1559  (1.0f + sbr->q_mapped[e][m] * delta)));
1560  } else {
1561  sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] * sbr->q_mapped[e][m] /
1562  ((1.0f + sbr->e_curr[e][m]) *
1563  (1.0f + sbr->q_mapped[e][m])));
1564  }
1565  }
1566  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1567  sum[0] += sbr->e_origmapped[e][m];
1568  sum[1] += sbr->e_curr[e][m];
1569  }
1570  gain_max = limgain[sbr->bs_limiter_gains] * sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
1571  gain_max = FFMIN(100000.f, gain_max);
1572  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1573  float q_m_max = sbr->q_m[e][m] * gain_max / sbr->gain[e][m];
1574  sbr->q_m[e][m] = FFMIN(sbr->q_m[e][m], q_m_max);
1575  sbr->gain[e][m] = FFMIN(sbr->gain[e][m], gain_max);
1576  }
1577  sum[0] = sum[1] = 0.0f;
1578  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1579  sum[0] += sbr->e_origmapped[e][m];
1580  sum[1] += sbr->e_curr[e][m] * sbr->gain[e][m] * sbr->gain[e][m]
1581  + sbr->s_m[e][m] * sbr->s_m[e][m]
1582  + (delta && !sbr->s_m[e][m]) * sbr->q_m[e][m] * sbr->q_m[e][m];
1583  }
1584  gain_boost = sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
1585  gain_boost = FFMIN(1.584893192f, gain_boost);
1586  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1587  sbr->gain[e][m] *= gain_boost;
1588  sbr->q_m[e][m] *= gain_boost;
1589  sbr->s_m[e][m] *= gain_boost;
1590  }
1591  }
1592  }
1593 }
1594 
1595 /// Assembling HF Signals (14496-3 sp04 p220)
1596 static void sbr_hf_assemble(float Y1[38][64][2],
1597  const float X_high[64][40][2],
1598  SpectralBandReplication *sbr, SBRData *ch_data,
1599  const int e_a[2])
1600 {
1601  int e, i, j, m;
1602  const int h_SL = 4 * !sbr->bs_smoothing_mode;
1603  const int kx = sbr->kx[1];
1604  const int m_max = sbr->m[1];
1605  static const float h_smooth[5] = {
1606  0.33333333333333,
1607  0.30150283239582,
1608  0.21816949906249,
1609  0.11516383427084,
1610  0.03183050093751,
1611  };
1612  float (*g_temp)[48] = ch_data->g_temp, (*q_temp)[48] = ch_data->q_temp;
1613  int indexnoise = ch_data->f_indexnoise;
1614  int indexsine = ch_data->f_indexsine;
1615 
1616  if (sbr->reset) {
1617  for (i = 0; i < h_SL; i++) {
1618  memcpy(g_temp[i + 2*ch_data->t_env[0]], sbr->gain[0], m_max * sizeof(sbr->gain[0][0]));
1619  memcpy(q_temp[i + 2*ch_data->t_env[0]], sbr->q_m[0], m_max * sizeof(sbr->q_m[0][0]));
1620  }
1621  } else if (h_SL) {
1622  for (i = 0; i < 4; i++) {
1623  memcpy(g_temp[i + 2 * ch_data->t_env[0]],
1624  g_temp[i + 2 * ch_data->t_env_num_env_old],
1625  sizeof(g_temp[0]));
1626  memcpy(q_temp[i + 2 * ch_data->t_env[0]],
1627  q_temp[i + 2 * ch_data->t_env_num_env_old],
1628  sizeof(q_temp[0]));
1629  }
1630  }
1631 
1632  for (e = 0; e < ch_data->bs_num_env; e++) {
1633  for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
1634  memcpy(g_temp[h_SL + i], sbr->gain[e], m_max * sizeof(sbr->gain[0][0]));
1635  memcpy(q_temp[h_SL + i], sbr->q_m[e], m_max * sizeof(sbr->q_m[0][0]));
1636  }
1637  }
1638 
1639  for (e = 0; e < ch_data->bs_num_env; e++) {
1640  for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
1641  LOCAL_ALIGNED_16(float, g_filt_tab, [48]);
1642  LOCAL_ALIGNED_16(float, q_filt_tab, [48]);
1643  float *g_filt, *q_filt;
1644 
1645  if (h_SL && e != e_a[0] && e != e_a[1]) {
1646  g_filt = g_filt_tab;
1647  q_filt = q_filt_tab;
1648  for (m = 0; m < m_max; m++) {
1649  const int idx1 = i + h_SL;
1650  g_filt[m] = 0.0f;
1651  q_filt[m] = 0.0f;
1652  for (j = 0; j <= h_SL; j++) {
1653  g_filt[m] += g_temp[idx1 - j][m] * h_smooth[j];
1654  q_filt[m] += q_temp[idx1 - j][m] * h_smooth[j];
1655  }
1656  }
1657  } else {
1658  g_filt = g_temp[i + h_SL];
1659  q_filt = q_temp[i];
1660  }
1661 
1662  sbr->dsp.hf_g_filt(Y1[i] + kx, X_high + kx, g_filt, m_max,
1664 
1665  if (e != e_a[0] && e != e_a[1]) {
1666  sbr->dsp.hf_apply_noise[indexsine](Y1[i] + kx, sbr->s_m[e],
1667  q_filt, indexnoise,
1668  kx, m_max);
1669  } else {
1670  int idx = indexsine&1;
1671  int A = (1-((indexsine+(kx & 1))&2));
1672  int B = (A^(-idx)) + idx;
1673  float *out = &Y1[i][kx][idx];
1674  float *in = sbr->s_m[e];
1675  for (m = 0; m+1 < m_max; m+=2) {
1676  out[2*m ] += in[m ] * A;
1677  out[2*m+2] += in[m+1] * B;
1678  }
1679  if(m_max&1)
1680  out[2*m ] += in[m ] * A;
1681  }
1682  indexnoise = (indexnoise + m_max) & 0x1ff;
1683  indexsine = (indexsine + 1) & 3;
1684  }
1685  }
1686  ch_data->f_indexnoise = indexnoise;
1687  ch_data->f_indexsine = indexsine;
1688 }
1689 
1691  float* L, float* R)
1692 {
1693  int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
1694  int ch;
1695  int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1696  int err;
1697 
1698  if (!sbr->kx_and_m_pushed) {
1699  sbr->kx[0] = sbr->kx[1];
1700  sbr->m[0] = sbr->m[1];
1701  } else {
1702  sbr->kx_and_m_pushed = 0;
1703  }
1704 
1705  if (sbr->start) {
1706  sbr_dequant(sbr, id_aac);
1707  }
1708  for (ch = 0; ch < nch; ch++) {
1709  /* decode channel */
1710  sbr_qmf_analysis(ac->fdsp, &sbr->mdct_ana, &sbr->dsp, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1711  (float*)sbr->qmf_filter_scratch,
1712  sbr->data[ch].W, sbr->data[ch].Ypos);
1713  sbr->c.sbr_lf_gen(ac, sbr, sbr->X_low,
1714  (const float (*)[32][32][2]) sbr->data[ch].W,
1715  sbr->data[ch].Ypos);
1716  sbr->data[ch].Ypos ^= 1;
1717  if (sbr->start) {
1718  sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
1719  (const float (*)[40][2]) sbr->X_low, sbr->k[0]);
1720  sbr_chirp(sbr, &sbr->data[ch]);
1721  sbr_hf_gen(ac, sbr, sbr->X_high,
1722  (const float (*)[40][2]) sbr->X_low,
1723  (const float (*)[2]) sbr->alpha0,
1724  (const float (*)[2]) sbr->alpha1,
1725  sbr->data[ch].bw_array, sbr->data[ch].t_env,
1726  sbr->data[ch].bs_num_env);
1727 
1728  // hf_adj
1729  err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1730  if (!err) {
1731  sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1732  sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1733  sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
1734  (const float (*)[40][2]) sbr->X_high,
1735  sbr, &sbr->data[ch],
1736  sbr->data[ch].e_a);
1737  }
1738  }
1739 
1740  /* synthesis */
1741  sbr->c.sbr_x_gen(sbr, sbr->X[ch],
1742  (const float (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
1743  (const float (*)[64][2]) sbr->data[ch].Y[ sbr->data[ch].Ypos],
1744  (const float (*)[40][2]) sbr->X_low, ch);
1745  }
1746 
1747  if (ac->oc[1].m4ac.ps == 1) {
1748  if (sbr->ps.start) {
1749  ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1750  } else {
1751  memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1752  }
1753  nch = 2;
1754  }
1755 
1756  sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
1757  L, sbr->X[0], sbr->qmf_filter_scratch,
1760  downsampled);
1761  if (nch == 2)
1762  sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
1763  R, sbr->X[1], sbr->qmf_filter_scratch,
1766  downsampled);
1767 }
1768 
1770 {
1771  c->sbr_lf_gen = sbr_lf_gen;
1773  c->sbr_x_gen = sbr_x_gen;
1775 
1776  if(ARCH_MIPS)
1778 }