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.

3495 lines
121KB

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