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