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