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.

2893 lines
100KB

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