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