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