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.

3450 lines
119KB

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