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