You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1730 lines
63KB

  1. /*
  2. * AAC decoder
  3. * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
  4. * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/aac.c
  24. * AAC decoder
  25. * @author Oded Shimon ( ods15 ods15 dyndns org )
  26. * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
  27. */
  28. /*
  29. * supported tools
  30. *
  31. * Support? Name
  32. * N (code in SoC repo) gain control
  33. * Y block switching
  34. * Y window shapes - standard
  35. * N window shapes - Low Delay
  36. * Y filterbank - standard
  37. * N (code in SoC repo) filterbank - Scalable Sample Rate
  38. * Y Temporal Noise Shaping
  39. * N (code in SoC repo) Long Term Prediction
  40. * Y intensity stereo
  41. * Y channel coupling
  42. * Y frequency domain prediction
  43. * Y Perceptual Noise Substitution
  44. * Y Mid/Side stereo
  45. * N Scalable Inverse AAC Quantization
  46. * N Frequency Selective Switch
  47. * N upsampling filter
  48. * Y quantization & coding - AAC
  49. * N quantization & coding - TwinVQ
  50. * N quantization & coding - BSAC
  51. * N AAC Error Resilience tools
  52. * N Error Resilience payload syntax
  53. * N Error Protection tool
  54. * N CELP
  55. * N Silence Compression
  56. * N HVXC
  57. * N HVXC 4kbits/s VR
  58. * N Structured Audio tools
  59. * N Structured Audio Sample Bank Format
  60. * N MIDI
  61. * N Harmonic and Individual Lines plus Noise
  62. * N Text-To-Speech Interface
  63. * N (in progress) Spectral Band Replication
  64. * Y (not in this code) Layer-1
  65. * Y (not in this code) Layer-2
  66. * Y (not in this code) Layer-3
  67. * N SinuSoidal Coding (Transient, Sinusoid, Noise)
  68. * N (planned) Parametric Stereo
  69. * N Direct Stream Transfer
  70. *
  71. * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
  72. * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
  73. Parametric Stereo.
  74. */
  75. #include "avcodec.h"
  76. #include "internal.h"
  77. #include "get_bits.h"
  78. #include "dsputil.h"
  79. #include "lpc.h"
  80. #include "aac.h"
  81. #include "aactab.h"
  82. #include "aacdectab.h"
  83. #include "mpeg4audio.h"
  84. #include "aac_parser.h"
  85. #include <assert.h>
  86. #include <errno.h>
  87. #include <math.h>
  88. #include <string.h>
  89. union float754 { float f; uint32_t i; };
  90. static VLC vlc_scalefactors;
  91. static VLC vlc_spectral[11];
  92. static ChannelElement* get_che(AACContext *ac, int type, int elem_id) {
  93. static const int8_t tags_per_config[16] = { 0, 1, 1, 2, 3, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0 };
  94. if (ac->tag_che_map[type][elem_id]) {
  95. return ac->tag_che_map[type][elem_id];
  96. }
  97. if (ac->tags_mapped >= tags_per_config[ac->m4ac.chan_config]) {
  98. return NULL;
  99. }
  100. switch (ac->m4ac.chan_config) {
  101. case 7:
  102. if (ac->tags_mapped == 3 && type == TYPE_CPE) {
  103. ac->tags_mapped++;
  104. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
  105. }
  106. case 6:
  107. /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
  108. instead of SCE[0] CPE[0] CPE[0] LFE[0]. If we seem to have
  109. encountered such a stream, transfer the LFE[0] element to SCE[1] */
  110. if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
  111. ac->tags_mapped++;
  112. return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
  113. }
  114. case 5:
  115. if (ac->tags_mapped == 2 && type == TYPE_CPE) {
  116. ac->tags_mapped++;
  117. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
  118. }
  119. case 4:
  120. if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
  121. ac->tags_mapped++;
  122. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
  123. }
  124. case 3:
  125. case 2:
  126. if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
  127. ac->tags_mapped++;
  128. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
  129. } else if (ac->m4ac.chan_config == 2) {
  130. return NULL;
  131. }
  132. case 1:
  133. if (!ac->tags_mapped && type == TYPE_SCE) {
  134. ac->tags_mapped++;
  135. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
  136. }
  137. default:
  138. return NULL;
  139. }
  140. }
  141. /**
  142. * Configure output channel order based on the current program configuration element.
  143. *
  144. * @param che_pos current channel position configuration
  145. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  146. *
  147. * @return Returns error status. 0 - OK, !0 - error
  148. */
  149. static int output_configure(AACContext *ac, enum ChannelPosition che_pos[4][MAX_ELEM_ID],
  150. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID], int channel_config) {
  151. AVCodecContext *avctx = ac->avccontext;
  152. int i, type, channels = 0;
  153. memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  154. /* Allocate or free elements depending on if they are in the
  155. * current program configuration.
  156. *
  157. * Set up default 1:1 output mapping.
  158. *
  159. * For a 5.1 stream the output order will be:
  160. * [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ]
  161. */
  162. for(i = 0; i < MAX_ELEM_ID; i++) {
  163. for(type = 0; type < 4; type++) {
  164. if(che_pos[type][i]) {
  165. if(!ac->che[type][i] && !(ac->che[type][i] = av_mallocz(sizeof(ChannelElement))))
  166. return AVERROR(ENOMEM);
  167. if(type != TYPE_CCE) {
  168. ac->output_data[channels++] = ac->che[type][i]->ch[0].ret;
  169. if(type == TYPE_CPE) {
  170. ac->output_data[channels++] = ac->che[type][i]->ch[1].ret;
  171. }
  172. }
  173. } else
  174. av_freep(&ac->che[type][i]);
  175. }
  176. }
  177. if (channel_config) {
  178. memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  179. ac->tags_mapped = 0;
  180. } else {
  181. memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  182. ac->tags_mapped = 4*MAX_ELEM_ID;
  183. }
  184. avctx->channels = channels;
  185. return 0;
  186. }
  187. /**
  188. * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
  189. *
  190. * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present.
  191. * @param sce_map mono (Single Channel Element) map
  192. * @param type speaker type/position for these channels
  193. */
  194. static void decode_channel_map(enum ChannelPosition *cpe_map,
  195. enum ChannelPosition *sce_map, enum ChannelPosition type, GetBitContext * gb, int n) {
  196. while(n--) {
  197. enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
  198. map[get_bits(gb, 4)] = type;
  199. }
  200. }
  201. /**
  202. * Decode program configuration element; reference: table 4.2.
  203. *
  204. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  205. *
  206. * @return Returns error status. 0 - OK, !0 - error
  207. */
  208. static int decode_pce(AACContext * ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  209. GetBitContext * gb) {
  210. int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
  211. skip_bits(gb, 2); // object_type
  212. sampling_index = get_bits(gb, 4);
  213. if(sampling_index > 12) {
  214. av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
  215. return -1;
  216. }
  217. ac->m4ac.sampling_index = sampling_index;
  218. ac->m4ac.sample_rate = ff_mpeg4audio_sample_rates[ac->m4ac.sampling_index];
  219. num_front = get_bits(gb, 4);
  220. num_side = get_bits(gb, 4);
  221. num_back = get_bits(gb, 4);
  222. num_lfe = get_bits(gb, 2);
  223. num_assoc_data = get_bits(gb, 3);
  224. num_cc = get_bits(gb, 4);
  225. if (get_bits1(gb))
  226. skip_bits(gb, 4); // mono_mixdown_tag
  227. if (get_bits1(gb))
  228. skip_bits(gb, 4); // stereo_mixdown_tag
  229. if (get_bits1(gb))
  230. skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
  231. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
  232. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
  233. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
  234. decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
  235. skip_bits_long(gb, 4 * num_assoc_data);
  236. decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
  237. align_get_bits(gb);
  238. /* comment field, first byte is length */
  239. skip_bits_long(gb, 8 * get_bits(gb, 8));
  240. return 0;
  241. }
  242. /**
  243. * Set up channel positions based on a default channel configuration
  244. * as specified in table 1.17.
  245. *
  246. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  247. *
  248. * @return Returns error status. 0 - OK, !0 - error
  249. */
  250. static int set_default_channel_config(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  251. int channel_config)
  252. {
  253. if(channel_config < 1 || channel_config > 7) {
  254. av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
  255. channel_config);
  256. return -1;
  257. }
  258. /* default channel configurations:
  259. *
  260. * 1ch : front center (mono)
  261. * 2ch : L + R (stereo)
  262. * 3ch : front center + L + R
  263. * 4ch : front center + L + R + back center
  264. * 5ch : front center + L + R + back stereo
  265. * 6ch : front center + L + R + back stereo + LFE
  266. * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
  267. */
  268. if(channel_config != 2)
  269. new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
  270. if(channel_config > 1)
  271. new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
  272. if(channel_config == 4)
  273. new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK; // back center
  274. if(channel_config > 4)
  275. new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
  276. = AAC_CHANNEL_BACK; // back stereo
  277. if(channel_config > 5)
  278. new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE; // LFE
  279. if(channel_config == 7)
  280. new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
  281. return 0;
  282. }
  283. /**
  284. * Decode GA "General Audio" specific configuration; reference: table 4.1.
  285. *
  286. * @return Returns error status. 0 - OK, !0 - error
  287. */
  288. static int decode_ga_specific_config(AACContext * ac, GetBitContext * gb, int channel_config) {
  289. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  290. int extension_flag, ret;
  291. if(get_bits1(gb)) { // frameLengthFlag
  292. ff_log_missing_feature(ac->avccontext, "960/120 MDCT window is", 1);
  293. return -1;
  294. }
  295. if (get_bits1(gb)) // dependsOnCoreCoder
  296. skip_bits(gb, 14); // coreCoderDelay
  297. extension_flag = get_bits1(gb);
  298. if(ac->m4ac.object_type == AOT_AAC_SCALABLE ||
  299. ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
  300. skip_bits(gb, 3); // layerNr
  301. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  302. if (channel_config == 0) {
  303. skip_bits(gb, 4); // element_instance_tag
  304. if((ret = decode_pce(ac, new_che_pos, gb)))
  305. return ret;
  306. } else {
  307. if((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
  308. return ret;
  309. }
  310. if((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config)))
  311. return ret;
  312. if (extension_flag) {
  313. switch (ac->m4ac.object_type) {
  314. case AOT_ER_BSAC:
  315. skip_bits(gb, 5); // numOfSubFrame
  316. skip_bits(gb, 11); // layer_length
  317. break;
  318. case AOT_ER_AAC_LC:
  319. case AOT_ER_AAC_LTP:
  320. case AOT_ER_AAC_SCALABLE:
  321. case AOT_ER_AAC_LD:
  322. skip_bits(gb, 3); /* aacSectionDataResilienceFlag
  323. * aacScalefactorDataResilienceFlag
  324. * aacSpectralDataResilienceFlag
  325. */
  326. break;
  327. }
  328. skip_bits1(gb); // extensionFlag3 (TBD in version 3)
  329. }
  330. return 0;
  331. }
  332. /**
  333. * Decode audio specific configuration; reference: table 1.13.
  334. *
  335. * @param data pointer to AVCodecContext extradata
  336. * @param data_size size of AVCCodecContext extradata
  337. *
  338. * @return Returns error status. 0 - OK, !0 - error
  339. */
  340. static int decode_audio_specific_config(AACContext * ac, void *data, int data_size) {
  341. GetBitContext gb;
  342. int i;
  343. init_get_bits(&gb, data, data_size * 8);
  344. if((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
  345. return -1;
  346. if(ac->m4ac.sampling_index > 12) {
  347. av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
  348. return -1;
  349. }
  350. skip_bits_long(&gb, i);
  351. switch (ac->m4ac.object_type) {
  352. case AOT_AAC_MAIN:
  353. case AOT_AAC_LC:
  354. if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
  355. return -1;
  356. break;
  357. default:
  358. av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
  359. ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
  360. return -1;
  361. }
  362. return 0;
  363. }
  364. /**
  365. * linear congruential pseudorandom number generator
  366. *
  367. * @param previous_val pointer to the current state of the generator
  368. *
  369. * @return Returns a 32-bit pseudorandom integer
  370. */
  371. static av_always_inline int lcg_random(int previous_val) {
  372. return previous_val * 1664525 + 1013904223;
  373. }
  374. static void reset_predict_state(PredictorState * ps) {
  375. ps->r0 = 0.0f;
  376. ps->r1 = 0.0f;
  377. ps->cor0 = 0.0f;
  378. ps->cor1 = 0.0f;
  379. ps->var0 = 1.0f;
  380. ps->var1 = 1.0f;
  381. }
  382. static void reset_all_predictors(PredictorState * ps) {
  383. int i;
  384. for (i = 0; i < MAX_PREDICTORS; i++)
  385. reset_predict_state(&ps[i]);
  386. }
  387. static void reset_predictor_group(PredictorState * ps, int group_num) {
  388. int i;
  389. for (i = group_num-1; i < MAX_PREDICTORS; i+=30)
  390. reset_predict_state(&ps[i]);
  391. }
  392. static av_cold int aac_decode_init(AVCodecContext * avccontext) {
  393. AACContext * ac = avccontext->priv_data;
  394. int i;
  395. ac->avccontext = avccontext;
  396. if (avccontext->extradata_size > 0) {
  397. if(decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size))
  398. return -1;
  399. avccontext->sample_rate = ac->m4ac.sample_rate;
  400. } else if (avccontext->channels > 0) {
  401. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  402. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  403. if(set_default_channel_config(ac, new_che_pos, avccontext->channels - (avccontext->channels == 8)))
  404. return -1;
  405. if(output_configure(ac, ac->che_pos, new_che_pos, 1))
  406. return -1;
  407. ac->m4ac.sample_rate = avccontext->sample_rate;
  408. }
  409. avccontext->sample_fmt = SAMPLE_FMT_S16;
  410. avccontext->frame_size = 1024;
  411. AAC_INIT_VLC_STATIC( 0, 144);
  412. AAC_INIT_VLC_STATIC( 1, 114);
  413. AAC_INIT_VLC_STATIC( 2, 188);
  414. AAC_INIT_VLC_STATIC( 3, 180);
  415. AAC_INIT_VLC_STATIC( 4, 172);
  416. AAC_INIT_VLC_STATIC( 5, 140);
  417. AAC_INIT_VLC_STATIC( 6, 168);
  418. AAC_INIT_VLC_STATIC( 7, 114);
  419. AAC_INIT_VLC_STATIC( 8, 262);
  420. AAC_INIT_VLC_STATIC( 9, 248);
  421. AAC_INIT_VLC_STATIC(10, 384);
  422. dsputil_init(&ac->dsp, avccontext);
  423. ac->random_state = 0x1f2e3d4c;
  424. // -1024 - Compensate wrong IMDCT method.
  425. // 32768 - Required to scale values to the correct range for the bias method
  426. // for float to int16 conversion.
  427. if(ac->dsp.float_to_int16 == ff_float_to_int16_c) {
  428. ac->add_bias = 385.0f;
  429. ac->sf_scale = 1. / (-1024. * 32768.);
  430. ac->sf_offset = 0;
  431. } else {
  432. ac->add_bias = 0.0f;
  433. ac->sf_scale = 1. / -1024.;
  434. ac->sf_offset = 60;
  435. }
  436. #if !CONFIG_HARDCODED_TABLES
  437. for (i = 0; i < 428; i++)
  438. ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.);
  439. #endif /* CONFIG_HARDCODED_TABLES */
  440. INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
  441. ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
  442. ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
  443. 352);
  444. ff_mdct_init(&ac->mdct, 11, 1, 1.0);
  445. ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
  446. // window initialization
  447. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  448. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  449. ff_sine_window_init(ff_sine_1024, 1024);
  450. ff_sine_window_init(ff_sine_128, 128);
  451. return 0;
  452. }
  453. /**
  454. * Skip data_stream_element; reference: table 4.10.
  455. */
  456. static void skip_data_stream_element(GetBitContext * gb) {
  457. int byte_align = get_bits1(gb);
  458. int count = get_bits(gb, 8);
  459. if (count == 255)
  460. count += get_bits(gb, 8);
  461. if (byte_align)
  462. align_get_bits(gb);
  463. skip_bits_long(gb, 8 * count);
  464. }
  465. static int decode_prediction(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb) {
  466. int sfb;
  467. if (get_bits1(gb)) {
  468. ics->predictor_reset_group = get_bits(gb, 5);
  469. if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
  470. av_log(ac->avccontext, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
  471. return -1;
  472. }
  473. }
  474. for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
  475. ics->prediction_used[sfb] = get_bits1(gb);
  476. }
  477. return 0;
  478. }
  479. /**
  480. * Decode Individual Channel Stream info; reference: table 4.6.
  481. *
  482. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  483. */
  484. static int decode_ics_info(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb, int common_window) {
  485. if (get_bits1(gb)) {
  486. av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n");
  487. memset(ics, 0, sizeof(IndividualChannelStream));
  488. return -1;
  489. }
  490. ics->window_sequence[1] = ics->window_sequence[0];
  491. ics->window_sequence[0] = get_bits(gb, 2);
  492. ics->use_kb_window[1] = ics->use_kb_window[0];
  493. ics->use_kb_window[0] = get_bits1(gb);
  494. ics->num_window_groups = 1;
  495. ics->group_len[0] = 1;
  496. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  497. int i;
  498. ics->max_sfb = get_bits(gb, 4);
  499. for (i = 0; i < 7; i++) {
  500. if (get_bits1(gb)) {
  501. ics->group_len[ics->num_window_groups-1]++;
  502. } else {
  503. ics->num_window_groups++;
  504. ics->group_len[ics->num_window_groups-1] = 1;
  505. }
  506. }
  507. ics->num_windows = 8;
  508. ics->swb_offset = swb_offset_128[ac->m4ac.sampling_index];
  509. ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
  510. ics->tns_max_bands = tns_max_bands_128[ac->m4ac.sampling_index];
  511. ics->predictor_present = 0;
  512. } else {
  513. ics->max_sfb = get_bits(gb, 6);
  514. ics->num_windows = 1;
  515. ics->swb_offset = swb_offset_1024[ac->m4ac.sampling_index];
  516. ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
  517. ics->tns_max_bands = tns_max_bands_1024[ac->m4ac.sampling_index];
  518. ics->predictor_present = get_bits1(gb);
  519. ics->predictor_reset_group = 0;
  520. if (ics->predictor_present) {
  521. if (ac->m4ac.object_type == AOT_AAC_MAIN) {
  522. if (decode_prediction(ac, ics, gb)) {
  523. memset(ics, 0, sizeof(IndividualChannelStream));
  524. return -1;
  525. }
  526. } else if (ac->m4ac.object_type == AOT_AAC_LC) {
  527. av_log(ac->avccontext, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
  528. memset(ics, 0, sizeof(IndividualChannelStream));
  529. return -1;
  530. } else {
  531. ff_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1);
  532. memset(ics, 0, sizeof(IndividualChannelStream));
  533. return -1;
  534. }
  535. }
  536. }
  537. if(ics->max_sfb > ics->num_swb) {
  538. av_log(ac->avccontext, AV_LOG_ERROR,
  539. "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
  540. ics->max_sfb, ics->num_swb);
  541. memset(ics, 0, sizeof(IndividualChannelStream));
  542. return -1;
  543. }
  544. return 0;
  545. }
  546. /**
  547. * Decode band types (section_data payload); reference: table 4.46.
  548. *
  549. * @param band_type array of the used band type
  550. * @param band_type_run_end array of the last scalefactor band of a band type run
  551. *
  552. * @return Returns error status. 0 - OK, !0 - error
  553. */
  554. static int decode_band_types(AACContext * ac, enum BandType band_type[120],
  555. int band_type_run_end[120], GetBitContext * gb, IndividualChannelStream * ics) {
  556. int g, idx = 0;
  557. const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
  558. for (g = 0; g < ics->num_window_groups; g++) {
  559. int k = 0;
  560. while (k < ics->max_sfb) {
  561. uint8_t sect_len = k;
  562. int sect_len_incr;
  563. int sect_band_type = get_bits(gb, 4);
  564. if (sect_band_type == 12) {
  565. av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n");
  566. return -1;
  567. }
  568. while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits)-1)
  569. sect_len += sect_len_incr;
  570. sect_len += sect_len_incr;
  571. if (sect_len > ics->max_sfb) {
  572. av_log(ac->avccontext, AV_LOG_ERROR,
  573. "Number of bands (%d) exceeds limit (%d).\n",
  574. sect_len, ics->max_sfb);
  575. return -1;
  576. }
  577. for (; k < sect_len; k++) {
  578. band_type [idx] = sect_band_type;
  579. band_type_run_end[idx++] = sect_len;
  580. }
  581. }
  582. }
  583. return 0;
  584. }
  585. /**
  586. * Decode scalefactors; reference: table 4.47.
  587. *
  588. * @param global_gain first scalefactor value as scalefactors are differentially coded
  589. * @param band_type array of the used band type
  590. * @param band_type_run_end array of the last scalefactor band of a band type run
  591. * @param sf array of scalefactors or intensity stereo positions
  592. *
  593. * @return Returns error status. 0 - OK, !0 - error
  594. */
  595. static int decode_scalefactors(AACContext * ac, float sf[120], GetBitContext * gb,
  596. unsigned int global_gain, IndividualChannelStream * ics,
  597. enum BandType band_type[120], int band_type_run_end[120]) {
  598. const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
  599. int g, i, idx = 0;
  600. int offset[3] = { global_gain, global_gain - 90, 100 };
  601. int noise_flag = 1;
  602. static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
  603. for (g = 0; g < ics->num_window_groups; g++) {
  604. for (i = 0; i < ics->max_sfb;) {
  605. int run_end = band_type_run_end[idx];
  606. if (band_type[idx] == ZERO_BT) {
  607. for(; i < run_end; i++, idx++)
  608. sf[idx] = 0.;
  609. }else if((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
  610. for(; i < run_end; i++, idx++) {
  611. offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  612. if(offset[2] > 255U) {
  613. av_log(ac->avccontext, AV_LOG_ERROR,
  614. "%s (%d) out of range.\n", sf_str[2], offset[2]);
  615. return -1;
  616. }
  617. sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
  618. }
  619. }else if(band_type[idx] == NOISE_BT) {
  620. for(; i < run_end; i++, idx++) {
  621. if(noise_flag-- > 0)
  622. offset[1] += get_bits(gb, 9) - 256;
  623. else
  624. offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  625. if(offset[1] > 255U) {
  626. av_log(ac->avccontext, AV_LOG_ERROR,
  627. "%s (%d) out of range.\n", sf_str[1], offset[1]);
  628. return -1;
  629. }
  630. sf[idx] = -ff_aac_pow2sf_tab[ offset[1] + sf_offset + 100];
  631. }
  632. }else {
  633. for(; i < run_end; i++, idx++) {
  634. offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  635. if(offset[0] > 255U) {
  636. av_log(ac->avccontext, AV_LOG_ERROR,
  637. "%s (%d) out of range.\n", sf_str[0], offset[0]);
  638. return -1;
  639. }
  640. sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
  641. }
  642. }
  643. }
  644. }
  645. return 0;
  646. }
  647. /**
  648. * Decode pulse data; reference: table 4.7.
  649. */
  650. static int decode_pulses(Pulse * pulse, GetBitContext * gb, const uint16_t * swb_offset, int num_swb) {
  651. int i, pulse_swb;
  652. pulse->num_pulse = get_bits(gb, 2) + 1;
  653. pulse_swb = get_bits(gb, 6);
  654. if (pulse_swb >= num_swb)
  655. return -1;
  656. pulse->pos[0] = swb_offset[pulse_swb];
  657. pulse->pos[0] += get_bits(gb, 5);
  658. if (pulse->pos[0] > 1023)
  659. return -1;
  660. pulse->amp[0] = get_bits(gb, 4);
  661. for (i = 1; i < pulse->num_pulse; i++) {
  662. pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i-1];
  663. if (pulse->pos[i] > 1023)
  664. return -1;
  665. pulse->amp[i] = get_bits(gb, 4);
  666. }
  667. return 0;
  668. }
  669. /**
  670. * Decode Temporal Noise Shaping data; reference: table 4.48.
  671. *
  672. * @return Returns error status. 0 - OK, !0 - error
  673. */
  674. static int decode_tns(AACContext * ac, TemporalNoiseShaping * tns,
  675. GetBitContext * gb, const IndividualChannelStream * ics) {
  676. int w, filt, i, coef_len, coef_res, coef_compress;
  677. const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  678. const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
  679. for (w = 0; w < ics->num_windows; w++) {
  680. if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
  681. coef_res = get_bits1(gb);
  682. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  683. int tmp2_idx;
  684. tns->length[w][filt] = get_bits(gb, 6 - 2*is8);
  685. if ((tns->order[w][filt] = get_bits(gb, 5 - 2*is8)) > tns_max_order) {
  686. av_log(ac->avccontext, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.",
  687. tns->order[w][filt], tns_max_order);
  688. tns->order[w][filt] = 0;
  689. return -1;
  690. }
  691. if (tns->order[w][filt]) {
  692. tns->direction[w][filt] = get_bits1(gb);
  693. coef_compress = get_bits1(gb);
  694. coef_len = coef_res + 3 - coef_compress;
  695. tmp2_idx = 2*coef_compress + coef_res;
  696. for (i = 0; i < tns->order[w][filt]; i++)
  697. tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
  698. }
  699. }
  700. }
  701. }
  702. return 0;
  703. }
  704. /**
  705. * Decode Mid/Side data; reference: table 4.54.
  706. *
  707. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  708. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  709. * [3] reserved for scalable AAC
  710. */
  711. static void decode_mid_side_stereo(ChannelElement * cpe, GetBitContext * gb,
  712. int ms_present) {
  713. int idx;
  714. if (ms_present == 1) {
  715. for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
  716. cpe->ms_mask[idx] = get_bits1(gb);
  717. } else if (ms_present == 2) {
  718. memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
  719. }
  720. }
  721. /**
  722. * Decode spectral data; reference: table 4.50.
  723. * Dequantize and scale spectral data; reference: 4.6.3.3.
  724. *
  725. * @param coef array of dequantized, scaled spectral data
  726. * @param sf array of scalefactors or intensity stereo positions
  727. * @param pulse_present set if pulses are present
  728. * @param pulse pointer to pulse data struct
  729. * @param band_type array of the used band type
  730. *
  731. * @return Returns error status. 0 - OK, !0 - error
  732. */
  733. static int decode_spectrum_and_dequant(AACContext * ac, float coef[1024], GetBitContext * gb, float sf[120],
  734. int pulse_present, const Pulse * pulse, const IndividualChannelStream * ics, enum BandType band_type[120]) {
  735. int i, k, g, idx = 0;
  736. const int c = 1024/ics->num_windows;
  737. const uint16_t * offsets = ics->swb_offset;
  738. float *coef_base = coef;
  739. static const float sign_lookup[] = { 1.0f, -1.0f };
  740. for (g = 0; g < ics->num_windows; g++)
  741. memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float)*(c - offsets[ics->max_sfb]));
  742. for (g = 0; g < ics->num_window_groups; g++) {
  743. for (i = 0; i < ics->max_sfb; i++, idx++) {
  744. const int cur_band_type = band_type[idx];
  745. const int dim = cur_band_type >= FIRST_PAIR_BT ? 2 : 4;
  746. const int is_cb_unsigned = IS_CODEBOOK_UNSIGNED(cur_band_type);
  747. int group;
  748. if (cur_band_type == ZERO_BT || cur_band_type == INTENSITY_BT2 || cur_band_type == INTENSITY_BT) {
  749. for (group = 0; group < ics->group_len[g]; group++) {
  750. memset(coef + group * 128 + offsets[i], 0, (offsets[i+1] - offsets[i])*sizeof(float));
  751. }
  752. }else if (cur_band_type == NOISE_BT) {
  753. for (group = 0; group < ics->group_len[g]; group++) {
  754. float scale;
  755. float band_energy = 0;
  756. for (k = offsets[i]; k < offsets[i+1]; k++) {
  757. ac->random_state = lcg_random(ac->random_state);
  758. coef[group*128+k] = ac->random_state;
  759. band_energy += coef[group*128+k]*coef[group*128+k];
  760. }
  761. scale = sf[idx] / sqrtf(band_energy);
  762. for (k = offsets[i]; k < offsets[i+1]; k++) {
  763. coef[group*128+k] *= scale;
  764. }
  765. }
  766. }else {
  767. for (group = 0; group < ics->group_len[g]; group++) {
  768. for (k = offsets[i]; k < offsets[i+1]; k += dim) {
  769. const int index = get_vlc2(gb, vlc_spectral[cur_band_type - 1].table, 6, 3);
  770. const int coef_tmp_idx = (group << 7) + k;
  771. const float *vq_ptr;
  772. int j;
  773. if(index >= ff_aac_spectral_sizes[cur_band_type - 1]) {
  774. av_log(ac->avccontext, AV_LOG_ERROR,
  775. "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n",
  776. cur_band_type - 1, index, ff_aac_spectral_sizes[cur_band_type - 1]);
  777. return -1;
  778. }
  779. vq_ptr = &ff_aac_codebook_vectors[cur_band_type - 1][index * dim];
  780. if (is_cb_unsigned) {
  781. if (vq_ptr[0]) coef[coef_tmp_idx ] = sign_lookup[get_bits1(gb)];
  782. if (vq_ptr[1]) coef[coef_tmp_idx + 1] = sign_lookup[get_bits1(gb)];
  783. if (dim == 4) {
  784. if (vq_ptr[2]) coef[coef_tmp_idx + 2] = sign_lookup[get_bits1(gb)];
  785. if (vq_ptr[3]) coef[coef_tmp_idx + 3] = sign_lookup[get_bits1(gb)];
  786. }
  787. if (cur_band_type == ESC_BT) {
  788. for (j = 0; j < 2; j++) {
  789. if (vq_ptr[j] == 64.0f) {
  790. int n = 4;
  791. /* The total length of escape_sequence must be < 22 bits according
  792. to the specification (i.e. max is 11111111110xxxxxxxxxx). */
  793. while (get_bits1(gb) && n < 15) n++;
  794. if(n == 15) {
  795. av_log(ac->avccontext, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
  796. return -1;
  797. }
  798. n = (1<<n) + get_bits(gb, n);
  799. coef[coef_tmp_idx + j] *= cbrtf(n) * n;
  800. }else
  801. coef[coef_tmp_idx + j] *= vq_ptr[j];
  802. }
  803. }else
  804. {
  805. coef[coef_tmp_idx ] *= vq_ptr[0];
  806. coef[coef_tmp_idx + 1] *= vq_ptr[1];
  807. if (dim == 4) {
  808. coef[coef_tmp_idx + 2] *= vq_ptr[2];
  809. coef[coef_tmp_idx + 3] *= vq_ptr[3];
  810. }
  811. }
  812. }else {
  813. coef[coef_tmp_idx ] = vq_ptr[0];
  814. coef[coef_tmp_idx + 1] = vq_ptr[1];
  815. if (dim == 4) {
  816. coef[coef_tmp_idx + 2] = vq_ptr[2];
  817. coef[coef_tmp_idx + 3] = vq_ptr[3];
  818. }
  819. }
  820. coef[coef_tmp_idx ] *= sf[idx];
  821. coef[coef_tmp_idx + 1] *= sf[idx];
  822. if (dim == 4) {
  823. coef[coef_tmp_idx + 2] *= sf[idx];
  824. coef[coef_tmp_idx + 3] *= sf[idx];
  825. }
  826. }
  827. }
  828. }
  829. }
  830. coef += ics->group_len[g]<<7;
  831. }
  832. if (pulse_present) {
  833. idx = 0;
  834. for(i = 0; i < pulse->num_pulse; i++){
  835. float co = coef_base[ pulse->pos[i] ];
  836. while(offsets[idx + 1] <= pulse->pos[i])
  837. idx++;
  838. if (band_type[idx] != NOISE_BT && sf[idx]) {
  839. float ico = -pulse->amp[i];
  840. if (co) {
  841. co /= sf[idx];
  842. ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
  843. }
  844. coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
  845. }
  846. }
  847. }
  848. return 0;
  849. }
  850. static av_always_inline float flt16_round(float pf) {
  851. union float754 tmp;
  852. tmp.f = pf;
  853. tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
  854. return tmp.f;
  855. }
  856. static av_always_inline float flt16_even(float pf) {
  857. union float754 tmp;
  858. tmp.f = pf;
  859. tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U>>16)) & 0xFFFF0000U;
  860. return tmp.f;
  861. }
  862. static av_always_inline float flt16_trunc(float pf) {
  863. union float754 pun;
  864. pun.f = pf;
  865. pun.i &= 0xFFFF0000U;
  866. return pun.f;
  867. }
  868. static void predict(AACContext * ac, PredictorState * ps, float* coef, int output_enable) {
  869. const float a = 0.953125; // 61.0/64
  870. const float alpha = 0.90625; // 29.0/32
  871. float e0, e1;
  872. float pv;
  873. float k1, k2;
  874. k1 = ps->var0 > 1 ? ps->cor0 * flt16_even(a / ps->var0) : 0;
  875. k2 = ps->var1 > 1 ? ps->cor1 * flt16_even(a / ps->var1) : 0;
  876. pv = flt16_round(k1 * ps->r0 + k2 * ps->r1);
  877. if (output_enable)
  878. *coef += pv * ac->sf_scale;
  879. e0 = *coef / ac->sf_scale;
  880. e1 = e0 - k1 * ps->r0;
  881. ps->cor1 = flt16_trunc(alpha * ps->cor1 + ps->r1 * e1);
  882. ps->var1 = flt16_trunc(alpha * ps->var1 + 0.5 * (ps->r1 * ps->r1 + e1 * e1));
  883. ps->cor0 = flt16_trunc(alpha * ps->cor0 + ps->r0 * e0);
  884. ps->var0 = flt16_trunc(alpha * ps->var0 + 0.5 * (ps->r0 * ps->r0 + e0 * e0));
  885. ps->r1 = flt16_trunc(a * (ps->r0 - k1 * e0));
  886. ps->r0 = flt16_trunc(a * e0);
  887. }
  888. /**
  889. * Apply AAC-Main style frequency domain prediction.
  890. */
  891. static void apply_prediction(AACContext * ac, SingleChannelElement * sce) {
  892. int sfb, k;
  893. if (!sce->ics.predictor_initialized) {
  894. reset_all_predictors(sce->predictor_state);
  895. sce->ics.predictor_initialized = 1;
  896. }
  897. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  898. for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
  899. for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
  900. predict(ac, &sce->predictor_state[k], &sce->coeffs[k],
  901. sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
  902. }
  903. }
  904. if (sce->ics.predictor_reset_group)
  905. reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
  906. } else
  907. reset_all_predictors(sce->predictor_state);
  908. }
  909. /**
  910. * Decode an individual_channel_stream payload; reference: table 4.44.
  911. *
  912. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  913. * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
  914. *
  915. * @return Returns error status. 0 - OK, !0 - error
  916. */
  917. static int decode_ics(AACContext * ac, SingleChannelElement * sce, GetBitContext * gb, int common_window, int scale_flag) {
  918. Pulse pulse;
  919. TemporalNoiseShaping * tns = &sce->tns;
  920. IndividualChannelStream * ics = &sce->ics;
  921. float * out = sce->coeffs;
  922. int global_gain, pulse_present = 0;
  923. /* This assignment is to silence a GCC warning about the variable being used
  924. * uninitialized when in fact it always is.
  925. */
  926. pulse.num_pulse = 0;
  927. global_gain = get_bits(gb, 8);
  928. if (!common_window && !scale_flag) {
  929. if (decode_ics_info(ac, ics, gb, 0) < 0)
  930. return -1;
  931. }
  932. if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
  933. return -1;
  934. if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
  935. return -1;
  936. pulse_present = 0;
  937. if (!scale_flag) {
  938. if ((pulse_present = get_bits1(gb))) {
  939. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  940. av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
  941. return -1;
  942. }
  943. if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
  944. av_log(ac->avccontext, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
  945. return -1;
  946. }
  947. }
  948. if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
  949. return -1;
  950. if (get_bits1(gb)) {
  951. ff_log_missing_feature(ac->avccontext, "SSR", 1);
  952. return -1;
  953. }
  954. }
  955. if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
  956. return -1;
  957. if(ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
  958. apply_prediction(ac, sce);
  959. return 0;
  960. }
  961. /**
  962. * Mid/Side stereo decoding; reference: 4.6.8.1.3.
  963. */
  964. static void apply_mid_side_stereo(ChannelElement * cpe) {
  965. const IndividualChannelStream * ics = &cpe->ch[0].ics;
  966. float *ch0 = cpe->ch[0].coeffs;
  967. float *ch1 = cpe->ch[1].coeffs;
  968. int g, i, k, group, idx = 0;
  969. const uint16_t * offsets = ics->swb_offset;
  970. for (g = 0; g < ics->num_window_groups; g++) {
  971. for (i = 0; i < ics->max_sfb; i++, idx++) {
  972. if (cpe->ms_mask[idx] &&
  973. cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
  974. for (group = 0; group < ics->group_len[g]; group++) {
  975. for (k = offsets[i]; k < offsets[i+1]; k++) {
  976. float tmp = ch0[group*128 + k] - ch1[group*128 + k];
  977. ch0[group*128 + k] += ch1[group*128 + k];
  978. ch1[group*128 + k] = tmp;
  979. }
  980. }
  981. }
  982. }
  983. ch0 += ics->group_len[g]*128;
  984. ch1 += ics->group_len[g]*128;
  985. }
  986. }
  987. /**
  988. * intensity stereo decoding; reference: 4.6.8.2.3
  989. *
  990. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  991. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  992. * [3] reserved for scalable AAC
  993. */
  994. static void apply_intensity_stereo(ChannelElement * cpe, int ms_present) {
  995. const IndividualChannelStream * ics = &cpe->ch[1].ics;
  996. SingleChannelElement * sce1 = &cpe->ch[1];
  997. float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
  998. const uint16_t * offsets = ics->swb_offset;
  999. int g, group, i, k, idx = 0;
  1000. int c;
  1001. float scale;
  1002. for (g = 0; g < ics->num_window_groups; g++) {
  1003. for (i = 0; i < ics->max_sfb;) {
  1004. if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
  1005. const int bt_run_end = sce1->band_type_run_end[idx];
  1006. for (; i < bt_run_end; i++, idx++) {
  1007. c = -1 + 2 * (sce1->band_type[idx] - 14);
  1008. if (ms_present)
  1009. c *= 1 - 2 * cpe->ms_mask[idx];
  1010. scale = c * sce1->sf[idx];
  1011. for (group = 0; group < ics->group_len[g]; group++)
  1012. for (k = offsets[i]; k < offsets[i+1]; k++)
  1013. coef1[group*128 + k] = scale * coef0[group*128 + k];
  1014. }
  1015. } else {
  1016. int bt_run_end = sce1->band_type_run_end[idx];
  1017. idx += bt_run_end - i;
  1018. i = bt_run_end;
  1019. }
  1020. }
  1021. coef0 += ics->group_len[g]*128;
  1022. coef1 += ics->group_len[g]*128;
  1023. }
  1024. }
  1025. /**
  1026. * Decode a channel_pair_element; reference: table 4.4.
  1027. *
  1028. * @param elem_id Identifies the instance of a syntax element.
  1029. *
  1030. * @return Returns error status. 0 - OK, !0 - error
  1031. */
  1032. static int decode_cpe(AACContext * ac, GetBitContext * gb, ChannelElement * cpe) {
  1033. int i, ret, common_window, ms_present = 0;
  1034. common_window = get_bits1(gb);
  1035. if (common_window) {
  1036. if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
  1037. return -1;
  1038. i = cpe->ch[1].ics.use_kb_window[0];
  1039. cpe->ch[1].ics = cpe->ch[0].ics;
  1040. cpe->ch[1].ics.use_kb_window[1] = i;
  1041. ms_present = get_bits(gb, 2);
  1042. if(ms_present == 3) {
  1043. av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
  1044. return -1;
  1045. } else if(ms_present)
  1046. decode_mid_side_stereo(cpe, gb, ms_present);
  1047. }
  1048. if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
  1049. return ret;
  1050. if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
  1051. return ret;
  1052. if (common_window) {
  1053. if (ms_present)
  1054. apply_mid_side_stereo(cpe);
  1055. if (ac->m4ac.object_type == AOT_AAC_MAIN) {
  1056. apply_prediction(ac, &cpe->ch[0]);
  1057. apply_prediction(ac, &cpe->ch[1]);
  1058. }
  1059. }
  1060. apply_intensity_stereo(cpe, ms_present);
  1061. return 0;
  1062. }
  1063. /**
  1064. * Decode coupling_channel_element; reference: table 4.8.
  1065. *
  1066. * @param elem_id Identifies the instance of a syntax element.
  1067. *
  1068. * @return Returns error status. 0 - OK, !0 - error
  1069. */
  1070. static int decode_cce(AACContext * ac, GetBitContext * gb, ChannelElement * che) {
  1071. int num_gain = 0;
  1072. int c, g, sfb, ret;
  1073. int sign;
  1074. float scale;
  1075. SingleChannelElement * sce = &che->ch[0];
  1076. ChannelCoupling * coup = &che->coup;
  1077. coup->coupling_point = 2*get_bits1(gb);
  1078. coup->num_coupled = get_bits(gb, 3);
  1079. for (c = 0; c <= coup->num_coupled; c++) {
  1080. num_gain++;
  1081. coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
  1082. coup->id_select[c] = get_bits(gb, 4);
  1083. if (coup->type[c] == TYPE_CPE) {
  1084. coup->ch_select[c] = get_bits(gb, 2);
  1085. if (coup->ch_select[c] == 3)
  1086. num_gain++;
  1087. } else
  1088. coup->ch_select[c] = 2;
  1089. }
  1090. coup->coupling_point += get_bits1(gb) || (coup->coupling_point>>1);
  1091. sign = get_bits(gb, 1);
  1092. scale = pow(2., pow(2., (int)get_bits(gb, 2) - 3));
  1093. if ((ret = decode_ics(ac, sce, gb, 0, 0)))
  1094. return ret;
  1095. for (c = 0; c < num_gain; c++) {
  1096. int idx = 0;
  1097. int cge = 1;
  1098. int gain = 0;
  1099. float gain_cache = 1.;
  1100. if (c) {
  1101. cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
  1102. gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
  1103. gain_cache = pow(scale, -gain);
  1104. }
  1105. if (coup->coupling_point == AFTER_IMDCT) {
  1106. coup->gain[c][0] = gain_cache;
  1107. } else {
  1108. for (g = 0; g < sce->ics.num_window_groups; g++) {
  1109. for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
  1110. if (sce->band_type[idx] != ZERO_BT) {
  1111. if (!cge) {
  1112. int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1113. if (t) {
  1114. int s = 1;
  1115. t = gain += t;
  1116. if (sign) {
  1117. s -= 2 * (t & 0x1);
  1118. t >>= 1;
  1119. }
  1120. gain_cache = pow(scale, -t) * s;
  1121. }
  1122. }
  1123. coup->gain[c][idx] = gain_cache;
  1124. }
  1125. }
  1126. }
  1127. }
  1128. }
  1129. return 0;
  1130. }
  1131. /**
  1132. * Decode Spectral Band Replication extension data; reference: table 4.55.
  1133. *
  1134. * @param crc flag indicating the presence of CRC checksum
  1135. * @param cnt length of TYPE_FIL syntactic element in bytes
  1136. *
  1137. * @return Returns number of bytes consumed from the TYPE_FIL element.
  1138. */
  1139. static int decode_sbr_extension(AACContext * ac, GetBitContext * gb, int crc, int cnt) {
  1140. // TODO : sbr_extension implementation
  1141. ff_log_missing_feature(ac->avccontext, "SBR", 0);
  1142. skip_bits_long(gb, 8*cnt - 4); // -4 due to reading extension type
  1143. return cnt;
  1144. }
  1145. /**
  1146. * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
  1147. *
  1148. * @return Returns number of bytes consumed.
  1149. */
  1150. static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, GetBitContext * gb) {
  1151. int i;
  1152. int num_excl_chan = 0;
  1153. do {
  1154. for (i = 0; i < 7; i++)
  1155. che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
  1156. } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
  1157. return num_excl_chan / 7;
  1158. }
  1159. /**
  1160. * Decode dynamic range information; reference: table 4.52.
  1161. *
  1162. * @param cnt length of TYPE_FIL syntactic element in bytes
  1163. *
  1164. * @return Returns number of bytes consumed.
  1165. */
  1166. static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext * gb, int cnt) {
  1167. int n = 1;
  1168. int drc_num_bands = 1;
  1169. int i;
  1170. /* pce_tag_present? */
  1171. if(get_bits1(gb)) {
  1172. che_drc->pce_instance_tag = get_bits(gb, 4);
  1173. skip_bits(gb, 4); // tag_reserved_bits
  1174. n++;
  1175. }
  1176. /* excluded_chns_present? */
  1177. if(get_bits1(gb)) {
  1178. n += decode_drc_channel_exclusions(che_drc, gb);
  1179. }
  1180. /* drc_bands_present? */
  1181. if (get_bits1(gb)) {
  1182. che_drc->band_incr = get_bits(gb, 4);
  1183. che_drc->interpolation_scheme = get_bits(gb, 4);
  1184. n++;
  1185. drc_num_bands += che_drc->band_incr;
  1186. for (i = 0; i < drc_num_bands; i++) {
  1187. che_drc->band_top[i] = get_bits(gb, 8);
  1188. n++;
  1189. }
  1190. }
  1191. /* prog_ref_level_present? */
  1192. if (get_bits1(gb)) {
  1193. che_drc->prog_ref_level = get_bits(gb, 7);
  1194. skip_bits1(gb); // prog_ref_level_reserved_bits
  1195. n++;
  1196. }
  1197. for (i = 0; i < drc_num_bands; i++) {
  1198. che_drc->dyn_rng_sgn[i] = get_bits1(gb);
  1199. che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
  1200. n++;
  1201. }
  1202. return n;
  1203. }
  1204. /**
  1205. * Decode extension data (incomplete); reference: table 4.51.
  1206. *
  1207. * @param cnt length of TYPE_FIL syntactic element in bytes
  1208. *
  1209. * @return Returns number of bytes consumed
  1210. */
  1211. static int decode_extension_payload(AACContext * ac, GetBitContext * gb, int cnt) {
  1212. int crc_flag = 0;
  1213. int res = cnt;
  1214. switch (get_bits(gb, 4)) { // extension type
  1215. case EXT_SBR_DATA_CRC:
  1216. crc_flag++;
  1217. case EXT_SBR_DATA:
  1218. res = decode_sbr_extension(ac, gb, crc_flag, cnt);
  1219. break;
  1220. case EXT_DYNAMIC_RANGE:
  1221. res = decode_dynamic_range(&ac->che_drc, gb, cnt);
  1222. break;
  1223. case EXT_FILL:
  1224. case EXT_FILL_DATA:
  1225. case EXT_DATA_ELEMENT:
  1226. default:
  1227. skip_bits_long(gb, 8*cnt - 4);
  1228. break;
  1229. };
  1230. return res;
  1231. }
  1232. /**
  1233. * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
  1234. *
  1235. * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
  1236. * @param coef spectral coefficients
  1237. */
  1238. static void apply_tns(float coef[1024], TemporalNoiseShaping * tns, IndividualChannelStream * ics, int decode) {
  1239. const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
  1240. int w, filt, m, i;
  1241. int bottom, top, order, start, end, size, inc;
  1242. float lpc[TNS_MAX_ORDER];
  1243. for (w = 0; w < ics->num_windows; w++) {
  1244. bottom = ics->num_swb;
  1245. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  1246. top = bottom;
  1247. bottom = FFMAX(0, top - tns->length[w][filt]);
  1248. order = tns->order[w][filt];
  1249. if (order == 0)
  1250. continue;
  1251. // tns_decode_coef
  1252. compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
  1253. start = ics->swb_offset[FFMIN(bottom, mmm)];
  1254. end = ics->swb_offset[FFMIN( top, mmm)];
  1255. if ((size = end - start) <= 0)
  1256. continue;
  1257. if (tns->direction[w][filt]) {
  1258. inc = -1; start = end - 1;
  1259. } else {
  1260. inc = 1;
  1261. }
  1262. start += w * 128;
  1263. // ar filter
  1264. for (m = 0; m < size; m++, start += inc)
  1265. for (i = 1; i <= FFMIN(m, order); i++)
  1266. coef[start] -= coef[start - i*inc] * lpc[i-1];
  1267. }
  1268. }
  1269. }
  1270. /**
  1271. * Conduct IMDCT and windowing.
  1272. */
  1273. static void imdct_and_windowing(AACContext * ac, SingleChannelElement * sce) {
  1274. IndividualChannelStream * ics = &sce->ics;
  1275. float * in = sce->coeffs;
  1276. float * out = sce->ret;
  1277. float * saved = sce->saved;
  1278. const float * swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  1279. const float * lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1280. const float * swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  1281. float * buf = ac->buf_mdct;
  1282. float * temp = ac->temp;
  1283. int i;
  1284. // imdct
  1285. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1286. if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE)
  1287. av_log(ac->avccontext, AV_LOG_WARNING,
  1288. "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. "
  1289. "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n");
  1290. for (i = 0; i < 1024; i += 128)
  1291. ff_imdct_half(&ac->mdct_small, buf + i, in + i);
  1292. } else
  1293. ff_imdct_half(&ac->mdct, buf, in);
  1294. /* window overlapping
  1295. * NOTE: To simplify the overlapping code, all 'meaningless' short to long
  1296. * and long to short transitions are considered to be short to short
  1297. * transitions. This leaves just two cases (long to long and short to short)
  1298. * with a little special sauce for EIGHT_SHORT_SEQUENCE.
  1299. */
  1300. if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
  1301. (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
  1302. ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, ac->add_bias, 512);
  1303. } else {
  1304. for (i = 0; i < 448; i++)
  1305. out[i] = saved[i] + ac->add_bias;
  1306. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1307. ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, ac->add_bias, 64);
  1308. ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, ac->add_bias, 64);
  1309. ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, ac->add_bias, 64);
  1310. ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, ac->add_bias, 64);
  1311. ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, ac->add_bias, 64);
  1312. memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
  1313. } else {
  1314. ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, ac->add_bias, 64);
  1315. for (i = 576; i < 1024; i++)
  1316. out[i] = buf[i-512] + ac->add_bias;
  1317. }
  1318. }
  1319. // buffer update
  1320. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1321. for (i = 0; i < 64; i++)
  1322. saved[i] = temp[64 + i] - ac->add_bias;
  1323. ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
  1324. ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
  1325. ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
  1326. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  1327. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  1328. memcpy( saved, buf + 512, 448 * sizeof(float));
  1329. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  1330. } else { // LONG_STOP or ONLY_LONG
  1331. memcpy( saved, buf + 512, 512 * sizeof(float));
  1332. }
  1333. }
  1334. /**
  1335. * Apply dependent channel coupling (applied before IMDCT).
  1336. *
  1337. * @param index index into coupling gain array
  1338. */
  1339. static void apply_dependent_coupling(AACContext * ac, SingleChannelElement * target, ChannelElement * cce, int index) {
  1340. IndividualChannelStream * ics = &cce->ch[0].ics;
  1341. const uint16_t * offsets = ics->swb_offset;
  1342. float * dest = target->coeffs;
  1343. const float * src = cce->ch[0].coeffs;
  1344. int g, i, group, k, idx = 0;
  1345. if(ac->m4ac.object_type == AOT_AAC_LTP) {
  1346. av_log(ac->avccontext, AV_LOG_ERROR,
  1347. "Dependent coupling is not supported together with LTP\n");
  1348. return;
  1349. }
  1350. for (g = 0; g < ics->num_window_groups; g++) {
  1351. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1352. if (cce->ch[0].band_type[idx] != ZERO_BT) {
  1353. const float gain = cce->coup.gain[index][idx];
  1354. for (group = 0; group < ics->group_len[g]; group++) {
  1355. for (k = offsets[i]; k < offsets[i+1]; k++) {
  1356. // XXX dsputil-ize
  1357. dest[group*128+k] += gain * src[group*128+k];
  1358. }
  1359. }
  1360. }
  1361. }
  1362. dest += ics->group_len[g]*128;
  1363. src += ics->group_len[g]*128;
  1364. }
  1365. }
  1366. /**
  1367. * Apply independent channel coupling (applied after IMDCT).
  1368. *
  1369. * @param index index into coupling gain array
  1370. */
  1371. static void apply_independent_coupling(AACContext * ac, SingleChannelElement * target, ChannelElement * cce, int index) {
  1372. int i;
  1373. const float gain = cce->coup.gain[index][0];
  1374. const float bias = ac->add_bias;
  1375. const float* src = cce->ch[0].ret;
  1376. float* dest = target->ret;
  1377. for (i = 0; i < 1024; i++)
  1378. dest[i] += gain * (src[i] - bias);
  1379. }
  1380. /**
  1381. * channel coupling transformation interface
  1382. *
  1383. * @param index index into coupling gain array
  1384. * @param apply_coupling_method pointer to (in)dependent coupling function
  1385. */
  1386. static void apply_channel_coupling(AACContext * ac, ChannelElement * cc,
  1387. enum RawDataBlockType type, int elem_id, enum CouplingPoint coupling_point,
  1388. void (*apply_coupling_method)(AACContext * ac, SingleChannelElement * target, ChannelElement * cce, int index))
  1389. {
  1390. int i, c;
  1391. for (i = 0; i < MAX_ELEM_ID; i++) {
  1392. ChannelElement *cce = ac->che[TYPE_CCE][i];
  1393. int index = 0;
  1394. if (cce && cce->coup.coupling_point == coupling_point) {
  1395. ChannelCoupling * coup = &cce->coup;
  1396. for (c = 0; c <= coup->num_coupled; c++) {
  1397. if (coup->type[c] == type && coup->id_select[c] == elem_id) {
  1398. if (coup->ch_select[c] != 1) {
  1399. apply_coupling_method(ac, &cc->ch[0], cce, index);
  1400. if (coup->ch_select[c] != 0)
  1401. index++;
  1402. }
  1403. if (coup->ch_select[c] != 2)
  1404. apply_coupling_method(ac, &cc->ch[1], cce, index++);
  1405. } else
  1406. index += 1 + (coup->ch_select[c] == 3);
  1407. }
  1408. }
  1409. }
  1410. }
  1411. /**
  1412. * Convert spectral data to float samples, applying all supported tools as appropriate.
  1413. */
  1414. static void spectral_to_sample(AACContext * ac) {
  1415. int i, type;
  1416. for(type = 3; type >= 0; type--) {
  1417. for (i = 0; i < MAX_ELEM_ID; i++) {
  1418. ChannelElement *che = ac->che[type][i];
  1419. if(che) {
  1420. if(type <= TYPE_CPE)
  1421. apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
  1422. if(che->ch[0].tns.present)
  1423. apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
  1424. if(che->ch[1].tns.present)
  1425. apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
  1426. if(type <= TYPE_CPE)
  1427. apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
  1428. if(type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT)
  1429. imdct_and_windowing(ac, &che->ch[0]);
  1430. if(type == TYPE_CPE)
  1431. imdct_and_windowing(ac, &che->ch[1]);
  1432. if(type <= TYPE_CCE)
  1433. apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
  1434. }
  1435. }
  1436. }
  1437. }
  1438. static int parse_adts_frame_header(AACContext * ac, GetBitContext * gb) {
  1439. int size;
  1440. AACADTSHeaderInfo hdr_info;
  1441. size = ff_aac_parse_header(gb, &hdr_info);
  1442. if (size > 0) {
  1443. if (hdr_info.chan_config)
  1444. ac->m4ac.chan_config = hdr_info.chan_config;
  1445. ac->m4ac.sample_rate = hdr_info.sample_rate;
  1446. ac->m4ac.sampling_index = hdr_info.sampling_index;
  1447. ac->m4ac.object_type = hdr_info.object_type;
  1448. if (hdr_info.num_aac_frames == 1) {
  1449. if (!hdr_info.crc_absent)
  1450. skip_bits(gb, 16);
  1451. } else {
  1452. ff_log_missing_feature(ac->avccontext, "More than one AAC RDB per ADTS frame is", 0);
  1453. return -1;
  1454. }
  1455. }
  1456. return size;
  1457. }
  1458. static int aac_decode_frame(AVCodecContext * avccontext, void * data, int * data_size, AVPacket *avpkt) {
  1459. const uint8_t *buf = avpkt->data;
  1460. int buf_size = avpkt->size;
  1461. AACContext * ac = avccontext->priv_data;
  1462. ChannelElement * che = NULL;
  1463. GetBitContext gb;
  1464. enum RawDataBlockType elem_type;
  1465. int err, elem_id, data_size_tmp;
  1466. init_get_bits(&gb, buf, buf_size*8);
  1467. if (show_bits(&gb, 12) == 0xfff) {
  1468. if (parse_adts_frame_header(ac, &gb) < 0) {
  1469. av_log(avccontext, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
  1470. return -1;
  1471. }
  1472. if (ac->m4ac.sampling_index > 12) {
  1473. av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
  1474. return -1;
  1475. }
  1476. }
  1477. // parse
  1478. while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
  1479. elem_id = get_bits(&gb, 4);
  1480. if(elem_type < TYPE_DSE && !(che=get_che(ac, elem_type, elem_id))) {
  1481. av_log(ac->avccontext, AV_LOG_ERROR, "channel element %d.%d is not allocated\n", elem_type, elem_id);
  1482. return -1;
  1483. }
  1484. switch (elem_type) {
  1485. case TYPE_SCE:
  1486. err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
  1487. break;
  1488. case TYPE_CPE:
  1489. err = decode_cpe(ac, &gb, che);
  1490. break;
  1491. case TYPE_CCE:
  1492. err = decode_cce(ac, &gb, che);
  1493. break;
  1494. case TYPE_LFE:
  1495. err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
  1496. break;
  1497. case TYPE_DSE:
  1498. skip_data_stream_element(&gb);
  1499. err = 0;
  1500. break;
  1501. case TYPE_PCE:
  1502. {
  1503. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  1504. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  1505. if((err = decode_pce(ac, new_che_pos, &gb)))
  1506. break;
  1507. err = output_configure(ac, ac->che_pos, new_che_pos, 0);
  1508. break;
  1509. }
  1510. case TYPE_FIL:
  1511. if (elem_id == 15)
  1512. elem_id += get_bits(&gb, 8) - 1;
  1513. while (elem_id > 0)
  1514. elem_id -= decode_extension_payload(ac, &gb, elem_id);
  1515. err = 0; /* FIXME */
  1516. break;
  1517. default:
  1518. err = -1; /* should not happen, but keeps compiler happy */
  1519. break;
  1520. }
  1521. if(err)
  1522. return err;
  1523. }
  1524. spectral_to_sample(ac);
  1525. if (!ac->is_saved) {
  1526. ac->is_saved = 1;
  1527. *data_size = 0;
  1528. return buf_size;
  1529. }
  1530. data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t);
  1531. if(*data_size < data_size_tmp) {
  1532. av_log(avccontext, AV_LOG_ERROR,
  1533. "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
  1534. *data_size, data_size_tmp);
  1535. return -1;
  1536. }
  1537. *data_size = data_size_tmp;
  1538. ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels);
  1539. return buf_size;
  1540. }
  1541. static av_cold int aac_decode_close(AVCodecContext * avccontext) {
  1542. AACContext * ac = avccontext->priv_data;
  1543. int i, type;
  1544. for (i = 0; i < MAX_ELEM_ID; i++) {
  1545. for(type = 0; type < 4; type++)
  1546. av_freep(&ac->che[type][i]);
  1547. }
  1548. ff_mdct_end(&ac->mdct);
  1549. ff_mdct_end(&ac->mdct_small);
  1550. return 0 ;
  1551. }
  1552. AVCodec aac_decoder = {
  1553. "aac",
  1554. CODEC_TYPE_AUDIO,
  1555. CODEC_ID_AAC,
  1556. sizeof(AACContext),
  1557. aac_decode_init,
  1558. NULL,
  1559. aac_decode_close,
  1560. aac_decode_frame,
  1561. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  1562. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  1563. };