3 * Copyright (c) 2002-2007 The FFmpeg Project
5 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/float_dsp.h"
30 #include "fmtconvert.h"
33 #define BLOCK_MIN_BITS 7
34 #define BLOCK_MAX_BITS 11
35 #define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
37 #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
39 /* XXX: find exact max size */
40 #define HIGH_BAND_MAX_SIZE 16
42 #define NB_LSP_COEFS 10
44 /* XXX: is it a suitable value ? */
45 #define MAX_CODED_SUPERFRAME_SIZE 16384
47 #define MAX_CHANNELS 2
49 #define NOISE_TAB_SIZE 8192
51 #define LSP_POW_BITS 7
53 //FIXME should be in wmadec
55 #define VLCMAX ((22+VLCBITS-1)/VLCBITS)
57 typedef float WMACoef; ///< type for decoded coefficients, int16_t would be enough for wma 1/2
59 typedef struct CoefVLCTable {
60 int n; ///< total number of codes
62 const uint32_t *huffcodes; ///< VLC bit values
63 const uint8_t *huffbits; ///< VLC bit size
64 const uint16_t *levels; ///< table to build run/level tables
67 typedef struct WMACodecContext {
68 AVCodecContext* avctx;
75 int version; ///< 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
77 int use_bit_reservoir;
78 int use_variable_block_len;
79 int use_exp_vlc; ///< exponent coding: 0 = lsp, 1 = vlc + delta
80 int use_noise_coding; ///< true if perceptual noise is added
83 int exponent_sizes[BLOCK_NB_SIZES];
84 uint16_t exponent_bands[BLOCK_NB_SIZES][25];
85 int high_band_start[BLOCK_NB_SIZES]; ///< index of first coef in high band
86 int coefs_start; ///< first coded coef
87 int coefs_end[BLOCK_NB_SIZES]; ///< max number of coded coefficients
88 int exponent_high_sizes[BLOCK_NB_SIZES];
89 int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
92 /* coded values in high bands */
93 int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
94 int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
96 /* there are two possible tables for spectral coefficients */
97 //FIXME the following 3 tables should be shared between decoders
99 uint16_t *run_table[2];
100 float *level_table[2];
101 uint16_t *int_table[2];
102 const CoefVLCTable *coef_vlcs[2];
104 int frame_len; ///< frame length in samples
105 int frame_len_bits; ///< frame_len = 1 << frame_len_bits
106 int nb_block_sizes; ///< number of block sizes
108 int reset_block_lengths;
109 int block_len_bits; ///< log2 of current block length
110 int next_block_len_bits; ///< log2 of next block length
111 int prev_block_len_bits; ///< log2 of prev block length
112 int block_len; ///< block length in samples
113 int block_num; ///< block number in current frame
114 int block_pos; ///< current position in frame
115 uint8_t ms_stereo; ///< true if mid/side stereo mode
116 uint8_t channel_coded[MAX_CHANNELS]; ///< true if channel is coded
117 int exponents_bsize[MAX_CHANNELS]; ///< log2 ratio frame/exp. length
118 DECLARE_ALIGNED(32, float, exponents)[MAX_CHANNELS][BLOCK_MAX_SIZE];
119 float max_exponent[MAX_CHANNELS];
120 WMACoef coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
121 DECLARE_ALIGNED(32, float, coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE];
122 DECLARE_ALIGNED(32, FFTSample, output)[BLOCK_MAX_SIZE * 2];
123 FFTContext mdct_ctx[BLOCK_NB_SIZES];
124 float *windows[BLOCK_NB_SIZES];
125 /* output buffer for one frame and the last for IMDCT windowing */
126 DECLARE_ALIGNED(32, float, frame_out)[MAX_CHANNELS][BLOCK_MAX_SIZE * 2];
127 /* last frame info */
128 uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; /* padding added */
130 int last_superframe_len;
131 float noise_table[NOISE_TAB_SIZE];
133 float noise_mult; /* XXX: suppress that and integrate it in the noise array */
134 /* lsp_to_curve tables */
135 float lsp_cos_table[BLOCK_MAX_SIZE];
136 float lsp_pow_e_table[256];
137 float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
138 float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
140 FmtConvertContext fmt_conv;
141 AVFloatDSPContext fdsp;
148 extern const uint16_t ff_wma_critical_freqs[25];
149 extern const uint16_t ff_wma_hgain_huffcodes[37];
150 extern const uint8_t ff_wma_hgain_huffbits[37];
151 extern const float ff_wma_lsp_codebook[NB_LSP_COEFS][16];
152 extern const uint32_t ff_aac_scalefactor_code[121];
153 extern const uint8_t ff_aac_scalefactor_bits[121];
155 int ff_wma_init(AVCodecContext * avctx, int flags2);
156 int ff_wma_total_gain_to_bits(int total_gain);
157 int ff_wma_end(AVCodecContext *avctx);
158 unsigned int ff_wma_get_large_val(GetBitContext* gb);
159 int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,
161 const float *level_table, const uint16_t *run_table,
162 int version, WMACoef *ptr, int offset,
163 int num_coefs, int block_len, int frame_len_bits,
166 #endif /* AVCODEC_WMA_H */