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