1 /*
2  * WMA compatible decoder
3  * Copyright (c) 2002 The FFmpeg Project.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 #ifndef _WMADEC_H
21 #define _WMADEC_H
22 
23 #include <inttypes.h>
24 #include <math.h>
25 
26 #include "asf.h"
27 #include "bitstream.h" /* For GetBitContext */
28 #include "mdct.h"
29 
30 #undef TRACE
31 
32 /* size of blocks */
33 #define BLOCK_MIN_BITS 7
34 #define BLOCK_MAX_BITS 11
35 #define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
36 
37 #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
38 
39 /* XXX: find exact max size */
40 #define HIGH_BAND_MAX_SIZE 16
41 
42 #define NB_LSP_COEFS 10
43 
44 /* XXX: is it a suitable value ? */
45 #define MAX_CODED_SUPERFRAME_SIZE 16384
46 
47 #define M_PI_F  0x3243f // in fixed 32 format
48 #define TWO_M_PI_F  0x6487f   //in fixed 32
49 
50 #define MAX_CHANNELS 2
51 
52 #define NOISE_TAB_SIZE 8192
53 
54 #define LSP_POW_BITS 7
55 
56 typedef struct WMADecodeContext
57 {
58     GetBitContext gb;
59 
60     int nb_block_sizes;  /* number of block sizes */
61 
62     int sample_rate;
63     int nb_channels;
64     int bit_rate;
65     int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */
66     int block_align;
67     int use_bit_reservoir;
68     int use_variable_block_len;
69     int use_exp_vlc;  /* exponent coding: 0 = lsp, 1 = vlc + delta */
70     int use_noise_coding; /* true if perceptual noise is added */
71     int byte_offset_bits;
72     VLC exp_vlc;
73     int exponent_sizes[BLOCK_NB_SIZES];
74     uint16_t exponent_bands[BLOCK_NB_SIZES][25];
75     int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */
76     int coefs_start;               /* first coded coef */
77     int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */
78     int exponent_high_sizes[BLOCK_NB_SIZES];
79     int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
80     VLC hgain_vlc;
81 
82     /* coded values in high bands */
83     int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
84     int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
85 
86     /* there are two possible tables for spectral coefficients */
87     VLC coef_vlc[2];
88     uint16_t *run_table[2];
89     uint16_t *level_table[2];
90     /* frame info */
91     int frame_len;       /* frame length in samples */
92     int frame_len_bits;  /* frame_len = 1 << frame_len_bits */
93 
94     /* block info */
95     int reset_block_lengths;
96     int block_len_bits; /* log2 of current block length */
97     int next_block_len_bits; /* log2 of next block length */
98     int prev_block_len_bits; /* log2 of prev block length */
99     int block_len; /* block length in samples */
100     int block_num; /* block number in current frame */
101     int block_pos; /* current position in frame */
102     uint8_t ms_stereo; /* true if mid/side stereo mode */
103     uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */
104     int exponents_bsize[MAX_CHANNELS];      // log2 ratio frame/exp. length
105     int32_t exponents[MAX_CHANNELS][BLOCK_MAX_SIZE];
106     int32_t max_exponent[MAX_CHANNELS];
107     int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
108     int32_t (*coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE];
109     MDCTContext mdct_ctx[BLOCK_NB_SIZES];
110     int32_t *windows[BLOCK_NB_SIZES];
111     /* output buffer for one frame and the last for IMDCT windowing */
112     int32_t frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2];
113     /* last frame info */
114     uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */
115     int last_bitoffset;
116     int last_superframe_len;
117     int32_t *noise_table;
118     int noise_index;
119     int32_t noise_mult; /* XXX: suppress that and integrate it in the noise array */
120     /* lsp_to_curve tables */
121     int32_t lsp_cos_table[BLOCK_MAX_SIZE];
122     int64_t lsp_pow_e_table[256];
123     int32_t lsp_pow_m_table1[(1 << LSP_POW_BITS)];
124     int32_t lsp_pow_m_table2[(1 << LSP_POW_BITS)];
125 
126     /* State of current superframe decoding */
127     int bit_offset;
128     int nb_frames;
129     int current_frame;
130 
131 #ifdef TRACE
132 
133     int frame_count;
134 #endif
135 }
136 WMADecodeContext;
137 
138 int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx);
139 int wma_decode_superframe_init(WMADecodeContext* s,
140                                uint8_t *buf, int buf_size);
141 int wma_decode_superframe_frame(WMADecodeContext* s,
142                                 int32_t *samples,
143                                 uint8_t *buf, int buf_size);
144 #endif
145