FFmpeg
aacdec_usac.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 Lynne <dev@lynne.ee>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "aacdec_usac.h"
22 #include "aacdec_tab.h"
23 #include "aacdec_lpd.h"
24 #include "aacdec_ac.h"
25 
26 #include "libavcodec/aacsbr.h"
27 #include "libavcodec/aactab.h"
28 #include "libavcodec/mpeg4audio.h"
29 #include "libavcodec/unary.h"
30 
31 #include "libavutil/mem.h"
32 #include "libavutil/refstruct.h"
33 
34 #include "aacdec_usac_mps212.h"
35 
36 /* Number of scalefactor bands per complex prediction band, equal to 2. */
37 #define SFB_PER_PRED_BAND 2
38 
39 static inline uint32_t get_escaped_value(GetBitContext *gb, int nb1, int nb2, int nb3)
40 {
41  uint32_t val = get_bits(gb, nb1), val2;
42  if (val < ((1 << nb1) - 1))
43  return val;
44 
45  val += val2 = get_bits(gb, nb2);
46  if (nb3 && (val2 == ((1 << nb2) - 1)))
47  val += get_bits(gb, nb3);
48 
49  return val;
50 }
51 
52 /* ISO/IEC 23003-3, Table 74 — bsOutputChannelPos */
53 static const enum AVChannel usac_ch_pos_to_av[64] = {
54  [0] = AV_CHAN_FRONT_LEFT,
55  [1] = AV_CHAN_FRONT_RIGHT,
58  [4] = AV_CHAN_SIDE_LEFT, // +110 degrees, Ls|LS|kAudioChannelLabel_LeftSurround
59  [5] = AV_CHAN_SIDE_RIGHT, // -110 degrees, Rs|RS|kAudioChannelLabel_RightSurround
62  [8] = AV_CHAN_BACK_LEFT, // +135 degrees, Lsr|BL|kAudioChannelLabel_RearSurroundLeft
63  [9] = AV_CHAN_BACK_RIGHT, // -135 degrees, Rsr|BR|kAudioChannelLabel_RearSurroundRight
64  [10] = AV_CHAN_BACK_CENTER,
67  [13] = AV_CHAN_SIDE_SURROUND_LEFT, // +90 degrees, Lss|SL|kAudioChannelLabel_LeftSideSurround
68  [14] = AV_CHAN_SIDE_SURROUND_RIGHT, // -90 degrees, Rss|SR|kAudioChannelLabel_RightSideSurround
69  [15] = AV_CHAN_WIDE_LEFT, // +60 degrees, Lw|FLw|kAudioChannelLabel_LeftWide
70  [16] = AV_CHAN_WIDE_RIGHT, // -60 degrees, Rw|FRw|kAudioChannelLabel_RightWide
74  [20] = AV_CHAN_TOP_BACK_LEFT,
77  [23] = AV_CHAN_TOP_SIDE_LEFT,
79  [25] = AV_CHAN_TOP_CENTER,
84  [30] = AV_CHAN_TOP_SURROUND_LEFT, ///< +110 degrees, Lvs, TpLS
85  [31] = AV_CHAN_TOP_SURROUND_RIGHT, ///< -110 degrees, Rvs, TpRS
86 };
87 
89  GetBitContext *gb)
90 {
91  info->drc_set_id = get_bits(gb, 6);
92  info->downmix_id = get_bits(gb, 7);
93 
94  if ((info->sample_peak.present = get_bits1(gb))) /* samplePeakLevelPresent */
95  info->sample_peak.lvl = get_bits(gb, 12);
96 
97  if ((info->true_peak.present = get_bits1(gb))) { /* truePeakLevelPresent */
98  info->true_peak.lvl = get_bits(gb, 12);
99  info->true_peak.measurement = get_bits(gb, 4);
100  info->true_peak.reliability = get_bits(gb, 2);
101  }
102 
103  info->nb_measurements = get_bits(gb, 4);
104  for (int i = 0; i < info->nb_measurements; i++) {
105  info->measurements[i].method_def = get_bits(gb, 4);
106  info->measurements[i].method_val = get_unary(gb, 0, 8);
107  info->measurements[i].measurement = get_bits(gb, 4);
108  info->measurements[i].reliability = get_bits(gb, 2);
109  }
110 
111  return 0;
112 }
113 
115  GetBitContext *gb)
116 {
117  int ret;
118 
119  usac->loudness.nb_album = get_bits(gb, 6); /* loudnessInfoAlbumCount */
120  usac->loudness.nb_info = get_bits(gb, 6); /* loudnessInfoCount */
121 
122  for (int i = 0; i < usac->loudness.nb_album; i++) {
123  ret = decode_loudness_info(ac, &usac->loudness.album_info[i], gb);
124  if (ret < 0)
125  return ret;
126  }
127 
128  for (int i = 0; i < usac->loudness.nb_info; i++) {
129  ret = decode_loudness_info(ac, &usac->loudness.info[i], gb);
130  if (ret < 0)
131  return ret;
132  }
133 
134  if (get_bits1(gb)) { /* loudnessInfoSetExtPresent */
136  while ((type = get_bits(gb, 4)) != UNIDRCLOUDEXT_TERM) {
137  uint8_t size_bits = get_bits(gb, 4) + 4;
138  uint8_t bit_size = get_bits(gb, size_bits) + 1;
139  switch (type) {
140  case UNIDRCLOUDEXT_EQ:
141  avpriv_report_missing_feature(ac->avctx, "loudnessInfoV1");
142  return AVERROR_PATCHWELCOME;
143  default:
144  for (int i = 0; i < bit_size; i++)
145  skip_bits1(gb);
146  }
147  }
148  }
149 
150  return 0;
151 }
152 
155 {
156  uint8_t header_extra1;
157  uint8_t header_extra2;
158 
159  e->sbr.harmonic_sbr = get_bits1(gb); /* harmonicSBR */
160  e->sbr.bs_intertes = get_bits1(gb); /* bs_interTes */
161  e->sbr.bs_pvc = get_bits1(gb); /* bs_pvc */
162  if (e->sbr.harmonic_sbr || e->sbr.bs_intertes || e->sbr.bs_pvc) {
163  avpriv_report_missing_feature(ac->avctx, "AAC USAC eSBR");
164  return AVERROR_PATCHWELCOME;
165  }
166 
167  e->sbr.dflt.start_freq = get_bits(gb, 4); /* dflt_start_freq */
168  e->sbr.dflt.stop_freq = get_bits(gb, 4); /* dflt_stop_freq */
169 
170  header_extra1 = get_bits1(gb); /* dflt_header_extra1 */
171  header_extra2 = get_bits1(gb); /* dflt_header_extra2 */
172 
173  e->sbr.dflt.freq_scale = 2;
174  e->sbr.dflt.alter_scale = 1;
175  e->sbr.dflt.noise_bands = 2;
176  if (header_extra1) {
177  e->sbr.dflt.freq_scale = get_bits(gb, 2); /* dflt_freq_scale */
178  e->sbr.dflt.alter_scale = get_bits1(gb); /* dflt_alter_scale */
179  e->sbr.dflt.noise_bands = get_bits(gb, 2); /* dflt_noise_bands */
180  }
181 
182  e->sbr.dflt.limiter_bands = 2;
183  e->sbr.dflt.limiter_gains = 2;
184  e->sbr.dflt.interpol_freq = 1;
185  e->sbr.dflt.smoothing_mode = 1;
186  if (header_extra2) {
187  e->sbr.dflt.limiter_bands = get_bits(gb, 2); /* dflt_limiter_bands */
188  e->sbr.dflt.limiter_gains = get_bits(gb, 2); /* dflt_limiter_gains */
189  e->sbr.dflt.interpol_freq = get_bits1(gb); /* dflt_interpol_freq */
190  e->sbr.dflt.smoothing_mode = get_bits1(gb); /* dflt_smoothing_mode */
191  }
192 
193  return 0;
194 }
195 
197  GetBitContext *gb,
198  int sbr_ratio)
199 {
200  e->tw_mdct = get_bits1(gb); /* tw_mdct */
201  e->noise_fill = get_bits1(gb);
202  e->sbr.ratio = sbr_ratio;
203 }
204 
207 {
208  e->stereo_config_index = 0;
209  if (e->sbr.ratio) {
210  int ret = decode_usac_sbr_data(ac, e, gb);
211  if (ret < 0)
212  return ret;
213  e->stereo_config_index = get_bits(gb, 2);
214  }
215 
216  if (e->stereo_config_index) {
217  e->mps.freq_res = get_bits(gb, 3); /* bsFreqRes */
218  e->mps.fixed_gain = get_bits(gb, 3); /* bsFixedGainDMX */
219  e->mps.temp_shape_config = get_bits(gb, 2); /* bsTempShapeConfig */
220  e->mps.decorr_config = get_bits(gb, 2); /* bsDecorrConfig */
221  e->mps.high_rate_mode = get_bits1(gb); /* bsHighRateMode */
222  e->mps.phase_coding = get_bits1(gb); /* bsPhaseCoding */
223 
225  if (e->mps.otts_bands_phase_present) /* bsOttBandsPhasePresent */
226  e->mps.otts_bands_phase = get_bits(gb, 5); /* bsOttBandsPhase */
227 
228  e->mps.residual_coding = e->stereo_config_index >= 2; /* bsResidualCoding */
229  if (e->mps.residual_coding) {
230  e->mps.residual_bands = get_bits(gb, 5); /* bsResidualBands */
232  e->mps.residual_bands);
233  e->mps.pseudo_lr = get_bits1(gb); /* bsPseudoLr */
234  }
235  if (e->mps.temp_shape_config == 2)
236  e->mps.env_quant_mode = get_bits1(gb); /* bsEnvQuantMode */
237  }
238 
239  return 0;
240 }
241 
243  GetBitContext *gb)
244 {
245  int len = 0, ext_config_len;
246 
247  e->ext.type = get_escaped_value(gb, 4, 8, 16); /* usacExtElementType */
248  ext_config_len = get_escaped_value(gb, 4, 8, 16); /* usacExtElementConfigLength */
249 
250  if (get_bits1(gb)) /* usacExtElementDefaultLengthPresent */
251  len = get_escaped_value(gb, 8, 16, 0) + 1;
252 
253  e->ext.default_len = len;
254  e->ext.payload_frag = get_bits1(gb); /* usacExtElementPayloadFrag */
255 
256  av_log(ac->avctx, AV_LOG_DEBUG, "Extension present: type %i, len %i\n",
257  e->ext.type, ext_config_len);
258 
259  switch (e->ext.type) {
260 #if 0 /* Skip unsupported values */
261  case ID_EXT_ELE_MPEGS:
262  break;
263  case ID_EXT_ELE_SAOC:
264  break;
265  case ID_EXT_ELE_UNI_DRC:
266  break;
267 #endif
268  case ID_EXT_ELE_FILL:
269  break; /* This is what the spec does */
271  /* No configuration needed - fallthrough (len should be 0) */
272  default:
273  skip_bits(gb, 8*ext_config_len);
274  e->ext.type = ID_EXT_ELE_FILL;
275  break;
276  };
277 
278  return 0;
279 }
280 
282 {
283  AACUSACConfig *usac = &oc->usac;
284  int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
285 
286  ChannelElement *che;
287  enum RawDataBlockType type;
288  int id, ch;
289 
290  /* Initialize state */
291  for (int i = 0; i < usac->nb_elems; i++) {
292  AACUsacElemConfig *e = &usac->elems[i];
293  if (e->type == ID_USAC_EXT)
294  continue;
295 
296  switch (e->type) {
297  case ID_USAC_SCE:
298  ch = 1;
299  type = TYPE_SCE;
300  id = elem_id[0]++;
301  break;
302  case ID_USAC_CPE:
303  ch = 2;
304  type = TYPE_CPE;
305  id = elem_id[1]++;
306  break;
307  case ID_USAC_LFE:
308  ch = 1;
309  type = TYPE_LFE;
310  id = elem_id[2]++;
311  break;
312  }
313 
314  che = ff_aac_get_che(ac, type, id);
315  if (che) {
316  AACUsacStereo *us = &che->us;
317  memset(us, 0, sizeof(*us));
318 
319  if (e->sbr.ratio)
320  ff_aac_sbr_config_usac(ac, che, e);
321 
322  for (int j = 0; j < ch; j++) {
323  SingleChannelElement *sce = &che->ch[j];
324  AACUsacElemData *ue = &sce->ue;
325 
326  memset(ue, 0, sizeof(*ue));
327 
328  if (!ch)
329  ue->noise.seed = 0x3039;
330  else
331  che->ch[1].ue.noise.seed = 0x10932;
332  }
333  }
334  }
335 
336  return 0;
337 }
338 
339 /* UsacConfig */
342  int channel_config)
343 {
344  int ret;
345  uint8_t freq_idx;
346  uint8_t channel_config_idx;
347  int nb_channels = 0;
348  int ratio_mult, ratio_dec;
349  int samplerate;
350  int sbr_ratio;
351  MPEG4AudioConfig *m4ac = &oc->m4ac;
352  AACUSACConfig *usac = &oc->usac;
353  int elem_id[3 /* SCE, CPE, LFE */];
354 
355  int map_pos_set = 0;
356  uint8_t layout_map[MAX_ELEM_ID*4][3] = { 0 };
357 
358  if (!ac)
359  return AVERROR_PATCHWELCOME;
360 
361  memset(usac, 0, sizeof(*usac));
362 
363  freq_idx = get_bits(gb, 5); /* usacSamplingFrequencyIndex */
364  if (freq_idx == 0x1f) {
365  samplerate = get_bits(gb, 24); /* usacSamplingFrequency */
366  } else {
367  samplerate = ff_aac_usac_samplerate[freq_idx];
368  if (samplerate < 0)
369  return AVERROR(EINVAL);
370  }
371 
372  usac->core_sbr_frame_len_idx = get_bits(gb, 3); /* coreSbrFrameLengthIndex */
373  m4ac->frame_length_short = usac->core_sbr_frame_len_idx == 0 ||
374  usac->core_sbr_frame_len_idx == 2;
375 
376  usac->core_frame_len = (usac->core_sbr_frame_len_idx == 0 ||
377  usac->core_sbr_frame_len_idx == 2) ? 768 : 1024;
378 
379  sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
380  usac->core_sbr_frame_len_idx == 3 ? 3 :
381  usac->core_sbr_frame_len_idx == 4 ? 1 :
382  0;
383 
384  if (sbr_ratio == 2) {
385  ratio_mult = 8;
386  ratio_dec = 3;
387  } else if (sbr_ratio == 3) {
388  ratio_mult = 2;
389  ratio_dec = 1;
390  } else if (sbr_ratio == 4) {
391  ratio_mult = 4;
392  ratio_dec = 1;
393  } else {
394  ratio_mult = 1;
395  ratio_dec = 1;
396  }
397 
398  avctx->sample_rate = samplerate;
399  m4ac->ext_sample_rate = samplerate;
400  m4ac->sample_rate = (samplerate * ratio_dec) / ratio_mult;
401 
403  m4ac->sbr = sbr_ratio > 0;
404 
405  channel_config_idx = get_bits(gb, 5); /* channelConfigurationIndex */
406  if (!channel_config_idx) {
407  /* UsacChannelConfig() */
408  nb_channels = get_escaped_value(gb, 5, 8, 16); /* numOutChannels */
409  if (nb_channels > 64)
410  return AVERROR(EINVAL);
411 
413 
414  ret = av_channel_layout_custom_init(&ac->oc[1].ch_layout, nb_channels);
415  if (ret < 0)
416  return ret;
417 
418  for (int i = 0; i < nb_channels; i++) {
419  AVChannelCustom *cm = &ac->oc[1].ch_layout.u.map[i];
420  cm->id = usac_ch_pos_to_av[get_bits(gb, 5)]; /* bsOutputChannelPos */
421  }
422 
426  if (ret < 0)
427  return ret;
428 
429  ret = av_channel_layout_copy(&avctx->ch_layout, &ac->oc[1].ch_layout);
430  if (ret < 0)
431  return ret;
432  } else {
433  int nb_elements;
434  if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
435  &nb_elements, channel_config_idx)))
436  return ret;
437 
438  /* Fill in the number of expected channels */
439  for (int i = 0; i < nb_elements; i++)
440  nb_channels += layout_map[i][0] == TYPE_CPE ? 2 : 1;
441 
442  map_pos_set = 1;
443  }
444 
445  /* UsacDecoderConfig */
446  elem_id[0] = elem_id[1] = elem_id[2] = 0;
447  usac->nb_elems = get_escaped_value(gb, 4, 8, 16) + 1;
448  if (usac->nb_elems > 64) {
449  av_log(ac->avctx, AV_LOG_ERROR, "Too many elements: %i\n",
450  usac->nb_elems);
451  usac->nb_elems = 0;
452  return AVERROR(EINVAL);
453  }
454 
455  for (int i = 0; i < usac->nb_elems; i++) {
456  int map_count = elem_id[0] + elem_id[1] + elem_id[2];
457  AACUsacElemConfig *e = &usac->elems[i];
458  memset(e, 0, sizeof(*e));
459 
460  e->type = get_bits(gb, 2); /* usacElementType */
461  if (e->type != ID_USAC_EXT && (map_count + 1) > nb_channels) {
462  av_log(ac->avctx, AV_LOG_ERROR, "Too many channels for the channel "
463  "configuration\n");
464  usac->nb_elems = 0;
465  return AVERROR(EINVAL);
466  }
467 
468  av_log(ac->avctx, AV_LOG_DEBUG, "Element present: idx %i, type %i\n",
469  i, e->type);
470 
471  switch (e->type) {
472  case ID_USAC_SCE: /* SCE */
473  /* UsacCoreConfig */
474  decode_usac_element_core(e, gb, sbr_ratio);
475  if (e->sbr.ratio > 0) {
476  ret = decode_usac_sbr_data(ac, e, gb);
477  if (ret < 0)
478  return ret;
479  }
480  layout_map[map_count][0] = TYPE_SCE;
481  layout_map[map_count][1] = elem_id[0]++;
482  if (!map_pos_set)
483  layout_map[map_count][2] = AAC_CHANNEL_FRONT;
484 
485  break;
486  case ID_USAC_CPE: /* UsacChannelPairElementConf */
487  /* UsacCoreConfig */
488  decode_usac_element_core(e, gb, sbr_ratio);
489  ret = decode_usac_element_pair(ac, e, gb);
490  if (ret < 0)
491  return ret;
492  layout_map[map_count][0] = TYPE_CPE;
493  layout_map[map_count][1] = elem_id[1]++;
494  if (!map_pos_set)
495  layout_map[map_count][2] = AAC_CHANNEL_FRONT;
496 
497  break;
498  case ID_USAC_LFE: /* LFE */
499  /* LFE has no need for any configuration */
500  e->tw_mdct = 0;
501  e->noise_fill = 0;
502  layout_map[map_count][0] = TYPE_LFE;
503  layout_map[map_count][1] = elem_id[2]++;
504  if (!map_pos_set)
505  layout_map[map_count][2] = AAC_CHANNEL_LFE;
506 
507  break;
508  case ID_USAC_EXT: /* EXT */
509  ret = decode_usac_extension(ac, e, gb);
510  if (ret < 0)
511  return ret;
512  break;
513  };
514  }
515 
516  ret = ff_aac_output_configure(ac, layout_map, elem_id[0] + elem_id[1] + elem_id[2],
517  OC_GLOBAL_HDR, 0);
518  if (ret < 0) {
519  av_log(avctx, AV_LOG_ERROR, "Unable to parse channel config!\n");
520  usac->nb_elems = 0;
521  return ret;
522  }
523 
524  if (get_bits1(gb)) { /* usacConfigExtensionPresent */
525  int invalid;
526  int nb_extensions = get_escaped_value(gb, 2, 4, 8) + 1; /* numConfigExtensions */
527  for (int i = 0; i < nb_extensions; i++) {
528  int type = get_escaped_value(gb, 4, 8, 16);
529  int len = get_escaped_value(gb, 4, 8, 16);
530  switch (type) {
532  ret = decode_loudness_set(ac, usac, gb);
533  if (ret < 0)
534  return ret;
535  break;
537  usac->stream_identifier = get_bits(gb, 16);
538  break;
539  case ID_CONFIG_EXT_FILL: /* fallthrough */
540  invalid = 0;
541  while (len--) {
542  if (get_bits(gb, 8) != 0xA5)
543  invalid++;
544  }
545  if (invalid)
546  av_log(avctx, AV_LOG_WARNING, "Invalid fill bytes: %i\n",
547  invalid);
548  break;
549  default:
550  while (len--)
551  skip_bits(gb, 8);
552  break;
553  }
554  }
555  }
556 
558 
559  ret = ff_aac_usac_reset_state(ac, oc);
560  if (ret < 0)
561  return ret;
562 
563  return 0;
564 }
565 
568  GetBitContext *gb, uint8_t global_gain)
569 {
570  IndividualChannelStream *ics = &sce->ics;
571 
572  /* Decode all scalefactors. */
573  int offset_sf = global_gain;
574  for (int g = 0; g < ics->num_window_groups; g++) {
575  for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
576  if (g || sfb)
577  offset_sf += get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - SCALE_DIFF_ZERO;
578  if (offset_sf > 255U) {
580  "Scalefactor (%d) out of range.\n", offset_sf);
581  return AVERROR_INVALIDDATA;
582  }
583 
584  sce->sfo[g*ics->max_sfb + sfb] = offset_sf - 100;
585  }
586  }
587 
588  return 0;
589 }
590 
591 /**
592  * Decode and dequantize arithmetically coded, uniformly quantized value
593  *
594  * @param coef array of dequantized, scaled spectral data
595  * @param sf array of scalefactors or intensity stereo positions
596  *
597  * @return Returns error status. 0 - OK, !0 - error
598  */
599 static int decode_spectrum_ac(AACDecContext *s, float coef[1024],
601  int reset, uint16_t len, uint16_t N)
602 {
603  AACArith ac;
604  int i, a, b;
605  uint32_t c;
606 
607  int gb_count;
608  GetBitContext gb2;
609 
610  c = ff_aac_ac_map_process(state, reset, N);
611 
612  if (!len) {
613  ff_aac_ac_finish(state, 0, N);
614  return 0;
615  }
616 
617  ff_aac_ac_init(&ac, gb);
618 
619  /* Backup reader for rolling back by 14 bits at the end */
620  gb2 = *gb;
621  gb_count = get_bits_count(&gb2);
622 
623  for (i = 0; i < len/2; i++) {
624  /* MSB */
625  int lvl, esc_nb, m;
627  for (lvl=esc_nb=0;;) {
628  uint32_t pki = ff_aac_ac_get_pk(c + (esc_nb << 17));
629  m = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_msb_cdfs[pki],
631  if (m < FF_AAC_AC_ESCAPE)
632  break;
633  lvl++;
634 
635  /* Cargo-culted value. */
636  if (lvl > 23)
637  return AVERROR(EINVAL);
638 
639  if ((esc_nb = lvl) > 7)
640  esc_nb = 7;
641  }
642 
643  b = m >> 2;
644  a = m - (b << 2);
645 
646  /* ARITH_STOP detection */
647  if (!m) {
648  if (esc_nb)
649  break;
650  a = b = 0;
651  }
652 
653  /* LSB */
654  for (int l = lvl; l > 0; l--) {
655  int lsbidx = !a ? 1 : (!b ? 0 : 2);
656  uint8_t r = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_lsb_cdfs[lsbidx],
658  a = (a << 1) | (r & 1);
659  b = (b << 1) | ((r >> 1) & 1);
660  }
661 
662  /* Dequantize coeffs here */
663  coef[2*i + 0] = a * cbrt(a);
664  coef[2*i + 1] = b * cbrt(b);
666  }
667 
668  if (len > 1) {
669  /* "Rewind" bitstream back by 14 bits */
670  int gb_count2 = get_bits_count(&gb2);
671  skip_bits(gb, gb_count2 - gb_count - 14);
672  } else {
673  *gb = gb2;
674  }
675 
677 
678  for (; i < N/2; i++) {
679  coef[2*i + 0] = 0;
680  coef[2*i + 1] = 0;
681  }
682 
683  /* Signs */
684  for (i = 0; i < len; i++) {
685  if (coef[i]) {
686  if (!get_bits1(gb)) /* s */
687  coef[i] *= -1;
688  }
689  }
690 
691  return 0;
692 }
693 
695  ChannelElement *cpe, GetBitContext *gb,
696  int num_window_groups,
697  int prev_num_window_groups,
698  int indep_flag)
699 {
700  int delta_code_time;
701  IndividualChannelStream *ics = &cpe->ch[0].ics;
702 
703  if (!get_bits1(gb)) { /* cplx_pred_all */
704  for (int g = 0; g < num_window_groups; g++) {
705  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
706  const uint8_t val = get_bits1(gb);
707  us->pred_used[g*cpe->max_sfb_ste + sfb] = val;
708  if ((sfb + 1) < cpe->max_sfb_ste)
709  us->pred_used[g*cpe->max_sfb_ste + sfb + 1] = val;
710  }
711  }
712  } else {
713  for (int g = 0; g < num_window_groups; g++)
714  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++)
715  us->pred_used[g*cpe->max_sfb_ste + sfb] = 1;
716  }
717 
718  us->pred_dir = get_bits1(gb);
719  us->complex_coef = get_bits1(gb);
720 
721  us->use_prev_frame = 0;
722  if (us->complex_coef && !indep_flag)
723  us->use_prev_frame = get_bits1(gb);
724 
725  delta_code_time = 0;
726  if (!indep_flag)
727  delta_code_time = get_bits1(gb);
728 
729  /* TODO: shouldn't be needed */
730  for (int g = 0; g < num_window_groups; g++) {
731  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
732  float last_alpha_q_re = 0;
733  float last_alpha_q_im = 0;
734  if (delta_code_time) {
735  if (g) {
736  /* Transient, after the first group - use the current frame,
737  * previous window, alpha values. */
738  last_alpha_q_re = us->alpha_q_re[(g - 1)*cpe->max_sfb_ste + sfb];
739  last_alpha_q_im = us->alpha_q_im[(g - 1)*cpe->max_sfb_ste + sfb];
740  } else if (!g &&
741  (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) &&
742  (ics->window_sequence[1] == EIGHT_SHORT_SEQUENCE)) {
743  /* The spec doesn't explicitly mention this, but it doesn't make
744  * any other sense otherwise! */
745  const int wg = prev_num_window_groups - 1;
746  last_alpha_q_re = us->prev_alpha_q_re[wg*cpe->max_sfb_ste + sfb];
747  last_alpha_q_im = us->prev_alpha_q_im[wg*cpe->max_sfb_ste + sfb];
748  } else {
749  last_alpha_q_re = us->prev_alpha_q_re[g*cpe->max_sfb_ste + sfb];
750  last_alpha_q_im = us->prev_alpha_q_im[g*cpe->max_sfb_ste + sfb];
751  }
752  } else {
753  if (sfb) {
754  last_alpha_q_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb - 1];
755  last_alpha_q_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb - 1];
756  }
757  }
758 
759  if (us->pred_used[g*cpe->max_sfb_ste + sfb]) {
760  int val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60;
761  last_alpha_q_re += val * 0.1f;
762  if (us->complex_coef) {
763  val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60;
764  last_alpha_q_im += val * 0.1f;
765  }
766  us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = last_alpha_q_re;
767  us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = last_alpha_q_im;
768  } else {
769  us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = 0;
770  us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = 0;
771  }
772 
773  if ((sfb + 1) < cpe->max_sfb_ste) {
774  us->alpha_q_re[g*cpe->max_sfb_ste + sfb + 1] =
775  us->alpha_q_re[g*cpe->max_sfb_ste + sfb];
776  us->alpha_q_im[g*cpe->max_sfb_ste + sfb + 1] =
777  us->alpha_q_im[g*cpe->max_sfb_ste + sfb];
778  }
779  }
780  }
781 
782  return 0;
783 }
784 
786  AACUSACConfig *usac)
787 {
788  AACUsacElemData *ue = &sce->ue;
789  IndividualChannelStream *ics = &sce->ics;
790  const int sampling_index = ac->oc[1].m4ac.sampling_index;
791 
792  /* Setup window parameters */
794  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
795  if (usac->core_frame_len == 768) {
796  ics->swb_offset = ff_swb_offset_96[sampling_index];
797  ics->num_swb = ff_aac_num_swb_96[sampling_index];
798  } else {
799  ics->swb_offset = ff_swb_offset_128[sampling_index];
800  ics->num_swb = ff_aac_num_swb_128[sampling_index];
801  }
802  ics->tns_max_bands = ff_tns_max_bands_usac_128[sampling_index];
803 
804  /* Setup scalefactor grouping. 7 bit mask. */
805  ics->num_window_groups = 0;
806  for (int j = 0; j < 7; j++) {
807  ics->group_len[j] = 1;
808  if (ue->scale_factor_grouping & (1 << (6 - j)))
809  ics->group_len[ics->num_window_groups] += 1;
810  else
811  ics->num_window_groups++;
812  }
813 
814  ics->group_len[7] = 1;
815  ics->num_window_groups++;
816  ics->num_windows = 8;
817  } else {
818  if (usac->core_frame_len == 768) {
819  ics->swb_offset = ff_swb_offset_768[sampling_index];
820  ics->num_swb = ff_aac_num_swb_768[sampling_index];
821  } else {
822  ics->swb_offset = ff_swb_offset_1024[sampling_index];
823  ics->num_swb = ff_aac_num_swb_1024[sampling_index];
824  }
825  ics->tns_max_bands = ff_tns_max_bands_usac_1024[sampling_index];
826 
827  ics->group_len[0] = 1;
828  ics->num_window_groups = 1;
829  ics->num_windows = 1;
830  }
831 
832  if (ics->max_sfb > ics->num_swb) {
834  "Number of scalefactor bands in group (%d) "
835  "exceeds limit (%d).\n",
836  ics->max_sfb, ics->num_swb);
837  ics->max_sfb = 0;
838  return AVERROR(EINVAL);
839  }
840 
841  /* Just some defaults for the band types */
842  for (int i = 0; i < FF_ARRAY_ELEMS(sce->band_type); i++)
843  sce->band_type[i] = ESC_BT;
844 
845  return 0;
846 }
847 
850  GetBitContext *gb, int indep_flag)
851 {
852  int ret, tns_active;
853 
854  AACUsacStereo *us = &cpe->us;
855  SingleChannelElement *sce1 = &cpe->ch[0];
856  SingleChannelElement *sce2 = &cpe->ch[1];
857  IndividualChannelStream *ics1 = &sce1->ics;
858  IndividualChannelStream *ics2 = &sce2->ics;
859  AACUsacElemData *ue1 = &sce1->ue;
860  AACUsacElemData *ue2 = &sce2->ue;
861 
862  us->common_window = 0;
863  us->common_tw = 0;
864 
865  /* Alpha values must always be zeroed out for the current frame,
866  * as they are propagated to the next frame and may be used. */
867  memset(us->alpha_q_re, 0, sizeof(us->alpha_q_re));
868  memset(us->alpha_q_im, 0, sizeof(us->alpha_q_im));
869 
870  if (!(!ue1->core_mode && !ue2->core_mode))
871  return 0;
872 
873  tns_active = get_bits1(gb);
874  us->common_window = get_bits1(gb);
875 
876  if (!us->common_window || indep_flag) {
877  memset(us->prev_alpha_q_re, 0, sizeof(us->prev_alpha_q_re));
878  memset(us->prev_alpha_q_im, 0, sizeof(us->prev_alpha_q_im));
879  }
880 
881  if (us->common_window) {
882  /* ics_info() */
883  ics1->window_sequence[1] = ics1->window_sequence[0];
884  ics2->window_sequence[1] = ics2->window_sequence[0];
885  ics1->window_sequence[0] = ics2->window_sequence[0] = get_bits(gb, 2);
886 
887  ics1->use_kb_window[1] = ics1->use_kb_window[0];
888  ics2->use_kb_window[1] = ics2->use_kb_window[0];
889  ics1->use_kb_window[0] = ics2->use_kb_window[0] = get_bits1(gb);
890 
891  /* If there's a change in the transform sequence, zero out last frame's
892  * stereo prediction coefficients */
893  if ((ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE &&
894  ics1->window_sequence[1] != EIGHT_SHORT_SEQUENCE) ||
895  (ics1->window_sequence[1] == EIGHT_SHORT_SEQUENCE &&
896  ics1->window_sequence[0] != EIGHT_SHORT_SEQUENCE) ||
897  (ics2->window_sequence[0] == EIGHT_SHORT_SEQUENCE &&
898  ics2->window_sequence[1] != EIGHT_SHORT_SEQUENCE) ||
899  (ics2->window_sequence[1] == EIGHT_SHORT_SEQUENCE &&
900  ics2->window_sequence[0] != EIGHT_SHORT_SEQUENCE)) {
901  memset(us->prev_alpha_q_re, 0, sizeof(us->prev_alpha_q_re));
902  memset(us->prev_alpha_q_im, 0, sizeof(us->prev_alpha_q_im));
903  }
904 
905  if (ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
906  ics1->max_sfb = ics2->max_sfb = get_bits(gb, 4);
908  } else {
909  ics1->max_sfb = ics2->max_sfb = get_bits(gb, 6);
910  }
911 
912  if (!get_bits1(gb)) { /* common_max_sfb */
913  if (ics2->window_sequence[0] == EIGHT_SHORT_SEQUENCE)
914  ics2->max_sfb = get_bits(gb, 4);
915  else
916  ics2->max_sfb = get_bits(gb, 6);
917  }
918 
919  ret = setup_sce(ac, sce1, usac);
920  if (ret < 0) {
921  ics2->max_sfb = 0;
922  return ret;
923  }
924 
925  ret = setup_sce(ac, sce2, usac);
926  if (ret < 0)
927  return ret;
928 
929  cpe->max_sfb_ste = FFMAX(ics1->max_sfb, ics2->max_sfb);
930 
931  us->ms_mask_mode = get_bits(gb, 2); /* ms_mask_present */
932  memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
933  if (us->ms_mask_mode == 1) {
934  for (int g = 0; g < ics1->num_window_groups; g++)
935  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++)
936  cpe->ms_mask[g*cpe->max_sfb_ste + sfb] = get_bits1(gb);
937  } else if (us->ms_mask_mode == 2) {
938  memset(cpe->ms_mask, 0xFF, sizeof(cpe->ms_mask));
939  } else if ((us->ms_mask_mode == 3) && !ec->stereo_config_index) {
940  ret = decode_usac_stereo_cplx(ac, us, cpe, gb,
941  ics1->num_window_groups,
943  indep_flag);
944  if (ret < 0)
945  return ret;
946  }
947  }
948 
949  if (ec->tw_mdct) {
950  us->common_tw = get_bits1(gb);
952  "AAC USAC timewarping");
953  return AVERROR_PATCHWELCOME;
954  }
955 
956  us->tns_on_lr = 0;
957  ue1->tns_data_present = ue2->tns_data_present = 0;
958  if (tns_active) {
959  int common_tns = 0;
960  if (us->common_window)
961  common_tns = get_bits1(gb);
962 
963  us->tns_on_lr = get_bits1(gb);
964  if (common_tns) {
965  ret = ff_aac_decode_tns(ac, &sce1->tns, gb, ics1);
966  if (ret < 0)
967  return ret;
968  memcpy(&sce2->tns, &sce1->tns, sizeof(sce1->tns));
969  sce2->tns.present = 1;
970  sce1->tns.present = 1;
971  ue1->tns_data_present = 0;
972  ue2->tns_data_present = 0;
973  } else {
974  if (get_bits1(gb)) {
975  ue1->tns_data_present = 1;
976  ue2->tns_data_present = 1;
977  } else {
978  ue2->tns_data_present = get_bits1(gb);
979  ue1->tns_data_present = !ue2->tns_data_present;
980  }
981  }
982  }
983 
984  return 0;
985 }
986 
987 /* 7.2.4 Generation of random signs for spectral noise filling
988  * This function is exactly defined, though we've helped the definition
989  * along with being slightly faster. */
990 static inline float noise_random_sign(unsigned int *seed)
991 {
992  unsigned int new_seed = *seed = ((*seed) * 69069) + 5;
993  if (((new_seed) & 0x10000) > 0)
994  return -1.f;
995  return +1.f;
996 }
997 
1000 {
1001  float *coef;
1002  IndividualChannelStream *ics = &sce->ics;
1003 
1004  float noise_val = powf(2, ((float)ue->noise.level - 14.0f)/3.0f);
1005  int noise_offset = ue->noise.offset - 16;
1006  int band_off;
1007 
1010 
1011  coef = sce->coeffs;
1012  for (int g = 0; g < ics->num_window_groups; g++) {
1013  unsigned g_len = ics->group_len[g];
1014 
1015  for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1016  float *cb = coef + ics->swb_offset[sfb];
1017  int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb];
1018  int band_quantized_to_zero = 1;
1019 
1020  if (ics->swb_offset[sfb] < band_off)
1021  continue;
1022 
1023  for (int group = 0; group < (unsigned)g_len; group++, cb += 128) {
1024  for (int z = 0; z < cb_len; z++) {
1025  if (cb[z] == 0)
1026  cb[z] = noise_random_sign(&sce->ue.noise.seed) * noise_val;
1027  else
1028  band_quantized_to_zero = 0;
1029  }
1030  }
1031 
1032  if (band_quantized_to_zero) {
1033  sce->sfo[g*ics->max_sfb + sfb] = FFMAX(sce->sfo[g*ics->max_sfb + sfb] + noise_offset, -200);
1034  }
1035  }
1036  coef += g_len << 7;
1037  }
1038 }
1039 
1042 {
1043  IndividualChannelStream *ics = &sce->ics;
1044  float *coef;
1045 
1046  /* Synthesise noise */
1047  if (ue->noise.level)
1048  apply_noise_fill(ac, sce, ue);
1049 
1050  /* Noise filling may apply an offset to the scalefactor offset */
1051  ac->dsp.dequant_scalefactors(sce);
1052 
1053  /* Apply scalefactors */
1054  coef = sce->coeffs;
1055  for (int g = 0; g < ics->num_window_groups; g++) {
1056  unsigned g_len = ics->group_len[g];
1057 
1058  for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1059  float *cb = coef + ics->swb_offset[sfb];
1060  int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb];
1061  float sf = sce->sf[g*ics->max_sfb + sfb];
1062 
1063  for (int group = 0; group < (unsigned)g_len; group++, cb += 128)
1064  ac->fdsp->vector_fmul_scalar(cb, cb, sf, cb_len);
1065  }
1066  coef += g_len << 7;
1067  }
1068 }
1069 
1071  float *dmix_re)
1072 {
1073  IndividualChannelStream *ics = &cpe->ch[0].ics;
1074  int sign = !cpe->us.pred_dir ? +1 : -1;
1075  float *coef1 = cpe->ch[0].coeffs;
1076  float *coef2 = cpe->ch[1].coeffs;
1077 
1078  for (int g = 0; g < ics->num_window_groups; g++) {
1079  unsigned g_len = ics->group_len[g];
1080  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1081  int off = ics->swb_offset[sfb];
1082  int cb_len = ics->swb_offset[sfb + 1] - off;
1083 
1084  float *c1 = coef1 + off;
1085  float *c2 = coef2 + off;
1086  float *dm = dmix_re + off;
1087 
1088  for (int group = 0; group < (unsigned)g_len;
1089  group++, c1 += 128, c2 += 128, dm += 128) {
1090  for (int z = 0; z < cb_len; z++)
1091  dm[z] = 0.5*(c1[z] + sign*c2[z]);
1092  }
1093  }
1094 
1095  coef1 += g_len << 7;
1096  coef2 += g_len << 7;
1097  dmix_re += g_len << 7;
1098  }
1099 }
1100 
1102  float *dmix_re)
1103 {
1104  AACUsacStereo *us = &cpe->us;
1105  IndividualChannelStream *ics = &cpe->ch[0].ics;
1106  int sign = !cpe->us.pred_dir ? +1 : -1;
1107  float *coef1 = cpe->ch[0].coeffs;
1108  float *coef2 = cpe->ch[1].coeffs;
1109 
1110  for (int g = 0; g < ics->num_window_groups; g++) {
1111  unsigned g_len = ics->group_len[g];
1112  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1113  int off = ics->swb_offset[sfb];
1114  int cb_len = ics->swb_offset[sfb + 1] - off;
1115 
1116  float *c1 = coef1 + off;
1117  float *c2 = coef2 + off;
1118  float *dm = dmix_re + off;
1119 
1120  if (us->pred_used[g*cpe->max_sfb_ste + sfb]) {
1121  for (int group = 0; group < (unsigned)g_len;
1122  group++, c1 += 128, c2 += 128, dm += 128) {
1123  for (int z = 0; z < cb_len; z++)
1124  dm[z] = 0.5*(c1[z] + sign*c2[z]);
1125  }
1126  } else {
1127  for (int group = 0; group < (unsigned)g_len;
1128  group++, c1 += 128, c2 += 128, dm += 128) {
1129  for (int z = 0; z < cb_len; z++)
1130  dm[z] = c1[z];
1131  }
1132  }
1133  }
1134 
1135  coef1 += g_len << 7;
1136  coef2 += g_len << 7;
1137  dmix_re += g_len << 7;
1138  }
1139 }
1140 
1141 static void complex_stereo_interpolate_imag(float *im, float *re, const float f[7],
1142  int len, int factor_even, int factor_odd)
1143 {
1144  int i = 0;
1145  float s;
1146 
1147  s = f[6]*re[2] + f[5]*re[1] + f[4]*re[0] +
1148  f[3]*re[0] +
1149  f[2]*re[1] + f[1]*re[2] + f[0]*re[3];
1150  im[i] += s*factor_even;
1151 
1152  i = 1;
1153  s = f[6]*re[1] + f[5]*re[0] + f[4]*re[0] +
1154  f[3]*re[1] +
1155  f[2]*re[2] + f[1]*re[3] + f[0]*re[4];
1156  im[i] += s*factor_odd;
1157 
1158  i = 2;
1159  s = f[6]*re[0] + f[5]*re[0] + f[4]*re[1] +
1160  f[3]*re[2] +
1161  f[2]*re[3] + f[1]*re[4] + f[0]*re[5];
1162 
1163  im[i] += s*factor_even;
1164  for (i = 3; i < len - 4; i += 2) {
1165  s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1166  f[3]*re[i] +
1167  f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+3];
1168  im[i+0] += s*factor_odd;
1169 
1170  s = f[6]*re[i-2] + f[5]*re[i-1] + f[4]*re[i] +
1171  f[3]*re[i+1] +
1172  f[2]*re[i+2] + f[1]*re[i+3] + f[0]*re[i+4];
1173  im[i+1] += s*factor_even;
1174  }
1175 
1176  i = len - 3;
1177  s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1178  f[3]*re[i] +
1179  f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+2];
1180  im[i] += s*factor_odd;
1181 
1182  i = len - 2;
1183  s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1184  f[3]*re[i] +
1185  f[2]*re[i+1] + f[1]*re[i+1] + f[0]*re[i];
1186  im[i] += s*factor_even;
1187 
1188  i = len - 1;
1189  s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1190  f[3]*re[i] +
1191  f[2]*re[i] + f[1]*re[i-1] + f[0]*re[i-2];
1192  im[i] += s*factor_odd;
1193 }
1194 
1196 {
1197  AACUsacStereo *us = &cpe->us;
1198  IndividualChannelStream *ics = &cpe->ch[0].ics;
1199  float *coef1 = cpe->ch[0].coeffs;
1200  float *coef2 = cpe->ch[1].coeffs;
1201  float *dmix_im = us->dmix_im;
1202 
1203  for (int g = 0; g < ics->num_window_groups; g++) {
1204  unsigned g_len = ics->group_len[g];
1205  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1206  int off = ics->swb_offset[sfb];
1207  int cb_len = ics->swb_offset[sfb + 1] - off;
1208 
1209  float *c1 = coef1 + off;
1210  float *c2 = coef2 + off;
1211  float *dm_im = dmix_im + off;
1212  float alpha_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb];
1213  float alpha_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb];
1214 
1215  if (!us->pred_used[g*cpe->max_sfb_ste + sfb])
1216  continue;
1217 
1218  if (!cpe->us.pred_dir) {
1219  for (int group = 0; group < (unsigned)g_len;
1220  group++, c1 += 128, c2 += 128, dm_im += 128) {
1221  for (int z = 0; z < cb_len; z++) {
1222  float side;
1223  side = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z];
1224  c2[z] = c1[z] - side;
1225  c1[z] = c1[z] + side;
1226  }
1227  }
1228  } else {
1229  for (int group = 0; group < (unsigned)g_len;
1230  group++, c1 += 128, c2 += 128, dm_im += 128) {
1231  for (int z = 0; z < cb_len; z++) {
1232  float mid;
1233  mid = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z];
1234  c2[z] = mid - c1[z];
1235  c1[z] = mid + c1[z];
1236  }
1237  }
1238  }
1239  }
1240 
1241  coef1 += g_len << 7;
1242  coef2 += g_len << 7;
1243  dmix_im += g_len << 7;
1244  }
1245 }
1246 
1247 static const float *complex_stereo_get_filter(ChannelElement *cpe, int is_prev)
1248 {
1249  int win, shape;
1250  if (!is_prev) {
1251  switch (cpe->ch[0].ics.window_sequence[0]) {
1252  default:
1253  case ONLY_LONG_SEQUENCE:
1254  case EIGHT_SHORT_SEQUENCE:
1255  win = 0;
1256  break;
1257  case LONG_START_SEQUENCE:
1258  win = 1;
1259  break;
1260  case LONG_STOP_SEQUENCE:
1261  win = 2;
1262  break;
1263  }
1264 
1265  if (cpe->ch[0].ics.use_kb_window[0] == 0 &&
1266  cpe->ch[0].ics.use_kb_window[1] == 0)
1267  shape = 0;
1268  else if (cpe->ch[0].ics.use_kb_window[0] == 1 &&
1269  cpe->ch[0].ics.use_kb_window[1] == 1)
1270  shape = 1;
1271  else if (cpe->ch[0].ics.use_kb_window[0] == 0 &&
1272  cpe->ch[0].ics.use_kb_window[1] == 1)
1273  shape = 2;
1274  else if (cpe->ch[0].ics.use_kb_window[0] == 1 &&
1275  cpe->ch[0].ics.use_kb_window[1] == 0)
1276  shape = 3;
1277  else
1278  shape = 3;
1279  } else {
1280  win = cpe->ch[0].ics.window_sequence[0] == LONG_STOP_SEQUENCE;
1281  shape = cpe->ch[0].ics.use_kb_window[1];
1282  }
1283 
1284  return ff_aac_usac_mdst_filt_cur[win][shape];
1285 }
1286 
1288  ChannelElement *cpe, int nb_channels)
1289 {
1290  AACUsacStereo *us = &cpe->us;
1291 
1292  for (int ch = 0; ch < nb_channels; ch++) {
1293  SingleChannelElement *sce = &cpe->ch[ch];
1294  AACUsacElemData *ue = &sce->ue;
1295 
1296  spectrum_scale(ac, sce, ue);
1297  }
1298 
1299  if (nb_channels > 1 && us->common_window) {
1300  for (int ch = 0; ch < nb_channels; ch++) {
1301  SingleChannelElement *sce = &cpe->ch[ch];
1302 
1303  /* Apply TNS, if the tns_on_lr bit is not set. */
1304  if (sce->tns.present && !us->tns_on_lr)
1305  ac->dsp.apply_tns(sce->coeffs, &sce->tns, &sce->ics, 1);
1306  }
1307 
1308  if (us->ms_mask_mode == 3) {
1309  const float *filt;
1310  complex_stereo_downmix_cur(ac, cpe, us->dmix_re);
1311  complex_stereo_downmix_prev(ac, cpe, us->prev_dmix_re);
1312 
1313  filt = complex_stereo_get_filter(cpe, 0);
1314  complex_stereo_interpolate_imag(us->dmix_im, us->dmix_re, filt,
1315  usac->core_frame_len, 1, 1);
1316  if (us->use_prev_frame) {
1317  filt = complex_stereo_get_filter(cpe, 1);
1318  complex_stereo_interpolate_imag(us->dmix_im, us->prev_dmix_re, filt,
1319  usac->core_frame_len, -1, 1);
1320  }
1321 
1322  apply_complex_stereo(ac, cpe);
1323  } else if (us->ms_mask_mode > 0) {
1324  ac->dsp.apply_mid_side_stereo(ac, cpe);
1325  }
1326  }
1327 
1328  /* Save coefficients and alpha values for prediction reasons */
1329  if (nb_channels > 1) {
1330  AACUsacStereo *us = &cpe->us;
1331  for (int ch = 0; ch < nb_channels; ch++) {
1332  SingleChannelElement *sce = &cpe->ch[ch];
1333  memcpy(sce->prev_coeffs, sce->coeffs, sizeof(sce->coeffs));
1334  }
1335  memcpy(us->prev_alpha_q_re, us->alpha_q_re, sizeof(us->alpha_q_re));
1336  memcpy(us->prev_alpha_q_im, us->alpha_q_im, sizeof(us->alpha_q_im));
1337  }
1338 
1339  for (int ch = 0; ch < nb_channels; ch++) {
1340  SingleChannelElement *sce = &cpe->ch[ch];
1341 
1342  /* Apply TNS, if it hasn't been applied yet. */
1343  if (sce->tns.present && ((nb_channels == 1) || (us->tns_on_lr)))
1344  ac->dsp.apply_tns(sce->coeffs, &sce->tns, &sce->ics, 1);
1345 
1346  ac->oc[1].m4ac.frame_length_short ? ac->dsp.imdct_and_windowing_768(ac, sce) :
1347  ac->dsp.imdct_and_windowing(ac, sce);
1348  }
1349 }
1350 
1351 static const uint8_t mps_fr_nb_bands[8] = {
1352  255 /* Reserved */, 28, 20, 14, 10, 7, 5, 4,
1353 };
1354 
1355 static const uint8_t mps_fr_stride_smg[4] = {
1356  1, 2, 5, 28,
1357 };
1358 
1359 static void decode_tsd(GetBitContext *gb, int *data,
1360  int nb_tr_slots, int nb_slots)
1361 {
1362  int nb_bits = av_log2(nb_slots / (nb_tr_slots + 1));
1363  int s = get_bits(gb, nb_bits);
1364  for (int k = 0; k < nb_slots; k++)
1365  data[k]=0;
1366 
1367  int p = nb_tr_slots + 1;
1368  for (int k = nb_slots - 1; k >= 0; k--) {
1369  if (p > k) {
1370  for (; k >= 0; k--)
1371  data[k] = 1;
1372  break;
1373  }
1374  int64_t c = k - p + 1;
1375  for (int h = 2; h <= p; h++) {
1376  c *= k - p + h;
1377  c /= h;
1378  }
1379  if (s >= (int)c) { /* c is long long for up to 32 slots */
1380  s -= c;
1381  data[k] = 1;
1382  p--;
1383  if (!p)
1384  break;
1385  }
1386  }
1387 }
1388 
1391  GetBitContext *gb, int frame_indep_flag)
1392 {
1393  int err;
1394  int nb_bands = mps_fr_nb_bands[ec->mps.freq_res];
1395 
1396  /* Framing info */
1397  mps->framing_type = 0;
1398  mps->nb_param_sets = 2;
1399  if (ec->mps.high_rate_mode) {
1400  mps->framing_type = get_bits1(gb);
1401  mps->nb_param_sets = get_bits(gb, 3) + 1;
1402  }
1403  int param_slot_bits = usac->core_sbr_frame_len_idx == 4 ? 6 : 5;
1404  int nb_time_slots = usac->core_sbr_frame_len_idx == 4 ? 64 : 32;
1405 
1406  if (mps->framing_type)
1407  for (int i = 0; i < mps->nb_param_sets; i++)
1408  mps->param_sets[i] = get_bits(gb, param_slot_bits);
1409 
1410  int indep = frame_indep_flag;
1411  if (!frame_indep_flag)
1412  indep = get_bits1(gb);
1413 
1414  int extend_frame = mps->param_sets[mps->nb_param_sets - 1] !=
1415  (nb_time_slots - 1);
1416 
1417  /* CLD */
1418  err = ff_aac_ec_data_dec(gb, &mps->ott[MPS_CLD], MPS_CLD,
1419  0, 0, nb_bands,
1420  indep, indep, mps->nb_param_sets);
1421  if (err < 0) {
1422  av_log(ac->avctx, AV_LOG_ERROR, "Error parsing OTT CLD data!\n");
1423  return err;
1424  }
1426  0, 0, nb_bands, mps->nb_param_sets,
1427  mps->param_sets, extend_frame);
1428 
1429  /* ICC */
1430  err = ff_aac_ec_data_dec(gb, &mps->ott[MPS_ICC], MPS_ICC, 0, 0, nb_bands,
1431  indep, indep, mps->nb_param_sets);
1432  if (err < 0) {
1433  av_log(ac->avctx, AV_LOG_ERROR, "Error parsing OTT ICC data!\n");
1434  return err;
1435  }
1437  0, 0, nb_bands, mps->nb_param_sets,
1438  mps->param_sets, extend_frame);
1439 
1440  /* IPD */
1441  if (ec->mps.phase_coding) {
1442  if (get_bits1(gb)) {
1443  mps->opd_smoothing_mode = get_bits1(gb);
1444  err = ff_aac_ec_data_dec(gb, &mps->ott[MPS_IPD], MPS_IPD, 0, 0,
1445  ec->mps.otts_bands_phase,
1446  indep, indep, mps->nb_param_sets);
1448  0, 0, nb_bands, mps->nb_param_sets,
1449  mps->param_sets, extend_frame);
1450  if (err < 0) {
1451  av_log(ac->avctx, AV_LOG_ERROR, "Error parsing OTT IPD data!\n");
1452  return err;
1453  }
1454  }
1455  }
1456 
1457  /* SMG data */
1458  memset(mps->smooth_mode, 0, sizeof(mps->smooth_mode));
1459  if (ec->mps.high_rate_mode) {
1460  for (int i = 0; i < mps->nb_param_sets; i++) {
1461  mps->smooth_mode[i] = get_bits(gb, 2);
1462  if (mps->smooth_mode[i] >= 2)
1463  mps->smooth_time[i] = get_bits(gb, 2);
1464  if (mps->smooth_mode[i] >= 3) {
1465  mps->freq_res_stride_smg[i] = get_bits(gb, 2);
1466  int nb_data_bands = (nb_bands - 1);
1467  nb_data_bands /= (mps_fr_stride_smg[mps->freq_res_stride_smg[i]] + 1);
1468  for (int j = 0; j < nb_data_bands; j++)
1469  mps->smg_data[i][j] = get_bits1(gb);
1470  }
1471  }
1472  }
1473 
1474  /* Temp shape data */
1475  mps->tsd_enable = 0;
1476  if (ec->mps.temp_shape_config == 3) {
1477  mps->tsd_enable = get_bits1(gb);
1478  } else if (ec->mps.temp_shape_config) {
1479  mps->temp_shape_enable = get_bits1(gb);
1480  if (mps->temp_shape_enable) {
1481  for (int i = 0; i < 2; i++)
1482  mps->temp_shape_enable_ch[i] = get_bits1(gb);
1483  if (ec->mps.temp_shape_config == 2) {
1484  err = ff_aac_huff_dec_reshape(gb, mps->temp_shape_data, 16);
1485  if (err < 0) {
1486  av_log(ac->avctx, AV_LOG_ERROR,
1487  "Error parsing TSD reshape data!\n");
1488  return err;
1489  }
1490  }
1491  }
1492  }
1493 
1494  /* TSD data */
1495  if (mps->tsd_enable) {
1496  mps->tsd_num_tr_slots = get_bits(gb, param_slot_bits - 1);
1497  int tsd_pos[64];
1498  decode_tsd(gb, tsd_pos, mps->tsd_num_tr_slots, nb_time_slots);
1499  for (int i = 0; i < nb_time_slots; i++) {
1500  mps->tsd_phase_data[i] = 0;
1501  if (tsd_pos[i])
1502  mps->tsd_phase_data[i] = get_bits(gb, 3);
1503  }
1504  }
1505 
1506  return 0;
1507 }
1508 
1511  GetBitContext *gb, int indep_flag, int nb_channels)
1512 {
1513  int ret;
1514  int arith_reset_flag;
1515  AACUsacStereo *us = &che->us;
1516  int core_nb_channels = nb_channels;
1517 
1518  /* Local symbols */
1519  uint8_t global_gain;
1520 
1521  us->common_window = 0;
1522 
1523  for (int ch = 0; ch < core_nb_channels; ch++) {
1524  SingleChannelElement *sce = &che->ch[ch];
1525  AACUsacElemData *ue = &sce->ue;
1526 
1527  sce->tns.present = 0;
1528  ue->tns_data_present = 0;
1529 
1530  ue->core_mode = get_bits1(gb);
1531  }
1532 
1533  if (nb_channels > 1 && ec->stereo_config_index == 1)
1534  core_nb_channels = 1;
1535 
1536  if (core_nb_channels == 2) {
1537  ret = decode_usac_stereo_info(ac, usac, ec, che, gb, indep_flag);
1538  if (ret)
1539  return ret;
1540  }
1541 
1542  for (int ch = 0; ch < core_nb_channels; ch++) {
1543  SingleChannelElement *sce = &che->ch[ch];
1544  IndividualChannelStream *ics = &sce->ics;
1545  AACUsacElemData *ue = &sce->ue;
1546 
1547  if (ue->core_mode) { /* lpd_channel_stream */
1548  ret = ff_aac_ldp_parse_channel_stream(ac, usac, ue, gb);
1549  if (ret < 0)
1550  return ret;
1551  continue;
1552  }
1553 
1554  if ((core_nb_channels == 1) ||
1555  (che->ch[0].ue.core_mode != che->ch[1].ue.core_mode))
1556  ue->tns_data_present = get_bits1(gb);
1557 
1558  /* fd_channel_stream */
1559  global_gain = get_bits(gb, 8);
1560 
1561  ue->noise.level = 0;
1562  if (ec->noise_fill) {
1563  ue->noise.level = get_bits(gb, 3);
1564  ue->noise.offset = get_bits(gb, 5);
1565  }
1566 
1567  if (!us->common_window) {
1568  /* ics_info() */
1569  ics->window_sequence[1] = ics->window_sequence[0];
1570  ics->window_sequence[0] = get_bits(gb, 2);
1571  ics->use_kb_window[1] = ics->use_kb_window[0];
1572  ics->use_kb_window[0] = get_bits1(gb);
1573  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1574  ics->max_sfb = get_bits(gb, 4);
1575  ue->scale_factor_grouping = get_bits(gb, 7);
1576  } else {
1577  ics->max_sfb = get_bits(gb, 6);
1578  }
1579 
1580  ret = setup_sce(ac, sce, usac);
1581  if (ret < 0)
1582  return ret;
1583  }
1584 
1585  if (ec->tw_mdct && !us->common_tw) {
1586  /* tw_data() */
1587  if (get_bits1(gb)) { /* tw_data_present */
1588  /* Time warping is not supported in baseline profile streams. */
1590  "AAC USAC timewarping");
1591  return AVERROR_PATCHWELCOME;
1592  }
1593  }
1594 
1595  ret = decode_usac_scale_factors(ac, sce, gb, global_gain);
1596  if (ret < 0)
1597  return ret;
1598 
1599  if (ue->tns_data_present) {
1600  sce->tns.present = 1;
1601  ret = ff_aac_decode_tns(ac, &sce->tns, gb, ics);
1602  if (ret < 0)
1603  return ret;
1604  }
1605 
1606  /* ac_spectral_data */
1607  arith_reset_flag = indep_flag;
1608  if (!arith_reset_flag)
1609  arith_reset_flag = get_bits1(gb);
1610 
1611  /* Decode coeffs */
1612  memset(&sce->coeffs[0], 0, 1024*sizeof(float));
1613  for (int win = 0; win < ics->num_windows; win++) {
1614  int lg = ics->swb_offset[ics->max_sfb];
1615  int N;
1616  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE)
1617  N = usac->core_frame_len / 8;
1618  else
1619  N = usac->core_frame_len;
1620 
1621  ret = decode_spectrum_ac(ac, sce->coeffs + win*128, gb, &ue->ac,
1622  arith_reset_flag && (win == 0), lg, N);
1623  if (ret < 0)
1624  return ret;
1625  }
1626 
1627  if (get_bits1(gb)) { /* fac_data_present */
1628  const uint16_t len_8 = usac->core_frame_len / 8;
1629  const uint16_t len_16 = usac->core_frame_len / 16;
1630  const uint16_t fac_len = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ?
1631  len_16 : len_8;
1632  ret = ff_aac_parse_fac_data(ue, gb, 1, fac_len);
1633  if (ret < 0)
1634  return ret;
1635  }
1636  }
1637 
1638  if (ec->sbr.ratio) {
1639  int sbr_ch = nb_channels;
1640  if (nb_channels == 2 &&
1641  !(ec->stereo_config_index == 0 || ec->stereo_config_index == 3))
1642  sbr_ch = 1;
1643 
1644  ret = ff_aac_sbr_decode_usac_data(ac, che, ec, gb, sbr_ch, indep_flag);
1645  if (ret < 0)
1646  return ret;
1647  }
1648 
1649  if (ec->stereo_config_index) {
1650  ret = parse_mps212(ac, usac, &us->mps, ec, gb, indep_flag);
1651  if (ret < 0)
1652  return ret;
1653  }
1654 
1655  spectrum_decode(ac, usac, che, core_nb_channels);
1656 
1657  if (ac->oc[1].m4ac.sbr > 0) {
1658  ac->proc.sbr_apply(ac, che, nb_channels == 2 ? TYPE_CPE : TYPE_SCE,
1659  che->ch[0].output,
1660  che->ch[1].output);
1661  }
1662 
1663  return 0;
1664 }
1665 
1667 {
1668  int ret = 0;
1669  GetBitContext gbc;
1670  OutputConfiguration *oc = &ac->oc[1];
1671  MPEG4AudioConfig *m4ac = &oc->m4ac;
1672  MPEG4AudioConfig m4ac_bak = oc->m4ac;
1673  uint8_t temp_data[512];
1674  uint8_t *tmp_buf = temp_data;
1675  size_t tmp_buf_size = sizeof(temp_data);
1676 
1677  av_unused int crossfade;
1678  int num_preroll_frames;
1679 
1680  int config_len = get_escaped_value(gb, 4, 4, 8);
1681 
1682  /* Implementations are free to pad the config to any length, so use a
1683  * different reader for this. */
1684  gbc = *gb;
1685  ret = ff_aac_usac_config_decode(ac, ac->avctx, &gbc, oc, m4ac->chan_config);
1686  if (ret < 0) {
1687  *m4ac = m4ac_bak;
1688  return ret;
1689  } else {
1690  ac->oc[1].m4ac.chan_config = 0;
1691  }
1692 
1693  /* 7.18.3.3 Bitrate adaption
1694  * If configuration didn't change after applying preroll, continue
1695  * without decoding it. */
1696  if (!memcmp(m4ac, &m4ac_bak, sizeof(m4ac_bak)))
1697  return 0;
1698 
1699  skip_bits_long(gb, config_len*8);
1700 
1701  crossfade = get_bits1(gb); /* applyCrossfade */
1702  skip_bits1(gb); /* reserved */
1703  num_preroll_frames = get_escaped_value(gb, 2, 4, 0); /* numPreRollFrames */
1704 
1705  for (int i = 0; i < num_preroll_frames; i++) {
1706  int got_frame_ptr = 0;
1707  int au_len = get_escaped_value(gb, 16, 16, 0);
1708 
1709  if (au_len*8 > tmp_buf_size) {
1710  uint8_t *tmp2;
1711  tmp_buf = tmp_buf == temp_data ? NULL : tmp_buf;
1712  tmp2 = av_realloc_array(tmp_buf, au_len, 8);
1713  if (!tmp2) {
1714  if (tmp_buf != temp_data)
1715  av_free(tmp_buf);
1716  return AVERROR(ENOMEM);
1717  }
1718  tmp_buf = tmp2;
1719  }
1720 
1721  /* Byte alignment is not guaranteed. */
1722  for (int i = 0; i < au_len; i++)
1723  tmp_buf[i] = get_bits(gb, 8);
1724 
1725  ret = init_get_bits8(&gbc, tmp_buf, au_len);
1726  if (ret < 0)
1727  break;
1728 
1729  ret = ff_aac_usac_decode_frame(ac->avctx, ac, &gbc, &got_frame_ptr);
1730  if (ret < 0)
1731  break;
1732  }
1733 
1734  if (tmp_buf != temp_data)
1735  av_free(tmp_buf);
1736 
1737  return 0;
1738 }
1739 
1741  GetBitContext *gb)
1742 {
1743  uint8_t pl_frag_start = 1;
1744  uint8_t pl_frag_end = 1;
1745  uint32_t len;
1746 
1747  if (!get_bits1(gb)) /* usacExtElementPresent */
1748  return 0;
1749 
1750  if (get_bits1(gb)) { /* usacExtElementUseDefaultLength */
1751  len = e->ext.default_len;
1752  } else {
1753  len = get_bits(gb, 8); /* usacExtElementPayloadLength */
1754  if (len == 255)
1755  len += get_bits(gb, 16) - 2;
1756  }
1757 
1758  if (!len)
1759  return 0;
1760 
1761  if (e->ext.payload_frag) {
1762  pl_frag_start = get_bits1(gb); /* usacExtElementStart */
1763  pl_frag_end = get_bits1(gb); /* usacExtElementStop */
1764  }
1765 
1766  if (pl_frag_start)
1767  e->ext.pl_data_offset = 0;
1768 
1769  /* If an extension starts and ends this packet, we can directly use it below.
1770  * Otherwise, we have to copy it to a buffer and accumulate it. */
1771  if (!(pl_frag_start && pl_frag_end)) {
1772  /* Reallocate the data */
1773  uint8_t *tmp_buf = av_refstruct_alloc_ext(e->ext.pl_data_offset + len,
1775  NULL, NULL);
1776  if (!tmp_buf)
1777  return AVERROR(ENOMEM);
1778 
1779  /* Copy the data over only if we had saved data to begin with */
1780  if (e->ext.pl_buf)
1781  memcpy(tmp_buf, e->ext.pl_buf, e->ext.pl_data_offset);
1782 
1784  e->ext.pl_buf = tmp_buf;
1785 
1786  /* Readout data to a buffer */
1787  for (int i = 0; i < len; i++)
1788  e->ext.pl_buf[e->ext.pl_data_offset + i] = get_bits(gb, 8);
1789  }
1790 
1791  e->ext.pl_data_offset += len;
1792 
1793  if (pl_frag_end) {
1794  int ret = 0;
1795  int start_bits = get_bits_count(gb);
1796  const int pl_len = e->ext.pl_data_offset;
1797  GetBitContext *gb2 = gb;
1798  GetBitContext gbc;
1799  if (!(pl_frag_start && pl_frag_end)) {
1800  ret = init_get_bits8(&gbc, e->ext.pl_buf, pl_len);
1801  if (ret < 0)
1802  return ret;
1803 
1804  gb2 = &gbc;
1805  }
1806 
1807  switch (e->ext.type) {
1808  case ID_EXT_ELE_FILL:
1809  /* Filler elements have no usable payload */
1810  break;
1812  ret = parse_audio_preroll(ac, gb2);
1813  break;
1814  default:
1815  /* This should never happen */
1816  av_assert0(0);
1817  }
1819  if (ret < 0)
1820  return ret;
1821 
1822  skip_bits_long(gb, pl_len*8 - (get_bits_count(gb) - start_bits));
1823  }
1824 
1825  return 0;
1826 }
1827 
1829  GetBitContext *gb, int *got_frame_ptr)
1830 {
1831  int ret, is_dmono = 0;
1832  int indep_flag, samples = 0;
1833  int audio_found = 0;
1834  int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
1835  AVFrame *frame = ac->frame;
1836 
1837  int ratio_mult, ratio_dec;
1838  AACUSACConfig *usac = &ac->oc[1].usac;
1839  int sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
1840  usac->core_sbr_frame_len_idx == 3 ? 3 :
1841  usac->core_sbr_frame_len_idx == 4 ? 1 :
1842  0;
1843 
1844  if (sbr_ratio == 2) {
1845  ratio_mult = 8;
1846  ratio_dec = 3;
1847  } else if (sbr_ratio == 3) {
1848  ratio_mult = 2;
1849  ratio_dec = 1;
1850  } else if (sbr_ratio == 4) {
1851  ratio_mult = 4;
1852  ratio_dec = 1;
1853  } else {
1854  ratio_mult = 1;
1855  ratio_dec = 1;
1856  }
1857 
1859  ac->oc[1].status, 0);
1860 
1862 
1863  indep_flag = get_bits1(gb);
1864 
1865  for (int i = 0; i < ac->oc[1].usac.nb_elems; i++) {
1866  int layout_id;
1867  int layout_type;
1868  AACUsacElemConfig *e = &ac->oc[1].usac.elems[i];
1869  ChannelElement *che;
1870 
1871  if (e->type == ID_USAC_SCE) {
1872  layout_id = elem_id[0]++;
1873  layout_type = TYPE_SCE;
1874  che = ff_aac_get_che(ac, TYPE_SCE, layout_id);
1875  } else if (e->type == ID_USAC_CPE) {
1876  layout_id = elem_id[1]++;
1877  layout_type = TYPE_CPE;
1878  che = ff_aac_get_che(ac, TYPE_CPE, layout_id);
1879  } else if (e->type == ID_USAC_LFE) {
1880  layout_id = elem_id[2]++;
1881  layout_type = TYPE_LFE;
1882  che = ff_aac_get_che(ac, TYPE_LFE, layout_id);
1883  }
1884 
1885  if (e->type != ID_USAC_EXT && !che) {
1886  av_log(ac->avctx, AV_LOG_ERROR,
1887  "channel element %d.%d is not allocated\n",
1888  layout_type, layout_id);
1889  return AVERROR_INVALIDDATA;
1890  }
1891 
1892  switch (e->type) {
1893  case ID_USAC_LFE:
1894  /* Fallthrough */
1895  case ID_USAC_SCE:
1896  ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb,
1897  indep_flag, 1);
1898  if (ret < 0)
1899  return ret;
1900 
1901  audio_found = 1;
1902  che->present = 1;
1903  break;
1904  case ID_USAC_CPE:
1905  ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb,
1906  indep_flag, 2);
1907  if (ret < 0)
1908  return ret;
1909 
1910  audio_found = 1;
1911  che->present = 1;
1912  break;
1913  case ID_USAC_EXT:
1914  ret = parse_ext_ele(ac, e, gb);
1915  if (ret < 0)
1916  return ret;
1917  break;
1918  }
1919  }
1920 
1921  if (audio_found)
1922  samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024;
1923 
1924  samples = (samples * ratio_mult) / ratio_dec;
1925 
1926  if (ac->oc[1].status && audio_found) {
1927  avctx->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
1928  avctx->frame_size = samples;
1929  ac->oc[1].status = OC_LOCKED;
1930  }
1931 
1932  if (!frame->data[0] && samples) {
1933  av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
1934  return AVERROR_INVALIDDATA;
1935  }
1936 
1937  if (samples) {
1938  frame->nb_samples = samples;
1939  frame->sample_rate = avctx->sample_rate;
1940  frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0;
1941  *got_frame_ptr = 1;
1942  } else {
1943  av_frame_unref(ac->frame);
1944  frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0;
1945  *got_frame_ptr = 0;
1946  }
1947 
1948  /* for dual-mono audio (SCE + SCE) */
1949  is_dmono = ac->dmono_mode && elem_id[0] == 2 &&
1952  if (is_dmono) {
1953  if (ac->dmono_mode == 1)
1954  frame->data[1] = frame->data[0];
1955  else if (ac->dmono_mode == 2)
1956  frame->data[0] = frame->data[1];
1957  }
1958 
1959  return 0;
1960 }
AACUsacMPSData::nb_param_sets
int nb_param_sets
Definition: aacdec.h:236
MAX_ELEM_ID
#define MAX_ELEM_ID
Definition: aac.h:34
AACUsacElemConfig::stereo_config_index
uint8_t stereo_config_index
Definition: aacdec.h:333
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1059
ff_usac_noise_fill_start_offset
const uint8_t ff_usac_noise_fill_start_offset[2][2]
Definition: aactab.c:1999
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:280
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
r
const char * r
Definition: vf_curves.c:127
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
decode_usac_stereo_info
static int decode_usac_stereo_info(AACDecContext *ac, AACUSACConfig *usac, AACUsacElemConfig *ec, ChannelElement *cpe, GetBitContext *gb, int indep_flag)
Definition: aacdec_usac.c:848
aacdec_ac.h
AACUSACConfig
Definition: aacdec.h:382
ID_EXT_ELE_SAOC
@ ID_EXT_ELE_SAOC
Definition: aacdec.h:91
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:395
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1032
AACUSACConfig::stream_identifier
uint16_t stream_identifier
Definition: aacdec.h:385
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:247
AACUSACConfig::nb_album
uint8_t nb_album
Definition: aacdec.h:391
spectrum_decode
static void spectrum_decode(AACDecContext *ac, AACUSACConfig *usac, ChannelElement *cpe, int nb_channels)
Definition: aacdec_usac.c:1287
AACUsacElemConfig::payload_frag
uint8_t payload_frag
Definition: aacdec.h:375
AACUsacMPSData::temp_shape_data
int16_t temp_shape_data[MPS_MAX_TIME_SLOTS]
Definition: aacdec.h:254
ff_aac_usac_config_decode
int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx, GetBitContext *gb, OutputConfiguration *oc, int channel_config)
Definition: aacdec_usac.c:340
AV_CHAN_WIDE_LEFT
@ AV_CHAN_WIDE_LEFT
Definition: channel_layout.h:72
ID_USAC_LFE
@ ID_USAC_LFE
Definition: aacdec.h:78
ff_aac_ac_lsb_cdfs
const uint16_t ff_aac_ac_lsb_cdfs[3][4]
Definition: aactab.c:1331
int64_t
long long int64_t
Definition: coverity.c:34
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:254
AVChannelLayout::map
AVChannelCustom * map
This member must be used when the channel order is AV_CHANNEL_ORDER_CUSTOM.
Definition: channel_layout.h:370
av_unused
#define av_unused
Definition: attributes.h:151
AVChannelLayout::u
union AVChannelLayout::@501 u
Details about which channels are present in this layout.
AACUsacElemConfig::tw_mdct
uint8_t tw_mdct
Definition: aacdec.h:330
aacsbr.h
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AV_CHAN_TOP_SURROUND_LEFT
@ AV_CHAN_TOP_SURROUND_LEFT
+110 degrees, Lvs, TpLS
Definition: channel_layout.h:84
aacdec_usac_mps212.h
mps_fr_stride_smg
static const uint8_t mps_fr_stride_smg[4]
Definition: aacdec_usac.c:1355
complex_stereo_downmix_cur
static void complex_stereo_downmix_cur(AACDecContext *ac, ChannelElement *cpe, float *dmix_re)
Definition: aacdec_usac.c:1101
MPS_ICC
@ MPS_ICC
Definition: aacdec_usac_mps212.h:31
b
#define b
Definition: input.c:42
AACUsacMPSData
Definition: aacdec.h:233
data
const char data[16]
Definition: mxf.c:149
aacdec_usac.h
TemporalNoiseShaping::present
int present
Definition: aacdec.h:186
ue
#define ue(name, range_min, range_max)
Definition: cbs_h264.c:61
AACUsacElemData::scale_factor_grouping
uint8_t scale_factor_grouping
Definition: aacdec.h:128
AACUSACConfig::nb_info
uint8_t nb_info
Definition: aacdec.h:393
AACUsacElemConfig::dflt
struct AACUsacElemConfig::@28::@31 dflt
AACDecDSP::apply_tns
void(* apply_tns)(void *_coef_param, TemporalNoiseShaping *tns, IndividualChannelStream *ics, int decode)
Definition: aacdec.h:452
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AACDecContext::proc
AACDecProc proc
Definition: aacdec.h:484
AACUsacElemConfig::bs_pvc
uint8_t bs_pvc
Definition: aacdec.h:340
c1
static const uint64_t c1
Definition: murmur3.c:52
AACUsacStereo::pred_dir
uint8_t pred_dir
Definition: aacdec.h:270
AACUsacElemData::tns_data_present
uint8_t tns_data_present
Definition: aacdec.h:129
ChannelElement::ch
SingleChannelElement ch[2]
Definition: aacdec.h:296
ff_aac_sample_rate_idx
static int ff_aac_sample_rate_idx(int rate)
Definition: aac.h:110
ff_swb_offset_128
const uint16_t *const ff_swb_offset_128[]
Definition: aactab.c:1940
ChannelElement::present
int present
Definition: aacdec.h:291
ID_CONFIG_EXT_STREAM_ID
@ ID_CONFIG_EXT_STREAM_ID
Definition: aacdec.h:85
ID_USAC_EXT
@ ID_USAC_EXT
Definition: aacdec.h:79
win
static float win(SuperEqualizerContext *s, float n, int N)
Definition: af_superequalizer.c:119
AACDecContext::dmono_mode
int dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition: aacdec.h:553
MPEG4AudioConfig
Definition: mpeg4audio.h:29
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:383
AACUsacElemConfig::pseudo_lr
uint8_t pseudo_lr
Definition: aacdec.h:369
IndividualChannelStream::num_swb
int num_swb
number of scalefactor window bands
Definition: aacdec.h:172
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
AV_CHAN_SURROUND_DIRECT_LEFT
@ AV_CHAN_SURROUND_DIRECT_LEFT
Definition: channel_layout.h:74
SingleChannelElement::coeffs
float coeffs[1024]
coefficients for IMDCT, maybe processed
Definition: aacenc.h:119
AACUsacElemData::core_mode
uint8_t core_mode
Definition: aacdec.h:127
mpeg4audio.h
AACArith
Definition: aacdec_ac.h:34
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1047
AV_CHAN_TOP_BACK_RIGHT
@ AV_CHAN_TOP_BACK_RIGHT
Definition: channel_layout.h:67
parse_ext_ele
static int parse_ext_ele(AACDecContext *ac, AACUsacElemConfig *e, GetBitContext *gb)
Definition: aacdec_usac.c:1740
ID_EXT_ELE_AUDIOPREROLL
@ ID_EXT_ELE_AUDIOPREROLL
Definition: aacdec.h:92
TYPE_CPE
@ TYPE_CPE
Definition: aac.h:45
SFB_PER_PRED_BAND
#define SFB_PER_PRED_BAND
Definition: aacdec_usac.c:37
GetBitContext
Definition: get_bits.h:109
decode_spectrum_ac
static int decode_spectrum_ac(AACDecContext *s, float coef[1024], GetBitContext *gb, AACArithState *state, int reset, uint16_t len, uint16_t N)
Decode and dequantize arithmetically coded, uniformly quantized value.
Definition: aacdec_usac.c:599
AACUsacElemConfig::high_rate_mode
uint8_t high_rate_mode
Definition: aacdec.h:362
val
static double val(void *priv, double ch)
Definition: aeval.c:77
AACDecProc::sbr_apply
void(* sbr_apply)(AACDecContext *ac, ChannelElement *che, int id_aac, void *L, void *R)
Definition: aacdec.h:437
OutputConfiguration::status
enum OCStatus status
Definition: aacdec.h:403
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AACUsacElemConfig::freq_res
uint8_t freq_res
Definition: aacdec.h:358
refstruct.h
SingleChannelElement::ics
IndividualChannelStream ics
Definition: aacdec.h:212
AACUsacMPSData::smooth_time
int smooth_time[MPS_MAX_PARAM_SETS]
Definition: aacdec.h:246
cbrt
#define cbrt
Definition: tablegen.h:35
AACUsacElemConfig::pl_data_offset
uint32_t pl_data_offset
Definition: aacdec.h:377
ID_CONFIG_EXT_FILL
@ ID_CONFIG_EXT_FILL
Definition: aacdec.h:83
AACUsacElemConfig
Definition: aacdec.h:327
ff_aac_ec_data_dec
int ff_aac_ec_data_dec(GetBitContext *gb, AACMPSLosslessData *ld, enum AACMPSDataType data_type, int default_val, int start_band, int end_band, int frame_indep_flag, int indep_flag, int nb_param_sets)
Definition: aacdec_usac_mps212.c:592
AACUsacMPSData::param_sets
int param_sets[MPS_MAX_PARAM_SETS]
Definition: aacdec.h:237
complex_stereo_interpolate_imag
static void complex_stereo_interpolate_imag(float *im, float *re, const float f[7], int len, int factor_even, int factor_odd)
Definition: aacdec_usac.c:1141
AV_CHAN_BOTTOM_FRONT_LEFT
@ AV_CHAN_BOTTOM_FRONT_LEFT
Definition: channel_layout.h:80
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
AACDecDSP::dequant_scalefactors
void(* dequant_scalefactors)(SingleChannelElement *sce)
Definition: aacdec.h:446
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
AACUsacElemConfig::residual_bands
uint8_t residual_bands
Definition: aacdec.h:368
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:642
OC_GLOBAL_HDR
@ OC_GLOBAL_HDR
Output configuration set in a global header but not yet locked.
Definition: aacdec.h:57
parse_mps212
static int parse_mps212(AACDecContext *ac, AACUSACConfig *usac, AACUsacMPSData *mps, AACUsacElemConfig *ec, GetBitContext *gb, int frame_indep_flag)
Definition: aacdec_usac.c:1389
s
#define s(width, name)
Definition: cbs_vp9.c:198
AACUsacElemConfig::harmonic_sbr
uint8_t harmonic_sbr
Definition: aacdec.h:338
AACDecDSP::apply_mid_side_stereo
void(* apply_mid_side_stereo)(AACDecContext *ac, ChannelElement *cpe)
Definition: aacdec.h:448
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:217
AVChannelCustom
An AVChannelCustom defines a single channel within a custom order layout.
Definition: channel_layout.h:283
ff_aac_ac_finish
void ff_aac_ac_finish(AACArithState *state, int offset, int N)
Definition: aacdec_ac.c:196
g
const char * g
Definition: vf_curves.c:128
AACUsacMPSData::tsd_phase_data
int tsd_phase_data[64]
Definition: aacdec.h:257
AACUsacMPSData::tsd_num_tr_slots
int tsd_num_tr_slots
Definition: aacdec.h:256
EIGHT_SHORT_SEQUENCE
@ EIGHT_SHORT_SEQUENCE
Definition: aac.h:66
info
MIPS optimizations info
Definition: mips.txt:2
decode_usac_scale_factors
static int decode_usac_scale_factors(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb, uint8_t global_gain)
Definition: aacdec_usac.c:566
AV_CHAN_SIDE_RIGHT
@ AV_CHAN_SIDE_RIGHT
Definition: channel_layout.h:60
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
decode_tsd
static void decode_tsd(GetBitContext *gb, int *data, int nb_tr_slots, int nb_slots)
Definition: aacdec_usac.c:1359
av_refstruct_alloc_ext
static void * av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition: refstruct.h:94
ff_aac_get_che
ChannelElement * ff_aac_get_che(AACDecContext *ac, int type, int elem_id)
Definition: aacdec.c:623
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
AACUsacMPSData::tsd_enable
bool tsd_enable
Definition: aacdec.h:251
AACUsacElemData
Definition: aacdec.h:126
AACUSACConfig::core_sbr_frame_len_idx
uint8_t core_sbr_frame_len_idx
Definition: aacdec.h:383
IndividualChannelStream
Individual Channel Stream.
Definition: aacdec.h:163
UNIDRCLOUDEXT_TERM
@ UNIDRCLOUDEXT_TERM
Definition: aacdec.h:97
ID_USAC_CPE
@ ID_USAC_CPE
Definition: aacdec.h:77
SCALE_DIFF_ZERO
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
Definition: aac.h:95
AV_CHAN_TOP_SIDE_LEFT
@ AV_CHAN_TOP_SIDE_LEFT
Definition: channel_layout.h:77
ff_tns_max_bands_usac_1024
const uint8_t ff_tns_max_bands_usac_1024[]
Definition: aactab.c:1978
AACUsacElemConfig::sbr
struct AACUsacElemConfig::@28 sbr
AACDecContext::fdsp
AVFloatDSPContext * fdsp
Definition: aacdec.h:535
ff_aac_usac_decode_frame
int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac, GetBitContext *gb, int *got_frame_ptr)
Definition: aacdec_usac.c:1828
AV_CHAN_TOP_SIDE_RIGHT
@ AV_CHAN_TOP_SIDE_RIGHT
Definition: channel_layout.h:78
ff_aac_ac_init
void ff_aac_ac_init(AACArith *ac, GetBitContext *gb)
Definition: aacdec_ac.c:103
decode_loudness_info
static int decode_loudness_info(AACDecContext *ac, AACUSACLoudnessInfo *info, GetBitContext *gb)
Definition: aacdec_usac.c:88
AV_CHAN_SIDE_SURROUND_LEFT
@ AV_CHAN_SIDE_SURROUND_LEFT
+90 degrees, Lss, SiL
Definition: channel_layout.h:82
AACUSACConfig::loudness
struct AACUSACConfig::@32 loudness
ff_aac_ldp_parse_channel_stream
int ff_aac_ldp_parse_channel_stream(AACDecContext *ac, AACUSACConfig *usac, AACUsacElemData *ce, GetBitContext *gb)
Definition: aacdec_lpd.c:112
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AACUsacMPSData::ott
AACMPSLosslessData ott[MPS_ELE_NB]
Definition: aacdec.h:240
AACUsacElemConfig::ext
struct AACUsacElemConfig::@30 ext
AACUsacElemData::seed
unsigned int seed
Definition: aacdec.h:147
AACUSACConfig::core_frame_len
uint16_t core_frame_len
Definition: aacdec.h:384
AVFloatDSPContext::vector_fmul_scalar
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:85
IndividualChannelStream::use_kb_window
uint8_t use_kb_window[2]
If set, use Kaiser-Bessel window, otherwise use a sine window.
Definition: aacdec.h:166
ff_aac_num_swb_128
const uint8_t ff_aac_num_swb_128[]
Definition: aactab.c:169
IndividualChannelStream::num_window_groups
int num_window_groups
Definition: aacdec.h:167
ff_tns_max_bands_usac_128
const uint8_t ff_tns_max_bands_usac_128[]
Definition: aactab.c:1994
AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
#define AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
The specified retype target order is ignored and the simplest possible (canonical) order is used for ...
Definition: channel_layout.h:721
AV_CHAN_TOP_BACK_CENTER
@ AV_CHAN_TOP_BACK_CENTER
Definition: channel_layout.h:66
AV_REFSTRUCT_FLAG_NO_ZEROING
#define AV_REFSTRUCT_FLAG_NO_ZEROING
If this flag is set in av_refstruct_alloc_ext_c(), the object will not be initially zeroed.
Definition: refstruct.h:67
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
MPEG4AudioConfig::sampling_index
int sampling_index
Definition: mpeg4audio.h:31
ChannelElement::ms_mask
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
Definition: aacdec.h:294
mps_fr_nb_bands
static const uint8_t mps_fr_nb_bands[8]
Definition: aacdec_usac.c:1351
parse_audio_preroll
static int parse_audio_preroll(AACDecContext *ac, GetBitContext *gb)
Definition: aacdec_usac.c:1666
get_escaped_value
static uint32_t get_escaped_value(GetBitContext *gb, int nb1, int nb2, int nb3)
Definition: aacdec_usac.c:39
aactab.h
AV_CHAN_BOTTOM_FRONT_RIGHT
@ AV_CHAN_BOTTOM_FRONT_RIGHT
Definition: channel_layout.h:81
AACUsacElemConfig::noise_fill
uint8_t noise_fill
Definition: aacdec.h:331
AV_CHAN_TOP_CENTER
@ AV_CHAN_TOP_CENTER
Definition: channel_layout.h:61
AAC_CHANNEL_FRONT
@ AAC_CHANNEL_FRONT
Definition: aac.h:82
AACUsacElemConfig::temp_shape_config
uint8_t temp_shape_config
Definition: aacdec.h:360
seed
static unsigned int seed
Definition: videogen.c:78
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:651
decode_usac_stereo_cplx
static int decode_usac_stereo_cplx(AACDecContext *ac, AACUsacStereo *us, ChannelElement *cpe, GetBitContext *gb, int num_window_groups, int prev_num_window_groups, int indep_flag)
Definition: aacdec_usac.c:694
ff_aac_ac_decode
uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb, const uint16_t *cdf, uint16_t cdf_len)
Definition: aacdec_ac.c:110
spectrum_scale
static void spectrum_scale(AACDecContext *ac, SingleChannelElement *sce, AACUsacElemData *ue)
Definition: aacdec_usac.c:1040
AACUsacStereo
Definition: aacdec.h:260
OC_LOCKED
@ OC_LOCKED
Output configuration locked in place.
Definition: aacdec.h:58
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_CHAN_FRONT_RIGHT_OF_CENTER
@ AV_CHAN_FRONT_RIGHT_OF_CENTER
Definition: channel_layout.h:57
IndividualChannelStream::prev_num_window_groups
int prev_num_window_groups
Previous frame's number of window groups.
Definition: aacdec.h:168
get_unary
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:46
AACUsacElemConfig::default_len
uint32_t default_len
Definition: aacdec.h:376
OutputConfiguration::layout_map_tags
int layout_map_tags
Definition: aacdec.h:401
SingleChannelElement::ue
AACUsacElemData ue
USAC element data.
Definition: aacdec.h:213
AV_CHAN_FRONT_RIGHT
@ AV_CHAN_FRONT_RIGHT
Definition: channel_layout.h:51
AV_CHAN_FRONT_CENTER
@ AV_CHAN_FRONT_CENTER
Definition: channel_layout.h:52
OutputConfiguration::layout_map
uint8_t layout_map[MAX_ELEM_ID *4][3]
Definition: aacdec.h:400
AACUsacMPSData::smg_data
bool smg_data[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS]
Definition: aacdec.h:248
AACUsacElemConfig::bs_intertes
uint8_t bs_intertes
Definition: aacdec.h:339
IndividualChannelStream::window_sequence
enum WindowSequence window_sequence[2]
Definition: aacdec.h:165
AACUsacMPSData::ott_idx
int ott_idx[MPS_ELE_NB][MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS]
Definition: aacdec.h:241
AACDecContext::dsp
AACDecDSP dsp
Definition: aacdec.h:483
f
f
Definition: af_crystalizer.c:122
ff_swb_offset_1024
const uint16_t *const ff_swb_offset_1024[]
Definition: aactab.c:1900
AACUsacElemConfig::otts_bands_phase_present
uint8_t otts_bands_phase_present
Definition: aacdec.h:365
AACUsacMPSData::framing_type
int framing_type
Definition: aacdec.h:235
powf
#define powf(x, y)
Definition: libm.h:52
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
ONLY_LONG_SEQUENCE
@ ONLY_LONG_SEQUENCE
Definition: aac.h:64
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
AACDecDSP::imdct_and_windowing
void(* imdct_and_windowing)(AACDecContext *ac, SingleChannelElement *sce)
Definition: aacdec.h:467
ChannelElement::max_sfb_ste
uint8_t max_sfb_ste
(USAC) Maximum of both max_sfb values
Definition: aacdec.h:293
AV_CHAN_LOW_FREQUENCY
@ AV_CHAN_LOW_FREQUENCY
Definition: channel_layout.h:53
ESC_BT
@ ESC_BT
Spectral data are coded with an escape sequence.
Definition: aac.h:73
SingleChannelElement::sfo
int sfo[128]
scalefactor offsets
Definition: aacdec.h:216
MPS_CLD
@ MPS_CLD
Definition: aacdec_usac_mps212.h:30
AV_CHAN_BACK_RIGHT
@ AV_CHAN_BACK_RIGHT
Definition: channel_layout.h:55
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
AV_CHAN_SIDE_LEFT
@ AV_CHAN_SIDE_LEFT
Definition: channel_layout.h:59
AACUsacElemData::noise
struct AACUsacElemData::@19 noise
ChannelElement::us
AACUsacStereo us
Definition: aacdec.h:300
OutputConfiguration
Definition: aacdec.h:398
ff_aac_usac_mdst_filt_cur
const float ff_aac_usac_mdst_filt_cur[4][4][7]
Definition: aactab.c:3885
AACUsacMPSData::freq_res_stride_smg
int freq_res_stride_smg[MPS_MAX_PARAM_SETS]
Definition: aacdec.h:247
MPS_IPD
@ MPS_IPD
Definition: aacdec_usac_mps212.h:32
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
av_channel_layout_retype
int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
Change the AVChannelOrder of a channel layout.
Definition: channel_layout.c:885
AV_CHAN_TOP_FRONT_RIGHT
@ AV_CHAN_TOP_FRONT_RIGHT
Definition: channel_layout.h:64
AV_CHANNEL_ORDER_NATIVE
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
Definition: channel_layout.h:125
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:416
ff_aac_ac_get_context
uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t c, int i, int N)
Definition: aacdec_ac.c:57
AV_CHAN_FRONT_LEFT_OF_CENTER
@ AV_CHAN_FRONT_LEFT_OF_CENTER
Definition: channel_layout.h:56
N
#define N
Definition: af_mcompand.c:54
SingleChannelElement::band_type
enum BandType band_type[128]
band types
Definition: aacdec.h:215
AACUsacElemConfig::fixed_gain
uint8_t fixed_gain
Definition: aacdec.h:359
ID_CONFIG_EXT_LOUDNESS_INFO
@ ID_CONFIG_EXT_LOUDNESS_INFO
Definition: aacdec.h:84
unary.h
SingleChannelElement::output
float * output
PCM output.
Definition: aacdec.h:228
av_channel_layout_compare
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
Definition: channel_layout.c:809
ff_aac_ac_get_pk
uint32_t ff_aac_ac_get_pk(uint32_t c)
Definition: aacdec_ac.c:73
av_channel_layout_custom_init
int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels)
Initialize a custom channel layout with the specified number of channels.
Definition: channel_layout.c:232
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
AVChannel
AVChannel
Definition: channel_layout.h:47
apply_noise_fill
static void apply_noise_fill(AACDecContext *ac, SingleChannelElement *sce, AACUsacElemData *ue)
Definition: aacdec_usac.c:998
AV_CHAN_TOP_SURROUND_RIGHT
@ AV_CHAN_TOP_SURROUND_RIGHT
-110 degrees, Rvs, TpRS
Definition: channel_layout.h:85
RawDataBlockType
RawDataBlockType
Definition: aac.h:43
AV_CHAN_SURROUND_DIRECT_RIGHT
@ AV_CHAN_SURROUND_DIRECT_RIGHT
Definition: channel_layout.h:75
SingleChannelElement
Single Channel Element - used for both SCE and LFE elements.
Definition: aacdec.h:211
ff_swb_offset_768
const uint16_t *const ff_swb_offset_768[]
Definition: aactab.c:1916
decode_usac_element_pair
static int decode_usac_element_pair(AACDecContext *ac, AACUsacElemConfig *e, GetBitContext *gb)
Definition: aacdec_usac.c:205
IndividualChannelStream::num_windows
int num_windows
Definition: aacdec.h:173
OutputConfiguration::usac
AACUSACConfig usac
Definition: aacdec.h:404
LONG_STOP_SEQUENCE
@ LONG_STOP_SEQUENCE
Definition: aac.h:67
usac_ch_pos_to_av
static enum AVChannel usac_ch_pos_to_av[64]
Definition: aacdec_usac.c:53
ChannelElement
channel element - generic struct for SCE/CPE/CCE/LFE
Definition: aacdec.h:290
IndividualChannelStream::swb_offset
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Definition: aacdec.h:171
ff_aac_parse_fac_data
int ff_aac_parse_fac_data(AACUsacElemData *ce, GetBitContext *gb, int use_gain, int len)
Definition: aacdec_lpd.c:93
ff_aac_usac_samplerate
const int ff_aac_usac_samplerate[32]
Definition: aactab.c:3877
AACUsacElemConfig::type
enum AACUsacElem type
Definition: aacdec.h:328
TYPE_LFE
@ TYPE_LFE
Definition: aac.h:47
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:496
complex_stereo_get_filter
static const float * complex_stereo_get_filter(ChannelElement *cpe, int is_prev)
Definition: aacdec_usac.c:1247
MPEG4AudioConfig::chan_config
int chan_config
Definition: mpeg4audio.h:33
AACUsacElemConfig::decorr_config
uint8_t decorr_config
Definition: aacdec.h:361
ff_aac_ac_update_context
void ff_aac_ac_update_context(AACArithState *state, int idx, uint16_t a, uint16_t b)
Definition: aacdec_ac.c:91
TYPE_SCE
@ TYPE_SCE
Definition: aac.h:44
len
int len
Definition: vorbis_enc_data.h:426
filt
static const int8_t filt[NUMTAPS *2]
Definition: af_earwax.c:40
complex_stereo_downmix_prev
static void complex_stereo_downmix_prev(AACDecContext *ac, ChannelElement *cpe, float *dmix_re)
Definition: aacdec_usac.c:1070
AACDecContext::oc
OutputConfiguration oc[2]
Definition: aacdec.h:558
MPEG4AudioConfig::ext_sample_rate
int ext_sample_rate
Definition: mpeg4audio.h:37
IndividualChannelStream::tns_max_bands
int tns_max_bands
Definition: aacdec.h:174
ff_aac_num_swb_96
const uint8_t ff_aac_num_swb_96[]
Definition: aactab.c:173
ff_aac_ac_map_process
uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int N)
Definition: aacdec_ac.c:25
AACUSACConfig::nb_elems
int nb_elems
Definition: aacdec.h:388
ID_EXT_ELE_UNI_DRC
@ ID_EXT_ELE_UNI_DRC
Definition: aacdec.h:93
AAC_CHANNEL_LFE
@ AAC_CHANNEL_LFE
Definition: aac.h:85
decode_usac_sbr_data
static int decode_usac_sbr_data(AACDecContext *ac, AACUsacElemConfig *e, GetBitContext *gb)
Definition: aacdec_usac.c:153
ret
ret
Definition: filter_design.txt:187
ff_aac_num_swb_1024
const uint8_t ff_aac_num_swb_1024[]
Definition: aactab.c:149
AACUsacMPSData::temp_shape_enable
bool temp_shape_enable
Definition: aacdec.h:252
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
AACDecContext::frame
struct AVFrame * frame
Definition: aacdec.h:486
decode_usac_element_core
static void decode_usac_element_core(AACUsacElemConfig *e, GetBitContext *gb, int sbr_ratio)
Definition: aacdec_usac.c:196
LONG_START_SEQUENCE
@ LONG_START_SEQUENCE
Definition: aac.h:65
UNIDRCLOUDEXT_EQ
@ UNIDRCLOUDEXT_EQ
Definition: aacdec.h:98
id
enum AVCodecID id
Definition: dts2pts.c:549
AACUsacMPSData::smooth_mode
int smooth_mode[MPS_MAX_PARAM_SETS]
Definition: aacdec.h:245
aacdec_lpd.h
AV_CHAN_BACK_CENTER
@ AV_CHAN_BACK_CENTER
Definition: channel_layout.h:58
SingleChannelElement::tns
TemporalNoiseShaping tns
Definition: aacdec.h:214
U
#define U(x)
Definition: vpx_arith.h:37
AACDecContext
main AAC decoding context
Definition: aacdec.h:479
AACUSACConfig::info
AACUSACLoudnessInfo info[64]
Definition: aacdec.h:394
AVCodecContext
main external API structure.
Definition: avcodec.h:439
c2
static const uint64_t c2
Definition: murmur3.c:53
AACUsacMPSData::temp_shape_enable_ch
bool temp_shape_enable_ch[2]
Definition: aacdec.h:253
AV_CHAN_LOW_FREQUENCY_2
@ AV_CHAN_LOW_FREQUENCY_2
Definition: channel_layout.h:76
AACDecContext::avctx
struct AVCodecContext * avctx
Definition: aacdec.h:481
AV_CHAN_TOP_BACK_LEFT
@ AV_CHAN_TOP_BACK_LEFT
Definition: channel_layout.h:65
noise_random_sign
static float noise_random_sign(unsigned int *seed)
Definition: aacdec_usac.c:990
apply_complex_stereo
static void apply_complex_stereo(AACDecContext *ac, ChannelElement *cpe)
Definition: aacdec_usac.c:1195
aacdec_tab.h
setup_sce
static int setup_sce(AACDecContext *ac, SingleChannelElement *sce, AACUSACConfig *usac)
Definition: aacdec_usac.c:785
AACUSACLoudnessInfo
Definition: aacdec.h:303
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1626
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:442
cm
#define cm
Definition: dvbsubdec.c:40
AACUsacElemConfig::env_quant_mode
uint8_t env_quant_mode
Definition: aacdec.h:370
AV_CHAN_BACK_LEFT
@ AV_CHAN_BACK_LEFT
Definition: channel_layout.h:54
MPEG4AudioConfig::sbr
int sbr
-1 implicit, 1 presence
Definition: mpeg4audio.h:34
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
ff_aac_decode_tns
int ff_aac_decode_tns(AACDecContext *ac, TemporalNoiseShaping *tns, GetBitContext *gb, const IndividualChannelStream *ics)
Decode Temporal Noise Shaping data; reference: table 4.48.
Definition: aacdec.c:1590
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
ff_aac_usac_reset_state
int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc)
Definition: aacdec_usac.c:281
AV_CHAN_BOTTOM_FRONT_CENTER
@ AV_CHAN_BOTTOM_FRONT_CENTER
Definition: channel_layout.h:79
AACUsacElemConfig::ratio
int ratio
Definition: aacdec.h:336
decode_loudness_set
static int decode_loudness_set(AACDecContext *ac, AACUSACConfig *usac, GetBitContext *gb)
Definition: aacdec_usac.c:114
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:449
OutputConfiguration::m4ac
MPEG4AudioConfig m4ac
Definition: aacdec.h:399
ff_aac_map_index_data
int ff_aac_map_index_data(AACMPSLosslessData *ld, enum AACMPSDataType data_type, int dst_idx[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS], int default_value, int start_band, int stop_band, int nb_param_sets, const int *param_set_idx, int extend_frame)
Definition: aacdec_usac_mps212.c:778
AV_CHAN_TOP_FRONT_CENTER
@ AV_CHAN_TOP_FRONT_CENTER
Definition: channel_layout.h:63
AACUSACConfig::elems
AACUsacElemConfig elems[MAX_ELEM_ID]
Definition: aacdec.h:387
us
#define us(width, name, range_min, range_max, subs,...)
Definition: cbs_apv.c:70
AV_CHAN_SIDE_SURROUND_RIGHT
@ AV_CHAN_SIDE_SURROUND_RIGHT
-90 degrees, Rss, SiR
Definition: channel_layout.h:83
mem.h
FF_AAC_AC_ESCAPE
#define FF_AAC_AC_ESCAPE
Definition: aacdec_ac.h:40
OutputConfiguration::ch_layout
AVChannelLayout ch_layout
Definition: aacdec.h:402
AV_CHAN_WIDE_RIGHT
@ AV_CHAN_WIDE_RIGHT
Definition: channel_layout.h:73
ff_aac_sbr_decode_usac_data
int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che, AACUsacElemConfig *ue, GetBitContext *gb, int sbr_ch, int indep_flag)
Decode frame SBR data, USAC.
Definition: aacsbr_template.c:1209
decode_usac_core_coder
static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac, AACUsacElemConfig *ec, ChannelElement *che, GetBitContext *gb, int indep_flag, int nb_channels)
Definition: aacdec_usac.c:1509
AACDecDSP::imdct_and_windowing_768
void(* imdct_and_windowing_768)(AACDecContext *ac, SingleChannelElement *sce)
Definition: aacdec.h:468
MPEG4AudioConfig::frame_length_short
int frame_length_short
Definition: mpeg4audio.h:41
ID_EXT_ELE_MPEGS
@ ID_EXT_ELE_MPEGS
Definition: aacdec.h:90
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AACUsacMPSData::opd_smoothing_mode
bool opd_smoothing_mode
Definition: aacdec.h:242
ff_aac_ac_msb_cdfs
const uint16_t ff_aac_ac_msb_cdfs[64][17]
Definition: aactab.c:1200
ff_vlc_scalefactors
VLCElem ff_vlc_scalefactors[352]
Definition: aacdec_tab.c:111
AV_CHAN_TOP_FRONT_LEFT
@ AV_CHAN_TOP_FRONT_LEFT
Definition: channel_layout.h:62
IndividualChannelStream::max_sfb
uint8_t max_sfb
number of scalefactor bands per group
Definition: aacdec.h:164
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2070
AV_CHAN_FRONT_LEFT
@ AV_CHAN_FRONT_LEFT
Definition: channel_layout.h:50
ff_aac_set_default_channel_config
int ff_aac_set_default_channel_config(AACDecContext *ac, AVCodecContext *avctx, uint8_t(*layout_map)[3], int *tags, int channel_config)
Set up channel positions based on a default channel configuration as specified in table 1....
Definition: aacdec.c:583
ff_aac_huff_dec_reshape
int ff_aac_huff_dec_reshape(GetBitContext *gb, int16_t *out_data, int nb_val)
Definition: aacdec_usac_mps212.c:678
AACArithState
Definition: aacdec_ac.h:27
state
static struct @557 state
AACUsacElemConfig::residual_coding
uint8_t residual_coding
Definition: aacdec.h:367
ff_aac_sbr_config_usac
int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che, AACUsacElemConfig *ue)
Due to channel allocation not being known upon SBR parameter transmission, supply the parameters sepa...
Definition: aacsbr_template.c:1201
AACUsacElemConfig::otts_bands_phase
uint8_t otts_bands_phase
Definition: aacdec.h:366
AACUSACLoudnessExt
AACUSACLoudnessExt
Definition: aacdec.h:96
IndividualChannelStream::group_len
uint8_t group_len[8]
Definition: aacdec.h:169
ff_aac_num_swb_768
const uint8_t ff_aac_num_swb_768[]
Definition: aactab.c:157
AACUsacElemConfig::pl_buf
uint8_t * pl_buf
Definition: aacdec.h:378
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
AV_PROFILE_AAC_USAC
#define AV_PROFILE_AAC_USAC
Definition: defs.h:76
AACUsacElemConfig::phase_coding
uint8_t phase_coding
Definition: aacdec.h:363
ff_aac_output_configure
int ff_aac_output_configure(AACDecContext *ac, uint8_t layout_map[MAX_ELEM_ID *4][3], int tags, enum OCStatus oc_type, int get_new_frame)
Configure output channel order based on the current program configuration element.
Definition: aacdec.c:487
AACUsacElemConfig::mps
struct AACUsacElemConfig::@29 mps
ID_EXT_ELE_FILL
@ ID_EXT_ELE_FILL
Definition: aacdec.h:89
AACUSACConfig::album_info
AACUSACLoudnessInfo album_info[64]
Definition: aacdec.h:392
MPEG4AudioConfig::sample_rate
int sample_rate
Definition: mpeg4audio.h:32
ff_swb_offset_96
const uint16_t *const ff_swb_offset_96[]
Definition: aactab.c:1958
decode_usac_extension
static int decode_usac_extension(AACDecContext *ac, AACUsacElemConfig *e, GetBitContext *gb)
Definition: aacdec_usac.c:242
ID_USAC_SCE
@ ID_USAC_SCE
Definition: aacdec.h:76