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.

2925 lines
102KB

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