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.

3560 lines
124KB

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