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