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