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.

587 lines
18KB

  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. #define FFT_FLOAT 1
  34. #define FFT_FIXED_32 0
  35. #define USE_FIXED 0
  36. #include "libavutil/float_dsp.h"
  37. #include "libavutil/opt.h"
  38. #include "avcodec.h"
  39. #include "internal.h"
  40. #include "get_bits.h"
  41. #include "fft.h"
  42. #include "imdct15.h"
  43. #include "lpc.h"
  44. #include "kbdwin.h"
  45. #include "sinewin.h"
  46. #include "aac.h"
  47. #include "aactab.h"
  48. #include "aacdectab.h"
  49. #include "cbrt_data.h"
  50. #include "sbr.h"
  51. #include "aacsbr.h"
  52. #include "mpeg4audio.h"
  53. #include "aacadtsdec.h"
  54. #include "profiles.h"
  55. #include "libavutil/intfloat.h"
  56. #include <errno.h>
  57. #include <math.h>
  58. #include <stdint.h>
  59. #include <string.h>
  60. #if ARCH_ARM
  61. # include "arm/aac.h"
  62. #elif ARCH_MIPS
  63. # include "mips/aacdec_mips.h"
  64. #endif
  65. static av_always_inline void reset_predict_state(PredictorState *ps)
  66. {
  67. ps->r0 = 0.0f;
  68. ps->r1 = 0.0f;
  69. ps->cor0 = 0.0f;
  70. ps->cor1 = 0.0f;
  71. ps->var0 = 1.0f;
  72. ps->var1 = 1.0f;
  73. }
  74. #ifndef VMUL2
  75. static inline float *VMUL2(float *dst, const float *v, unsigned idx,
  76. const float *scale)
  77. {
  78. float s = *scale;
  79. *dst++ = v[idx & 15] * s;
  80. *dst++ = v[idx>>4 & 15] * s;
  81. return dst;
  82. }
  83. #endif
  84. #ifndef VMUL4
  85. static inline float *VMUL4(float *dst, const float *v, unsigned idx,
  86. const float *scale)
  87. {
  88. float s = *scale;
  89. *dst++ = v[idx & 3] * s;
  90. *dst++ = v[idx>>2 & 3] * s;
  91. *dst++ = v[idx>>4 & 3] * s;
  92. *dst++ = v[idx>>6 & 3] * s;
  93. return dst;
  94. }
  95. #endif
  96. #ifndef VMUL2S
  97. static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
  98. unsigned sign, const float *scale)
  99. {
  100. union av_intfloat32 s0, s1;
  101. s0.f = s1.f = *scale;
  102. s0.i ^= sign >> 1 << 31;
  103. s1.i ^= sign << 31;
  104. *dst++ = v[idx & 15] * s0.f;
  105. *dst++ = v[idx>>4 & 15] * s1.f;
  106. return dst;
  107. }
  108. #endif
  109. #ifndef VMUL4S
  110. static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
  111. unsigned sign, const float *scale)
  112. {
  113. unsigned nz = idx >> 12;
  114. union av_intfloat32 s = { .f = *scale };
  115. union av_intfloat32 t;
  116. t.i = s.i ^ (sign & 1U<<31);
  117. *dst++ = v[idx & 3] * t.f;
  118. sign <<= nz & 1; nz >>= 1;
  119. t.i = s.i ^ (sign & 1U<<31);
  120. *dst++ = v[idx>>2 & 3] * t.f;
  121. sign <<= nz & 1; nz >>= 1;
  122. t.i = s.i ^ (sign & 1U<<31);
  123. *dst++ = v[idx>>4 & 3] * t.f;
  124. sign <<= nz & 1;
  125. t.i = s.i ^ (sign & 1U<<31);
  126. *dst++ = v[idx>>6 & 3] * t.f;
  127. return dst;
  128. }
  129. #endif
  130. static av_always_inline float flt16_round(float pf)
  131. {
  132. union av_intfloat32 tmp;
  133. tmp.f = pf;
  134. tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
  135. return tmp.f;
  136. }
  137. static av_always_inline float flt16_even(float pf)
  138. {
  139. union av_intfloat32 tmp;
  140. tmp.f = pf;
  141. tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
  142. return tmp.f;
  143. }
  144. static av_always_inline float flt16_trunc(float pf)
  145. {
  146. union av_intfloat32 pun;
  147. pun.f = pf;
  148. pun.i &= 0xFFFF0000U;
  149. return pun.f;
  150. }
  151. static av_always_inline void predict(PredictorState *ps, float *coef,
  152. int output_enable)
  153. {
  154. const float a = 0.953125; // 61.0 / 64
  155. const float alpha = 0.90625; // 29.0 / 32
  156. float e0, e1;
  157. float pv;
  158. float k1, k2;
  159. float r0 = ps->r0, r1 = ps->r1;
  160. float cor0 = ps->cor0, cor1 = ps->cor1;
  161. float var0 = ps->var0, var1 = ps->var1;
  162. k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
  163. k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
  164. pv = flt16_round(k1 * r0 + k2 * r1);
  165. if (output_enable)
  166. *coef += pv;
  167. e0 = *coef;
  168. e1 = e0 - k1 * r0;
  169. ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
  170. ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
  171. ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
  172. ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
  173. ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
  174. ps->r0 = flt16_trunc(a * e0);
  175. }
  176. /**
  177. * Apply dependent channel coupling (applied before IMDCT).
  178. *
  179. * @param index index into coupling gain array
  180. */
  181. static void apply_dependent_coupling(AACContext *ac,
  182. SingleChannelElement *target,
  183. ChannelElement *cce, int index)
  184. {
  185. IndividualChannelStream *ics = &cce->ch[0].ics;
  186. const uint16_t *offsets = ics->swb_offset;
  187. float *dest = target->coeffs;
  188. const float *src = cce->ch[0].coeffs;
  189. int g, i, group, k, idx = 0;
  190. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
  191. av_log(ac->avctx, AV_LOG_ERROR,
  192. "Dependent coupling is not supported together with LTP\n");
  193. return;
  194. }
  195. for (g = 0; g < ics->num_window_groups; g++) {
  196. for (i = 0; i < ics->max_sfb; i++, idx++) {
  197. if (cce->ch[0].band_type[idx] != ZERO_BT) {
  198. const float gain = cce->coup.gain[index][idx];
  199. for (group = 0; group < ics->group_len[g]; group++) {
  200. for (k = offsets[i]; k < offsets[i + 1]; k++) {
  201. // FIXME: SIMDify
  202. dest[group * 128 + k] += gain * src[group * 128 + k];
  203. }
  204. }
  205. }
  206. }
  207. dest += ics->group_len[g] * 128;
  208. src += ics->group_len[g] * 128;
  209. }
  210. }
  211. /**
  212. * Apply independent channel coupling (applied after IMDCT).
  213. *
  214. * @param index index into coupling gain array
  215. */
  216. static void apply_independent_coupling(AACContext *ac,
  217. SingleChannelElement *target,
  218. ChannelElement *cce, int index)
  219. {
  220. int i;
  221. const float gain = cce->coup.gain[index][0];
  222. const float *src = cce->ch[0].ret;
  223. float *dest = target->ret;
  224. const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
  225. for (i = 0; i < len; i++)
  226. dest[i] += gain * src[i];
  227. }
  228. #include "aacdec_template.c"
  229. #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
  230. struct LATMContext {
  231. AACContext aac_ctx; ///< containing AACContext
  232. int initialized; ///< initialized after a valid extradata was seen
  233. // parser data
  234. int audio_mux_version_A; ///< LATM syntax version
  235. int frame_length_type; ///< 0/1 variable/fixed frame length
  236. int frame_length; ///< frame length for fixed frame length
  237. };
  238. static inline uint32_t latm_get_value(GetBitContext *b)
  239. {
  240. int length = get_bits(b, 2);
  241. return get_bits_long(b, (length+1)*8);
  242. }
  243. static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
  244. GetBitContext *gb, int asclen)
  245. {
  246. AACContext *ac = &latmctx->aac_ctx;
  247. AVCodecContext *avctx = ac->avctx;
  248. MPEG4AudioConfig m4ac = { 0 };
  249. int config_start_bit = get_bits_count(gb);
  250. int sync_extension = 0;
  251. int bits_consumed, esize;
  252. if (asclen) {
  253. sync_extension = 1;
  254. asclen = FFMIN(asclen, get_bits_left(gb));
  255. } else
  256. asclen = get_bits_left(gb);
  257. if (config_start_bit % 8) {
  258. avpriv_request_sample(latmctx->aac_ctx.avctx,
  259. "Non-byte-aligned audio-specific config");
  260. return AVERROR_PATCHWELCOME;
  261. }
  262. if (asclen <= 0)
  263. return AVERROR_INVALIDDATA;
  264. bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
  265. gb->buffer + (config_start_bit / 8),
  266. asclen, sync_extension);
  267. if (bits_consumed < 0)
  268. return AVERROR_INVALIDDATA;
  269. if (!latmctx->initialized ||
  270. ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
  271. ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
  272. if(latmctx->initialized) {
  273. av_log(avctx, AV_LOG_INFO, "audio config changed\n");
  274. } else {
  275. av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
  276. }
  277. latmctx->initialized = 0;
  278. esize = (bits_consumed+7) / 8;
  279. if (avctx->extradata_size < esize) {
  280. av_free(avctx->extradata);
  281. avctx->extradata = av_malloc(esize + AV_INPUT_BUFFER_PADDING_SIZE);
  282. if (!avctx->extradata)
  283. return AVERROR(ENOMEM);
  284. }
  285. avctx->extradata_size = esize;
  286. memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
  287. memset(avctx->extradata+esize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  288. }
  289. skip_bits_long(gb, bits_consumed);
  290. return bits_consumed;
  291. }
  292. static int read_stream_mux_config(struct LATMContext *latmctx,
  293. GetBitContext *gb)
  294. {
  295. int ret, audio_mux_version = get_bits(gb, 1);
  296. latmctx->audio_mux_version_A = 0;
  297. if (audio_mux_version)
  298. latmctx->audio_mux_version_A = get_bits(gb, 1);
  299. if (!latmctx->audio_mux_version_A) {
  300. if (audio_mux_version)
  301. latm_get_value(gb); // taraFullness
  302. skip_bits(gb, 1); // allStreamSameTimeFraming
  303. skip_bits(gb, 6); // numSubFrames
  304. // numPrograms
  305. if (get_bits(gb, 4)) { // numPrograms
  306. avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
  307. return AVERROR_PATCHWELCOME;
  308. }
  309. // for each program (which there is only one in DVB)
  310. // for each layer (which there is only one in DVB)
  311. if (get_bits(gb, 3)) { // numLayer
  312. avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
  313. return AVERROR_PATCHWELCOME;
  314. }
  315. // for all but first stream: use_same_config = get_bits(gb, 1);
  316. if (!audio_mux_version) {
  317. if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
  318. return ret;
  319. } else {
  320. int ascLen = latm_get_value(gb);
  321. if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
  322. return ret;
  323. ascLen -= ret;
  324. skip_bits_long(gb, ascLen);
  325. }
  326. latmctx->frame_length_type = get_bits(gb, 3);
  327. switch (latmctx->frame_length_type) {
  328. case 0:
  329. skip_bits(gb, 8); // latmBufferFullness
  330. break;
  331. case 1:
  332. latmctx->frame_length = get_bits(gb, 9);
  333. break;
  334. case 3:
  335. case 4:
  336. case 5:
  337. skip_bits(gb, 6); // CELP frame length table index
  338. break;
  339. case 6:
  340. case 7:
  341. skip_bits(gb, 1); // HVXC frame length table index
  342. break;
  343. }
  344. if (get_bits(gb, 1)) { // other data
  345. if (audio_mux_version) {
  346. latm_get_value(gb); // other_data_bits
  347. } else {
  348. int esc;
  349. do {
  350. esc = get_bits(gb, 1);
  351. skip_bits(gb, 8);
  352. } while (esc);
  353. }
  354. }
  355. if (get_bits(gb, 1)) // crc present
  356. skip_bits(gb, 8); // config_crc
  357. }
  358. return 0;
  359. }
  360. static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
  361. {
  362. uint8_t tmp;
  363. if (ctx->frame_length_type == 0) {
  364. int mux_slot_length = 0;
  365. do {
  366. if (get_bits_left(gb) < 8)
  367. return AVERROR_INVALIDDATA;
  368. tmp = get_bits(gb, 8);
  369. mux_slot_length += tmp;
  370. } while (tmp == 255);
  371. return mux_slot_length;
  372. } else if (ctx->frame_length_type == 1) {
  373. return ctx->frame_length;
  374. } else if (ctx->frame_length_type == 3 ||
  375. ctx->frame_length_type == 5 ||
  376. ctx->frame_length_type == 7) {
  377. skip_bits(gb, 2); // mux_slot_length_coded
  378. }
  379. return 0;
  380. }
  381. static int read_audio_mux_element(struct LATMContext *latmctx,
  382. GetBitContext *gb)
  383. {
  384. int err;
  385. uint8_t use_same_mux = get_bits(gb, 1);
  386. if (!use_same_mux) {
  387. if ((err = read_stream_mux_config(latmctx, gb)) < 0)
  388. return err;
  389. } else if (!latmctx->aac_ctx.avctx->extradata) {
  390. av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
  391. "no decoder config found\n");
  392. return AVERROR(EAGAIN);
  393. }
  394. if (latmctx->audio_mux_version_A == 0) {
  395. int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
  396. if (mux_slot_length_bytes < 0 || mux_slot_length_bytes * 8LL > get_bits_left(gb)) {
  397. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
  398. return AVERROR_INVALIDDATA;
  399. } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
  400. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  401. "frame length mismatch %d << %d\n",
  402. mux_slot_length_bytes * 8, get_bits_left(gb));
  403. return AVERROR_INVALIDDATA;
  404. }
  405. }
  406. return 0;
  407. }
  408. static int latm_decode_frame(AVCodecContext *avctx, void *out,
  409. int *got_frame_ptr, AVPacket *avpkt)
  410. {
  411. struct LATMContext *latmctx = avctx->priv_data;
  412. int muxlength, err;
  413. GetBitContext gb;
  414. if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
  415. return err;
  416. // check for LOAS sync word
  417. if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
  418. return AVERROR_INVALIDDATA;
  419. muxlength = get_bits(&gb, 13) + 3;
  420. // not enough data, the parser should have sorted this out
  421. if (muxlength > avpkt->size)
  422. return AVERROR_INVALIDDATA;
  423. if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
  424. return err;
  425. if (!latmctx->initialized) {
  426. if (!avctx->extradata) {
  427. *got_frame_ptr = 0;
  428. return avpkt->size;
  429. } else {
  430. push_output_configuration(&latmctx->aac_ctx);
  431. if ((err = decode_audio_specific_config(
  432. &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
  433. avctx->extradata, avctx->extradata_size*8LL, 1)) < 0) {
  434. pop_output_configuration(&latmctx->aac_ctx);
  435. return err;
  436. }
  437. latmctx->initialized = 1;
  438. }
  439. }
  440. if (show_bits(&gb, 12) == 0xfff) {
  441. av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  442. "ADTS header detected, probably as result of configuration "
  443. "misparsing\n");
  444. return AVERROR_INVALIDDATA;
  445. }
  446. switch (latmctx->aac_ctx.oc[1].m4ac.object_type) {
  447. case AOT_ER_AAC_LC:
  448. case AOT_ER_AAC_LTP:
  449. case AOT_ER_AAC_LD:
  450. case AOT_ER_AAC_ELD:
  451. err = aac_decode_er_frame(avctx, out, got_frame_ptr, &gb);
  452. break;
  453. default:
  454. err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt);
  455. }
  456. if (err < 0)
  457. return err;
  458. return muxlength;
  459. }
  460. static av_cold int latm_decode_init(AVCodecContext *avctx)
  461. {
  462. struct LATMContext *latmctx = avctx->priv_data;
  463. int ret = aac_decode_init(avctx);
  464. if (avctx->extradata_size > 0)
  465. latmctx->initialized = !ret;
  466. return ret;
  467. }
  468. AVCodec ff_aac_decoder = {
  469. .name = "aac",
  470. .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
  471. .type = AVMEDIA_TYPE_AUDIO,
  472. .id = AV_CODEC_ID_AAC,
  473. .priv_data_size = sizeof(AACContext),
  474. .init = aac_decode_init,
  475. .close = aac_decode_close,
  476. .decode = aac_decode_frame,
  477. .sample_fmts = (const enum AVSampleFormat[]) {
  478. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
  479. },
  480. .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
  481. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  482. .channel_layouts = aac_channel_layout,
  483. .flush = flush,
  484. .priv_class = &aac_decoder_class,
  485. .profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
  486. };
  487. /*
  488. Note: This decoder filter is intended to decode LATM streams transferred
  489. in MPEG transport streams which only contain one program.
  490. To do a more complex LATM demuxing a separate LATM demuxer should be used.
  491. */
  492. AVCodec ff_aac_latm_decoder = {
  493. .name = "aac_latm",
  494. .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Coding LATM syntax)"),
  495. .type = AVMEDIA_TYPE_AUDIO,
  496. .id = AV_CODEC_ID_AAC_LATM,
  497. .priv_data_size = sizeof(struct LATMContext),
  498. .init = latm_decode_init,
  499. .close = aac_decode_close,
  500. .decode = latm_decode_frame,
  501. .sample_fmts = (const enum AVSampleFormat[]) {
  502. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
  503. },
  504. .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
  505. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  506. .channel_layouts = aac_channel_layout,
  507. .flush = flush,
  508. .profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
  509. };