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