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.

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