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.

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