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.

838 lines
28KB

  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. * N 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 "bitstream.h"
  77. #include "dsputil.h"
  78. #include "aac.h"
  79. #include "aactab.h"
  80. #include "aacdectab.h"
  81. #include "mpeg4audio.h"
  82. #include <assert.h>
  83. #include <errno.h>
  84. #include <math.h>
  85. #include <string.h>
  86. #ifndef CONFIG_HARDCODED_TABLES
  87. static float ff_aac_ivquant_tab[IVQUANT_SIZE];
  88. static float ff_aac_pow2sf_tab[316];
  89. #endif /* CONFIG_HARDCODED_TABLES */
  90. static VLC vlc_scalefactors;
  91. static VLC vlc_spectral[11];
  92. /**
  93. * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
  94. *
  95. * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present.
  96. * @param sce_map mono (Single Channel Element) map
  97. * @param type speaker type/position for these channels
  98. */
  99. static void decode_channel_map(enum ChannelPosition *cpe_map,
  100. enum ChannelPosition *sce_map, enum ChannelPosition type, GetBitContext * gb, int n) {
  101. while(n--) {
  102. enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
  103. map[get_bits(gb, 4)] = type;
  104. }
  105. }
  106. /**
  107. * Decode program configuration element; reference: table 4.2.
  108. *
  109. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  110. *
  111. * @return Returns error status. 0 - OK, !0 - error
  112. */
  113. static int decode_pce(AACContext * ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  114. GetBitContext * gb) {
  115. int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
  116. skip_bits(gb, 2); // object_type
  117. ac->m4ac.sampling_index = get_bits(gb, 4);
  118. if(ac->m4ac.sampling_index > 11) {
  119. av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
  120. return -1;
  121. }
  122. ac->m4ac.sample_rate = ff_mpeg4audio_sample_rates[ac->m4ac.sampling_index];
  123. num_front = get_bits(gb, 4);
  124. num_side = get_bits(gb, 4);
  125. num_back = get_bits(gb, 4);
  126. num_lfe = get_bits(gb, 2);
  127. num_assoc_data = get_bits(gb, 3);
  128. num_cc = get_bits(gb, 4);
  129. if (get_bits1(gb))
  130. skip_bits(gb, 4); // mono_mixdown_tag
  131. if (get_bits1(gb))
  132. skip_bits(gb, 4); // stereo_mixdown_tag
  133. if (get_bits1(gb))
  134. skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
  135. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
  136. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
  137. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
  138. decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
  139. skip_bits_long(gb, 4 * num_assoc_data);
  140. decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
  141. align_get_bits(gb);
  142. /* comment field, first byte is length */
  143. skip_bits_long(gb, 8 * get_bits(gb, 8));
  144. return 0;
  145. }
  146. /**
  147. * Set up channel positions based on a default channel configuration
  148. * as specified in table 1.17.
  149. *
  150. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  151. *
  152. * @return Returns error status. 0 - OK, !0 - error
  153. */
  154. static int set_default_channel_config(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  155. int channel_config)
  156. {
  157. if(channel_config < 1 || channel_config > 7) {
  158. av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
  159. channel_config);
  160. return -1;
  161. }
  162. /* default channel configurations:
  163. *
  164. * 1ch : front center (mono)
  165. * 2ch : L + R (stereo)
  166. * 3ch : front center + L + R
  167. * 4ch : front center + L + R + back center
  168. * 5ch : front center + L + R + back stereo
  169. * 6ch : front center + L + R + back stereo + LFE
  170. * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
  171. */
  172. if(channel_config != 2)
  173. new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
  174. if(channel_config > 1)
  175. new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
  176. if(channel_config == 4)
  177. new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK; // back center
  178. if(channel_config > 4)
  179. new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
  180. = AAC_CHANNEL_BACK; // back stereo
  181. if(channel_config > 5)
  182. new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE; // LFE
  183. if(channel_config == 7)
  184. new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
  185. return 0;
  186. }
  187. return -1;
  188. }
  189. if (get_bits1(gb)) // dependsOnCoreCoder
  190. skip_bits(gb, 14); // coreCoderDelay
  191. extension_flag = get_bits1(gb);
  192. if(ac->m4ac.object_type == AOT_AAC_SCALABLE ||
  193. ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
  194. skip_bits(gb, 3); // layerNr
  195. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  196. if (channel_config == 0) {
  197. skip_bits(gb, 4); // element_instance_tag
  198. if((ret = decode_pce(ac, new_che_pos, gb)))
  199. return ret;
  200. } else {
  201. if((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
  202. return ret;
  203. }
  204. if((ret = output_configure(ac, ac->che_pos, new_che_pos)))
  205. return ret;
  206. if (extension_flag) {
  207. switch (ac->m4ac.object_type) {
  208. case AOT_ER_BSAC:
  209. skip_bits(gb, 5); // numOfSubFrame
  210. skip_bits(gb, 11); // layer_length
  211. break;
  212. case AOT_ER_AAC_LC:
  213. case AOT_ER_AAC_LTP:
  214. case AOT_ER_AAC_SCALABLE:
  215. case AOT_ER_AAC_LD:
  216. skip_bits(gb, 3); /* aacSectionDataResilienceFlag
  217. * aacScalefactorDataResilienceFlag
  218. * aacSpectralDataResilienceFlag
  219. */
  220. break;
  221. }
  222. skip_bits1(gb); // extensionFlag3 (TBD in version 3)
  223. }
  224. return 0;
  225. }
  226. /**
  227. * Decode audio specific configuration; reference: table 1.13.
  228. *
  229. * @param data pointer to AVCodecContext extradata
  230. * @param data_size size of AVCCodecContext extradata
  231. *
  232. * @return Returns error status. 0 - OK, !0 - error
  233. */
  234. static int decode_audio_specific_config(AACContext * ac, void *data, int data_size) {
  235. GetBitContext gb;
  236. int i;
  237. init_get_bits(&gb, data, data_size * 8);
  238. if((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
  239. return -1;
  240. if(ac->m4ac.sampling_index > 11) {
  241. av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
  242. return -1;
  243. }
  244. skip_bits_long(&gb, i);
  245. switch (ac->m4ac.object_type) {
  246. case AOT_AAC_LC:
  247. if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
  248. return -1;
  249. break;
  250. default:
  251. av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
  252. ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
  253. return -1;
  254. }
  255. return 0;
  256. }
  257. static av_cold int aac_decode_init(AVCodecContext * avccontext) {
  258. AACContext * ac = avccontext->priv_data;
  259. int i;
  260. ac->avccontext = avccontext;
  261. if (avccontext->extradata_size <= 0 ||
  262. decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size))
  263. return -1;
  264. avccontext->sample_fmt = SAMPLE_FMT_S16;
  265. avccontext->sample_rate = ac->m4ac.sample_rate;
  266. avccontext->frame_size = 1024;
  267. AAC_INIT_VLC_STATIC( 0, 144);
  268. AAC_INIT_VLC_STATIC( 1, 114);
  269. AAC_INIT_VLC_STATIC( 2, 188);
  270. AAC_INIT_VLC_STATIC( 3, 180);
  271. AAC_INIT_VLC_STATIC( 4, 172);
  272. AAC_INIT_VLC_STATIC( 5, 140);
  273. AAC_INIT_VLC_STATIC( 6, 168);
  274. AAC_INIT_VLC_STATIC( 7, 114);
  275. AAC_INIT_VLC_STATIC( 8, 262);
  276. AAC_INIT_VLC_STATIC( 9, 248);
  277. AAC_INIT_VLC_STATIC(10, 384);
  278. dsputil_init(&ac->dsp, avccontext);
  279. ac->random_state = 0x1f2e3d4c;
  280. // -1024 - Compensate wrong IMDCT method.
  281. // 32768 - Required to scale values to the correct range for the bias method
  282. // for float to int16 conversion.
  283. if(ac->dsp.float_to_int16 == ff_float_to_int16_c) {
  284. ac->add_bias = 385.0f;
  285. ac->sf_scale = 1. / (-1024. * 32768.);
  286. ac->sf_offset = 0;
  287. } else {
  288. ac->add_bias = 0.0f;
  289. ac->sf_scale = 1. / -1024.;
  290. ac->sf_offset = 60;
  291. }
  292. #ifndef CONFIG_HARDCODED_TABLES
  293. for (i = 1 - IVQUANT_SIZE/2; i < IVQUANT_SIZE/2; i++)
  294. ff_aac_ivquant_tab[i + IVQUANT_SIZE/2 - 1] = cbrt(fabs(i)) * i;
  295. for (i = 0; i < 316; i++)
  296. ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.);
  297. #endif /* CONFIG_HARDCODED_TABLES */
  298. INIT_VLC_STATIC(&vlc_scalefactors, 7, sizeof(ff_aac_scalefactor_code)/sizeof(ff_aac_scalefactor_code[0]),
  299. ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
  300. ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
  301. 352);
  302. ff_mdct_init(&ac->mdct, 11, 1);
  303. ff_mdct_init(&ac->mdct_small, 8, 1);
  304. return 0;
  305. }
  306. /**
  307. * Skip data_stream_element; reference: table 4.10.
  308. */
  309. static void skip_data_stream_element(GetBitContext * gb) {
  310. int byte_align = get_bits1(gb);
  311. int count = get_bits(gb, 8);
  312. if (count == 255)
  313. count += get_bits(gb, 8);
  314. if (byte_align)
  315. align_get_bits(gb);
  316. skip_bits_long(gb, 8 * count);
  317. }
  318. /**
  319. * Decode Individual Channel Stream info; reference: table 4.6.
  320. *
  321. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  322. */
  323. static int decode_ics_info(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb, int common_window) {
  324. if (get_bits1(gb)) {
  325. av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n");
  326. memset(ics, 0, sizeof(IndividualChannelStream));
  327. return -1;
  328. }
  329. ics->window_sequence[1] = ics->window_sequence[0];
  330. ics->window_sequence[0] = get_bits(gb, 2);
  331. ics->use_kb_window[1] = ics->use_kb_window[0];
  332. ics->use_kb_window[0] = get_bits1(gb);
  333. ics->num_window_groups = 1;
  334. ics->group_len[0] = 1;
  335. return 0;
  336. }
  337. /**
  338. * inverse quantization
  339. *
  340. * @param a quantized value to be dequantized
  341. * @return Returns dequantized value.
  342. */
  343. static inline float ivquant(int a) {
  344. if (a + (unsigned int)IVQUANT_SIZE/2 - 1 < (unsigned int)IVQUANT_SIZE - 1)
  345. return ff_aac_ivquant_tab[a + IVQUANT_SIZE/2 - 1];
  346. else
  347. return cbrtf(fabsf(a)) * a;
  348. }
  349. /**
  350. * Decode band types (section_data payload); reference: table 4.46.
  351. *
  352. * @param band_type array of the used band type
  353. * @param band_type_run_end array of the last scalefactor band of a band type run
  354. *
  355. * @return Returns error status. 0 - OK, !0 - error
  356. */
  357. static int decode_band_types(AACContext * ac, enum BandType band_type[120],
  358. int band_type_run_end[120], GetBitContext * gb, IndividualChannelStream * ics) {
  359. int g, idx = 0;
  360. const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
  361. for (g = 0; g < ics->num_window_groups; g++) {
  362. int k = 0;
  363. while (k < ics->max_sfb) {
  364. uint8_t sect_len = k;
  365. int sect_len_incr;
  366. int sect_band_type = get_bits(gb, 4);
  367. if (sect_band_type == 12) {
  368. av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n");
  369. return -1;
  370. }
  371. while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits)-1)
  372. sect_len += sect_len_incr;
  373. sect_len += sect_len_incr;
  374. if (sect_len > ics->max_sfb) {
  375. av_log(ac->avccontext, AV_LOG_ERROR,
  376. "Number of bands (%d) exceeds limit (%d).\n",
  377. sect_len, ics->max_sfb);
  378. return -1;
  379. }
  380. }
  381. }
  382. return 0;
  383. }
  384. /**
  385. * Decode scalefactors; reference: table 4.47.
  386. *
  387. * @param global_gain first scalefactor value as scalefactors are differentially coded
  388. * @param band_type array of the used band type
  389. * @param band_type_run_end array of the last scalefactor band of a band type run
  390. * @param sf array of scalefactors or intensity stereo positions
  391. *
  392. * @return Returns error status. 0 - OK, !0 - error
  393. */
  394. static int decode_scalefactors(AACContext * ac, float sf[120], GetBitContext * gb,
  395. unsigned int global_gain, IndividualChannelStream * ics,
  396. enum BandType band_type[120], int band_type_run_end[120]) {
  397. const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
  398. int g, i, idx = 0;
  399. int offset[3] = { global_gain, global_gain - 90, 100 };
  400. int noise_flag = 1;
  401. static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
  402. ics->intensity_present = 0;
  403. for (g = 0; g < ics->num_window_groups; g++) {
  404. for (i = 0; i < ics->max_sfb;) {
  405. int run_end = band_type_run_end[idx];
  406. if (band_type[idx] == ZERO_BT) {
  407. for(; i < run_end; i++, idx++)
  408. sf[idx] = 0.;
  409. }else if((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
  410. ics->intensity_present = 1;
  411. for(; i < run_end; i++, idx++) {
  412. offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  413. if(offset[2] > 255U) {
  414. av_log(ac->avccontext, AV_LOG_ERROR,
  415. "%s (%d) out of range.\n", sf_str[2], offset[2]);
  416. return -1;
  417. }
  418. sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
  419. }
  420. }else if(band_type[idx] == NOISE_BT) {
  421. for(; i < run_end; i++, idx++) {
  422. if(noise_flag-- > 0)
  423. offset[1] += get_bits(gb, 9) - 256;
  424. else
  425. offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  426. if(offset[1] > 255U) {
  427. av_log(ac->avccontext, AV_LOG_ERROR,
  428. "%s (%d) out of range.\n", sf_str[1], offset[1]);
  429. return -1;
  430. }
  431. sf[idx] = -ff_aac_pow2sf_tab[ offset[1] + sf_offset];
  432. }
  433. }else {
  434. for(; i < run_end; i++, idx++) {
  435. offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  436. if(offset[0] > 255U) {
  437. av_log(ac->avccontext, AV_LOG_ERROR,
  438. "%s (%d) out of range.\n", sf_str[0], offset[0]);
  439. return -1;
  440. }
  441. sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
  442. }
  443. }
  444. }
  445. }
  446. return 0;
  447. }
  448. /**
  449. * Decode pulse data; reference: table 4.7.
  450. */
  451. static void decode_pulses(Pulse * pulse, GetBitContext * gb) {
  452. int i;
  453. pulse->num_pulse = get_bits(gb, 2) + 1;
  454. pulse->start = get_bits(gb, 6);
  455. for (i = 0; i < pulse->num_pulse; i++) {
  456. pulse->offset[i] = get_bits(gb, 5);
  457. pulse->amp [i] = get_bits(gb, 4);
  458. }
  459. }
  460. /**
  461. * Decode Mid/Side data; reference: table 4.54.
  462. *
  463. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  464. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  465. * [3] reserved for scalable AAC
  466. */
  467. static void decode_mid_side_stereo(ChannelElement * cpe, GetBitContext * gb,
  468. int ms_present) {
  469. /**
  470. * Add pulses with particular amplitudes to the quantized spectral data; reference: 4.6.3.3.
  471. *
  472. * @param pulse pointer to pulse data struct
  473. * @param icoef array of quantized spectral data
  474. */
  475. static void add_pulses(int icoef[1024], const Pulse * pulse, const IndividualChannelStream * ics) {
  476. int i, off = ics->swb_offset[pulse->start];
  477. for (i = 0; i < pulse->num_pulse; i++) {
  478. int ic;
  479. off += pulse->offset[i];
  480. ic = (icoef[off] - 1)>>31;
  481. icoef[off] += (pulse->amp[i]^ic) - ic;
  482. }
  483. }
  484. /**
  485. * Decode an individual_channel_stream payload; reference: table 4.44.
  486. *
  487. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  488. * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
  489. *
  490. * @return Returns error status. 0 - OK, !0 - error
  491. */
  492. static int decode_ics(AACContext * ac, SingleChannelElement * sce, GetBitContext * gb, int common_window, int scale_flag) {
  493. int icoeffs[1024];
  494. Pulse pulse;
  495. TemporalNoiseShaping * tns = &sce->tns;
  496. IndividualChannelStream * ics = &sce->ics;
  497. float * out = sce->coeffs;
  498. int global_gain, pulse_present = 0;
  499. /* These two assignments are to silence some GCC warnings about the
  500. * variables being used uninitialised when in fact they always are.
  501. */
  502. pulse.num_pulse = 0;
  503. pulse.start = 0;
  504. global_gain = get_bits(gb, 8);
  505. if (!common_window && !scale_flag) {
  506. if (decode_ics_info(ac, ics, gb, 0) < 0)
  507. return -1;
  508. }
  509. if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
  510. return -1;
  511. if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
  512. return -1;
  513. pulse_present = 0;
  514. if (!scale_flag) {
  515. if ((pulse_present = get_bits1(gb))) {
  516. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  517. av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
  518. return -1;
  519. }
  520. decode_pulses(&pulse, gb);
  521. }
  522. if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
  523. return -1;
  524. if (get_bits1(gb)) {
  525. av_log_missing_feature(ac->avccontext, "SSR", 1);
  526. return -1;
  527. }
  528. }
  529. if (decode_spectrum(ac, icoeffs, gb, ics, sce->band_type) < 0)
  530. return -1;
  531. if (pulse_present)
  532. add_pulses(icoeffs, &pulse, ics);
  533. dequant(ac, out, icoeffs, sce->sf, ics, sce->band_type);
  534. return 0;
  535. }
  536. /**
  537. * Decode a channel_pair_element; reference: table 4.4.
  538. *
  539. * @param elem_id Identifies the instance of a syntax element.
  540. *
  541. * @return Returns error status. 0 - OK, !0 - error
  542. */
  543. static int decode_cpe(AACContext * ac, GetBitContext * gb, int elem_id) {
  544. int i, ret, common_window, ms_present = 0;
  545. ChannelElement * cpe;
  546. cpe = ac->che[TYPE_CPE][elem_id];
  547. common_window = get_bits1(gb);
  548. if (common_window) {
  549. if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
  550. return -1;
  551. i = cpe->ch[1].ics.use_kb_window[0];
  552. cpe->ch[1].ics = cpe->ch[0].ics;
  553. cpe->ch[1].ics.use_kb_window[1] = i;
  554. ms_present = get_bits(gb, 2);
  555. if(ms_present == 3) {
  556. av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
  557. return -1;
  558. } else if(ms_present)
  559. decode_mid_side_stereo(cpe, gb, ms_present);
  560. }
  561. if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
  562. return ret;
  563. if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
  564. return ret;
  565. if (common_window && ms_present)
  566. apply_mid_side_stereo(cpe);
  567. if (cpe->ch[1].ics.intensity_present)
  568. apply_intensity_stereo(cpe, ms_present);
  569. return 0;
  570. }
  571. /**
  572. * Decode Spectral Band Replication extension data; reference: table 4.55.
  573. *
  574. * @param crc flag indicating the presence of CRC checksum
  575. * @param cnt length of TYPE_FIL syntactic element in bytes
  576. *
  577. * @return Returns number of bytes consumed from the TYPE_FIL element.
  578. */
  579. static int decode_sbr_extension(AACContext * ac, GetBitContext * gb, int crc, int cnt) {
  580. // TODO : sbr_extension implementation
  581. av_log_missing_feature(ac->avccontext, "SBR", 0);
  582. skip_bits_long(gb, 8*cnt - 4); // -4 due to reading extension type
  583. return cnt;
  584. }
  585. /**
  586. * Decode dynamic range information; reference: table 4.52.
  587. *
  588. * @param cnt length of TYPE_FIL syntactic element in bytes
  589. *
  590. * @return Returns number of bytes consumed.
  591. */
  592. static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext * gb, int cnt) {
  593. int n = 1;
  594. int drc_num_bands = 1;
  595. int i;
  596. /* pce_tag_present? */
  597. if(get_bits1(gb)) {
  598. che_drc->pce_instance_tag = get_bits(gb, 4);
  599. skip_bits(gb, 4); // tag_reserved_bits
  600. n++;
  601. }
  602. /* excluded_chns_present? */
  603. if(get_bits1(gb)) {
  604. n += decode_drc_channel_exclusions(che_drc, gb);
  605. }
  606. /* drc_bands_present? */
  607. if (get_bits1(gb)) {
  608. che_drc->band_incr = get_bits(gb, 4);
  609. che_drc->interpolation_scheme = get_bits(gb, 4);
  610. n++;
  611. drc_num_bands += che_drc->band_incr;
  612. for (i = 0; i < drc_num_bands; i++) {
  613. che_drc->band_top[i] = get_bits(gb, 8);
  614. n++;
  615. }
  616. }
  617. /* prog_ref_level_present? */
  618. if (get_bits1(gb)) {
  619. che_drc->prog_ref_level = get_bits(gb, 7);
  620. skip_bits1(gb); // prog_ref_level_reserved_bits
  621. n++;
  622. }
  623. for (i = 0; i < drc_num_bands; i++) {
  624. che_drc->dyn_rng_sgn[i] = get_bits1(gb);
  625. che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
  626. n++;
  627. }
  628. return n;
  629. }
  630. /**
  631. * Decode extension data (incomplete); reference: table 4.51.
  632. *
  633. * @param cnt length of TYPE_FIL syntactic element in bytes
  634. *
  635. * @return Returns number of bytes consumed
  636. */
  637. static int decode_extension_payload(AACContext * ac, GetBitContext * gb, int cnt) {
  638. int crc_flag = 0;
  639. int res = cnt;
  640. switch (get_bits(gb, 4)) { // extension type
  641. case EXT_SBR_DATA_CRC:
  642. crc_flag++;
  643. case EXT_SBR_DATA:
  644. res = decode_sbr_extension(ac, gb, crc_flag, cnt);
  645. break;
  646. case EXT_DYNAMIC_RANGE:
  647. res = decode_dynamic_range(&ac->che_drc, gb, cnt);
  648. break;
  649. case EXT_FILL:
  650. case EXT_FILL_DATA:
  651. case EXT_DATA_ELEMENT:
  652. default:
  653. skip_bits_long(gb, 8*cnt - 4);
  654. break;
  655. };
  656. return res;
  657. }
  658. /**
  659. * Conduct IMDCT and windowing.
  660. */
  661. static void imdct_and_windowing(AACContext * ac, SingleChannelElement * sce) {
  662. IndividualChannelStream * ics = &sce->ics;
  663. float * in = sce->coeffs;
  664. float * out = sce->ret;
  665. float * saved = sce->saved;
  666. const float * lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_aac_sine_long_1024;
  667. const float * swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_aac_sine_short_128;
  668. const float * lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_aac_sine_long_1024;
  669. const float * swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_aac_sine_short_128;
  670. float * buf = ac->buf_mdct;
  671. int i;
  672. /**
  673. * Apply dependent channel coupling (applied before IMDCT).
  674. *
  675. * @param index index into coupling gain array
  676. */
  677. static void apply_dependent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) {
  678. IndividualChannelStream * ics = &cc->ch[0].ics;
  679. const uint16_t * offsets = ics->swb_offset;
  680. float * dest = sce->coeffs;
  681. const float * src = cc->ch[0].coeffs;
  682. int g, i, group, k, idx = 0;
  683. if(ac->m4ac.object_type == AOT_AAC_LTP) {
  684. av_log(ac->avccontext, AV_LOG_ERROR,
  685. "Dependent coupling is not supported together with LTP\n");
  686. return;
  687. }
  688. for (g = 0; g < ics->num_window_groups; g++) {
  689. for (i = 0; i < ics->max_sfb; i++, idx++) {
  690. if (cc->ch[0].band_type[idx] != ZERO_BT) {
  691. for (group = 0; group < ics->group_len[g]; group++) {
  692. for (k = offsets[i]; k < offsets[i+1]; k++) {
  693. // XXX dsputil-ize
  694. dest[group*128+k] += cc->coup.gain[index][idx] * src[group*128+k];
  695. }
  696. }
  697. }
  698. }
  699. dest += ics->group_len[g]*128;
  700. src += ics->group_len[g]*128;
  701. }
  702. }
  703. /**
  704. * Apply independent channel coupling (applied after IMDCT).
  705. *
  706. * @param index index into coupling gain array
  707. */
  708. static void apply_independent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) {
  709. int i;
  710. for (i = 0; i < 1024; i++)
  711. sce->ret[i] += cc->coup.gain[index][0] * (cc->ch[0].ret[i] - ac->add_bias);
  712. }
  713. if (!ac->is_saved) {
  714. ac->is_saved = 1;
  715. *data_size = 0;
  716. return 0;
  717. }
  718. data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t);
  719. if(*data_size < data_size_tmp) {
  720. av_log(avccontext, AV_LOG_ERROR,
  721. "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
  722. *data_size, data_size_tmp);
  723. return -1;
  724. }
  725. *data_size = data_size_tmp;
  726. ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels);
  727. return buf_size;
  728. }
  729. static av_cold int aac_decode_close(AVCodecContext * avccontext) {
  730. AACContext * ac = avccontext->priv_data;
  731. int i, type;
  732. for (i = 0; i < MAX_ELEM_ID; i++) {
  733. for(type = 0; type < 4; type++)
  734. av_freep(&ac->che[type][i]);
  735. }
  736. ff_mdct_end(&ac->mdct);
  737. ff_mdct_end(&ac->mdct_small);
  738. return 0 ;
  739. }
  740. AVCodec aac_decoder = {
  741. "aac",
  742. CODEC_TYPE_AUDIO,
  743. CODEC_ID_AAC,
  744. sizeof(AACContext),
  745. aac_decode_init,
  746. NULL,
  747. aac_decode_close,
  748. aac_decode_frame,
  749. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  750. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  751. };