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.

2905 lines
101KB

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