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.

2877 lines
100KB

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