libavcodec/aacdec.c
Go to the documentation of this file.
1 /*
2  * AAC decoder
3  * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4  * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
5  *
6  * AAC LATM decoder
7  * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
8  * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
9  *
10  * This file is part of FFmpeg.
11  *
12  * FFmpeg is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * FFmpeg is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with FFmpeg; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 
27 /**
28  * @file
29  * AAC decoder
30  * @author Oded Shimon ( ods15 ods15 dyndns org )
31  * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
32  */
33 
34 /*
35  * supported tools
36  *
37  * Support? Name
38  * N (code in SoC repo) gain control
39  * Y block switching
40  * Y window shapes - standard
41  * N window shapes - Low Delay
42  * Y filterbank - standard
43  * N (code in SoC repo) filterbank - Scalable Sample Rate
44  * Y Temporal Noise Shaping
45  * Y Long Term Prediction
46  * Y intensity stereo
47  * Y channel coupling
48  * Y frequency domain prediction
49  * Y Perceptual Noise Substitution
50  * Y Mid/Side stereo
51  * N Scalable Inverse AAC Quantization
52  * N Frequency Selective Switch
53  * N upsampling filter
54  * Y quantization & coding - AAC
55  * N quantization & coding - TwinVQ
56  * N quantization & coding - BSAC
57  * N AAC Error Resilience tools
58  * N Error Resilience payload syntax
59  * N Error Protection tool
60  * N CELP
61  * N Silence Compression
62  * N HVXC
63  * N HVXC 4kbits/s VR
64  * N Structured Audio tools
65  * N Structured Audio Sample Bank Format
66  * N MIDI
67  * N Harmonic and Individual Lines plus Noise
68  * N Text-To-Speech Interface
69  * Y Spectral Band Replication
70  * Y (not in this code) Layer-1
71  * Y (not in this code) Layer-2
72  * Y (not in this code) Layer-3
73  * N SinuSoidal Coding (Transient, Sinusoid, Noise)
74  * Y Parametric Stereo
75  * N Direct Stream Transfer
76  *
77  * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
78  * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
79  Parametric Stereo.
80  */
81 
82 #include "libavutil/float_dsp.h"
83 #include "libavutil/opt.h"
84 #include "avcodec.h"
85 #include "internal.h"
86 #include "get_bits.h"
87 #include "fft.h"
88 #include "fmtconvert.h"
89 #include "lpc.h"
90 #include "kbdwin.h"
91 #include "sinewin.h"
92 
93 #include "aac.h"
94 #include "aactab.h"
95 #include "aacdectab.h"
96 #include "cbrt_tablegen.h"
97 #include "sbr.h"
98 #include "aacsbr.h"
99 #include "mpeg4audio.h"
100 #include "aacadtsdec.h"
101 #include "libavutil/intfloat.h"
102 
103 #include <assert.h>
104 #include <errno.h>
105 #include <math.h>
106 #include <string.h>
107 
108 #if ARCH_ARM
109 # include "arm/aac.h"
110 #elif ARCH_MIPS
111 # include "mips/aacdec_mips.h"
112 #endif
113 
115 static VLC vlc_spectral[11];
116 
117 static int output_configure(AACContext *ac,
118  uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
119  enum OCStatus oc_type, int get_new_frame);
120 
121 #define overread_err "Input buffer exhausted before END element found\n"
122 
123 static int count_channels(uint8_t (*layout)[3], int tags)
124 {
125  int i, sum = 0;
126  for (i = 0; i < tags; i++) {
127  int syn_ele = layout[i][0];
128  int pos = layout[i][2];
129  sum += (1 + (syn_ele == TYPE_CPE)) *
130  (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
131  }
132  return sum;
133 }
134 
135 /**
136  * Check for the channel element in the current channel position configuration.
137  * If it exists, make sure the appropriate element is allocated and map the
138  * channel order to match the internal FFmpeg channel layout.
139  *
140  * @param che_pos current channel position configuration
141  * @param type channel element type
142  * @param id channel element id
143  * @param channels count of the number of channels in the configuration
144  *
145  * @return Returns error status. 0 - OK, !0 - error
146  */
148  enum ChannelPosition che_pos,
149  int type, int id, int *channels)
150 {
151  if (che_pos) {
152  if (!ac->che[type][id]) {
153  if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
154  return AVERROR(ENOMEM);
155  ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr);
156  }
157  if (type != TYPE_CCE) {
158  if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
159  av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
160  return AVERROR_INVALIDDATA;
161  }
162  ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
163  if (type == TYPE_CPE ||
164  (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
165  ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
166  }
167  }
168  } else {
169  if (ac->che[type][id])
170  ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
171  av_freep(&ac->che[type][id]);
172  }
173  return 0;
174 }
175 
177 {
178  AACContext *ac = avctx->priv_data;
179  int type, id, ch, ret;
180 
181  /* set channel pointers to internal buffers by default */
182  for (type = 0; type < 4; type++) {
183  for (id = 0; id < MAX_ELEM_ID; id++) {
184  ChannelElement *che = ac->che[type][id];
185  if (che) {
186  che->ch[0].ret = che->ch[0].ret_buf;
187  che->ch[1].ret = che->ch[1].ret_buf;
188  }
189  }
190  }
191 
192  /* get output buffer */
193  av_frame_unref(ac->frame);
194  ac->frame->nb_samples = 2048;
195  if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
196  return ret;
197 
198  /* map output channel pointers to AVFrame data */
199  for (ch = 0; ch < avctx->channels; ch++) {
200  if (ac->output_element[ch])
201  ac->output_element[ch]->ret = (float *)ac->frame->extended_data[ch];
202  }
203 
204  return 0;
205 }
206 
208  uint64_t av_position;
212 };
213 
214 static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
215  uint8_t (*layout_map)[3], int offset, uint64_t left,
216  uint64_t right, int pos)
217 {
218  if (layout_map[offset][0] == TYPE_CPE) {
219  e2c_vec[offset] = (struct elem_to_channel) {
220  .av_position = left | right, .syn_ele = TYPE_CPE,
221  .elem_id = layout_map[offset ][1], .aac_position = pos };
222  return 1;
223  } else {
224  e2c_vec[offset] = (struct elem_to_channel) {
225  .av_position = left, .syn_ele = TYPE_SCE,
226  .elem_id = layout_map[offset ][1], .aac_position = pos };
227  e2c_vec[offset + 1] = (struct elem_to_channel) {
228  .av_position = right, .syn_ele = TYPE_SCE,
229  .elem_id = layout_map[offset + 1][1], .aac_position = pos };
230  return 2;
231  }
232 }
233 
234 static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos, int *current) {
235  int num_pos_channels = 0;
236  int first_cpe = 0;
237  int sce_parity = 0;
238  int i;
239  for (i = *current; i < tags; i++) {
240  if (layout_map[i][2] != pos)
241  break;
242  if (layout_map[i][0] == TYPE_CPE) {
243  if (sce_parity) {
244  if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
245  sce_parity = 0;
246  } else {
247  return -1;
248  }
249  }
250  num_pos_channels += 2;
251  first_cpe = 1;
252  } else {
253  num_pos_channels++;
254  sce_parity ^= 1;
255  }
256  }
257  if (sce_parity &&
258  ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
259  return -1;
260  *current = i;
261  return num_pos_channels;
262 }
263 
264 static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
265 {
266  int i, n, total_non_cc_elements;
267  struct elem_to_channel e2c_vec[4*MAX_ELEM_ID] = {{ 0 }};
268  int num_front_channels, num_side_channels, num_back_channels;
269  uint64_t layout;
270 
271  if (FF_ARRAY_ELEMS(e2c_vec) < tags)
272  return 0;
273 
274  i = 0;
275  num_front_channels =
276  count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
277  if (num_front_channels < 0)
278  return 0;
279  num_side_channels =
280  count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
281  if (num_side_channels < 0)
282  return 0;
283  num_back_channels =
284  count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
285  if (num_back_channels < 0)
286  return 0;
287 
288  i = 0;
289  if (num_front_channels & 1) {
290  e2c_vec[i] = (struct elem_to_channel) {
291  .av_position = AV_CH_FRONT_CENTER, .syn_ele = TYPE_SCE,
292  .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_FRONT };
293  i++;
294  num_front_channels--;
295  }
296  if (num_front_channels >= 4) {
297  i += assign_pair(e2c_vec, layout_map, i,
301  num_front_channels -= 2;
302  }
303  if (num_front_channels >= 2) {
304  i += assign_pair(e2c_vec, layout_map, i,
308  num_front_channels -= 2;
309  }
310  while (num_front_channels >= 2) {
311  i += assign_pair(e2c_vec, layout_map, i,
312  UINT64_MAX,
313  UINT64_MAX,
315  num_front_channels -= 2;
316  }
317 
318  if (num_side_channels >= 2) {
319  i += assign_pair(e2c_vec, layout_map, i,
323  num_side_channels -= 2;
324  }
325  while (num_side_channels >= 2) {
326  i += assign_pair(e2c_vec, layout_map, i,
327  UINT64_MAX,
328  UINT64_MAX,
330  num_side_channels -= 2;
331  }
332 
333  while (num_back_channels >= 4) {
334  i += assign_pair(e2c_vec, layout_map, i,
335  UINT64_MAX,
336  UINT64_MAX,
338  num_back_channels -= 2;
339  }
340  if (num_back_channels >= 2) {
341  i += assign_pair(e2c_vec, layout_map, i,
345  num_back_channels -= 2;
346  }
347  if (num_back_channels) {
348  e2c_vec[i] = (struct elem_to_channel) {
349  .av_position = AV_CH_BACK_CENTER, .syn_ele = TYPE_SCE,
350  .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_BACK };
351  i++;
352  num_back_channels--;
353  }
354 
355  if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
356  e2c_vec[i] = (struct elem_to_channel) {
358  .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_LFE };
359  i++;
360  }
361  while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
362  e2c_vec[i] = (struct elem_to_channel) {
363  .av_position = UINT64_MAX, .syn_ele = TYPE_LFE,
364  .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_LFE };
365  i++;
366  }
367 
368  // Must choose a stable sort
369  total_non_cc_elements = n = i;
370  do {
371  int next_n = 0;
372  for (i = 1; i < n; i++) {
373  if (e2c_vec[i-1].av_position > e2c_vec[i].av_position) {
374  FFSWAP(struct elem_to_channel, e2c_vec[i-1], e2c_vec[i]);
375  next_n = i;
376  }
377  }
378  n = next_n;
379  } while (n > 0);
380 
381  layout = 0;
382  for (i = 0; i < total_non_cc_elements; i++) {
383  layout_map[i][0] = e2c_vec[i].syn_ele;
384  layout_map[i][1] = e2c_vec[i].elem_id;
385  layout_map[i][2] = e2c_vec[i].aac_position;
386  if (e2c_vec[i].av_position != UINT64_MAX) {
387  layout |= e2c_vec[i].av_position;
388  }
389  }
390 
391  return layout;
392 }
393 
394 /**
395  * Save current output configuration if and only if it has been locked.
396  */
398  if (ac->oc[1].status == OC_LOCKED) {
399  ac->oc[0] = ac->oc[1];
400  }
401  ac->oc[1].status = OC_NONE;
402 }
403 
404 /**
405  * Restore the previous output configuration if and only if the current
406  * configuration is unlocked.
407  */
409  if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
410  ac->oc[1] = ac->oc[0];
411  ac->avctx->channels = ac->oc[1].channels;
412  ac->avctx->channel_layout = ac->oc[1].channel_layout;
413  output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
414  ac->oc[1].status, 0);
415  }
416 }
417 
418 /**
419  * Configure output channel order based on the current program configuration element.
420  *
421  * @return Returns error status. 0 - OK, !0 - error
422  */
424  uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
425  enum OCStatus oc_type, int get_new_frame)
426 {
427  AVCodecContext *avctx = ac->avctx;
428  int i, channels = 0, ret;
429  uint64_t layout = 0;
430 
431  if (ac->oc[1].layout_map != layout_map) {
432  memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
433  ac->oc[1].layout_map_tags = tags;
434  }
435 
436  // Try to sniff a reasonable channel order, otherwise output the
437  // channels in the order the PCE declared them.
439  layout = sniff_channel_order(layout_map, tags);
440  for (i = 0; i < tags; i++) {
441  int type = layout_map[i][0];
442  int id = layout_map[i][1];
443  int position = layout_map[i][2];
444  // Allocate or free elements depending on if they are in the
445  // current program configuration.
446  ret = che_configure(ac, position, type, id, &channels);
447  if (ret < 0)
448  return ret;
449  }
450  if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
451  if (layout == AV_CH_FRONT_CENTER) {
453  } else {
454  layout = 0;
455  }
456  }
457 
458  memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
459  if (layout) avctx->channel_layout = layout;
460  ac->oc[1].channel_layout = layout;
461  avctx->channels = ac->oc[1].channels = channels;
462  ac->oc[1].status = oc_type;
463 
464  if (get_new_frame) {
465  if ((ret = frame_configure_elements(ac->avctx)) < 0)
466  return ret;
467  }
468 
469  return 0;
470 }
471 
472 static void flush(AVCodecContext *avctx)
473 {
474  AACContext *ac= avctx->priv_data;
475  int type, i, j;
476 
477  for (type = 3; type >= 0; type--) {
478  for (i = 0; i < MAX_ELEM_ID; i++) {
479  ChannelElement *che = ac->che[type][i];
480  if (che) {
481  for (j = 0; j <= 1; j++) {
482  memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
483  }
484  }
485  }
486  }
487 }
488 
489 /**
490  * Set up channel positions based on a default channel configuration
491  * as specified in table 1.17.
492  *
493  * @return Returns error status. 0 - OK, !0 - error
494  */
496  uint8_t (*layout_map)[3],
497  int *tags,
498  int channel_config)
499 {
500  if (channel_config < 1 || channel_config > 7) {
501  av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
502  channel_config);
503  return -1;
504  }
505  *tags = tags_per_config[channel_config];
506  memcpy(layout_map, aac_channel_layout_map[channel_config-1], *tags * sizeof(*layout_map));
507  return 0;
508 }
509 
511 {
512  // For PCE based channel configurations map the channels solely based on tags.
513  if (!ac->oc[1].m4ac.chan_config) {
514  return ac->tag_che_map[type][elem_id];
515  }
516  // Allow single CPE stereo files to be signalled with mono configuration.
517  if (!ac->tags_mapped && type == TYPE_CPE && ac->oc[1].m4ac.chan_config == 1) {
518  uint8_t layout_map[MAX_ELEM_ID*4][3];
519  int layout_map_tags;
521 
522  av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
523 
524  if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
525  2) < 0)
526  return NULL;
527  if (output_configure(ac, layout_map, layout_map_tags,
528  OC_TRIAL_FRAME, 1) < 0)
529  return NULL;
530 
531  ac->oc[1].m4ac.chan_config = 2;
532  ac->oc[1].m4ac.ps = 0;
533  }
534  // And vice-versa
535  if (!ac->tags_mapped && type == TYPE_SCE && ac->oc[1].m4ac.chan_config == 2) {
536  uint8_t layout_map[MAX_ELEM_ID*4][3];
537  int layout_map_tags;
539 
540  av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
541 
542  if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
543  1) < 0)
544  return NULL;
545  if (output_configure(ac, layout_map, layout_map_tags,
546  OC_TRIAL_FRAME, 1) < 0)
547  return NULL;
548 
549  ac->oc[1].m4ac.chan_config = 1;
550  if (ac->oc[1].m4ac.sbr)
551  ac->oc[1].m4ac.ps = -1;
552  }
553  // For indexed channel configurations map the channels solely based on position.
554  switch (ac->oc[1].m4ac.chan_config) {
555  case 7:
556  if (ac->tags_mapped == 3 && type == TYPE_CPE) {
557  ac->tags_mapped++;
558  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
559  }
560  case 6:
561  /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
562  instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have
563  encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */
564  if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
565  ac->tags_mapped++;
566  return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
567  }
568  case 5:
569  if (ac->tags_mapped == 2 && type == TYPE_CPE) {
570  ac->tags_mapped++;
571  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
572  }
573  case 4:
574  if (ac->tags_mapped == 2 && ac->oc[1].m4ac.chan_config == 4 && type == TYPE_SCE) {
575  ac->tags_mapped++;
576  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
577  }
578  case 3:
579  case 2:
580  if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) && type == TYPE_CPE) {
581  ac->tags_mapped++;
582  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
583  } else if (ac->oc[1].m4ac.chan_config == 2) {
584  return NULL;
585  }
586  case 1:
587  if (!ac->tags_mapped && type == TYPE_SCE) {
588  ac->tags_mapped++;
589  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
590  }
591  default:
592  return NULL;
593  }
594 }
595 
596 /**
597  * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
598  *
599  * @param type speaker type/position for these channels
600  */
601 static void decode_channel_map(uint8_t layout_map[][3],
602  enum ChannelPosition type,
603  GetBitContext *gb, int n)
604 {
605  while (n--) {
607  switch (type) {
608  case AAC_CHANNEL_FRONT:
609  case AAC_CHANNEL_BACK:
610  case AAC_CHANNEL_SIDE:
611  syn_ele = get_bits1(gb);
612  break;
613  case AAC_CHANNEL_CC:
614  skip_bits1(gb);
615  syn_ele = TYPE_CCE;
616  break;
617  case AAC_CHANNEL_LFE:
618  syn_ele = TYPE_LFE;
619  break;
620  default:
621  av_assert0(0);
622  }
623  layout_map[0][0] = syn_ele;
624  layout_map[0][1] = get_bits(gb, 4);
625  layout_map[0][2] = type;
626  layout_map++;
627  }
628 }
629 
630 /**
631  * Decode program configuration element; reference: table 4.2.
632  *
633  * @return Returns error status. 0 - OK, !0 - error
634  */
635 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
636  uint8_t (*layout_map)[3],
637  GetBitContext *gb)
638 {
639  int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
640  int comment_len;
641  int tags;
642 
643  skip_bits(gb, 2); // object_type
644 
645  sampling_index = get_bits(gb, 4);
646  if (m4ac->sampling_index != sampling_index)
647  av_log(avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
648 
649  num_front = get_bits(gb, 4);
650  num_side = get_bits(gb, 4);
651  num_back = get_bits(gb, 4);
652  num_lfe = get_bits(gb, 2);
653  num_assoc_data = get_bits(gb, 3);
654  num_cc = get_bits(gb, 4);
655 
656  if (get_bits1(gb))
657  skip_bits(gb, 4); // mono_mixdown_tag
658  if (get_bits1(gb))
659  skip_bits(gb, 4); // stereo_mixdown_tag
660 
661  if (get_bits1(gb))
662  skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
663 
664  if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
665  av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
666  return -1;
667  }
668  decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
669  tags = num_front;
670  decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
671  tags += num_side;
672  decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
673  tags += num_back;
674  decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
675  tags += num_lfe;
676 
677  skip_bits_long(gb, 4 * num_assoc_data);
678 
679  decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
680  tags += num_cc;
681 
682  align_get_bits(gb);
683 
684  /* comment field, first byte is length */
685  comment_len = get_bits(gb, 8) * 8;
686  if (get_bits_left(gb) < comment_len) {
687  av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
688  return -1;
689  }
690  skip_bits_long(gb, comment_len);
691  return tags;
692 }
693 
694 /**
695  * Decode GA "General Audio" specific configuration; reference: table 4.1.
696  *
697  * @param ac pointer to AACContext, may be null
698  * @param avctx pointer to AVCCodecContext, used for logging
699  *
700  * @return Returns error status. 0 - OK, !0 - error
701  */
703  GetBitContext *gb,
704  MPEG4AudioConfig *m4ac,
705  int channel_config)
706 {
707  int extension_flag, ret;
708  uint8_t layout_map[MAX_ELEM_ID*4][3];
709  int tags = 0;
710 
711  if (get_bits1(gb)) { // frameLengthFlag
712  avpriv_request_sample(avctx, "960/120 MDCT window");
713  return AVERROR_PATCHWELCOME;
714  }
715 
716  if (get_bits1(gb)) // dependsOnCoreCoder
717  skip_bits(gb, 14); // coreCoderDelay
718  extension_flag = get_bits1(gb);
719 
720  if (m4ac->object_type == AOT_AAC_SCALABLE ||
722  skip_bits(gb, 3); // layerNr
723 
724  if (channel_config == 0) {
725  skip_bits(gb, 4); // element_instance_tag
726  tags = decode_pce(avctx, m4ac, layout_map, gb);
727  if (tags < 0)
728  return tags;
729  } else {
730  if ((ret = set_default_channel_config(avctx, layout_map, &tags, channel_config)))
731  return ret;
732  }
733 
734  if (count_channels(layout_map, tags) > 1) {
735  m4ac->ps = 0;
736  } else if (m4ac->sbr == 1 && m4ac->ps == -1)
737  m4ac->ps = 1;
738 
739  if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
740  return ret;
741 
742  if (extension_flag) {
743  switch (m4ac->object_type) {
744  case AOT_ER_BSAC:
745  skip_bits(gb, 5); // numOfSubFrame
746  skip_bits(gb, 11); // layer_length
747  break;
748  case AOT_ER_AAC_LC:
749  case AOT_ER_AAC_LTP:
750  case AOT_ER_AAC_SCALABLE:
751  case AOT_ER_AAC_LD:
752  skip_bits(gb, 3); /* aacSectionDataResilienceFlag
753  * aacScalefactorDataResilienceFlag
754  * aacSpectralDataResilienceFlag
755  */
756  break;
757  }
758  skip_bits1(gb); // extensionFlag3 (TBD in version 3)
759  }
760  return 0;
761 }
762 
763 /**
764  * Decode audio specific configuration; reference: table 1.13.
765  *
766  * @param ac pointer to AACContext, may be null
767  * @param avctx pointer to AVCCodecContext, used for logging
768  * @param m4ac pointer to MPEG4AudioConfig, used for parsing
769  * @param data pointer to buffer holding an audio specific config
770  * @param bit_size size of audio specific config or data in bits
771  * @param sync_extension look for an appended sync extension
772  *
773  * @return Returns error status or number of consumed bits. <0 - error
774  */
776  AVCodecContext *avctx,
777  MPEG4AudioConfig *m4ac,
778  const uint8_t *data, int bit_size,
779  int sync_extension)
780 {
781  GetBitContext gb;
782  int i;
783  int ret;
784 
785  av_dlog(avctx, "audio specific config size %d\n", bit_size >> 3);
786  for (i = 0; i < bit_size >> 3; i++)
787  av_dlog(avctx, "%02x ", data[i]);
788  av_dlog(avctx, "\n");
789 
790  if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
791  return ret;
792 
793  if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, sync_extension)) < 0)
794  return -1;
795  if (m4ac->sampling_index > 12) {
796  av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
797  return -1;
798  }
799 
800  skip_bits_long(&gb, i);
801 
802  switch (m4ac->object_type) {
803  case AOT_AAC_MAIN:
804  case AOT_AAC_LC:
805  case AOT_AAC_LTP:
806  if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
807  return -1;
808  break;
809  default:
810  av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
811  m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
812  return -1;
813  }
814 
815  av_dlog(avctx, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
816  m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
817  m4ac->sample_rate, m4ac->sbr, m4ac->ps);
818 
819  return get_bits_count(&gb);
820 }
821 
822 /**
823  * linear congruential pseudorandom number generator
824  *
825  * @param previous_val pointer to the current state of the generator
826  *
827  * @return Returns a 32-bit pseudorandom integer
828  */
829 static av_always_inline int lcg_random(unsigned previous_val)
830 {
831  union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
832  return v.s;
833 }
834 
836 {
837  ps->r0 = 0.0f;
838  ps->r1 = 0.0f;
839  ps->cor0 = 0.0f;
840  ps->cor1 = 0.0f;
841  ps->var0 = 1.0f;
842  ps->var1 = 1.0f;
843 }
844 
846 {
847  int i;
848  for (i = 0; i < MAX_PREDICTORS; i++)
849  reset_predict_state(&ps[i]);
850 }
851 
852 static int sample_rate_idx (int rate)
853 {
854  if (92017 <= rate) return 0;
855  else if (75132 <= rate) return 1;
856  else if (55426 <= rate) return 2;
857  else if (46009 <= rate) return 3;
858  else if (37566 <= rate) return 4;
859  else if (27713 <= rate) return 5;
860  else if (23004 <= rate) return 6;
861  else if (18783 <= rate) return 7;
862  else if (13856 <= rate) return 8;
863  else if (11502 <= rate) return 9;
864  else if (9391 <= rate) return 10;
865  else return 11;
866 }
867 
868 static void reset_predictor_group(PredictorState *ps, int group_num)
869 {
870  int i;
871  for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
872  reset_predict_state(&ps[i]);
873 }
874 
875 #define AAC_INIT_VLC_STATIC(num, size) \
876  INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
877  ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
878  ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
879  size);
880 
881 static void aacdec_init(AACContext *ac);
882 
884 {
885  AACContext *ac = avctx->priv_data;
886 
887  ac->avctx = avctx;
888  ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
889 
890  aacdec_init(ac);
891 
893 
894  if (avctx->extradata_size > 0) {
895  if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
896  avctx->extradata,
897  avctx->extradata_size*8, 1) < 0)
898  return -1;
899  } else {
900  int sr, i;
901  uint8_t layout_map[MAX_ELEM_ID*4][3];
902  int layout_map_tags;
903 
904  sr = sample_rate_idx(avctx->sample_rate);
905  ac->oc[1].m4ac.sampling_index = sr;
906  ac->oc[1].m4ac.channels = avctx->channels;
907  ac->oc[1].m4ac.sbr = -1;
908  ac->oc[1].m4ac.ps = -1;
909 
910  for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
911  if (ff_mpeg4audio_channels[i] == avctx->channels)
912  break;
914  i = 0;
915  }
916  ac->oc[1].m4ac.chan_config = i;
917 
918  if (ac->oc[1].m4ac.chan_config) {
919  int ret = set_default_channel_config(avctx, layout_map,
920  &layout_map_tags, ac->oc[1].m4ac.chan_config);
921  if (!ret)
922  output_configure(ac, layout_map, layout_map_tags,
923  OC_GLOBAL_HDR, 0);
924  else if (avctx->err_recognition & AV_EF_EXPLODE)
925  return AVERROR_INVALIDDATA;
926  }
927  }
928 
929  if (avctx->channels > MAX_CHANNELS) {
930  av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
931  return AVERROR_INVALIDDATA;
932  }
933 
934  AAC_INIT_VLC_STATIC( 0, 304);
935  AAC_INIT_VLC_STATIC( 1, 270);
936  AAC_INIT_VLC_STATIC( 2, 550);
937  AAC_INIT_VLC_STATIC( 3, 300);
938  AAC_INIT_VLC_STATIC( 4, 328);
939  AAC_INIT_VLC_STATIC( 5, 294);
940  AAC_INIT_VLC_STATIC( 6, 306);
941  AAC_INIT_VLC_STATIC( 7, 268);
942  AAC_INIT_VLC_STATIC( 8, 510);
943  AAC_INIT_VLC_STATIC( 9, 366);
944  AAC_INIT_VLC_STATIC(10, 462);
945 
946  ff_aac_sbr_init();
947 
948  ff_fmt_convert_init(&ac->fmt_conv, avctx);
950 
951  ac->random_state = 0x1f2e3d4c;
952 
954 
958  352);
959 
960  ff_mdct_init(&ac->mdct, 11, 1, 1.0 / (32768.0 * 1024.0));
961  ff_mdct_init(&ac->mdct_small, 8, 1, 1.0 / (32768.0 * 128.0));
962  ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0 * 32768.0);
963  // window initialization
964  ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
965  ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
968 
969  cbrt_tableinit();
970 
971  return 0;
972 }
973 
974 /**
975  * Skip data_stream_element; reference: table 4.10.
976  */
978 {
979  int byte_align = get_bits1(gb);
980  int count = get_bits(gb, 8);
981  if (count == 255)
982  count += get_bits(gb, 8);
983  if (byte_align)
984  align_get_bits(gb);
985 
986  if (get_bits_left(gb) < 8 * count) {
987  av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
988  return -1;
989  }
990  skip_bits_long(gb, 8 * count);
991  return 0;
992 }
993 
995  GetBitContext *gb)
996 {
997  int sfb;
998  if (get_bits1(gb)) {
999  ics->predictor_reset_group = get_bits(gb, 5);
1000  if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
1001  av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
1002  return -1;
1003  }
1004  }
1005  for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1006  ics->prediction_used[sfb] = get_bits1(gb);
1007  }
1008  return 0;
1009 }
1010 
1011 /**
1012  * Decode Long Term Prediction data; reference: table 4.xx.
1013  */
1015  GetBitContext *gb, uint8_t max_sfb)
1016 {
1017  int sfb;
1018 
1019  ltp->lag = get_bits(gb, 11);
1020  ltp->coef = ltp_coef[get_bits(gb, 3)];
1021  for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
1022  ltp->used[sfb] = get_bits1(gb);
1023 }
1024 
1025 /**
1026  * Decode Individual Channel Stream info; reference: table 4.6.
1027  */
1029  GetBitContext *gb)
1030 {
1031  if (get_bits1(gb)) {
1032  av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1033  return AVERROR_INVALIDDATA;
1034  }
1035  ics->window_sequence[1] = ics->window_sequence[0];
1036  ics->window_sequence[0] = get_bits(gb, 2);
1037  ics->use_kb_window[1] = ics->use_kb_window[0];
1038  ics->use_kb_window[0] = get_bits1(gb);
1039  ics->num_window_groups = 1;
1040  ics->group_len[0] = 1;
1041  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1042  int i;
1043  ics->max_sfb = get_bits(gb, 4);
1044  for (i = 0; i < 7; i++) {
1045  if (get_bits1(gb)) {
1046  ics->group_len[ics->num_window_groups - 1]++;
1047  } else {
1048  ics->num_window_groups++;
1049  ics->group_len[ics->num_window_groups - 1] = 1;
1050  }
1051  }
1052  ics->num_windows = 8;
1056  ics->predictor_present = 0;
1057  } else {
1058  ics->max_sfb = get_bits(gb, 6);
1059  ics->num_windows = 1;
1063  ics->predictor_present = get_bits1(gb);
1064  ics->predictor_reset_group = 0;
1065  if (ics->predictor_present) {
1066  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1067  if (decode_prediction(ac, ics, gb)) {
1068  goto fail;
1069  }
1070  } else if (ac->oc[1].m4ac.object_type == AOT_AAC_LC) {
1071  av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
1072  goto fail;
1073  } else {
1074  if ((ics->ltp.present = get_bits(gb, 1)))
1075  decode_ltp(&ics->ltp, gb, ics->max_sfb);
1076  }
1077  }
1078  }
1079 
1080  if (ics->max_sfb > ics->num_swb) {
1081  av_log(ac->avctx, AV_LOG_ERROR,
1082  "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
1083  ics->max_sfb, ics->num_swb);
1084  goto fail;
1085  }
1086 
1087  return 0;
1088 fail:
1089  ics->max_sfb = 0;
1090  return AVERROR_INVALIDDATA;
1091 }
1092 
1093 /**
1094  * Decode band types (section_data payload); reference: table 4.46.
1095  *
1096  * @param band_type array of the used band type
1097  * @param band_type_run_end array of the last scalefactor band of a band type run
1098  *
1099  * @return Returns error status. 0 - OK, !0 - error
1100  */
1101 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
1102  int band_type_run_end[120], GetBitContext *gb,
1104 {
1105  int g, idx = 0;
1106  const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1107  for (g = 0; g < ics->num_window_groups; g++) {
1108  int k = 0;
1109  while (k < ics->max_sfb) {
1110  uint8_t sect_end = k;
1111  int sect_len_incr;
1112  int sect_band_type = get_bits(gb, 4);
1113  if (sect_band_type == 12) {
1114  av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1115  return -1;
1116  }
1117  do {
1118  sect_len_incr = get_bits(gb, bits);
1119  sect_end += sect_len_incr;
1120  if (get_bits_left(gb) < 0) {
1121  av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1122  return -1;
1123  }
1124  if (sect_end > ics->max_sfb) {
1125  av_log(ac->avctx, AV_LOG_ERROR,
1126  "Number of bands (%d) exceeds limit (%d).\n",
1127  sect_end, ics->max_sfb);
1128  return -1;
1129  }
1130  } while (sect_len_incr == (1 << bits) - 1);
1131  for (; k < sect_end; k++) {
1132  band_type [idx] = sect_band_type;
1133  band_type_run_end[idx++] = sect_end;
1134  }
1135  }
1136  }
1137  return 0;
1138 }
1139 
1140 /**
1141  * Decode scalefactors; reference: table 4.47.
1142  *
1143  * @param global_gain first scalefactor value as scalefactors are differentially coded
1144  * @param band_type array of the used band type
1145  * @param band_type_run_end array of the last scalefactor band of a band type run
1146  * @param sf array of scalefactors or intensity stereo positions
1147  *
1148  * @return Returns error status. 0 - OK, !0 - error
1149  */
1150 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
1151  unsigned int global_gain,
1153  enum BandType band_type[120],
1154  int band_type_run_end[120])
1155 {
1156  int g, i, idx = 0;
1157  int offset[3] = { global_gain, global_gain - 90, 0 };
1158  int clipped_offset;
1159  int noise_flag = 1;
1160  for (g = 0; g < ics->num_window_groups; g++) {
1161  for (i = 0; i < ics->max_sfb;) {
1162  int run_end = band_type_run_end[idx];
1163  if (band_type[idx] == ZERO_BT) {
1164  for (; i < run_end; i++, idx++)
1165  sf[idx] = 0.;
1166  } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
1167  for (; i < run_end; i++, idx++) {
1168  offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1169  clipped_offset = av_clip(offset[2], -155, 100);
1170  if (offset[2] != clipped_offset) {
1172  "If you heard an audible artifact, there may be a bug in the decoder. "
1173  "Clipped intensity stereo position (%d -> %d)",
1174  offset[2], clipped_offset);
1175  }
1176  sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
1177  }
1178  } else if (band_type[idx] == NOISE_BT) {
1179  for (; i < run_end; i++, idx++) {
1180  if (noise_flag-- > 0)
1181  offset[1] += get_bits(gb, 9) - 256;
1182  else
1183  offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1184  clipped_offset = av_clip(offset[1], -100, 155);
1185  if (offset[1] != clipped_offset) {
1187  "If you heard an audible artifact, there may be a bug in the decoder. "
1188  "Clipped noise gain (%d -> %d)",
1189  offset[1], clipped_offset);
1190  }
1191  sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
1192  }
1193  } else {
1194  for (; i < run_end; i++, idx++) {
1195  offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1196  if (offset[0] > 255U) {
1197  av_log(ac->avctx, AV_LOG_ERROR,
1198  "Scalefactor (%d) out of range.\n", offset[0]);
1199  return -1;
1200  }
1201  sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
1202  }
1203  }
1204  }
1205  }
1206  return 0;
1207 }
1208 
1209 /**
1210  * Decode pulse data; reference: table 4.7.
1211  */
1212 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1213  const uint16_t *swb_offset, int num_swb)
1214 {
1215  int i, pulse_swb;
1216  pulse->num_pulse = get_bits(gb, 2) + 1;
1217  pulse_swb = get_bits(gb, 6);
1218  if (pulse_swb >= num_swb)
1219  return -1;
1220  pulse->pos[0] = swb_offset[pulse_swb];
1221  pulse->pos[0] += get_bits(gb, 5);
1222  if (pulse->pos[0] > 1023)
1223  return -1;
1224  pulse->amp[0] = get_bits(gb, 4);
1225  for (i = 1; i < pulse->num_pulse; i++) {
1226  pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1227  if (pulse->pos[i] > 1023)
1228  return -1;
1229  pulse->amp[i] = get_bits(gb, 4);
1230  }
1231  return 0;
1232 }
1233 
1234 /**
1235  * Decode Temporal Noise Shaping data; reference: table 4.48.
1236  *
1237  * @return Returns error status. 0 - OK, !0 - error
1238  */
1240  GetBitContext *gb, const IndividualChannelStream *ics)
1241 {
1242  int w, filt, i, coef_len, coef_res, coef_compress;
1243  const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1244  const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1245  for (w = 0; w < ics->num_windows; w++) {
1246  if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1247  coef_res = get_bits1(gb);
1248 
1249  for (filt = 0; filt < tns->n_filt[w]; filt++) {
1250  int tmp2_idx;
1251  tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1252 
1253  if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
1254  av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
1255  tns->order[w][filt], tns_max_order);
1256  tns->order[w][filt] = 0;
1257  return -1;
1258  }
1259  if (tns->order[w][filt]) {
1260  tns->direction[w][filt] = get_bits1(gb);
1261  coef_compress = get_bits1(gb);
1262  coef_len = coef_res + 3 - coef_compress;
1263  tmp2_idx = 2 * coef_compress + coef_res;
1264 
1265  for (i = 0; i < tns->order[w][filt]; i++)
1266  tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1267  }
1268  }
1269  }
1270  }
1271  return 0;
1272 }
1273 
1274 /**
1275  * Decode Mid/Side data; reference: table 4.54.
1276  *
1277  * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1278  * [1] mask is decoded from bitstream; [2] mask is all 1s;
1279  * [3] reserved for scalable AAC
1280  */
1282  int ms_present)
1283 {
1284  int idx;
1285  if (ms_present == 1) {
1286  for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
1287  cpe->ms_mask[idx] = get_bits1(gb);
1288  } else if (ms_present == 2) {
1289  memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask[0]) * cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb);
1290  }
1291 }
1292 
1293 #ifndef VMUL2
1294 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
1295  const float *scale)
1296 {
1297  float s = *scale;
1298  *dst++ = v[idx & 15] * s;
1299  *dst++ = v[idx>>4 & 15] * s;
1300  return dst;
1301 }
1302 #endif
1303 
1304 #ifndef VMUL4
1305 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
1306  const float *scale)
1307 {
1308  float s = *scale;
1309  *dst++ = v[idx & 3] * s;
1310  *dst++ = v[idx>>2 & 3] * s;
1311  *dst++ = v[idx>>4 & 3] * s;
1312  *dst++ = v[idx>>6 & 3] * s;
1313  return dst;
1314 }
1315 #endif
1316 
1317 #ifndef VMUL2S
1318 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
1319  unsigned sign, const float *scale)
1320 {
1321  union av_intfloat32 s0, s1;
1322 
1323  s0.f = s1.f = *scale;
1324  s0.i ^= sign >> 1 << 31;
1325  s1.i ^= sign << 31;
1326 
1327  *dst++ = v[idx & 15] * s0.f;
1328  *dst++ = v[idx>>4 & 15] * s1.f;
1329 
1330  return dst;
1331 }
1332 #endif
1333 
1334 #ifndef VMUL4S
1335 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
1336  unsigned sign, const float *scale)
1337 {
1338  unsigned nz = idx >> 12;
1339  union av_intfloat32 s = { .f = *scale };
1340  union av_intfloat32 t;
1341 
1342  t.i = s.i ^ (sign & 1U<<31);
1343  *dst++ = v[idx & 3] * t.f;
1344 
1345  sign <<= nz & 1; nz >>= 1;
1346  t.i = s.i ^ (sign & 1U<<31);
1347  *dst++ = v[idx>>2 & 3] * t.f;
1348 
1349  sign <<= nz & 1; nz >>= 1;
1350  t.i = s.i ^ (sign & 1U<<31);
1351  *dst++ = v[idx>>4 & 3] * t.f;
1352 
1353  sign <<= nz & 1;
1354  t.i = s.i ^ (sign & 1U<<31);
1355  *dst++ = v[idx>>6 & 3] * t.f;
1356 
1357  return dst;
1358 }
1359 #endif
1360 
1361 /**
1362  * Decode spectral data; reference: table 4.50.
1363  * Dequantize and scale spectral data; reference: 4.6.3.3.
1364  *
1365  * @param coef array of dequantized, scaled spectral data
1366  * @param sf array of scalefactors or intensity stereo positions
1367  * @param pulse_present set if pulses are present
1368  * @param pulse pointer to pulse data struct
1369  * @param band_type array of the used band type
1370  *
1371  * @return Returns error status. 0 - OK, !0 - error
1372  */
1373 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
1374  GetBitContext *gb, const float sf[120],
1375  int pulse_present, const Pulse *pulse,
1376  const IndividualChannelStream *ics,
1377  enum BandType band_type[120])
1378 {
1379  int i, k, g, idx = 0;
1380  const int c = 1024 / ics->num_windows;
1381  const uint16_t *offsets = ics->swb_offset;
1382  float *coef_base = coef;
1383 
1384  for (g = 0; g < ics->num_windows; g++)
1385  memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
1386 
1387  for (g = 0; g < ics->num_window_groups; g++) {
1388  unsigned g_len = ics->group_len[g];
1389 
1390  for (i = 0; i < ics->max_sfb; i++, idx++) {
1391  const unsigned cbt_m1 = band_type[idx] - 1;
1392  float *cfo = coef + offsets[i];
1393  int off_len = offsets[i + 1] - offsets[i];
1394  int group;
1395 
1396  if (cbt_m1 >= INTENSITY_BT2 - 1) {
1397  for (group = 0; group < g_len; group++, cfo+=128) {
1398  memset(cfo, 0, off_len * sizeof(float));
1399  }
1400  } else if (cbt_m1 == NOISE_BT - 1) {
1401  for (group = 0; group < g_len; group++, cfo+=128) {
1402  float scale;
1403  float band_energy;
1404 
1405  for (k = 0; k < off_len; k++) {
1407  cfo[k] = ac->random_state;
1408  }
1409 
1410  band_energy = ac->fdsp.scalarproduct_float(cfo, cfo, off_len);
1411  scale = sf[idx] / sqrtf(band_energy);
1412  ac->fdsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
1413  }
1414  } else {
1415  const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1416  const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
1417  VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1418  OPEN_READER(re, gb);
1419 
1420  switch (cbt_m1 >> 1) {
1421  case 0:
1422  for (group = 0; group < g_len; group++, cfo+=128) {
1423  float *cf = cfo;
1424  int len = off_len;
1425 
1426  do {
1427  int code;
1428  unsigned cb_idx;
1429 
1430  UPDATE_CACHE(re, gb);
1431  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1432  cb_idx = cb_vector_idx[code];
1433  cf = VMUL4(cf, vq, cb_idx, sf + idx);
1434  } while (len -= 4);
1435  }
1436  break;
1437 
1438  case 1:
1439  for (group = 0; group < g_len; group++, cfo+=128) {
1440  float *cf = cfo;
1441  int len = off_len;
1442 
1443  do {
1444  int code;
1445  unsigned nnz;
1446  unsigned cb_idx;
1447  uint32_t bits;
1448 
1449  UPDATE_CACHE(re, gb);
1450  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1451  cb_idx = cb_vector_idx[code];
1452  nnz = cb_idx >> 8 & 15;
1453  bits = nnz ? GET_CACHE(re, gb) : 0;
1454  LAST_SKIP_BITS(re, gb, nnz);
1455  cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1456  } while (len -= 4);
1457  }
1458  break;
1459 
1460  case 2:
1461  for (group = 0; group < g_len; group++, cfo+=128) {
1462  float *cf = cfo;
1463  int len = off_len;
1464 
1465  do {
1466  int code;
1467  unsigned cb_idx;
1468 
1469  UPDATE_CACHE(re, gb);
1470  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1471  cb_idx = cb_vector_idx[code];
1472  cf = VMUL2(cf, vq, cb_idx, sf + idx);
1473  } while (len -= 2);
1474  }
1475  break;
1476 
1477  case 3:
1478  case 4:
1479  for (group = 0; group < g_len; group++, cfo+=128) {
1480  float *cf = cfo;
1481  int len = off_len;
1482 
1483  do {
1484  int code;
1485  unsigned nnz;
1486  unsigned cb_idx;
1487  unsigned sign;
1488 
1489  UPDATE_CACHE(re, gb);
1490  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1491  cb_idx = cb_vector_idx[code];
1492  nnz = cb_idx >> 8 & 15;
1493  sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
1494  LAST_SKIP_BITS(re, gb, nnz);
1495  cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1496  } while (len -= 2);
1497  }
1498  break;
1499 
1500  default:
1501  for (group = 0; group < g_len; group++, cfo+=128) {
1502  float *cf = cfo;
1503  uint32_t *icf = (uint32_t *) cf;
1504  int len = off_len;
1505 
1506  do {
1507  int code;
1508  unsigned nzt, nnz;
1509  unsigned cb_idx;
1510  uint32_t bits;
1511  int j;
1512 
1513  UPDATE_CACHE(re, gb);
1514  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1515 
1516  if (!code) {
1517  *icf++ = 0;
1518  *icf++ = 0;
1519  continue;
1520  }
1521 
1522  cb_idx = cb_vector_idx[code];
1523  nnz = cb_idx >> 12;
1524  nzt = cb_idx >> 8;
1525  bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1526  LAST_SKIP_BITS(re, gb, nnz);
1527 
1528  for (j = 0; j < 2; j++) {
1529  if (nzt & 1<<j) {
1530  uint32_t b;
1531  int n;
1532  /* The total length of escape_sequence must be < 22 bits according
1533  to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1534  UPDATE_CACHE(re, gb);
1535  b = GET_CACHE(re, gb);
1536  b = 31 - av_log2(~b);
1537 
1538  if (b > 8) {
1539  av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1540  return -1;
1541  }
1542 
1543  SKIP_BITS(re, gb, b + 1);
1544  b += 4;
1545  n = (1 << b) + SHOW_UBITS(re, gb, b);
1546  LAST_SKIP_BITS(re, gb, b);
1547  *icf++ = cbrt_tab[n] | (bits & 1U<<31);
1548  bits <<= 1;
1549  } else {
1550  unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1551  *icf++ = (bits & 1U<<31) | v;
1552  bits <<= !!v;
1553  }
1554  cb_idx >>= 4;
1555  }
1556  } while (len -= 2);
1557 
1558  ac->fdsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1559  }
1560  }
1561 
1562  CLOSE_READER(re, gb);
1563  }
1564  }
1565  coef += g_len << 7;
1566  }
1567 
1568  if (pulse_present) {
1569  idx = 0;
1570  for (i = 0; i < pulse->num_pulse; i++) {
1571  float co = coef_base[ pulse->pos[i] ];
1572  while (offsets[idx + 1] <= pulse->pos[i])
1573  idx++;
1574  if (band_type[idx] != NOISE_BT && sf[idx]) {
1575  float ico = -pulse->amp[i];
1576  if (co) {
1577  co /= sf[idx];
1578  ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
1579  }
1580  coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
1581  }
1582  }
1583  }
1584  return 0;
1585 }
1586 
1587 static av_always_inline float flt16_round(float pf)
1588 {
1589  union av_intfloat32 tmp;
1590  tmp.f = pf;
1591  tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
1592  return tmp.f;
1593 }
1594 
1595 static av_always_inline float flt16_even(float pf)
1596 {
1597  union av_intfloat32 tmp;
1598  tmp.f = pf;
1599  tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
1600  return tmp.f;
1601 }
1602 
1603 static av_always_inline float flt16_trunc(float pf)
1604 {
1605  union av_intfloat32 pun;
1606  pun.f = pf;
1607  pun.i &= 0xFFFF0000U;
1608  return pun.f;
1609 }
1610 
1611 static av_always_inline void predict(PredictorState *ps, float *coef,
1612  int output_enable)
1613 {
1614  const float a = 0.953125; // 61.0 / 64
1615  const float alpha = 0.90625; // 29.0 / 32
1616  float e0, e1;
1617  float pv;
1618  float k1, k2;
1619  float r0 = ps->r0, r1 = ps->r1;
1620  float cor0 = ps->cor0, cor1 = ps->cor1;
1621  float var0 = ps->var0, var1 = ps->var1;
1622 
1623  k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
1624  k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
1625 
1626  pv = flt16_round(k1 * r0 + k2 * r1);
1627  if (output_enable)
1628  *coef += pv;
1629 
1630  e0 = *coef;
1631  e1 = e0 - k1 * r0;
1632 
1633  ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
1634  ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
1635  ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
1636  ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
1637 
1638  ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
1639  ps->r0 = flt16_trunc(a * e0);
1640 }
1641 
1642 /**
1643  * Apply AAC-Main style frequency domain prediction.
1644  */
1646 {
1647  int sfb, k;
1648 
1649  if (!sce->ics.predictor_initialized) {
1651  sce->ics.predictor_initialized = 1;
1652  }
1653 
1654  if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1655  for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]; sfb++) {
1656  for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
1657  predict(&sce->predictor_state[k], &sce->coeffs[k],
1658  sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
1659  }
1660  }
1661  if (sce->ics.predictor_reset_group)
1663  } else
1665 }
1666 
1667 /**
1668  * Decode an individual_channel_stream payload; reference: table 4.44.
1669  *
1670  * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
1671  * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1672  *
1673  * @return Returns error status. 0 - OK, !0 - error
1674  */
1676  GetBitContext *gb, int common_window, int scale_flag)
1677 {
1678  Pulse pulse;
1679  TemporalNoiseShaping *tns = &sce->tns;
1680  IndividualChannelStream *ics = &sce->ics;
1681  float *out = sce->coeffs;
1682  int global_gain, pulse_present = 0;
1683 
1684  /* This assignment is to silence a GCC warning about the variable being used
1685  * uninitialized when in fact it always is.
1686  */
1687  pulse.num_pulse = 0;
1688 
1689  global_gain = get_bits(gb, 8);
1690 
1691  if (!common_window && !scale_flag) {
1692  if (decode_ics_info(ac, ics, gb) < 0)
1693  return AVERROR_INVALIDDATA;
1694  }
1695 
1696  if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
1697  return -1;
1698  if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
1699  return -1;
1700 
1701  pulse_present = 0;
1702  if (!scale_flag) {
1703  if ((pulse_present = get_bits1(gb))) {
1704  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1705  av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
1706  return -1;
1707  }
1708  if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1709  av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
1710  return -1;
1711  }
1712  }
1713  if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
1714  return -1;
1715  if (get_bits1(gb)) {
1716  avpriv_request_sample(ac->avctx, "SSR");
1717  return AVERROR_PATCHWELCOME;
1718  }
1719  }
1720 
1721  if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
1722  return -1;
1723 
1724  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
1725  apply_prediction(ac, sce);
1726 
1727  return 0;
1728 }
1729 
1730 /**
1731  * Mid/Side stereo decoding; reference: 4.6.8.1.3.
1732  */
1734 {
1735  const IndividualChannelStream *ics = &cpe->ch[0].ics;
1736  float *ch0 = cpe->ch[0].coeffs;
1737  float *ch1 = cpe->ch[1].coeffs;
1738  int g, i, group, idx = 0;
1739  const uint16_t *offsets = ics->swb_offset;
1740  for (g = 0; g < ics->num_window_groups; g++) {
1741  for (i = 0; i < ics->max_sfb; i++, idx++) {
1742  if (cpe->ms_mask[idx] &&
1743  cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
1744  for (group = 0; group < ics->group_len[g]; group++) {
1745  ac->fdsp.butterflies_float(ch0 + group * 128 + offsets[i],
1746  ch1 + group * 128 + offsets[i],
1747  offsets[i+1] - offsets[i]);
1748  }
1749  }
1750  }
1751  ch0 += ics->group_len[g] * 128;
1752  ch1 += ics->group_len[g] * 128;
1753  }
1754 }
1755 
1756 /**
1757  * intensity stereo decoding; reference: 4.6.8.2.3
1758  *
1759  * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1760  * [1] mask is decoded from bitstream; [2] mask is all 1s;
1761  * [3] reserved for scalable AAC
1762  */
1763 static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
1764 {
1765  const IndividualChannelStream *ics = &cpe->ch[1].ics;
1766  SingleChannelElement *sce1 = &cpe->ch[1];
1767  float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
1768  const uint16_t *offsets = ics->swb_offset;
1769  int g, group, i, idx = 0;
1770  int c;
1771  float scale;
1772  for (g = 0; g < ics->num_window_groups; g++) {
1773  for (i = 0; i < ics->max_sfb;) {
1774  if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
1775  const int bt_run_end = sce1->band_type_run_end[idx];
1776  for (; i < bt_run_end; i++, idx++) {
1777  c = -1 + 2 * (sce1->band_type[idx] - 14);
1778  if (ms_present)
1779  c *= 1 - 2 * cpe->ms_mask[idx];
1780  scale = c * sce1->sf[idx];
1781  for (group = 0; group < ics->group_len[g]; group++)
1782  ac->fdsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
1783  coef0 + group * 128 + offsets[i],
1784  scale,
1785  offsets[i + 1] - offsets[i]);
1786  }
1787  } else {
1788  int bt_run_end = sce1->band_type_run_end[idx];
1789  idx += bt_run_end - i;
1790  i = bt_run_end;
1791  }
1792  }
1793  coef0 += ics->group_len[g] * 128;
1794  coef1 += ics->group_len[g] * 128;
1795  }
1796 }
1797 
1798 /**
1799  * Decode a channel_pair_element; reference: table 4.4.
1800  *
1801  * @return Returns error status. 0 - OK, !0 - error
1802  */
1804 {
1805  int i, ret, common_window, ms_present = 0;
1806 
1807  common_window = get_bits1(gb);
1808  if (common_window) {
1809  if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
1810  return AVERROR_INVALIDDATA;
1811  i = cpe->ch[1].ics.use_kb_window[0];
1812  cpe->ch[1].ics = cpe->ch[0].ics;
1813  cpe->ch[1].ics.use_kb_window[1] = i;
1814  if (cpe->ch[1].ics.predictor_present && (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
1815  if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
1816  decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
1817  ms_present = get_bits(gb, 2);
1818  if (ms_present == 3) {
1819  av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1820  return -1;
1821  } else if (ms_present)
1822  decode_mid_side_stereo(cpe, gb, ms_present);
1823  }
1824  if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1825  return ret;
1826  if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1827  return ret;
1828 
1829  if (common_window) {
1830  if (ms_present)
1831  apply_mid_side_stereo(ac, cpe);
1832  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1833  apply_prediction(ac, &cpe->ch[0]);
1834  apply_prediction(ac, &cpe->ch[1]);
1835  }
1836  }
1837 
1838  apply_intensity_stereo(ac, cpe, ms_present);
1839  return 0;
1840 }
1841 
1842 static const float cce_scale[] = {
1843  1.09050773266525765921, //2^(1/8)
1844  1.18920711500272106672, //2^(1/4)
1845  M_SQRT2,
1846  2,
1847 };
1848 
1849 /**
1850  * Decode coupling_channel_element; reference: table 4.8.
1851  *
1852  * @return Returns error status. 0 - OK, !0 - error
1853  */
1855 {
1856  int num_gain = 0;
1857  int c, g, sfb, ret;
1858  int sign;
1859  float scale;
1860  SingleChannelElement *sce = &che->ch[0];
1861  ChannelCoupling *coup = &che->coup;
1862 
1863  coup->coupling_point = 2 * get_bits1(gb);
1864  coup->num_coupled = get_bits(gb, 3);
1865  for (c = 0; c <= coup->num_coupled; c++) {
1866  num_gain++;
1867  coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
1868  coup->id_select[c] = get_bits(gb, 4);
1869  if (coup->type[c] == TYPE_CPE) {
1870  coup->ch_select[c] = get_bits(gb, 2);
1871  if (coup->ch_select[c] == 3)
1872  num_gain++;
1873  } else
1874  coup->ch_select[c] = 2;
1875  }
1876  coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
1877 
1878  sign = get_bits(gb, 1);
1879  scale = cce_scale[get_bits(gb, 2)];
1880 
1881  if ((ret = decode_ics(ac, sce, gb, 0, 0)))
1882  return ret;
1883 
1884  for (c = 0; c < num_gain; c++) {
1885  int idx = 0;
1886  int cge = 1;
1887  int gain = 0;
1888  float gain_cache = 1.;
1889  if (c) {
1890  cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
1891  gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
1892  gain_cache = powf(scale, -gain);
1893  }
1894  if (coup->coupling_point == AFTER_IMDCT) {
1895  coup->gain[c][0] = gain_cache;
1896  } else {
1897  for (g = 0; g < sce->ics.num_window_groups; g++) {
1898  for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
1899  if (sce->band_type[idx] != ZERO_BT) {
1900  if (!cge) {
1901  int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1902  if (t) {
1903  int s = 1;
1904  t = gain += t;
1905  if (sign) {
1906  s -= 2 * (t & 0x1);
1907  t >>= 1;
1908  }
1909  gain_cache = powf(scale, -t) * s;
1910  }
1911  }
1912  coup->gain[c][idx] = gain_cache;
1913  }
1914  }
1915  }
1916  }
1917  }
1918  return 0;
1919 }
1920 
1921 /**
1922  * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1923  *
1924  * @return Returns number of bytes consumed.
1925  */
1927  GetBitContext *gb)
1928 {
1929  int i;
1930  int num_excl_chan = 0;
1931 
1932  do {
1933  for (i = 0; i < 7; i++)
1934  che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1935  } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
1936 
1937  return num_excl_chan / 7;
1938 }
1939 
1940 /**
1941  * Decode dynamic range information; reference: table 4.52.
1942  *
1943  * @return Returns number of bytes consumed.
1944  */
1946  GetBitContext *gb)
1947 {
1948  int n = 1;
1949  int drc_num_bands = 1;
1950  int i;
1951 
1952  /* pce_tag_present? */
1953  if (get_bits1(gb)) {
1954  che_drc->pce_instance_tag = get_bits(gb, 4);
1955  skip_bits(gb, 4); // tag_reserved_bits
1956  n++;
1957  }
1958 
1959  /* excluded_chns_present? */
1960  if (get_bits1(gb)) {
1961  n += decode_drc_channel_exclusions(che_drc, gb);
1962  }
1963 
1964  /* drc_bands_present? */
1965  if (get_bits1(gb)) {
1966  che_drc->band_incr = get_bits(gb, 4);
1967  che_drc->interpolation_scheme = get_bits(gb, 4);
1968  n++;
1969  drc_num_bands += che_drc->band_incr;
1970  for (i = 0; i < drc_num_bands; i++) {
1971  che_drc->band_top[i] = get_bits(gb, 8);
1972  n++;
1973  }
1974  }
1975 
1976  /* prog_ref_level_present? */
1977  if (get_bits1(gb)) {
1978  che_drc->prog_ref_level = get_bits(gb, 7);
1979  skip_bits1(gb); // prog_ref_level_reserved_bits
1980  n++;
1981  }
1982 
1983  for (i = 0; i < drc_num_bands; i++) {
1984  che_drc->dyn_rng_sgn[i] = get_bits1(gb);
1985  che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
1986  n++;
1987  }
1988 
1989  return n;
1990 }
1991 
1992 static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
1993  uint8_t buf[256];
1994  int i, major, minor;
1995 
1996  if (len < 13+7*8)
1997  goto unknown;
1998 
1999  get_bits(gb, 13); len -= 13;
2000 
2001  for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
2002  buf[i] = get_bits(gb, 8);
2003 
2004  buf[i] = 0;
2005  if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
2006  av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
2007 
2008  if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
2009  ac->avctx->internal->skip_samples = 1024;
2010  }
2011 
2012 unknown:
2013  skip_bits_long(gb, len);
2014 
2015  return 0;
2016 }
2017 
2018 /**
2019  * Decode extension data (incomplete); reference: table 4.51.
2020  *
2021  * @param cnt length of TYPE_FIL syntactic element in bytes
2022  *
2023  * @return Returns number of bytes consumed
2024  */
2026  ChannelElement *che, enum RawDataBlockType elem_type)
2027 {
2028  int crc_flag = 0;
2029  int res = cnt;
2030  switch (get_bits(gb, 4)) { // extension type
2031  case EXT_SBR_DATA_CRC:
2032  crc_flag++;
2033  case EXT_SBR_DATA:
2034  if (!che) {
2035  av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
2036  return res;
2037  } else if (!ac->oc[1].m4ac.sbr) {
2038  av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
2039  skip_bits_long(gb, 8 * cnt - 4);
2040  return res;
2041  } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
2042  av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
2043  skip_bits_long(gb, 8 * cnt - 4);
2044  return res;
2045  } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
2046  ac->oc[1].m4ac.sbr = 1;
2047  ac->oc[1].m4ac.ps = 1;
2048  output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
2049  ac->oc[1].status, 1);
2050  } else {
2051  ac->oc[1].m4ac.sbr = 1;
2052  }
2053  res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
2054  break;
2055  case EXT_DYNAMIC_RANGE:
2056  res = decode_dynamic_range(&ac->che_drc, gb);
2057  break;
2058  case EXT_FILL:
2059  decode_fill(ac, gb, 8 * cnt - 4);
2060  break;
2061  case EXT_FILL_DATA:
2062  case EXT_DATA_ELEMENT:
2063  default:
2064  skip_bits_long(gb, 8 * cnt - 4);
2065  break;
2066  };
2067  return res;
2068 }
2069 
2070 /**
2071  * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
2072  *
2073  * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
2074  * @param coef spectral coefficients
2075  */
2076 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
2077  IndividualChannelStream *ics, int decode)
2078 {
2079  const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
2080  int w, filt, m, i;
2081  int bottom, top, order, start, end, size, inc;
2082  float lpc[TNS_MAX_ORDER];
2083  float tmp[TNS_MAX_ORDER+1];
2084 
2085  for (w = 0; w < ics->num_windows; w++) {
2086  bottom = ics->num_swb;
2087  for (filt = 0; filt < tns->n_filt[w]; filt++) {
2088  top = bottom;
2089  bottom = FFMAX(0, top - tns->length[w][filt]);
2090  order = tns->order[w][filt];
2091  if (order == 0)
2092  continue;
2093 
2094  // tns_decode_coef
2095  compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
2096 
2097  start = ics->swb_offset[FFMIN(bottom, mmm)];
2098  end = ics->swb_offset[FFMIN( top, mmm)];
2099  if ((size = end - start) <= 0)
2100  continue;
2101  if (tns->direction[w][filt]) {
2102  inc = -1;
2103  start = end - 1;
2104  } else {
2105  inc = 1;
2106  }
2107  start += w * 128;
2108 
2109  if (decode) {
2110  // ar filter
2111  for (m = 0; m < size; m++, start += inc)
2112  for (i = 1; i <= FFMIN(m, order); i++)
2113  coef[start] -= coef[start - i * inc] * lpc[i - 1];
2114  } else {
2115  // ma filter
2116  for (m = 0; m < size; m++, start += inc) {
2117  tmp[0] = coef[start];
2118  for (i = 1; i <= FFMIN(m, order); i++)
2119  coef[start] += tmp[i] * lpc[i - 1];
2120  for (i = order; i > 0; i--)
2121  tmp[i] = tmp[i - 1];
2122  }
2123  }
2124  }
2125  }
2126 }
2127 
2128 /**
2129  * Apply windowing and MDCT to obtain the spectral
2130  * coefficient from the predicted sample by LTP.
2131  */
2133  float *in, IndividualChannelStream *ics)
2134 {
2135  const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2136  const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2137  const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2138  const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2139 
2140  if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
2141  ac->fdsp.vector_fmul(in, in, lwindow_prev, 1024);
2142  } else {
2143  memset(in, 0, 448 * sizeof(float));
2144  ac->fdsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
2145  }
2146  if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
2147  ac->fdsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
2148  } else {
2149  ac->fdsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
2150  memset(in + 1024 + 576, 0, 448 * sizeof(float));
2151  }
2152  ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
2153 }
2154 
2155 /**
2156  * Apply the long term prediction
2157  */
2159 {
2160  const LongTermPrediction *ltp = &sce->ics.ltp;
2161  const uint16_t *offsets = sce->ics.swb_offset;
2162  int i, sfb;
2163 
2164  if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2165  float *predTime = sce->ret;
2166  float *predFreq = ac->buf_mdct;
2167  int16_t num_samples = 2048;
2168 
2169  if (ltp->lag < 1024)
2170  num_samples = ltp->lag + 1024;
2171  for (i = 0; i < num_samples; i++)
2172  predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
2173  memset(&predTime[i], 0, (2048 - i) * sizeof(float));
2174 
2175  ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
2176 
2177  if (sce->tns.present)
2178  ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
2179 
2180  for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
2181  if (ltp->used[sfb])
2182  for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
2183  sce->coeffs[i] += predFreq[i];
2184  }
2185 }
2186 
2187 /**
2188  * Update the LTP buffer for next frame
2189  */
2191 {
2192  IndividualChannelStream *ics = &sce->ics;
2193  float *saved = sce->saved;
2194  float *saved_ltp = sce->coeffs;
2195  const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2196  const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2197  int i;
2198 
2199  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2200  memcpy(saved_ltp, saved, 512 * sizeof(float));
2201  memset(saved_ltp + 576, 0, 448 * sizeof(float));
2202  ac->fdsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2203  for (i = 0; i < 64; i++)
2204  saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2205  } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2206  memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
2207  memset(saved_ltp + 576, 0, 448 * sizeof(float));
2208  ac->fdsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2209  for (i = 0; i < 64; i++)
2210  saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2211  } else { // LONG_STOP or ONLY_LONG
2212  ac->fdsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
2213  for (i = 0; i < 512; i++)
2214  saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
2215  }
2216 
2217  memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
2218  memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
2219  memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
2220 }
2221 
2222 /**
2223  * Conduct IMDCT and windowing.
2224  */
2226 {
2227  IndividualChannelStream *ics = &sce->ics;
2228  float *in = sce->coeffs;
2229  float *out = sce->ret;
2230  float *saved = sce->saved;
2231  const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2232  const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2233  const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2234  float *buf = ac->buf_mdct;
2235  float *temp = ac->temp;
2236  int i;
2237 
2238  // imdct
2239  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2240  for (i = 0; i < 1024; i += 128)
2241  ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
2242  } else
2243  ac->mdct.imdct_half(&ac->mdct, buf, in);
2244 
2245  /* window overlapping
2246  * NOTE: To simplify the overlapping code, all 'meaningless' short to long
2247  * and long to short transitions are considered to be short to short
2248  * transitions. This leaves just two cases (long to long and short to short)
2249  * with a little special sauce for EIGHT_SHORT_SEQUENCE.
2250  */
2251  if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2253  ac->fdsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
2254  } else {
2255  memcpy( out, saved, 448 * sizeof(float));
2256 
2257  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2258  ac->fdsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
2259  ac->fdsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
2260  ac->fdsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
2261  ac->fdsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
2262  ac->fdsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
2263  memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
2264  } else {
2265  ac->fdsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
2266  memcpy( out + 576, buf + 64, 448 * sizeof(float));
2267  }
2268  }
2269 
2270  // buffer update
2271  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2272  memcpy( saved, temp + 64, 64 * sizeof(float));
2273  ac->fdsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
2274  ac->fdsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
2275  ac->fdsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
2276  memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2277  } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2278  memcpy( saved, buf + 512, 448 * sizeof(float));
2279  memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2280  } else { // LONG_STOP or ONLY_LONG
2281  memcpy( saved, buf + 512, 512 * sizeof(float));
2282  }
2283 }
2284 
2285 /**
2286  * Apply dependent channel coupling (applied before IMDCT).
2287  *
2288  * @param index index into coupling gain array
2289  */
2291  SingleChannelElement *target,
2292  ChannelElement *cce, int index)
2293 {
2294  IndividualChannelStream *ics = &cce->ch[0].ics;
2295  const uint16_t *offsets = ics->swb_offset;
2296  float *dest = target->coeffs;
2297  const float *src = cce->ch[0].coeffs;
2298  int g, i, group, k, idx = 0;
2299  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2300  av_log(ac->avctx, AV_LOG_ERROR,
2301  "Dependent coupling is not supported together with LTP\n");
2302  return;
2303  }
2304  for (g = 0; g < ics->num_window_groups; g++) {
2305  for (i = 0; i < ics->max_sfb; i++, idx++) {
2306  if (cce->ch[0].band_type[idx] != ZERO_BT) {
2307  const float gain = cce->coup.gain[index][idx];
2308  for (group = 0; group < ics->group_len[g]; group++) {
2309  for (k = offsets[i]; k < offsets[i + 1]; k++) {
2310  // XXX dsputil-ize
2311  dest[group * 128 + k] += gain * src[group * 128 + k];
2312  }
2313  }
2314  }
2315  }
2316  dest += ics->group_len[g] * 128;
2317  src += ics->group_len[g] * 128;
2318  }
2319 }
2320 
2321 /**
2322  * Apply independent channel coupling (applied after IMDCT).
2323  *
2324  * @param index index into coupling gain array
2325  */
2327  SingleChannelElement *target,
2328  ChannelElement *cce, int index)
2329 {
2330  int i;
2331  const float gain = cce->coup.gain[index][0];
2332  const float *src = cce->ch[0].ret;
2333  float *dest = target->ret;
2334  const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
2335 
2336  for (i = 0; i < len; i++)
2337  dest[i] += gain * src[i];
2338 }
2339 
2340 /**
2341  * channel coupling transformation interface
2342  *
2343  * @param apply_coupling_method pointer to (in)dependent coupling function
2344  */
2346  enum RawDataBlockType type, int elem_id,
2347  enum CouplingPoint coupling_point,
2348  void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2349 {
2350  int i, c;
2351 
2352  for (i = 0; i < MAX_ELEM_ID; i++) {
2353  ChannelElement *cce = ac->che[TYPE_CCE][i];
2354  int index = 0;
2355 
2356  if (cce && cce->coup.coupling_point == coupling_point) {
2357  ChannelCoupling *coup = &cce->coup;
2358 
2359  for (c = 0; c <= coup->num_coupled; c++) {
2360  if (coup->type[c] == type && coup->id_select[c] == elem_id) {
2361  if (coup->ch_select[c] != 1) {
2362  apply_coupling_method(ac, &cc->ch[0], cce, index);
2363  if (coup->ch_select[c] != 0)
2364  index++;
2365  }
2366  if (coup->ch_select[c] != 2)
2367  apply_coupling_method(ac, &cc->ch[1], cce, index++);
2368  } else
2369  index += 1 + (coup->ch_select[c] == 3);
2370  }
2371  }
2372  }
2373 }
2374 
2375 /**
2376  * Convert spectral data to float samples, applying all supported tools as appropriate.
2377  */
2379 {
2380  int i, type;
2381  for (type = 3; type >= 0; type--) {
2382  for (i = 0; i < MAX_ELEM_ID; i++) {
2383  ChannelElement *che = ac->che[type][i];
2384  if (che) {
2385  if (type <= TYPE_CPE)
2387  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2388  if (che->ch[0].ics.predictor_present) {
2389  if (che->ch[0].ics.ltp.present)
2390  ac->apply_ltp(ac, &che->ch[0]);
2391  if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
2392  ac->apply_ltp(ac, &che->ch[1]);
2393  }
2394  }
2395  if (che->ch[0].tns.present)
2396  ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
2397  if (che->ch[1].tns.present)
2398  ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
2399  if (type <= TYPE_CPE)
2401  if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2402  ac->imdct_and_windowing(ac, &che->ch[0]);
2403  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2404  ac->update_ltp(ac, &che->ch[0]);
2405  if (type == TYPE_CPE) {
2406  ac->imdct_and_windowing(ac, &che->ch[1]);
2407  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2408  ac->update_ltp(ac, &che->ch[1]);
2409  }
2410  if (ac->oc[1].m4ac.sbr > 0) {
2411  ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
2412  }
2413  }
2414  if (type <= TYPE_CCE)
2416  }
2417  }
2418  }
2419 }
2420 
2422 {
2423  int size;
2424  AACADTSHeaderInfo hdr_info;
2425  uint8_t layout_map[MAX_ELEM_ID*4][3];
2426  int layout_map_tags;
2427 
2428  size = avpriv_aac_parse_header(gb, &hdr_info);
2429  if (size > 0) {
2430  if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
2431  // This is 2 for "VLB " audio in NSV files.
2432  // See samples/nsv/vlb_audio.
2434  "More than one AAC RDB per ADTS frame");
2435  ac->warned_num_aac_frames = 1;
2436  }
2438  if (hdr_info.chan_config) {
2439  ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2440  if (set_default_channel_config(ac->avctx, layout_map,
2441  &layout_map_tags, hdr_info.chan_config))
2442  return -7;
2443  if (output_configure(ac, layout_map, layout_map_tags,
2444  FFMAX(ac->oc[1].status, OC_TRIAL_FRAME), 0))
2445  return -7;
2446  } else {
2447  ac->oc[1].m4ac.chan_config = 0;
2448  /**
2449  * dual mono frames in Japanese DTV can have chan_config 0
2450  * WITHOUT specifying PCE.
2451  * thus, set dual mono as default.
2452  */
2453  if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
2454  layout_map_tags = 2;
2455  layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
2456  layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
2457  layout_map[0][1] = 0;
2458  layout_map[1][1] = 1;
2459  if (output_configure(ac, layout_map, layout_map_tags,
2460  OC_TRIAL_FRAME, 0))
2461  return -7;
2462  }
2463  }
2464  ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2465  ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2466  ac->oc[1].m4ac.object_type = hdr_info.object_type;
2467  if (ac->oc[0].status != OC_LOCKED ||
2468  ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2469  ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2470  ac->oc[1].m4ac.sbr = -1;
2471  ac->oc[1].m4ac.ps = -1;
2472  }
2473  if (!hdr_info.crc_absent)
2474  skip_bits(gb, 16);
2475  }
2476  return size;
2477 }
2478 
2479 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
2480  int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
2481 {
2482  AACContext *ac = avctx->priv_data;
2483  ChannelElement *che = NULL, *che_prev = NULL;
2484  enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
2485  int err, elem_id;
2486  int samples = 0, multiplier, audio_found = 0, pce_found = 0;
2487  int is_dmono, sce_count = 0;
2488 
2489  ac->frame = data;
2490 
2491  if (show_bits(gb, 12) == 0xfff) {
2492  if (parse_adts_frame_header(ac, gb) < 0) {
2493  av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2494  err = -1;
2495  goto fail;
2496  }
2497  if (ac->oc[1].m4ac.sampling_index > 12) {
2498  av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2499  err = -1;
2500  goto fail;
2501  }
2502  }
2503 
2504  if (frame_configure_elements(avctx) < 0) {
2505  err = -1;
2506  goto fail;
2507  }
2508 
2509  ac->tags_mapped = 0;
2510  // parse
2511  while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2512  elem_id = get_bits(gb, 4);
2513 
2514  if (elem_type < TYPE_DSE) {
2515  if (!(che=get_che(ac, elem_type, elem_id))) {
2516  av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2517  elem_type, elem_id);
2518  err = -1;
2519  goto fail;
2520  }
2521  samples = 1024;
2522  }
2523 
2524  switch (elem_type) {
2525 
2526  case TYPE_SCE:
2527  err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2528  audio_found = 1;
2529  sce_count++;
2530  break;
2531 
2532  case TYPE_CPE:
2533  err = decode_cpe(ac, gb, che);
2534  audio_found = 1;
2535  break;
2536 
2537  case TYPE_CCE:
2538  err = decode_cce(ac, gb, che);
2539  break;
2540 
2541  case TYPE_LFE:
2542  err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2543  audio_found = 1;
2544  break;
2545 
2546  case TYPE_DSE:
2547  err = skip_data_stream_element(ac, gb);
2548  break;
2549 
2550  case TYPE_PCE: {
2551  uint8_t layout_map[MAX_ELEM_ID*4][3];
2552  int tags;
2554  tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
2555  if (tags < 0) {
2556  err = tags;
2557  break;
2558  }
2559  if (pce_found) {
2560  av_log(avctx, AV_LOG_ERROR,
2561  "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2562  } else {
2563  err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
2564  if (!err)
2565  ac->oc[1].m4ac.chan_config = 0;
2566  pce_found = 1;
2567  }
2568  break;
2569  }
2570 
2571  case TYPE_FIL:
2572  if (elem_id == 15)
2573  elem_id += get_bits(gb, 8) - 1;
2574  if (get_bits_left(gb) < 8 * elem_id) {
2575  av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
2576  err = -1;
2577  goto fail;
2578  }
2579  while (elem_id > 0)
2580  elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
2581  err = 0; /* FIXME */
2582  break;
2583 
2584  default:
2585  err = -1; /* should not happen, but keeps compiler happy */
2586  break;
2587  }
2588 
2589  che_prev = che;
2590  elem_type_prev = elem_type;
2591 
2592  if (err)
2593  goto fail;
2594 
2595  if (get_bits_left(gb) < 3) {
2596  av_log(avctx, AV_LOG_ERROR, overread_err);
2597  err = -1;
2598  goto fail;
2599  }
2600  }
2601 
2602  spectral_to_sample(ac);
2603 
2604  multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
2605  samples <<= multiplier;
2606  /* for dual-mono audio (SCE + SCE) */
2607  is_dmono = ac->dmono_mode && sce_count == 2 &&
2609 
2610  if (samples)
2611  ac->frame->nb_samples = samples;
2612  *got_frame_ptr = !!samples;
2613 
2614  if (is_dmono) {
2615  if (ac->dmono_mode == 1)
2616  ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
2617  else if (ac->dmono_mode == 2)
2618  ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
2619  }
2620 
2621  if (ac->oc[1].status && audio_found) {
2622  avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
2623  avctx->frame_size = samples;
2624  ac->oc[1].status = OC_LOCKED;
2625  }
2626 
2627  if (multiplier) {
2628  int side_size;
2629  const uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2630  if (side && side_size>=4)
2631  AV_WL32(side, 2*AV_RL32(side));
2632  }
2633  return 0;
2634 fail:
2636  return err;
2637 }
2638 
2639 static int aac_decode_frame(AVCodecContext *avctx, void *data,
2640  int *got_frame_ptr, AVPacket *avpkt)
2641 {
2642  AACContext *ac = avctx->priv_data;
2643  const uint8_t *buf = avpkt->data;
2644  int buf_size = avpkt->size;
2645  GetBitContext gb;
2646  int buf_consumed;
2647  int buf_offset;
2648  int err;
2649  int new_extradata_size;
2650  const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
2652  &new_extradata_size);
2653  int jp_dualmono_size;
2654  const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
2656  &jp_dualmono_size);
2657 
2658  if (new_extradata && 0) {
2659  av_free(avctx->extradata);
2660  avctx->extradata = av_mallocz(new_extradata_size +
2662  if (!avctx->extradata)
2663  return AVERROR(ENOMEM);
2664  avctx->extradata_size = new_extradata_size;
2665  memcpy(avctx->extradata, new_extradata, new_extradata_size);
2667  if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
2668  avctx->extradata,
2669  avctx->extradata_size*8, 1) < 0) {
2671  return AVERROR_INVALIDDATA;
2672  }
2673  }
2674 
2675  ac->dmono_mode = 0;
2676  if (jp_dualmono && jp_dualmono_size > 0)
2677  ac->dmono_mode = 1 + *jp_dualmono;
2678  if (ac->force_dmono_mode >= 0)
2679  ac->dmono_mode = ac->force_dmono_mode;
2680 
2681  if (INT_MAX / 8 <= buf_size)
2682  return AVERROR_INVALIDDATA;
2683 
2684  init_get_bits(&gb, buf, buf_size * 8);
2685 
2686  if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt)) < 0)
2687  return err;
2688 
2689  buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2690  for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2691  if (buf[buf_offset])
2692  break;
2693 
2694  return buf_size > buf_offset ? buf_consumed : buf_size;
2695 }
2696 
2698 {
2699  AACContext *ac = avctx->priv_data;
2700  int i, type;
2701 
2702  for (i = 0; i < MAX_ELEM_ID; i++) {
2703  for (type = 0; type < 4; type++) {
2704  if (ac->che[type][i])
2705  ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
2706  av_freep(&ac->che[type][i]);
2707  }
2708  }
2709 
2710  ff_mdct_end(&ac->mdct);
2711  ff_mdct_end(&ac->mdct_small);
2712  ff_mdct_end(&ac->mdct_ltp);
2713  return 0;
2714 }
2715 
2716 
2717 #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
2718 
2719 struct LATMContext {
2720  AACContext aac_ctx; ///< containing AACContext
2721  int initialized; ///< initialized after a valid extradata was seen
2722 
2723  // parser data
2724  int audio_mux_version_A; ///< LATM syntax version
2725  int frame_length_type; ///< 0/1 variable/fixed frame length
2726  int frame_length; ///< frame length for fixed frame length
2727 };
2728 
2729 static inline uint32_t latm_get_value(GetBitContext *b)
2730 {
2731  int length = get_bits(b, 2);
2732 
2733  return get_bits_long(b, (length+1)*8);
2734 }
2735 
2737  GetBitContext *gb, int asclen)
2738 {
2739  AACContext *ac = &latmctx->aac_ctx;
2740  AVCodecContext *avctx = ac->avctx;
2741  MPEG4AudioConfig m4ac = { 0 };
2742  int config_start_bit = get_bits_count(gb);
2743  int sync_extension = 0;
2744  int bits_consumed, esize;
2745 
2746  if (asclen) {
2747  sync_extension = 1;
2748  asclen = FFMIN(asclen, get_bits_left(gb));
2749  } else
2750  asclen = get_bits_left(gb);
2751 
2752  if (config_start_bit % 8) {
2754  "Non-byte-aligned audio-specific config");
2755  return AVERROR_PATCHWELCOME;
2756  }
2757  if (asclen <= 0)
2758  return AVERROR_INVALIDDATA;
2759  bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
2760  gb->buffer + (config_start_bit / 8),
2761  asclen, sync_extension);
2762 
2763  if (bits_consumed < 0)
2764  return AVERROR_INVALIDDATA;
2765 
2766  if (!latmctx->initialized ||
2767  ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
2768  ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
2769 
2770  if(latmctx->initialized) {
2771  av_log(avctx, AV_LOG_INFO, "audio config changed\n");
2772  } else {
2773  av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
2774  }
2775  latmctx->initialized = 0;
2776 
2777  esize = (bits_consumed+7) / 8;
2778 
2779  if (avctx->extradata_size < esize) {
2780  av_free(avctx->extradata);
2782  if (!avctx->extradata)
2783  return AVERROR(ENOMEM);
2784  }
2785 
2786  avctx->extradata_size = esize;
2787  memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
2788  memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2789  }
2790  skip_bits_long(gb, bits_consumed);
2791 
2792  return bits_consumed;
2793 }
2794 
2795 static int read_stream_mux_config(struct LATMContext *latmctx,
2796  GetBitContext *gb)
2797 {
2798  int ret, audio_mux_version = get_bits(gb, 1);
2799 
2800  latmctx->audio_mux_version_A = 0;
2801  if (audio_mux_version)
2802  latmctx->audio_mux_version_A = get_bits(gb, 1);
2803 
2804  if (!latmctx->audio_mux_version_A) {
2805 
2806  if (audio_mux_version)
2807  latm_get_value(gb); // taraFullness
2808 
2809  skip_bits(gb, 1); // allStreamSameTimeFraming
2810  skip_bits(gb, 6); // numSubFrames
2811  // numPrograms
2812  if (get_bits(gb, 4)) { // numPrograms
2813  avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
2814  return AVERROR_PATCHWELCOME;
2815  }
2816 
2817  // for each program (which there is only one in DVB)
2818 
2819  // for each layer (which there is only one in DVB)
2820  if (get_bits(gb, 3)) { // numLayer
2821  avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
2822  return AVERROR_PATCHWELCOME;
2823  }
2824 
2825  // for all but first stream: use_same_config = get_bits(gb, 1);
2826  if (!audio_mux_version) {
2827  if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
2828  return ret;
2829  } else {
2830  int ascLen = latm_get_value(gb);
2831  if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
2832  return ret;
2833  ascLen -= ret;
2834  skip_bits_long(gb, ascLen);
2835  }
2836 
2837  latmctx->frame_length_type = get_bits(gb, 3);
2838  switch (latmctx->frame_length_type) {
2839  case 0:
2840  skip_bits(gb, 8); // latmBufferFullness
2841  break;
2842  case 1:
2843  latmctx->frame_length = get_bits(gb, 9);
2844  break;
2845  case 3:
2846  case 4:
2847  case 5:
2848  skip_bits(gb, 6); // CELP frame length table index
2849  break;
2850  case 6:
2851  case 7:
2852  skip_bits(gb, 1); // HVXC frame length table index
2853  break;
2854  }
2855 
2856  if (get_bits(gb, 1)) { // other data
2857  if (audio_mux_version) {
2858  latm_get_value(gb); // other_data_bits
2859  } else {
2860  int esc;
2861  do {
2862  esc = get_bits(gb, 1);
2863  skip_bits(gb, 8);
2864  } while (esc);
2865  }
2866  }
2867 
2868  if (get_bits(gb, 1)) // crc present
2869  skip_bits(gb, 8); // config_crc
2870  }
2871 
2872  return 0;
2873 }
2874 
2876 {
2877  uint8_t tmp;
2878 
2879  if (ctx->frame_length_type == 0) {
2880  int mux_slot_length = 0;
2881  do {
2882  tmp = get_bits(gb, 8);
2883  mux_slot_length += tmp;
2884  } while (tmp == 255);
2885  return mux_slot_length;
2886  } else if (ctx->frame_length_type == 1) {
2887  return ctx->frame_length;
2888  } else if (ctx->frame_length_type == 3 ||
2889  ctx->frame_length_type == 5 ||
2890  ctx->frame_length_type == 7) {
2891  skip_bits(gb, 2); // mux_slot_length_coded
2892  }
2893  return 0;
2894 }
2895 
2896 static int read_audio_mux_element(struct LATMContext *latmctx,
2897  GetBitContext *gb)
2898 {
2899  int err;
2900  uint8_t use_same_mux = get_bits(gb, 1);
2901  if (!use_same_mux) {
2902  if ((err = read_stream_mux_config(latmctx, gb)) < 0)
2903  return err;
2904  } else if (!latmctx->aac_ctx.avctx->extradata) {
2905  av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
2906  "no decoder config found\n");
2907  return AVERROR(EAGAIN);
2908  }
2909  if (latmctx->audio_mux_version_A == 0) {
2910  int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
2911  if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
2912  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
2913  return AVERROR_INVALIDDATA;
2914  } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
2915  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2916  "frame length mismatch %d << %d\n",
2917  mux_slot_length_bytes * 8, get_bits_left(gb));
2918  return AVERROR_INVALIDDATA;
2919  }
2920  }
2921  return 0;
2922 }
2923 
2924 
2925 static int latm_decode_frame(AVCodecContext *avctx, void *out,
2926  int *got_frame_ptr, AVPacket *avpkt)
2927 {
2928  struct LATMContext *latmctx = avctx->priv_data;
2929  int muxlength, err;
2930  GetBitContext gb;
2931 
2932  if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
2933  return err;
2934 
2935  // check for LOAS sync word
2936  if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
2937  return AVERROR_INVALIDDATA;
2938 
2939  muxlength = get_bits(&gb, 13) + 3;
2940  // not enough data, the parser should have sorted this out
2941  if (muxlength > avpkt->size)
2942  return AVERROR_INVALIDDATA;
2943 
2944  if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
2945  return err;
2946 
2947  if (!latmctx->initialized) {
2948  if (!avctx->extradata) {
2949  *got_frame_ptr = 0;
2950  return avpkt->size;
2951  } else {
2953  if ((err = decode_audio_specific_config(
2954  &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
2955  avctx->extradata, avctx->extradata_size*8, 1)) < 0) {
2956  pop_output_configuration(&latmctx->aac_ctx);
2957  return err;
2958  }
2959  latmctx->initialized = 1;
2960  }
2961  }
2962 
2963  if (show_bits(&gb, 12) == 0xfff) {
2964  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2965  "ADTS header detected, probably as result of configuration "
2966  "misparsing\n");
2967  return AVERROR_INVALIDDATA;
2968  }
2969 
2970  if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt)) < 0)
2971  return err;
2972 
2973  return muxlength;
2974 }
2975 
2977 {
2978  struct LATMContext *latmctx = avctx->priv_data;
2979  int ret = aac_decode_init(avctx);
2980 
2981  if (avctx->extradata_size > 0)
2982  latmctx->initialized = !ret;
2983 
2984  return ret;
2985 }
2986 
2987 static void aacdec_init(AACContext *c)
2988 {
2990  c->apply_ltp = apply_ltp;
2991  c->apply_tns = apply_tns;
2993  c->update_ltp = update_ltp;
2994 
2995  if(ARCH_MIPS)
2997 }
2998 /**
2999  * AVOptions for Japanese DTV specific extensions (ADTS only)
3000  */
3001 #define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
3002 static const AVOption options[] = {
3003  {"dual_mono_mode", "Select the channel to decode for dual mono",
3004  offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
3005  AACDEC_FLAGS, "dual_mono_mode"},
3006 
3007  {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3008  {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3009  {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3010  {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3011 
3012  {NULL},
3013 };
3014 
3015 static const AVClass aac_decoder_class = {
3016  .class_name = "AAC decoder",
3017  .item_name = av_default_item_name,
3018  .option = options,
3019  .version = LIBAVUTIL_VERSION_INT,
3020 };
3021 
3023  .name = "aac",
3024  .type = AVMEDIA_TYPE_AUDIO,
3025  .id = AV_CODEC_ID_AAC,
3026  .priv_data_size = sizeof(AACContext),
3027  .init = aac_decode_init,
3030  .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
3031  .sample_fmts = (const enum AVSampleFormat[]) {
3033  },
3034  .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
3035  .channel_layouts = aac_channel_layout,
3036  .flush = flush,
3037  .priv_class = &aac_decoder_class,
3038 };
3039 
3040 /*
3041  Note: This decoder filter is intended to decode LATM streams transferred
3042  in MPEG transport streams which only contain one program.
3043  To do a more complex LATM demuxing a separate LATM demuxer should be used.
3044 */
3046  .name = "aac_latm",
3047  .type = AVMEDIA_TYPE_AUDIO,
3048  .id = AV_CODEC_ID_AAC_LATM,
3049  .priv_data_size = sizeof(struct LATMContext),
3050  .init = latm_decode_init,
3051  .close = aac_decode_close,
3052  .decode = latm_decode_frame,
3053  .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Coding LATM syntax)"),
3054  .sample_fmts = (const enum AVSampleFormat[]) {
3056  },
3057  .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
3058  .channel_layouts = aac_channel_layout,
3059  .flush = flush,
3060 };
int predictor_initialized
Definition: aac.h:170
static int decode_fill(AACContext *ac, GetBitContext *gb, int len)
static int output_configure(AACContext *ac, uint8_t layout_map[MAX_ELEM_ID *4][3], int tags, enum OCStatus oc_type, int get_new_frame)
Configure output channel order based on the current program configuration element.
static float * VMUL4S(float *dst, const float *v, unsigned idx, unsigned sign, const float *scale)
float(* scalarproduct_float)(const float *v1, const float *v2, int len)
Calculate the scalar product of two vectors of floats.
Definition: float_dsp.h:161
AAC decoder data.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
float v
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Definition: aac.h:53
static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
Conduct IMDCT and windowing.
uint8_t use_kb_window[2]
If set, use Kaiser-Bessel window, otherwise use a sinus window.
Definition: aac.h:160
static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
#define overread_err
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static int decode_audio_specific_config(AACContext *ac, AVCodecContext *avctx, MPEG4AudioConfig *m4ac, const uint8_t *data, int bit_size, int sync_extension)
Decode audio specific configuration; reference: table 1.13.
uint8_t object_type
Definition: aacadtsdec.h:36
AVOption.
Definition: opt.h:251
static const int8_t tags_per_config[16]
Definition: aacdectab.h:81
AVCodecContext * avctx
Definition: aac.h:264
Definition: aac.h:203
enum AVCodecID id
Definition: mxfenc.c:89
static void push_output_configuration(AACContext *ac)
Save current output configuration if and only if it has been locked.
av_default_item_name
static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
intensity stereo decoding; reference: 4.6.8.2.3
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
av_cold void ff_kbd_window_init(float *window, float alpha, int n)
Generate a Kaiser-Bessel Derived Window.
Definition: kbdwin.c:26
else temp
Definition: vf_mcdeint.c:148
Definition: aac.h:56
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static float * VMUL2S(float *dst, const float *v, unsigned idx, unsigned sign, const float *scale)
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
Definition: aac.h:49
Definition: aac.h:50
ChannelElement * che[4][MAX_ELEM_ID]
Definition: aac.h:274
av_cold void ff_aac_sbr_init(void)
Initialize SBR.
Definition: aacsbr.c:96
static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
Decode Individual Channel Stream info; reference: table 4.6.
float cor1
Definition: aac.h:129
Sinusoidal phase f
const uint8_t * buffer
Definition: get_bits.h:55
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
uint64_t channel_layout
Definition: aac.h:120
#define AACDEC_FLAGS
AVOptions for Japanese DTV specific extensions (ADTS only)
#define VLC_TYPE
Definition: get_bits.h:61
static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID], uint8_t(*layout_map)[3], int offset, uint64_t left, uint64_t right, int pos)
#define FF_ARRAY_ELEMS(a)
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
Definition: aac.h:251
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static void apply_independent_coupling(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Apply independent channel coupling (applied after IMDCT).
static int frame_configure_elements(AVCodecContext *avctx)
#define MAX_LTP_LONG_SFB
Definition: aac.h:46
static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac, uint8_t(*layout_map)[3], GetBitContext *gb)
Decode program configuration element; reference: table 4.2.
Dynamic Range Control - decoded from the bitstream but not processed further.
Definition: aac.h:190
float coef[8][4][TNS_MAX_ORDER]
Definition: aac.h:184
Reference: libavcodec/aacdec.c.
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
enum RawDataBlockType type[8]
Type of channel element to be coupled - SCE or CPE.
Definition: aac.h:216
struct AACContext AACContext
ChannelPosition
Definition: aac.h:86
Spectral data are scaled white noise not coded in the bitstream.
Definition: aac.h:79
Definition: aac.h:51
output residual component w
static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024], GetBitContext *gb, const float sf[120], int pulse_present, const Pulse *pulse, const IndividualChannelStream *ics, enum BandType band_type[120])
Decode spectral data; reference: table 4.50.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
int band_incr
Number of DRC bands greater than 1 having DRC info.
Definition: aac.h:195
const uint8_t ff_aac_num_swb_128[]
Definition: aactab.c:43
av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
Close one SBR context.
Definition: aacsbr.c:167
void(* apply_tns)(float coef[1024], TemporalNoiseShaping *tns, IndividualChannelStream *ics, int decode)
Definition: aac.h:323
int dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition: aac.h:312
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Definition: aac.h:164
N Error Resilient Long Term Prediction.
Definition: mpeg4audio.h:75
#define AV_WL32(p, darg)
Definition: intreadwrite.h:282
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:55
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
Definition: aac.h:60
BandType
Definition: aac.h:75
uint8_t bits
Definition: crc.c:216
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
Apply AAC-Main style frequency domain prediction.
static const uint8_t aac_channel_layout_map[7][5][3]
Definition: aacdectab.h:83
#define av_cold
Definition: attributes.h:78
uint8_t layout_map[MAX_ELEM_ID *4][3]
Definition: aac.h:117
AVOptions.
window constants for m
Output configuration under trial specified by an inband PCE.
Definition: aac.h:109
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
Definition: get_bits.h:445
SingleChannelElement ch[2]
Definition: aac.h:253
Definition: aac.h:52
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the product of two vectors of floats and store the result in a vector of floats...
Definition: float_dsp.h:38
TemporalNoiseShaping tns
Definition: aac.h:229
#define b
Definition: input.c:42
N Error Resilient Low Delay.
Definition: mpeg4audio.h:79
end end
static int aac_decode_frame_int(AVCodecContext *avctx, void *data, int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
static VLC vlc_scalefactors
const uint8_t ff_aac_scalefactor_bits[121]
Definition: aactab.c:70
CouplingPoint
The point during decoding at which channel coupling is applied.
Definition: aac.h:98
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int num_coupled
number of target elements
Definition: aac.h:215
#define AV_CH_LOW_FREQUENCY
int exclude_mask[MAX_CHANNELS]
Channels to be excluded from DRC processing.
Definition: aac.h:194
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb_host, int crc, int cnt, int id_aac)
Decode Spectral Band Replication extension data; reference: table 4.55.
Definition: aacsbr.c:1076
int n_filt[8]
Definition: aac.h:180
FFTContext mdct_ltp
Definition: aac.h:293
static int decode_band_types(AACContext *ac, enum BandType band_type[120], int band_type_run_end[120], GetBitContext *gb, IndividualChannelStream *ics)
Decode band types (section_data payload); reference: table 4.46.
SingleChannelElement * output_element[MAX_CHANNELS]
Points to each SingleChannelElement.
Definition: aac.h:303
static int decode_pulses(Pulse *pulse, GetBitContext *gb, const uint16_t *swb_offset, int num_swb)
Decode pulse data; reference: table 4.7.
static int count_paired_channels(uint8_t(*layout_map)[3], int tags, int pos, int *current)
uint8_t * data
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
Scalefactor data are intensity stereo positions.
Definition: aac.h:81
bitstream reader API header.
static int read_stream_mux_config(struct LATMContext *latmctx, GetBitContext *gb)
end end ac
#define AV_CH_BACK_LEFT
static av_cold int che_configure(AACContext *ac, enum ChannelPosition che_pos, int type, int id, int *channels)
Check for the channel element in the current channel position configuration.
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
int id_select[8]
element id
Definition: aac.h:217
const float *const ff_aac_codebook_vector_vals[]
Definition: aactab.c:1052
static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext *gb)
Decode dynamic range information; reference: table 4.52.
N Error Resilient Low Complexity.
Definition: mpeg4audio.h:74
ChannelElement * tag_che_map[4][MAX_ELEM_ID]
Definition: aac.h:275
Output configuration set in a global header but not yet locked.
Definition: aac.h:111
float, planar
Definition: samplefmt.h:60
AACContext aac_ctx
containing AACContext
static void apply_channel_coupling(AACContext *ac, ChannelElement *cc, enum RawDataBlockType type, int elem_id, enum CouplingPoint coupling_point, void(*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
channel coupling transformation interface
static uint32_t latm_get_value(GetBitContext *b)
int random_state
Definition: aac.h:296
float var1
Definition: aac.h:131
static av_cold int aac_decode_close(AVCodecContext *avctx)
#define U(x)
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:557
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
Definition: float_dsp.h:103
MPEG4AudioConfig m4ac
Definition: aac.h:116
int dyn_rng_sgn[17]
DRC sign information; 0 - positive, 1 - negative.
Definition: aac.h:192
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
static double alpha(void *priv, double x, double y)
Definition: vf_geq.c:86
PredictorState predictor_state[MAX_PREDICTORS]
Definition: aac.h:240
AVCodec ff_aac_decoder
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt, ChannelElement *che, enum RawDataBlockType elem_type)
Decode extension data (incomplete); reference: table 4.51.
SpectralBandReplication sbr
Definition: aac.h:256
FFTContext mdct_small
Definition: aac.h:292
#define AV_EF_EXPLODE
enum CouplingPoint coupling_point
The point during decoding at which coupling is applied.
Definition: aac.h:214
int frame_length_type
0/1 variable/fixed frame length
#define pv
Definition: regdef.h:60
const uint8_t ff_aac_num_swb_1024[]
Definition: aactab.c:39
FmtConvertContext fmt_conv
Definition: aac.h:294
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
int flags
CODEC_FLAG_*.
Spectral Band Replication definitions and structures.
void(* mdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:83
uint8_t sampling_index
Definition: aacadtsdec.h:37
int amp[4]
Definition: aac.h:207
void(* apply_ltp)(AACContext *ac, SingleChannelElement *sce)
Definition: aac.h:322
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
uint8_t max_sfb
number of scalefactor bands per group
Definition: aac.h:158
static int latm_decode_frame(AVCodecContext *avctx, void *out, int *got_frame_ptr, AVPacket *avpkt)
void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac, float *L, float *R)
Apply one SBR element to one AAC element.
Definition: aacsbr.c:1681
#define ff_mdct_init
Definition: fft.h:147
#define LOAS_SYNC_WORD
11 bits LOAS sync word
AVCodec ff_aac_latm_decoder
Definition: aac.h:55
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
#define CLOSE_READER(name, gb)
Definition: get_bits.h:140
int num_swb
number of scalefactor window bands
Definition: aac.h:166
#define FFMAX(a, b)
Definition: common.h:56
external API header
#define AAC_INIT_VLC_STATIC(num, size)
int prog_ref_level
A reference level for the long-term program audio level for all channels combined.
Definition: aac.h:198
int size
Output configuration locked in place.
Definition: aac.h:112
Predictor State.
Definition: aac.h:127
uint8_t chan_config
Definition: aacadtsdec.h:38
Definition: get_bits.h:63
uint64_t channel_layout
Audio channel layout.
#define powf(x, y)
Definition: libm.h:48
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:175
#define POW_SF2_ZERO
ff_aac_pow2sf_tab index corresponding to pow(2, 0);
static const float cce_scale[]
AVFloatDSPContext fdsp
Definition: aac.h:295
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
N Error Resilient Scalable.
Definition: mpeg4audio.h:76
static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
AAC Spectral Band Replication function declarations.
FFT buffer for g
Definition: stft_peak.m:17
enum WindowSequence window_sequence[2]
Definition: aac.h:159
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
static void pop_output_configuration(AACContext *ac)
Restore the previous output configuration if and only if the current configuration is unlocked...
int predictor_reset_group
Definition: aac.h:171
#define FFMIN(a, b)
Definition: common.h:58
static void reset_predictor_group(PredictorState *ps, int group_num)
int dyn_rng_ctl[17]
DRC magnitude information.
Definition: aac.h:193
ret
Definition: avfilter.c:821
uint8_t num_aac_frames
Definition: aacadtsdec.h:39
int pos[4]
Definition: aac.h:206
int initialized
initialized after a valid extradata was seen
t
Definition: genspecsines3.m:6
static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb, int ms_present)
Decode Mid/Side data; reference: table 4.54.
Y Main.
Definition: mpeg4audio.h:60
float var0
Definition: aac.h:130
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:255
int channels
channel count
Definition: aacenc.h:67
void ff_aacdec_init_mips(AACContext *c)
Definition: aacdec_mips.c:822
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:181
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:524
uint32_t i
Definition: intfloat.h:28
int length[8][4]
Definition: aac.h:181
#define AV_RL32
AAC definitions and structures.
#define AV_CH_FRONT_LEFT_OF_CENTER
float u
const uint8_t ff_tns_max_bands_1024[]
Definition: aactab.c:1200
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
Definition: get_bits.h:458
static void cbrt_tableinit(void)
Definition: cbrt_tablegen.h:35
#define AV_CH_FRONT_CENTER
static void apply_dependent_coupling(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Apply dependent channel coupling (applied before IMDCT).
int pce_instance_tag
Indicates with which program the DRC info is associated.
Definition: aac.h:191
static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, GetBitContext *gb)
Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4...
N Scalable.
Definition: mpeg4audio.h:65
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:187
#define AV_CH_FRONT_RIGHT_OF_CENTER
static void flush(AVCodecContext *avctx)
int interpolation_scheme
Indicates the interpolation scheme used in the SBR QMF domain.
Definition: aac.h:196
coupling parameters
Definition: aac.h:213
int tags_mapped
Definition: aac.h:276
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx, GetBitContext *gb, MPEG4AudioConfig *m4ac, int channel_config)
Decode GA "General Audio" specific configuration; reference: table 4.1.
int ch_select[8]
[0] shared list of gains; [1] list of gains for right channel; [2] list of gains for left channel; [3...
Definition: aac.h:218
float coef
Definition: aac.h:150
for k
static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
Mid/Side stereo decoding; reference: 4.6.8.1.3.
int frame_size
Number of samples per channel in an audio frame.
int frame_length
frame length for fixed frame length
int force_dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition: aac.h:311
NULL
Definition: eval.c:55
or the Software in violation of any applicable export control laws in any jurisdiction Except as provided by mandatorily applicable UPF has no obligation to provide you with source code to the Software In the event Software contains any source code
int order[8][4]
Definition: aac.h:183
dest
Definition: start.py:60
int warned_num_aac_frames
Definition: aac.h:318
AVS_Value src
Definition: avisynth_c.h:523
int audio_mux_version_A
LATM syntax version.
Temporal Noise Shaping.
Definition: aac.h:178
int sample_rate
samples per second
static int compute_lpc_coefs(const LPC_TYPE *autoc, int max_order, LPC_TYPE *lpc, int lpc_stride, int fail, int normalize)
Levinson-Durbin recursion.
Definition: lpc.h:151
static uint32_t cbrt_tab[1<< 13]
Definition: cbrt_tablegen.h:33
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:410
#define AV_CH_LAYOUT_NATIVE
Channel mask value used for AVCodecContext.request_channel_layout to indicate that the user requests ...
static void update_ltp(AACContext *ac, SingleChannelElement *sce)
Update the LTP buffer for next frame.
Long Term Prediction.
Definition: aac.h:147
main external API structure.
#define AV_CH_FRONT_LEFT
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
static int decode_ics(AACContext *ac, SingleChannelElement *sce, GetBitContext *gb, int common_window, int scale_flag)
Decode an individual_channel_stream payload; reference: table 4.44.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
#define OPEN_READER(name, gb)
Definition: get_bits.h:126
IndividualChannelStream ics
Definition: aac.h:228
void(* butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
Definition: float_dsp.h:150
int avpriv_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
Parse AAC frame header.
Definition: aacadtsdec.c:29
void * buf
Definition: avisynth_c.h:594
#define MAX_PREDICTORS
Definition: aac.h:136
static av_always_inline float cbrtf(float x)
Definition: libm.h:59
uint8_t group_len[8]
Definition: aac.h:162
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:298
#define MAX_ELEM_ID
Definition: aac.h:43
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
Describe the class of an AVClass context structure.
Definition: log.h:50
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
int index
Definition: gxfenc.c:89
static av_cold int latm_decode_init(AVCodecContext *avctx)
static void spectral_to_sample(AACContext *ac)
Convert spectral data to float samples, applying all supported tools as appropriate.
synthesis window for stochastic i
static const AVClass aac_decoder_class
static av_always_inline int lcg_random(unsigned previous_val)
linear congruential pseudorandom number generator
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
Recommmends skipping the specified number of samples.
#define GET_CACHE(name, gb)
Definition: get_bits.h:191
static int read_audio_mux_element(struct LATMContext *latmctx, GetBitContext *gb)
static int latm_decode_audio_specific_config(struct LATMContext *latmctx, GetBitContext *gb, int asclen)
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:69
static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
Apply the long term prediction.
static float * VMUL2(float *dst, const float *v, unsigned idx, const float *scale)
OCStatus
Output configuration status.
Definition: aac.h:107
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
#define MAX_CHANNELS
Definition: aac.h:42
N Error Resilient Bit-Sliced Arithmetic Coding.
Definition: mpeg4audio.h:78
float * ret
PCM output.
Definition: aac.h:241
#define ARCH_MIPS
Definition: config.h:23
#define TNS_MAX_ORDER
Definition: aac.h:45
av_cold void ff_aac_sbr_ctx_init(AACContext *ac, SpectralBandReplication *sbr)
Initialize one SBR context.
Definition: aacsbr.c:149
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
main AAC context
Definition: aac.h:262
static void reset_all_predictors(PredictorState *ps)
const uint32_t ff_aac_scalefactor_code[121]
Definition: aactab.c:51
LongTermPrediction ltp
Definition: aac.h:163
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:306
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
#define type
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:82
ChannelCoupling coup
Definition: aac.h:255
float gain[16][120]
Definition: aac.h:221
Output configuration under trial specified by a frame header.
Definition: aac.h:110
const uint8_t ff_tns_max_bands_128[]
Definition: aactab.c:1204
static const uint64_t aac_channel_layout[8]
Definition: aacdectab.h:93
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static const int8_t filt[NUMTAPS]
Definition: af_earwax.c:39
int band_type_run_end[120]
band type run end points
Definition: aac.h:232
float sf[120]
scalefactors
Definition: aac.h:233
#define AV_CH_BACK_CENTER
int band_top[17]
Indicates the top of the i-th DRC band in units of 4 spectral lines.
Definition: aac.h:197
static void aacdec_init(AACContext *ac)
#define AV_CH_SIDE_RIGHT
#define FF_DEBUG_PICT_INFO
static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns, GetBitContext *gb, const IndividualChannelStream *ics)
Decode Temporal Noise Shaping data; reference: table 4.48.
enum OCStatus status
Definition: aac.h:121
Scalefactor data are intensity stereo positions.
Definition: aac.h:80
static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
Decode a channel_pair_element; reference: table 4.4.
#define M_SQRT2
Definition: mathematics.h:52
int16_t lag
Definition: aac.h:149
DynamicRangeControl che_drc
Definition: aac.h:268
static av_always_inline void reset_predict_state(PredictorState *ps)
AVFrame * frame
Definition: aac.h:265
OutputConfiguration oc[2]
Definition: aac.h:317
An AV_PKT_DATA_JP_DUALMONO side data packet indicates that the packet may contain "dual mono" audio s...
const uint8_t ff_aac_pred_sfb_max[]
Definition: aactab.c:47
int direction[8][4]
Definition: aac.h:182
uint8_t prediction_used[41]
Definition: aac.h:172
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
Single Channel Element - used for both SCE and LFE elements.
Definition: aac.h:227
#define CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
#define ff_mdct_end
Definition: fft.h:148
static double c[64]
const uint16_t *const ff_swb_offset_1024[]
Definition: aactab.c:1174
static const AVOption options[]
static av_cold int aac_decode_init(AVCodecContext *avctx)
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
Definition: aac.h:54
Individual Channel Stream.
Definition: aac.h:157
float ff_aac_pow2sf_tab[428]
Definition: aac_tablegen.h:32
Same thing on a dB scale
static ChannelElement * get_che(AACContext *ac, int type, int elem_id)
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int bit_size, int sync_extension)
Parse MPEG-4 systems extradata to retrieve audio configuration.
Definition: mpeg4audio.c:81
static const float ltp_coef[8]
Definition: aacdectab.h:41
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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
const uint16_t *const ff_aac_codebook_vector_idx[]
Definition: aactab.c:1061
static void windowing_and_mdct_ltp(AACContext *ac, float *out, float *in, IndividualChannelStream *ics)
Apply windowing and MDCT to obtain the spectral coefficient from the predicted sample by LTP...
void(* update_ltp)(AACContext *ac, SingleChannelElement *sce)
Definition: aac.h:327
channel element - generic struct for SCE/CPE/CCE/LFE
Definition: aac.h:247
float re
Definition: fft-test.c:64
float r1
Definition: aac.h:133
static uint64_t sniff_channel_order(uint8_t(*layout_map)[3], int tags)
av_cold void ff_fmt_convert_init(FmtConvertContext *c, AVCodecContext *avctx)
Definition: fmtconvert.c:79
int len
Scalefactors and spectral data are all zero.
Definition: aac.h:76
int channels
number of audio channels
int num_pulse
Definition: aac.h:204
#define av_log2
Definition: intmath.h:89
struct AVCodecInternal * internal
Private context used for internal data.
const uint8_t ff_mpeg4audio_channels[8]
Definition: mpeg4audio.c:62
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb, unsigned int global_gain, IndividualChannelStream *ics, enum BandType band_type[120], int band_type_run_end[120])
Decode scalefactors; reference: table 4.47.
void(* windowing_and_mdct_ltp)(AACContext *ac, float *out, float *in, IndividualChannelStream *ics)
Definition: aac.h:325
Y Long Term Prediction.
Definition: mpeg4audio.h:63
float cor0
Definition: aac.h:128
uint8_t crc_absent
Definition: aacadtsdec.h:35
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:418
enum BandType band_type[128]
band types
Definition: aac.h:231
static int set_default_channel_config(AVCodecContext *avctx, uint8_t(*layout_map)[3], int *tags, int channel_config)
Set up channel positions based on a default channel configuration as specified in table 1...
static int sample_rate_idx(int rate)
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...
#define AV_CH_FRONT_RIGHT
static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
Skip data_stream_element; reference: table 4.10.
#define AV_LOG_INFO
Definition: log.h:156
void ff_aac_tableinit(void)
Definition: aac_tablegen.h:34
Filter the word “frame” indicates either a video frame or a group of audio samples
FFTContext mdct
Definition: aac.h:291
int sbr
-1 implicit, 1 presence
Definition: mpeg4audio.h:34
void INT64 INT64 count
Definition: avisynth_c.h:594
void INT64 start
Definition: avisynth_c.h:594
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:289
static int decode_prediction(AACContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
#define av_always_inline
Definition: attributes.h:41
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
static void apply_tns(float coef[1024], TemporalNoiseShaping *tns, IndividualChannelStream *ics, int decode)
Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4...
static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
Decode coupling_channel_element; reference: table 4.8.
void(* imdct_and_windowing)(AACContext *ac, SingleChannelElement *sce)
Definition: aac.h:321
float r0
Definition: aac.h:132
static int aac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
#define AV_CH_SIDE_LEFT
#define FFSWAP(type, a, b)
Definition: common.h:61
int ps
-1 implicit, 1 presence
Definition: mpeg4audio.h:40
const char int length
Definition: avisynth_c.h:668
int8_t used[MAX_LTP_LONG_SFB]
Definition: aac.h:151
void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:118
static av_always_inline float flt16_trunc(float pf)
const uint16_t *const ff_swb_offset_128[]
Definition: aactab.c:1182
static av_always_inline float flt16_even(float pf)
static const float *const tns_tmp2_map[4]
Definition: aacdectab.h:73
int8_t present
Definition: aac.h:148
uint32_t sample_rate
Definition: aacadtsdec.h:32
Definition: aac.h:99
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
AAC data declarations.
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
int layout_map_tags
Definition: aac.h:118
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
static VLC vlc_spectral[11]
static int count_channels(uint8_t(*layout)[3], int tags)
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
static av_always_inline float flt16_round(float pf)
static void decode_ltp(LongTermPrediction *ltp, GetBitContext *gb, uint8_t max_sfb)
Decode Long Term Prediction data; reference: table 4.xx.
#define AV_CH_BACK_RIGHT
Y Low Complexity.
Definition: mpeg4audio.h:61
static float * VMUL4(float *dst, const float *v, unsigned idx, const float *scale)
Output unconfigured.
Definition: aac.h:108
RawDataBlockType
Definition: aac.h:48
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the product of two vectors of floats, and store the result in a vector of floats...
Definition: float_dsp.h:140