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