wma.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "libavutil/float_dsp.h"
  24. #include "avcodec.h"
  25. #include "fft.h"
  26. #include "get_bits.h"
  27. #include "put_bits.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 32768
  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. GetBitContext gb;
  55. PutBitContext pb;
  56. int version; ///< 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
  57. int use_bit_reservoir;
  58. int use_variable_block_len;
  59. int use_exp_vlc; ///< exponent coding: 0 = lsp, 1 = vlc + delta
  60. int use_noise_coding; ///< true if perceptual noise is added
  61. int byte_offset_bits;
  62. VLC exp_vlc;
  63. int exponent_sizes[BLOCK_NB_SIZES];
  64. uint16_t exponent_bands[BLOCK_NB_SIZES][25];
  65. int high_band_start[BLOCK_NB_SIZES]; ///< index of first coef in high band
  66. int coefs_start; ///< first coded coef
  67. int coefs_end[BLOCK_NB_SIZES]; ///< max number of coded coefficients
  68. int exponent_high_sizes[BLOCK_NB_SIZES];
  69. int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
  70. VLC hgain_vlc;
  71. /* coded values in high bands */
  72. int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
  73. int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
  74. /* there are two possible tables for spectral coefficients */
  75. // FIXME the following 3 tables should be shared between decoders
  76. VLC coef_vlc[2];
  77. uint16_t *run_table[2];
  78. float *level_table[2];
  79. uint16_t *int_table[2];
  80. const CoefVLCTable *coef_vlcs[2];
  81. /* frame info */
  82. int frame_len; ///< frame length in samples
  83. int frame_len_bits; ///< frame_len = 1 << frame_len_bits
  84. int nb_block_sizes; ///< number of block sizes
  85. /* block info */
  86. int reset_block_lengths;
  87. int block_len_bits; ///< log2 of current block length
  88. int next_block_len_bits; ///< log2 of next block length
  89. int prev_block_len_bits; ///< log2 of prev block length
  90. int block_len; ///< block length in samples
  91. int block_num; ///< block number in current frame
  92. int block_pos; ///< current position in frame
  93. uint8_t ms_stereo; ///< true if mid/side stereo mode
  94. uint8_t channel_coded[MAX_CHANNELS]; ///< true if channel is coded
  95. int exponents_bsize[MAX_CHANNELS]; ///< log2 ratio frame/exp. length
  96. DECLARE_ALIGNED(32, float, exponents)[MAX_CHANNELS][BLOCK_MAX_SIZE];
  97. float max_exponent[MAX_CHANNELS];
  98. WMACoef coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
  99. DECLARE_ALIGNED(32, float, coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE];
  100. DECLARE_ALIGNED(32, FFTSample, output)[BLOCK_MAX_SIZE * 2];
  101. FFTContext mdct_ctx[BLOCK_NB_SIZES];
  102. const float *windows[BLOCK_NB_SIZES];
  103. /* output buffer for one frame and the last for IMDCT windowing */
  104. DECLARE_ALIGNED(32, float, frame_out)[MAX_CHANNELS][BLOCK_MAX_SIZE * 2];
  105. /* last frame info */
  106. uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; /* padding added */
  107. int last_bitoffset;
  108. int last_superframe_len;
  109. int exponents_initialized[MAX_CHANNELS];
  110. float noise_table[NOISE_TAB_SIZE];
  111. int noise_index;
  112. float noise_mult; /* XXX: suppress that and integrate it in the noise array */
  113. /* lsp_to_curve tables */
  114. float lsp_cos_table[BLOCK_MAX_SIZE];
  115. float lsp_pow_e_table[256];
  116. float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
  117. float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
  118. AVFloatDSPContext *fdsp;
  119. #ifdef TRACE
  120. int frame_count;
  121. #endif /* TRACE */
  122. } WMACodecContext;
  123. extern const uint16_t ff_wma_hgain_huffcodes[37];
  124. extern const uint8_t ff_wma_hgain_huffbits[37];
  125. extern const float ff_wma_lsp_codebook[NB_LSP_COEFS][16];
  126. extern const uint32_t ff_aac_scalefactor_code[121];
  127. extern const uint8_t ff_aac_scalefactor_bits[121];
  128. av_warn_unused_result
  129. int ff_wma_init(AVCodecContext *avctx, int flags2);
  130. int ff_wma_total_gain_to_bits(int total_gain);
  131. int ff_wma_end(AVCodecContext *avctx);
  132. unsigned int ff_wma_get_large_val(GetBitContext *gb);
  133. int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
  134. VLC *vlc, const float *level_table,
  135. const uint16_t *run_table, int version,
  136. WMACoef *ptr, int offset, int num_coefs,
  137. int block_len, int frame_len_bits,
  138. int coef_nb_bits);
  139. #endif /* AVCODEC_WMA_H */