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 "fft.h"
34 #include "aacps.h"
35 #include "sbrdsp.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/libm.h"
38 #include "libavutil/avassert.h"
39 
40 #include <stdint.h>
41 #include <float.h>
42 #include <math.h>
43 
44 #define ENVELOPE_ADJUSTMENT_OFFSET 2
45 #define NOISE_FLOOR_OFFSET 6.0f
46 
47 #if ARCH_MIPS
48 #include "mips/aacsbr_mips.h"
49 #endif /* ARCH_MIPS */
50 
51 /**
52  * SBR VLC tables
53  */
54 enum {
65 };
66 
67 /**
68  * bs_frame_class - frame class of current SBR frame (14496-3 sp04 p98)
69  */
70 enum {
75 };
76 
77 enum {
79 };
80 
81 static VLC vlc_sbr[10];
82 static const int8_t vlc_sbr_lav[10] =
83  { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
84 
85 #define SBR_INIT_VLC_STATIC(num, size) \
86  INIT_VLC_STATIC(&vlc_sbr[num], 9, sbr_tmp[num].table_size / sbr_tmp[num].elem_size, \
87  sbr_tmp[num].sbr_bits , 1, 1, \
88  sbr_tmp[num].sbr_codes, sbr_tmp[num].elem_size, sbr_tmp[num].elem_size, \
89  size)
90 
91 #define SBR_VLC_ROW(name) \
92  { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
93 
95 
97 {
98  int n;
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 
127  for (n = 1; n < 320; n++)
128  sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n];
129  sbr_qmf_window_us[384] = -sbr_qmf_window_us[384];
130  sbr_qmf_window_us[512] = -sbr_qmf_window_us[512];
131 
132  for (n = 0; n < 320; n++)
133  sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n];
134 
135  ff_ps_init();
136 }
137 
138 /** Places SBR in pure upsampling mode. */
140  sbr->start = 0;
141  // Init defults used in pure upsampling mode
142  sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
143  sbr->m[1] = 0;
144  // Reset values for first SBR header
145  sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
146  memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
147 }
148 
150 {
151  if(sbr->mdct.mdct_bits)
152  return;
153  sbr->kx[0] = sbr->kx[1];
154  sbr_turnoff(sbr);
157  /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
158  * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
159  * and scale back down at synthesis. */
160  ff_mdct_init(&sbr->mdct, 7, 1, 1.0 / (64 * 32768.0));
161  ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0 * 32768.0);
162  ff_ps_ctx_init(&sbr->ps);
163  ff_sbrdsp_init(&sbr->dsp);
164  aacsbr_func_ptr_init(&sbr->c);
165 }
166 
168 {
169  ff_mdct_end(&sbr->mdct);
170  ff_mdct_end(&sbr->mdct_ana);
171 }
172 
173 static int qsort_comparison_function_int16(const void *a, const void *b)
174 {
175  return *(const int16_t *)a - *(const int16_t *)b;
176 }
177 
178 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
179 {
180  int i;
181  for (i = 0; i <= last_el; i++)
182  if (table[i] == needle)
183  return 1;
184  return 0;
185 }
186 
187 /// Limiter Frequency Band Table (14496-3 sp04 p198)
189 {
190  int k;
191  if (sbr->bs_limiter_bands > 0) {
192  static const float bands_warped[3] = { 1.32715174233856803909f, //2^(0.49/1.2)
193  1.18509277094158210129f, //2^(0.49/2)
194  1.11987160404675912501f }; //2^(0.49/3)
195  const float lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
196  int16_t patch_borders[7];
197  uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
198 
199  patch_borders[0] = sbr->kx[1];
200  for (k = 1; k <= sbr->num_patches; k++)
201  patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
202 
203  memcpy(sbr->f_tablelim, sbr->f_tablelow,
204  (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
205  if (sbr->num_patches > 1)
206  memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
207  (sbr->num_patches - 1) * sizeof(patch_borders[0]));
208 
209  qsort(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
210  sizeof(sbr->f_tablelim[0]),
212 
213  sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
214  while (out < sbr->f_tablelim + sbr->n_lim) {
215  if (*in >= *out * lim_bands_per_octave_warped) {
216  *++out = *in++;
217  } else if (*in == *out ||
218  !in_table_int16(patch_borders, sbr->num_patches, *in)) {
219  in++;
220  sbr->n_lim--;
221  } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
222  *out = *in++;
223  sbr->n_lim--;
224  } else {
225  *++out = *in++;
226  }
227  }
228  } else {
229  sbr->f_tablelim[0] = sbr->f_tablelow[0];
230  sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
231  sbr->n_lim = 1;
232  }
233 }
234 
236 {
237  unsigned int cnt = get_bits_count(gb);
238  uint8_t bs_header_extra_1;
239  uint8_t bs_header_extra_2;
240  int old_bs_limiter_bands = sbr->bs_limiter_bands;
241  SpectrumParameters old_spectrum_params;
242 
243  sbr->start = 1;
244 
245  // Save last spectrum parameters variables to compare to new ones
246  memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
247 
248  sbr->bs_amp_res_header = get_bits1(gb);
249  sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
250  sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
251  sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
252  skip_bits(gb, 2); // bs_reserved
253 
254  bs_header_extra_1 = get_bits1(gb);
255  bs_header_extra_2 = get_bits1(gb);
256 
257  if (bs_header_extra_1) {
258  sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
261  } else {
265  }
266 
267  // Check if spectrum parameters changed
268  if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
269  sbr->reset = 1;
270 
271  if (bs_header_extra_2) {
272  sbr->bs_limiter_bands = get_bits(gb, 2);
273  sbr->bs_limiter_gains = get_bits(gb, 2);
274  sbr->bs_interpol_freq = get_bits1(gb);
275  sbr->bs_smoothing_mode = get_bits1(gb);
276  } else {
277  sbr->bs_limiter_bands = 2;
278  sbr->bs_limiter_gains = 2;
279  sbr->bs_interpol_freq = 1;
280  sbr->bs_smoothing_mode = 1;
281  }
282 
283  if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
284  sbr_make_f_tablelim(sbr);
285 
286  return get_bits_count(gb) - cnt;
287 }
288 
289 static int array_min_int16(const int16_t *array, int nel)
290 {
291  int i, min = array[0];
292  for (i = 1; i < nel; i++)
293  min = FFMIN(array[i], min);
294  return min;
295 }
296 
297 static void make_bands(int16_t* bands, int start, int stop, int num_bands)
298 {
299  int k, previous, present;
300  float base, prod;
301 
302  base = powf((float)stop / start, 1.0f / num_bands);
303  prod = start;
304  previous = start;
305 
306  for (k = 0; k < num_bands-1; k++) {
307  prod *= base;
308  present = lrintf(prod);
309  bands[k] = present - previous;
310  previous = present;
311  }
312  bands[num_bands-1] = stop - previous;
313 }
314 
315 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
316 {
317  // Requirements (14496-3 sp04 p205)
318  if (n_master <= 0) {
319  av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
320  return -1;
321  }
322  if (bs_xover_band >= n_master) {
323  av_log(avctx, AV_LOG_ERROR,
324  "Invalid bitstream, crossover band index beyond array bounds: %d\n",
325  bs_xover_band);
326  return -1;
327  }
328  return 0;
329 }
330 
331 /// Master Frequency Band Table (14496-3 sp04 p194)
334 {
335  unsigned int temp, max_qmf_subbands;
336  unsigned int start_min, stop_min;
337  int k;
338  const int8_t *sbr_offset_ptr;
339  int16_t stop_dk[13];
340 
341  if (sbr->sample_rate < 32000) {
342  temp = 3000;
343  } else if (sbr->sample_rate < 64000) {
344  temp = 4000;
345  } else
346  temp = 5000;
347 
348  switch (sbr->sample_rate) {
349  case 16000:
350  sbr_offset_ptr = sbr_offset[0];
351  break;
352  case 22050:
353  sbr_offset_ptr = sbr_offset[1];
354  break;
355  case 24000:
356  sbr_offset_ptr = sbr_offset[2];
357  break;
358  case 32000:
359  sbr_offset_ptr = sbr_offset[3];
360  break;
361  case 44100: case 48000: case 64000:
362  sbr_offset_ptr = sbr_offset[4];
363  break;
364  case 88200: case 96000: case 128000: case 176400: case 192000:
365  sbr_offset_ptr = sbr_offset[5];
366  break;
367  default:
369  "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
370  return -1;
371  }
372 
373  start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
374  stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
375 
376  sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
377 
378  if (spectrum->bs_stop_freq < 14) {
379  sbr->k[2] = stop_min;
380  make_bands(stop_dk, stop_min, 64, 13);
381  qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16);
382  for (k = 0; k < spectrum->bs_stop_freq; k++)
383  sbr->k[2] += stop_dk[k];
384  } else if (spectrum->bs_stop_freq == 14) {
385  sbr->k[2] = 2*sbr->k[0];
386  } else if (spectrum->bs_stop_freq == 15) {
387  sbr->k[2] = 3*sbr->k[0];
388  } else {
390  "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
391  return -1;
392  }
393  sbr->k[2] = FFMIN(64, sbr->k[2]);
394 
395  // Requirements (14496-3 sp04 p205)
396  if (sbr->sample_rate <= 32000) {
397  max_qmf_subbands = 48;
398  } else if (sbr->sample_rate == 44100) {
399  max_qmf_subbands = 35;
400  } else if (sbr->sample_rate >= 48000)
401  max_qmf_subbands = 32;
402  else
403  av_assert0(0);
404 
405  if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
407  "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
408  return -1;
409  }
410 
411  if (!spectrum->bs_freq_scale) {
412  int dk, k2diff;
413 
414  dk = spectrum->bs_alter_scale + 1;
415  sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
417  return -1;
418 
419  for (k = 1; k <= sbr->n_master; k++)
420  sbr->f_master[k] = dk;
421 
422  k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
423  if (k2diff < 0) {
424  sbr->f_master[1]--;
425  sbr->f_master[2]-= (k2diff < -1);
426  } else if (k2diff) {
427  sbr->f_master[sbr->n_master]++;
428  }
429 
430  sbr->f_master[0] = sbr->k[0];
431  for (k = 1; k <= sbr->n_master; k++)
432  sbr->f_master[k] += sbr->f_master[k - 1];
433 
434  } else {
435  int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3}
436  int two_regions, num_bands_0;
437  int vdk0_max, vdk1_min;
438  int16_t vk0[49];
439 
440  if (49 * sbr->k[2] > 110 * sbr->k[0]) {
441  two_regions = 1;
442  sbr->k[1] = 2 * sbr->k[0];
443  } else {
444  two_regions = 0;
445  sbr->k[1] = sbr->k[2];
446  }
447 
448  num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
449 
450  if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
451  av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
452  return -1;
453  }
454 
455  vk0[0] = 0;
456 
457  make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
458 
459  qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16);
460  vdk0_max = vk0[num_bands_0];
461 
462  vk0[0] = sbr->k[0];
463  for (k = 1; k <= num_bands_0; k++) {
464  if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
465  av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
466  return -1;
467  }
468  vk0[k] += vk0[k-1];
469  }
470 
471  if (two_regions) {
472  int16_t vk1[49];
473  float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
474  : 1.0f; // bs_alter_scale = {0,1}
475  int num_bands_1 = lrintf(half_bands * invwarp *
476  log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
477 
478  make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
479 
480  vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
481 
482  if (vdk1_min < vdk0_max) {
483  int change;
484  qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
485  change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
486  vk1[1] += change;
487  vk1[num_bands_1] -= change;
488  }
489 
490  qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
491 
492  vk1[0] = sbr->k[1];
493  for (k = 1; k <= num_bands_1; k++) {
494  if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
495  av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
496  return -1;
497  }
498  vk1[k] += vk1[k-1];
499  }
500 
501  sbr->n_master = num_bands_0 + num_bands_1;
503  return -1;
504  memcpy(&sbr->f_master[0], vk0,
505  (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
506  memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
507  num_bands_1 * sizeof(sbr->f_master[0]));
508 
509  } else {
510  sbr->n_master = num_bands_0;
512  return -1;
513  memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
514  }
515  }
516 
517  return 0;
518 }
519 
520 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
522 {
523  int i, k, sb = 0;
524  int msb = sbr->k[0];
525  int usb = sbr->kx[1];
526  int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
527 
528  sbr->num_patches = 0;
529 
530  if (goal_sb < sbr->kx[1] + sbr->m[1]) {
531  for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
532  } else
533  k = sbr->n_master;
534 
535  do {
536  int odd = 0;
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 && sbr->patch_num_subbands[sbr->num_patches-1] < 3)
566  sbr->num_patches--;
567 
568  return 0;
569 }
570 
571 /// Derived Frequency Band Tables (14496-3 sp04 p197)
573 {
574  int k, temp;
575 
576  sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
577  sbr->n[0] = (sbr->n[1] + 1) >> 1;
578 
579  memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
580  (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
581  sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
582  sbr->kx[1] = sbr->f_tablehigh[0];
583 
584  // Requirements (14496-3 sp04 p205)
585  if (sbr->kx[1] + sbr->m[1] > 64) {
587  "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
588  return -1;
589  }
590  if (sbr->kx[1] > 32) {
591  av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
592  return -1;
593  }
594 
595  sbr->f_tablelow[0] = sbr->f_tablehigh[0];
596  temp = sbr->n[1] & 1;
597  for (k = 1; k <= sbr->n[0]; k++)
598  sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
599 
601  log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
602  if (sbr->n_q > 5) {
603  av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
604  return -1;
605  }
606 
607  sbr->f_tablenoise[0] = sbr->f_tablelow[0];
608  temp = 0;
609  for (k = 1; k <= sbr->n_q; k++) {
610  temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
611  sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
612  }
613 
614  if (sbr_hf_calc_npatches(ac, sbr) < 0)
615  return -1;
616 
617  sbr_make_f_tablelim(sbr);
618 
619  sbr->data[0].f_indexnoise = 0;
620  sbr->data[1].f_indexnoise = 0;
621 
622  return 0;
623 }
624 
626  int elements)
627 {
628  int i;
629  for (i = 0; i < elements; i++) {
630  vec[i] = get_bits1(gb);
631  }
632 }
633 
634 /** ceil(log2(index+1)) */
635 static const int8_t ceil_log2[] = {
636  0, 1, 2, 2, 3, 3,
637 };
638 
640  GetBitContext *gb, SBRData *ch_data)
641 {
642  int i;
643  unsigned bs_pointer = 0;
644  // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
645  int abs_bord_trail = 16;
646  int num_rel_lead, num_rel_trail;
647  unsigned bs_num_env_old = ch_data->bs_num_env;
648 
649  ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
650  ch_data->bs_amp_res = sbr->bs_amp_res_header;
651  ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
652 
653  switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
654  case FIXFIX:
655  ch_data->bs_num_env = 1 << get_bits(gb, 2);
656  num_rel_lead = ch_data->bs_num_env - 1;
657  if (ch_data->bs_num_env == 1)
658  ch_data->bs_amp_res = 0;
659 
660  if (ch_data->bs_num_env > 4) {
662  "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
663  ch_data->bs_num_env);
664  return -1;
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  ch_data->bs_num_env = num_rel_lead + num_rel_trail + 1;
714 
715  if (ch_data->bs_num_env > 5) {
717  "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
718  ch_data->bs_num_env);
719  return -1;
720  }
721 
722  ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
723 
724  for (i = 0; i < num_rel_lead; i++)
725  ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
726  for (i = 0; i < num_rel_trail; i++)
727  ch_data->t_env[ch_data->bs_num_env - 1 - i] =
728  ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
729 
730  bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
731 
732  get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
733  break;
734  }
735 
736  if (bs_pointer > ch_data->bs_num_env + 1) {
738  "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
739  bs_pointer);
740  return -1;
741  }
742 
743  for (i = 1; i <= ch_data->bs_num_env; i++) {
744  if (ch_data->t_env[i-1] > ch_data->t_env[i]) {
745  av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n");
746  return -1;
747  }
748  }
749 
750  ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
751 
752  ch_data->t_q[0] = ch_data->t_env[0];
753  ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
754  if (ch_data->bs_num_noise > 1) {
755  unsigned int idx;
756  if (ch_data->bs_frame_class == FIXFIX) {
757  idx = ch_data->bs_num_env >> 1;
758  } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
759  idx = ch_data->bs_num_env - FFMAX((int)bs_pointer - 1, 1);
760  } else { // VARFIX
761  if (!bs_pointer)
762  idx = 1;
763  else if (bs_pointer == 1)
764  idx = ch_data->bs_num_env - 1;
765  else // bs_pointer > 1
766  idx = bs_pointer - 1;
767  }
768  ch_data->t_q[1] = ch_data->t_env[idx];
769  }
770 
771  ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
772  ch_data->e_a[1] = -1;
773  if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
774  ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
775  } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
776  ch_data->e_a[1] = bs_pointer - 1;
777 
778  return 0;
779 }
780 
781 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
782  //These variables are saved from the previous frame rather than copied
783  dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
784  dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
785  dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
786 
787  //These variables are read from the bitstream and therefore copied
788  memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
789  memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
790  memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
791  dst->bs_num_env = src->bs_num_env;
792  dst->bs_amp_res = src->bs_amp_res;
793  dst->bs_num_noise = src->bs_num_noise;
794  dst->bs_frame_class = src->bs_frame_class;
795  dst->e_a[1] = src->e_a[1];
796 }
797 
798 /// Read how the envelope and noise floor data is delta coded
800  SBRData *ch_data)
801 {
802  get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
803  get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
804 }
805 
806 /// Read inverse filtering data
808  SBRData *ch_data)
809 {
810  int i;
811 
812  memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
813  for (i = 0; i < sbr->n_q; i++)
814  ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
815 }
816 
818  SBRData *ch_data, int ch)
819 {
820  int bits;
821  int i, j, k;
822  VLC_TYPE (*t_huff)[2], (*f_huff)[2];
823  int t_lav, f_lav;
824  const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
825  const int odd = sbr->n[1] & 1;
826 
827  if (sbr->bs_coupling && ch) {
828  if (ch_data->bs_amp_res) {
829  bits = 5;
830  t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
832  f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
834  } else {
835  bits = 6;
836  t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
838  f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
840  }
841  } else {
842  if (ch_data->bs_amp_res) {
843  bits = 6;
844  t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
846  f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
848  } else {
849  bits = 7;
850  t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
852  f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
854  }
855  }
856 
857  for (i = 0; i < ch_data->bs_num_env; i++) {
858  if (ch_data->bs_df_env[i]) {
859  // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
860  if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
861  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
862  ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
863  } else if (ch_data->bs_freq_res[i + 1]) {
864  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
865  k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
866  ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
867  }
868  } else {
869  for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
870  k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
871  ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
872  }
873  }
874  } else {
875  ch_data->env_facs[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
876  for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
877  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);
878  }
879  }
880 
881  //assign 0th elements of env_facs from last elements
882  memcpy(ch_data->env_facs[0], ch_data->env_facs[ch_data->bs_num_env],
883  sizeof(ch_data->env_facs[0]));
884 }
885 
887  SBRData *ch_data, int ch)
888 {
889  int i, j;
890  VLC_TYPE (*t_huff)[2], (*f_huff)[2];
891  int t_lav, f_lav;
892  int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
893 
894  if (sbr->bs_coupling && ch) {
895  t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
897  f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
899  } else {
900  t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
902  f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
904  }
905 
906  for (i = 0; i < ch_data->bs_num_noise; i++) {
907  if (ch_data->bs_df_noise[i]) {
908  for (j = 0; j < sbr->n_q; j++)
909  ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
910  } else {
911  ch_data->noise_facs[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
912  for (j = 1; j < sbr->n_q; j++)
913  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);
914  }
915  }
916 
917  //assign 0th elements of noise_facs from last elements
918  memcpy(ch_data->noise_facs[0], ch_data->noise_facs[ch_data->bs_num_noise],
919  sizeof(ch_data->noise_facs[0]));
920 }
921 
923  GetBitContext *gb,
924  int bs_extension_id, int *num_bits_left)
925 {
926  switch (bs_extension_id) {
927  case EXTENSION_ID_PS:
928  if (!ac->oc[1].m4ac.ps) {
929  av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
930  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
931  *num_bits_left = 0;
932  } else {
933 #if 1
934  *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left);
935 #else
936  avpriv_report_missing_feature(ac->avctx, "Parametric Stereo");
937  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
938  *num_bits_left = 0;
939 #endif
940  }
941  break;
942  default:
943  // some files contain 0-padding
944  if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
945  avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
946  skip_bits_long(gb, *num_bits_left); // bs_fill_bits
947  *num_bits_left = 0;
948  break;
949  }
950 }
951 
954  GetBitContext *gb)
955 {
956  if (get_bits1(gb)) // bs_data_extra
957  skip_bits(gb, 4); // bs_reserved
958 
959  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
960  return -1;
961  read_sbr_dtdf(sbr, gb, &sbr->data[0]);
962  read_sbr_invf(sbr, gb, &sbr->data[0]);
963  read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
964  read_sbr_noise(sbr, gb, &sbr->data[0], 0);
965 
966  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
967  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
968 
969  return 0;
970 }
971 
974  GetBitContext *gb)
975 {
976  if (get_bits1(gb)) // bs_data_extra
977  skip_bits(gb, 8); // bs_reserved
978 
979  if ((sbr->bs_coupling = get_bits1(gb))) {
980  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
981  return -1;
982  copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
983  read_sbr_dtdf(sbr, gb, &sbr->data[0]);
984  read_sbr_dtdf(sbr, gb, &sbr->data[1]);
985  read_sbr_invf(sbr, gb, &sbr->data[0]);
986  memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
987  memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
988  read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
989  read_sbr_noise(sbr, gb, &sbr->data[0], 0);
990  read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
991  read_sbr_noise(sbr, gb, &sbr->data[1], 1);
992  } else {
993  if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
994  read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
995  return -1;
996  read_sbr_dtdf(sbr, gb, &sbr->data[0]);
997  read_sbr_dtdf(sbr, gb, &sbr->data[1]);
998  read_sbr_invf(sbr, gb, &sbr->data[0]);
999  read_sbr_invf(sbr, gb, &sbr->data[1]);
1000  read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
1001  read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
1002  read_sbr_noise(sbr, gb, &sbr->data[0], 0);
1003  read_sbr_noise(sbr, gb, &sbr->data[1], 1);
1004  }
1005 
1006  if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1007  get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1008  if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1009  get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1010 
1011  return 0;
1012 }
1013 
1015  GetBitContext *gb, int id_aac)
1016 {
1017  unsigned int cnt = get_bits_count(gb);
1018 
1019  if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
1020  if (read_sbr_single_channel_element(ac, sbr, gb)) {
1021  sbr_turnoff(sbr);
1022  return get_bits_count(gb) - cnt;
1023  }
1024  } else if (id_aac == TYPE_CPE) {
1025  if (read_sbr_channel_pair_element(ac, sbr, gb)) {
1026  sbr_turnoff(sbr);
1027  return get_bits_count(gb) - cnt;
1028  }
1029  } else {
1030  av_log(ac->avctx, AV_LOG_ERROR,
1031  "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1032  sbr_turnoff(sbr);
1033  return get_bits_count(gb) - cnt;
1034  }
1035  if (get_bits1(gb)) { // bs_extended_data
1036  int num_bits_left = get_bits(gb, 4); // bs_extension_size
1037  if (num_bits_left == 15)
1038  num_bits_left += get_bits(gb, 8); // bs_esc_count
1039 
1040  num_bits_left <<= 3;
1041  while (num_bits_left > 7) {
1042  num_bits_left -= 2;
1043  read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
1044  }
1045  if (num_bits_left < 0) {
1046  av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
1047  }
1048  if (num_bits_left > 0)
1049  skip_bits(gb, num_bits_left);
1050  }
1051 
1052  return get_bits_count(gb) - cnt;
1053 }
1054 
1056 {
1057  int err;
1058  err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
1059  if (err >= 0)
1060  err = sbr_make_f_derived(ac, sbr);
1061  if (err < 0) {
1062  av_log(ac->avctx, AV_LOG_ERROR,
1063  "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1064  sbr_turnoff(sbr);
1065  }
1066 }
1067 
1068 /**
1069  * Decode Spectral Band Replication extension data; reference: table 4.55.
1070  *
1071  * @param crc flag indicating the presence of CRC checksum
1072  * @param cnt length of TYPE_FIL syntactic element in bytes
1073  *
1074  * @return Returns number of bytes consumed from the TYPE_FIL element.
1075  */
1077  GetBitContext *gb_host, int crc, int cnt, int id_aac)
1078 {
1079  unsigned int num_sbr_bits = 0, num_align_bits;
1080  unsigned bytes_read;
1081  GetBitContext gbc = *gb_host, *gb = &gbc;
1082  skip_bits_long(gb_host, cnt*8 - 4);
1083 
1084  sbr->reset = 0;
1085 
1086  if (!sbr->sample_rate)
1087  sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
1088  if (!ac->oc[1].m4ac.ext_sample_rate)
1089  ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
1090 
1091  if (crc) {
1092  skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
1093  num_sbr_bits += 10;
1094  }
1095 
1096  //Save some state from the previous frame.
1097  sbr->kx[0] = sbr->kx[1];
1098  sbr->m[0] = sbr->m[1];
1099  sbr->kx_and_m_pushed = 1;
1100 
1101  num_sbr_bits++;
1102  if (get_bits1(gb)) // bs_header_flag
1103  num_sbr_bits += read_sbr_header(sbr, gb);
1104 
1105  if (sbr->reset)
1106  sbr_reset(ac, sbr);
1107 
1108  if (sbr->start)
1109  num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
1110 
1111  num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1112  bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1113 
1114  if (bytes_read > cnt) {
1115  av_log(ac->avctx, AV_LOG_ERROR,
1116  "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1117  }
1118  return cnt;
1119 }
1120 
1121 /// Dequantization and stereo decoding (14496-3 sp04 p203)
1122 static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
1123 {
1124  int k, e;
1125  int ch;
1126 
1127  if (id_aac == TYPE_CPE && sbr->bs_coupling) {
1128  float alpha = sbr->data[0].bs_amp_res ? 1.0f : 0.5f;
1129  float pan_offset = sbr->data[0].bs_amp_res ? 12.0f : 24.0f;
1130  for (e = 1; e <= sbr->data[0].bs_num_env; e++) {
1131  for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
1132  float temp1 = exp2f(sbr->data[0].env_facs[e][k] * alpha + 7.0f);
1133  float temp2 = exp2f((pan_offset - sbr->data[1].env_facs[e][k]) * alpha);
1134  float fac;
1135  if (temp1 > 1E20) {
1136  av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
1137  temp1 = 1;
1138  }
1139  fac = temp1 / (1.0f + temp2);
1140  sbr->data[0].env_facs[e][k] = fac;
1141  sbr->data[1].env_facs[e][k] = fac * temp2;
1142  }
1143  }
1144  for (e = 1; e <= sbr->data[0].bs_num_noise; e++) {
1145  for (k = 0; k < sbr->n_q; k++) {
1146  float temp1 = exp2f(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs[e][k] + 1);
1147  float temp2 = exp2f(12 - sbr->data[1].noise_facs[e][k]);
1148  float fac;
1149  if (temp1 > 1E20) {
1150  av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
1151  temp1 = 1;
1152  }
1153  fac = temp1 / (1.0f + temp2);
1154  sbr->data[0].noise_facs[e][k] = fac;
1155  sbr->data[1].noise_facs[e][k] = fac * temp2;
1156  }
1157  }
1158  } else { // SCE or one non-coupled CPE
1159  for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
1160  float alpha = sbr->data[ch].bs_amp_res ? 1.0f : 0.5f;
1161  for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
1162  for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++){
1163  sbr->data[ch].env_facs[e][k] =
1164  exp2f(alpha * sbr->data[ch].env_facs[e][k] + 6.0f);
1165  if (sbr->data[ch].env_facs[e][k] > 1E20) {
1166  av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
1167  sbr->data[ch].env_facs[e][k] = 1;
1168  }
1169  }
1170 
1171  for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
1172  for (k = 0; k < sbr->n_q; k++)
1173  sbr->data[ch].noise_facs[e][k] =
1174  exp2f(NOISE_FLOOR_OFFSET - sbr->data[ch].noise_facs[e][k]);
1175  }
1176  }
1177 }
1178 
1179 /**
1180  * Analysis QMF Bank (14496-3 sp04 p206)
1181  *
1182  * @param x pointer to the beginning of the first sample window
1183  * @param W array of complex-valued samples split into subbands
1184  */
1185 #ifndef sbr_qmf_analysis
1187  SBRDSPContext *sbrdsp, const float *in, float *x,
1188  float z[320], float W[2][32][32][2], int buf_idx)
1189 {
1190  int i;
1191  memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1192  memcpy(x+288, in, 1024*sizeof(x[0]));
1193  for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1194  // are not supported
1195  dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1196  sbrdsp->sum64x5(z);
1197  sbrdsp->qmf_pre_shuffle(z);
1198  mdct->imdct_half(mdct, z, z+64);
1199  sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
1200  x += 32;
1201  }
1202 }
1203 #endif
1204 
1205 /**
1206  * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1207  * (14496-3 sp04 p206)
1208  */
1209 #ifndef sbr_qmf_synthesis
1210 static void sbr_qmf_synthesis(FFTContext *mdct,
1211  SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
1212  float *out, float X[2][38][64],
1213  float mdct_buf[2][64],
1214  float *v0, int *v_off, const unsigned int div)
1215 {
1216  int i, n;
1217  const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
1218  const int step = 128 >> div;
1219  float *v;
1220  for (i = 0; i < 32; i++) {
1221  if (*v_off < step) {
1222  int saved_samples = (1280 - 128) >> div;
1223  memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(float));
1224  *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
1225  } else {
1226  *v_off -= step;
1227  }
1228  v = v0 + *v_off;
1229  if (div) {
1230  for (n = 0; n < 32; n++) {
1231  X[0][i][ n] = -X[0][i][n];
1232  X[0][i][32+n] = X[1][i][31-n];
1233  }
1234  mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1235  sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
1236  } else {
1237  sbrdsp->neg_odd_64(X[1][i]);
1238  mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1239  mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
1240  sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
1241  }
1242  dsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1243  dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1244  dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1245  dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1246  dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1247  dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1248  dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1249  dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1250  dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1251  dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1252  out += 64 >> div;
1253  }
1254 }
1255 #endif
1256 
1257 /** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering
1258  * (14496-3 sp04 p214)
1259  * Warning: This routine does not seem numerically stable.
1260  */
1262  float (*alpha0)[2], float (*alpha1)[2],
1263  const float X_low[32][40][2], int k0)
1264 {
1265  int k;
1266  for (k = 0; k < k0; k++) {
1267  LOCAL_ALIGNED_16(float, phi, [3], [2][2]);
1268  float dk;
1269 
1270  dsp->autocorrelate(X_low[k], phi);
1271 
1272  dk = phi[2][1][0] * phi[1][0][0] -
1273  (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
1274 
1275  if (!dk) {
1276  alpha1[k][0] = 0;
1277  alpha1[k][1] = 0;
1278  } else {
1279  float temp_real, temp_im;
1280  temp_real = phi[0][0][0] * phi[1][1][0] -
1281  phi[0][0][1] * phi[1][1][1] -
1282  phi[0][1][0] * phi[1][0][0];
1283  temp_im = phi[0][0][0] * phi[1][1][1] +
1284  phi[0][0][1] * phi[1][1][0] -
1285  phi[0][1][1] * phi[1][0][0];
1286 
1287  alpha1[k][0] = temp_real / dk;
1288  alpha1[k][1] = temp_im / dk;
1289  }
1290 
1291  if (!phi[1][0][0]) {
1292  alpha0[k][0] = 0;
1293  alpha0[k][1] = 0;
1294  } else {
1295  float temp_real, temp_im;
1296  temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
1297  alpha1[k][1] * phi[1][1][1];
1298  temp_im = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
1299  alpha1[k][0] * phi[1][1][1];
1300 
1301  alpha0[k][0] = -temp_real / phi[1][0][0];
1302  alpha0[k][1] = -temp_im / phi[1][0][0];
1303  }
1304 
1305  if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
1306  alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
1307  alpha1[k][0] = 0;
1308  alpha1[k][1] = 0;
1309  alpha0[k][0] = 0;
1310  alpha0[k][1] = 0;
1311  }
1312  }
1313 }
1314 
1315 /// Chirp Factors (14496-3 sp04 p214)
1316 static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
1317 {
1318  int i;
1319  float new_bw;
1320  static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f };
1321 
1322  for (i = 0; i < sbr->n_q; i++) {
1323  if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1) {
1324  new_bw = 0.6f;
1325  } else
1326  new_bw = bw_tab[ch_data->bs_invf_mode[0][i]];
1327 
1328  if (new_bw < ch_data->bw_array[i]) {
1329  new_bw = 0.75f * new_bw + 0.25f * ch_data->bw_array[i];
1330  } else
1331  new_bw = 0.90625f * new_bw + 0.09375f * ch_data->bw_array[i];
1332  ch_data->bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw;
1333  }
1334 }
1335 
1336 /// Generate the subband filtered lowband
1338  float X_low[32][40][2], const float W[2][32][32][2],
1339  int buf_idx)
1340 {
1341  int i, k;
1342  const int t_HFGen = 8;
1343  const int i_f = 32;
1344  memset(X_low, 0, 32*sizeof(*X_low));
1345  for (k = 0; k < sbr->kx[1]; k++) {
1346  for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1347  X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1348  X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1349  }
1350  }
1351  buf_idx = 1-buf_idx;
1352  for (k = 0; k < sbr->kx[0]; k++) {
1353  for (i = 0; i < t_HFGen; i++) {
1354  X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1355  X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1356  }
1357  }
1358  return 0;
1359 }
1360 
1361 /// High Frequency Generator (14496-3 sp04 p215)
1363  float X_high[64][40][2], const float X_low[32][40][2],
1364  const float (*alpha0)[2], const float (*alpha1)[2],
1365  const float bw_array[5], const uint8_t *t_env,
1366  int bs_num_env)
1367 {
1368  int j, x;
1369  int g = 0;
1370  int k = sbr->kx[1];
1371  for (j = 0; j < sbr->num_patches; j++) {
1372  for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
1373  const int p = sbr->patch_start_subband[j] + x;
1374  while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
1375  g++;
1376  g--;
1377 
1378  if (g < 0) {
1379  av_log(ac->avctx, AV_LOG_ERROR,
1380  "ERROR : no subband found for frequency %d\n", k);
1381  return -1;
1382  }
1383 
1384  sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
1385  X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
1386  alpha0[p], alpha1[p], bw_array[g],
1387  2 * t_env[0], 2 * t_env[bs_num_env]);
1388  }
1389  }
1390  if (k < sbr->m[1] + sbr->kx[1])
1391  memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
1392 
1393  return 0;
1394 }
1395 
1396 /// Generate the subband filtered lowband
1397 static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64],
1398  const float Y0[38][64][2], const float Y1[38][64][2],
1399  const float X_low[32][40][2], int ch)
1400 {
1401  int k, i;
1402  const int i_f = 32;
1403  const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
1404  memset(X, 0, 2*sizeof(*X));
1405  for (k = 0; k < sbr->kx[0]; k++) {
1406  for (i = 0; i < i_Temp; i++) {
1407  X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1408  X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1409  }
1410  }
1411  for (; k < sbr->kx[0] + sbr->m[0]; k++) {
1412  for (i = 0; i < i_Temp; i++) {
1413  X[0][i][k] = Y0[i + i_f][k][0];
1414  X[1][i][k] = Y0[i + i_f][k][1];
1415  }
1416  }
1417 
1418  for (k = 0; k < sbr->kx[1]; k++) {
1419  for (i = i_Temp; i < 38; i++) {
1420  X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1421  X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1422  }
1423  }
1424  for (; k < sbr->kx[1] + sbr->m[1]; k++) {
1425  for (i = i_Temp; i < i_f; i++) {
1426  X[0][i][k] = Y1[i][k][0];
1427  X[1][i][k] = Y1[i][k][1];
1428  }
1429  }
1430  return 0;
1431 }
1432 
1433 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1434  * (14496-3 sp04 p217)
1435  */
1437  SBRData *ch_data, int e_a[2])
1438 {
1439  int e, i, m;
1440 
1441  memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
1442  for (e = 0; e < ch_data->bs_num_env; e++) {
1443  const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
1444  uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1445  int k;
1446 
1447  if (sbr->kx[1] != table[0]) {
1448  av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
1449  "Derived frequency tables were not regenerated.\n");
1450  sbr_turnoff(sbr);
1451  return AVERROR_BUG;
1452  }
1453  for (i = 0; i < ilim; i++)
1454  for (m = table[i]; m < table[i + 1]; m++)
1455  sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
1456 
1457  // ch_data->bs_num_noise > 1 => 2 noise floors
1458  k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
1459  for (i = 0; i < sbr->n_q; i++)
1460  for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
1461  sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
1462 
1463  for (i = 0; i < sbr->n[1]; i++) {
1464  if (ch_data->bs_add_harmonic_flag) {
1465  const unsigned int m_midpoint =
1466  (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
1467 
1468  ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
1469  (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
1470  }
1471  }
1472 
1473  for (i = 0; i < ilim; i++) {
1474  int additional_sinusoid_present = 0;
1475  for (m = table[i]; m < table[i + 1]; m++) {
1476  if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
1477  additional_sinusoid_present = 1;
1478  break;
1479  }
1480  }
1481  memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
1482  (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
1483  }
1484  }
1485 
1486  memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
1487  return 0;
1488 }
1489 
1490 /// Estimation of current envelope (14496-3 sp04 p218)
1491 static void sbr_env_estimate(float (*e_curr)[48], float X_high[64][40][2],
1492  SpectralBandReplication *sbr, SBRData *ch_data)
1493 {
1494  int e, m;
1495  int kx1 = sbr->kx[1];
1496 
1497  if (sbr->bs_interpol_freq) {
1498  for (e = 0; e < ch_data->bs_num_env; e++) {
1499  const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1500  int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1501  int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1502 
1503  for (m = 0; m < sbr->m[1]; m++) {
1504  float sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
1505  e_curr[e][m] = sum * recip_env_size;
1506  }
1507  }
1508  } else {
1509  int k, p;
1510 
1511  for (e = 0; e < ch_data->bs_num_env; e++) {
1512  const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1513  int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1514  int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1515  const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1516 
1517  for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
1518  float sum = 0.0f;
1519  const int den = env_size * (table[p + 1] - table[p]);
1520 
1521  for (k = table[p]; k < table[p + 1]; k++) {
1522  sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
1523  }
1524  sum /= den;
1525  for (k = table[p]; k < table[p + 1]; k++) {
1526  e_curr[e][k - kx1] = sum;
1527  }
1528  }
1529  }
1530  }
1531 }
1532 
1533 /**
1534  * Calculation of levels of additional HF signal components (14496-3 sp04 p219)
1535  * and Calculation of gain (14496-3 sp04 p219)
1536  */
1538  SBRData *ch_data, const int e_a[2])
1539 {
1540  int e, k, m;
1541  // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off)
1542  static const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
1543 
1544  for (e = 0; e < ch_data->bs_num_env; e++) {
1545  int delta = !((e == e_a[1]) || (e == e_a[0]));
1546  for (k = 0; k < sbr->n_lim; k++) {
1547  float gain_boost, gain_max;
1548  float sum[2] = { 0.0f, 0.0f };
1549  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1550  const float temp = sbr->e_origmapped[e][m] / (1.0f + sbr->q_mapped[e][m]);
1551  sbr->q_m[e][m] = sqrtf(temp * sbr->q_mapped[e][m]);
1552  sbr->s_m[e][m] = sqrtf(temp * ch_data->s_indexmapped[e + 1][m]);
1553  if (!sbr->s_mapped[e][m]) {
1554  sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] /
1555  ((1.0f + sbr->e_curr[e][m]) *
1556  (1.0f + sbr->q_mapped[e][m] * delta)));
1557  } else {
1558  sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] * sbr->q_mapped[e][m] /
1559  ((1.0f + sbr->e_curr[e][m]) *
1560  (1.0f + sbr->q_mapped[e][m])));
1561  }
1562  }
1563  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1564  sum[0] += sbr->e_origmapped[e][m];
1565  sum[1] += sbr->e_curr[e][m];
1566  }
1567  gain_max = limgain[sbr->bs_limiter_gains] * sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
1568  gain_max = FFMIN(100000.f, gain_max);
1569  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1570  float q_m_max = sbr->q_m[e][m] * gain_max / sbr->gain[e][m];
1571  sbr->q_m[e][m] = FFMIN(sbr->q_m[e][m], q_m_max);
1572  sbr->gain[e][m] = FFMIN(sbr->gain[e][m], gain_max);
1573  }
1574  sum[0] = sum[1] = 0.0f;
1575  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1576  sum[0] += sbr->e_origmapped[e][m];
1577  sum[1] += sbr->e_curr[e][m] * sbr->gain[e][m] * sbr->gain[e][m]
1578  + sbr->s_m[e][m] * sbr->s_m[e][m]
1579  + (delta && !sbr->s_m[e][m]) * sbr->q_m[e][m] * sbr->q_m[e][m];
1580  }
1581  gain_boost = sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
1582  gain_boost = FFMIN(1.584893192f, gain_boost);
1583  for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1584  sbr->gain[e][m] *= gain_boost;
1585  sbr->q_m[e][m] *= gain_boost;
1586  sbr->s_m[e][m] *= gain_boost;
1587  }
1588  }
1589  }
1590 }
1591 
1592 /// Assembling HF Signals (14496-3 sp04 p220)
1593 static void sbr_hf_assemble(float Y1[38][64][2],
1594  const float X_high[64][40][2],
1595  SpectralBandReplication *sbr, SBRData *ch_data,
1596  const int e_a[2])
1597 {
1598  int e, i, j, m;
1599  const int h_SL = 4 * !sbr->bs_smoothing_mode;
1600  const int kx = sbr->kx[1];
1601  const int m_max = sbr->m[1];
1602  static const float h_smooth[5] = {
1603  0.33333333333333,
1604  0.30150283239582,
1605  0.21816949906249,
1606  0.11516383427084,
1607  0.03183050093751,
1608  };
1609  float (*g_temp)[48] = ch_data->g_temp, (*q_temp)[48] = ch_data->q_temp;
1610  int indexnoise = ch_data->f_indexnoise;
1611  int indexsine = ch_data->f_indexsine;
1612 
1613  if (sbr->reset) {
1614  for (i = 0; i < h_SL; i++) {
1615  memcpy(g_temp[i + 2*ch_data->t_env[0]], sbr->gain[0], m_max * sizeof(sbr->gain[0][0]));
1616  memcpy(q_temp[i + 2*ch_data->t_env[0]], sbr->q_m[0], m_max * sizeof(sbr->q_m[0][0]));
1617  }
1618  } else if (h_SL) {
1619  memcpy(g_temp[2*ch_data->t_env[0]], g_temp[2*ch_data->t_env_num_env_old], 4*sizeof(g_temp[0]));
1620  memcpy(q_temp[2*ch_data->t_env[0]], q_temp[2*ch_data->t_env_num_env_old], 4*sizeof(q_temp[0]));
1621  }
1622 
1623  for (e = 0; e < ch_data->bs_num_env; e++) {
1624  for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
1625  memcpy(g_temp[h_SL + i], sbr->gain[e], m_max * sizeof(sbr->gain[0][0]));
1626  memcpy(q_temp[h_SL + i], sbr->q_m[e], m_max * sizeof(sbr->q_m[0][0]));
1627  }
1628  }
1629 
1630  for (e = 0; e < ch_data->bs_num_env; e++) {
1631  for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
1632  LOCAL_ALIGNED_16(float, g_filt_tab, [48]);
1633  LOCAL_ALIGNED_16(float, q_filt_tab, [48]);
1634  float *g_filt, *q_filt;
1635 
1636  if (h_SL && e != e_a[0] && e != e_a[1]) {
1637  g_filt = g_filt_tab;
1638  q_filt = q_filt_tab;
1639  for (m = 0; m < m_max; m++) {
1640  const int idx1 = i + h_SL;
1641  g_filt[m] = 0.0f;
1642  q_filt[m] = 0.0f;
1643  for (j = 0; j <= h_SL; j++) {
1644  g_filt[m] += g_temp[idx1 - j][m] * h_smooth[j];
1645  q_filt[m] += q_temp[idx1 - j][m] * h_smooth[j];
1646  }
1647  }
1648  } else {
1649  g_filt = g_temp[i + h_SL];
1650  q_filt = q_temp[i];
1651  }
1652 
1653  sbr->dsp.hf_g_filt(Y1[i] + kx, X_high + kx, g_filt, m_max,
1655 
1656  if (e != e_a[0] && e != e_a[1]) {
1657  sbr->dsp.hf_apply_noise[indexsine](Y1[i] + kx, sbr->s_m[e],
1658  q_filt, indexnoise,
1659  kx, m_max);
1660  } else {
1661  int idx = indexsine&1;
1662  int A = (1-((indexsine+(kx & 1))&2));
1663  int B = (A^(-idx)) + idx;
1664  float *out = &Y1[i][kx][idx];
1665  float *in = sbr->s_m[e];
1666  for (m = 0; m+1 < m_max; m+=2) {
1667  out[2*m ] += in[m ] * A;
1668  out[2*m+2] += in[m+1] * B;
1669  }
1670  if(m_max&1)
1671  out[2*m ] += in[m ] * A;
1672  }
1673  indexnoise = (indexnoise + m_max) & 0x1ff;
1674  indexsine = (indexsine + 1) & 3;
1675  }
1676  }
1677  ch_data->f_indexnoise = indexnoise;
1678  ch_data->f_indexsine = indexsine;
1679 }
1680 
1682  float* L, float* R)
1683 {
1684  int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
1685  int ch;
1686  int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1687  int err;
1688 
1689  if (!sbr->kx_and_m_pushed) {
1690  sbr->kx[0] = sbr->kx[1];
1691  sbr->m[0] = sbr->m[1];
1692  } else {
1693  sbr->kx_and_m_pushed = 0;
1694  }
1695 
1696  if (sbr->start) {
1697  sbr_dequant(sbr, id_aac);
1698  }
1699  for (ch = 0; ch < nch; ch++) {
1700  /* decode channel */
1701  sbr_qmf_analysis(&ac->fdsp, &sbr->mdct_ana, &sbr->dsp, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1702  (float*)sbr->qmf_filter_scratch,
1703  sbr->data[ch].W, sbr->data[ch].Ypos);
1704  sbr->c.sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W, sbr->data[ch].Ypos);
1705  sbr->data[ch].Ypos ^= 1;
1706  if (sbr->start) {
1707  sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1, sbr->X_low, sbr->k[0]);
1708  sbr_chirp(sbr, &sbr->data[ch]);
1709  sbr_hf_gen(ac, sbr, sbr->X_high, sbr->X_low, sbr->alpha0, sbr->alpha1,
1710  sbr->data[ch].bw_array, sbr->data[ch].t_env,
1711  sbr->data[ch].bs_num_env);
1712 
1713  // hf_adj
1714  err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1715  if (!err) {
1716  sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1717  sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1718  sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
1719  sbr->X_high, sbr, &sbr->data[ch],
1720  sbr->data[ch].e_a);
1721  }
1722  }
1723 
1724  /* synthesis */
1725  sbr->c.sbr_x_gen(sbr, sbr->X[ch],
1726  sbr->data[ch].Y[1-sbr->data[ch].Ypos],
1727  sbr->data[ch].Y[ sbr->data[ch].Ypos],
1728  sbr->X_low, ch);
1729  }
1730 
1731  if (ac->oc[1].m4ac.ps == 1) {
1732  if (sbr->ps.start) {
1733  ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1734  } else {
1735  memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1736  }
1737  nch = 2;
1738  }
1739 
1740  sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, &ac->fdsp,
1741  L, sbr->X[0], sbr->qmf_filter_scratch,
1742  sbr->data[0].synthesis_filterbank_samples,
1744  downsampled);
1745  if (nch == 2)
1746  sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, &ac->fdsp,
1747  R, sbr->X[1], sbr->qmf_filter_scratch,
1748  sbr->data[1].synthesis_filterbank_samples,
1750  downsampled);
1751 }
1752 
1754 {
1755  c->sbr_lf_gen = sbr_lf_gen;
1757  c->sbr_x_gen = sbr_x_gen;
1759 
1760  if(ARCH_MIPS)
1762 }
uint8_t s_indexmapped[8][48]
Definition: sbr.h:97
unsigned bs_add_harmonic_flag
Definition: sbr.h:68
Definition: start.py:1
int(* sbr_x_gen)(SpectralBandReplication *sbr, float X[2][38][64], const float Y0[38][64][2], const float Y1[38][64][2], const float X_low[32][40][2], int ch)
Definition: sbr.h:126
float v
float e_curr[7][48]
Estimated envelope.
Definition: sbr.h:200
static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
Derived Frequency Band Tables (14496-3 sp04 p197)
Definition: aacsbr.c:572
int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)
Definition: aacps.c:904
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:1593
unsigned bs_smoothing_mode
Definition: sbr.h:150
static void read_sbr_noise(SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data, int ch)
Definition: aacsbr.c:886
AVCodecContext * avctx
Definition: aac.h:264
float(* sum_square)(float(*x)[2], int n)
Definition: sbrdsp.h:28
#define B
Definition: dsputil.c:2025
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
else temp
Definition: vf_mcdeint.c:148
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static void copy_sbr_grid(SBRData *dst, const SBRData *src)
Definition: aacsbr.c:781
Definition: aac.h:49
Definition: aac.h:50
int e_a[2]
l_APrev and l_A
Definition: sbr.h:87
static void aacsbr_func_ptr_init(AACSBRContext *c)
Definition: aacsbr.c:1753
av_cold void ff_aac_sbr_init(void)
Initialize SBR.
Definition: aacsbr.c:96
Sinusoidal phase f
void(* sum64x5)(float *z)
Definition: sbrdsp.h:27
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
unsigned kx[2]
kx&#39;, and kx respectively, kx is the first QMF subband where SBR is used.
Definition: sbr.h:156
static void sbr_qmf_synthesis(FFTContext *mdct, SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp, float *out, float X[2][38][64], float mdct_buf[2][64], float *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.c:1210
float q_m[7][48]
Amplitude adjusted noise scalefactors.
Definition: sbr.h:202
float q_mapped[7][48]
Dequantized noise scalefactors, remapped.
Definition: sbr.h:196
unsigned n_lim
Number of limiter bands.
Definition: sbr.h:169
static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr, float X_high[64][40][2], const float X_low[32][40][2], const float(*alpha0)[2], const float(*alpha1)[2], const float bw_array[5], const uint8_t *t_env, int bs_num_env)
High Frequency Generator (14496-3 sp04 p215)
Definition: aacsbr.c:1362
static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data)
Definition: aacsbr.c:639
#define VLC_TYPE
Definition: get_bits.h:61
the sinusoids Y1
Definition: lab5.m:33
float e_origmapped[7][48]
Dequantized envelope scalefactors, remapped.
Definition: sbr.h:194
FFTContext mdct
Definition: sbr.h:208
uint8_t bs_xover_band
Definition: sbr.h:45
SpectrumParameters spectrum_params
Definition: sbr.h:141
Definition: aac.h:51
Definition: aacsbr.c:74
av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
Close one SBR context.
Definition: aacsbr.c:167
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
float bw_array[5]
Chirp factors.
Definition: sbr.h:89
unsigned kx_and_m_pushed
Definition: sbr.h:159
unsigned n[2]
N_Low and N_High respectively, the number of frequency bands for low and high resolution.
Definition: sbr.h:165
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static const int8_t sbr_offset[6][16]
window coefficients for analysis/synthesis QMF banks
Definition: aacsbrdata.h:260
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t bits
Definition: crc.c:216
uint8_t
#define av_cold
Definition: attributes.h:78
static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
Definition: aacsbr.c:315
float delta
window constants for m
uint16_t f_tablehigh[49]
Frequency borders for high resolution SBR.
Definition: sbr.h:175
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the product of two vectors of floats and store the result in a vector of floats...
Definition: float_dsp.h:38
#define b
Definition: input.c:42
void ff_aacsbr_func_ptr_init_mips(AACSBRContext *c)
Definition: aacsbr_mips.c:608
static int qsort_comparison_function_int16(const void *a, const void *b)
Definition: aacsbr.c:173
int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb_host, int crc, int cnt, int id_aac)
Decode Spectral Band Replication extension data; reference: table 4.55.
Definition: aacsbr.c:1076
#define R
Definition: dsputil.c:2027
#define SBR_INIT_VLC_STATIC(num, size)
Definition: aacsbr.c:85
float env_facs[6][48]
Envelope scalefactors.
Definition: sbr.h:99
AAC Spectral Band Replication decoding data.
#define ENVELOPE_ADJUSTMENT_OFFSET
Definition: aacsbr.c:44
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
#define lrintf(x)
Definition: libm_mips.h:70
end end ac
SBRData data[2]
Definition: sbr.h:162
static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
Definition: aacsbr.c:235
uint8_t bs_df_noise[2]
Definition: sbr.h:73
static void sbr_env_estimate(float(*e_curr)[48], float X_high[64][40][2], SpectralBandReplication *sbr, SBRData *ch_data)
Estimation of current envelope (14496-3 sp04 p218)
Definition: aacsbr.c:1491
static int sbr_mapping(AACContext *ac, SpectralBandReplication *sbr, SBRData *ch_data, int e_a[2])
High Frequency Adjustment (14496-3 sp04 p217) and Mapping (14496-3 sp04 p217)
Definition: aacsbr.c:1436
Sinusoidal Amplitude phi
#define A(x)
static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec, int elements)
Definition: aacsbr.c:625
static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, SpectrumParameters *spectrum)
Master Frequency Band Table (14496-3 sp04 p194)
Definition: aacsbr.c:332
uint8_t patch_num_subbands[6]
Definition: sbr.h:181
static int array_min_int16(const int16_t *array, int nel)
Definition: aacsbr.c:289
uint16_t f_tablenoise[6]
Frequency borders for noise floors.
Definition: sbr.h:177
float gain[7][48]
Definition: sbr.h:205
Discrete Time axis x
MPEG4AudioConfig m4ac
Definition: aac.h:116
uint8_t t_q[3]
Noise time borders.
Definition: sbr.h:107
static double alpha(void *priv, double x, double y)
Definition: vf_geq.c:86
uint16_t f_tablelow[25]
Frequency borders for low resolution SBR.
Definition: sbr.h:173
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:1261
Spectral Band Replication header - spectrum parameters that invoke a reset if they differ from the pr...
Definition: sbr.h:42
static int read_sbr_channel_pair_element(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb)
Definition: aacsbr.c:972
static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
Dequantization and stereo decoding (14496-3 sp04 p203)
Definition: aacsbr.c:1122
unsigned num_patches
Definition: sbr.h:180
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])
Definition: sbr.h:122
av_cold void ff_ps_ctx_init(PSContext *ps)
Definition: aacps.c:966
static const struct endianess table[]
static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64], const float Y0[38][64][2], const float Y1[38][64][2], const float X_low[32][40][2], int ch)
Generate the subband filtered lowband.
Definition: aacsbr.c:1397
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip please avoid problems through this extra level of scrutiny For cosmetics only commits you should e g by running git config global user name My Name git config global user email my email which is either set in your personal configuration file through git config core editor or set by one of the following environment VISUAL or EDITOR Log messages should be concise but descriptive Explain why you made a change
Definition: git-howto.txt:153
#define NOISE_FLOOR_OFFSET
Definition: aacsbr.c:45
Spectral Band Replication definitions and structures.
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
Definition: aacsbr.c:521
phase spectrum(unwrapped) ploc
void(* hf_apply_noise[4])(float(*Y)[2], const float *s_m, const float *q_filt, int noise, int kx, int m_max)
Definition: sbrdsp.h:40
void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac, float *L, float *R)
Apply one SBR element to one AAC element.
Definition: aacsbr.c:1681
#define ff_mdct_init
Definition: fft.h:147
#define FFMAX(a, b)
Definition: common.h:56
unsigned n_master
The number of frequency bands in f_master.
Definition: sbr.h:161
float q_temp[42][48]
Definition: sbr.h:96
unsigned bs_interpol_freq
Definition: sbr.h:149
Reference: libavcodec/aacsbr.c.
static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data)
Read how the envelope and noise floor data is delta coded.
Definition: aacsbr.c:799
FFTContext mdct_ana
Definition: sbr.h:207
Definition: get_bits.h:63
#define powf(x, y)
Definition: libm.h:48
static void sbr_qmf_analysis(AVFloatDSPContext *dsp, FFTContext *mdct, SBRDSPContext *sbrdsp, const float *in, float *x, float z[320], float W[2][32][32][2], int buf_idx)
Analysis QMF Bank (14496-3 sp04 p206)
Definition: aacsbr.c:1186
unsigned f_indexnoise
Definition: sbr.h:108
av_cold void ff_sbrdsp_init(SBRDSPContext *s)
Definition: sbrdsp.c:267
void(* qmf_pre_shuffle)(float *z)
Definition: sbrdsp.h:30
AVFloatDSPContext fdsp
Definition: aac.h:295
common internal API header
static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, int id_aac)
Definition: aacsbr.c:1014
static void sbr_turnoff(SpectralBandReplication *sbr)
Places SBR in pure upsampling mode.
Definition: aacsbr.c:139
uint8_t t_env_num_env_old
Envelope time border of the last envelope of the previous frame.
Definition: sbr.h:105
AAC Spectral Band Replication function declarations.
Definition: fft.h:62
FFT buffer for g
Definition: stft_peak.m:17
unsigned bs_amp_res
Definition: sbr.h:76
void(* neg_odd_64)(float *x)
Definition: sbrdsp.h:29
#define FFMIN(a, b)
Definition: common.h:58
uint8_t bs_freq_scale
Definition: sbr.h:51
static void read_sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data, int ch)
Definition: aacsbr.c:817
static int read_sbr_single_channel_element(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb)
Definition: aacsbr.c:952
Definition: aacsbr.c:73
unsigned bs_limiter_gains
Definition: sbr.h:148
Sampled sinusoid X
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:255
float W[2][32][32][2]
QMF values of the original signal.
Definition: sbr.h:91
uint8_t s_mapped[7][48]
Sinusoidal presence, remapped.
Definition: sbr.h:198
#define SBR_VLC_ROW(name)
Definition: aacsbr.c:91
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:524
AAC definitions and structures.
uint8_t bs_freq_res[7]
Definition: sbr.h:70
#define L(x)
static void sbr_gain_calc(AACContext *ac, SpectralBandReplication *sbr, SBRData *ch_data, const int e_a[2])
Calculation of levels of additional HF signal components (14496-3 sp04 p219) and Calculation of gain ...
Definition: aacsbr.c:1537
av_cold void ff_ps_init(void)
Definition: aacps.c:934
void(* autocorrelate)(const float x[40][2], float phi[3][2][2])
Definition: sbrdsp.h:34
int start
Definition: aacps.h:42
for k
NULL
Definition: eval.c:55
#define SBR_SYNTHESIS_BUF_SIZE
Definition: sbr.h:57
float s_m[7][48]
Sinusoidal levels.
Definition: sbr.h:204
#define exp2f(x)
Definition: libm.h:82
AVS_Value src
Definition: avisynth_c.h:523
Definition: aacsbr.c:72
static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
Limiter Frequency Band Table (14496-3 sp04 p198)
Definition: aacsbr.c:188
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned m[2]
M&#39; and M respectively, M is the number of QMF subbands that use SBR.
Definition: sbr.h:158
Replacements for frequently missing libm functions.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
int synthesis_filterbank_samples_offset
Definition: sbr.h:85
synthesis window for stochastic i
int mdct_bits
Definition: fft.h:68
unsigned k[5]
k0, k1, k2
Definition: sbr.h:153
static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr, float X_low[32][40][2], const float W[2][32][32][2], int buf_idx)
Generate the subband filtered lowband.
Definition: aacsbr.c:1337
static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
Chirp Factors (14496-3 sp04 p214)
Definition: aacsbr.c:1316
uint8_t bs_noise_bands
Definition: sbr.h:53
#define ARCH_MIPS
Definition: config.h:23
av_cold void ff_aac_sbr_ctx_init(AACContext *ac, SpectralBandReplication *sbr)
Initialize one SBR context.
Definition: aacsbr.c:149
main AAC context
Definition: aac.h:262
AACSBRContext c
Definition: sbr.h:210
uint8_t bs_stop_freq
Definition: sbr.h:44
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:82
static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, int bs_extension_id, int *num_bits_left)
Definition: aacsbr.c:922
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint16_t f_master[49]
The master QMF frequency grouping.
Definition: sbr.h:171
uint8_t bs_invf_mode[2][5]
Definition: sbr.h:74
static int in_table_int16(const int16_t *table, int last_el, int16_t needle)
Definition: aacsbr.c:178
#define v0
Definition: regdef.h:26
float noise_facs[3][5]
Noise scalefactors.
Definition: sbr.h:101
Definition: aacsbr.c:71
static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
Definition: aacsbr.c:1055
OutputConfiguration oc[2]
Definition: aac.h:317
int(* sbr_lf_gen)(AACContext *ac, SpectralBandReplication *sbr, float X_low[32][40][2], const float W[2][32][32][2], int buf_idx)
Definition: sbr.h:119
static const int8_t ceil_log2[]
ceil(log2(index+1))
Definition: aacsbr.c:635
#define log2f(x)
Definition: libm.h:127
unsigned f_indexsine
Definition: sbr.h:109
#define ff_mdct_end
Definition: fft.h:148
static double c[64]
uint8_t patch_start_subband[6]
Definition: sbr.h:182
uint8_t t_env[8]
Envelope time borders.
Definition: sbr.h:103
void(* vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len)
Calculate the product of two vectors of floats, add a third vector of floats and store the result in ...
Definition: float_dsp.h:121
aacsbr functions pointers
Definition: sbr.h:118
uint16_t f_tablelim[30]
Frequency borders for the limiter.
Definition: sbr.h:179
Spectral Band Replication per channel data.
Definition: sbr.h:62
void(* sbr_hf_inverse_filter)(SBRDSPContext *dsp, float(*alpha0)[2], float(*alpha1)[2], const float X_low[32][40][2], int k0)
Definition: sbr.h:129
static void make_bands(int16_t *bands, int start, int stop, int num_bands)
Definition: aacsbr.c:297
unsigned bs_limiter_bands
Definition: sbr.h:147
int Ypos
QMF output of the HF adjustor.
Definition: sbr.h:93
uint8_t bs_alter_scale
Definition: sbr.h:52
unsigned bs_frame_class
Definition: sbr.h:67
void(* qmf_post_shuffle)(float W[32][2], const float *z)
Definition: sbrdsp.h:31
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
uint8_t bs_df_env[5]
Definition: sbr.h:72
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
unsigned bs_num_noise
Definition: sbr.h:71
void(* qmf_deint_bfly)(float *v, const float *src0, const float *src1)
Definition: sbrdsp.h:33
static const int8_t vlc_sbr_lav[10]
Definition: aacsbr.c:82
unsigned n_q
Number of noise floor bands.
Definition: sbr.h:167
SBRDSPContext dsp
Definition: sbr.h:209
#define LOCAL_ALIGNED_16(t, v,...)
void INT64 start
Definition: avisynth_c.h:594
#define av_always_inline
Definition: attributes.h:41
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
int ps
-1 implicit, 1 presence
Definition: mpeg4audio.h:40
static VLC vlc_sbr[10]
Definition: aacsbr.c:81
void(* qmf_deint_neg)(float *v, const float *src)
Definition: sbrdsp.h:32
unsigned bs_coupling
Definition: sbr.h:152
void(* hf_gen)(float(*X_high)[2], const float(*X_low)[2], const float alpha0[2], const float alpha1[2], float bw, int start, int end)
Definition: sbrdsp.h:35
Spectral Band Replication.
Definition: sbr.h:137
unsigned bs_num_env
Definition: sbr.h:69
void(* hf_g_filt)(float(*Y)[2], const float(*X_high)[40][2], const float *g_filt, int m_max, intptr_t ixh)
Definition: sbrdsp.h:38
float min
uint8_t bs_add_harmonic[48]
Definition: sbr.h:75
int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
Definition: aacps.c:151
static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb, SBRData *ch_data)
Read inverse filtering data.
Definition: aacsbr.c:807
PSContext ps
Definition: sbr.h:163
uint8_t bs_start_freq
Definition: sbr.h:43
normalize window W
Definition: stft_peak.m:10
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
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the product of two vectors of floats, and store the result in a vector of floats...
Definition: float_dsp.h:140