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.

2890 lines
100KB

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