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 <libasf/asf.h>
24 #include "ffmpeg_get_bits.h"
25 #include "types.h"
26 
27 //#define TRACE
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 
33 #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
34 
35 /* XXX: find exact max size */
36 #define HIGH_BAND_MAX_SIZE 16
37 
38 #define NB_LSP_COEFS 10
39 
40 /* XXX: is it a suitable value ? */
41 #define MAX_CODED_SUPERFRAME_SIZE 16384
42 
43 #define M_PI    3.14159265358979323846
44 
45 #define M_PI_F  0x3243f // in fixed 32 format
46 #define TWO_M_PI_F  0x6487f   //in fixed 32
47 
48 #define MAX_CHANNELS 2
49 
50 #define NOISE_TAB_SIZE 8192
51 
52 #define LSP_POW_BITS 7
53 
54 
55 #if (CONFIG_CPU == PP5022) || (CONFIG_CPU == PP5024) || (CONFIG_CPU == MCF5250)
56 /* PP5022/24 and MCF5250 have 128KB of IRAM. 80KB are allocated for codecs */
57 #define IBSS_ATTR_WMA_LARGE_IRAM IBSS_ATTR
58 #define IBSS_ATTR_WMA_XL_IRAM
59 #define ICONST_ATTR_WMA_XL_IRAM
60 
61 #elif defined(CPU_S5L870X)
62 /* S5L870x has even more IRAM. Use it. */
63 #define IBSS_ATTR_WMA_LARGE_IRAM IBSS_ATTR
64 #define IBSS_ATTR_WMA_XL_IRAM    IBSS_ATTR
65 #define ICONST_ATTR_WMA_XL_IRAM  ICONST_ATTR
66 
67 #else
68 /* other PP's and MCF5249 have 96KB of IRAM */
69 #define IBSS_ATTR_WMA_LARGE_IRAM
70 #define IBSS_ATTR_WMA_XL_IRAM
71 #define ICONST_ATTR_WMA_XL_IRAM
72 
73 #endif
74 
75 
76 #define VLCBITS 7       /*7 is the lowest without glitching*/
77 #define VLCMAX ((22+VLCBITS-1)/VLCBITS)
78 
79 #define EXPVLCBITS 7
80 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
81 
82 #define HGAINVLCBITS 9
83 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
84 
85 
86 typedef struct CoefVLCTable
87 {
88     int n;                               /* total number of codes */
89     const uint32_t *huffcodes;           /* VLC bit values */
90     const uint8_t *huffbits;             /* VLC bit size */
91     const uint16_t *levels;              /* table to build run/level tables */
92 }
93 CoefVLCTable;
94 
95 /* Define MEM_ALIGN_ATTR which may be used to align e.g. buffers for faster
96  * access. */
97 #if defined(CPU_ARM)
98     /* Use ARMs cache alignment. */
99     #define MEM_ALIGN_ATTR CACHEALIGN_ATTR
100     #define MEM_ALIGN_SIZE CACHEALIGN_SIZE
101 #elif defined(CPU_COLDFIRE)
102     /* Use fixed alignment of 16 bytes. Speed up only for 'movem' in DRAM. */
103     #define MEM_ALIGN_ATTR __attribute__((aligned(16)))
104     #define MEM_ALIGN_SIZE 16
105 #else
106     /* Align pointer size */
107     #define MEM_ALIGN_ATTR __attribute__((aligned(sizeof(intptr_t))))
108     #define MEM_ALIGN_SIZE sizeof(intptr_t)
109 #endif
110 
111 typedef struct WMADecodeContext
112 {
113     GetBitContext gb;
114 
115     int nb_block_sizes;  /* number of block sizes */
116 
117     int sample_rate;
118     int nb_channels;
119     int bit_rate;
120     int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */
121     int block_align;
122     int use_bit_reservoir;
123     int use_variable_block_len;
124     int use_exp_vlc;  /* exponent coding: 0 = lsp, 1 = vlc + delta */
125     int use_noise_coding; /* true if perceptual noise is added */
126     int byte_offset_bits;
127     VLC exp_vlc;
128     int exponent_sizes[BLOCK_NB_SIZES];
129     uint16_t exponent_bands[BLOCK_NB_SIZES][25];
130     int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */
131     int coefs_start;               /* first coded coef */
132     int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */
133     int exponent_high_sizes[BLOCK_NB_SIZES];
134     int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
135     VLC hgain_vlc;
136 
137     /* coded values in high bands */
138     int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
139     int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
140 
141     /* there are two possible tables for spectral coefficients */
142     VLC coef_vlc[2];
143     uint16_t *run_table[2];
144     uint16_t *level_table[2];
145     /* frame info */
146     int frame_len;       /* frame length in samples */
147     int frame_len_bits;  /* frame_len = 1 << frame_len_bits */
148 
149     /* block info */
150     int reset_block_lengths;
151     int block_len_bits; /* log2 of current block length */
152     int next_block_len_bits; /* log2 of next block length */
153     int prev_block_len_bits; /* log2 of prev block length */
154     int block_len; /* block length in samples */
155     int block_num; /* block number in current frame */
156     int block_pos; /* current position in frame */
157     uint8_t ms_stereo; /* true if mid/side stereo mode */
158     uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */
159     int exponents_bsize[MAX_CHANNELS];      // log2 ratio frame/exp. length
160     fixed32 exponents[MAX_CHANNELS][BLOCK_MAX_SIZE] MEM_ALIGN_ATTR;
161     fixed32 max_exponent[MAX_CHANNELS];
162     int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
163     fixed32 coefs[MAX_CHANNELS][BLOCK_MAX_SIZE];
164     fixed32 *windows[BLOCK_NB_SIZES];
165     /* output buffer for one frame and the last for IMDCT windowing */
166     fixed32 frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE*2];
167 
168     /* last frame info */
169     uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4] MEM_ALIGN_ATTR; /* padding added */
170     int last_bitoffset;
171     int last_superframe_len;
172     fixed32 *noise_table;
173     int noise_index;
174     fixed32 noise_mult; /* XXX: suppress that and integrate it in the noise array */
175     /* lsp_to_curve tables */
176     fixed32 lsp_cos_table[BLOCK_MAX_SIZE] MEM_ALIGN_ATTR;
177     void *lsp_pow_m_table1;
178     void *lsp_pow_m_table2;
179 
180     /* State of current superframe decoding */
181     int bit_offset;
182     int nb_frames;
183     int current_frame;
184 
185 #ifdef TRACE
186 
187     int frame_count;
188 #endif
189 }
190 WMADecodeContext;
191 
192 int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx);
193 int wma_decode_superframe_init(WMADecodeContext* s,
194                                const uint8_t *buf, int buf_size);
195 int wma_decode_superframe_frame(WMADecodeContext* s,
196                                 const uint8_t *buf, int buf_size);
197 #endif
198