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.

2888 lines
100KB

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