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.

2889 lines
100KB

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