aacdec.c
Go to the documentation of this file.
1 /*
2  * AAC decoder
3  * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4  * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
5  *
6  * AAC LATM decoder
7  * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
8  * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
9  *
10  * This file is part of Libav.
11  *
12  * Libav is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * Libav is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with Libav; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 
34 /*
35  * supported tools
36  *
37  * Support? Name
38  * N (code in SoC repo) gain control
39  * Y block switching
40  * Y window shapes - standard
41  * N window shapes - Low Delay
42  * Y filterbank - standard
43  * N (code in SoC repo) filterbank - Scalable Sample Rate
44  * Y Temporal Noise Shaping
45  * Y Long Term Prediction
46  * Y intensity stereo
47  * Y channel coupling
48  * Y frequency domain prediction
49  * Y Perceptual Noise Substitution
50  * Y Mid/Side stereo
51  * N Scalable Inverse AAC Quantization
52  * N Frequency Selective Switch
53  * N upsampling filter
54  * Y quantization & coding - AAC
55  * N quantization & coding - TwinVQ
56  * N quantization & coding - BSAC
57  * N AAC Error Resilience tools
58  * N Error Resilience payload syntax
59  * N Error Protection tool
60  * N CELP
61  * N Silence Compression
62  * N HVXC
63  * N HVXC 4kbits/s VR
64  * N Structured Audio tools
65  * N Structured Audio Sample Bank Format
66  * N MIDI
67  * N Harmonic and Individual Lines plus Noise
68  * N Text-To-Speech Interface
69  * Y Spectral Band Replication
70  * Y (not in this code) Layer-1
71  * Y (not in this code) Layer-2
72  * Y (not in this code) Layer-3
73  * N SinuSoidal Coding (Transient, Sinusoid, Noise)
74  * Y Parametric Stereo
75  * N Direct Stream Transfer
76  *
77  * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
78  * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
79  Parametric Stereo.
80  */
81 
82 #include "libavutil/float_dsp.h"
83 #include "avcodec.h"
84 #include "internal.h"
85 #include "get_bits.h"
86 #include "dsputil.h"
87 #include "fft.h"
88 #include "fmtconvert.h"
89 #include "lpc.h"
90 #include "kbdwin.h"
91 #include "sinewin.h"
92 
93 #include "aac.h"
94 #include "aactab.h"
95 #include "aacdectab.h"
96 #include "cbrt_tablegen.h"
97 #include "sbr.h"
98 #include "aacsbr.h"
99 #include "mpeg4audio.h"
100 #include "aacadtsdec.h"
101 #include "libavutil/intfloat.h"
102 
103 #include <assert.h>
104 #include <errno.h>
105 #include <math.h>
106 #include <string.h>
107 
108 #if ARCH_ARM
109 # include "arm/aac.h"
110 #endif
111 
113 static VLC vlc_spectral[11];
114 
115 static const char overread_err[] = "Input buffer exhausted before END element found\n";
116 
117 static int count_channels(uint8_t (*layout)[3], int tags)
118 {
119  int i, sum = 0;
120  for (i = 0; i < tags; i++) {
121  int syn_ele = layout[i][0];
122  int pos = layout[i][2];
123  sum += (1 + (syn_ele == TYPE_CPE)) *
124  (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
125  }
126  return sum;
127 }
128 
142  enum ChannelPosition che_pos,
143  int type, int id, int *channels)
144 {
145  if (*channels >= MAX_CHANNELS)
146  return AVERROR_INVALIDDATA;
147  if (che_pos) {
148  if (!ac->che[type][id]) {
149  if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
150  return AVERROR(ENOMEM);
151  ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr);
152  }
153  if (type != TYPE_CCE) {
154  ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
155  if (type == TYPE_CPE ||
156  (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
157  ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
158  }
159  }
160  } else {
161  if (ac->che[type][id])
162  ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
163  av_freep(&ac->che[type][id]);
164  }
165  return 0;
166 }
167 
169 {
170  AACContext *ac = avctx->priv_data;
171  int type, id, ch, ret;
172 
173  /* set channel pointers to internal buffers by default */
174  for (type = 0; type < 4; type++) {
175  for (id = 0; id < MAX_ELEM_ID; id++) {
176  ChannelElement *che = ac->che[type][id];
177  if (che) {
178  che->ch[0].ret = che->ch[0].ret_buf;
179  che->ch[1].ret = che->ch[1].ret_buf;
180  }
181  }
182  }
183 
184  /* get output buffer */
185  ac->frame.nb_samples = 2048;
186  if ((ret = ff_get_buffer(avctx, &ac->frame)) < 0) {
187  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
188  return ret;
189  }
190 
191  /* map output channel pointers to AVFrame data */
192  for (ch = 0; ch < avctx->channels; ch++) {
193  if (ac->output_element[ch])
194  ac->output_element[ch]->ret = (float *)ac->frame.extended_data[ch];
195  }
196 
197  return 0;
198 }
199 
201  uint64_t av_position;
205 };
206 
207 static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
208  uint8_t (*layout_map)[3], int offset, uint64_t left,
209  uint64_t right, int pos)
210 {
211  if (layout_map[offset][0] == TYPE_CPE) {
212  e2c_vec[offset] = (struct elem_to_channel) {
213  .av_position = left | right,
214  .syn_ele = TYPE_CPE,
215  .elem_id = layout_map[offset][1],
216  .aac_position = pos
217  };
218  return 1;
219  } else {
220  e2c_vec[offset] = (struct elem_to_channel) {
221  .av_position = left,
222  .syn_ele = TYPE_SCE,
223  .elem_id = layout_map[offset][1],
224  .aac_position = pos
225  };
226  e2c_vec[offset + 1] = (struct elem_to_channel) {
227  .av_position = right,
228  .syn_ele = TYPE_SCE,
229  .elem_id = layout_map[offset + 1][1],
230  .aac_position = pos
231  };
232  return 2;
233  }
234 }
235 
236 static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
237  int *current)
238 {
239  int num_pos_channels = 0;
240  int first_cpe = 0;
241  int sce_parity = 0;
242  int i;
243  for (i = *current; i < tags; i++) {
244  if (layout_map[i][2] != pos)
245  break;
246  if (layout_map[i][0] == TYPE_CPE) {
247  if (sce_parity) {
248  if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
249  sce_parity = 0;
250  } else {
251  return -1;
252  }
253  }
254  num_pos_channels += 2;
255  first_cpe = 1;
256  } else {
257  num_pos_channels++;
258  sce_parity ^= 1;
259  }
260  }
261  if (sce_parity &&
262  ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
263  return -1;
264  *current = i;
265  return num_pos_channels;
266 }
267 
268 static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
269 {
270  int i, n, total_non_cc_elements;
271  struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
272  int num_front_channels, num_side_channels, num_back_channels;
273  uint64_t layout;
274 
275  if (FF_ARRAY_ELEMS(e2c_vec) < tags)
276  return 0;
277 
278  i = 0;
279  num_front_channels =
280  count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
281  if (num_front_channels < 0)
282  return 0;
283  num_side_channels =
284  count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
285  if (num_side_channels < 0)
286  return 0;
287  num_back_channels =
288  count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
289  if (num_back_channels < 0)
290  return 0;
291 
292  i = 0;
293  if (num_front_channels & 1) {
294  e2c_vec[i] = (struct elem_to_channel) {
296  .syn_ele = TYPE_SCE,
297  .elem_id = layout_map[i][1],
298  .aac_position = AAC_CHANNEL_FRONT
299  };
300  i++;
301  num_front_channels--;
302  }
303  if (num_front_channels >= 4) {
304  i += assign_pair(e2c_vec, layout_map, i,
308  num_front_channels -= 2;
309  }
310  if (num_front_channels >= 2) {
311  i += assign_pair(e2c_vec, layout_map, i,
315  num_front_channels -= 2;
316  }
317  while (num_front_channels >= 2) {
318  i += assign_pair(e2c_vec, layout_map, i,
319  UINT64_MAX,
320  UINT64_MAX,
322  num_front_channels -= 2;
323  }
324 
325  if (num_side_channels >= 2) {
326  i += assign_pair(e2c_vec, layout_map, i,
330  num_side_channels -= 2;
331  }
332  while (num_side_channels >= 2) {
333  i += assign_pair(e2c_vec, layout_map, i,
334  UINT64_MAX,
335  UINT64_MAX,
337  num_side_channels -= 2;
338  }
339 
340  while (num_back_channels >= 4) {
341  i += assign_pair(e2c_vec, layout_map, i,
342  UINT64_MAX,
343  UINT64_MAX,
345  num_back_channels -= 2;
346  }
347  if (num_back_channels >= 2) {
348  i += assign_pair(e2c_vec, layout_map, i,
352  num_back_channels -= 2;
353  }
354  if (num_back_channels) {
355  e2c_vec[i] = (struct elem_to_channel) {
357  .syn_ele = TYPE_SCE,
358  .elem_id = layout_map[i][1],
359  .aac_position = AAC_CHANNEL_BACK
360  };
361  i++;
362  num_back_channels--;
363  }
364 
365  if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
366  e2c_vec[i] = (struct elem_to_channel) {
368  .syn_ele = TYPE_LFE,
369  .elem_id = layout_map[i][1],
370  .aac_position = AAC_CHANNEL_LFE
371  };
372  i++;
373  }
374  while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
375  e2c_vec[i] = (struct elem_to_channel) {
376  .av_position = UINT64_MAX,
377  .syn_ele = TYPE_LFE,
378  .elem_id = layout_map[i][1],
379  .aac_position = AAC_CHANNEL_LFE
380  };
381  i++;
382  }
383 
384  // Must choose a stable sort
385  total_non_cc_elements = n = i;
386  do {
387  int next_n = 0;
388  for (i = 1; i < n; i++)
389  if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
390  FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
391  next_n = i;
392  }
393  n = next_n;
394  } while (n > 0);
395 
396  layout = 0;
397  for (i = 0; i < total_non_cc_elements; i++) {
398  layout_map[i][0] = e2c_vec[i].syn_ele;
399  layout_map[i][1] = e2c_vec[i].elem_id;
400  layout_map[i][2] = e2c_vec[i].aac_position;
401  if (e2c_vec[i].av_position != UINT64_MAX) {
402  layout |= e2c_vec[i].av_position;
403  }
404  }
405 
406  return layout;
407 }
408 
413  if (ac->oc[1].status == OC_LOCKED) {
414  ac->oc[0] = ac->oc[1];
415  }
416  ac->oc[1].status = OC_NONE;
417 }
418 
424  if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
425  ac->oc[1] = ac->oc[0];
426  ac->avctx->channels = ac->oc[1].channels;
427  ac->avctx->channel_layout = ac->oc[1].channel_layout;
428  }
429 }
430 
438  uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
439  enum OCStatus oc_type, int get_new_frame)
440 {
441  AVCodecContext *avctx = ac->avctx;
442  int i, channels = 0, ret;
443  uint64_t layout = 0;
444 
445  if (ac->oc[1].layout_map != layout_map) {
446  memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
447  ac->oc[1].layout_map_tags = tags;
448  }
449 
450  // Try to sniff a reasonable channel order, otherwise output the
451  // channels in the order the PCE declared them.
453  layout = sniff_channel_order(layout_map, tags);
454  for (i = 0; i < tags; i++) {
455  int type = layout_map[i][0];
456  int id = layout_map[i][1];
457  int position = layout_map[i][2];
458  // Allocate or free elements depending on if they are in the
459  // current program configuration.
460  ret = che_configure(ac, position, type, id, &channels);
461  if (ret < 0)
462  return ret;
463  }
464  if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
465  if (layout == AV_CH_FRONT_CENTER) {
467  } else {
468  layout = 0;
469  }
470  }
471 
472  memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
473  avctx->channel_layout = ac->oc[1].channel_layout = layout;
474  avctx->channels = ac->oc[1].channels = channels;
475  ac->oc[1].status = oc_type;
476 
477  if (get_new_frame) {
478  if ((ret = frame_configure_elements(ac->avctx)) < 0)
479  return ret;
480  }
481 
482  return 0;
483 }
484 
492  uint8_t (*layout_map)[3],
493  int *tags,
494  int channel_config)
495 {
496  if (channel_config < 1 || channel_config > 7) {
497  av_log(avctx, AV_LOG_ERROR,
498  "invalid default channel configuration (%d)\n",
499  channel_config);
500  return AVERROR_INVALIDDATA;
501  }
502  *tags = tags_per_config[channel_config];
503  memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
504  *tags * sizeof(*layout_map));
505  return 0;
506 }
507 
508 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
509 {
510  /* For PCE based channel configurations map the channels solely based
511  * on tags. */
512  if (!ac->oc[1].m4ac.chan_config) {
513  return ac->tag_che_map[type][elem_id];
514  }
515  // Allow single CPE stereo files to be signalled with mono configuration.
516  if (!ac->tags_mapped && type == TYPE_CPE &&
517  ac->oc[1].m4ac.chan_config == 1) {
518  uint8_t layout_map[MAX_ELEM_ID*4][3];
519  int layout_map_tags;
521 
522  if (set_default_channel_config(ac->avctx, layout_map,
523  &layout_map_tags, 2) < 0)
524  return NULL;
525  if (output_configure(ac, layout_map, layout_map_tags,
526  OC_TRIAL_FRAME, 1) < 0)
527  return NULL;
528 
529  ac->oc[1].m4ac.chan_config = 2;
530  ac->oc[1].m4ac.ps = 0;
531  }
532  // And vice-versa
533  if (!ac->tags_mapped && type == TYPE_SCE &&
534  ac->oc[1].m4ac.chan_config == 2) {
535  uint8_t layout_map[MAX_ELEM_ID * 4][3];
536  int layout_map_tags;
538 
539  if (set_default_channel_config(ac->avctx, layout_map,
540  &layout_map_tags, 1) < 0)
541  return NULL;
542  if (output_configure(ac, layout_map, layout_map_tags,
543  OC_TRIAL_FRAME, 1) < 0)
544  return NULL;
545 
546  ac->oc[1].m4ac.chan_config = 1;
547  if (ac->oc[1].m4ac.sbr)
548  ac->oc[1].m4ac.ps = -1;
549  }
550  /* For indexed channel configurations map the channels solely based
551  * on position. */
552  switch (ac->oc[1].m4ac.chan_config) {
553  case 7:
554  if (ac->tags_mapped == 3 && type == TYPE_CPE) {
555  ac->tags_mapped++;
556  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
557  }
558  case 6:
559  /* Some streams incorrectly code 5.1 audio as
560  * SCE[0] CPE[0] CPE[1] SCE[1]
561  * instead of
562  * SCE[0] CPE[0] CPE[1] LFE[0].
563  * If we seem to have encountered such a stream, transfer
564  * the LFE[0] element to the SCE[1]'s mapping */
565  if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
566  ac->tags_mapped++;
567  return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
568  }
569  case 5:
570  if (ac->tags_mapped == 2 && type == TYPE_CPE) {
571  ac->tags_mapped++;
572  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
573  }
574  case 4:
575  if (ac->tags_mapped == 2 &&
576  ac->oc[1].m4ac.chan_config == 4 &&
577  type == TYPE_SCE) {
578  ac->tags_mapped++;
579  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
580  }
581  case 3:
582  case 2:
583  if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
584  type == TYPE_CPE) {
585  ac->tags_mapped++;
586  return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
587  } else if (ac->oc[1].m4ac.chan_config == 2) {
588  return NULL;
589  }
590  case 1:
591  if (!ac->tags_mapped && type == TYPE_SCE) {
592  ac->tags_mapped++;
593  return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
594  }
595  default:
596  return NULL;
597  }
598 }
599 
606 static void decode_channel_map(uint8_t layout_map[][3],
607  enum ChannelPosition type,
608  GetBitContext *gb, int n)
609 {
610  while (n--) {
611  enum RawDataBlockType syn_ele;
612  switch (type) {
613  case AAC_CHANNEL_FRONT:
614  case AAC_CHANNEL_BACK:
615  case AAC_CHANNEL_SIDE:
616  syn_ele = get_bits1(gb);
617  break;
618  case AAC_CHANNEL_CC:
619  skip_bits1(gb);
620  syn_ele = TYPE_CCE;
621  break;
622  case AAC_CHANNEL_LFE:
623  syn_ele = TYPE_LFE;
624  break;
625  }
626  layout_map[0][0] = syn_ele;
627  layout_map[0][1] = get_bits(gb, 4);
628  layout_map[0][2] = type;
629  layout_map++;
630  }
631 }
632 
638 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
639  uint8_t (*layout_map)[3],
640  GetBitContext *gb)
641 {
642  int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
643  int sampling_index;
644  int comment_len;
645  int tags;
646 
647  skip_bits(gb, 2); // object_type
648 
649  sampling_index = get_bits(gb, 4);
650  if (m4ac->sampling_index != sampling_index)
651  av_log(avctx, AV_LOG_WARNING,
652  "Sample rate index in program config element does not "
653  "match the sample rate index configured by the container.\n");
654 
655  num_front = get_bits(gb, 4);
656  num_side = get_bits(gb, 4);
657  num_back = get_bits(gb, 4);
658  num_lfe = get_bits(gb, 2);
659  num_assoc_data = get_bits(gb, 3);
660  num_cc = get_bits(gb, 4);
661 
662  if (get_bits1(gb))
663  skip_bits(gb, 4); // mono_mixdown_tag
664  if (get_bits1(gb))
665  skip_bits(gb, 4); // stereo_mixdown_tag
666 
667  if (get_bits1(gb))
668  skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
669 
670  decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
671  tags = num_front;
672  decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
673  tags += num_side;
674  decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
675  tags += num_back;
676  decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
677  tags += num_lfe;
678 
679  skip_bits_long(gb, 4 * num_assoc_data);
680 
681  decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
682  tags += num_cc;
683 
684  align_get_bits(gb);
685 
686  /* comment field, first byte is length */
687  comment_len = get_bits(gb, 8) * 8;
688  if (get_bits_left(gb) < comment_len) {
690  return AVERROR_INVALIDDATA;
691  }
692  skip_bits_long(gb, comment_len);
693  return tags;
694 }
695 
705  GetBitContext *gb,
706  MPEG4AudioConfig *m4ac,
707  int channel_config)
708 {
709  int extension_flag, ret;
710  uint8_t layout_map[MAX_ELEM_ID*4][3];
711  int tags = 0;
712 
713  if (get_bits1(gb)) { // frameLengthFlag
714  av_log_missing_feature(avctx, "960/120 MDCT window", 1);
715  return AVERROR_PATCHWELCOME;
716  }
717 
718  if (get_bits1(gb)) // dependsOnCoreCoder
719  skip_bits(gb, 14); // coreCoderDelay
720  extension_flag = get_bits1(gb);
721 
722  if (m4ac->object_type == AOT_AAC_SCALABLE ||
724  skip_bits(gb, 3); // layerNr
725 
726  if (channel_config == 0) {
727  skip_bits(gb, 4); // element_instance_tag
728  tags = decode_pce(avctx, m4ac, layout_map, gb);
729  if (tags < 0)
730  return tags;
731  } else {
732  if ((ret = set_default_channel_config(avctx, layout_map,
733  &tags, channel_config)))
734  return ret;
735  }
736 
737  if (count_channels(layout_map, tags) > 1) {
738  m4ac->ps = 0;
739  } else if (m4ac->sbr == 1 && m4ac->ps == -1)
740  m4ac->ps = 1;
741 
742  if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
743  return ret;
744 
745  if (extension_flag) {
746  switch (m4ac->object_type) {
747  case AOT_ER_BSAC:
748  skip_bits(gb, 5); // numOfSubFrame
749  skip_bits(gb, 11); // layer_length
750  break;
751  case AOT_ER_AAC_LC:
752  case AOT_ER_AAC_LTP:
753  case AOT_ER_AAC_SCALABLE:
754  case AOT_ER_AAC_LD:
755  skip_bits(gb, 3); /* aacSectionDataResilienceFlag
756  * aacScalefactorDataResilienceFlag
757  * aacSpectralDataResilienceFlag
758  */
759  break;
760  }
761  skip_bits1(gb); // extensionFlag3 (TBD in version 3)
762  }
763  return 0;
764 }
765 
779  AVCodecContext *avctx,
780  MPEG4AudioConfig *m4ac,
781  const uint8_t *data, int bit_size,
782  int sync_extension)
783 {
784  GetBitContext gb;
785  int i, ret;
786 
787  av_dlog(avctx, "extradata size %d\n", avctx->extradata_size);
788  for (i = 0; i < avctx->extradata_size; i++)
789  av_dlog(avctx, "%02x ", avctx->extradata[i]);
790  av_dlog(avctx, "\n");
791 
792  if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
793  return ret;
794 
795  if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size,
796  sync_extension)) < 0)
797  return AVERROR_INVALIDDATA;
798  if (m4ac->sampling_index > 12) {
799  av_log(avctx, AV_LOG_ERROR,
800  "invalid sampling rate index %d\n",
801  m4ac->sampling_index);
802  return AVERROR_INVALIDDATA;
803  }
804 
805  skip_bits_long(&gb, i);
806 
807  switch (m4ac->object_type) {
808  case AOT_AAC_MAIN:
809  case AOT_AAC_LC:
810  case AOT_AAC_LTP:
811  if ((ret = decode_ga_specific_config(ac, avctx, &gb,
812  m4ac, m4ac->chan_config)) < 0)
813  return ret;
814  break;
815  default:
816  av_log(avctx, AV_LOG_ERROR,
817  "Audio object type %s%d is not supported.\n",
818  m4ac->sbr == 1 ? "SBR+" : "",
819  m4ac->object_type);
820  return AVERROR(ENOSYS);
821  }
822 
823  av_dlog(avctx,
824  "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
825  m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
826  m4ac->sample_rate, m4ac->sbr,
827  m4ac->ps);
828 
829  return get_bits_count(&gb);
830 }
831 
839 static av_always_inline int lcg_random(int previous_val)
840 {
841  union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
842  return v.s;
843 }
844 
846 {
847  ps->r0 = 0.0f;
848  ps->r1 = 0.0f;
849  ps->cor0 = 0.0f;
850  ps->cor1 = 0.0f;
851  ps->var0 = 1.0f;
852  ps->var1 = 1.0f;
853 }
854 
856 {
857  int i;
858  for (i = 0; i < MAX_PREDICTORS; i++)
859  reset_predict_state(&ps[i]);
860 }
861 
862 static int sample_rate_idx (int rate)
863 {
864  if (92017 <= rate) return 0;
865  else if (75132 <= rate) return 1;
866  else if (55426 <= rate) return 2;
867  else if (46009 <= rate) return 3;
868  else if (37566 <= rate) return 4;
869  else if (27713 <= rate) return 5;
870  else if (23004 <= rate) return 6;
871  else if (18783 <= rate) return 7;
872  else if (13856 <= rate) return 8;
873  else if (11502 <= rate) return 9;
874  else if (9391 <= rate) return 10;
875  else return 11;
876 }
877 
878 static void reset_predictor_group(PredictorState *ps, int group_num)
879 {
880  int i;
881  for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
882  reset_predict_state(&ps[i]);
883 }
884 
885 #define AAC_INIT_VLC_STATIC(num, size) \
886  INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
887  ff_aac_spectral_bits[num], sizeof(ff_aac_spectral_bits[num][0]), \
888  sizeof(ff_aac_spectral_bits[num][0]), \
889  ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), \
890  sizeof(ff_aac_spectral_codes[num][0]), \
891  size);
892 
894 {
895  AACContext *ac = avctx->priv_data;
896  int ret;
897 
898  ac->avctx = avctx;
899  ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
900 
902 
903  if (avctx->extradata_size > 0) {
904  if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
905  avctx->extradata,
906  avctx->extradata_size * 8,
907  1)) < 0)
908  return ret;
909  } else {
910  int sr, i;
911  uint8_t layout_map[MAX_ELEM_ID*4][3];
912  int layout_map_tags;
913 
914  sr = sample_rate_idx(avctx->sample_rate);
915  ac->oc[1].m4ac.sampling_index = sr;
916  ac->oc[1].m4ac.channels = avctx->channels;
917  ac->oc[1].m4ac.sbr = -1;
918  ac->oc[1].m4ac.ps = -1;
919 
920  for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
921  if (ff_mpeg4audio_channels[i] == avctx->channels)
922  break;
923  if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
924  i = 0;
925  }
926  ac->oc[1].m4ac.chan_config = i;
927 
928  if (ac->oc[1].m4ac.chan_config) {
929  int ret = set_default_channel_config(avctx, layout_map,
930  &layout_map_tags, ac->oc[1].m4ac.chan_config);
931  if (!ret)
932  output_configure(ac, layout_map, layout_map_tags,
933  OC_GLOBAL_HDR, 0);
934  else if (avctx->err_recognition & AV_EF_EXPLODE)
935  return AVERROR_INVALIDDATA;
936  }
937  }
938 
939  AAC_INIT_VLC_STATIC( 0, 304);
940  AAC_INIT_VLC_STATIC( 1, 270);
941  AAC_INIT_VLC_STATIC( 2, 550);
942  AAC_INIT_VLC_STATIC( 3, 300);
943  AAC_INIT_VLC_STATIC( 4, 328);
944  AAC_INIT_VLC_STATIC( 5, 294);
945  AAC_INIT_VLC_STATIC( 6, 306);
946  AAC_INIT_VLC_STATIC( 7, 268);
947  AAC_INIT_VLC_STATIC( 8, 510);
948  AAC_INIT_VLC_STATIC( 9, 366);
949  AAC_INIT_VLC_STATIC(10, 462);
950 
951  ff_aac_sbr_init();
952 
953  ff_dsputil_init(&ac->dsp, avctx);
954  ff_fmt_convert_init(&ac->fmt_conv, avctx);
956 
957  ac->random_state = 0x1f2e3d4c;
958 
960 
961  INIT_VLC_STATIC(&vlc_scalefactors, 7,
964  sizeof(ff_aac_scalefactor_bits[0]),
965  sizeof(ff_aac_scalefactor_bits[0]),
967  sizeof(ff_aac_scalefactor_code[0]),
968  sizeof(ff_aac_scalefactor_code[0]),
969  352);
970 
971  ff_mdct_init(&ac->mdct, 11, 1, 1.0 / (32768.0 * 1024.0));
972  ff_mdct_init(&ac->mdct_small, 8, 1, 1.0 / (32768.0 * 128.0));
973  ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0 * 32768.0);
974  // window initialization
979 
980  cbrt_tableinit();
981 
983  avctx->coded_frame = &ac->frame;
984 
985  return 0;
986 }
987 
992 {
993  int byte_align = get_bits1(gb);
994  int count = get_bits(gb, 8);
995  if (count == 255)
996  count += get_bits(gb, 8);
997  if (byte_align)
998  align_get_bits(gb);
999 
1000  if (get_bits_left(gb) < 8 * count) {
1002  return AVERROR_INVALIDDATA;
1003  }
1004  skip_bits_long(gb, 8 * count);
1005  return 0;
1006 }
1007 
1009  GetBitContext *gb)
1010 {
1011  int sfb;
1012  if (get_bits1(gb)) {
1013  ics->predictor_reset_group = get_bits(gb, 5);
1014  if (ics->predictor_reset_group == 0 ||
1015  ics->predictor_reset_group > 30) {
1016  av_log(ac->avctx, AV_LOG_ERROR,
1017  "Invalid Predictor Reset Group.\n");
1018  return AVERROR_INVALIDDATA;
1019  }
1020  }
1021  for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1022  ics->prediction_used[sfb] = get_bits1(gb);
1023  }
1024  return 0;
1025 }
1026 
1031  GetBitContext *gb, uint8_t max_sfb)
1032 {
1033  int sfb;
1034 
1035  ltp->lag = get_bits(gb, 11);
1036  ltp->coef = ltp_coef[get_bits(gb, 3)];
1037  for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
1038  ltp->used[sfb] = get_bits1(gb);
1039 }
1040 
1045  GetBitContext *gb)
1046 {
1047  if (get_bits1(gb)) {
1048  av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1049  return AVERROR_INVALIDDATA;
1050  }
1051  ics->window_sequence[1] = ics->window_sequence[0];
1052  ics->window_sequence[0] = get_bits(gb, 2);
1053  ics->use_kb_window[1] = ics->use_kb_window[0];
1054  ics->use_kb_window[0] = get_bits1(gb);
1055  ics->num_window_groups = 1;
1056  ics->group_len[0] = 1;
1057  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1058  int i;
1059  ics->max_sfb = get_bits(gb, 4);
1060  for (i = 0; i < 7; i++) {
1061  if (get_bits1(gb)) {
1062  ics->group_len[ics->num_window_groups - 1]++;
1063  } else {
1064  ics->num_window_groups++;
1065  ics->group_len[ics->num_window_groups - 1] = 1;
1066  }
1067  }
1068  ics->num_windows = 8;
1072  ics->predictor_present = 0;
1073  } else {
1074  ics->max_sfb = get_bits(gb, 6);
1075  ics->num_windows = 1;
1079  ics->predictor_present = get_bits1(gb);
1080  ics->predictor_reset_group = 0;
1081  if (ics->predictor_present) {
1082  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1083  if (decode_prediction(ac, ics, gb)) {
1084  return AVERROR_INVALIDDATA;
1085  }
1086  } else if (ac->oc[1].m4ac.object_type == AOT_AAC_LC) {
1087  av_log(ac->avctx, AV_LOG_ERROR,
1088  "Prediction is not allowed in AAC-LC.\n");
1089  return AVERROR_INVALIDDATA;
1090  } else {
1091  if ((ics->ltp.present = get_bits(gb, 1)))
1092  decode_ltp(&ics->ltp, gb, ics->max_sfb);
1093  }
1094  }
1095  }
1096 
1097  if (ics->max_sfb > ics->num_swb) {
1098  av_log(ac->avctx, AV_LOG_ERROR,
1099  "Number of scalefactor bands in group (%d) "
1100  "exceeds limit (%d).\n",
1101  ics->max_sfb, ics->num_swb);
1102  return AVERROR_INVALIDDATA;
1103  }
1104 
1105  return 0;
1106 }
1107 
1116 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
1117  int band_type_run_end[120], GetBitContext *gb,
1119 {
1120  int g, idx = 0;
1121  const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1122  for (g = 0; g < ics->num_window_groups; g++) {
1123  int k = 0;
1124  while (k < ics->max_sfb) {
1125  uint8_t sect_end = k;
1126  int sect_len_incr;
1127  int sect_band_type = get_bits(gb, 4);
1128  if (sect_band_type == 12) {
1129  av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1130  return AVERROR_INVALIDDATA;
1131  }
1132  do {
1133  sect_len_incr = get_bits(gb, bits);
1134  sect_end += sect_len_incr;
1135  if (get_bits_left(gb) < 0) {
1137  return AVERROR_INVALIDDATA;
1138  }
1139  if (sect_end > ics->max_sfb) {
1140  av_log(ac->avctx, AV_LOG_ERROR,
1141  "Number of bands (%d) exceeds limit (%d).\n",
1142  sect_end, ics->max_sfb);
1143  return AVERROR_INVALIDDATA;
1144  }
1145  } while (sect_len_incr == (1 << bits) - 1);
1146  for (; k < sect_end; k++) {
1147  band_type [idx] = sect_band_type;
1148  band_type_run_end[idx++] = sect_end;
1149  }
1150  }
1151  }
1152  return 0;
1153 }
1154 
1165 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
1166  unsigned int global_gain,
1168  enum BandType band_type[120],
1169  int band_type_run_end[120])
1170 {
1171  int g, i, idx = 0;
1172  int offset[3] = { global_gain, global_gain - 90, 0 };
1173  int clipped_offset;
1174  int noise_flag = 1;
1175  for (g = 0; g < ics->num_window_groups; g++) {
1176  for (i = 0; i < ics->max_sfb;) {
1177  int run_end = band_type_run_end[idx];
1178  if (band_type[idx] == ZERO_BT) {
1179  for (; i < run_end; i++, idx++)
1180  sf[idx] = 0.;
1181  } else if ((band_type[idx] == INTENSITY_BT) ||
1182  (band_type[idx] == INTENSITY_BT2)) {
1183  for (; i < run_end; i++, idx++) {
1184  offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1185  clipped_offset = av_clip(offset[2], -155, 100);
1186  if (offset[2] != clipped_offset) {
1187  av_log_ask_for_sample(ac->avctx, "Intensity stereo "
1188  "position clipped (%d -> %d).\nIf you heard an "
1189  "audible artifact, there may be a bug in the "
1190  "decoder. ", offset[2], clipped_offset);
1191  }
1192  sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
1193  }
1194  } else if (band_type[idx] == NOISE_BT) {
1195  for (; i < run_end; i++, idx++) {
1196  if (noise_flag-- > 0)
1197  offset[1] += get_bits(gb, 9) - 256;
1198  else
1199  offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1200  clipped_offset = av_clip(offset[1], -100, 155);
1201  if (offset[1] != clipped_offset) {
1202  av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
1203  "(%d -> %d).\nIf you heard an audible "
1204  "artifact, there may be a bug in the decoder. ",
1205  offset[1], clipped_offset);
1206  }
1207  sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
1208  }
1209  } else {
1210  for (; i < run_end; i++, idx++) {
1211  offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1212  if (offset[0] > 255U) {
1213  av_log(ac->avctx, AV_LOG_ERROR,
1214  "Scalefactor (%d) out of range.\n", offset[0]);
1215  return AVERROR_INVALIDDATA;
1216  }
1217  sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
1218  }
1219  }
1220  }
1221  }
1222  return 0;
1223 }
1224 
1228 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1229  const uint16_t *swb_offset, int num_swb)
1230 {
1231  int i, pulse_swb;
1232  pulse->num_pulse = get_bits(gb, 2) + 1;
1233  pulse_swb = get_bits(gb, 6);
1234  if (pulse_swb >= num_swb)
1235  return -1;
1236  pulse->pos[0] = swb_offset[pulse_swb];
1237  pulse->pos[0] += get_bits(gb, 5);
1238  if (pulse->pos[0] > 1023)
1239  return -1;
1240  pulse->amp[0] = get_bits(gb, 4);
1241  for (i = 1; i < pulse->num_pulse; i++) {
1242  pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1243  if (pulse->pos[i] > 1023)
1244  return -1;
1245  pulse->amp[i] = get_bits(gb, 4);
1246  }
1247  return 0;
1248 }
1249 
1256  GetBitContext *gb, const IndividualChannelStream *ics)
1257 {
1258  int w, filt, i, coef_len, coef_res, coef_compress;
1259  const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1260  const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1261  for (w = 0; w < ics->num_windows; w++) {
1262  if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1263  coef_res = get_bits1(gb);
1264 
1265  for (filt = 0; filt < tns->n_filt[w]; filt++) {
1266  int tmp2_idx;
1267  tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1268 
1269  if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
1270  av_log(ac->avctx, AV_LOG_ERROR,
1271  "TNS filter order %d is greater than maximum %d.\n",
1272  tns->order[w][filt], tns_max_order);
1273  tns->order[w][filt] = 0;
1274  return AVERROR_INVALIDDATA;
1275  }
1276  if (tns->order[w][filt]) {
1277  tns->direction[w][filt] = get_bits1(gb);
1278  coef_compress = get_bits1(gb);
1279  coef_len = coef_res + 3 - coef_compress;
1280  tmp2_idx = 2 * coef_compress + coef_res;
1281 
1282  for (i = 0; i < tns->order[w][filt]; i++)
1283  tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1284  }
1285  }
1286  }
1287  }
1288  return 0;
1289 }
1290 
1299  int ms_present)
1300 {
1301  int idx;
1302  if (ms_present == 1) {
1303  for (idx = 0;
1304  idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1305  idx++)
1306  cpe->ms_mask[idx] = get_bits1(gb);
1307  } else if (ms_present == 2) {
1308  memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
1309  }
1310 }
1311 
1312 #ifndef VMUL2
1313 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
1314  const float *scale)
1315 {
1316  float s = *scale;
1317  *dst++ = v[idx & 15] * s;
1318  *dst++ = v[idx>>4 & 15] * s;
1319  return dst;
1320 }
1321 #endif
1322 
1323 #ifndef VMUL4
1324 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
1325  const float *scale)
1326 {
1327  float s = *scale;
1328  *dst++ = v[idx & 3] * s;
1329  *dst++ = v[idx>>2 & 3] * s;
1330  *dst++ = v[idx>>4 & 3] * s;
1331  *dst++ = v[idx>>6 & 3] * s;
1332  return dst;
1333 }
1334 #endif
1335 
1336 #ifndef VMUL2S
1337 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
1338  unsigned sign, const float *scale)
1339 {
1340  union av_intfloat32 s0, s1;
1341 
1342  s0.f = s1.f = *scale;
1343  s0.i ^= sign >> 1 << 31;
1344  s1.i ^= sign << 31;
1345 
1346  *dst++ = v[idx & 15] * s0.f;
1347  *dst++ = v[idx>>4 & 15] * s1.f;
1348 
1349  return dst;
1350 }
1351 #endif
1352 
1353 #ifndef VMUL4S
1354 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
1355  unsigned sign, const float *scale)
1356 {
1357  unsigned nz = idx >> 12;
1358  union av_intfloat32 s = { .f = *scale };
1359  union av_intfloat32 t;
1360 
1361  t.i = s.i ^ (sign & 1U<<31);
1362  *dst++ = v[idx & 3] * t.f;
1363 
1364  sign <<= nz & 1; nz >>= 1;
1365  t.i = s.i ^ (sign & 1U<<31);
1366  *dst++ = v[idx>>2 & 3] * t.f;
1367 
1368  sign <<= nz & 1; nz >>= 1;
1369  t.i = s.i ^ (sign & 1U<<31);
1370  *dst++ = v[idx>>4 & 3] * t.f;
1371 
1372  sign <<= nz & 1;
1373  t.i = s.i ^ (sign & 1U<<31);
1374  *dst++ = v[idx>>6 & 3] * t.f;
1375 
1376  return dst;
1377 }
1378 #endif
1379 
1392 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
1393  GetBitContext *gb, const float sf[120],
1394  int pulse_present, const Pulse *pulse,
1395  const IndividualChannelStream *ics,
1396  enum BandType band_type[120])
1397 {
1398  int i, k, g, idx = 0;
1399  const int c = 1024 / ics->num_windows;
1400  const uint16_t *offsets = ics->swb_offset;
1401  float *coef_base = coef;
1402 
1403  for (g = 0; g < ics->num_windows; g++)
1404  memset(coef + g * 128 + offsets[ics->max_sfb], 0,
1405  sizeof(float) * (c - offsets[ics->max_sfb]));
1406 
1407  for (g = 0; g < ics->num_window_groups; g++) {
1408  unsigned g_len = ics->group_len[g];
1409 
1410  for (i = 0; i < ics->max_sfb; i++, idx++) {
1411  const unsigned cbt_m1 = band_type[idx] - 1;
1412  float *cfo = coef + offsets[i];
1413  int off_len = offsets[i + 1] - offsets[i];
1414  int group;
1415 
1416  if (cbt_m1 >= INTENSITY_BT2 - 1) {
1417  for (group = 0; group < g_len; group++, cfo+=128) {
1418  memset(cfo, 0, off_len * sizeof(float));
1419  }
1420  } else if (cbt_m1 == NOISE_BT - 1) {
1421  for (group = 0; group < g_len; group++, cfo+=128) {
1422  float scale;
1423  float band_energy;
1424 
1425  for (k = 0; k < off_len; k++) {
1427  cfo[k] = ac->random_state;
1428  }
1429 
1430  band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
1431  scale = sf[idx] / sqrtf(band_energy);
1432  ac->fdsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
1433  }
1434  } else {
1435  const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1436  const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
1437  VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1438  OPEN_READER(re, gb);
1439 
1440  switch (cbt_m1 >> 1) {
1441  case 0:
1442  for (group = 0; group < g_len; group++, cfo+=128) {
1443  float *cf = cfo;
1444  int len = off_len;
1445 
1446  do {
1447  int code;
1448  unsigned cb_idx;
1449 
1450  UPDATE_CACHE(re, gb);
1451  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1452  cb_idx = cb_vector_idx[code];
1453  cf = VMUL4(cf, vq, cb_idx, sf + idx);
1454  } while (len -= 4);
1455  }
1456  break;
1457 
1458  case 1:
1459  for (group = 0; group < g_len; group++, cfo+=128) {
1460  float *cf = cfo;
1461  int len = off_len;
1462 
1463  do {
1464  int code;
1465  unsigned nnz;
1466  unsigned cb_idx;
1467  uint32_t bits;
1468 
1469  UPDATE_CACHE(re, gb);
1470  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1471  cb_idx = cb_vector_idx[code];
1472  nnz = cb_idx >> 8 & 15;
1473  bits = nnz ? GET_CACHE(re, gb) : 0;
1474  LAST_SKIP_BITS(re, gb, nnz);
1475  cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1476  } while (len -= 4);
1477  }
1478  break;
1479 
1480  case 2:
1481  for (group = 0; group < g_len; group++, cfo+=128) {
1482  float *cf = cfo;
1483  int len = off_len;
1484 
1485  do {
1486  int code;
1487  unsigned cb_idx;
1488 
1489  UPDATE_CACHE(re, gb);
1490  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1491  cb_idx = cb_vector_idx[code];
1492  cf = VMUL2(cf, vq, cb_idx, sf + idx);
1493  } while (len -= 2);
1494  }
1495  break;
1496 
1497  case 3:
1498  case 4:
1499  for (group = 0; group < g_len; group++, cfo+=128) {
1500  float *cf = cfo;
1501  int len = off_len;
1502 
1503  do {
1504  int code;
1505  unsigned nnz;
1506  unsigned cb_idx;
1507  unsigned sign;
1508 
1509  UPDATE_CACHE(re, gb);
1510  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1511  cb_idx = cb_vector_idx[code];
1512  nnz = cb_idx >> 8 & 15;
1513  sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
1514  LAST_SKIP_BITS(re, gb, nnz);
1515  cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1516  } while (len -= 2);
1517  }
1518  break;
1519 
1520  default:
1521  for (group = 0; group < g_len; group++, cfo+=128) {
1522  float *cf = cfo;
1523  uint32_t *icf = (uint32_t *) cf;
1524  int len = off_len;
1525 
1526  do {
1527  int code;
1528  unsigned nzt, nnz;
1529  unsigned cb_idx;
1530  uint32_t bits;
1531  int j;
1532 
1533  UPDATE_CACHE(re, gb);
1534  GET_VLC(code, re, gb, vlc_tab, 8, 2);
1535 
1536  if (!code) {
1537  *icf++ = 0;
1538  *icf++ = 0;
1539  continue;
1540  }
1541 
1542  cb_idx = cb_vector_idx[code];
1543  nnz = cb_idx >> 12;
1544  nzt = cb_idx >> 8;
1545  bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1546  LAST_SKIP_BITS(re, gb, nnz);
1547 
1548  for (j = 0; j < 2; j++) {
1549  if (nzt & 1<<j) {
1550  uint32_t b;
1551  int n;
1552  /* The total length of escape_sequence must be < 22 bits according
1553  to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1554  UPDATE_CACHE(re, gb);
1555  b = GET_CACHE(re, gb);
1556  b = 31 - av_log2(~b);
1557 
1558  if (b > 8) {
1559  av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1560  return AVERROR_INVALIDDATA;
1561  }
1562 
1563  SKIP_BITS(re, gb, b + 1);
1564  b += 4;
1565  n = (1 << b) + SHOW_UBITS(re, gb, b);
1566  LAST_SKIP_BITS(re, gb, b);
1567  *icf++ = cbrt_tab[n] | (bits & 1U<<31);
1568  bits <<= 1;
1569  } else {
1570  unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1571  *icf++ = (bits & 1U<<31) | v;
1572  bits <<= !!v;
1573  }
1574  cb_idx >>= 4;
1575  }
1576  } while (len -= 2);
1577 
1578  ac->fdsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1579  }
1580  }
1581 
1582  CLOSE_READER(re, gb);
1583  }
1584  }
1585  coef += g_len << 7;
1586  }
1587 
1588  if (pulse_present) {
1589  idx = 0;
1590  for (i = 0; i < pulse->num_pulse; i++) {
1591  float co = coef_base[ pulse->pos[i] ];
1592  while (offsets[idx + 1] <= pulse->pos[i])
1593  idx++;
1594  if (band_type[idx] != NOISE_BT && sf[idx]) {
1595  float ico = -pulse->amp[i];
1596  if (co) {
1597  co /= sf[idx];
1598  ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
1599  }
1600  coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
1601  }
1602  }
1603  }
1604  return 0;
1605 }
1606 
1607 static av_always_inline float flt16_round(float pf)
1608 {
1609  union av_intfloat32 tmp;
1610  tmp.f = pf;
1611  tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
1612  return tmp.f;
1613 }
1614 
1615 static av_always_inline float flt16_even(float pf)
1616 {
1617  union av_intfloat32 tmp;
1618  tmp.f = pf;
1619  tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
1620  return tmp.f;
1621 }
1622 
1623 static av_always_inline float flt16_trunc(float pf)
1624 {
1625  union av_intfloat32 pun;
1626  pun.f = pf;
1627  pun.i &= 0xFFFF0000U;
1628  return pun.f;
1629 }
1630 
1631 static av_always_inline void predict(PredictorState *ps, float *coef,
1632  int output_enable)
1633 {
1634  const float a = 0.953125; // 61.0 / 64
1635  const float alpha = 0.90625; // 29.0 / 32
1636  float e0, e1;
1637  float pv;
1638  float k1, k2;
1639  float r0 = ps->r0, r1 = ps->r1;
1640  float cor0 = ps->cor0, cor1 = ps->cor1;
1641  float var0 = ps->var0, var1 = ps->var1;
1642 
1643  k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
1644  k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
1645 
1646  pv = flt16_round(k1 * r0 + k2 * r1);
1647  if (output_enable)
1648  *coef += pv;
1649 
1650  e0 = *coef;
1651  e1 = e0 - k1 * r0;
1652 
1653  ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
1654  ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
1655  ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
1656  ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
1657 
1658  ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
1659  ps->r0 = flt16_trunc(a * e0);
1660 }
1661 
1666 {
1667  int sfb, k;
1668 
1669  if (!sce->ics.predictor_initialized) {
1671  sce->ics.predictor_initialized = 1;
1672  }
1673 
1674  if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1675  for (sfb = 0;
1676  sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
1677  sfb++) {
1678  for (k = sce->ics.swb_offset[sfb];
1679  k < sce->ics.swb_offset[sfb + 1];
1680  k++) {
1681  predict(&sce->predictor_state[k], &sce->coeffs[k],
1682  sce->ics.predictor_present &&
1683  sce->ics.prediction_used[sfb]);
1684  }
1685  }
1686  if (sce->ics.predictor_reset_group)
1688  sce->ics.predictor_reset_group);
1689  } else
1691 }
1692 
1702  GetBitContext *gb, int common_window, int scale_flag)
1703 {
1704  Pulse pulse;
1705  TemporalNoiseShaping *tns = &sce->tns;
1706  IndividualChannelStream *ics = &sce->ics;
1707  float *out = sce->coeffs;
1708  int global_gain, pulse_present = 0;
1709  int ret;
1710 
1711  /* This assignment is to silence a GCC warning about the variable being used
1712  * uninitialized when in fact it always is.
1713  */
1714  pulse.num_pulse = 0;
1715 
1716  global_gain = get_bits(gb, 8);
1717 
1718  if (!common_window && !scale_flag) {
1719  if (decode_ics_info(ac, ics, gb) < 0)
1720  return AVERROR_INVALIDDATA;
1721  }
1722 
1723  if ((ret = decode_band_types(ac, sce->band_type,
1724  sce->band_type_run_end, gb, ics)) < 0)
1725  return ret;
1726  if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
1727  sce->band_type, sce->band_type_run_end)) < 0)
1728  return ret;
1729 
1730  pulse_present = 0;
1731  if (!scale_flag) {
1732  if ((pulse_present = get_bits1(gb))) {
1733  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1734  av_log(ac->avctx, AV_LOG_ERROR,
1735  "Pulse tool not allowed in eight short sequence.\n");
1736  return AVERROR_INVALIDDATA;
1737  }
1738  if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1739  av_log(ac->avctx, AV_LOG_ERROR,
1740  "Pulse data corrupt or invalid.\n");
1741  return AVERROR_INVALIDDATA;
1742  }
1743  }
1744  if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
1745  return AVERROR_INVALIDDATA;
1746  if (get_bits1(gb)) {
1747  av_log_missing_feature(ac->avctx, "SSR", 1);
1748  return AVERROR_PATCHWELCOME;
1749  }
1750  }
1751 
1752  if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
1753  &pulse, ics, sce->band_type) < 0)
1754  return AVERROR_INVALIDDATA;
1755 
1756  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
1757  apply_prediction(ac, sce);
1758 
1759  return 0;
1760 }
1761 
1766 {
1767  const IndividualChannelStream *ics = &cpe->ch[0].ics;
1768  float *ch0 = cpe->ch[0].coeffs;
1769  float *ch1 = cpe->ch[1].coeffs;
1770  int g, i, group, idx = 0;
1771  const uint16_t *offsets = ics->swb_offset;
1772  for (g = 0; g < ics->num_window_groups; g++) {
1773  for (i = 0; i < ics->max_sfb; i++, idx++) {
1774  if (cpe->ms_mask[idx] &&
1775  cpe->ch[0].band_type[idx] < NOISE_BT &&
1776  cpe->ch[1].band_type[idx] < NOISE_BT) {
1777  for (group = 0; group < ics->group_len[g]; group++) {
1778  ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
1779  ch1 + group * 128 + offsets[i],
1780  offsets[i+1] - offsets[i]);
1781  }
1782  }
1783  }
1784  ch0 += ics->group_len[g] * 128;
1785  ch1 += ics->group_len[g] * 128;
1786  }
1787 }
1788 
1797  ChannelElement *cpe, int ms_present)
1798 {
1799  const IndividualChannelStream *ics = &cpe->ch[1].ics;
1800  SingleChannelElement *sce1 = &cpe->ch[1];
1801  float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
1802  const uint16_t *offsets = ics->swb_offset;
1803  int g, group, i, idx = 0;
1804  int c;
1805  float scale;
1806  for (g = 0; g < ics->num_window_groups; g++) {
1807  for (i = 0; i < ics->max_sfb;) {
1808  if (sce1->band_type[idx] == INTENSITY_BT ||
1809  sce1->band_type[idx] == INTENSITY_BT2) {
1810  const int bt_run_end = sce1->band_type_run_end[idx];
1811  for (; i < bt_run_end; i++, idx++) {
1812  c = -1 + 2 * (sce1->band_type[idx] - 14);
1813  if (ms_present)
1814  c *= 1 - 2 * cpe->ms_mask[idx];
1815  scale = c * sce1->sf[idx];
1816  for (group = 0; group < ics->group_len[g]; group++)
1817  ac->fdsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
1818  coef0 + group * 128 + offsets[i],
1819  scale,
1820  offsets[i + 1] - offsets[i]);
1821  }
1822  } else {
1823  int bt_run_end = sce1->band_type_run_end[idx];
1824  idx += bt_run_end - i;
1825  i = bt_run_end;
1826  }
1827  }
1828  coef0 += ics->group_len[g] * 128;
1829  coef1 += ics->group_len[g] * 128;
1830  }
1831 }
1832 
1839 {
1840  int i, ret, common_window, ms_present = 0;
1841 
1842  common_window = get_bits1(gb);
1843  if (common_window) {
1844  if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
1845  return AVERROR_INVALIDDATA;
1846  i = cpe->ch[1].ics.use_kb_window[0];
1847  cpe->ch[1].ics = cpe->ch[0].ics;
1848  cpe->ch[1].ics.use_kb_window[1] = i;
1849  if (cpe->ch[1].ics.predictor_present &&
1850  (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
1851  if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
1852  decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
1853  ms_present = get_bits(gb, 2);
1854  if (ms_present == 3) {
1855  av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1856  return AVERROR_INVALIDDATA;
1857  } else if (ms_present)
1858  decode_mid_side_stereo(cpe, gb, ms_present);
1859  }
1860  if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1861  return ret;
1862  if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1863  return ret;
1864 
1865  if (common_window) {
1866  if (ms_present)
1867  apply_mid_side_stereo(ac, cpe);
1868  if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1869  apply_prediction(ac, &cpe->ch[0]);
1870  apply_prediction(ac, &cpe->ch[1]);
1871  }
1872  }
1873 
1874  apply_intensity_stereo(ac, cpe, ms_present);
1875  return 0;
1876 }
1877 
1878 static const float cce_scale[] = {
1879  1.09050773266525765921, //2^(1/8)
1880  1.18920711500272106672, //2^(1/4)
1881  M_SQRT2,
1882  2,
1883 };
1884 
1891 {
1892  int num_gain = 0;
1893  int c, g, sfb, ret;
1894  int sign;
1895  float scale;
1896  SingleChannelElement *sce = &che->ch[0];
1897  ChannelCoupling *coup = &che->coup;
1898 
1899  coup->coupling_point = 2 * get_bits1(gb);
1900  coup->num_coupled = get_bits(gb, 3);
1901  for (c = 0; c <= coup->num_coupled; c++) {
1902  num_gain++;
1903  coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
1904  coup->id_select[c] = get_bits(gb, 4);
1905  if (coup->type[c] == TYPE_CPE) {
1906  coup->ch_select[c] = get_bits(gb, 2);
1907  if (coup->ch_select[c] == 3)
1908  num_gain++;
1909  } else
1910  coup->ch_select[c] = 2;
1911  }
1912  coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
1913 
1914  sign = get_bits(gb, 1);
1915  scale = cce_scale[get_bits(gb, 2)];
1916 
1917  if ((ret = decode_ics(ac, sce, gb, 0, 0)))
1918  return ret;
1919 
1920  for (c = 0; c < num_gain; c++) {
1921  int idx = 0;
1922  int cge = 1;
1923  int gain = 0;
1924  float gain_cache = 1.;
1925  if (c) {
1926  cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
1927  gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
1928  gain_cache = powf(scale, -gain);
1929  }
1930  if (coup->coupling_point == AFTER_IMDCT) {
1931  coup->gain[c][0] = gain_cache;
1932  } else {
1933  for (g = 0; g < sce->ics.num_window_groups; g++) {
1934  for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
1935  if (sce->band_type[idx] != ZERO_BT) {
1936  if (!cge) {
1937  int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1938  if (t) {
1939  int s = 1;
1940  t = gain += t;
1941  if (sign) {
1942  s -= 2 * (t & 0x1);
1943  t >>= 1;
1944  }
1945  gain_cache = powf(scale, -t) * s;
1946  }
1947  }
1948  coup->gain[c][idx] = gain_cache;
1949  }
1950  }
1951  }
1952  }
1953  }
1954  return 0;
1955 }
1956 
1963  GetBitContext *gb)
1964 {
1965  int i;
1966  int num_excl_chan = 0;
1967 
1968  do {
1969  for (i = 0; i < 7; i++)
1970  che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1971  } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
1972 
1973  return num_excl_chan / 7;
1974 }
1975 
1982  GetBitContext *gb)
1983 {
1984  int n = 1;
1985  int drc_num_bands = 1;
1986  int i;
1987 
1988  /* pce_tag_present? */
1989  if (get_bits1(gb)) {
1990  che_drc->pce_instance_tag = get_bits(gb, 4);
1991  skip_bits(gb, 4); // tag_reserved_bits
1992  n++;
1993  }
1994 
1995  /* excluded_chns_present? */
1996  if (get_bits1(gb)) {
1997  n += decode_drc_channel_exclusions(che_drc, gb);
1998  }
1999 
2000  /* drc_bands_present? */
2001  if (get_bits1(gb)) {
2002  che_drc->band_incr = get_bits(gb, 4);
2003  che_drc->interpolation_scheme = get_bits(gb, 4);
2004  n++;
2005  drc_num_bands += che_drc->band_incr;
2006  for (i = 0; i < drc_num_bands; i++) {
2007  che_drc->band_top[i] = get_bits(gb, 8);
2008  n++;
2009  }
2010  }
2011 
2012  /* prog_ref_level_present? */
2013  if (get_bits1(gb)) {
2014  che_drc->prog_ref_level = get_bits(gb, 7);
2015  skip_bits1(gb); // prog_ref_level_reserved_bits
2016  n++;
2017  }
2018 
2019  for (i = 0; i < drc_num_bands; i++) {
2020  che_drc->dyn_rng_sgn[i] = get_bits1(gb);
2021  che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
2022  n++;
2023  }
2024 
2025  return n;
2026 }
2027 
2036  ChannelElement *che, enum RawDataBlockType elem_type)
2037 {
2038  int crc_flag = 0;
2039  int res = cnt;
2040  switch (get_bits(gb, 4)) { // extension type
2041  case EXT_SBR_DATA_CRC:
2042  crc_flag++;
2043  case EXT_SBR_DATA:
2044  if (!che) {
2045  av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
2046  return res;
2047  } else if (!ac->oc[1].m4ac.sbr) {
2048  av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
2049  skip_bits_long(gb, 8 * cnt - 4);
2050  return res;
2051  } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
2052  av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
2053  skip_bits_long(gb, 8 * cnt - 4);
2054  return res;
2055  } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
2056  ac->oc[1].m4ac.sbr = 1;
2057  ac->oc[1].m4ac.ps = 1;
2058  output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
2059  ac->oc[1].status, 1);
2060  } else {
2061  ac->oc[1].m4ac.sbr = 1;
2062  }
2063  res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
2064  break;
2065  case EXT_DYNAMIC_RANGE:
2066  res = decode_dynamic_range(&ac->che_drc, gb);
2067  break;
2068  case EXT_FILL:
2069  case EXT_FILL_DATA:
2070  case EXT_DATA_ELEMENT:
2071  default:
2072  skip_bits_long(gb, 8 * cnt - 4);
2073  break;
2074  };
2075  return res;
2076 }
2077 
2084 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
2085  IndividualChannelStream *ics, int decode)
2086 {
2087  const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
2088  int w, filt, m, i;
2089  int bottom, top, order, start, end, size, inc;
2090  float lpc[TNS_MAX_ORDER];
2091  float tmp[TNS_MAX_ORDER + 1];
2092 
2093  for (w = 0; w < ics->num_windows; w++) {
2094  bottom = ics->num_swb;
2095  for (filt = 0; filt < tns->n_filt[w]; filt++) {
2096  top = bottom;
2097  bottom = FFMAX(0, top - tns->length[w][filt]);
2098  order = tns->order[w][filt];
2099  if (order == 0)
2100  continue;
2101 
2102  // tns_decode_coef
2103  compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
2104 
2105  start = ics->swb_offset[FFMIN(bottom, mmm)];
2106  end = ics->swb_offset[FFMIN( top, mmm)];
2107  if ((size = end - start) <= 0)
2108  continue;
2109  if (tns->direction[w][filt]) {
2110  inc = -1;
2111  start = end - 1;
2112  } else {
2113  inc = 1;
2114  }
2115  start += w * 128;
2116 
2117  if (decode) {
2118  // ar filter
2119  for (m = 0; m < size; m++, start += inc)
2120  for (i = 1; i <= FFMIN(m, order); i++)
2121  coef[start] -= coef[start - i * inc] * lpc[i - 1];
2122  } else {
2123  // ma filter
2124  for (m = 0; m < size; m++, start += inc) {
2125  tmp[0] = coef[start];
2126  for (i = 1; i <= FFMIN(m, order); i++)
2127  coef[start] += tmp[i] * lpc[i - 1];
2128  for (i = order; i > 0; i--)
2129  tmp[i] = tmp[i - 1];
2130  }
2131  }
2132  }
2133  }
2134 }
2135 
2140 static void windowing_and_mdct_ltp(AACContext *ac, float *out,
2141  float *in, IndividualChannelStream *ics)
2142 {
2143  const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2144  const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2145  const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2146  const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2147 
2148  if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
2149  ac->fdsp.vector_fmul(in, in, lwindow_prev, 1024);
2150  } else {
2151  memset(in, 0, 448 * sizeof(float));
2152  ac->fdsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
2153  }
2154  if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
2155  ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
2156  } else {
2157  ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
2158  memset(in + 1024 + 576, 0, 448 * sizeof(float));
2159  }
2160  ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
2161 }
2162 
2167 {
2168  const LongTermPrediction *ltp = &sce->ics.ltp;
2169  const uint16_t *offsets = sce->ics.swb_offset;
2170  int i, sfb;
2171 
2172  if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2173  float *predTime = sce->ret;
2174  float *predFreq = ac->buf_mdct;
2175  int16_t num_samples = 2048;
2176 
2177  if (ltp->lag < 1024)
2178  num_samples = ltp->lag + 1024;
2179  for (i = 0; i < num_samples; i++)
2180  predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
2181  memset(&predTime[i], 0, (2048 - i) * sizeof(float));
2182 
2183  windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
2184 
2185  if (sce->tns.present)
2186  apply_tns(predFreq, &sce->tns, &sce->ics, 0);
2187 
2188  for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
2189  if (ltp->used[sfb])
2190  for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
2191  sce->coeffs[i] += predFreq[i];
2192  }
2193 }
2194 
2199 {
2200  IndividualChannelStream *ics = &sce->ics;
2201  float *saved = sce->saved;
2202  float *saved_ltp = sce->coeffs;
2203  const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2204  const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2205  int i;
2206 
2207  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2208  memcpy(saved_ltp, saved, 512 * sizeof(float));
2209  memset(saved_ltp + 576, 0, 448 * sizeof(float));
2210  ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2211  for (i = 0; i < 64; i++)
2212  saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2213  } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2214  memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
2215  memset(saved_ltp + 576, 0, 448 * sizeof(float));
2216  ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2217  for (i = 0; i < 64; i++)
2218  saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2219  } else { // LONG_STOP or ONLY_LONG
2220  ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
2221  for (i = 0; i < 512; i++)
2222  saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
2223  }
2224 
2225  memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
2226  memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
2227  memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
2228 }
2229 
2234 {
2235  IndividualChannelStream *ics = &sce->ics;
2236  float *in = sce->coeffs;
2237  float *out = sce->ret;
2238  float *saved = sce->saved;
2239  const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2240  const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2241  const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2242  float *buf = ac->buf_mdct;
2243  float *temp = ac->temp;
2244  int i;
2245 
2246  // imdct
2247  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2248  for (i = 0; i < 1024; i += 128)
2249  ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
2250  } else
2251  ac->mdct.imdct_half(&ac->mdct, buf, in);
2252 
2253  /* window overlapping
2254  * NOTE: To simplify the overlapping code, all 'meaningless' short to long
2255  * and long to short transitions are considered to be short to short
2256  * transitions. This leaves just two cases (long to long and short to short)
2257  * with a little special sauce for EIGHT_SHORT_SEQUENCE.
2258  */
2259  if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2261  ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
2262  } else {
2263  memcpy( out, saved, 448 * sizeof(float));
2264 
2265  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2266  ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
2267  ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
2268  ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
2269  ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
2270  ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
2271  memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
2272  } else {
2273  ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
2274  memcpy( out + 576, buf + 64, 448 * sizeof(float));
2275  }
2276  }
2277 
2278  // buffer update
2279  if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2280  memcpy( saved, temp + 64, 64 * sizeof(float));
2281  ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
2282  ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
2283  ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
2284  memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2285  } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2286  memcpy( saved, buf + 512, 448 * sizeof(float));
2287  memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2288  } else { // LONG_STOP or ONLY_LONG
2289  memcpy( saved, buf + 512, 512 * sizeof(float));
2290  }
2291 }
2292 
2299  SingleChannelElement *target,
2300  ChannelElement *cce, int index)
2301 {
2302  IndividualChannelStream *ics = &cce->ch[0].ics;
2303  const uint16_t *offsets = ics->swb_offset;
2304  float *dest = target->coeffs;
2305  const float *src = cce->ch[0].coeffs;
2306  int g, i, group, k, idx = 0;
2307  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2308  av_log(ac->avctx, AV_LOG_ERROR,
2309  "Dependent coupling is not supported together with LTP\n");
2310  return;
2311  }
2312  for (g = 0; g < ics->num_window_groups; g++) {
2313  for (i = 0; i < ics->max_sfb; i++, idx++) {
2314  if (cce->ch[0].band_type[idx] != ZERO_BT) {
2315  const float gain = cce->coup.gain[index][idx];
2316  for (group = 0; group < ics->group_len[g]; group++) {
2317  for (k = offsets[i]; k < offsets[i + 1]; k++) {
2318  // XXX dsputil-ize
2319  dest[group * 128 + k] += gain * src[group * 128 + k];
2320  }
2321  }
2322  }
2323  }
2324  dest += ics->group_len[g] * 128;
2325  src += ics->group_len[g] * 128;
2326  }
2327 }
2328 
2335  SingleChannelElement *target,
2336  ChannelElement *cce, int index)
2337 {
2338  int i;
2339  const float gain = cce->coup.gain[index][0];
2340  const float *src = cce->ch[0].ret;
2341  float *dest = target->ret;
2342  const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
2343 
2344  for (i = 0; i < len; i++)
2345  dest[i] += gain * src[i];
2346 }
2347 
2354  enum RawDataBlockType type, int elem_id,
2355  enum CouplingPoint coupling_point,
2356  void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2357 {
2358  int i, c;
2359 
2360  for (i = 0; i < MAX_ELEM_ID; i++) {
2361  ChannelElement *cce = ac->che[TYPE_CCE][i];
2362  int index = 0;
2363 
2364  if (cce && cce->coup.coupling_point == coupling_point) {
2365  ChannelCoupling *coup = &cce->coup;
2366 
2367  for (c = 0; c <= coup->num_coupled; c++) {
2368  if (coup->type[c] == type && coup->id_select[c] == elem_id) {
2369  if (coup->ch_select[c] != 1) {
2370  apply_coupling_method(ac, &cc->ch[0], cce, index);
2371  if (coup->ch_select[c] != 0)
2372  index++;
2373  }
2374  if (coup->ch_select[c] != 2)
2375  apply_coupling_method(ac, &cc->ch[1], cce, index++);
2376  } else
2377  index += 1 + (coup->ch_select[c] == 3);
2378  }
2379  }
2380  }
2381 }
2382 
2387 {
2388  int i, type;
2389  for (type = 3; type >= 0; type--) {
2390  for (i = 0; i < MAX_ELEM_ID; i++) {
2391  ChannelElement *che = ac->che[type][i];
2392  if (che) {
2393  if (type <= TYPE_CPE)
2395  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2396  if (che->ch[0].ics.predictor_present) {
2397  if (che->ch[0].ics.ltp.present)
2398  apply_ltp(ac, &che->ch[0]);
2399  if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
2400  apply_ltp(ac, &che->ch[1]);
2401  }
2402  }
2403  if (che->ch[0].tns.present)
2404  apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
2405  if (che->ch[1].tns.present)
2406  apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
2407  if (type <= TYPE_CPE)
2409  if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2410  imdct_and_windowing(ac, &che->ch[0]);
2411  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2412  update_ltp(ac, &che->ch[0]);
2413  if (type == TYPE_CPE) {
2414  imdct_and_windowing(ac, &che->ch[1]);
2415  if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2416  update_ltp(ac, &che->ch[1]);
2417  }
2418  if (ac->oc[1].m4ac.sbr > 0) {
2419  ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
2420  }
2421  }
2422  if (type <= TYPE_CCE)
2424  }
2425  }
2426  }
2427 }
2428 
2430 {
2431  int size;
2432  AACADTSHeaderInfo hdr_info;
2433  uint8_t layout_map[MAX_ELEM_ID*4][3];
2434  int layout_map_tags;
2435 
2436  size = avpriv_aac_parse_header(gb, &hdr_info);
2437  if (size > 0) {
2438  if (hdr_info.num_aac_frames != 1) {
2439  av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame", 0);
2440  return AVERROR_PATCHWELCOME;
2441  }
2443  if (hdr_info.chan_config) {
2444  ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2445  if (set_default_channel_config(ac->avctx, layout_map,
2446  &layout_map_tags, hdr_info.chan_config))
2447  return -7;
2448  if (output_configure(ac, layout_map, layout_map_tags,
2449  FFMAX(ac->oc[1].status, OC_TRIAL_FRAME), 0))
2450  return -7;
2451  } else {
2452  ac->oc[1].m4ac.chan_config = 0;
2453  }
2454  ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2455  ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2456  ac->oc[1].m4ac.object_type = hdr_info.object_type;
2457  if (ac->oc[0].status != OC_LOCKED ||
2458  ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2459  ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2460  ac->oc[1].m4ac.sbr = -1;
2461  ac->oc[1].m4ac.ps = -1;
2462  }
2463  if (!hdr_info.crc_absent)
2464  skip_bits(gb, 16);
2465  }
2466  return size;
2467 }
2468 
2469 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
2470  int *got_frame_ptr, GetBitContext *gb)
2471 {
2472  AACContext *ac = avctx->priv_data;
2473  ChannelElement *che = NULL, *che_prev = NULL;
2474  enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
2475  int err, elem_id;
2476  int samples = 0, multiplier, audio_found = 0, pce_found = 0;
2477 
2478  if (show_bits(gb, 12) == 0xfff) {
2479  if (parse_adts_frame_header(ac, gb) < 0) {
2480  av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2481  err = -1;
2482  goto fail;
2483  }
2484  if (ac->oc[1].m4ac.sampling_index > 12) {
2485  av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2486  err = -1;
2487  goto fail;
2488  }
2489  }
2490 
2491  if (frame_configure_elements(avctx) < 0) {
2492  err = -1;
2493  goto fail;
2494  }
2495 
2496  ac->tags_mapped = 0;
2497  // parse
2498  while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2499  elem_id = get_bits(gb, 4);
2500 
2501  if (elem_type < TYPE_DSE) {
2502  if (!(che=get_che(ac, elem_type, elem_id))) {
2503  av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2504  elem_type, elem_id);
2505  err = -1;
2506  goto fail;
2507  }
2508  samples = 1024;
2509  }
2510 
2511  switch (elem_type) {
2512 
2513  case TYPE_SCE:
2514  err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2515  audio_found = 1;
2516  break;
2517 
2518  case TYPE_CPE:
2519  err = decode_cpe(ac, gb, che);
2520  audio_found = 1;
2521  break;
2522 
2523  case TYPE_CCE:
2524  err = decode_cce(ac, gb, che);
2525  break;
2526 
2527  case TYPE_LFE:
2528  err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2529  audio_found = 1;
2530  break;
2531 
2532  case TYPE_DSE:
2533  err = skip_data_stream_element(ac, gb);
2534  break;
2535 
2536  case TYPE_PCE: {
2537  uint8_t layout_map[MAX_ELEM_ID*4][3];
2538  int tags;
2540  tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
2541  if (tags < 0) {
2542  err = tags;
2543  break;
2544  }
2545  if (pce_found) {
2546  av_log(avctx, AV_LOG_ERROR,
2547  "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2549  } else {
2550  err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
2551  pce_found = 1;
2552  }
2553  break;
2554  }
2555 
2556  case TYPE_FIL:
2557  if (elem_id == 15)
2558  elem_id += get_bits(gb, 8) - 1;
2559  if (get_bits_left(gb) < 8 * elem_id) {
2560  av_log(avctx, AV_LOG_ERROR, overread_err);
2561  err = -1;
2562  goto fail;
2563  }
2564  while (elem_id > 0)
2565  elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
2566  err = 0; /* FIXME */
2567  break;
2568 
2569  default:
2570  err = -1; /* should not happen, but keeps compiler happy */
2571  break;
2572  }
2573 
2574  che_prev = che;
2575  elem_type_prev = elem_type;
2576 
2577  if (err)
2578  goto fail;
2579 
2580  if (get_bits_left(gb) < 3) {
2581  av_log(avctx, AV_LOG_ERROR, overread_err);
2582  err = -1;
2583  goto fail;
2584  }
2585  }
2586 
2587  spectral_to_sample(ac);
2588 
2589  multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
2590  samples <<= multiplier;
2591 
2592  if (samples) {
2593  ac->frame.nb_samples = samples;
2594  *(AVFrame *)data = ac->frame;
2595  }
2596  *got_frame_ptr = !!samples;
2597 
2598  if (ac->oc[1].status && audio_found) {
2599  avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
2600  avctx->frame_size = samples;
2601  ac->oc[1].status = OC_LOCKED;
2602  }
2603 
2604  return 0;
2605 fail:
2607  return err;
2608 }
2609 
2610 static int aac_decode_frame(AVCodecContext *avctx, void *data,
2611  int *got_frame_ptr, AVPacket *avpkt)
2612 {
2613  AACContext *ac = avctx->priv_data;
2614  const uint8_t *buf = avpkt->data;
2615  int buf_size = avpkt->size;
2616  GetBitContext gb;
2617  int buf_consumed;
2618  int buf_offset;
2619  int err;
2620  int new_extradata_size;
2621  const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
2623  &new_extradata_size);
2624 
2625  if (new_extradata) {
2626  av_free(avctx->extradata);
2627  avctx->extradata = av_mallocz(new_extradata_size +
2629  if (!avctx->extradata)
2630  return AVERROR(ENOMEM);
2631  avctx->extradata_size = new_extradata_size;
2632  memcpy(avctx->extradata, new_extradata, new_extradata_size);
2634  if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
2635  avctx->extradata,
2636  avctx->extradata_size*8, 1) < 0) {
2638  return AVERROR_INVALIDDATA;
2639  }
2640  }
2641 
2642  if ((err = init_get_bits(&gb, buf, buf_size * 8)) < 0)
2643  return err;
2644 
2645  if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb)) < 0)
2646  return err;
2647 
2648  buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2649  for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2650  if (buf[buf_offset])
2651  break;
2652 
2653  return buf_size > buf_offset ? buf_consumed : buf_size;
2654 }
2655 
2657 {
2658  AACContext *ac = avctx->priv_data;
2659  int i, type;
2660 
2661  for (i = 0; i < MAX_ELEM_ID; i++) {
2662  for (type = 0; type < 4; type++) {
2663  if (ac->che[type][i])
2664  ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
2665  av_freep(&ac->che[type][i]);
2666  }
2667  }
2668 
2669  ff_mdct_end(&ac->mdct);
2670  ff_mdct_end(&ac->mdct_small);
2671  ff_mdct_end(&ac->mdct_ltp);
2672  return 0;
2673 }
2674 
2675 
2676 #define LOAS_SYNC_WORD 0x2b7
2677 
2678 struct LATMContext {
2681 
2682  // parser data
2686 };
2687 
2688 static inline uint32_t latm_get_value(GetBitContext *b)
2689 {
2690  int length = get_bits(b, 2);
2691 
2692  return get_bits_long(b, (length+1)*8);
2693 }
2694 
2696  GetBitContext *gb, int asclen)
2697 {
2698  AACContext *ac = &latmctx->aac_ctx;
2699  AVCodecContext *avctx = ac->avctx;
2700  MPEG4AudioConfig m4ac = { 0 };
2701  int config_start_bit = get_bits_count(gb);
2702  int sync_extension = 0;
2703  int bits_consumed, esize;
2704 
2705  if (asclen) {
2706  sync_extension = 1;
2707  asclen = FFMIN(asclen, get_bits_left(gb));
2708  } else
2709  asclen = get_bits_left(gb);
2710 
2711  if (config_start_bit % 8) {
2713  "Non-byte-aligned audio-specific config", 1);
2714  return AVERROR_PATCHWELCOME;
2715  }
2716  if (asclen <= 0)
2717  return AVERROR_INVALIDDATA;
2718  bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
2719  gb->buffer + (config_start_bit / 8),
2720  asclen, sync_extension);
2721 
2722  if (bits_consumed < 0)
2723  return AVERROR_INVALIDDATA;
2724 
2725  if (ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
2726  ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
2727 
2728  av_log(avctx, AV_LOG_INFO, "audio config changed\n");
2729  latmctx->initialized = 0;
2730 
2731  esize = (bits_consumed+7) / 8;
2732 
2733  if (avctx->extradata_size < esize) {
2734  av_free(avctx->extradata);
2736  if (!avctx->extradata)
2737  return AVERROR(ENOMEM);
2738  }
2739 
2740  avctx->extradata_size = esize;
2741  memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
2742  memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2743  }
2744  skip_bits_long(gb, bits_consumed);
2745 
2746  return bits_consumed;
2747 }
2748 
2749 static int read_stream_mux_config(struct LATMContext *latmctx,
2750  GetBitContext *gb)
2751 {
2752  int ret, audio_mux_version = get_bits(gb, 1);
2753 
2754  latmctx->audio_mux_version_A = 0;
2755  if (audio_mux_version)
2756  latmctx->audio_mux_version_A = get_bits(gb, 1);
2757 
2758  if (!latmctx->audio_mux_version_A) {
2759 
2760  if (audio_mux_version)
2761  latm_get_value(gb); // taraFullness
2762 
2763  skip_bits(gb, 1); // allStreamSameTimeFraming
2764  skip_bits(gb, 6); // numSubFrames
2765  // numPrograms
2766  if (get_bits(gb, 4)) { // numPrograms
2768  "Multiple programs", 1);
2769  return AVERROR_PATCHWELCOME;
2770  }
2771 
2772  // for each program (which there is only on in DVB)
2773 
2774  // for each layer (which there is only on in DVB)
2775  if (get_bits(gb, 3)) { // numLayer
2777  "Multiple layers", 1);
2778  return AVERROR_PATCHWELCOME;
2779  }
2780 
2781  // for all but first stream: use_same_config = get_bits(gb, 1);
2782  if (!audio_mux_version) {
2783  if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
2784  return ret;
2785  } else {
2786  int ascLen = latm_get_value(gb);
2787  if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
2788  return ret;
2789  ascLen -= ret;
2790  skip_bits_long(gb, ascLen);
2791  }
2792 
2793  latmctx->frame_length_type = get_bits(gb, 3);
2794  switch (latmctx->frame_length_type) {
2795  case 0:
2796  skip_bits(gb, 8); // latmBufferFullness
2797  break;
2798  case 1:
2799  latmctx->frame_length = get_bits(gb, 9);
2800  break;
2801  case 3:
2802  case 4:
2803  case 5:
2804  skip_bits(gb, 6); // CELP frame length table index
2805  break;
2806  case 6:
2807  case 7:
2808  skip_bits(gb, 1); // HVXC frame length table index
2809  break;
2810  }
2811 
2812  if (get_bits(gb, 1)) { // other data
2813  if (audio_mux_version) {
2814  latm_get_value(gb); // other_data_bits
2815  } else {
2816  int esc;
2817  do {
2818  esc = get_bits(gb, 1);
2819  skip_bits(gb, 8);
2820  } while (esc);
2821  }
2822  }
2823 
2824  if (get_bits(gb, 1)) // crc present
2825  skip_bits(gb, 8); // config_crc
2826  }
2827 
2828  return 0;
2829 }
2830 
2832 {
2833  uint8_t tmp;
2834 
2835  if (ctx->frame_length_type == 0) {
2836  int mux_slot_length = 0;
2837  do {
2838  tmp = get_bits(gb, 8);
2839  mux_slot_length += tmp;
2840  } while (tmp == 255);
2841  return mux_slot_length;
2842  } else if (ctx->frame_length_type == 1) {
2843  return ctx->frame_length;
2844  } else if (ctx->frame_length_type == 3 ||
2845  ctx->frame_length_type == 5 ||
2846  ctx->frame_length_type == 7) {
2847  skip_bits(gb, 2); // mux_slot_length_coded
2848  }
2849  return 0;
2850 }
2851 
2852 static int read_audio_mux_element(struct LATMContext *latmctx,
2853  GetBitContext *gb)
2854 {
2855  int err;
2856  uint8_t use_same_mux = get_bits(gb, 1);
2857  if (!use_same_mux) {
2858  if ((err = read_stream_mux_config(latmctx, gb)) < 0)
2859  return err;
2860  } else if (!latmctx->aac_ctx.avctx->extradata) {
2861  av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
2862  "no decoder config found\n");
2863  return AVERROR(EAGAIN);
2864  }
2865  if (latmctx->audio_mux_version_A == 0) {
2866  int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
2867  if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
2868  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
2869  return AVERROR_INVALIDDATA;
2870  } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
2871  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2872  "frame length mismatch %d << %d\n",
2873  mux_slot_length_bytes * 8, get_bits_left(gb));
2874  return AVERROR_INVALIDDATA;
2875  }
2876  }
2877  return 0;
2878 }
2879 
2880 
2881 static int latm_decode_frame(AVCodecContext *avctx, void *out,
2882  int *got_frame_ptr, AVPacket *avpkt)
2883 {
2884  struct LATMContext *latmctx = avctx->priv_data;
2885  int muxlength, err;
2886  GetBitContext gb;
2887 
2888  if ((err = init_get_bits(&gb, avpkt->data, avpkt->size * 8)) < 0)
2889  return err;
2890 
2891  // check for LOAS sync word
2892  if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
2893  return AVERROR_INVALIDDATA;
2894 
2895  muxlength = get_bits(&gb, 13) + 3;
2896  // not enough data, the parser should have sorted this
2897  if (muxlength > avpkt->size)
2898  return AVERROR_INVALIDDATA;
2899 
2900  if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
2901  return err;
2902 
2903  if (!latmctx->initialized) {
2904  if (!avctx->extradata) {
2905  *got_frame_ptr = 0;
2906  return avpkt->size;
2907  } else {
2909  if ((err = decode_audio_specific_config(
2910  &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
2911  avctx->extradata, avctx->extradata_size*8, 1)) < 0) {
2912  pop_output_configuration(&latmctx->aac_ctx);
2913  return err;
2914  }
2915  latmctx->initialized = 1;
2916  }
2917  }
2918 
2919  if (show_bits(&gb, 12) == 0xfff) {
2920  av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2921  "ADTS header detected, probably as result of configuration "
2922  "misparsing\n");
2923  return AVERROR_INVALIDDATA;
2924  }
2925 
2926  if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb)) < 0)
2927  return err;
2928 
2929  return muxlength;
2930 }
2931 
2933 {
2934  struct LATMContext *latmctx = avctx->priv_data;
2935  int ret = aac_decode_init(avctx);
2936 
2937  if (avctx->extradata_size > 0)
2938  latmctx->initialized = !ret;
2939 
2940  return ret;
2941 }
2942 
2943 
2945  .name = "aac",
2946  .type = AVMEDIA_TYPE_AUDIO,
2947  .id = AV_CODEC_ID_AAC,
2948  .priv_data_size = sizeof(AACContext),
2949  .init = aac_decode_init,
2952  .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
2953  .sample_fmts = (const enum AVSampleFormat[]) {
2955  },
2956  .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2957  .channel_layouts = aac_channel_layout,
2958 };
2959 
2960 /*
2961  Note: This decoder filter is intended to decode LATM streams transferred
2962  in MPEG transport streams which only contain one program.
2963  To do a more complex LATM demuxing a separate LATM demuxer should be used.
2964 */
2966  .name = "aac_latm",
2967  .type = AVMEDIA_TYPE_AUDIO,
2968  .id = AV_CODEC_ID_AAC_LATM,
2969  .priv_data_size = sizeof(struct LATMContext),
2970  .init = latm_decode_init,
2971  .close = aac_decode_close,
2972  .decode = latm_decode_frame,
2973  .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Coding LATM syntax)"),
2974  .sample_fmts = (const enum AVSampleFormat[]) {
2976  },
2977  .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2978  .channel_layouts = aac_channel_layout,
2979 };