Mercurial > libavcodec.hg
annotate wmadec.c @ 3018:1d22fe43aa78 libavcodec
use h264 MC functions for rectangular blocks too
author | michael |
---|---|
date | Sat, 07 Jan 2006 18:34:19 +0000 |
parents | f74ae8aff2a9 |
children | d4080a510724 |
rev | line source |
---|---|
783 | 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 */ | |
1106 | 19 |
20 /** | |
21 * @file wmadec.c | |
22 * WMA compatible decoder. | |
1967
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
23 * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2. |
2967 | 24 * WMA v1 is identified by audio format 0x160 in Microsoft media files |
1967
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
25 * (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161. |
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
26 * |
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
27 * To use this decoder, a calling application must supply the extra data |
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
28 * bytes provided with the WMA data. These are the extra, codec-specific |
2967 | 29 * bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes |
30 * to the decoder using the extradata[_size] fields in AVCodecContext. There | |
1967
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
31 * should be 4 extra bytes for v1 data and 6 extra bytes for v2 data. |
1106 | 32 */ |
33 | |
783 | 34 #include "avcodec.h" |
2398
582e635cfa08
common.c -> bitstream.c (and the single non bitstream func -> utils.c)
michael
parents:
2370
diff
changeset
|
35 #include "bitstream.h" |
783 | 36 #include "dsputil.h" |
37 | |
38 /* size of blocks */ | |
39 #define BLOCK_MIN_BITS 7 | |
40 #define BLOCK_MAX_BITS 11 | |
41 #define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS) | |
42 | |
43 #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1) | |
44 | |
45 /* XXX: find exact max size */ | |
46 #define HIGH_BAND_MAX_SIZE 16 | |
47 | |
48 #define NB_LSP_COEFS 10 | |
49 | |
817 | 50 /* XXX: is it a suitable value ? */ |
2775
f3cdd51c9e16
WMA MAX_CODED_SUPERFRAME_SIZE too small patch by (Mark Weaver: mark-clist, npsl co uk)
michael
parents:
2398
diff
changeset
|
51 #define MAX_CODED_SUPERFRAME_SIZE 16384 |
783 | 52 |
53 #define MAX_CHANNELS 2 | |
54 | |
55 #define NOISE_TAB_SIZE 8192 | |
56 | |
57 #define LSP_POW_BITS 7 | |
58 | |
59 typedef struct WMADecodeContext { | |
60 GetBitContext gb; | |
61 int sample_rate; | |
62 int nb_channels; | |
63 int bit_rate; | |
64 int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */ | |
65 int block_align; | |
66 int use_bit_reservoir; | |
67 int use_variable_block_len; | |
68 int use_exp_vlc; /* exponent coding: 0 = lsp, 1 = vlc + delta */ | |
69 int use_noise_coding; /* true if perceptual noise is added */ | |
70 int byte_offset_bits; | |
71 VLC exp_vlc; | |
72 int exponent_sizes[BLOCK_NB_SIZES]; | |
73 uint16_t exponent_bands[BLOCK_NB_SIZES][25]; | |
74 int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */ | |
75 int coefs_start; /* first coded coef */ | |
76 int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */ | |
77 int exponent_high_sizes[BLOCK_NB_SIZES]; | |
2967 | 78 int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE]; |
783 | 79 VLC hgain_vlc; |
2967 | 80 |
783 | 81 /* coded values in high bands */ |
82 int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE]; | |
83 int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE]; | |
84 | |
85 /* there are two possible tables for spectral coefficients */ | |
86 VLC coef_vlc[2]; | |
87 uint16_t *run_table[2]; | |
88 uint16_t *level_table[2]; | |
89 /* frame info */ | |
90 int frame_len; /* frame length in samples */ | |
91 int frame_len_bits; /* frame_len = 1 << frame_len_bits */ | |
92 int nb_block_sizes; /* number of block sizes */ | |
93 /* block info */ | |
94 int reset_block_lengths; | |
95 int block_len_bits; /* log2 of current block length */ | |
96 int next_block_len_bits; /* log2 of next block length */ | |
97 int prev_block_len_bits; /* log2 of prev block length */ | |
98 int block_len; /* block length in samples */ | |
99 int block_num; /* block number in current frame */ | |
100 int block_pos; /* current position in frame */ | |
101 uint8_t ms_stereo; /* true if mid/side stereo mode */ | |
102 uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */ | |
972 | 103 float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16))); |
783 | 104 float max_exponent[MAX_CHANNELS]; |
105 int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE]; | |
972 | 106 float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16))); |
783 | 107 MDCTContext mdct_ctx[BLOCK_NB_SIZES]; |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
1025
diff
changeset
|
108 float *windows[BLOCK_NB_SIZES]; |
972 | 109 FFTSample mdct_tmp[BLOCK_MAX_SIZE] __attribute__((aligned(16))); /* temporary storage for imdct */ |
783 | 110 /* output buffer for one frame and the last for IMDCT windowing */ |
972 | 111 float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] __attribute__((aligned(16))); |
783 | 112 /* last frame info */ |
113 uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */ | |
114 int last_bitoffset; | |
115 int last_superframe_len; | |
116 float noise_table[NOISE_TAB_SIZE]; | |
117 int noise_index; | |
118 float noise_mult; /* XXX: suppress that and integrate it in the noise array */ | |
119 /* lsp_to_curve tables */ | |
120 float lsp_cos_table[BLOCK_MAX_SIZE]; | |
121 float lsp_pow_e_table[256]; | |
122 float lsp_pow_m_table1[(1 << LSP_POW_BITS)]; | |
123 float lsp_pow_m_table2[(1 << LSP_POW_BITS)]; | |
1343 | 124 |
125 #ifdef TRACE | |
126 int frame_count; | |
127 #endif | |
783 | 128 } WMADecodeContext; |
129 | |
130 typedef struct CoefVLCTable { | |
131 int n; /* total number of codes */ | |
132 const uint32_t *huffcodes; /* VLC bit values */ | |
133 const uint8_t *huffbits; /* VLC bit size */ | |
134 const uint16_t *levels; /* table to build run/level tables */ | |
135 } CoefVLCTable; | |
136 | |
137 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len); | |
138 | |
139 #include "wmadata.h" | |
140 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
141 #ifdef TRACE |
783 | 142 static void dump_shorts(const char *name, const short *tab, int n) |
143 { | |
144 int i; | |
145 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
146 tprintf("%s[%d]:\n", name, n); |
783 | 147 for(i=0;i<n;i++) { |
148 if ((i & 7) == 0) | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
149 tprintf("%4d: ", i); |
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
150 tprintf(" %5d.0", tab[i]); |
783 | 151 if ((i & 7) == 7) |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
152 tprintf("\n"); |
783 | 153 } |
154 } | |
155 | |
156 static void dump_floats(const char *name, int prec, const float *tab, int n) | |
157 { | |
158 int i; | |
159 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
160 tprintf("%s[%d]:\n", name, n); |
783 | 161 for(i=0;i<n;i++) { |
162 if ((i & 7) == 0) | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
163 tprintf("%4d: ", i); |
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
164 tprintf(" %8.*f", prec, tab[i]); |
783 | 165 if ((i & 7) == 7) |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
166 tprintf("\n"); |
783 | 167 } |
168 if ((i & 7) != 0) | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
169 tprintf("\n"); |
783 | 170 } |
171 #endif | |
172 | |
173 /* XXX: use same run/length optimization as mpeg decoders */ | |
2967 | 174 static void init_coef_vlc(VLC *vlc, |
783 | 175 uint16_t **prun_table, uint16_t **plevel_table, |
176 const CoefVLCTable *vlc_table) | |
177 { | |
178 int n = vlc_table->n; | |
179 const uint8_t *table_bits = vlc_table->huffbits; | |
180 const uint32_t *table_codes = vlc_table->huffcodes; | |
181 const uint16_t *levels_table = vlc_table->levels; | |
182 uint16_t *run_table, *level_table; | |
183 const uint16_t *p; | |
184 int i, l, j, level; | |
185 | |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2180
diff
changeset
|
186 init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4, 0); |
783 | 187 |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
1025
diff
changeset
|
188 run_table = av_malloc(n * sizeof(uint16_t)); |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
1025
diff
changeset
|
189 level_table = av_malloc(n * sizeof(uint16_t)); |
783 | 190 p = levels_table; |
191 i = 2; | |
192 level = 1; | |
193 while (i < n) { | |
194 l = *p++; | |
195 for(j=0;j<l;j++) { | |
196 run_table[i] = j; | |
197 level_table[i] = level; | |
198 i++; | |
199 } | |
200 level++; | |
201 } | |
202 *prun_table = run_table; | |
203 *plevel_table = level_table; | |
204 } | |
205 | |
206 static int wma_decode_init(AVCodecContext * avctx) | |
207 { | |
208 WMADecodeContext *s = avctx->priv_data; | |
209 int i, flags1, flags2; | |
210 float *window; | |
211 uint8_t *extradata; | |
2180
7524cda1604e
k6 pop fix patch by (Diego Biurrun <diego at biurrun dot de>)
michael
parents:
2101
diff
changeset
|
212 float bps1, high_freq; |
7524cda1604e
k6 pop fix patch by (Diego Biurrun <diego at biurrun dot de>)
michael
parents:
2101
diff
changeset
|
213 volatile float bps; |
783 | 214 int sample_rate1; |
215 int coef_vlc_table; | |
2967 | 216 |
783 | 217 s->sample_rate = avctx->sample_rate; |
218 s->nb_channels = avctx->channels; | |
219 s->bit_rate = avctx->bit_rate; | |
220 s->block_align = avctx->block_align; | |
221 | |
808
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
222 if (avctx->codec->id == CODEC_ID_WMAV1) { |
783 | 223 s->version = 1; |
224 } else { | |
225 s->version = 2; | |
226 } | |
2967 | 227 |
783 | 228 /* extract flag infos */ |
229 flags1 = 0; | |
230 flags2 = 0; | |
231 extradata = avctx->extradata; | |
232 if (s->version == 1 && avctx->extradata_size >= 4) { | |
233 flags1 = extradata[0] | (extradata[1] << 8); | |
234 flags2 = extradata[2] | (extradata[3] << 8); | |
235 } else if (s->version == 2 && avctx->extradata_size >= 6) { | |
2967 | 236 flags1 = extradata[0] | (extradata[1] << 8) | |
783 | 237 (extradata[2] << 16) | (extradata[3] << 24); |
238 flags2 = extradata[4] | (extradata[5] << 8); | |
239 } | |
240 s->use_exp_vlc = flags2 & 0x0001; | |
241 s->use_bit_reservoir = flags2 & 0x0002; | |
242 s->use_variable_block_len = flags2 & 0x0004; | |
243 | |
244 /* compute MDCT block size */ | |
245 if (s->sample_rate <= 16000) { | |
246 s->frame_len_bits = 9; | |
2967 | 247 } else if (s->sample_rate <= 22050 || |
795
55add0e7eafb
avoid name clash - fixed again block size selection
bellard
parents:
785
diff
changeset
|
248 (s->sample_rate <= 32000 && s->version == 1)) { |
783 | 249 s->frame_len_bits = 10; |
250 } else { | |
251 s->frame_len_bits = 11; | |
252 } | |
253 s->frame_len = 1 << s->frame_len_bits; | |
254 if (s->use_variable_block_len) { | |
808
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
255 int nb_max, nb; |
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
256 nb = ((flags2 >> 3) & 3) + 1; |
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
257 if ((s->bit_rate / s->nb_channels) >= 32000) |
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
258 nb += 2; |
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
259 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; |
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
260 if (nb > nb_max) |
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
261 nb = nb_max; |
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
262 s->nb_block_sizes = nb + 1; |
783 | 263 } else { |
264 s->nb_block_sizes = 1; | |
265 } | |
266 | |
267 /* init rate dependant parameters */ | |
268 s->use_noise_coding = 1; | |
269 high_freq = s->sample_rate * 0.5; | |
270 | |
271 /* if version 2, then the rates are normalized */ | |
272 sample_rate1 = s->sample_rate; | |
273 if (s->version == 2) { | |
2967 | 274 if (sample_rate1 >= 44100) |
783 | 275 sample_rate1 = 44100; |
2967 | 276 else if (sample_rate1 >= 22050) |
783 | 277 sample_rate1 = 22050; |
2967 | 278 else if (sample_rate1 >= 16000) |
783 | 279 sample_rate1 = 16000; |
2967 | 280 else if (sample_rate1 >= 11025) |
783 | 281 sample_rate1 = 11025; |
2967 | 282 else if (sample_rate1 >= 8000) |
783 | 283 sample_rate1 = 8000; |
284 } | |
285 | |
286 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate); | |
2992 | 287 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2; |
783 | 288 |
289 /* compute high frequency value and choose if noise coding should | |
290 be activated */ | |
291 bps1 = bps; | |
292 if (s->nb_channels == 2) | |
293 bps1 = bps * 1.6; | |
294 if (sample_rate1 == 44100) { | |
295 if (bps1 >= 0.61) | |
296 s->use_noise_coding = 0; | |
297 else | |
298 high_freq = high_freq * 0.4; | |
299 } else if (sample_rate1 == 22050) { | |
300 if (bps1 >= 1.16) | |
301 s->use_noise_coding = 0; | |
2967 | 302 else if (bps1 >= 0.72) |
783 | 303 high_freq = high_freq * 0.7; |
304 else | |
305 high_freq = high_freq * 0.6; | |
306 } else if (sample_rate1 == 16000) { | |
307 if (bps > 0.5) | |
308 high_freq = high_freq * 0.5; | |
309 else | |
310 high_freq = high_freq * 0.3; | |
311 } else if (sample_rate1 == 11025) { | |
312 high_freq = high_freq * 0.7; | |
313 } else if (sample_rate1 == 8000) { | |
314 if (bps <= 0.625) { | |
315 high_freq = high_freq * 0.5; | |
316 } else if (bps > 0.75) { | |
317 s->use_noise_coding = 0; | |
318 } else { | |
319 high_freq = high_freq * 0.65; | |
320 } | |
321 } else { | |
322 if (bps >= 0.8) { | |
323 high_freq = high_freq * 0.75; | |
324 } else if (bps >= 0.6) { | |
325 high_freq = high_freq * 0.6; | |
326 } else { | |
327 high_freq = high_freq * 0.5; | |
328 } | |
329 } | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
330 dprintf("flags1=0x%x flags2=0x%x\n", flags1, flags2); |
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
331 dprintf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n", |
2967 | 332 s->version, s->nb_channels, s->sample_rate, s->bit_rate, |
783 | 333 s->block_align); |
2967 | 334 dprintf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n", |
783 | 335 bps, bps1, high_freq, s->byte_offset_bits); |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
336 dprintf("use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n", |
808
e9bfaabcf07d
fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
bellard
parents:
797
diff
changeset
|
337 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes); |
783 | 338 |
339 /* compute the scale factor band sizes for each MDCT block size */ | |
340 { | |
341 int a, b, pos, lpos, k, block_len, i, j, n; | |
342 const uint8_t *table; | |
2967 | 343 |
783 | 344 if (s->version == 1) { |
345 s->coefs_start = 3; | |
346 } else { | |
347 s->coefs_start = 0; | |
348 } | |
349 for(k = 0; k < s->nb_block_sizes; k++) { | |
350 block_len = s->frame_len >> k; | |
351 | |
352 if (s->version == 1) { | |
353 lpos = 0; | |
354 for(i=0;i<25;i++) { | |
355 a = wma_critical_freqs[i]; | |
356 b = s->sample_rate; | |
357 pos = ((block_len * 2 * a) + (b >> 1)) / b; | |
2967 | 358 if (pos > block_len) |
783 | 359 pos = block_len; |
360 s->exponent_bands[0][i] = pos - lpos; | |
361 if (pos >= block_len) { | |
362 i++; | |
363 break; | |
364 } | |
365 lpos = pos; | |
366 } | |
367 s->exponent_sizes[0] = i; | |
368 } else { | |
369 /* hardcoded tables */ | |
370 table = NULL; | |
371 a = s->frame_len_bits - BLOCK_MIN_BITS - k; | |
372 if (a < 3) { | |
373 if (s->sample_rate >= 44100) | |
374 table = exponent_band_44100[a]; | |
375 else if (s->sample_rate >= 32000) | |
376 table = exponent_band_32000[a]; | |
377 else if (s->sample_rate >= 22050) | |
378 table = exponent_band_22050[a]; | |
379 } | |
380 if (table) { | |
381 n = *table++; | |
382 for(i=0;i<n;i++) | |
383 s->exponent_bands[k][i] = table[i]; | |
384 s->exponent_sizes[k] = n; | |
385 } else { | |
386 j = 0; | |
387 lpos = 0; | |
388 for(i=0;i<25;i++) { | |
389 a = wma_critical_freqs[i]; | |
390 b = s->sample_rate; | |
391 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b); | |
392 pos <<= 2; | |
2967 | 393 if (pos > block_len) |
783 | 394 pos = block_len; |
395 if (pos > lpos) | |
396 s->exponent_bands[k][j++] = pos - lpos; | |
397 if (pos >= block_len) | |
398 break; | |
399 lpos = pos; | |
400 } | |
401 s->exponent_sizes[k] = j; | |
402 } | |
403 } | |
404 | |
405 /* max number of coefs */ | |
406 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k; | |
407 /* high freq computation */ | |
2967 | 408 s->high_band_start[k] = (int)((block_len * 2 * high_freq) / |
783 | 409 s->sample_rate + 0.5); |
410 n = s->exponent_sizes[k]; | |
411 j = 0; | |
412 pos = 0; | |
413 for(i=0;i<n;i++) { | |
414 int start, end; | |
415 start = pos; | |
416 pos += s->exponent_bands[k][i]; | |
417 end = pos; | |
418 if (start < s->high_band_start[k]) | |
419 start = s->high_band_start[k]; | |
420 if (end > s->coefs_end[k]) | |
421 end = s->coefs_end[k]; | |
422 if (end > start) | |
423 s->exponent_high_bands[k][j++] = end - start; | |
424 } | |
425 s->exponent_high_sizes[k] = j; | |
426 #if 0 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
427 tprintf("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ", |
2967 | 428 s->frame_len >> k, |
783 | 429 s->coefs_end[k], |
430 s->high_band_start[k], | |
431 s->exponent_high_sizes[k]); | |
432 for(j=0;j<s->exponent_high_sizes[k];j++) | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
433 tprintf(" %d", s->exponent_high_bands[k][j]); |
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
434 tprintf("\n"); |
783 | 435 #endif |
436 } | |
437 } | |
438 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
439 #ifdef TRACE |
783 | 440 { |
441 int i, j; | |
442 for(i = 0; i < s->nb_block_sizes; i++) { | |
2967 | 443 tprintf("%5d: n=%2d:", |
444 s->frame_len >> i, | |
783 | 445 s->exponent_sizes[i]); |
446 for(j=0;j<s->exponent_sizes[i];j++) | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
447 tprintf(" %d", s->exponent_bands[i][j]); |
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
448 tprintf("\n"); |
783 | 449 } |
450 } | |
451 #endif | |
452 | |
453 /* init MDCT */ | |
454 for(i = 0; i < s->nb_block_sizes; i++) | |
795
55add0e7eafb
avoid name clash - fixed again block size selection
bellard
parents:
785
diff
changeset
|
455 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1); |
2967 | 456 |
783 | 457 /* init MDCT windows : simple sinus window */ |
458 for(i = 0; i < s->nb_block_sizes; i++) { | |
459 int n, j; | |
460 float alpha; | |
461 n = 1 << (s->frame_len_bits - i); | |
462 window = av_malloc(sizeof(float) * n); | |
463 alpha = M_PI / (2.0 * n); | |
464 for(j=0;j<n;j++) { | |
465 window[n - j - 1] = sin((j + 0.5) * alpha); | |
466 } | |
467 s->windows[i] = window; | |
468 } | |
469 | |
470 s->reset_block_lengths = 1; | |
2967 | 471 |
783 | 472 if (s->use_noise_coding) { |
473 | |
474 /* init the noise generator */ | |
475 if (s->use_exp_vlc) | |
476 s->noise_mult = 0.02; | |
477 else | |
478 s->noise_mult = 0.04; | |
2967 | 479 |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
480 #ifdef TRACE |
783 | 481 for(i=0;i<NOISE_TAB_SIZE;i++) |
482 s->noise_table[i] = 1.0 * s->noise_mult; | |
483 #else | |
484 { | |
485 unsigned int seed; | |
486 float norm; | |
487 seed = 1; | |
488 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult; | |
489 for(i=0;i<NOISE_TAB_SIZE;i++) { | |
490 seed = seed * 314159 + 1; | |
491 s->noise_table[i] = (float)((int)seed) * norm; | |
492 } | |
493 } | |
494 #endif | |
2967 | 495 init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits), |
783 | 496 hgain_huffbits, 1, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2180
diff
changeset
|
497 hgain_huffcodes, 2, 2, 0); |
783 | 498 } |
499 | |
500 if (s->use_exp_vlc) { | |
2967 | 501 init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits), |
783 | 502 scale_huffbits, 1, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2180
diff
changeset
|
503 scale_huffcodes, 4, 4, 0); |
783 | 504 } else { |
505 wma_lsp_to_curve_init(s, s->frame_len); | |
506 } | |
507 | |
508 /* choose the VLC tables for the coefficients */ | |
509 coef_vlc_table = 2; | |
510 if (s->sample_rate >= 32000) { | |
511 if (bps1 < 0.72) | |
512 coef_vlc_table = 0; | |
513 else if (bps1 < 1.16) | |
514 coef_vlc_table = 1; | |
515 } | |
516 | |
517 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], | |
518 &coef_vlcs[coef_vlc_table * 2]); | |
519 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], | |
520 &coef_vlcs[coef_vlc_table * 2 + 1]); | |
521 return 0; | |
522 } | |
523 | |
524 /* interpolate values for a bigger or smaller block. The block must | |
525 have multiple sizes */ | |
526 static void interpolate_array(float *scale, int old_size, int new_size) | |
527 { | |
528 int i, j, jincr, k; | |
529 float v; | |
530 | |
531 if (new_size > old_size) { | |
532 jincr = new_size / old_size; | |
533 j = new_size; | |
534 for(i = old_size - 1; i >=0; i--) { | |
535 v = scale[i]; | |
536 k = jincr; | |
537 do { | |
538 scale[--j] = v; | |
539 } while (--k); | |
540 } | |
541 } else if (new_size < old_size) { | |
542 j = 0; | |
543 jincr = old_size / new_size; | |
544 for(i = 0; i < new_size; i++) { | |
545 scale[i] = scale[j]; | |
546 j += jincr; | |
547 } | |
548 } | |
549 } | |
550 | |
551 /* compute x^-0.25 with an exponent and mantissa table. We use linear | |
552 interpolation to reduce the mantissa table size at a small speed | |
553 expense (linear interpolation approximately doubles the number of | |
554 bits of precision). */ | |
555 static inline float pow_m1_4(WMADecodeContext *s, float x) | |
556 { | |
557 union { | |
558 float f; | |
559 unsigned int v; | |
560 } u, t; | |
561 unsigned int e, m; | |
562 float a, b; | |
563 | |
564 u.f = x; | |
565 e = u.v >> 23; | |
566 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1); | |
567 /* build interpolation scale: 1 <= t < 2. */ | |
568 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23); | |
569 a = s->lsp_pow_m_table1[m]; | |
570 b = s->lsp_pow_m_table2[m]; | |
571 return s->lsp_pow_e_table[e] * (a + b * t.f); | |
572 } | |
573 | |
574 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len) | |
2967 | 575 { |
783 | 576 float wdel, a, b; |
577 int i, e, m; | |
578 | |
579 wdel = M_PI / frame_len; | |
580 for(i=0;i<frame_len;i++) | |
581 s->lsp_cos_table[i] = 2.0f * cos(wdel * i); | |
582 | |
583 /* tables for x^-0.25 computation */ | |
584 for(i=0;i<256;i++) { | |
585 e = i - 126; | |
586 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25); | |
587 } | |
588 | |
589 /* NOTE: these two tables are needed to avoid two operations in | |
590 pow_m1_4 */ | |
591 b = 1.0; | |
592 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) { | |
593 m = (1 << LSP_POW_BITS) + i; | |
594 a = (float)m * (0.5 / (1 << LSP_POW_BITS)); | |
595 a = pow(a, -0.25); | |
596 s->lsp_pow_m_table1[i] = 2 * a - b; | |
597 s->lsp_pow_m_table2[i] = b - a; | |
598 b = a; | |
599 } | |
600 #if 0 | |
601 for(i=1;i<20;i++) { | |
602 float v, r1, r2; | |
603 v = 5.0 / i; | |
604 r1 = pow_m1_4(s, v); | |
605 r2 = pow(v,-0.25); | |
606 printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1); | |
607 } | |
608 #endif | |
609 } | |
610 | |
611 /* NOTE: We use the same code as Vorbis here */ | |
612 /* XXX: optimize it further with SSE/3Dnow */ | |
2967 | 613 static void wma_lsp_to_curve(WMADecodeContext *s, |
614 float *out, float *val_max_ptr, | |
783 | 615 int n, float *lsp) |
616 { | |
617 int i, j; | |
618 float p, q, w, v, val_max; | |
619 | |
620 val_max = 0; | |
621 for(i=0;i<n;i++) { | |
622 p = 0.5f; | |
623 q = 0.5f; | |
624 w = s->lsp_cos_table[i]; | |
625 for(j=1;j<NB_LSP_COEFS;j+=2){ | |
626 q *= w - lsp[j - 1]; | |
627 p *= w - lsp[j]; | |
628 } | |
629 p *= p * (2.0f - w); | |
630 q *= q * (2.0f + w); | |
631 v = p + q; | |
632 v = pow_m1_4(s, v); | |
633 if (v > val_max) | |
634 val_max = v; | |
635 out[i] = v; | |
636 } | |
637 *val_max_ptr = val_max; | |
638 } | |
639 | |
640 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */ | |
641 static void decode_exp_lsp(WMADecodeContext *s, int ch) | |
642 { | |
643 float lsp_coefs[NB_LSP_COEFS]; | |
644 int val, i; | |
645 | |
646 for(i = 0; i < NB_LSP_COEFS; i++) { | |
647 if (i == 0 || i >= 8) | |
648 val = get_bits(&s->gb, 3); | |
649 else | |
650 val = get_bits(&s->gb, 4); | |
651 lsp_coefs[i] = lsp_codebook[i][val]; | |
652 } | |
653 | |
654 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch], | |
655 s->block_len, lsp_coefs); | |
656 } | |
657 | |
658 /* decode exponents coded with VLC codes */ | |
659 static int decode_exp_vlc(WMADecodeContext *s, int ch) | |
660 { | |
661 int last_exp, n, code; | |
662 const uint16_t *ptr, *band_ptr; | |
663 float v, *q, max_scale, *q_end; | |
2967 | 664 |
783 | 665 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits]; |
666 ptr = band_ptr; | |
667 q = s->exponents[ch]; | |
668 q_end = q + s->block_len; | |
669 max_scale = 0; | |
670 if (s->version == 1) { | |
671 last_exp = get_bits(&s->gb, 5) + 10; | |
672 /* XXX: use a table */ | |
673 v = pow(10, last_exp * (1.0 / 16.0)); | |
674 max_scale = v; | |
675 n = *ptr++; | |
676 do { | |
677 *q++ = v; | |
678 } while (--n); | |
679 } | |
680 last_exp = 36; | |
681 while (q < q_end) { | |
682 code = get_vlc(&s->gb, &s->exp_vlc); | |
683 if (code < 0) | |
684 return -1; | |
685 /* NOTE: this offset is the same as MPEG4 AAC ! */ | |
686 last_exp += code - 60; | |
687 /* XXX: use a table */ | |
688 v = pow(10, last_exp * (1.0 / 16.0)); | |
689 if (v > max_scale) | |
690 max_scale = v; | |
691 n = *ptr++; | |
692 do { | |
693 *q++ = v; | |
694 } while (--n); | |
695 } | |
696 s->max_exponent[ch] = max_scale; | |
697 return 0; | |
698 } | |
699 | |
700 /* return 0 if OK. return 1 if last block of frame. return -1 if | |
701 unrecorrable error. */ | |
702 static int wma_decode_block(WMADecodeContext *s) | |
703 { | |
704 int n, v, a, ch, code, bsize; | |
705 int coef_nb_bits, total_gain, parse_exponents; | |
706 float window[BLOCK_MAX_SIZE * 2]; | |
2101 | 707 // XXX: FIXME!! there's a bug somewhere which makes this mandatory under altivec |
708 #ifdef HAVE_ALTIVEC | |
709 volatile int nb_coefs[MAX_CHANNELS] __attribute__((aligned(16))); | |
710 #else | |
783 | 711 int nb_coefs[MAX_CHANNELS]; |
2101 | 712 #endif |
783 | 713 float mdct_norm; |
714 | |
1343 | 715 #ifdef TRACE |
716 tprintf("***decode_block: %d:%d\n", s->frame_count - 1, s->block_num); | |
717 #endif | |
783 | 718 |
719 /* compute current block length */ | |
720 if (s->use_variable_block_len) { | |
721 n = av_log2(s->nb_block_sizes - 1) + 1; | |
2967 | 722 |
783 | 723 if (s->reset_block_lengths) { |
724 s->reset_block_lengths = 0; | |
725 v = get_bits(&s->gb, n); | |
726 if (v >= s->nb_block_sizes) | |
727 return -1; | |
728 s->prev_block_len_bits = s->frame_len_bits - v; | |
729 v = get_bits(&s->gb, n); | |
730 if (v >= s->nb_block_sizes) | |
731 return -1; | |
732 s->block_len_bits = s->frame_len_bits - v; | |
733 } else { | |
734 /* update block lengths */ | |
735 s->prev_block_len_bits = s->block_len_bits; | |
736 s->block_len_bits = s->next_block_len_bits; | |
737 } | |
738 v = get_bits(&s->gb, n); | |
739 if (v >= s->nb_block_sizes) | |
740 return -1; | |
741 s->next_block_len_bits = s->frame_len_bits - v; | |
742 } else { | |
743 /* fixed block len */ | |
744 s->next_block_len_bits = s->frame_len_bits; | |
745 s->prev_block_len_bits = s->frame_len_bits; | |
746 s->block_len_bits = s->frame_len_bits; | |
747 } | |
748 | |
749 /* now check if the block length is coherent with the frame length */ | |
750 s->block_len = 1 << s->block_len_bits; | |
751 if ((s->block_pos + s->block_len) > s->frame_len) | |
752 return -1; | |
753 | |
754 if (s->nb_channels == 2) { | |
755 s->ms_stereo = get_bits(&s->gb, 1); | |
756 } | |
757 v = 0; | |
758 for(ch = 0; ch < s->nb_channels; ch++) { | |
759 a = get_bits(&s->gb, 1); | |
760 s->channel_coded[ch] = a; | |
761 v |= a; | |
762 } | |
763 /* if no channel coded, no need to go further */ | |
764 /* XXX: fix potential framing problems */ | |
765 if (!v) | |
766 goto next; | |
767 | |
768 bsize = s->frame_len_bits - s->block_len_bits; | |
769 | |
770 /* read total gain and extract corresponding number of bits for | |
771 coef escape coding */ | |
772 total_gain = 1; | |
773 for(;;) { | |
774 a = get_bits(&s->gb, 7); | |
775 total_gain += a; | |
776 if (a != 127) | |
777 break; | |
778 } | |
2967 | 779 |
783 | 780 if (total_gain < 15) |
781 coef_nb_bits = 13; | |
782 else if (total_gain < 32) | |
783 coef_nb_bits = 12; | |
784 else if (total_gain < 40) | |
785 coef_nb_bits = 11; | |
786 else if (total_gain < 45) | |
787 coef_nb_bits = 10; | |
788 else | |
789 coef_nb_bits = 9; | |
790 | |
791 /* compute number of coefficients */ | |
792 n = s->coefs_end[bsize] - s->coefs_start; | |
793 for(ch = 0; ch < s->nb_channels; ch++) | |
794 nb_coefs[ch] = n; | |
795 | |
796 /* complex coding */ | |
797 if (s->use_noise_coding) { | |
798 | |
799 for(ch = 0; ch < s->nb_channels; ch++) { | |
800 if (s->channel_coded[ch]) { | |
801 int i, n, a; | |
802 n = s->exponent_high_sizes[bsize]; | |
803 for(i=0;i<n;i++) { | |
804 a = get_bits(&s->gb, 1); | |
805 s->high_band_coded[ch][i] = a; | |
806 /* if noise coding, the coefficients are not transmitted */ | |
807 if (a) | |
808 nb_coefs[ch] -= s->exponent_high_bands[bsize][i]; | |
809 } | |
810 } | |
811 } | |
812 for(ch = 0; ch < s->nb_channels; ch++) { | |
813 if (s->channel_coded[ch]) { | |
814 int i, n, val, code; | |
815 | |
816 n = s->exponent_high_sizes[bsize]; | |
817 val = (int)0x80000000; | |
818 for(i=0;i<n;i++) { | |
819 if (s->high_band_coded[ch][i]) { | |
820 if (val == (int)0x80000000) { | |
821 val = get_bits(&s->gb, 7) - 19; | |
822 } else { | |
823 code = get_vlc(&s->gb, &s->hgain_vlc); | |
824 if (code < 0) | |
825 return -1; | |
826 val += code - 18; | |
827 } | |
828 s->high_band_values[ch][i] = val; | |
829 } | |
830 } | |
831 } | |
832 } | |
833 } | |
2967 | 834 |
783 | 835 /* exposant can be interpolated in short blocks. */ |
836 parse_exponents = 1; | |
837 if (s->block_len_bits != s->frame_len_bits) { | |
838 parse_exponents = get_bits(&s->gb, 1); | |
839 } | |
2967 | 840 |
783 | 841 if (parse_exponents) { |
842 for(ch = 0; ch < s->nb_channels; ch++) { | |
843 if (s->channel_coded[ch]) { | |
844 if (s->use_exp_vlc) { | |
845 if (decode_exp_vlc(s, ch) < 0) | |
846 return -1; | |
847 } else { | |
848 decode_exp_lsp(s, ch); | |
849 } | |
850 } | |
851 } | |
852 } else { | |
853 for(ch = 0; ch < s->nb_channels; ch++) { | |
854 if (s->channel_coded[ch]) { | |
2967 | 855 interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits, |
783 | 856 s->block_len); |
857 } | |
858 } | |
859 } | |
860 | |
861 /* parse spectral coefficients : just RLE encoding */ | |
862 for(ch = 0; ch < s->nb_channels; ch++) { | |
863 if (s->channel_coded[ch]) { | |
864 VLC *coef_vlc; | |
865 int level, run, sign, tindex; | |
866 int16_t *ptr, *eptr; | |
867 const int16_t *level_table, *run_table; | |
868 | |
869 /* special VLC tables are used for ms stereo because | |
870 there is potentially less energy there */ | |
871 tindex = (ch == 1 && s->ms_stereo); | |
872 coef_vlc = &s->coef_vlc[tindex]; | |
873 run_table = s->run_table[tindex]; | |
874 level_table = s->level_table[tindex]; | |
875 /* XXX: optimize */ | |
876 ptr = &s->coefs1[ch][0]; | |
877 eptr = ptr + nb_coefs[ch]; | |
878 memset(ptr, 0, s->block_len * sizeof(int16_t)); | |
879 for(;;) { | |
880 code = get_vlc(&s->gb, coef_vlc); | |
881 if (code < 0) | |
882 return -1; | |
883 if (code == 1) { | |
884 /* EOB */ | |
885 break; | |
886 } else if (code == 0) { | |
887 /* escape */ | |
888 level = get_bits(&s->gb, coef_nb_bits); | |
889 /* NOTE: this is rather suboptimal. reading | |
890 block_len_bits would be better */ | |
891 run = get_bits(&s->gb, s->frame_len_bits); | |
892 } else { | |
893 /* normal code */ | |
894 run = run_table[code]; | |
895 level = level_table[code]; | |
896 } | |
897 sign = get_bits(&s->gb, 1); | |
898 if (!sign) | |
899 level = -level; | |
900 ptr += run; | |
901 if (ptr >= eptr) | |
902 return -1; | |
903 *ptr++ = level; | |
904 /* NOTE: EOB can be omitted */ | |
905 if (ptr >= eptr) | |
906 break; | |
907 } | |
908 } | |
909 if (s->version == 1 && s->nb_channels >= 2) { | |
910 align_get_bits(&s->gb); | |
911 } | |
912 } | |
2967 | 913 |
783 | 914 /* normalize */ |
915 { | |
916 int n4 = s->block_len / 2; | |
917 mdct_norm = 1.0 / (float)n4; | |
918 if (s->version == 1) { | |
919 mdct_norm *= sqrt(n4); | |
920 } | |
921 } | |
922 | |
923 /* finally compute the MDCT coefficients */ | |
924 for(ch = 0; ch < s->nb_channels; ch++) { | |
925 if (s->channel_coded[ch]) { | |
926 int16_t *coefs1; | |
927 float *coefs, *exponents, mult, mult1, noise, *exp_ptr; | |
928 int i, j, n, n1, last_high_band; | |
929 float exp_power[HIGH_BAND_MAX_SIZE]; | |
930 | |
931 coefs1 = s->coefs1[ch]; | |
932 exponents = s->exponents[ch]; | |
933 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch]; | |
934 mult *= mdct_norm; | |
935 coefs = s->coefs[ch]; | |
936 if (s->use_noise_coding) { | |
937 mult1 = mult; | |
938 /* very low freqs : noise */ | |
939 for(i = 0;i < s->coefs_start; i++) { | |
940 *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1; | |
941 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
942 } | |
2967 | 943 |
783 | 944 n1 = s->exponent_high_sizes[bsize]; |
945 | |
946 /* compute power of high bands */ | |
2967 | 947 exp_ptr = exponents + |
948 s->high_band_start[bsize] - | |
783 | 949 s->coefs_start; |
950 last_high_band = 0; /* avoid warning */ | |
951 for(j=0;j<n1;j++) { | |
2967 | 952 n = s->exponent_high_bands[s->frame_len_bits - |
783 | 953 s->block_len_bits][j]; |
954 if (s->high_band_coded[ch][j]) { | |
955 float e2, v; | |
956 e2 = 0; | |
957 for(i = 0;i < n; i++) { | |
958 v = exp_ptr[i]; | |
959 e2 += v * v; | |
960 } | |
961 exp_power[j] = e2 / n; | |
962 last_high_band = j; | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
963 tprintf("%d: power=%f (%d)\n", j, exp_power[j], n); |
783 | 964 } |
965 exp_ptr += n; | |
966 } | |
967 | |
968 /* main freqs and high freqs */ | |
969 for(j=-1;j<n1;j++) { | |
970 if (j < 0) { | |
2967 | 971 n = s->high_band_start[bsize] - |
783 | 972 s->coefs_start; |
973 } else { | |
2967 | 974 n = s->exponent_high_bands[s->frame_len_bits - |
783 | 975 s->block_len_bits][j]; |
976 } | |
977 if (j >= 0 && s->high_band_coded[ch][j]) { | |
978 /* use noise with specified power */ | |
979 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]); | |
980 /* XXX: use a table */ | |
981 mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05); | |
982 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult); | |
983 mult1 *= mdct_norm; | |
984 for(i = 0;i < n; i++) { | |
985 noise = s->noise_table[s->noise_index]; | |
986 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
987 *coefs++ = (*exponents++) * noise * mult1; | |
988 } | |
989 } else { | |
990 /* coded values + small noise */ | |
991 for(i = 0;i < n; i++) { | |
992 noise = s->noise_table[s->noise_index]; | |
993 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
994 *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult; | |
995 } | |
996 } | |
997 } | |
998 | |
999 /* very high freqs : noise */ | |
1000 n = s->block_len - s->coefs_end[bsize]; | |
1001 mult1 = mult * exponents[-1]; | |
1002 for(i = 0; i < n; i++) { | |
1003 *coefs++ = s->noise_table[s->noise_index] * mult1; | |
1004 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
1005 } | |
1006 } else { | |
1007 /* XXX: optimize more */ | |
1008 for(i = 0;i < s->coefs_start; i++) | |
1009 *coefs++ = 0.0; | |
1010 n = nb_coefs[ch]; | |
1011 for(i = 0;i < n; i++) { | |
1012 *coefs++ = coefs1[i] * exponents[i] * mult; | |
1013 } | |
1014 n = s->block_len - s->coefs_end[bsize]; | |
1015 for(i = 0;i < n; i++) | |
1016 *coefs++ = 0.0; | |
1017 } | |
1018 } | |
1019 } | |
1020 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
1021 #ifdef TRACE |
783 | 1022 for(ch = 0; ch < s->nb_channels; ch++) { |
1023 if (s->channel_coded[ch]) { | |
1024 dump_floats("exponents", 3, s->exponents[ch], s->block_len); | |
1025 dump_floats("coefs", 1, s->coefs[ch], s->block_len); | |
1026 } | |
1027 } | |
1028 #endif | |
2967 | 1029 |
783 | 1030 if (s->ms_stereo && s->channel_coded[1]) { |
1031 float a, b; | |
1032 int i; | |
1033 | |
1034 /* nominal case for ms stereo: we do it before mdct */ | |
1035 /* no need to optimize this case because it should almost | |
1036 never happen */ | |
1037 if (!s->channel_coded[0]) { | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
1038 tprintf("rare ms-stereo case happened\n"); |
783 | 1039 memset(s->coefs[0], 0, sizeof(float) * s->block_len); |
1040 s->channel_coded[0] = 1; | |
1041 } | |
2967 | 1042 |
783 | 1043 for(i = 0; i < s->block_len; i++) { |
1044 a = s->coefs[0][i]; | |
1045 b = s->coefs[1][i]; | |
1046 s->coefs[0][i] = a + b; | |
1047 s->coefs[1][i] = a - b; | |
1048 } | |
1049 } | |
1050 | |
1051 /* build the window : we ensure that when the windows overlap | |
1052 their squared sum is always 1 (MDCT reconstruction rule) */ | |
1053 /* XXX: merge with output */ | |
1054 { | |
1055 int i, next_block_len, block_len, prev_block_len, n; | |
1056 float *wptr; | |
1057 | |
1058 block_len = s->block_len; | |
1059 prev_block_len = 1 << s->prev_block_len_bits; | |
1060 next_block_len = 1 << s->next_block_len_bits; | |
1061 | |
1062 /* right part */ | |
1063 wptr = window + block_len; | |
1064 if (block_len <= next_block_len) { | |
1065 for(i=0;i<block_len;i++) | |
1066 *wptr++ = s->windows[bsize][i]; | |
1067 } else { | |
1068 /* overlap */ | |
1069 n = (block_len / 2) - (next_block_len / 2); | |
1070 for(i=0;i<n;i++) | |
1071 *wptr++ = 1.0; | |
1072 for(i=0;i<next_block_len;i++) | |
1073 *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i]; | |
1074 for(i=0;i<n;i++) | |
1075 *wptr++ = 0.0; | |
1076 } | |
1077 | |
1078 /* left part */ | |
1079 wptr = window + block_len; | |
1080 if (block_len <= prev_block_len) { | |
1081 for(i=0;i<block_len;i++) | |
1082 *--wptr = s->windows[bsize][i]; | |
1083 } else { | |
1084 /* overlap */ | |
1085 n = (block_len / 2) - (prev_block_len / 2); | |
1086 for(i=0;i<n;i++) | |
1087 *--wptr = 1.0; | |
1088 for(i=0;i<prev_block_len;i++) | |
1089 *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i]; | |
1090 for(i=0;i<n;i++) | |
1091 *--wptr = 0.0; | |
1092 } | |
1093 } | |
1094 | |
2967 | 1095 |
783 | 1096 for(ch = 0; ch < s->nb_channels; ch++) { |
1097 if (s->channel_coded[ch]) { | |
972 | 1098 FFTSample output[BLOCK_MAX_SIZE * 2] __attribute__((aligned(16))); |
783 | 1099 float *ptr; |
1100 int i, n4, index, n; | |
1101 | |
1102 n = s->block_len; | |
1103 n4 = s->block_len / 2; | |
2967 | 1104 ff_imdct_calc(&s->mdct_ctx[bsize], |
795
55add0e7eafb
avoid name clash - fixed again block size selection
bellard
parents:
785
diff
changeset
|
1105 output, s->coefs[ch], s->mdct_tmp); |
783 | 1106 |
1107 /* XXX: optimize all that by build the window and | |
1108 multipying/adding at the same time */ | |
1109 /* multiply by the window */ | |
1110 for(i=0;i<n * 2;i++) { | |
1111 output[i] *= window[i]; | |
1112 } | |
1113 | |
1114 /* add in the frame */ | |
1115 index = (s->frame_len / 2) + s->block_pos - n4; | |
1116 ptr = &s->frame_out[ch][index]; | |
1117 for(i=0;i<n * 2;i++) { | |
1118 *ptr += output[i]; | |
1119 ptr++; | |
1120 } | |
1121 | |
1122 /* specific fast case for ms-stereo : add to second | |
1123 channel if it is not coded */ | |
1124 if (s->ms_stereo && !s->channel_coded[1]) { | |
1125 ptr = &s->frame_out[1][index]; | |
1126 for(i=0;i<n * 2;i++) { | |
1127 *ptr += output[i]; | |
1128 ptr++; | |
1129 } | |
1130 } | |
1131 } | |
1132 } | |
1133 next: | |
1134 /* update block number */ | |
1135 s->block_num++; | |
1136 s->block_pos += s->block_len; | |
1137 if (s->block_pos >= s->frame_len) | |
1138 return 1; | |
1139 else | |
1140 return 0; | |
1141 } | |
1142 | |
1143 /* decode a frame of frame_len samples */ | |
1144 static int wma_decode_frame(WMADecodeContext *s, int16_t *samples) | |
1145 { | |
1146 int ret, i, n, a, ch, incr; | |
1147 int16_t *ptr; | |
1148 float *iptr; | |
1149 | |
1343 | 1150 #ifdef TRACE |
1151 tprintf("***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len); | |
1152 #endif | |
783 | 1153 |
1154 /* read each block */ | |
1155 s->block_num = 0; | |
1156 s->block_pos = 0; | |
1157 for(;;) { | |
1158 ret = wma_decode_block(s); | |
2967 | 1159 if (ret < 0) |
783 | 1160 return -1; |
1161 if (ret) | |
1162 break; | |
1163 } | |
1164 | |
1165 /* convert frame to integer */ | |
1166 n = s->frame_len; | |
1167 incr = s->nb_channels; | |
1168 for(ch = 0; ch < s->nb_channels; ch++) { | |
1169 ptr = samples + ch; | |
1170 iptr = s->frame_out[ch]; | |
1171 | |
1172 for(i=0;i<n;i++) { | |
797 | 1173 a = lrintf(*iptr++); |
783 | 1174 if (a > 32767) |
1175 a = 32767; | |
1176 else if (a < -32768) | |
1177 a = -32768; | |
1178 *ptr = a; | |
1179 ptr += incr; | |
1180 } | |
1181 /* prepare for next block */ | |
1182 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len], | |
1183 s->frame_len * sizeof(float)); | |
1184 /* XXX: suppress this */ | |
2967 | 1185 memset(&s->frame_out[ch][s->frame_len], 0, |
783 | 1186 s->frame_len * sizeof(float)); |
1187 } | |
1188 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
1189 #ifdef TRACE |
783 | 1190 dump_shorts("samples", samples, n * s->nb_channels); |
1191 #endif | |
1192 return 0; | |
1193 } | |
1194 | |
2967 | 1195 static int wma_decode_superframe(AVCodecContext *avctx, |
783 | 1196 void *data, int *data_size, |
1064 | 1197 uint8_t *buf, int buf_size) |
783 | 1198 { |
1199 WMADecodeContext *s = avctx->priv_data; | |
1200 int nb_frames, bit_offset, i, pos, len; | |
1201 uint8_t *q; | |
1202 int16_t *samples; | |
2967 | 1203 |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
1204 tprintf("***decode_superframe:\n"); |
783 | 1205 |
1750 | 1206 if(buf_size==0){ |
1207 s->last_superframe_len = 0; | |
1208 return 0; | |
1209 } | |
2967 | 1210 |
783 | 1211 samples = data; |
1212 | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
972
diff
changeset
|
1213 init_get_bits(&s->gb, buf, buf_size*8); |
2967 | 1214 |
783 | 1215 if (s->use_bit_reservoir) { |
1216 /* read super frame header */ | |
1217 get_bits(&s->gb, 4); /* super frame index */ | |
1218 nb_frames = get_bits(&s->gb, 4) - 1; | |
1219 | |
1220 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3); | |
1221 | |
1222 if (s->last_superframe_len > 0) { | |
1223 // printf("skip=%d\n", s->last_bitoffset); | |
1224 /* add bit_offset bits to last frame */ | |
2967 | 1225 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) > |
783 | 1226 MAX_CODED_SUPERFRAME_SIZE) |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
1227 goto fail; |
783 | 1228 q = s->last_superframe + s->last_superframe_len; |
1229 len = bit_offset; | |
1230 while (len > 0) { | |
1231 *q++ = (get_bits)(&s->gb, 8); | |
1232 len -= 8; | |
1233 } | |
1234 if (len > 0) { | |
1235 *q++ = (get_bits)(&s->gb, len) << (8 - len); | |
1236 } | |
2967 | 1237 |
783 | 1238 /* XXX: bit_offset bits into last frame */ |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
972
diff
changeset
|
1239 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8); |
783 | 1240 /* skip unused bits */ |
1241 if (s->last_bitoffset > 0) | |
1242 skip_bits(&s->gb, s->last_bitoffset); | |
1243 /* this frame is stored in the last superframe and in the | |
1244 current one */ | |
1245 if (wma_decode_frame(s, samples) < 0) | |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
1246 goto fail; |
783 | 1247 samples += s->nb_channels * s->frame_len; |
1248 } | |
1249 | |
1250 /* read each frame starting from bit_offset */ | |
1251 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3; | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
972
diff
changeset
|
1252 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8); |
783 | 1253 len = pos & 7; |
1254 if (len > 0) | |
1255 skip_bits(&s->gb, len); | |
2967 | 1256 |
783 | 1257 s->reset_block_lengths = 1; |
1258 for(i=0;i<nb_frames;i++) { | |
1259 if (wma_decode_frame(s, samples) < 0) | |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
1260 goto fail; |
783 | 1261 samples += s->nb_channels * s->frame_len; |
1262 } | |
1263 | |
1264 /* we copy the end of the frame in the last frame buffer */ | |
1265 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7); | |
1266 s->last_bitoffset = pos & 7; | |
1267 pos >>= 3; | |
1268 len = buf_size - pos; | |
819 | 1269 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) { |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
1270 goto fail; |
783 | 1271 } |
1272 s->last_superframe_len = len; | |
1273 memcpy(s->last_superframe, buf + pos, len); | |
1274 } else { | |
1275 /* single frame decode */ | |
1276 if (wma_decode_frame(s, samples) < 0) | |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
1277 goto fail; |
783 | 1278 samples += s->nb_channels * s->frame_len; |
1279 } | |
1280 *data_size = (int8_t *)samples - (int8_t *)data; | |
1281 return s->block_align; | |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
1282 fail: |
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
1283 /* when error, we reset the bit reservoir */ |
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
1284 s->last_superframe_len = 0; |
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
1285 return -1; |
783 | 1286 } |
1287 | |
1288 static int wma_decode_end(AVCodecContext *avctx) | |
1289 { | |
1290 WMADecodeContext *s = avctx->priv_data; | |
1291 int i; | |
1292 | |
1293 for(i = 0; i < s->nb_block_sizes; i++) | |
795
55add0e7eafb
avoid name clash - fixed again block size selection
bellard
parents:
785
diff
changeset
|
1294 ff_mdct_end(&s->mdct_ctx[i]); |
783 | 1295 for(i = 0; i < s->nb_block_sizes; i++) |
1296 av_free(s->windows[i]); | |
1297 | |
1298 if (s->use_exp_vlc) { | |
1299 free_vlc(&s->exp_vlc); | |
1300 } | |
1301 if (s->use_noise_coding) { | |
1302 free_vlc(&s->hgain_vlc); | |
1303 } | |
1304 for(i = 0;i < 2; i++) { | |
1305 free_vlc(&s->coef_vlc[i]); | |
1306 av_free(s->run_table[i]); | |
1307 av_free(s->level_table[i]); | |
1308 } | |
2967 | 1309 |
783 | 1310 return 0; |
1311 } | |
1312 | |
1313 AVCodec wmav1_decoder = | |
1314 { | |
1315 "wmav1", | |
1316 CODEC_TYPE_AUDIO, | |
1317 CODEC_ID_WMAV1, | |
1318 sizeof(WMADecodeContext), | |
1319 wma_decode_init, | |
1320 NULL, | |
1321 wma_decode_end, | |
1322 wma_decode_superframe, | |
1323 }; | |
1324 | |
1325 AVCodec wmav2_decoder = | |
1326 { | |
1327 "wmav2", | |
1328 CODEC_TYPE_AUDIO, | |
1329 CODEC_ID_WMAV2, | |
1330 sizeof(WMADecodeContext), | |
1331 wma_decode_init, | |
1332 NULL, | |
1333 wma_decode_end, | |
1334 wma_decode_superframe, | |
1335 }; |