You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2873 lines
100KB

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