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.

3475 lines
120KB

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