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.

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