FFmpeg  4.4.7
aacdec_template.c
Go to the documentation of this file.
1 /*
2  * AAC decoder
3  * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4  * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
5  * Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
6  *
7  * AAC LATM decoder
8  * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
9  * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
10  *
11  * AAC decoder fixed-point implementation
12  * Copyright (c) 2013
13  * MIPS Technologies, Inc., California.
14  *
15  * This file is part of FFmpeg.
16  *
17  * FFmpeg is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  *
22  * FFmpeg is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  * Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with FFmpeg; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30  */
31 
32 /**
33  * @file
34  * AAC decoder
35  * @author Oded Shimon ( ods15 ods15 dyndns org )
36  * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
37  *
38  * AAC decoder fixed-point implementation
39  * @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
40  * @author Nedeljko Babic ( nedeljko.babic imgtec com )
41  */
42 
43 /*
44  * supported tools
45  *
46  * Support? Name
47  * N (code in SoC repo) gain control
48  * Y block switching
49  * Y window shapes - standard
50  * N window shapes - Low Delay
51  * Y filterbank - standard
52  * N (code in SoC repo) filterbank - Scalable Sample Rate
53  * Y Temporal Noise Shaping
54  * Y Long Term Prediction
55  * Y intensity stereo
56  * Y channel coupling
57  * Y frequency domain prediction
58  * Y Perceptual Noise Substitution
59  * Y Mid/Side stereo
60  * N Scalable Inverse AAC Quantization
61  * N Frequency Selective Switch
62  * N upsampling filter
63  * Y quantization & coding - AAC
64  * N quantization & coding - TwinVQ
65  * N quantization & coding - BSAC
66  * N AAC Error Resilience tools
67  * N Error Resilience payload syntax
68  * N Error Protection tool
69  * N CELP
70  * N Silence Compression
71  * N HVXC
72  * N HVXC 4kbits/s VR
73  * N Structured Audio tools
74  * N Structured Audio Sample Bank Format
75  * N MIDI
76  * N Harmonic and Individual Lines plus Noise
77  * N Text-To-Speech Interface
78  * Y Spectral Band Replication
79  * Y (not in this code) Layer-1
80  * Y (not in this code) Layer-2
81  * Y (not in this code) Layer-3
82  * N SinuSoidal Coding (Transient, Sinusoid, Noise)
83  * Y Parametric Stereo
84  * N Direct Stream Transfer
85  * Y (not in fixed point code) Enhanced AAC Low Delay (ER AAC ELD)
86  *
87  * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
88  * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
89  Parametric Stereo.
90  */
91 
92 #include "libavutil/thread.h"
93 
95 static VLC vlc_spectral[11];
96 
97 static int output_configure(AACContext *ac,
98  uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
99  enum OCStatus oc_type, int get_new_frame);
100 
101 #define overread_err "Input buffer exhausted before END element found\n"
102 
103 static int count_channels(uint8_t (*layout)[3], int tags)
104 {
105  int i, sum = 0;
106  for (i = 0; i < tags; i++) {
107  int syn_ele = layout[i][0];
108  int pos = layout[i][2];
109  sum += (1 + (syn_ele == TYPE_CPE)) *
111  }
112  return sum;
113 }
114 
115 /**
116  * Check for the channel element in the current channel position configuration.
117  * If it exists, make sure the appropriate element is allocated and map the
118  * channel order to match the internal FFmpeg channel layout.
119  *
120  * @param che_pos current channel position configuration
121  * @param type channel element type
122  * @param id channel element id
123  * @param channels count of the number of channels in the configuration
124  *
125  * @return Returns error status. 0 - OK, !0 - error
126  */
128  enum ChannelPosition che_pos,
129  int type, int id, int *channels)
130 {
131  if (*channels >= MAX_CHANNELS)
132  return AVERROR_INVALIDDATA;
133  if (che_pos) {
134  if (!ac->che[type][id]) {
135  if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
136  return AVERROR(ENOMEM);
138  }
139  if (type != TYPE_CCE) {
140  if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
141  av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
142  return AVERROR_INVALIDDATA;
143  }
144  ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
145  if (type == TYPE_CPE ||
146  (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
147  ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
148  }
149  }
150  } else {
151  if (ac->che[type][id])
153  av_freep(&ac->che[type][id]);
154  }
155  return 0;
156 }
157 
159 {
160  AACContext *ac = avctx->priv_data;
161  int type, id, ch, ret;
162 
163  /* set channel pointers to internal buffers by default */
164  for (type = 0; type < 4; type++) {
165  for (id = 0; id < MAX_ELEM_ID; id++) {
166  ChannelElement *che = ac->che[type][id];
167  if (che) {
168  che->ch[0].ret = che->ch[0].ret_buf;
169  che->ch[1].ret = che->ch[1].ret_buf;
170  }
171  }
172  }
173 
174  /* get output buffer */
175  av_frame_unref(ac->frame);
176  if (!avctx->channels)
177  return 1;
178 
179  ac->frame->nb_samples = 2048;
180  if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
181  return ret;
182 
183  /* map output channel pointers to AVFrame data */
184  for (ch = 0; ch < avctx->channels; ch++) {
185  if (ac->output_element[ch])
186  ac->output_element[ch]->ret = (INTFLOAT *)ac->frame->extended_data[ch];
187  }
188 
189  return 0;
190 }
191 
193  uint64_t av_position;
197 };
198 
199 static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
200  uint8_t (*layout_map)[3], int offset, uint64_t left,
201  uint64_t right, int pos, uint64_t *layout)
202 {
203  if (layout_map[offset][0] == TYPE_CPE) {
204  e2c_vec[offset] = (struct elem_to_channel) {
205  .av_position = left | right,
206  .syn_ele = TYPE_CPE,
207  .elem_id = layout_map[offset][1],
208  .aac_position = pos
209  };
210  if (e2c_vec[offset].av_position != UINT64_MAX)
211  *layout |= e2c_vec[offset].av_position;
212 
213  return 1;
214  } else {
215  e2c_vec[offset] = (struct elem_to_channel) {
216  .av_position = left,
217  .syn_ele = TYPE_SCE,
218  .elem_id = layout_map[offset][1],
219  .aac_position = pos
220  };
221  e2c_vec[offset + 1] = (struct elem_to_channel) {
222  .av_position = right,
223  .syn_ele = TYPE_SCE,
224  .elem_id = layout_map[offset + 1][1],
225  .aac_position = pos
226  };
227  if (left != UINT64_MAX)
228  *layout |= left;
229 
230  if (right != UINT64_MAX)
231  *layout |= right;
232 
233  return 2;
234  }
235 }
236 
237 static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
238  int *current)
239 {
240  int num_pos_channels = 0;
241  int first_cpe = 0;
242  int sce_parity = 0;
243  int i;
244  for (i = *current; i < tags; i++) {
245  if (layout_map[i][2] != pos)
246  break;
247  if (layout_map[i][0] == TYPE_CPE) {
248  if (sce_parity) {
249  if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
250  sce_parity = 0;
251  } else {
252  return -1;
253  }
254  }
255  num_pos_channels += 2;
256  first_cpe = 1;
257  } else {
258  num_pos_channels++;
259  sce_parity ^= 1;
260  }
261  }
262  if (sce_parity &&
263  ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
264  return -1;
265  *current = i;
266  return num_pos_channels;
267 }
268 
269 #define PREFIX_FOR_22POINT2 (AV_CH_LAYOUT_7POINT1_WIDE_BACK|AV_CH_BACK_CENTER|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_LOW_FREQUENCY_2)
270 static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
271 {
272  int i, n, total_non_cc_elements;
273  struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
274  int num_front_channels, num_side_channels, num_back_channels;
275  uint64_t layout = 0;
276 
277  if (FF_ARRAY_ELEMS(e2c_vec) < tags)
278  return 0;
279 
280  i = 0;
281  num_front_channels =
282  count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
283  if (num_front_channels < 0)
284  return 0;
285  num_side_channels =
286  count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
287  if (num_side_channels < 0)
288  return 0;
289  num_back_channels =
290  count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
291  if (num_back_channels < 0)
292  return 0;
293 
294  if (num_side_channels == 0 && num_back_channels >= 4) {
295  num_side_channels = 2;
296  num_back_channels -= 2;
297  }
298 
299  i = 0;
300  if (num_front_channels & 1) {
301  e2c_vec[i] = (struct elem_to_channel) {
303  .syn_ele = TYPE_SCE,
304  .elem_id = layout_map[i][1],
305  .aac_position = AAC_CHANNEL_FRONT
306  };
307  layout |= e2c_vec[i].av_position;
308  i++;
309  num_front_channels--;
310  }
311  if (num_front_channels >= 4) {
312  i += assign_pair(e2c_vec, layout_map, i,
316  num_front_channels -= 2;
317  }
318  if (num_front_channels >= 2) {
319  i += assign_pair(e2c_vec, layout_map, i,
323  num_front_channels -= 2;
324  }
325  while (num_front_channels >= 2) {
326  i += assign_pair(e2c_vec, layout_map, i,
327  UINT64_MAX,
328  UINT64_MAX,
330  num_front_channels -= 2;
331  }
332 
333  if (num_side_channels >= 2) {
334  i += assign_pair(e2c_vec, layout_map, i,
338  num_side_channels -= 2;
339  }
340  while (num_side_channels >= 2) {
341  i += assign_pair(e2c_vec, layout_map, i,
342  UINT64_MAX,
343  UINT64_MAX,
345  num_side_channels -= 2;
346  }
347 
348  while (num_back_channels >= 4) {
349  i += assign_pair(e2c_vec, layout_map, i,
350  UINT64_MAX,
351  UINT64_MAX,
353  num_back_channels -= 2;
354  }
355  if (num_back_channels >= 2) {
356  i += assign_pair(e2c_vec, layout_map, i,
360  num_back_channels -= 2;
361  }
362  if (num_back_channels) {
363  e2c_vec[i] = (struct elem_to_channel) {
365  .syn_ele = TYPE_SCE,
366  .elem_id = layout_map[i][1],
367  .aac_position = AAC_CHANNEL_BACK
368  };
369  layout |= e2c_vec[i].av_position;
370  i++;
371  num_back_channels--;
372  }
373 
374  if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
375  e2c_vec[i] = (struct elem_to_channel) {
377  .syn_ele = TYPE_LFE,
378  .elem_id = layout_map[i][1],
379  .aac_position = AAC_CHANNEL_LFE
380  };
381  layout |= e2c_vec[i].av_position;
382  i++;
383  }
384  if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
385  e2c_vec[i] = (struct elem_to_channel) {
387  .syn_ele = TYPE_LFE,
388  .elem_id = layout_map[i][1],
389  .aac_position = AAC_CHANNEL_LFE
390  };
391  layout |= e2c_vec[i].av_position;
392  i++;
393  }
394  while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
395  e2c_vec[i] = (struct elem_to_channel) {
396  .av_position = UINT64_MAX,
397  .syn_ele = TYPE_LFE,
398  .elem_id = layout_map[i][1],
399  .aac_position = AAC_CHANNEL_LFE
400  };
401  i++;
402  }
403 
404  // The previous checks would end up at 8 at this point for 22.2
405  if (layout == PREFIX_FOR_22POINT2 && tags == 16 && i == 8) {
406  const uint8_t (*reference_layout_map)[3] = aac_channel_layout_map[12];
407  for (int j = 0; j < tags; j++) {
408  if (layout_map[j][0] != reference_layout_map[j][0] ||
409  layout_map[j][2] != reference_layout_map[j][2])
410  goto end_of_layout_definition;
411  }
412 
413  e2c_vec[i] = (struct elem_to_channel) {
415  .syn_ele = layout_map[i][0],
416  .elem_id = layout_map[i][1],
417  .aac_position = layout_map[i][2]
418  }; layout |= e2c_vec[i].av_position; i++;
419  i += assign_pair(e2c_vec, layout_map, i,
423  &layout);
424  i += assign_pair(e2c_vec, layout_map, i,
428  &layout);
429  e2c_vec[i] = (struct elem_to_channel) {
431  .syn_ele = layout_map[i][0],
432  .elem_id = layout_map[i][1],
433  .aac_position = layout_map[i][2]
434  }; layout |= e2c_vec[i].av_position; i++;
435  i += assign_pair(e2c_vec, layout_map, i,
439  &layout);
440  e2c_vec[i] = (struct elem_to_channel) {
442  .syn_ele = layout_map[i][0],
443  .elem_id = layout_map[i][1],
444  .aac_position = layout_map[i][2]
445  }; layout |= e2c_vec[i].av_position; i++;
446  e2c_vec[i] = (struct elem_to_channel) {
448  .syn_ele = layout_map[i][0],
449  .elem_id = layout_map[i][1],
450  .aac_position = layout_map[i][2]
451  }; layout |= e2c_vec[i].av_position; i++;
452  i += assign_pair(e2c_vec, layout_map, i,
456  &layout);
457  }
458 
459 end_of_layout_definition:
460 
461  total_non_cc_elements = n = i;
462 
463  if (layout == AV_CH_LAYOUT_22POINT2) {
464  // For 22.2 reorder the result as needed
465  FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[0]); // FL & FR first (final), FC third
466  FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[1]); // FC second (final), FLc & FRc third
467  FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[2]); // LFE1 third (final), FLc & FRc seventh
468  FFSWAP(struct elem_to_channel, e2c_vec[4], e2c_vec[3]); // BL & BR fourth (final), SiL & SiR fifth
469  FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[4]); // FLc & FRc fifth (final), SiL & SiR seventh
470  FFSWAP(struct elem_to_channel, e2c_vec[7], e2c_vec[6]); // LFE2 seventh (final), SiL & SiR eight (final)
471  FFSWAP(struct elem_to_channel, e2c_vec[9], e2c_vec[8]); // TpFL & TpFR ninth (final), TFC tenth (final)
472  FFSWAP(struct elem_to_channel, e2c_vec[11], e2c_vec[10]); // TC eleventh (final), TpSiL & TpSiR twelth
473  FFSWAP(struct elem_to_channel, e2c_vec[12], e2c_vec[11]); // TpBL & TpBR twelth (final), TpSiL & TpSiR thirteenth (final)
474  } else {
475  // For everything else, utilize the AV channel position define as a
476  // stable sort.
477  do {
478  int next_n = 0;
479  for (i = 1; i < n; i++)
480  if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
481  FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
482  next_n = i;
483  }
484  n = next_n;
485  } while (n > 0);
486 
487  }
488 
489  for (i = 0; i < total_non_cc_elements; i++) {
490  layout_map[i][0] = e2c_vec[i].syn_ele;
491  layout_map[i][1] = e2c_vec[i].elem_id;
492  layout_map[i][2] = e2c_vec[i].aac_position;
493  }
494 
495  return layout;
496 }
497 
498 /**
499  * Save current output configuration if and only if it has been locked.
500  */
502  int pushed = 0;
503 
504  if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
505  ac->oc[0] = ac->oc[1];
506  pushed = 1;
507  }
508  ac->oc[1].status = OC_NONE;
509  return pushed;
510 }
511 
512 /**
513  * Restore the previous output configuration if and only if the current
514  * configuration is unlocked.
515  */
517  if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
518  ac->oc[1] = ac->oc[0];
519  ac->avctx->channels = ac->oc[1].channels;
520  ac->avctx->channel_layout = ac->oc[1].channel_layout;
521  output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
522  ac->oc[1].status, 0);
523  }
524 }
525 
526 /**
527  * Configure output channel order based on the current program
528  * configuration element.
529  *
530  * @return Returns error status. 0 - OK, !0 - error
531  */
533  uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
534  enum OCStatus oc_type, int get_new_frame)
535 {
536  AVCodecContext *avctx = ac->avctx;
537  int i, channels = 0, ret;
538  uint64_t layout = 0;
539  uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
540  uint8_t type_counts[TYPE_END] = { 0 };
541 
542  if (get_new_frame && !ac->frame)
543  return AVERROR_INVALIDDATA;
544 
545  if (ac->oc[1].layout_map != layout_map) {
546  memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
547  ac->oc[1].layout_map_tags = tags;
548  }
549  for (i = 0; i < tags; i++) {
550  int type = layout_map[i][0];
551  int id = layout_map[i][1];
552  id_map[type][id] = type_counts[type]++;
553  if (id_map[type][id] >= MAX_ELEM_ID) {
554  avpriv_request_sample(ac->avctx, "Too large remapped id");
555  return AVERROR_PATCHWELCOME;
556  }
557  }
558  // Try to sniff a reasonable channel order, otherwise output the
559  // channels in the order the PCE declared them.
561  layout = sniff_channel_order(layout_map, tags);
562  for (i = 0; i < tags; i++) {
563  int type = layout_map[i][0];
564  int id = layout_map[i][1];
565  int iid = id_map[type][id];
566  int position = layout_map[i][2];
567  // Allocate or free elements depending on if they are in the
568  // current program configuration.
569  ret = che_configure(ac, position, type, iid, &channels);
570  if (ret < 0)
571  return ret;
572  ac->tag_che_map[type][id] = ac->che[type][iid];
573  }
574  if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
575  if (layout == AV_CH_FRONT_CENTER) {
577  } else {
578  layout = 0;
579  }
580  }
581 
582  if (layout) avctx->channel_layout = layout;
583  ac->oc[1].channel_layout = layout;
584  avctx->channels = ac->oc[1].channels = channels;
585  ac->oc[1].status = oc_type;
586 
587  if (get_new_frame) {
588  if ((ret = frame_configure_elements(ac->avctx)) < 0)
589  return ret;
590  }
591 
592  return 0;
593 }
594 
595 static void flush(AVCodecContext *avctx)
596 {
597  AACContext *ac= avctx->priv_data;
598  int type, i, j;
599 
600  for (type = 3; type >= 0; type--) {
601  for (i = 0; i < MAX_ELEM_ID; i++) {
602  ChannelElement *che = ac->che[type][i];
603  if (che) {
604  for (j = 0; j <= 1; j++) {
605  memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
606  }
607  }
608  }
609  }
610 }
611 
612 /**
613  * Set up channel positions based on a default channel configuration
614  * as specified in table 1.17.
615  *
616  * @return Returns error status. 0 - OK, !0 - error
617  */
619  uint8_t (*layout_map)[3],
620  int *tags,
621  int channel_config)
622 {
623  if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
624  channel_config > 13) {
625  av_log(avctx, AV_LOG_ERROR,
626  "invalid default channel configuration (%d)\n",
627  channel_config);
628  return AVERROR_INVALIDDATA;
629  }
630  *tags = tags_per_config[channel_config];
631  memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
632  *tags * sizeof(*layout_map));
633 
634  /*
635  * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
636  * However, at least Nero AAC encoder encodes 7.1 streams using the default
637  * channel config 7, mapping the side channels of the original audio stream
638  * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
639  * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
640  * the incorrect streams as if they were correct (and as the encoder intended).
641  *
642  * As actual intended 7.1(wide) streams are very rare, default to assuming a
643  * 7.1 layout was intended.
644  */
645  if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
646  layout_map[2][2] = AAC_CHANNEL_SIDE;
647 
648  if (!ac || !ac->warned_71_wide++) {
649  av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
650  " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
651  " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
652  }
653  }
654 
655  return 0;
656 }
657 
658 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
659 {
660  /* For PCE based channel configurations map the channels solely based
661  * on tags. */
662  if (!ac->oc[1].m4ac.chan_config) {
663  return ac->tag_che_map[type][elem_id];
664  }
665  // Allow single CPE stereo files to be signalled with mono configuration.
666  if (!ac->tags_mapped && type == TYPE_CPE &&
667  ac->oc[1].m4ac.chan_config == 1) {
668  uint8_t layout_map[MAX_ELEM_ID*4][3];
669  int layout_map_tags;
671 
672  av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
673 
674  if (set_default_channel_config(ac, ac->avctx, layout_map,
675  &layout_map_tags, 2) < 0)
676  return NULL;
677  if (output_configure(ac, layout_map, layout_map_tags,
678  OC_TRIAL_FRAME, 1) < 0)
679  return NULL;
680 
681  ac->oc[1].m4ac.chan_config = 2;
682  ac->oc[1].m4ac.ps = 0;
683  }
684  // And vice-versa
685  if (!ac->tags_mapped && type == TYPE_SCE &&
686  ac->oc[1].m4ac.chan_config == 2) {
687  uint8_t layout_map[MAX_ELEM_ID * 4][3];
688  int layout_map_tags;
690 
691  av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
692 
693  if (set_default_channel_config(ac, ac->avctx, layout_map,
694  &layout_map_tags, 1) < 0)
695  return NULL;
696  if (output_configure(ac, layout_map, layout_map_tags,
697  OC_TRIAL_FRAME, 1) < 0)
698  return NULL;
699 
700  ac->oc[1].m4ac.chan_config = 1;
701  if (ac->oc[1].m4ac.sbr)
702  ac->oc[1].m4ac.ps = -1;
703  }
704  /* For indexed channel configurations map the channels solely based
705  * on position. */
706  switch (ac->oc[1].m4ac.chan_config) {
707  case 13:
708  if (ac->tags_mapped > 3 && ((type == TYPE_CPE && elem_id < 8) ||
709  (type == TYPE_SCE && elem_id < 6) ||
710  (type == TYPE_LFE && elem_id < 2))) {
711  ac->tags_mapped++;
712  return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
713  }
714  case 12:
715  case 7:
716  if (ac->tags_mapped == 3 && type == TYPE_CPE) {
717  ac->tags_mapped++;
718  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
719  }
720  case 11:
721  if (ac->tags_mapped == 2 &&
722  ac->oc[1].m4ac.chan_config == 11 &&
723  type == TYPE_SCE) {
724  ac->tags_mapped++;
725  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
726  }
727  case 6:
728  /* Some streams incorrectly code 5.1 audio as
729  * SCE[0] CPE[0] CPE[1] SCE[1]
730  * instead of
731  * SCE[0] CPE[0] CPE[1] LFE[0].
732  * If we seem to have encountered such a stream, transfer
733  * the LFE[0] element to the SCE[1]'s mapping */
734  if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
735  if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
737  "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
738  type == TYPE_SCE ? "SCE" : "LFE", elem_id);
739  ac->warned_remapping_once++;
740  }
741  ac->tags_mapped++;
742  return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
743  }
744  case 5:
745  if (ac->tags_mapped == 2 && type == TYPE_CPE) {
746  ac->tags_mapped++;
747  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
748  }
749  case 4:
750  /* Some streams incorrectly code 4.0 audio as
751  * SCE[0] CPE[0] LFE[0]
752  * instead of
753  * SCE[0] CPE[0] SCE[1].
754  * If we seem to have encountered such a stream, transfer
755  * the SCE[1] element to the LFE[0]'s mapping */
756  if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
757  if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
759  "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
760  type == TYPE_SCE ? "SCE" : "LFE", elem_id);
761  ac->warned_remapping_once++;
762  }
763  ac->tags_mapped++;
764  return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
765  }
766  if (ac->tags_mapped == 2 &&
767  ac->oc[1].m4ac.chan_config == 4 &&
768  type == TYPE_SCE) {
769  ac->tags_mapped++;
770  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
771  }
772  case 3:
773  case 2:
774  if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
775  type == TYPE_CPE) {
776  ac->tags_mapped++;
777  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
778  } else if (ac->oc[1].m4ac.chan_config == 2) {
779  return NULL;
780  }
781  case 1:
782  if (!ac->tags_mapped && type == TYPE_SCE) {
783  ac->tags_mapped++;
784  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
785  }
786  default:
787  return NULL;
788  }
789 }
790 
791 /**
792  * Decode an array of 4 bit element IDs, optionally interleaved with a
793  * stereo/mono switching bit.
794  *
795  * @param type speaker type/position for these channels
796  */
797 static void decode_channel_map(uint8_t layout_map[][3],
798  enum ChannelPosition type,
799  GetBitContext *gb, int n)
800 {
801  while (n--) {
802  enum RawDataBlockType syn_ele;
803  switch (type) {
804  case AAC_CHANNEL_FRONT:
805  case AAC_CHANNEL_BACK:
806  case AAC_CHANNEL_SIDE:
807  syn_ele = get_bits1(gb);
808  break;
809  case AAC_CHANNEL_CC:
810  skip_bits1(gb);
811  syn_ele = TYPE_CCE;
812  break;
813  case AAC_CHANNEL_LFE:
814  syn_ele = TYPE_LFE;
815  break;
816  default:
817  // AAC_CHANNEL_OFF has no channel map
818  av_assert0(0);
819  }
820  layout_map[0][0] = syn_ele;
821  layout_map[0][1] = get_bits(gb, 4);
822  layout_map[0][2] = type;
823  layout_map++;
824  }
825 }
826 
827 static inline void relative_align_get_bits(GetBitContext *gb,
828  int reference_position) {
829  int n = (reference_position - get_bits_count(gb) & 7);
830  if (n)
831  skip_bits(gb, n);
832 }
833 
834 /**
835  * Decode program configuration element; reference: table 4.2.
836  *
837  * @return Returns error status. 0 - OK, !0 - error
838  */
839 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
840  uint8_t (*layout_map)[3],
841  GetBitContext *gb, int byte_align_ref)
842 {
843  int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
844  int sampling_index;
845  int comment_len;
846  int tags;
847 
848  skip_bits(gb, 2); // object_type
849 
850  sampling_index = get_bits(gb, 4);
851  if (m4ac->sampling_index != sampling_index)
852  av_log(avctx, AV_LOG_WARNING,
853  "Sample rate index in program config element does not "
854  "match the sample rate index configured by the container.\n");
855 
856  num_front = get_bits(gb, 4);
857  num_side = get_bits(gb, 4);
858  num_back = get_bits(gb, 4);
859  num_lfe = get_bits(gb, 2);
860  num_assoc_data = get_bits(gb, 3);
861  num_cc = get_bits(gb, 4);
862 
863  if (get_bits1(gb))
864  skip_bits(gb, 4); // mono_mixdown_tag
865  if (get_bits1(gb))
866  skip_bits(gb, 4); // stereo_mixdown_tag
867 
868  if (get_bits1(gb))
869  skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
870 
871  if (get_bits_left(gb) < 5 * (num_front + num_side + num_back + num_cc) + 4 *(num_lfe + num_assoc_data + num_cc)) {
872  av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
873  return -1;
874  }
875  decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
876  tags = num_front;
877  decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
878  tags += num_side;
879  decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
880  tags += num_back;
881  decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
882  tags += num_lfe;
883 
884  skip_bits_long(gb, 4 * num_assoc_data);
885 
886  decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
887  tags += num_cc;
888 
889  relative_align_get_bits(gb, byte_align_ref);
890 
891  /* comment field, first byte is length */
892  comment_len = get_bits(gb, 8) * 8;
893  if (get_bits_left(gb) < comment_len) {
894  av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
895  return AVERROR_INVALIDDATA;
896  }
897  skip_bits_long(gb, comment_len);
898  return tags;
899 }
900 
901 /**
902  * Decode GA "General Audio" specific configuration; reference: table 4.1.
903  *
904  * @param ac pointer to AACContext, may be null
905  * @param avctx pointer to AVCCodecContext, used for logging
906  *
907  * @return Returns error status. 0 - OK, !0 - error
908  */
910  GetBitContext *gb,
911  int get_bit_alignment,
912  MPEG4AudioConfig *m4ac,
913  int channel_config)
914 {
915  int extension_flag, ret, ep_config, res_flags;
916  uint8_t layout_map[MAX_ELEM_ID*4][3];
917  int tags = 0;
918 
919 #if USE_FIXED
920  if (get_bits1(gb)) { // frameLengthFlag
921  avpriv_report_missing_feature(avctx, "Fixed point 960/120 MDCT window");
922  return AVERROR_PATCHWELCOME;
923  }
924  m4ac->frame_length_short = 0;
925 #else
926  m4ac->frame_length_short = get_bits1(gb);
927  if (m4ac->frame_length_short && m4ac->sbr == 1) {
928  avpriv_report_missing_feature(avctx, "SBR with 960 frame length");
929  if (ac) ac->warned_960_sbr = 1;
930  m4ac->sbr = 0;
931  m4ac->ps = 0;
932  }
933 #endif
934 
935  if (get_bits1(gb)) // dependsOnCoreCoder
936  skip_bits(gb, 14); // coreCoderDelay
937  extension_flag = get_bits1(gb);
938 
939  if (m4ac->object_type == AOT_AAC_SCALABLE ||
941  skip_bits(gb, 3); // layerNr
942 
943  if (channel_config == 0) {
944  skip_bits(gb, 4); // element_instance_tag
945  tags = decode_pce(avctx, m4ac, layout_map, gb, get_bit_alignment);
946  if (tags < 0)
947  return tags;
948  } else {
949  if ((ret = set_default_channel_config(ac, avctx, layout_map,
950  &tags, channel_config)))
951  return ret;
952  }
953 
954  if (count_channels(layout_map, tags) > 1) {
955  m4ac->ps = 0;
956  } else if (m4ac->sbr == 1 && m4ac->ps == -1)
957  m4ac->ps = 1;
958 
959  if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
960  return ret;
961 
962  if (extension_flag) {
963  switch (m4ac->object_type) {
964  case AOT_ER_BSAC:
965  skip_bits(gb, 5); // numOfSubFrame
966  skip_bits(gb, 11); // layer_length
967  break;
968  case AOT_ER_AAC_LC:
969  case AOT_ER_AAC_LTP:
970  case AOT_ER_AAC_SCALABLE:
971  case AOT_ER_AAC_LD:
972  res_flags = get_bits(gb, 3);
973  if (res_flags) {
975  "AAC data resilience (flags %x)",
976  res_flags);
977  return AVERROR_PATCHWELCOME;
978  }
979  break;
980  }
981  skip_bits1(gb); // extensionFlag3 (TBD in version 3)
982  }
983  switch (m4ac->object_type) {
984  case AOT_ER_AAC_LC:
985  case AOT_ER_AAC_LTP:
986  case AOT_ER_AAC_SCALABLE:
987  case AOT_ER_AAC_LD:
988  ep_config = get_bits(gb, 2);
989  if (ep_config) {
991  "epConfig %d", ep_config);
992  return AVERROR_PATCHWELCOME;
993  }
994  }
995  return 0;
996 }
997 
999  GetBitContext *gb,
1000  MPEG4AudioConfig *m4ac,
1001  int channel_config)
1002 {
1003  int ret, ep_config, res_flags;
1004  uint8_t layout_map[MAX_ELEM_ID*4][3];
1005  int tags = 0;
1006  const int ELDEXT_TERM = 0;
1007 
1008  m4ac->ps = 0;
1009  m4ac->sbr = 0;
1010 #if USE_FIXED
1011  if (get_bits1(gb)) { // frameLengthFlag
1012  avpriv_request_sample(avctx, "960/120 MDCT window");
1013  return AVERROR_PATCHWELCOME;
1014  }
1015 #else
1016  m4ac->frame_length_short = get_bits1(gb);
1017 #endif
1018  res_flags = get_bits(gb, 3);
1019  if (res_flags) {
1021  "AAC data resilience (flags %x)",
1022  res_flags);
1023  return AVERROR_PATCHWELCOME;
1024  }
1025 
1026  if (get_bits1(gb)) { // ldSbrPresentFlag
1028  "Low Delay SBR");
1029  return AVERROR_PATCHWELCOME;
1030  }
1031 
1032  while (get_bits(gb, 4) != ELDEXT_TERM) {
1033  int len = get_bits(gb, 4);
1034  if (len == 15)
1035  len += get_bits(gb, 8);
1036  if (len == 15 + 255)
1037  len += get_bits(gb, 16);
1038  if (get_bits_left(gb) < len * 8 + 4) {
1039  av_log(avctx, AV_LOG_ERROR, overread_err);
1040  return AVERROR_INVALIDDATA;
1041  }
1042  skip_bits_long(gb, 8 * len);
1043  }
1044 
1045  if ((ret = set_default_channel_config(ac, avctx, layout_map,
1046  &tags, channel_config)))
1047  return ret;
1048 
1049  if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
1050  return ret;
1051 
1052  ep_config = get_bits(gb, 2);
1053  if (ep_config) {
1055  "epConfig %d", ep_config);
1056  return AVERROR_PATCHWELCOME;
1057  }
1058  return 0;
1059 }
1060 
1061 /**
1062  * Decode audio specific configuration; reference: table 1.13.
1063  *
1064  * @param ac pointer to AACContext, may be null
1065  * @param avctx pointer to AVCCodecContext, used for logging
1066  * @param m4ac pointer to MPEG4AudioConfig, used for parsing
1067  * @param gb buffer holding an audio specific config
1068  * @param get_bit_alignment relative alignment for byte align operations
1069  * @param sync_extension look for an appended sync extension
1070  *
1071  * @return Returns error status or number of consumed bits. <0 - error
1072  */
1074  AVCodecContext *avctx,
1075  MPEG4AudioConfig *m4ac,
1076  GetBitContext *gb,
1077  int get_bit_alignment,
1078  int sync_extension)
1079 {
1080  int i, ret;
1081  GetBitContext gbc = *gb;
1082  MPEG4AudioConfig m4ac_bak = *m4ac;
1083 
1084  if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension, avctx)) < 0) {
1085  *m4ac = m4ac_bak;
1086  return AVERROR_INVALIDDATA;
1087  }
1088 
1089  if (m4ac->sampling_index > 12) {
1090  av_log(avctx, AV_LOG_ERROR,
1091  "invalid sampling rate index %d\n",
1092  m4ac->sampling_index);
1093  *m4ac = m4ac_bak;
1094  return AVERROR_INVALIDDATA;
1095  }
1096  if (m4ac->object_type == AOT_ER_AAC_LD &&
1097  (m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
1098  av_log(avctx, AV_LOG_ERROR,
1099  "invalid low delay sampling rate index %d\n",
1100  m4ac->sampling_index);
1101  *m4ac = m4ac_bak;
1102  return AVERROR_INVALIDDATA;
1103  }
1104 
1105  skip_bits_long(gb, i);
1106 
1107  switch (m4ac->object_type) {
1108  case AOT_AAC_MAIN:
1109  case AOT_AAC_LC:
1110  case AOT_AAC_SSR:
1111  case AOT_AAC_LTP:
1112  case AOT_ER_AAC_LC:
1113  case AOT_ER_AAC_LD:
1114  if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
1115  m4ac, m4ac->chan_config)) < 0)
1116  return ret;
1117  break;
1118  case AOT_ER_AAC_ELD:
1119  if ((ret = decode_eld_specific_config(ac, avctx, gb,
1120  m4ac, m4ac->chan_config)) < 0)
1121  return ret;
1122  break;
1123  default:
1125  "Audio object type %s%d",
1126  m4ac->sbr == 1 ? "SBR+" : "",
1127  m4ac->object_type);
1128  return AVERROR(ENOSYS);
1129  }
1130 
1131  ff_dlog(avctx,
1132  "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
1133  m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
1134  m4ac->sample_rate, m4ac->sbr,
1135  m4ac->ps);
1136 
1137  return get_bits_count(gb);
1138 }
1139 
1141  AVCodecContext *avctx,
1142  MPEG4AudioConfig *m4ac,
1143  const uint8_t *data, int64_t bit_size,
1144  int sync_extension)
1145 {
1146  int i, ret;
1147  GetBitContext gb;
1148 
1149  if (bit_size < 0 || bit_size > INT_MAX) {
1150  av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
1151  return AVERROR_INVALIDDATA;
1152  }
1153 
1154  ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
1155  for (i = 0; i < bit_size >> 3; i++)
1156  ff_dlog(avctx, "%02x ", data[i]);
1157  ff_dlog(avctx, "\n");
1158 
1159  if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
1160  return ret;
1161 
1162  return decode_audio_specific_config_gb(ac, avctx, m4ac, &gb, 0,
1163  sync_extension);
1164 }
1165 
1166 /**
1167  * linear congruential pseudorandom number generator
1168  *
1169  * @param previous_val pointer to the current state of the generator
1170  *
1171  * @return Returns a 32-bit pseudorandom integer
1172  */
1173 static av_always_inline int lcg_random(unsigned previous_val)
1174 {
1175  union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
1176  return v.s;
1177 }
1178 
1180 {
1181  int i;
1182  for (i = 0; i < MAX_PREDICTORS; i++)
1183  reset_predict_state(&ps[i]);
1184 }
1185 
1186 static int sample_rate_idx (int rate)
1187 {
1188  if (92017 <= rate) return 0;
1189  else if (75132 <= rate) return 1;
1190  else if (55426 <= rate) return 2;
1191  else if (46009 <= rate) return 3;
1192  else if (37566 <= rate) return 4;
1193  else if (27713 <= rate) return 5;
1194  else if (23004 <= rate) return 6;
1195  else if (18783 <= rate) return 7;
1196  else if (13856 <= rate) return 8;
1197  else if (11502 <= rate) return 9;
1198  else if (9391 <= rate) return 10;
1199  else return 11;
1200 }
1201 
1202 static void reset_predictor_group(PredictorState *ps, int group_num)
1203 {
1204  int i;
1205  for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
1206  reset_predict_state(&ps[i]);
1207 }
1208 
1209 static void aacdec_init(AACContext *ac);
1210 
1212 {
1213  static VLC_TYPE vlc_buf[304 + 270 + 550 + 300 + 328 +
1214  294 + 306 + 268 + 510 + 366 + 462][2];
1215  for (unsigned i = 0, offset = 0; i < 11; i++) {
1220  sizeof(ff_aac_spectral_bits[i][0]),
1222  sizeof(ff_aac_spectral_codes[i][0]),
1224  sizeof(ff_aac_codebook_vector_idx[i][0]),
1227  }
1228 
1230 
1231  ff_aac_tableinit();
1232 
1236  sizeof(ff_aac_scalefactor_bits[0]),
1237  sizeof(ff_aac_scalefactor_bits[0]),
1239  sizeof(ff_aac_scalefactor_code[0]),
1240  sizeof(ff_aac_scalefactor_code[0]),
1241  352);
1242 
1243  // window initialization
1244 #if !USE_FIXED
1251 #else
1252  AAC_RENAME(ff_kbd_window_init)(AAC_RENAME2(aac_kbd_long_1024), 4.0, 1024);
1253  AAC_RENAME(ff_kbd_window_init)(AAC_RENAME2(aac_kbd_short_128), 6.0, 128);
1255 #endif
1256 
1258 }
1259 
1261 
1263 {
1264  AACContext *ac = avctx->priv_data;
1265  int ret;
1266 
1267  if (avctx->sample_rate > 96000)
1268  return AVERROR_INVALIDDATA;
1269 
1271  if (ret != 0)
1272  return AVERROR_UNKNOWN;
1273 
1274  ac->avctx = avctx;
1275  ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
1276 
1277  aacdec_init(ac);
1278 #if USE_FIXED
1279  avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1280 #else
1281  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1282 #endif /* USE_FIXED */
1283 
1284  if (avctx->extradata_size > 0) {
1285  if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
1286  avctx->extradata,
1287  avctx->extradata_size * 8LL,
1288  1)) < 0)
1289  return ret;
1290  } else {
1291  int sr, i;
1292  uint8_t layout_map[MAX_ELEM_ID*4][3];
1293  int layout_map_tags;
1294 
1295  sr = sample_rate_idx(avctx->sample_rate);
1296  ac->oc[1].m4ac.sampling_index = sr;
1297  ac->oc[1].m4ac.channels = avctx->channels;
1298  ac->oc[1].m4ac.sbr = -1;
1299  ac->oc[1].m4ac.ps = -1;
1300 
1301  for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
1302  if (ff_mpeg4audio_channels[i] == avctx->channels)
1303  break;
1305  i = 0;
1306  }
1307  ac->oc[1].m4ac.chan_config = i;
1308 
1309  if (ac->oc[1].m4ac.chan_config) {
1310  int ret = set_default_channel_config(ac, avctx, layout_map,
1311  &layout_map_tags, ac->oc[1].m4ac.chan_config);
1312  if (!ret)
1313  output_configure(ac, layout_map, layout_map_tags,
1314  OC_GLOBAL_HDR, 0);
1315  else if (avctx->err_recognition & AV_EF_EXPLODE)
1316  return AVERROR_INVALIDDATA;
1317  }
1318  }
1319 
1320  if (avctx->channels > MAX_CHANNELS) {
1321  av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
1322  return AVERROR_INVALIDDATA;
1323  }
1324 
1325 #if USE_FIXED
1327 #else
1329 #endif /* USE_FIXED */
1330  if (!ac->fdsp) {
1331  return AVERROR(ENOMEM);
1332  }
1333 
1334  ac->random_state = 0x1f2e3d4c;
1335 
1336  AAC_RENAME_32(ff_mdct_init)(&ac->mdct, 11, 1, 1.0 / RANGE15(1024.0));
1337  AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ld, 10, 1, 1.0 / RANGE15(512.0));
1338  AAC_RENAME_32(ff_mdct_init)(&ac->mdct_small, 8, 1, 1.0 / RANGE15(128.0));
1339  AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ltp, 11, 0, RANGE15(-2.0));
1340 #if !USE_FIXED
1341  ret = ff_mdct15_init(&ac->mdct120, 1, 3, 1.0f/(16*1024*120*2));
1342  if (ret < 0)
1343  return ret;
1344  ret = ff_mdct15_init(&ac->mdct480, 1, 5, 1.0f/(16*1024*960));
1345  if (ret < 0)
1346  return ret;
1347  ret = ff_mdct15_init(&ac->mdct960, 1, 6, 1.0f/(16*1024*960*2));
1348  if (ret < 0)
1349  return ret;
1350 #endif
1351 
1352  return 0;
1353 }
1354 
1355 /**
1356  * Skip data_stream_element; reference: table 4.10.
1357  */
1359 {
1360  int byte_align = get_bits1(gb);
1361  int count = get_bits(gb, 8);
1362  if (count == 255)
1363  count += get_bits(gb, 8);
1364  if (byte_align)
1365  align_get_bits(gb);
1366 
1367  if (get_bits_left(gb) < 8 * count) {
1368  av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
1369  return AVERROR_INVALIDDATA;
1370  }
1371  skip_bits_long(gb, 8 * count);
1372  return 0;
1373 }
1374 
1376  GetBitContext *gb)
1377 {
1378  int sfb;
1379  if (get_bits1(gb)) {
1380  ics->predictor_reset_group = get_bits(gb, 5);
1381  if (ics->predictor_reset_group == 0 ||
1382  ics->predictor_reset_group > 30) {
1383  av_log(ac->avctx, AV_LOG_ERROR,
1384  "Invalid Predictor Reset Group.\n");
1385  return AVERROR_INVALIDDATA;
1386  }
1387  }
1388  for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1389  ics->prediction_used[sfb] = get_bits1(gb);
1390  }
1391  return 0;
1392 }
1393 
1394 /**
1395  * Decode Long Term Prediction data; reference: table 4.xx.
1396  */
1398  GetBitContext *gb, uint8_t max_sfb)
1399 {
1400  int sfb;
1401 
1402  ltp->lag = get_bits(gb, 11);
1403  ltp->coef = ltp_coef[get_bits(gb, 3)];
1404  for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
1405  ltp->used[sfb] = get_bits1(gb);
1406 }
1407 
1408 /**
1409  * Decode Individual Channel Stream info; reference: table 4.6.
1410  */
1412  GetBitContext *gb)
1413 {
1414  const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
1415  const int aot = m4ac->object_type;
1416  const int sampling_index = m4ac->sampling_index;
1417  int ret_fail = AVERROR_INVALIDDATA;
1418 
1419  if (aot != AOT_ER_AAC_ELD) {
1420  if (get_bits1(gb)) {
1421  av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1423  return AVERROR_INVALIDDATA;
1424  }
1425  ics->window_sequence[1] = ics->window_sequence[0];
1426  ics->window_sequence[0] = get_bits(gb, 2);
1427  if (aot == AOT_ER_AAC_LD &&
1428  ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
1429  av_log(ac->avctx, AV_LOG_ERROR,
1430  "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
1431  "window sequence %d found.\n", ics->window_sequence[0]);
1433  return AVERROR_INVALIDDATA;
1434  }
1435  ics->use_kb_window[1] = ics->use_kb_window[0];
1436  ics->use_kb_window[0] = get_bits1(gb);
1437  }
1438  ics->num_window_groups = 1;
1439  ics->group_len[0] = 1;
1440  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1441  int i;
1442  ics->max_sfb = get_bits(gb, 4);
1443  for (i = 0; i < 7; i++) {
1444  if (get_bits1(gb)) {
1445  ics->group_len[ics->num_window_groups - 1]++;
1446  } else {
1447  ics->num_window_groups++;
1448  ics->group_len[ics->num_window_groups - 1] = 1;
1449  }
1450  }
1451  ics->num_windows = 8;
1452  if (m4ac->frame_length_short) {
1453  ics->swb_offset = ff_swb_offset_120[sampling_index];
1454  ics->num_swb = ff_aac_num_swb_120[sampling_index];
1455  } else {
1456  ics->swb_offset = ff_swb_offset_128[sampling_index];
1457  ics->num_swb = ff_aac_num_swb_128[sampling_index];
1458  }
1459  ics->tns_max_bands = ff_tns_max_bands_128[sampling_index];
1460  ics->predictor_present = 0;
1461  } else {
1462  ics->max_sfb = get_bits(gb, 6);
1463  ics->num_windows = 1;
1464  if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
1465  if (m4ac->frame_length_short) {
1466  ics->swb_offset = ff_swb_offset_480[sampling_index];
1467  ics->num_swb = ff_aac_num_swb_480[sampling_index];
1468  ics->tns_max_bands = ff_tns_max_bands_480[sampling_index];
1469  } else {
1470  ics->swb_offset = ff_swb_offset_512[sampling_index];
1471  ics->num_swb = ff_aac_num_swb_512[sampling_index];
1472  ics->tns_max_bands = ff_tns_max_bands_512[sampling_index];
1473  }
1474  if (!ics->num_swb || !ics->swb_offset) {
1475  ret_fail = AVERROR_BUG;
1476  goto fail;
1477  }
1478  } else {
1479  if (m4ac->frame_length_short) {
1480  ics->num_swb = ff_aac_num_swb_960[sampling_index];
1481  ics->swb_offset = ff_swb_offset_960[sampling_index];
1482  } else {
1483  ics->num_swb = ff_aac_num_swb_1024[sampling_index];
1484  ics->swb_offset = ff_swb_offset_1024[sampling_index];
1485  }
1486  ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
1487  }
1488  if (aot != AOT_ER_AAC_ELD) {
1489  ics->predictor_present = get_bits1(gb);
1490  ics->predictor_reset_group = 0;
1491  }
1492  if (ics->predictor_present) {
1493  if (aot == AOT_AAC_MAIN) {
1494  if (decode_prediction(ac, ics, gb)) {
1495  goto fail;
1496  }
1497  } else if (aot == AOT_AAC_LC ||
1498  aot == AOT_ER_AAC_LC) {
1499  av_log(ac->avctx, AV_LOG_ERROR,
1500  "Prediction is not allowed in AAC-LC.\n");
1501  goto fail;
1502  } else {
1503  if (aot == AOT_ER_AAC_LD) {
1504  av_log(ac->avctx, AV_LOG_ERROR,
1505  "LTP in ER AAC LD not yet implemented.\n");
1506  ret_fail = AVERROR_PATCHWELCOME;
1507  goto fail;
1508  }
1509  if ((ics->ltp.present = get_bits(gb, 1)))
1510  decode_ltp(&ics->ltp, gb, ics->max_sfb);
1511  }
1512  }
1513  }
1514 
1515  if (ics->max_sfb > ics->num_swb) {
1516  av_log(ac->avctx, AV_LOG_ERROR,
1517  "Number of scalefactor bands in group (%d) "
1518  "exceeds limit (%d).\n",
1519  ics->max_sfb, ics->num_swb);
1520  goto fail;
1521  }
1522 
1523  return 0;
1524 fail:
1525  ics->max_sfb = 0;
1526  return ret_fail;
1527 }
1528 
1529 /**
1530  * Decode band types (section_data payload); reference: table 4.46.
1531  *
1532  * @param band_type array of the used band type
1533  * @param band_type_run_end array of the last scalefactor band of a band type run
1534  *
1535  * @return Returns error status. 0 - OK, !0 - error
1536  */
1537 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
1538  int band_type_run_end[120], GetBitContext *gb,
1540 {
1541  int g, idx = 0;
1542  const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1543  for (g = 0; g < ics->num_window_groups; g++) {
1544  int k = 0;
1545  while (k < ics->max_sfb) {
1546  uint8_t sect_end = k;
1547  int sect_len_incr;
1548  int sect_band_type = get_bits(gb, 4);
1549  if (sect_band_type == 12) {
1550  av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1551  return AVERROR_INVALIDDATA;
1552  }
1553  do {
1554  sect_len_incr = get_bits(gb, bits);
1555  sect_end += sect_len_incr;
1556  if (get_bits_left(gb) < 0) {
1557  av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1558  return AVERROR_INVALIDDATA;
1559  }
1560  if (sect_end > ics->max_sfb) {
1561  av_log(ac->avctx, AV_LOG_ERROR,
1562  "Number of bands (%d) exceeds limit (%d).\n",
1563  sect_end, ics->max_sfb);
1564  return AVERROR_INVALIDDATA;
1565  }
1566  } while (sect_len_incr == (1 << bits) - 1);
1567  for (; k < sect_end; k++) {
1568  band_type [idx] = sect_band_type;
1569  band_type_run_end[idx++] = sect_end;
1570  }
1571  }
1572  }
1573  return 0;
1574 }
1575 
1576 /**
1577  * Decode scalefactors; reference: table 4.47.
1578  *
1579  * @param global_gain first scalefactor value as scalefactors are differentially coded
1580  * @param band_type array of the used band type
1581  * @param band_type_run_end array of the last scalefactor band of a band type run
1582  * @param sf array of scalefactors or intensity stereo positions
1583  *
1584  * @return Returns error status. 0 - OK, !0 - error
1585  */
1587  unsigned int global_gain,
1589  enum BandType band_type[120],
1590  int band_type_run_end[120])
1591 {
1592  int g, i, idx = 0;
1593  int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
1594  int clipped_offset;
1595  int noise_flag = 1;
1596  for (g = 0; g < ics->num_window_groups; g++) {
1597  for (i = 0; i < ics->max_sfb;) {
1598  int run_end = band_type_run_end[idx];
1599  if (band_type[idx] == ZERO_BT) {
1600  for (; i < run_end; i++, idx++)
1601  sf[idx] = FIXR(0.);
1602  } else if ((band_type[idx] == INTENSITY_BT) ||
1603  (band_type[idx] == INTENSITY_BT2)) {
1604  for (; i < run_end; i++, idx++) {
1606  clipped_offset = av_clip(offset[2], -155, 100);
1607  if (offset[2] != clipped_offset) {
1609  "If you heard an audible artifact, there may be a bug in the decoder. "
1610  "Clipped intensity stereo position (%d -> %d)",
1611  offset[2], clipped_offset);
1612  }
1613 #if USE_FIXED
1614  sf[idx] = 100 - clipped_offset;
1615 #else
1616  sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
1617 #endif /* USE_FIXED */
1618  }
1619  } else if (band_type[idx] == NOISE_BT) {
1620  for (; i < run_end; i++, idx++) {
1621  if (noise_flag-- > 0)
1622  offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
1623  else
1625  clipped_offset = av_clip(offset[1], -100, 155);
1626  if (offset[1] != clipped_offset) {
1628  "If you heard an audible artifact, there may be a bug in the decoder. "
1629  "Clipped noise gain (%d -> %d)",
1630  offset[1], clipped_offset);
1631  }
1632 #if USE_FIXED
1633  sf[idx] = -(100 + clipped_offset);
1634 #else
1635  sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
1636 #endif /* USE_FIXED */
1637  }
1638  } else {
1639  for (; i < run_end; i++, idx++) {
1641  if (offset[0] > 255U) {
1642  av_log(ac->avctx, AV_LOG_ERROR,
1643  "Scalefactor (%d) out of range.\n", offset[0]);
1644  return AVERROR_INVALIDDATA;
1645  }
1646 #if USE_FIXED
1647  sf[idx] = -offset[0];
1648 #else
1649  sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
1650 #endif /* USE_FIXED */
1651  }
1652  }
1653  }
1654  }
1655  return 0;
1656 }
1657 
1658 /**
1659  * Decode pulse data; reference: table 4.7.
1660  */
1661 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1662  const uint16_t *swb_offset, int num_swb)
1663 {
1664  int i, pulse_swb;
1665  pulse->num_pulse = get_bits(gb, 2) + 1;
1666  pulse_swb = get_bits(gb, 6);
1667  if (pulse_swb >= num_swb)
1668  return -1;
1669  pulse->pos[0] = swb_offset[pulse_swb];
1670  pulse->pos[0] += get_bits(gb, 5);
1671  if (pulse->pos[0] >= swb_offset[num_swb])
1672  return -1;
1673  pulse->amp[0] = get_bits(gb, 4);
1674  for (i = 1; i < pulse->num_pulse; i++) {
1675  pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1676  if (pulse->pos[i] >= swb_offset[num_swb])
1677  return -1;
1678  pulse->amp[i] = get_bits(gb, 4);
1679  }
1680  return 0;
1681 }
1682 
1683 /**
1684  * Decode Temporal Noise Shaping data; reference: table 4.48.
1685  *
1686  * @return Returns error status. 0 - OK, !0 - error
1687  */
1689  GetBitContext *gb, const IndividualChannelStream *ics)
1690 {
1691  int w, filt, i, coef_len, coef_res, coef_compress;
1692  const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1693  const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1694  for (w = 0; w < ics->num_windows; w++) {
1695  if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1696  coef_res = get_bits1(gb);
1697 
1698  for (filt = 0; filt < tns->n_filt[w]; filt++) {
1699  int tmp2_idx;
1700  tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1701 
1702  if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
1703  av_log(ac->avctx, AV_LOG_ERROR,
1704  "TNS filter order %d is greater than maximum %d.\n",
1705  tns->order[w][filt], tns_max_order);
1706  tns->order[w][filt] = 0;
1707  return AVERROR_INVALIDDATA;
1708  }
1709  if (tns->order[w][filt]) {
1710  tns->direction[w][filt] = get_bits1(gb);
1711  coef_compress = get_bits1(gb);
1712  coef_len = coef_res + 3 - coef_compress;
1713  tmp2_idx = 2 * coef_compress + coef_res;
1714 
1715  for (i = 0; i < tns->order[w][filt]; i++)
1716  tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1717  }
1718  }
1719  }
1720  }
1721  return 0;
1722 }
1723 
1724 /**
1725  * Decode Mid/Side data; reference: table 4.54.
1726  *
1727  * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1728  * [1] mask is decoded from bitstream; [2] mask is all 1s;
1729  * [3] reserved for scalable AAC
1730  */
1732  int ms_present)
1733 {
1734  int idx;
1735  int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1736  if (ms_present == 1) {
1737  for (idx = 0; idx < max_idx; idx++)
1738  cpe->ms_mask[idx] = get_bits1(gb);
1739  } else if (ms_present == 2) {
1740  memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
1741  }
1742 }
1743 
1744 /**
1745  * Decode spectral data; reference: table 4.50.
1746  * Dequantize and scale spectral data; reference: 4.6.3.3.
1747  *
1748  * @param coef array of dequantized, scaled spectral data
1749  * @param sf array of scalefactors or intensity stereo positions
1750  * @param pulse_present set if pulses are present
1751  * @param pulse pointer to pulse data struct
1752  * @param band_type array of the used band type
1753  *
1754  * @return Returns error status. 0 - OK, !0 - error
1755  */
1757  GetBitContext *gb, const INTFLOAT sf[120],
1758  int pulse_present, const Pulse *pulse,
1759  const IndividualChannelStream *ics,
1760  enum BandType band_type[120])
1761 {
1762  int i, k, g, idx = 0;
1763  const int c = 1024 / ics->num_windows;
1764  const uint16_t *offsets = ics->swb_offset;
1765  INTFLOAT *coef_base = coef;
1766 
1767  for (g = 0; g < ics->num_windows; g++)
1768  memset(coef + g * 128 + offsets[ics->max_sfb], 0,
1769  sizeof(INTFLOAT) * (c - offsets[ics->max_sfb]));
1770 
1771  for (g = 0; g < ics->num_window_groups; g++) {
1772  unsigned g_len = ics->group_len[g];
1773 
1774  for (i = 0; i < ics->max_sfb; i++, idx++) {
1775  const unsigned cbt_m1 = band_type[idx] - 1;
1776  INTFLOAT *cfo = coef + offsets[i];
1777  int off_len = offsets[i + 1] - offsets[i];
1778  int group;
1779 
1780  if (cbt_m1 >= INTENSITY_BT2 - 1) {
1781  for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1782  memset(cfo, 0, off_len * sizeof(*cfo));
1783  }
1784  } else if (cbt_m1 == NOISE_BT - 1) {
1785  for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1786  INTFLOAT band_energy;
1787 #if USE_FIXED
1788  for (k = 0; k < off_len; k++) {
1790  cfo[k] = ac->random_state >> 3;
1791  }
1792 
1793  band_energy = ac->fdsp->scalarproduct_fixed(cfo, cfo, off_len);
1794  band_energy = fixed_sqrt(band_energy, 31);
1795  noise_scale(cfo, sf[idx], band_energy, off_len);
1796 #else
1797  float scale;
1798 
1799  for (k = 0; k < off_len; k++) {
1801  cfo[k] = ac->random_state;
1802  }
1803 
1804  band_energy = ac->fdsp->scalarproduct_float(cfo, cfo, off_len);
1805  scale = sf[idx] / sqrtf(band_energy);
1806  ac->fdsp->vector_fmul_scalar(cfo, cfo, scale, off_len);
1807 #endif /* USE_FIXED */
1808  }
1809  } else {
1810 #if !USE_FIXED
1811  const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1812 #endif /* !USE_FIXED */
1813  VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1814  OPEN_READER(re, gb);
1815 
1816  switch (cbt_m1 >> 1) {
1817  case 0:
1818  for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1819  INTFLOAT *cf = cfo;
1820  int len = off_len;
1821 
1822  do {
1823  int code;
1824  unsigned cb_idx;
1825 
1826  UPDATE_CACHE(re, gb);
1827  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1828  cb_idx = code;
1829 #if USE_FIXED
1830  cf = DEC_SQUAD(cf, cb_idx);
1831 #else
1832  cf = VMUL4(cf, vq, cb_idx, sf + idx);
1833 #endif /* USE_FIXED */
1834  } while (len -= 4);
1835  }
1836  break;
1837 
1838  case 1:
1839  for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1840  INTFLOAT *cf = cfo;
1841  int len = off_len;
1842 
1843  do {
1844  int code;
1845  unsigned nnz;
1846  unsigned cb_idx;
1847  uint32_t bits;
1848 
1849  UPDATE_CACHE(re, gb);
1850  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1851  cb_idx = code;
1852  nnz = cb_idx >> 8 & 15;
1853  bits = nnz ? GET_CACHE(re, gb) : 0;
1854  LAST_SKIP_BITS(re, gb, nnz);
1855 #if USE_FIXED
1856  cf = DEC_UQUAD(cf, cb_idx, bits);
1857 #else
1858  cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1859 #endif /* USE_FIXED */
1860  } while (len -= 4);
1861  }
1862  break;
1863 
1864  case 2:
1865  for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1866  INTFLOAT *cf = cfo;
1867  int len = off_len;
1868 
1869  do {
1870  int code;
1871  unsigned cb_idx;
1872 
1873  UPDATE_CACHE(re, gb);
1874  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1875  cb_idx = code;
1876 #if USE_FIXED
1877  cf = DEC_SPAIR(cf, cb_idx);
1878 #else
1879  cf = VMUL2(cf, vq, cb_idx, sf + idx);
1880 #endif /* USE_FIXED */
1881  } while (len -= 2);
1882  }
1883  break;
1884 
1885  case 3:
1886  case 4:
1887  for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1888  INTFLOAT *cf = cfo;
1889  int len = off_len;
1890 
1891  do {
1892  int code;
1893  unsigned nnz;
1894  unsigned cb_idx;
1895  unsigned sign;
1896 
1897  UPDATE_CACHE(re, gb);
1898  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1899  cb_idx = code;
1900  nnz = cb_idx >> 8 & 15;
1901  sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
1902  LAST_SKIP_BITS(re, gb, nnz);
1903 #if USE_FIXED
1904  cf = DEC_UPAIR(cf, cb_idx, sign);
1905 #else
1906  cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1907 #endif /* USE_FIXED */
1908  } while (len -= 2);
1909  }
1910  break;
1911 
1912  default:
1913  for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1914 #if USE_FIXED
1915  int *icf = cfo;
1916  int v;
1917 #else
1918  float *cf = cfo;
1919  uint32_t *icf = (uint32_t *) cf;
1920 #endif /* USE_FIXED */
1921  int len = off_len;
1922 
1923  do {
1924  int code;
1925  unsigned nzt, nnz;
1926  unsigned cb_idx;
1927  uint32_t bits;
1928  int j;
1929 
1930  UPDATE_CACHE(re, gb);
1931  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1932  cb_idx = code;
1933 
1934  if (cb_idx == 0x0000) {
1935  *icf++ = 0;
1936  *icf++ = 0;
1937  continue;
1938  }
1939 
1940  nnz = cb_idx >> 12;
1941  nzt = cb_idx >> 8;
1942  bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1943  LAST_SKIP_BITS(re, gb, nnz);
1944 
1945  for (j = 0; j < 2; j++) {
1946  if (nzt & 1<<j) {
1947  uint32_t b;
1948  int n;
1949  /* The total length of escape_sequence must be < 22 bits according
1950  to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1951  UPDATE_CACHE(re, gb);
1952  b = GET_CACHE(re, gb);
1953  b = 31 - av_log2(~b);
1954 
1955  if (b > 8) {
1956  av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1957  return AVERROR_INVALIDDATA;
1958  }
1959 
1960  SKIP_BITS(re, gb, b + 1);
1961  b += 4;
1962  n = (1 << b) + SHOW_UBITS(re, gb, b);
1963  LAST_SKIP_BITS(re, gb, b);
1964 #if USE_FIXED
1965  v = n;
1966  if (bits & 1U<<31)
1967  v = -v;
1968  *icf++ = v;
1969 #else
1970  *icf++ = ff_cbrt_tab[n] | (bits & 1U<<31);
1971 #endif /* USE_FIXED */
1972  bits <<= 1;
1973  } else {
1974 #if USE_FIXED
1975  v = cb_idx & 15;
1976  if (bits & 1U<<31)
1977  v = -v;
1978  *icf++ = v;
1979 #else
1980  unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1981  *icf++ = (bits & 1U<<31) | v;
1982 #endif /* USE_FIXED */
1983  bits <<= !!v;
1984  }
1985  cb_idx >>= 4;
1986  }
1987  } while (len -= 2);
1988 #if !USE_FIXED
1989  ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1990 #endif /* !USE_FIXED */
1991  }
1992  }
1993 
1994  CLOSE_READER(re, gb);
1995  }
1996  }
1997  coef += g_len << 7;
1998  }
1999 
2000  if (pulse_present) {
2001  idx = 0;
2002  for (i = 0; i < pulse->num_pulse; i++) {
2003  INTFLOAT co = coef_base[ pulse->pos[i] ];
2004  while (offsets[idx + 1] <= pulse->pos[i])
2005  idx++;
2006  if (band_type[idx] != NOISE_BT && sf[idx]) {
2007  INTFLOAT ico = -pulse->amp[i];
2008 #if USE_FIXED
2009  if (co) {
2010  ico = co + (co > 0 ? -ico : ico);
2011  }
2012  coef_base[ pulse->pos[i] ] = ico;
2013 #else
2014  if (co) {
2015  co /= sf[idx];
2016  ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
2017  }
2018  coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
2019 #endif /* USE_FIXED */
2020  }
2021  }
2022  }
2023 #if USE_FIXED
2024  coef = coef_base;
2025  idx = 0;
2026  for (g = 0; g < ics->num_window_groups; g++) {
2027  unsigned g_len = ics->group_len[g];
2028 
2029  for (i = 0; i < ics->max_sfb; i++, idx++) {
2030  const unsigned cbt_m1 = band_type[idx] - 1;
2031  int *cfo = coef + offsets[i];
2032  int off_len = offsets[i + 1] - offsets[i];
2033  int group;
2034 
2035  if (cbt_m1 < NOISE_BT - 1) {
2036  for (group = 0; group < (int)g_len; group++, cfo+=128) {
2037  ac->vector_pow43(cfo, off_len);
2038  ac->subband_scale(cfo, cfo, sf[idx], 34, off_len, ac->avctx);
2039  }
2040  }
2041  }
2042  coef += g_len << 7;
2043  }
2044 #endif /* USE_FIXED */
2045  return 0;
2046 }
2047 
2048 /**
2049  * Apply AAC-Main style frequency domain prediction.
2050  */
2052 {
2053  int sfb, k;
2054 
2055  if (!sce->ics.predictor_initialized) {
2057  sce->ics.predictor_initialized = 1;
2058  }
2059 
2060  if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2061  for (sfb = 0;
2062  sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
2063  sfb++) {
2064  for (k = sce->ics.swb_offset[sfb];
2065  k < sce->ics.swb_offset[sfb + 1];
2066  k++) {
2067  predict(&sce->predictor_state[k], &sce->coeffs[k],
2068  sce->ics.predictor_present &&
2069  sce->ics.prediction_used[sfb]);
2070  }
2071  }
2072  if (sce->ics.predictor_reset_group)
2074  sce->ics.predictor_reset_group);
2075  } else
2077 }
2078 
2080 {
2081  // wd_num, wd_test, aloc_size
2082  static const uint8_t gain_mode[4][3] = {
2083  {1, 0, 5}, // ONLY_LONG_SEQUENCE = 0,
2084  {2, 1, 2}, // LONG_START_SEQUENCE,
2085  {8, 0, 2}, // EIGHT_SHORT_SEQUENCE,
2086  {2, 1, 5}, // LONG_STOP_SEQUENCE
2087  };
2088 
2089  const int mode = sce->ics.window_sequence[0];
2090  uint8_t bd, wd, ad;
2091 
2092  // FIXME: Store the gain control data on |sce| and do something with it.
2093  uint8_t max_band = get_bits(gb, 2);
2094  for (bd = 0; bd < max_band; bd++) {
2095  for (wd = 0; wd < gain_mode[mode][0]; wd++) {
2096  uint8_t adjust_num = get_bits(gb, 3);
2097  for (ad = 0; ad < adjust_num; ad++) {
2098  skip_bits(gb, 4 + ((wd == 0 && gain_mode[mode][1])
2099  ? 4
2100  : gain_mode[mode][2]));
2101  }
2102  }
2103  }
2104 }
2105 
2106 /**
2107  * Decode an individual_channel_stream payload; reference: table 4.44.
2108  *
2109  * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
2110  * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
2111  *
2112  * @return Returns error status. 0 - OK, !0 - error
2113  */
2115  GetBitContext *gb, int common_window, int scale_flag)
2116 {
2117  Pulse pulse;
2118  TemporalNoiseShaping *tns = &sce->tns;
2119  IndividualChannelStream *ics = &sce->ics;
2120  INTFLOAT *out = sce->coeffs;
2121  int global_gain, eld_syntax, er_syntax, pulse_present = 0;
2122  int ret;
2123 
2124  eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
2125  er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
2126  ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
2127  ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
2128  ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
2129 
2130  /* This assignment is to silence a GCC warning about the variable being used
2131  * uninitialized when in fact it always is.
2132  */
2133  pulse.num_pulse = 0;
2134 
2135  global_gain = get_bits(gb, 8);
2136 
2137  if (!common_window && !scale_flag) {
2138  ret = decode_ics_info(ac, ics, gb);
2139  if (ret < 0)
2140  goto fail;
2141  }
2142 
2143  if ((ret = decode_band_types(ac, sce->band_type,
2144  sce->band_type_run_end, gb, ics)) < 0)
2145  goto fail;
2146  if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
2147  sce->band_type, sce->band_type_run_end)) < 0)
2148  goto fail;
2149 
2150  pulse_present = 0;
2151  if (!scale_flag) {
2152  if (!eld_syntax && (pulse_present = get_bits1(gb))) {
2153  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2154  av_log(ac->avctx, AV_LOG_ERROR,
2155  "Pulse tool not allowed in eight short sequence.\n");
2156  ret = AVERROR_INVALIDDATA;
2157  goto fail;
2158  }
2159  if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
2160  av_log(ac->avctx, AV_LOG_ERROR,
2161  "Pulse data corrupt or invalid.\n");
2162  ret = AVERROR_INVALIDDATA;
2163  goto fail;
2164  }
2165  }
2166  tns->present = get_bits1(gb);
2167  if (tns->present && !er_syntax) {
2168  ret = decode_tns(ac, tns, gb, ics);
2169  if (ret < 0)
2170  goto fail;
2171  }
2172  if (!eld_syntax && get_bits1(gb)) {
2173  decode_gain_control(sce, gb);
2174  if (!ac->warned_gain_control) {
2175  avpriv_report_missing_feature(ac->avctx, "Gain control");
2176  ac->warned_gain_control = 1;
2177  }
2178  }
2179  // I see no textual basis in the spec for this occurring after SSR gain
2180  // control, but this is what both reference and real implmentations do
2181  if (tns->present && er_syntax) {
2182  ret = decode_tns(ac, tns, gb, ics);
2183  if (ret < 0)
2184  goto fail;
2185  }
2186  }
2187 
2188  ret = decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
2189  &pulse, ics, sce->band_type);
2190  if (ret < 0)
2191  goto fail;
2192 
2193  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
2194  apply_prediction(ac, sce);
2195 
2196  return 0;
2197 fail:
2198  tns->present = 0;
2199  return ret;
2200 }
2201 
2202 /**
2203  * Mid/Side stereo decoding; reference: 4.6.8.1.3.
2204  */
2206 {
2207  const IndividualChannelStream *ics = &cpe->ch[0].ics;
2208  INTFLOAT *ch0 = cpe->ch[0].coeffs;
2209  INTFLOAT *ch1 = cpe->ch[1].coeffs;
2210  int g, i, group, idx = 0;
2211  const uint16_t *offsets = ics->swb_offset;
2212  for (g = 0; g < ics->num_window_groups; g++) {
2213  for (i = 0; i < ics->max_sfb; i++, idx++) {
2214  if (cpe->ms_mask[idx] &&
2215  cpe->ch[0].band_type[idx] < NOISE_BT &&
2216  cpe->ch[1].band_type[idx] < NOISE_BT) {
2217 #if USE_FIXED
2218  for (group = 0; group < ics->group_len[g]; group++) {
2219  ac->fdsp->butterflies_fixed(ch0 + group * 128 + offsets[i],
2220  ch1 + group * 128 + offsets[i],
2221  offsets[i+1] - offsets[i]);
2222 #else
2223  for (group = 0; group < ics->group_len[g]; group++) {
2224  ac->fdsp->butterflies_float(ch0 + group * 128 + offsets[i],
2225  ch1 + group * 128 + offsets[i],
2226  offsets[i+1] - offsets[i]);
2227 #endif /* USE_FIXED */
2228  }
2229  }
2230  }
2231  ch0 += ics->group_len[g] * 128;
2232  ch1 += ics->group_len[g] * 128;
2233  }
2234 }
2235 
2236 /**
2237  * intensity stereo decoding; reference: 4.6.8.2.3
2238  *
2239  * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
2240  * [1] mask is decoded from bitstream; [2] mask is all 1s;
2241  * [3] reserved for scalable AAC
2242  */
2244  ChannelElement *cpe, int ms_present)
2245 {
2246  const IndividualChannelStream *ics = &cpe->ch[1].ics;
2247  SingleChannelElement *sce1 = &cpe->ch[1];
2248  INTFLOAT *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
2249  const uint16_t *offsets = ics->swb_offset;
2250  int g, group, i, idx = 0;
2251  int c;
2252  INTFLOAT scale;
2253  for (g = 0; g < ics->num_window_groups; g++) {
2254  for (i = 0; i < ics->max_sfb;) {
2255  if (sce1->band_type[idx] == INTENSITY_BT ||
2256  sce1->band_type[idx] == INTENSITY_BT2) {
2257  const int bt_run_end = sce1->band_type_run_end[idx];
2258  for (; i < bt_run_end; i++, idx++) {
2259  c = -1 + 2 * (sce1->band_type[idx] - 14);
2260  if (ms_present)
2261  c *= 1 - 2 * cpe->ms_mask[idx];
2262  scale = c * sce1->sf[idx];
2263  for (group = 0; group < ics->group_len[g]; group++)
2264 #if USE_FIXED
2265  ac->subband_scale(coef1 + group * 128 + offsets[i],
2266  coef0 + group * 128 + offsets[i],
2267  scale,
2268  23,
2269  offsets[i + 1] - offsets[i] ,ac->avctx);
2270 #else
2271  ac->fdsp->vector_fmul_scalar(coef1 + group * 128 + offsets[i],
2272  coef0 + group * 128 + offsets[i],
2273  scale,
2274  offsets[i + 1] - offsets[i]);
2275 #endif /* USE_FIXED */
2276  }
2277  } else {
2278  int bt_run_end = sce1->band_type_run_end[idx];
2279  idx += bt_run_end - i;
2280  i = bt_run_end;
2281  }
2282  }
2283  coef0 += ics->group_len[g] * 128;
2284  coef1 += ics->group_len[g] * 128;
2285  }
2286 }
2287 
2288 /**
2289  * Decode a channel_pair_element; reference: table 4.4.
2290  *
2291  * @return Returns error status. 0 - OK, !0 - error
2292  */
2294 {
2295  int i, ret, common_window, ms_present = 0;
2296  int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
2297 
2298  common_window = eld_syntax || get_bits1(gb);
2299  if (common_window) {
2300  if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
2301  return AVERROR_INVALIDDATA;
2302  i = cpe->ch[1].ics.use_kb_window[0];
2303  cpe->ch[1].ics = cpe->ch[0].ics;
2304  cpe->ch[1].ics.use_kb_window[1] = i;
2305  if (cpe->ch[1].ics.predictor_present &&
2306  (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
2307  if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
2308  decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
2309  ms_present = get_bits(gb, 2);
2310  if (ms_present == 3) {
2311  av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
2312  return AVERROR_INVALIDDATA;
2313  } else if (ms_present)
2314  decode_mid_side_stereo(cpe, gb, ms_present);
2315  }
2316  if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
2317  return ret;
2318  if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
2319  return ret;
2320 
2321  if (common_window) {
2322  if (ms_present)
2323  apply_mid_side_stereo(ac, cpe);
2324  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
2325  apply_prediction(ac, &cpe->ch[0]);
2326  apply_prediction(ac, &cpe->ch[1]);
2327  }
2328  }
2329 
2330  apply_intensity_stereo(ac, cpe, ms_present);
2331  return 0;
2332 }
2333 
2334 static const float cce_scale[] = {
2335  1.09050773266525765921, //2^(1/8)
2336  1.18920711500272106672, //2^(1/4)
2337  M_SQRT2,
2338  2,
2339 };
2340 
2341 /**
2342  * Decode coupling_channel_element; reference: table 4.8.
2343  *
2344  * @return Returns error status. 0 - OK, !0 - error
2345  */
2347 {
2348  int num_gain = 0;
2349  int c, g, sfb, ret;
2350  int sign;
2351  INTFLOAT scale;
2352  SingleChannelElement *sce = &che->ch[0];
2353  ChannelCoupling *coup = &che->coup;
2354 
2355  coup->coupling_point = 2 * get_bits1(gb);
2356  coup->num_coupled = get_bits(gb, 3);
2357  for (c = 0; c <= coup->num_coupled; c++) {
2358  num_gain++;
2359  coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
2360  coup->id_select[c] = get_bits(gb, 4);
2361  if (coup->type[c] == TYPE_CPE) {
2362  coup->ch_select[c] = get_bits(gb, 2);
2363  if (coup->ch_select[c] == 3)
2364  num_gain++;
2365  } else
2366  coup->ch_select[c] = 2;
2367  }
2368  coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
2369 
2370  sign = get_bits(gb, 1);
2371 #if USE_FIXED
2372  scale = get_bits(gb, 2);
2373 #else
2374  scale = cce_scale[get_bits(gb, 2)];
2375 #endif
2376 
2377  if ((ret = decode_ics(ac, sce, gb, 0, 0)))
2378  return ret;
2379 
2380  for (c = 0; c < num_gain; c++) {
2381  int idx = 0;
2382  int cge = 1;
2383  int gain = 0;
2384  INTFLOAT gain_cache = FIXR10(1.);
2385  if (c) {
2386  cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
2387  gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
2388  gain_cache = GET_GAIN(scale, gain);
2389 #if USE_FIXED
2390  if ((abs(gain_cache)-1024) >> 3 > 30)
2391  return AVERROR(ERANGE);
2392 #endif
2393  }
2394  if (coup->coupling_point == AFTER_IMDCT) {
2395  coup->gain[c][0] = gain_cache;
2396  } else {
2397  for (g = 0; g < sce->ics.num_window_groups; g++) {
2398  for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
2399  if (sce->band_type[idx] != ZERO_BT) {
2400  if (!cge) {
2401  int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
2402  if (t) {
2403  int s = 1;
2404  t = gain += t;
2405  if (sign) {
2406  s -= 2 * (t & 0x1);
2407  t >>= 1;
2408  }
2409  gain_cache = GET_GAIN(scale, t) * s;
2410 #if USE_FIXED
2411  if ((abs(gain_cache)-1024) >> 3 > 30)
2412  return AVERROR(ERANGE);
2413 #endif
2414  }
2415  }
2416  coup->gain[c][idx] = gain_cache;
2417  }
2418  }
2419  }
2420  }
2421  }
2422  return 0;
2423 }
2424 
2425 /**
2426  * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
2427  *
2428  * @return Returns number of bytes consumed.
2429  */
2431  GetBitContext *gb)
2432 {
2433  int i;
2434  int num_excl_chan = 0;
2435 
2436  do {
2437  for (i = 0; i < 7; i++)
2438  che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
2439  } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
2440 
2441  return num_excl_chan / 7;
2442 }
2443 
2444 /**
2445  * Decode dynamic range information; reference: table 4.52.
2446  *
2447  * @return Returns number of bytes consumed.
2448  */
2450  GetBitContext *gb)
2451 {
2452  int n = 1;
2453  int drc_num_bands = 1;
2454  int i;
2455 
2456  /* pce_tag_present? */
2457  if (get_bits1(gb)) {
2458  che_drc->pce_instance_tag = get_bits(gb, 4);
2459  skip_bits(gb, 4); // tag_reserved_bits
2460  n++;
2461  }
2462 
2463  /* excluded_chns_present? */
2464  if (get_bits1(gb)) {
2465  n += decode_drc_channel_exclusions(che_drc, gb);
2466  }
2467 
2468  /* drc_bands_present? */
2469  if (get_bits1(gb)) {
2470  che_drc->band_incr = get_bits(gb, 4);
2471  che_drc->interpolation_scheme = get_bits(gb, 4);
2472  n++;
2473  drc_num_bands += che_drc->band_incr;
2474  for (i = 0; i < drc_num_bands; i++) {
2475  che_drc->band_top[i] = get_bits(gb, 8);
2476  n++;
2477  }
2478  }
2479 
2480  /* prog_ref_level_present? */
2481  if (get_bits1(gb)) {
2482  che_drc->prog_ref_level = get_bits(gb, 7);
2483  skip_bits1(gb); // prog_ref_level_reserved_bits
2484  n++;
2485  }
2486 
2487  for (i = 0; i < drc_num_bands; i++) {
2488  che_drc->dyn_rng_sgn[i] = get_bits1(gb);
2489  che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
2490  n++;
2491  }
2492 
2493  return n;
2494 }
2495 
2496 static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
2497  uint8_t buf[256];
2498  int i, major, minor;
2499 
2500  if (len < 13+7*8)
2501  goto unknown;
2502 
2503  get_bits(gb, 13); len -= 13;
2504 
2505  for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
2506  buf[i] = get_bits(gb, 8);
2507 
2508  buf[i] = 0;
2509  if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
2510  av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
2511 
2512  if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
2513  ac->avctx->internal->skip_samples = 1024;
2514  }
2515 
2516 unknown:
2517  skip_bits_long(gb, len);
2518 
2519  return 0;
2520 }
2521 
2522 /**
2523  * Decode extension data (incomplete); reference: table 4.51.
2524  *
2525  * @param cnt length of TYPE_FIL syntactic element in bytes
2526  *
2527  * @return Returns number of bytes consumed
2528  */
2530  ChannelElement *che, enum RawDataBlockType elem_type)
2531 {
2532  int crc_flag = 0;
2533  int res = cnt;
2534  int type = get_bits(gb, 4);
2535 
2536  if (ac->avctx->debug & FF_DEBUG_STARTCODE)
2537  av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
2538 
2539  switch (type) { // extension type
2540  case EXT_SBR_DATA_CRC:
2541  crc_flag++;
2542  case EXT_SBR_DATA:
2543  if (!che) {
2544  av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
2545  return res;
2546  } else if (ac->oc[1].m4ac.frame_length_short) {
2547  if (!ac->warned_960_sbr)
2549  "SBR with 960 frame length");
2550  ac->warned_960_sbr = 1;
2551  skip_bits_long(gb, 8 * cnt - 4);
2552  return res;
2553  } else if (!ac->oc[1].m4ac.sbr) {
2554  av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
2555  skip_bits_long(gb, 8 * cnt - 4);
2556  return res;
2557  } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
2558  av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
2559  skip_bits_long(gb, 8 * cnt - 4);
2560  return res;
2561  } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
2562  ac->oc[1].m4ac.sbr = 1;
2563  ac->oc[1].m4ac.ps = 1;
2565  output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
2566  ac->oc[1].status, 1);
2567  } else {
2568  ac->oc[1].m4ac.sbr = 1;
2570  }
2571  res = AAC_RENAME(ff_decode_sbr_extension)(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
2572  break;
2573  case EXT_DYNAMIC_RANGE:
2574  res = decode_dynamic_range(&ac->che_drc, gb);
2575  break;
2576  case EXT_FILL:
2577  decode_fill(ac, gb, 8 * cnt - 4);
2578  break;
2579  case EXT_FILL_DATA:
2580  case EXT_DATA_ELEMENT:
2581  default:
2582  skip_bits_long(gb, 8 * cnt - 4);
2583  break;
2584  };
2585  return res;
2586 }
2587 
2588 /**
2589  * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
2590  *
2591  * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
2592  * @param coef spectral coefficients
2593  */
2594 static void apply_tns(INTFLOAT coef_param[1024], TemporalNoiseShaping *tns,
2595  IndividualChannelStream *ics, int decode)
2596 {
2597  const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
2598  int w, filt, m, i;
2599  int bottom, top, order, start, end, size, inc;
2600  INTFLOAT lpc[TNS_MAX_ORDER];
2602  UINTFLOAT *coef = coef_param;
2603 
2604  if(!mmm)
2605  return;
2606 
2607  for (w = 0; w < ics->num_windows; w++) {
2608  bottom = ics->num_swb;
2609  for (filt = 0; filt < tns->n_filt[w]; filt++) {
2610  top = bottom;
2611  bottom = FFMAX(0, top - tns->length[w][filt]);
2612  order = tns->order[w][filt];
2613  if (order == 0)
2614  continue;
2615 
2616  // tns_decode_coef
2617  AAC_RENAME(compute_lpc_coefs)(tns->coef[w][filt], order, lpc, 0, 0, 0);
2618 
2619  start = ics->swb_offset[FFMIN(bottom, mmm)];
2620  end = ics->swb_offset[FFMIN( top, mmm)];
2621  if ((size = end - start) <= 0)
2622  continue;
2623  if (tns->direction[w][filt]) {
2624  inc = -1;
2625  start = end - 1;
2626  } else {
2627  inc = 1;
2628  }
2629  start += w * 128;
2630 
2631  if (decode) {
2632  // ar filter
2633  for (m = 0; m < size; m++, start += inc)
2634  for (i = 1; i <= FFMIN(m, order); i++)
2635  coef[start] -= AAC_MUL26((INTFLOAT)coef[start - i * inc], lpc[i - 1]);
2636  } else {
2637  // ma filter
2638  for (m = 0; m < size; m++, start += inc) {
2639  tmp[0] = coef[start];
2640  for (i = 1; i <= FFMIN(m, order); i++)
2641  coef[start] += AAC_MUL26(tmp[i], lpc[i - 1]);
2642  for (i = order; i > 0; i--)
2643  tmp[i] = tmp[i - 1];
2644  }
2645  }
2646  }
2647  }
2648 }
2649 
2650 /**
2651  * Apply windowing and MDCT to obtain the spectral
2652  * coefficient from the predicted sample by LTP.
2653  */
2656 {
2657  const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
2658  const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2659  const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
2660  const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2661 
2662  if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
2663  ac->fdsp->vector_fmul(in, in, lwindow_prev, 1024);
2664  } else {
2665  memset(in, 0, 448 * sizeof(*in));
2666  ac->fdsp->vector_fmul(in + 448, in + 448, swindow_prev, 128);
2667  }
2668  if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
2669  ac->fdsp->vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
2670  } else {
2671  ac->fdsp->vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
2672  memset(in + 1024 + 576, 0, 448 * sizeof(*in));
2673  }
2674  ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
2675 }
2676 
2677 /**
2678  * Apply the long term prediction
2679  */
2681 {
2682  const LongTermPrediction *ltp = &sce->ics.ltp;
2683  const uint16_t *offsets = sce->ics.swb_offset;
2684  int i, sfb;
2685 
2686  if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2687  INTFLOAT *predTime = sce->ret;
2688  INTFLOAT *predFreq = ac->buf_mdct;
2689  int16_t num_samples = 2048;
2690 
2691  if (ltp->lag < 1024)
2692  num_samples = ltp->lag + 1024;
2693  for (i = 0; i < num_samples; i++)
2694  predTime[i] = AAC_MUL30(sce->ltp_state[i + 2048 - ltp->lag], ltp->coef);
2695  memset(&predTime[i], 0, (2048 - i) * sizeof(*predTime));
2696 
2697  ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
2698 
2699  if (sce->tns.present)
2700  ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
2701 
2702  for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
2703  if (ltp->used[sfb])
2704  for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
2705  sce->coeffs[i] += (UINTFLOAT)predFreq[i];
2706  }
2707 }
2708 
2709 /**
2710  * Update the LTP buffer for next frame
2711  */
2713 {
2714  IndividualChannelStream *ics = &sce->ics;
2715  INTFLOAT *saved = sce->saved;
2716  INTFLOAT *saved_ltp = sce->coeffs;
2717  const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
2718  const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2719  int i;
2720 
2721  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2722  memcpy(saved_ltp, saved, 512 * sizeof(*saved_ltp));
2723  memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
2724  ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2725 
2726  for (i = 0; i < 64; i++)
2727  saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
2728  } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2729  memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(*saved_ltp));
2730  memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
2731  ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2732 
2733  for (i = 0; i < 64; i++)
2734  saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
2735  } else { // LONG_STOP or ONLY_LONG
2736  ac->fdsp->vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
2737 
2738  for (i = 0; i < 512; i++)
2739  saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], lwindow[511 - i]);
2740  }
2741 
2742  memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
2743  memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
2744  memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
2745 }
2746 
2747 /**
2748  * Conduct IMDCT and windowing.
2749  */
2751 {
2752  IndividualChannelStream *ics = &sce->ics;
2753  INTFLOAT *in = sce->coeffs;
2754  INTFLOAT *out = sce->ret;
2755  INTFLOAT *saved = sce->saved;
2756  const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2757  const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
2758  const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2759  INTFLOAT *buf = ac->buf_mdct;
2760  INTFLOAT *temp = ac->temp;
2761  int i;
2762 
2763  // imdct
2764  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2765  for (i = 0; i < 1024; i += 128)
2766  ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
2767  } else {
2768  ac->mdct.imdct_half(&ac->mdct, buf, in);
2769 #if USE_FIXED
2770  for (i=0; i<1024; i++)
2771  buf[i] = (buf[i] + 4LL) >> 3;
2772 #endif /* USE_FIXED */
2773  }
2774 
2775  /* window overlapping
2776  * NOTE: To simplify the overlapping code, all 'meaningless' short to long
2777  * and long to short transitions are considered to be short to short
2778  * transitions. This leaves just two cases (long to long and short to short)
2779  * with a little special sauce for EIGHT_SHORT_SEQUENCE.
2780  */
2781  if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2783  ac->fdsp->vector_fmul_window( out, saved, buf, lwindow_prev, 512);
2784  } else {
2785  memcpy( out, saved, 448 * sizeof(*out));
2786 
2787  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2788  ac->fdsp->vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
2789  ac->fdsp->vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
2790  ac->fdsp->vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
2791  ac->fdsp->vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
2792  ac->fdsp->vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
2793  memcpy( out + 448 + 4*128, temp, 64 * sizeof(*out));
2794  } else {
2795  ac->fdsp->vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
2796  memcpy( out + 576, buf + 64, 448 * sizeof(*out));
2797  }
2798  }
2799 
2800  // buffer update
2801  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2802  memcpy( saved, temp + 64, 64 * sizeof(*saved));
2803  ac->fdsp->vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
2804  ac->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
2805  ac->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
2806  memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(*saved));
2807  } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2808  memcpy( saved, buf + 512, 448 * sizeof(*saved));
2809  memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(*saved));
2810  } else { // LONG_STOP or ONLY_LONG
2811  memcpy( saved, buf + 512, 512 * sizeof(*saved));
2812  }
2813 }
2814 
2815 /**
2816  * Conduct IMDCT and windowing.
2817  */
2819 {
2820 #if !USE_FIXED
2821  IndividualChannelStream *ics = &sce->ics;
2822  INTFLOAT *in = sce->coeffs;
2823  INTFLOAT *out = sce->ret;
2824  INTFLOAT *saved = sce->saved;
2825  const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(aac_kbd_short_120) : AAC_RENAME(sine_120);
2826  const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(aac_kbd_long_960) : AAC_RENAME(sine_960);
2827  const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(aac_kbd_short_120) : AAC_RENAME(sine_120);
2828  INTFLOAT *buf = ac->buf_mdct;
2829  INTFLOAT *temp = ac->temp;
2830  int i;
2831 
2832  // imdct
2833  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2834  for (i = 0; i < 8; i++)
2835  ac->mdct120->imdct_half(ac->mdct120, buf + i * 120, in + i * 128, 1);
2836  } else {
2837  ac->mdct960->imdct_half(ac->mdct960, buf, in, 1);
2838  }
2839 
2840  /* window overlapping
2841  * NOTE: To simplify the overlapping code, all 'meaningless' short to long
2842  * and long to short transitions are considered to be short to short
2843  * transitions. This leaves just two cases (long to long and short to short)
2844  * with a little special sauce for EIGHT_SHORT_SEQUENCE.
2845  */
2846 
2847  if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2849  ac->fdsp->vector_fmul_window( out, saved, buf, lwindow_prev, 480);
2850  } else {
2851  memcpy( out, saved, 420 * sizeof(*out));
2852 
2853  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2854  ac->fdsp->vector_fmul_window(out + 420 + 0*120, saved + 420, buf + 0*120, swindow_prev, 60);
2855  ac->fdsp->vector_fmul_window(out + 420 + 1*120, buf + 0*120 + 60, buf + 1*120, swindow, 60);
2856  ac->fdsp->vector_fmul_window(out + 420 + 2*120, buf + 1*120 + 60, buf + 2*120, swindow, 60);
2857  ac->fdsp->vector_fmul_window(out + 420 + 3*120, buf + 2*120 + 60, buf + 3*120, swindow, 60);
2858  ac->fdsp->vector_fmul_window(temp, buf + 3*120 + 60, buf + 4*120, swindow, 60);
2859  memcpy( out + 420 + 4*120, temp, 60 * sizeof(*out));
2860  } else {
2861  ac->fdsp->vector_fmul_window(out + 420, saved + 420, buf, swindow_prev, 60);
2862  memcpy( out + 540, buf + 60, 420 * sizeof(*out));
2863  }
2864  }
2865 
2866  // buffer update
2867  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2868  memcpy( saved, temp + 60, 60 * sizeof(*saved));
2869  ac->fdsp->vector_fmul_window(saved + 60, buf + 4*120 + 60, buf + 5*120, swindow, 60);
2870  ac->fdsp->vector_fmul_window(saved + 180, buf + 5*120 + 60, buf + 6*120, swindow, 60);
2871  ac->fdsp->vector_fmul_window(saved + 300, buf + 6*120 + 60, buf + 7*120, swindow, 60);
2872  memcpy( saved + 420, buf + 7*120 + 60, 60 * sizeof(*saved));
2873  } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2874  memcpy( saved, buf + 480, 420 * sizeof(*saved));
2875  memcpy( saved + 420, buf + 7*120 + 60, 60 * sizeof(*saved));
2876  } else { // LONG_STOP or ONLY_LONG
2877  memcpy( saved, buf + 480, 480 * sizeof(*saved));
2878  }
2879 #endif
2880 }
2882 {
2883  IndividualChannelStream *ics = &sce->ics;
2884  INTFLOAT *in = sce->coeffs;
2885  INTFLOAT *out = sce->ret;
2886  INTFLOAT *saved = sce->saved;
2887  INTFLOAT *buf = ac->buf_mdct;
2888 #if USE_FIXED
2889  int i;
2890 #endif /* USE_FIXED */
2891 
2892  // imdct
2893  ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
2894 
2895 #if USE_FIXED
2896  for (i = 0; i < 1024; i++)
2897  buf[i] = (buf[i] + 2) >> 2;
2898 #endif /* USE_FIXED */
2899 
2900  // window overlapping
2901  if (ics->use_kb_window[1]) {
2902  // AAC LD uses a low overlap sine window instead of a KBD window
2903  memcpy(out, saved, 192 * sizeof(*out));
2904  ac->fdsp->vector_fmul_window(out + 192, saved + 192, buf, AAC_RENAME2(sine_128), 64);
2905  memcpy( out + 320, buf + 64, 192 * sizeof(*out));
2906  } else {
2907  ac->fdsp->vector_fmul_window(out, saved, buf, AAC_RENAME2(sine_512), 256);
2908  }
2909 
2910  // buffer update
2911  memcpy(saved, buf + 256, 256 * sizeof(*saved));
2912 }
2913 
2915 {
2916  UINTFLOAT *in = sce->coeffs;
2917  INTFLOAT *out = sce->ret;
2918  INTFLOAT *saved = sce->saved;
2919  INTFLOAT *buf = ac->buf_mdct;
2920  int i;
2921  const int n = ac->oc[1].m4ac.frame_length_short ? 480 : 512;
2922  const int n2 = n >> 1;
2923  const int n4 = n >> 2;
2924  const INTFLOAT *const window = n == 480 ? AAC_RENAME(ff_aac_eld_window_480) :
2926 
2927  // Inverse transform, mapped to the conventional IMDCT by
2928  // Chivukula, R.K.; Reznik, Y.A.; Devarajan, V.,
2929  // "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks,"
2930  // International Conference on Audio, Language and Image Processing, ICALIP 2008.
2931  // URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950
2932  for (i = 0; i < n2; i+=2) {
2933  INTFLOAT temp;
2934  temp = in[i ]; in[i ] = -in[n - 1 - i]; in[n - 1 - i] = temp;
2935  temp = -in[i + 1]; in[i + 1] = in[n - 2 - i]; in[n - 2 - i] = temp;
2936  }
2937 #if !USE_FIXED
2938  if (n == 480)
2939  ac->mdct480->imdct_half(ac->mdct480, buf, in, 1);
2940  else
2941 #endif
2942  ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
2943 
2944 #if USE_FIXED
2945  for (i = 0; i < 1024; i++)
2946  buf[i] = (buf[i] + 1) >> 1;
2947 #endif /* USE_FIXED */
2948 
2949  for (i = 0; i < n; i+=2) {
2950  buf[i] = -buf[i];
2951  }
2952  // Like with the regular IMDCT at this point we still have the middle half
2953  // of a transform but with even symmetry on the left and odd symmetry on
2954  // the right
2955 
2956  // window overlapping
2957  // The spec says to use samples [0..511] but the reference decoder uses
2958  // samples [128..639].
2959  for (i = n4; i < n2; i ++) {
2960  out[i - n4] = AAC_MUL31( buf[ n2 - 1 - i] , window[i - n4]) +
2961  AAC_MUL31( saved[ i + n2] , window[i + n - n4]) +
2962  AAC_MUL31(-saved[n + n2 - 1 - i] , window[i + 2*n - n4]) +
2963  AAC_MUL31(-saved[ 2*n + n2 + i] , window[i + 3*n - n4]);
2964  }
2965  for (i = 0; i < n2; i ++) {
2966  out[n4 + i] = AAC_MUL31( buf[ i] , window[i + n2 - n4]) +
2967  AAC_MUL31(-saved[ n - 1 - i] , window[i + n2 + n - n4]) +
2968  AAC_MUL31(-saved[ n + i] , window[i + n2 + 2*n - n4]) +
2969  AAC_MUL31( saved[2*n + n - 1 - i] , window[i + n2 + 3*n - n4]);
2970  }
2971  for (i = 0; i < n4; i ++) {
2972  out[n2 + n4 + i] = AAC_MUL31( buf[ i + n2] , window[i + n - n4]) +
2973  AAC_MUL31(-saved[n2 - 1 - i] , window[i + 2*n - n4]) +
2974  AAC_MUL31(-saved[n + n2 + i] , window[i + 3*n - n4]);
2975  }
2976 
2977  // buffer update
2978  memmove(saved + n, saved, 2 * n * sizeof(*saved));
2979  memcpy( saved, buf, n * sizeof(*saved));
2980 }
2981 
2982 /**
2983  * channel coupling transformation interface
2984  *
2985  * @param apply_coupling_method pointer to (in)dependent coupling function
2986  */
2988  enum RawDataBlockType type, int elem_id,
2989  enum CouplingPoint coupling_point,
2990  void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2991 {
2992  int i, c;
2993 
2994  for (i = 0; i < MAX_ELEM_ID; i++) {
2995  ChannelElement *cce = ac->che[TYPE_CCE][i];
2996  int index = 0;
2997 
2998  if (cce && cce->coup.coupling_point == coupling_point) {
2999  ChannelCoupling *coup = &cce->coup;
3000 
3001  for (c = 0; c <= coup->num_coupled; c++) {
3002  if (coup->type[c] == type && coup->id_select[c] == elem_id) {
3003  if (coup->ch_select[c] != 1) {
3004  apply_coupling_method(ac, &cc->ch[0], cce, index);
3005  if (coup->ch_select[c] != 0)
3006  index++;
3007  }
3008  if (coup->ch_select[c] != 2)
3009  apply_coupling_method(ac, &cc->ch[1], cce, index++);
3010  } else
3011  index += 1 + (coup->ch_select[c] == 3);
3012  }
3013  }
3014  }
3015 }
3016 
3017 /**
3018  * Convert spectral data to samples, applying all supported tools as appropriate.
3019  */
3020 static void spectral_to_sample(AACContext *ac, int samples)
3021 {
3022  int i, type;
3024  switch (ac->oc[1].m4ac.object_type) {
3025  case AOT_ER_AAC_LD:
3027  break;
3028  case AOT_ER_AAC_ELD:
3030  break;
3031  default:
3032  if (ac->oc[1].m4ac.frame_length_short)
3034  else
3036  }
3037  for (type = 3; type >= 0; type--) {
3038  for (i = 0; i < MAX_ELEM_ID; i++) {
3039  ChannelElement *che = ac->che[type][i];
3040  if (che && che->present) {
3041  if (type <= TYPE_CPE)
3043  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
3044  if (che->ch[0].ics.predictor_present) {
3045  if (che->ch[0].ics.ltp.present)
3046  ac->apply_ltp(ac, &che->ch[0]);
3047  if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
3048  ac->apply_ltp(ac, &che->ch[1]);
3049  }
3050  }
3051  if (che->ch[0].tns.present)
3052  ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
3053  if (che->ch[1].tns.present)
3054  ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
3055  if (type <= TYPE_CPE)
3057  if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
3058  imdct_and_window(ac, &che->ch[0]);
3059  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
3060  ac->update_ltp(ac, &che->ch[0]);
3061  if (type == TYPE_CPE) {
3062  imdct_and_window(ac, &che->ch[1]);
3063  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
3064  ac->update_ltp(ac, &che->ch[1]);
3065  }
3066  if (ac->oc[1].m4ac.sbr > 0) {
3067  AAC_RENAME(ff_sbr_apply)(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
3068  }
3069  }
3070  if (type <= TYPE_CCE)
3072 
3073 #if USE_FIXED
3074  {
3075  int j;
3076  /* preparation for resampler */
3077  for(j = 0; j<samples; j++){
3078  che->ch[0].ret[j] = (int32_t)av_clip64((int64_t)che->ch[0].ret[j]*128, INT32_MIN, INT32_MAX-0x8000)+0x8000;
3079  if(type == TYPE_CPE)
3080  che->ch[1].ret[j] = (int32_t)av_clip64((int64_t)che->ch[1].ret[j]*128, INT32_MIN, INT32_MAX-0x8000)+0x8000;
3081  }
3082  }
3083 #endif /* USE_FIXED */
3084  che->present = 0;
3085  } else if (che) {
3086  av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
3087  }
3088  }
3089  }
3090 }
3091 
3093 {
3094  int size;
3095  AACADTSHeaderInfo hdr_info;
3096  uint8_t layout_map[MAX_ELEM_ID*4][3];
3097  int layout_map_tags, ret;
3098 
3099  size = ff_adts_header_parse(gb, &hdr_info);
3100  if (size > 0) {
3101  if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
3102  // This is 2 for "VLB " audio in NSV files.
3103  // See samples/nsv/vlb_audio.
3105  "More than one AAC RDB per ADTS frame");
3106  ac->warned_num_aac_frames = 1;
3107  }
3109  if (hdr_info.chan_config) {
3110  ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
3111  if ((ret = set_default_channel_config(ac, ac->avctx,
3112  layout_map,
3113  &layout_map_tags,
3114  hdr_info.chan_config)) < 0)
3115  return ret;
3116  if ((ret = output_configure(ac, layout_map, layout_map_tags,
3117  FFMAX(ac->oc[1].status,
3118  OC_TRIAL_FRAME), 0)) < 0)
3119  return ret;
3120  } else {
3121  ac->oc[1].m4ac.chan_config = 0;
3122  /**
3123  * dual mono frames in Japanese DTV can have chan_config 0
3124  * WITHOUT specifying PCE.
3125  * thus, set dual mono as default.
3126  */
3127  if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
3128  layout_map_tags = 2;
3129  layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
3130  layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
3131  layout_map[0][1] = 0;
3132  layout_map[1][1] = 1;
3133  if (output_configure(ac, layout_map, layout_map_tags,
3134  OC_TRIAL_FRAME, 0))
3135  return -7;
3136  }
3137  }
3138  ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
3139  ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
3140  ac->oc[1].m4ac.object_type = hdr_info.object_type;
3141  ac->oc[1].m4ac.frame_length_short = 0;
3142  if (ac->oc[0].status != OC_LOCKED ||
3143  ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
3144  ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
3145  ac->oc[1].m4ac.sbr = -1;
3146  ac->oc[1].m4ac.ps = -1;
3147  }
3148  if (!hdr_info.crc_absent)
3149  skip_bits(gb, 16);
3150  }
3151  return size;
3152 }
3153 
3154 static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
3155  int *got_frame_ptr, GetBitContext *gb)
3156 {
3157  AACContext *ac = avctx->priv_data;
3158  const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
3159  ChannelElement *che;
3160  int err, i;
3161  int samples = m4ac->frame_length_short ? 960 : 1024;
3162  int chan_config = m4ac->chan_config;
3163  int aot = m4ac->object_type;
3164 
3165  if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
3166  samples >>= 1;
3167 
3168  ac->frame = data;
3169 
3170  if ((err = frame_configure_elements(avctx)) < 0)
3171  return err;
3172 
3173  // The FF_PROFILE_AAC_* defines are all object_type - 1
3174  // This may lead to an undefined profile being signaled
3175  ac->avctx->profile = aot - 1;
3176 
3177  ac->tags_mapped = 0;
3178 
3179  if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
3180  avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
3181  chan_config);
3182  return AVERROR_INVALIDDATA;
3183  }
3184  for (i = 0; i < tags_per_config[chan_config]; i++) {
3185  const int elem_type = aac_channel_layout_map[chan_config-1][i][0];
3186  const int elem_id = aac_channel_layout_map[chan_config-1][i][1];
3187  if (!(che=get_che(ac, elem_type, elem_id))) {
3188  av_log(ac->avctx, AV_LOG_ERROR,
3189  "channel element %d.%d is not allocated\n",
3190  elem_type, elem_id);
3191  return AVERROR_INVALIDDATA;
3192  }
3193  che->present = 1;
3194  if (aot != AOT_ER_AAC_ELD)
3195  skip_bits(gb, 4);
3196  switch (elem_type) {
3197  case TYPE_SCE:
3198  err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3199  break;
3200  case TYPE_CPE:
3201  err = decode_cpe(ac, gb, che);
3202  break;
3203  case TYPE_LFE:
3204  err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3205  break;
3206  }
3207  if (err < 0)
3208  return err;
3209  }
3210 
3211  spectral_to_sample(ac, samples);
3212 
3213  if (!ac->frame->data[0] && samples) {
3214  av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
3215  return AVERROR_INVALIDDATA;
3216  }
3217 
3218  ac->frame->nb_samples = samples;
3219  ac->frame->sample_rate = avctx->sample_rate;
3220  *got_frame_ptr = 1;
3221 
3222  skip_bits_long(gb, get_bits_left(gb));
3223  return 0;
3224 }
3225 
3226 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
3227  int *got_frame_ptr, GetBitContext *gb,
3228  const AVPacket *avpkt)
3229 {
3230  AACContext *ac = avctx->priv_data;
3231  ChannelElement *che = NULL, *che_prev = NULL;
3232  enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
3233  int err, elem_id;
3234  int samples = 0, multiplier, audio_found = 0, pce_found = 0;
3235  int is_dmono, sce_count = 0;
3236  int payload_alignment;
3237  uint8_t che_presence[4][MAX_ELEM_ID] = {{0}};
3238 
3239  ac->frame = data;
3240 
3241  if (show_bits(gb, 12) == 0xfff) {
3242  if ((err = parse_adts_frame_header(ac, gb)) < 0) {
3243  av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
3244  goto fail;
3245  }
3246  if (ac->oc[1].m4ac.sampling_index > 12) {
3247  av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
3248  err = AVERROR_INVALIDDATA;
3249  goto fail;
3250  }
3251  }
3252 
3253  if ((err = frame_configure_elements(avctx)) < 0)
3254  goto fail;
3255 
3256  // The FF_PROFILE_AAC_* defines are all object_type - 1
3257  // This may lead to an undefined profile being signaled
3258  ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
3259 
3260  payload_alignment = get_bits_count(gb);
3261  ac->tags_mapped = 0;
3262  // parse
3263  while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
3264  elem_id = get_bits(gb, 4);
3265 
3266  if (avctx->debug & FF_DEBUG_STARTCODE)
3267  av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
3268 
3269  if (!avctx->channels && elem_type != TYPE_PCE) {
3270  err = AVERROR_INVALIDDATA;
3271  goto fail;
3272  }
3273 
3274  if (elem_type < TYPE_DSE) {
3275  if (che_presence[elem_type][elem_id]) {
3276  int error = che_presence[elem_type][elem_id] > 1;
3277  av_log(ac->avctx, error ? AV_LOG_ERROR : AV_LOG_DEBUG, "channel element %d.%d duplicate\n",
3278  elem_type, elem_id);
3279  if (error) {
3280  err = AVERROR_INVALIDDATA;
3281  goto fail;
3282  }
3283  }
3284  che_presence[elem_type][elem_id]++;
3285 
3286  if (!(che=get_che(ac, elem_type, elem_id))) {
3287  av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
3288  elem_type, elem_id);
3289  err = AVERROR_INVALIDDATA;
3290  goto fail;
3291  }
3292  samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;
3293  che->present = 1;
3294  }
3295 
3296  switch (elem_type) {
3297 
3298  case TYPE_SCE:
3299  err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3300  audio_found = 1;
3301  sce_count++;
3302  break;
3303 
3304  case TYPE_CPE:
3305  err = decode_cpe(ac, gb, che);
3306  audio_found = 1;
3307  break;
3308 
3309  case TYPE_CCE:
3310  err = decode_cce(ac, gb, che);
3311  break;
3312 
3313  case TYPE_LFE:
3314  err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3315  audio_found = 1;
3316  break;
3317 
3318  case TYPE_DSE:
3319  err = skip_data_stream_element(ac, gb);
3320  break;
3321 
3322  case TYPE_PCE: {
3323  uint8_t layout_map[MAX_ELEM_ID*4][3] = {{0}};
3324  int tags;
3325 
3326  int pushed = push_output_configuration(ac);
3327  if (pce_found && !pushed) {
3328  err = AVERROR_INVALIDDATA;
3329  goto fail;
3330  }
3331 
3332  tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,
3333  payload_alignment);
3334  if (tags < 0) {
3335  err = tags;
3336  break;
3337  }
3338  if (pce_found) {
3339  av_log(avctx, AV_LOG_ERROR,
3340  "Not evaluating a further program_config_element as this construct is dubious at best.\n");
3342  } else {
3343  err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
3344  if (!err)
3345  ac->oc[1].m4ac.chan_config = 0;
3346  pce_found = 1;
3347  }
3348  break;
3349  }
3350 
3351  case TYPE_FIL:
3352  if (elem_id == 15)
3353  elem_id += get_bits(gb, 8) - 1;
3354  if (get_bits_left(gb) < 8 * elem_id) {
3355  av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
3356  err = AVERROR_INVALIDDATA;
3357  goto fail;
3358  }
3359  err = 0;
3360  while (elem_id > 0) {
3361  int ret = decode_extension_payload(ac, gb, elem_id, che_prev, che_prev_type);
3362  if (ret < 0) {
3363  err = ret;
3364  break;
3365  }
3366  elem_id -= ret;
3367  }
3368  break;
3369 
3370  default:
3371  err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
3372  break;
3373  }
3374 
3375  if (elem_type < TYPE_DSE) {
3376  che_prev = che;
3377  che_prev_type = elem_type;
3378  }
3379 
3380  if (err)
3381  goto fail;
3382 
3383  if (get_bits_left(gb) < 3) {
3384  av_log(avctx, AV_LOG_ERROR, overread_err);
3385  err = AVERROR_INVALIDDATA;
3386  goto fail;
3387  }
3388  }
3389 
3390  if (!avctx->channels) {
3391  *got_frame_ptr = 0;
3392  return 0;
3393  }
3394 
3395  multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
3396  samples <<= multiplier;
3397 
3398  spectral_to_sample(ac, samples);
3399 
3400  if (ac->oc[1].status && audio_found) {
3401  avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
3402  avctx->frame_size = samples;
3403  ac->oc[1].status = OC_LOCKED;
3404  }
3405 
3406  if (multiplier)
3407  avctx->internal->skip_samples_multiplier = 2;
3408 
3409  if (!ac->frame->data[0] && samples) {
3410  av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
3411  err = AVERROR_INVALIDDATA;
3412  goto fail;
3413  }
3414 
3415  if (samples) {
3416  ac->frame->nb_samples = samples;
3417  ac->frame->sample_rate = avctx->sample_rate;
3418  } else
3419  av_frame_unref(ac->frame);
3420  *got_frame_ptr = !!samples;
3421 
3422  /* for dual-mono audio (SCE + SCE) */
3423  is_dmono = ac->dmono_mode && sce_count == 2 &&
3425  if (is_dmono) {
3426  if (ac->dmono_mode == 1)
3427  ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
3428  else if (ac->dmono_mode == 2)
3429  ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
3430  }
3431 
3432  return 0;
3433 fail:
3435  return err;
3436 }
3437 
3438 static int aac_decode_frame(AVCodecContext *avctx, void *data,
3439  int *got_frame_ptr, AVPacket *avpkt)
3440 {
3441  AACContext *ac = avctx->priv_data;
3442  const uint8_t *buf = avpkt->data;
3443  int buf_size = avpkt->size;
3444  GetBitContext gb;
3445  int buf_consumed;
3446  int buf_offset;
3447  int err;
3448  buffer_size_t new_extradata_size;
3449  const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
3451  &new_extradata_size);
3452  buffer_size_t jp_dualmono_size;
3453  const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
3455  &jp_dualmono_size);
3456 
3457  if (new_extradata) {
3458  /* discard previous configuration */
3459  ac->oc[1].status = OC_NONE;
3460  err = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
3461  new_extradata,
3462  new_extradata_size * 8LL, 1);
3463  if (err < 0) {
3464  return err;
3465  }
3466  }
3467 
3468  ac->dmono_mode = 0;
3469  if (jp_dualmono && jp_dualmono_size > 0)
3470  ac->dmono_mode = 1 + *jp_dualmono;
3471  if (ac->force_dmono_mode >= 0)
3472  ac->dmono_mode = ac->force_dmono_mode;
3473 
3474  if (INT_MAX / 8 <= buf_size)
3475  return AVERROR_INVALIDDATA;
3476 
3477  if ((err = init_get_bits8(&gb, buf, buf_size)) < 0)
3478  return err;
3479 
3480  switch (ac->oc[1].m4ac.object_type) {
3481  case AOT_ER_AAC_LC:
3482  case AOT_ER_AAC_LTP:
3483  case AOT_ER_AAC_LD:
3484  case AOT_ER_AAC_ELD:
3485  err = aac_decode_er_frame(avctx, data, got_frame_ptr, &gb);
3486  break;
3487  default:
3488  err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt);
3489  }
3490  if (err < 0)
3491  return err;
3492 
3493  buf_consumed = (get_bits_count(&gb) + 7) >> 3;
3494  for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
3495  if (buf[buf_offset])
3496  break;
3497 
3498  return buf_size > buf_offset ? buf_consumed : buf_size;
3499 }
3500 
3502 {
3503  AACContext *ac = avctx->priv_data;
3504  int i, type;
3505 
3506  for (i = 0; i < MAX_ELEM_ID; i++) {
3507  for (type = 0; type < 4; type++) {
3508  if (ac->che[type][i])
3510  av_freep(&ac->che[type][i]);
3511  }
3512  }
3513 
3514  ff_mdct_end(&ac->mdct);
3515  ff_mdct_end(&ac->mdct_small);
3516  ff_mdct_end(&ac->mdct_ld);
3517  ff_mdct_end(&ac->mdct_ltp);
3518 #if !USE_FIXED
3519  ff_mdct15_uninit(&ac->mdct120);
3520  ff_mdct15_uninit(&ac->mdct480);
3521  ff_mdct15_uninit(&ac->mdct960);
3522 #endif
3523  av_freep(&ac->fdsp);
3524  return 0;
3525 }
3526 
3527 static void aacdec_init(AACContext *c)
3528 {
3529  c->imdct_and_windowing = imdct_and_windowing;
3530  c->apply_ltp = apply_ltp;
3531  c->apply_tns = apply_tns;
3532  c->windowing_and_mdct_ltp = windowing_and_mdct_ltp;
3533  c->update_ltp = update_ltp;
3534 #if USE_FIXED
3535  c->vector_pow43 = vector_pow43;
3536  c->subband_scale = subband_scale;
3537 #endif
3538 
3539 #if !USE_FIXED
3540  if(ARCH_MIPS)
3542 #endif /* !USE_FIXED */
3543 }
3544 /**
3545  * AVOptions for Japanese DTV specific extensions (ADTS only)
3546  */
3547 #define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
3548 static const AVOption options[] = {
3549  {"dual_mono_mode", "Select the channel to decode for dual mono",
3550  offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
3551  AACDEC_FLAGS, "dual_mono_mode"},
3552 
3553  {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3554  {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3555  {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3556  {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3557 
3558  {NULL},
3559 };
3560 
3561 static const AVClass aac_decoder_class = {
3562  .class_name = "AAC decoder",
3563  .item_name = av_default_item_name,
3564  .option = options,
3565  .version = LIBAVUTIL_VERSION_INT,
3566 };
@ EIGHT_SHORT_SEQUENCE
Definition: aac.h:79
@ LONG_STOP_SEQUENCE
Definition: aac.h:80
@ ONLY_LONG_SEQUENCE
Definition: aac.h:77
@ LONG_START_SEQUENCE
Definition: aac.h:78
@ EXT_DATA_ELEMENT
Definition: aac.h:70
@ EXT_SBR_DATA_CRC
Definition: aac.h:73
@ EXT_DYNAMIC_RANGE
Definition: aac.h:71
@ EXT_FILL_DATA
Definition: aac.h:69
@ EXT_FILL
Definition: aac.h:68
@ EXT_SBR_DATA
Definition: aac.h:72
#define NOISE_PRE
preamble for NOISE_BT, put in bitstream with the first noise band
Definition: aac.h:157
BandType
Definition: aac.h:83
@ INTENSITY_BT
Scalefactor data are intensity stereo positions (in phase).
Definition: aac.h:90
@ ZERO_BT
Scalefactors and spectral data are all zero.
Definition: aac.h:84
@ INTENSITY_BT2
Scalefactor data are intensity stereo positions (out of phase).
Definition: aac.h:89
@ NOISE_BT
Spectral data are scaled white noise not coded in the bitstream.
Definition: aac.h:88
CouplingPoint
The point during decoding at which channel coupling is applied.
Definition: aac.h:107
@ BETWEEN_TNS_AND_IMDCT
Definition: aac.h:109
@ AFTER_IMDCT
Definition: aac.h:110
@ BEFORE_TNS
Definition: aac.h:108
ChannelPosition
Definition: aac.h:95
@ AAC_CHANNEL_LFE
Definition: aac.h:100
@ AAC_CHANNEL_SIDE
Definition: aac.h:98
@ AAC_CHANNEL_OFF
Definition: aac.h:96
@ AAC_CHANNEL_CC
Definition: aac.h:101
@ AAC_CHANNEL_BACK
Definition: aac.h:99
@ AAC_CHANNEL_FRONT
Definition: aac.h:97
RawDataBlockType
Definition: aac.h:56
@ TYPE_CCE
Definition: aac.h:59
@ TYPE_PCE
Definition: aac.h:62
@ TYPE_CPE
Definition: aac.h:58
@ TYPE_SCE
Definition: aac.h:57
@ TYPE_FIL
Definition: aac.h:63
@ TYPE_LFE
Definition: aac.h:60
@ TYPE_END
Definition: aac.h:64
@ TYPE_DSE
Definition: aac.h:61
#define MAX_LTP_LONG_SFB
Definition: aac.h:52
#define TNS_MAX_ORDER
Definition: aac.h:51
#define NOISE_PRE_BITS
length of preamble
Definition: aac.h:158
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
Definition: aac.h:153
#define MAX_CHANNELS
Definition: aac.h:48
OCStatus
Output configuration status.
Definition: aac.h:116
@ OC_TRIAL_FRAME
Output configuration under trial specified by a frame header.
Definition: aac.h:119
@ OC_TRIAL_PCE
Output configuration under trial specified by an inband PCE.
Definition: aac.h:118
@ OC_LOCKED
Output configuration locked in place.
Definition: aac.h:121
@ OC_GLOBAL_HDR
Output configuration set in a global header but not yet locked.
Definition: aac.h:120
@ OC_NONE
Output unconfigured.
Definition: aac.h:117
void ff_aacdec_init_mips(AACContext *c)
Definition: aacdec_mips.c:433
#define MAX_PREDICTORS
Definition: aac.h:147
#define NOISE_OFFSET
subtracted from global gain, used as offset for the preamble
Definition: aac.h:159
#define MAX_ELEM_ID
Definition: aac.h:49
#define POW_SF2_ZERO
ff_aac_pow2sf_tab index corresponding to pow(2, 0);
Definition: aac.h:155
#define AAC_RENAME(x)
Definition: aac_defines.h:85
#define RANGE15(x)
Definition: aac_defines.h:99
#define FIXR(x)
Definition: aac_defines.h:94
#define AAC_MUL31(x, y)
Definition: aac_defines.h:104
#define AAC_MUL30(x, y)
Definition: aac_defines.h:103
float INTFLOAT
Definition: aac_defines.h:88
#define USE_FIXED
Definition: aac_defines.h:25
#define AAC_MUL26(x, y)
Definition: aac_defines.h:102
#define AAC_RENAME2(x)
Definition: aac_defines.h:87
unsigned AAC_SIGNE
Definition: aac_defines.h:93
#define AAC_RENAME_32(x)
Definition: aac_defines.h:86
#define FIXR10(x)
Definition: aac_defines.h:95
#define GET_GAIN(x, y)
Definition: aac_defines.h:100
float UINTFLOAT
Definition: aac_defines.h:89
static void subband_scale(int *dst, int *src, int scale, int offset, int len, void *log_context)
Definition: aacdec_fixed.c:168
static int * DEC_UQUAD(int *dst, unsigned idx, unsigned sign)
Definition: aacdec_fixed.c:136
static int * DEC_SQUAD(int *dst, unsigned idx)
Definition: aacdec_fixed.c:118
static int * DEC_SPAIR(int *dst, unsigned idx)
Definition: aacdec_fixed.c:110
static void vector_pow43(int *coefs, int len)
Definition: aacdec_fixed.c:154
static int * DEC_UPAIR(int *dst, unsigned idx, unsigned sign)
Definition: aacdec_fixed.c:128
static void noise_scale(int *coefs, int scale, int band_energy, int len)
Definition: aacdec_fixed.c:199
static int decode_eld_specific_config(AACContext *ac, AVCodecContext *avctx, GetBitContext *gb, MPEG4AudioConfig *m4ac, int channel_config)
static void imdct_and_windowing_eld(AACContext *ac, SingleChannelElement *sce)
static void relative_align_get_bits(GetBitContext *gb, int reference_position)
#define AACDEC_FLAGS
AVOptions for Japanese DTV specific extensions (ADTS only)
static uint64_t sniff_channel_order(uint8_t(*layout_map)[3], int tags)
static void decode_channel_map(uint8_t layout_map[][3], enum ChannelPosition type, GetBitContext *gb, int n)
Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
static int aac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static int aac_decode_er_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, GetBitContext *gb)
static int sample_rate_idx(int rate)
static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
Apply AAC-Main style frequency domain prediction.
static int decode_spectrum_and_dequant(AACContext *ac, INTFLOAT coef[1024], GetBitContext *gb, const INTFLOAT sf[120], int pulse_present, const Pulse *pulse, const IndividualChannelStream *ics, enum BandType band_type[120])
Decode spectral data; reference: table 4.50.
static void spectral_to_sample(AACContext *ac, int samples)
Convert spectral data to samples, applying all supported tools as appropriate.
static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns, GetBitContext *gb, const IndividualChannelStream *ics)
Decode Temporal Noise Shaping data; reference: table 4.48.
static const AVOption options[]
static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
Mid/Side stereo decoding; reference: 4.6.8.1.3.
static int decode_audio_specific_config_gb(AACContext *ac, AVCodecContext *avctx, MPEG4AudioConfig *m4ac, GetBitContext *gb, int get_bit_alignment, int sync_extension)
Decode audio specific configuration; reference: table 1.13.
static void imdct_and_windowing_960(AACContext *ac, SingleChannelElement *sce)
Conduct IMDCT and windowing.
static VLC vlc_scalefactors
#define overread_err
static av_cold void aac_static_table_init(void)
static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb, int ms_present)
Decode Mid/Side data; reference: table 4.54.
static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
Decode a channel_pair_element; reference: table 4.4.
static int aac_decode_frame_int(AVCodecContext *avctx, void *data, int *got_frame_ptr, GetBitContext *gb, const AVPacket *avpkt)
static void windowing_and_mdct_ltp(AACContext *ac, INTFLOAT *out, INTFLOAT *in, IndividualChannelStream *ics)
Apply windowing and MDCT to obtain the spectral coefficient from the predicted sample by LTP.
static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
Apply the long term prediction.
static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
Skip data_stream_element; reference: table 4.10.
static VLC vlc_spectral[11]
static int frame_configure_elements(AVCodecContext *avctx)
static int output_configure(AACContext *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.
static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, GetBitContext *gb)
Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4....
static int count_paired_channels(uint8_t(*layout_map)[3], int tags, int pos, int *current)
static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt, ChannelElement *che, enum RawDataBlockType elem_type)
Decode extension data (incomplete); reference: table 4.51.
static int decode_pulses(Pulse *pulse, GetBitContext *gb, const uint16_t *swb_offset, int num_swb)
Decode pulse data; reference: table 4.7.
static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext *gb)
Decode dynamic range information; reference: table 4.52.
static void decode_gain_control(SingleChannelElement *sce, GetBitContext *gb)
static av_cold int che_configure(AACContext *ac, enum ChannelPosition che_pos, int type, int id, int *channels)
Check for the channel element in the current channel position configuration.
static int decode_fill(AACContext *ac, GetBitContext *gb, int len)
static av_cold int aac_decode_close(AVCodecContext *avctx)
static int count_channels(uint8_t(*layout)[3], int tags)
static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac, uint8_t(*layout_map)[3], GetBitContext *gb, int byte_align_ref)
Decode program configuration element; reference: table 4.2.
static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
Conduct IMDCT and windowing.
static av_cold int aac_decode_init(AVCodecContext *avctx)
static ChannelElement * get_che(AACContext *ac, int type, int elem_id)
static int decode_audio_specific_config(AACContext *ac, AVCodecContext *avctx, MPEG4AudioConfig *m4ac, const uint8_t *data, int64_t bit_size, int sync_extension)
static void aacdec_init(AACContext *ac)
static int decode_band_types(AACContext *ac, enum BandType band_type[120], int band_type_run_end[120], GetBitContext *gb, IndividualChannelStream *ics)
Decode band types (section_data payload); reference: table 4.46.
static int set_default_channel_config(AACContext *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....
static void update_ltp(AACContext *ac, SingleChannelElement *sce)
Update the LTP buffer for next frame.
static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
Decode coupling_channel_element; reference: table 4.8.
static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
intensity stereo decoding; reference: 4.6.8.2.3
static void apply_tns(INTFLOAT coef_param[1024], TemporalNoiseShaping *tns, IndividualChannelStream *ics, int decode)
Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4....
static AVOnce aac_table_init
static void decode_ltp(LongTermPrediction *ltp, GetBitContext *gb, uint8_t max_sfb)
Decode Long Term Prediction data; reference: table 4.xx.
static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID], uint8_t(*layout_map)[3], int offset, uint64_t left, uint64_t right, int pos, uint64_t *layout)
static void reset_all_predictors(PredictorState *ps)
static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx, GetBitContext *gb, int get_bit_alignment, MPEG4AudioConfig *m4ac, int channel_config)
Decode GA "General Audio" specific configuration; reference: table 4.1.
static void apply_channel_coupling(AACContext *ac, ChannelElement *cc, enum RawDataBlockType type, int elem_id, enum CouplingPoint coupling_point, void(*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
channel coupling transformation interface
static void reset_predictor_group(PredictorState *ps, int group_num)
static int decode_ics(AACContext *ac, SingleChannelElement *sce, GetBitContext *gb, int common_window, int scale_flag)
Decode an individual_channel_stream payload; reference: table 4.44.
static int decode_prediction(AACContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
Decode Individual Channel Stream info; reference: table 4.6.
static av_always_inline int lcg_random(unsigned previous_val)
linear congruential pseudorandom number generator
static int push_output_configuration(AACContext *ac)
Save current output configuration if and only if it has been locked.
static void pop_output_configuration(AACContext *ac)
Restore the previous output configuration if and only if the current configuration is unlocked.
static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
#define PREFIX_FOR_22POINT2
static void imdct_and_windowing_ld(AACContext *ac, SingleChannelElement *sce)
static const AVClass aac_decoder_class
static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *gb, unsigned int global_gain, IndividualChannelStream *ics, enum BandType band_type[120], int band_type_run_end[120])
Decode scalefactors; reference: table 4.47.
static const float cce_scale[]
static void flush(AVCodecContext *avctx)
static const uint8_t aac_channel_layout_map[16][16][3]
Definition: aacdectab.h:40
static const int8_t tags_per_config[16]
Definition: aacdectab.h:38
void AAC_RENAME() ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
Close one SBR context.
void AAC_RENAME() ff_aac_sbr_ctx_init(AACContext *ac, SpectralBandReplication *sbr, int id_aac)
Initialize one SBR context.
void AAC_RENAME() ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac, INTFLOAT *L, INTFLOAT *R)
Apply one SBR element to one AAC element.
void AAC_RENAME() ff_aac_sbr_init(void)
Initialize SBR.
int AAC_RENAME() ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, int crc, int cnt, int id_aac)
Decode one SBR element.
const uint32_t ff_aac_scalefactor_code[121]
Definition: aactab.c:92
const uint8_t ff_aac_num_swb_120[]
Definition: aactab.c:84
const uint8_t ff_aac_num_swb_960[]
Definition: aactab.c:68
const float ff_aac_eld_window_480[1800]
Definition: aactab.c:2397
const uint8_t ff_tns_max_bands_512[]
Definition: aactab.c:1417
const uint8_t ff_tns_max_bands_1024[]
Definition: aactab.c:1413
const uint16_t *const ff_swb_offset_128[]
Definition: aactab.c:1387
const uint16_t *const ff_swb_offset_120[]
Definition: aactab.c:1397
const uint8_t ff_aac_num_swb_480[]
Definition: aactab.c:76
const uint8_t ff_aac_pred_sfb_max[]
Definition: aactab.c:88
const uint16_t *const ff_swb_offset_1024[]
Definition: aactab.c:1355
const uint8_t ff_aac_scalefactor_bits[121]
Definition: aactab.c:111
const uint16_t *const ff_aac_codebook_vector_idx[]
Definition: aactab.c:1102
void ff_aac_tableinit(void)
Definition: aactab.c:3347
float ff_aac_pow2sf_tab[428]
Definition: aactab.c:39
const uint16_t *const ff_swb_offset_960[]
Definition: aactab.c:1363
const uint16_t *const ff_swb_offset_480[]
Definition: aactab.c:1379
const uint8_t ff_aac_num_swb_1024[]
Definition: aactab.c:64
const uint8_t ff_aac_num_swb_128[]
Definition: aactab.c:80
const float ff_aac_eld_window_512[1920]
Definition: aactab.c:1430
const uint8_t ff_tns_max_bands_480[]
Definition: aactab.c:1421
const uint8_t *const ff_aac_spectral_bits[11]
Definition: aactab.c:441
const uint8_t ff_aac_num_swb_512[]
Definition: aactab.c:72
const uint16_t ff_aac_spectral_sizes[11]
Definition: aactab.c:446
const uint16_t *const ff_aac_spectral_codes[11]
Definition: aactab.c:436
const uint8_t ff_tns_max_bands_128[]
Definition: aactab.c:1425
const float *const ff_aac_codebook_vector_vals[]
Definition: aactab.c:1093
const uint16_t *const ff_swb_offset_512[]
Definition: aactab.c:1371
static const INTFLOAT *const tns_tmp2_map[4]
Definition: aactab.h:82
void ff_aac_float_common_init(void)
static const INTFLOAT ltp_coef[8]
Definition: aactab.h:50
int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
Parse the ADTS frame header to the end of the variable header, which is the first 54 bits.
Definition: adts_header.c:30
static const int8_t filt[NUMTAPS *2]
Definition: af_earwax.c:39
channels
Definition: aptx.h:33
#define av_always_inline
Definition: attributes.h:45
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
uint8_t
int32_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:1635
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition: avcodec.h:1658
#define FF_PROFILE_AAC_HE
Definition: avcodec.h:1870
#define FF_PROFILE_AAC_HE_V2
Definition: avcodec.h:1871
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: avcodec.h:1607
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1628
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1660
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t *size)
Definition: avpacket.c:368
int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:323
void ff_cbrt_tableinit(void)
Definition: cbrt_tablegen.h:40
uint32_t ff_cbrt_tab[1<< 13]
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
#define s(width, name)
Definition: cbs_vp9.c:257
uint64_t layout
#define fail()
Definition: checkasm.h:133
static VLC_TYPE vlc_buf[16716][2]
Definition: clearvideo.c:86
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define av_clip
Definition: common.h:122
#define FFMAX(a, b)
Definition: common.h:103
#define av_clip64
Definition: common.h:125
#define ARCH_MIPS
Definition: config.h:27
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
#define abs(x)
Definition: cuda_runtime.h:35
static __device__ float fabsf(float a)
Definition: cuda_runtime.h:181
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1900
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
enum AVCodecID id
int
static SDL_Window * window
Definition: ffplay.c:366
float re
Definition: fft.c:82
#define ff_mdct_init
Definition: fft.h:161
#define ff_mdct_end
Definition: fft.h:162
static av_always_inline int fixed_sqrt(int x, int bits)
Calculate the square root.
Definition: fixed_dsp.h:176
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
Definition: get_bits.h:706
#define GET_CACHE(name, gb)
Definition: get_bits.h:215
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
#define CLOSE_READER(name, gb)
Definition: get_bits.h:149
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:211
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
#define OPEN_READER(name, gb)
Definition: get_bits.h:138
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:193
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:178
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:199
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:693
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
#define AV_CH_LAYOUT_22POINT2
#define AV_CH_SIDE_LEFT
#define AV_CH_TOP_FRONT_LEFT
#define AV_CH_FRONT_RIGHT
#define AV_CH_TOP_BACK_CENTER
#define AV_CH_BOTTOM_FRONT_CENTER
#define AV_CH_FRONT_RIGHT_OF_CENTER
#define AV_CH_BACK_CENTER
#define AV_CH_TOP_FRONT_CENTER
#define AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_LOW_FREQUENCY_2
#define AV_CH_BACK_RIGHT
#define AV_CH_LAYOUT_NATIVE
Channel mask value used for AVCodecContext.request_channel_layout to indicate that the user requests ...
#define AV_CH_TOP_SIDE_RIGHT
#define AV_CH_FRONT_CENTER
#define AV_CH_TOP_BACK_RIGHT
#define AV_CH_TOP_CENTER
#define AV_CH_SIDE_RIGHT
#define AV_CH_BACK_LEFT
#define AV_CH_TOP_SIDE_LEFT
#define AV_CH_TOP_BACK_LEFT
#define AV_CH_LOW_FREQUENCY
#define AV_CH_BOTTOM_FRONT_RIGHT
#define AV_CH_TOP_FRONT_RIGHT
#define AV_CH_BOTTOM_FRONT_LEFT
#define AV_CH_FRONT_LEFT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:333
@ AV_PKT_DATA_JP_DUALMONO
An AV_PKT_DATA_JP_DUALMONO side data packet indicates that the packet may contain "dual mono" audio s...
Definition: packet.h:166
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:68
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int index
Definition: gxfenc.c:89
static const int offsets[]
Definition: hevc_pel.c:34
cl_device_type type
int i
Definition: input.c:407
#define av_log2
Definition: intmath.h:83
av_cold void ff_kbd_window_init(float *window, float alpha, int n)
Generate a Kaiser-Bessel Derived Window.
Definition: kbdwin.c:26
static av_always_inline void reset_predict_state(PredictorState *ps)
Definition: aacdec.c:77
static float * VMUL4S(float *dst, const float *v, unsigned idx, unsigned sign, const float *scale)
Definition: aacdec.c:129
static void apply_dependent_coupling(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Apply dependent channel coupling (applied before IMDCT).
Definition: aacdec.c:215
static float * VMUL4(float *dst, const float *v, unsigned idx, const float *scale)
Definition: aacdec.c:99
static INTFLOAT aac_kbd_short_120[120]
Definition: aacdec.c:75
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
Definition: aacdec.c:179
static void apply_independent_coupling(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Apply independent channel coupling (applied after IMDCT).
Definition: aacdec.c:251
static INTFLOAT aac_kbd_long_960[960]
Definition: aacdec.c:74
static INTFLOAT sine_960[960]
Definition: aacdec.c:73
static INTFLOAT sine_120[120]
Definition: aacdec.c:72
static float * VMUL2(float *dst, const float *v, unsigned idx, const float *scale)
Definition: aacdec.c:88
static float * VMUL2S(float *dst, const float *v, unsigned idx, unsigned sign, const float *scale)
Definition: aacdec.c:112
AVFixedDSPContext * avpriv_alloc_fixed_dsp(int bit_exact)
Allocate and initialize a fixed DSP context.
Definition: fixed_dsp.c:149
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
int buffer_size_t
Definition: internal.h:306
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define AVOnce
Definition: thread.h:172
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
#define AV_ONCE_INIT
Definition: thread.h:173
static av_always_inline float cbrtf(float x)
Definition: libm.h:61
uint8_t w
Definition: llviddspenc.c:39
static int AAC_RENAME() compute_lpc_coefs(const LPC_TYPE *autoc, int max_order, LPC_TYPE *lpc, int lpc_stride, int fail, int normalize)
Levinson-Durbin recursion.
Definition: lpc.h:166
#define M_SQRT2
Definition: mathematics.h:61
av_cold void ff_mdct15_uninit(MDCT15Context **ps)
Definition: mdct15.c:43
av_cold int ff_mdct15_init(MDCT15Context **ps, int inverse, int N, double scale)
Definition: mdct15.c:247
int ff_mpeg4audio_get_config_gb(MPEG4AudioConfig *c, GetBitContext *gb, int sync_extension, void *logctx)
Parse MPEG-4 systems extradata from a potentially unaligned GetBitContext to retrieve audio configura...
Definition: mpeg4audio.c:99
const uint8_t ff_mpeg4audio_channels[14]
Definition: mpeg4audio.c:67
@ AOT_AAC_LTP
Y Long Term Prediction.
Definition: mpeg4audio.h:93
@ AOT_ER_AAC_LD
N Error Resilient Low Delay.
Definition: mpeg4audio.h:109
@ AOT_ER_AAC_ELD
N Error Resilient Enhanced Low Delay.
Definition: mpeg4audio.h:125
@ AOT_ER_AAC_LC
N Error Resilient Low Complexity.
Definition: mpeg4audio.h:104
@ AOT_AAC_SCALABLE
N Scalable.
Definition: mpeg4audio.h:95
@ AOT_ER_BSAC
N Error Resilient Bit-Sliced Arithmetic Coding.
Definition: mpeg4audio.h:108
@ AOT_AAC_SSR
N (code in SoC repo) Scalable Sample Rate.
Definition: mpeg4audio.h:92
@ AOT_AAC_LC
Y Low Complexity.
Definition: mpeg4audio.h:91
@ AOT_ER_AAC_LTP
N Error Resilient Long Term Prediction.
Definition: mpeg4audio.h:105
@ AOT_ER_AAC_SCALABLE
N Error Resilient Scalable.
Definition: mpeg4audio.h:106
@ AOT_AAC_MAIN
Y Main.
Definition: mpeg4audio.h:90
const char data[16]
Definition: mxf.c:142
typedef void(RENAME(mix_any_func_type))
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
void ff_sine_window_init(float *window, int n)
Generate a sine window.
static av_cold void init_sine_windows_fixed(void)
#define FF_ARRAY_ELEMS(a)
const uint8_t * code
Definition: spdifenc.c:413
unsigned int pos
Definition: spdifenc.c:412
uint8_t num_aac_frames
Definition: adts_header.h:36
uint32_t sample_rate
Definition: adts_header.h:29
uint8_t object_type
Definition: adts_header.h:33
uint8_t chan_config
Definition: adts_header.h:35
uint8_t crc_absent
Definition: adts_header.h:32
uint8_t sampling_index
Definition: adts_header.h:34
main AAC context
Definition: aac.h:294
int warned_960_sbr
Definition: aac.h:359
FFTContext mdct
Definition: aac.h:324
SingleChannelElement * output_element[MAX_CHANNELS]
Points to each SingleChannelElement.
Definition: aac.h:343
FFTContext mdct_ld
Definition: aac.h:326
int tags_mapped
Definition: aac.h:308
AVCodecContext * avctx
Definition: aac.h:296
void(* update_ltp)(AACContext *ac, SingleChannelElement *sce)
Definition: aac.h:370
int dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition: aac.h:352
DynamicRangeControl che_drc
Definition: aac.h:300
void(* vector_pow43)(int *coefs, int len)
Definition: aac.h:371
OutputConfiguration oc[2]
Definition: aac.h:357
MDCT15Context * mdct120
Definition: aac.h:331
FFTContext mdct_ltp
Definition: aac.h:327
int force_dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition: aac.h:351
MDCT15Context * mdct480
Definition: aac.h:332
void(* apply_ltp)(AACContext *ac, SingleChannelElement *sce)
Definition: aac.h:365
FFTContext mdct_small
Definition: aac.h:325
int warned_num_aac_frames
Definition: aac.h:358
MDCT15Context * mdct960
Definition: aac.h:333
AVFloatDSPContext * fdsp
Definition: aac.h:334
INTFLOAT buf_mdct[1024]
Definition: aac.h:317
void(* imdct_and_windowing)(AACContext *ac, SingleChannelElement *sce)
Definition: aac.h:364
int warned_gain_control
Definition: aac.h:361
void(* subband_scale)(int *dst, int *src, int scale, int offset, int len, void *log_context)
Definition: aac.h:372
int warned_remapping_once
Definition: aac.h:309
AVFrame * frame
Definition: aac.h:297
ChannelElement * tag_che_map[4][MAX_ELEM_ID]
Definition: aac.h:307
ChannelElement * che[4][MAX_ELEM_ID]
Definition: aac.h:306
int random_state
Definition: aac.h:336
void(* apply_tns)(INTFLOAT coef[1024], TemporalNoiseShaping *tns, IndividualChannelStream *ics, int decode)
Definition: aac.h:366
unsigned warned_71_wide
Definition: aac.h:360
INTFLOAT temp[128]
Definition: aac.h:355
void(* windowing_and_mdct_ltp)(AACContext *ac, INTFLOAT *out, INTFLOAT *in, IndividualChannelStream *ics)
Definition: aac.h:368
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
main external API structure.
Definition: avcodec.h:536
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1204
int debug
debug
Definition: avcodec.h:1627
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1605
int profile
profile
Definition: avcodec.h:1862
int sample_rate
samples per second
Definition: avcodec.h:1196
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
Definition: avcodec.h:1254
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
int channels
number of audio channels
Definition: avcodec.h:1197
int extradata_size
Definition: avcodec.h:638
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1247
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1216
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:571
void * priv_data
Definition: avcodec.h:563
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1649
int skip_samples_multiplier
Definition: internal.h:208
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:175
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats, and store the result in a vector of floats...
Definition: float_dsp.h:154
void(* butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
Definition: float_dsp.h:164
float(* scalarproduct_float)(const float *v1, const float *v2, int len)
Calculate the scalar product of two vectors of floats.
Definition: float_dsp.h:175
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
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
Definition: float_dsp.h:119
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats.
Definition: float_dsp.h:38
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:384
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int sample_rate
Sample rate of the audio data.
Definition: frame.h:490
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:365
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
coupling parameters
Definition: aac.h:235
int id_select[8]
element id
Definition: aac.h:239
enum CouplingPoint coupling_point
The point during decoding at which coupling is applied.
Definition: aac.h:236
int num_coupled
number of target elements
Definition: aac.h:237
INTFLOAT gain[16][120]
Definition: aac.h:243
int ch_select[8]
[0] shared list of gains; [1] list of gains for right channel; [2] list of gains for left channel; [3...
Definition: aac.h:240
enum RawDataBlockType type[8]
Type of channel element to be coupled - SCE or CPE.
Definition: aac.h:238
channel element - generic struct for SCE/CPE/CCE/LFE
Definition: aac.h:276
int present
Definition: aac.h:277
ChannelCoupling coup
Definition: aac.h:287
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
Definition: aac.h:282
SpectralBandReplication sbr
Definition: aac.h:288
SingleChannelElement ch[2]
Definition: aac.h:285
Dynamic Range Control - decoded from the bitstream but not processed further.
Definition: aac.h:212
int interpolation_scheme
Indicates the interpolation scheme used in the SBR QMF domain.
Definition: aac.h:218
int exclude_mask[MAX_CHANNELS]
Channels to be excluded from DRC processing.
Definition: aac.h:216
int band_incr
Number of DRC bands greater than 1 having DRC info.
Definition: aac.h:217
int dyn_rng_ctl[17]
DRC magnitude information.
Definition: aac.h:215
int prog_ref_level
A reference level for the long-term program audio level for all channels combined.
Definition: aac.h:220
int pce_instance_tag
Indicates with which program the DRC info is associated.
Definition: aac.h:213
int band_top[17]
Indicates the top of the i-th DRC band in units of 4 spectral lines.
Definition: aac.h:219
int dyn_rng_sgn[17]
DRC sign information; 0 - positive, 1 - negative.
Definition: aac.h:214
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:103
void(* mdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:104
Individual Channel Stream.
Definition: aac.h:175
uint8_t max_sfb
number of scalefactor bands per group
Definition: aac.h:176
uint8_t group_len[8]
Definition: aac.h:180
int num_swb
number of scalefactor window bands
Definition: aac.h:184
LongTermPrediction ltp
Definition: aac.h:181
uint8_t use_kb_window[2]
If set, use Kaiser-Bessel window, otherwise use a sine window.
Definition: aac.h:178
int predictor_reset_group
Definition: aac.h:189
uint8_t prediction_used[41]
Definition: aac.h:191
int predictor_initialized
Definition: aac.h:188
enum WindowSequence window_sequence[2]
Definition: aac.h:177
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Definition: aac.h:182
Long Term Prediction.
Definition: aac.h:164
int8_t present
Definition: aac.h:165
int16_t lag
Definition: aac.h:166
INTFLOAT coef
Definition: aac.h:168
int8_t used[MAX_LTP_LONG_SFB]
Definition: aac.h:169
void(* imdct_half)(struct MDCT15Context *s, float *dst, const float *src, ptrdiff_t stride)
Definition: mdct15.h:54
int sbr
-1 implicit, 1 presence
Definition: mpeg4audio.h:38
int frame_length_short
Definition: mpeg4audio.h:45
int ps
-1 implicit, 1 presence
Definition: mpeg4audio.h:44
int layout_map_tags
Definition: aac.h:127
enum OCStatus status
Definition: aac.h:130
MPEG4AudioConfig m4ac
Definition: aac.h:125
uint8_t layout_map[MAX_ELEM_ID *4][3]
Definition: aac.h:126
uint64_t channel_layout
Definition: aac.h:129
Predictor State.
Definition: aac.h:136
Definition: aac.h:225
int pos[4]
Definition: aac.h:228
int amp[4]
Definition: aac.h:229
int num_pulse
Definition: aac.h:226
Single Channel Element - used for both SCE and LFE elements.
Definition: aac.h:249
INTFLOAT * ret
PCM output.
Definition: aac.h:270
enum BandType band_type[128]
band types
Definition: aac.h:253
PredictorState predictor_state[MAX_PREDICTORS]
Definition: aac.h:269
int band_type_run_end[120]
band type run end points
Definition: aac.h:255
INTFLOAT ret_buf[2048]
PCM output buffer.
Definition: aac.h:265
TemporalNoiseShaping tns
Definition: aac.h:251
INTFLOAT sf[120]
scalefactors
Definition: aac.h:256
INTFLOAT coeffs[1024]
coefficients for IMDCT, maybe processed
Definition: aac.h:263
INTFLOAT saved[1536]
overlap
Definition: aac.h:264
INTFLOAT ltp_state[3072]
time signal for LTP
Definition: aac.h:266
IndividualChannelStream ics
Definition: aac.h:250
Temporal Noise Shaping.
Definition: aac.h:199
INTFLOAT coef[8][4][TNS_MAX_ORDER]
Definition: aac.h:206
int direction[8][4]
Definition: aac.h:203
int order[8][4]
Definition: aac.h:204
int length[8][4]
Definition: aac.h:202
int n_filt[8]
Definition: aac.h:201
Definition: vlc.h:26
int table_size
Definition: vlc.h:29
int table_allocated
Definition: vlc.h:29
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
#define ff_dlog(a,...)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
FILE * out
Definition: movenc.c:54
static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float *in, float *prev, int ch)
Definition: twinvq.c:327
int size
const char * b
Definition: vf_curves.c:118
const char * g
Definition: vf_curves.c:117
else temp
Definition: vf_mcdeint.c:259
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
Definition: vlc.h:120
#define INIT_VLC_STATIC_OVERLONG
Definition: vlc.h:96
#define VLC_TYPE
Definition: vlc.h:24
int len
uint8_t bits
Definition: vp3data.h:141
static double c[64]