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