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.

2078 lines
82KB

  1. /*
  2. * Windows Media Audio Voice decoder.
  3. * Copyright (c) 2009 Ronald S. Bultje
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * @brief Windows Media Audio Voice compatible decoder
  24. * @author Ronald S. Bultje <rsbultje@gmail.com>
  25. */
  26. #include <math.h>
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/float_dsp.h"
  29. #include "libavutil/mem.h"
  30. #include "avcodec.h"
  31. #include "bitstream.h"
  32. #include "internal.h"
  33. #include "put_bits.h"
  34. #include "wmavoice_data.h"
  35. #include "celp_filters.h"
  36. #include "acelp_vectors.h"
  37. #include "acelp_filters.h"
  38. #include "lsp.h"
  39. #include "dct.h"
  40. #include "rdft.h"
  41. #include "sinewin.h"
  42. #define MAX_BLOCKS 8 ///< maximum number of blocks per frame
  43. #define MAX_LSPS 16 ///< maximum filter order
  44. #define MAX_LSPS_ALIGN16 16 ///< same as #MAX_LSPS; needs to be multiple
  45. ///< of 16 for ASM input buffer alignment
  46. #define MAX_FRAMES 3 ///< maximum number of frames per superframe
  47. #define MAX_FRAMESIZE 160 ///< maximum number of samples per frame
  48. #define MAX_SIGNAL_HISTORY 416 ///< maximum excitation signal history
  49. #define MAX_SFRAMESIZE (MAX_FRAMESIZE * MAX_FRAMES)
  50. ///< maximum number of samples per superframe
  51. #define SFRAME_CACHE_MAXSIZE 256 ///< maximum cache size for frame data that
  52. ///< was split over two packets
  53. #define VLC_NBITS 6 ///< number of bits to read per VLC iteration
  54. /**
  55. * Frame type VLC coding.
  56. */
  57. static VLC frame_type_vlc;
  58. /**
  59. * Adaptive codebook types.
  60. */
  61. enum {
  62. ACB_TYPE_NONE = 0, ///< no adaptive codebook (only hardcoded fixed)
  63. ACB_TYPE_ASYMMETRIC = 1, ///< adaptive codebook with per-frame pitch, which
  64. ///< we interpolate to get a per-sample pitch.
  65. ///< Signal is generated using an asymmetric sinc
  66. ///< window function
  67. ///< @note see #wmavoice_ipol1_coeffs
  68. ACB_TYPE_HAMMING = 2 ///< Per-block pitch with signal generation using
  69. ///< a Hamming sinc window function
  70. ///< @note see #wmavoice_ipol2_coeffs
  71. };
  72. /**
  73. * Fixed codebook types.
  74. */
  75. enum {
  76. FCB_TYPE_SILENCE = 0, ///< comfort noise during silence
  77. ///< generated from a hardcoded (fixed) codebook
  78. ///< with per-frame (low) gain values
  79. FCB_TYPE_HARDCODED = 1, ///< hardcoded (fixed) codebook with per-block
  80. ///< gain values
  81. FCB_TYPE_AW_PULSES = 2, ///< Pitch-adaptive window (AW) pulse signals,
  82. ///< used in particular for low-bitrate streams
  83. FCB_TYPE_EXC_PULSES = 3, ///< Innovation (fixed) codebook pulse sets in
  84. ///< combinations of either single pulses or
  85. ///< pulse pairs
  86. };
  87. /**
  88. * Description of frame types.
  89. */
  90. static const struct frame_type_desc {
  91. uint8_t n_blocks; ///< amount of blocks per frame (each block
  92. ///< (contains 160/#n_blocks samples)
  93. uint8_t log_n_blocks; ///< log2(#n_blocks)
  94. uint8_t acb_type; ///< Adaptive codebook type (ACB_TYPE_*)
  95. uint8_t fcb_type; ///< Fixed codebook type (FCB_TYPE_*)
  96. uint8_t dbl_pulses; ///< how many pulse vectors have pulse pairs
  97. ///< (rather than just one single pulse)
  98. ///< only if #fcb_type == #FCB_TYPE_EXC_PULSES
  99. uint16_t frame_size; ///< the amount of bits that make up the block
  100. ///< data (per frame)
  101. } frame_descs[17] = {
  102. { 1, 0, ACB_TYPE_NONE, FCB_TYPE_SILENCE, 0, 0 },
  103. { 2, 1, ACB_TYPE_NONE, FCB_TYPE_HARDCODED, 0, 28 },
  104. { 2, 1, ACB_TYPE_ASYMMETRIC, FCB_TYPE_AW_PULSES, 0, 46 },
  105. { 2, 1, ACB_TYPE_ASYMMETRIC, FCB_TYPE_EXC_PULSES, 2, 80 },
  106. { 2, 1, ACB_TYPE_ASYMMETRIC, FCB_TYPE_EXC_PULSES, 5, 104 },
  107. { 4, 2, ACB_TYPE_ASYMMETRIC, FCB_TYPE_EXC_PULSES, 0, 108 },
  108. { 4, 2, ACB_TYPE_ASYMMETRIC, FCB_TYPE_EXC_PULSES, 2, 132 },
  109. { 4, 2, ACB_TYPE_ASYMMETRIC, FCB_TYPE_EXC_PULSES, 5, 168 },
  110. { 2, 1, ACB_TYPE_HAMMING, FCB_TYPE_EXC_PULSES, 0, 64 },
  111. { 2, 1, ACB_TYPE_HAMMING, FCB_TYPE_EXC_PULSES, 2, 80 },
  112. { 2, 1, ACB_TYPE_HAMMING, FCB_TYPE_EXC_PULSES, 5, 104 },
  113. { 4, 2, ACB_TYPE_HAMMING, FCB_TYPE_EXC_PULSES, 0, 108 },
  114. { 4, 2, ACB_TYPE_HAMMING, FCB_TYPE_EXC_PULSES, 2, 132 },
  115. { 4, 2, ACB_TYPE_HAMMING, FCB_TYPE_EXC_PULSES, 5, 168 },
  116. { 8, 3, ACB_TYPE_HAMMING, FCB_TYPE_EXC_PULSES, 0, 176 },
  117. { 8, 3, ACB_TYPE_HAMMING, FCB_TYPE_EXC_PULSES, 2, 208 },
  118. { 8, 3, ACB_TYPE_HAMMING, FCB_TYPE_EXC_PULSES, 5, 256 }
  119. };
  120. /**
  121. * WMA Voice decoding context.
  122. */
  123. typedef struct WMAVoiceContext {
  124. /**
  125. * @name Global values specified in the stream header / extradata or used all over.
  126. * @{
  127. */
  128. BitstreamContext bc; ///< packet bitreader. During decoder init,
  129. ///< it contains the extradata from the
  130. ///< demuxer. During decoding, it contains
  131. ///< packet data.
  132. int8_t vbm_tree[25]; ///< converts VLC codes to frame type
  133. int spillover_bitsize; ///< number of bits used to specify
  134. ///< #spillover_nbits in the packet header
  135. ///< = ceil(log2(ctx->block_align << 3))
  136. int history_nsamples; ///< number of samples in history for signal
  137. ///< prediction (through ACB)
  138. /* postfilter specific values */
  139. int do_apf; ///< whether to apply the averaged
  140. ///< projection filter (APF)
  141. int denoise_strength; ///< strength of denoising in Wiener filter
  142. ///< [0-11]
  143. int denoise_tilt_corr; ///< Whether to apply tilt correction to the
  144. ///< Wiener filter coefficients (postfilter)
  145. int dc_level; ///< Predicted amount of DC noise, based
  146. ///< on which a DC removal filter is used
  147. int lsps; ///< number of LSPs per frame [10 or 16]
  148. int lsp_q_mode; ///< defines quantizer defaults [0, 1]
  149. int lsp_def_mode; ///< defines different sets of LSP defaults
  150. ///< [0, 1]
  151. int frame_lsp_bitsize; ///< size (in bits) of LSPs, when encoded
  152. ///< per-frame (independent coding)
  153. int sframe_lsp_bitsize; ///< size (in bits) of LSPs, when encoded
  154. ///< per superframe (residual coding)
  155. int min_pitch_val; ///< base value for pitch parsing code
  156. int max_pitch_val; ///< max value + 1 for pitch parsing
  157. int pitch_nbits; ///< number of bits used to specify the
  158. ///< pitch value in the frame header
  159. int block_pitch_nbits; ///< number of bits used to specify the
  160. ///< first block's pitch value
  161. int block_pitch_range; ///< range of the block pitch
  162. int block_delta_pitch_nbits; ///< number of bits used to specify the
  163. ///< delta pitch between this and the last
  164. ///< block's pitch value, used in all but
  165. ///< first block
  166. int block_delta_pitch_hrange; ///< 1/2 range of the delta (full range is
  167. ///< from -this to +this-1)
  168. uint16_t block_conv_table[4]; ///< boundaries for block pitch unit/scale
  169. ///< conversion
  170. /**
  171. * @}
  172. *
  173. * @name Packet values specified in the packet header or related to a packet.
  174. *
  175. * A packet is considered to be a single unit of data provided to this
  176. * decoder by the demuxer.
  177. * @{
  178. */
  179. int spillover_nbits; ///< number of bits of the previous packet's
  180. ///< last superframe preceding this
  181. ///< packet's first full superframe (useful
  182. ///< for re-synchronization also)
  183. int has_residual_lsps; ///< if set, superframes contain one set of
  184. ///< LSPs that cover all frames, encoded as
  185. ///< independent and residual LSPs; if not
  186. ///< set, each frame contains its own, fully
  187. ///< independent, LSPs
  188. int skip_bits_next; ///< number of bits to skip at the next call
  189. ///< to #wmavoice_decode_packet() (since
  190. ///< they're part of the previous superframe)
  191. uint8_t sframe_cache[SFRAME_CACHE_MAXSIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  192. ///< cache for superframe data split over
  193. ///< multiple packets
  194. int sframe_cache_size; ///< set to >0 if we have data from an
  195. ///< (incomplete) superframe from a previous
  196. ///< packet that spilled over in the current
  197. ///< packet; specifies the amount of bits in
  198. ///< #sframe_cache
  199. PutBitContext pb; ///< bitstream writer for #sframe_cache
  200. /**
  201. * @}
  202. *
  203. * @name Frame and superframe values
  204. * Superframe and frame data - these can change from frame to frame,
  205. * although some of them do in that case serve as a cache / history for
  206. * the next frame or superframe.
  207. * @{
  208. */
  209. double prev_lsps[MAX_LSPS]; ///< LSPs of the last frame of the previous
  210. ///< superframe
  211. int last_pitch_val; ///< pitch value of the previous frame
  212. int last_acb_type; ///< frame type [0-2] of the previous frame
  213. int pitch_diff_sh16; ///< ((cur_pitch_val - #last_pitch_val)
  214. ///< << 16) / #MAX_FRAMESIZE
  215. float silence_gain; ///< set for use in blocks if #ACB_TYPE_NONE
  216. int aw_idx_is_ext; ///< whether the AW index was encoded in
  217. ///< 8 bits (instead of 6)
  218. int aw_pulse_range; ///< the range over which #aw_pulse_set1()
  219. ///< can apply the pulse, relative to the
  220. ///< value in aw_first_pulse_off. The exact
  221. ///< position of the first AW-pulse is within
  222. ///< [pulse_off, pulse_off + this], and
  223. ///< depends on bitstream values; [16 or 24]
  224. int aw_n_pulses[2]; ///< number of AW-pulses in each block; note
  225. ///< that this number can be negative (in
  226. ///< which case it basically means "zero")
  227. int aw_first_pulse_off[2]; ///< index of first sample to which to
  228. ///< apply AW-pulses, or -0xff if unset
  229. int aw_next_pulse_off_cache; ///< the position (relative to start of the
  230. ///< second block) at which pulses should
  231. ///< start to be positioned, serves as a
  232. ///< cache for pitch-adaptive window pulses
  233. ///< between blocks
  234. int frame_cntr; ///< current frame index [0 - 0xFFFE]; is
  235. ///< only used for comfort noise in #pRNG()
  236. float gain_pred_err[6]; ///< cache for gain prediction
  237. float excitation_history[MAX_SIGNAL_HISTORY];
  238. ///< cache of the signal of previous
  239. ///< superframes, used as a history for
  240. ///< signal generation
  241. float synth_history[MAX_LSPS]; ///< see #excitation_history
  242. /**
  243. * @}
  244. *
  245. * @name Postfilter values
  246. *
  247. * Variables used for postfilter implementation, mostly history for
  248. * smoothing and so on, and context variables for FFT/iFFT.
  249. * @{
  250. */
  251. RDFTContext rdft, irdft; ///< contexts for FFT-calculation in the
  252. ///< postfilter (for denoise filter)
  253. DCTContext dct, dst; ///< contexts for phase shift (in Hilbert
  254. ///< transform, part of postfilter)
  255. float sin[511], cos[511]; ///< 8-bit cosine/sine windows over [-pi,pi]
  256. ///< range
  257. float postfilter_agc; ///< gain control memory, used in
  258. ///< #adaptive_gain_control()
  259. float dcf_mem[2]; ///< DC filter history
  260. float zero_exc_pf[MAX_SIGNAL_HISTORY + MAX_SFRAMESIZE];
  261. ///< zero filter output (i.e. excitation)
  262. ///< by postfilter
  263. float denoise_filter_cache[MAX_FRAMESIZE];
  264. int denoise_filter_cache_size; ///< samples in #denoise_filter_cache
  265. DECLARE_ALIGNED(32, float, tilted_lpcs_pf)[0x80];
  266. ///< aligned buffer for LPC tilting
  267. DECLARE_ALIGNED(32, float, denoise_coeffs_pf)[0x80];
  268. ///< aligned buffer for denoise coefficients
  269. DECLARE_ALIGNED(32, float, synth_filter_out_buf)[0x80 + MAX_LSPS_ALIGN16];
  270. ///< aligned buffer for postfilter speech
  271. ///< synthesis
  272. /**
  273. * @}
  274. */
  275. } WMAVoiceContext;
  276. /**
  277. * Set up the variable bit mode (VBM) tree from container extradata.
  278. * @param bc bit I/O context.
  279. * The bit context (s->bc) should be loaded with byte 23-46 of the
  280. * container extradata (i.e. the ones containing the VBM tree).
  281. * @param vbm_tree pointer to array to which the decoded VBM tree will be
  282. * written.
  283. * @return 0 on success, <0 on error.
  284. */
  285. static av_cold int decode_vbmtree(BitstreamContext *bc, int8_t vbm_tree[25])
  286. {
  287. int cntr[8] = { 0 }, n, res;
  288. memset(vbm_tree, 0xff, sizeof(vbm_tree[0]) * 25);
  289. for (n = 0; n < 17; n++) {
  290. res = bitstream_read(bc, 3);
  291. if (cntr[res] > 3) // should be >= 3 + (res == 7))
  292. return -1;
  293. vbm_tree[res * 3 + cntr[res]++] = n;
  294. }
  295. return 0;
  296. }
  297. static av_cold void wmavoice_init_static_data(AVCodec *codec)
  298. {
  299. static const uint8_t bits[] = {
  300. 2, 2, 2, 4, 4, 4,
  301. 6, 6, 6, 8, 8, 8,
  302. 10, 10, 10, 12, 12, 12,
  303. 14, 14, 14, 14
  304. };
  305. static const uint16_t codes[] = {
  306. 0x0000, 0x0001, 0x0002, // 00/01/10
  307. 0x000c, 0x000d, 0x000e, // 11+00/01/10
  308. 0x003c, 0x003d, 0x003e, // 1111+00/01/10
  309. 0x00fc, 0x00fd, 0x00fe, // 111111+00/01/10
  310. 0x03fc, 0x03fd, 0x03fe, // 11111111+00/01/10
  311. 0x0ffc, 0x0ffd, 0x0ffe, // 1111111111+00/01/10
  312. 0x3ffc, 0x3ffd, 0x3ffe, 0x3fff // 111111111111+xx
  313. };
  314. INIT_VLC_STATIC(&frame_type_vlc, VLC_NBITS, sizeof(bits),
  315. bits, 1, 1, codes, 2, 2, 132);
  316. }
  317. /**
  318. * Set up decoder with parameters from demuxer (extradata etc.).
  319. */
  320. static av_cold int wmavoice_decode_init(AVCodecContext *ctx)
  321. {
  322. int n, flags, pitch_range, lsp16_flag;
  323. WMAVoiceContext *s = ctx->priv_data;
  324. /**
  325. * Extradata layout:
  326. * - byte 0-18: WMAPro-in-WMAVoice extradata (see wmaprodec.c),
  327. * - byte 19-22: flags field (annoyingly in LE; see below for known
  328. * values),
  329. * - byte 23-46: variable bitmode tree (really just 17 * 3 bits,
  330. * rest is 0).
  331. */
  332. if (ctx->extradata_size != 46) {
  333. av_log(ctx, AV_LOG_ERROR,
  334. "Invalid extradata size %d (should be 46)\n",
  335. ctx->extradata_size);
  336. return AVERROR_INVALIDDATA;
  337. }
  338. flags = AV_RL32(ctx->extradata + 18);
  339. s->spillover_bitsize = 3 + av_ceil_log2(ctx->block_align);
  340. s->do_apf = flags & 0x1;
  341. if (s->do_apf) {
  342. ff_rdft_init(&s->rdft, 7, DFT_R2C);
  343. ff_rdft_init(&s->irdft, 7, IDFT_C2R);
  344. ff_dct_init(&s->dct, 6, DCT_I);
  345. ff_dct_init(&s->dst, 6, DST_I);
  346. ff_sine_window_init(s->cos, 256);
  347. memcpy(&s->sin[255], s->cos, 256 * sizeof(s->cos[0]));
  348. for (n = 0; n < 255; n++) {
  349. s->sin[n] = -s->sin[510 - n];
  350. s->cos[510 - n] = s->cos[n];
  351. }
  352. }
  353. s->denoise_strength = (flags >> 2) & 0xF;
  354. if (s->denoise_strength >= 12) {
  355. av_log(ctx, AV_LOG_ERROR,
  356. "Invalid denoise filter strength %d (max=11)\n",
  357. s->denoise_strength);
  358. return AVERROR_INVALIDDATA;
  359. }
  360. s->denoise_tilt_corr = !!(flags & 0x40);
  361. s->dc_level = (flags >> 7) & 0xF;
  362. s->lsp_q_mode = !!(flags & 0x2000);
  363. s->lsp_def_mode = !!(flags & 0x4000);
  364. lsp16_flag = flags & 0x1000;
  365. if (lsp16_flag) {
  366. s->lsps = 16;
  367. s->frame_lsp_bitsize = 34;
  368. s->sframe_lsp_bitsize = 60;
  369. } else {
  370. s->lsps = 10;
  371. s->frame_lsp_bitsize = 24;
  372. s->sframe_lsp_bitsize = 48;
  373. }
  374. for (n = 0; n < s->lsps; n++)
  375. s->prev_lsps[n] = M_PI * (n + 1.0) / (s->lsps + 1.0);
  376. bitstream_init8(&s->bc, ctx->extradata + 22, ctx->extradata_size - 22);
  377. if (decode_vbmtree(&s->bc, s->vbm_tree) < 0) {
  378. av_log(ctx, AV_LOG_ERROR, "Invalid VBM tree; broken extradata?\n");
  379. return AVERROR_INVALIDDATA;
  380. }
  381. s->min_pitch_val = ((ctx->sample_rate << 8) / 400 + 50) >> 8;
  382. s->max_pitch_val = ((ctx->sample_rate << 8) * 37 / 2000 + 50) >> 8;
  383. pitch_range = s->max_pitch_val - s->min_pitch_val;
  384. if (pitch_range <= 0) {
  385. av_log(ctx, AV_LOG_ERROR, "Invalid pitch range; broken extradata?\n");
  386. return AVERROR_INVALIDDATA;
  387. }
  388. s->pitch_nbits = av_ceil_log2(pitch_range);
  389. s->last_pitch_val = 40;
  390. s->last_acb_type = ACB_TYPE_NONE;
  391. s->history_nsamples = s->max_pitch_val + 8;
  392. if (s->min_pitch_val < 1 || s->history_nsamples > MAX_SIGNAL_HISTORY) {
  393. int min_sr = ((((1 << 8) - 50) * 400) + 0xFF) >> 8,
  394. max_sr = ((((MAX_SIGNAL_HISTORY - 8) << 8) + 205) * 2000 / 37) >> 8;
  395. av_log(ctx, AV_LOG_ERROR,
  396. "Unsupported samplerate %d (min=%d, max=%d)\n",
  397. ctx->sample_rate, min_sr, max_sr); // 322-22097 Hz
  398. return AVERROR(ENOSYS);
  399. }
  400. s->block_conv_table[0] = s->min_pitch_val;
  401. s->block_conv_table[1] = (pitch_range * 25) >> 6;
  402. s->block_conv_table[2] = (pitch_range * 44) >> 6;
  403. s->block_conv_table[3] = s->max_pitch_val - 1;
  404. s->block_delta_pitch_hrange = (pitch_range >> 3) & ~0xF;
  405. if (s->block_delta_pitch_hrange <= 0) {
  406. av_log(ctx, AV_LOG_ERROR, "Invalid delta pitch hrange; broken extradata?\n");
  407. return AVERROR_INVALIDDATA;
  408. }
  409. s->block_delta_pitch_nbits = 1 + av_ceil_log2(s->block_delta_pitch_hrange);
  410. s->block_pitch_range = s->block_conv_table[2] +
  411. s->block_conv_table[3] + 1 +
  412. 2 * (s->block_conv_table[1] - 2 * s->min_pitch_val);
  413. s->block_pitch_nbits = av_ceil_log2(s->block_pitch_range);
  414. ctx->channels = 1;
  415. ctx->channel_layout = AV_CH_LAYOUT_MONO;
  416. ctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  417. return 0;
  418. }
  419. /**
  420. * @name Postfilter functions
  421. * Postfilter functions (gain control, wiener denoise filter, DC filter,
  422. * kalman smoothening, plus surrounding code to wrap it)
  423. * @{
  424. */
  425. /**
  426. * Adaptive gain control (as used in postfilter).
  427. *
  428. * Identical to #ff_adaptive_gain_control() in acelp_vectors.c, except
  429. * that the energy here is calculated using sum(abs(...)), whereas the
  430. * other codecs (e.g. AMR-NB, SIPRO) use sqrt(dotproduct(...)).
  431. *
  432. * @param out output buffer for filtered samples
  433. * @param in input buffer containing the samples as they are after the
  434. * postfilter steps so far
  435. * @param speech_synth input buffer containing speech synth before postfilter
  436. * @param size input buffer size
  437. * @param alpha exponential filter factor
  438. * @param gain_mem pointer to filter memory (single float)
  439. */
  440. static void adaptive_gain_control(float *out, const float *in,
  441. const float *speech_synth,
  442. int size, float alpha, float *gain_mem)
  443. {
  444. int i;
  445. float speech_energy = 0.0, postfilter_energy = 0.0, gain_scale_factor;
  446. float mem = *gain_mem;
  447. for (i = 0; i < size; i++) {
  448. speech_energy += fabsf(speech_synth[i]);
  449. postfilter_energy += fabsf(in[i]);
  450. }
  451. gain_scale_factor = (1.0 - alpha) * speech_energy / postfilter_energy;
  452. for (i = 0; i < size; i++) {
  453. mem = alpha * mem + gain_scale_factor;
  454. out[i] = in[i] * mem;
  455. }
  456. *gain_mem = mem;
  457. }
  458. /**
  459. * Kalman smoothing function.
  460. *
  461. * This function looks back pitch +/- 3 samples back into history to find
  462. * the best fitting curve (that one giving the optimal gain of the two
  463. * signals, i.e. the highest dot product between the two), and then
  464. * uses that signal history to smoothen the output of the speech synthesis
  465. * filter.
  466. *
  467. * @param s WMA Voice decoding context
  468. * @param pitch pitch of the speech signal
  469. * @param in input speech signal
  470. * @param out output pointer for smoothened signal
  471. * @param size input/output buffer size
  472. *
  473. * @returns -1 if no smoothening took place, e.g. because no optimal
  474. * fit could be found, or 0 on success.
  475. */
  476. static int kalman_smoothen(WMAVoiceContext *s, int pitch,
  477. const float *in, float *out, int size)
  478. {
  479. int n;
  480. float optimal_gain = 0, dot;
  481. const float *ptr = &in[-FFMAX(s->min_pitch_val, pitch - 3)],
  482. *end = &in[-FFMIN(s->max_pitch_val, pitch + 3)],
  483. *best_hist_ptr;
  484. /* find best fitting point in history */
  485. do {
  486. dot = avpriv_scalarproduct_float_c(in, ptr, size);
  487. if (dot > optimal_gain) {
  488. optimal_gain = dot;
  489. best_hist_ptr = ptr;
  490. }
  491. } while (--ptr >= end);
  492. if (optimal_gain <= 0)
  493. return -1;
  494. dot = avpriv_scalarproduct_float_c(best_hist_ptr, best_hist_ptr, size);
  495. if (dot <= 0) // would be 1.0
  496. return -1;
  497. if (optimal_gain <= dot) {
  498. dot = dot / (dot + 0.6 * optimal_gain); // 0.625-1.000
  499. } else
  500. dot = 0.625;
  501. /* actual smoothing */
  502. for (n = 0; n < size; n++)
  503. out[n] = best_hist_ptr[n] + dot * (in[n] - best_hist_ptr[n]);
  504. return 0;
  505. }
  506. /**
  507. * Get the tilt factor of a formant filter from its transfer function
  508. * @see #tilt_factor() in amrnbdec.c, which does essentially the same,
  509. * but somehow (??) it does a speech synthesis filter in the
  510. * middle, which is missing here
  511. *
  512. * @param lpcs LPC coefficients
  513. * @param n_lpcs Size of LPC buffer
  514. * @returns the tilt factor
  515. */
  516. static float tilt_factor(const float *lpcs, int n_lpcs)
  517. {
  518. float rh0, rh1;
  519. rh0 = 1.0 + avpriv_scalarproduct_float_c(lpcs, lpcs, n_lpcs);
  520. rh1 = lpcs[0] + avpriv_scalarproduct_float_c(lpcs, &lpcs[1], n_lpcs - 1);
  521. return rh1 / rh0;
  522. }
  523. /**
  524. * Derive denoise filter coefficients (in real domain) from the LPCs.
  525. */
  526. static void calc_input_response(WMAVoiceContext *s, float *lpcs,
  527. int fcb_type, float *coeffs, int remainder)
  528. {
  529. float last_coeff, min = 15.0, max = -15.0;
  530. float irange, angle_mul, gain_mul, range, sq;
  531. int n, idx;
  532. /* Create frequency power spectrum of speech input (i.e. RDFT of LPCs) */
  533. s->rdft.rdft_calc(&s->rdft, lpcs);
  534. #define log_range(var, assign) do { \
  535. float tmp = log10f(assign); var = tmp; \
  536. max = FFMAX(max, tmp); min = FFMIN(min, tmp); \
  537. } while (0)
  538. log_range(last_coeff, lpcs[1] * lpcs[1]);
  539. for (n = 1; n < 64; n++)
  540. log_range(lpcs[n], lpcs[n * 2] * lpcs[n * 2] +
  541. lpcs[n * 2 + 1] * lpcs[n * 2 + 1]);
  542. log_range(lpcs[0], lpcs[0] * lpcs[0]);
  543. #undef log_range
  544. range = max - min;
  545. lpcs[64] = last_coeff;
  546. /* Now, use this spectrum to pick out these frequencies with higher
  547. * (relative) power/energy (which we then take to be "not noise"),
  548. * and set up a table (still in lpc[]) of (relative) gains per frequency.
  549. * These frequencies will be maintained, while others ("noise") will be
  550. * decreased in the filter output. */
  551. irange = 64.0 / range; // so irange*(max-value) is in the range [0, 63]
  552. gain_mul = range * (fcb_type == FCB_TYPE_HARDCODED ? (5.0 / 13.0) :
  553. (5.0 / 14.7));
  554. angle_mul = gain_mul * (8.0 * M_LN10 / M_PI);
  555. for (n = 0; n <= 64; n++) {
  556. float pwr;
  557. idx = FFMAX(0, lrint((max - lpcs[n]) * irange) - 1);
  558. pwr = wmavoice_denoise_power_table[s->denoise_strength][idx];
  559. lpcs[n] = angle_mul * pwr;
  560. /* 70.57 =~ 1/log10(1.0331663) */
  561. idx = (pwr * gain_mul - 0.0295) * 70.570526123;
  562. if (idx > 127) { // fall back if index falls outside table range
  563. coeffs[n] = wmavoice_energy_table[127] *
  564. powf(1.0331663, idx - 127);
  565. } else
  566. coeffs[n] = wmavoice_energy_table[FFMAX(0, idx)];
  567. }
  568. /* calculate the Hilbert transform of the gains, which we do (since this
  569. * is a sine input) by doing a phase shift (in theory, H(sin())=cos()).
  570. * Hilbert_Transform(RDFT(x)) = Laplace_Transform(x), which calculates the
  571. * "moment" of the LPCs in this filter. */
  572. s->dct.dct_calc(&s->dct, lpcs);
  573. s->dst.dct_calc(&s->dst, lpcs);
  574. /* Split out the coefficient indexes into phase/magnitude pairs */
  575. idx = 255 + av_clip(lpcs[64], -255, 255);
  576. coeffs[0] = coeffs[0] * s->cos[idx];
  577. idx = 255 + av_clip(lpcs[64] - 2 * lpcs[63], -255, 255);
  578. last_coeff = coeffs[64] * s->cos[idx];
  579. for (n = 63;; n--) {
  580. idx = 255 + av_clip(-lpcs[64] - 2 * lpcs[n - 1], -255, 255);
  581. coeffs[n * 2 + 1] = coeffs[n] * s->sin[idx];
  582. coeffs[n * 2] = coeffs[n] * s->cos[idx];
  583. if (!--n) break;
  584. idx = 255 + av_clip( lpcs[64] - 2 * lpcs[n - 1], -255, 255);
  585. coeffs[n * 2 + 1] = coeffs[n] * s->sin[idx];
  586. coeffs[n * 2] = coeffs[n] * s->cos[idx];
  587. }
  588. coeffs[1] = last_coeff;
  589. /* move into real domain */
  590. s->irdft.rdft_calc(&s->irdft, coeffs);
  591. /* tilt correction and normalize scale */
  592. memset(&coeffs[remainder], 0, sizeof(coeffs[0]) * (128 - remainder));
  593. if (s->denoise_tilt_corr) {
  594. float tilt_mem = 0;
  595. coeffs[remainder - 1] = 0;
  596. ff_tilt_compensation(&tilt_mem,
  597. -1.8 * tilt_factor(coeffs, remainder - 1),
  598. coeffs, remainder);
  599. }
  600. sq = (1.0 / 64.0) * sqrtf(1 / avpriv_scalarproduct_float_c(coeffs, coeffs,
  601. remainder));
  602. for (n = 0; n < remainder; n++)
  603. coeffs[n] *= sq;
  604. }
  605. /**
  606. * This function applies a Wiener filter on the (noisy) speech signal as
  607. * a means to denoise it.
  608. *
  609. * - take RDFT of LPCs to get the power spectrum of the noise + speech;
  610. * - using this power spectrum, calculate (for each frequency) the Wiener
  611. * filter gain, which depends on the frequency power and desired level
  612. * of noise subtraction (when set too high, this leads to artifacts)
  613. * We can do this symmetrically over the X-axis (so 0-4kHz is the inverse
  614. * of 4-8kHz);
  615. * - by doing a phase shift, calculate the Hilbert transform of this array
  616. * of per-frequency filter-gains to get the filtering coefficients;
  617. * - smoothen/normalize/de-tilt these filter coefficients as desired;
  618. * - take RDFT of noisy sound, apply the coefficients and take its IRDFT
  619. * to get the denoised speech signal;
  620. * - the leftover (i.e. output of the IRDFT on denoised speech data beyond
  621. * the frame boundary) are saved and applied to subsequent frames by an
  622. * overlap-add method (otherwise you get clicking-artifacts).
  623. *
  624. * @param s WMA Voice decoding context
  625. * @param fcb_type Frame (codebook) type
  626. * @param synth_pf input: the noisy speech signal, output: denoised speech
  627. * data; should be 16-byte aligned (for ASM purposes)
  628. * @param size size of the speech data
  629. * @param lpcs LPCs used to synthesize this frame's speech data
  630. */
  631. static void wiener_denoise(WMAVoiceContext *s, int fcb_type,
  632. float *synth_pf, int size,
  633. const float *lpcs)
  634. {
  635. int remainder, lim, n;
  636. if (fcb_type != FCB_TYPE_SILENCE) {
  637. float *tilted_lpcs = s->tilted_lpcs_pf,
  638. *coeffs = s->denoise_coeffs_pf, tilt_mem = 0;
  639. tilted_lpcs[0] = 1.0;
  640. memcpy(&tilted_lpcs[1], lpcs, sizeof(lpcs[0]) * s->lsps);
  641. memset(&tilted_lpcs[s->lsps + 1], 0,
  642. sizeof(tilted_lpcs[0]) * (128 - s->lsps - 1));
  643. ff_tilt_compensation(&tilt_mem, 0.7 * tilt_factor(lpcs, s->lsps),
  644. tilted_lpcs, s->lsps + 2);
  645. /* The IRDFT output (127 samples for 7-bit filter) beyond the frame
  646. * size is applied to the next frame. All input beyond this is zero,
  647. * and thus all output beyond this will go towards zero, hence we can
  648. * limit to min(size-1, 127-size) as a performance consideration. */
  649. remainder = FFMIN(127 - size, size - 1);
  650. calc_input_response(s, tilted_lpcs, fcb_type, coeffs, remainder);
  651. /* apply coefficients (in frequency spectrum domain), i.e. complex
  652. * number multiplication */
  653. memset(&synth_pf[size], 0, sizeof(synth_pf[0]) * (128 - size));
  654. s->rdft.rdft_calc(&s->rdft, synth_pf);
  655. s->rdft.rdft_calc(&s->rdft, coeffs);
  656. synth_pf[0] *= coeffs[0];
  657. synth_pf[1] *= coeffs[1];
  658. for (n = 1; n < 64; n++) {
  659. float v1 = synth_pf[n * 2], v2 = synth_pf[n * 2 + 1];
  660. synth_pf[n * 2] = v1 * coeffs[n * 2] - v2 * coeffs[n * 2 + 1];
  661. synth_pf[n * 2 + 1] = v2 * coeffs[n * 2] + v1 * coeffs[n * 2 + 1];
  662. }
  663. s->irdft.rdft_calc(&s->irdft, synth_pf);
  664. }
  665. /* merge filter output with the history of previous runs */
  666. if (s->denoise_filter_cache_size) {
  667. lim = FFMIN(s->denoise_filter_cache_size, size);
  668. for (n = 0; n < lim; n++)
  669. synth_pf[n] += s->denoise_filter_cache[n];
  670. s->denoise_filter_cache_size -= lim;
  671. memmove(s->denoise_filter_cache, &s->denoise_filter_cache[size],
  672. sizeof(s->denoise_filter_cache[0]) * s->denoise_filter_cache_size);
  673. }
  674. /* move remainder of filter output into a cache for future runs */
  675. if (fcb_type != FCB_TYPE_SILENCE) {
  676. lim = FFMIN(remainder, s->denoise_filter_cache_size);
  677. for (n = 0; n < lim; n++)
  678. s->denoise_filter_cache[n] += synth_pf[size + n];
  679. if (lim < remainder) {
  680. memcpy(&s->denoise_filter_cache[lim], &synth_pf[size + lim],
  681. sizeof(s->denoise_filter_cache[0]) * (remainder - lim));
  682. s->denoise_filter_cache_size = remainder;
  683. }
  684. }
  685. }
  686. /**
  687. * Averaging projection filter, the postfilter used in WMAVoice.
  688. *
  689. * This uses the following steps:
  690. * - A zero-synthesis filter (generate excitation from synth signal)
  691. * - Kalman smoothing on excitation, based on pitch
  692. * - Re-synthesized smoothened output
  693. * - Iterative Wiener denoise filter
  694. * - Adaptive gain filter
  695. * - DC filter
  696. *
  697. * @param s WMAVoice decoding context
  698. * @param synth Speech synthesis output (before postfilter)
  699. * @param samples Output buffer for filtered samples
  700. * @param size Buffer size of synth & samples
  701. * @param lpcs Generated LPCs used for speech synthesis
  702. * @param zero_exc_pf destination for zero synthesis filter (16-byte aligned)
  703. * @param fcb_type Frame type (silence, hardcoded, AW-pulses or FCB-pulses)
  704. * @param pitch Pitch of the input signal
  705. */
  706. static void postfilter(WMAVoiceContext *s, const float *synth,
  707. float *samples, int size,
  708. const float *lpcs, float *zero_exc_pf,
  709. int fcb_type, int pitch)
  710. {
  711. float synth_filter_in_buf[MAX_FRAMESIZE / 2],
  712. *synth_pf = &s->synth_filter_out_buf[MAX_LSPS_ALIGN16],
  713. *synth_filter_in = zero_exc_pf;
  714. assert(size <= MAX_FRAMESIZE / 2);
  715. /* generate excitation from input signal */
  716. ff_celp_lp_zero_synthesis_filterf(zero_exc_pf, lpcs, synth, size, s->lsps);
  717. if (fcb_type >= FCB_TYPE_AW_PULSES &&
  718. !kalman_smoothen(s, pitch, zero_exc_pf, synth_filter_in_buf, size))
  719. synth_filter_in = synth_filter_in_buf;
  720. /* re-synthesize speech after smoothening, and keep history */
  721. ff_celp_lp_synthesis_filterf(synth_pf, lpcs,
  722. synth_filter_in, size, s->lsps);
  723. memcpy(&synth_pf[-s->lsps], &synth_pf[size - s->lsps],
  724. sizeof(synth_pf[0]) * s->lsps);
  725. wiener_denoise(s, fcb_type, synth_pf, size, lpcs);
  726. adaptive_gain_control(samples, synth_pf, synth, size, 0.99,
  727. &s->postfilter_agc);
  728. if (s->dc_level > 8) {
  729. /* remove ultra-low frequency DC noise / highpass filter;
  730. * coefficients are identical to those used in SIPR decoding,
  731. * and very closely resemble those used in AMR-NB decoding. */
  732. ff_acelp_apply_order_2_transfer_function(samples, samples,
  733. (const float[2]) { -1.99997, 1.0 },
  734. (const float[2]) { -1.9330735188, 0.93589198496 },
  735. 0.93980580475, s->dcf_mem, size);
  736. }
  737. }
  738. /**
  739. * @}
  740. */
  741. /**
  742. * Dequantize LSPs
  743. * @param lsps output pointer to the array that will hold the LSPs
  744. * @param num number of LSPs to be dequantized
  745. * @param values quantized values, contains n_stages values
  746. * @param sizes range (i.e. max value) of each quantized value
  747. * @param n_stages number of dequantization runs
  748. * @param table dequantization table to be used
  749. * @param mul_q LSF multiplier
  750. * @param base_q base (lowest) LSF values
  751. */
  752. static void dequant_lsps(double *lsps, int num,
  753. const uint16_t *values,
  754. const uint16_t *sizes,
  755. int n_stages, const uint8_t *table,
  756. const double *mul_q,
  757. const double *base_q)
  758. {
  759. int n, m;
  760. memset(lsps, 0, num * sizeof(*lsps));
  761. for (n = 0; n < n_stages; n++) {
  762. const uint8_t *t_off = &table[values[n] * num];
  763. double base = base_q[n], mul = mul_q[n];
  764. for (m = 0; m < num; m++)
  765. lsps[m] += base + mul * t_off[m];
  766. table += sizes[n] * num;
  767. }
  768. }
  769. /**
  770. * @name LSP dequantization routines
  771. * LSP dequantization routines, for 10/16LSPs and independent/residual coding.
  772. * @note we assume enough bits are available, caller should check.
  773. * lsp10i() consumes 24 bits; lsp10r() consumes an additional 24 bits;
  774. * lsp16i() consumes 34 bits; lsp16r() consumes an additional 26 bits.
  775. * @{
  776. */
  777. /**
  778. * Parse 10 independently-coded LSPs.
  779. */
  780. static void dequant_lsp10i(BitstreamContext *bc, double *lsps)
  781. {
  782. static const uint16_t vec_sizes[4] = { 256, 64, 32, 32 };
  783. static const double mul_lsf[4] = {
  784. 5.2187144800e-3, 1.4626986422e-3,
  785. 9.6179549166e-4, 1.1325736225e-3
  786. };
  787. static const double base_lsf[4] = {
  788. M_PI * -2.15522e-1, M_PI * -6.1646e-2,
  789. M_PI * -3.3486e-2, M_PI * -5.7408e-2
  790. };
  791. uint16_t v[4];
  792. v[0] = bitstream_read(bc, 8);
  793. v[1] = bitstream_read(bc, 6);
  794. v[2] = bitstream_read(bc, 5);
  795. v[3] = bitstream_read(bc, 5);
  796. dequant_lsps(lsps, 10, v, vec_sizes, 4, wmavoice_dq_lsp10i,
  797. mul_lsf, base_lsf);
  798. }
  799. /**
  800. * Parse 10 independently-coded LSPs, and then derive the tables to
  801. * generate LSPs for the other frames from them (residual coding).
  802. */
  803. static void dequant_lsp10r(BitstreamContext *bc,
  804. double *i_lsps, const double *old,
  805. double *a1, double *a2, int q_mode)
  806. {
  807. static const uint16_t vec_sizes[3] = { 128, 64, 64 };
  808. static const double mul_lsf[3] = {
  809. 2.5807601174e-3, 1.2354460219e-3, 1.1763821673e-3
  810. };
  811. static const double base_lsf[3] = {
  812. M_PI * -1.07448e-1, M_PI * -5.2706e-2, M_PI * -5.1634e-2
  813. };
  814. const float (*ipol_tab)[2][10] = q_mode ?
  815. wmavoice_lsp10_intercoeff_b : wmavoice_lsp10_intercoeff_a;
  816. uint16_t interpol, v[3];
  817. int n;
  818. dequant_lsp10i(bc, i_lsps);
  819. interpol = bitstream_read(bc, 5);
  820. v[0] = bitstream_read(bc, 7);
  821. v[1] = bitstream_read(bc, 6);
  822. v[2] = bitstream_read(bc, 6);
  823. for (n = 0; n < 10; n++) {
  824. double delta = old[n] - i_lsps[n];
  825. a1[n] = ipol_tab[interpol][0][n] * delta + i_lsps[n];
  826. a1[10 + n] = ipol_tab[interpol][1][n] * delta + i_lsps[n];
  827. }
  828. dequant_lsps(a2, 20, v, vec_sizes, 3, wmavoice_dq_lsp10r,
  829. mul_lsf, base_lsf);
  830. }
  831. /**
  832. * Parse 16 independently-coded LSPs.
  833. */
  834. static void dequant_lsp16i(BitstreamContext *bc, double *lsps)
  835. {
  836. static const uint16_t vec_sizes[5] = { 256, 64, 128, 64, 128 };
  837. static const double mul_lsf[5] = {
  838. 3.3439586280e-3, 6.9908173703e-4,
  839. 3.3216608306e-3, 1.0334960326e-3,
  840. 3.1899104283e-3
  841. };
  842. static const double base_lsf[5] = {
  843. M_PI * -1.27576e-1, M_PI * -2.4292e-2,
  844. M_PI * -1.28094e-1, M_PI * -3.2128e-2,
  845. M_PI * -1.29816e-1
  846. };
  847. uint16_t v[5];
  848. v[0] = bitstream_read(bc, 8);
  849. v[1] = bitstream_read(bc, 6);
  850. v[2] = bitstream_read(bc, 7);
  851. v[3] = bitstream_read(bc, 6);
  852. v[4] = bitstream_read(bc, 7);
  853. dequant_lsps( lsps, 5, v, vec_sizes, 2,
  854. wmavoice_dq_lsp16i1, mul_lsf, base_lsf);
  855. dequant_lsps(&lsps[5], 5, &v[2], &vec_sizes[2], 2,
  856. wmavoice_dq_lsp16i2, &mul_lsf[2], &base_lsf[2]);
  857. dequant_lsps(&lsps[10], 6, &v[4], &vec_sizes[4], 1,
  858. wmavoice_dq_lsp16i3, &mul_lsf[4], &base_lsf[4]);
  859. }
  860. /**
  861. * Parse 16 independently-coded LSPs, and then derive the tables to
  862. * generate LSPs for the other frames from them (residual coding).
  863. */
  864. static void dequant_lsp16r(BitstreamContext *bc,
  865. double *i_lsps, const double *old,
  866. double *a1, double *a2, int q_mode)
  867. {
  868. static const uint16_t vec_sizes[3] = { 128, 128, 128 };
  869. static const double mul_lsf[3] = {
  870. 1.2232979501e-3, 1.4062241527e-3, 1.6114744851e-3
  871. };
  872. static const double base_lsf[3] = {
  873. M_PI * -5.5830e-2, M_PI * -5.2908e-2, M_PI * -5.4776e-2
  874. };
  875. const float (*ipol_tab)[2][16] = q_mode ?
  876. wmavoice_lsp16_intercoeff_b : wmavoice_lsp16_intercoeff_a;
  877. uint16_t interpol, v[3];
  878. int n;
  879. dequant_lsp16i(bc, i_lsps);
  880. interpol = bitstream_read(bc, 5);
  881. v[0] = bitstream_read(bc, 7);
  882. v[1] = bitstream_read(bc, 7);
  883. v[2] = bitstream_read(bc, 7);
  884. for (n = 0; n < 16; n++) {
  885. double delta = old[n] - i_lsps[n];
  886. a1[n] = ipol_tab[interpol][0][n] * delta + i_lsps[n];
  887. a1[16 + n] = ipol_tab[interpol][1][n] * delta + i_lsps[n];
  888. }
  889. dequant_lsps( a2, 10, v, vec_sizes, 1,
  890. wmavoice_dq_lsp16r1, mul_lsf, base_lsf);
  891. dequant_lsps(&a2[10], 10, &v[1], &vec_sizes[1], 1,
  892. wmavoice_dq_lsp16r2, &mul_lsf[1], &base_lsf[1]);
  893. dequant_lsps(&a2[20], 12, &v[2], &vec_sizes[2], 1,
  894. wmavoice_dq_lsp16r3, &mul_lsf[2], &base_lsf[2]);
  895. }
  896. /**
  897. * @}
  898. * @name Pitch-adaptive window coding functions
  899. * The next few functions are for pitch-adaptive window coding.
  900. * @{
  901. */
  902. /**
  903. * Parse the offset of the first pitch-adaptive window pulses, and
  904. * the distribution of pulses between the two blocks in this frame.
  905. * @param s WMA Voice decoding context private data
  906. * @param bc bit I/O context
  907. * @param pitch pitch for each block in this frame
  908. */
  909. static void aw_parse_coords(WMAVoiceContext *s, BitstreamContext *bc,
  910. const int *pitch)
  911. {
  912. static const int16_t start_offset[94] = {
  913. -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11,
  914. 13, 15, 18, 17, 19, 20, 21, 22, 23, 24, 25, 26,
  915. 27, 28, 29, 30, 31, 32, 33, 35, 37, 39, 41, 43,
  916. 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67,
  917. 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91,
  918. 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115,
  919. 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139,
  920. 141, 143, 145, 147, 149, 151, 153, 155, 157, 159
  921. };
  922. int bits, offset;
  923. /* position of pulse */
  924. s->aw_idx_is_ext = 0;
  925. if ((bits = bitstream_read(bc, 6)) >= 54) {
  926. s->aw_idx_is_ext = 1;
  927. bits += (bits - 54) * 3 + bitstream_read(bc, 2);
  928. }
  929. /* for a repeated pulse at pulse_off with a pitch_lag of pitch[], count
  930. * the distribution of the pulses in each block contained in this frame. */
  931. s->aw_pulse_range = FFMIN(pitch[0], pitch[1]) > 32 ? 24 : 16;
  932. for (offset = start_offset[bits]; offset < 0; offset += pitch[0]) ;
  933. s->aw_n_pulses[0] = (pitch[0] - 1 + MAX_FRAMESIZE / 2 - offset) / pitch[0];
  934. s->aw_first_pulse_off[0] = offset - s->aw_pulse_range / 2;
  935. offset += s->aw_n_pulses[0] * pitch[0];
  936. s->aw_n_pulses[1] = (pitch[1] - 1 + MAX_FRAMESIZE - offset) / pitch[1];
  937. s->aw_first_pulse_off[1] = offset - (MAX_FRAMESIZE + s->aw_pulse_range) / 2;
  938. /* if continuing from a position before the block, reset position to
  939. * start of block (when corrected for the range over which it can be
  940. * spread in aw_pulse_set1()). */
  941. if (start_offset[bits] < MAX_FRAMESIZE / 2) {
  942. while (s->aw_first_pulse_off[1] - pitch[1] + s->aw_pulse_range > 0)
  943. s->aw_first_pulse_off[1] -= pitch[1];
  944. if (start_offset[bits] < 0)
  945. while (s->aw_first_pulse_off[0] - pitch[0] + s->aw_pulse_range > 0)
  946. s->aw_first_pulse_off[0] -= pitch[0];
  947. }
  948. }
  949. /**
  950. * Apply second set of pitch-adaptive window pulses.
  951. * @param s WMA Voice decoding context private data
  952. * @param bc bit I/O context
  953. * @param block_idx block index in frame [0, 1]
  954. * @param fcb structure containing fixed codebook vector info
  955. * @return -1 on error, 0 otherwise
  956. */
  957. static int aw_pulse_set2(WMAVoiceContext *s, BitstreamContext *bc,
  958. int block_idx, AMRFixed *fcb)
  959. {
  960. uint16_t use_mask_mem[9]; // only 5 are used, rest is padding
  961. uint16_t *use_mask = use_mask_mem + 2;
  962. /* in this function, idx is the index in the 80-bit (+ padding) use_mask
  963. * bit-array. Since use_mask consists of 16-bit values, the lower 4 bits
  964. * of idx are the position of the bit within a particular item in the
  965. * array (0 being the most significant bit, and 15 being the least
  966. * significant bit), and the remainder (>> 4) is the index in the
  967. * use_mask[]-array. This is faster and uses less memory than using a
  968. * 80-byte/80-int array. */
  969. int pulse_off = s->aw_first_pulse_off[block_idx],
  970. pulse_start, n, idx, range, aidx, start_off = 0;
  971. /* set offset of first pulse to within this block */
  972. if (s->aw_n_pulses[block_idx] > 0)
  973. while (pulse_off + s->aw_pulse_range < 1)
  974. pulse_off += fcb->pitch_lag;
  975. /* find range per pulse */
  976. if (s->aw_n_pulses[0] > 0) {
  977. if (block_idx == 0) {
  978. range = 32;
  979. } else /* block_idx = 1 */ {
  980. range = 8;
  981. if (s->aw_n_pulses[block_idx] > 0)
  982. pulse_off = s->aw_next_pulse_off_cache;
  983. }
  984. } else
  985. range = 16;
  986. pulse_start = s->aw_n_pulses[block_idx] > 0 ? pulse_off - range / 2 : 0;
  987. /* aw_pulse_set1() already applies pulses around pulse_off (to be exactly,
  988. * in the range of [pulse_off, pulse_off + s->aw_pulse_range], and thus
  989. * we exclude that range from being pulsed again in this function. */
  990. memset(&use_mask[-2], 0, 2 * sizeof(use_mask[0]));
  991. memset( use_mask, -1, 5 * sizeof(use_mask[0]));
  992. memset(&use_mask[5], 0, 2 * sizeof(use_mask[0]));
  993. if (s->aw_n_pulses[block_idx] > 0)
  994. for (idx = pulse_off; idx < MAX_FRAMESIZE / 2; idx += fcb->pitch_lag) {
  995. int excl_range = s->aw_pulse_range; // always 16 or 24
  996. uint16_t *use_mask_ptr = &use_mask[idx >> 4];
  997. int first_sh = 16 - (idx & 15);
  998. *use_mask_ptr++ &= 0xFFFFu << first_sh;
  999. excl_range -= first_sh;
  1000. if (excl_range >= 16) {
  1001. *use_mask_ptr++ = 0;
  1002. *use_mask_ptr &= 0xFFFF >> (excl_range - 16);
  1003. } else
  1004. *use_mask_ptr &= 0xFFFF >> excl_range;
  1005. }
  1006. /* find the 'aidx'th offset that is not excluded */
  1007. aidx = bitstream_read(bc, s->aw_n_pulses[0] > 0 ? 5 - 2 * block_idx : 4);
  1008. for (n = 0; n <= aidx; pulse_start++) {
  1009. for (idx = pulse_start; idx < 0; idx += fcb->pitch_lag) ;
  1010. if (idx >= MAX_FRAMESIZE / 2) { // find from zero
  1011. if (use_mask[0]) idx = 0x0F;
  1012. else if (use_mask[1]) idx = 0x1F;
  1013. else if (use_mask[2]) idx = 0x2F;
  1014. else if (use_mask[3]) idx = 0x3F;
  1015. else if (use_mask[4]) idx = 0x4F;
  1016. else return -1;
  1017. idx -= av_log2_16bit(use_mask[idx >> 4]);
  1018. }
  1019. if (use_mask[idx >> 4] & (0x8000 >> (idx & 15))) {
  1020. use_mask[idx >> 4] &= ~(0x8000 >> (idx & 15));
  1021. n++;
  1022. start_off = idx;
  1023. }
  1024. }
  1025. fcb->x[fcb->n] = start_off;
  1026. fcb->y[fcb->n] = bitstream_read_bit(bc) ? -1.0 : 1.0;
  1027. fcb->n++;
  1028. /* set offset for next block, relative to start of that block */
  1029. n = (MAX_FRAMESIZE / 2 - start_off) % fcb->pitch_lag;
  1030. s->aw_next_pulse_off_cache = n ? fcb->pitch_lag - n : 0;
  1031. return 0;
  1032. }
  1033. /**
  1034. * Apply first set of pitch-adaptive window pulses.
  1035. * @param s WMA Voice decoding context private data
  1036. * @param bc bit I/O context
  1037. * @param block_idx block index in frame [0, 1]
  1038. * @param fcb storage location for fixed codebook pulse info
  1039. */
  1040. static void aw_pulse_set1(WMAVoiceContext *s, BitstreamContext *bc,
  1041. int block_idx, AMRFixed *fcb)
  1042. {
  1043. int val = bitstream_read(bc, 12 - 2 * (s->aw_idx_is_ext && !block_idx));
  1044. float v;
  1045. if (s->aw_n_pulses[block_idx] > 0) {
  1046. int n, v_mask, i_mask, sh, n_pulses;
  1047. if (s->aw_pulse_range == 24) { // 3 pulses, 1:sign + 3:index each
  1048. n_pulses = 3;
  1049. v_mask = 8;
  1050. i_mask = 7;
  1051. sh = 4;
  1052. } else { // 4 pulses, 1:sign + 2:index each
  1053. n_pulses = 4;
  1054. v_mask = 4;
  1055. i_mask = 3;
  1056. sh = 3;
  1057. }
  1058. for (n = n_pulses - 1; n >= 0; n--, val >>= sh) {
  1059. fcb->y[fcb->n] = (val & v_mask) ? -1.0 : 1.0;
  1060. fcb->x[fcb->n] = (val & i_mask) * n_pulses + n +
  1061. s->aw_first_pulse_off[block_idx];
  1062. while (fcb->x[fcb->n] < 0)
  1063. fcb->x[fcb->n] += fcb->pitch_lag;
  1064. if (fcb->x[fcb->n] < MAX_FRAMESIZE / 2)
  1065. fcb->n++;
  1066. }
  1067. } else {
  1068. int num2 = (val & 0x1FF) >> 1, delta, idx;
  1069. if (num2 < 1 * 79) { delta = 1; idx = num2 + 1; }
  1070. else if (num2 < 2 * 78) { delta = 3; idx = num2 + 1 - 1 * 77; }
  1071. else if (num2 < 3 * 77) { delta = 5; idx = num2 + 1 - 2 * 76; }
  1072. else { delta = 7; idx = num2 + 1 - 3 * 75; }
  1073. v = (val & 0x200) ? -1.0 : 1.0;
  1074. fcb->no_repeat_mask |= 3 << fcb->n;
  1075. fcb->x[fcb->n] = idx - delta;
  1076. fcb->y[fcb->n] = v;
  1077. fcb->x[fcb->n + 1] = idx;
  1078. fcb->y[fcb->n + 1] = (val & 1) ? -v : v;
  1079. fcb->n += 2;
  1080. }
  1081. }
  1082. /**
  1083. * @}
  1084. *
  1085. * Generate a random number from frame_cntr and block_idx, which will live
  1086. * in the range [0, 1000 - block_size] (so it can be used as an index in a
  1087. * table of size 1000 of which you want to read block_size entries).
  1088. *
  1089. * @param frame_cntr current frame number
  1090. * @param block_num current block index
  1091. * @param block_size amount of entries we want to read from a table
  1092. * that has 1000 entries
  1093. * @return a (non-)random number in the [0, 1000 - block_size] range.
  1094. */
  1095. static int pRNG(int frame_cntr, int block_num, int block_size)
  1096. {
  1097. /* array to simplify the calculation of z:
  1098. * y = (x % 9) * 5 + 6;
  1099. * z = (49995 * x) / y;
  1100. * Since y only has 9 values, we can remove the division by using a
  1101. * LUT and using FASTDIV-style divisions. For each of the 9 values
  1102. * of y, we can rewrite z as:
  1103. * z = x * (49995 / y) + x * ((49995 % y) / y)
  1104. * In this table, each col represents one possible value of y, the
  1105. * first number is 49995 / y, and the second is the FASTDIV variant
  1106. * of 49995 % y / y. */
  1107. static const unsigned int div_tbl[9][2] = {
  1108. { 8332, 3 * 715827883U }, // y = 6
  1109. { 4545, 0 * 390451573U }, // y = 11
  1110. { 3124, 11 * 268435456U }, // y = 16
  1111. { 2380, 15 * 204522253U }, // y = 21
  1112. { 1922, 23 * 165191050U }, // y = 26
  1113. { 1612, 23 * 138547333U }, // y = 31
  1114. { 1388, 27 * 119304648U }, // y = 36
  1115. { 1219, 16 * 104755300U }, // y = 41
  1116. { 1086, 39 * 93368855U } // y = 46
  1117. };
  1118. unsigned int z, y, x = MUL16(block_num, 1877) + frame_cntr;
  1119. if (x >= 0xFFFF) x -= 0xFFFF; // max value of x is 8*1877+0xFFFE=0x13AA6,
  1120. // so this is effectively a modulo (%)
  1121. y = x - 9 * MULH(477218589, x); // x % 9
  1122. z = (uint16_t) (x * div_tbl[y][0] + UMULH(x, div_tbl[y][1]));
  1123. // z = x * 49995 / (y * 5 + 6)
  1124. return z % (1000 - block_size);
  1125. }
  1126. /**
  1127. * Parse hardcoded signal for a single block.
  1128. * @note see #synth_block().
  1129. */
  1130. static void synth_block_hardcoded(WMAVoiceContext *s, BitstreamContext *bc,
  1131. int block_idx, int size,
  1132. const struct frame_type_desc *frame_desc,
  1133. float *excitation)
  1134. {
  1135. float gain;
  1136. int n, r_idx;
  1137. assert(size <= MAX_FRAMESIZE);
  1138. /* Set the offset from which we start reading wmavoice_std_codebook */
  1139. if (frame_desc->fcb_type == FCB_TYPE_SILENCE) {
  1140. r_idx = pRNG(s->frame_cntr, block_idx, size);
  1141. gain = s->silence_gain;
  1142. } else /* FCB_TYPE_HARDCODED */ {
  1143. r_idx = bitstream_read(bc, 8);
  1144. gain = wmavoice_gain_universal[bitstream_read(bc, 6)];
  1145. }
  1146. /* Clear gain prediction parameters */
  1147. memset(s->gain_pred_err, 0, sizeof(s->gain_pred_err));
  1148. /* Apply gain to hardcoded codebook and use that as excitation signal */
  1149. for (n = 0; n < size; n++)
  1150. excitation[n] = wmavoice_std_codebook[r_idx + n] * gain;
  1151. }
  1152. /**
  1153. * Parse FCB/ACB signal for a single block.
  1154. * @note see #synth_block().
  1155. */
  1156. static void synth_block_fcb_acb(WMAVoiceContext *s, BitstreamContext *bc,
  1157. int block_idx, int size,
  1158. int block_pitch_sh2,
  1159. const struct frame_type_desc *frame_desc,
  1160. float *excitation)
  1161. {
  1162. static const float gain_coeff[6] = {
  1163. 0.8169, -0.06545, 0.1726, 0.0185, -0.0359, 0.0458
  1164. };
  1165. float pulses[MAX_FRAMESIZE / 2], pred_err, acb_gain, fcb_gain;
  1166. int n, idx, gain_weight;
  1167. AMRFixed fcb;
  1168. assert(size <= MAX_FRAMESIZE / 2);
  1169. memset(pulses, 0, sizeof(*pulses) * size);
  1170. fcb.pitch_lag = block_pitch_sh2 >> 2;
  1171. fcb.pitch_fac = 1.0;
  1172. fcb.no_repeat_mask = 0;
  1173. fcb.n = 0;
  1174. /* For the other frame types, this is where we apply the innovation
  1175. * (fixed) codebook pulses of the speech signal. */
  1176. if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {
  1177. aw_pulse_set1(s, bc, block_idx, &fcb);
  1178. if (aw_pulse_set2(s, bc, block_idx, &fcb)) {
  1179. /* Conceal the block with silence and return.
  1180. * Skip the correct amount of bits to read the next
  1181. * block from the correct offset. */
  1182. int r_idx = pRNG(s->frame_cntr, block_idx, size);
  1183. for (n = 0; n < size; n++)
  1184. excitation[n] =
  1185. wmavoice_std_codebook[r_idx + n] * s->silence_gain;
  1186. bitstream_skip(bc, 7 + 1);
  1187. return;
  1188. }
  1189. } else /* FCB_TYPE_EXC_PULSES */ {
  1190. int offset_nbits = 5 - frame_desc->log_n_blocks;
  1191. fcb.no_repeat_mask = -1;
  1192. /* similar to ff_decode_10_pulses_35bits(), but with single pulses
  1193. * (instead of double) for a subset of pulses */
  1194. for (n = 0; n < 5; n++) {
  1195. float sign;
  1196. int pos1, pos2;
  1197. sign = bitstream_read_bit(bc) ? 1.0 : -1.0;
  1198. pos1 = bitstream_read(bc, offset_nbits);
  1199. fcb.x[fcb.n] = n + 5 * pos1;
  1200. fcb.y[fcb.n++] = sign;
  1201. if (n < frame_desc->dbl_pulses) {
  1202. pos2 = bitstream_read(bc, offset_nbits);
  1203. fcb.x[fcb.n] = n + 5 * pos2;
  1204. fcb.y[fcb.n++] = (pos1 < pos2) ? -sign : sign;
  1205. }
  1206. }
  1207. }
  1208. ff_set_fixed_vector(pulses, &fcb, 1.0, size);
  1209. /* Calculate gain for adaptive & fixed codebook signal.
  1210. * see ff_amr_set_fixed_gain(). */
  1211. idx = bitstream_read(bc, 7);
  1212. fcb_gain = expf(avpriv_scalarproduct_float_c(s->gain_pred_err,
  1213. gain_coeff, 6) -
  1214. 5.2409161640 + wmavoice_gain_codebook_fcb[idx]);
  1215. acb_gain = wmavoice_gain_codebook_acb[idx];
  1216. pred_err = av_clipf(wmavoice_gain_codebook_fcb[idx],
  1217. -2.9957322736 /* log(0.05) */,
  1218. 1.6094379124 /* log(5.0) */);
  1219. gain_weight = 8 >> frame_desc->log_n_blocks;
  1220. memmove(&s->gain_pred_err[gain_weight], s->gain_pred_err,
  1221. sizeof(*s->gain_pred_err) * (6 - gain_weight));
  1222. for (n = 0; n < gain_weight; n++)
  1223. s->gain_pred_err[n] = pred_err;
  1224. /* Calculation of adaptive codebook */
  1225. if (frame_desc->acb_type == ACB_TYPE_ASYMMETRIC) {
  1226. int len;
  1227. for (n = 0; n < size; n += len) {
  1228. int next_idx_sh16;
  1229. int abs_idx = block_idx * size + n;
  1230. int pitch_sh16 = (s->last_pitch_val << 16) +
  1231. s->pitch_diff_sh16 * abs_idx;
  1232. int pitch = (pitch_sh16 + 0x6FFF) >> 16;
  1233. int idx_sh16 = ((pitch << 16) - pitch_sh16) * 8 + 0x58000;
  1234. idx = idx_sh16 >> 16;
  1235. if (s->pitch_diff_sh16) {
  1236. if (s->pitch_diff_sh16 > 0) {
  1237. next_idx_sh16 = (idx_sh16) &~ 0xFFFF;
  1238. } else
  1239. next_idx_sh16 = (idx_sh16 + 0x10000) &~ 0xFFFF;
  1240. len = av_clip((idx_sh16 - next_idx_sh16) / s->pitch_diff_sh16 / 8,
  1241. 1, size - n);
  1242. } else
  1243. len = size;
  1244. ff_acelp_interpolatef(&excitation[n], &excitation[n - pitch],
  1245. wmavoice_ipol1_coeffs, 17,
  1246. idx, 9, len);
  1247. }
  1248. } else /* ACB_TYPE_HAMMING */ {
  1249. int block_pitch = block_pitch_sh2 >> 2;
  1250. idx = block_pitch_sh2 & 3;
  1251. if (idx) {
  1252. ff_acelp_interpolatef(excitation, &excitation[-block_pitch],
  1253. wmavoice_ipol2_coeffs, 4,
  1254. idx, 8, size);
  1255. } else
  1256. av_memcpy_backptr((uint8_t *) excitation, sizeof(float) * block_pitch,
  1257. sizeof(float) * size);
  1258. }
  1259. /* Interpolate ACB/FCB and use as excitation signal */
  1260. ff_weighted_vector_sumf(excitation, excitation, pulses,
  1261. acb_gain, fcb_gain, size);
  1262. }
  1263. /**
  1264. * Parse data in a single block.
  1265. * @note we assume enough bits are available, caller should check.
  1266. *
  1267. * @param s WMA Voice decoding context private data
  1268. * @param bc bit I/O context
  1269. * @param block_idx index of the to-be-read block
  1270. * @param size amount of samples to be read in this block
  1271. * @param block_pitch_sh2 pitch for this block << 2
  1272. * @param lsps LSPs for (the end of) this frame
  1273. * @param prev_lsps LSPs for the last frame
  1274. * @param frame_desc frame type descriptor
  1275. * @param excitation target memory for the ACB+FCB interpolated signal
  1276. * @param synth target memory for the speech synthesis filter output
  1277. * @return 0 on success, <0 on error.
  1278. */
  1279. static void synth_block(WMAVoiceContext *s, BitstreamContext *bc,
  1280. int block_idx, int size,
  1281. int block_pitch_sh2,
  1282. const double *lsps, const double *prev_lsps,
  1283. const struct frame_type_desc *frame_desc,
  1284. float *excitation, float *synth)
  1285. {
  1286. double i_lsps[MAX_LSPS];
  1287. float lpcs[MAX_LSPS];
  1288. float fac;
  1289. int n;
  1290. if (frame_desc->acb_type == ACB_TYPE_NONE)
  1291. synth_block_hardcoded(s, bc, block_idx, size, frame_desc, excitation);
  1292. else
  1293. synth_block_fcb_acb(s, bc, block_idx, size, block_pitch_sh2,
  1294. frame_desc, excitation);
  1295. /* convert interpolated LSPs to LPCs */
  1296. fac = (block_idx + 0.5) / frame_desc->n_blocks;
  1297. for (n = 0; n < s->lsps; n++) // LSF -> LSP
  1298. i_lsps[n] = cos(prev_lsps[n] + fac * (lsps[n] - prev_lsps[n]));
  1299. ff_acelp_lspd2lpc(i_lsps, lpcs, s->lsps >> 1);
  1300. /* Speech synthesis */
  1301. ff_celp_lp_synthesis_filterf(synth, lpcs, excitation, size, s->lsps);
  1302. }
  1303. /**
  1304. * Synthesize output samples for a single frame.
  1305. * @note we assume enough bits are available, caller should check.
  1306. *
  1307. * @param ctx WMA Voice decoder context
  1308. * @param bc bit I/O context (s->bc or one for cross-packet superframes)
  1309. * @param frame_idx Frame number within superframe [0-2]
  1310. * @param samples pointer to output sample buffer, has space for at least 160
  1311. * samples
  1312. * @param lsps LSP array
  1313. * @param prev_lsps array of previous frame's LSPs
  1314. * @param excitation target buffer for excitation signal
  1315. * @param synth target buffer for synthesized speech data
  1316. * @return 0 on success, <0 on error.
  1317. */
  1318. static int synth_frame(AVCodecContext *ctx, BitstreamContext *bc,
  1319. int frame_idx, float *samples,
  1320. const double *lsps, const double *prev_lsps,
  1321. float *excitation, float *synth)
  1322. {
  1323. WMAVoiceContext *s = ctx->priv_data;
  1324. int n, n_blocks_x2, log_n_blocks_x2, cur_pitch_val;
  1325. int pitch[MAX_BLOCKS], last_block_pitch;
  1326. /* Parse frame type ("frame header"), see frame_descs */
  1327. int bd_idx = s->vbm_tree[bitstream_read_vlc(bc, frame_type_vlc.table, 6, 3)], block_nsamples;
  1328. if (bd_idx < 0) {
  1329. av_log(ctx, AV_LOG_ERROR,
  1330. "Invalid frame type VLC code, skipping\n");
  1331. return AVERROR_INVALIDDATA;
  1332. }
  1333. block_nsamples = MAX_FRAMESIZE / frame_descs[bd_idx].n_blocks;
  1334. /* Pitch calculation for ACB_TYPE_ASYMMETRIC ("pitch-per-frame") */
  1335. if (frame_descs[bd_idx].acb_type == ACB_TYPE_ASYMMETRIC) {
  1336. /* Pitch is provided per frame, which is interpreted as the pitch of
  1337. * the last sample of the last block of this frame. We can interpolate
  1338. * the pitch of other blocks (and even pitch-per-sample) by gradually
  1339. * incrementing/decrementing prev_frame_pitch to cur_pitch_val. */
  1340. n_blocks_x2 = frame_descs[bd_idx].n_blocks << 1;
  1341. log_n_blocks_x2 = frame_descs[bd_idx].log_n_blocks + 1;
  1342. cur_pitch_val = s->min_pitch_val + bitstream_read(bc, s->pitch_nbits);
  1343. cur_pitch_val = FFMIN(cur_pitch_val, s->max_pitch_val - 1);
  1344. if (s->last_acb_type == ACB_TYPE_NONE ||
  1345. 20 * abs(cur_pitch_val - s->last_pitch_val) >
  1346. (cur_pitch_val + s->last_pitch_val))
  1347. s->last_pitch_val = cur_pitch_val;
  1348. /* pitch per block */
  1349. for (n = 0; n < frame_descs[bd_idx].n_blocks; n++) {
  1350. int fac = n * 2 + 1;
  1351. pitch[n] = (MUL16(fac, cur_pitch_val) +
  1352. MUL16((n_blocks_x2 - fac), s->last_pitch_val) +
  1353. frame_descs[bd_idx].n_blocks) >> log_n_blocks_x2;
  1354. }
  1355. /* "pitch-diff-per-sample" for calculation of pitch per sample */
  1356. s->pitch_diff_sh16 =
  1357. ((cur_pitch_val - s->last_pitch_val) << 16) / MAX_FRAMESIZE;
  1358. }
  1359. /* Global gain (if silence) and pitch-adaptive window coordinates */
  1360. switch (frame_descs[bd_idx].fcb_type) {
  1361. case FCB_TYPE_SILENCE:
  1362. s->silence_gain = wmavoice_gain_silence[bitstream_read(bc, 8)];
  1363. break;
  1364. case FCB_TYPE_AW_PULSES:
  1365. aw_parse_coords(s, bc, pitch);
  1366. break;
  1367. }
  1368. for (n = 0; n < frame_descs[bd_idx].n_blocks; n++) {
  1369. int bl_pitch_sh2;
  1370. /* Pitch calculation for ACB_TYPE_HAMMING ("pitch-per-block") */
  1371. switch (frame_descs[bd_idx].acb_type) {
  1372. case ACB_TYPE_HAMMING: {
  1373. /* Pitch is given per block. Per-block pitches are encoded as an
  1374. * absolute value for the first block, and then delta values
  1375. * relative to this value) for all subsequent blocks. The scale of
  1376. * this pitch value is semi-logarithmic compared to its use in the
  1377. * decoder, so we convert it to normal scale also. */
  1378. int block_pitch,
  1379. t1 = (s->block_conv_table[1] - s->block_conv_table[0]) << 2,
  1380. t2 = (s->block_conv_table[2] - s->block_conv_table[1]) << 1,
  1381. t3 = s->block_conv_table[3] - s->block_conv_table[2] + 1;
  1382. if (n == 0) {
  1383. block_pitch = bitstream_read(bc, s->block_pitch_nbits);
  1384. } else
  1385. block_pitch = last_block_pitch - s->block_delta_pitch_hrange +
  1386. bitstream_read(bc, s->block_delta_pitch_nbits);
  1387. /* Convert last_ so that any next delta is within _range */
  1388. last_block_pitch = av_clip(block_pitch,
  1389. s->block_delta_pitch_hrange,
  1390. s->block_pitch_range -
  1391. s->block_delta_pitch_hrange);
  1392. /* Convert semi-log-style scale back to normal scale */
  1393. if (block_pitch < t1) {
  1394. bl_pitch_sh2 = (s->block_conv_table[0] << 2) + block_pitch;
  1395. } else {
  1396. block_pitch -= t1;
  1397. if (block_pitch < t2) {
  1398. bl_pitch_sh2 =
  1399. (s->block_conv_table[1] << 2) + (block_pitch << 1);
  1400. } else {
  1401. block_pitch -= t2;
  1402. if (block_pitch < t3) {
  1403. bl_pitch_sh2 =
  1404. (s->block_conv_table[2] + block_pitch) << 2;
  1405. } else
  1406. bl_pitch_sh2 = s->block_conv_table[3] << 2;
  1407. }
  1408. }
  1409. pitch[n] = bl_pitch_sh2 >> 2;
  1410. break;
  1411. }
  1412. case ACB_TYPE_ASYMMETRIC: {
  1413. bl_pitch_sh2 = pitch[n] << 2;
  1414. break;
  1415. }
  1416. default: // ACB_TYPE_NONE has no pitch
  1417. bl_pitch_sh2 = 0;
  1418. break;
  1419. }
  1420. synth_block(s, bc, n, block_nsamples, bl_pitch_sh2,
  1421. lsps, prev_lsps, &frame_descs[bd_idx],
  1422. &excitation[n * block_nsamples],
  1423. &synth[n * block_nsamples]);
  1424. }
  1425. /* Averaging projection filter, if applicable. Else, just copy samples
  1426. * from synthesis buffer */
  1427. if (s->do_apf) {
  1428. double i_lsps[MAX_LSPS];
  1429. float lpcs[MAX_LSPS];
  1430. for (n = 0; n < s->lsps; n++) // LSF -> LSP
  1431. i_lsps[n] = cos(0.5 * (prev_lsps[n] + lsps[n]));
  1432. ff_acelp_lspd2lpc(i_lsps, lpcs, s->lsps >> 1);
  1433. postfilter(s, synth, samples, 80, lpcs,
  1434. &s->zero_exc_pf[s->history_nsamples + MAX_FRAMESIZE * frame_idx],
  1435. frame_descs[bd_idx].fcb_type, pitch[0]);
  1436. for (n = 0; n < s->lsps; n++) // LSF -> LSP
  1437. i_lsps[n] = cos(lsps[n]);
  1438. ff_acelp_lspd2lpc(i_lsps, lpcs, s->lsps >> 1);
  1439. postfilter(s, &synth[80], &samples[80], 80, lpcs,
  1440. &s->zero_exc_pf[s->history_nsamples + MAX_FRAMESIZE * frame_idx + 80],
  1441. frame_descs[bd_idx].fcb_type, pitch[0]);
  1442. } else
  1443. memcpy(samples, synth, 160 * sizeof(synth[0]));
  1444. /* Cache values for next frame */
  1445. s->frame_cntr++;
  1446. if (s->frame_cntr >= 0xFFFF) s->frame_cntr -= 0xFFFF; // i.e. modulo (%)
  1447. s->last_acb_type = frame_descs[bd_idx].acb_type;
  1448. switch (frame_descs[bd_idx].acb_type) {
  1449. case ACB_TYPE_NONE:
  1450. s->last_pitch_val = 0;
  1451. break;
  1452. case ACB_TYPE_ASYMMETRIC:
  1453. s->last_pitch_val = cur_pitch_val;
  1454. break;
  1455. case ACB_TYPE_HAMMING:
  1456. s->last_pitch_val = pitch[frame_descs[bd_idx].n_blocks - 1];
  1457. break;
  1458. }
  1459. return 0;
  1460. }
  1461. /**
  1462. * Ensure minimum value for first item, maximum value for last value,
  1463. * proper spacing between each value and proper ordering.
  1464. *
  1465. * @param lsps array of LSPs
  1466. * @param num size of LSP array
  1467. *
  1468. * @note basically a double version of #ff_acelp_reorder_lsf(), might be
  1469. * useful to put in a generic location later on. Parts are also
  1470. * present in #ff_set_min_dist_lsf() + #ff_sort_nearly_sorted_floats(),
  1471. * which is in float.
  1472. */
  1473. static void stabilize_lsps(double *lsps, int num)
  1474. {
  1475. int n, m, l;
  1476. /* set minimum value for first, maximum value for last and minimum
  1477. * spacing between LSF values.
  1478. * Very similar to ff_set_min_dist_lsf(), but in double. */
  1479. lsps[0] = FFMAX(lsps[0], 0.0015 * M_PI);
  1480. for (n = 1; n < num; n++)
  1481. lsps[n] = FFMAX(lsps[n], lsps[n - 1] + 0.0125 * M_PI);
  1482. lsps[num - 1] = FFMIN(lsps[num - 1], 0.9985 * M_PI);
  1483. /* reorder (looks like one-time / non-recursed bubblesort).
  1484. * Very similar to ff_sort_nearly_sorted_floats(), but in double. */
  1485. for (n = 1; n < num; n++) {
  1486. if (lsps[n] < lsps[n - 1]) {
  1487. for (m = 1; m < num; m++) {
  1488. double tmp = lsps[m];
  1489. for (l = m - 1; l >= 0; l--) {
  1490. if (lsps[l] <= tmp) break;
  1491. lsps[l + 1] = lsps[l];
  1492. }
  1493. lsps[l + 1] = tmp;
  1494. }
  1495. break;
  1496. }
  1497. }
  1498. }
  1499. /**
  1500. * Test if there's enough bits to read 1 superframe.
  1501. *
  1502. * @param orig_bc bit I/O context used for reading. This function
  1503. * does not modify the state of the bitreader; it
  1504. * only uses it to copy the current stream position
  1505. * @param s WMA Voice decoding context private data
  1506. * @return < 0 on error, 1 on not enough bits or 0 if OK.
  1507. */
  1508. static int check_bits_for_superframe(BitstreamContext *orig_bc,
  1509. WMAVoiceContext *s)
  1510. {
  1511. BitstreamContext s_bc, *bc = &s_bc;
  1512. int n, need_bits, bd_idx;
  1513. const struct frame_type_desc *frame_desc;
  1514. /* initialize a copy */
  1515. *bc = *orig_bc;
  1516. /* superframe header */
  1517. if (bitstream_bits_left(bc) < 14)
  1518. return 1;
  1519. if (!bitstream_read_bit(bc))
  1520. return AVERROR(ENOSYS); // WMAPro-in-WMAVoice superframe
  1521. if (bitstream_read_bit(bc)) bitstream_skip(bc, 12); // number of samples in superframe
  1522. if (s->has_residual_lsps) { // residual LSPs (for all frames)
  1523. if (bitstream_bits_left(bc) < s->sframe_lsp_bitsize)
  1524. return 1;
  1525. bitstream_skip(bc, s->sframe_lsp_bitsize);
  1526. }
  1527. /* frames */
  1528. for (n = 0; n < MAX_FRAMES; n++) {
  1529. int aw_idx_is_ext = 0;
  1530. if (!s->has_residual_lsps) { // independent LSPs (per-frame)
  1531. if (bitstream_bits_left(bc) < s->frame_lsp_bitsize)
  1532. return 1;
  1533. bitstream_skip(bc, s->frame_lsp_bitsize);
  1534. }
  1535. bd_idx = s->vbm_tree[bitstream_read_vlc(bc, frame_type_vlc.table, 6, 3)];
  1536. if (bd_idx < 0)
  1537. return AVERROR_INVALIDDATA; // invalid frame type VLC code
  1538. frame_desc = &frame_descs[bd_idx];
  1539. if (frame_desc->acb_type == ACB_TYPE_ASYMMETRIC) {
  1540. if (bitstream_bits_left(bc) < s->pitch_nbits)
  1541. return 1;
  1542. bitstream_skip(bc, s->pitch_nbits);
  1543. }
  1544. if (frame_desc->fcb_type == FCB_TYPE_SILENCE) {
  1545. bitstream_skip(bc, 8);
  1546. } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {
  1547. int tmp = bitstream_read(bc, 6);
  1548. if (tmp >= 0x36) {
  1549. bitstream_skip(bc, 2);
  1550. aw_idx_is_ext = 1;
  1551. }
  1552. }
  1553. /* blocks */
  1554. if (frame_desc->acb_type == ACB_TYPE_HAMMING) {
  1555. need_bits = s->block_pitch_nbits +
  1556. (frame_desc->n_blocks - 1) * s->block_delta_pitch_nbits;
  1557. } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {
  1558. need_bits = 2 * !aw_idx_is_ext;
  1559. } else
  1560. need_bits = 0;
  1561. need_bits += frame_desc->frame_size;
  1562. if (bitstream_bits_left(bc) < need_bits)
  1563. return 1;
  1564. bitstream_skip(bc, need_bits);
  1565. }
  1566. return 0;
  1567. }
  1568. /**
  1569. * Synthesize output samples for a single superframe. If we have any data
  1570. * cached in s->sframe_cache, that will be used instead of whatever is loaded
  1571. * in s->bc.
  1572. *
  1573. * WMA Voice superframes contain 3 frames, each containing 160 audio samples,
  1574. * to give a total of 480 samples per frame. See #synth_frame() for frame
  1575. * parsing. In addition to 3 frames, superframes can also contain the LSPs
  1576. * (if these are globally specified for all frames (residually); they can
  1577. * also be specified individually per-frame. See the s->has_residual_lsps
  1578. * option), and can specify the number of samples encoded in this superframe
  1579. * (if less than 480), usually used to prevent blanks at track boundaries.
  1580. *
  1581. * @param ctx WMA Voice decoder context
  1582. * @return 0 on success, <0 on error or 1 if there was not enough data to
  1583. * fully parse the superframe
  1584. */
  1585. static int synth_superframe(AVCodecContext *ctx, AVFrame *frame,
  1586. int *got_frame_ptr)
  1587. {
  1588. WMAVoiceContext *s = ctx->priv_data;
  1589. BitstreamContext *bc = &s->bc, s_bc;
  1590. int n, res, n_samples = 480;
  1591. double lsps[MAX_FRAMES][MAX_LSPS];
  1592. const double *mean_lsf = s->lsps == 16 ?
  1593. wmavoice_mean_lsf16[s->lsp_def_mode] : wmavoice_mean_lsf10[s->lsp_def_mode];
  1594. float excitation[MAX_SIGNAL_HISTORY + MAX_SFRAMESIZE + 12];
  1595. float synth[MAX_LSPS + MAX_SFRAMESIZE];
  1596. float *samples;
  1597. memcpy(synth, s->synth_history,
  1598. s->lsps * sizeof(*synth));
  1599. memcpy(excitation, s->excitation_history,
  1600. s->history_nsamples * sizeof(*excitation));
  1601. if (s->sframe_cache_size > 0) {
  1602. bc = &s_bc;
  1603. bitstream_init(bc, s->sframe_cache, s->sframe_cache_size);
  1604. s->sframe_cache_size = 0;
  1605. }
  1606. if ((res = check_bits_for_superframe(bc, s)) == 1) {
  1607. *got_frame_ptr = 0;
  1608. return 1;
  1609. } else if (res < 0)
  1610. return res;
  1611. /* First bit is speech/music bit, it differentiates between WMAVoice
  1612. * speech samples (the actual codec) and WMAVoice music samples, which
  1613. * are really WMAPro-in-WMAVoice-superframes. I've never seen those in
  1614. * the wild yet. */
  1615. if (!bitstream_read_bit(bc)) {
  1616. avpriv_request_sample(ctx, "WMAPro-in-WMAVoice");
  1617. return AVERROR_PATCHWELCOME;
  1618. }
  1619. /* (optional) nr. of samples in superframe; always <= 480 and >= 0 */
  1620. if (bitstream_read_bit(bc)) {
  1621. if ((n_samples = bitstream_read(bc, 12)) > 480) {
  1622. av_log(ctx, AV_LOG_ERROR,
  1623. "Superframe encodes >480 samples (%d), not allowed\n",
  1624. n_samples);
  1625. return AVERROR_INVALIDDATA;
  1626. }
  1627. }
  1628. /* Parse LSPs, if global for the superframe (can also be per-frame). */
  1629. if (s->has_residual_lsps) {
  1630. double prev_lsps[MAX_LSPS], a1[MAX_LSPS * 2], a2[MAX_LSPS * 2];
  1631. for (n = 0; n < s->lsps; n++)
  1632. prev_lsps[n] = s->prev_lsps[n] - mean_lsf[n];
  1633. if (s->lsps == 10) {
  1634. dequant_lsp10r(bc, lsps[2], prev_lsps, a1, a2, s->lsp_q_mode);
  1635. } else /* s->lsps == 16 */
  1636. dequant_lsp16r(bc, lsps[2], prev_lsps, a1, a2, s->lsp_q_mode);
  1637. for (n = 0; n < s->lsps; n++) {
  1638. lsps[0][n] = mean_lsf[n] + (a1[n] - a2[n * 2]);
  1639. lsps[1][n] = mean_lsf[n] + (a1[s->lsps + n] - a2[n * 2 + 1]);
  1640. lsps[2][n] += mean_lsf[n];
  1641. }
  1642. for (n = 0; n < 3; n++)
  1643. stabilize_lsps(lsps[n], s->lsps);
  1644. }
  1645. /* get output buffer */
  1646. frame->nb_samples = 480;
  1647. if ((res = ff_get_buffer(ctx, frame, 0)) < 0) {
  1648. av_log(ctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1649. return res;
  1650. }
  1651. frame->nb_samples = n_samples;
  1652. samples = (float *)frame->data[0];
  1653. /* Parse frames, optionally preceded by per-frame (independent) LSPs. */
  1654. for (n = 0; n < 3; n++) {
  1655. if (!s->has_residual_lsps) {
  1656. int m;
  1657. if (s->lsps == 10) {
  1658. dequant_lsp10i(bc, lsps[n]);
  1659. } else /* s->lsps == 16 */
  1660. dequant_lsp16i(bc, lsps[n]);
  1661. for (m = 0; m < s->lsps; m++)
  1662. lsps[n][m] += mean_lsf[m];
  1663. stabilize_lsps(lsps[n], s->lsps);
  1664. }
  1665. if ((res = synth_frame(ctx, bc, n,
  1666. &samples[n * MAX_FRAMESIZE],
  1667. lsps[n], n == 0 ? s->prev_lsps : lsps[n - 1],
  1668. &excitation[s->history_nsamples + n * MAX_FRAMESIZE],
  1669. &synth[s->lsps + n * MAX_FRAMESIZE]))) {
  1670. *got_frame_ptr = 0;
  1671. return res;
  1672. }
  1673. }
  1674. /* Statistics? FIXME - we don't check for length, a slight overrun
  1675. * will be caught by internal buffer padding, and anything else
  1676. * will be skipped, not read. */
  1677. if (bitstream_read_bit(bc)) {
  1678. res = bitstream_read(bc, 4);
  1679. bitstream_skip(bc, 10 * (res + 1));
  1680. }
  1681. *got_frame_ptr = 1;
  1682. /* Update history */
  1683. memcpy(s->prev_lsps, lsps[2],
  1684. s->lsps * sizeof(*s->prev_lsps));
  1685. memcpy(s->synth_history, &synth[MAX_SFRAMESIZE],
  1686. s->lsps * sizeof(*synth));
  1687. memcpy(s->excitation_history, &excitation[MAX_SFRAMESIZE],
  1688. s->history_nsamples * sizeof(*excitation));
  1689. if (s->do_apf)
  1690. memmove(s->zero_exc_pf, &s->zero_exc_pf[MAX_SFRAMESIZE],
  1691. s->history_nsamples * sizeof(*s->zero_exc_pf));
  1692. return 0;
  1693. }
  1694. /**
  1695. * Parse the packet header at the start of each packet (input data to this
  1696. * decoder).
  1697. *
  1698. * @param s WMA Voice decoding context private data
  1699. * @return 1 if not enough bits were available, or 0 on success.
  1700. */
  1701. static int parse_packet_header(WMAVoiceContext *s)
  1702. {
  1703. BitstreamContext *bc = &s->bc;
  1704. unsigned int res;
  1705. if (bitstream_bits_left(bc) < 11)
  1706. return 1;
  1707. bitstream_skip(bc, 4); // packet sequence number
  1708. s->has_residual_lsps = bitstream_read_bit(bc);
  1709. do {
  1710. res = bitstream_read(bc, 6); // number of superframes per packet
  1711. // (minus first one if there is spillover)
  1712. if (bitstream_bits_left(bc) < 6 * (res == 0x3F) + s->spillover_bitsize)
  1713. return 1;
  1714. } while (res == 0x3F);
  1715. s->spillover_nbits = bitstream_read(bc, s->spillover_bitsize);
  1716. return 0;
  1717. }
  1718. /**
  1719. * Copy (unaligned) bits from bc/data/size to pb.
  1720. *
  1721. * @param pb target buffer to copy bits into
  1722. * @param data source buffer to copy bits from
  1723. * @param size size of the source data, in bytes
  1724. * @param bc bit I/O context specifying the current position in the source.
  1725. * data. This function might use this to align the bit position to
  1726. * a whole-byte boundary before calling #avpriv_copy_bits() on aligned
  1727. * source data
  1728. * @param nbits the amount of bits to copy from source to target
  1729. *
  1730. * @note after calling this function, the current position in the input bit
  1731. * I/O context is undefined.
  1732. */
  1733. static void copy_bits(PutBitContext *pb,
  1734. const uint8_t *data, int size,
  1735. BitstreamContext *bc, int nbits)
  1736. {
  1737. int rmn_bytes, rmn_bits;
  1738. rmn_bits = rmn_bytes = bitstream_bits_left(bc);
  1739. if (rmn_bits < nbits)
  1740. return;
  1741. if (nbits > pb->size_in_bits - put_bits_count(pb))
  1742. return;
  1743. rmn_bits &= 7; rmn_bytes >>= 3;
  1744. if ((rmn_bits = FFMIN(rmn_bits, nbits)) > 0)
  1745. put_bits(pb, rmn_bits, bitstream_read(bc, rmn_bits));
  1746. avpriv_copy_bits(pb, data + size - rmn_bytes,
  1747. FFMIN(nbits - rmn_bits, rmn_bytes << 3));
  1748. }
  1749. /**
  1750. * Packet decoding: a packet is anything that the (ASF) demuxer contains,
  1751. * and we expect that the demuxer / application provides it to us as such
  1752. * (else you'll probably get garbage as output). Every packet has a size of
  1753. * ctx->block_align bytes, starts with a packet header (see
  1754. * #parse_packet_header()), and then a series of superframes. Superframe
  1755. * boundaries may exceed packets, i.e. superframes can split data over
  1756. * multiple (two) packets.
  1757. *
  1758. * For more information about frames, see #synth_superframe().
  1759. */
  1760. static int wmavoice_decode_packet(AVCodecContext *ctx, void *data,
  1761. int *got_frame_ptr, AVPacket *avpkt)
  1762. {
  1763. WMAVoiceContext *s = ctx->priv_data;
  1764. BitstreamContext *bc = &s->bc;
  1765. int size, res, pos;
  1766. /* Packets are sometimes a multiple of ctx->block_align, with a packet
  1767. * header at each ctx->block_align bytes. However, Libav's ASF demuxer
  1768. * feeds us ASF packets, which may concatenate multiple "codec" packets
  1769. * in a single "muxer" packet, so we artificially emulate that by
  1770. * capping the packet size at ctx->block_align. */
  1771. for (size = avpkt->size; size > ctx->block_align; size -= ctx->block_align);
  1772. if (!size) {
  1773. *got_frame_ptr = 0;
  1774. return 0;
  1775. }
  1776. bitstream_init8(&s->bc, avpkt->data, size);
  1777. /* size == ctx->block_align is used to indicate whether we are dealing with
  1778. * a new packet or a packet of which we already read the packet header
  1779. * previously. */
  1780. if (size == ctx->block_align) { // new packet header
  1781. if ((res = parse_packet_header(s)) < 0)
  1782. return res;
  1783. /* If the packet header specifies a s->spillover_nbits, then we want
  1784. * to push out all data of the previous packet (+ spillover) before
  1785. * continuing to parse new superframes in the current packet. */
  1786. if (s->spillover_nbits > 0) {
  1787. if (s->sframe_cache_size > 0) {
  1788. int cnt = bitstream_tell(bc);
  1789. copy_bits(&s->pb, avpkt->data, size, bc, s->spillover_nbits);
  1790. flush_put_bits(&s->pb);
  1791. s->sframe_cache_size += s->spillover_nbits;
  1792. if ((res = synth_superframe(ctx, data, got_frame_ptr)) == 0 &&
  1793. *got_frame_ptr) {
  1794. cnt += s->spillover_nbits;
  1795. s->skip_bits_next = cnt & 7;
  1796. return cnt >> 3;
  1797. } else
  1798. bitstream_skip (bc, s->spillover_nbits - cnt +
  1799. bitstream_tell(bc)); // resync
  1800. } else
  1801. bitstream_skip(bc, s->spillover_nbits); // resync
  1802. }
  1803. } else if (s->skip_bits_next)
  1804. bitstream_skip(bc, s->skip_bits_next);
  1805. /* Try parsing superframes in current packet */
  1806. s->sframe_cache_size = 0;
  1807. s->skip_bits_next = 0;
  1808. pos = bitstream_bits_left(bc);
  1809. if ((res = synth_superframe(ctx, data, got_frame_ptr)) < 0) {
  1810. return res;
  1811. } else if (*got_frame_ptr) {
  1812. int cnt = bitstream_tell(bc);
  1813. s->skip_bits_next = cnt & 7;
  1814. return cnt >> 3;
  1815. } else if ((s->sframe_cache_size = pos) > 0) {
  1816. /* rewind bit reader to start of last (incomplete) superframe... */
  1817. bitstream_init8(bc, avpkt->data, size);
  1818. bitstream_skip(bc, (size << 3) - pos);
  1819. assert(bitstream_bits_left(bc) == pos);
  1820. /* ...and cache it for spillover in next packet */
  1821. init_put_bits(&s->pb, s->sframe_cache, SFRAME_CACHE_MAXSIZE);
  1822. copy_bits(&s->pb, avpkt->data, size, bc, s->sframe_cache_size);
  1823. // FIXME bad - just copy bytes as whole and add use the
  1824. // skip_bits_next field
  1825. }
  1826. return size;
  1827. }
  1828. static av_cold int wmavoice_decode_end(AVCodecContext *ctx)
  1829. {
  1830. WMAVoiceContext *s = ctx->priv_data;
  1831. if (s->do_apf) {
  1832. ff_rdft_end(&s->rdft);
  1833. ff_rdft_end(&s->irdft);
  1834. ff_dct_end(&s->dct);
  1835. ff_dct_end(&s->dst);
  1836. }
  1837. return 0;
  1838. }
  1839. static av_cold void wmavoice_flush(AVCodecContext *ctx)
  1840. {
  1841. WMAVoiceContext *s = ctx->priv_data;
  1842. int n;
  1843. s->postfilter_agc = 0;
  1844. s->sframe_cache_size = 0;
  1845. s->skip_bits_next = 0;
  1846. for (n = 0; n < s->lsps; n++)
  1847. s->prev_lsps[n] = M_PI * (n + 1.0) / (s->lsps + 1.0);
  1848. memset(s->excitation_history, 0,
  1849. sizeof(*s->excitation_history) * MAX_SIGNAL_HISTORY);
  1850. memset(s->synth_history, 0,
  1851. sizeof(*s->synth_history) * MAX_LSPS);
  1852. memset(s->gain_pred_err, 0,
  1853. sizeof(s->gain_pred_err));
  1854. if (s->do_apf) {
  1855. memset(&s->synth_filter_out_buf[MAX_LSPS_ALIGN16 - s->lsps], 0,
  1856. sizeof(*s->synth_filter_out_buf) * s->lsps);
  1857. memset(s->dcf_mem, 0,
  1858. sizeof(*s->dcf_mem) * 2);
  1859. memset(s->zero_exc_pf, 0,
  1860. sizeof(*s->zero_exc_pf) * s->history_nsamples);
  1861. memset(s->denoise_filter_cache, 0, sizeof(s->denoise_filter_cache));
  1862. }
  1863. }
  1864. AVCodec ff_wmavoice_decoder = {
  1865. .name = "wmavoice",
  1866. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Voice"),
  1867. .type = AVMEDIA_TYPE_AUDIO,
  1868. .id = AV_CODEC_ID_WMAVOICE,
  1869. .priv_data_size = sizeof(WMAVoiceContext),
  1870. .init = wmavoice_decode_init,
  1871. .init_static_data = wmavoice_init_static_data,
  1872. .close = wmavoice_decode_end,
  1873. .decode = wmavoice_decode_packet,
  1874. .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
  1875. .flush = wmavoice_flush,
  1876. };