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.

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