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.

2914 lines
101KB

  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-libav@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. * Y Long Term Prediction
  44. * Y intensity stereo
  45. * Y channel coupling
  46. * Y frequency domain prediction
  47. * Y Perceptual Noise Substitution
  48. * Y Mid/Side stereo
  49. * N Scalable Inverse AAC Quantization
  50. * N Frequency Selective Switch
  51. * N upsampling filter
  52. * Y quantization & coding - AAC
  53. * N quantization & coding - TwinVQ
  54. * N quantization & coding - BSAC
  55. * N AAC Error Resilience tools
  56. * N Error Resilience payload syntax
  57. * N Error Protection tool
  58. * N CELP
  59. * N Silence Compression
  60. * N HVXC
  61. * N HVXC 4kbits/s VR
  62. * N Structured Audio tools
  63. * N Structured Audio Sample Bank Format
  64. * N MIDI
  65. * N Harmonic and Individual Lines plus Noise
  66. * N Text-To-Speech Interface
  67. * Y Spectral Band Replication
  68. * Y (not in this code) Layer-1
  69. * Y (not in this code) Layer-2
  70. * Y (not in this code) Layer-3
  71. * N SinuSoidal Coding (Transient, Sinusoid, Noise)
  72. * Y Parametric Stereo
  73. * N Direct Stream Transfer
  74. *
  75. * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
  76. * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
  77. Parametric Stereo.
  78. */
  79. #include "avcodec.h"
  80. #include "internal.h"
  81. #include "get_bits.h"
  82. #include "dsputil.h"
  83. #include "fft.h"
  84. #include "fmtconvert.h"
  85. #include "lpc.h"
  86. #include "kbdwin.h"
  87. #include "sinewin.h"
  88. #include "aac.h"
  89. #include "aactab.h"
  90. #include "aacdectab.h"
  91. #include "cbrt_tablegen.h"
  92. #include "sbr.h"
  93. #include "aacsbr.h"
  94. #include "mpeg4audio.h"
  95. #include "aacadtsdec.h"
  96. #include "libavutil/intfloat.h"
  97. #include <assert.h>
  98. #include <errno.h>
  99. #include <math.h>
  100. #include <string.h>
  101. #if ARCH_ARM
  102. # include "arm/aac.h"
  103. #endif
  104. static VLC vlc_scalefactors;
  105. static VLC vlc_spectral[11];
  106. #define overread_err "Input buffer exhausted before END element found\n"
  107. static int count_channels(uint8_t (*layout)[3], int tags)
  108. {
  109. int i, sum = 0;
  110. for (i = 0; i < tags; i++) {
  111. int syn_ele = layout[i][0];
  112. int pos = layout[i][2];
  113. sum += (1 + (syn_ele == TYPE_CPE)) *
  114. (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
  115. }
  116. return sum;
  117. }
  118. /**
  119. * Check for the channel element in the current channel position configuration.
  120. * If it exists, make sure the appropriate element is allocated and map the
  121. * channel order to match the internal FFmpeg channel layout.
  122. *
  123. * @param che_pos current channel position configuration
  124. * @param type channel element type
  125. * @param id channel element id
  126. * @param channels count of the number of channels in the configuration
  127. *
  128. * @return Returns error status. 0 - OK, !0 - error
  129. */
  130. static av_cold int che_configure(AACContext *ac,
  131. enum ChannelPosition che_pos,
  132. int type, int id, int *channels)
  133. {
  134. if (che_pos) {
  135. if (!ac->che[type][id]) {
  136. if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
  137. return AVERROR(ENOMEM);
  138. ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr);
  139. }
  140. if (type != TYPE_CCE) {
  141. if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
  142. av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
  143. return AVERROR_INVALIDDATA;
  144. }
  145. ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
  146. if (type == TYPE_CPE ||
  147. (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
  148. ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
  149. }
  150. }
  151. } else {
  152. if (ac->che[type][id])
  153. ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
  154. av_freep(&ac->che[type][id]);
  155. }
  156. return 0;
  157. }
  158. struct elem_to_channel {
  159. uint64_t av_position;
  160. uint8_t syn_ele;
  161. uint8_t elem_id;
  162. uint8_t aac_position;
  163. };
  164. static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
  165. uint8_t (*layout_map)[3], int offset, int tags, uint64_t left,
  166. uint64_t right, int pos)
  167. {
  168. if (layout_map[offset][0] == TYPE_CPE) {
  169. e2c_vec[offset] = (struct elem_to_channel) {
  170. .av_position = left | right, .syn_ele = TYPE_CPE,
  171. .elem_id = layout_map[offset ][1], .aac_position = pos };
  172. return 1;
  173. } else {
  174. e2c_vec[offset] = (struct elem_to_channel) {
  175. .av_position = left, .syn_ele = TYPE_SCE,
  176. .elem_id = layout_map[offset ][1], .aac_position = pos };
  177. e2c_vec[offset + 1] = (struct elem_to_channel) {
  178. .av_position = right, .syn_ele = TYPE_SCE,
  179. .elem_id = layout_map[offset + 1][1], .aac_position = pos };
  180. return 2;
  181. }
  182. }
  183. static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos, int *current) {
  184. int num_pos_channels = 0;
  185. int first_cpe = 0;
  186. int sce_parity = 0;
  187. int i;
  188. for (i = *current; i < tags; i++) {
  189. if (layout_map[i][2] != pos)
  190. break;
  191. if (layout_map[i][0] == TYPE_CPE) {
  192. if (sce_parity) {
  193. if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
  194. sce_parity = 0;
  195. } else {
  196. return -1;
  197. }
  198. }
  199. num_pos_channels += 2;
  200. first_cpe = 1;
  201. } else {
  202. num_pos_channels++;
  203. sce_parity ^= 1;
  204. }
  205. }
  206. if (sce_parity &&
  207. ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
  208. return -1;
  209. *current = i;
  210. return num_pos_channels;
  211. }
  212. static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
  213. {
  214. int i, n, total_non_cc_elements;
  215. struct elem_to_channel e2c_vec[4*MAX_ELEM_ID] = {{ 0 }};
  216. int num_front_channels, num_side_channels, num_back_channels;
  217. uint64_t layout;
  218. if (FF_ARRAY_ELEMS(e2c_vec) < tags)
  219. return 0;
  220. i = 0;
  221. num_front_channels =
  222. count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
  223. if (num_front_channels < 0)
  224. return 0;
  225. num_side_channels =
  226. count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
  227. if (num_side_channels < 0)
  228. return 0;
  229. num_back_channels =
  230. count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
  231. if (num_back_channels < 0)
  232. return 0;
  233. i = 0;
  234. if (num_front_channels & 1) {
  235. e2c_vec[i] = (struct elem_to_channel) {
  236. .av_position = AV_CH_FRONT_CENTER, .syn_ele = TYPE_SCE,
  237. .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_FRONT };
  238. i++;
  239. num_front_channels--;
  240. }
  241. if (num_front_channels >= 4) {
  242. i += assign_pair(e2c_vec, layout_map, i, tags,
  243. AV_CH_FRONT_LEFT_OF_CENTER,
  244. AV_CH_FRONT_RIGHT_OF_CENTER,
  245. AAC_CHANNEL_FRONT);
  246. num_front_channels -= 2;
  247. }
  248. if (num_front_channels >= 2) {
  249. i += assign_pair(e2c_vec, layout_map, i, tags,
  250. AV_CH_FRONT_LEFT,
  251. AV_CH_FRONT_RIGHT,
  252. AAC_CHANNEL_FRONT);
  253. num_front_channels -= 2;
  254. }
  255. while (num_front_channels >= 2) {
  256. i += assign_pair(e2c_vec, layout_map, i, tags,
  257. UINT64_MAX,
  258. UINT64_MAX,
  259. AAC_CHANNEL_FRONT);
  260. num_front_channels -= 2;
  261. }
  262. if (num_side_channels >= 2) {
  263. i += assign_pair(e2c_vec, layout_map, i, tags,
  264. AV_CH_SIDE_LEFT,
  265. AV_CH_SIDE_RIGHT,
  266. AAC_CHANNEL_FRONT);
  267. num_side_channels -= 2;
  268. }
  269. while (num_side_channels >= 2) {
  270. i += assign_pair(e2c_vec, layout_map, i, tags,
  271. UINT64_MAX,
  272. UINT64_MAX,
  273. AAC_CHANNEL_SIDE);
  274. num_side_channels -= 2;
  275. }
  276. while (num_back_channels >= 4) {
  277. i += assign_pair(e2c_vec, layout_map, i, tags,
  278. UINT64_MAX,
  279. UINT64_MAX,
  280. AAC_CHANNEL_BACK);
  281. num_back_channels -= 2;
  282. }
  283. if (num_back_channels >= 2) {
  284. i += assign_pair(e2c_vec, layout_map, i, tags,
  285. AV_CH_BACK_LEFT,
  286. AV_CH_BACK_RIGHT,
  287. AAC_CHANNEL_BACK);
  288. num_back_channels -= 2;
  289. }
  290. if (num_back_channels) {
  291. e2c_vec[i] = (struct elem_to_channel) {
  292. .av_position = AV_CH_BACK_CENTER, .syn_ele = TYPE_SCE,
  293. .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_BACK };
  294. i++;
  295. num_back_channels--;
  296. }
  297. if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
  298. e2c_vec[i] = (struct elem_to_channel) {
  299. .av_position = AV_CH_LOW_FREQUENCY, .syn_ele = TYPE_LFE,
  300. .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_LFE };
  301. i++;
  302. }
  303. while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
  304. e2c_vec[i] = (struct elem_to_channel) {
  305. .av_position = UINT64_MAX, .syn_ele = TYPE_LFE,
  306. .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_LFE };
  307. i++;
  308. }
  309. // Must choose a stable sort
  310. total_non_cc_elements = n = i;
  311. do {
  312. int next_n = 0;
  313. for (i = 1; i < n; i++) {
  314. if (e2c_vec[i-1].av_position > e2c_vec[i].av_position) {
  315. FFSWAP(struct elem_to_channel, e2c_vec[i-1], e2c_vec[i]);
  316. next_n = i;
  317. }
  318. }
  319. n = next_n;
  320. } while (n > 0);
  321. layout = 0;
  322. for (i = 0; i < total_non_cc_elements; i++) {
  323. layout_map[i][0] = e2c_vec[i].syn_ele;
  324. layout_map[i][1] = e2c_vec[i].elem_id;
  325. layout_map[i][2] = e2c_vec[i].aac_position;
  326. if (e2c_vec[i].av_position != UINT64_MAX) {
  327. layout |= e2c_vec[i].av_position;
  328. }
  329. }
  330. return layout;
  331. }
  332. /**
  333. * Save current output configuration if and only if it has been locked.
  334. */
  335. static void push_output_configuration(AACContext *ac) {
  336. if (ac->oc[1].status == OC_LOCKED) {
  337. ac->oc[0] = ac->oc[1];
  338. }
  339. ac->oc[1].status = OC_NONE;
  340. }
  341. /**
  342. * Restore the previous output configuration if and only if the current
  343. * configuration is unlocked.
  344. */
  345. static void pop_output_configuration(AACContext *ac) {
  346. if (ac->oc[1].status != OC_LOCKED) {
  347. if (ac->oc[0].status == OC_LOCKED) {
  348. ac->oc[1] = ac->oc[0];
  349. ac->avctx->channels = ac->oc[1].channels;
  350. ac->avctx->channel_layout = ac->oc[1].channel_layout;
  351. }
  352. }
  353. }
  354. /**
  355. * Configure output channel order based on the current program configuration element.
  356. *
  357. * @return Returns error status. 0 - OK, !0 - error
  358. */
  359. static int output_configure(AACContext *ac,
  360. uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
  361. int channel_config, enum OCStatus oc_type)
  362. {
  363. AVCodecContext *avctx = ac->avctx;
  364. int i, channels = 0, ret;
  365. uint64_t layout = 0;
  366. if (ac->oc[1].layout_map != layout_map) {
  367. memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
  368. ac->oc[1].layout_map_tags = tags;
  369. }
  370. // Try to sniff a reasonable channel order, otherwise output the
  371. // channels in the order the PCE declared them.
  372. if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
  373. layout = sniff_channel_order(layout_map, tags);
  374. for (i = 0; i < tags; i++) {
  375. int type = layout_map[i][0];
  376. int id = layout_map[i][1];
  377. int position = layout_map[i][2];
  378. // Allocate or free elements depending on if they are in the
  379. // current program configuration.
  380. ret = che_configure(ac, position, type, id, &channels);
  381. if (ret < 0)
  382. return ret;
  383. }
  384. if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
  385. if (layout == AV_CH_FRONT_CENTER) {
  386. layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
  387. } else {
  388. layout = 0;
  389. }
  390. }
  391. memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  392. if (layout) avctx->channel_layout = layout;
  393. ac->oc[1].channel_layout = layout;
  394. avctx->channels = ac->oc[1].channels = channels;
  395. ac->oc[1].status = oc_type;
  396. return 0;
  397. }
  398. static void flush(AVCodecContext *avctx)
  399. {
  400. AACContext *ac= avctx->priv_data;
  401. int type, i, j;
  402. for (type = 3; type >= 0; type--) {
  403. for (i = 0; i < MAX_ELEM_ID; i++) {
  404. ChannelElement *che = ac->che[type][i];
  405. if (che) {
  406. for (j = 0; j <= 1; j++) {
  407. memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
  408. }
  409. }
  410. }
  411. }
  412. }
  413. /**
  414. * Set up channel positions based on a default channel configuration
  415. * as specified in table 1.17.
  416. *
  417. * @return Returns error status. 0 - OK, !0 - error
  418. */
  419. static int set_default_channel_config(AVCodecContext *avctx,
  420. uint8_t (*layout_map)[3],
  421. int *tags,
  422. int channel_config)
  423. {
  424. if (channel_config < 1 || channel_config > 7) {
  425. av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
  426. channel_config);
  427. return -1;
  428. }
  429. *tags = tags_per_config[channel_config];
  430. memcpy(layout_map, aac_channel_layout_map[channel_config-1], *tags * sizeof(*layout_map));
  431. return 0;
  432. }
  433. static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
  434. {
  435. // For PCE based channel configurations map the channels solely based on tags.
  436. if (!ac->oc[1].m4ac.chan_config) {
  437. return ac->tag_che_map[type][elem_id];
  438. }
  439. // Allow single CPE stereo files to be signalled with mono configuration.
  440. if (!ac->tags_mapped && type == TYPE_CPE && ac->oc[1].m4ac.chan_config == 1) {
  441. uint8_t layout_map[MAX_ELEM_ID*4][3];
  442. int layout_map_tags;
  443. push_output_configuration(ac);
  444. av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
  445. if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
  446. 2) < 0)
  447. return NULL;
  448. if (output_configure(ac, layout_map, layout_map_tags,
  449. 2, OC_TRIAL_FRAME) < 0)
  450. return NULL;
  451. ac->oc[1].m4ac.chan_config = 2;
  452. }
  453. // And vice-versa
  454. if (!ac->tags_mapped && type == TYPE_SCE && ac->oc[1].m4ac.chan_config == 2) {
  455. uint8_t layout_map[MAX_ELEM_ID*4][3];
  456. int layout_map_tags;
  457. push_output_configuration(ac);
  458. av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
  459. if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
  460. 1) < 0)
  461. return NULL;
  462. if (output_configure(ac, layout_map, layout_map_tags,
  463. 1, OC_TRIAL_FRAME) < 0)
  464. return NULL;
  465. ac->oc[1].m4ac.chan_config = 1;
  466. }
  467. // For indexed channel configurations map the channels solely based on position.
  468. switch (ac->oc[1].m4ac.chan_config) {
  469. case 7:
  470. if (ac->tags_mapped == 3 && type == TYPE_CPE) {
  471. ac->tags_mapped++;
  472. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
  473. }
  474. case 6:
  475. /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
  476. instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have
  477. encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */
  478. if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
  479. ac->tags_mapped++;
  480. return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
  481. }
  482. case 5:
  483. if (ac->tags_mapped == 2 && type == TYPE_CPE) {
  484. ac->tags_mapped++;
  485. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
  486. }
  487. case 4:
  488. if (ac->tags_mapped == 2 && ac->oc[1].m4ac.chan_config == 4 && type == TYPE_SCE) {
  489. ac->tags_mapped++;
  490. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
  491. }
  492. case 3:
  493. case 2:
  494. if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) && type == TYPE_CPE) {
  495. ac->tags_mapped++;
  496. return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
  497. } else if (ac->oc[1].m4ac.chan_config == 2) {
  498. return NULL;
  499. }
  500. case 1:
  501. if (!ac->tags_mapped && type == TYPE_SCE) {
  502. ac->tags_mapped++;
  503. return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
  504. }
  505. default:
  506. return NULL;
  507. }
  508. }
  509. /**
  510. * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
  511. *
  512. * @param type speaker type/position for these channels
  513. */
  514. static void decode_channel_map(uint8_t layout_map[][3],
  515. enum ChannelPosition type,
  516. GetBitContext *gb, int n)
  517. {
  518. while (n--) {
  519. enum RawDataBlockType syn_ele;
  520. switch (type) {
  521. case AAC_CHANNEL_FRONT:
  522. case AAC_CHANNEL_BACK:
  523. case AAC_CHANNEL_SIDE:
  524. syn_ele = get_bits1(gb);
  525. break;
  526. case AAC_CHANNEL_CC:
  527. skip_bits1(gb);
  528. syn_ele = TYPE_CCE;
  529. break;
  530. case AAC_CHANNEL_LFE:
  531. syn_ele = TYPE_LFE;
  532. break;
  533. }
  534. layout_map[0][0] = syn_ele;
  535. layout_map[0][1] = get_bits(gb, 4);
  536. layout_map[0][2] = type;
  537. layout_map++;
  538. }
  539. }
  540. /**
  541. * Decode program configuration element; reference: table 4.2.
  542. *
  543. * @return Returns error status. 0 - OK, !0 - error
  544. */
  545. static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
  546. uint8_t (*layout_map)[3],
  547. GetBitContext *gb)
  548. {
  549. int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
  550. int comment_len;
  551. int tags;
  552. skip_bits(gb, 2); // object_type
  553. sampling_index = get_bits(gb, 4);
  554. if (m4ac->sampling_index != sampling_index)
  555. 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");
  556. num_front = get_bits(gb, 4);
  557. num_side = get_bits(gb, 4);
  558. num_back = get_bits(gb, 4);
  559. num_lfe = get_bits(gb, 2);
  560. num_assoc_data = get_bits(gb, 3);
  561. num_cc = get_bits(gb, 4);
  562. if (get_bits1(gb))
  563. skip_bits(gb, 4); // mono_mixdown_tag
  564. if (get_bits1(gb))
  565. skip_bits(gb, 4); // stereo_mixdown_tag
  566. if (get_bits1(gb))
  567. skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
  568. if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
  569. av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
  570. return -1;
  571. }
  572. decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
  573. tags = num_front;
  574. decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
  575. tags += num_side;
  576. decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
  577. tags += num_back;
  578. decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
  579. tags += num_lfe;
  580. skip_bits_long(gb, 4 * num_assoc_data);
  581. decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
  582. tags += num_cc;
  583. align_get_bits(gb);
  584. /* comment field, first byte is length */
  585. comment_len = get_bits(gb, 8) * 8;
  586. if (get_bits_left(gb) < comment_len) {
  587. av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
  588. return -1;
  589. }
  590. skip_bits_long(gb, comment_len);
  591. return tags;
  592. }
  593. /**
  594. * Decode GA "General Audio" specific configuration; reference: table 4.1.
  595. *
  596. * @param ac pointer to AACContext, may be null
  597. * @param avctx pointer to AVCCodecContext, used for logging
  598. *
  599. * @return Returns error status. 0 - OK, !0 - error
  600. */
  601. static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
  602. GetBitContext *gb,
  603. MPEG4AudioConfig *m4ac,
  604. int channel_config)
  605. {
  606. int extension_flag, ret;
  607. uint8_t layout_map[MAX_ELEM_ID*4][3];
  608. int tags = 0;
  609. if (get_bits1(gb)) { // frameLengthFlag
  610. av_log_missing_feature(avctx, "960/120 MDCT window is", 1);
  611. return -1;
  612. }
  613. if (get_bits1(gb)) // dependsOnCoreCoder
  614. skip_bits(gb, 14); // coreCoderDelay
  615. extension_flag = get_bits1(gb);
  616. if (m4ac->object_type == AOT_AAC_SCALABLE ||
  617. m4ac->object_type == AOT_ER_AAC_SCALABLE)
  618. skip_bits(gb, 3); // layerNr
  619. if (channel_config == 0) {
  620. skip_bits(gb, 4); // element_instance_tag
  621. tags = decode_pce(avctx, m4ac, layout_map, gb);
  622. if (tags < 0)
  623. return tags;
  624. } else {
  625. if ((ret = set_default_channel_config(avctx, layout_map, &tags, channel_config)))
  626. return ret;
  627. }
  628. if (count_channels(layout_map, tags) > 1) {
  629. m4ac->ps = 0;
  630. } else if (m4ac->sbr == 1 && m4ac->ps == -1)
  631. m4ac->ps = 1;
  632. if (ac && (ret = output_configure(ac, layout_map, tags,
  633. channel_config, OC_GLOBAL_HDR)))
  634. return ret;
  635. if (extension_flag) {
  636. switch (m4ac->object_type) {
  637. case AOT_ER_BSAC:
  638. skip_bits(gb, 5); // numOfSubFrame
  639. skip_bits(gb, 11); // layer_length
  640. break;
  641. case AOT_ER_AAC_LC:
  642. case AOT_ER_AAC_LTP:
  643. case AOT_ER_AAC_SCALABLE:
  644. case AOT_ER_AAC_LD:
  645. skip_bits(gb, 3); /* aacSectionDataResilienceFlag
  646. * aacScalefactorDataResilienceFlag
  647. * aacSpectralDataResilienceFlag
  648. */
  649. break;
  650. }
  651. skip_bits1(gb); // extensionFlag3 (TBD in version 3)
  652. }
  653. return 0;
  654. }
  655. /**
  656. * Decode audio specific configuration; reference: table 1.13.
  657. *
  658. * @param ac pointer to AACContext, may be null
  659. * @param avctx pointer to AVCCodecContext, used for logging
  660. * @param m4ac pointer to MPEG4AudioConfig, used for parsing
  661. * @param data pointer to buffer holding an audio specific config
  662. * @param bit_size size of audio specific config or data in bits
  663. * @param sync_extension look for an appended sync extension
  664. *
  665. * @return Returns error status or number of consumed bits. <0 - error
  666. */
  667. static int decode_audio_specific_config(AACContext *ac,
  668. AVCodecContext *avctx,
  669. MPEG4AudioConfig *m4ac,
  670. const uint8_t *data, int bit_size,
  671. int sync_extension)
  672. {
  673. GetBitContext gb;
  674. int i;
  675. av_dlog(avctx, "audio specific config size %d\n", bit_size >> 3);
  676. for (i = 0; i < bit_size >> 3; i++)
  677. av_dlog(avctx, "%02x ", data[i]);
  678. av_dlog(avctx, "\n");
  679. init_get_bits(&gb, data, bit_size);
  680. if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, sync_extension)) < 0)
  681. return -1;
  682. if (m4ac->sampling_index > 12) {
  683. av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
  684. return -1;
  685. }
  686. skip_bits_long(&gb, i);
  687. switch (m4ac->object_type) {
  688. case AOT_AAC_MAIN:
  689. case AOT_AAC_LC:
  690. case AOT_AAC_LTP:
  691. if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
  692. return -1;
  693. break;
  694. default:
  695. av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
  696. m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
  697. return -1;
  698. }
  699. av_dlog(avctx, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
  700. m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
  701. m4ac->sample_rate, m4ac->sbr, m4ac->ps);
  702. return get_bits_count(&gb);
  703. }
  704. /**
  705. * linear congruential pseudorandom number generator
  706. *
  707. * @param previous_val pointer to the current state of the generator
  708. *
  709. * @return Returns a 32-bit pseudorandom integer
  710. */
  711. static av_always_inline int lcg_random(int previous_val)
  712. {
  713. return previous_val * 1664525 + 1013904223;
  714. }
  715. static av_always_inline void reset_predict_state(PredictorState *ps)
  716. {
  717. ps->r0 = 0.0f;
  718. ps->r1 = 0.0f;
  719. ps->cor0 = 0.0f;
  720. ps->cor1 = 0.0f;
  721. ps->var0 = 1.0f;
  722. ps->var1 = 1.0f;
  723. }
  724. static void reset_all_predictors(PredictorState *ps)
  725. {
  726. int i;
  727. for (i = 0; i < MAX_PREDICTORS; i++)
  728. reset_predict_state(&ps[i]);
  729. }
  730. static int sample_rate_idx (int rate)
  731. {
  732. if (92017 <= rate) return 0;
  733. else if (75132 <= rate) return 1;
  734. else if (55426 <= rate) return 2;
  735. else if (46009 <= rate) return 3;
  736. else if (37566 <= rate) return 4;
  737. else if (27713 <= rate) return 5;
  738. else if (23004 <= rate) return 6;
  739. else if (18783 <= rate) return 7;
  740. else if (13856 <= rate) return 8;
  741. else if (11502 <= rate) return 9;
  742. else if (9391 <= rate) return 10;
  743. else return 11;
  744. }
  745. static void reset_predictor_group(PredictorState *ps, int group_num)
  746. {
  747. int i;
  748. for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
  749. reset_predict_state(&ps[i]);
  750. }
  751. #define AAC_INIT_VLC_STATIC(num, size) \
  752. INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
  753. ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
  754. ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
  755. size);
  756. static av_cold int aac_decode_init(AVCodecContext *avctx)
  757. {
  758. AACContext *ac = avctx->priv_data;
  759. float output_scale_factor;
  760. ac->avctx = avctx;
  761. ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
  762. if (avctx->extradata_size > 0) {
  763. if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
  764. avctx->extradata,
  765. avctx->extradata_size*8, 1) < 0)
  766. return -1;
  767. } else {
  768. int sr, i;
  769. uint8_t layout_map[MAX_ELEM_ID*4][3];
  770. int layout_map_tags;
  771. sr = sample_rate_idx(avctx->sample_rate);
  772. ac->oc[1].m4ac.sampling_index = sr;
  773. ac->oc[1].m4ac.channels = avctx->channels;
  774. ac->oc[1].m4ac.sbr = -1;
  775. ac->oc[1].m4ac.ps = -1;
  776. for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
  777. if (ff_mpeg4audio_channels[i] == avctx->channels)
  778. break;
  779. if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
  780. i = 0;
  781. }
  782. ac->oc[1].m4ac.chan_config = i;
  783. if (ac->oc[1].m4ac.chan_config) {
  784. int ret = set_default_channel_config(avctx, layout_map,
  785. &layout_map_tags, ac->oc[1].m4ac.chan_config);
  786. if (!ret)
  787. output_configure(ac, layout_map, layout_map_tags,
  788. ac->oc[1].m4ac.chan_config, OC_GLOBAL_HDR);
  789. else if (avctx->err_recognition & AV_EF_EXPLODE)
  790. return AVERROR_INVALIDDATA;
  791. }
  792. }
  793. if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
  794. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  795. output_scale_factor = 1.0 / 32768.0;
  796. } else {
  797. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  798. output_scale_factor = 1.0;
  799. }
  800. AAC_INIT_VLC_STATIC( 0, 304);
  801. AAC_INIT_VLC_STATIC( 1, 270);
  802. AAC_INIT_VLC_STATIC( 2, 550);
  803. AAC_INIT_VLC_STATIC( 3, 300);
  804. AAC_INIT_VLC_STATIC( 4, 328);
  805. AAC_INIT_VLC_STATIC( 5, 294);
  806. AAC_INIT_VLC_STATIC( 6, 306);
  807. AAC_INIT_VLC_STATIC( 7, 268);
  808. AAC_INIT_VLC_STATIC( 8, 510);
  809. AAC_INIT_VLC_STATIC( 9, 366);
  810. AAC_INIT_VLC_STATIC(10, 462);
  811. ff_aac_sbr_init();
  812. ff_dsputil_init(&ac->dsp, avctx);
  813. ff_fmt_convert_init(&ac->fmt_conv, avctx);
  814. ac->random_state = 0x1f2e3d4c;
  815. ff_aac_tableinit();
  816. INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
  817. ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
  818. ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
  819. 352);
  820. ff_mdct_init(&ac->mdct, 11, 1, output_scale_factor/1024.0);
  821. ff_mdct_init(&ac->mdct_small, 8, 1, output_scale_factor/128.0);
  822. ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0/output_scale_factor);
  823. // window initialization
  824. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  825. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  826. ff_init_ff_sine_windows(10);
  827. ff_init_ff_sine_windows( 7);
  828. cbrt_tableinit();
  829. avcodec_get_frame_defaults(&ac->frame);
  830. avctx->coded_frame = &ac->frame;
  831. return 0;
  832. }
  833. /**
  834. * Skip data_stream_element; reference: table 4.10.
  835. */
  836. static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
  837. {
  838. int byte_align = get_bits1(gb);
  839. int count = get_bits(gb, 8);
  840. if (count == 255)
  841. count += get_bits(gb, 8);
  842. if (byte_align)
  843. align_get_bits(gb);
  844. if (get_bits_left(gb) < 8 * count) {
  845. av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
  846. return -1;
  847. }
  848. skip_bits_long(gb, 8 * count);
  849. return 0;
  850. }
  851. static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
  852. GetBitContext *gb)
  853. {
  854. int sfb;
  855. if (get_bits1(gb)) {
  856. ics->predictor_reset_group = get_bits(gb, 5);
  857. if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
  858. av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
  859. return -1;
  860. }
  861. }
  862. for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
  863. ics->prediction_used[sfb] = get_bits1(gb);
  864. }
  865. return 0;
  866. }
  867. /**
  868. * Decode Long Term Prediction data; reference: table 4.xx.
  869. */
  870. static void decode_ltp(AACContext *ac, LongTermPrediction *ltp,
  871. GetBitContext *gb, uint8_t max_sfb)
  872. {
  873. int sfb;
  874. ltp->lag = get_bits(gb, 11);
  875. ltp->coef = ltp_coef[get_bits(gb, 3)];
  876. for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
  877. ltp->used[sfb] = get_bits1(gb);
  878. }
  879. /**
  880. * Decode Individual Channel Stream info; reference: table 4.6.
  881. */
  882. static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
  883. GetBitContext *gb)
  884. {
  885. if (get_bits1(gb)) {
  886. av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
  887. return AVERROR_INVALIDDATA;
  888. }
  889. ics->window_sequence[1] = ics->window_sequence[0];
  890. ics->window_sequence[0] = get_bits(gb, 2);
  891. ics->use_kb_window[1] = ics->use_kb_window[0];
  892. ics->use_kb_window[0] = get_bits1(gb);
  893. ics->num_window_groups = 1;
  894. ics->group_len[0] = 1;
  895. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  896. int i;
  897. ics->max_sfb = get_bits(gb, 4);
  898. for (i = 0; i < 7; i++) {
  899. if (get_bits1(gb)) {
  900. ics->group_len[ics->num_window_groups - 1]++;
  901. } else {
  902. ics->num_window_groups++;
  903. ics->group_len[ics->num_window_groups - 1] = 1;
  904. }
  905. }
  906. ics->num_windows = 8;
  907. ics->swb_offset = ff_swb_offset_128[ac->oc[1].m4ac.sampling_index];
  908. ics->num_swb = ff_aac_num_swb_128[ac->oc[1].m4ac.sampling_index];
  909. ics->tns_max_bands = ff_tns_max_bands_128[ac->oc[1].m4ac.sampling_index];
  910. ics->predictor_present = 0;
  911. } else {
  912. ics->max_sfb = get_bits(gb, 6);
  913. ics->num_windows = 1;
  914. ics->swb_offset = ff_swb_offset_1024[ac->oc[1].m4ac.sampling_index];
  915. ics->num_swb = ff_aac_num_swb_1024[ac->oc[1].m4ac.sampling_index];
  916. ics->tns_max_bands = ff_tns_max_bands_1024[ac->oc[1].m4ac.sampling_index];
  917. ics->predictor_present = get_bits1(gb);
  918. ics->predictor_reset_group = 0;
  919. if (ics->predictor_present) {
  920. if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
  921. if (decode_prediction(ac, ics, gb)) {
  922. goto fail;
  923. }
  924. } else if (ac->oc[1].m4ac.object_type == AOT_AAC_LC) {
  925. av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
  926. goto fail;
  927. } else {
  928. if ((ics->ltp.present = get_bits(gb, 1)))
  929. decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
  930. }
  931. }
  932. }
  933. if (ics->max_sfb > ics->num_swb) {
  934. av_log(ac->avctx, AV_LOG_ERROR,
  935. "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
  936. ics->max_sfb, ics->num_swb);
  937. goto fail;
  938. }
  939. return 0;
  940. fail:
  941. ics->max_sfb = 0;
  942. return AVERROR_INVALIDDATA;
  943. }
  944. /**
  945. * Decode band types (section_data payload); reference: table 4.46.
  946. *
  947. * @param band_type array of the used band type
  948. * @param band_type_run_end array of the last scalefactor band of a band type run
  949. *
  950. * @return Returns error status. 0 - OK, !0 - error
  951. */
  952. static int decode_band_types(AACContext *ac, enum BandType band_type[120],
  953. int band_type_run_end[120], GetBitContext *gb,
  954. IndividualChannelStream *ics)
  955. {
  956. int g, idx = 0;
  957. const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
  958. for (g = 0; g < ics->num_window_groups; g++) {
  959. int k = 0;
  960. while (k < ics->max_sfb) {
  961. uint8_t sect_end = k;
  962. int sect_len_incr;
  963. int sect_band_type = get_bits(gb, 4);
  964. if (sect_band_type == 12) {
  965. av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
  966. return -1;
  967. }
  968. do {
  969. sect_len_incr = get_bits(gb, bits);
  970. sect_end += sect_len_incr;
  971. if (get_bits_left(gb) < 0) {
  972. av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
  973. return -1;
  974. }
  975. if (sect_end > ics->max_sfb) {
  976. av_log(ac->avctx, AV_LOG_ERROR,
  977. "Number of bands (%d) exceeds limit (%d).\n",
  978. sect_end, ics->max_sfb);
  979. return -1;
  980. }
  981. } while (sect_len_incr == (1 << bits) - 1);
  982. for (; k < sect_end; k++) {
  983. band_type [idx] = sect_band_type;
  984. band_type_run_end[idx++] = sect_end;
  985. }
  986. }
  987. }
  988. return 0;
  989. }
  990. /**
  991. * Decode scalefactors; reference: table 4.47.
  992. *
  993. * @param global_gain first scalefactor value as scalefactors are differentially coded
  994. * @param band_type array of the used band type
  995. * @param band_type_run_end array of the last scalefactor band of a band type run
  996. * @param sf array of scalefactors or intensity stereo positions
  997. *
  998. * @return Returns error status. 0 - OK, !0 - error
  999. */
  1000. static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
  1001. unsigned int global_gain,
  1002. IndividualChannelStream *ics,
  1003. enum BandType band_type[120],
  1004. int band_type_run_end[120])
  1005. {
  1006. int g, i, idx = 0;
  1007. int offset[3] = { global_gain, global_gain - 90, 0 };
  1008. int clipped_offset;
  1009. int noise_flag = 1;
  1010. for (g = 0; g < ics->num_window_groups; g++) {
  1011. for (i = 0; i < ics->max_sfb;) {
  1012. int run_end = band_type_run_end[idx];
  1013. if (band_type[idx] == ZERO_BT) {
  1014. for (; i < run_end; i++, idx++)
  1015. sf[idx] = 0.;
  1016. } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
  1017. for (; i < run_end; i++, idx++) {
  1018. offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1019. clipped_offset = av_clip(offset[2], -155, 100);
  1020. if (offset[2] != clipped_offset) {
  1021. av_log_ask_for_sample(ac->avctx, "Intensity stereo "
  1022. "position clipped (%d -> %d).\nIf you heard an "
  1023. "audible artifact, there may be a bug in the "
  1024. "decoder. ", offset[2], clipped_offset);
  1025. }
  1026. sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
  1027. }
  1028. } else if (band_type[idx] == NOISE_BT) {
  1029. for (; i < run_end; i++, idx++) {
  1030. if (noise_flag-- > 0)
  1031. offset[1] += get_bits(gb, 9) - 256;
  1032. else
  1033. offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1034. clipped_offset = av_clip(offset[1], -100, 155);
  1035. if (offset[1] != clipped_offset) {
  1036. av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
  1037. "(%d -> %d).\nIf you heard an audible "
  1038. "artifact, there may be a bug in the decoder. ",
  1039. offset[1], clipped_offset);
  1040. }
  1041. sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
  1042. }
  1043. } else {
  1044. for (; i < run_end; i++, idx++) {
  1045. offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1046. if (offset[0] > 255U) {
  1047. av_log(ac->avctx, AV_LOG_ERROR,
  1048. "Scalefactor (%d) out of range.\n", offset[0]);
  1049. return -1;
  1050. }
  1051. sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
  1052. }
  1053. }
  1054. }
  1055. }
  1056. return 0;
  1057. }
  1058. /**
  1059. * Decode pulse data; reference: table 4.7.
  1060. */
  1061. static int decode_pulses(Pulse *pulse, GetBitContext *gb,
  1062. const uint16_t *swb_offset, int num_swb)
  1063. {
  1064. int i, pulse_swb;
  1065. pulse->num_pulse = get_bits(gb, 2) + 1;
  1066. pulse_swb = get_bits(gb, 6);
  1067. if (pulse_swb >= num_swb)
  1068. return -1;
  1069. pulse->pos[0] = swb_offset[pulse_swb];
  1070. pulse->pos[0] += get_bits(gb, 5);
  1071. if (pulse->pos[0] > 1023)
  1072. return -1;
  1073. pulse->amp[0] = get_bits(gb, 4);
  1074. for (i = 1; i < pulse->num_pulse; i++) {
  1075. pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
  1076. if (pulse->pos[i] > 1023)
  1077. return -1;
  1078. pulse->amp[i] = get_bits(gb, 4);
  1079. }
  1080. return 0;
  1081. }
  1082. /**
  1083. * Decode Temporal Noise Shaping data; reference: table 4.48.
  1084. *
  1085. * @return Returns error status. 0 - OK, !0 - error
  1086. */
  1087. static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
  1088. GetBitContext *gb, const IndividualChannelStream *ics)
  1089. {
  1090. int w, filt, i, coef_len, coef_res, coef_compress;
  1091. const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  1092. const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
  1093. for (w = 0; w < ics->num_windows; w++) {
  1094. if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
  1095. coef_res = get_bits1(gb);
  1096. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  1097. int tmp2_idx;
  1098. tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
  1099. if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
  1100. av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
  1101. tns->order[w][filt], tns_max_order);
  1102. tns->order[w][filt] = 0;
  1103. return -1;
  1104. }
  1105. if (tns->order[w][filt]) {
  1106. tns->direction[w][filt] = get_bits1(gb);
  1107. coef_compress = get_bits1(gb);
  1108. coef_len = coef_res + 3 - coef_compress;
  1109. tmp2_idx = 2 * coef_compress + coef_res;
  1110. for (i = 0; i < tns->order[w][filt]; i++)
  1111. tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
  1112. }
  1113. }
  1114. }
  1115. }
  1116. return 0;
  1117. }
  1118. /**
  1119. * Decode Mid/Side data; reference: table 4.54.
  1120. *
  1121. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  1122. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  1123. * [3] reserved for scalable AAC
  1124. */
  1125. static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
  1126. int ms_present)
  1127. {
  1128. int idx;
  1129. if (ms_present == 1) {
  1130. for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
  1131. cpe->ms_mask[idx] = get_bits1(gb);
  1132. } else if (ms_present == 2) {
  1133. memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
  1134. }
  1135. }
  1136. #ifndef VMUL2
  1137. static inline float *VMUL2(float *dst, const float *v, unsigned idx,
  1138. const float *scale)
  1139. {
  1140. float s = *scale;
  1141. *dst++ = v[idx & 15] * s;
  1142. *dst++ = v[idx>>4 & 15] * s;
  1143. return dst;
  1144. }
  1145. #endif
  1146. #ifndef VMUL4
  1147. static inline float *VMUL4(float *dst, const float *v, unsigned idx,
  1148. const float *scale)
  1149. {
  1150. float s = *scale;
  1151. *dst++ = v[idx & 3] * s;
  1152. *dst++ = v[idx>>2 & 3] * s;
  1153. *dst++ = v[idx>>4 & 3] * s;
  1154. *dst++ = v[idx>>6 & 3] * s;
  1155. return dst;
  1156. }
  1157. #endif
  1158. #ifndef VMUL2S
  1159. static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
  1160. unsigned sign, const float *scale)
  1161. {
  1162. union av_intfloat32 s0, s1;
  1163. s0.f = s1.f = *scale;
  1164. s0.i ^= sign >> 1 << 31;
  1165. s1.i ^= sign << 31;
  1166. *dst++ = v[idx & 15] * s0.f;
  1167. *dst++ = v[idx>>4 & 15] * s1.f;
  1168. return dst;
  1169. }
  1170. #endif
  1171. #ifndef VMUL4S
  1172. static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
  1173. unsigned sign, const float *scale)
  1174. {
  1175. unsigned nz = idx >> 12;
  1176. union av_intfloat32 s = { .f = *scale };
  1177. union av_intfloat32 t;
  1178. t.i = s.i ^ (sign & 1U<<31);
  1179. *dst++ = v[idx & 3] * t.f;
  1180. sign <<= nz & 1; nz >>= 1;
  1181. t.i = s.i ^ (sign & 1U<<31);
  1182. *dst++ = v[idx>>2 & 3] * t.f;
  1183. sign <<= nz & 1; nz >>= 1;
  1184. t.i = s.i ^ (sign & 1U<<31);
  1185. *dst++ = v[idx>>4 & 3] * t.f;
  1186. sign <<= nz & 1; nz >>= 1;
  1187. t.i = s.i ^ (sign & 1U<<31);
  1188. *dst++ = v[idx>>6 & 3] * t.f;
  1189. return dst;
  1190. }
  1191. #endif
  1192. /**
  1193. * Decode spectral data; reference: table 4.50.
  1194. * Dequantize and scale spectral data; reference: 4.6.3.3.
  1195. *
  1196. * @param coef array of dequantized, scaled spectral data
  1197. * @param sf array of scalefactors or intensity stereo positions
  1198. * @param pulse_present set if pulses are present
  1199. * @param pulse pointer to pulse data struct
  1200. * @param band_type array of the used band type
  1201. *
  1202. * @return Returns error status. 0 - OK, !0 - error
  1203. */
  1204. static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
  1205. GetBitContext *gb, const float sf[120],
  1206. int pulse_present, const Pulse *pulse,
  1207. const IndividualChannelStream *ics,
  1208. enum BandType band_type[120])
  1209. {
  1210. int i, k, g, idx = 0;
  1211. const int c = 1024 / ics->num_windows;
  1212. const uint16_t *offsets = ics->swb_offset;
  1213. float *coef_base = coef;
  1214. for (g = 0; g < ics->num_windows; g++)
  1215. memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
  1216. for (g = 0; g < ics->num_window_groups; g++) {
  1217. unsigned g_len = ics->group_len[g];
  1218. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1219. const unsigned cbt_m1 = band_type[idx] - 1;
  1220. float *cfo = coef + offsets[i];
  1221. int off_len = offsets[i + 1] - offsets[i];
  1222. int group;
  1223. if (cbt_m1 >= INTENSITY_BT2 - 1) {
  1224. for (group = 0; group < g_len; group++, cfo+=128) {
  1225. memset(cfo, 0, off_len * sizeof(float));
  1226. }
  1227. } else if (cbt_m1 == NOISE_BT - 1) {
  1228. for (group = 0; group < g_len; group++, cfo+=128) {
  1229. float scale;
  1230. float band_energy;
  1231. for (k = 0; k < off_len; k++) {
  1232. ac->random_state = lcg_random(ac->random_state);
  1233. cfo[k] = ac->random_state;
  1234. }
  1235. band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
  1236. scale = sf[idx] / sqrtf(band_energy);
  1237. ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
  1238. }
  1239. } else {
  1240. const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
  1241. const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
  1242. VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
  1243. OPEN_READER(re, gb);
  1244. switch (cbt_m1 >> 1) {
  1245. case 0:
  1246. for (group = 0; group < g_len; group++, cfo+=128) {
  1247. float *cf = cfo;
  1248. int len = off_len;
  1249. do {
  1250. int code;
  1251. unsigned cb_idx;
  1252. UPDATE_CACHE(re, gb);
  1253. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1254. cb_idx = cb_vector_idx[code];
  1255. cf = VMUL4(cf, vq, cb_idx, sf + idx);
  1256. } while (len -= 4);
  1257. }
  1258. break;
  1259. case 1:
  1260. for (group = 0; group < g_len; group++, cfo+=128) {
  1261. float *cf = cfo;
  1262. int len = off_len;
  1263. do {
  1264. int code;
  1265. unsigned nnz;
  1266. unsigned cb_idx;
  1267. uint32_t bits;
  1268. UPDATE_CACHE(re, gb);
  1269. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1270. cb_idx = cb_vector_idx[code];
  1271. nnz = cb_idx >> 8 & 15;
  1272. bits = nnz ? GET_CACHE(re, gb) : 0;
  1273. LAST_SKIP_BITS(re, gb, nnz);
  1274. cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
  1275. } while (len -= 4);
  1276. }
  1277. break;
  1278. case 2:
  1279. for (group = 0; group < g_len; group++, cfo+=128) {
  1280. float *cf = cfo;
  1281. int len = off_len;
  1282. do {
  1283. int code;
  1284. unsigned cb_idx;
  1285. UPDATE_CACHE(re, gb);
  1286. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1287. cb_idx = cb_vector_idx[code];
  1288. cf = VMUL2(cf, vq, cb_idx, sf + idx);
  1289. } while (len -= 2);
  1290. }
  1291. break;
  1292. case 3:
  1293. case 4:
  1294. for (group = 0; group < g_len; group++, cfo+=128) {
  1295. float *cf = cfo;
  1296. int len = off_len;
  1297. do {
  1298. int code;
  1299. unsigned nnz;
  1300. unsigned cb_idx;
  1301. unsigned sign;
  1302. UPDATE_CACHE(re, gb);
  1303. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1304. cb_idx = cb_vector_idx[code];
  1305. nnz = cb_idx >> 8 & 15;
  1306. sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
  1307. LAST_SKIP_BITS(re, gb, nnz);
  1308. cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
  1309. } while (len -= 2);
  1310. }
  1311. break;
  1312. default:
  1313. for (group = 0; group < g_len; group++, cfo+=128) {
  1314. float *cf = cfo;
  1315. uint32_t *icf = (uint32_t *) cf;
  1316. int len = off_len;
  1317. do {
  1318. int code;
  1319. unsigned nzt, nnz;
  1320. unsigned cb_idx;
  1321. uint32_t bits;
  1322. int j;
  1323. UPDATE_CACHE(re, gb);
  1324. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1325. if (!code) {
  1326. *icf++ = 0;
  1327. *icf++ = 0;
  1328. continue;
  1329. }
  1330. cb_idx = cb_vector_idx[code];
  1331. nnz = cb_idx >> 12;
  1332. nzt = cb_idx >> 8;
  1333. bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
  1334. LAST_SKIP_BITS(re, gb, nnz);
  1335. for (j = 0; j < 2; j++) {
  1336. if (nzt & 1<<j) {
  1337. uint32_t b;
  1338. int n;
  1339. /* The total length of escape_sequence must be < 22 bits according
  1340. to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
  1341. UPDATE_CACHE(re, gb);
  1342. b = GET_CACHE(re, gb);
  1343. b = 31 - av_log2(~b);
  1344. if (b > 8) {
  1345. av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
  1346. return -1;
  1347. }
  1348. SKIP_BITS(re, gb, b + 1);
  1349. b += 4;
  1350. n = (1 << b) + SHOW_UBITS(re, gb, b);
  1351. LAST_SKIP_BITS(re, gb, b);
  1352. *icf++ = cbrt_tab[n] | (bits & 1U<<31);
  1353. bits <<= 1;
  1354. } else {
  1355. unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
  1356. *icf++ = (bits & 1U<<31) | v;
  1357. bits <<= !!v;
  1358. }
  1359. cb_idx >>= 4;
  1360. }
  1361. } while (len -= 2);
  1362. ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
  1363. }
  1364. }
  1365. CLOSE_READER(re, gb);
  1366. }
  1367. }
  1368. coef += g_len << 7;
  1369. }
  1370. if (pulse_present) {
  1371. idx = 0;
  1372. for (i = 0; i < pulse->num_pulse; i++) {
  1373. float co = coef_base[ pulse->pos[i] ];
  1374. while (offsets[idx + 1] <= pulse->pos[i])
  1375. idx++;
  1376. if (band_type[idx] != NOISE_BT && sf[idx]) {
  1377. float ico = -pulse->amp[i];
  1378. if (co) {
  1379. co /= sf[idx];
  1380. ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
  1381. }
  1382. coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
  1383. }
  1384. }
  1385. }
  1386. return 0;
  1387. }
  1388. static av_always_inline float flt16_round(float pf)
  1389. {
  1390. union av_intfloat32 tmp;
  1391. tmp.f = pf;
  1392. tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
  1393. return tmp.f;
  1394. }
  1395. static av_always_inline float flt16_even(float pf)
  1396. {
  1397. union av_intfloat32 tmp;
  1398. tmp.f = pf;
  1399. tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
  1400. return tmp.f;
  1401. }
  1402. static av_always_inline float flt16_trunc(float pf)
  1403. {
  1404. union av_intfloat32 pun;
  1405. pun.f = pf;
  1406. pun.i &= 0xFFFF0000U;
  1407. return pun.f;
  1408. }
  1409. static av_always_inline void predict(PredictorState *ps, float *coef,
  1410. int output_enable)
  1411. {
  1412. const float a = 0.953125; // 61.0 / 64
  1413. const float alpha = 0.90625; // 29.0 / 32
  1414. float e0, e1;
  1415. float pv;
  1416. float k1, k2;
  1417. float r0 = ps->r0, r1 = ps->r1;
  1418. float cor0 = ps->cor0, cor1 = ps->cor1;
  1419. float var0 = ps->var0, var1 = ps->var1;
  1420. k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
  1421. k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
  1422. pv = flt16_round(k1 * r0 + k2 * r1);
  1423. if (output_enable)
  1424. *coef += pv;
  1425. e0 = *coef;
  1426. e1 = e0 - k1 * r0;
  1427. ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
  1428. ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
  1429. ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
  1430. ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
  1431. ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
  1432. ps->r0 = flt16_trunc(a * e0);
  1433. }
  1434. /**
  1435. * Apply AAC-Main style frequency domain prediction.
  1436. */
  1437. static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
  1438. {
  1439. int sfb, k;
  1440. if (!sce->ics.predictor_initialized) {
  1441. reset_all_predictors(sce->predictor_state);
  1442. sce->ics.predictor_initialized = 1;
  1443. }
  1444. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  1445. for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]; sfb++) {
  1446. for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
  1447. predict(&sce->predictor_state[k], &sce->coeffs[k],
  1448. sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
  1449. }
  1450. }
  1451. if (sce->ics.predictor_reset_group)
  1452. reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
  1453. } else
  1454. reset_all_predictors(sce->predictor_state);
  1455. }
  1456. /**
  1457. * Decode an individual_channel_stream payload; reference: table 4.44.
  1458. *
  1459. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  1460. * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
  1461. *
  1462. * @return Returns error status. 0 - OK, !0 - error
  1463. */
  1464. static int decode_ics(AACContext *ac, SingleChannelElement *sce,
  1465. GetBitContext *gb, int common_window, int scale_flag)
  1466. {
  1467. Pulse pulse;
  1468. TemporalNoiseShaping *tns = &sce->tns;
  1469. IndividualChannelStream *ics = &sce->ics;
  1470. float *out = sce->coeffs;
  1471. int global_gain, pulse_present = 0;
  1472. /* This assignment is to silence a GCC warning about the variable being used
  1473. * uninitialized when in fact it always is.
  1474. */
  1475. pulse.num_pulse = 0;
  1476. global_gain = get_bits(gb, 8);
  1477. if (!common_window && !scale_flag) {
  1478. if (decode_ics_info(ac, ics, gb) < 0)
  1479. return AVERROR_INVALIDDATA;
  1480. }
  1481. if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
  1482. return -1;
  1483. if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
  1484. return -1;
  1485. pulse_present = 0;
  1486. if (!scale_flag) {
  1487. if ((pulse_present = get_bits1(gb))) {
  1488. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1489. av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
  1490. return -1;
  1491. }
  1492. if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
  1493. av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
  1494. return -1;
  1495. }
  1496. }
  1497. if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
  1498. return -1;
  1499. if (get_bits1(gb)) {
  1500. av_log_missing_feature(ac->avctx, "SSR", 1);
  1501. return -1;
  1502. }
  1503. }
  1504. if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
  1505. return -1;
  1506. if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
  1507. apply_prediction(ac, sce);
  1508. return 0;
  1509. }
  1510. /**
  1511. * Mid/Side stereo decoding; reference: 4.6.8.1.3.
  1512. */
  1513. static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
  1514. {
  1515. const IndividualChannelStream *ics = &cpe->ch[0].ics;
  1516. float *ch0 = cpe->ch[0].coeffs;
  1517. float *ch1 = cpe->ch[1].coeffs;
  1518. int g, i, group, idx = 0;
  1519. const uint16_t *offsets = ics->swb_offset;
  1520. for (g = 0; g < ics->num_window_groups; g++) {
  1521. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1522. if (cpe->ms_mask[idx] &&
  1523. cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
  1524. for (group = 0; group < ics->group_len[g]; group++) {
  1525. ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
  1526. ch1 + group * 128 + offsets[i],
  1527. offsets[i+1] - offsets[i]);
  1528. }
  1529. }
  1530. }
  1531. ch0 += ics->group_len[g] * 128;
  1532. ch1 += ics->group_len[g] * 128;
  1533. }
  1534. }
  1535. /**
  1536. * intensity stereo decoding; reference: 4.6.8.2.3
  1537. *
  1538. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  1539. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  1540. * [3] reserved for scalable AAC
  1541. */
  1542. static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
  1543. {
  1544. const IndividualChannelStream *ics = &cpe->ch[1].ics;
  1545. SingleChannelElement *sce1 = &cpe->ch[1];
  1546. float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
  1547. const uint16_t *offsets = ics->swb_offset;
  1548. int g, group, i, idx = 0;
  1549. int c;
  1550. float scale;
  1551. for (g = 0; g < ics->num_window_groups; g++) {
  1552. for (i = 0; i < ics->max_sfb;) {
  1553. if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
  1554. const int bt_run_end = sce1->band_type_run_end[idx];
  1555. for (; i < bt_run_end; i++, idx++) {
  1556. c = -1 + 2 * (sce1->band_type[idx] - 14);
  1557. if (ms_present)
  1558. c *= 1 - 2 * cpe->ms_mask[idx];
  1559. scale = c * sce1->sf[idx];
  1560. for (group = 0; group < ics->group_len[g]; group++)
  1561. ac->dsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
  1562. coef0 + group * 128 + offsets[i],
  1563. scale,
  1564. offsets[i + 1] - offsets[i]);
  1565. }
  1566. } else {
  1567. int bt_run_end = sce1->band_type_run_end[idx];
  1568. idx += bt_run_end - i;
  1569. i = bt_run_end;
  1570. }
  1571. }
  1572. coef0 += ics->group_len[g] * 128;
  1573. coef1 += ics->group_len[g] * 128;
  1574. }
  1575. }
  1576. /**
  1577. * Decode a channel_pair_element; reference: table 4.4.
  1578. *
  1579. * @return Returns error status. 0 - OK, !0 - error
  1580. */
  1581. static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
  1582. {
  1583. int i, ret, common_window, ms_present = 0;
  1584. common_window = get_bits1(gb);
  1585. if (common_window) {
  1586. if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
  1587. return AVERROR_INVALIDDATA;
  1588. i = cpe->ch[1].ics.use_kb_window[0];
  1589. cpe->ch[1].ics = cpe->ch[0].ics;
  1590. cpe->ch[1].ics.use_kb_window[1] = i;
  1591. if (cpe->ch[1].ics.predictor_present && (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
  1592. if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
  1593. decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
  1594. ms_present = get_bits(gb, 2);
  1595. if (ms_present == 3) {
  1596. av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
  1597. return -1;
  1598. } else if (ms_present)
  1599. decode_mid_side_stereo(cpe, gb, ms_present);
  1600. }
  1601. if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
  1602. return ret;
  1603. if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
  1604. return ret;
  1605. if (common_window) {
  1606. if (ms_present)
  1607. apply_mid_side_stereo(ac, cpe);
  1608. if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
  1609. apply_prediction(ac, &cpe->ch[0]);
  1610. apply_prediction(ac, &cpe->ch[1]);
  1611. }
  1612. }
  1613. apply_intensity_stereo(ac, cpe, ms_present);
  1614. return 0;
  1615. }
  1616. static const float cce_scale[] = {
  1617. 1.09050773266525765921, //2^(1/8)
  1618. 1.18920711500272106672, //2^(1/4)
  1619. M_SQRT2,
  1620. 2,
  1621. };
  1622. /**
  1623. * Decode coupling_channel_element; reference: table 4.8.
  1624. *
  1625. * @return Returns error status. 0 - OK, !0 - error
  1626. */
  1627. static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
  1628. {
  1629. int num_gain = 0;
  1630. int c, g, sfb, ret;
  1631. int sign;
  1632. float scale;
  1633. SingleChannelElement *sce = &che->ch[0];
  1634. ChannelCoupling *coup = &che->coup;
  1635. coup->coupling_point = 2 * get_bits1(gb);
  1636. coup->num_coupled = get_bits(gb, 3);
  1637. for (c = 0; c <= coup->num_coupled; c++) {
  1638. num_gain++;
  1639. coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
  1640. coup->id_select[c] = get_bits(gb, 4);
  1641. if (coup->type[c] == TYPE_CPE) {
  1642. coup->ch_select[c] = get_bits(gb, 2);
  1643. if (coup->ch_select[c] == 3)
  1644. num_gain++;
  1645. } else
  1646. coup->ch_select[c] = 2;
  1647. }
  1648. coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
  1649. sign = get_bits(gb, 1);
  1650. scale = cce_scale[get_bits(gb, 2)];
  1651. if ((ret = decode_ics(ac, sce, gb, 0, 0)))
  1652. return ret;
  1653. for (c = 0; c < num_gain; c++) {
  1654. int idx = 0;
  1655. int cge = 1;
  1656. int gain = 0;
  1657. float gain_cache = 1.;
  1658. if (c) {
  1659. cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
  1660. gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
  1661. gain_cache = powf(scale, -gain);
  1662. }
  1663. if (coup->coupling_point == AFTER_IMDCT) {
  1664. coup->gain[c][0] = gain_cache;
  1665. } else {
  1666. for (g = 0; g < sce->ics.num_window_groups; g++) {
  1667. for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
  1668. if (sce->band_type[idx] != ZERO_BT) {
  1669. if (!cge) {
  1670. int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1671. if (t) {
  1672. int s = 1;
  1673. t = gain += t;
  1674. if (sign) {
  1675. s -= 2 * (t & 0x1);
  1676. t >>= 1;
  1677. }
  1678. gain_cache = powf(scale, -t) * s;
  1679. }
  1680. }
  1681. coup->gain[c][idx] = gain_cache;
  1682. }
  1683. }
  1684. }
  1685. }
  1686. }
  1687. return 0;
  1688. }
  1689. /**
  1690. * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
  1691. *
  1692. * @return Returns number of bytes consumed.
  1693. */
  1694. static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
  1695. GetBitContext *gb)
  1696. {
  1697. int i;
  1698. int num_excl_chan = 0;
  1699. do {
  1700. for (i = 0; i < 7; i++)
  1701. che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
  1702. } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
  1703. return num_excl_chan / 7;
  1704. }
  1705. /**
  1706. * Decode dynamic range information; reference: table 4.52.
  1707. *
  1708. * @param cnt length of TYPE_FIL syntactic element in bytes
  1709. *
  1710. * @return Returns number of bytes consumed.
  1711. */
  1712. static int decode_dynamic_range(DynamicRangeControl *che_drc,
  1713. GetBitContext *gb, int cnt)
  1714. {
  1715. int n = 1;
  1716. int drc_num_bands = 1;
  1717. int i;
  1718. /* pce_tag_present? */
  1719. if (get_bits1(gb)) {
  1720. che_drc->pce_instance_tag = get_bits(gb, 4);
  1721. skip_bits(gb, 4); // tag_reserved_bits
  1722. n++;
  1723. }
  1724. /* excluded_chns_present? */
  1725. if (get_bits1(gb)) {
  1726. n += decode_drc_channel_exclusions(che_drc, gb);
  1727. }
  1728. /* drc_bands_present? */
  1729. if (get_bits1(gb)) {
  1730. che_drc->band_incr = get_bits(gb, 4);
  1731. che_drc->interpolation_scheme = get_bits(gb, 4);
  1732. n++;
  1733. drc_num_bands += che_drc->band_incr;
  1734. for (i = 0; i < drc_num_bands; i++) {
  1735. che_drc->band_top[i] = get_bits(gb, 8);
  1736. n++;
  1737. }
  1738. }
  1739. /* prog_ref_level_present? */
  1740. if (get_bits1(gb)) {
  1741. che_drc->prog_ref_level = get_bits(gb, 7);
  1742. skip_bits1(gb); // prog_ref_level_reserved_bits
  1743. n++;
  1744. }
  1745. for (i = 0; i < drc_num_bands; i++) {
  1746. che_drc->dyn_rng_sgn[i] = get_bits1(gb);
  1747. che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
  1748. n++;
  1749. }
  1750. return n;
  1751. }
  1752. /**
  1753. * Decode extension data (incomplete); reference: table 4.51.
  1754. *
  1755. * @param cnt length of TYPE_FIL syntactic element in bytes
  1756. *
  1757. * @return Returns number of bytes consumed
  1758. */
  1759. static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
  1760. ChannelElement *che, enum RawDataBlockType elem_type)
  1761. {
  1762. int crc_flag = 0;
  1763. int res = cnt;
  1764. switch (get_bits(gb, 4)) { // extension type
  1765. case EXT_SBR_DATA_CRC:
  1766. crc_flag++;
  1767. case EXT_SBR_DATA:
  1768. if (!che) {
  1769. av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
  1770. return res;
  1771. } else if (!ac->oc[1].m4ac.sbr) {
  1772. av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
  1773. skip_bits_long(gb, 8 * cnt - 4);
  1774. return res;
  1775. } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
  1776. av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
  1777. skip_bits_long(gb, 8 * cnt - 4);
  1778. return res;
  1779. } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
  1780. ac->oc[1].m4ac.sbr = 1;
  1781. ac->oc[1].m4ac.ps = 1;
  1782. output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
  1783. ac->oc[1].m4ac.chan_config, ac->oc[1].status);
  1784. } else {
  1785. ac->oc[1].m4ac.sbr = 1;
  1786. }
  1787. res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
  1788. break;
  1789. case EXT_DYNAMIC_RANGE:
  1790. res = decode_dynamic_range(&ac->che_drc, gb, cnt);
  1791. break;
  1792. case EXT_FILL:
  1793. case EXT_FILL_DATA:
  1794. case EXT_DATA_ELEMENT:
  1795. default:
  1796. skip_bits_long(gb, 8 * cnt - 4);
  1797. break;
  1798. };
  1799. return res;
  1800. }
  1801. /**
  1802. * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
  1803. *
  1804. * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
  1805. * @param coef spectral coefficients
  1806. */
  1807. static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
  1808. IndividualChannelStream *ics, int decode)
  1809. {
  1810. const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
  1811. int w, filt, m, i;
  1812. int bottom, top, order, start, end, size, inc;
  1813. float lpc[TNS_MAX_ORDER];
  1814. float tmp[TNS_MAX_ORDER];
  1815. for (w = 0; w < ics->num_windows; w++) {
  1816. bottom = ics->num_swb;
  1817. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  1818. top = bottom;
  1819. bottom = FFMAX(0, top - tns->length[w][filt]);
  1820. order = tns->order[w][filt];
  1821. if (order == 0)
  1822. continue;
  1823. // tns_decode_coef
  1824. compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
  1825. start = ics->swb_offset[FFMIN(bottom, mmm)];
  1826. end = ics->swb_offset[FFMIN( top, mmm)];
  1827. if ((size = end - start) <= 0)
  1828. continue;
  1829. if (tns->direction[w][filt]) {
  1830. inc = -1;
  1831. start = end - 1;
  1832. } else {
  1833. inc = 1;
  1834. }
  1835. start += w * 128;
  1836. if (decode) {
  1837. // ar filter
  1838. for (m = 0; m < size; m++, start += inc)
  1839. for (i = 1; i <= FFMIN(m, order); i++)
  1840. coef[start] -= coef[start - i * inc] * lpc[i - 1];
  1841. } else {
  1842. // ma filter
  1843. for (m = 0; m < size; m++, start += inc) {
  1844. tmp[0] = coef[start];
  1845. for (i = 1; i <= FFMIN(m, order); i++)
  1846. coef[start] += tmp[i] * lpc[i - 1];
  1847. for (i = order; i > 0; i--)
  1848. tmp[i] = tmp[i - 1];
  1849. }
  1850. }
  1851. }
  1852. }
  1853. }
  1854. /**
  1855. * Apply windowing and MDCT to obtain the spectral
  1856. * coefficient from the predicted sample by LTP.
  1857. */
  1858. static void windowing_and_mdct_ltp(AACContext *ac, float *out,
  1859. float *in, IndividualChannelStream *ics)
  1860. {
  1861. const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1862. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  1863. const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1864. const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  1865. if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
  1866. ac->dsp.vector_fmul(in, in, lwindow_prev, 1024);
  1867. } else {
  1868. memset(in, 0, 448 * sizeof(float));
  1869. ac->dsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
  1870. }
  1871. if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
  1872. ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
  1873. } else {
  1874. ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
  1875. memset(in + 1024 + 576, 0, 448 * sizeof(float));
  1876. }
  1877. ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
  1878. }
  1879. /**
  1880. * Apply the long term prediction
  1881. */
  1882. static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
  1883. {
  1884. const LongTermPrediction *ltp = &sce->ics.ltp;
  1885. const uint16_t *offsets = sce->ics.swb_offset;
  1886. int i, sfb;
  1887. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  1888. float *predTime = sce->ret;
  1889. float *predFreq = ac->buf_mdct;
  1890. int16_t num_samples = 2048;
  1891. if (ltp->lag < 1024)
  1892. num_samples = ltp->lag + 1024;
  1893. for (i = 0; i < num_samples; i++)
  1894. predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
  1895. memset(&predTime[i], 0, (2048 - i) * sizeof(float));
  1896. windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
  1897. if (sce->tns.present)
  1898. apply_tns(predFreq, &sce->tns, &sce->ics, 0);
  1899. for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
  1900. if (ltp->used[sfb])
  1901. for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
  1902. sce->coeffs[i] += predFreq[i];
  1903. }
  1904. }
  1905. /**
  1906. * Update the LTP buffer for next frame
  1907. */
  1908. static void update_ltp(AACContext *ac, SingleChannelElement *sce)
  1909. {
  1910. IndividualChannelStream *ics = &sce->ics;
  1911. float *saved = sce->saved;
  1912. float *saved_ltp = sce->coeffs;
  1913. const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1914. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  1915. int i;
  1916. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1917. memcpy(saved_ltp, saved, 512 * sizeof(float));
  1918. memset(saved_ltp + 576, 0, 448 * sizeof(float));
  1919. ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  1920. for (i = 0; i < 64; i++)
  1921. saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
  1922. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  1923. memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
  1924. memset(saved_ltp + 576, 0, 448 * sizeof(float));
  1925. ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  1926. for (i = 0; i < 64; i++)
  1927. saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
  1928. } else { // LONG_STOP or ONLY_LONG
  1929. ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
  1930. for (i = 0; i < 512; i++)
  1931. saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
  1932. }
  1933. memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
  1934. memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
  1935. memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
  1936. }
  1937. /**
  1938. * Conduct IMDCT and windowing.
  1939. */
  1940. static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
  1941. {
  1942. IndividualChannelStream *ics = &sce->ics;
  1943. float *in = sce->coeffs;
  1944. float *out = sce->ret;
  1945. float *saved = sce->saved;
  1946. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  1947. const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  1948. const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  1949. float *buf = ac->buf_mdct;
  1950. float *temp = ac->temp;
  1951. int i;
  1952. // imdct
  1953. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1954. for (i = 0; i < 1024; i += 128)
  1955. ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
  1956. } else
  1957. ac->mdct.imdct_half(&ac->mdct, buf, in);
  1958. /* window overlapping
  1959. * NOTE: To simplify the overlapping code, all 'meaningless' short to long
  1960. * and long to short transitions are considered to be short to short
  1961. * transitions. This leaves just two cases (long to long and short to short)
  1962. * with a little special sauce for EIGHT_SHORT_SEQUENCE.
  1963. */
  1964. if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
  1965. (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
  1966. ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
  1967. } else {
  1968. memcpy( out, saved, 448 * sizeof(float));
  1969. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1970. ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
  1971. ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
  1972. ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
  1973. ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
  1974. ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
  1975. memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
  1976. } else {
  1977. ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
  1978. memcpy( out + 576, buf + 64, 448 * sizeof(float));
  1979. }
  1980. }
  1981. // buffer update
  1982. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1983. memcpy( saved, temp + 64, 64 * sizeof(float));
  1984. ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
  1985. ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
  1986. ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
  1987. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  1988. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  1989. memcpy( saved, buf + 512, 448 * sizeof(float));
  1990. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  1991. } else { // LONG_STOP or ONLY_LONG
  1992. memcpy( saved, buf + 512, 512 * sizeof(float));
  1993. }
  1994. }
  1995. /**
  1996. * Apply dependent channel coupling (applied before IMDCT).
  1997. *
  1998. * @param index index into coupling gain array
  1999. */
  2000. static void apply_dependent_coupling(AACContext *ac,
  2001. SingleChannelElement *target,
  2002. ChannelElement *cce, int index)
  2003. {
  2004. IndividualChannelStream *ics = &cce->ch[0].ics;
  2005. const uint16_t *offsets = ics->swb_offset;
  2006. float *dest = target->coeffs;
  2007. const float *src = cce->ch[0].coeffs;
  2008. int g, i, group, k, idx = 0;
  2009. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
  2010. av_log(ac->avctx, AV_LOG_ERROR,
  2011. "Dependent coupling is not supported together with LTP\n");
  2012. return;
  2013. }
  2014. for (g = 0; g < ics->num_window_groups; g++) {
  2015. for (i = 0; i < ics->max_sfb; i++, idx++) {
  2016. if (cce->ch[0].band_type[idx] != ZERO_BT) {
  2017. const float gain = cce->coup.gain[index][idx];
  2018. for (group = 0; group < ics->group_len[g]; group++) {
  2019. for (k = offsets[i]; k < offsets[i + 1]; k++) {
  2020. // XXX dsputil-ize
  2021. dest[group * 128 + k] += gain * src[group * 128 + k];
  2022. }
  2023. }
  2024. }
  2025. }
  2026. dest += ics->group_len[g] * 128;
  2027. src += ics->group_len[g] * 128;
  2028. }
  2029. }
  2030. /**
  2031. * Apply independent channel coupling (applied after IMDCT).
  2032. *
  2033. * @param index index into coupling gain array
  2034. */
  2035. static void apply_independent_coupling(AACContext *ac,
  2036. SingleChannelElement *target,
  2037. ChannelElement *cce, int index)
  2038. {
  2039. int i;
  2040. const float gain = cce->coup.gain[index][0];
  2041. const float *src = cce->ch[0].ret;
  2042. float *dest = target->ret;
  2043. const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
  2044. for (i = 0; i < len; i++)
  2045. dest[i] += gain * src[i];
  2046. }
  2047. /**
  2048. * channel coupling transformation interface
  2049. *
  2050. * @param apply_coupling_method pointer to (in)dependent coupling function
  2051. */
  2052. static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
  2053. enum RawDataBlockType type, int elem_id,
  2054. enum CouplingPoint coupling_point,
  2055. void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
  2056. {
  2057. int i, c;
  2058. for (i = 0; i < MAX_ELEM_ID; i++) {
  2059. ChannelElement *cce = ac->che[TYPE_CCE][i];
  2060. int index = 0;
  2061. if (cce && cce->coup.coupling_point == coupling_point) {
  2062. ChannelCoupling *coup = &cce->coup;
  2063. for (c = 0; c <= coup->num_coupled; c++) {
  2064. if (coup->type[c] == type && coup->id_select[c] == elem_id) {
  2065. if (coup->ch_select[c] != 1) {
  2066. apply_coupling_method(ac, &cc->ch[0], cce, index);
  2067. if (coup->ch_select[c] != 0)
  2068. index++;
  2069. }
  2070. if (coup->ch_select[c] != 2)
  2071. apply_coupling_method(ac, &cc->ch[1], cce, index++);
  2072. } else
  2073. index += 1 + (coup->ch_select[c] == 3);
  2074. }
  2075. }
  2076. }
  2077. }
  2078. /**
  2079. * Convert spectral data to float samples, applying all supported tools as appropriate.
  2080. */
  2081. static void spectral_to_sample(AACContext *ac)
  2082. {
  2083. int i, type;
  2084. for (type = 3; type >= 0; type--) {
  2085. for (i = 0; i < MAX_ELEM_ID; i++) {
  2086. ChannelElement *che = ac->che[type][i];
  2087. if (che) {
  2088. if (type <= TYPE_CPE)
  2089. apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
  2090. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
  2091. if (che->ch[0].ics.predictor_present) {
  2092. if (che->ch[0].ics.ltp.present)
  2093. apply_ltp(ac, &che->ch[0]);
  2094. if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
  2095. apply_ltp(ac, &che->ch[1]);
  2096. }
  2097. }
  2098. if (che->ch[0].tns.present)
  2099. apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
  2100. if (che->ch[1].tns.present)
  2101. apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
  2102. if (type <= TYPE_CPE)
  2103. apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
  2104. if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
  2105. imdct_and_windowing(ac, &che->ch[0]);
  2106. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
  2107. update_ltp(ac, &che->ch[0]);
  2108. if (type == TYPE_CPE) {
  2109. imdct_and_windowing(ac, &che->ch[1]);
  2110. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
  2111. update_ltp(ac, &che->ch[1]);
  2112. }
  2113. if (ac->oc[1].m4ac.sbr > 0) {
  2114. ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
  2115. }
  2116. }
  2117. if (type <= TYPE_CCE)
  2118. apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
  2119. }
  2120. }
  2121. }
  2122. }
  2123. static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
  2124. {
  2125. int size;
  2126. AACADTSHeaderInfo hdr_info;
  2127. uint8_t layout_map[MAX_ELEM_ID*4][3];
  2128. int layout_map_tags;
  2129. size = avpriv_aac_parse_header(gb, &hdr_info);
  2130. if (size > 0) {
  2131. if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
  2132. // This is 2 for "VLB " audio in NSV files.
  2133. // See samples/nsv/vlb_audio.
  2134. av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
  2135. ac->warned_num_aac_frames = 1;
  2136. }
  2137. push_output_configuration(ac);
  2138. if (hdr_info.chan_config) {
  2139. ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
  2140. if (set_default_channel_config(ac->avctx, layout_map,
  2141. &layout_map_tags, hdr_info.chan_config))
  2142. return -7;
  2143. if (output_configure(ac, layout_map, layout_map_tags,
  2144. hdr_info.chan_config,
  2145. FFMAX(ac->oc[1].status, OC_TRIAL_FRAME)))
  2146. return -7;
  2147. } else {
  2148. ac->oc[1].m4ac.chan_config = 0;
  2149. }
  2150. ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
  2151. ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
  2152. ac->oc[1].m4ac.object_type = hdr_info.object_type;
  2153. if (ac->oc[0].status != OC_LOCKED ||
  2154. ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
  2155. ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
  2156. ac->oc[1].m4ac.sbr = -1;
  2157. ac->oc[1].m4ac.ps = -1;
  2158. }
  2159. if (!hdr_info.crc_absent)
  2160. skip_bits(gb, 16);
  2161. }
  2162. return size;
  2163. }
  2164. static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
  2165. int *got_frame_ptr, GetBitContext *gb)
  2166. {
  2167. AACContext *ac = avctx->priv_data;
  2168. ChannelElement *che = NULL, *che_prev = NULL;
  2169. enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
  2170. int err, elem_id;
  2171. int samples = 0, multiplier, audio_found = 0, pce_found = 0;
  2172. if (show_bits(gb, 12) == 0xfff) {
  2173. if (parse_adts_frame_header(ac, gb) < 0) {
  2174. av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
  2175. err = -1;
  2176. goto fail;
  2177. }
  2178. if (ac->oc[1].m4ac.sampling_index > 12) {
  2179. av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
  2180. err = -1;
  2181. goto fail;
  2182. }
  2183. }
  2184. ac->tags_mapped = 0;
  2185. // parse
  2186. while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
  2187. elem_id = get_bits(gb, 4);
  2188. if (elem_type < TYPE_DSE) {
  2189. if (!(che=get_che(ac, elem_type, elem_id))) {
  2190. av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
  2191. elem_type, elem_id);
  2192. err = -1;
  2193. goto fail;
  2194. }
  2195. samples = 1024;
  2196. }
  2197. switch (elem_type) {
  2198. case TYPE_SCE:
  2199. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2200. audio_found = 1;
  2201. break;
  2202. case TYPE_CPE:
  2203. err = decode_cpe(ac, gb, che);
  2204. audio_found = 1;
  2205. break;
  2206. case TYPE_CCE:
  2207. err = decode_cce(ac, gb, che);
  2208. break;
  2209. case TYPE_LFE:
  2210. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2211. audio_found = 1;
  2212. break;
  2213. case TYPE_DSE:
  2214. err = skip_data_stream_element(ac, gb);
  2215. break;
  2216. case TYPE_PCE: {
  2217. uint8_t layout_map[MAX_ELEM_ID*4][3];
  2218. int tags;
  2219. push_output_configuration(ac);
  2220. tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
  2221. if (tags < 0) {
  2222. err = tags;
  2223. break;
  2224. }
  2225. if (pce_found) {
  2226. av_log(avctx, AV_LOG_ERROR,
  2227. "Not evaluating a further program_config_element as this construct is dubious at best.\n");
  2228. pop_output_configuration(ac);
  2229. } else {
  2230. err = output_configure(ac, layout_map, tags, 0, OC_TRIAL_PCE);
  2231. if (!err)
  2232. ac->oc[1].m4ac.chan_config = 0;
  2233. pce_found = 1;
  2234. }
  2235. break;
  2236. }
  2237. case TYPE_FIL:
  2238. if (elem_id == 15)
  2239. elem_id += get_bits(gb, 8) - 1;
  2240. if (get_bits_left(gb) < 8 * elem_id) {
  2241. av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
  2242. err = -1;
  2243. goto fail;
  2244. }
  2245. while (elem_id > 0)
  2246. elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
  2247. err = 0; /* FIXME */
  2248. break;
  2249. default:
  2250. err = -1; /* should not happen, but keeps compiler happy */
  2251. break;
  2252. }
  2253. che_prev = che;
  2254. elem_type_prev = elem_type;
  2255. if (err)
  2256. goto fail;
  2257. if (get_bits_left(gb) < 3) {
  2258. av_log(avctx, AV_LOG_ERROR, overread_err);
  2259. err = -1;
  2260. goto fail;
  2261. }
  2262. }
  2263. spectral_to_sample(ac);
  2264. multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
  2265. samples <<= multiplier;
  2266. if (samples) {
  2267. /* get output buffer */
  2268. ac->frame.nb_samples = samples;
  2269. if ((err = avctx->get_buffer(avctx, &ac->frame)) < 0) {
  2270. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  2271. err = -1;
  2272. goto fail;
  2273. }
  2274. if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
  2275. ac->fmt_conv.float_interleave((float *)ac->frame.data[0],
  2276. (const float **)ac->output_data,
  2277. samples, avctx->channels);
  2278. else
  2279. ac->fmt_conv.float_to_int16_interleave((int16_t *)ac->frame.data[0],
  2280. (const float **)ac->output_data,
  2281. samples, avctx->channels);
  2282. *(AVFrame *)data = ac->frame;
  2283. }
  2284. *got_frame_ptr = !!samples;
  2285. if (ac->oc[1].status && audio_found) {
  2286. avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
  2287. avctx->frame_size = samples;
  2288. ac->oc[1].status = OC_LOCKED;
  2289. }
  2290. return 0;
  2291. fail:
  2292. pop_output_configuration(ac);
  2293. return err;
  2294. }
  2295. static int aac_decode_frame(AVCodecContext *avctx, void *data,
  2296. int *got_frame_ptr, AVPacket *avpkt)
  2297. {
  2298. AACContext *ac = avctx->priv_data;
  2299. const uint8_t *buf = avpkt->data;
  2300. int buf_size = avpkt->size;
  2301. GetBitContext gb;
  2302. int buf_consumed;
  2303. int buf_offset;
  2304. int err;
  2305. int new_extradata_size;
  2306. const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
  2307. AV_PKT_DATA_NEW_EXTRADATA,
  2308. &new_extradata_size);
  2309. if (new_extradata && 0) {
  2310. av_free(avctx->extradata);
  2311. avctx->extradata = av_mallocz(new_extradata_size +
  2312. FF_INPUT_BUFFER_PADDING_SIZE);
  2313. if (!avctx->extradata)
  2314. return AVERROR(ENOMEM);
  2315. avctx->extradata_size = new_extradata_size;
  2316. memcpy(avctx->extradata, new_extradata, new_extradata_size);
  2317. push_output_configuration(ac);
  2318. if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
  2319. avctx->extradata,
  2320. avctx->extradata_size*8, 1) < 0) {
  2321. pop_output_configuration(ac);
  2322. return AVERROR_INVALIDDATA;
  2323. }
  2324. }
  2325. init_get_bits(&gb, buf, buf_size * 8);
  2326. if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb)) < 0)
  2327. return err;
  2328. buf_consumed = (get_bits_count(&gb) + 7) >> 3;
  2329. for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
  2330. if (buf[buf_offset])
  2331. break;
  2332. return buf_size > buf_offset ? buf_consumed : buf_size;
  2333. }
  2334. static av_cold int aac_decode_close(AVCodecContext *avctx)
  2335. {
  2336. AACContext *ac = avctx->priv_data;
  2337. int i, type;
  2338. for (i = 0; i < MAX_ELEM_ID; i++) {
  2339. for (type = 0; type < 4; type++) {
  2340. if (ac->che[type][i])
  2341. ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
  2342. av_freep(&ac->che[type][i]);
  2343. }
  2344. }
  2345. ff_mdct_end(&ac->mdct);
  2346. ff_mdct_end(&ac->mdct_small);
  2347. ff_mdct_end(&ac->mdct_ltp);
  2348. return 0;
  2349. }
  2350. #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
  2351. struct LATMContext {
  2352. AACContext aac_ctx; ///< containing AACContext
  2353. int initialized; ///< initilized after a valid extradata was seen
  2354. // parser data
  2355. int audio_mux_version_A; ///< LATM syntax version
  2356. int frame_length_type; ///< 0/1 variable/fixed frame length
  2357. int frame_length; ///< frame length for fixed frame length
  2358. };
  2359. static inline uint32_t latm_get_value(GetBitContext *b)
  2360. {
  2361. int length = get_bits(b, 2);
  2362. return get_bits_long(b, (length+1)*8);
  2363. }
  2364. static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
  2365. GetBitContext *gb, int asclen)
  2366. {
  2367. AACContext *ac = &latmctx->aac_ctx;
  2368. AVCodecContext *avctx = ac->avctx;
  2369. MPEG4AudioConfig m4ac = { 0 };
  2370. int config_start_bit = get_bits_count(gb);
  2371. int sync_extension = 0;
  2372. int bits_consumed, esize;
  2373. if (asclen) {
  2374. sync_extension = 1;
  2375. asclen = FFMIN(asclen, get_bits_left(gb));
  2376. } else
  2377. asclen = get_bits_left(gb);
  2378. if (config_start_bit % 8) {
  2379. av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
  2380. "config not byte aligned.\n", 1);
  2381. return AVERROR_INVALIDDATA;
  2382. }
  2383. if (asclen <= 0)
  2384. return AVERROR_INVALIDDATA;
  2385. bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
  2386. gb->buffer + (config_start_bit / 8),
  2387. asclen, sync_extension);
  2388. if (bits_consumed < 0)
  2389. return AVERROR_INVALIDDATA;
  2390. if (ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
  2391. ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
  2392. av_log(avctx, AV_LOG_INFO, "audio config changed\n");
  2393. latmctx->initialized = 0;
  2394. esize = (bits_consumed+7) / 8;
  2395. if (avctx->extradata_size < esize) {
  2396. av_free(avctx->extradata);
  2397. avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
  2398. if (!avctx->extradata)
  2399. return AVERROR(ENOMEM);
  2400. }
  2401. avctx->extradata_size = esize;
  2402. memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
  2403. memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  2404. }
  2405. skip_bits_long(gb, bits_consumed);
  2406. return bits_consumed;
  2407. }
  2408. static int read_stream_mux_config(struct LATMContext *latmctx,
  2409. GetBitContext *gb)
  2410. {
  2411. int ret, audio_mux_version = get_bits(gb, 1);
  2412. latmctx->audio_mux_version_A = 0;
  2413. if (audio_mux_version)
  2414. latmctx->audio_mux_version_A = get_bits(gb, 1);
  2415. if (!latmctx->audio_mux_version_A) {
  2416. if (audio_mux_version)
  2417. latm_get_value(gb); // taraFullness
  2418. skip_bits(gb, 1); // allStreamSameTimeFraming
  2419. skip_bits(gb, 6); // numSubFrames
  2420. // numPrograms
  2421. if (get_bits(gb, 4)) { // numPrograms
  2422. av_log_missing_feature(latmctx->aac_ctx.avctx,
  2423. "multiple programs are not supported\n", 1);
  2424. return AVERROR_PATCHWELCOME;
  2425. }
  2426. // for each program (which there is only on in DVB)
  2427. // for each layer (which there is only on in DVB)
  2428. if (get_bits(gb, 3)) { // numLayer
  2429. av_log_missing_feature(latmctx->aac_ctx.avctx,
  2430. "multiple layers are not supported\n", 1);
  2431. return AVERROR_PATCHWELCOME;
  2432. }
  2433. // for all but first stream: use_same_config = get_bits(gb, 1);
  2434. if (!audio_mux_version) {
  2435. if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
  2436. return ret;
  2437. } else {
  2438. int ascLen = latm_get_value(gb);
  2439. if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
  2440. return ret;
  2441. ascLen -= ret;
  2442. skip_bits_long(gb, ascLen);
  2443. }
  2444. latmctx->frame_length_type = get_bits(gb, 3);
  2445. switch (latmctx->frame_length_type) {
  2446. case 0:
  2447. skip_bits(gb, 8); // latmBufferFullness
  2448. break;
  2449. case 1:
  2450. latmctx->frame_length = get_bits(gb, 9);
  2451. break;
  2452. case 3:
  2453. case 4:
  2454. case 5:
  2455. skip_bits(gb, 6); // CELP frame length table index
  2456. break;
  2457. case 6:
  2458. case 7:
  2459. skip_bits(gb, 1); // HVXC frame length table index
  2460. break;
  2461. }
  2462. if (get_bits(gb, 1)) { // other data
  2463. if (audio_mux_version) {
  2464. latm_get_value(gb); // other_data_bits
  2465. } else {
  2466. int esc;
  2467. do {
  2468. esc = get_bits(gb, 1);
  2469. skip_bits(gb, 8);
  2470. } while (esc);
  2471. }
  2472. }
  2473. if (get_bits(gb, 1)) // crc present
  2474. skip_bits(gb, 8); // config_crc
  2475. }
  2476. return 0;
  2477. }
  2478. static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
  2479. {
  2480. uint8_t tmp;
  2481. if (ctx->frame_length_type == 0) {
  2482. int mux_slot_length = 0;
  2483. do {
  2484. tmp = get_bits(gb, 8);
  2485. mux_slot_length += tmp;
  2486. } while (tmp == 255);
  2487. return mux_slot_length;
  2488. } else if (ctx->frame_length_type == 1) {
  2489. return ctx->frame_length;
  2490. } else if (ctx->frame_length_type == 3 ||
  2491. ctx->frame_length_type == 5 ||
  2492. ctx->frame_length_type == 7) {
  2493. skip_bits(gb, 2); // mux_slot_length_coded
  2494. }
  2495. return 0;
  2496. }
  2497. static int read_audio_mux_element(struct LATMContext *latmctx,
  2498. GetBitContext *gb)
  2499. {
  2500. int err;
  2501. uint8_t use_same_mux = get_bits(gb, 1);
  2502. if (!use_same_mux) {
  2503. if ((err = read_stream_mux_config(latmctx, gb)) < 0)
  2504. return err;
  2505. } else if (!latmctx->aac_ctx.avctx->extradata) {
  2506. av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
  2507. "no decoder config found\n");
  2508. return AVERROR(EAGAIN);
  2509. }
  2510. if (latmctx->audio_mux_version_A == 0) {
  2511. int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
  2512. if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
  2513. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
  2514. return AVERROR_INVALIDDATA;
  2515. } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
  2516. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  2517. "frame length mismatch %d << %d\n",
  2518. mux_slot_length_bytes * 8, get_bits_left(gb));
  2519. return AVERROR_INVALIDDATA;
  2520. }
  2521. }
  2522. return 0;
  2523. }
  2524. static int latm_decode_frame(AVCodecContext *avctx, void *out,
  2525. int *got_frame_ptr, AVPacket *avpkt)
  2526. {
  2527. struct LATMContext *latmctx = avctx->priv_data;
  2528. int muxlength, err;
  2529. GetBitContext gb;
  2530. init_get_bits(&gb, avpkt->data, avpkt->size * 8);
  2531. // check for LOAS sync word
  2532. if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
  2533. return AVERROR_INVALIDDATA;
  2534. muxlength = get_bits(&gb, 13) + 3;
  2535. // not enough data, the parser should have sorted this
  2536. if (muxlength > avpkt->size)
  2537. return AVERROR_INVALIDDATA;
  2538. if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
  2539. return err;
  2540. if (!latmctx->initialized) {
  2541. if (!avctx->extradata) {
  2542. *got_frame_ptr = 0;
  2543. return avpkt->size;
  2544. } else {
  2545. push_output_configuration(&latmctx->aac_ctx);
  2546. if ((err = decode_audio_specific_config(
  2547. &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
  2548. avctx->extradata, avctx->extradata_size*8, 1)) < 0) {
  2549. pop_output_configuration(&latmctx->aac_ctx);
  2550. return err;
  2551. }
  2552. latmctx->initialized = 1;
  2553. }
  2554. }
  2555. if (show_bits(&gb, 12) == 0xfff) {
  2556. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  2557. "ADTS header detected, probably as result of configuration "
  2558. "misparsing\n");
  2559. return AVERROR_INVALIDDATA;
  2560. }
  2561. if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb)) < 0)
  2562. return err;
  2563. return muxlength;
  2564. }
  2565. static av_cold int latm_decode_init(AVCodecContext *avctx)
  2566. {
  2567. struct LATMContext *latmctx = avctx->priv_data;
  2568. int ret = aac_decode_init(avctx);
  2569. if (avctx->extradata_size > 0)
  2570. latmctx->initialized = !ret;
  2571. return ret;
  2572. }
  2573. AVCodec ff_aac_decoder = {
  2574. .name = "aac",
  2575. .type = AVMEDIA_TYPE_AUDIO,
  2576. .id = CODEC_ID_AAC,
  2577. .priv_data_size = sizeof(AACContext),
  2578. .init = aac_decode_init,
  2579. .close = aac_decode_close,
  2580. .decode = aac_decode_frame,
  2581. .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
  2582. .sample_fmts = (const enum AVSampleFormat[]) {
  2583. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
  2584. },
  2585. .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
  2586. .channel_layouts = aac_channel_layout,
  2587. .flush = flush,
  2588. };
  2589. /*
  2590. Note: This decoder filter is intended to decode LATM streams transferred
  2591. in MPEG transport streams which only contain one program.
  2592. To do a more complex LATM demuxing a separate LATM demuxer should be used.
  2593. */
  2594. AVCodec ff_aac_latm_decoder = {
  2595. .name = "aac_latm",
  2596. .type = AVMEDIA_TYPE_AUDIO,
  2597. .id = CODEC_ID_AAC_LATM,
  2598. .priv_data_size = sizeof(struct LATMContext),
  2599. .init = latm_decode_init,
  2600. .close = aac_decode_close,
  2601. .decode = latm_decode_frame,
  2602. .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"),
  2603. .sample_fmts = (const enum AVSampleFormat[]) {
  2604. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
  2605. },
  2606. .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
  2607. .channel_layouts = aac_channel_layout,
  2608. .flush = flush,
  2609. };