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  if (!e->mps.freq_res)
219  return AVERROR_INVALIDDATA; /* value 0 is reserved */
220 
221  int numBands = ((int[]){0,28,20,14,10,7,5,4})[e->mps.freq_res]; // ISO/IEC 23003-1:2007, 5.2, Table 39
222 
223  e->mps.fixed_gain = get_bits(gb, 3); /* bsFixedGainDMX */
224  e->mps.temp_shape_config = get_bits(gb, 2); /* bsTempShapeConfig */
225  e->mps.decorr_config = get_bits(gb, 2); /* bsDecorrConfig */
226  e->mps.high_rate_mode = get_bits1(gb); /* bsHighRateMode */
227  e->mps.phase_coding = get_bits1(gb); /* bsPhaseCoding */
228 
230  int otts_bands_phase = ((int[]){0,10,10,7,5,3,2,2})[e->mps.freq_res]; // Table 109 — Default value of bsOttBandsPhase
231  if (e->mps.otts_bands_phase_present) { /* bsOttBandsPhasePresent */
232  otts_bands_phase = get_bits(gb, 5); /* bsOttBandsPhase */
233  if (otts_bands_phase > numBands)
234  return AVERROR_INVALIDDATA;
235  }
236  e->mps.otts_bands_phase = otts_bands_phase;
237 
238  e->mps.residual_coding = e->stereo_config_index >= 2; /* bsResidualCoding */
239  if (e->mps.residual_coding) {
240  int residual_bands = get_bits(gb, 5); /* bsResidualBands */
241  if (residual_bands > numBands)
242  return AVERROR_INVALIDDATA;
243  e->mps.residual_bands = residual_bands;
244 
246  e->mps.residual_bands);
247  e->mps.pseudo_lr = get_bits1(gb); /* bsPseudoLr */
248  }
249  if (e->mps.temp_shape_config == 2)
250  e->mps.env_quant_mode = get_bits1(gb); /* bsEnvQuantMode */
251  }
252 
253  return 0;
254 }
255 
257  GetBitContext *gb)
258 {
259  int len = 0, ext_config_len;
260 
261  e->ext.type = get_escaped_value(gb, 4, 8, 16); /* usacExtElementType */
262  ext_config_len = get_escaped_value(gb, 4, 8, 16); /* usacExtElementConfigLength */
263 
264  if (get_bits1(gb)) /* usacExtElementDefaultLengthPresent */
265  len = get_escaped_value(gb, 8, 16, 0) + 1;
266 
267  e->ext.default_len = len;
268  e->ext.payload_frag = get_bits1(gb); /* usacExtElementPayloadFrag */
269 
270  av_log(ac->avctx, AV_LOG_DEBUG, "Extension present: type %i, len %i\n",
271  e->ext.type, ext_config_len);
272 
273  switch (e->ext.type) {
274 #if 0 /* Skip unsupported values */
275  case ID_EXT_ELE_MPEGS:
276  break;
277  case ID_EXT_ELE_SAOC:
278  break;
279  case ID_EXT_ELE_UNI_DRC:
280  break;
281 #endif
282  case ID_EXT_ELE_FILL:
283  break; /* This is what the spec does */
285  /* No configuration needed - fallthrough (len should be 0) */
286  default:
287  skip_bits(gb, 8*ext_config_len);
288  e->ext.type = ID_EXT_ELE_FILL;
289  break;
290  };
291 
292  return 0;
293 }
294 
296 {
297  AACUSACConfig *usac = &oc->usac;
298  int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
299 
300  ChannelElement *che;
301  enum RawDataBlockType type;
302  int id, ch;
303 
304  /* Initialize state */
305  for (int i = 0; i < usac->nb_elems; i++) {
306  AACUsacElemConfig *e = &usac->elems[i];
307  if (e->type == ID_USAC_EXT)
308  continue;
309 
310  switch (e->type) {
311  case ID_USAC_SCE:
312  ch = 1;
313  type = TYPE_SCE;
314  id = elem_id[0]++;
315  break;
316  case ID_USAC_CPE:
317  ch = 2;
318  type = TYPE_CPE;
319  id = elem_id[1]++;
320  break;
321  case ID_USAC_LFE:
322  ch = 1;
323  type = TYPE_LFE;
324  id = elem_id[2]++;
325  break;
326  }
327 
328  che = ff_aac_get_che(ac, type, id);
329  if (che) {
330  AACUsacStereo *us = &che->us;
331  memset(us, 0, sizeof(*us));
332 
333  if (e->sbr.ratio)
334  ff_aac_sbr_config_usac(ac, che, e);
335 
336  for (int j = 0; j < ch; j++) {
337  SingleChannelElement *sce = &che->ch[j];
338  AACUsacElemData *ue = &sce->ue;
339 
340  memset(ue, 0, sizeof(*ue));
341 
342  if (!ch)
343  ue->noise.seed = 0x3039;
344  else
345  che->ch[1].ue.noise.seed = 0x10932;
346  }
347  }
348  }
349 
350  return 0;
351 }
352 
353 /* UsacConfig */
356  int channel_config)
357 {
358  int ret;
359  uint8_t freq_idx;
360  uint8_t channel_config_idx;
361  int nb_channels = 0;
362  int ratio_mult, ratio_dec;
363  int samplerate;
364  int sbr_ratio;
365  MPEG4AudioConfig *m4ac = &oc->m4ac;
366  AACUSACConfig *usac = &oc->usac;
367  int elem_id[3 /* SCE, CPE, LFE */];
368 
369  int map_pos_set = 0;
370  uint8_t layout_map[MAX_ELEM_ID*4][3] = { 0 };
371 
372  if (!ac)
373  return AVERROR_PATCHWELCOME;
374 
375  memset(usac, 0, sizeof(*usac));
376 
377  freq_idx = get_bits(gb, 5); /* usacSamplingFrequencyIndex */
378  if (freq_idx == 0x1f) {
379  samplerate = get_bits(gb, 24); /* usacSamplingFrequency */
380  } else {
381  samplerate = ff_aac_usac_samplerate[freq_idx];
382  if (samplerate < 0)
383  return AVERROR(EINVAL);
384  }
385 
386  usac->core_sbr_frame_len_idx = get_bits(gb, 3); /* coreSbrFrameLengthIndex */
387  m4ac->frame_length_short = usac->core_sbr_frame_len_idx == 0 ||
388  usac->core_sbr_frame_len_idx == 2;
389 
390  usac->core_frame_len = (usac->core_sbr_frame_len_idx == 0 ||
391  usac->core_sbr_frame_len_idx == 2) ? 768 : 1024;
392 
393  sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
394  usac->core_sbr_frame_len_idx == 3 ? 3 :
395  usac->core_sbr_frame_len_idx == 4 ? 1 :
396  0;
397 
398  if (sbr_ratio == 2) {
399  ratio_mult = 8;
400  ratio_dec = 3;
401  } else if (sbr_ratio == 3) {
402  ratio_mult = 2;
403  ratio_dec = 1;
404  } else if (sbr_ratio == 4) {
405  ratio_mult = 4;
406  ratio_dec = 1;
407  } else {
408  ratio_mult = 1;
409  ratio_dec = 1;
410  }
411 
412  avctx->sample_rate = samplerate;
413  m4ac->ext_sample_rate = samplerate;
414  m4ac->sample_rate = (samplerate * ratio_dec) / ratio_mult;
415 
417  m4ac->sbr = sbr_ratio > 0;
418 
419  channel_config_idx = get_bits(gb, 5); /* channelConfigurationIndex */
420  if (!channel_config_idx) {
421  /* UsacChannelConfig() */
422  nb_channels = get_escaped_value(gb, 5, 8, 16); /* numOutChannels */
423  if (nb_channels > 64)
424  return AVERROR(EINVAL);
425 
427 
428  ret = av_channel_layout_custom_init(&ac->oc[1].ch_layout, nb_channels);
429  if (ret < 0)
430  return ret;
431 
432  for (int i = 0; i < nb_channels; i++) {
433  AVChannelCustom *cm = &ac->oc[1].ch_layout.u.map[i];
434  cm->id = usac_ch_pos_to_av[get_bits(gb, 5)]; /* bsOutputChannelPos */
435  }
436 
440  if (ret < 0)
441  return ret;
442 
443  ret = av_channel_layout_copy(&avctx->ch_layout, &ac->oc[1].ch_layout);
444  if (ret < 0)
445  return ret;
446  } else {
447  int nb_elements;
448  if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
449  &nb_elements, channel_config_idx)))
450  return ret;
451 
452  /* Fill in the number of expected channels */
453  for (int i = 0; i < nb_elements; i++)
454  nb_channels += layout_map[i][0] == TYPE_CPE ? 2 : 1;
455 
456  map_pos_set = 1;
457  }
458 
459  /* UsacDecoderConfig */
460  elem_id[0] = elem_id[1] = elem_id[2] = 0;
461  usac->nb_elems = get_escaped_value(gb, 4, 8, 16) + 1;
462  if (usac->nb_elems > 64) {
463  av_log(ac->avctx, AV_LOG_ERROR, "Too many elements: %i\n",
464  usac->nb_elems);
465  usac->nb_elems = 0;
466  return AVERROR(EINVAL);
467  }
468 
469  for (int i = 0; i < usac->nb_elems; i++) {
470  int map_count = elem_id[0] + elem_id[1] + elem_id[2];
471  AACUsacElemConfig *e = &usac->elems[i];
472  memset(e, 0, sizeof(*e));
473 
474  e->type = get_bits(gb, 2); /* usacElementType */
475  if (e->type != ID_USAC_EXT && (map_count + 1) > nb_channels) {
476  av_log(ac->avctx, AV_LOG_ERROR, "Too many channels for the channel "
477  "configuration\n");
478  usac->nb_elems = 0;
479  return AVERROR(EINVAL);
480  }
481 
482  av_log(ac->avctx, AV_LOG_DEBUG, "Element present: idx %i, type %i\n",
483  i, e->type);
484 
485  switch (e->type) {
486  case ID_USAC_SCE: /* SCE */
487  /* UsacCoreConfig */
488  decode_usac_element_core(e, gb, sbr_ratio);
489  if (e->sbr.ratio > 0) {
490  ret = decode_usac_sbr_data(ac, e, gb);
491  if (ret < 0)
492  return ret;
493  }
494  layout_map[map_count][0] = TYPE_SCE;
495  layout_map[map_count][1] = elem_id[0]++;
496  if (!map_pos_set)
497  layout_map[map_count][2] = AAC_CHANNEL_FRONT;
498 
499  break;
500  case ID_USAC_CPE: /* UsacChannelPairElementConf */
501  /* UsacCoreConfig */
502  decode_usac_element_core(e, gb, sbr_ratio);
503  ret = decode_usac_element_pair(ac, e, gb);
504  if (ret < 0)
505  return ret;
506  layout_map[map_count][0] = TYPE_CPE;
507  layout_map[map_count][1] = elem_id[1]++;
508  if (!map_pos_set)
509  layout_map[map_count][2] = AAC_CHANNEL_FRONT;
510 
511  break;
512  case ID_USAC_LFE: /* LFE */
513  /* LFE has no need for any configuration */
514  e->tw_mdct = 0;
515  e->noise_fill = 0;
516  layout_map[map_count][0] = TYPE_LFE;
517  layout_map[map_count][1] = elem_id[2]++;
518  if (!map_pos_set)
519  layout_map[map_count][2] = AAC_CHANNEL_LFE;
520 
521  break;
522  case ID_USAC_EXT: /* EXT */
523  ret = decode_usac_extension(ac, e, gb);
524  if (ret < 0)
525  return ret;
526  break;
527  };
528  }
529 
530  ret = ff_aac_output_configure(ac, layout_map, elem_id[0] + elem_id[1] + elem_id[2],
531  OC_GLOBAL_HDR, 0);
532  if (ret < 0) {
533  av_log(avctx, AV_LOG_ERROR, "Unable to parse channel config!\n");
534  usac->nb_elems = 0;
535  return ret;
536  }
537 
538  if (get_bits1(gb)) { /* usacConfigExtensionPresent */
539  int invalid;
540  int nb_extensions = get_escaped_value(gb, 2, 4, 8) + 1; /* numConfigExtensions */
541  for (int i = 0; i < nb_extensions; i++) {
542  int type = get_escaped_value(gb, 4, 8, 16);
543  int len = get_escaped_value(gb, 4, 8, 16);
544  switch (type) {
546  ret = decode_loudness_set(ac, usac, gb);
547  if (ret < 0)
548  return ret;
549  break;
551  usac->stream_identifier = get_bits(gb, 16);
552  break;
553  case ID_CONFIG_EXT_FILL: /* fallthrough */
554  invalid = 0;
555  while (len--) {
556  if (get_bits(gb, 8) != 0xA5)
557  invalid++;
558  }
559  if (invalid)
560  av_log(avctx, AV_LOG_WARNING, "Invalid fill bytes: %i\n",
561  invalid);
562  break;
563  default:
564  while (len--)
565  skip_bits(gb, 8);
566  break;
567  }
568  }
569  }
570 
572 
573  ret = ff_aac_usac_reset_state(ac, oc);
574  if (ret < 0)
575  return ret;
576 
577  return 0;
578 }
579 
582  GetBitContext *gb, uint8_t global_gain)
583 {
584  IndividualChannelStream *ics = &sce->ics;
585 
586  /* Decode all scalefactors. */
587  int offset_sf = global_gain;
588  for (int g = 0; g < ics->num_window_groups; g++) {
589  for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
590  if (g || sfb)
591  offset_sf += get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - SCALE_DIFF_ZERO;
592  if (offset_sf > 255U) {
594  "Scalefactor (%d) out of range.\n", offset_sf);
595  return AVERROR_INVALIDDATA;
596  }
597 
598  sce->sfo[g*ics->max_sfb + sfb] = offset_sf - 100;
599  }
600  }
601 
602  return 0;
603 }
604 
605 /**
606  * Decode and dequantize arithmetically coded, uniformly quantized value
607  *
608  * @param coef array of dequantized, scaled spectral data
609  * @param sf array of scalefactors or intensity stereo positions
610  *
611  * @return Returns error status. 0 - OK, !0 - error
612  */
613 static int decode_spectrum_ac(AACDecContext *s, float coef[1024],
615  int reset, uint16_t len, uint16_t N)
616 {
617  AACArith ac;
618  int i, a, b;
619  uint32_t c;
620 
621  int gb_count;
622  GetBitContext gb2;
623 
624  c = ff_aac_ac_map_process(state, reset, N);
625 
626  if (!len) {
627  ff_aac_ac_finish(state, 0, N);
628  return 0;
629  }
630 
631  ff_aac_ac_init(&ac, gb);
632 
633  /* Backup reader for rolling back by 14 bits at the end */
634  gb2 = *gb;
635  gb_count = get_bits_count(&gb2);
636 
637  for (i = 0; i < len/2; i++) {
638  /* MSB */
639  int lvl, esc_nb, m;
641  for (lvl=esc_nb=0;;) {
642  uint32_t pki = ff_aac_ac_get_pk(c + (esc_nb << 17));
643  m = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_msb_cdfs[pki],
645  if (m < FF_AAC_AC_ESCAPE)
646  break;
647  lvl++;
648 
649  /* Cargo-culted value. */
650  if (lvl > 23)
651  return AVERROR(EINVAL);
652 
653  if ((esc_nb = lvl) > 7)
654  esc_nb = 7;
655  }
656 
657  b = m >> 2;
658  a = m - (b << 2);
659 
660  /* ARITH_STOP detection */
661  if (!m) {
662  if (esc_nb)
663  break;
664  a = b = 0;
665  }
666 
667  /* LSB */
668  for (int l = lvl; l > 0; l--) {
669  int lsbidx = !a ? 1 : (!b ? 0 : 2);
670  uint8_t r = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_lsb_cdfs[lsbidx],
672  a = (a << 1) | (r & 1);
673  b = (b << 1) | ((r >> 1) & 1);
674  }
675 
676  /* Dequantize coeffs here */
677  coef[2*i + 0] = a * cbrt(a);
678  coef[2*i + 1] = b * cbrt(b);
680  }
681 
682  if (len > 1) {
683  /* "Rewind" bitstream back by 14 bits */
684  int gb_count2 = get_bits_count(&gb2);
685  skip_bits(gb, gb_count2 - gb_count - 14);
686  } else {
687  *gb = gb2;
688  }
689 
691 
692  for (; i < N/2; i++) {
693  coef[2*i + 0] = 0;
694  coef[2*i + 1] = 0;
695  }
696 
697  /* Signs */
698  for (i = 0; i < len; i++) {
699  if (coef[i]) {
700  if (!get_bits1(gb)) /* s */
701  coef[i] *= -1;
702  }
703  }
704 
705  return 0;
706 }
707 
709  ChannelElement *cpe, GetBitContext *gb,
710  int num_window_groups,
711  int prev_num_window_groups,
712  int indep_flag)
713 {
714  int delta_code_time;
715  IndividualChannelStream *ics = &cpe->ch[0].ics;
716 
717  if (!get_bits1(gb)) { /* cplx_pred_all */
718  for (int g = 0; g < num_window_groups; g++) {
719  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
720  const uint8_t val = get_bits1(gb);
721  us->pred_used[g*cpe->max_sfb_ste + sfb] = val;
722  if ((sfb + 1) < cpe->max_sfb_ste)
723  us->pred_used[g*cpe->max_sfb_ste + sfb + 1] = val;
724  }
725  }
726  } else {
727  for (int g = 0; g < num_window_groups; g++)
728  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++)
729  us->pred_used[g*cpe->max_sfb_ste + sfb] = 1;
730  }
731 
732  us->pred_dir = get_bits1(gb);
733  us->complex_coef = get_bits1(gb);
734 
735  us->use_prev_frame = 0;
736  if (us->complex_coef && !indep_flag)
737  us->use_prev_frame = get_bits1(gb);
738 
739  delta_code_time = 0;
740  if (!indep_flag)
741  delta_code_time = get_bits1(gb);
742 
743  /* TODO: shouldn't be needed */
744  for (int g = 0; g < num_window_groups; g++) {
745  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
746  float last_alpha_q_re = 0;
747  float last_alpha_q_im = 0;
748  if (delta_code_time) {
749  if (g) {
750  /* Transient, after the first group - use the current frame,
751  * previous window, alpha values. */
752  last_alpha_q_re = us->alpha_q_re[(g - 1)*cpe->max_sfb_ste + sfb];
753  last_alpha_q_im = us->alpha_q_im[(g - 1)*cpe->max_sfb_ste + sfb];
754  } else if (!g &&
755  (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) &&
756  (ics->window_sequence[1] == EIGHT_SHORT_SEQUENCE)) {
757  /* The spec doesn't explicitly mention this, but it doesn't make
758  * any other sense otherwise! */
759  const int wg = prev_num_window_groups - 1;
760  last_alpha_q_re = us->prev_alpha_q_re[wg*cpe->max_sfb_ste + sfb];
761  last_alpha_q_im = us->prev_alpha_q_im[wg*cpe->max_sfb_ste + sfb];
762  } else {
763  last_alpha_q_re = us->prev_alpha_q_re[g*cpe->max_sfb_ste + sfb];
764  last_alpha_q_im = us->prev_alpha_q_im[g*cpe->max_sfb_ste + sfb];
765  }
766  } else {
767  if (sfb) {
768  last_alpha_q_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb - 1];
769  last_alpha_q_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb - 1];
770  }
771  }
772 
773  if (us->pred_used[g*cpe->max_sfb_ste + sfb]) {
774  int val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60;
775  last_alpha_q_re += val * 0.1f;
776  if (us->complex_coef) {
777  val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60;
778  last_alpha_q_im += val * 0.1f;
779  }
780  us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = last_alpha_q_re;
781  us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = last_alpha_q_im;
782  } else {
783  us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = 0;
784  us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = 0;
785  }
786 
787  if ((sfb + 1) < cpe->max_sfb_ste) {
788  us->alpha_q_re[g*cpe->max_sfb_ste + sfb + 1] =
789  us->alpha_q_re[g*cpe->max_sfb_ste + sfb];
790  us->alpha_q_im[g*cpe->max_sfb_ste + sfb + 1] =
791  us->alpha_q_im[g*cpe->max_sfb_ste + sfb];
792  }
793  }
794  }
795 
796  return 0;
797 }
798 
800  AACUSACConfig *usac)
801 {
802  AACUsacElemData *ue = &sce->ue;
803  IndividualChannelStream *ics = &sce->ics;
804  const int sampling_index = ac->oc[1].m4ac.sampling_index;
805 
806  /* Setup window parameters */
808  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
809  if (usac->core_frame_len == 768) {
810  ics->swb_offset = ff_swb_offset_96[sampling_index];
811  ics->num_swb = ff_aac_num_swb_96[sampling_index];
812  } else {
813  ics->swb_offset = ff_swb_offset_128[sampling_index];
814  ics->num_swb = ff_aac_num_swb_128[sampling_index];
815  }
816  ics->tns_max_bands = ff_tns_max_bands_usac_128[sampling_index];
817 
818  /* Setup scalefactor grouping. 7 bit mask. */
819  ics->num_window_groups = 0;
820  for (int j = 0; j < 7; j++) {
821  ics->group_len[j] = 1;
822  if (ue->scale_factor_grouping & (1 << (6 - j)))
823  ics->group_len[ics->num_window_groups] += 1;
824  else
825  ics->num_window_groups++;
826  }
827 
828  ics->group_len[7] = 1;
829  ics->num_window_groups++;
830  ics->num_windows = 8;
831  } else {
832  if (usac->core_frame_len == 768) {
833  ics->swb_offset = ff_swb_offset_768[sampling_index];
834  ics->num_swb = ff_aac_num_swb_768[sampling_index];
835  } else {
836  ics->swb_offset = ff_swb_offset_1024[sampling_index];
837  ics->num_swb = ff_aac_num_swb_1024[sampling_index];
838  }
839  ics->tns_max_bands = ff_tns_max_bands_usac_1024[sampling_index];
840 
841  ics->group_len[0] = 1;
842  ics->num_window_groups = 1;
843  ics->num_windows = 1;
844  }
845 
846  if (ics->max_sfb > ics->num_swb) {
848  "Number of scalefactor bands in group (%d) "
849  "exceeds limit (%d).\n",
850  ics->max_sfb, ics->num_swb);
851  ics->max_sfb = 0;
852  return AVERROR(EINVAL);
853  }
854 
855  /* Just some defaults for the band types */
856  for (int i = 0; i < FF_ARRAY_ELEMS(sce->band_type); i++)
857  sce->band_type[i] = ESC_BT;
858 
859  return 0;
860 }
861 
864  GetBitContext *gb, int indep_flag)
865 {
866  int ret, tns_active;
867 
868  AACUsacStereo *us = &cpe->us;
869  SingleChannelElement *sce1 = &cpe->ch[0];
870  SingleChannelElement *sce2 = &cpe->ch[1];
871  IndividualChannelStream *ics1 = &sce1->ics;
872  IndividualChannelStream *ics2 = &sce2->ics;
873  AACUsacElemData *ue1 = &sce1->ue;
874  AACUsacElemData *ue2 = &sce2->ue;
875 
876  us->common_window = 0;
877  us->common_tw = 0;
878 
879  /* Alpha values must always be zeroed out for the current frame,
880  * as they are propagated to the next frame and may be used. */
881  memset(us->alpha_q_re, 0, sizeof(us->alpha_q_re));
882  memset(us->alpha_q_im, 0, sizeof(us->alpha_q_im));
883 
884  if (!(!ue1->core_mode && !ue2->core_mode))
885  return 0;
886 
887  tns_active = get_bits1(gb);
888  us->common_window = get_bits1(gb);
889 
890  if (!us->common_window || indep_flag) {
891  memset(us->prev_alpha_q_re, 0, sizeof(us->prev_alpha_q_re));
892  memset(us->prev_alpha_q_im, 0, sizeof(us->prev_alpha_q_im));
893  }
894 
895  if (us->common_window) {
896  /* ics_info() */
897  ics1->window_sequence[1] = ics1->window_sequence[0];
898  ics2->window_sequence[1] = ics2->window_sequence[0];
899  ics1->window_sequence[0] = ics2->window_sequence[0] = get_bits(gb, 2);
900 
901  ics1->use_kb_window[1] = ics1->use_kb_window[0];
902  ics2->use_kb_window[1] = ics2->use_kb_window[0];
903  ics1->use_kb_window[0] = ics2->use_kb_window[0] = get_bits1(gb);
904 
905  /* If there's a change in the transform sequence, zero out last frame's
906  * stereo prediction coefficients */
907  if ((ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE &&
908  ics1->window_sequence[1] != EIGHT_SHORT_SEQUENCE) ||
909  (ics1->window_sequence[1] == EIGHT_SHORT_SEQUENCE &&
910  ics1->window_sequence[0] != EIGHT_SHORT_SEQUENCE) ||
911  (ics2->window_sequence[0] == EIGHT_SHORT_SEQUENCE &&
912  ics2->window_sequence[1] != EIGHT_SHORT_SEQUENCE) ||
913  (ics2->window_sequence[1] == EIGHT_SHORT_SEQUENCE &&
914  ics2->window_sequence[0] != EIGHT_SHORT_SEQUENCE)) {
915  memset(us->prev_alpha_q_re, 0, sizeof(us->prev_alpha_q_re));
916  memset(us->prev_alpha_q_im, 0, sizeof(us->prev_alpha_q_im));
917  }
918 
919  if (ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
920  ics1->max_sfb = ics2->max_sfb = get_bits(gb, 4);
922  } else {
923  ics1->max_sfb = ics2->max_sfb = get_bits(gb, 6);
924  }
925 
926  if (!get_bits1(gb)) { /* common_max_sfb */
927  if (ics2->window_sequence[0] == EIGHT_SHORT_SEQUENCE)
928  ics2->max_sfb = get_bits(gb, 4);
929  else
930  ics2->max_sfb = get_bits(gb, 6);
931  }
932 
933  ret = setup_sce(ac, sce1, usac);
934  if (ret < 0) {
935  ics2->max_sfb = 0;
936  return ret;
937  }
938 
939  ret = setup_sce(ac, sce2, usac);
940  if (ret < 0)
941  return ret;
942 
943  cpe->max_sfb_ste = FFMAX(ics1->max_sfb, ics2->max_sfb);
944 
945  us->ms_mask_mode = get_bits(gb, 2); /* ms_mask_present */
946  memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
947  if (us->ms_mask_mode == 1) {
948  for (int g = 0; g < ics1->num_window_groups; g++)
949  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++)
950  cpe->ms_mask[g*cpe->max_sfb_ste + sfb] = get_bits1(gb);
951  } else if (us->ms_mask_mode == 2) {
952  memset(cpe->ms_mask, 0xFF, sizeof(cpe->ms_mask));
953  } else if ((us->ms_mask_mode == 3) && !ec->stereo_config_index) {
954  ret = decode_usac_stereo_cplx(ac, us, cpe, gb,
955  ics1->num_window_groups,
957  indep_flag);
958  if (ret < 0)
959  return ret;
960  }
961  }
962 
963  if (ec->tw_mdct) {
964  us->common_tw = get_bits1(gb);
966  "AAC USAC timewarping");
967  return AVERROR_PATCHWELCOME;
968  }
969 
970  us->tns_on_lr = 0;
971  ue1->tns_data_present = ue2->tns_data_present = 0;
972  if (tns_active) {
973  int common_tns = 0;
974  if (us->common_window)
975  common_tns = get_bits1(gb);
976 
977  us->tns_on_lr = get_bits1(gb);
978  if (common_tns) {
979  ret = ff_aac_decode_tns(ac, &sce1->tns, gb, ics1);
980  if (ret < 0)
981  return ret;
982  memcpy(&sce2->tns, &sce1->tns, sizeof(sce1->tns));
983  sce2->tns.present = 1;
984  sce1->tns.present = 1;
985  ue1->tns_data_present = 0;
986  ue2->tns_data_present = 0;
987  } else {
988  if (get_bits1(gb)) {
989  ue1->tns_data_present = 1;
990  ue2->tns_data_present = 1;
991  } else {
992  ue2->tns_data_present = get_bits1(gb);
993  ue1->tns_data_present = !ue2->tns_data_present;
994  }
995  }
996  }
997 
998  return 0;
999 }
1000 
1001 /* 7.2.4 Generation of random signs for spectral noise filling
1002  * This function is exactly defined, though we've helped the definition
1003  * along with being slightly faster. */
1004 static inline float noise_random_sign(unsigned int *seed)
1005 {
1006  unsigned int new_seed = *seed = ((*seed) * 69069) + 5;
1007  if (((new_seed) & 0x10000) > 0)
1008  return -1.f;
1009  return +1.f;
1010 }
1011 
1014 {
1015  float *coef;
1016  IndividualChannelStream *ics = &sce->ics;
1017 
1018  float noise_val = powf(2, ((float)ue->noise.level - 14.0f)/3.0f);
1019  int noise_offset = ue->noise.offset - 16;
1020  int band_off;
1021 
1024 
1025  coef = sce->coeffs;
1026  for (int g = 0; g < ics->num_window_groups; g++) {
1027  unsigned g_len = ics->group_len[g];
1028 
1029  for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1030  float *cb = coef + ics->swb_offset[sfb];
1031  int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb];
1032  int band_quantized_to_zero = 1;
1033 
1034  if (ics->swb_offset[sfb] < band_off)
1035  continue;
1036 
1037  for (int group = 0; group < (unsigned)g_len; group++, cb += 128) {
1038  for (int z = 0; z < cb_len; z++) {
1039  if (cb[z] == 0)
1040  cb[z] = noise_random_sign(&sce->ue.noise.seed) * noise_val;
1041  else
1042  band_quantized_to_zero = 0;
1043  }
1044  }
1045 
1046  if (band_quantized_to_zero) {
1047  sce->sfo[g*ics->max_sfb + sfb] = FFMAX(sce->sfo[g*ics->max_sfb + sfb] + noise_offset, -200);
1048  }
1049  }
1050  coef += g_len << 7;
1051  }
1052 }
1053 
1056 {
1057  IndividualChannelStream *ics = &sce->ics;
1058  float *coef;
1059 
1060  /* Synthesise noise */
1061  if (ue->noise.level)
1062  apply_noise_fill(ac, sce, ue);
1063 
1064  /* Noise filling may apply an offset to the scalefactor offset */
1065  ac->dsp.dequant_scalefactors(sce);
1066 
1067  /* Apply scalefactors */
1068  coef = sce->coeffs;
1069  for (int g = 0; g < ics->num_window_groups; g++) {
1070  unsigned g_len = ics->group_len[g];
1071 
1072  for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1073  float *cb = coef + ics->swb_offset[sfb];
1074  int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb];
1075  float sf = sce->sf[g*ics->max_sfb + sfb];
1076 
1077  for (int group = 0; group < (unsigned)g_len; group++, cb += 128)
1078  ac->fdsp->vector_fmul_scalar(cb, cb, sf, cb_len);
1079  }
1080  coef += g_len << 7;
1081  }
1082 }
1083 
1085  float *dmix_re)
1086 {
1087  IndividualChannelStream *ics = &cpe->ch[0].ics;
1088  int sign = !cpe->us.pred_dir ? +1 : -1;
1089  float *coef1 = cpe->ch[0].coeffs;
1090  float *coef2 = cpe->ch[1].coeffs;
1091 
1092  for (int g = 0; g < ics->num_window_groups; g++) {
1093  unsigned g_len = ics->group_len[g];
1094  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1095  int off = ics->swb_offset[sfb];
1096  int cb_len = ics->swb_offset[sfb + 1] - off;
1097 
1098  float *c1 = coef1 + off;
1099  float *c2 = coef2 + off;
1100  float *dm = dmix_re + off;
1101 
1102  for (int group = 0; group < (unsigned)g_len;
1103  group++, c1 += 128, c2 += 128, dm += 128) {
1104  for (int z = 0; z < cb_len; z++)
1105  dm[z] = 0.5*(c1[z] + sign*c2[z]);
1106  }
1107  }
1108 
1109  coef1 += g_len << 7;
1110  coef2 += g_len << 7;
1111  dmix_re += g_len << 7;
1112  }
1113 }
1114 
1116  float *dmix_re)
1117 {
1118  AACUsacStereo *us = &cpe->us;
1119  IndividualChannelStream *ics = &cpe->ch[0].ics;
1120  int sign = !cpe->us.pred_dir ? +1 : -1;
1121  float *coef1 = cpe->ch[0].coeffs;
1122  float *coef2 = cpe->ch[1].coeffs;
1123 
1124  for (int g = 0; g < ics->num_window_groups; g++) {
1125  unsigned g_len = ics->group_len[g];
1126  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1127  int off = ics->swb_offset[sfb];
1128  int cb_len = ics->swb_offset[sfb + 1] - off;
1129 
1130  float *c1 = coef1 + off;
1131  float *c2 = coef2 + off;
1132  float *dm = dmix_re + off;
1133 
1134  if (us->pred_used[g*cpe->max_sfb_ste + sfb]) {
1135  for (int group = 0; group < (unsigned)g_len;
1136  group++, c1 += 128, c2 += 128, dm += 128) {
1137  for (int z = 0; z < cb_len; z++)
1138  dm[z] = 0.5*(c1[z] + sign*c2[z]);
1139  }
1140  } else {
1141  for (int group = 0; group < (unsigned)g_len;
1142  group++, c1 += 128, c2 += 128, dm += 128) {
1143  for (int z = 0; z < cb_len; z++)
1144  dm[z] = c1[z];
1145  }
1146  }
1147  }
1148 
1149  coef1 += g_len << 7;
1150  coef2 += g_len << 7;
1151  dmix_re += g_len << 7;
1152  }
1153 }
1154 
1155 static void complex_stereo_interpolate_imag(float *im, float *re, const float f[7],
1156  int len, int factor_even, int factor_odd)
1157 {
1158  int i = 0;
1159  float s;
1160 
1161  s = f[6]*re[2] + f[5]*re[1] + f[4]*re[0] +
1162  f[3]*re[0] +
1163  f[2]*re[1] + f[1]*re[2] + f[0]*re[3];
1164  im[i] += s*factor_even;
1165 
1166  i = 1;
1167  s = f[6]*re[1] + f[5]*re[0] + f[4]*re[0] +
1168  f[3]*re[1] +
1169  f[2]*re[2] + f[1]*re[3] + f[0]*re[4];
1170  im[i] += s*factor_odd;
1171 
1172  i = 2;
1173  s = f[6]*re[0] + f[5]*re[0] + f[4]*re[1] +
1174  f[3]*re[2] +
1175  f[2]*re[3] + f[1]*re[4] + f[0]*re[5];
1176 
1177  im[i] += s*factor_even;
1178  for (i = 3; i < len - 4; i += 2) {
1179  s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1180  f[3]*re[i] +
1181  f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+3];
1182  im[i+0] += s*factor_odd;
1183 
1184  s = f[6]*re[i-2] + f[5]*re[i-1] + f[4]*re[i] +
1185  f[3]*re[i+1] +
1186  f[2]*re[i+2] + f[1]*re[i+3] + f[0]*re[i+4];
1187  im[i+1] += s*factor_even;
1188  }
1189 
1190  i = len - 3;
1191  s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1192  f[3]*re[i] +
1193  f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+2];
1194  im[i] += s*factor_odd;
1195 
1196  i = len - 2;
1197  s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1198  f[3]*re[i] +
1199  f[2]*re[i+1] + f[1]*re[i+1] + f[0]*re[i];
1200  im[i] += s*factor_even;
1201 
1202  i = len - 1;
1203  s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1204  f[3]*re[i] +
1205  f[2]*re[i] + f[1]*re[i-1] + f[0]*re[i-2];
1206  im[i] += s*factor_odd;
1207 }
1208 
1210 {
1211  AACUsacStereo *us = &cpe->us;
1212  IndividualChannelStream *ics = &cpe->ch[0].ics;
1213  float *coef1 = cpe->ch[0].coeffs;
1214  float *coef2 = cpe->ch[1].coeffs;
1215  float *dmix_im = us->dmix_im;
1216 
1217  for (int g = 0; g < ics->num_window_groups; g++) {
1218  unsigned g_len = ics->group_len[g];
1219  for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1220  int off = ics->swb_offset[sfb];
1221  int cb_len = ics->swb_offset[sfb + 1] - off;
1222 
1223  float *c1 = coef1 + off;
1224  float *c2 = coef2 + off;
1225  float *dm_im = dmix_im + off;
1226  float alpha_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb];
1227  float alpha_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb];
1228 
1229  if (!us->pred_used[g*cpe->max_sfb_ste + sfb])
1230  continue;
1231 
1232  if (!cpe->us.pred_dir) {
1233  for (int group = 0; group < (unsigned)g_len;
1234  group++, c1 += 128, c2 += 128, dm_im += 128) {
1235  for (int z = 0; z < cb_len; z++) {
1236  float side;
1237  side = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z];
1238  c2[z] = c1[z] - side;
1239  c1[z] = c1[z] + side;
1240  }
1241  }
1242  } else {
1243  for (int group = 0; group < (unsigned)g_len;
1244  group++, c1 += 128, c2 += 128, dm_im += 128) {
1245  for (int z = 0; z < cb_len; z++) {
1246  float mid;
1247  mid = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z];
1248  c2[z] = mid - c1[z];
1249  c1[z] = mid + c1[z];
1250  }
1251  }
1252  }
1253  }
1254 
1255  coef1 += g_len << 7;
1256  coef2 += g_len << 7;
1257  dmix_im += g_len << 7;
1258  }
1259 }
1260 
1261 static const float *complex_stereo_get_filter(ChannelElement *cpe, int is_prev)
1262 {
1263  int win, shape;
1264  if (!is_prev) {
1265  switch (cpe->ch[0].ics.window_sequence[0]) {
1266  default:
1267  case ONLY_LONG_SEQUENCE:
1268  case EIGHT_SHORT_SEQUENCE:
1269  win = 0;
1270  break;
1271  case LONG_START_SEQUENCE:
1272  win = 1;
1273  break;
1274  case LONG_STOP_SEQUENCE:
1275  win = 2;
1276  break;
1277  }
1278 
1279  if (cpe->ch[0].ics.use_kb_window[0] == 0 &&
1280  cpe->ch[0].ics.use_kb_window[1] == 0)
1281  shape = 0;
1282  else if (cpe->ch[0].ics.use_kb_window[0] == 1 &&
1283  cpe->ch[0].ics.use_kb_window[1] == 1)
1284  shape = 1;
1285  else if (cpe->ch[0].ics.use_kb_window[0] == 0 &&
1286  cpe->ch[0].ics.use_kb_window[1] == 1)
1287  shape = 2;
1288  else if (cpe->ch[0].ics.use_kb_window[0] == 1 &&
1289  cpe->ch[0].ics.use_kb_window[1] == 0)
1290  shape = 3;
1291  else
1292  shape = 3;
1293  } else {
1294  win = cpe->ch[0].ics.window_sequence[0] == LONG_STOP_SEQUENCE;
1295  shape = cpe->ch[0].ics.use_kb_window[1];
1296  }
1297 
1298  return ff_aac_usac_mdst_filt_cur[win][shape];
1299 }
1300 
1302  ChannelElement *cpe, int nb_channels)
1303 {
1304  AACUsacStereo *us = &cpe->us;
1305 
1306  for (int ch = 0; ch < nb_channels; ch++) {
1307  SingleChannelElement *sce = &cpe->ch[ch];
1308  AACUsacElemData *ue = &sce->ue;
1309 
1310  if (!ue->core_mode)
1311  spectrum_scale(ac, sce, ue);
1312  }
1313 
1314  if (nb_channels > 1 && us->common_window) {
1315  for (int ch = 0; ch < nb_channels; ch++) {
1316  SingleChannelElement *sce = &cpe->ch[ch];
1317 
1318  /* Apply TNS, if the tns_on_lr bit is not set. */
1319  if (sce->tns.present && !us->tns_on_lr)
1320  ac->dsp.apply_tns(sce->coeffs, &sce->tns, &sce->ics, 1);
1321  }
1322 
1323  if (us->ms_mask_mode == 3) {
1324  const float *filt;
1325  complex_stereo_downmix_cur(ac, cpe, us->dmix_re);
1326  complex_stereo_downmix_prev(ac, cpe, us->prev_dmix_re);
1327 
1328  filt = complex_stereo_get_filter(cpe, 0);
1329  complex_stereo_interpolate_imag(us->dmix_im, us->dmix_re, filt,
1330  usac->core_frame_len, 1, 1);
1331  if (us->use_prev_frame) {
1332  filt = complex_stereo_get_filter(cpe, 1);
1333  complex_stereo_interpolate_imag(us->dmix_im, us->prev_dmix_re, filt,
1334  usac->core_frame_len, -1, 1);
1335  }
1336 
1337  apply_complex_stereo(ac, cpe);
1338  } else if (us->ms_mask_mode > 0) {
1339  ac->dsp.apply_mid_side_stereo(ac, cpe);
1340  }
1341  }
1342 
1343  /* Save coefficients and alpha values for prediction reasons */
1344  if (nb_channels > 1) {
1345  AACUsacStereo *us2 = &cpe->us;
1346  for (int ch = 0; ch < nb_channels; ch++) {
1347  SingleChannelElement *sce = &cpe->ch[ch];
1348  memcpy(sce->prev_coeffs, sce->coeffs, sizeof(sce->coeffs));
1349  }
1350  memcpy(us2->prev_alpha_q_re, us2->alpha_q_re, sizeof(us2->alpha_q_re));
1351  memcpy(us2->prev_alpha_q_im, us2->alpha_q_im, sizeof(us2->alpha_q_im));
1352  }
1353 
1354  for (int ch = 0; ch < nb_channels; ch++) {
1355  SingleChannelElement *sce = &cpe->ch[ch];
1356 
1357  /* Apply TNS, if it hasn't been applied yet. */
1358  if (sce->tns.present && ((nb_channels == 1) || (us->tns_on_lr)))
1359  ac->dsp.apply_tns(sce->coeffs, &sce->tns, &sce->ics, 1);
1360 
1361  if (!sce->ue.core_mode)
1362  ac->oc[1].m4ac.frame_length_short ? ac->dsp.imdct_and_windowing_768(ac, sce) :
1363  ac->dsp.imdct_and_windowing(ac, sce);
1364  }
1365 }
1366 
1367 static const uint8_t mps_fr_nb_bands[8] = {
1368  255 /* Reserved */, 28, 20, 14, 10, 7, 5, 4,
1369 };
1370 
1371 static const uint8_t mps_fr_stride_smg[4] = {
1372  1, 2, 5, 28,
1373 };
1374 
1375 static void decode_tsd(GetBitContext *gb, int *data,
1376  int nb_tr_slots, int nb_slots)
1377 {
1378  int nb_bits = av_log2(nb_slots / (nb_tr_slots + 1));
1379  int s = get_bits(gb, nb_bits);
1380  for (int k = 0; k < nb_slots; k++)
1381  data[k]=0;
1382 
1383  int p = nb_tr_slots + 1;
1384  for (int k = nb_slots - 1; k >= 0; k--) {
1385  if (p > k) {
1386  for (; k >= 0; k--)
1387  data[k] = 1;
1388  break;
1389  }
1390  int64_t c = k - p + 1;
1391  for (int h = 2; h <= p; h++) {
1392  c *= k - p + h;
1393  c /= h;
1394  }
1395  if (s >= (int)c) { /* c is long long for up to 32 slots */
1396  s -= c;
1397  data[k] = 1;
1398  p--;
1399  if (!p)
1400  break;
1401  }
1402  }
1403 }
1404 
1407  GetBitContext *gb, int frame_indep_flag)
1408 {
1409  int err;
1410  int nb_bands = mps_fr_nb_bands[ec->mps.freq_res];
1411 
1412  /* Framing info */
1413  mps->framing_type = 0;
1414  mps->nb_param_sets = 2;
1415  if (ec->mps.high_rate_mode) {
1416  mps->framing_type = get_bits1(gb);
1417  mps->nb_param_sets = get_bits(gb, 3) + 1;
1418  }
1419  int param_slot_bits = usac->core_sbr_frame_len_idx == 4 ? 6 : 5;
1420  int nb_time_slots = usac->core_sbr_frame_len_idx == 4 ? 64 : 32;
1421 
1422  if (mps->framing_type)
1423  for (int i = 0; i < mps->nb_param_sets; i++)
1424  mps->param_sets[i] = get_bits(gb, param_slot_bits);
1425 
1426  int indep = frame_indep_flag;
1427  if (!frame_indep_flag)
1428  indep = get_bits1(gb);
1429 
1430  int extend_frame = mps->param_sets[mps->nb_param_sets - 1] !=
1431  (nb_time_slots - 1);
1432 
1433  /* CLD */
1434  err = ff_aac_ec_data_dec(gb, &mps->ott[MPS_CLD], MPS_CLD,
1435  0, 0, nb_bands,
1436  indep, indep, mps->nb_param_sets);
1437  if (err < 0) {
1438  av_log(ac->avctx, AV_LOG_ERROR, "Error parsing OTT CLD data!\n");
1439  return err;
1440  }
1442  0, 0, nb_bands, mps->nb_param_sets,
1443  mps->param_sets, extend_frame);
1444 
1445  /* ICC */
1446  err = ff_aac_ec_data_dec(gb, &mps->ott[MPS_ICC], MPS_ICC, 0, 0, nb_bands,
1447  indep, indep, mps->nb_param_sets);
1448  if (err < 0) {
1449  av_log(ac->avctx, AV_LOG_ERROR, "Error parsing OTT ICC data!\n");
1450  return err;
1451  }
1453  0, 0, nb_bands, mps->nb_param_sets,
1454  mps->param_sets, extend_frame);
1455 
1456  /* IPD */
1457  if (ec->mps.phase_coding) {
1458  if (get_bits1(gb)) {
1459  mps->opd_smoothing_mode = get_bits1(gb);
1460  err = ff_aac_ec_data_dec(gb, &mps->ott[MPS_IPD], MPS_IPD, 0, 0,
1461  ec->mps.otts_bands_phase,
1462  indep, indep, mps->nb_param_sets);
1464  0, 0, nb_bands, mps->nb_param_sets,
1465  mps->param_sets, extend_frame);
1466  if (err < 0) {
1467  av_log(ac->avctx, AV_LOG_ERROR, "Error parsing OTT IPD data!\n");
1468  return err;
1469  }
1470  }
1471  }
1472 
1473  /* SMG data */
1474  memset(mps->smooth_mode, 0, sizeof(mps->smooth_mode));
1475  if (ec->mps.high_rate_mode) {
1476  for (int i = 0; i < mps->nb_param_sets; i++) {
1477  mps->smooth_mode[i] = get_bits(gb, 2);
1478  if (mps->smooth_mode[i] >= 2)
1479  mps->smooth_time[i] = get_bits(gb, 2);
1480  if (mps->smooth_mode[i] >= 3) {
1481  mps->freq_res_stride_smg[i] = get_bits(gb, 2);
1482  int nb_data_bands = (nb_bands - 1);
1483  nb_data_bands /= (mps_fr_stride_smg[mps->freq_res_stride_smg[i]] + 1);
1484  for (int j = 0; j < nb_data_bands; j++)
1485  mps->smg_data[i][j] = get_bits1(gb);
1486  }
1487  }
1488  }
1489 
1490  /* Temp shape data */
1491  mps->tsd_enable = 0;
1492  if (ec->mps.temp_shape_config == 3) {
1493  mps->tsd_enable = get_bits1(gb);
1494  } else if (ec->mps.temp_shape_config) {
1495  mps->temp_shape_enable = get_bits1(gb);
1496  if (mps->temp_shape_enable) {
1497  for (int i = 0; i < 2; i++)
1498  mps->temp_shape_enable_ch[i] = get_bits1(gb);
1499  if (ec->mps.temp_shape_config == 2) {
1500  err = ff_aac_huff_dec_reshape(gb, mps->temp_shape_data, 16);
1501  if (err < 0) {
1502  av_log(ac->avctx, AV_LOG_ERROR,
1503  "Error parsing TSD reshape data!\n");
1504  return err;
1505  }
1506  }
1507  }
1508  }
1509 
1510  /* TSD data */
1511  if (mps->tsd_enable) {
1512  mps->tsd_num_tr_slots = get_bits(gb, param_slot_bits - 1);
1513  int tsd_pos[64];
1514  decode_tsd(gb, tsd_pos, mps->tsd_num_tr_slots, nb_time_slots);
1515  for (int i = 0; i < nb_time_slots; i++) {
1516  mps->tsd_phase_data[i] = 0;
1517  if (tsd_pos[i])
1518  mps->tsd_phase_data[i] = get_bits(gb, 3);
1519  }
1520  }
1521 
1522  return 0;
1523 }
1524 
1527  GetBitContext *gb, int indep_flag, int nb_channels)
1528 {
1529  int ret;
1530  int arith_reset_flag;
1531  AACUsacStereo *us = &che->us;
1532  int core_nb_channels = nb_channels;
1533 
1534  /* Local symbols */
1535  uint8_t global_gain;
1536 
1537  us->common_window = 0;
1538 
1539  for (int ch = 0; ch < core_nb_channels; ch++) {
1540  SingleChannelElement *sce = &che->ch[ch];
1541  AACUsacElemData *ue = &sce->ue;
1542 
1543  sce->tns.present = 0;
1544  ue->tns_data_present = 0;
1545 
1546  ue->core_mode = get_bits1(gb);
1547  }
1548 
1549  if (nb_channels > 1 && ec->stereo_config_index == 1)
1550  core_nb_channels = 1;
1551 
1552  if (core_nb_channels == 2) {
1553  ret = decode_usac_stereo_info(ac, usac, ec, che, gb, indep_flag);
1554  if (ret)
1555  return ret;
1556  }
1557 
1558  for (int ch = 0; ch < core_nb_channels; ch++) {
1559  SingleChannelElement *sce = &che->ch[ch];
1560  IndividualChannelStream *ics = &sce->ics;
1561  AACUsacElemData *ue = &sce->ue;
1562 
1563  if (ue->core_mode) { /* lpd_channel_stream */
1564  ret = ff_aac_ldp_parse_channel_stream(ac, usac, ue, gb);
1565  if (ret < 0)
1566  return ret;
1567  continue;
1568  }
1569 
1570  if ((core_nb_channels == 1) ||
1571  (che->ch[0].ue.core_mode != che->ch[1].ue.core_mode))
1572  ue->tns_data_present = get_bits1(gb);
1573 
1574  /* fd_channel_stream */
1575  global_gain = get_bits(gb, 8);
1576 
1577  ue->noise.level = 0;
1578  if (ec->noise_fill) {
1579  ue->noise.level = get_bits(gb, 3);
1580  ue->noise.offset = get_bits(gb, 5);
1581  }
1582 
1583  if (!us->common_window) {
1584  /* ics_info() */
1585  ics->window_sequence[1] = ics->window_sequence[0];
1586  ics->window_sequence[0] = get_bits(gb, 2);
1587  ics->use_kb_window[1] = ics->use_kb_window[0];
1588  ics->use_kb_window[0] = get_bits1(gb);
1589  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1590  ics->max_sfb = get_bits(gb, 4);
1591  ue->scale_factor_grouping = get_bits(gb, 7);
1592  } else {
1593  ics->max_sfb = get_bits(gb, 6);
1594  }
1595 
1596  ret = setup_sce(ac, sce, usac);
1597  if (ret < 0)
1598  return ret;
1599  }
1600 
1601  if (ec->tw_mdct && !us->common_tw) {
1602  /* tw_data() */
1603  if (get_bits1(gb)) { /* tw_data_present */
1604  /* Time warping is not supported in baseline profile streams. */
1606  "AAC USAC timewarping");
1607  return AVERROR_PATCHWELCOME;
1608  }
1609  }
1610 
1611  ret = decode_usac_scale_factors(ac, sce, gb, global_gain);
1612  if (ret < 0)
1613  return ret;
1614 
1615  if (ue->tns_data_present) {
1616  sce->tns.present = 1;
1617  ret = ff_aac_decode_tns(ac, &sce->tns, gb, ics);
1618  if (ret < 0)
1619  return ret;
1620  }
1621 
1622  /* ac_spectral_data */
1623  arith_reset_flag = indep_flag;
1624  if (!arith_reset_flag)
1625  arith_reset_flag = get_bits1(gb);
1626 
1627  /* Decode coeffs */
1628  memset(&sce->coeffs[0], 0, 1024*sizeof(float));
1629  for (int win = 0; win < ics->num_windows; win++) {
1630  int lg = ics->swb_offset[ics->max_sfb];
1631  int N;
1632  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE)
1633  N = usac->core_frame_len / 8;
1634  else
1635  N = usac->core_frame_len;
1636 
1637  ret = decode_spectrum_ac(ac, sce->coeffs + win*128, gb, &ue->ac,
1638  arith_reset_flag && (win == 0), lg, N);
1639  if (ret < 0)
1640  return ret;
1641  }
1642 
1643  if (get_bits1(gb)) { /* fac_data_present */
1644  const uint16_t len_8 = usac->core_frame_len / 8;
1645  const uint16_t len_16 = usac->core_frame_len / 16;
1646  const uint16_t fac_len = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ?
1647  len_16 : len_8;
1648  ret = ff_aac_parse_fac_data(ue, gb, 1, fac_len);
1649  if (ret < 0)
1650  return ret;
1651  }
1652  }
1653 
1654  if (ec->sbr.ratio) {
1655  int sbr_ch = nb_channels;
1656  if (nb_channels == 2 &&
1657  !(ec->stereo_config_index == 0 || ec->stereo_config_index == 3))
1658  sbr_ch = 1;
1659 
1660  ret = ff_aac_sbr_decode_usac_data(ac, che, ec, gb, sbr_ch, indep_flag);
1661  if (ret < 0)
1662  return ret;
1663  }
1664 
1665  if (ec->stereo_config_index) {
1666  ret = parse_mps212(ac, usac, &us->mps, ec, gb, indep_flag);
1667  if (ret < 0)
1668  return ret;
1669  }
1670 
1671  spectrum_decode(ac, usac, che, core_nb_channels);
1672 
1673  if (ac->oc[1].m4ac.sbr > 0) {
1674  ac->proc.sbr_apply(ac, che, nb_channels == 2 ? TYPE_CPE : TYPE_SCE,
1675  che->ch[0].output,
1676  che->ch[1].output);
1677  }
1678 
1679  return 0;
1680 }
1681 
1683 {
1684  int ret = 0;
1685  GetBitContext gbc;
1686  OutputConfiguration *oc = &ac->oc[1];
1687  MPEG4AudioConfig *m4ac = &oc->m4ac;
1688  MPEG4AudioConfig m4ac_bak = oc->m4ac;
1689  uint8_t temp_data[512];
1690  uint8_t *tmp_buf = temp_data;
1691  size_t tmp_buf_size = sizeof(temp_data);
1692 
1693  av_unused int crossfade;
1694  int num_preroll_frames;
1695 
1696  int config_len = get_escaped_value(gb, 4, 4, 8);
1697 
1698  /* Implementations are free to pad the config to any length, so use a
1699  * different reader for this. */
1700  gbc = *gb;
1701  ret = ff_aac_usac_config_decode(ac, ac->avctx, &gbc, oc, m4ac->chan_config);
1702  if (ret < 0) {
1703  *m4ac = m4ac_bak;
1704  return ret;
1705  } else {
1706  ac->oc[1].m4ac.chan_config = 0;
1707  }
1708 
1709  /* 7.18.3.3 Bitrate adaption
1710  * If configuration didn't change after applying preroll, continue
1711  * without decoding it. */
1712  if (!memcmp(m4ac, &m4ac_bak, sizeof(m4ac_bak)))
1713  return 0;
1714 
1715  skip_bits_long(gb, config_len*8);
1716 
1717  crossfade = get_bits1(gb); /* applyCrossfade */
1718  skip_bits1(gb); /* reserved */
1719  num_preroll_frames = get_escaped_value(gb, 2, 4, 0); /* numPreRollFrames */
1720 
1721  for (int i = 0; i < num_preroll_frames; i++) {
1722  int got_frame_ptr = 0;
1723  int au_len = get_escaped_value(gb, 16, 16, 0);
1724 
1725  if (au_len*8 > tmp_buf_size) {
1726  uint8_t *tmp2;
1727  tmp_buf = tmp_buf == temp_data ? NULL : tmp_buf;
1728  tmp2 = av_realloc_array(tmp_buf, au_len, 8);
1729  if (!tmp2) {
1730  if (tmp_buf != temp_data)
1731  av_free(tmp_buf);
1732  return AVERROR(ENOMEM);
1733  }
1734  tmp_buf = tmp2;
1735  }
1736 
1737  /* Byte alignment is not guaranteed. */
1738  for (int j = 0; j < au_len; j++)
1739  tmp_buf[j] = get_bits(gb, 8);
1740 
1741  ret = init_get_bits8(&gbc, tmp_buf, au_len);
1742  if (ret < 0)
1743  break;
1744 
1745  ret = ff_aac_usac_decode_frame(ac->avctx, ac, &gbc, &got_frame_ptr);
1746  if (ret < 0)
1747  break;
1748  }
1749 
1750  if (tmp_buf != temp_data)
1751  av_free(tmp_buf);
1752 
1753  return 0;
1754 }
1755 
1757  GetBitContext *gb)
1758 {
1759  uint8_t pl_frag_start = 1;
1760  uint8_t pl_frag_end = 1;
1761  uint32_t len;
1762 
1763  if (!get_bits1(gb)) /* usacExtElementPresent */
1764  return 0;
1765 
1766  if (get_bits1(gb)) { /* usacExtElementUseDefaultLength */
1767  len = e->ext.default_len;
1768  } else {
1769  len = get_bits(gb, 8); /* usacExtElementPayloadLength */
1770  if (len == 255)
1771  len += get_bits(gb, 16) - 2;
1772  }
1773 
1774  if (!len)
1775  return 0;
1776 
1777  if (e->ext.payload_frag) {
1778  pl_frag_start = get_bits1(gb); /* usacExtElementStart */
1779  pl_frag_end = get_bits1(gb); /* usacExtElementStop */
1780  }
1781 
1782  if (pl_frag_start)
1783  e->ext.pl_data_offset = 0;
1784 
1785  /* If an extension starts and ends this packet, we can directly use it below.
1786  * Otherwise, we have to copy it to a buffer and accumulate it. */
1787  if (!(pl_frag_start && pl_frag_end)) {
1788  /* Reallocate the data */
1789  uint8_t *tmp_buf = av_refstruct_alloc_ext(e->ext.pl_data_offset + len,
1791  NULL, NULL);
1792  if (!tmp_buf)
1793  return AVERROR(ENOMEM);
1794 
1795  /* Copy the data over only if we had saved data to begin with */
1796  if (e->ext.pl_buf)
1797  memcpy(tmp_buf, e->ext.pl_buf, e->ext.pl_data_offset);
1798 
1800  e->ext.pl_buf = tmp_buf;
1801 
1802  /* Readout data to a buffer */
1803  for (int i = 0; i < len; i++)
1804  e->ext.pl_buf[e->ext.pl_data_offset + i] = get_bits(gb, 8);
1805  }
1806 
1807  e->ext.pl_data_offset += len;
1808 
1809  if (pl_frag_end) {
1810  int ret = 0;
1811  int start_bits = get_bits_count(gb);
1812  const int pl_len = e->ext.pl_data_offset;
1813  GetBitContext *gb2 = gb;
1814  GetBitContext gbc;
1815  if (!(pl_frag_start && pl_frag_end)) {
1816  ret = init_get_bits8(&gbc, e->ext.pl_buf, pl_len);
1817  if (ret < 0)
1818  return ret;
1819 
1820  gb2 = &gbc;
1821  }
1822 
1823  switch (e->ext.type) {
1824  case ID_EXT_ELE_FILL:
1825  /* Filler elements have no usable payload */
1826  break;
1828  ret = parse_audio_preroll(ac, gb2);
1829  break;
1830  default:
1831  /* This should never happen */
1832  av_assert0(0);
1833  }
1835  if (ret < 0)
1836  return ret;
1837 
1838  skip_bits_long(gb, pl_len*8 - (get_bits_count(gb) - start_bits));
1839  }
1840 
1841  return 0;
1842 }
1843 
1845  GetBitContext *gb, int *got_frame_ptr)
1846 {
1847  int ret, is_dmono = 0;
1848  int indep_flag, samples = 0;
1849  int audio_found = 0;
1850  int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
1851  AVFrame *frame = ac->frame;
1852 
1853  int ratio_mult, ratio_dec;
1854  AACUSACConfig *usac = &ac->oc[1].usac;
1855  int sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
1856  usac->core_sbr_frame_len_idx == 3 ? 3 :
1857  usac->core_sbr_frame_len_idx == 4 ? 1 :
1858  0;
1859 
1860  if (sbr_ratio == 2) {
1861  ratio_mult = 8;
1862  ratio_dec = 3;
1863  } else if (sbr_ratio == 3) {
1864  ratio_mult = 2;
1865  ratio_dec = 1;
1866  } else if (sbr_ratio == 4) {
1867  ratio_mult = 4;
1868  ratio_dec = 1;
1869  } else {
1870  ratio_mult = 1;
1871  ratio_dec = 1;
1872  }
1873 
1875  ac->oc[1].status, 0);
1876 
1878 
1879  indep_flag = get_bits1(gb);
1880 
1881  for (int i = 0; i < ac->oc[1].usac.nb_elems; i++) {
1882  int layout_id;
1883  int layout_type;
1884  AACUsacElemConfig *e = &ac->oc[1].usac.elems[i];
1885  ChannelElement *che;
1886 
1887  if (e->type == ID_USAC_SCE) {
1888  layout_id = elem_id[0]++;
1889  layout_type = TYPE_SCE;
1890  che = ff_aac_get_che(ac, TYPE_SCE, layout_id);
1891  } else if (e->type == ID_USAC_CPE) {
1892  layout_id = elem_id[1]++;
1893  layout_type = TYPE_CPE;
1894  che = ff_aac_get_che(ac, TYPE_CPE, layout_id);
1895  } else if (e->type == ID_USAC_LFE) {
1896  layout_id = elem_id[2]++;
1897  layout_type = TYPE_LFE;
1898  che = ff_aac_get_che(ac, TYPE_LFE, layout_id);
1899  }
1900 
1901  if (e->type != ID_USAC_EXT && !che) {
1902  av_log(ac->avctx, AV_LOG_ERROR,
1903  "channel element %d.%d is not allocated\n",
1904  layout_type, layout_id);
1905  return AVERROR_INVALIDDATA;
1906  }
1907 
1908  switch (e->type) {
1909  case ID_USAC_LFE:
1910  /* Fallthrough */
1911  case ID_USAC_SCE:
1912  ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb,
1913  indep_flag, 1);
1914  if (ret < 0)
1915  return ret;
1916 
1917  audio_found = 1;
1918  che->present = 1;
1919  break;
1920  case ID_USAC_CPE:
1921  ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb,
1922  indep_flag, 2);
1923  if (ret < 0)
1924  return ret;
1925 
1926  audio_found = 1;
1927  che->present = 1;
1928  break;
1929  case ID_USAC_EXT:
1930  ret = parse_ext_ele(ac, e, gb);
1931  if (ret < 0)
1932  return ret;
1933  break;
1934  }
1935  }
1936 
1937  if (audio_found)
1938  samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024;
1939 
1940  samples = (samples * ratio_mult) / ratio_dec;
1941 
1942  if (ac->oc[1].status && audio_found) {
1943  avctx->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
1944  avctx->frame_size = samples;
1945  ac->oc[1].status = OC_LOCKED;
1946  }
1947 
1948  if (!frame->data[0] && samples) {
1949  av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
1950  return AVERROR_INVALIDDATA;
1951  }
1952 
1953  if (samples) {
1954  frame->nb_samples = samples;
1955  frame->sample_rate = avctx->sample_rate;
1956  frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0;
1957  *got_frame_ptr = 1;
1958  } else {
1959  av_frame_unref(ac->frame);
1960  frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0;
1961  *got_frame_ptr = 0;
1962  }
1963 
1964  /* for dual-mono audio (SCE + SCE) */
1965  is_dmono = ac->dmono_mode && elem_id[0] == 2 &&
1968  if (is_dmono) {
1969  if (ac->dmono_mode == 1)
1970  frame->data[1] = frame->data[0];
1971  else if (ac->dmono_mode == 2)
1972  frame->data[0] = frame->data[1];
1973  }
1974 
1975  return 0;
1976 }
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:1063
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:862
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:1036
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:1301
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:354
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:156
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:1371
complex_stereo_downmix_cur
static void complex_stereo_downmix_cur(AACDecContext *ac, ChannelElement *cpe, float *dmix_re)
Definition: aacdec_usac.c:1115
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
state
static struct @580 state
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:1051
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:1756
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:613
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:599
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:1155
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:1405
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:580
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:1375
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:1844
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
if
if(ret)
Definition: filter_design.txt:179
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
AVChannelLayout::u
union AVChannelLayout::@513 u
Details about which channels are present in this layout.
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:1367
parse_audio_preroll
static int parse_audio_preroll(AACDecContext *ac, GetBitContext *gb)
Definition: aacdec_usac.c:1682
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:645
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:708
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:1054
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:1012
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:1261
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:1084
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:1004
apply_complex_stereo
static void apply_complex_stereo(AACDecContext *ac, ChannelElement *cpe)
Definition: aacdec_usac.c:1209
aacdec_tab.h
setup_sce
static int setup_sce(AACDecContext *ac, SingleChannelElement *sce, AACUSACConfig *usac)
Definition: aacdec_usac.c:799
AACUSACLoudnessInfo
Definition: aacdec.h:303
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1630
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:295
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:786
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:1525
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:686
AACArithState
Definition: aacdec_ac.h:27
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:256
ID_USAC_SCE
@ ID_USAC_SCE
Definition: aacdec.h:76