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.

2378 lines
82KB

  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. * AAC LATM decoder
  7. * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
  8. * Copyright (c) 2010 Janne Grunau <janne-ffmpeg@jannau.net>
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. /**
  27. * @file
  28. * AAC decoder
  29. * @author Oded Shimon ( ods15 ods15 dyndns org )
  30. * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
  31. */
  32. /*
  33. * supported tools
  34. *
  35. * Support? Name
  36. * N (code in SoC repo) gain control
  37. * Y block switching
  38. * Y window shapes - standard
  39. * N window shapes - Low Delay
  40. * Y filterbank - standard
  41. * N (code in SoC repo) filterbank - Scalable Sample Rate
  42. * Y Temporal Noise Shaping
  43. * N (code in SoC repo) Long Term Prediction
  44. * Y intensity stereo
  45. * Y channel coupling
  46. * Y frequency domain prediction
  47. * Y Perceptual Noise Substitution
  48. * Y Mid/Side stereo
  49. * N Scalable Inverse AAC Quantization
  50. * N Frequency Selective Switch
  51. * N upsampling filter
  52. * Y quantization & coding - AAC
  53. * N quantization & coding - TwinVQ
  54. * N quantization & coding - BSAC
  55. * N AAC Error Resilience tools
  56. * N Error Resilience payload syntax
  57. * N Error Protection tool
  58. * N CELP
  59. * N Silence Compression
  60. * N HVXC
  61. * N HVXC 4kbits/s VR
  62. * N Structured Audio tools
  63. * N Structured Audio Sample Bank Format
  64. * N MIDI
  65. * N Harmonic and Individual Lines plus Noise
  66. * N Text-To-Speech Interface
  67. * Y Spectral Band Replication
  68. * Y (not in this code) Layer-1
  69. * Y (not in this code) Layer-2
  70. * Y (not in this code) Layer-3
  71. * N SinuSoidal Coding (Transient, Sinusoid, Noise)
  72. * Y Parametric Stereo
  73. * N Direct Stream Transfer
  74. *
  75. * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
  76. * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
  77. Parametric Stereo.
  78. */
  79. #include "avcodec.h"
  80. #include "internal.h"
  81. #include "get_bits.h"
  82. #include "dsputil.h"
  83. #include "fft.h"
  84. #include "fmtconvert.h"
  85. #include "lpc.h"
  86. #include "aac.h"
  87. #include "aactab.h"
  88. #include "aacdectab.h"
  89. #include "cbrt_tablegen.h"
  90. #include "sbr.h"
  91. #include "aacsbr.h"
  92. #include "mpeg4audio.h"
  93. #include "aacadtsdec.h"
  94. #include <assert.h>
  95. #include <errno.h>
  96. #include <math.h>
  97. #include <string.h>
  98. #if ARCH_ARM
  99. # include "arm/aac.h"
  100. #endif
  101. union float754 {
  102. float f;
  103. uint32_t i;
  104. };
  105. static VLC vlc_scalefactors;
  106. static VLC vlc_spectral[11];
  107. static const char overread_err[] = "Input buffer exhausted before END element found\n";
  108. static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
  109. {
  110. // For PCE based channel configurations map the channels solely based on tags.
  111. if (!ac->m4ac.chan_config) {
  112. return ac->tag_che_map[type][elem_id];
  113. }
  114. // For indexed channel configurations map the channels solely based on position.
  115. switch (ac->m4ac.chan_config) {
  116. case 7:
  117. if (ac->tags_mapped == 3 && type == TYPE_CPE) {
  118. ac->tags_mapped++;
  119. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
  120. }
  121. case 6:
  122. /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
  123. instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have
  124. encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */
  125. if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
  126. ac->tags_mapped++;
  127. return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
  128. }
  129. case 5:
  130. if (ac->tags_mapped == 2 && type == TYPE_CPE) {
  131. ac->tags_mapped++;
  132. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
  133. }
  134. case 4:
  135. if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
  136. ac->tags_mapped++;
  137. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
  138. }
  139. case 3:
  140. case 2:
  141. if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
  142. ac->tags_mapped++;
  143. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
  144. } else if (ac->m4ac.chan_config == 2) {
  145. return NULL;
  146. }
  147. case 1:
  148. if (!ac->tags_mapped && type == TYPE_SCE) {
  149. ac->tags_mapped++;
  150. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
  151. }
  152. default:
  153. return NULL;
  154. }
  155. }
  156. /**
  157. * Check for the channel element in the current channel position configuration.
  158. * If it exists, make sure the appropriate element is allocated and map the
  159. * channel order to match the internal FFmpeg channel layout.
  160. *
  161. * @param che_pos current channel position configuration
  162. * @param type channel element type
  163. * @param id channel element id
  164. * @param channels count of the number of channels in the configuration
  165. *
  166. * @return Returns error status. 0 - OK, !0 - error
  167. */
  168. static av_cold int che_configure(AACContext *ac,
  169. enum ChannelPosition che_pos[4][MAX_ELEM_ID],
  170. int type, int id,
  171. int *channels)
  172. {
  173. if (che_pos[type][id]) {
  174. if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
  175. return AVERROR(ENOMEM);
  176. ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr);
  177. if (type != TYPE_CCE) {
  178. ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
  179. if (type == TYPE_CPE ||
  180. (type == TYPE_SCE && ac->m4ac.ps == 1)) {
  181. ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
  182. }
  183. }
  184. } else {
  185. if (ac->che[type][id])
  186. ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
  187. av_freep(&ac->che[type][id]);
  188. }
  189. return 0;
  190. }
  191. /**
  192. * Configure output channel order based on the current program configuration element.
  193. *
  194. * @param che_pos current channel position configuration
  195. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  196. *
  197. * @return Returns error status. 0 - OK, !0 - error
  198. */
  199. static av_cold int output_configure(AACContext *ac,
  200. enum ChannelPosition che_pos[4][MAX_ELEM_ID],
  201. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  202. int channel_config, enum OCStatus oc_type)
  203. {
  204. AVCodecContext *avctx = ac->avctx;
  205. int i, type, channels = 0, ret;
  206. if (new_che_pos != che_pos)
  207. memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  208. if (channel_config) {
  209. for (i = 0; i < tags_per_config[channel_config]; i++) {
  210. if ((ret = che_configure(ac, che_pos,
  211. aac_channel_layout_map[channel_config - 1][i][0],
  212. aac_channel_layout_map[channel_config - 1][i][1],
  213. &channels)))
  214. return ret;
  215. }
  216. memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  217. avctx->channel_layout = aac_channel_layout[channel_config - 1];
  218. } else {
  219. /* Allocate or free elements depending on if they are in the
  220. * current program configuration.
  221. *
  222. * Set up default 1:1 output mapping.
  223. *
  224. * For a 5.1 stream the output order will be:
  225. * [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ]
  226. */
  227. for (i = 0; i < MAX_ELEM_ID; i++) {
  228. for (type = 0; type < 4; type++) {
  229. if ((ret = che_configure(ac, che_pos, type, i, &channels)))
  230. return ret;
  231. }
  232. }
  233. memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  234. avctx->channel_layout = 0;
  235. }
  236. avctx->channels = channels;
  237. ac->output_configured = oc_type;
  238. return 0;
  239. }
  240. /**
  241. * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
  242. *
  243. * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present.
  244. * @param sce_map mono (Single Channel Element) map
  245. * @param type speaker type/position for these channels
  246. */
  247. static void decode_channel_map(enum ChannelPosition *cpe_map,
  248. enum ChannelPosition *sce_map,
  249. enum ChannelPosition type,
  250. GetBitContext *gb, int n)
  251. {
  252. while (n--) {
  253. enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
  254. map[get_bits(gb, 4)] = type;
  255. }
  256. }
  257. /**
  258. * Decode program configuration element; reference: table 4.2.
  259. *
  260. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  261. *
  262. * @return Returns error status. 0 - OK, !0 - error
  263. */
  264. static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
  265. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  266. GetBitContext *gb)
  267. {
  268. int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
  269. int comment_len;
  270. skip_bits(gb, 2); // object_type
  271. sampling_index = get_bits(gb, 4);
  272. if (m4ac->sampling_index != sampling_index)
  273. av_log(avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
  274. num_front = get_bits(gb, 4);
  275. num_side = get_bits(gb, 4);
  276. num_back = get_bits(gb, 4);
  277. num_lfe = get_bits(gb, 2);
  278. num_assoc_data = get_bits(gb, 3);
  279. num_cc = get_bits(gb, 4);
  280. if (get_bits1(gb))
  281. skip_bits(gb, 4); // mono_mixdown_tag
  282. if (get_bits1(gb))
  283. skip_bits(gb, 4); // stereo_mixdown_tag
  284. if (get_bits1(gb))
  285. skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
  286. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
  287. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
  288. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
  289. decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
  290. skip_bits_long(gb, 4 * num_assoc_data);
  291. decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
  292. align_get_bits(gb);
  293. /* comment field, first byte is length */
  294. comment_len = get_bits(gb, 8) * 8;
  295. if (get_bits_left(gb) < comment_len) {
  296. av_log(avctx, AV_LOG_ERROR, overread_err);
  297. return -1;
  298. }
  299. skip_bits_long(gb, comment_len);
  300. return 0;
  301. }
  302. /**
  303. * Set up channel positions based on a default channel configuration
  304. * as specified in table 1.17.
  305. *
  306. * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
  307. *
  308. * @return Returns error status. 0 - OK, !0 - error
  309. */
  310. static av_cold int set_default_channel_config(AVCodecContext *avctx,
  311. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  312. int channel_config)
  313. {
  314. if (channel_config < 1 || channel_config > 7) {
  315. av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
  316. channel_config);
  317. return -1;
  318. }
  319. /* default channel configurations:
  320. *
  321. * 1ch : front center (mono)
  322. * 2ch : L + R (stereo)
  323. * 3ch : front center + L + R
  324. * 4ch : front center + L + R + back center
  325. * 5ch : front center + L + R + back stereo
  326. * 6ch : front center + L + R + back stereo + LFE
  327. * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
  328. */
  329. if (channel_config != 2)
  330. new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
  331. if (channel_config > 1)
  332. new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
  333. if (channel_config == 4)
  334. new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK; // back center
  335. if (channel_config > 4)
  336. new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
  337. = AAC_CHANNEL_BACK; // back stereo
  338. if (channel_config > 5)
  339. new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE; // LFE
  340. if (channel_config == 7)
  341. new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
  342. return 0;
  343. }
  344. /**
  345. * Decode GA "General Audio" specific configuration; reference: table 4.1.
  346. *
  347. * @param ac pointer to AACContext, may be null
  348. * @param avctx pointer to AVCCodecContext, used for logging
  349. *
  350. * @return Returns error status. 0 - OK, !0 - error
  351. */
  352. static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
  353. GetBitContext *gb,
  354. MPEG4AudioConfig *m4ac,
  355. int channel_config)
  356. {
  357. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  358. int extension_flag, ret;
  359. if (get_bits1(gb)) { // frameLengthFlag
  360. av_log_missing_feature(avctx, "960/120 MDCT window is", 1);
  361. return -1;
  362. }
  363. if (get_bits1(gb)) // dependsOnCoreCoder
  364. skip_bits(gb, 14); // coreCoderDelay
  365. extension_flag = get_bits1(gb);
  366. if (m4ac->object_type == AOT_AAC_SCALABLE ||
  367. m4ac->object_type == AOT_ER_AAC_SCALABLE)
  368. skip_bits(gb, 3); // layerNr
  369. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  370. if (channel_config == 0) {
  371. skip_bits(gb, 4); // element_instance_tag
  372. if ((ret = decode_pce(avctx, m4ac, new_che_pos, gb)))
  373. return ret;
  374. } else {
  375. if ((ret = set_default_channel_config(avctx, new_che_pos, channel_config)))
  376. return ret;
  377. }
  378. if (ac && (ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
  379. return ret;
  380. if (extension_flag) {
  381. switch (m4ac->object_type) {
  382. case AOT_ER_BSAC:
  383. skip_bits(gb, 5); // numOfSubFrame
  384. skip_bits(gb, 11); // layer_length
  385. break;
  386. case AOT_ER_AAC_LC:
  387. case AOT_ER_AAC_LTP:
  388. case AOT_ER_AAC_SCALABLE:
  389. case AOT_ER_AAC_LD:
  390. skip_bits(gb, 3); /* aacSectionDataResilienceFlag
  391. * aacScalefactorDataResilienceFlag
  392. * aacSpectralDataResilienceFlag
  393. */
  394. break;
  395. }
  396. skip_bits1(gb); // extensionFlag3 (TBD in version 3)
  397. }
  398. return 0;
  399. }
  400. /**
  401. * Decode audio specific configuration; reference: table 1.13.
  402. *
  403. * @param ac pointer to AACContext, may be null
  404. * @param avctx pointer to AVCCodecContext, used for logging
  405. * @param m4ac pointer to MPEG4AudioConfig, used for parsing
  406. * @param data pointer to AVCodecContext extradata
  407. * @param data_size size of AVCCodecContext extradata
  408. *
  409. * @return Returns error status or number of consumed bits. <0 - error
  410. */
  411. static int decode_audio_specific_config(AACContext *ac,
  412. AVCodecContext *avctx,
  413. MPEG4AudioConfig *m4ac,
  414. const uint8_t *data, int data_size)
  415. {
  416. GetBitContext gb;
  417. int i;
  418. init_get_bits(&gb, data, data_size * 8);
  419. if ((i = ff_mpeg4audio_get_config(m4ac, data, data_size)) < 0)
  420. return -1;
  421. if (m4ac->sampling_index > 12) {
  422. av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
  423. return -1;
  424. }
  425. if (m4ac->sbr == 1 && m4ac->ps == -1)
  426. m4ac->ps = 1;
  427. skip_bits_long(&gb, i);
  428. switch (m4ac->object_type) {
  429. case AOT_AAC_MAIN:
  430. case AOT_AAC_LC:
  431. if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
  432. return -1;
  433. break;
  434. default:
  435. av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
  436. m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
  437. return -1;
  438. }
  439. return get_bits_count(&gb);
  440. }
  441. /**
  442. * linear congruential pseudorandom number generator
  443. *
  444. * @param previous_val pointer to the current state of the generator
  445. *
  446. * @return Returns a 32-bit pseudorandom integer
  447. */
  448. static av_always_inline int lcg_random(int previous_val)
  449. {
  450. return previous_val * 1664525 + 1013904223;
  451. }
  452. static av_always_inline void reset_predict_state(PredictorState *ps)
  453. {
  454. ps->r0 = 0.0f;
  455. ps->r1 = 0.0f;
  456. ps->cor0 = 0.0f;
  457. ps->cor1 = 0.0f;
  458. ps->var0 = 1.0f;
  459. ps->var1 = 1.0f;
  460. }
  461. static void reset_all_predictors(PredictorState *ps)
  462. {
  463. int i;
  464. for (i = 0; i < MAX_PREDICTORS; i++)
  465. reset_predict_state(&ps[i]);
  466. }
  467. static void reset_predictor_group(PredictorState *ps, int group_num)
  468. {
  469. int i;
  470. for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
  471. reset_predict_state(&ps[i]);
  472. }
  473. #define AAC_INIT_VLC_STATIC(num, size) \
  474. INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
  475. ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
  476. ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
  477. size);
  478. static av_cold int aac_decode_init(AVCodecContext *avctx)
  479. {
  480. AACContext *ac = avctx->priv_data;
  481. ac->avctx = avctx;
  482. ac->m4ac.sample_rate = avctx->sample_rate;
  483. if (avctx->extradata_size > 0) {
  484. if (decode_audio_specific_config(ac, ac->avctx, &ac->m4ac,
  485. avctx->extradata,
  486. avctx->extradata_size) < 0)
  487. return -1;
  488. }
  489. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  490. AAC_INIT_VLC_STATIC( 0, 304);
  491. AAC_INIT_VLC_STATIC( 1, 270);
  492. AAC_INIT_VLC_STATIC( 2, 550);
  493. AAC_INIT_VLC_STATIC( 3, 300);
  494. AAC_INIT_VLC_STATIC( 4, 328);
  495. AAC_INIT_VLC_STATIC( 5, 294);
  496. AAC_INIT_VLC_STATIC( 6, 306);
  497. AAC_INIT_VLC_STATIC( 7, 268);
  498. AAC_INIT_VLC_STATIC( 8, 510);
  499. AAC_INIT_VLC_STATIC( 9, 366);
  500. AAC_INIT_VLC_STATIC(10, 462);
  501. ff_aac_sbr_init();
  502. dsputil_init(&ac->dsp, avctx);
  503. ff_fmt_convert_init(&ac->fmt_conv, avctx);
  504. ac->random_state = 0x1f2e3d4c;
  505. // -1024 - Compensate wrong IMDCT method.
  506. // 60 - Required to scale values to the correct range [-32768,32767]
  507. // for float to int16 conversion. (1 << (60 / 4)) == 32768
  508. ac->sf_scale = 1. / -1024.;
  509. ac->sf_offset = 60;
  510. ff_aac_tableinit();
  511. INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
  512. ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
  513. ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
  514. 352);
  515. ff_mdct_init(&ac->mdct, 11, 1, 1.0);
  516. ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
  517. // window initialization
  518. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  519. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  520. ff_init_ff_sine_windows(10);
  521. ff_init_ff_sine_windows( 7);
  522. cbrt_tableinit();
  523. return 0;
  524. }
  525. /**
  526. * Skip data_stream_element; reference: table 4.10.
  527. */
  528. static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
  529. {
  530. int byte_align = get_bits1(gb);
  531. int count = get_bits(gb, 8);
  532. if (count == 255)
  533. count += get_bits(gb, 8);
  534. if (byte_align)
  535. align_get_bits(gb);
  536. if (get_bits_left(gb) < 8 * count) {
  537. av_log(ac->avctx, AV_LOG_ERROR, overread_err);
  538. return -1;
  539. }
  540. skip_bits_long(gb, 8 * count);
  541. return 0;
  542. }
  543. static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
  544. GetBitContext *gb)
  545. {
  546. int sfb;
  547. if (get_bits1(gb)) {
  548. ics->predictor_reset_group = get_bits(gb, 5);
  549. if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
  550. av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
  551. return -1;
  552. }
  553. }
  554. for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
  555. ics->prediction_used[sfb] = get_bits1(gb);
  556. }
  557. return 0;
  558. }
  559. /**
  560. * Decode Individual Channel Stream info; reference: table 4.6.
  561. *
  562. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  563. */
  564. static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
  565. GetBitContext *gb, int common_window)
  566. {
  567. if (get_bits1(gb)) {
  568. av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
  569. memset(ics, 0, sizeof(IndividualChannelStream));
  570. return -1;
  571. }
  572. ics->window_sequence[1] = ics->window_sequence[0];
  573. ics->window_sequence[0] = get_bits(gb, 2);
  574. ics->use_kb_window[1] = ics->use_kb_window[0];
  575. ics->use_kb_window[0] = get_bits1(gb);
  576. ics->num_window_groups = 1;
  577. ics->group_len[0] = 1;
  578. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  579. int i;
  580. ics->max_sfb = get_bits(gb, 4);
  581. for (i = 0; i < 7; i++) {
  582. if (get_bits1(gb)) {
  583. ics->group_len[ics->num_window_groups - 1]++;
  584. } else {
  585. ics->num_window_groups++;
  586. ics->group_len[ics->num_window_groups - 1] = 1;
  587. }
  588. }
  589. ics->num_windows = 8;
  590. ics->swb_offset = ff_swb_offset_128[ac->m4ac.sampling_index];
  591. ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
  592. ics->tns_max_bands = ff_tns_max_bands_128[ac->m4ac.sampling_index];
  593. ics->predictor_present = 0;
  594. } else {
  595. ics->max_sfb = get_bits(gb, 6);
  596. ics->num_windows = 1;
  597. ics->swb_offset = ff_swb_offset_1024[ac->m4ac.sampling_index];
  598. ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
  599. ics->tns_max_bands = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
  600. ics->predictor_present = get_bits1(gb);
  601. ics->predictor_reset_group = 0;
  602. if (ics->predictor_present) {
  603. if (ac->m4ac.object_type == AOT_AAC_MAIN) {
  604. if (decode_prediction(ac, ics, gb)) {
  605. memset(ics, 0, sizeof(IndividualChannelStream));
  606. return -1;
  607. }
  608. } else if (ac->m4ac.object_type == AOT_AAC_LC) {
  609. av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
  610. memset(ics, 0, sizeof(IndividualChannelStream));
  611. return -1;
  612. } else {
  613. av_log_missing_feature(ac->avctx, "Predictor bit set but LTP is", 1);
  614. memset(ics, 0, sizeof(IndividualChannelStream));
  615. return -1;
  616. }
  617. }
  618. }
  619. if (ics->max_sfb > ics->num_swb) {
  620. av_log(ac->avctx, AV_LOG_ERROR,
  621. "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
  622. ics->max_sfb, ics->num_swb);
  623. memset(ics, 0, sizeof(IndividualChannelStream));
  624. return -1;
  625. }
  626. return 0;
  627. }
  628. /**
  629. * Decode band types (section_data payload); reference: table 4.46.
  630. *
  631. * @param band_type array of the used band type
  632. * @param band_type_run_end array of the last scalefactor band of a band type run
  633. *
  634. * @return Returns error status. 0 - OK, !0 - error
  635. */
  636. static int decode_band_types(AACContext *ac, enum BandType band_type[120],
  637. int band_type_run_end[120], GetBitContext *gb,
  638. IndividualChannelStream *ics)
  639. {
  640. int g, idx = 0;
  641. const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
  642. for (g = 0; g < ics->num_window_groups; g++) {
  643. int k = 0;
  644. while (k < ics->max_sfb) {
  645. uint8_t sect_end = k;
  646. int sect_len_incr;
  647. int sect_band_type = get_bits(gb, 4);
  648. if (sect_band_type == 12) {
  649. av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
  650. return -1;
  651. }
  652. while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
  653. sect_end += sect_len_incr;
  654. sect_end += sect_len_incr;
  655. if (get_bits_left(gb) < 0) {
  656. av_log(ac->avctx, AV_LOG_ERROR, overread_err);
  657. return -1;
  658. }
  659. if (sect_end > ics->max_sfb) {
  660. av_log(ac->avctx, AV_LOG_ERROR,
  661. "Number of bands (%d) exceeds limit (%d).\n",
  662. sect_end, ics->max_sfb);
  663. return -1;
  664. }
  665. for (; k < sect_end; k++) {
  666. band_type [idx] = sect_band_type;
  667. band_type_run_end[idx++] = sect_end;
  668. }
  669. }
  670. }
  671. return 0;
  672. }
  673. /**
  674. * Decode scalefactors; reference: table 4.47.
  675. *
  676. * @param global_gain first scalefactor value as scalefactors are differentially coded
  677. * @param band_type array of the used band type
  678. * @param band_type_run_end array of the last scalefactor band of a band type run
  679. * @param sf array of scalefactors or intensity stereo positions
  680. *
  681. * @return Returns error status. 0 - OK, !0 - error
  682. */
  683. static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
  684. unsigned int global_gain,
  685. IndividualChannelStream *ics,
  686. enum BandType band_type[120],
  687. int band_type_run_end[120])
  688. {
  689. const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
  690. int g, i, idx = 0;
  691. int offset[3] = { global_gain, global_gain - 90, 100 };
  692. int noise_flag = 1;
  693. static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
  694. for (g = 0; g < ics->num_window_groups; g++) {
  695. for (i = 0; i < ics->max_sfb;) {
  696. int run_end = band_type_run_end[idx];
  697. if (band_type[idx] == ZERO_BT) {
  698. for (; i < run_end; i++, idx++)
  699. sf[idx] = 0.;
  700. } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
  701. for (; i < run_end; i++, idx++) {
  702. offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  703. if (offset[2] > 255U) {
  704. av_log(ac->avctx, AV_LOG_ERROR,
  705. "%s (%d) out of range.\n", sf_str[2], offset[2]);
  706. return -1;
  707. }
  708. sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
  709. }
  710. } else if (band_type[idx] == NOISE_BT) {
  711. for (; i < run_end; i++, idx++) {
  712. if (noise_flag-- > 0)
  713. offset[1] += get_bits(gb, 9) - 256;
  714. else
  715. offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  716. if (offset[1] > 255U) {
  717. av_log(ac->avctx, AV_LOG_ERROR,
  718. "%s (%d) out of range.\n", sf_str[1], offset[1]);
  719. return -1;
  720. }
  721. sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
  722. }
  723. } else {
  724. for (; i < run_end; i++, idx++) {
  725. offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  726. if (offset[0] > 255U) {
  727. av_log(ac->avctx, AV_LOG_ERROR,
  728. "%s (%d) out of range.\n", sf_str[0], offset[0]);
  729. return -1;
  730. }
  731. sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
  732. }
  733. }
  734. }
  735. }
  736. return 0;
  737. }
  738. /**
  739. * Decode pulse data; reference: table 4.7.
  740. */
  741. static int decode_pulses(Pulse *pulse, GetBitContext *gb,
  742. const uint16_t *swb_offset, int num_swb)
  743. {
  744. int i, pulse_swb;
  745. pulse->num_pulse = get_bits(gb, 2) + 1;
  746. pulse_swb = get_bits(gb, 6);
  747. if (pulse_swb >= num_swb)
  748. return -1;
  749. pulse->pos[0] = swb_offset[pulse_swb];
  750. pulse->pos[0] += get_bits(gb, 5);
  751. if (pulse->pos[0] > 1023)
  752. return -1;
  753. pulse->amp[0] = get_bits(gb, 4);
  754. for (i = 1; i < pulse->num_pulse; i++) {
  755. pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
  756. if (pulse->pos[i] > 1023)
  757. return -1;
  758. pulse->amp[i] = get_bits(gb, 4);
  759. }
  760. return 0;
  761. }
  762. /**
  763. * Decode Temporal Noise Shaping data; reference: table 4.48.
  764. *
  765. * @return Returns error status. 0 - OK, !0 - error
  766. */
  767. static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
  768. GetBitContext *gb, const IndividualChannelStream *ics)
  769. {
  770. int w, filt, i, coef_len, coef_res, coef_compress;
  771. const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  772. const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
  773. for (w = 0; w < ics->num_windows; w++) {
  774. if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
  775. coef_res = get_bits1(gb);
  776. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  777. int tmp2_idx;
  778. tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
  779. if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
  780. av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
  781. tns->order[w][filt], tns_max_order);
  782. tns->order[w][filt] = 0;
  783. return -1;
  784. }
  785. if (tns->order[w][filt]) {
  786. tns->direction[w][filt] = get_bits1(gb);
  787. coef_compress = get_bits1(gb);
  788. coef_len = coef_res + 3 - coef_compress;
  789. tmp2_idx = 2 * coef_compress + coef_res;
  790. for (i = 0; i < tns->order[w][filt]; i++)
  791. tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
  792. }
  793. }
  794. }
  795. }
  796. return 0;
  797. }
  798. /**
  799. * Decode Mid/Side data; reference: table 4.54.
  800. *
  801. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  802. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  803. * [3] reserved for scalable AAC
  804. */
  805. static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
  806. int ms_present)
  807. {
  808. int idx;
  809. if (ms_present == 1) {
  810. for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
  811. cpe->ms_mask[idx] = get_bits1(gb);
  812. } else if (ms_present == 2) {
  813. memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
  814. }
  815. }
  816. #ifndef VMUL2
  817. static inline float *VMUL2(float *dst, const float *v, unsigned idx,
  818. const float *scale)
  819. {
  820. float s = *scale;
  821. *dst++ = v[idx & 15] * s;
  822. *dst++ = v[idx>>4 & 15] * s;
  823. return dst;
  824. }
  825. #endif
  826. #ifndef VMUL4
  827. static inline float *VMUL4(float *dst, const float *v, unsigned idx,
  828. const float *scale)
  829. {
  830. float s = *scale;
  831. *dst++ = v[idx & 3] * s;
  832. *dst++ = v[idx>>2 & 3] * s;
  833. *dst++ = v[idx>>4 & 3] * s;
  834. *dst++ = v[idx>>6 & 3] * s;
  835. return dst;
  836. }
  837. #endif
  838. #ifndef VMUL2S
  839. static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
  840. unsigned sign, const float *scale)
  841. {
  842. union float754 s0, s1;
  843. s0.f = s1.f = *scale;
  844. s0.i ^= sign >> 1 << 31;
  845. s1.i ^= sign << 31;
  846. *dst++ = v[idx & 15] * s0.f;
  847. *dst++ = v[idx>>4 & 15] * s1.f;
  848. return dst;
  849. }
  850. #endif
  851. #ifndef VMUL4S
  852. static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
  853. unsigned sign, const float *scale)
  854. {
  855. unsigned nz = idx >> 12;
  856. union float754 s = { .f = *scale };
  857. union float754 t;
  858. t.i = s.i ^ (sign & 1<<31);
  859. *dst++ = v[idx & 3] * t.f;
  860. sign <<= nz & 1; nz >>= 1;
  861. t.i = s.i ^ (sign & 1<<31);
  862. *dst++ = v[idx>>2 & 3] * t.f;
  863. sign <<= nz & 1; nz >>= 1;
  864. t.i = s.i ^ (sign & 1<<31);
  865. *dst++ = v[idx>>4 & 3] * t.f;
  866. sign <<= nz & 1; nz >>= 1;
  867. t.i = s.i ^ (sign & 1<<31);
  868. *dst++ = v[idx>>6 & 3] * t.f;
  869. return dst;
  870. }
  871. #endif
  872. /**
  873. * Decode spectral data; reference: table 4.50.
  874. * Dequantize and scale spectral data; reference: 4.6.3.3.
  875. *
  876. * @param coef array of dequantized, scaled spectral data
  877. * @param sf array of scalefactors or intensity stereo positions
  878. * @param pulse_present set if pulses are present
  879. * @param pulse pointer to pulse data struct
  880. * @param band_type array of the used band type
  881. *
  882. * @return Returns error status. 0 - OK, !0 - error
  883. */
  884. static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
  885. GetBitContext *gb, const float sf[120],
  886. int pulse_present, const Pulse *pulse,
  887. const IndividualChannelStream *ics,
  888. enum BandType band_type[120])
  889. {
  890. int i, k, g, idx = 0;
  891. const int c = 1024 / ics->num_windows;
  892. const uint16_t *offsets = ics->swb_offset;
  893. float *coef_base = coef;
  894. for (g = 0; g < ics->num_windows; g++)
  895. memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
  896. for (g = 0; g < ics->num_window_groups; g++) {
  897. unsigned g_len = ics->group_len[g];
  898. for (i = 0; i < ics->max_sfb; i++, idx++) {
  899. const unsigned cbt_m1 = band_type[idx] - 1;
  900. float *cfo = coef + offsets[i];
  901. int off_len = offsets[i + 1] - offsets[i];
  902. int group;
  903. if (cbt_m1 >= INTENSITY_BT2 - 1) {
  904. for (group = 0; group < g_len; group++, cfo+=128) {
  905. memset(cfo, 0, off_len * sizeof(float));
  906. }
  907. } else if (cbt_m1 == NOISE_BT - 1) {
  908. for (group = 0; group < g_len; group++, cfo+=128) {
  909. float scale;
  910. float band_energy;
  911. for (k = 0; k < off_len; k++) {
  912. ac->random_state = lcg_random(ac->random_state);
  913. cfo[k] = ac->random_state;
  914. }
  915. band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
  916. scale = sf[idx] / sqrtf(band_energy);
  917. ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
  918. }
  919. } else {
  920. const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
  921. const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
  922. VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
  923. OPEN_READER(re, gb);
  924. switch (cbt_m1 >> 1) {
  925. case 0:
  926. for (group = 0; group < g_len; group++, cfo+=128) {
  927. float *cf = cfo;
  928. int len = off_len;
  929. do {
  930. int code;
  931. unsigned cb_idx;
  932. UPDATE_CACHE(re, gb);
  933. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  934. cb_idx = cb_vector_idx[code];
  935. cf = VMUL4(cf, vq, cb_idx, sf + idx);
  936. } while (len -= 4);
  937. }
  938. break;
  939. case 1:
  940. for (group = 0; group < g_len; group++, cfo+=128) {
  941. float *cf = cfo;
  942. int len = off_len;
  943. do {
  944. int code;
  945. unsigned nnz;
  946. unsigned cb_idx;
  947. uint32_t bits;
  948. UPDATE_CACHE(re, gb);
  949. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  950. cb_idx = cb_vector_idx[code];
  951. nnz = cb_idx >> 8 & 15;
  952. bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
  953. LAST_SKIP_BITS(re, gb, nnz);
  954. cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
  955. } while (len -= 4);
  956. }
  957. break;
  958. case 2:
  959. for (group = 0; group < g_len; group++, cfo+=128) {
  960. float *cf = cfo;
  961. int len = off_len;
  962. do {
  963. int code;
  964. unsigned cb_idx;
  965. UPDATE_CACHE(re, gb);
  966. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  967. cb_idx = cb_vector_idx[code];
  968. cf = VMUL2(cf, vq, cb_idx, sf + idx);
  969. } while (len -= 2);
  970. }
  971. break;
  972. case 3:
  973. case 4:
  974. for (group = 0; group < g_len; group++, cfo+=128) {
  975. float *cf = cfo;
  976. int len = off_len;
  977. do {
  978. int code;
  979. unsigned nnz;
  980. unsigned cb_idx;
  981. unsigned sign;
  982. UPDATE_CACHE(re, gb);
  983. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  984. cb_idx = cb_vector_idx[code];
  985. nnz = cb_idx >> 8 & 15;
  986. sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
  987. LAST_SKIP_BITS(re, gb, nnz);
  988. cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
  989. } while (len -= 2);
  990. }
  991. break;
  992. default:
  993. for (group = 0; group < g_len; group++, cfo+=128) {
  994. float *cf = cfo;
  995. uint32_t *icf = (uint32_t *) cf;
  996. int len = off_len;
  997. do {
  998. int code;
  999. unsigned nzt, nnz;
  1000. unsigned cb_idx;
  1001. uint32_t bits;
  1002. int j;
  1003. UPDATE_CACHE(re, gb);
  1004. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1005. if (!code) {
  1006. *icf++ = 0;
  1007. *icf++ = 0;
  1008. continue;
  1009. }
  1010. cb_idx = cb_vector_idx[code];
  1011. nnz = cb_idx >> 12;
  1012. nzt = cb_idx >> 8;
  1013. bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
  1014. LAST_SKIP_BITS(re, gb, nnz);
  1015. for (j = 0; j < 2; j++) {
  1016. if (nzt & 1<<j) {
  1017. uint32_t b;
  1018. int n;
  1019. /* The total length of escape_sequence must be < 22 bits according
  1020. to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
  1021. UPDATE_CACHE(re, gb);
  1022. b = GET_CACHE(re, gb);
  1023. b = 31 - av_log2(~b);
  1024. if (b > 8) {
  1025. av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
  1026. return -1;
  1027. }
  1028. SKIP_BITS(re, gb, b + 1);
  1029. b += 4;
  1030. n = (1 << b) + SHOW_UBITS(re, gb, b);
  1031. LAST_SKIP_BITS(re, gb, b);
  1032. *icf++ = cbrt_tab[n] | (bits & 1<<31);
  1033. bits <<= 1;
  1034. } else {
  1035. unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
  1036. *icf++ = (bits & 1<<31) | v;
  1037. bits <<= !!v;
  1038. }
  1039. cb_idx >>= 4;
  1040. }
  1041. } while (len -= 2);
  1042. ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
  1043. }
  1044. }
  1045. CLOSE_READER(re, gb);
  1046. }
  1047. }
  1048. coef += g_len << 7;
  1049. }
  1050. if (pulse_present) {
  1051. idx = 0;
  1052. for (i = 0; i < pulse->num_pulse; i++) {
  1053. float co = coef_base[ pulse->pos[i] ];
  1054. while (offsets[idx + 1] <= pulse->pos[i])
  1055. idx++;
  1056. if (band_type[idx] != NOISE_BT && sf[idx]) {
  1057. float ico = -pulse->amp[i];
  1058. if (co) {
  1059. co /= sf[idx];
  1060. ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
  1061. }
  1062. coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
  1063. }
  1064. }
  1065. }
  1066. return 0;
  1067. }
  1068. static av_always_inline float flt16_round(float pf)
  1069. {
  1070. union float754 tmp;
  1071. tmp.f = pf;
  1072. tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
  1073. return tmp.f;
  1074. }
  1075. static av_always_inline float flt16_even(float pf)
  1076. {
  1077. union float754 tmp;
  1078. tmp.f = pf;
  1079. tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
  1080. return tmp.f;
  1081. }
  1082. static av_always_inline float flt16_trunc(float pf)
  1083. {
  1084. union float754 pun;
  1085. pun.f = pf;
  1086. pun.i &= 0xFFFF0000U;
  1087. return pun.f;
  1088. }
  1089. static av_always_inline void predict(PredictorState *ps, float *coef,
  1090. float sf_scale, float inv_sf_scale,
  1091. int output_enable)
  1092. {
  1093. const float a = 0.953125; // 61.0 / 64
  1094. const float alpha = 0.90625; // 29.0 / 32
  1095. float e0, e1;
  1096. float pv;
  1097. float k1, k2;
  1098. float r0 = ps->r0, r1 = ps->r1;
  1099. float cor0 = ps->cor0, cor1 = ps->cor1;
  1100. float var0 = ps->var0, var1 = ps->var1;
  1101. k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
  1102. k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
  1103. pv = flt16_round(k1 * r0 + k2 * r1);
  1104. if (output_enable)
  1105. *coef += pv * sf_scale;
  1106. e0 = *coef * inv_sf_scale;
  1107. e1 = e0 - k1 * r0;
  1108. ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
  1109. ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
  1110. ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
  1111. ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
  1112. ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
  1113. ps->r0 = flt16_trunc(a * e0);
  1114. }
  1115. /**
  1116. * Apply AAC-Main style frequency domain prediction.
  1117. */
  1118. static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
  1119. {
  1120. int sfb, k;
  1121. float sf_scale = ac->sf_scale, inv_sf_scale = 1 / ac->sf_scale;
  1122. if (!sce->ics.predictor_initialized) {
  1123. reset_all_predictors(sce->predictor_state);
  1124. sce->ics.predictor_initialized = 1;
  1125. }
  1126. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  1127. for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
  1128. for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
  1129. predict(&sce->predictor_state[k], &sce->coeffs[k],
  1130. sf_scale, inv_sf_scale,
  1131. sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
  1132. }
  1133. }
  1134. if (sce->ics.predictor_reset_group)
  1135. reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
  1136. } else
  1137. reset_all_predictors(sce->predictor_state);
  1138. }
  1139. /**
  1140. * Decode an individual_channel_stream payload; reference: table 4.44.
  1141. *
  1142. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  1143. * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
  1144. *
  1145. * @return Returns error status. 0 - OK, !0 - error
  1146. */
  1147. static int decode_ics(AACContext *ac, SingleChannelElement *sce,
  1148. GetBitContext *gb, int common_window, int scale_flag)
  1149. {
  1150. Pulse pulse;
  1151. TemporalNoiseShaping *tns = &sce->tns;
  1152. IndividualChannelStream *ics = &sce->ics;
  1153. float *out = sce->coeffs;
  1154. int global_gain, pulse_present = 0;
  1155. /* This assignment is to silence a GCC warning about the variable being used
  1156. * uninitialized when in fact it always is.
  1157. */
  1158. pulse.num_pulse = 0;
  1159. global_gain = get_bits(gb, 8);
  1160. if (!common_window && !scale_flag) {
  1161. if (decode_ics_info(ac, ics, gb, 0) < 0)
  1162. return -1;
  1163. }
  1164. if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
  1165. return -1;
  1166. if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
  1167. return -1;
  1168. pulse_present = 0;
  1169. if (!scale_flag) {
  1170. if ((pulse_present = get_bits1(gb))) {
  1171. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1172. av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
  1173. return -1;
  1174. }
  1175. if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
  1176. av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
  1177. return -1;
  1178. }
  1179. }
  1180. if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
  1181. return -1;
  1182. if (get_bits1(gb)) {
  1183. av_log_missing_feature(ac->avctx, "SSR", 1);
  1184. return -1;
  1185. }
  1186. }
  1187. if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
  1188. return -1;
  1189. if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
  1190. apply_prediction(ac, sce);
  1191. return 0;
  1192. }
  1193. /**
  1194. * Mid/Side stereo decoding; reference: 4.6.8.1.3.
  1195. */
  1196. static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
  1197. {
  1198. const IndividualChannelStream *ics = &cpe->ch[0].ics;
  1199. float *ch0 = cpe->ch[0].coeffs;
  1200. float *ch1 = cpe->ch[1].coeffs;
  1201. int g, i, group, idx = 0;
  1202. const uint16_t *offsets = ics->swb_offset;
  1203. for (g = 0; g < ics->num_window_groups; g++) {
  1204. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1205. if (cpe->ms_mask[idx] &&
  1206. cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
  1207. for (group = 0; group < ics->group_len[g]; group++) {
  1208. ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
  1209. ch1 + group * 128 + offsets[i],
  1210. offsets[i+1] - offsets[i]);
  1211. }
  1212. }
  1213. }
  1214. ch0 += ics->group_len[g] * 128;
  1215. ch1 += ics->group_len[g] * 128;
  1216. }
  1217. }
  1218. /**
  1219. * intensity stereo decoding; reference: 4.6.8.2.3
  1220. *
  1221. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  1222. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  1223. * [3] reserved for scalable AAC
  1224. */
  1225. static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
  1226. {
  1227. const IndividualChannelStream *ics = &cpe->ch[1].ics;
  1228. SingleChannelElement *sce1 = &cpe->ch[1];
  1229. float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
  1230. const uint16_t *offsets = ics->swb_offset;
  1231. int g, group, i, k, idx = 0;
  1232. int c;
  1233. float scale;
  1234. for (g = 0; g < ics->num_window_groups; g++) {
  1235. for (i = 0; i < ics->max_sfb;) {
  1236. if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
  1237. const int bt_run_end = sce1->band_type_run_end[idx];
  1238. for (; i < bt_run_end; i++, idx++) {
  1239. c = -1 + 2 * (sce1->band_type[idx] - 14);
  1240. if (ms_present)
  1241. c *= 1 - 2 * cpe->ms_mask[idx];
  1242. scale = c * sce1->sf[idx];
  1243. for (group = 0; group < ics->group_len[g]; group++)
  1244. for (k = offsets[i]; k < offsets[i + 1]; k++)
  1245. coef1[group * 128 + k] = scale * coef0[group * 128 + k];
  1246. }
  1247. } else {
  1248. int bt_run_end = sce1->band_type_run_end[idx];
  1249. idx += bt_run_end - i;
  1250. i = bt_run_end;
  1251. }
  1252. }
  1253. coef0 += ics->group_len[g] * 128;
  1254. coef1 += ics->group_len[g] * 128;
  1255. }
  1256. }
  1257. /**
  1258. * Decode a channel_pair_element; reference: table 4.4.
  1259. *
  1260. * @return Returns error status. 0 - OK, !0 - error
  1261. */
  1262. static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
  1263. {
  1264. int i, ret, common_window, ms_present = 0;
  1265. common_window = get_bits1(gb);
  1266. if (common_window) {
  1267. if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
  1268. return -1;
  1269. i = cpe->ch[1].ics.use_kb_window[0];
  1270. cpe->ch[1].ics = cpe->ch[0].ics;
  1271. cpe->ch[1].ics.use_kb_window[1] = i;
  1272. ms_present = get_bits(gb, 2);
  1273. if (ms_present == 3) {
  1274. av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
  1275. return -1;
  1276. } else if (ms_present)
  1277. decode_mid_side_stereo(cpe, gb, ms_present);
  1278. }
  1279. if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
  1280. return ret;
  1281. if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
  1282. return ret;
  1283. if (common_window) {
  1284. if (ms_present)
  1285. apply_mid_side_stereo(ac, cpe);
  1286. if (ac->m4ac.object_type == AOT_AAC_MAIN) {
  1287. apply_prediction(ac, &cpe->ch[0]);
  1288. apply_prediction(ac, &cpe->ch[1]);
  1289. }
  1290. }
  1291. apply_intensity_stereo(cpe, ms_present);
  1292. return 0;
  1293. }
  1294. static const float cce_scale[] = {
  1295. 1.09050773266525765921, //2^(1/8)
  1296. 1.18920711500272106672, //2^(1/4)
  1297. M_SQRT2,
  1298. 2,
  1299. };
  1300. /**
  1301. * Decode coupling_channel_element; reference: table 4.8.
  1302. *
  1303. * @return Returns error status. 0 - OK, !0 - error
  1304. */
  1305. static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
  1306. {
  1307. int num_gain = 0;
  1308. int c, g, sfb, ret;
  1309. int sign;
  1310. float scale;
  1311. SingleChannelElement *sce = &che->ch[0];
  1312. ChannelCoupling *coup = &che->coup;
  1313. coup->coupling_point = 2 * get_bits1(gb);
  1314. coup->num_coupled = get_bits(gb, 3);
  1315. for (c = 0; c <= coup->num_coupled; c++) {
  1316. num_gain++;
  1317. coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
  1318. coup->id_select[c] = get_bits(gb, 4);
  1319. if (coup->type[c] == TYPE_CPE) {
  1320. coup->ch_select[c] = get_bits(gb, 2);
  1321. if (coup->ch_select[c] == 3)
  1322. num_gain++;
  1323. } else
  1324. coup->ch_select[c] = 2;
  1325. }
  1326. coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
  1327. sign = get_bits(gb, 1);
  1328. scale = cce_scale[get_bits(gb, 2)];
  1329. if ((ret = decode_ics(ac, sce, gb, 0, 0)))
  1330. return ret;
  1331. for (c = 0; c < num_gain; c++) {
  1332. int idx = 0;
  1333. int cge = 1;
  1334. int gain = 0;
  1335. float gain_cache = 1.;
  1336. if (c) {
  1337. cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
  1338. gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
  1339. gain_cache = powf(scale, -gain);
  1340. }
  1341. if (coup->coupling_point == AFTER_IMDCT) {
  1342. coup->gain[c][0] = gain_cache;
  1343. } else {
  1344. for (g = 0; g < sce->ics.num_window_groups; g++) {
  1345. for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
  1346. if (sce->band_type[idx] != ZERO_BT) {
  1347. if (!cge) {
  1348. int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1349. if (t) {
  1350. int s = 1;
  1351. t = gain += t;
  1352. if (sign) {
  1353. s -= 2 * (t & 0x1);
  1354. t >>= 1;
  1355. }
  1356. gain_cache = powf(scale, -t) * s;
  1357. }
  1358. }
  1359. coup->gain[c][idx] = gain_cache;
  1360. }
  1361. }
  1362. }
  1363. }
  1364. }
  1365. return 0;
  1366. }
  1367. /**
  1368. * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
  1369. *
  1370. * @return Returns number of bytes consumed.
  1371. */
  1372. static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
  1373. GetBitContext *gb)
  1374. {
  1375. int i;
  1376. int num_excl_chan = 0;
  1377. do {
  1378. for (i = 0; i < 7; i++)
  1379. che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
  1380. } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
  1381. return num_excl_chan / 7;
  1382. }
  1383. /**
  1384. * Decode dynamic range information; reference: table 4.52.
  1385. *
  1386. * @param cnt length of TYPE_FIL syntactic element in bytes
  1387. *
  1388. * @return Returns number of bytes consumed.
  1389. */
  1390. static int decode_dynamic_range(DynamicRangeControl *che_drc,
  1391. GetBitContext *gb, int cnt)
  1392. {
  1393. int n = 1;
  1394. int drc_num_bands = 1;
  1395. int i;
  1396. /* pce_tag_present? */
  1397. if (get_bits1(gb)) {
  1398. che_drc->pce_instance_tag = get_bits(gb, 4);
  1399. skip_bits(gb, 4); // tag_reserved_bits
  1400. n++;
  1401. }
  1402. /* excluded_chns_present? */
  1403. if (get_bits1(gb)) {
  1404. n += decode_drc_channel_exclusions(che_drc, gb);
  1405. }
  1406. /* drc_bands_present? */
  1407. if (get_bits1(gb)) {
  1408. che_drc->band_incr = get_bits(gb, 4);
  1409. che_drc->interpolation_scheme = get_bits(gb, 4);
  1410. n++;
  1411. drc_num_bands += che_drc->band_incr;
  1412. for (i = 0; i < drc_num_bands; i++) {
  1413. che_drc->band_top[i] = get_bits(gb, 8);
  1414. n++;
  1415. }
  1416. }
  1417. /* prog_ref_level_present? */
  1418. if (get_bits1(gb)) {
  1419. che_drc->prog_ref_level = get_bits(gb, 7);
  1420. skip_bits1(gb); // prog_ref_level_reserved_bits
  1421. n++;
  1422. }
  1423. for (i = 0; i < drc_num_bands; i++) {
  1424. che_drc->dyn_rng_sgn[i] = get_bits1(gb);
  1425. che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
  1426. n++;
  1427. }
  1428. return n;
  1429. }
  1430. /**
  1431. * Decode extension data (incomplete); reference: table 4.51.
  1432. *
  1433. * @param cnt length of TYPE_FIL syntactic element in bytes
  1434. *
  1435. * @return Returns number of bytes consumed
  1436. */
  1437. static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
  1438. ChannelElement *che, enum RawDataBlockType elem_type)
  1439. {
  1440. int crc_flag = 0;
  1441. int res = cnt;
  1442. switch (get_bits(gb, 4)) { // extension type
  1443. case EXT_SBR_DATA_CRC:
  1444. crc_flag++;
  1445. case EXT_SBR_DATA:
  1446. if (!che) {
  1447. av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
  1448. return res;
  1449. } else if (!ac->m4ac.sbr) {
  1450. av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
  1451. skip_bits_long(gb, 8 * cnt - 4);
  1452. return res;
  1453. } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
  1454. av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
  1455. skip_bits_long(gb, 8 * cnt - 4);
  1456. return res;
  1457. } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) {
  1458. ac->m4ac.sbr = 1;
  1459. ac->m4ac.ps = 1;
  1460. output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured);
  1461. } else {
  1462. ac->m4ac.sbr = 1;
  1463. }
  1464. res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
  1465. break;
  1466. case EXT_DYNAMIC_RANGE:
  1467. res = decode_dynamic_range(&ac->che_drc, gb, cnt);
  1468. break;
  1469. case EXT_FILL:
  1470. case EXT_FILL_DATA:
  1471. case EXT_DATA_ELEMENT:
  1472. default:
  1473. skip_bits_long(gb, 8 * cnt - 4);
  1474. break;
  1475. };
  1476. return res;
  1477. }
  1478. /**
  1479. * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
  1480. *
  1481. * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
  1482. * @param coef spectral coefficients
  1483. */
  1484. static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
  1485. IndividualChannelStream *ics, int decode)
  1486. {
  1487. const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
  1488. int w, filt, m, i;
  1489. int bottom, top, order, start, end, size, inc;
  1490. float lpc[TNS_MAX_ORDER];
  1491. for (w = 0; w < ics->num_windows; w++) {
  1492. bottom = ics->num_swb;
  1493. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  1494. top = bottom;
  1495. bottom = FFMAX(0, top - tns->length[w][filt]);
  1496. order = tns->order[w][filt];
  1497. if (order == 0)
  1498. continue;
  1499. // tns_decode_coef
  1500. compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
  1501. start = ics->swb_offset[FFMIN(bottom, mmm)];
  1502. end = ics->swb_offset[FFMIN( top, mmm)];
  1503. if ((size = end - start) <= 0)
  1504. continue;
  1505. if (tns->direction[w][filt]) {
  1506. inc = -1;
  1507. start = end - 1;
  1508. } else {
  1509. inc = 1;
  1510. }
  1511. start += w * 128;
  1512. // ar filter
  1513. for (m = 0; m < size; m++, start += inc)
  1514. for (i = 1; i <= FFMIN(m, order); i++)
  1515. coef[start] -= coef[start - i * inc] * lpc[i - 1];
  1516. }
  1517. }
  1518. }
  1519. /**
  1520. * Conduct IMDCT and windowing.
  1521. */
  1522. static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
  1523. {
  1524. IndividualChannelStream *ics = &sce->ics;
  1525. float *in = sce->coeffs;
  1526. float *out = sce->ret;
  1527. float *saved = sce->saved;
  1528. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  1529. const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1530. const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  1531. float *buf = ac->buf_mdct;
  1532. float *temp = ac->temp;
  1533. int i;
  1534. // imdct
  1535. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1536. for (i = 0; i < 1024; i += 128)
  1537. ff_imdct_half(&ac->mdct_small, buf + i, in + i);
  1538. } else
  1539. ff_imdct_half(&ac->mdct, buf, in);
  1540. /* window overlapping
  1541. * NOTE: To simplify the overlapping code, all 'meaningless' short to long
  1542. * and long to short transitions are considered to be short to short
  1543. * transitions. This leaves just two cases (long to long and short to short)
  1544. * with a little special sauce for EIGHT_SHORT_SEQUENCE.
  1545. */
  1546. if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
  1547. (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
  1548. ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
  1549. } else {
  1550. memcpy( out, saved, 448 * sizeof(float));
  1551. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1552. ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
  1553. ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
  1554. ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
  1555. ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
  1556. ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
  1557. memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
  1558. } else {
  1559. ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
  1560. memcpy( out + 576, buf + 64, 448 * sizeof(float));
  1561. }
  1562. }
  1563. // buffer update
  1564. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1565. memcpy( saved, temp + 64, 64 * sizeof(float));
  1566. ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
  1567. ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
  1568. ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
  1569. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  1570. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  1571. memcpy( saved, buf + 512, 448 * sizeof(float));
  1572. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  1573. } else { // LONG_STOP or ONLY_LONG
  1574. memcpy( saved, buf + 512, 512 * sizeof(float));
  1575. }
  1576. }
  1577. /**
  1578. * Apply dependent channel coupling (applied before IMDCT).
  1579. *
  1580. * @param index index into coupling gain array
  1581. */
  1582. static void apply_dependent_coupling(AACContext *ac,
  1583. SingleChannelElement *target,
  1584. ChannelElement *cce, int index)
  1585. {
  1586. IndividualChannelStream *ics = &cce->ch[0].ics;
  1587. const uint16_t *offsets = ics->swb_offset;
  1588. float *dest = target->coeffs;
  1589. const float *src = cce->ch[0].coeffs;
  1590. int g, i, group, k, idx = 0;
  1591. if (ac->m4ac.object_type == AOT_AAC_LTP) {
  1592. av_log(ac->avctx, AV_LOG_ERROR,
  1593. "Dependent coupling is not supported together with LTP\n");
  1594. return;
  1595. }
  1596. for (g = 0; g < ics->num_window_groups; g++) {
  1597. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1598. if (cce->ch[0].band_type[idx] != ZERO_BT) {
  1599. const float gain = cce->coup.gain[index][idx];
  1600. for (group = 0; group < ics->group_len[g]; group++) {
  1601. for (k = offsets[i]; k < offsets[i + 1]; k++) {
  1602. // XXX dsputil-ize
  1603. dest[group * 128 + k] += gain * src[group * 128 + k];
  1604. }
  1605. }
  1606. }
  1607. }
  1608. dest += ics->group_len[g] * 128;
  1609. src += ics->group_len[g] * 128;
  1610. }
  1611. }
  1612. /**
  1613. * Apply independent channel coupling (applied after IMDCT).
  1614. *
  1615. * @param index index into coupling gain array
  1616. */
  1617. static void apply_independent_coupling(AACContext *ac,
  1618. SingleChannelElement *target,
  1619. ChannelElement *cce, int index)
  1620. {
  1621. int i;
  1622. const float gain = cce->coup.gain[index][0];
  1623. const float *src = cce->ch[0].ret;
  1624. float *dest = target->ret;
  1625. const int len = 1024 << (ac->m4ac.sbr == 1);
  1626. for (i = 0; i < len; i++)
  1627. dest[i] += gain * src[i];
  1628. }
  1629. /**
  1630. * channel coupling transformation interface
  1631. *
  1632. * @param apply_coupling_method pointer to (in)dependent coupling function
  1633. */
  1634. static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
  1635. enum RawDataBlockType type, int elem_id,
  1636. enum CouplingPoint coupling_point,
  1637. void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
  1638. {
  1639. int i, c;
  1640. for (i = 0; i < MAX_ELEM_ID; i++) {
  1641. ChannelElement *cce = ac->che[TYPE_CCE][i];
  1642. int index = 0;
  1643. if (cce && cce->coup.coupling_point == coupling_point) {
  1644. ChannelCoupling *coup = &cce->coup;
  1645. for (c = 0; c <= coup->num_coupled; c++) {
  1646. if (coup->type[c] == type && coup->id_select[c] == elem_id) {
  1647. if (coup->ch_select[c] != 1) {
  1648. apply_coupling_method(ac, &cc->ch[0], cce, index);
  1649. if (coup->ch_select[c] != 0)
  1650. index++;
  1651. }
  1652. if (coup->ch_select[c] != 2)
  1653. apply_coupling_method(ac, &cc->ch[1], cce, index++);
  1654. } else
  1655. index += 1 + (coup->ch_select[c] == 3);
  1656. }
  1657. }
  1658. }
  1659. }
  1660. /**
  1661. * Convert spectral data to float samples, applying all supported tools as appropriate.
  1662. */
  1663. static void spectral_to_sample(AACContext *ac)
  1664. {
  1665. int i, type;
  1666. for (type = 3; type >= 0; type--) {
  1667. for (i = 0; i < MAX_ELEM_ID; i++) {
  1668. ChannelElement *che = ac->che[type][i];
  1669. if (che) {
  1670. if (type <= TYPE_CPE)
  1671. apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
  1672. if (che->ch[0].tns.present)
  1673. apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
  1674. if (che->ch[1].tns.present)
  1675. apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
  1676. if (type <= TYPE_CPE)
  1677. apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
  1678. if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
  1679. imdct_and_windowing(ac, &che->ch[0]);
  1680. if (type == TYPE_CPE) {
  1681. imdct_and_windowing(ac, &che->ch[1]);
  1682. }
  1683. if (ac->m4ac.sbr > 0) {
  1684. ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
  1685. }
  1686. }
  1687. if (type <= TYPE_CCE)
  1688. apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
  1689. }
  1690. }
  1691. }
  1692. }
  1693. static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
  1694. {
  1695. int size;
  1696. AACADTSHeaderInfo hdr_info;
  1697. size = ff_aac_parse_header(gb, &hdr_info);
  1698. if (size > 0) {
  1699. if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
  1700. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  1701. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  1702. ac->m4ac.chan_config = hdr_info.chan_config;
  1703. if (set_default_channel_config(ac->avctx, new_che_pos, hdr_info.chan_config))
  1704. return -7;
  1705. if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
  1706. return -7;
  1707. } else if (ac->output_configured != OC_LOCKED) {
  1708. ac->output_configured = OC_NONE;
  1709. }
  1710. if (ac->output_configured != OC_LOCKED) {
  1711. ac->m4ac.sbr = -1;
  1712. ac->m4ac.ps = -1;
  1713. }
  1714. ac->m4ac.sample_rate = hdr_info.sample_rate;
  1715. ac->m4ac.sampling_index = hdr_info.sampling_index;
  1716. ac->m4ac.object_type = hdr_info.object_type;
  1717. if (!ac->avctx->sample_rate)
  1718. ac->avctx->sample_rate = hdr_info.sample_rate;
  1719. if (hdr_info.num_aac_frames == 1) {
  1720. if (!hdr_info.crc_absent)
  1721. skip_bits(gb, 16);
  1722. } else {
  1723. av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
  1724. return -1;
  1725. }
  1726. }
  1727. return size;
  1728. }
  1729. static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
  1730. int *data_size, GetBitContext *gb)
  1731. {
  1732. AACContext *ac = avctx->priv_data;
  1733. ChannelElement *che = NULL, *che_prev = NULL;
  1734. enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
  1735. int err, elem_id, data_size_tmp;
  1736. int samples = 0, multiplier;
  1737. if (show_bits(gb, 12) == 0xfff) {
  1738. if (parse_adts_frame_header(ac, gb) < 0) {
  1739. av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
  1740. return -1;
  1741. }
  1742. if (ac->m4ac.sampling_index > 12) {
  1743. av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
  1744. return -1;
  1745. }
  1746. }
  1747. ac->tags_mapped = 0;
  1748. // parse
  1749. while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
  1750. elem_id = get_bits(gb, 4);
  1751. if (elem_type < TYPE_DSE) {
  1752. if (!(che=get_che(ac, elem_type, elem_id))) {
  1753. av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
  1754. elem_type, elem_id);
  1755. return -1;
  1756. }
  1757. samples = 1024;
  1758. }
  1759. switch (elem_type) {
  1760. case TYPE_SCE:
  1761. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  1762. break;
  1763. case TYPE_CPE:
  1764. err = decode_cpe(ac, gb, che);
  1765. break;
  1766. case TYPE_CCE:
  1767. err = decode_cce(ac, gb, che);
  1768. break;
  1769. case TYPE_LFE:
  1770. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  1771. break;
  1772. case TYPE_DSE:
  1773. err = skip_data_stream_element(ac, gb);
  1774. break;
  1775. case TYPE_PCE: {
  1776. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  1777. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  1778. if ((err = decode_pce(avctx, &ac->m4ac, new_che_pos, gb)))
  1779. break;
  1780. if (ac->output_configured > OC_TRIAL_PCE)
  1781. av_log(avctx, AV_LOG_ERROR,
  1782. "Not evaluating a further program_config_element as this construct is dubious at best.\n");
  1783. else
  1784. err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
  1785. break;
  1786. }
  1787. case TYPE_FIL:
  1788. if (elem_id == 15)
  1789. elem_id += get_bits(gb, 8) - 1;
  1790. if (get_bits_left(gb) < 8 * elem_id) {
  1791. av_log(avctx, AV_LOG_ERROR, overread_err);
  1792. return -1;
  1793. }
  1794. while (elem_id > 0)
  1795. elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
  1796. err = 0; /* FIXME */
  1797. break;
  1798. default:
  1799. err = -1; /* should not happen, but keeps compiler happy */
  1800. break;
  1801. }
  1802. che_prev = che;
  1803. elem_type_prev = elem_type;
  1804. if (err)
  1805. return err;
  1806. if (get_bits_left(gb) < 3) {
  1807. av_log(avctx, AV_LOG_ERROR, overread_err);
  1808. return -1;
  1809. }
  1810. }
  1811. spectral_to_sample(ac);
  1812. multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
  1813. samples <<= multiplier;
  1814. if (ac->output_configured < OC_LOCKED) {
  1815. avctx->sample_rate = ac->m4ac.sample_rate << multiplier;
  1816. avctx->frame_size = samples;
  1817. }
  1818. data_size_tmp = samples * avctx->channels * sizeof(int16_t);
  1819. if (*data_size < data_size_tmp) {
  1820. av_log(avctx, AV_LOG_ERROR,
  1821. "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
  1822. *data_size, data_size_tmp);
  1823. return -1;
  1824. }
  1825. *data_size = data_size_tmp;
  1826. if (samples)
  1827. ac->fmt_conv.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels);
  1828. if (ac->output_configured)
  1829. ac->output_configured = OC_LOCKED;
  1830. return 0;
  1831. }
  1832. static int aac_decode_frame(AVCodecContext *avctx, void *data,
  1833. int *data_size, AVPacket *avpkt)
  1834. {
  1835. const uint8_t *buf = avpkt->data;
  1836. int buf_size = avpkt->size;
  1837. GetBitContext gb;
  1838. int buf_consumed;
  1839. int buf_offset;
  1840. int err;
  1841. init_get_bits(&gb, buf, buf_size * 8);
  1842. if ((err = aac_decode_frame_int(avctx, data, data_size, &gb)) < 0)
  1843. return err;
  1844. buf_consumed = (get_bits_count(&gb) + 7) >> 3;
  1845. for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
  1846. if (buf[buf_offset])
  1847. break;
  1848. return buf_size > buf_offset ? buf_consumed : buf_size;
  1849. }
  1850. static av_cold int aac_decode_close(AVCodecContext *avctx)
  1851. {
  1852. AACContext *ac = avctx->priv_data;
  1853. int i, type;
  1854. for (i = 0; i < MAX_ELEM_ID; i++) {
  1855. for (type = 0; type < 4; type++) {
  1856. if (ac->che[type][i])
  1857. ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
  1858. av_freep(&ac->che[type][i]);
  1859. }
  1860. }
  1861. ff_mdct_end(&ac->mdct);
  1862. ff_mdct_end(&ac->mdct_small);
  1863. return 0;
  1864. }
  1865. #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
  1866. struct LATMContext {
  1867. AACContext aac_ctx; ///< containing AACContext
  1868. int initialized; ///< initilized after a valid extradata was seen
  1869. // parser data
  1870. int audio_mux_version_A; ///< LATM syntax version
  1871. int frame_length_type; ///< 0/1 variable/fixed frame length
  1872. int frame_length; ///< frame length for fixed frame length
  1873. };
  1874. static inline uint32_t latm_get_value(GetBitContext *b)
  1875. {
  1876. int length = get_bits(b, 2);
  1877. return get_bits_long(b, (length+1)*8);
  1878. }
  1879. static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
  1880. GetBitContext *gb)
  1881. {
  1882. AVCodecContext *avctx = latmctx->aac_ctx.avctx;
  1883. MPEG4AudioConfig m4ac;
  1884. int config_start_bit = get_bits_count(gb);
  1885. int bits_consumed, esize;
  1886. if (config_start_bit % 8) {
  1887. av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
  1888. "config not byte aligned.\n", 1);
  1889. return AVERROR_INVALIDDATA;
  1890. } else {
  1891. bits_consumed =
  1892. decode_audio_specific_config(NULL, avctx, &m4ac,
  1893. gb->buffer + (config_start_bit / 8),
  1894. get_bits_left(gb) / 8);
  1895. if (bits_consumed < 0)
  1896. return AVERROR_INVALIDDATA;
  1897. esize = (bits_consumed+7) / 8;
  1898. if (avctx->extradata_size <= esize) {
  1899. av_free(avctx->extradata);
  1900. avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
  1901. if (!avctx->extradata)
  1902. return AVERROR(ENOMEM);
  1903. }
  1904. avctx->extradata_size = esize;
  1905. memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
  1906. memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  1907. skip_bits_long(gb, bits_consumed);
  1908. }
  1909. return bits_consumed;
  1910. }
  1911. static int read_stream_mux_config(struct LATMContext *latmctx,
  1912. GetBitContext *gb)
  1913. {
  1914. int ret, audio_mux_version = get_bits(gb, 1);
  1915. latmctx->audio_mux_version_A = 0;
  1916. if (audio_mux_version)
  1917. latmctx->audio_mux_version_A = get_bits(gb, 1);
  1918. if (!latmctx->audio_mux_version_A) {
  1919. if (audio_mux_version)
  1920. latm_get_value(gb); // taraFullness
  1921. skip_bits(gb, 1); // allStreamSameTimeFraming
  1922. skip_bits(gb, 6); // numSubFrames
  1923. // numPrograms
  1924. if (get_bits(gb, 4)) { // numPrograms
  1925. av_log_missing_feature(latmctx->aac_ctx.avctx,
  1926. "multiple programs are not supported\n", 1);
  1927. return AVERROR_PATCHWELCOME;
  1928. }
  1929. // for each program (which there is only on in DVB)
  1930. // for each layer (which there is only on in DVB)
  1931. if (get_bits(gb, 3)) { // numLayer
  1932. av_log_missing_feature(latmctx->aac_ctx.avctx,
  1933. "multiple layers are not supported\n", 1);
  1934. return AVERROR_PATCHWELCOME;
  1935. }
  1936. // for all but first stream: use_same_config = get_bits(gb, 1);
  1937. if (!audio_mux_version) {
  1938. if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
  1939. return ret;
  1940. } else {
  1941. int ascLen = latm_get_value(gb);
  1942. if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
  1943. return ret;
  1944. ascLen -= ret;
  1945. skip_bits_long(gb, ascLen);
  1946. }
  1947. latmctx->frame_length_type = get_bits(gb, 3);
  1948. switch (latmctx->frame_length_type) {
  1949. case 0:
  1950. skip_bits(gb, 8); // latmBufferFullness
  1951. break;
  1952. case 1:
  1953. latmctx->frame_length = get_bits(gb, 9);
  1954. break;
  1955. case 3:
  1956. case 4:
  1957. case 5:
  1958. skip_bits(gb, 6); // CELP frame length table index
  1959. break;
  1960. case 6:
  1961. case 7:
  1962. skip_bits(gb, 1); // HVXC frame length table index
  1963. break;
  1964. }
  1965. if (get_bits(gb, 1)) { // other data
  1966. if (audio_mux_version) {
  1967. latm_get_value(gb); // other_data_bits
  1968. } else {
  1969. int esc;
  1970. do {
  1971. esc = get_bits(gb, 1);
  1972. skip_bits(gb, 8);
  1973. } while (esc);
  1974. }
  1975. }
  1976. if (get_bits(gb, 1)) // crc present
  1977. skip_bits(gb, 8); // config_crc
  1978. }
  1979. return 0;
  1980. }
  1981. static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
  1982. {
  1983. uint8_t tmp;
  1984. if (ctx->frame_length_type == 0) {
  1985. int mux_slot_length = 0;
  1986. do {
  1987. tmp = get_bits(gb, 8);
  1988. mux_slot_length += tmp;
  1989. } while (tmp == 255);
  1990. return mux_slot_length;
  1991. } else if (ctx->frame_length_type == 1) {
  1992. return ctx->frame_length;
  1993. } else if (ctx->frame_length_type == 3 ||
  1994. ctx->frame_length_type == 5 ||
  1995. ctx->frame_length_type == 7) {
  1996. skip_bits(gb, 2); // mux_slot_length_coded
  1997. }
  1998. return 0;
  1999. }
  2000. static int read_audio_mux_element(struct LATMContext *latmctx,
  2001. GetBitContext *gb)
  2002. {
  2003. int err;
  2004. uint8_t use_same_mux = get_bits(gb, 1);
  2005. if (!use_same_mux) {
  2006. if ((err = read_stream_mux_config(latmctx, gb)) < 0)
  2007. return err;
  2008. } else if (!latmctx->aac_ctx.avctx->extradata) {
  2009. av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
  2010. "no decoder config found\n");
  2011. return AVERROR(EAGAIN);
  2012. }
  2013. if (latmctx->audio_mux_version_A == 0) {
  2014. int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
  2015. if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
  2016. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
  2017. return AVERROR_INVALIDDATA;
  2018. } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
  2019. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  2020. "frame length mismatch %d << %d\n",
  2021. mux_slot_length_bytes * 8, get_bits_left(gb));
  2022. return AVERROR_INVALIDDATA;
  2023. }
  2024. }
  2025. return 0;
  2026. }
  2027. static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size,
  2028. AVPacket *avpkt)
  2029. {
  2030. struct LATMContext *latmctx = avctx->priv_data;
  2031. int muxlength, err;
  2032. GetBitContext gb;
  2033. if (avpkt->size == 0)
  2034. return 0;
  2035. init_get_bits(&gb, avpkt->data, avpkt->size * 8);
  2036. // check for LOAS sync word
  2037. if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
  2038. return AVERROR_INVALIDDATA;
  2039. muxlength = get_bits(&gb, 13) + 3;
  2040. // not enough data, the parser should have sorted this
  2041. if (muxlength > avpkt->size)
  2042. return AVERROR_INVALIDDATA;
  2043. if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
  2044. return err;
  2045. if (!latmctx->initialized) {
  2046. if (!avctx->extradata) {
  2047. *out_size = 0;
  2048. return avpkt->size;
  2049. } else {
  2050. if ((err = aac_decode_init(avctx)) < 0)
  2051. return err;
  2052. latmctx->initialized = 1;
  2053. }
  2054. }
  2055. if (show_bits(&gb, 12) == 0xfff) {
  2056. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  2057. "ADTS header detected, probably as result of configuration "
  2058. "misparsing\n");
  2059. return AVERROR_INVALIDDATA;
  2060. }
  2061. if ((err = aac_decode_frame_int(avctx, out, out_size, &gb)) < 0)
  2062. return err;
  2063. return muxlength;
  2064. }
  2065. av_cold static int latm_decode_init(AVCodecContext *avctx)
  2066. {
  2067. struct LATMContext *latmctx = avctx->priv_data;
  2068. int ret;
  2069. ret = aac_decode_init(avctx);
  2070. if (avctx->extradata_size > 0) {
  2071. latmctx->initialized = !ret;
  2072. } else {
  2073. latmctx->initialized = 0;
  2074. }
  2075. return ret;
  2076. }
  2077. AVCodec ff_aac_decoder = {
  2078. "aac",
  2079. AVMEDIA_TYPE_AUDIO,
  2080. CODEC_ID_AAC,
  2081. sizeof(AACContext),
  2082. aac_decode_init,
  2083. NULL,
  2084. aac_decode_close,
  2085. aac_decode_frame,
  2086. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  2087. .sample_fmts = (const enum AVSampleFormat[]) {
  2088. AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE
  2089. },
  2090. .channel_layouts = aac_channel_layout,
  2091. };
  2092. /*
  2093. Note: This decoder filter is intended to decode LATM streams transferred
  2094. in MPEG transport streams which only contain one program.
  2095. To do a more complex LATM demuxing a separate LATM demuxer should be used.
  2096. */
  2097. AVCodec ff_aac_latm_decoder = {
  2098. .name = "aac_latm",
  2099. .type = AVMEDIA_TYPE_AUDIO,
  2100. .id = CODEC_ID_AAC_LATM,
  2101. .priv_data_size = sizeof(struct LATMContext),
  2102. .init = latm_decode_init,
  2103. .close = aac_decode_close,
  2104. .decode = latm_decode_frame,
  2105. .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"),
  2106. .sample_fmts = (const enum AVSampleFormat[]) {
  2107. AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE
  2108. },
  2109. .channel_layouts = aac_channel_layout,
  2110. };