Mercurial > libavcodec.hg
annotate wma.c @ 10003:24952f1a8979 libavcodec
cosmetics: K&R coding style
author | diego |
---|---|
date | Sun, 02 Aug 2009 10:34:30 +0000 |
parents | 01ccb357a33c |
children | 89cd870ca180 |
rev | line source |
---|---|
4490 | 1 /* |
2 * WMA compatible codec | |
8629
04423b2f6e0b
cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents:
7682
diff
changeset
|
3 * Copyright (c) 2002-2007 The FFmpeg Project |
4490 | 4 * |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 #include "avcodec.h" | |
23 #include "wma.h" | |
24 #include "wmadata.h" | |
25 | |
26 #undef NDEBUG | |
27 #include <assert.h> | |
28 | |
29 /* XXX: use same run/length optimization as mpeg decoders */ | |
30 //FIXME maybe split decode / encode or pass flag | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
31 static void init_coef_vlc(VLC *vlc, uint16_t **prun_table, |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
32 uint16_t **plevel_table, uint16_t **pint_table, |
4490 | 33 const CoefVLCTable *vlc_table) |
34 { | |
35 int n = vlc_table->n; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
36 const uint8_t *table_bits = vlc_table->huffbits; |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
37 const uint32_t *table_codes = vlc_table->huffcodes; |
4490 | 38 const uint16_t *levels_table = vlc_table->levels; |
39 uint16_t *run_table, *level_table, *int_table; | |
40 int i, l, j, k, level; | |
41 | |
42 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0); | |
43 | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
44 run_table = av_malloc(n * sizeof(uint16_t)); |
4490 | 45 level_table = av_malloc(n * sizeof(uint16_t)); |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
46 int_table = av_malloc(n * sizeof(uint16_t)); |
4490 | 47 i = 2; |
48 level = 1; | |
49 k = 0; | |
50 while (i < n) { | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
51 int_table[k] = i; |
4490 | 52 l = levels_table[k++]; |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
53 for (j = 0; j < l; j++) { |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
54 run_table[i] = j; |
4490 | 55 level_table[i] = level; |
56 i++; | |
57 } | |
58 level++; | |
59 } | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
60 *prun_table = run_table; |
4490 | 61 *plevel_table = level_table; |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
62 *pint_table = int_table; |
4490 | 63 } |
64 | |
9841
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
65 /** |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
66 *@brief Get the samples per frame for this stream. |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
67 *@param sample_rate output sample_rate |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
68 *@param version wma version |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
69 *@param decode_flags codec compression features |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
70 *@return log2 of the number of output samples per frame |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
71 */ |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
72 int av_cold ff_wma_get_frame_len_bits(int sample_rate, int version, |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
73 unsigned int decode_flags) |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
74 { |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
75 |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
76 int frame_len_bits; |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
77 |
9843 | 78 if (sample_rate <= 16000) { |
9841
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
79 frame_len_bits = 9; |
9843 | 80 } else if (sample_rate <= 22050 || |
81 (sample_rate <= 32000 && version == 1)) { | |
9841
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
82 frame_len_bits = 10; |
9843 | 83 } else if (sample_rate <= 48000) { |
9841
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
84 frame_len_bits = 11; |
9842
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
85 } else if (sample_rate <= 96000) { |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
86 frame_len_bits = 12; |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
87 } else { |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
88 frame_len_bits = 13; |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
89 } |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
90 |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
91 if (version == 3) { |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
92 int tmp = decode_flags & 0x6; |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
93 if (tmp == 0x2) { |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
94 ++frame_len_bits; |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
95 } else if (tmp == 0x4) { |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
96 --frame_len_bits; |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
97 } else if (tmp == 0x6) { |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
98 frame_len_bits -= 2; |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
99 } |
32d100ad81a6
Add additional sample rates to the frame len bits calculation code
faust3
parents:
9841
diff
changeset
|
100 } |
9841
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
101 |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
102 return frame_len_bits; |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
103 } |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
104 |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
105 int ff_wma_init(AVCodecContext *avctx, int flags2) |
4490 | 106 { |
4601 | 107 WMACodecContext *s = avctx->priv_data; |
4490 | 108 int i; |
109 float bps1, high_freq; | |
110 volatile float bps; | |
111 int sample_rate1; | |
112 int coef_vlc_table; | |
113 | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
114 if ( avctx->sample_rate <= 0 || avctx->sample_rate > 50000 |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
115 || avctx->channels <= 0 || avctx->channels > 8 |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
116 || avctx->bit_rate <= 0) |
5086
a10ebd496bd9
sanity checks (should prevent hypothetical div by zero issue)
michael
parents:
4737
diff
changeset
|
117 return -1; |
a10ebd496bd9
sanity checks (should prevent hypothetical div by zero issue)
michael
parents:
4737
diff
changeset
|
118 |
4490 | 119 s->sample_rate = avctx->sample_rate; |
120 s->nb_channels = avctx->channels; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
121 s->bit_rate = avctx->bit_rate; |
4490 | 122 s->block_align = avctx->block_align; |
123 | |
124 dsputil_init(&s->dsp, avctx); | |
125 | |
126 if (avctx->codec->id == CODEC_ID_WMAV1) { | |
127 s->version = 1; | |
128 } else { | |
129 s->version = 2; | |
130 } | |
131 | |
132 /* compute MDCT block size */ | |
9841
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
133 s->frame_len_bits = ff_wma_get_frame_len_bits(s->sample_rate, s->version, 0); |
39bb2646fe00
Move frame len bits calculation to ff_wma_get_frame_len_bits
faust3
parents:
9840
diff
changeset
|
134 |
4490 | 135 s->frame_len = 1 << s->frame_len_bits; |
136 if (s->use_variable_block_len) { | |
137 int nb_max, nb; | |
138 nb = ((flags2 >> 3) & 3) + 1; | |
139 if ((s->bit_rate / s->nb_channels) >= 32000) | |
140 nb += 2; | |
141 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; | |
142 if (nb > nb_max) | |
143 nb = nb_max; | |
144 s->nb_block_sizes = nb + 1; | |
145 } else { | |
146 s->nb_block_sizes = 1; | |
147 } | |
148 | |
4588
fc155ff94878
cosmetics: Fix another common typo, dependAnt --> dependEnt.
diego
parents:
4490
diff
changeset
|
149 /* init rate dependent parameters */ |
4490 | 150 s->use_noise_coding = 1; |
151 high_freq = s->sample_rate * 0.5; | |
152 | |
153 /* if version 2, then the rates are normalized */ | |
154 sample_rate1 = s->sample_rate; | |
155 if (s->version == 2) { | |
9843 | 156 if (sample_rate1 >= 44100) { |
4490 | 157 sample_rate1 = 44100; |
9843 | 158 } else if (sample_rate1 >= 22050) { |
4490 | 159 sample_rate1 = 22050; |
9843 | 160 } else if (sample_rate1 >= 16000) { |
4490 | 161 sample_rate1 = 16000; |
9843 | 162 } else if (sample_rate1 >= 11025) { |
4490 | 163 sample_rate1 = 11025; |
9843 | 164 } else if (sample_rate1 >= 8000) { |
4490 | 165 sample_rate1 = 8000; |
9843 | 166 } |
4490 | 167 } |
168 | |
169 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate); | |
170 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2; | |
171 | |
172 /* compute high frequency value and choose if noise coding should | |
173 be activated */ | |
174 bps1 = bps; | |
175 if (s->nb_channels == 2) | |
176 bps1 = bps * 1.6; | |
177 if (sample_rate1 == 44100) { | |
9843 | 178 if (bps1 >= 0.61) { |
4490 | 179 s->use_noise_coding = 0; |
9843 | 180 } else { |
4490 | 181 high_freq = high_freq * 0.4; |
9843 | 182 } |
4490 | 183 } else if (sample_rate1 == 22050) { |
9843 | 184 if (bps1 >= 1.16) { |
4490 | 185 s->use_noise_coding = 0; |
9843 | 186 } else if (bps1 >= 0.72) { |
4490 | 187 high_freq = high_freq * 0.7; |
9843 | 188 } else { |
4490 | 189 high_freq = high_freq * 0.6; |
9843 | 190 } |
4490 | 191 } else if (sample_rate1 == 16000) { |
9843 | 192 if (bps > 0.5) { |
4490 | 193 high_freq = high_freq * 0.5; |
9843 | 194 } else { |
4490 | 195 high_freq = high_freq * 0.3; |
9843 | 196 } |
4490 | 197 } else if (sample_rate1 == 11025) { |
198 high_freq = high_freq * 0.7; | |
199 } else if (sample_rate1 == 8000) { | |
200 if (bps <= 0.625) { | |
201 high_freq = high_freq * 0.5; | |
202 } else if (bps > 0.75) { | |
203 s->use_noise_coding = 0; | |
204 } else { | |
205 high_freq = high_freq * 0.65; | |
206 } | |
207 } else { | |
208 if (bps >= 0.8) { | |
209 high_freq = high_freq * 0.75; | |
210 } else if (bps >= 0.6) { | |
211 high_freq = high_freq * 0.6; | |
212 } else { | |
213 high_freq = high_freq * 0.5; | |
214 } | |
215 } | |
4652 | 216 dprintf(s->avctx, "flags2=0x%x\n", flags2); |
217 dprintf(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n", | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
218 s->version, s->nb_channels, s->sample_rate, s->bit_rate, |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
219 s->block_align); |
4652 | 220 dprintf(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n", |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
221 bps, bps1, high_freq, s->byte_offset_bits); |
4652 | 222 dprintf(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n", |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
223 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes); |
4490 | 224 |
225 /* compute the scale factor band sizes for each MDCT block size */ | |
226 { | |
227 int a, b, pos, lpos, k, block_len, i, j, n; | |
228 const uint8_t *table; | |
229 | |
230 if (s->version == 1) { | |
231 s->coefs_start = 3; | |
232 } else { | |
233 s->coefs_start = 0; | |
234 } | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
235 for (k = 0; k < s->nb_block_sizes; k++) { |
4490 | 236 block_len = s->frame_len >> k; |
237 | |
238 if (s->version == 1) { | |
239 lpos = 0; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
240 for (i = 0; i < 25; i++) { |
4490 | 241 a = wma_critical_freqs[i]; |
242 b = s->sample_rate; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
243 pos = ((block_len * 2 * a) + (b >> 1)) / b; |
4490 | 244 if (pos > block_len) |
245 pos = block_len; | |
246 s->exponent_bands[0][i] = pos - lpos; | |
247 if (pos >= block_len) { | |
248 i++; | |
249 break; | |
250 } | |
251 lpos = pos; | |
252 } | |
253 s->exponent_sizes[0] = i; | |
254 } else { | |
255 /* hardcoded tables */ | |
256 table = NULL; | |
257 a = s->frame_len_bits - BLOCK_MIN_BITS - k; | |
258 if (a < 3) { | |
9843 | 259 if (s->sample_rate >= 44100) { |
4490 | 260 table = exponent_band_44100[a]; |
9843 | 261 } else if (s->sample_rate >= 32000) { |
4490 | 262 table = exponent_band_32000[a]; |
9843 | 263 } else if (s->sample_rate >= 22050) { |
4490 | 264 table = exponent_band_22050[a]; |
9843 | 265 } |
4490 | 266 } |
267 if (table) { | |
268 n = *table++; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
269 for (i = 0; i < n; i++) |
4490 | 270 s->exponent_bands[k][i] = table[i]; |
271 s->exponent_sizes[k] = n; | |
272 } else { | |
273 j = 0; | |
274 lpos = 0; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
275 for (i = 0; i < 25; i++) { |
4490 | 276 a = wma_critical_freqs[i]; |
277 b = s->sample_rate; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
278 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b); |
4490 | 279 pos <<= 2; |
280 if (pos > block_len) | |
281 pos = block_len; | |
282 if (pos > lpos) | |
283 s->exponent_bands[k][j++] = pos - lpos; | |
284 if (pos >= block_len) | |
285 break; | |
286 lpos = pos; | |
287 } | |
288 s->exponent_sizes[k] = j; | |
289 } | |
290 } | |
291 | |
292 /* max number of coefs */ | |
293 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k; | |
294 /* high freq computation */ | |
295 s->high_band_start[k] = (int)((block_len * 2 * high_freq) / | |
296 s->sample_rate + 0.5); | |
297 n = s->exponent_sizes[k]; | |
298 j = 0; | |
299 pos = 0; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
300 for (i = 0; i < n; i++) { |
4490 | 301 int start, end; |
302 start = pos; | |
303 pos += s->exponent_bands[k][i]; | |
304 end = pos; | |
305 if (start < s->high_band_start[k]) | |
306 start = s->high_band_start[k]; | |
307 if (end > s->coefs_end[k]) | |
308 end = s->coefs_end[k]; | |
309 if (end > start) | |
310 s->exponent_high_bands[k][j++] = end - start; | |
311 } | |
312 s->exponent_high_sizes[k] = j; | |
313 #if 0 | |
4600 | 314 tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ", |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
315 s->frame_len >> k, |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
316 s->coefs_end[k], |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
317 s->high_band_start[k], |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
318 s->exponent_high_sizes[k]); |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
319 for (j = 0; j < s->exponent_high_sizes[k]; j++) |
4600 | 320 tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]); |
321 tprintf(s->avctx, "\n"); | |
4490 | 322 #endif |
323 } | |
324 } | |
325 | |
326 #ifdef TRACE | |
327 { | |
328 int i, j; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
329 for (i = 0; i < s->nb_block_sizes; i++) { |
4600 | 330 tprintf(s->avctx, "%5d: n=%2d:", |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
331 s->frame_len >> i, |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
332 s->exponent_sizes[i]); |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
333 for (j = 0; j < s->exponent_sizes[i]; j++) |
4600 | 334 tprintf(s->avctx, " %d", s->exponent_bands[i][j]); |
335 tprintf(s->avctx, "\n"); | |
4490 | 336 } |
337 } | |
338 #endif | |
339 | |
340 /* init MDCT windows : simple sinus window */ | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
341 for (i = 0; i < s->nb_block_sizes; i++) { |
7572
d72f9295fe6e
Change wma.c to use the ff_sine_window_init() from mdct.c
superdump
parents:
5369
diff
changeset
|
342 int n; |
4490 | 343 n = 1 << (s->frame_len_bits - i); |
7682
a08c1a035386
Fix index to ff_sine_windows[]. Previously the index was usually in reverse
superdump
parents:
7582
diff
changeset
|
344 ff_sine_window_init(ff_sine_windows[s->frame_len_bits - i - 7], n); |
a08c1a035386
Fix index to ff_sine_windows[]. Previously the index was usually in reverse
superdump
parents:
7582
diff
changeset
|
345 s->windows[i] = ff_sine_windows[s->frame_len_bits - i - 7]; |
4490 | 346 } |
347 | |
348 s->reset_block_lengths = 1; | |
349 | |
350 if (s->use_noise_coding) { | |
351 | |
352 /* init the noise generator */ | |
9843 | 353 if (s->use_exp_vlc) { |
4490 | 354 s->noise_mult = 0.02; |
9843 | 355 } else { |
4490 | 356 s->noise_mult = 0.04; |
9843 | 357 } |
4490 | 358 |
359 #ifdef TRACE | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
360 for (i = 0; i < NOISE_TAB_SIZE; i++) |
4490 | 361 s->noise_table[i] = 1.0 * s->noise_mult; |
362 #else | |
363 { | |
364 unsigned int seed; | |
365 float norm; | |
366 seed = 1; | |
367 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult; | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
368 for (i = 0; i < NOISE_TAB_SIZE; i++) { |
4490 | 369 seed = seed * 314159 + 1; |
370 s->noise_table[i] = (float)((int)seed) * norm; | |
371 } | |
372 } | |
373 #endif | |
374 } | |
375 | |
376 /* choose the VLC tables for the coefficients */ | |
377 coef_vlc_table = 2; | |
378 if (s->sample_rate >= 32000) { | |
9843 | 379 if (bps1 < 0.72) { |
4490 | 380 coef_vlc_table = 0; |
9843 | 381 } else if (bps1 < 1.16) { |
4490 | 382 coef_vlc_table = 1; |
9843 | 383 } |
4490 | 384 } |
385 s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ]; | |
386 s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1]; | |
387 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0], | |
388 s->coef_vlcs[0]); | |
389 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1], | |
390 s->coef_vlcs[1]); | |
391 | |
392 return 0; | |
393 } | |
394 | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
395 int ff_wma_total_gain_to_bits(int total_gain) |
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
396 { |
4490 | 397 if (total_gain < 15) return 13; |
398 else if (total_gain < 32) return 12; | |
399 else if (total_gain < 40) return 11; | |
400 else if (total_gain < 45) return 10; | |
401 else return 9; | |
402 } | |
403 | |
404 int ff_wma_end(AVCodecContext *avctx) | |
405 { | |
4601 | 406 WMACodecContext *s = avctx->priv_data; |
4490 | 407 int i; |
408 | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
409 for (i = 0; i < s->nb_block_sizes; i++) |
4490 | 410 ff_mdct_end(&s->mdct_ctx[i]); |
411 | |
412 if (s->use_exp_vlc) { | |
413 free_vlc(&s->exp_vlc); | |
414 } | |
415 if (s->use_noise_coding) { | |
416 free_vlc(&s->hgain_vlc); | |
417 } | |
9840
71066f133e1c
cosmetics: Prettyprint and reformat wma.c closer to K&R style.
diego
parents:
8629
diff
changeset
|
418 for (i = 0; i < 2; i++) { |
4490 | 419 free_vlc(&s->coef_vlc[i]); |
420 av_free(s->run_table[i]); | |
421 av_free(s->level_table[i]); | |
5369 | 422 av_free(s->int_table[i]); |
4490 | 423 } |
424 | |
425 return 0; | |
426 } | |
9844
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
427 |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
428 /** |
9869
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
429 * Decode an uncompressed coefficient. |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
430 * @param s codec context |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
431 * @return the decoded coefficient |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
432 */ |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
433 unsigned int ff_wma_get_large_val(GetBitContext* gb) |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
434 { |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
435 /** consumes up to 34 bits */ |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
436 int n_bits = 8; |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
437 /** decode length */ |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
438 if (get_bits1(gb)) { |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
439 n_bits += 8; |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
440 if (get_bits1(gb)) { |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
441 n_bits += 8; |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
442 if (get_bits1(gb)) { |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
443 n_bits += 7; |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
444 } |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
445 } |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
446 } |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
447 return get_bits_long(gb, n_bits); |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
448 } |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
449 |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
450 /** |
9844
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
451 * Decode run level compressed coefficients. |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
452 * @param avctx codec context |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
453 * @param gb bitstream reader context |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
454 * @param vlc vlc table for get_vlc2 |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
455 * @param level_table level codes |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
456 * @param run_table run codes |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
457 * @param version 0 for wma1,2 1 for wmapro |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
458 * @param ptr output buffer |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
459 * @param offset offset in the output buffer |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
460 * @param num_coefs number of input coefficents |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
461 * @param block_len input buffer length (2^n) |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
462 * @param frame_len_bits number of bits for escaped run codes |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
463 * @param coef_nb_bits number of bits for escaped level codes |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
464 * @return 0 on success, -1 otherwise |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
465 */ |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
466 int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb, |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
467 VLC *vlc, |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
468 const uint16_t *level_table, const uint16_t *run_table, |
9868 | 469 int version, WMACoef *ptr, int offset, |
9844
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
470 int num_coefs, int block_len, int frame_len_bits, |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
471 int coef_nb_bits) |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
472 { |
9871 | 473 int code, level, sign; |
474 const unsigned int coef_mask = block_len - 1; | |
475 for (; offset < num_coefs; offset++) { | |
9844
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
476 code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX); |
9871 | 477 if (code > 1) { |
478 /** normal code */ | |
479 offset += run_table[code]; | |
480 level = level_table[code]; | |
481 } else if (code == 1) { | |
9872
01ccb357a33c
Make remaining run level decode comments doxygen comments
faust3
parents:
9871
diff
changeset
|
482 /** EOB */ |
9844
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
483 break; |
9871 | 484 } else { |
9872
01ccb357a33c
Make remaining run level decode comments doxygen comments
faust3
parents:
9871
diff
changeset
|
485 /** escape */ |
9869
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
486 if (!version) { |
9870 | 487 level = get_bits(gb, coef_nb_bits); |
9872
01ccb357a33c
Make remaining run level decode comments doxygen comments
faust3
parents:
9871
diff
changeset
|
488 /** NOTE: this is rather suboptimal. reading |
01ccb357a33c
Make remaining run level decode comments doxygen comments
faust3
parents:
9871
diff
changeset
|
489 block_len_bits would be better */ |
9871 | 490 offset += get_bits(gb, frame_len_bits); |
9869
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
491 } else { |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
492 level = ff_wma_get_large_val(gb); |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
493 /** escape decode */ |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
494 if (get_bits1(gb)) { |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
495 if (get_bits1(gb)) { |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
496 if (get_bits1(gb)) { |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
497 av_log(avctx,AV_LOG_ERROR, |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
498 "broken escape sequence\n"); |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
499 return -1; |
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
500 } else |
9871 | 501 offset += get_bits(gb, frame_len_bits) + 4; |
9869
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
502 } else |
9871 | 503 offset += get_bits(gb, 2) + 1; |
504 } | |
9869
1f6b569bf958
Add support for escape coded wmapro run level coefficients
faust3
parents:
9868
diff
changeset
|
505 } |
9844
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
506 } |
9871 | 507 sign = get_bits1(gb) - 1; |
508 ptr[offset & coef_mask] = (level^sign) - sign; | |
9844
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
509 } |
9871 | 510 /** NOTE: EOB can be omitted */ |
511 if (offset > num_coefs) { | |
512 av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n"); | |
513 return -1; | |
514 } | |
515 | |
9844
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
516 return 0; |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
517 } |
eb5916527064
Move run level decode functionality to ff_wma_run_level_decode
faust3
parents:
9843
diff
changeset
|
518 |