FFmpeg
aacdec.c
Go to the documentation of this file.
1 /*
2  * Common parts of the AAC decoders
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 /* We use several quantization functions here (Q31, Q30),
33  * for which we need this to be defined for them to work as expected. */
34 #define USE_FIXED 1
35 
36 #include "config_components.h"
37 
38 #include <limits.h>
39 #include <stddef.h>
40 
41 #include "aacdec.h"
42 #include "aacdec_tab.h"
43 #include "aacdec_usac.h"
44 
45 #include "libavcodec/aac.h"
46 #include "libavcodec/aac_defines.h"
47 #include "libavcodec/aacsbr.h"
48 #include "libavcodec/aactab.h"
49 #include "libavcodec/adts_header.h"
50 
51 #include "libavcodec/avcodec.h"
52 #include "libavcodec/internal.h"
54 #include "libavcodec/decode.h"
55 #include "libavcodec/profiles.h"
56 
57 #include "libavutil/attributes.h"
58 #include "libavutil/error.h"
59 #include "libavutil/log.h"
60 #include "libavutil/macros.h"
61 #include "libavutil/mem.h"
62 #include "libavutil/opt.h"
63 #include "libavutil/tx.h"
64 #include "libavutil/version.h"
65 #include "libavutil/refstruct.h"
66 
67 /*
68  * supported tools
69  *
70  * Support? Name
71  * N (code in SoC repo) gain control
72  * Y block switching
73  * Y window shapes - standard
74  * N window shapes - Low Delay
75  * Y filterbank - standard
76  * N (code in SoC repo) filterbank - Scalable Sample Rate
77  * Y Temporal Noise Shaping
78  * Y Long Term Prediction
79  * Y intensity stereo
80  * Y channel coupling
81  * Y frequency domain prediction
82  * Y Perceptual Noise Substitution
83  * Y Mid/Side stereo
84  * N Scalable Inverse AAC Quantization
85  * N Frequency Selective Switch
86  * N upsampling filter
87  * Y quantization & coding - AAC
88  * N quantization & coding - TwinVQ
89  * N quantization & coding - BSAC
90  * N AAC Error Resilience tools
91  * N Error Resilience payload syntax
92  * N Error Protection tool
93  * N CELP
94  * N Silence Compression
95  * N HVXC
96  * N HVXC 4kbits/s VR
97  * N Structured Audio tools
98  * N Structured Audio Sample Bank Format
99  * N MIDI
100  * N Harmonic and Individual Lines plus Noise
101  * N Text-To-Speech Interface
102  * Y Spectral Band Replication
103  * Y (not in this code) Layer-1
104  * Y (not in this code) Layer-2
105  * Y (not in this code) Layer-3
106  * N SinuSoidal Coding (Transient, Sinusoid, Noise)
107  * Y Parametric Stereo
108  * N Direct Stream Transfer
109  * Y (not in fixed point code) Enhanced AAC Low Delay (ER AAC ELD)
110  *
111  * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
112  * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
113  Parametric Stereo.
114  */
115 
116 #define overread_err "Input buffer exhausted before END element found\n"
117 
118 static int count_channels(uint8_t (*layout)[3], int tags)
119 {
120  int i, sum = 0;
121  for (i = 0; i < tags; i++) {
122  int syn_ele = layout[i][0];
123  int pos = layout[i][2];
124  sum += (1 + (syn_ele == TYPE_CPE)) *
126  }
127  return sum;
128 }
129 
130 /**
131  * Check for the channel element in the current channel position configuration.
132  * If it exists, make sure the appropriate element is allocated and map the
133  * channel order to match the internal FFmpeg channel layout.
134  *
135  * @param che_pos current channel position configuration
136  * @param type channel element type
137  * @param id channel element id
138  * @param channels count of the number of channels in the configuration
139  *
140  * @return Returns error status. 0 - OK, !0 - error
141  */
143  enum ChannelPosition che_pos,
144  int type, int id, int *channels)
145 {
146  if (*channels >= MAX_CHANNELS)
147  return AVERROR_INVALIDDATA;
148  if (che_pos) {
149  if (!ac->che[type][id]) {
150  int ret = ac->proc.sbr_ctx_alloc_init(ac, &ac->che[type][id], type);
151  if (ret < 0)
152  return ret;
153  }
154  if (type != TYPE_CCE) {
155  if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
156  av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
157  return AVERROR_INVALIDDATA;
158  }
159  ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
160  if (type == TYPE_CPE ||
161  (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
162  ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
163  }
164  }
165  } else {
166  if (ac->che[type][id]) {
167  for (int i = 0; i < FF_ARRAY_ELEMS(ac->tag_che_map); i++) {
168  for (int j = 0; j < MAX_ELEM_ID; j++) {
169  if (ac->tag_che_map[i][j] == ac->che[type][id])
170  ac->tag_che_map[i][j] = NULL;
171  }
172  }
173  ac->proc.sbr_ctx_close(ac->che[type][id]);
174  }
175  av_freep(&ac->che[type][id]);
176  memset(ac->output_element, 0, sizeof(ac->output_element));
177  }
178  return 0;
179 }
180 
182 {
183  AACDecContext *ac = avctx->priv_data;
184  int type, id, ch, ret;
185 
186  /* set channel pointers to internal buffers by default */
187  for (type = 0; type < 4; type++) {
188  for (id = 0; id < MAX_ELEM_ID; id++) {
189  ChannelElement *che = ac->che[type][id];
190  if (che) {
191  che->ch[0].output = che->ch[0].ret_buf;
192  che->ch[1].output = che->ch[1].ret_buf;
193  }
194  }
195  }
196 
197  /* get output buffer */
198  av_frame_unref(ac->frame);
199  if (!avctx->ch_layout.nb_channels)
200  return 1;
201 
202  ac->frame->nb_samples = 2048;
203  if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
204  return ret;
205 
206  /* map output channel pointers to AVFrame data */
207  for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
208  if (ac->output_element[ch])
209  ac->output_element[ch]->output = (void *)ac->frame->extended_data[ch];
210  }
211 
212  return 0;
213 }
214 
216  uint64_t av_position;
217  uint8_t syn_ele;
218  uint8_t elem_id;
219  uint8_t aac_position;
220 };
221 
222 static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
223  uint8_t (*layout_map)[3], int offset, uint64_t left,
224  uint64_t right, int pos, uint64_t *layout)
225 {
226  if (layout_map[offset][0] == TYPE_CPE) {
227  e2c_vec[offset] = (struct elem_to_channel) {
228  .av_position = left | right,
229  .syn_ele = TYPE_CPE,
230  .elem_id = layout_map[offset][1],
231  .aac_position = pos
232  };
233  if (e2c_vec[offset].av_position != UINT64_MAX)
234  *layout |= e2c_vec[offset].av_position;
235 
236  return 1;
237  } else {
238  e2c_vec[offset] = (struct elem_to_channel) {
239  .av_position = left,
240  .syn_ele = TYPE_SCE,
241  .elem_id = layout_map[offset][1],
242  .aac_position = pos
243  };
244  e2c_vec[offset + 1] = (struct elem_to_channel) {
245  .av_position = right,
246  .syn_ele = TYPE_SCE,
247  .elem_id = layout_map[offset + 1][1],
248  .aac_position = pos
249  };
250  if (left != UINT64_MAX)
251  *layout |= left;
252 
253  if (right != UINT64_MAX)
254  *layout |= right;
255 
256  return 2;
257  }
258 }
259 
260 static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
261  int current)
262 {
263  int num_pos_channels = 0;
264  int first_cpe = 0;
265  int sce_parity = 0;
266  int i;
267  for (i = current; i < tags; i++) {
268  if (layout_map[i][2] != pos)
269  break;
270  if (layout_map[i][0] == TYPE_CPE) {
271  if (sce_parity) {
272  if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
273  sce_parity = 0;
274  } else {
275  return -1;
276  }
277  }
278  num_pos_channels += 2;
279  first_cpe = 1;
280  } else {
281  num_pos_channels++;
282  sce_parity ^= (pos != AAC_CHANNEL_LFE);
283  }
284  }
285  if (sce_parity &&
286  (pos == AAC_CHANNEL_FRONT && first_cpe))
287  return -1;
288 
289  return num_pos_channels;
290 }
291 
292 static int assign_channels(struct elem_to_channel e2c_vec[MAX_ELEM_ID], uint8_t (*layout_map)[3],
293  uint64_t *layout, int tags, int layer, int pos, int *current)
294 {
295  int i = *current, j = 0;
296  int nb_channels = count_paired_channels(layout_map, tags, pos, i);
297 
298  if (nb_channels < 0 || nb_channels > 5)
299  return 0;
300 
301  if (pos == AAC_CHANNEL_LFE) {
302  while (nb_channels) {
303  if (ff_aac_channel_map[layer][pos - 1][j] == AV_CHAN_NONE)
304  return -1;
305  e2c_vec[i] = (struct elem_to_channel) {
306  .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][j],
307  .syn_ele = layout_map[i][0],
308  .elem_id = layout_map[i][1],
309  .aac_position = pos
310  };
311  *layout |= e2c_vec[i].av_position;
312  i++;
313  j++;
314  nb_channels--;
315  }
316  *current = i;
317 
318  return 0;
319  }
320 
321  while (nb_channels & 1) {
322  if (ff_aac_channel_map[layer][pos - 1][0] == AV_CHAN_NONE)
323  return -1;
324  if (ff_aac_channel_map[layer][pos - 1][0] == AV_CHAN_UNUSED)
325  break;
326  e2c_vec[i] = (struct elem_to_channel) {
327  .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][0],
328  .syn_ele = layout_map[i][0],
329  .elem_id = layout_map[i][1],
330  .aac_position = pos
331  };
332  *layout |= e2c_vec[i].av_position;
333  i++;
334  nb_channels--;
335  }
336 
337  j = (pos != AAC_CHANNEL_SIDE) && nb_channels <= 3 ? 3 : 1;
338  while (nb_channels >= 2) {
339  if (ff_aac_channel_map[layer][pos - 1][j] == AV_CHAN_NONE ||
340  ff_aac_channel_map[layer][pos - 1][j+1] == AV_CHAN_NONE)
341  return -1;
342  i += assign_pair(e2c_vec, layout_map, i,
343  1ULL << ff_aac_channel_map[layer][pos - 1][j],
344  1ULL << ff_aac_channel_map[layer][pos - 1][j+1],
345  pos, layout);
346  j += 2;
347  nb_channels -= 2;
348  }
349  while (nb_channels & 1) {
350  if (ff_aac_channel_map[layer][pos - 1][5] == AV_CHAN_NONE)
351  return -1;
352  e2c_vec[i] = (struct elem_to_channel) {
353  .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][5],
354  .syn_ele = layout_map[i][0],
355  .elem_id = layout_map[i][1],
356  .aac_position = pos
357  };
358  *layout |= e2c_vec[i].av_position;
359  i++;
360  nb_channels--;
361  }
362  if (nb_channels)
363  return -1;
364 
365  *current = i;
366 
367  return 0;
368 }
369 
370 static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
371 {
372  int i, n, total_non_cc_elements;
373  struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
374  uint64_t layout = 0;
375 
376  if (FF_ARRAY_ELEMS(e2c_vec) < tags)
377  return 0;
378 
379  for (n = 0, i = 0; n < 3 && i < tags; n++) {
380  int ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_FRONT, &i);
381  if (ret < 0)
382  return 0;
383  ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_SIDE, &i);
384  if (ret < 0)
385  return 0;
386  ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_BACK, &i);
387  if (ret < 0)
388  return 0;
389  ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_LFE, &i);
390  if (ret < 0)
391  return 0;
392  }
393 
394  total_non_cc_elements = n = i;
395 
396  if (layout == AV_CH_LAYOUT_22POINT2) {
397  // For 22.2 reorder the result as needed
398  FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[0]); // FL & FR first (final), FC third
399  FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[1]); // FC second (final), FLc & FRc third
400  FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[2]); // LFE1 third (final), FLc & FRc seventh
401  FFSWAP(struct elem_to_channel, e2c_vec[4], e2c_vec[3]); // BL & BR fourth (final), SiL & SiR fifth
402  FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[4]); // FLc & FRc fifth (final), SiL & SiR seventh
403  FFSWAP(struct elem_to_channel, e2c_vec[7], e2c_vec[6]); // LFE2 seventh (final), SiL & SiR eight (final)
404  FFSWAP(struct elem_to_channel, e2c_vec[9], e2c_vec[8]); // TpFL & TpFR ninth (final), TFC tenth (final)
405  FFSWAP(struct elem_to_channel, e2c_vec[11], e2c_vec[10]); // TC eleventh (final), TpSiL & TpSiR twelfth
406  FFSWAP(struct elem_to_channel, e2c_vec[12], e2c_vec[11]); // TpBL & TpBR twelfth (final), TpSiL & TpSiR thirteenth (final)
407  } else {
408  // For everything else, utilize the AV channel position define as a
409  // stable sort.
410  do {
411  int next_n = 0;
412  for (i = 1; i < n; i++)
413  if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
414  FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
415  next_n = i;
416  }
417  n = next_n;
418  } while (n > 0);
419 
420  }
421 
422  for (i = 0; i < total_non_cc_elements; i++) {
423  layout_map[i][0] = e2c_vec[i].syn_ele;
424  layout_map[i][1] = e2c_vec[i].elem_id;
425  layout_map[i][2] = e2c_vec[i].aac_position;
426  }
427 
428  return layout;
429 }
430 
432 {
433  int i;
434 
435  for (i = 0; i < src->usac.nb_elems; i++) {
436  AACUsacElemConfig *src_e = &src->usac.elems[i];
437  AACUsacElemConfig *dst_e = &dst->usac.elems[i];
438  /* dst_e->ext.pl_buf is guaranteed to be set to src_e->ext.pl_buf
439  * upon this function's return */
440  av_refstruct_replace(&dst_e->ext.pl_buf, src_e->ext.pl_buf);
441  }
442 
443  /* Unref all additional buffers to close leaks */
444  for (; i < dst->usac.nb_elems; i++)
445  av_refstruct_unref(&dst->usac.elems[i].ext.pl_buf);
446 
447  /* Set all other properties */
448  *dst = *src;
449 }
450 
451 /**
452  * Save current output configuration if and only if it has been locked.
453  */
455 {
456  int pushed = 0;
457 
458  if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
459  copy_oc(&ac->oc[0], &ac->oc[1]);
460  pushed = 1;
461  }
462  ac->oc[1].status = OC_NONE;
463  return pushed;
464 }
465 
466 /**
467  * Restore the previous output configuration if and only if the current
468  * configuration is unlocked.
469  */
471 {
472  if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
473  copy_oc(&ac->oc[1], &ac->oc[0]);
474 
475  ac->avctx->ch_layout = ac->oc[1].ch_layout;
477  ac->oc[1].status, 0);
478  }
479 }
480 
481 /**
482  * Configure output channel order based on the current program
483  * configuration element.
484  *
485  * @return Returns error status. 0 - OK, !0 - error
486  */
488  uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
489  enum OCStatus oc_type, int get_new_frame)
490 {
491  AVCodecContext *avctx = ac->avctx;
492  int i, channels = 0, ret;
493  uint64_t layout = 0;
494  uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
495  uint8_t type_counts[TYPE_END] = { 0 };
496 
497  if (get_new_frame && !ac->frame)
498  return AVERROR_INVALIDDATA;
499 
500  if (ac->oc[1].layout_map != layout_map) {
501  memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
502  ac->oc[1].layout_map_tags = tags;
503  }
504  for (i = 0; i < tags; i++) {
505  int type = layout_map[i][0];
506  int id = layout_map[i][1];
507  id_map[type][id] = type_counts[type]++;
508  if (id_map[type][id] >= MAX_ELEM_ID) {
509  avpriv_request_sample(ac->avctx, "Too large remapped id");
510  return AVERROR_PATCHWELCOME;
511  }
512  }
513  // Try to sniff a reasonable channel order, otherwise output the
514  // channels in the order the PCE declared them.
516  layout = sniff_channel_order(layout_map, tags);
517  for (i = 0; i < tags; i++) {
518  int type = layout_map[i][0];
519  int id = layout_map[i][1];
520  int iid = id_map[type][id];
521  int position = layout_map[i][2];
522  // Allocate or free elements depending on if they are in the
523  // current program configuration.
524  ret = che_configure(ac, position, type, iid, &channels);
525  if (ret < 0)
526  return ret;
527  ac->tag_che_map[type][id] = ac->che[type][iid];
528  }
529  if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
530  if (layout == AV_CH_FRONT_CENTER) {
532  } else {
533  layout = 0;
534  }
535  }
536 
538  if (layout)
540  else {
542  ac->oc[1].ch_layout.nb_channels = channels;
543  }
544 
545  av_channel_layout_copy(&avctx->ch_layout, &ac->oc[1].ch_layout);
546  ac->oc[1].status = oc_type;
547 
548  if (get_new_frame) {
549  if ((ret = frame_configure_elements(ac->avctx)) < 0)
550  return ret;
551  }
552 
553  return 0;
554 }
555 
556 static av_cold void flush(AVCodecContext *avctx)
557 {
558  AACDecContext *ac= avctx->priv_data;
559  int type, i, j;
560 
561  for (type = 3; type >= 0; type--) {
562  for (i = 0; i < MAX_ELEM_ID; i++) {
563  ChannelElement *che = ac->che[type][i];
564  if (che) {
565  for (j = 0; j <= 1; j++) {
566  memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
567  }
568  }
569  }
570  }
571 
572 #if CONFIG_AAC_DECODER
573  ff_aac_usac_reset_state(ac, &ac->oc[1]);
574 #endif
575 }
576 
577 /**
578  * Set up channel positions based on a default channel configuration
579  * as specified in table 1.17.
580  *
581  * @return Returns error status. 0 - OK, !0 - error
582  */
584  uint8_t (*layout_map)[3],
585  int *tags,
586  int channel_config)
587 {
588  if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
589  channel_config > 14) {
590  av_log(avctx, AV_LOG_ERROR,
591  "invalid default channel configuration (%d)\n",
592  channel_config);
593  return AVERROR_INVALIDDATA;
594  }
595  *tags = ff_tags_per_config[channel_config];
596  memcpy(layout_map, ff_aac_channel_layout_map[channel_config - 1],
597  *tags * sizeof(*layout_map));
598 
599  /*
600  * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
601  * However, at least Nero AAC encoder encodes 7.1 streams using the default
602  * channel config 7, mapping the side channels of the original audio stream
603  * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
604  * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
605  * the incorrect streams as if they were correct (and as the encoder intended).
606  *
607  * As actual intended 7.1(wide) streams are very rare, default to assuming a
608  * 7.1 layout was intended.
609  */
610  if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
611  layout_map[2][2] = AAC_CHANNEL_BACK;
612 
613  if (!ac || !ac->warned_71_wide++) {
614  av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
615  " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
616  " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
617  }
618  }
619 
620  return 0;
621 }
622 
624 {
625  /* For PCE based channel configurations map the channels solely based
626  * on tags. */
627  if (!ac->oc[1].m4ac.chan_config) {
628  return ac->tag_che_map[type][elem_id];
629  }
630  // Allow single CPE stereo files to be signalled with mono configuration.
631  if (!ac->tags_mapped && type == TYPE_CPE &&
632  ac->oc[1].m4ac.chan_config == 1) {
633  uint8_t layout_map[MAX_ELEM_ID*4][3];
634  int layout_map_tags;
636 
637  av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
638 
639  if (ff_aac_set_default_channel_config(ac, ac->avctx, layout_map,
640  &layout_map_tags, 2) < 0)
641  return NULL;
642  if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
643  OC_TRIAL_FRAME, 1) < 0)
644  return NULL;
645 
646  ac->oc[1].m4ac.chan_config = 2;
647  ac->oc[1].m4ac.ps = 0;
648  }
649  // And vice-versa
650  if (!ac->tags_mapped && type == TYPE_SCE &&
651  ac->oc[1].m4ac.chan_config == 2) {
652  uint8_t layout_map[MAX_ELEM_ID * 4][3];
653  int layout_map_tags;
655 
656  av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
657 
658  layout_map_tags = 2;
659  layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
660  layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
661  layout_map[0][1] = 0;
662  layout_map[1][1] = 1;
663  if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
664  OC_TRIAL_FRAME, 1) < 0)
665  return NULL;
666 
667  if (ac->oc[1].m4ac.sbr)
668  ac->oc[1].m4ac.ps = -1;
669  }
670  /* For indexed channel configurations map the channels solely based
671  * on position. */
672  switch (ac->oc[1].m4ac.chan_config) {
673  case 14:
674  if (ac->tags_mapped > 2 && ((type == TYPE_CPE && elem_id < 3) ||
675  (type == TYPE_LFE && elem_id < 1))) {
676  ac->tags_mapped++;
677  return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
678  }
679  case 13:
680  if (ac->tags_mapped > 3 && ((type == TYPE_CPE && elem_id < 8) ||
681  (type == TYPE_SCE && elem_id < 6) ||
682  (type == TYPE_LFE && elem_id < 2))) {
683  ac->tags_mapped++;
684  return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
685  }
686  case 12:
687  case 7:
688  if (ac->tags_mapped == 3 && type == TYPE_CPE) {
689  ac->tags_mapped++;
690  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
691  }
692  case 11:
693  if (ac->tags_mapped == 3 && type == TYPE_SCE) {
694  ac->tags_mapped++;
695  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
696  }
697  case 6:
698  /* Some streams incorrectly code 5.1 audio as
699  * SCE[0] CPE[0] CPE[1] SCE[1]
700  * instead of
701  * SCE[0] CPE[0] CPE[1] LFE[0].
702  * If we seem to have encountered such a stream, transfer
703  * the LFE[0] element to the SCE[1]'s mapping */
704  if (ac->tags_mapped == ff_tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
705  if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
707  "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
708  type == TYPE_SCE ? "SCE" : "LFE", elem_id);
709  ac->warned_remapping_once++;
710  }
711  ac->tags_mapped++;
712  return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
713  }
714  case 5:
715  if (ac->tags_mapped == 2 && type == TYPE_CPE) {
716  ac->tags_mapped++;
717  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
718  }
719  case 4:
720  /* Some streams incorrectly code 4.0 audio as
721  * SCE[0] CPE[0] LFE[0]
722  * instead of
723  * SCE[0] CPE[0] SCE[1].
724  * If we seem to have encountered such a stream, transfer
725  * the SCE[1] element to the LFE[0]'s mapping */
726  if (ac->tags_mapped == ff_tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
727  if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
729  "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
730  type == TYPE_SCE ? "SCE" : "LFE", elem_id);
731  ac->warned_remapping_once++;
732  }
733  ac->tags_mapped++;
734  return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
735  }
736  if (ac->tags_mapped == 2 &&
737  ac->oc[1].m4ac.chan_config == 4 &&
738  type == TYPE_SCE) {
739  ac->tags_mapped++;
740  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
741  }
742  case 3:
743  case 2:
744  if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
745  type == TYPE_CPE) {
746  ac->tags_mapped++;
747  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
748  } else if (ac->tags_mapped == 1 && ac->oc[1].m4ac.chan_config == 2 &&
749  type == TYPE_SCE) {
750  ac->tags_mapped++;
751  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
752  }
753  case 1:
754  if (!ac->tags_mapped && type == TYPE_SCE) {
755  ac->tags_mapped++;
756  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
757  }
758  default:
759  return NULL;
760  }
761 }
762 
763 /**
764  * Decode an array of 4 bit element IDs, optionally interleaved with a
765  * stereo/mono switching bit.
766  *
767  * @param type speaker type/position for these channels
768  */
769 static void decode_channel_map(uint8_t layout_map[][3],
770  enum ChannelPosition type,
771  GetBitContext *gb, int n)
772 {
773  while (n--) {
775  switch (type) {
776  case AAC_CHANNEL_FRONT:
777  case AAC_CHANNEL_BACK:
778  case AAC_CHANNEL_SIDE:
779  syn_ele = get_bits1(gb);
780  break;
781  case AAC_CHANNEL_CC:
782  skip_bits1(gb);
783  syn_ele = TYPE_CCE;
784  break;
785  case AAC_CHANNEL_LFE:
786  syn_ele = TYPE_LFE;
787  break;
788  default:
789  // AAC_CHANNEL_OFF has no channel map
790  av_assert0(0);
791  }
792  layout_map[0][0] = syn_ele;
793  layout_map[0][1] = get_bits(gb, 4);
794  layout_map[0][2] = type;
795  layout_map++;
796  }
797 }
798 
799 static inline void relative_align_get_bits(GetBitContext *gb,
800  int reference_position) {
801  int n = (reference_position - get_bits_count(gb) & 7);
802  if (n)
803  skip_bits(gb, n);
804 }
805 
806 /**
807  * Decode program configuration element; reference: table 4.2.
808  *
809  * @return Returns error status. 0 - OK, !0 - error
810  */
811 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
812  uint8_t (*layout_map)[3],
813  GetBitContext *gb, int byte_align_ref)
814 {
815  int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
816  int sampling_index;
817  int comment_len;
818  int tags;
819 
820  skip_bits(gb, 2); // object_type
821 
822  sampling_index = get_bits(gb, 4);
823  if (m4ac->sampling_index != sampling_index)
824  av_log(avctx, AV_LOG_WARNING,
825  "Sample rate index in program config element does not "
826  "match the sample rate index configured by the container.\n");
827 
828  num_front = get_bits(gb, 4);
829  num_side = get_bits(gb, 4);
830  num_back = get_bits(gb, 4);
831  num_lfe = get_bits(gb, 2);
832  num_assoc_data = get_bits(gb, 3);
833  num_cc = get_bits(gb, 4);
834 
835  if (get_bits1(gb))
836  skip_bits(gb, 4); // mono_mixdown_tag
837  if (get_bits1(gb))
838  skip_bits(gb, 4); // stereo_mixdown_tag
839 
840  if (get_bits1(gb))
841  skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
842 
843  if (get_bits_left(gb) < 5 * (num_front + num_side + num_back + num_cc) + 4 *(num_lfe + num_assoc_data + num_cc)) {
844  av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
845  return -1;
846  }
847  decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
848  tags = num_front;
849  decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
850  tags += num_side;
851  decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
852  tags += num_back;
853  decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
854  tags += num_lfe;
855 
856  skip_bits_long(gb, 4 * num_assoc_data);
857 
858  decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
859  tags += num_cc;
860 
861  relative_align_get_bits(gb, byte_align_ref);
862 
863  /* comment field, first byte is length */
864  comment_len = get_bits(gb, 8) * 8;
865  if (get_bits_left(gb) < comment_len) {
866  av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
867  return AVERROR_INVALIDDATA;
868  }
869  skip_bits_long(gb, comment_len);
870  return tags;
871 }
872 
873 /**
874  * Decode GA "General Audio" specific configuration; reference: table 4.1.
875  *
876  * @param ac pointer to AACDecContext, may be null
877  * @param avctx pointer to AVCCodecContext, used for logging
878  *
879  * @return Returns error status. 0 - OK, !0 - error
880  */
882  GetBitContext *gb,
883  int get_bit_alignment,
884  MPEG4AudioConfig *m4ac,
885  int channel_config)
886 {
887  int extension_flag, ret, ep_config, res_flags;
888  uint8_t layout_map[MAX_ELEM_ID*4][3];
889  int tags = 0;
890 
891  m4ac->frame_length_short = get_bits1(gb);
892  if (m4ac->frame_length_short && m4ac->sbr == 1) {
893  avpriv_report_missing_feature(avctx, "SBR with 960 frame length");
894  if (ac) ac->warned_960_sbr = 1;
895  m4ac->sbr = 0;
896  m4ac->ps = 0;
897  }
898 
899  if (get_bits1(gb)) // dependsOnCoreCoder
900  skip_bits(gb, 14); // coreCoderDelay
901  extension_flag = get_bits1(gb);
902 
903  if (m4ac->object_type == AOT_AAC_SCALABLE ||
905  skip_bits(gb, 3); // layerNr
906 
907  if (channel_config == 0) {
908  skip_bits(gb, 4); // element_instance_tag
909  tags = decode_pce(avctx, m4ac, layout_map, gb, get_bit_alignment);
910  if (tags < 0)
911  return tags;
912  } else {
913  if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
914  &tags, channel_config)))
915  return ret;
916  }
917 
918  if (count_channels(layout_map, tags) > 1) {
919  m4ac->ps = 0;
920  } else if (m4ac->sbr == 1 && m4ac->ps == -1)
921  m4ac->ps = 1;
922 
923  if (ac && (ret = ff_aac_output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
924  return ret;
925 
926  if (extension_flag) {
927  switch (m4ac->object_type) {
928  case AOT_ER_BSAC:
929  skip_bits(gb, 5); // numOfSubFrame
930  skip_bits(gb, 11); // layer_length
931  break;
932  case AOT_ER_AAC_LC:
933  case AOT_ER_AAC_LTP:
934  case AOT_ER_AAC_SCALABLE:
935  case AOT_ER_AAC_LD:
936  res_flags = get_bits(gb, 3);
937  if (res_flags) {
939  "AAC data resilience (flags %x)",
940  res_flags);
941  return AVERROR_PATCHWELCOME;
942  }
943  break;
944  }
945  skip_bits1(gb); // extensionFlag3 (TBD in version 3)
946  }
947  switch (m4ac->object_type) {
948  case AOT_ER_AAC_LC:
949  case AOT_ER_AAC_LTP:
950  case AOT_ER_AAC_SCALABLE:
951  case AOT_ER_AAC_LD:
952  ep_config = get_bits(gb, 2);
953  if (ep_config) {
955  "epConfig %d", ep_config);
956  return AVERROR_PATCHWELCOME;
957  }
958  }
959  return 0;
960 }
961 
963  GetBitContext *gb,
964  MPEG4AudioConfig *m4ac,
965  int channel_config)
966 {
967  int ret, ep_config, res_flags;
968  uint8_t layout_map[MAX_ELEM_ID*4][3];
969  int tags = 0;
970  const int ELDEXT_TERM = 0;
971 
972  m4ac->ps = 0;
973  m4ac->sbr = 0;
974  m4ac->frame_length_short = get_bits1(gb);
975 
976  res_flags = get_bits(gb, 3);
977  if (res_flags) {
979  "AAC data resilience (flags %x)",
980  res_flags);
981  return AVERROR_PATCHWELCOME;
982  }
983 
984  if (get_bits1(gb)) { // ldSbrPresentFlag
986  "Low Delay SBR");
987  return AVERROR_PATCHWELCOME;
988  }
989 
990  while (get_bits(gb, 4) != ELDEXT_TERM) {
991  int len = get_bits(gb, 4);
992  if (len == 15)
993  len += get_bits(gb, 8);
994  if (len == 15 + 255)
995  len += get_bits(gb, 16);
996  if (get_bits_left(gb) < len * 8 + 4) {
998  return AVERROR_INVALIDDATA;
999  }
1000  skip_bits_long(gb, 8 * len);
1001  }
1002 
1003  if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1004  &tags, channel_config)))
1005  return ret;
1006 
1007  if (ac && (ret = ff_aac_output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
1008  return ret;
1009 
1010  ep_config = get_bits(gb, 2);
1011  if (ep_config) {
1013  "epConfig %d", ep_config);
1014  return AVERROR_PATCHWELCOME;
1015  }
1016  return 0;
1017 }
1018 
1019 /**
1020  * Decode audio specific configuration; reference: table 1.13.
1021  *
1022  * @param ac pointer to AACDecContext, may be null
1023  * @param avctx pointer to AVCCodecContext, used for logging
1024  * @param m4ac pointer to MPEG4AudioConfig, used for parsing
1025  * @param gb buffer holding an audio specific config
1026  * @param get_bit_alignment relative alignment for byte align operations
1027  * @param sync_extension look for an appended sync extension
1028  *
1029  * @return Returns error status or number of consumed bits. <0 - error
1030  */
1032  AVCodecContext *avctx,
1033  OutputConfiguration *oc,
1034  GetBitContext *gb,
1035  int get_bit_alignment,
1036  int sync_extension)
1037 {
1038  int i, ret;
1039  GetBitContext gbc = *gb;
1040  MPEG4AudioConfig *m4ac = &oc->m4ac;
1041  MPEG4AudioConfig m4ac_bak = *m4ac;
1042 
1043  if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension, avctx)) < 0) {
1044  *m4ac = m4ac_bak;
1045  return AVERROR_INVALIDDATA;
1046  }
1047 
1048  if (m4ac->sampling_index > 12) {
1049  av_log(avctx, AV_LOG_ERROR,
1050  "invalid sampling rate index %d\n",
1051  m4ac->sampling_index);
1052  *m4ac = m4ac_bak;
1053  return AVERROR_INVALIDDATA;
1054  }
1055  if (m4ac->object_type == AOT_ER_AAC_LD &&
1056  (m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
1057  av_log(avctx, AV_LOG_ERROR,
1058  "invalid low delay sampling rate index %d\n",
1059  m4ac->sampling_index);
1060  *m4ac = m4ac_bak;
1061  return AVERROR_INVALIDDATA;
1062  }
1063 
1064  skip_bits_long(gb, i);
1065 
1066  switch (m4ac->object_type) {
1067  case AOT_AAC_MAIN:
1068  case AOT_AAC_LC:
1069  case AOT_AAC_SSR:
1070  case AOT_AAC_LTP:
1071  case AOT_ER_AAC_LC:
1072  case AOT_ER_AAC_LD:
1073  if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
1074  &oc->m4ac, m4ac->chan_config)) < 0)
1075  return ret;
1076  break;
1077  case AOT_ER_AAC_ELD:
1078  if ((ret = decode_eld_specific_config(ac, avctx, gb,
1079  &oc->m4ac, m4ac->chan_config)) < 0)
1080  return ret;
1081  break;
1082 #if CONFIG_AAC_DECODER
1083  case AOT_USAC:
1084  if ((ret = ff_aac_usac_config_decode(ac, avctx, gb,
1085  oc, m4ac->chan_config)) < 0)
1086  return ret;
1087  break;
1088 #endif
1089  default:
1091  "Audio object type %s%d",
1092  m4ac->sbr == 1 ? "SBR+" : "",
1093  m4ac->object_type);
1094  return AVERROR(ENOSYS);
1095  }
1096 
1097  ff_dlog(avctx,
1098  "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
1099  m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
1100  m4ac->sample_rate, m4ac->sbr,
1101  m4ac->ps);
1102 
1103  return get_bits_count(gb);
1104 }
1105 
1107  AVCodecContext *avctx,
1108  OutputConfiguration *oc,
1109  const uint8_t *data, int64_t bit_size,
1110  int sync_extension)
1111 {
1112  int i, ret;
1113  GetBitContext gb;
1114 
1115  if (bit_size < 0 || bit_size > INT_MAX) {
1116  av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
1117  return AVERROR_INVALIDDATA;
1118  }
1119 
1120  ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
1121  for (i = 0; i < bit_size >> 3; i++)
1122  ff_dlog(avctx, "%02x ", data[i]);
1123  ff_dlog(avctx, "\n");
1124 
1125  if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
1126  return ret;
1127 
1128  return decode_audio_specific_config_gb(ac, avctx, oc, &gb, 0,
1129  sync_extension);
1130 }
1131 
1133 {
1134  AACDecContext *ac = avctx->priv_data;
1135 
1136  for (int i = 0; i < 2; i++) {
1137  OutputConfiguration *oc = &ac->oc[i];
1138  AACUSACConfig *usac = &oc->usac;
1139  for (int j = 0; j < usac->nb_elems; j++) {
1140  AACUsacElemConfig *ec = &usac->elems[j];
1142  }
1143 
1145  }
1146 
1147  for (int type = 0; type < FF_ARRAY_ELEMS(ac->che); type++) {
1148  for (int i = 0; i < MAX_ELEM_ID; i++) {
1149  if (ac->che[type][i]) {
1150  ac->proc.sbr_ctx_close(ac->che[type][i]);
1151  av_freep(&ac->che[type][i]);
1152  }
1153  }
1154  }
1155 
1156  av_tx_uninit(&ac->mdct96);
1157  av_tx_uninit(&ac->mdct120);
1158  av_tx_uninit(&ac->mdct128);
1159  av_tx_uninit(&ac->mdct480);
1160  av_tx_uninit(&ac->mdct512);
1161  av_tx_uninit(&ac->mdct768);
1162  av_tx_uninit(&ac->mdct960);
1163  av_tx_uninit(&ac->mdct1024);
1164  av_tx_uninit(&ac->mdct_ltp);
1165 
1166  // Compiler will optimize this branch away.
1167  if (ac->is_fixed)
1168  av_freep(&ac->RENAME_FIXED(fdsp));
1169  else
1170  av_freep(&ac->fdsp);
1171 
1172  return 0;
1173 }
1174 
1175 static av_cold int init_dsp(AVCodecContext *avctx)
1176 {
1177  AACDecContext *ac = avctx->priv_data;
1178  int is_fixed = ac->is_fixed, ret;
1179  float scale_fixed, scale_float;
1180  const float *const scalep = is_fixed ? &scale_fixed : &scale_float;
1181  enum AVTXType tx_type = is_fixed ? AV_TX_INT32_MDCT : AV_TX_FLOAT_MDCT;
1182 
1183 #define MDCT_INIT(s, fn, len, sval) \
1184  scale_fixed = (sval) * 128.0f; \
1185  scale_float = (sval) / 32768.0f; \
1186  ret = av_tx_init(&s, &fn, tx_type, 1, len, scalep, 0); \
1187  if (ret < 0) \
1188  return ret
1189 
1190  MDCT_INIT(ac->mdct96, ac->mdct96_fn, 96, 1.0/96);
1191  MDCT_INIT(ac->mdct120, ac->mdct120_fn, 120, 1.0/120);
1192  MDCT_INIT(ac->mdct128, ac->mdct128_fn, 128, 1.0/128);
1193  MDCT_INIT(ac->mdct480, ac->mdct480_fn, 480, 1.0/480);
1194  MDCT_INIT(ac->mdct512, ac->mdct512_fn, 512, 1.0/512);
1195  MDCT_INIT(ac->mdct768, ac->mdct768_fn, 768, 1.0/768);
1196  MDCT_INIT(ac->mdct960, ac->mdct960_fn, 960, 1.0/960);
1197  MDCT_INIT(ac->mdct1024, ac->mdct1024_fn, 1024, 1.0/1024);
1198 #undef MDCT_INIT
1199 
1200  /* LTP forward MDCT */
1201  scale_fixed = -1.0;
1202  scale_float = -32786.0*2 + 36;
1203  ret = av_tx_init(&ac->mdct_ltp, &ac->mdct_ltp_fn, tx_type, 0, 1024, scalep, 0);
1204  if (ret < 0)
1205  return ret;
1206 
1207  return 0;
1208 }
1209 
1211 {
1212  AACDecContext *ac = avctx->priv_data;
1213  int ret;
1214 
1215  if (avctx->sample_rate > 96000)
1216  return AVERROR_INVALIDDATA;
1217 
1219 
1220  ac->avctx = avctx;
1221  ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
1222 
1223  if (avctx->extradata_size > 0) {
1224  if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
1225  avctx->extradata,
1226  avctx->extradata_size * 8LL,
1227  1)) < 0)
1228  return ret;
1229  } else {
1230  int sr, i;
1231  uint8_t layout_map[MAX_ELEM_ID*4][3];
1232  int layout_map_tags;
1233 
1234  sr = ff_aac_sample_rate_idx(avctx->sample_rate);
1235  ac->oc[1].m4ac.sampling_index = sr;
1236  ac->oc[1].m4ac.channels = avctx->ch_layout.nb_channels;
1237  ac->oc[1].m4ac.sbr = -1;
1238  ac->oc[1].m4ac.ps = -1;
1239 
1240  for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
1242  break;
1244  i = 0;
1245  }
1246  ac->oc[1].m4ac.chan_config = i;
1247 
1248  if (ac->oc[1].m4ac.chan_config) {
1249  int ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1250  &layout_map_tags,
1251  ac->oc[1].m4ac.chan_config);
1252  if (!ret)
1253  ff_aac_output_configure(ac, layout_map, layout_map_tags,
1254  OC_GLOBAL_HDR, 0);
1255  else if (avctx->err_recognition & AV_EF_EXPLODE)
1256  return AVERROR_INVALIDDATA;
1257  }
1258  }
1259 
1260  if (avctx->ch_layout.nb_channels > MAX_CHANNELS) {
1261  av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
1262  return AVERROR_INVALIDDATA;
1263  }
1264 
1265  ac->random_state = 0x1f2e3d4c;
1266 
1267  return init_dsp(avctx);
1268 }
1269 
1270 /**
1271  * Skip data_stream_element; reference: table 4.10.
1272  */
1274 {
1275  int byte_align = get_bits1(gb);
1276  int count = get_bits(gb, 8);
1277  if (count == 255)
1278  count += get_bits(gb, 8);
1279  if (byte_align)
1280  align_get_bits(gb);
1281 
1282  if (get_bits_left(gb) < 8 * count) {
1283  av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
1284  return AVERROR_INVALIDDATA;
1285  }
1286  skip_bits_long(gb, 8 * count);
1287  return 0;
1288 }
1289 
1291  GetBitContext *gb)
1292 {
1293  int sfb;
1294  if (get_bits1(gb)) {
1295  ics->predictor_reset_group = get_bits(gb, 5);
1296  if (ics->predictor_reset_group == 0 ||
1297  ics->predictor_reset_group > 30) {
1298  av_log(ac->avctx, AV_LOG_ERROR,
1299  "Invalid Predictor Reset Group.\n");
1300  return AVERROR_INVALIDDATA;
1301  }
1302  }
1303  for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1304  ics->prediction_used[sfb] = get_bits1(gb);
1305  }
1306  return 0;
1307 }
1308 
1309 /**
1310  * Decode Long Term Prediction data; reference: table 4.xx.
1311  */
1313  GetBitContext *gb, uint8_t max_sfb)
1314 {
1315  int sfb;
1316 
1317  ltp->lag = get_bits(gb, 11);
1318  if (CONFIG_AAC_FIXED_DECODER && ac->is_fixed)
1319  ltp->coef_fixed = Q30(ff_ltp_coef[get_bits(gb, 3)]);
1320  else if (CONFIG_AAC_DECODER)
1321  ltp->coef = ff_ltp_coef[get_bits(gb, 3)];
1322 
1323  for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
1324  ltp->used[sfb] = get_bits1(gb);
1325 }
1326 
1327 /**
1328  * Decode Individual Channel Stream info; reference: table 4.6.
1329  */
1331  GetBitContext *gb)
1332 {
1333  const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
1334  const int aot = m4ac->object_type;
1335  const int sampling_index = m4ac->sampling_index;
1336  int ret_fail = AVERROR_INVALIDDATA;
1337 
1338  if (aot != AOT_ER_AAC_ELD) {
1339  if (get_bits1(gb)) {
1340  av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1342  return AVERROR_INVALIDDATA;
1343  }
1344  ics->window_sequence[1] = ics->window_sequence[0];
1345  ics->window_sequence[0] = get_bits(gb, 2);
1346  if (aot == AOT_ER_AAC_LD &&
1347  ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
1348  av_log(ac->avctx, AV_LOG_ERROR,
1349  "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
1350  "window sequence %d found.\n", ics->window_sequence[0]);
1352  return AVERROR_INVALIDDATA;
1353  }
1354  ics->use_kb_window[1] = ics->use_kb_window[0];
1355  ics->use_kb_window[0] = get_bits1(gb);
1356  }
1358  ics->num_window_groups = 1;
1359  ics->group_len[0] = 1;
1360  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1361  int i;
1362  ics->max_sfb = get_bits(gb, 4);
1363  for (i = 0; i < 7; i++) {
1364  if (get_bits1(gb)) {
1365  ics->group_len[ics->num_window_groups - 1]++;
1366  } else {
1367  ics->num_window_groups++;
1368  ics->group_len[ics->num_window_groups - 1] = 1;
1369  }
1370  }
1371  ics->num_windows = 8;
1372  if (m4ac->frame_length_short) {
1373  ics->swb_offset = ff_swb_offset_120[sampling_index];
1374  ics->num_swb = ff_aac_num_swb_120[sampling_index];
1375  } else {
1376  ics->swb_offset = ff_swb_offset_128[sampling_index];
1377  ics->num_swb = ff_aac_num_swb_128[sampling_index];
1378  }
1379  ics->tns_max_bands = ff_tns_max_bands_128[sampling_index];
1380  ics->predictor_present = 0;
1381  } else {
1382  ics->max_sfb = get_bits(gb, 6);
1383  ics->num_windows = 1;
1384  if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
1385  if (m4ac->frame_length_short) {
1386  ics->swb_offset = ff_swb_offset_480[sampling_index];
1387  ics->num_swb = ff_aac_num_swb_480[sampling_index];
1388  ics->tns_max_bands = ff_tns_max_bands_480[sampling_index];
1389  } else {
1390  ics->swb_offset = ff_swb_offset_512[sampling_index];
1391  ics->num_swb = ff_aac_num_swb_512[sampling_index];
1392  ics->tns_max_bands = ff_tns_max_bands_512[sampling_index];
1393  }
1394  if (!ics->num_swb || !ics->swb_offset) {
1395  ret_fail = AVERROR_BUG;
1396  goto fail;
1397  }
1398  } else {
1399  if (m4ac->frame_length_short) {
1400  ics->num_swb = ff_aac_num_swb_960[sampling_index];
1401  ics->swb_offset = ff_swb_offset_960[sampling_index];
1402  } else {
1403  ics->num_swb = ff_aac_num_swb_1024[sampling_index];
1404  ics->swb_offset = ff_swb_offset_1024[sampling_index];
1405  }
1406  ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
1407  }
1408  if (aot != AOT_ER_AAC_ELD) {
1409  ics->predictor_present = get_bits1(gb);
1410  ics->predictor_reset_group = 0;
1411  }
1412  if (ics->predictor_present) {
1413  if (aot == AOT_AAC_MAIN) {
1414  if (decode_prediction(ac, ics, gb)) {
1415  goto fail;
1416  }
1417  } else if (aot == AOT_AAC_LC ||
1418  aot == AOT_ER_AAC_LC) {
1419  av_log(ac->avctx, AV_LOG_ERROR,
1420  "Prediction is not allowed in AAC-LC.\n");
1421  goto fail;
1422  } else {
1423  if (aot == AOT_ER_AAC_LD) {
1424  av_log(ac->avctx, AV_LOG_ERROR,
1425  "LTP in ER AAC LD not yet implemented.\n");
1426  ret_fail = AVERROR_PATCHWELCOME;
1427  goto fail;
1428  }
1429  if ((ics->ltp.present = get_bits(gb, 1)))
1430  decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
1431  }
1432  }
1433  }
1434 
1435  if (ics->max_sfb > ics->num_swb) {
1436  av_log(ac->avctx, AV_LOG_ERROR,
1437  "Number of scalefactor bands in group (%d) "
1438  "exceeds limit (%d).\n",
1439  ics->max_sfb, ics->num_swb);
1440  goto fail;
1441  }
1442 
1443  return 0;
1444 fail:
1445  ics->max_sfb = 0;
1446  return ret_fail;
1447 }
1448 
1449 /**
1450  * Decode band types (section_data payload); reference: table 4.46.
1451  *
1452  * @param band_type array of the used band type
1453  * @param band_type_run_end array of the last scalefactor band of a band type run
1454  *
1455  * @return Returns error status. 0 - OK, !0 - error
1456  */
1458  GetBitContext *gb)
1459 {
1460  IndividualChannelStream *ics = &sce->ics;
1461  const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1462 
1463  for (int g = 0; g < ics->num_window_groups; g++) {
1464  int k = 0;
1465  while (k < ics->max_sfb) {
1466  uint8_t sect_end = k;
1467  int sect_len_incr;
1468  int sect_band_type = get_bits(gb, 4);
1469  if (sect_band_type == 12) {
1470  av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1471  return AVERROR_INVALIDDATA;
1472  }
1473  do {
1474  sect_len_incr = get_bits(gb, bits);
1475  sect_end += sect_len_incr;
1476  if (get_bits_left(gb) < 0) {
1477  av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1478  return AVERROR_INVALIDDATA;
1479  }
1480  if (sect_end > ics->max_sfb) {
1481  av_log(ac->avctx, AV_LOG_ERROR,
1482  "Number of bands (%d) exceeds limit (%d).\n",
1483  sect_end, ics->max_sfb);
1484  return AVERROR_INVALIDDATA;
1485  }
1486  } while (sect_len_incr == (1 << bits) - 1);
1487  for (; k < sect_end; k++)
1488  sce->band_type[g*ics->max_sfb + k] = sect_band_type;
1489  }
1490  }
1491  return 0;
1492 }
1493 
1494 /**
1495  * Decode scalefactors; reference: table 4.47.
1496  *
1497  * @param global_gain first scalefactor value as scalefactors are differentially coded
1498  * @param band_type array of the used band type
1499  * @param band_type_run_end array of the last scalefactor band of a band type run
1500  * @param sf array of scalefactors or intensity stereo positions
1501  *
1502  * @return Returns error status. 0 - OK, !0 - error
1503  */
1505  GetBitContext *gb, unsigned int global_gain)
1506 {
1507  IndividualChannelStream *ics = &sce->ics;
1508  int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
1509  int clipped_offset;
1510  int noise_flag = 1;
1511 
1512  for (int g = 0; g < ics->num_window_groups; g++) {
1513  for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1514  switch (sce->band_type[g*ics->max_sfb + sfb]) {
1515  case ZERO_BT:
1516  sce->sfo[g*ics->max_sfb + sfb] = 0;
1517  break;
1518  case INTENSITY_BT: /* fallthrough */
1519  case INTENSITY_BT2:
1521  clipped_offset = av_clip(offset[2], -155, 100);
1522  if (offset[2] != clipped_offset) {
1524  "If you heard an audible artifact, there may be a bug in the decoder. "
1525  "Clipped intensity stereo position (%d -> %d)",
1526  offset[2], clipped_offset);
1527  }
1528  sce->sfo[g*ics->max_sfb + sfb] = clipped_offset - 100;
1529  break;
1530  case NOISE_BT:
1531  if (noise_flag-- > 0)
1532  offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
1533  else
1535  clipped_offset = av_clip(offset[1], -100, 155);
1536  if (offset[1] != clipped_offset) {
1538  "If you heard an audible artifact, there may be a bug in the decoder. "
1539  "Clipped noise gain (%d -> %d)",
1540  offset[1], clipped_offset);
1541  }
1542  sce->sfo[g*ics->max_sfb + sfb] = clipped_offset;
1543  break;
1544  default:
1546  if (offset[0] > 255U) {
1547  av_log(ac->avctx, AV_LOG_ERROR,
1548  "Scalefactor (%d) out of range.\n", offset[0]);
1549  return AVERROR_INVALIDDATA;
1550  }
1551  sce->sfo[g*ics->max_sfb + sfb] = offset[0] - 100;
1552  break;
1553  }
1554  }
1555  }
1556 
1557  return 0;
1558 }
1559 
1560 /**
1561  * Decode pulse data; reference: table 4.7.
1562  */
1563 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1564  const uint16_t *swb_offset, int num_swb)
1565 {
1566  int i, pulse_swb;
1567  pulse->num_pulse = get_bits(gb, 2) + 1;
1568  pulse_swb = get_bits(gb, 6);
1569  if (pulse_swb >= num_swb)
1570  return -1;
1571  pulse->pos[0] = swb_offset[pulse_swb];
1572  pulse->pos[0] += get_bits(gb, 5);
1573  if (pulse->pos[0] >= swb_offset[num_swb])
1574  return -1;
1575  pulse->amp[0] = get_bits(gb, 4);
1576  for (i = 1; i < pulse->num_pulse; i++) {
1577  pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1578  if (pulse->pos[i] >= swb_offset[num_swb])
1579  return -1;
1580  pulse->amp[i] = get_bits(gb, 4);
1581  }
1582  return 0;
1583 }
1584 
1585 /**
1586  * Decode Temporal Noise Shaping data; reference: table 4.48.
1587  *
1588  * @return Returns error status. 0 - OK, !0 - error
1589  */
1591  GetBitContext *gb, const IndividualChannelStream *ics)
1592 {
1593  int tns_max_order = INT32_MAX;
1594  const int is_usac = ac->oc[1].m4ac.object_type == AOT_USAC;
1595  int w, filt, i, coef_len, coef_res, coef_compress;
1596  const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1597 
1598  /* USAC doesn't seem to have a limit */
1599  if (!is_usac)
1600  tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1601 
1602  for (w = 0; w < ics->num_windows; w++) {
1603  if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1604  coef_res = get_bits1(gb);
1605 
1606  for (filt = 0; filt < tns->n_filt[w]; filt++) {
1607  int tmp2_idx;
1608  tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1609 
1610  if (is_usac)
1611  tns->order[w][filt] = get_bits(gb, 4 - is8);
1612  else
1613  tns->order[w][filt] = get_bits(gb, 5 - (2 * is8));
1614 
1615  if (tns->order[w][filt] > tns_max_order) {
1616  av_log(ac->avctx, AV_LOG_ERROR,
1617  "TNS filter order %d is greater than maximum %d.\n",
1618  tns->order[w][filt], tns_max_order);
1619  tns->order[w][filt] = 0;
1620  return AVERROR_INVALIDDATA;
1621  }
1622  if (tns->order[w][filt]) {
1623  tns->direction[w][filt] = get_bits1(gb);
1624  coef_compress = get_bits1(gb);
1625  coef_len = coef_res + 3 - coef_compress;
1626  tmp2_idx = 2 * coef_compress + coef_res;
1627 
1628  for (i = 0; i < tns->order[w][filt]; i++) {
1629  if (CONFIG_AAC_FIXED_DECODER && ac->is_fixed)
1630  tns->coef_fixed[w][filt][i] = Q31(ff_tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)]);
1631  else if (CONFIG_AAC_DECODER)
1632  tns->coef[w][filt][i] = ff_tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1633  }
1634  }
1635  }
1636  }
1637  }
1638  return 0;
1639 }
1640 
1641 /**
1642  * Decode Mid/Side data; reference: table 4.54.
1643  *
1644  * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1645  * [1] mask is decoded from bitstream; [2] mask is all 1s;
1646  * [3] reserved for scalable AAC
1647  */
1649  int ms_present)
1650 {
1651  int idx;
1652  int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1653  cpe->max_sfb_ste = cpe->ch[0].ics.max_sfb;
1654  if (ms_present == 1) {
1655  for (idx = 0; idx < max_idx; idx++)
1656  cpe->ms_mask[idx] = get_bits1(gb);
1657  } else if (ms_present == 2) {
1658  memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
1659  }
1660 }
1661 
1663 {
1664  // wd_num, wd_test, aloc_size
1665  static const uint8_t gain_mode[4][3] = {
1666  {1, 0, 5}, // ONLY_LONG_SEQUENCE = 0,
1667  {2, 1, 2}, // LONG_START_SEQUENCE,
1668  {8, 0, 2}, // EIGHT_SHORT_SEQUENCE,
1669  {2, 1, 5}, // LONG_STOP_SEQUENCE
1670  };
1671 
1672  const int mode = sce->ics.window_sequence[0];
1673  uint8_t bd, wd, ad;
1674 
1675  // FIXME: Store the gain control data on |sce| and do something with it.
1676  uint8_t max_band = get_bits(gb, 2);
1677  for (bd = 0; bd < max_band; bd++) {
1678  for (wd = 0; wd < gain_mode[mode][0]; wd++) {
1679  uint8_t adjust_num = get_bits(gb, 3);
1680  for (ad = 0; ad < adjust_num; ad++) {
1681  skip_bits(gb, 4 + ((wd == 0 && gain_mode[mode][1])
1682  ? 4
1683  : gain_mode[mode][2]));
1684  }
1685  }
1686  }
1687 }
1688 
1689 /**
1690  * Decode an individual_channel_stream payload; reference: table 4.44.
1691  *
1692  * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
1693  * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1694  *
1695  * @return Returns error status. 0 - OK, !0 - error
1696  */
1698  GetBitContext *gb, int common_window, int scale_flag)
1699 {
1700  Pulse pulse;
1701  TemporalNoiseShaping *tns = &sce->tns;
1702  IndividualChannelStream *ics = &sce->ics;
1703  int global_gain, eld_syntax, er_syntax, pulse_present = 0;
1704  int ret;
1705 
1706  eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1707  er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
1708  ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
1709  ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
1710  ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1711 
1712  /* This assignment is to silence a GCC warning about the variable being used
1713  * uninitialized when in fact it always is.
1714  */
1715  pulse.num_pulse = 0;
1716 
1717  global_gain = get_bits(gb, 8);
1718 
1719  if (!common_window && !scale_flag) {
1720  ret = decode_ics_info(ac, ics, gb);
1721  if (ret < 0)
1722  goto fail;
1723  }
1724 
1725  if ((ret = decode_band_types(ac, sce, gb)) < 0)
1726  goto fail;
1727  if ((ret = decode_scalefactors(ac, sce, gb, global_gain)) < 0)
1728  goto fail;
1729 
1730  ac->dsp.dequant_scalefactors(sce);
1731 
1732  pulse_present = 0;
1733  if (!scale_flag) {
1734  if (!eld_syntax && (pulse_present = get_bits1(gb))) {
1735  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1736  av_log(ac->avctx, AV_LOG_ERROR,
1737  "Pulse tool not allowed in eight short sequence.\n");
1739  goto fail;
1740  }
1741  if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1742  av_log(ac->avctx, AV_LOG_ERROR,
1743  "Pulse data corrupt or invalid.\n");
1745  goto fail;
1746  }
1747  }
1748  tns->present = get_bits1(gb);
1749  if (tns->present && !er_syntax) {
1750  ret = ff_aac_decode_tns(ac, tns, gb, ics);
1751  if (ret < 0)
1752  goto fail;
1753  }
1754  if (!eld_syntax && get_bits1(gb)) {
1755  decode_gain_control(sce, gb);
1756  if (!ac->warned_gain_control) {
1757  avpriv_report_missing_feature(ac->avctx, "Gain control");
1758  ac->warned_gain_control = 1;
1759  }
1760  }
1761  // I see no textual basis in the spec for this occurring after SSR gain
1762  // control, but this is what both reference and real implementations do
1763  if (tns->present && er_syntax) {
1764  ret = ff_aac_decode_tns(ac, tns, gb, ics);
1765  if (ret < 0)
1766  goto fail;
1767  }
1768  }
1769 
1770  ret = ac->proc.decode_spectrum_and_dequant(ac, gb,
1771  pulse_present ? &pulse : NULL,
1772  sce);
1773  if (ret < 0)
1774  goto fail;
1775 
1776  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
1777  ac->dsp.apply_prediction(ac, sce);
1778 
1779  return 0;
1780 fail:
1781  memset(sce->sfo, 0, sizeof(sce->sfo));
1782  tns->present = 0;
1783  return ret;
1784 }
1785 
1786 /**
1787  * Decode a channel_pair_element; reference: table 4.4.
1788  *
1789  * @return Returns error status. 0 - OK, !0 - error
1790  */
1792 {
1793  int i, ret, common_window, ms_present = 0;
1794  int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1795 
1796  common_window = eld_syntax || get_bits1(gb);
1797  if (common_window) {
1798  if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
1799  return AVERROR_INVALIDDATA;
1800  i = cpe->ch[1].ics.use_kb_window[0];
1801  cpe->ch[1].ics = cpe->ch[0].ics;
1802  cpe->ch[1].ics.use_kb_window[1] = i;
1803  if (cpe->ch[1].ics.predictor_present &&
1804  (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
1805  if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
1806  decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
1807  ms_present = get_bits(gb, 2);
1808  if (ms_present == 3) {
1809  av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1810  return AVERROR_INVALIDDATA;
1811  } else if (ms_present)
1812  decode_mid_side_stereo(cpe, gb, ms_present);
1813  }
1814  if ((ret = ff_aac_decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1815  return ret;
1816  if ((ret = ff_aac_decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1817  return ret;
1818 
1819  if (common_window) {
1820  if (ms_present)
1821  ac->dsp.apply_mid_side_stereo(ac, cpe);
1822  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1823  ac->dsp.apply_prediction(ac, &cpe->ch[0]);
1824  ac->dsp.apply_prediction(ac, &cpe->ch[1]);
1825  }
1826  }
1827 
1828  ac->dsp.apply_intensity_stereo(ac, cpe, ms_present);
1829  return 0;
1830 }
1831 
1832 /**
1833  * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1834  *
1835  * @return Returns number of bytes consumed.
1836  */
1838  GetBitContext *gb)
1839 {
1840  int i;
1841  int num_excl_chan = 0;
1842 
1843  do {
1844  for (i = 0; i < 7; i++)
1845  che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1846  } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
1847 
1848  return num_excl_chan / 7;
1849 }
1850 
1851 /**
1852  * Decode dynamic range information; reference: table 4.52.
1853  *
1854  * @return Returns number of bytes consumed.
1855  */
1857  GetBitContext *gb)
1858 {
1859  int n = 1;
1860  int drc_num_bands = 1;
1861  int i;
1862 
1863  /* pce_tag_present? */
1864  if (get_bits1(gb)) {
1865  che_drc->pce_instance_tag = get_bits(gb, 4);
1866  skip_bits(gb, 4); // tag_reserved_bits
1867  n++;
1868  }
1869 
1870  /* excluded_chns_present? */
1871  if (get_bits1(gb)) {
1872  n += decode_drc_channel_exclusions(che_drc, gb);
1873  }
1874 
1875  /* drc_bands_present? */
1876  if (get_bits1(gb)) {
1877  che_drc->band_incr = get_bits(gb, 4);
1878  che_drc->interpolation_scheme = get_bits(gb, 4);
1879  n++;
1880  drc_num_bands += che_drc->band_incr;
1881  for (i = 0; i < drc_num_bands; i++) {
1882  che_drc->band_top[i] = get_bits(gb, 8);
1883  n++;
1884  }
1885  }
1886 
1887  /* prog_ref_level_present? */
1888  if (get_bits1(gb)) {
1889  che_drc->prog_ref_level = get_bits(gb, 7);
1890  skip_bits1(gb); // prog_ref_level_reserved_bits
1891  n++;
1892  }
1893 
1894  for (i = 0; i < drc_num_bands; i++) {
1895  che_drc->dyn_rng_sgn[i] = get_bits1(gb);
1896  che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
1897  n++;
1898  }
1899 
1900  return n;
1901 }
1902 
1903 static int decode_fill(AACDecContext *ac, GetBitContext *gb, int len) {
1904  uint8_t buf[256];
1905  int i, major, minor;
1906 
1907  if (len < 13+7*8)
1908  goto unknown;
1909 
1910  get_bits(gb, 13); len -= 13;
1911 
1912  for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
1913  buf[i] = get_bits(gb, 8);
1914 
1915  buf[i] = 0;
1916  if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
1917  av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
1918 
1919  if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
1920  ac->avctx->internal->skip_samples = 1024;
1921  }
1922 
1923 unknown:
1924  skip_bits_long(gb, len);
1925 
1926  return 0;
1927 }
1928 
1929 /**
1930  * Decode extension data (incomplete); reference: table 4.51.
1931  *
1932  * @param cnt length of TYPE_FIL syntactic element in bytes
1933  *
1934  * @return Returns number of bytes consumed
1935  */
1937  ChannelElement *che, enum RawDataBlockType elem_type)
1938 {
1939  int crc_flag = 0;
1940  int res = cnt;
1941  int type = get_bits(gb, 4);
1942 
1943  if (ac->avctx->debug & FF_DEBUG_STARTCODE)
1944  av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
1945 
1946  switch (type) { // extension type
1947  case EXT_SBR_DATA_CRC:
1948  crc_flag++;
1949  case EXT_SBR_DATA:
1950  if (!che) {
1951  av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
1952  return res;
1953  } else if (ac->oc[1].m4ac.frame_length_short) {
1954  if (!ac->warned_960_sbr)
1956  "SBR with 960 frame length");
1957  ac->warned_960_sbr = 1;
1958  skip_bits_long(gb, 8 * cnt - 4);
1959  return res;
1960  } else if (!ac->oc[1].m4ac.sbr) {
1961  av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
1962  skip_bits_long(gb, 8 * cnt - 4);
1963  return res;
1964  } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
1965  av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
1966  skip_bits_long(gb, 8 * cnt - 4);
1967  return res;
1968  } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED &&
1969  ac->avctx->ch_layout.nb_channels == 1) {
1970  ac->oc[1].m4ac.sbr = 1;
1971  ac->oc[1].m4ac.ps = 1;
1974  ac->oc[1].status, 1);
1975  } else {
1976  ac->oc[1].m4ac.sbr = 1;
1978  }
1979 
1980  ac->proc.sbr_decode_extension(ac, che, gb, crc_flag, cnt, elem_type);
1981 
1982  if (ac->oc[1].m4ac.ps == 1 && !ac->warned_he_aac_mono) {
1983  av_log(ac->avctx, AV_LOG_VERBOSE, "Treating HE-AAC mono as stereo.\n");
1984  ac->warned_he_aac_mono = 1;
1985  }
1986  break;
1987  case EXT_DYNAMIC_RANGE:
1988  res = decode_dynamic_range(&ac->che_drc, gb);
1989  break;
1990  case EXT_FILL:
1991  decode_fill(ac, gb, 8 * cnt - 4);
1992  break;
1993  case EXT_FILL_DATA:
1994  case EXT_DATA_ELEMENT:
1995  default:
1996  skip_bits_long(gb, 8 * cnt - 4);
1997  break;
1998  };
1999  return res;
2000 }
2001 
2002 /**
2003  * channel coupling transformation interface
2004  *
2005  * @param apply_coupling_method pointer to (in)dependent coupling function
2006  */
2008  enum RawDataBlockType type, int elem_id,
2009  enum CouplingPoint coupling_point,
2010  void (*apply_coupling_method)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2011 {
2012  int i, c;
2013 
2014  for (i = 0; i < MAX_ELEM_ID; i++) {
2015  ChannelElement *cce = ac->che[TYPE_CCE][i];
2016  int index = 0;
2017 
2018  if (cce && cce->coup.coupling_point == coupling_point) {
2019  ChannelCoupling *coup = &cce->coup;
2020 
2021  for (c = 0; c <= coup->num_coupled; c++) {
2022  if (coup->type[c] == type && coup->id_select[c] == elem_id) {
2023  if (coup->ch_select[c] != 1) {
2024  apply_coupling_method(ac, &cc->ch[0], cce, index);
2025  if (coup->ch_select[c] != 0)
2026  index++;
2027  }
2028  if (coup->ch_select[c] != 2)
2029  apply_coupling_method(ac, &cc->ch[1], cce, index++);
2030  } else
2031  index += 1 + (coup->ch_select[c] == 3);
2032  }
2033  }
2034  }
2035 }
2036 
2037 /**
2038  * Convert spectral data to samples, applying all supported tools as appropriate.
2039  */
2041 {
2042  int i, type;
2044  switch (ac->oc[1].m4ac.object_type) {
2045  case AOT_ER_AAC_LD:
2047  break;
2048  case AOT_ER_AAC_ELD:
2050  break;
2051  default:
2052  if (ac->oc[1].m4ac.frame_length_short)
2054  else
2056  }
2057  for (type = 3; type >= 0; type--) {
2058  for (i = 0; i < MAX_ELEM_ID; i++) {
2059  ChannelElement *che = ac->che[type][i];
2060  if (che && che->present) {
2061  if (type <= TYPE_CPE)
2063  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2064  if (che->ch[0].ics.predictor_present) {
2065  if (che->ch[0].ics.ltp.present)
2066  ac->dsp.apply_ltp(ac, &che->ch[0]);
2067  if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
2068  ac->dsp.apply_ltp(ac, &che->ch[1]);
2069  }
2070  }
2071  if (che->ch[0].tns.present)
2072  ac->dsp.apply_tns(che->ch[0].coeffs,
2073  &che->ch[0].tns, &che->ch[0].ics, 1);
2074  if (che->ch[1].tns.present)
2075  ac->dsp.apply_tns(che->ch[1].coeffs,
2076  &che->ch[1].tns, &che->ch[1].ics, 1);
2077  if (type <= TYPE_CPE)
2079  if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2080  imdct_and_window(ac, &che->ch[0]);
2081  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2082  ac->dsp.update_ltp(ac, &che->ch[0]);
2083  if (type == TYPE_CPE) {
2084  imdct_and_window(ac, &che->ch[1]);
2085  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2086  ac->dsp.update_ltp(ac, &che->ch[1]);
2087  }
2088  if (ac->oc[1].m4ac.sbr > 0) {
2089  ac->proc.sbr_apply(ac, che, type,
2090  che->ch[0].output,
2091  che->ch[1].output);
2092  }
2093  }
2094  if (type <= TYPE_CCE)
2096  ac->dsp.clip_output(ac, che, type, samples);
2097  che->present = 0;
2098  } else if (che) {
2099  av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
2100  }
2101  }
2102  }
2103 }
2104 
2106 {
2107  int size;
2108  AACADTSHeaderInfo hdr_info;
2109  uint8_t layout_map[MAX_ELEM_ID*4][3];
2110  int layout_map_tags, ret;
2111 
2112  size = ff_adts_header_parse(gb, &hdr_info);
2113  if (size > 0) {
2114  if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
2115  // This is 2 for "VLB " audio in NSV files.
2116  // See samples/nsv/vlb_audio.
2118  "More than one AAC RDB per ADTS frame");
2119  ac->warned_num_aac_frames = 1;
2120  }
2122  if (hdr_info.chan_config) {
2123  ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2125  layout_map,
2126  &layout_map_tags,
2127  hdr_info.chan_config)) < 0)
2128  return ret;
2129  if ((ret = ff_aac_output_configure(ac, layout_map, layout_map_tags,
2130  FFMAX(ac->oc[1].status,
2131  OC_TRIAL_FRAME), 0)) < 0)
2132  return ret;
2133  } else {
2134  ac->oc[1].m4ac.chan_config = 0;
2135  /**
2136  * dual mono frames in Japanese DTV can have chan_config 0
2137  * WITHOUT specifying PCE.
2138  * thus, set dual mono as default.
2139  */
2140  if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
2141  layout_map_tags = 2;
2142  layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
2143  layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
2144  layout_map[0][1] = 0;
2145  layout_map[1][1] = 1;
2146  if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
2147  OC_TRIAL_FRAME, 0))
2148  return -7;
2149  }
2150  }
2151  ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2152  ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2153  ac->oc[1].m4ac.object_type = hdr_info.object_type;
2154  ac->oc[1].m4ac.frame_length_short = 0;
2155  if (ac->oc[0].status != OC_LOCKED ||
2156  ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2157  ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2158  ac->oc[1].m4ac.sbr = -1;
2159  ac->oc[1].m4ac.ps = -1;
2160  }
2161  if (!hdr_info.crc_absent)
2162  skip_bits(gb, 16);
2163  }
2164  return size;
2165 }
2166 
2168  int *got_frame_ptr, GetBitContext *gb)
2169 {
2170  AACDecContext *ac = avctx->priv_data;
2171  const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
2172  ChannelElement *che;
2173  int err, i;
2174  int samples = m4ac->frame_length_short ? 960 : 1024;
2175  int chan_config = m4ac->chan_config;
2176  int aot = m4ac->object_type;
2177 
2178  if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
2179  samples >>= 1;
2180 
2181  ac->frame = frame;
2182 
2183  if ((err = frame_configure_elements(avctx)) < 0)
2184  return err;
2185 
2186  // The AV_PROFILE_AAC_* defines are all object_type - 1
2187  // This may lead to an undefined profile being signaled
2188  ac->avctx->profile = aot - 1;
2189 
2190  ac->tags_mapped = 0;
2191 
2192  if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
2193  avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
2194  chan_config);
2195  return AVERROR_INVALIDDATA;
2196  }
2197  for (i = 0; i < ff_tags_per_config[chan_config]; i++) {
2198  const int elem_type = ff_aac_channel_layout_map[chan_config-1][i][0];
2199  const int elem_id = ff_aac_channel_layout_map[chan_config-1][i][1];
2200  if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {
2201  av_log(ac->avctx, AV_LOG_ERROR,
2202  "channel element %d.%d is not allocated\n",
2203  elem_type, elem_id);
2204  return AVERROR_INVALIDDATA;
2205  }
2206  che->present = 1;
2207  if (aot != AOT_ER_AAC_ELD)
2208  skip_bits(gb, 4);
2209  switch (elem_type) {
2210  case TYPE_SCE:
2211  err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2212  break;
2213  case TYPE_CPE:
2214  err = decode_cpe(ac, gb, che);
2215  break;
2216  case TYPE_LFE:
2217  err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2218  break;
2219  }
2220  if (err < 0)
2221  return err;
2222  }
2223 
2225 
2226  if (!ac->frame->data[0] && samples) {
2227  av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2228  return AVERROR_INVALIDDATA;
2229  }
2230 
2231  ac->frame->nb_samples = samples;
2232  ac->frame->sample_rate = avctx->sample_rate;
2233  ac->frame->flags |= AV_FRAME_FLAG_KEY;
2234  *got_frame_ptr = 1;
2235 
2236  skip_bits_long(gb, get_bits_left(gb));
2237  return 0;
2238 }
2239 
2241  GetBitContext *gb, int *got_frame_ptr)
2242 {
2243  int err;
2244  int is_dmono;
2245  int elem_id;
2246  enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
2247  uint8_t che_presence[4][MAX_ELEM_ID] = {{0}};
2248  ChannelElement *che = NULL, *che_prev = NULL;
2249  int samples = 0, multiplier, audio_found = 0, pce_found = 0, sce_count = 0;
2250  AVFrame *frame = ac->frame;
2251 
2252  int payload_alignment = get_bits_count(gb);
2253  // parse
2254  while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2255  elem_id = get_bits(gb, 4);
2256 
2257  if (avctx->debug & FF_DEBUG_STARTCODE)
2258  av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
2259 
2260  if (!avctx->ch_layout.nb_channels && elem_type != TYPE_PCE)
2261  return AVERROR_INVALIDDATA;
2262 
2263  if (elem_type < TYPE_DSE) {
2264  if (che_presence[elem_type][elem_id]) {
2265  int error = che_presence[elem_type][elem_id] > 1;
2266  av_log(ac->avctx, error ? AV_LOG_ERROR : AV_LOG_DEBUG, "channel element %d.%d duplicate\n",
2267  elem_type, elem_id);
2268  if (error)
2269  return AVERROR_INVALIDDATA;
2270  }
2271  che_presence[elem_type][elem_id]++;
2272 
2273  if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {
2274  av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2275  elem_type, elem_id);
2276  return AVERROR_INVALIDDATA;
2277  }
2278  samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;
2279  che->present = 1;
2280  }
2281 
2282  switch (elem_type) {
2283 
2284  case TYPE_SCE:
2285  err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2286  audio_found = 1;
2287  sce_count++;
2288  break;
2289 
2290  case TYPE_CPE:
2291  err = decode_cpe(ac, gb, che);
2292  audio_found = 1;
2293  break;
2294 
2295  case TYPE_CCE:
2296  err = ac->proc.decode_cce(ac, gb, che);
2297  break;
2298 
2299  case TYPE_LFE:
2300  err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2301  audio_found = 1;
2302  break;
2303 
2304  case TYPE_DSE:
2305  err = skip_data_stream_element(ac, gb);
2306  break;
2307 
2308  case TYPE_PCE: {
2309  uint8_t layout_map[MAX_ELEM_ID*4][3] = {{0}};
2310  int tags;
2311 
2312  int pushed = push_output_configuration(ac);
2313  if (pce_found && !pushed)
2314  return AVERROR_INVALIDDATA;
2315 
2316  tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,
2317  payload_alignment);
2318  if (tags < 0) {
2319  err = tags;
2320  break;
2321  }
2322  if (pce_found) {
2323  av_log(avctx, AV_LOG_ERROR,
2324  "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2326  } else {
2327  err = ff_aac_output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
2328  if (!err)
2329  ac->oc[1].m4ac.chan_config = 0;
2330  pce_found = 1;
2331  }
2332  break;
2333  }
2334 
2335  case TYPE_FIL:
2336  if (elem_id == 15)
2337  elem_id += get_bits(gb, 8) - 1;
2338  if (get_bits_left(gb) < 8 * elem_id) {
2339  av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
2340  return AVERROR_INVALIDDATA;
2341  }
2342  err = 0;
2343  while (elem_id > 0) {
2344  int ret = decode_extension_payload(ac, gb, elem_id, che_prev, che_prev_type);
2345  if (ret < 0) {
2346  err = ret;
2347  break;
2348  }
2349  elem_id -= ret;
2350  }
2351  break;
2352 
2353  default:
2354  err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
2355  break;
2356  }
2357 
2358  if (elem_type < TYPE_DSE) {
2359  che_prev = che;
2360  che_prev_type = elem_type;
2361  }
2362 
2363  if (err)
2364  return err;
2365 
2366  if (get_bits_left(gb) < 3) {
2367  av_log(avctx, AV_LOG_ERROR, overread_err);
2368  return AVERROR_INVALIDDATA;
2369  }
2370  }
2371 
2372  if (!avctx->ch_layout.nb_channels)
2373  return 0;
2374 
2375  multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
2376  samples <<= multiplier;
2377 
2379 
2380  if (ac->oc[1].status && audio_found) {
2381  avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
2382  avctx->frame_size = samples;
2383  ac->oc[1].status = OC_LOCKED;
2384  }
2385 
2386  if (!ac->frame->data[0] && samples) {
2387  av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2388  return AVERROR_INVALIDDATA;
2389  }
2390 
2391  if (samples) {
2392  ac->frame->nb_samples = samples;
2393  ac->frame->sample_rate = avctx->sample_rate;
2394  ac->frame->flags |= AV_FRAME_FLAG_KEY;
2395  *got_frame_ptr = 1;
2396  } else {
2397  av_frame_unref(ac->frame);
2398  *got_frame_ptr = 0;
2399  }
2400 
2401  /* for dual-mono audio (SCE + SCE) */
2402  is_dmono = ac->dmono_mode && sce_count == 2 &&
2405  if (is_dmono) {
2406  if (ac->dmono_mode == 1)
2407  frame->data[1] = frame->data[0];
2408  else if (ac->dmono_mode == 2)
2409  frame->data[0] = frame->data[1];
2410  }
2411 
2412  return 0;
2413 }
2414 
2416  int *got_frame_ptr, GetBitContext *gb,
2417  const AVPacket *avpkt)
2418 {
2419  int err;
2420  AACDecContext *ac = avctx->priv_data;
2421 
2422  ac->frame = frame;
2423  *got_frame_ptr = 0;
2424 
2425  // USAC can't be packed into ADTS due to field size limitations.
2426  if (show_bits(gb, 12) == 0xfff && ac->oc[1].m4ac.object_type != AOT_USAC) {
2427  if ((err = parse_adts_frame_header(ac, gb)) < 0) {
2428  av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2429  goto fail;
2430  }
2431  if (ac->oc[1].m4ac.sampling_index > 12) {
2432  av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2433  err = AVERROR_INVALIDDATA;
2434  goto fail;
2435  }
2436  }
2437 
2438  if ((err = frame_configure_elements(avctx)) < 0)
2439  goto fail;
2440 
2441  // The AV_PROFILE_AAC_* defines are all object_type - 1
2442  // This may lead to an undefined profile being signaled
2443  ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
2444 
2445  ac->tags_mapped = 0;
2446 
2447  if (ac->oc[1].m4ac.object_type == AOT_USAC) {
2448  if (ac->is_fixed) {
2450  "AAC USAC fixed-point decoding");
2451  return AVERROR_PATCHWELCOME;
2452  }
2453 #if CONFIG_AAC_DECODER
2454  err = ff_aac_usac_decode_frame(avctx, ac, gb, got_frame_ptr);
2455  if (err < 0)
2456  goto fail;
2457 #endif
2458  } else {
2459  err = decode_frame_ga(avctx, ac, gb, got_frame_ptr);
2460  if (err < 0)
2461  goto fail;
2462  }
2463 
2464  return err;
2465 
2466 fail:
2468  return err;
2469 }
2470 
2472  int *got_frame_ptr, AVPacket *avpkt)
2473 {
2474  AACDecContext *ac = avctx->priv_data;
2475  const uint8_t *buf = avpkt->data;
2476  int buf_size = avpkt->size;
2477  GetBitContext gb;
2478  int buf_consumed;
2479  int buf_offset;
2480  int err;
2481  size_t new_extradata_size;
2482  const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
2484  &new_extradata_size);
2485  size_t jp_dualmono_size;
2486  const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
2488  &jp_dualmono_size);
2489 
2490  if (new_extradata) {
2491  /* discard previous configuration */
2492  ac->oc[1].status = OC_NONE;
2493  err = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
2494  new_extradata,
2495  new_extradata_size * 8LL, 1);
2496  if (err < 0) {
2497  return err;
2498  }
2499  }
2500 
2501  ac->dmono_mode = 0;
2502  if (jp_dualmono && jp_dualmono_size > 0)
2503  ac->dmono_mode = 1 + *jp_dualmono;
2504  if (ac->force_dmono_mode >= 0)
2505  ac->dmono_mode = ac->force_dmono_mode;
2506 
2507  if (INT_MAX / 8 <= buf_size)
2508  return AVERROR_INVALIDDATA;
2509 
2510  if ((err = init_get_bits8(&gb, buf, buf_size)) < 0)
2511  return err;
2512 
2513  switch (ac->oc[1].m4ac.object_type) {
2514  case AOT_ER_AAC_LC:
2515  case AOT_ER_AAC_LTP:
2516  case AOT_ER_AAC_LD:
2517  case AOT_ER_AAC_ELD:
2518  err = aac_decode_er_frame(avctx, frame, got_frame_ptr, &gb);
2519  break;
2520  default:
2521  err = aac_decode_frame_int(avctx, frame, got_frame_ptr, &gb, avpkt);
2522  }
2523  if (err < 0)
2524  return err;
2525 
2526  buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2527  for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2528  if (buf[buf_offset])
2529  break;
2530 
2531  return buf_size > buf_offset ? buf_consumed : buf_size;
2532 }
2533 
2534 #if CONFIG_AAC_LATM_DECODER
2535 #include "aacdec_latm.h"
2536 #endif
2537 
2538 #define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2539 #define OFF(field) offsetof(AACDecContext, field)
2540 static const AVOption options[] = {
2541  /**
2542  * AVOptions for Japanese DTV specific extensions (ADTS only)
2543  */
2544  {"dual_mono_mode", "Select the channel to decode for dual mono",
2545  OFF(force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
2546  AACDEC_FLAGS, .unit = "dual_mono_mode"},
2547 
2548  {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2549  {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2550  {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2551  {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2552 
2553  { "channel_order", "Order in which the channels are to be exported",
2554  OFF(output_channel_order), AV_OPT_TYPE_INT,
2555  { .i64 = CHANNEL_ORDER_DEFAULT }, 0, 1, AACDEC_FLAGS, .unit = "channel_order" },
2556  { "default", "normal libavcodec channel order", 0, AV_OPT_TYPE_CONST,
2557  { .i64 = CHANNEL_ORDER_DEFAULT }, .flags = AACDEC_FLAGS, .unit = "channel_order" },
2558  { "coded", "order in which the channels are coded in the bitstream",
2559  0, AV_OPT_TYPE_CONST, { .i64 = CHANNEL_ORDER_CODED }, .flags = AACDEC_FLAGS, .unit = "channel_order" },
2560 
2561  {NULL},
2562 };
2563 
2564 static const AVClass decoder_class = {
2565  .class_name = "AAC decoder",
2566  .item_name = av_default_item_name,
2567  .option = options,
2568  .version = LIBAVUTIL_VERSION_INT,
2569 };
2570 
2571 #if CONFIG_AAC_DECODER
2572 const FFCodec ff_aac_decoder = {
2573  .p.name = "aac",
2574  CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
2575  .p.type = AVMEDIA_TYPE_AUDIO,
2576  .p.id = AV_CODEC_ID_AAC,
2577  .p.priv_class = &decoder_class,
2578  .priv_data_size = sizeof(AACDecContext),
2580  .close = decode_close,
2583  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
2584  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2586  .flush = flush,
2587  .p.profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
2588 };
2589 #endif
2590 
2591 #if CONFIG_AAC_FIXED_DECODER
2592 const FFCodec ff_aac_fixed_decoder = {
2593  .p.name = "aac_fixed",
2594  CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
2595  .p.type = AVMEDIA_TYPE_AUDIO,
2596  .p.id = AV_CODEC_ID_AAC,
2597  .p.priv_class = &decoder_class,
2598  .priv_data_size = sizeof(AACDecContext),
2600  .close = decode_close,
2603  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
2604  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2606  .p.profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
2607  .flush = flush,
2608 };
2609 #endif
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
ChannelCoupling::type
enum RawDataBlockType type[8]
Type of channel element to be coupled - SCE or CPE.
Definition: aacdec.h:199
CouplingPoint
CouplingPoint
The point during decoding at which channel coupling is applied.
Definition: aacdec.h:68
MAX_ELEM_ID
#define MAX_ELEM_ID
Definition: aac.h:34
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1059
AAC_CHANNEL_BACK
@ AAC_CHANNEL_BACK
Definition: aac.h:80
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
decode_close
static av_cold int decode_close(AVCodecContext *avctx)
Definition: aacdec.c:1132
decode_frame_ga
static int decode_frame_ga(AVCodecContext *avctx, AACDecContext *ac, GetBitContext *gb, int *got_frame_ptr)
Definition: aacdec.c:2240
AACDecProc::decode_spectrum_and_dequant
int(* decode_spectrum_and_dequant)(AACDecContext *ac, GetBitContext *gb, const Pulse *pulse, SingleChannelElement *sce)
Definition: aacdec.h:396
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:280
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
ff_aac_num_swb_120
#define ff_aac_num_swb_120
Definition: aactab.h:79
pop_output_configuration
static void pop_output_configuration(AACDecContext *ac)
Restore the previous output configuration if and only if the current configuration is unlocked.
Definition: aacdec.c:470
AACDecContext::mdct960_fn
av_tx_fn mdct960_fn
Definition: aacdec.h:499
ff_tns_max_bands_128
const uint8_t ff_tns_max_bands_128[]
Definition: aactab.c:1990
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
av_clip
#define av_clip
Definition: common.h:100
BETWEEN_TNS_AND_IMDCT
@ BETWEEN_TNS_AND_IMDCT
Definition: aacdec.h:70
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
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
opt.h
AACDecDSP::apply_intensity_stereo
void(* apply_intensity_stereo)(AACDecContext *ac, ChannelElement *cpe, int ms_present)
Definition: aacdec.h:418
AACUSACConfig
Definition: aacdec.h:351
assign_channels
static int assign_channels(struct elem_to_channel e2c_vec[MAX_ELEM_ID], uint8_t(*layout_map)[3], uint64_t *layout, int tags, int layer, int pos, int *current)
Definition: aacdec.c:292
TYPE_FIL
@ TYPE_FIL
Definition: aac.h:46
EXT_FILL
@ EXT_FILL
Definition: aac.h:51
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:395
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1032
AACDecContext::mdct1024_fn
av_tx_fn mdct1024_fn
Definition: aacdec.h:500
decode_scalefactors
static int decode_scalefactors(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb, unsigned int global_gain)
Decode scalefactors; reference: table 4.47.
Definition: aacdec.c:1504
AACDecContext::warned_he_aac_mono
int warned_he_aac_mono
Definition: aacdec.h:532
AACDecContext::mdct96
AVTXContext * mdct96
Definition: aacdec.h:483
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:56
ff_aac_usac_config_decode
int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx, GetBitContext *gb, OutputConfiguration *oc, int channel_config)
Definition: aacdec_usac.c:335
AVCodecInternal::skip_samples
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:125
AACDecProc::sbr_ctx_alloc_init
int(* sbr_ctx_alloc_init)(AACDecContext *ac, ChannelElement **che, int id_aac)
Definition: aacdec.h:403
AVCodecContext::err_recognition
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1406
Pulse::num_pulse
int num_pulse
Definition: aac.h:100
ff_ltp_coef
const float ff_ltp_coef[8]
Definition: aactab.c:110
int64_t
long long int64_t
Definition: coverity.c:34
decode_audio_specific_config
static int decode_audio_specific_config(AACDecContext *ac, AVCodecContext *avctx, OutputConfiguration *oc, const uint8_t *data, int64_t bit_size, int sync_extension)
Definition: aacdec.c:1106
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:254
AACDecContext::mdct768
AVTXContext * mdct768
Definition: aacdec.h:488
OC_TRIAL_PCE
@ OC_TRIAL_PCE
Output configuration under trial specified by an inband PCE.
Definition: aacdec.h:54
aacsbr.h
mode
Definition: swscale.c:56
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
aac_decode_frame_int
static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb, const AVPacket *avpkt)
Definition: aacdec.c:2415
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.c:1837
w
uint8_t w
Definition: llviddspenc.c:38
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:588
ff_aac_num_swb_960
const uint8_t ff_aac_num_swb_960[]
Definition: aactab.c:153
AVOption
AVOption.
Definition: opt.h:429
AACDecContext::mdct960
AVTXContext * mdct960
Definition: aacdec.h:489
AOT_ER_AAC_LTP
@ AOT_ER_AAC_LTP
N Error Resilient Long Term Prediction.
Definition: mpeg4audio.h:90
TYPE_PCE
@ TYPE_PCE
Definition: aac.h:45
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
data
const char data[16]
Definition: mxf.c:149
aacdec_usac.h
FF_COMPLIANCE_STRICT
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: defs.h:59
TemporalNoiseShaping::present
int present
Definition: aacdec.h:185
FFCodec
Definition: codec_internal.h:127
parse_adts_frame_header
static int parse_adts_frame_header(AACDecContext *ac, GetBitContext *gb)
Definition: aacdec.c:2105
ff_aac_profiles
const AVProfile ff_aac_profiles[]
Definition: profiles.c:27
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
AACDecContext::tag_che_map
ChannelElement * tag_che_map[4][MAX_ELEM_ID]
Definition: aacdec.h:465
LongTermPrediction::used
int8_t used[MAX_LTP_LONG_SFB]
Definition: aacdec.h:121
AACDecDSP::apply_tns
void(* apply_tns)(void *_coef_param, TemporalNoiseShaping *tns, IndividualChannelStream *ics, int decode)
Definition: aacdec.h:421
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:671
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:324
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:165
AACDecContext::warned_remapping_once
int warned_remapping_once
Definition: aacdec.h:467
AACDecContext::proc
AACDecProc proc
Definition: aacdec.h:453
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
AACDecContext::mdct512_fn
av_tx_fn mdct512_fn
Definition: aacdec.h:497
AACDecDSP::apply_prediction
void(* apply_prediction)(AACDecContext *ac, SingleChannelElement *sce)
Definition: aacdec.h:427
ChannelElement::ch
SingleChannelElement ch[2]
Definition: aacdec.h:266
ff_aac_sample_rate_idx
static int ff_aac_sample_rate_idx(int rate)
Definition: aac.h:106
EXT_DYNAMIC_RANGE
@ EXT_DYNAMIC_RANGE
Definition: aac.h:54
ff_swb_offset_128
const uint16_t *const ff_swb_offset_128[]
Definition: aactab.c:1940
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:517
av_tx_init
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition: tx.c:903
ff_aac_decode_ics
int ff_aac_decode_ics(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb, int common_window, int scale_flag)
Decode an individual_channel_stream payload; reference: table 4.44.
Definition: aacdec.c:1697
ChannelElement::present
int present
Definition: aacdec.h:261
FF_DEBUG_PICT_INFO
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1383
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:448
ff_tns_max_bands_1024
const uint8_t ff_tns_max_bands_1024[]
Definition: aactab.c:1974
ff_aac_decode_init_float
int ff_aac_decode_init_float(AVCodecContext *avctx)
Definition: aacdec_float.c:164
AACDecContext::dmono_mode
int dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition: aacdec.h:522
MPEG4AudioConfig
Definition: mpeg4audio.h:29
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:383
DynamicRangeControl
Dynamic Range Control - decoded from the bitstream but not processed further.
Definition: aacdec.h:379
IndividualChannelStream::num_swb
int num_swb
number of scalefactor window bands
Definition: aacdec.h:171
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:136
options
static const AVOption options[]
Definition: aacdec.c:2540
ff_aac_decode_init_fixed
int ff_aac_decode_init_fixed(AVCodecContext *avctx)
Dequantization-related.
Definition: aacdec_fixed.c:87
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
ChannelCoupling::coupling_point
enum CouplingPoint coupling_point
The point during decoding at which coupling is applied.
Definition: aacdec.h:197
SingleChannelElement::coeffs
float coeffs[1024]
coefficients for IMDCT, maybe processed
Definition: aacenc.h:119
ff_aac_num_swb_512
const uint8_t ff_aac_num_swb_512[]
Definition: aactab.c:161
AACDecContext::force_dmono_mode
int force_dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition: aacdec.h:521
AACDecContext::warned_960_sbr
int warned_960_sbr
Definition: aacdec.h:529
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AACDecContext::mdct480
AVTXContext * mdct480
Definition: aacdec.h:486
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1047
macros.h
fail
#define fail()
Definition: checkasm.h:210
ChannelElement::coup
ChannelCoupling coup
Definition: aacdec.h:268
ChannelCoupling::id_select
int id_select[8]
element id
Definition: aacdec.h:200
SingleChannelElement::ret_buf
float ret_buf[2048]
PCM output buffer.
Definition: aacenc.h:120
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
AACDecContext::warned_71_wide
unsigned warned_71_wide
Definition: aacdec.h:530
TYPE_CPE
@ TYPE_CPE
Definition: aac.h:41
GetBitContext
Definition: get_bits.h:109
AV_EF_BITSTREAM
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition: defs.h:49
AACDecContext::tags_mapped
int tags_mapped
Definition: aacdec.h:466
Pulse::amp
int amp[4]
Definition: aac.h:103
Pulse::pos
int pos[4]
Definition: aac.h:102
AACDecProc::sbr_apply
void(* sbr_apply)(AACDecContext *ac, ChannelElement *che, int id_aac, void *L, void *R)
Definition: aacdec.h:406
OutputConfiguration::status
enum OCStatus status
Definition: aacdec.h:372
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
IndividualChannelStream::prediction_used
uint8_t prediction_used[41]
Definition: aacdec.h:177
AACDecContext::che_drc
DynamicRangeControl che_drc
Definition: aacdec.h:458
MAX_LTP_LONG_SFB
#define MAX_LTP_LONG_SFB
Definition: aac.h:37
refstruct.h
SingleChannelElement::ics
IndividualChannelStream ics
Definition: aacdec.h:211
AACDecContext::mdct480_fn
av_tx_fn mdct480_fn
Definition: aacdec.h:496
decode_cpe
static int decode_cpe(AACDecContext *ac, GetBitContext *gb, ChannelElement *cpe)
Decode a channel_pair_element; reference: table 4.4.
Definition: aacdec.c:1791
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.c:1563
AACUsacElemConfig
Definition: aacdec.h:297
AOT_ER_AAC_LC
@ AOT_ER_AAC_LC
N Error Resilient Low Complexity.
Definition: mpeg4audio.h:88
AACADTSHeaderInfo::chan_config
uint8_t chan_config
Definition: adts_header.h:42
decode_fill
static int decode_fill(AACDecContext *ac, GetBitContext *gb, int len)
Definition: aacdec.c:1903
AACUsacElemConfig::ext
struct AACUsacElemConfig::@26 ext
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
ZERO_BT
@ ZERO_BT
Scalefactors and spectral data are all zero.
Definition: aac.h:67
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
AACDecDSP::dequant_scalefactors
void(* dequant_scalefactors)(SingleChannelElement *sce)
Definition: aacdec.h:415
av_cold
#define av_cold
Definition: attributes.h:106
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
DynamicRangeControl::exclude_mask
int exclude_mask[MAX_CHANNELS]
Channels to be excluded from DRC processing.
Definition: aacdec.h:383
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:642
AV_CH_LAYOUT_22POINT2
#define AV_CH_LAYOUT_22POINT2
Definition: channel_layout.h:256
ff_aac_decode_init
av_cold int ff_aac_decode_init(AVCodecContext *avctx)
Definition: aacdec.c:1210
OC_GLOBAL_HDR
@ OC_GLOBAL_HDR
Output configuration set in a global header but not yet locked.
Definition: aacdec.h:56
AACDecContext::mdct_ltp
AVTXContext * mdct_ltp
Definition: aacdec.h:491
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:523
NOISE_BT
@ NOISE_BT
Spectral data are scaled white noise not coded in the bitstream.
Definition: aac.h:71
AV_TX_FLOAT_MDCT
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
Definition: tx.h:68
AOT_ER_AAC_LD
@ AOT_ER_AAC_LD
N Error Resilient Low Delay.
Definition: mpeg4audio.h:94
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:347
AACDecDSP::apply_mid_side_stereo
void(* apply_mid_side_stereo)(AACDecContext *ac, ChannelElement *cpe)
Definition: aacdec.h:417
ff_swb_offset_960
const uint16_t *const ff_swb_offset_960[]
Definition: aactab.c:1908
ChannelCoupling::num_coupled
int num_coupled
number of target elements
Definition: aacdec.h:198
AV_TX_INT32_MDCT
@ AV_TX_INT32_MDCT
Definition: tx.h:70
g
const char * g
Definition: vf_curves.c:128
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
EIGHT_SHORT_SEQUENCE
@ EIGHT_SHORT_SEQUENCE
Definition: aac.h:62
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:119
TemporalNoiseShaping::direction
int direction[8][4]
Definition: aacdec.h:188
av_channel_layout_from_mask
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:252
INTENSITY_BT2
@ INTENSITY_BT2
Scalefactor data are intensity stereo positions (out of phase).
Definition: aac.h:72
bits
uint8_t bits
Definition: vp3data.h:128
AACDecProc::decode_cce
int(* decode_cce)(AACDecContext *ac, GetBitContext *gb, ChannelElement *che)
Definition: aacdec.h:401
TYPE_DSE
@ TYPE_DSE
Definition: aac.h:44
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
elem_to_channel::av_position
uint64_t av_position
Definition: aacdec.c:216
ff_aac_get_che
ChannelElement * ff_aac_get_che(AACDecContext *ac, int type, int elem_id)
Definition: aacdec.c:623
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
flush
static av_cold void flush(AVCodecContext *avctx)
Definition: aacdec.c:556
ChannelPosition
ChannelPosition
Definition: aac.h:76
AACDecDSP::imdct_and_windowing_ld
void(* imdct_and_windowing_ld)(AACDecContext *ac, SingleChannelElement *sce)
Definition: aacdec.h:439
channels
channels
Definition: aptx.h:31
decode.h
limits.h
LongTermPrediction::present
int8_t present
Definition: aacdec.h:118
IndividualChannelStream
Individual Channel Stream.
Definition: aacdec.h:162
AACDecContext::che
ChannelElement * che[4][MAX_ELEM_ID]
Definition: aacdec.h:464
SCALE_DIFF_ZERO
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
Definition: aac.h:91
NOISE_PRE
#define NOISE_PRE
preamble for NOISE_BT, put in bitstream with the first noise band
Definition: aac.h:95
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
AACDecContext::fdsp
AVFloatDSPContext * fdsp
Definition: aacdec.h:504
ff_aac_usac_decode_frame
int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac, GetBitContext *gb, int *got_frame_ptr)
Definition: aacdec_usac.c:1663
AACDecContext::warned_num_aac_frames
int warned_num_aac_frames
Definition: aacdec.h:528
AACADTSHeaderInfo::num_aac_frames
uint8_t num_aac_frames
Definition: adts_header.h:43
INTENSITY_BT
@ INTENSITY_BT
Scalefactor data are intensity stereo positions (in phase).
Definition: aac.h:73
elem_to_channel::syn_ele
uint8_t syn_ele
Definition: aacdec.c:217
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
decode_extension_payload
static int decode_extension_payload(AACDecContext *ac, GetBitContext *gb, int cnt, ChannelElement *che, enum RawDataBlockType elem_type)
Decode extension data (incomplete); reference: table 4.51.
Definition: aacdec.c:1936
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
AACDecContext::mdct96_fn
av_tx_fn mdct96_fn
Definition: aacdec.h:493
NULL
#define NULL
Definition: coverity.c:32
spectral_to_sample
static void spectral_to_sample(AACDecContext *ac, int samples)
Convert spectral data to samples, applying all supported tools as appropriate.
Definition: aacdec.c:2040
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AACDecProc::sbr_decode_extension
int(* sbr_decode_extension)(AACDecContext *ac, ChannelElement *che, GetBitContext *gb, int crc, int cnt, int id_aac)
Definition: aacdec.h:404
IndividualChannelStream::use_kb_window
uint8_t use_kb_window[2]
If set, use Kaiser-Bessel window, otherwise use a sine window.
Definition: aacdec.h:165
ff_aac_num_swb_128
const uint8_t ff_aac_num_swb_128[]
Definition: aactab.c:169
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:474
IndividualChannelStream::num_window_groups
int num_window_groups
Definition: aacdec.h:166
AAC_CHANNEL_SIDE
@ AAC_CHANNEL_SIDE
Definition: aac.h:79
BEFORE_TNS
@ BEFORE_TNS
Definition: aacdec.h:69
AACADTSHeaderInfo::sampling_index
uint8_t sampling_index
Definition: adts_header.h:41
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:241
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
ff_aac_ch_layout
const AVChannelLayout ff_aac_ch_layout[]
Definition: aacdec_tab.c:96
profiles.h
MPEG4AudioConfig::sampling_index
int sampling_index
Definition: mpeg4audio.h:31
ff_aac_fixed_decoder
const FFCodec ff_aac_fixed_decoder
AOT_USAC
@ AOT_USAC
Y Unified Speech and Audio Coding.
Definition: mpeg4audio.h:113
ChannelElement::ms_mask
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
Definition: aacdec.h:264
options
Definition: swscale.c:43
aac.h
aactab.h
IndividualChannelStream::predictor_present
int predictor_present
Definition: aacdec.h:174
DynamicRangeControl::band_top
int band_top[17]
Indicates the top of the i-th DRC band in units of 4 spectral lines.
Definition: aacdec.h:386
ff_swb_offset_480
const uint16_t *const ff_swb_offset_480[]
Definition: aactab.c:1932
AAC_CHANNEL_FRONT
@ AAC_CHANNEL_FRONT
Definition: aac.h:78
sniff_channel_order
static uint64_t sniff_channel_order(uint8_t(*layout_map)[3], int tags)
Definition: aacdec.c:370
aac_decode_er_frame
static int aac_decode_er_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb)
Definition: aacdec.c:2167
AV_CH_FRONT_CENTER
#define AV_CH_FRONT_CENTER
Definition: channel_layout.h:177
count_channels
static int count_channels(uint8_t(*layout)[3], int tags)
Definition: aacdec.c:118
AOT_AAC_MAIN
@ AOT_AAC_MAIN
Y Main.
Definition: mpeg4audio.h:73
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.c:1648
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:651
AAC_CHANNEL_OFF
@ AAC_CHANNEL_OFF
Definition: aac.h:77
AACDecContext::mdct120
AVTXContext * mdct120
Definition: aacdec.h:484
OC_LOCKED
@ OC_LOCKED
Output configuration locked in place.
Definition: aacdec.h:57
index
int index
Definition: gxfenc.c:90
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
IndividualChannelStream::prev_num_window_groups
int prev_num_window_groups
Previous frame's number of window groups.
Definition: aacdec.h:167
aac_decode_frame
static int aac_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: aacdec.c:2471
error.h
ff_tns_max_bands_512
const uint8_t ff_tns_max_bands_512[]
Definition: aactab.c:1982
OutputConfiguration::layout_map_tags
int layout_map_tags
Definition: aacdec.h:370
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:91
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:461
OutputConfiguration::layout_map
uint8_t layout_map[MAX_ELEM_ID *4][3]
Definition: aacdec.h:369
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
AACDecDSP::update_ltp
void(* update_ltp)(AACDecContext *ac, SingleChannelElement *sce)
Definition: aacdec.h:425
AACDecDSP::apply_independent_coupling
void(* apply_independent_coupling)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Definition: aacdec.h:432
frame_configure_elements
static int frame_configure_elements(AVCodecContext *avctx)
Definition: aacdec.c:181
ff_aac_pred_sfb_max
const uint8_t ff_aac_pred_sfb_max[]
Definition: aactab.c:177
IndividualChannelStream::window_sequence
enum WindowSequence window_sequence[2]
Definition: aacdec.h:164
AACDecContext::dsp
AACDecDSP dsp
Definition: aacdec.h:452
AACDecDSP::clip_output
void(* clip_output)(AACDecContext *ac, ChannelElement *che, int type, int samples)
Definition: aacdec.h:442
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1729
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:550
AOT_ER_AAC_SCALABLE
@ AOT_ER_AAC_SCALABLE
N Error Resilient Scalable.
Definition: mpeg4audio.h:91
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
OC_NONE
@ OC_NONE
Output unconfigured.
Definition: aacdec.h:53
AACDecDSP::apply_dependent_coupling
void(* apply_dependent_coupling)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Definition: aacdec.h:429
ff_swb_offset_1024
const uint16_t *const ff_swb_offset_1024[]
Definition: aactab.c:1900
AOT_AAC_SCALABLE
@ AOT_AAC_SCALABLE
N Scalable.
Definition: mpeg4audio.h:78
AVPacket::size
int size
Definition: packet.h:589
skip_data_stream_element
static int skip_data_stream_element(AACDecContext *ac, GetBitContext *gb)
Skip data_stream_element; reference: table 4.10.
Definition: aacdec.c:1273
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
codec_internal.h
ONLY_LONG_SEQUENCE
@ ONLY_LONG_SEQUENCE
Definition: aac.h:60
TYPE_END
@ TYPE_END
Definition: aac.h:47
AACDecContext::mdct1024
AVTXContext * mdct1024
Definition: aacdec.h:490
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
AVTXType
AVTXType
Definition: tx.h:39
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:590
AACDecDSP::imdct_and_windowing
void(* imdct_and_windowing)(AACDecContext *ac, SingleChannelElement *sce)
Definition: aacdec.h:436
ChannelElement::max_sfb_ste
uint8_t max_sfb_ste
(USAC) Maximum of both max_sfb values
Definition: aacdec.h:263
OCStatus
OCStatus
Output configuration status.
Definition: aacdec.h:52
size
int size
Definition: twinvq_data.h:10344
SingleChannelElement::sfo
int sfo[128]
scalefactor offsets
Definition: aacdec.h:215
ff_tags_per_config
const int8_t ff_tags_per_config[16]
Definition: aacdec_tab.c:38
DynamicRangeControl::prog_ref_level
int prog_ref_level
A reference level for the long-term program audio level for all channels combined.
Definition: aacdec.h:387
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.
AACDecContext::output_element
SingleChannelElement * output_element[MAX_CHANNELS]
Points to each SingleChannelElement.
Definition: aacdec.h:513
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
AACDecContext::output_channel_order
enum AACOutputChannelOrder output_channel_order
Definition: aacdec.h:525
decode_dynamic_range
static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext *gb)
Decode dynamic range information; reference: table 4.52.
Definition: aacdec.c:1856
OutputConfiguration
Definition: aacdec.h:367
elem_to_channel::elem_id
uint8_t elem_id
Definition: aacdec.c:218
ff_tns_max_bands_480
const uint8_t ff_tns_max_bands_480[]
Definition: aactab.c:1986
elem_to_channel
Definition: aacdec.c:215
ff_swb_offset_512
const uint16_t *const ff_swb_offset_512[]
Definition: aactab.c:1924
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
attributes.h
decoder_class
static const AVClass decoder_class
Definition: aacdec.c:2564
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:416
AACADTSHeaderInfo::object_type
uint8_t object_type
Definition: adts_header.h:40
CODEC_SAMPLEFMTS
#define CODEC_SAMPLEFMTS(...)
Definition: codec_internal.h:386
SingleChannelElement::band_type
enum BandType band_type[128]
band types
Definition: aacdec.h:214
MAX_CHANNELS
#define MAX_CHANNELS
Definition: aac.h:33
AV_CHAN_UNUSED
@ AV_CHAN_UNUSED
Channel is empty can be safely skipped.
Definition: channel_layout.h:91
AACDecContext::mdct128
AVTXContext * mdct128
Definition: aacdec.h:485
DynamicRangeControl::dyn_rng_ctl
int dyn_rng_ctl[17]
DRC magnitude information.
Definition: aacdec.h:382
decode_ga_specific_config
static int decode_ga_specific_config(AACDecContext *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.c:881
AACDecDSP::apply_ltp
void(* apply_ltp)(AACDecContext *ac, SingleChannelElement *sce)
Definition: aacdec.h:424
SingleChannelElement::output
float * output
PCM output.
Definition: aacdec.h:227
MPEG4AudioConfig::channels
int channels
Definition: mpeg4audio.h:39
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:295
decode_eld_specific_config
static int decode_eld_specific_config(AACDecContext *ac, AVCodecContext *avctx, GetBitContext *gb, MPEG4AudioConfig *m4ac, int channel_config)
Definition: aacdec.c:962
av_channel_layout_compare
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
Definition: channel_layout.c:809
EXT_FILL_DATA
@ EXT_FILL_DATA
Definition: aac.h:52
decode_prediction
static int decode_prediction(AACDecContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
Definition: aacdec.c:1290
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
AOT_AAC_SSR
@ AOT_AAC_SSR
N (code in SoC repo) Scalable Sample Rate.
Definition: mpeg4audio.h:75
AACDecContext::mdct768_fn
av_tx_fn mdct768_fn
Definition: aacdec.h:498
MDCT_INIT
#define MDCT_INIT(s, fn, len, sval)
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
AACDecDSP::imdct_and_windowing_960
void(* imdct_and_windowing_960)(AACDecContext *ac, SingleChannelElement *sce)
Definition: aacdec.h:438
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
decode_audio_specific_config_gb
static int decode_audio_specific_config_gb(AACDecContext *ac, AVCodecContext *avctx, OutputConfiguration *oc, GetBitContext *gb, int get_bit_alignment, int sync_extension)
Decode audio specific configuration; reference: table 1.13.
Definition: aacdec.c:1031
ff_tns_tmp2_map
const float *const ff_tns_tmp2_map[4]
Definition: aactab.c:142
CHANNEL_ORDER_CODED
@ CHANNEL_ORDER_CODED
Definition: aacdec.h:62
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:507
RawDataBlockType
RawDataBlockType
Definition: aac.h:39
log.h
SingleChannelElement
Single Channel Element - used for both SCE and LFE elements.
Definition: aacdec.h:210
AACDecContext::is_fixed
int is_fixed
Definition: aacdec.h:534
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
IndividualChannelStream::num_windows
int num_windows
Definition: aacdec.h:172
OutputConfiguration::usac
AACUSACConfig usac
Definition: aacdec.h:373
AACDecContext::warned_gain_control
int warned_gain_control
Definition: aacdec.h:531
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:522
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:373
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: packet.c:252
ff_aac_channel_layout_map
const uint8_t ff_aac_channel_layout_map[16][16][3]
Definition: aacdec_tab.c:40
push_output_configuration
static int push_output_configuration(AACDecContext *ac)
Save current output configuration if and only if it has been locked.
Definition: aacdec.c:454
AACDecContext::random_state
int random_state
Definition: aacdec.h:506
relative_align_get_bits
static void relative_align_get_bits(GetBitContext *gb, int reference_position)
Definition: aacdec.c:799
AACDEC_FLAGS
#define AACDEC_FLAGS
Definition: aacdec.c:2538
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:488
ChannelElement
channel element - generic struct for SCE/CPE/CCE/LFE
Definition: aacdec.h:260
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: aacdec.h:170
AOT_ER_AAC_ELD
@ AOT_ER_AAC_ELD
N Error Resilient Enhanced Low Delay.
Definition: mpeg4audio.h:110
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.c:222
NOISE_PRE_BITS
#define NOISE_PRE_BITS
length of preamble
Definition: aac.h:96
FF_DEBUG_STARTCODE
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:1390
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_CH_FRONT_LEFT
#define AV_CH_FRONT_LEFT
Definition: channel_layout.h:175
TYPE_LFE
@ TYPE_LFE
Definition: aac.h:43
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:496
LongTermPrediction::lag
int16_t lag
Definition: aacdec.h:119
ff_aac_decoder
const FFCodec ff_aac_decoder
MPEG4AudioConfig::chan_config
int chan_config
Definition: mpeg4audio.h:33
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
TemporalNoiseShaping::order
int order[8][4]
Definition: aacdec.h:189
TYPE_SCE
@ TYPE_SCE
Definition: aac.h:40
decode_ics_info
static int decode_ics_info(AACDecContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
Decode Individual Channel Stream info; reference: table 4.6.
Definition: aacdec.c:1330
len
int len
Definition: vorbis_enc_data.h:426
filt
static const int8_t filt[NUMTAPS *2]
Definition: af_earwax.c:40
che_configure
static av_cold int che_configure(AACDecContext *ac, enum ChannelPosition che_pos, int type, int id, int *channels)
Check for the channel element in the current channel position configuration.
Definition: aacdec.c:142
AACDecContext::oc
OutputConfiguration oc[2]
Definition: aacdec.h:527
MPEG4AudioConfig::ext_sample_rate
int ext_sample_rate
Definition: mpeg4audio.h:37
IndividualChannelStream::tns_max_bands
int tns_max_bands
Definition: aacdec.h:173
TemporalNoiseShaping::length
int length[8][4]
Definition: aacdec.h:187
AACDecDSP::imdct_and_windowing_eld
void(* imdct_and_windowing_eld)(AACDecContext *ac, SingleChannelElement *sce)
Definition: aacdec.h:440
AACUSACConfig::nb_elems
int nb_elems
Definition: aacdec.h:357
AACADTSHeaderInfo::sample_rate
uint32_t sample_rate
Definition: adts_header.h:36
avcodec.h
ff_swb_offset_120
const uint16_t *const ff_swb_offset_120[]
Definition: aactab.c:1950
AAC_CHANNEL_LFE
@ AAC_CHANNEL_LFE
Definition: aac.h:81
version.h
AOT_ER_BSAC
@ AOT_ER_BSAC
N Error Resilient Bit-Sliced Arithmetic Coding.
Definition: mpeg4audio.h:93
DynamicRangeControl::pce_instance_tag
int pce_instance_tag
Indicates with which program the DRC info is associated.
Definition: aacdec.h:380
decode_ltp
static void decode_ltp(AACDecContext *ac, LongTermPrediction *ltp, GetBitContext *gb, uint8_t max_sfb)
Decode Long Term Prediction data; reference: table 4.xx.
Definition: aacdec.c:1312
ret
ret
Definition: filter_design.txt:187
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:163
elem_to_channel::aac_position
uint8_t aac_position
Definition: aacdec.c:219
ff_aac_num_swb_1024
const uint8_t ff_aac_num_swb_1024[]
Definition: aactab.c:149
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:81
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
AACDecContext::frame
struct AVFrame * frame
Definition: aacdec.h:455
AVCodecContext::strict_std_compliance
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1365
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:560
pos
unsigned int pos
Definition: spdifenc.c:414
count_paired_channels
static int count_paired_channels(uint8_t(*layout_map)[3], int tags, int pos, int current)
Definition: aacdec.c:260
TemporalNoiseShaping::coef
float coef[8][4][TNS_MAX_ORDER]
Definition: aacenc.h:101
CHANNEL_ORDER_DEFAULT
@ CHANNEL_ORDER_DEFAULT
Definition: aacdec.h:61
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: aacdec.h:201
id
enum AVCodecID id
Definition: dts2pts.c:549
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:30
SingleChannelElement::tns
TemporalNoiseShaping tns
Definition: aacdec.h:213
U
#define U(x)
Definition: vpx_arith.h:37
overread_err
#define overread_err
Definition: aacdec.c:116
aacdec.h
imdct_and_window
static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float *in, float *prev, int ch)
Definition: twinvq.c:329
AACDecContext
main AAC decoding context
Definition: aacdec.h:448
AACADTSHeaderInfo::crc_absent
uint8_t crc_absent
Definition: adts_header.h:39
AV_CHAN_NONE
@ AV_CHAN_NONE
Invalid channel index.
Definition: channel_layout.h:49
init_dsp
static av_cold int init_dsp(AVCodecContext *avctx)
Definition: aacdec.c:1175
EXT_SBR_DATA_CRC
@ EXT_SBR_DATA_CRC
Definition: aac.h:56
AVCodecContext
main external API structure.
Definition: avcodec.h:439
EXT_SBR_DATA
@ EXT_SBR_DATA
Definition: aac.h:55
LongTermPrediction
Long Term Prediction.
Definition: aacdec.h:117
AV_PROFILE_AAC_HE_V2
#define AV_PROFILE_AAC_HE_V2
Definition: defs.h:73
AACDecContext::avctx
struct AVCodecContext * avctx
Definition: aacdec.h:450
MPEG4AudioConfig::ps
int ps
-1 implicit, 1 presence
Definition: mpeg4audio.h:40
aacdec_latm.h
NOISE_OFFSET
#define NOISE_OFFSET
subtracted from global gain, used as offset for the preamble
Definition: aac.h:97
aacdec_tab.h
av_refstruct_replace
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:160
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1626
TemporalNoiseShaping
Temporal Noise Shaping.
Definition: aacdec.h:184
copy_oc
static void copy_oc(OutputConfiguration *dst, OutputConfiguration *src)
Definition: aacdec.c:431
ff_mpeg4audio_channels
const uint8_t ff_mpeg4audio_channels[15]
Definition: mpeg4audio.c:59
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:442
MPEG4AudioConfig::sbr
int sbr
-1 implicit, 1 presence
Definition: mpeg4audio.h:34
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
ff_aac_decode_tns
int ff_aac_decode_tns(AACDecContext *ac, TemporalNoiseShaping *tns, GetBitContext *gb, const IndividualChannelStream *ics)
Decode Temporal Noise Shaping data; reference: table 4.48.
Definition: aacdec.c:1590
Q31
#define Q31(x)
Definition: aac_defines.h:111
DynamicRangeControl::band_incr
int band_incr
Number of DRC bands greater than 1 having DRC info.
Definition: aacdec.h:384
AACDecContext::mdct_ltp_fn
av_tx_fn mdct_ltp_fn
Definition: aacdec.h:501
ff_aac_usac_reset_state
int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc)
Definition: aacdec_usac.c:276
decode_gain_control
static void decode_gain_control(SingleChannelElement *sce, GetBitContext *gb)
Definition: aacdec.c:1662
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1382
AV_CH_FRONT_RIGHT
#define AV_CH_FRONT_RIGHT
Definition: channel_layout.h:176
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:449
OutputConfiguration::m4ac
MPEG4AudioConfig m4ac
Definition: aacdec.h:368
TYPE_CCE
@ TYPE_CCE
Definition: aac.h:42
apply_channel_coupling
static void apply_channel_coupling(AACDecContext *ac, ChannelElement *cc, enum RawDataBlockType type, int elem_id, enum CouplingPoint coupling_point, void(*apply_coupling_method)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
channel coupling transformation interface
Definition: aacdec.c:2007
AACUSACConfig::elems
AACUsacElemConfig elems[MAX_ELEM_ID]
Definition: aacdec.h:356
mem.h
OutputConfiguration::ch_layout
AVChannelLayout ch_layout
Definition: aacdec.h:371
ff_aacdec_common_init_once
av_cold void ff_aacdec_common_init_once(void)
Definition: aacdec_tab.c:304
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
adts_header.h
MPEG4AudioConfig::frame_length_short
int frame_length_short
Definition: mpeg4audio.h:41
AV_PROFILE_AAC_HE
#define AV_PROFILE_AAC_HE
Definition: defs.h:72
ff_aac_channel_map
const int16_t ff_aac_channel_map[3][4][6]
Definition: aacdec_tab.c:75
DynamicRangeControl::dyn_rng_sgn
int dyn_rng_sgn[17]
DRC sign information; 0 - positive, 1 - negative.
Definition: aacdec.h:381
AVPacket
This structure stores compressed data.
Definition: packet.h:565
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:466
ff_vlc_scalefactors
VLCElem ff_vlc_scalefactors[352]
Definition: aacdec_tab.c:111
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ChannelCoupling
coupling parameters
Definition: aacdec.h:196
EXT_DATA_ELEMENT
@ EXT_DATA_ELEMENT
Definition: aac.h:53
aac_defines.h
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: aacdec.h:163
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.c:769
Pulse
Definition: aac.h:99
AAC_CHANNEL_CC
@ AAC_CHANNEL_CC
Definition: aac.h:82
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
OFF
#define OFF(field)
Definition: aacdec.c:2539
AACDecContext::mdct512
AVTXContext * mdct512
Definition: aacdec.h:487
DynamicRangeControl::interpolation_scheme
int interpolation_scheme
Indicates the interpolation scheme used in the SBR QMF domain.
Definition: aacdec.h:385
AFTER_IMDCT
@ AFTER_IMDCT
Definition: aacdec.h:71
ff_aac_set_default_channel_config
int ff_aac_set_default_channel_config(AACDecContext *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.c:583
IndividualChannelStream::ltp
LongTermPrediction ltp
Definition: aacdec.h:169
IndividualChannelStream::group_len
uint8_t group_len[8]
Definition: aacdec.h:168
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
AACUsacElemConfig::pl_buf
uint8_t * pl_buf
Definition: aacdec.h:347
decode_band_types
static int decode_band_types(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb)
Decode band types (section_data payload); reference: table 4.46.
Definition: aacdec.c:1457
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.c:811
AOT_AAC_LC
@ AOT_AAC_LC
Y Low Complexity.
Definition: mpeg4audio.h:74
TemporalNoiseShaping::n_filt
int n_filt[8]
Definition: aacdec.h:186
AOT_AAC_LTP
@ AOT_AAC_LTP
Y Long Term Prediction.
Definition: mpeg4audio.h:76
CODEC_CH_LAYOUTS_ARRAY
#define CODEC_CH_LAYOUTS_ARRAY(array)
Definition: codec_internal.h:381
OC_TRIAL_FRAME
@ OC_TRIAL_FRAME
Output configuration under trial specified by a frame header.
Definition: aacdec.h:55
src
#define src
Definition: vp8dsp.c:248
Q30
#define Q30(x)
Definition: aac_defines.h:110
ff_aac_output_configure
int ff_aac_output_configure(AACDecContext *ac, uint8_t layout_map[MAX_ELEM_ID *4][3], int tags, enum OCStatus oc_type, int get_new_frame)
Configure output channel order based on the current program configuration element.
Definition: aacdec.c:487
AACDecProc::sbr_ctx_close
void(* sbr_ctx_close)(ChannelElement *che)
Definition: aacdec.h:408
AACADTSHeaderInfo
Definition: adts_header.h:35
IndividualChannelStream::predictor_reset_group
int predictor_reset_group
Definition: aacdec.h:176
tx.h
AACDecContext::mdct120_fn
av_tx_fn mdct120_fn
Definition: aacdec.h:494
MPEG4AudioConfig::sample_rate
int sample_rate
Definition: mpeg4audio.h:32
AACDecContext::mdct128_fn
av_tx_fn mdct128_fn
Definition: aacdec.h:495