Advertisement
Guest User

AAC fix for myth 0.24

a guest
Oct 5th, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 69.44 KB | None | 0 0
  1. diff --git a/mythtv/configure b/mythtv/configure
  2. index e17edb0..659e8d0 100755
  3. --- a/mythtv/configure
  4. +++ b/mythtv/configure
  5. @@ -1473,7 +1473,7 @@ rdft_select="fft"
  6. # decoders / encoders / hardware accelerators
  7. aac_decoder_select="mdct rdft"
  8. aac_encoder_select="mdct"
  9. -aac_latm_decoder_select="aac_decoder"
  10. +aac_latm_decoder_select="aac_decoder aac_latm_parser"
  11. ac3_decoder_select="mdct ac3_parser"
  12. alac_encoder_select="lpc"
  13. amrnb_decoder_select="lsp"
  14. diff --git a/mythtv/external/FFmpeg/libavcodec/Makefile b/mythtv/external/FFmpeg/libavcodec/Makefile
  15. index 0d6ab83..15a9493 100644
  16. --- a/mythtv/external/FFmpeg/libavcodec/Makefile
  17. +++ b/mythtv/external/FFmpeg/libavcodec/Makefile
  18. @@ -61,7 +61,6 @@ OBJS-$(CONFIG_AAC_ENCODER) += aacenc.o aaccoder.o \
  19. aacpsy.o aactab.o \
  20. psymodel.o iirfilter.o \
  21. mpeg4audio.o
  22. -OBJS-$(CONFIG_AAC_LATM_DECODER) += aaclatmdec.o
  23. OBJS-$(CONFIG_AASC_DECODER) += aasc.o msrledec.o
  24. OBJS-$(CONFIG_AC3_DECODER) += ac3dec.o ac3dec_data.o ac3.o
  25. OBJS-$(CONFIG_AC3_ENCODER) += ac3enc.o ac3tab.o ac3.o
  26. diff --git a/mythtv/external/FFmpeg/libavcodec/aac.h b/mythtv/external/FFmpeg/libavcodec/aac.h
  27. index 94f578f..da6603a 100644
  28. --- a/mythtv/external/FFmpeg/libavcodec/aac.h
  29. +++ b/mythtv/external/FFmpeg/libavcodec/aac.h
  30. @@ -42,6 +42,7 @@
  31. #define MAX_ELEM_ID 16
  32.  
  33. #define TNS_MAX_ORDER 20
  34. +#define MAX_LTP_LONG_SFB 40
  35.  
  36. enum RawDataBlockType {
  37. TYPE_SCE,
  38. @@ -128,6 +129,17 @@ typedef struct {
  39. #define SCALE_MAX_POS 255 ///< scalefactor index maximum value
  40. #define SCALE_MAX_DIFF 60 ///< maximum scalefactor difference allowed by standard
  41. #define SCALE_DIFF_ZERO 60 ///< codebook index corresponding to zero scalefactor indices difference
  42. +#define POW_SF2_ZERO 200 ///< ff_aac_pow2sf_tab index corresponding to pow(2, 0);
  43. +
  44. +/**
  45. + * Long Term Prediction
  46. + */
  47. +typedef struct {
  48. + int8_t present;
  49. + int16_t lag;
  50. + float coef;
  51. + int8_t used[MAX_LTP_LONG_SFB];
  52. +} LongTermPrediction;
  53.  
  54. /**
  55. * Individual Channel Stream
  56. @@ -138,6 +150,7 @@ typedef struct {
  57. uint8_t use_kb_window[2]; ///< If set, use Kaiser-Bessel window, otherwise use a sinus window.
  58. int num_window_groups;
  59. uint8_t group_len[8];
  60. + LongTermPrediction ltp;
  61. const uint16_t *swb_offset; ///< table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular window
  62. const uint8_t *swb_sizes; ///< table of scalefactor band sizes for a particular window
  63. int num_swb; ///< number of scalefactor window bands
  64. @@ -205,14 +218,15 @@ typedef struct {
  65. IndividualChannelStream ics;
  66. TemporalNoiseShaping tns;
  67. Pulse pulse;
  68. - enum BandType band_type[128]; ///< band types
  69. - int band_type_run_end[120]; ///< band type run end points
  70. - float sf[120]; ///< scalefactors
  71. - int sf_idx[128]; ///< scalefactor indices (used by encoder)
  72. - uint8_t zeroes[128]; ///< band is not coded (used by encoder)
  73. - DECLARE_ALIGNED(16, float, coeffs)[1024]; ///< coefficients for IMDCT
  74. - DECLARE_ALIGNED(16, float, saved)[1024]; ///< overlap
  75. - DECLARE_ALIGNED(16, float, ret)[2048]; ///< PCM output
  76. + enum BandType band_type[128]; ///< band types
  77. + int band_type_run_end[120]; ///< band type run end points
  78. + float sf[120]; ///< scalefactors
  79. + int sf_idx[128]; ///< scalefactor indices (used by encoder)
  80. + uint8_t zeroes[128]; ///< band is not coded (used by encoder)
  81. + DECLARE_ALIGNED(16, float, coeffs)[1024]; ///< coefficients for IMDCT
  82. + DECLARE_ALIGNED(16, float, saved)[1024]; ///< overlap
  83. + DECLARE_ALIGNED(16, float, ret)[2048]; ///< PCM output
  84. + DECLARE_ALIGNED(16, int16_t, ltp_state)[3072]; ///< time signal for LTP
  85. PredictorState predictor_state[MAX_PREDICTORS];
  86. } SingleChannelElement;
  87.  
  88. @@ -251,7 +265,6 @@ typedef struct {
  89. */
  90. ChannelElement *che[4][MAX_ELEM_ID];
  91. ChannelElement *tag_che_map[4][MAX_ELEM_ID];
  92. - uint8_t tags_seen_this_frame[4][MAX_ELEM_ID];
  93. int tags_mapped;
  94. /** @} */
  95.  
  96. @@ -259,7 +272,7 @@ typedef struct {
  97. * @defgroup temporary aligned temporary buffers (We do not want to have these on the stack.)
  98. * @{
  99. */
  100. - DECLARE_ALIGNED(16, float, buf_mdct)[1024];
  101. + DECLARE_ALIGNED(16, float, buf_mdct)[2048];
  102. /** @} */
  103.  
  104. /**
  105. @@ -268,7 +281,9 @@ typedef struct {
  106. */
  107. FFTContext mdct;
  108. FFTContext mdct_small;
  109. + FFTContext mdct_ltp;
  110. DSPContext dsp;
  111. +// FmtConvertContext fmt_conv;
  112. int random_state;
  113. /** @} */
  114.  
  115. @@ -277,7 +292,6 @@ typedef struct {
  116. * @{
  117. */
  118. float *output_data[MAX_CHANNELS]; ///< Points to each element's 'ret' buffer (PCM output).
  119. - float add_bias; ///< offset for dsp.float_to_int16
  120. float sf_scale; ///< Pre-scale for correct IMDCT and dsp.float_to_int16.
  121. int sf_offset; ///< offset into pow2sf_tab as appropriate for dsp.float_to_int16
  122. /** @} */
  123. diff --git a/mythtv/external/FFmpeg/libavcodec/aacdec.c b/mythtv/external/FFmpeg/libavcodec/aacdec.c
  124. index 62aab34..c983141 100644
  125. --- a/mythtv/external/FFmpeg/libavcodec/aacdec.c
  126. +++ b/mythtv/external/FFmpeg/libavcodec/aacdec.c
  127. @@ -3,6 +3,10 @@
  128. * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
  129. * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
  130. *
  131. + * AAC LATM decoder
  132. + * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
  133. + * Copyright (c) 2010 Janne Grunau <janne-ffmpeg@jannau.net>
  134. + *
  135. * This file is part of FFmpeg.
  136. *
  137. * FFmpeg is free software; you can redistribute it and/or
  138. @@ -38,7 +42,7 @@
  139. * Y filterbank - standard
  140. * N (code in SoC repo) filterbank - Scalable Sample Rate
  141. * Y Temporal Noise Shaping
  142. - * N (code in SoC repo) Long Term Prediction
  143. + * Y Long Term Prediction
  144. * Y intensity stereo
  145. * Y channel coupling
  146. * Y frequency domain prediction
  147. @@ -113,28 +117,11 @@ static const char overread_err[] = "Input buffer exhausted before END element fo
  148.  
  149. static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
  150. {
  151. - /* Some buggy encoders appear to set all elem_ids to zero and rely on
  152. - channels always occurring in the same order. This is expressly forbidden
  153. - by the spec but we will try to work around it.
  154. - */
  155. - int err_printed = 0;
  156. - while (ac->tags_seen_this_frame[type][elem_id] && elem_id < MAX_ELEM_ID) {
  157. - if (ac->output_configured < OC_LOCKED && !err_printed) {
  158. - av_log(ac->avctx, AV_LOG_WARNING, "Duplicate channel tag found, attempting to remap.\n");
  159. - err_printed = 1;
  160. - }
  161. - elem_id++;
  162. - }
  163. - if (elem_id == MAX_ELEM_ID)
  164. - return NULL;
  165. - ac->tags_seen_this_frame[type][elem_id] = 1;
  166. -
  167. - if (ac->tag_che_map[type][elem_id]) {
  168. + // For PCE based channel configurations map the channels solely based on tags.
  169. + if (!ac->m4ac.chan_config) {
  170. return ac->tag_che_map[type][elem_id];
  171. }
  172. - if (ac->tags_mapped >= tags_per_config[ac->m4ac.chan_config]) {
  173. - return NULL;
  174. - }
  175. + // For indexed channel configurations map the channels solely based on position.
  176. switch (ac->m4ac.chan_config) {
  177. case 7:
  178. if (ac->tags_mapped == 3 && type == TYPE_CPE) {
  179. @@ -190,9 +177,8 @@ static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
  180. * @return Returns error status. 0 - OK, !0 - error
  181. */
  182. static av_cold int che_configure(AACContext *ac,
  183. - enum ChannelPosition che_pos[4][MAX_ELEM_ID],
  184. - int type, int id,
  185. - int *channels)
  186. + enum ChannelPosition che_pos[4][MAX_ELEM_ID],
  187. + int type, int id, int *channels)
  188. {
  189. if (che_pos[type][id]) {
  190. if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
  191. @@ -222,9 +208,9 @@ static av_cold int che_configure(AACContext *ac,
  192. * @return Returns error status. 0 - OK, !0 - error
  193. */
  194. static av_cold int output_configure(AACContext *ac,
  195. - enum ChannelPosition che_pos[4][MAX_ELEM_ID],
  196. - enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  197. - int channel_config, enum OCStatus oc_type)
  198. + enum ChannelPosition che_pos[4][MAX_ELEM_ID],
  199. + enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  200. + int channel_config, enum OCStatus oc_type)
  201. {
  202. AVCodecContext *avctx = ac->avctx;
  203. int i, type, channels = 0, ret;
  204. @@ -241,8 +227,7 @@ static av_cold int output_configure(AACContext *ac,
  205. return ret;
  206. }
  207.  
  208. - memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  209. - ac->tags_mapped = 0;
  210. + memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  211.  
  212. avctx->channel_layout = aac_channel_layout[channel_config - 1];
  213. } else {
  214. @@ -263,9 +248,6 @@ static av_cold int output_configure(AACContext *ac,
  215. }
  216.  
  217. memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
  218. - ac->tags_mapped = 4 * MAX_ELEM_ID;
  219. -
  220. - avctx->channel_layout = 0;
  221. }
  222.  
  223. avctx->channels = channels;
  224. @@ -300,7 +282,8 @@ static void decode_channel_map(enum ChannelPosition *cpe_map,
  225. *
  226. * @return Returns error status. 0 - OK, !0 - error
  227. */
  228. -static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  229. +static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
  230. + enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  231. GetBitContext *gb)
  232. {
  233. int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
  234. @@ -309,8 +292,8 @@ static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_EL
  235. skip_bits(gb, 2); // object_type
  236.  
  237. sampling_index = get_bits(gb, 4);
  238. - if (ac->m4ac.sampling_index != sampling_index)
  239. - av_log(ac->avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
  240. + if (m4ac->sampling_index != sampling_index)
  241. + av_log(avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
  242.  
  243. num_front = get_bits(gb, 4);
  244. num_side = get_bits(gb, 4);
  245. @@ -327,6 +310,10 @@ static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_EL
  246. if (get_bits1(gb))
  247. skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
  248.  
  249. + if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
  250. + av_log(avctx, AV_LOG_ERROR, overread_err);
  251. + return -1;
  252. + }
  253. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
  254. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
  255. decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
  256. @@ -341,7 +328,7 @@ static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_EL
  257. /* comment field, first byte is length */
  258. comment_len = get_bits(gb, 8) * 8;
  259. if (get_bits_left(gb) < comment_len) {
  260. - av_log(ac->avctx, AV_LOG_ERROR, overread_err);
  261. + av_log(avctx, AV_LOG_ERROR, overread_err);
  262. return -1;
  263. }
  264. skip_bits_long(gb, comment_len);
  265. @@ -356,12 +343,12 @@ static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_EL
  266. *
  267. * @return Returns error status. 0 - OK, !0 - error
  268. */
  269. -static av_cold int set_default_channel_config(AACContext *ac,
  270. - enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  271. - int channel_config)
  272. +static av_cold int set_default_channel_config(AVCodecContext *avctx,
  273. + enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
  274. + int channel_config)
  275. {
  276. if (channel_config < 1 || channel_config > 7) {
  277. - av_log(ac->avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
  278. + av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
  279. channel_config);
  280. return -1;
  281. }
  282. @@ -397,16 +384,21 @@ static av_cold int set_default_channel_config(AACContext *ac,
  283. /**
  284. * Decode GA "General Audio" specific configuration; reference: table 4.1.
  285. *
  286. + * @param ac pointer to AACContext, may be null
  287. + * @param avctx pointer to AVCCodecContext, used for logging
  288. + *
  289. * @return Returns error status. 0 - OK, !0 - error
  290. */
  291. -static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb,
  292. +static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
  293. + GetBitContext *gb,
  294. + MPEG4AudioConfig *m4ac,
  295. int channel_config)
  296. {
  297. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  298. int extension_flag, ret;
  299.  
  300. if (get_bits1(gb)) { // frameLengthFlag
  301. - av_log_missing_feature(ac->avctx, "960/120 MDCT window is", 1);
  302. + av_log_missing_feature(avctx, "960/120 MDCT window is", 1);
  303. return -1;
  304. }
  305.  
  306. @@ -414,24 +406,24 @@ static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb,
  307. skip_bits(gb, 14); // coreCoderDelay
  308. extension_flag = get_bits1(gb);
  309.  
  310. - if (ac->m4ac.object_type == AOT_AAC_SCALABLE ||
  311. - ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
  312. + if (m4ac->object_type == AOT_AAC_SCALABLE ||
  313. + m4ac->object_type == AOT_ER_AAC_SCALABLE)
  314. skip_bits(gb, 3); // layerNr
  315.  
  316. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  317. if (channel_config == 0) {
  318. skip_bits(gb, 4); // element_instance_tag
  319. - if ((ret = decode_pce(ac, new_che_pos, gb)))
  320. + if ((ret = decode_pce(avctx, m4ac, new_che_pos, gb)))
  321. return ret;
  322. } else {
  323. - if ((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
  324. + if ((ret = set_default_channel_config(avctx, new_che_pos, channel_config)))
  325. return ret;
  326. }
  327. - if ((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
  328. + if (ac && (ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
  329. return ret;
  330.  
  331. if (extension_flag) {
  332. - switch (ac->m4ac.object_type) {
  333. + switch (m4ac->object_type) {
  334. case AOT_ER_BSAC:
  335. skip_bits(gb, 5); // numOfSubFrame
  336. skip_bits(gb, 11); // layer_length
  337. @@ -454,42 +446,58 @@ static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb,
  338. /**
  339. * Decode audio specific configuration; reference: table 1.13.
  340. *
  341. + * @param ac pointer to AACContext, may be null
  342. + * @param avctx pointer to AVCCodecContext, used for logging
  343. + * @param m4ac pointer to MPEG4AudioConfig, used for parsing
  344. * @param data pointer to AVCodecContext extradata
  345. * @param data_size size of AVCCodecContext extradata
  346. *
  347. - * @return Returns error status. 0 - OK, !0 - error
  348. + * @return Returns error status or number of consumed bits. <0 - error
  349. */
  350. -static int decode_audio_specific_config(AACContext *ac, void *data,
  351. - int data_size)
  352. +static int decode_audio_specific_config(AACContext *ac,
  353. + AVCodecContext *avctx,
  354. + MPEG4AudioConfig *m4ac,
  355. + const uint8_t *data, int data_size, int asclen)
  356. {
  357. GetBitContext gb;
  358. int i;
  359.  
  360. + av_log(avctx, AV_LOG_DEBUG, "extradata size %d\n", avctx->extradata_size);
  361. + for (i = 0; i < avctx->extradata_size; i++)
  362. + av_log(avctx, AV_LOG_DEBUG, "%02x ", avctx->extradata[i]);
  363. + av_log(avctx, AV_LOG_DEBUG, "\n");
  364. +
  365. init_get_bits(&gb, data, data_size * 8);
  366.  
  367. - if ((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
  368. + if ((i = ff_mpeg4audio_get_config(m4ac, data, asclen/8)) < 0)
  369. return -1;
  370. - if (ac->m4ac.sampling_index > 12) {
  371. - av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
  372. + if (m4ac->sampling_index > 12) {
  373. + av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
  374. return -1;
  375. }
  376. - if (ac->m4ac.sbr == 1 && ac->m4ac.ps == -1)
  377. - ac->m4ac.ps = 1;
  378. + if (m4ac->sbr == 1 && m4ac->ps == -1)
  379. + m4ac->ps = 1;
  380.  
  381. skip_bits_long(&gb, i);
  382.  
  383. - switch (ac->m4ac.object_type) {
  384. + switch (m4ac->object_type) {
  385. case AOT_AAC_MAIN:
  386. case AOT_AAC_LC:
  387. - if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
  388. + case AOT_AAC_LTP:
  389. + if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
  390. return -1;
  391. break;
  392. default:
  393. - av_log(ac->avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
  394. - ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
  395. + av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
  396. + m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
  397. return -1;
  398. }
  399. - return 0;
  400. +
  401. + av_log(avctx, AV_LOG_DEBUG, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
  402. + m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
  403. + m4ac->sample_rate, m4ac->sbr, m4ac->ps);
  404. +
  405. + return get_bits_count(&gb);
  406. }
  407.  
  408. /**
  409. @@ -521,6 +529,22 @@ static void reset_all_predictors(PredictorState *ps)
  410. reset_predict_state(&ps[i]);
  411. }
  412.  
  413. +static int sample_rate_idx (int rate)
  414. +{
  415. + if (92017 <= rate) return 0;
  416. + else if (75132 <= rate) return 1;
  417. + else if (55426 <= rate) return 2;
  418. + else if (46009 <= rate) return 3;
  419. + else if (37566 <= rate) return 4;
  420. + else if (27713 <= rate) return 5;
  421. + else if (23004 <= rate) return 6;
  422. + else if (18783 <= rate) return 7;
  423. + else if (13856 <= rate) return 8;
  424. + else if (11502 <= rate) return 9;
  425. + else if (9391 <= rate) return 10;
  426. + else return 11;
  427. +}
  428. +
  429. static void reset_predictor_group(PredictorState *ps, int group_num)
  430. {
  431. int i;
  432. @@ -542,8 +566,31 @@ static av_cold int aac_decode_init(AVCodecContext *avctx)
  433. ac->m4ac.sample_rate = avctx->sample_rate;
  434.  
  435. if (avctx->extradata_size > 0) {
  436. - if (decode_audio_specific_config(ac, avctx->extradata, avctx->extradata_size))
  437. + if (decode_audio_specific_config(ac, ac->avctx, &ac->m4ac,
  438. + avctx->extradata,
  439. + avctx->extradata_size, 8*avctx->extradata_size) < 0)
  440. return -1;
  441. + } else {
  442. + int sr, i;
  443. + enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  444. +
  445. + sr = sample_rate_idx(avctx->sample_rate);
  446. + ac->m4ac.sampling_index = sr;
  447. + ac->m4ac.channels = avctx->channels;
  448. + ac->m4ac.sbr = -1;
  449. + ac->m4ac.ps = -1;
  450. +
  451. + for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
  452. + if (ff_mpeg4audio_channels[i] == avctx->channels)
  453. + break;
  454. + if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
  455. + i = 0;
  456. + }
  457. + ac->m4ac.chan_config = i;
  458. +
  459. + if (ac->m4ac.chan_config) {
  460. + set_default_channel_config(avctx, new_che_pos, ac->m4ac.chan_config);
  461. + }
  462. }
  463.  
  464. avctx->sample_fmt = SAMPLE_FMT_S16;
  465. @@ -563,23 +610,10 @@ static av_cold int aac_decode_init(AVCodecContext *avctx)
  466. ff_aac_sbr_init();
  467.  
  468. dsputil_init(&ac->dsp, avctx);
  469. +// ff_fmt_convert_init(&ac->fmt_conv, avctx);
  470.  
  471. ac->random_state = 0x1f2e3d4c;
  472.  
  473. - // -1024 - Compensate wrong IMDCT method.
  474. - // 32768 - Required to scale values to the correct range for the bias method
  475. - // for float to int16 conversion.
  476. -
  477. - if (ac->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
  478. - ac->add_bias = 385.0f;
  479. - ac->sf_scale = 1. / (-1024. * 32768.);
  480. - ac->sf_offset = 0;
  481. - } else {
  482. - ac->add_bias = 0.0f;
  483. - ac->sf_scale = 1. / -1024.;
  484. - ac->sf_offset = 60;
  485. - }
  486. -
  487. ff_aac_tableinit();
  488.  
  489. INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
  490. @@ -587,8 +621,9 @@ static av_cold int aac_decode_init(AVCodecContext *avctx)
  491. ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
  492. 352);
  493.  
  494. - ff_mdct_init(&ac->mdct, 11, 1, 1.0);
  495. - ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
  496. + ff_mdct_init(&ac->mdct, 11, 1, 1.0/1024.0);
  497. + ff_mdct_init(&ac->mdct_small, 8, 1, 1.0/128.0);
  498. + ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0);
  499. // window initialization
  500. ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
  501. ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
  502. @@ -638,6 +673,20 @@ static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
  503. }
  504.  
  505. /**
  506. + * Decode Long Term Prediction data; reference: table 4.xx.
  507. + */
  508. +static void decode_ltp(AACContext *ac, LongTermPrediction *ltp,
  509. + GetBitContext *gb, uint8_t max_sfb)
  510. +{
  511. + int sfb;
  512. +
  513. + ltp->lag = get_bits(gb, 11);
  514. + ltp->coef = ltp_coef[get_bits(gb, 3)];
  515. + for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
  516. + ltp->used[sfb] = get_bits1(gb);
  517. +}
  518. +
  519. +/**
  520. * Decode Individual Channel Stream info; reference: table 4.6.
  521. *
  522. * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
  523. @@ -691,9 +740,8 @@ static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
  524. memset(ics, 0, sizeof(IndividualChannelStream));
  525. return -1;
  526. } else {
  527. - av_log_missing_feature(ac->avctx, "Predictor bit set but LTP is", 1);
  528. - memset(ics, 0, sizeof(IndividualChannelStream));
  529. - return -1;
  530. + if ((ics->ltp.present = get_bits(gb, 1)))
  531. + decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
  532. }
  533. }
  534. }
  535. @@ -771,9 +819,9 @@ static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
  536. enum BandType band_type[120],
  537. int band_type_run_end[120])
  538. {
  539. - const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
  540. int g, i, idx = 0;
  541. - int offset[3] = { global_gain, global_gain - 90, 100 };
  542. + int offset[3] = { global_gain, global_gain - 90, 0 };
  543. + int clipped_offset;
  544. int noise_flag = 1;
  545. static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
  546. for (g = 0; g < ics->num_window_groups; g++) {
  547. @@ -785,12 +833,15 @@ static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
  548. } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
  549. for (; i < run_end; i++, idx++) {
  550. offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  551. - if (offset[2] > 255U) {
  552. - av_log(ac->avctx, AV_LOG_ERROR,
  553. - "%s (%d) out of range.\n", sf_str[2], offset[2]);
  554. - return -1;
  555. + clipped_offset = av_clip(offset[2], -155, 100);
  556. + if (offset[2] != clipped_offset) {
  557. +/* av_log_ask_for_sample(ac->avctx, "Intensity stereo "
  558. + "position clipped (%d -> %d).\nIf you heard an "
  559. + "audible artifact, there may be a bug in the "
  560. + "decoder. ", offset[2], clipped_offset);
  561. +*/
  562. }
  563. - sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
  564. + sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
  565. }
  566. } else if (band_type[idx] == NOISE_BT) {
  567. for (; i < run_end; i++, idx++) {
  568. @@ -798,12 +849,15 @@ static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
  569. offset[1] += get_bits(gb, 9) - 256;
  570. else
  571. offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
  572. - if (offset[1] > 255U) {
  573. - av_log(ac->avctx, AV_LOG_ERROR,
  574. - "%s (%d) out of range.\n", sf_str[1], offset[1]);
  575. - return -1;
  576. + clipped_offset = av_clip(offset[1], -100, 155);
  577. + if (offset[1] != clipped_offset) {
  578. +/* av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
  579. + "(%d -> %d).\nIf you heard an audible "
  580. + "artifact, there may be a bug in the decoder. ",
  581. + offset[1], clipped_offset);
  582. +*/
  583. }
  584. - sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
  585. + sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
  586. }
  587. } else {
  588. for (; i < run_end; i++, idx++) {
  589. @@ -813,7 +867,7 @@ static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
  590. "%s (%d) out of range.\n", sf_str[0], offset[0]);
  591. return -1;
  592. }
  593. - sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
  594. + sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
  595. }
  596. }
  597. }
  598. @@ -954,19 +1008,19 @@ static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
  599. union float754 s = { .f = *scale };
  600. union float754 t;
  601.  
  602. - t.i = s.i ^ (sign & 1<<31);
  603. + t.i = s.i ^ (sign & 1U<<31);
  604. *dst++ = v[idx & 3] * t.f;
  605.  
  606. sign <<= nz & 1; nz >>= 1;
  607. - t.i = s.i ^ (sign & 1<<31);
  608. + t.i = s.i ^ (sign & 1U<<31);
  609. *dst++ = v[idx>>2 & 3] * t.f;
  610.  
  611. sign <<= nz & 1; nz >>= 1;
  612. - t.i = s.i ^ (sign & 1<<31);
  613. + t.i = s.i ^ (sign & 1U<<31);
  614. *dst++ = v[idx>>4 & 3] * t.f;
  615.  
  616. sign <<= nz & 1; nz >>= 1;
  617. - t.i = s.i ^ (sign & 1<<31);
  618. + t.i = s.i ^ (sign & 1U<<31);
  619. *dst++ = v[idx>>6 & 3] * t.f;
  620.  
  621. return dst;
  622. @@ -1063,9 +1117,6 @@ static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
  623.  
  624. UPDATE_CACHE(re, gb);
  625. GET_VLC(code, re, gb, vlc_tab, 8, 2);
  626. -#if MIN_CACHE_BITS < 20
  627. - UPDATE_CACHE(re, gb);
  628. -#endif
  629. cb_idx = cb_vector_idx[code];
  630. nnz = cb_idx >> 8 & 15;
  631. bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
  632. @@ -1158,20 +1209,15 @@ static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
  633. return -1;
  634. }
  635.  
  636. -#if MIN_CACHE_BITS < 21
  637. - LAST_SKIP_BITS(re, gb, b + 1);
  638. - UPDATE_CACHE(re, gb);
  639. -#else
  640. SKIP_BITS(re, gb, b + 1);
  641. -#endif
  642. b += 4;
  643. n = (1 << b) + SHOW_UBITS(re, gb, b);
  644. LAST_SKIP_BITS(re, gb, b);
  645. - *icf++ = cbrt_tab[n] | (bits & 1<<31);
  646. + *icf++ = cbrt_tab[n] | (bits & 1U<<31);
  647. bits <<= 1;
  648. } else {
  649. unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
  650. - *icf++ = (bits & 1<<31) | v;
  651. + *icf++ = (bits & 1U<<31) | v;
  652. bits <<= !!v;
  653. }
  654. cb_idx >>= 4;
  655. @@ -1232,8 +1278,7 @@ static av_always_inline float flt16_trunc(float pf)
  656. }
  657.  
  658. static av_always_inline void predict(PredictorState *ps, float *coef,
  659. - float sf_scale, float inv_sf_scale,
  660. - int output_enable)
  661. + int output_enable)
  662. {
  663. const float a = 0.953125; // 61.0 / 64
  664. const float alpha = 0.90625; // 29.0 / 32
  665. @@ -1249,9 +1294,9 @@ static av_always_inline void predict(PredictorState *ps, float *coef,
  666.  
  667. pv = flt16_round(k1 * r0 + k2 * r1);
  668. if (output_enable)
  669. - *coef += pv * sf_scale;
  670. + *coef += pv;
  671.  
  672. - e0 = *coef * inv_sf_scale;
  673. + e0 = *coef;
  674. e1 = e0 - k1 * r0;
  675.  
  676. ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
  677. @@ -1269,7 +1314,6 @@ static av_always_inline void predict(PredictorState *ps, float *coef,
  678. static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
  679. {
  680. int sfb, k;
  681. - float sf_scale = ac->sf_scale, inv_sf_scale = 1 / ac->sf_scale;
  682.  
  683. if (!sce->ics.predictor_initialized) {
  684. reset_all_predictors(sce->predictor_state);
  685. @@ -1280,7 +1324,6 @@ static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
  686. for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
  687. for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
  688. predict(&sce->predictor_state[k], &sce->coeffs[k],
  689. - sf_scale, inv_sf_scale,
  690. sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
  691. }
  692. }
  693. @@ -1386,13 +1429,13 @@ static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
  694. * [1] mask is decoded from bitstream; [2] mask is all 1s;
  695. * [3] reserved for scalable AAC
  696. */
  697. -static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
  698. +static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
  699. {
  700. const IndividualChannelStream *ics = &cpe->ch[1].ics;
  701. SingleChannelElement *sce1 = &cpe->ch[1];
  702. float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
  703. const uint16_t *offsets = ics->swb_offset;
  704. - int g, group, i, k, idx = 0;
  705. + int g, group, i, idx = 0;
  706. int c;
  707. float scale;
  708. for (g = 0; g < ics->num_window_groups; g++) {
  709. @@ -1405,8 +1448,10 @@ static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
  710. c *= 1 - 2 * cpe->ms_mask[idx];
  711. scale = c * sce1->sf[idx];
  712. for (group = 0; group < ics->group_len[g]; group++)
  713. - for (k = offsets[i]; k < offsets[i + 1]; k++)
  714. - coef1[group * 128 + k] = scale * coef0[group * 128 + k];
  715. + ac->dsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
  716. + coef0 + group * 128 + offsets[i],
  717. + scale,
  718. + offsets[i + 1] - offsets[i]);
  719. }
  720. } else {
  721. int bt_run_end = sce1->band_type_run_end[idx];
  722. @@ -1435,6 +1480,9 @@ static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
  723. i = cpe->ch[1].ics.use_kb_window[0];
  724. cpe->ch[1].ics = cpe->ch[0].ics;
  725. cpe->ch[1].ics.use_kb_window[1] = i;
  726. + if (cpe->ch[1].ics.predictor_present && (ac->m4ac.object_type != AOT_AAC_MAIN))
  727. + if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
  728. + decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
  729. ms_present = get_bits(gb, 2);
  730. if (ms_present == 3) {
  731. av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
  732. @@ -1456,7 +1504,7 @@ static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
  733. }
  734. }
  735.  
  736. - apply_intensity_stereo(cpe, ms_present);
  737. + apply_intensity_stereo(ac, cpe, ms_present);
  738. return 0;
  739. }
  740.  
  741. @@ -1674,6 +1722,7 @@ static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
  742. int w, filt, m, i;
  743. int bottom, top, order, start, end, size, inc;
  744. float lpc[TNS_MAX_ORDER];
  745. + float tmp[TNS_MAX_ORDER];
  746.  
  747. for (w = 0; w < ics->num_windows; w++) {
  748. bottom = ics->num_swb;
  749. @@ -1699,18 +1748,129 @@ static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
  750. }
  751. start += w * 128;
  752.  
  753. - // ar filter
  754. - for (m = 0; m < size; m++, start += inc)
  755. - for (i = 1; i <= FFMIN(m, order); i++)
  756. - coef[start] -= coef[start - i * inc] * lpc[i - 1];
  757. + if (decode) {
  758. + // ar filter
  759. + for (m = 0; m < size; m++, start += inc)
  760. + for (i = 1; i <= FFMIN(m, order); i++)
  761. + coef[start] -= coef[start - i * inc] * lpc[i - 1];
  762. + } else {
  763. + // ma filter
  764. + for (m = 0; m < size; m++, start += inc) {
  765. + tmp[0] = coef[start];
  766. + for (i = 1; i <= FFMIN(m, order); i++)
  767. + coef[start] += tmp[i] * lpc[i - 1];
  768. + for (i = order; i > 0; i--)
  769. + tmp[i] = tmp[i - 1];
  770. + }
  771. + }
  772. }
  773. }
  774. }
  775.  
  776. +static void vector_fmul(float *dst, const float *src0, const float *src1, int len){
  777. + int i;
  778. + for(i=0; i<len; i++)
  779. + dst[i] = src0[i] * src1[i];
  780. +}
  781. +
  782. +/**
  783. + * Apply windowing and MDCT to obtain the spectral
  784. + * coefficient from the predicted sample by LTP.
  785. + */
  786. +static void windowing_and_mdct_ltp(AACContext *ac, float *out,
  787. + float *in, IndividualChannelStream *ics)
  788. +{
  789. + const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  790. + const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  791. + const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  792. + const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
  793. +
  794. + if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
  795. + vector_fmul(in, in, lwindow_prev, 1024);
  796. + } else {
  797. + memset(in, 0, 448 * sizeof(float));
  798. + vector_fmul(in + 448, in + 448, swindow_prev, 128);
  799. + }
  800. + if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
  801. + ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
  802. + } else {
  803. + ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
  804. + memset(in + 1024 + 576, 0, 448 * sizeof(float));
  805. + }
  806. + ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
  807. +}
  808. +
  809. +/**
  810. + * Apply the long term prediction
  811. + */
  812. +static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
  813. +{
  814. + const LongTermPrediction *ltp = &sce->ics.ltp;
  815. + const uint16_t *offsets = sce->ics.swb_offset;
  816. + int i, sfb;
  817. +
  818. + if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
  819. + float *predTime = sce->ret;
  820. + float *predFreq = ac->buf_mdct;
  821. + int16_t num_samples = 2048;
  822. +
  823. + if (ltp->lag < 1024)
  824. + num_samples = ltp->lag + 1024;
  825. + for (i = 0; i < num_samples; i++)
  826. + predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
  827. + memset(&predTime[i], 0, (2048 - i) * sizeof(float));
  828. +
  829. + windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
  830. +
  831. + if (sce->tns.present)
  832. + apply_tns(predFreq, &sce->tns, &sce->ics, 0);
  833. +
  834. + for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
  835. + if (ltp->used[sfb])
  836. + for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
  837. + sce->coeffs[i] += predFreq[i];
  838. + }
  839. +}
  840. +
  841. +/**
  842. + * Update the LTP buffer for next frame
  843. + */
  844. +static void update_ltp(AACContext *ac, SingleChannelElement *sce)
  845. +{
  846. + IndividualChannelStream *ics = &sce->ics;
  847. + float *saved = sce->saved;
  848. + float *saved_ltp = sce->coeffs;
  849. + const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
  850. + const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
  851. + int i;
  852. +
  853. + if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  854. + memcpy(saved_ltp, saved, 512 * sizeof(float));
  855. + memset(saved_ltp + 576, 0, 448 * sizeof(float));
  856. + ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  857. + for (i = 0; i < 64; i++)
  858. + saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
  859. + } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  860. + memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
  861. + memset(saved_ltp + 576, 0, 448 * sizeof(float));
  862. + ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
  863. + for (i = 0; i < 64; i++)
  864. + saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
  865. + } else { // LONG_STOP or ONLY_LONG
  866. + ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
  867. + for (i = 0; i < 512; i++)
  868. + saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
  869. + }
  870. +
  871. + memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
  872. + memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
  873. + memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
  874. +}
  875. +
  876. /**
  877. * Conduct IMDCT and windowing.
  878. */
  879. -static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float bias)
  880. +static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
  881. {
  882. IndividualChannelStream *ics = &sce->ics;
  883. float *in = sce->coeffs;
  884. @@ -1726,9 +1886,9 @@ static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float
  885. // imdct
  886. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  887. for (i = 0; i < 1024; i += 128)
  888. - ff_imdct_half(&ac->mdct_small, buf + i, in + i);
  889. + ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
  890. } else
  891. - ff_imdct_half(&ac->mdct, buf, in);
  892. + ac->mdct.imdct_half(&ac->mdct, buf, in);
  893.  
  894. /* window overlapping
  895. * NOTE: To simplify the overlapping code, all 'meaningless' short to long
  896. @@ -1738,32 +1898,29 @@ static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float
  897. */
  898. if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
  899. (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
  900. - ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, bias, 512);
  901. + ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 0.0f, 512);
  902. } else {
  903. - for (i = 0; i < 448; i++)
  904. - out[i] = saved[i] + bias;
  905. + memcpy( out, saved, 448 * sizeof(float));
  906.  
  907. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  908. - ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, bias, 64);
  909. - ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, bias, 64);
  910. - ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, bias, 64);
  911. - ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, bias, 64);
  912. - ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, bias, 64);
  913. + ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 0.0f, 64);
  914. + ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 0.0f, 64);
  915. + ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 0.0f, 64);
  916. + ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 0.0f, 64);
  917. + ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 0.0f, 64);
  918. memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
  919. } else {
  920. - ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, bias, 64);
  921. - for (i = 576; i < 1024; i++)
  922. - out[i] = buf[i-512] + bias;
  923. + ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 0.0f, 64);
  924. + memcpy( out + 576, buf + 64, 448 * sizeof(float));
  925. }
  926. }
  927.  
  928. // buffer update
  929. if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  930. - for (i = 0; i < 64; i++)
  931. - saved[i] = temp[64 + i] - bias;
  932. - ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
  933. - ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
  934. - ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
  935. + memcpy( saved, temp + 64, 64 * sizeof(float));
  936. + ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0.0f, 64);
  937. + ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0.0f, 64);
  938. + ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0.0f, 64);
  939. memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
  940. } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
  941. memcpy( saved, buf + 512, 448 * sizeof(float));
  942. @@ -1820,13 +1977,12 @@ static void apply_independent_coupling(AACContext *ac,
  943. {
  944. int i;
  945. const float gain = cce->coup.gain[index][0];
  946. - const float bias = ac->add_bias;
  947. const float *src = cce->ch[0].ret;
  948. float *dest = target->ret;
  949. const int len = 1024 << (ac->m4ac.sbr == 1);
  950.  
  951. for (i = 0; i < len; i++)
  952. - dest[i] += gain * (src[i] - bias);
  953. + dest[i] += gain * src[i];
  954. }
  955.  
  956. /**
  957. @@ -1870,13 +2026,20 @@ static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
  958. static void spectral_to_sample(AACContext *ac)
  959. {
  960. int i, type;
  961. - float imdct_bias = (ac->m4ac.sbr <= 0) ? ac->add_bias : 0.0f;
  962. for (type = 3; type >= 0; type--) {
  963. for (i = 0; i < MAX_ELEM_ID; i++) {
  964. ChannelElement *che = ac->che[type][i];
  965. if (che) {
  966. if (type <= TYPE_CPE)
  967. apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
  968. + if (ac->m4ac.object_type == AOT_AAC_LTP) {
  969. + if (che->ch[0].ics.predictor_present) {
  970. + if (che->ch[0].ics.ltp.present)
  971. + apply_ltp(ac, &che->ch[0]);
  972. + if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
  973. + apply_ltp(ac, &che->ch[1]);
  974. + }
  975. + }
  976. if (che->ch[0].tns.present)
  977. apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
  978. if (che->ch[1].tns.present)
  979. @@ -1884,9 +2047,13 @@ static void spectral_to_sample(AACContext *ac)
  980. if (type <= TYPE_CPE)
  981. apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
  982. if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
  983. - imdct_and_windowing(ac, &che->ch[0], imdct_bias);
  984. + imdct_and_windowing(ac, &che->ch[0]);
  985. + if (ac->m4ac.object_type == AOT_AAC_LTP)
  986. + update_ltp(ac, &che->ch[0]);
  987. if (type == TYPE_CPE) {
  988. - imdct_and_windowing(ac, &che->ch[1], imdct_bias);
  989. + imdct_and_windowing(ac, &che->ch[1]);
  990. + if (ac->m4ac.object_type == AOT_AAC_LTP)
  991. + update_ltp(ac, &che->ch[1]);
  992. }
  993. if (ac->m4ac.sbr > 0) {
  994. ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
  995. @@ -1906,24 +2073,25 @@ static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
  996.  
  997. size = ff_aac_parse_header(gb, &hdr_info);
  998. if (size > 0) {
  999. - if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
  1000. + if (hdr_info.chan_config) {
  1001. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  1002. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  1003. ac->m4ac.chan_config = hdr_info.chan_config;
  1004. - if (set_default_channel_config(ac, new_che_pos, hdr_info.chan_config))
  1005. + if (set_default_channel_config(ac->avctx, new_che_pos, hdr_info.chan_config))
  1006. return -7;
  1007. if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
  1008. return -7;
  1009. } else if (ac->output_configured != OC_LOCKED) {
  1010. + ac->m4ac.chan_config = 0;
  1011. ac->output_configured = OC_NONE;
  1012. }
  1013. if (ac->output_configured != OC_LOCKED) {
  1014. ac->m4ac.sbr = -1;
  1015. ac->m4ac.ps = -1;
  1016. + ac->m4ac.sample_rate = hdr_info.sample_rate;
  1017. + ac->m4ac.sampling_index = hdr_info.sampling_index;
  1018. + ac->m4ac.object_type = hdr_info.object_type;
  1019. }
  1020. - ac->m4ac.sample_rate = hdr_info.sample_rate;
  1021. - ac->m4ac.sampling_index = hdr_info.sampling_index;
  1022. - ac->m4ac.object_type = hdr_info.object_type;
  1023. if (!ac->avctx->sample_rate)
  1024. ac->avctx->sample_rate = hdr_info.sample_rate;
  1025. if (hdr_info.num_aac_frames == 1) {
  1026. @@ -1937,24 +2105,17 @@ static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
  1027. return size;
  1028. }
  1029.  
  1030. -static int aac_decode_frame(AVCodecContext *avctx, void *data,
  1031. - int *data_size, AVPacket *avpkt)
  1032. +static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
  1033. + int *data_size, GetBitContext *gb)
  1034. {
  1035. - const uint8_t *buf = avpkt->data;
  1036. - int buf_size = avpkt->size;
  1037. AACContext *ac = avctx->priv_data;
  1038. ChannelElement *che = NULL, *che_prev = NULL;
  1039. - GetBitContext gb;
  1040. enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
  1041. int err, elem_id, data_size_tmp;
  1042. - int buf_consumed;
  1043. - int samples = 0, multiplier;
  1044. - int buf_offset;
  1045. + int samples = 0, multiplier, audio_found = 0;
  1046.  
  1047. - init_get_bits(&gb, buf, buf_size * 8);
  1048. -
  1049. - if (show_bits(&gb, 12) == 0xfff) {
  1050. - if (parse_adts_frame_header(ac, &gb) < 0) {
  1051. + if (show_bits(gb, 12) == 0xfff) {
  1052. + if (parse_adts_frame_header(ac, gb) < 0) {
  1053. av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
  1054. return -1;
  1055. }
  1056. @@ -1964,10 +2125,10 @@ static int aac_decode_frame(AVCodecContext *avctx, void *data,
  1057. }
  1058. }
  1059.  
  1060. - memset(ac->tags_seen_this_frame, 0, sizeof(ac->tags_seen_this_frame));
  1061. + ac->tags_mapped = 0;
  1062. // parse
  1063. - while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
  1064. - elem_id = get_bits(&gb, 4);
  1065. + while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
  1066. + elem_id = get_bits(gb, 4);
  1067.  
  1068. if (elem_type < TYPE_DSE) {
  1069. if (!(che=get_che(ac, elem_type, elem_id))) {
  1070. @@ -1981,29 +2142,32 @@ static int aac_decode_frame(AVCodecContext *avctx, void *data,
  1071. switch (elem_type) {
  1072.  
  1073. case TYPE_SCE:
  1074. - err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
  1075. + err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  1076. + audio_found = 1;
  1077. break;
  1078.  
  1079. case TYPE_CPE:
  1080. - err = decode_cpe(ac, &gb, che);
  1081. + err = decode_cpe(ac, gb, che);
  1082. + audio_found = 1;
  1083. break;
  1084.  
  1085. case TYPE_CCE:
  1086. - err = decode_cce(ac, &gb, che);
  1087. + err = decode_cce(ac, gb, che);
  1088. break;
  1089.  
  1090. case TYPE_LFE:
  1091. - err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
  1092. + err = decode_ics(ac, &che->ch[0], gb, 0, 0);
  1093. + audio_found = 1;
  1094. break;
  1095.  
  1096. case TYPE_DSE:
  1097. - err = skip_data_stream_element(ac, &gb);
  1098. + err = skip_data_stream_element(ac, gb);
  1099. break;
  1100.  
  1101. case TYPE_PCE: {
  1102. enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
  1103. memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
  1104. - if ((err = decode_pce(ac, new_che_pos, &gb)))
  1105. + if ((err = decode_pce(avctx, &ac->m4ac, new_che_pos, gb)))
  1106. break;
  1107. if (ac->output_configured > OC_TRIAL_PCE)
  1108. av_log(avctx, AV_LOG_ERROR,
  1109. @@ -2015,13 +2179,13 @@ static int aac_decode_frame(AVCodecContext *avctx, void *data,
  1110.  
  1111. case TYPE_FIL:
  1112. if (elem_id == 15)
  1113. - elem_id += get_bits(&gb, 8) - 1;
  1114. - if (get_bits_left(&gb) < 8 * elem_id) {
  1115. + elem_id += get_bits(gb, 8) - 1;
  1116. + if (get_bits_left(gb) < 8 * elem_id) {
  1117. av_log(avctx, AV_LOG_ERROR, overread_err);
  1118. return -1;
  1119. }
  1120. while (elem_id > 0)
  1121. - elem_id -= decode_extension_payload(ac, &gb, elem_id, che_prev, elem_type_prev);
  1122. + elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
  1123. err = 0; /* FIXME */
  1124. break;
  1125.  
  1126. @@ -2036,7 +2200,7 @@ static int aac_decode_frame(AVCodecContext *avctx, void *data,
  1127. if (err)
  1128. return err;
  1129.  
  1130. - if (get_bits_left(&gb) < 3) {
  1131. + if (get_bits_left(gb) < 3) {
  1132. av_log(avctx, AV_LOG_ERROR, overread_err);
  1133. return -1;
  1134. }
  1135. @@ -2063,9 +2227,30 @@ static int aac_decode_frame(AVCodecContext *avctx, void *data,
  1136. if (samples)
  1137. ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels);
  1138.  
  1139. - if (ac->output_configured)
  1140. +// if (samples)
  1141. +// ac->fmt_conv.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels);
  1142. +
  1143. + if (ac->output_configured && audio_found)
  1144. ac->output_configured = OC_LOCKED;
  1145.  
  1146. + return 0;
  1147. +}
  1148. +
  1149. +static int aac_decode_frame(AVCodecContext *avctx, void *data,
  1150. + int *data_size, AVPacket *avpkt)
  1151. +{
  1152. + const uint8_t *buf = avpkt->data;
  1153. + int buf_size = avpkt->size;
  1154. + GetBitContext gb;
  1155. + int buf_consumed;
  1156. + int buf_offset;
  1157. + int err;
  1158. +
  1159. + init_get_bits(&gb, buf, buf_size * 8);
  1160. +
  1161. + if ((err = aac_decode_frame_int(avctx, data, data_size, &gb)) < 0)
  1162. + return err;
  1163. +
  1164. buf_consumed = (get_bits_count(&gb) + 7) >> 3;
  1165. for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
  1166. if (buf[buf_offset])
  1167. @@ -2089,9 +2274,270 @@ static av_cold int aac_decode_close(AVCodecContext *avctx)
  1168.  
  1169. ff_mdct_end(&ac->mdct);
  1170. ff_mdct_end(&ac->mdct_small);
  1171. + ff_mdct_end(&ac->mdct_ltp);
  1172. return 0;
  1173. }
  1174.  
  1175. +
  1176. +#define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
  1177. +
  1178. +struct LATMContext {
  1179. + AACContext aac_ctx; ///< containing AACContext
  1180. + int initialized; ///< initilized after a valid extradata was seen
  1181. +
  1182. + // parser data
  1183. + int audio_mux_version_A; ///< LATM syntax version
  1184. + int frame_length_type; ///< 0/1 variable/fixed frame length
  1185. + int frame_length; ///< frame length for fixed frame length
  1186. +};
  1187. +
  1188. +static inline uint32_t latm_get_value(GetBitContext *b)
  1189. +{
  1190. + int length = get_bits(b, 2);
  1191. +
  1192. + return get_bits_long(b, (length+1)*8);
  1193. +}
  1194. +
  1195. +static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
  1196. + GetBitContext *gb, int asclen)
  1197. +{
  1198. + AVCodecContext *avctx = latmctx->aac_ctx.avctx;
  1199. + MPEG4AudioConfig m4ac;
  1200. + AACContext *ac= &latmctx->aac_ctx;
  1201. + int config_start_bit = get_bits_count(gb);
  1202. + int bits_consumed, esize;
  1203. +
  1204. + if (config_start_bit % 8) {
  1205. + av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
  1206. + "config not byte aligned.\n", 1);
  1207. + return AVERROR_INVALIDDATA;
  1208. + } else {
  1209. + bits_consumed =
  1210. + decode_audio_specific_config(ac, avctx, &m4ac,
  1211. + gb->buffer + (config_start_bit / 8),
  1212. + get_bits_left(gb) / 8, asclen);
  1213. +
  1214. + if (bits_consumed < 0)
  1215. + return AVERROR_INVALIDDATA;
  1216. + ac->m4ac= m4ac;
  1217. +
  1218. + esize = (bits_consumed+7) / 8;
  1219. +
  1220. + if (avctx->extradata_size <= esize) {
  1221. + av_free(avctx->extradata);
  1222. + avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
  1223. + if (!avctx->extradata)
  1224. + return AVERROR(ENOMEM);
  1225. + }
  1226. +
  1227. + avctx->extradata_size = esize;
  1228. + memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
  1229. + memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  1230. +
  1231. + skip_bits_long(gb, bits_consumed);
  1232. + }
  1233. +
  1234. + return bits_consumed;
  1235. +}
  1236. +
  1237. +static int read_stream_mux_config(struct LATMContext *latmctx,
  1238. + GetBitContext *gb)
  1239. +{
  1240. + int ret, audio_mux_version = get_bits(gb, 1);
  1241. +
  1242. + latmctx->audio_mux_version_A = 0;
  1243. + if (audio_mux_version)
  1244. + latmctx->audio_mux_version_A = get_bits(gb, 1);
  1245. +
  1246. + if (!latmctx->audio_mux_version_A) {
  1247. +
  1248. + if (audio_mux_version)
  1249. + latm_get_value(gb); // taraFullness
  1250. +
  1251. + skip_bits(gb, 1); // allStreamSameTimeFraming
  1252. + skip_bits(gb, 6); // numSubFrames
  1253. + // numPrograms
  1254. + if (get_bits(gb, 4)) { // numPrograms
  1255. + av_log_missing_feature(latmctx->aac_ctx.avctx,
  1256. + "multiple programs are not supported\n", 1);
  1257. + return AVERROR_PATCHWELCOME;
  1258. + }
  1259. +
  1260. + // for each program (which there is only on in DVB)
  1261. +
  1262. + // for each layer (which there is only on in DVB)
  1263. + if (get_bits(gb, 3)) { // numLayer
  1264. + av_log_missing_feature(latmctx->aac_ctx.avctx,
  1265. + "multiple layers are not supported\n", 1);
  1266. + return AVERROR_PATCHWELCOME;
  1267. + }
  1268. +
  1269. + // for all but first stream: use_same_config = get_bits(gb, 1);
  1270. + if (!audio_mux_version) {
  1271. + if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
  1272. + return ret;
  1273. + } else {
  1274. + int ascLen = latm_get_value(gb);
  1275. + if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
  1276. + return ret;
  1277. + ascLen -= ret;
  1278. + skip_bits_long(gb, ascLen);
  1279. + }
  1280. +
  1281. + latmctx->frame_length_type = get_bits(gb, 3);
  1282. + switch (latmctx->frame_length_type) {
  1283. + case 0:
  1284. + skip_bits(gb, 8); // latmBufferFullness
  1285. + break;
  1286. + case 1:
  1287. + latmctx->frame_length = get_bits(gb, 9);
  1288. + break;
  1289. + case 3:
  1290. + case 4:
  1291. + case 5:
  1292. + skip_bits(gb, 6); // CELP frame length table index
  1293. + break;
  1294. + case 6:
  1295. + case 7:
  1296. + skip_bits(gb, 1); // HVXC frame length table index
  1297. + break;
  1298. + }
  1299. +
  1300. + if (get_bits(gb, 1)) { // other data
  1301. + if (audio_mux_version) {
  1302. + latm_get_value(gb); // other_data_bits
  1303. + } else {
  1304. + int esc;
  1305. + do {
  1306. + esc = get_bits(gb, 1);
  1307. + skip_bits(gb, 8);
  1308. + } while (esc);
  1309. + }
  1310. + }
  1311. +
  1312. + if (get_bits(gb, 1)) // crc present
  1313. + skip_bits(gb, 8); // config_crc
  1314. + }
  1315. +
  1316. + return 0;
  1317. +}
  1318. +
  1319. +static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
  1320. +{
  1321. + uint8_t tmp;
  1322. +
  1323. + if (ctx->frame_length_type == 0) {
  1324. + int mux_slot_length = 0;
  1325. + do {
  1326. + tmp = get_bits(gb, 8);
  1327. + mux_slot_length += tmp;
  1328. + } while (tmp == 255);
  1329. + return mux_slot_length;
  1330. + } else if (ctx->frame_length_type == 1) {
  1331. + return ctx->frame_length;
  1332. + } else if (ctx->frame_length_type == 3 ||
  1333. + ctx->frame_length_type == 5 ||
  1334. + ctx->frame_length_type == 7) {
  1335. + skip_bits(gb, 2); // mux_slot_length_coded
  1336. + }
  1337. + return 0;
  1338. +}
  1339. +
  1340. +static int read_audio_mux_element(struct LATMContext *latmctx,
  1341. + GetBitContext *gb)
  1342. +{
  1343. + int err;
  1344. + uint8_t use_same_mux = get_bits(gb, 1);
  1345. + if (!use_same_mux) {
  1346. + if ((err = read_stream_mux_config(latmctx, gb)) < 0)
  1347. + return err;
  1348. + } else if (!latmctx->aac_ctx.avctx->extradata) {
  1349. + av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
  1350. + "no decoder config found\n");
  1351. + return AVERROR(EAGAIN);
  1352. + }
  1353. + if (latmctx->audio_mux_version_A == 0) {
  1354. + int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
  1355. + if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
  1356. + av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
  1357. + return AVERROR_INVALIDDATA;
  1358. + } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
  1359. + av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  1360. + "frame length mismatch %d << %d\n",
  1361. + mux_slot_length_bytes * 8, get_bits_left(gb));
  1362. + return AVERROR_INVALIDDATA;
  1363. + }
  1364. + }
  1365. + return 0;
  1366. +}
  1367. +
  1368. +
  1369. +static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size,
  1370. + AVPacket *avpkt)
  1371. +{
  1372. + struct LATMContext *latmctx = avctx->priv_data;
  1373. + int muxlength, err;
  1374. + GetBitContext gb;
  1375. +
  1376. + if (avpkt->size == 0)
  1377. + return 0;
  1378. +
  1379. + init_get_bits(&gb, avpkt->data, avpkt->size * 8);
  1380. +
  1381. + // check for LOAS sync word
  1382. + if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
  1383. + return AVERROR_INVALIDDATA;
  1384. +
  1385. + muxlength = get_bits(&gb, 13) + 3;
  1386. + // not enough data, the parser should have sorted this
  1387. + if (muxlength > avpkt->size)
  1388. + return AVERROR_INVALIDDATA;
  1389. +
  1390. + if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
  1391. + return err;
  1392. +
  1393. + if (!latmctx->initialized) {
  1394. + if (!avctx->extradata) {
  1395. + *out_size = 0;
  1396. + return avpkt->size;
  1397. + } else {
  1398. + aac_decode_close(avctx);
  1399. + if ((err = aac_decode_init(avctx)) < 0)
  1400. + return err;
  1401. + latmctx->initialized = 1;
  1402. + }
  1403. + }
  1404. +
  1405. + if (show_bits(&gb, 12) == 0xfff) {
  1406. + av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
  1407. + "ADTS header detected, probably as result of configuration "
  1408. + "misparsing\n");
  1409. + return AVERROR_INVALIDDATA;
  1410. + }
  1411. +
  1412. + if ((err = aac_decode_frame_int(avctx, out, out_size, &gb)) < 0)
  1413. + return err;
  1414. +
  1415. + return muxlength;
  1416. +}
  1417. +
  1418. +av_cold static int latm_decode_init(AVCodecContext *avctx)
  1419. +{
  1420. + struct LATMContext *latmctx = avctx->priv_data;
  1421. + int ret;
  1422. +
  1423. + ret = aac_decode_init(avctx);
  1424. +
  1425. + if (avctx->extradata_size > 0) {
  1426. + latmctx->initialized = !ret;
  1427. + } else {
  1428. + latmctx->initialized = 0;
  1429. + }
  1430. +
  1431. + return ret;
  1432. +}
  1433. +
  1434. +
  1435. AVCodec aac_decoder = {
  1436. "aac",
  1437. AVMEDIA_TYPE_AUDIO,
  1438. @@ -2107,3 +2553,23 @@ AVCodec aac_decoder = {
  1439. },
  1440. .channel_layouts = aac_channel_layout,
  1441. };
  1442. +
  1443. +/*
  1444. + Note: This decoder filter is intended to decode LATM streams transferred
  1445. + in MPEG transport streams which only contain one program.
  1446. + To do a more complex LATM demuxing a separate LATM demuxer should be used.
  1447. +*/
  1448. +AVCodec aac_latm_decoder = {
  1449. + .name = "aac_latm",
  1450. + .type = AVMEDIA_TYPE_AUDIO,
  1451. + .id = CODEC_ID_AAC_LATM,
  1452. + .priv_data_size = sizeof(struct LATMContext),
  1453. + .init = latm_decode_init,
  1454. + .close = aac_decode_close,
  1455. + .decode = latm_decode_frame,
  1456. + .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"),
  1457. + .sample_fmts = (const enum SampleFormat[]) {
  1458. + SAMPLE_FMT_S16,SAMPLE_FMT_NONE
  1459. + },
  1460. + .channel_layouts = aac_channel_layout,
  1461. +};
  1462. diff --git a/mythtv/external/FFmpeg/libavcodec/aacdectab.h b/mythtv/external/FFmpeg/libavcodec/aacdectab.h
  1463. index b74f100..2759c29 100644
  1464. --- a/mythtv/external/FFmpeg/libavcodec/aacdectab.h
  1465. +++ b/mythtv/external/FFmpeg/libavcodec/aacdectab.h
  1466. @@ -34,6 +34,14 @@
  1467.  
  1468. #include <stdint.h>
  1469.  
  1470. +/* @name ltp_coef
  1471. + * Table of the LTP coefficient (multiplied by 2)
  1472. + */
  1473. +static const float ltp_coef[8] = {
  1474. + 1.141658, 1.393232, 1.626008, 1.822608,
  1475. + 1.969800, 2.135788, 2.2389202, 2.739066,
  1476. +};
  1477. +
  1478. /* @name tns_tmp2_map
  1479. * Tables of the tmp2[] arrays of LPC coefficients used for TNS.
  1480. * The suffix _M_N[] indicate the values of coef_compress and coef_res
  1481. diff --git a/mythtv/external/FFmpeg/libavcodec/aacsbr.c b/mythtv/external/FFmpeg/libavcodec/aacsbr.c
  1482. index 050305a..ec7ad3c 100644
  1483. --- a/mythtv/external/FFmpeg/libavcodec/aacsbr.c
  1484. +++ b/mythtv/external/FFmpeg/libavcodec/aacsbr.c
  1485. @@ -32,9 +32,11 @@
  1486. #include "aacsbrdata.h"
  1487. #include "fft.h"
  1488. #include "aacps.h"
  1489. +#include "libavutil/libm.h"
  1490.  
  1491. #include <stdint.h>
  1492. #include <float.h>
  1493. +#include <math.h>
  1494.  
  1495. #define ENVELOPE_ADJUSTMENT_OFFSET 2
  1496. #define NOISE_FLOOR_OFFSET 6.0f
  1497. @@ -127,11 +129,13 @@ av_cold void ff_aac_sbr_init(void)
  1498.  
  1499. av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr)
  1500. {
  1501. + if(sbr->mdct.mdct_bits)
  1502. + return;
  1503. sbr->kx[0] = sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
  1504. sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
  1505. sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
  1506. sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
  1507. - ff_mdct_init(&sbr->mdct, 7, 1, 1.0/64);
  1508. + ff_mdct_init(&sbr->mdct, 7, 1, 1.0 / 64.0);
  1509. ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0);
  1510. ff_ps_ctx_init(&sbr->ps);
  1511. }
  1512. @@ -1134,16 +1138,12 @@ static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
  1513. * @param W array of complex-valued samples split into subbands
  1514. */
  1515. static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, float *x,
  1516. - float z[320], float W[2][32][32][2],
  1517. - float scale)
  1518. + float z[320], float W[2][32][32][2])
  1519. {
  1520. int i, k;
  1521. memcpy(W[0], W[1], sizeof(W[0]));
  1522. memcpy(x , x+1024, (320-32)*sizeof(x[0]));
  1523. - if (scale != 1.0f)
  1524. - dsp->vector_fmul_scalar(x+288, in, scale, 1024);
  1525. - else
  1526. - memcpy(x+288, in, 1024*sizeof(*x));
  1527. + memcpy(x+288, in, 1024*sizeof(x[0]));
  1528. for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
  1529. // are not supported
  1530. dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
  1531. @@ -1159,7 +1159,7 @@ static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in,
  1532. }
  1533. z[64+63] = z[32];
  1534.  
  1535. - ff_imdct_half(mdct, z, z+64);
  1536. + mdct->imdct_half(mdct, z, z+64);
  1537. for (k = 0; k < 32; k++) {
  1538. W[1][i][k][0] = -z[63-k];
  1539. W[1][i][k][1] = z[k];
  1540. @@ -1175,12 +1175,10 @@ static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in,
  1541. static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
  1542. float *out, float X[2][38][64],
  1543. float mdct_buf[2][64],
  1544. - float *v0, int *v_off, const unsigned int div,
  1545. - float bias, float scale)
  1546. + float *v0, int *v_off, const unsigned int div)
  1547. {
  1548. int i, n;
  1549. const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
  1550. - int scale_and_bias = scale != 1.0f || bias != 0.0f;
  1551. float *v;
  1552. for (i = 0; i < 32; i++) {
  1553. if (*v_off == 0) {
  1554. @@ -1196,7 +1194,7 @@ static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
  1555. X[0][i][ n] = -X[0][i][n];
  1556. X[0][i][32+n] = X[1][i][31-n];
  1557. }
  1558. - ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
  1559. + mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
  1560. for (n = 0; n < 32; n++) {
  1561. v[ n] = mdct_buf[0][63 - 2*n];
  1562. v[63 - n] = -mdct_buf[0][62 - 2*n];
  1563. @@ -1205,8 +1203,8 @@ static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
  1564. for (n = 1; n < 64; n+=2) {
  1565. X[1][i][n] = -X[1][i][n];
  1566. }
  1567. - ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
  1568. - ff_imdct_half(mdct, mdct_buf[1], X[1][i]);
  1569. + mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
  1570. + mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
  1571. for (n = 0; n < 64; n++) {
  1572. v[ n] = -mdct_buf[0][63 - n] + mdct_buf[1][ n ];
  1573. v[127 - n] = mdct_buf[0][63 - n] + mdct_buf[1][ n ];
  1574. @@ -1222,9 +1220,6 @@ static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
  1575. dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
  1576. dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
  1577. dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
  1578. - if (scale_and_bias)
  1579. - for (n = 0; n < 64 >> div; n++)
  1580. - out[n] = out[n] * scale + bias;
  1581. out += 64 >> div;
  1582. }
  1583. }
  1584. @@ -1459,6 +1454,7 @@ static void sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
  1585. uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
  1586. int k;
  1587.  
  1588. + //av_assert0(sbr->kx[1] <= table[0]);
  1589. for (i = 0; i < ilim; i++)
  1590. for (m = table[i]; m < table[i + 1]; m++)
  1591. sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
  1592. @@ -1727,7 +1723,7 @@ void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
  1593. /* decode channel */
  1594. sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
  1595. (float*)sbr->qmf_filter_scratch,
  1596. - sbr->data[ch].W, 1/(-1024 * ac->sf_scale));
  1597. + sbr->data[ch].W);
  1598. sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W);
  1599. if (sbr->start) {
  1600. sbr_hf_inverse_filter(sbr->alpha0, sbr->alpha1, sbr->X_low, sbr->k[0]);
  1601. @@ -1760,12 +1756,10 @@ void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
  1602. sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, L, sbr->X[0], sbr->qmf_filter_scratch,
  1603. sbr->data[0].synthesis_filterbank_samples,
  1604. &sbr->data[0].synthesis_filterbank_samples_offset,
  1605. - downsampled,
  1606. - ac->add_bias, -1024 * ac->sf_scale);
  1607. + downsampled);
  1608. if (nch == 2)
  1609. sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, R, sbr->X[1], sbr->qmf_filter_scratch,
  1610. sbr->data[1].synthesis_filterbank_samples,
  1611. &sbr->data[1].synthesis_filterbank_samples_offset,
  1612. - downsampled,
  1613. - ac->add_bias, -1024 * ac->sf_scale);
  1614. + downsampled);
  1615. }
  1616.  
  1617.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement