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.

1678 lines
61KB

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