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