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.

165 lines
6.2KB

  1. /*
  2. * WMA compatible codec
  3. * Copyright (c) 2002-2007 The FFmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVCODEC_WMA_H
  22. #define AVCODEC_WMA_H
  23. #include "get_bits.h"
  24. #include "put_bits.h"
  25. #include "dsputil.h"
  26. #include "fft.h"
  27. #include "fmtconvert.h"
  28. /* size of blocks */
  29. #define BLOCK_MIN_BITS 7
  30. #define BLOCK_MAX_BITS 11
  31. #define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
  32. #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
  33. /* XXX: find exact max size */
  34. #define HIGH_BAND_MAX_SIZE 16
  35. #define NB_LSP_COEFS 10
  36. /* XXX: is it a suitable value ? */
  37. #define MAX_CODED_SUPERFRAME_SIZE 16384
  38. #define MAX_CHANNELS 2
  39. #define NOISE_TAB_SIZE 8192
  40. #define LSP_POW_BITS 7
  41. //FIXME should be in wmadec
  42. #define VLCBITS 9
  43. #define VLCMAX ((22+VLCBITS-1)/VLCBITS)
  44. typedef float WMACoef; ///< type for decoded coefficients, int16_t would be enough for wma 1/2
  45. typedef struct CoefVLCTable {
  46. int n; ///< total number of codes
  47. int max_level;
  48. const uint32_t *huffcodes; ///< VLC bit values
  49. const uint8_t *huffbits; ///< VLC bit size
  50. const uint16_t *levels; ///< table to build run/level tables
  51. } CoefVLCTable;
  52. typedef struct WMACodecContext {
  53. AVCodecContext* avctx;
  54. AVFrame frame;
  55. GetBitContext gb;
  56. PutBitContext pb;
  57. int sample_rate;
  58. int nb_channels;
  59. int bit_rate;
  60. int version; ///< 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
  61. int block_align;
  62. int use_bit_reservoir;
  63. int use_variable_block_len;
  64. int use_exp_vlc; ///< exponent coding: 0 = lsp, 1 = vlc + delta
  65. int use_noise_coding; ///< true if perceptual noise is added
  66. int byte_offset_bits;
  67. VLC exp_vlc;
  68. int exponent_sizes[BLOCK_NB_SIZES];
  69. uint16_t exponent_bands[BLOCK_NB_SIZES][25];
  70. int high_band_start[BLOCK_NB_SIZES]; ///< index of first coef in high band
  71. int coefs_start; ///< first coded coef
  72. int coefs_end[BLOCK_NB_SIZES]; ///< max number of coded coefficients
  73. int exponent_high_sizes[BLOCK_NB_SIZES];
  74. int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
  75. VLC hgain_vlc;
  76. /* coded values in high bands */
  77. int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
  78. int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
  79. /* there are two possible tables for spectral coefficients */
  80. //FIXME the following 3 tables should be shared between decoders
  81. VLC coef_vlc[2];
  82. uint16_t *run_table[2];
  83. float *level_table[2];
  84. uint16_t *int_table[2];
  85. const CoefVLCTable *coef_vlcs[2];
  86. /* frame info */
  87. int frame_len; ///< frame length in samples
  88. int frame_len_bits; ///< frame_len = 1 << frame_len_bits
  89. int nb_block_sizes; ///< number of block sizes
  90. /* block info */
  91. int reset_block_lengths;
  92. int block_len_bits; ///< log2 of current block length
  93. int next_block_len_bits; ///< log2 of next block length
  94. int prev_block_len_bits; ///< log2 of prev block length
  95. int block_len; ///< block length in samples
  96. int block_num; ///< block number in current frame
  97. int block_pos; ///< current position in frame
  98. uint8_t ms_stereo; ///< true if mid/side stereo mode
  99. uint8_t channel_coded[MAX_CHANNELS]; ///< true if channel is coded
  100. int exponents_bsize[MAX_CHANNELS]; ///< log2 ratio frame/exp. length
  101. DECLARE_ALIGNED(32, float, exponents)[MAX_CHANNELS][BLOCK_MAX_SIZE];
  102. float max_exponent[MAX_CHANNELS];
  103. WMACoef coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
  104. DECLARE_ALIGNED(32, float, coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE];
  105. DECLARE_ALIGNED(32, FFTSample, output)[BLOCK_MAX_SIZE * 2];
  106. FFTContext mdct_ctx[BLOCK_NB_SIZES];
  107. float *windows[BLOCK_NB_SIZES];
  108. /* output buffer for one frame and the last for IMDCT windowing */
  109. DECLARE_ALIGNED(32, float, frame_out)[MAX_CHANNELS][BLOCK_MAX_SIZE * 2];
  110. /* last frame info */
  111. uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; /* padding added */
  112. int last_bitoffset;
  113. int last_superframe_len;
  114. float noise_table[NOISE_TAB_SIZE];
  115. int noise_index;
  116. float noise_mult; /* XXX: suppress that and integrate it in the noise array */
  117. /* lsp_to_curve tables */
  118. float lsp_cos_table[BLOCK_MAX_SIZE];
  119. float lsp_pow_e_table[256];
  120. float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
  121. float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
  122. DSPContext dsp;
  123. FmtConvertContext fmt_conv;
  124. #ifdef TRACE
  125. int frame_count;
  126. #endif
  127. } WMACodecContext;
  128. extern const uint16_t ff_wma_critical_freqs[25];
  129. extern const uint16_t ff_wma_hgain_huffcodes[37];
  130. extern const uint8_t ff_wma_hgain_huffbits[37];
  131. extern const float ff_wma_lsp_codebook[NB_LSP_COEFS][16];
  132. extern const uint32_t ff_aac_scalefactor_code[121];
  133. extern const uint8_t ff_aac_scalefactor_bits[121];
  134. int ff_wma_init(AVCodecContext * avctx, int flags2);
  135. int ff_wma_total_gain_to_bits(int total_gain);
  136. int ff_wma_end(AVCodecContext *avctx);
  137. unsigned int ff_wma_get_large_val(GetBitContext* gb);
  138. int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,
  139. VLC *vlc,
  140. const float *level_table, const uint16_t *run_table,
  141. int version, WMACoef *ptr, int offset,
  142. int num_coefs, int block_len, int frame_len_bits,
  143. int coef_nb_bits);
  144. #endif /* AVCODEC_WMA_H */