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.

3535 lines
123KB

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