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.

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