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.

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