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.

2902 lines
101KB

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