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