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.

3513 lines
122KB

  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. avpriv_float_dsp_init(&ac->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
  1035. ac->random_state = 0x1f2e3d4c;
  1036. ff_aac_tableinit();
  1037. INIT_VLC_STATIC(&vlc_scalefactors, 7,
  1038. FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
  1039. ff_aac_scalefactor_bits,
  1040. sizeof(ff_aac_scalefactor_bits[0]),
  1041. sizeof(ff_aac_scalefactor_bits[0]),
  1042. ff_aac_scalefactor_code,
  1043. sizeof(ff_aac_scalefactor_code[0]),
  1044. sizeof(ff_aac_scalefactor_code[0]),
  1045. 352);
  1046. ff_mdct_init(&ac->mdct, 11, 1, 1.0 / (32768.0 * 1024.0));
  1047. ff_mdct_init(&ac->mdct_ld, 10, 1, 1.0 / (32768.0 * 512.0));
  1048. ff_mdct_init(&ac->mdct_small, 8, 1, 1.0 / (32768.0 * 128.0));
  1049. ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0 * 32768.0);
  1050. // window initialization
  1051. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  1052. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  1053. ff_init_ff_sine_windows(10);
  1054. ff_init_ff_sine_windows( 9);
  1055. ff_init_ff_sine_windows( 7);
  1056. cbrt_tableinit();
  1057. return 0;
  1058. }
  1059. /**
  1060. * Skip data_stream_element; reference: table 4.10.
  1061. */
  1062. static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
  1063. {
  1064. int byte_align = get_bits1(gb);
  1065. int count = get_bits(gb, 8);
  1066. if (count == 255)
  1067. count += get_bits(gb, 8);
  1068. if (byte_align)
  1069. align_get_bits(gb);
  1070. if (get_bits_left(gb) < 8 * count) {
  1071. av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
  1072. return AVERROR_INVALIDDATA;
  1073. }
  1074. skip_bits_long(gb, 8 * count);
  1075. return 0;
  1076. }
  1077. static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
  1078. GetBitContext *gb)
  1079. {
  1080. int sfb;
  1081. if (get_bits1(gb)) {
  1082. ics->predictor_reset_group = get_bits(gb, 5);
  1083. if (ics->predictor_reset_group == 0 ||
  1084. ics->predictor_reset_group > 30) {
  1085. av_log(ac->avctx, AV_LOG_ERROR,
  1086. "Invalid Predictor Reset Group.\n");
  1087. return AVERROR_INVALIDDATA;
  1088. }
  1089. }
  1090. for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
  1091. ics->prediction_used[sfb] = get_bits1(gb);
  1092. }
  1093. return 0;
  1094. }
  1095. /**
  1096. * Decode Long Term Prediction data; reference: table 4.xx.
  1097. */
  1098. static void decode_ltp(LongTermPrediction *ltp,
  1099. GetBitContext *gb, uint8_t max_sfb)
  1100. {
  1101. int sfb;
  1102. ltp->lag = get_bits(gb, 11);
  1103. ltp->coef = ltp_coef[get_bits(gb, 3)];
  1104. for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
  1105. ltp->used[sfb] = get_bits1(gb);
  1106. }
  1107. /**
  1108. * Decode Individual Channel Stream info; reference: table 4.6.
  1109. */
  1110. static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
  1111. GetBitContext *gb)
  1112. {
  1113. int aot = ac->oc[1].m4ac.object_type;
  1114. if (aot != AOT_ER_AAC_ELD) {
  1115. if (get_bits1(gb)) {
  1116. av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
  1117. return AVERROR_INVALIDDATA;
  1118. }
  1119. ics->window_sequence[1] = ics->window_sequence[0];
  1120. ics->window_sequence[0] = get_bits(gb, 2);
  1121. if (aot == AOT_ER_AAC_LD &&
  1122. ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
  1123. av_log(ac->avctx, AV_LOG_ERROR,
  1124. "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
  1125. "window sequence %d found.\n", ics->window_sequence[0]);
  1126. ics->window_sequence[0] = ONLY_LONG_SEQUENCE;
  1127. return AVERROR_INVALIDDATA;
  1128. }
  1129. ics->use_kb_window[1] = ics->use_kb_window[0];
  1130. ics->use_kb_window[0] = get_bits1(gb);
  1131. }
  1132. ics->num_window_groups = 1;
  1133. ics->group_len[0] = 1;
  1134. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1135. int i;
  1136. ics->max_sfb = get_bits(gb, 4);
  1137. for (i = 0; i < 7; i++) {
  1138. if (get_bits1(gb)) {
  1139. ics->group_len[ics->num_window_groups - 1]++;
  1140. } else {
  1141. ics->num_window_groups++;
  1142. ics->group_len[ics->num_window_groups - 1] = 1;
  1143. }
  1144. }
  1145. ics->num_windows = 8;
  1146. ics->swb_offset = ff_swb_offset_128[ac->oc[1].m4ac.sampling_index];
  1147. ics->num_swb = ff_aac_num_swb_128[ac->oc[1].m4ac.sampling_index];
  1148. ics->tns_max_bands = ff_tns_max_bands_128[ac->oc[1].m4ac.sampling_index];
  1149. ics->predictor_present = 0;
  1150. } else {
  1151. ics->max_sfb = get_bits(gb, 6);
  1152. ics->num_windows = 1;
  1153. if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
  1154. ics->swb_offset = ff_swb_offset_512[ac->oc[1].m4ac.sampling_index];
  1155. ics->num_swb = ff_aac_num_swb_512[ac->oc[1].m4ac.sampling_index];
  1156. ics->tns_max_bands = ff_tns_max_bands_512[ac->oc[1].m4ac.sampling_index];
  1157. if (!ics->num_swb || !ics->swb_offset)
  1158. return AVERROR_BUG;
  1159. } else {
  1160. ics->swb_offset = ff_swb_offset_1024[ac->oc[1].m4ac.sampling_index];
  1161. ics->num_swb = ff_aac_num_swb_1024[ac->oc[1].m4ac.sampling_index];
  1162. ics->tns_max_bands = ff_tns_max_bands_1024[ac->oc[1].m4ac.sampling_index];
  1163. }
  1164. if (aot != AOT_ER_AAC_ELD) {
  1165. ics->predictor_present = get_bits1(gb);
  1166. ics->predictor_reset_group = 0;
  1167. }
  1168. if (ics->predictor_present) {
  1169. if (aot == AOT_AAC_MAIN) {
  1170. if (decode_prediction(ac, ics, gb)) {
  1171. goto fail;
  1172. }
  1173. } else if (aot == AOT_AAC_LC ||
  1174. aot == AOT_ER_AAC_LC) {
  1175. av_log(ac->avctx, AV_LOG_ERROR,
  1176. "Prediction is not allowed in AAC-LC.\n");
  1177. goto fail;
  1178. } else {
  1179. if (aot == AOT_ER_AAC_LD) {
  1180. av_log(ac->avctx, AV_LOG_ERROR,
  1181. "LTP in ER AAC LD not yet implemented.\n");
  1182. return AVERROR_PATCHWELCOME;
  1183. }
  1184. if ((ics->ltp.present = get_bits(gb, 1)))
  1185. decode_ltp(&ics->ltp, gb, ics->max_sfb);
  1186. }
  1187. }
  1188. }
  1189. if (ics->max_sfb > ics->num_swb) {
  1190. av_log(ac->avctx, AV_LOG_ERROR,
  1191. "Number of scalefactor bands in group (%d) "
  1192. "exceeds limit (%d).\n",
  1193. ics->max_sfb, ics->num_swb);
  1194. goto fail;
  1195. }
  1196. return 0;
  1197. fail:
  1198. ics->max_sfb = 0;
  1199. return AVERROR_INVALIDDATA;
  1200. }
  1201. /**
  1202. * Decode band types (section_data payload); reference: table 4.46.
  1203. *
  1204. * @param band_type array of the used band type
  1205. * @param band_type_run_end array of the last scalefactor band of a band type run
  1206. *
  1207. * @return Returns error status. 0 - OK, !0 - error
  1208. */
  1209. static int decode_band_types(AACContext *ac, enum BandType band_type[120],
  1210. int band_type_run_end[120], GetBitContext *gb,
  1211. IndividualChannelStream *ics)
  1212. {
  1213. int g, idx = 0;
  1214. const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
  1215. for (g = 0; g < ics->num_window_groups; g++) {
  1216. int k = 0;
  1217. while (k < ics->max_sfb) {
  1218. uint8_t sect_end = k;
  1219. int sect_len_incr;
  1220. int sect_band_type = get_bits(gb, 4);
  1221. if (sect_band_type == 12) {
  1222. av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
  1223. return AVERROR_INVALIDDATA;
  1224. }
  1225. do {
  1226. sect_len_incr = get_bits(gb, bits);
  1227. sect_end += sect_len_incr;
  1228. if (get_bits_left(gb) < 0) {
  1229. av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
  1230. return AVERROR_INVALIDDATA;
  1231. }
  1232. if (sect_end > ics->max_sfb) {
  1233. av_log(ac->avctx, AV_LOG_ERROR,
  1234. "Number of bands (%d) exceeds limit (%d).\n",
  1235. sect_end, ics->max_sfb);
  1236. return AVERROR_INVALIDDATA;
  1237. }
  1238. } while (sect_len_incr == (1 << bits) - 1);
  1239. for (; k < sect_end; k++) {
  1240. band_type [idx] = sect_band_type;
  1241. band_type_run_end[idx++] = sect_end;
  1242. }
  1243. }
  1244. }
  1245. return 0;
  1246. }
  1247. /**
  1248. * Decode scalefactors; reference: table 4.47.
  1249. *
  1250. * @param global_gain first scalefactor value as scalefactors are differentially coded
  1251. * @param band_type array of the used band type
  1252. * @param band_type_run_end array of the last scalefactor band of a band type run
  1253. * @param sf array of scalefactors or intensity stereo positions
  1254. *
  1255. * @return Returns error status. 0 - OK, !0 - error
  1256. */
  1257. static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
  1258. unsigned int global_gain,
  1259. IndividualChannelStream *ics,
  1260. enum BandType band_type[120],
  1261. int band_type_run_end[120])
  1262. {
  1263. int g, i, idx = 0;
  1264. int offset[3] = { global_gain, global_gain - 90, 0 };
  1265. int clipped_offset;
  1266. int noise_flag = 1;
  1267. for (g = 0; g < ics->num_window_groups; g++) {
  1268. for (i = 0; i < ics->max_sfb;) {
  1269. int run_end = band_type_run_end[idx];
  1270. if (band_type[idx] == ZERO_BT) {
  1271. for (; i < run_end; i++, idx++)
  1272. sf[idx] = 0.0;
  1273. } else if ((band_type[idx] == INTENSITY_BT) ||
  1274. (band_type[idx] == INTENSITY_BT2)) {
  1275. for (; i < run_end; i++, idx++) {
  1276. offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1277. clipped_offset = av_clip(offset[2], -155, 100);
  1278. if (offset[2] != clipped_offset) {
  1279. avpriv_request_sample(ac->avctx,
  1280. "If you heard an audible artifact, there may be a bug in the decoder. "
  1281. "Clipped intensity stereo position (%d -> %d)",
  1282. offset[2], clipped_offset);
  1283. }
  1284. sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
  1285. }
  1286. } else if (band_type[idx] == NOISE_BT) {
  1287. for (; i < run_end; i++, idx++) {
  1288. if (noise_flag-- > 0)
  1289. offset[1] += get_bits(gb, 9) - 256;
  1290. else
  1291. offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1292. clipped_offset = av_clip(offset[1], -100, 155);
  1293. if (offset[1] != clipped_offset) {
  1294. avpriv_request_sample(ac->avctx,
  1295. "If you heard an audible artifact, there may be a bug in the decoder. "
  1296. "Clipped noise gain (%d -> %d)",
  1297. offset[1], clipped_offset);
  1298. }
  1299. sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
  1300. }
  1301. } else {
  1302. for (; i < run_end; i++, idx++) {
  1303. offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1304. if (offset[0] > 255U) {
  1305. av_log(ac->avctx, AV_LOG_ERROR,
  1306. "Scalefactor (%d) out of range.\n", offset[0]);
  1307. return AVERROR_INVALIDDATA;
  1308. }
  1309. sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
  1310. }
  1311. }
  1312. }
  1313. }
  1314. return 0;
  1315. }
  1316. /**
  1317. * Decode pulse data; reference: table 4.7.
  1318. */
  1319. static int decode_pulses(Pulse *pulse, GetBitContext *gb,
  1320. const uint16_t *swb_offset, int num_swb)
  1321. {
  1322. int i, pulse_swb;
  1323. pulse->num_pulse = get_bits(gb, 2) + 1;
  1324. pulse_swb = get_bits(gb, 6);
  1325. if (pulse_swb >= num_swb)
  1326. return -1;
  1327. pulse->pos[0] = swb_offset[pulse_swb];
  1328. pulse->pos[0] += get_bits(gb, 5);
  1329. if (pulse->pos[0] >= swb_offset[num_swb])
  1330. return -1;
  1331. pulse->amp[0] = get_bits(gb, 4);
  1332. for (i = 1; i < pulse->num_pulse; i++) {
  1333. pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
  1334. if (pulse->pos[i] >= swb_offset[num_swb])
  1335. return -1;
  1336. pulse->amp[i] = get_bits(gb, 4);
  1337. }
  1338. return 0;
  1339. }
  1340. /**
  1341. * Decode Temporal Noise Shaping data; reference: table 4.48.
  1342. *
  1343. * @return Returns error status. 0 - OK, !0 - error
  1344. */
  1345. static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
  1346. GetBitContext *gb, const IndividualChannelStream *ics)
  1347. {
  1348. int w, filt, i, coef_len, coef_res, coef_compress;
  1349. const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  1350. const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
  1351. for (w = 0; w < ics->num_windows; w++) {
  1352. if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
  1353. coef_res = get_bits1(gb);
  1354. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  1355. int tmp2_idx;
  1356. tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
  1357. if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
  1358. av_log(ac->avctx, AV_LOG_ERROR,
  1359. "TNS filter order %d is greater than maximum %d.\n",
  1360. tns->order[w][filt], tns_max_order);
  1361. tns->order[w][filt] = 0;
  1362. return AVERROR_INVALIDDATA;
  1363. }
  1364. if (tns->order[w][filt]) {
  1365. tns->direction[w][filt] = get_bits1(gb);
  1366. coef_compress = get_bits1(gb);
  1367. coef_len = coef_res + 3 - coef_compress;
  1368. tmp2_idx = 2 * coef_compress + coef_res;
  1369. for (i = 0; i < tns->order[w][filt]; i++)
  1370. tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
  1371. }
  1372. }
  1373. }
  1374. }
  1375. return 0;
  1376. }
  1377. /**
  1378. * Decode Mid/Side data; reference: table 4.54.
  1379. *
  1380. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  1381. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  1382. * [3] reserved for scalable AAC
  1383. */
  1384. static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
  1385. int ms_present)
  1386. {
  1387. int idx;
  1388. if (ms_present == 1) {
  1389. for (idx = 0;
  1390. idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
  1391. idx++)
  1392. cpe->ms_mask[idx] = get_bits1(gb);
  1393. } else if (ms_present == 2) {
  1394. memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask[0]) * cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb);
  1395. }
  1396. }
  1397. #ifndef VMUL2
  1398. static inline float *VMUL2(float *dst, const float *v, unsigned idx,
  1399. const float *scale)
  1400. {
  1401. float s = *scale;
  1402. *dst++ = v[idx & 15] * s;
  1403. *dst++ = v[idx>>4 & 15] * s;
  1404. return dst;
  1405. }
  1406. #endif
  1407. #ifndef VMUL4
  1408. static inline float *VMUL4(float *dst, const float *v, unsigned idx,
  1409. const float *scale)
  1410. {
  1411. float s = *scale;
  1412. *dst++ = v[idx & 3] * s;
  1413. *dst++ = v[idx>>2 & 3] * s;
  1414. *dst++ = v[idx>>4 & 3] * s;
  1415. *dst++ = v[idx>>6 & 3] * s;
  1416. return dst;
  1417. }
  1418. #endif
  1419. #ifndef VMUL2S
  1420. static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
  1421. unsigned sign, const float *scale)
  1422. {
  1423. union av_intfloat32 s0, s1;
  1424. s0.f = s1.f = *scale;
  1425. s0.i ^= sign >> 1 << 31;
  1426. s1.i ^= sign << 31;
  1427. *dst++ = v[idx & 15] * s0.f;
  1428. *dst++ = v[idx>>4 & 15] * s1.f;
  1429. return dst;
  1430. }
  1431. #endif
  1432. #ifndef VMUL4S
  1433. static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
  1434. unsigned sign, const float *scale)
  1435. {
  1436. unsigned nz = idx >> 12;
  1437. union av_intfloat32 s = { .f = *scale };
  1438. union av_intfloat32 t;
  1439. t.i = s.i ^ (sign & 1U<<31);
  1440. *dst++ = v[idx & 3] * t.f;
  1441. sign <<= nz & 1; nz >>= 1;
  1442. t.i = s.i ^ (sign & 1U<<31);
  1443. *dst++ = v[idx>>2 & 3] * t.f;
  1444. sign <<= nz & 1; nz >>= 1;
  1445. t.i = s.i ^ (sign & 1U<<31);
  1446. *dst++ = v[idx>>4 & 3] * t.f;
  1447. sign <<= nz & 1;
  1448. t.i = s.i ^ (sign & 1U<<31);
  1449. *dst++ = v[idx>>6 & 3] * t.f;
  1450. return dst;
  1451. }
  1452. #endif
  1453. /**
  1454. * Decode spectral data; reference: table 4.50.
  1455. * Dequantize and scale spectral data; reference: 4.6.3.3.
  1456. *
  1457. * @param coef array of dequantized, scaled spectral data
  1458. * @param sf array of scalefactors or intensity stereo positions
  1459. * @param pulse_present set if pulses are present
  1460. * @param pulse pointer to pulse data struct
  1461. * @param band_type array of the used band type
  1462. *
  1463. * @return Returns error status. 0 - OK, !0 - error
  1464. */
  1465. static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
  1466. GetBitContext *gb, const float sf[120],
  1467. int pulse_present, const Pulse *pulse,
  1468. const IndividualChannelStream *ics,
  1469. enum BandType band_type[120])
  1470. {
  1471. int i, k, g, idx = 0;
  1472. const int c = 1024 / ics->num_windows;
  1473. const uint16_t *offsets = ics->swb_offset;
  1474. float *coef_base = coef;
  1475. for (g = 0; g < ics->num_windows; g++)
  1476. memset(coef + g * 128 + offsets[ics->max_sfb], 0,
  1477. sizeof(float) * (c - offsets[ics->max_sfb]));
  1478. for (g = 0; g < ics->num_window_groups; g++) {
  1479. unsigned g_len = ics->group_len[g];
  1480. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1481. const unsigned cbt_m1 = band_type[idx] - 1;
  1482. float *cfo = coef + offsets[i];
  1483. int off_len = offsets[i + 1] - offsets[i];
  1484. int group;
  1485. if (cbt_m1 >= INTENSITY_BT2 - 1) {
  1486. for (group = 0; group < g_len; group++, cfo+=128) {
  1487. memset(cfo, 0, off_len * sizeof(float));
  1488. }
  1489. } else if (cbt_m1 == NOISE_BT - 1) {
  1490. for (group = 0; group < g_len; group++, cfo+=128) {
  1491. float scale;
  1492. float band_energy;
  1493. for (k = 0; k < off_len; k++) {
  1494. ac->random_state = lcg_random(ac->random_state);
  1495. cfo[k] = ac->random_state;
  1496. }
  1497. band_energy = ac->fdsp.scalarproduct_float(cfo, cfo, off_len);
  1498. scale = sf[idx] / sqrtf(band_energy);
  1499. ac->fdsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
  1500. }
  1501. } else {
  1502. const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
  1503. const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
  1504. VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
  1505. OPEN_READER(re, gb);
  1506. switch (cbt_m1 >> 1) {
  1507. case 0:
  1508. for (group = 0; group < g_len; group++, cfo+=128) {
  1509. float *cf = cfo;
  1510. int len = off_len;
  1511. do {
  1512. int code;
  1513. unsigned cb_idx;
  1514. UPDATE_CACHE(re, gb);
  1515. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1516. cb_idx = cb_vector_idx[code];
  1517. cf = VMUL4(cf, vq, cb_idx, sf + idx);
  1518. } while (len -= 4);
  1519. }
  1520. break;
  1521. case 1:
  1522. for (group = 0; group < g_len; group++, cfo+=128) {
  1523. float *cf = cfo;
  1524. int len = off_len;
  1525. do {
  1526. int code;
  1527. unsigned nnz;
  1528. unsigned cb_idx;
  1529. uint32_t bits;
  1530. UPDATE_CACHE(re, gb);
  1531. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1532. cb_idx = cb_vector_idx[code];
  1533. nnz = cb_idx >> 8 & 15;
  1534. bits = nnz ? GET_CACHE(re, gb) : 0;
  1535. LAST_SKIP_BITS(re, gb, nnz);
  1536. cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
  1537. } while (len -= 4);
  1538. }
  1539. break;
  1540. case 2:
  1541. for (group = 0; group < g_len; group++, cfo+=128) {
  1542. float *cf = cfo;
  1543. int len = off_len;
  1544. do {
  1545. int code;
  1546. unsigned cb_idx;
  1547. UPDATE_CACHE(re, gb);
  1548. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1549. cb_idx = cb_vector_idx[code];
  1550. cf = VMUL2(cf, vq, cb_idx, sf + idx);
  1551. } while (len -= 2);
  1552. }
  1553. break;
  1554. case 3:
  1555. case 4:
  1556. for (group = 0; group < g_len; group++, cfo+=128) {
  1557. float *cf = cfo;
  1558. int len = off_len;
  1559. do {
  1560. int code;
  1561. unsigned nnz;
  1562. unsigned cb_idx;
  1563. unsigned sign;
  1564. UPDATE_CACHE(re, gb);
  1565. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1566. cb_idx = cb_vector_idx[code];
  1567. nnz = cb_idx >> 8 & 15;
  1568. sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
  1569. LAST_SKIP_BITS(re, gb, nnz);
  1570. cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
  1571. } while (len -= 2);
  1572. }
  1573. break;
  1574. default:
  1575. for (group = 0; group < g_len; group++, cfo+=128) {
  1576. float *cf = cfo;
  1577. uint32_t *icf = (uint32_t *) cf;
  1578. int len = off_len;
  1579. do {
  1580. int code;
  1581. unsigned nzt, nnz;
  1582. unsigned cb_idx;
  1583. uint32_t bits;
  1584. int j;
  1585. UPDATE_CACHE(re, gb);
  1586. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  1587. if (!code) {
  1588. *icf++ = 0;
  1589. *icf++ = 0;
  1590. continue;
  1591. }
  1592. cb_idx = cb_vector_idx[code];
  1593. nnz = cb_idx >> 12;
  1594. nzt = cb_idx >> 8;
  1595. bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
  1596. LAST_SKIP_BITS(re, gb, nnz);
  1597. for (j = 0; j < 2; j++) {
  1598. if (nzt & 1<<j) {
  1599. uint32_t b;
  1600. int n;
  1601. /* The total length of escape_sequence must be < 22 bits according
  1602. to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
  1603. UPDATE_CACHE(re, gb);
  1604. b = GET_CACHE(re, gb);
  1605. b = 31 - av_log2(~b);
  1606. if (b > 8) {
  1607. av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
  1608. return AVERROR_INVALIDDATA;
  1609. }
  1610. SKIP_BITS(re, gb, b + 1);
  1611. b += 4;
  1612. n = (1 << b) + SHOW_UBITS(re, gb, b);
  1613. LAST_SKIP_BITS(re, gb, b);
  1614. *icf++ = cbrt_tab[n] | (bits & 1U<<31);
  1615. bits <<= 1;
  1616. } else {
  1617. unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
  1618. *icf++ = (bits & 1U<<31) | v;
  1619. bits <<= !!v;
  1620. }
  1621. cb_idx >>= 4;
  1622. }
  1623. } while (len -= 2);
  1624. ac->fdsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
  1625. }
  1626. }
  1627. CLOSE_READER(re, gb);
  1628. }
  1629. }
  1630. coef += g_len << 7;
  1631. }
  1632. if (pulse_present) {
  1633. idx = 0;
  1634. for (i = 0; i < pulse->num_pulse; i++) {
  1635. float co = coef_base[ pulse->pos[i] ];
  1636. while (offsets[idx + 1] <= pulse->pos[i])
  1637. idx++;
  1638. if (band_type[idx] != NOISE_BT && sf[idx]) {
  1639. float ico = -pulse->amp[i];
  1640. if (co) {
  1641. co /= sf[idx];
  1642. ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
  1643. }
  1644. coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
  1645. }
  1646. }
  1647. }
  1648. return 0;
  1649. }
  1650. static av_always_inline float flt16_round(float pf)
  1651. {
  1652. union av_intfloat32 tmp;
  1653. tmp.f = pf;
  1654. tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
  1655. return tmp.f;
  1656. }
  1657. static av_always_inline float flt16_even(float pf)
  1658. {
  1659. union av_intfloat32 tmp;
  1660. tmp.f = pf;
  1661. tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
  1662. return tmp.f;
  1663. }
  1664. static av_always_inline float flt16_trunc(float pf)
  1665. {
  1666. union av_intfloat32 pun;
  1667. pun.f = pf;
  1668. pun.i &= 0xFFFF0000U;
  1669. return pun.f;
  1670. }
  1671. static av_always_inline void predict(PredictorState *ps, float *coef,
  1672. int output_enable)
  1673. {
  1674. const float a = 0.953125; // 61.0 / 64
  1675. const float alpha = 0.90625; // 29.0 / 32
  1676. float e0, e1;
  1677. float pv;
  1678. float k1, k2;
  1679. float r0 = ps->r0, r1 = ps->r1;
  1680. float cor0 = ps->cor0, cor1 = ps->cor1;
  1681. float var0 = ps->var0, var1 = ps->var1;
  1682. k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
  1683. k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
  1684. pv = flt16_round(k1 * r0 + k2 * r1);
  1685. if (output_enable)
  1686. *coef += pv;
  1687. e0 = *coef;
  1688. e1 = e0 - k1 * r0;
  1689. ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
  1690. ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
  1691. ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
  1692. ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
  1693. ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
  1694. ps->r0 = flt16_trunc(a * e0);
  1695. }
  1696. /**
  1697. * Apply AAC-Main style frequency domain prediction.
  1698. */
  1699. static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
  1700. {
  1701. int sfb, k;
  1702. if (!sce->ics.predictor_initialized) {
  1703. reset_all_predictors(sce->predictor_state);
  1704. sce->ics.predictor_initialized = 1;
  1705. }
  1706. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  1707. for (sfb = 0;
  1708. sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
  1709. sfb++) {
  1710. for (k = sce->ics.swb_offset[sfb];
  1711. k < sce->ics.swb_offset[sfb + 1];
  1712. k++) {
  1713. predict(&sce->predictor_state[k], &sce->coeffs[k],
  1714. sce->ics.predictor_present &&
  1715. sce->ics.prediction_used[sfb]);
  1716. }
  1717. }
  1718. if (sce->ics.predictor_reset_group)
  1719. reset_predictor_group(sce->predictor_state,
  1720. sce->ics.predictor_reset_group);
  1721. } else
  1722. reset_all_predictors(sce->predictor_state);
  1723. }
  1724. /**
  1725. * Decode an individual_channel_stream payload; reference: table 4.44.
  1726. *
  1727. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  1728. * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
  1729. *
  1730. * @return Returns error status. 0 - OK, !0 - error
  1731. */
  1732. static int decode_ics(AACContext *ac, SingleChannelElement *sce,
  1733. GetBitContext *gb, int common_window, int scale_flag)
  1734. {
  1735. Pulse pulse;
  1736. TemporalNoiseShaping *tns = &sce->tns;
  1737. IndividualChannelStream *ics = &sce->ics;
  1738. float *out = sce->coeffs;
  1739. int global_gain, eld_syntax, er_syntax, pulse_present = 0;
  1740. int ret;
  1741. eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
  1742. er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
  1743. ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
  1744. ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
  1745. ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
  1746. /* This assignment is to silence a GCC warning about the variable being used
  1747. * uninitialized when in fact it always is.
  1748. */
  1749. pulse.num_pulse = 0;
  1750. global_gain = get_bits(gb, 8);
  1751. if (!common_window && !scale_flag) {
  1752. if (decode_ics_info(ac, ics, gb) < 0)
  1753. return AVERROR_INVALIDDATA;
  1754. }
  1755. if ((ret = decode_band_types(ac, sce->band_type,
  1756. sce->band_type_run_end, gb, ics)) < 0)
  1757. return ret;
  1758. if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
  1759. sce->band_type, sce->band_type_run_end)) < 0)
  1760. return ret;
  1761. pulse_present = 0;
  1762. if (!scale_flag) {
  1763. if (!eld_syntax && (pulse_present = get_bits1(gb))) {
  1764. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  1765. av_log(ac->avctx, AV_LOG_ERROR,
  1766. "Pulse tool not allowed in eight short sequence.\n");
  1767. return AVERROR_INVALIDDATA;
  1768. }
  1769. if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
  1770. av_log(ac->avctx, AV_LOG_ERROR,
  1771. "Pulse data corrupt or invalid.\n");
  1772. return AVERROR_INVALIDDATA;
  1773. }
  1774. }
  1775. tns->present = get_bits1(gb);
  1776. if (tns->present && !er_syntax)
  1777. if (decode_tns(ac, tns, gb, ics) < 0)
  1778. return AVERROR_INVALIDDATA;
  1779. if (!eld_syntax && get_bits1(gb)) {
  1780. avpriv_request_sample(ac->avctx, "SSR");
  1781. return AVERROR_PATCHWELCOME;
  1782. }
  1783. // I see no textual basis in the spec for this occurring after SSR gain
  1784. // control, but this is what both reference and real implmentations do
  1785. if (tns->present && er_syntax)
  1786. if (decode_tns(ac, tns, gb, ics) < 0)
  1787. return AVERROR_INVALIDDATA;
  1788. }
  1789. if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
  1790. &pulse, ics, sce->band_type) < 0)
  1791. return AVERROR_INVALIDDATA;
  1792. if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
  1793. apply_prediction(ac, sce);
  1794. return 0;
  1795. }
  1796. /**
  1797. * Mid/Side stereo decoding; reference: 4.6.8.1.3.
  1798. */
  1799. static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
  1800. {
  1801. const IndividualChannelStream *ics = &cpe->ch[0].ics;
  1802. float *ch0 = cpe->ch[0].coeffs;
  1803. float *ch1 = cpe->ch[1].coeffs;
  1804. int g, i, group, idx = 0;
  1805. const uint16_t *offsets = ics->swb_offset;
  1806. for (g = 0; g < ics->num_window_groups; g++) {
  1807. for (i = 0; i < ics->max_sfb; i++, idx++) {
  1808. if (cpe->ms_mask[idx] &&
  1809. cpe->ch[0].band_type[idx] < NOISE_BT &&
  1810. cpe->ch[1].band_type[idx] < NOISE_BT) {
  1811. for (group = 0; group < ics->group_len[g]; group++) {
  1812. ac->fdsp.butterflies_float(ch0 + group * 128 + offsets[i],
  1813. ch1 + group * 128 + offsets[i],
  1814. offsets[i+1] - offsets[i]);
  1815. }
  1816. }
  1817. }
  1818. ch0 += ics->group_len[g] * 128;
  1819. ch1 += ics->group_len[g] * 128;
  1820. }
  1821. }
  1822. /**
  1823. * intensity stereo decoding; reference: 4.6.8.2.3
  1824. *
  1825. * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
  1826. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  1827. * [3] reserved for scalable AAC
  1828. */
  1829. static void apply_intensity_stereo(AACContext *ac,
  1830. ChannelElement *cpe, int ms_present)
  1831. {
  1832. const IndividualChannelStream *ics = &cpe->ch[1].ics;
  1833. SingleChannelElement *sce1 = &cpe->ch[1];
  1834. float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
  1835. const uint16_t *offsets = ics->swb_offset;
  1836. int g, group, i, idx = 0;
  1837. int c;
  1838. float scale;
  1839. for (g = 0; g < ics->num_window_groups; g++) {
  1840. for (i = 0; i < ics->max_sfb;) {
  1841. if (sce1->band_type[idx] == INTENSITY_BT ||
  1842. sce1->band_type[idx] == INTENSITY_BT2) {
  1843. const int bt_run_end = sce1->band_type_run_end[idx];
  1844. for (; i < bt_run_end; i++, idx++) {
  1845. c = -1 + 2 * (sce1->band_type[idx] - 14);
  1846. if (ms_present)
  1847. c *= 1 - 2 * cpe->ms_mask[idx];
  1848. scale = c * sce1->sf[idx];
  1849. for (group = 0; group < ics->group_len[g]; group++)
  1850. ac->fdsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
  1851. coef0 + group * 128 + offsets[i],
  1852. scale,
  1853. offsets[i + 1] - offsets[i]);
  1854. }
  1855. } else {
  1856. int bt_run_end = sce1->band_type_run_end[idx];
  1857. idx += bt_run_end - i;
  1858. i = bt_run_end;
  1859. }
  1860. }
  1861. coef0 += ics->group_len[g] * 128;
  1862. coef1 += ics->group_len[g] * 128;
  1863. }
  1864. }
  1865. /**
  1866. * Decode a channel_pair_element; reference: table 4.4.
  1867. *
  1868. * @return Returns error status. 0 - OK, !0 - error
  1869. */
  1870. static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
  1871. {
  1872. int i, ret, common_window, ms_present = 0;
  1873. int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
  1874. common_window = eld_syntax || get_bits1(gb);
  1875. if (common_window) {
  1876. if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
  1877. return AVERROR_INVALIDDATA;
  1878. i = cpe->ch[1].ics.use_kb_window[0];
  1879. cpe->ch[1].ics = cpe->ch[0].ics;
  1880. cpe->ch[1].ics.use_kb_window[1] = i;
  1881. if (cpe->ch[1].ics.predictor_present &&
  1882. (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
  1883. if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
  1884. decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
  1885. ms_present = get_bits(gb, 2);
  1886. if (ms_present == 3) {
  1887. av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
  1888. return AVERROR_INVALIDDATA;
  1889. } else if (ms_present)
  1890. decode_mid_side_stereo(cpe, gb, ms_present);
  1891. }
  1892. if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
  1893. return ret;
  1894. if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
  1895. return ret;
  1896. if (common_window) {
  1897. if (ms_present)
  1898. apply_mid_side_stereo(ac, cpe);
  1899. if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
  1900. apply_prediction(ac, &cpe->ch[0]);
  1901. apply_prediction(ac, &cpe->ch[1]);
  1902. }
  1903. }
  1904. apply_intensity_stereo(ac, cpe, ms_present);
  1905. return 0;
  1906. }
  1907. static const float cce_scale[] = {
  1908. 1.09050773266525765921, //2^(1/8)
  1909. 1.18920711500272106672, //2^(1/4)
  1910. M_SQRT2,
  1911. 2,
  1912. };
  1913. /**
  1914. * Decode coupling_channel_element; reference: table 4.8.
  1915. *
  1916. * @return Returns error status. 0 - OK, !0 - error
  1917. */
  1918. static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
  1919. {
  1920. int num_gain = 0;
  1921. int c, g, sfb, ret;
  1922. int sign;
  1923. float scale;
  1924. SingleChannelElement *sce = &che->ch[0];
  1925. ChannelCoupling *coup = &che->coup;
  1926. coup->coupling_point = 2 * get_bits1(gb);
  1927. coup->num_coupled = get_bits(gb, 3);
  1928. for (c = 0; c <= coup->num_coupled; c++) {
  1929. num_gain++;
  1930. coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
  1931. coup->id_select[c] = get_bits(gb, 4);
  1932. if (coup->type[c] == TYPE_CPE) {
  1933. coup->ch_select[c] = get_bits(gb, 2);
  1934. if (coup->ch_select[c] == 3)
  1935. num_gain++;
  1936. } else
  1937. coup->ch_select[c] = 2;
  1938. }
  1939. coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
  1940. sign = get_bits(gb, 1);
  1941. scale = cce_scale[get_bits(gb, 2)];
  1942. if ((ret = decode_ics(ac, sce, gb, 0, 0)))
  1943. return ret;
  1944. for (c = 0; c < num_gain; c++) {
  1945. int idx = 0;
  1946. int cge = 1;
  1947. int gain = 0;
  1948. float gain_cache = 1.0;
  1949. if (c) {
  1950. cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
  1951. gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
  1952. gain_cache = powf(scale, -gain);
  1953. }
  1954. if (coup->coupling_point == AFTER_IMDCT) {
  1955. coup->gain[c][0] = gain_cache;
  1956. } else {
  1957. for (g = 0; g < sce->ics.num_window_groups; g++) {
  1958. for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
  1959. if (sce->band_type[idx] != ZERO_BT) {
  1960. if (!cge) {
  1961. int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  1962. if (t) {
  1963. int s = 1;
  1964. t = gain += t;
  1965. if (sign) {
  1966. s -= 2 * (t & 0x1);
  1967. t >>= 1;
  1968. }
  1969. gain_cache = powf(scale, -t) * s;
  1970. }
  1971. }
  1972. coup->gain[c][idx] = gain_cache;
  1973. }
  1974. }
  1975. }
  1976. }
  1977. }
  1978. return 0;
  1979. }
  1980. /**
  1981. * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
  1982. *
  1983. * @return Returns number of bytes consumed.
  1984. */
  1985. static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
  1986. GetBitContext *gb)
  1987. {
  1988. int i;
  1989. int num_excl_chan = 0;
  1990. do {
  1991. for (i = 0; i < 7; i++)
  1992. che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
  1993. } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
  1994. return num_excl_chan / 7;
  1995. }
  1996. /**
  1997. * Decode dynamic range information; reference: table 4.52.
  1998. *
  1999. * @return Returns number of bytes consumed.
  2000. */
  2001. static int decode_dynamic_range(DynamicRangeControl *che_drc,
  2002. GetBitContext *gb)
  2003. {
  2004. int n = 1;
  2005. int drc_num_bands = 1;
  2006. int i;
  2007. /* pce_tag_present? */
  2008. if (get_bits1(gb)) {
  2009. che_drc->pce_instance_tag = get_bits(gb, 4);
  2010. skip_bits(gb, 4); // tag_reserved_bits
  2011. n++;
  2012. }
  2013. /* excluded_chns_present? */
  2014. if (get_bits1(gb)) {
  2015. n += decode_drc_channel_exclusions(che_drc, gb);
  2016. }
  2017. /* drc_bands_present? */
  2018. if (get_bits1(gb)) {
  2019. che_drc->band_incr = get_bits(gb, 4);
  2020. che_drc->interpolation_scheme = get_bits(gb, 4);
  2021. n++;
  2022. drc_num_bands += che_drc->band_incr;
  2023. for (i = 0; i < drc_num_bands; i++) {
  2024. che_drc->band_top[i] = get_bits(gb, 8);
  2025. n++;
  2026. }
  2027. }
  2028. /* prog_ref_level_present? */
  2029. if (get_bits1(gb)) {
  2030. che_drc->prog_ref_level = get_bits(gb, 7);
  2031. skip_bits1(gb); // prog_ref_level_reserved_bits
  2032. n++;
  2033. }
  2034. for (i = 0; i < drc_num_bands; i++) {
  2035. che_drc->dyn_rng_sgn[i] = get_bits1(gb);
  2036. che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
  2037. n++;
  2038. }
  2039. return n;
  2040. }
  2041. static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
  2042. uint8_t buf[256];
  2043. int i, major, minor;
  2044. if (len < 13+7*8)
  2045. goto unknown;
  2046. get_bits(gb, 13); len -= 13;
  2047. for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
  2048. buf[i] = get_bits(gb, 8);
  2049. buf[i] = 0;
  2050. if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
  2051. av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
  2052. if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
  2053. ac->avctx->internal->skip_samples = 1024;
  2054. }
  2055. unknown:
  2056. skip_bits_long(gb, len);
  2057. return 0;
  2058. }
  2059. /**
  2060. * Decode extension data (incomplete); reference: table 4.51.
  2061. *
  2062. * @param cnt length of TYPE_FIL syntactic element in bytes
  2063. *
  2064. * @return Returns number of bytes consumed
  2065. */
  2066. static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
  2067. ChannelElement *che, enum RawDataBlockType elem_type)
  2068. {
  2069. int crc_flag = 0;
  2070. int res = cnt;
  2071. int type = get_bits(gb, 4);
  2072. if (ac->avctx->debug & FF_DEBUG_STARTCODE)
  2073. av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
  2074. switch (type) { // extension type
  2075. case EXT_SBR_DATA_CRC:
  2076. crc_flag++;
  2077. case EXT_SBR_DATA:
  2078. if (!che) {
  2079. av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
  2080. return res;
  2081. } else if (!ac->oc[1].m4ac.sbr) {
  2082. av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
  2083. skip_bits_long(gb, 8 * cnt - 4);
  2084. return res;
  2085. } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
  2086. av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
  2087. skip_bits_long(gb, 8 * cnt - 4);
  2088. return res;
  2089. } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
  2090. ac->oc[1].m4ac.sbr = 1;
  2091. ac->oc[1].m4ac.ps = 1;
  2092. ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
  2093. output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
  2094. ac->oc[1].status, 1);
  2095. } else {
  2096. ac->oc[1].m4ac.sbr = 1;
  2097. ac->avctx->profile = FF_PROFILE_AAC_HE;
  2098. }
  2099. res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
  2100. break;
  2101. case EXT_DYNAMIC_RANGE:
  2102. res = decode_dynamic_range(&ac->che_drc, gb);
  2103. break;
  2104. case EXT_FILL:
  2105. decode_fill(ac, gb, 8 * cnt - 4);
  2106. break;
  2107. case EXT_FILL_DATA:
  2108. case EXT_DATA_ELEMENT:
  2109. default:
  2110. skip_bits_long(gb, 8 * cnt - 4);
  2111. break;
  2112. };
  2113. return res;
  2114. }
  2115. /**
  2116. * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
  2117. *
  2118. * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
  2119. * @param coef spectral coefficients
  2120. */
  2121. static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
  2122. IndividualChannelStream *ics, int decode)
  2123. {
  2124. const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
  2125. int w, filt, m, i;
  2126. int bottom, top, order, start, end, size, inc;
  2127. float lpc[TNS_MAX_ORDER];
  2128. float tmp[TNS_MAX_ORDER+1];
  2129. for (w = 0; w < ics->num_windows; w++) {
  2130. bottom = ics->num_swb;
  2131. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  2132. top = bottom;
  2133. bottom = FFMAX(0, top - tns->length[w][filt]);
  2134. order = tns->order[w][filt];
  2135. if (order == 0)
  2136. continue;
  2137. // tns_decode_coef
  2138. compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
  2139. start = ics->swb_offset[FFMIN(bottom, mmm)];
  2140. end = ics->swb_offset[FFMIN( top, mmm)];
  2141. if ((size = end - start) <= 0)
  2142. continue;
  2143. if (tns->direction[w][filt]) {
  2144. inc = -1;
  2145. start = end - 1;
  2146. } else {
  2147. inc = 1;
  2148. }
  2149. start += w * 128;
  2150. if (decode) {
  2151. // ar filter
  2152. for (m = 0; m < size; m++, start += inc)
  2153. for (i = 1; i <= FFMIN(m, order); i++)
  2154. coef[start] -= coef[start - i * inc] * lpc[i - 1];
  2155. } else {
  2156. // ma filter
  2157. for (m = 0; m < size; m++, start += inc) {
  2158. tmp[0] = coef[start];
  2159. for (i = 1; i <= FFMIN(m, order); i++)
  2160. coef[start] += tmp[i] * lpc[i - 1];
  2161. for (i = order; i > 0; i--)
  2162. tmp[i] = tmp[i - 1];
  2163. }
  2164. }
  2165. }
  2166. }
  2167. }
  2168. /**
  2169. * Apply windowing and MDCT to obtain the spectral
  2170. * coefficient from the predicted sample by LTP.
  2171. */
  2172. static void windowing_and_mdct_ltp(AACContext *ac, float *out,
  2173. float *in, IndividualChannelStream *ics)
  2174. {
  2175. const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  2176. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  2177. const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  2178. const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  2179. if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
  2180. ac->fdsp.vector_fmul(in, in, lwindow_prev, 1024);
  2181. } else {
  2182. memset(in, 0, 448 * sizeof(float));
  2183. ac->fdsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
  2184. }
  2185. if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
  2186. ac->fdsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
  2187. } else {
  2188. ac->fdsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
  2189. memset(in + 1024 + 576, 0, 448 * sizeof(float));
  2190. }
  2191. ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
  2192. }
  2193. /**
  2194. * Apply the long term prediction
  2195. */
  2196. static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
  2197. {
  2198. const LongTermPrediction *ltp = &sce->ics.ltp;
  2199. const uint16_t *offsets = sce->ics.swb_offset;
  2200. int i, sfb;
  2201. if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  2202. float *predTime = sce->ret;
  2203. float *predFreq = ac->buf_mdct;
  2204. int16_t num_samples = 2048;
  2205. if (ltp->lag < 1024)
  2206. num_samples = ltp->lag + 1024;
  2207. for (i = 0; i < num_samples; i++)
  2208. predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
  2209. memset(&predTime[i], 0, (2048 - i) * sizeof(float));
  2210. ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
  2211. if (sce->tns.present)
  2212. ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
  2213. for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
  2214. if (ltp->used[sfb])
  2215. for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
  2216. sce->coeffs[i] += predFreq[i];
  2217. }
  2218. }
  2219. /**
  2220. * Update the LTP buffer for next frame
  2221. */
  2222. static void update_ltp(AACContext *ac, SingleChannelElement *sce)
  2223. {
  2224. IndividualChannelStream *ics = &sce->ics;
  2225. float *saved = sce->saved;
  2226. float *saved_ltp = sce->coeffs;
  2227. const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  2228. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  2229. int i;
  2230. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2231. memcpy(saved_ltp, saved, 512 * sizeof(float));
  2232. memset(saved_ltp + 576, 0, 448 * sizeof(float));
  2233. ac->fdsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  2234. for (i = 0; i < 64; i++)
  2235. saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
  2236. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  2237. memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
  2238. memset(saved_ltp + 576, 0, 448 * sizeof(float));
  2239. ac->fdsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  2240. for (i = 0; i < 64; i++)
  2241. saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
  2242. } else { // LONG_STOP or ONLY_LONG
  2243. ac->fdsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
  2244. for (i = 0; i < 512; i++)
  2245. saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
  2246. }
  2247. memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
  2248. memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
  2249. memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
  2250. }
  2251. /**
  2252. * Conduct IMDCT and windowing.
  2253. */
  2254. static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
  2255. {
  2256. IndividualChannelStream *ics = &sce->ics;
  2257. float *in = sce->coeffs;
  2258. float *out = sce->ret;
  2259. float *saved = sce->saved;
  2260. const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  2261. const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  2262. const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  2263. float *buf = ac->buf_mdct;
  2264. float *temp = ac->temp;
  2265. int i;
  2266. // imdct
  2267. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2268. for (i = 0; i < 1024; i += 128)
  2269. ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
  2270. } else
  2271. ac->mdct.imdct_half(&ac->mdct, buf, in);
  2272. /* window overlapping
  2273. * NOTE: To simplify the overlapping code, all 'meaningless' short to long
  2274. * and long to short transitions are considered to be short to short
  2275. * transitions. This leaves just two cases (long to long and short to short)
  2276. * with a little special sauce for EIGHT_SHORT_SEQUENCE.
  2277. */
  2278. if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
  2279. (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
  2280. ac->fdsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
  2281. } else {
  2282. memcpy( out, saved, 448 * sizeof(float));
  2283. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2284. ac->fdsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
  2285. ac->fdsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
  2286. ac->fdsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
  2287. ac->fdsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
  2288. ac->fdsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
  2289. memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
  2290. } else {
  2291. ac->fdsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
  2292. memcpy( out + 576, buf + 64, 448 * sizeof(float));
  2293. }
  2294. }
  2295. // buffer update
  2296. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  2297. memcpy( saved, temp + 64, 64 * sizeof(float));
  2298. ac->fdsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
  2299. ac->fdsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
  2300. ac->fdsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
  2301. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  2302. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  2303. memcpy( saved, buf + 512, 448 * sizeof(float));
  2304. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  2305. } else { // LONG_STOP or ONLY_LONG
  2306. memcpy( saved, buf + 512, 512 * sizeof(float));
  2307. }
  2308. }
  2309. static void imdct_and_windowing_ld(AACContext *ac, SingleChannelElement *sce)
  2310. {
  2311. IndividualChannelStream *ics = &sce->ics;
  2312. float *in = sce->coeffs;
  2313. float *out = sce->ret;
  2314. float *saved = sce->saved;
  2315. float *buf = ac->buf_mdct;
  2316. // imdct
  2317. ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
  2318. // window overlapping
  2319. if (ics->use_kb_window[1]) {
  2320. // AAC LD uses a low overlap sine window instead of a KBD window
  2321. memcpy(out, saved, 192 * sizeof(float));
  2322. ac->fdsp.vector_fmul_window(out + 192, saved + 192, buf, ff_sine_128, 64);
  2323. memcpy( out + 320, buf + 64, 192 * sizeof(float));
  2324. } else {
  2325. ac->fdsp.vector_fmul_window(out, saved, buf, ff_sine_512, 256);
  2326. }
  2327. // buffer update
  2328. memcpy(saved, buf + 256, 256 * sizeof(float));
  2329. }
  2330. static void imdct_and_windowing_eld(AACContext *ac, SingleChannelElement *sce)
  2331. {
  2332. float *in = sce->coeffs;
  2333. float *out = sce->ret;
  2334. float *saved = sce->saved;
  2335. const float *const window = ff_aac_eld_window;
  2336. float *buf = ac->buf_mdct;
  2337. int i;
  2338. const int n = 512;
  2339. const int n2 = n >> 1;
  2340. const int n4 = n >> 2;
  2341. // Inverse transform, mapped to the conventional IMDCT by
  2342. // Chivukula, R.K.; Reznik, Y.A.; Devarajan, V.,
  2343. // "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks,"
  2344. // International Conference on Audio, Language and Image Processing, ICALIP 2008.
  2345. // URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950
  2346. for (i = 0; i < n2; i+=2) {
  2347. float temp;
  2348. temp = in[i ]; in[i ] = -in[n - 1 - i]; in[n - 1 - i] = temp;
  2349. temp = -in[i + 1]; in[i + 1] = in[n - 2 - i]; in[n - 2 - i] = temp;
  2350. }
  2351. ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
  2352. for (i = 0; i < n; i+=2) {
  2353. buf[i] = -buf[i];
  2354. }
  2355. // Like with the regular IMDCT at this point we still have the middle half
  2356. // of a transform but with even symmetry on the left and odd symmetry on
  2357. // the right
  2358. // window overlapping
  2359. // The spec says to use samples [0..511] but the reference decoder uses
  2360. // samples [128..639].
  2361. for (i = n4; i < n2; i ++) {
  2362. out[i - n4] = buf[n2 - 1 - i] * window[i - n4] +
  2363. saved[ i + n2] * window[i + n - n4] +
  2364. -saved[ n + n2 - 1 - i] * window[i + 2*n - n4] +
  2365. -saved[2*n + n2 + i] * window[i + 3*n - n4];
  2366. }
  2367. for (i = 0; i < n2; i ++) {
  2368. out[n4 + i] = buf[i] * window[i + n2 - n4] +
  2369. -saved[ n - 1 - i] * window[i + n2 + n - n4] +
  2370. -saved[ n + i] * window[i + n2 + 2*n - n4] +
  2371. saved[2*n + n - 1 - i] * window[i + n2 + 3*n - n4];
  2372. }
  2373. for (i = 0; i < n4; i ++) {
  2374. out[n2 + n4 + i] = buf[ i + n2] * window[i + n - n4] +
  2375. -saved[ n2 - 1 - i] * window[i + 2*n - n4] +
  2376. -saved[ n + n2 + i] * window[i + 3*n - n4];
  2377. }
  2378. // buffer update
  2379. memmove(saved + n, saved, 2 * n * sizeof(float));
  2380. memcpy( saved, buf, n * sizeof(float));
  2381. }
  2382. /**
  2383. * Apply dependent channel coupling (applied before IMDCT).
  2384. *
  2385. * @param index index into coupling gain array
  2386. */
  2387. static void apply_dependent_coupling(AACContext *ac,
  2388. SingleChannelElement *target,
  2389. ChannelElement *cce, int index)
  2390. {
  2391. IndividualChannelStream *ics = &cce->ch[0].ics;
  2392. const uint16_t *offsets = ics->swb_offset;
  2393. float *dest = target->coeffs;
  2394. const float *src = cce->ch[0].coeffs;
  2395. int g, i, group, k, idx = 0;
  2396. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
  2397. av_log(ac->avctx, AV_LOG_ERROR,
  2398. "Dependent coupling is not supported together with LTP\n");
  2399. return;
  2400. }
  2401. for (g = 0; g < ics->num_window_groups; g++) {
  2402. for (i = 0; i < ics->max_sfb; i++, idx++) {
  2403. if (cce->ch[0].band_type[idx] != ZERO_BT) {
  2404. const float gain = cce->coup.gain[index][idx];
  2405. for (group = 0; group < ics->group_len[g]; group++) {
  2406. for (k = offsets[i]; k < offsets[i + 1]; k++) {
  2407. // FIXME: SIMDify
  2408. dest[group * 128 + k] += gain * src[group * 128 + k];
  2409. }
  2410. }
  2411. }
  2412. }
  2413. dest += ics->group_len[g] * 128;
  2414. src += ics->group_len[g] * 128;
  2415. }
  2416. }
  2417. /**
  2418. * Apply independent channel coupling (applied after IMDCT).
  2419. *
  2420. * @param index index into coupling gain array
  2421. */
  2422. static void apply_independent_coupling(AACContext *ac,
  2423. SingleChannelElement *target,
  2424. ChannelElement *cce, int index)
  2425. {
  2426. int i;
  2427. const float gain = cce->coup.gain[index][0];
  2428. const float *src = cce->ch[0].ret;
  2429. float *dest = target->ret;
  2430. const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
  2431. for (i = 0; i < len; i++)
  2432. dest[i] += gain * src[i];
  2433. }
  2434. /**
  2435. * channel coupling transformation interface
  2436. *
  2437. * @param apply_coupling_method pointer to (in)dependent coupling function
  2438. */
  2439. static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
  2440. enum RawDataBlockType type, int elem_id,
  2441. enum CouplingPoint coupling_point,
  2442. void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
  2443. {
  2444. int i, c;
  2445. for (i = 0; i < MAX_ELEM_ID; i++) {
  2446. ChannelElement *cce = ac->che[TYPE_CCE][i];
  2447. int index = 0;
  2448. if (cce && cce->coup.coupling_point == coupling_point) {
  2449. ChannelCoupling *coup = &cce->coup;
  2450. for (c = 0; c <= coup->num_coupled; c++) {
  2451. if (coup->type[c] == type && coup->id_select[c] == elem_id) {
  2452. if (coup->ch_select[c] != 1) {
  2453. apply_coupling_method(ac, &cc->ch[0], cce, index);
  2454. if (coup->ch_select[c] != 0)
  2455. index++;
  2456. }
  2457. if (coup->ch_select[c] != 2)
  2458. apply_coupling_method(ac, &cc->ch[1], cce, index++);
  2459. } else
  2460. index += 1 + (coup->ch_select[c] == 3);
  2461. }
  2462. }
  2463. }
  2464. }
  2465. /**
  2466. * Convert spectral data to float samples, applying all supported tools as appropriate.
  2467. */
  2468. static void spectral_to_sample(AACContext *ac)
  2469. {
  2470. int i, type;
  2471. void (*imdct_and_window)(AACContext *ac, SingleChannelElement *sce);
  2472. switch (ac->oc[1].m4ac.object_type) {
  2473. case AOT_ER_AAC_LD:
  2474. imdct_and_window = imdct_and_windowing_ld;
  2475. break;
  2476. case AOT_ER_AAC_ELD:
  2477. imdct_and_window = imdct_and_windowing_eld;
  2478. break;
  2479. default:
  2480. imdct_and_window = ac->imdct_and_windowing;
  2481. }
  2482. for (type = 3; type >= 0; type--) {
  2483. for (i = 0; i < MAX_ELEM_ID; i++) {
  2484. ChannelElement *che = ac->che[type][i];
  2485. if (che) {
  2486. if (type <= TYPE_CPE)
  2487. apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
  2488. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
  2489. if (che->ch[0].ics.predictor_present) {
  2490. if (che->ch[0].ics.ltp.present)
  2491. ac->apply_ltp(ac, &che->ch[0]);
  2492. if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
  2493. ac->apply_ltp(ac, &che->ch[1]);
  2494. }
  2495. }
  2496. if (che->ch[0].tns.present)
  2497. ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
  2498. if (che->ch[1].tns.present)
  2499. ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
  2500. if (type <= TYPE_CPE)
  2501. apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
  2502. if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
  2503. imdct_and_window(ac, &che->ch[0]);
  2504. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
  2505. ac->update_ltp(ac, &che->ch[0]);
  2506. if (type == TYPE_CPE) {
  2507. imdct_and_window(ac, &che->ch[1]);
  2508. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
  2509. ac->update_ltp(ac, &che->ch[1]);
  2510. }
  2511. if (ac->oc[1].m4ac.sbr > 0) {
  2512. ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
  2513. }
  2514. }
  2515. if (type <= TYPE_CCE)
  2516. apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
  2517. }
  2518. }
  2519. }
  2520. }
  2521. static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
  2522. {
  2523. int size;
  2524. AACADTSHeaderInfo hdr_info;
  2525. uint8_t layout_map[MAX_ELEM_ID*4][3];
  2526. int layout_map_tags, ret;
  2527. size = avpriv_aac_parse_header(gb, &hdr_info);
  2528. if (size > 0) {
  2529. if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
  2530. // This is 2 for "VLB " audio in NSV files.
  2531. // See samples/nsv/vlb_audio.
  2532. avpriv_report_missing_feature(ac->avctx,
  2533. "More than one AAC RDB per ADTS frame");
  2534. ac->warned_num_aac_frames = 1;
  2535. }
  2536. push_output_configuration(ac);
  2537. if (hdr_info.chan_config) {
  2538. ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
  2539. if ((ret = set_default_channel_config(ac->avctx,
  2540. layout_map,
  2541. &layout_map_tags,
  2542. hdr_info.chan_config)) < 0)
  2543. return ret;
  2544. if ((ret = output_configure(ac, layout_map, layout_map_tags,
  2545. FFMAX(ac->oc[1].status,
  2546. OC_TRIAL_FRAME), 0)) < 0)
  2547. return ret;
  2548. } else {
  2549. ac->oc[1].m4ac.chan_config = 0;
  2550. /**
  2551. * dual mono frames in Japanese DTV can have chan_config 0
  2552. * WITHOUT specifying PCE.
  2553. * thus, set dual mono as default.
  2554. */
  2555. if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
  2556. layout_map_tags = 2;
  2557. layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
  2558. layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
  2559. layout_map[0][1] = 0;
  2560. layout_map[1][1] = 1;
  2561. if (output_configure(ac, layout_map, layout_map_tags,
  2562. OC_TRIAL_FRAME, 0))
  2563. return -7;
  2564. }
  2565. }
  2566. ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
  2567. ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
  2568. ac->oc[1].m4ac.object_type = hdr_info.object_type;
  2569. if (ac->oc[0].status != OC_LOCKED ||
  2570. ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
  2571. ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
  2572. ac->oc[1].m4ac.sbr = -1;
  2573. ac->oc[1].m4ac.ps = -1;
  2574. }
  2575. if (!hdr_info.crc_absent)
  2576. skip_bits(gb, 16);
  2577. }
  2578. return size;
  2579. }
  2580. static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
  2581. int *got_frame_ptr, GetBitContext *gb)
  2582. {
  2583. AACContext *ac = avctx->priv_data;
  2584. ChannelElement *che;
  2585. int err, i;
  2586. int samples = 1024;
  2587. int chan_config = ac->oc[1].m4ac.chan_config;
  2588. int aot = ac->oc[1].m4ac.object_type;
  2589. if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
  2590. samples >>= 1;
  2591. ac->frame = data;
  2592. if ((err = frame_configure_elements(avctx)) < 0)
  2593. return err;
  2594. // The FF_PROFILE_AAC_* defines are all object_type - 1
  2595. // This may lead to an undefined profile being signaled
  2596. ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
  2597. ac->tags_mapped = 0;
  2598. if (chan_config < 0 || chan_config >= 8) {
  2599. avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
  2600. ac->oc[1].m4ac.chan_config);
  2601. return AVERROR_INVALIDDATA;
  2602. }
  2603. for (i = 0; i < tags_per_config[chan_config]; i++) {
  2604. const int elem_type = aac_channel_layout_map[chan_config-1][i][0];
  2605. const int elem_id = aac_channel_layout_map[chan_config-1][i][1];
  2606. if (!(che=get_che(ac, elem_type, elem_id))) {
  2607. av_log(ac->avctx, AV_LOG_ERROR,
  2608. "channel element %d.%d is not allocated\n",
  2609. elem_type, elem_id);
  2610. return AVERROR_INVALIDDATA;
  2611. }
  2612. if (aot != AOT_ER_AAC_ELD)
  2613. skip_bits(gb, 4);
  2614. switch (elem_type) {
  2615. case TYPE_SCE:
  2616. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2617. break;
  2618. case TYPE_CPE:
  2619. err = decode_cpe(ac, gb, che);
  2620. break;
  2621. case TYPE_LFE:
  2622. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2623. break;
  2624. }
  2625. if (err < 0)
  2626. return err;
  2627. }
  2628. spectral_to_sample(ac);
  2629. ac->frame->nb_samples = samples;
  2630. ac->frame->sample_rate = avctx->sample_rate;
  2631. *got_frame_ptr = 1;
  2632. skip_bits_long(gb, get_bits_left(gb));
  2633. return 0;
  2634. }
  2635. static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
  2636. int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
  2637. {
  2638. AACContext *ac = avctx->priv_data;
  2639. ChannelElement *che = NULL, *che_prev = NULL;
  2640. enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
  2641. int err, elem_id;
  2642. int samples = 0, multiplier, audio_found = 0, pce_found = 0;
  2643. int is_dmono, sce_count = 0;
  2644. ac->frame = data;
  2645. if (show_bits(gb, 12) == 0xfff) {
  2646. if ((err = parse_adts_frame_header(ac, gb)) < 0) {
  2647. av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
  2648. goto fail;
  2649. }
  2650. if (ac->oc[1].m4ac.sampling_index > 12) {
  2651. av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
  2652. err = AVERROR_INVALIDDATA;
  2653. goto fail;
  2654. }
  2655. }
  2656. if ((err = frame_configure_elements(avctx)) < 0)
  2657. goto fail;
  2658. // The FF_PROFILE_AAC_* defines are all object_type - 1
  2659. // This may lead to an undefined profile being signaled
  2660. ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
  2661. ac->tags_mapped = 0;
  2662. // parse
  2663. while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
  2664. elem_id = get_bits(gb, 4);
  2665. if (avctx->debug & FF_DEBUG_STARTCODE)
  2666. av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
  2667. if (elem_type < TYPE_DSE) {
  2668. if (!(che=get_che(ac, elem_type, elem_id))) {
  2669. av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
  2670. elem_type, elem_id);
  2671. err = AVERROR_INVALIDDATA;
  2672. goto fail;
  2673. }
  2674. samples = 1024;
  2675. }
  2676. switch (elem_type) {
  2677. case TYPE_SCE:
  2678. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2679. audio_found = 1;
  2680. sce_count++;
  2681. break;
  2682. case TYPE_CPE:
  2683. err = decode_cpe(ac, gb, che);
  2684. audio_found = 1;
  2685. break;
  2686. case TYPE_CCE:
  2687. err = decode_cce(ac, gb, che);
  2688. break;
  2689. case TYPE_LFE:
  2690. err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  2691. audio_found = 1;
  2692. break;
  2693. case TYPE_DSE:
  2694. err = skip_data_stream_element(ac, gb);
  2695. break;
  2696. case TYPE_PCE: {
  2697. uint8_t layout_map[MAX_ELEM_ID*4][3];
  2698. int tags;
  2699. push_output_configuration(ac);
  2700. tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
  2701. if (tags < 0) {
  2702. err = tags;
  2703. break;
  2704. }
  2705. if (pce_found) {
  2706. av_log(avctx, AV_LOG_ERROR,
  2707. "Not evaluating a further program_config_element as this construct is dubious at best.\n");
  2708. } else {
  2709. err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
  2710. if (!err)
  2711. ac->oc[1].m4ac.chan_config = 0;
  2712. pce_found = 1;
  2713. }
  2714. break;
  2715. }
  2716. case TYPE_FIL:
  2717. if (elem_id == 15)
  2718. elem_id += get_bits(gb, 8) - 1;
  2719. if (get_bits_left(gb) < 8 * elem_id) {
  2720. av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
  2721. err = AVERROR_INVALIDDATA;
  2722. goto fail;
  2723. }
  2724. while (elem_id > 0)
  2725. elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
  2726. err = 0; /* FIXME */
  2727. break;
  2728. default:
  2729. err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
  2730. break;
  2731. }
  2732. che_prev = che;
  2733. elem_type_prev = elem_type;
  2734. if (err)
  2735. goto fail;
  2736. if (get_bits_left(gb) < 3) {
  2737. av_log(avctx, AV_LOG_ERROR, overread_err);
  2738. err = AVERROR_INVALIDDATA;
  2739. goto fail;
  2740. }
  2741. }
  2742. spectral_to_sample(ac);
  2743. multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
  2744. samples <<= multiplier;
  2745. if (ac->oc[1].status && audio_found) {
  2746. avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
  2747. avctx->frame_size = samples;
  2748. ac->oc[1].status = OC_LOCKED;
  2749. }
  2750. if (multiplier) {
  2751. int side_size;
  2752. const uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  2753. if (side && side_size>=4)
  2754. AV_WL32(side, 2*AV_RL32(side));
  2755. }
  2756. *got_frame_ptr = !!samples;
  2757. if (samples) {
  2758. ac->frame->nb_samples = samples;
  2759. ac->frame->sample_rate = avctx->sample_rate;
  2760. } else
  2761. av_frame_unref(ac->frame);
  2762. *got_frame_ptr = !!samples;
  2763. /* for dual-mono audio (SCE + SCE) */
  2764. is_dmono = ac->dmono_mode && sce_count == 2 &&
  2765. ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
  2766. if (is_dmono) {
  2767. if (ac->dmono_mode == 1)
  2768. ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
  2769. else if (ac->dmono_mode == 2)
  2770. ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
  2771. }
  2772. return 0;
  2773. fail:
  2774. pop_output_configuration(ac);
  2775. return err;
  2776. }
  2777. static int aac_decode_frame(AVCodecContext *avctx, void *data,
  2778. int *got_frame_ptr, AVPacket *avpkt)
  2779. {
  2780. AACContext *ac = avctx->priv_data;
  2781. const uint8_t *buf = avpkt->data;
  2782. int buf_size = avpkt->size;
  2783. GetBitContext gb;
  2784. int buf_consumed;
  2785. int buf_offset;
  2786. int err;
  2787. int new_extradata_size;
  2788. const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
  2789. AV_PKT_DATA_NEW_EXTRADATA,
  2790. &new_extradata_size);
  2791. int jp_dualmono_size;
  2792. const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
  2793. AV_PKT_DATA_JP_DUALMONO,
  2794. &jp_dualmono_size);
  2795. if (new_extradata && 0) {
  2796. av_free(avctx->extradata);
  2797. avctx->extradata = av_mallocz(new_extradata_size +
  2798. FF_INPUT_BUFFER_PADDING_SIZE);
  2799. if (!avctx->extradata)
  2800. return AVERROR(ENOMEM);
  2801. avctx->extradata_size = new_extradata_size;
  2802. memcpy(avctx->extradata, new_extradata, new_extradata_size);
  2803. push_output_configuration(ac);
  2804. if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
  2805. avctx->extradata,
  2806. avctx->extradata_size*8, 1) < 0) {
  2807. pop_output_configuration(ac);
  2808. return AVERROR_INVALIDDATA;
  2809. }
  2810. }
  2811. ac->dmono_mode = 0;
  2812. if (jp_dualmono && jp_dualmono_size > 0)
  2813. ac->dmono_mode = 1 + *jp_dualmono;
  2814. if (ac->force_dmono_mode >= 0)
  2815. ac->dmono_mode = ac->force_dmono_mode;
  2816. if (INT_MAX / 8 <= buf_size)
  2817. return AVERROR_INVALIDDATA;
  2818. if ((err = init_get_bits(&gb, buf, buf_size * 8)) < 0)
  2819. return err;
  2820. switch (ac->oc[1].m4ac.object_type) {
  2821. case AOT_ER_AAC_LC:
  2822. case AOT_ER_AAC_LTP:
  2823. case AOT_ER_AAC_LD:
  2824. case AOT_ER_AAC_ELD:
  2825. err = aac_decode_er_frame(avctx, data, got_frame_ptr, &gb);
  2826. break;
  2827. default:
  2828. err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt);
  2829. }
  2830. if (err < 0)
  2831. return err;
  2832. buf_consumed = (get_bits_count(&gb) + 7) >> 3;
  2833. for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
  2834. if (buf[buf_offset])
  2835. break;
  2836. return buf_size > buf_offset ? buf_consumed : buf_size;
  2837. }
  2838. static av_cold int aac_decode_close(AVCodecContext *avctx)
  2839. {
  2840. AACContext *ac = avctx->priv_data;
  2841. int i, type;
  2842. for (i = 0; i < MAX_ELEM_ID; i++) {
  2843. for (type = 0; type < 4; type++) {
  2844. if (ac->che[type][i])
  2845. ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
  2846. av_freep(&ac->che[type][i]);
  2847. }
  2848. }
  2849. ff_mdct_end(&ac->mdct);
  2850. ff_mdct_end(&ac->mdct_small);
  2851. ff_mdct_end(&ac->mdct_ld);
  2852. ff_mdct_end(&ac->mdct_ltp);
  2853. return 0;
  2854. }
  2855. #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
  2856. struct LATMContext {
  2857. AACContext aac_ctx; ///< containing AACContext
  2858. int initialized; ///< initialized after a valid extradata was seen
  2859. // parser data
  2860. int audio_mux_version_A; ///< LATM syntax version
  2861. int frame_length_type; ///< 0/1 variable/fixed frame length
  2862. int frame_length; ///< frame length for fixed frame length
  2863. };
  2864. static inline uint32_t latm_get_value(GetBitContext *b)
  2865. {
  2866. int length = get_bits(b, 2);
  2867. return get_bits_long(b, (length+1)*8);
  2868. }
  2869. static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
  2870. GetBitContext *gb, int asclen)
  2871. {
  2872. AACContext *ac = &latmctx->aac_ctx;
  2873. AVCodecContext *avctx = ac->avctx;
  2874. MPEG4AudioConfig m4ac = { 0 };
  2875. int config_start_bit = get_bits_count(gb);
  2876. int sync_extension = 0;
  2877. int bits_consumed, esize;
  2878. if (asclen) {
  2879. sync_extension = 1;
  2880. asclen = FFMIN(asclen, get_bits_left(gb));
  2881. } else
  2882. asclen = get_bits_left(gb);
  2883. if (config_start_bit % 8) {
  2884. avpriv_request_sample(latmctx->aac_ctx.avctx,
  2885. "Non-byte-aligned audio-specific config");
  2886. return AVERROR_PATCHWELCOME;
  2887. }
  2888. if (asclen <= 0)
  2889. return AVERROR_INVALIDDATA;
  2890. bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
  2891. gb->buffer + (config_start_bit / 8),
  2892. asclen, sync_extension);
  2893. if (bits_consumed < 0)
  2894. return AVERROR_INVALIDDATA;
  2895. if (!latmctx->initialized ||
  2896. ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
  2897. ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
  2898. if(latmctx->initialized) {
  2899. av_log(avctx, AV_LOG_INFO, "audio config changed\n");
  2900. } else {
  2901. av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
  2902. }
  2903. latmctx->initialized = 0;
  2904. esize = (bits_consumed+7) / 8;
  2905. if (avctx->extradata_size < esize) {
  2906. av_free(avctx->extradata);
  2907. avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
  2908. if (!avctx->extradata)
  2909. return AVERROR(ENOMEM);
  2910. }
  2911. avctx->extradata_size = esize;
  2912. memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
  2913. memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  2914. }
  2915. skip_bits_long(gb, bits_consumed);
  2916. return bits_consumed;
  2917. }
  2918. static int read_stream_mux_config(struct LATMContext *latmctx,
  2919. GetBitContext *gb)
  2920. {
  2921. int ret, audio_mux_version = get_bits(gb, 1);
  2922. latmctx->audio_mux_version_A = 0;
  2923. if (audio_mux_version)
  2924. latmctx->audio_mux_version_A = get_bits(gb, 1);
  2925. if (!latmctx->audio_mux_version_A) {
  2926. if (audio_mux_version)
  2927. latm_get_value(gb); // taraFullness
  2928. skip_bits(gb, 1); // allStreamSameTimeFraming
  2929. skip_bits(gb, 6); // numSubFrames
  2930. // numPrograms
  2931. if (get_bits(gb, 4)) { // numPrograms
  2932. avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
  2933. return AVERROR_PATCHWELCOME;
  2934. }
  2935. // for each program (which there is only one in DVB)
  2936. // for each layer (which there is only one in DVB)
  2937. if (get_bits(gb, 3)) { // numLayer
  2938. avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
  2939. return AVERROR_PATCHWELCOME;
  2940. }
  2941. // for all but first stream: use_same_config = get_bits(gb, 1);
  2942. if (!audio_mux_version) {
  2943. if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
  2944. return ret;
  2945. } else {
  2946. int ascLen = latm_get_value(gb);
  2947. if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
  2948. return ret;
  2949. ascLen -= ret;
  2950. skip_bits_long(gb, ascLen);
  2951. }
  2952. latmctx->frame_length_type = get_bits(gb, 3);
  2953. switch (latmctx->frame_length_type) {
  2954. case 0:
  2955. skip_bits(gb, 8); // latmBufferFullness
  2956. break;
  2957. case 1:
  2958. latmctx->frame_length = get_bits(gb, 9);
  2959. break;
  2960. case 3:
  2961. case 4:
  2962. case 5:
  2963. skip_bits(gb, 6); // CELP frame length table index
  2964. break;
  2965. case 6:
  2966. case 7:
  2967. skip_bits(gb, 1); // HVXC frame length table index
  2968. break;
  2969. }
  2970. if (get_bits(gb, 1)) { // other data
  2971. if (audio_mux_version) {
  2972. latm_get_value(gb); // other_data_bits
  2973. } else {
  2974. int esc;
  2975. do {
  2976. esc = get_bits(gb, 1);
  2977. skip_bits(gb, 8);
  2978. } while (esc);
  2979. }
  2980. }
  2981. if (get_bits(gb, 1)) // crc present
  2982. skip_bits(gb, 8); // config_crc
  2983. }
  2984. return 0;
  2985. }
  2986. static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
  2987. {
  2988. uint8_t tmp;
  2989. if (ctx->frame_length_type == 0) {
  2990. int mux_slot_length = 0;
  2991. do {
  2992. tmp = get_bits(gb, 8);
  2993. mux_slot_length += tmp;
  2994. } while (tmp == 255);
  2995. return mux_slot_length;
  2996. } else if (ctx->frame_length_type == 1) {
  2997. return ctx->frame_length;
  2998. } else if (ctx->frame_length_type == 3 ||
  2999. ctx->frame_length_type == 5 ||
  3000. ctx->frame_length_type == 7) {
  3001. skip_bits(gb, 2); // mux_slot_length_coded
  3002. }
  3003. return 0;
  3004. }
  3005. static int read_audio_mux_element(struct LATMContext *latmctx,
  3006. GetBitContext *gb)
  3007. {
  3008. int err;
  3009. uint8_t use_same_mux = get_bits(gb, 1);
  3010. if (!use_same_mux) {
  3011. if ((err = read_stream_mux_config(latmctx, gb)) < 0)
  3012. return err;
  3013. } else if (!latmctx->aac_ctx.avctx->extradata) {
  3014. av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
  3015. "no decoder config found\n");
  3016. return AVERROR(EAGAIN);
  3017. }
  3018. if (latmctx->audio_mux_version_A == 0) {
  3019. int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
  3020. if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
  3021. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
  3022. return AVERROR_INVALIDDATA;
  3023. } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
  3024. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  3025. "frame length mismatch %d << %d\n",
  3026. mux_slot_length_bytes * 8, get_bits_left(gb));
  3027. return AVERROR_INVALIDDATA;
  3028. }
  3029. }
  3030. return 0;
  3031. }
  3032. static int latm_decode_frame(AVCodecContext *avctx, void *out,
  3033. int *got_frame_ptr, AVPacket *avpkt)
  3034. {
  3035. struct LATMContext *latmctx = avctx->priv_data;
  3036. int muxlength, err;
  3037. GetBitContext gb;
  3038. if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
  3039. return err;
  3040. // check for LOAS sync word
  3041. if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
  3042. return AVERROR_INVALIDDATA;
  3043. muxlength = get_bits(&gb, 13) + 3;
  3044. // not enough data, the parser should have sorted this out
  3045. if (muxlength > avpkt->size)
  3046. return AVERROR_INVALIDDATA;
  3047. if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
  3048. return err;
  3049. if (!latmctx->initialized) {
  3050. if (!avctx->extradata) {
  3051. *got_frame_ptr = 0;
  3052. return avpkt->size;
  3053. } else {
  3054. push_output_configuration(&latmctx->aac_ctx);
  3055. if ((err = decode_audio_specific_config(
  3056. &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
  3057. avctx->extradata, avctx->extradata_size*8, 1)) < 0) {
  3058. pop_output_configuration(&latmctx->aac_ctx);
  3059. return err;
  3060. }
  3061. latmctx->initialized = 1;
  3062. }
  3063. }
  3064. if (show_bits(&gb, 12) == 0xfff) {
  3065. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  3066. "ADTS header detected, probably as result of configuration "
  3067. "misparsing\n");
  3068. return AVERROR_INVALIDDATA;
  3069. }
  3070. if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt)) < 0)
  3071. return err;
  3072. return muxlength;
  3073. }
  3074. static av_cold int latm_decode_init(AVCodecContext *avctx)
  3075. {
  3076. struct LATMContext *latmctx = avctx->priv_data;
  3077. int ret = aac_decode_init(avctx);
  3078. if (avctx->extradata_size > 0)
  3079. latmctx->initialized = !ret;
  3080. return ret;
  3081. }
  3082. static void aacdec_init(AACContext *c)
  3083. {
  3084. c->imdct_and_windowing = imdct_and_windowing;
  3085. c->apply_ltp = apply_ltp;
  3086. c->apply_tns = apply_tns;
  3087. c->windowing_and_mdct_ltp = windowing_and_mdct_ltp;
  3088. c->update_ltp = update_ltp;
  3089. if(ARCH_MIPS)
  3090. ff_aacdec_init_mips(c);
  3091. }
  3092. /**
  3093. * AVOptions for Japanese DTV specific extensions (ADTS only)
  3094. */
  3095. #define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  3096. static const AVOption options[] = {
  3097. {"dual_mono_mode", "Select the channel to decode for dual mono",
  3098. offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
  3099. AACDEC_FLAGS, "dual_mono_mode"},
  3100. {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
  3101. {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
  3102. {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
  3103. {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
  3104. {NULL},
  3105. };
  3106. static const AVClass aac_decoder_class = {
  3107. .class_name = "AAC decoder",
  3108. .item_name = av_default_item_name,
  3109. .option = options,
  3110. .version = LIBAVUTIL_VERSION_INT,
  3111. };
  3112. AVCodec ff_aac_decoder = {
  3113. .name = "aac",
  3114. .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
  3115. .type = AVMEDIA_TYPE_AUDIO,
  3116. .id = AV_CODEC_ID_AAC,
  3117. .priv_data_size = sizeof(AACContext),
  3118. .init = aac_decode_init,
  3119. .close = aac_decode_close,
  3120. .decode = aac_decode_frame,
  3121. .sample_fmts = (const enum AVSampleFormat[]) {
  3122. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
  3123. },
  3124. .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
  3125. .channel_layouts = aac_channel_layout,
  3126. .flush = flush,
  3127. .priv_class = &aac_decoder_class,
  3128. };
  3129. /*
  3130. Note: This decoder filter is intended to decode LATM streams transferred
  3131. in MPEG transport streams which only contain one program.
  3132. To do a more complex LATM demuxing a separate LATM demuxer should be used.
  3133. */
  3134. AVCodec ff_aac_latm_decoder = {
  3135. .name = "aac_latm",
  3136. .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Coding LATM syntax)"),
  3137. .type = AVMEDIA_TYPE_AUDIO,
  3138. .id = AV_CODEC_ID_AAC_LATM,
  3139. .priv_data_size = sizeof(struct LATMContext),
  3140. .init = latm_decode_init,
  3141. .close = aac_decode_close,
  3142. .decode = latm_decode_frame,
  3143. .sample_fmts = (const enum AVSampleFormat[]) {
  3144. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
  3145. },
  3146. .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
  3147. .channel_layouts = aac_channel_layout,
  3148. .flush = flush,
  3149. };