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.

3482 lines
121KB

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