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.

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