annotate wma.c @ 5466:587b3e6c8d4f libavcodec

no need to duplicate cid table vars in context
author bcoudurier
date Sat, 04 Aug 2007 13:09:08 +0000
parents 04a241c308e8
children d72f9295fe6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
1 /*
0efc832d9102 wma encoder
michael
parents:
diff changeset
2 * WMA compatible codec
0efc832d9102 wma encoder
michael
parents:
diff changeset
3 * Copyright (c) 2002-2007 The FFmpeg Project.
0efc832d9102 wma encoder
michael
parents:
diff changeset
4 *
0efc832d9102 wma encoder
michael
parents:
diff changeset
5 * This file is part of FFmpeg.
0efc832d9102 wma encoder
michael
parents:
diff changeset
6 *
0efc832d9102 wma encoder
michael
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
0efc832d9102 wma encoder
michael
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
0efc832d9102 wma encoder
michael
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
0efc832d9102 wma encoder
michael
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
0efc832d9102 wma encoder
michael
parents:
diff changeset
11 *
0efc832d9102 wma encoder
michael
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
0efc832d9102 wma encoder
michael
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0efc832d9102 wma encoder
michael
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0efc832d9102 wma encoder
michael
parents:
diff changeset
15 * Lesser General Public License for more details.
0efc832d9102 wma encoder
michael
parents:
diff changeset
16 *
0efc832d9102 wma encoder
michael
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
0efc832d9102 wma encoder
michael
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
0efc832d9102 wma encoder
michael
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0efc832d9102 wma encoder
michael
parents:
diff changeset
20 */
0efc832d9102 wma encoder
michael
parents:
diff changeset
21
0efc832d9102 wma encoder
michael
parents:
diff changeset
22 #include "avcodec.h"
0efc832d9102 wma encoder
michael
parents:
diff changeset
23 #include "wma.h"
0efc832d9102 wma encoder
michael
parents:
diff changeset
24 #include "wmadata.h"
0efc832d9102 wma encoder
michael
parents:
diff changeset
25
0efc832d9102 wma encoder
michael
parents:
diff changeset
26 #undef NDEBUG
0efc832d9102 wma encoder
michael
parents:
diff changeset
27 #include <assert.h>
0efc832d9102 wma encoder
michael
parents:
diff changeset
28
0efc832d9102 wma encoder
michael
parents:
diff changeset
29 /* XXX: use same run/length optimization as mpeg decoders */
0efc832d9102 wma encoder
michael
parents:
diff changeset
30 //FIXME maybe split decode / encode or pass flag
0efc832d9102 wma encoder
michael
parents:
diff changeset
31 static void init_coef_vlc(VLC *vlc,
0efc832d9102 wma encoder
michael
parents:
diff changeset
32 uint16_t **prun_table, uint16_t **plevel_table, uint16_t **pint_table,
0efc832d9102 wma encoder
michael
parents:
diff changeset
33 const CoefVLCTable *vlc_table)
0efc832d9102 wma encoder
michael
parents:
diff changeset
34 {
0efc832d9102 wma encoder
michael
parents:
diff changeset
35 int n = vlc_table->n;
0efc832d9102 wma encoder
michael
parents:
diff changeset
36 const uint8_t *table_bits = vlc_table->huffbits;
0efc832d9102 wma encoder
michael
parents:
diff changeset
37 const uint32_t *table_codes = vlc_table->huffcodes;
0efc832d9102 wma encoder
michael
parents:
diff changeset
38 const uint16_t *levels_table = vlc_table->levels;
0efc832d9102 wma encoder
michael
parents:
diff changeset
39 uint16_t *run_table, *level_table, *int_table;
0efc832d9102 wma encoder
michael
parents:
diff changeset
40 int i, l, j, k, level;
0efc832d9102 wma encoder
michael
parents:
diff changeset
41
0efc832d9102 wma encoder
michael
parents:
diff changeset
42 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
0efc832d9102 wma encoder
michael
parents:
diff changeset
43
0efc832d9102 wma encoder
michael
parents:
diff changeset
44 run_table = av_malloc(n * sizeof(uint16_t));
0efc832d9102 wma encoder
michael
parents:
diff changeset
45 level_table = av_malloc(n * sizeof(uint16_t));
0efc832d9102 wma encoder
michael
parents:
diff changeset
46 int_table = av_malloc(n * sizeof(uint16_t));
0efc832d9102 wma encoder
michael
parents:
diff changeset
47 i = 2;
0efc832d9102 wma encoder
michael
parents:
diff changeset
48 level = 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
49 k = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
50 while (i < n) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
51 int_table[k]= i;
0efc832d9102 wma encoder
michael
parents:
diff changeset
52 l = levels_table[k++];
0efc832d9102 wma encoder
michael
parents:
diff changeset
53 for(j=0;j<l;j++) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
54 run_table[i] = j;
0efc832d9102 wma encoder
michael
parents:
diff changeset
55 level_table[i] = level;
0efc832d9102 wma encoder
michael
parents:
diff changeset
56 i++;
0efc832d9102 wma encoder
michael
parents:
diff changeset
57 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
58 level++;
0efc832d9102 wma encoder
michael
parents:
diff changeset
59 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
60 *prun_table = run_table;
0efc832d9102 wma encoder
michael
parents:
diff changeset
61 *plevel_table = level_table;
0efc832d9102 wma encoder
michael
parents:
diff changeset
62 *pint_table= int_table;
0efc832d9102 wma encoder
michael
parents:
diff changeset
63 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
64
0efc832d9102 wma encoder
michael
parents:
diff changeset
65 int ff_wma_init(AVCodecContext * avctx, int flags2)
0efc832d9102 wma encoder
michael
parents:
diff changeset
66 {
4601
7b9ce6f729ae Rename WMADecodeContext to WMACodecContext
mbardiaux
parents: 4600
diff changeset
67 WMACodecContext *s = avctx->priv_data;
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
68 int i;
0efc832d9102 wma encoder
michael
parents:
diff changeset
69 float *window;
0efc832d9102 wma encoder
michael
parents:
diff changeset
70 float bps1, high_freq;
0efc832d9102 wma encoder
michael
parents:
diff changeset
71 volatile float bps;
0efc832d9102 wma encoder
michael
parents:
diff changeset
72 int sample_rate1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
73 int coef_vlc_table;
0efc832d9102 wma encoder
michael
parents:
diff changeset
74
5086
a10ebd496bd9 sanity checks (should prevent hypothetical div by zero issue)
michael
parents: 4737
diff changeset
75 if( avctx->sample_rate<=0 || avctx->sample_rate>50000
a10ebd496bd9 sanity checks (should prevent hypothetical div by zero issue)
michael
parents: 4737
diff changeset
76 || avctx->channels<=0 || avctx->channels>8
a10ebd496bd9 sanity checks (should prevent hypothetical div by zero issue)
michael
parents: 4737
diff changeset
77 || avctx->bit_rate<=0)
a10ebd496bd9 sanity checks (should prevent hypothetical div by zero issue)
michael
parents: 4737
diff changeset
78 return -1;
a10ebd496bd9 sanity checks (should prevent hypothetical div by zero issue)
michael
parents: 4737
diff changeset
79
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
80 s->sample_rate = avctx->sample_rate;
0efc832d9102 wma encoder
michael
parents:
diff changeset
81 s->nb_channels = avctx->channels;
0efc832d9102 wma encoder
michael
parents:
diff changeset
82 s->bit_rate = avctx->bit_rate;
0efc832d9102 wma encoder
michael
parents:
diff changeset
83 s->block_align = avctx->block_align;
0efc832d9102 wma encoder
michael
parents:
diff changeset
84
0efc832d9102 wma encoder
michael
parents:
diff changeset
85 dsputil_init(&s->dsp, avctx);
0efc832d9102 wma encoder
michael
parents:
diff changeset
86
0efc832d9102 wma encoder
michael
parents:
diff changeset
87 if (avctx->codec->id == CODEC_ID_WMAV1) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
88 s->version = 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
89 } else {
0efc832d9102 wma encoder
michael
parents:
diff changeset
90 s->version = 2;
0efc832d9102 wma encoder
michael
parents:
diff changeset
91 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
92
0efc832d9102 wma encoder
michael
parents:
diff changeset
93 /* compute MDCT block size */
0efc832d9102 wma encoder
michael
parents:
diff changeset
94 if (s->sample_rate <= 16000) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
95 s->frame_len_bits = 9;
0efc832d9102 wma encoder
michael
parents:
diff changeset
96 } else if (s->sample_rate <= 22050 ||
0efc832d9102 wma encoder
michael
parents:
diff changeset
97 (s->sample_rate <= 32000 && s->version == 1)) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
98 s->frame_len_bits = 10;
0efc832d9102 wma encoder
michael
parents:
diff changeset
99 } else {
0efc832d9102 wma encoder
michael
parents:
diff changeset
100 s->frame_len_bits = 11;
0efc832d9102 wma encoder
michael
parents:
diff changeset
101 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
102 s->frame_len = 1 << s->frame_len_bits;
0efc832d9102 wma encoder
michael
parents:
diff changeset
103 if (s->use_variable_block_len) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
104 int nb_max, nb;
0efc832d9102 wma encoder
michael
parents:
diff changeset
105 nb = ((flags2 >> 3) & 3) + 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
106 if ((s->bit_rate / s->nb_channels) >= 32000)
0efc832d9102 wma encoder
michael
parents:
diff changeset
107 nb += 2;
0efc832d9102 wma encoder
michael
parents:
diff changeset
108 nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
0efc832d9102 wma encoder
michael
parents:
diff changeset
109 if (nb > nb_max)
0efc832d9102 wma encoder
michael
parents:
diff changeset
110 nb = nb_max;
0efc832d9102 wma encoder
michael
parents:
diff changeset
111 s->nb_block_sizes = nb + 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
112 } else {
0efc832d9102 wma encoder
michael
parents:
diff changeset
113 s->nb_block_sizes = 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
114 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
115
4588
fc155ff94878 cosmetics: Fix another common typo, dependAnt --> dependEnt.
diego
parents: 4490
diff changeset
116 /* init rate dependent parameters */
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
117 s->use_noise_coding = 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
118 high_freq = s->sample_rate * 0.5;
0efc832d9102 wma encoder
michael
parents:
diff changeset
119
0efc832d9102 wma encoder
michael
parents:
diff changeset
120 /* if version 2, then the rates are normalized */
0efc832d9102 wma encoder
michael
parents:
diff changeset
121 sample_rate1 = s->sample_rate;
0efc832d9102 wma encoder
michael
parents:
diff changeset
122 if (s->version == 2) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
123 if (sample_rate1 >= 44100)
0efc832d9102 wma encoder
michael
parents:
diff changeset
124 sample_rate1 = 44100;
0efc832d9102 wma encoder
michael
parents:
diff changeset
125 else if (sample_rate1 >= 22050)
0efc832d9102 wma encoder
michael
parents:
diff changeset
126 sample_rate1 = 22050;
0efc832d9102 wma encoder
michael
parents:
diff changeset
127 else if (sample_rate1 >= 16000)
0efc832d9102 wma encoder
michael
parents:
diff changeset
128 sample_rate1 = 16000;
0efc832d9102 wma encoder
michael
parents:
diff changeset
129 else if (sample_rate1 >= 11025)
0efc832d9102 wma encoder
michael
parents:
diff changeset
130 sample_rate1 = 11025;
0efc832d9102 wma encoder
michael
parents:
diff changeset
131 else if (sample_rate1 >= 8000)
0efc832d9102 wma encoder
michael
parents:
diff changeset
132 sample_rate1 = 8000;
0efc832d9102 wma encoder
michael
parents:
diff changeset
133 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
134
0efc832d9102 wma encoder
michael
parents:
diff changeset
135 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
0efc832d9102 wma encoder
michael
parents:
diff changeset
136 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
0efc832d9102 wma encoder
michael
parents:
diff changeset
137
0efc832d9102 wma encoder
michael
parents:
diff changeset
138 /* compute high frequency value and choose if noise coding should
0efc832d9102 wma encoder
michael
parents:
diff changeset
139 be activated */
0efc832d9102 wma encoder
michael
parents:
diff changeset
140 bps1 = bps;
0efc832d9102 wma encoder
michael
parents:
diff changeset
141 if (s->nb_channels == 2)
0efc832d9102 wma encoder
michael
parents:
diff changeset
142 bps1 = bps * 1.6;
0efc832d9102 wma encoder
michael
parents:
diff changeset
143 if (sample_rate1 == 44100) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
144 if (bps1 >= 0.61)
0efc832d9102 wma encoder
michael
parents:
diff changeset
145 s->use_noise_coding = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
146 else
0efc832d9102 wma encoder
michael
parents:
diff changeset
147 high_freq = high_freq * 0.4;
0efc832d9102 wma encoder
michael
parents:
diff changeset
148 } else if (sample_rate1 == 22050) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
149 if (bps1 >= 1.16)
0efc832d9102 wma encoder
michael
parents:
diff changeset
150 s->use_noise_coding = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
151 else if (bps1 >= 0.72)
0efc832d9102 wma encoder
michael
parents:
diff changeset
152 high_freq = high_freq * 0.7;
0efc832d9102 wma encoder
michael
parents:
diff changeset
153 else
0efc832d9102 wma encoder
michael
parents:
diff changeset
154 high_freq = high_freq * 0.6;
0efc832d9102 wma encoder
michael
parents:
diff changeset
155 } else if (sample_rate1 == 16000) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
156 if (bps > 0.5)
0efc832d9102 wma encoder
michael
parents:
diff changeset
157 high_freq = high_freq * 0.5;
0efc832d9102 wma encoder
michael
parents:
diff changeset
158 else
0efc832d9102 wma encoder
michael
parents:
diff changeset
159 high_freq = high_freq * 0.3;
0efc832d9102 wma encoder
michael
parents:
diff changeset
160 } else if (sample_rate1 == 11025) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
161 high_freq = high_freq * 0.7;
0efc832d9102 wma encoder
michael
parents:
diff changeset
162 } else if (sample_rate1 == 8000) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
163 if (bps <= 0.625) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
164 high_freq = high_freq * 0.5;
0efc832d9102 wma encoder
michael
parents:
diff changeset
165 } else if (bps > 0.75) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
166 s->use_noise_coding = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
167 } else {
0efc832d9102 wma encoder
michael
parents:
diff changeset
168 high_freq = high_freq * 0.65;
0efc832d9102 wma encoder
michael
parents:
diff changeset
169 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
170 } else {
0efc832d9102 wma encoder
michael
parents:
diff changeset
171 if (bps >= 0.8) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
172 high_freq = high_freq * 0.75;
0efc832d9102 wma encoder
michael
parents:
diff changeset
173 } else if (bps >= 0.6) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
174 high_freq = high_freq * 0.6;
0efc832d9102 wma encoder
michael
parents:
diff changeset
175 } else {
0efc832d9102 wma encoder
michael
parents:
diff changeset
176 high_freq = high_freq * 0.5;
0efc832d9102 wma encoder
michael
parents:
diff changeset
177 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
178 }
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4636
diff changeset
179 dprintf(s->avctx, "flags2=0x%x\n", flags2);
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4636
diff changeset
180 dprintf(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
181 s->version, s->nb_channels, s->sample_rate, s->bit_rate,
0efc832d9102 wma encoder
michael
parents:
diff changeset
182 s->block_align);
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4636
diff changeset
183 dprintf(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
184 bps, bps1, high_freq, s->byte_offset_bits);
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4636
diff changeset
185 dprintf(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
186 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
0efc832d9102 wma encoder
michael
parents:
diff changeset
187
0efc832d9102 wma encoder
michael
parents:
diff changeset
188 /* compute the scale factor band sizes for each MDCT block size */
0efc832d9102 wma encoder
michael
parents:
diff changeset
189 {
0efc832d9102 wma encoder
michael
parents:
diff changeset
190 int a, b, pos, lpos, k, block_len, i, j, n;
0efc832d9102 wma encoder
michael
parents:
diff changeset
191 const uint8_t *table;
0efc832d9102 wma encoder
michael
parents:
diff changeset
192
0efc832d9102 wma encoder
michael
parents:
diff changeset
193 if (s->version == 1) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
194 s->coefs_start = 3;
0efc832d9102 wma encoder
michael
parents:
diff changeset
195 } else {
0efc832d9102 wma encoder
michael
parents:
diff changeset
196 s->coefs_start = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
197 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
198 for(k = 0; k < s->nb_block_sizes; k++) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
199 block_len = s->frame_len >> k;
0efc832d9102 wma encoder
michael
parents:
diff changeset
200
0efc832d9102 wma encoder
michael
parents:
diff changeset
201 if (s->version == 1) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
202 lpos = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
203 for(i=0;i<25;i++) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
204 a = wma_critical_freqs[i];
0efc832d9102 wma encoder
michael
parents:
diff changeset
205 b = s->sample_rate;
0efc832d9102 wma encoder
michael
parents:
diff changeset
206 pos = ((block_len * 2 * a) + (b >> 1)) / b;
0efc832d9102 wma encoder
michael
parents:
diff changeset
207 if (pos > block_len)
0efc832d9102 wma encoder
michael
parents:
diff changeset
208 pos = block_len;
0efc832d9102 wma encoder
michael
parents:
diff changeset
209 s->exponent_bands[0][i] = pos - lpos;
0efc832d9102 wma encoder
michael
parents:
diff changeset
210 if (pos >= block_len) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
211 i++;
0efc832d9102 wma encoder
michael
parents:
diff changeset
212 break;
0efc832d9102 wma encoder
michael
parents:
diff changeset
213 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
214 lpos = pos;
0efc832d9102 wma encoder
michael
parents:
diff changeset
215 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
216 s->exponent_sizes[0] = i;
0efc832d9102 wma encoder
michael
parents:
diff changeset
217 } else {
0efc832d9102 wma encoder
michael
parents:
diff changeset
218 /* hardcoded tables */
0efc832d9102 wma encoder
michael
parents:
diff changeset
219 table = NULL;
0efc832d9102 wma encoder
michael
parents:
diff changeset
220 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
0efc832d9102 wma encoder
michael
parents:
diff changeset
221 if (a < 3) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
222 if (s->sample_rate >= 44100)
0efc832d9102 wma encoder
michael
parents:
diff changeset
223 table = exponent_band_44100[a];
0efc832d9102 wma encoder
michael
parents:
diff changeset
224 else if (s->sample_rate >= 32000)
0efc832d9102 wma encoder
michael
parents:
diff changeset
225 table = exponent_band_32000[a];
0efc832d9102 wma encoder
michael
parents:
diff changeset
226 else if (s->sample_rate >= 22050)
0efc832d9102 wma encoder
michael
parents:
diff changeset
227 table = exponent_band_22050[a];
0efc832d9102 wma encoder
michael
parents:
diff changeset
228 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
229 if (table) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
230 n = *table++;
0efc832d9102 wma encoder
michael
parents:
diff changeset
231 for(i=0;i<n;i++)
0efc832d9102 wma encoder
michael
parents:
diff changeset
232 s->exponent_bands[k][i] = table[i];
0efc832d9102 wma encoder
michael
parents:
diff changeset
233 s->exponent_sizes[k] = n;
0efc832d9102 wma encoder
michael
parents:
diff changeset
234 } else {
0efc832d9102 wma encoder
michael
parents:
diff changeset
235 j = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
236 lpos = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
237 for(i=0;i<25;i++) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
238 a = wma_critical_freqs[i];
0efc832d9102 wma encoder
michael
parents:
diff changeset
239 b = s->sample_rate;
0efc832d9102 wma encoder
michael
parents:
diff changeset
240 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
0efc832d9102 wma encoder
michael
parents:
diff changeset
241 pos <<= 2;
0efc832d9102 wma encoder
michael
parents:
diff changeset
242 if (pos > block_len)
0efc832d9102 wma encoder
michael
parents:
diff changeset
243 pos = block_len;
0efc832d9102 wma encoder
michael
parents:
diff changeset
244 if (pos > lpos)
0efc832d9102 wma encoder
michael
parents:
diff changeset
245 s->exponent_bands[k][j++] = pos - lpos;
0efc832d9102 wma encoder
michael
parents:
diff changeset
246 if (pos >= block_len)
0efc832d9102 wma encoder
michael
parents:
diff changeset
247 break;
0efc832d9102 wma encoder
michael
parents:
diff changeset
248 lpos = pos;
0efc832d9102 wma encoder
michael
parents:
diff changeset
249 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
250 s->exponent_sizes[k] = j;
0efc832d9102 wma encoder
michael
parents:
diff changeset
251 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
252 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
253
0efc832d9102 wma encoder
michael
parents:
diff changeset
254 /* max number of coefs */
0efc832d9102 wma encoder
michael
parents:
diff changeset
255 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
0efc832d9102 wma encoder
michael
parents:
diff changeset
256 /* high freq computation */
0efc832d9102 wma encoder
michael
parents:
diff changeset
257 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
0efc832d9102 wma encoder
michael
parents:
diff changeset
258 s->sample_rate + 0.5);
0efc832d9102 wma encoder
michael
parents:
diff changeset
259 n = s->exponent_sizes[k];
0efc832d9102 wma encoder
michael
parents:
diff changeset
260 j = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
261 pos = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
262 for(i=0;i<n;i++) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
263 int start, end;
0efc832d9102 wma encoder
michael
parents:
diff changeset
264 start = pos;
0efc832d9102 wma encoder
michael
parents:
diff changeset
265 pos += s->exponent_bands[k][i];
0efc832d9102 wma encoder
michael
parents:
diff changeset
266 end = pos;
0efc832d9102 wma encoder
michael
parents:
diff changeset
267 if (start < s->high_band_start[k])
0efc832d9102 wma encoder
michael
parents:
diff changeset
268 start = s->high_band_start[k];
0efc832d9102 wma encoder
michael
parents:
diff changeset
269 if (end > s->coefs_end[k])
0efc832d9102 wma encoder
michael
parents:
diff changeset
270 end = s->coefs_end[k];
0efc832d9102 wma encoder
michael
parents:
diff changeset
271 if (end > start)
0efc832d9102 wma encoder
michael
parents:
diff changeset
272 s->exponent_high_bands[k][j++] = end - start;
0efc832d9102 wma encoder
michael
parents:
diff changeset
273 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
274 s->exponent_high_sizes[k] = j;
0efc832d9102 wma encoder
michael
parents:
diff changeset
275 #if 0
4600
6ac364a4ce2b Supply context to tprintf
mbardiaux
parents: 4588
diff changeset
276 tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
277 s->frame_len >> k,
0efc832d9102 wma encoder
michael
parents:
diff changeset
278 s->coefs_end[k],
0efc832d9102 wma encoder
michael
parents:
diff changeset
279 s->high_band_start[k],
0efc832d9102 wma encoder
michael
parents:
diff changeset
280 s->exponent_high_sizes[k]);
0efc832d9102 wma encoder
michael
parents:
diff changeset
281 for(j=0;j<s->exponent_high_sizes[k];j++)
4600
6ac364a4ce2b Supply context to tprintf
mbardiaux
parents: 4588
diff changeset
282 tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
6ac364a4ce2b Supply context to tprintf
mbardiaux
parents: 4588
diff changeset
283 tprintf(s->avctx, "\n");
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
284 #endif
0efc832d9102 wma encoder
michael
parents:
diff changeset
285 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
286 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
287
0efc832d9102 wma encoder
michael
parents:
diff changeset
288 #ifdef TRACE
0efc832d9102 wma encoder
michael
parents:
diff changeset
289 {
0efc832d9102 wma encoder
michael
parents:
diff changeset
290 int i, j;
0efc832d9102 wma encoder
michael
parents:
diff changeset
291 for(i = 0; i < s->nb_block_sizes; i++) {
4600
6ac364a4ce2b Supply context to tprintf
mbardiaux
parents: 4588
diff changeset
292 tprintf(s->avctx, "%5d: n=%2d:",
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
293 s->frame_len >> i,
0efc832d9102 wma encoder
michael
parents:
diff changeset
294 s->exponent_sizes[i]);
0efc832d9102 wma encoder
michael
parents:
diff changeset
295 for(j=0;j<s->exponent_sizes[i];j++)
4600
6ac364a4ce2b Supply context to tprintf
mbardiaux
parents: 4588
diff changeset
296 tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
6ac364a4ce2b Supply context to tprintf
mbardiaux
parents: 4588
diff changeset
297 tprintf(s->avctx, "\n");
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
298 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
299 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
300 #endif
0efc832d9102 wma encoder
michael
parents:
diff changeset
301
0efc832d9102 wma encoder
michael
parents:
diff changeset
302 /* init MDCT windows : simple sinus window */
0efc832d9102 wma encoder
michael
parents:
diff changeset
303 for(i = 0; i < s->nb_block_sizes; i++) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
304 int n, j;
0efc832d9102 wma encoder
michael
parents:
diff changeset
305 float alpha;
0efc832d9102 wma encoder
michael
parents:
diff changeset
306 n = 1 << (s->frame_len_bits - i);
0efc832d9102 wma encoder
michael
parents:
diff changeset
307 window = av_malloc(sizeof(float) * n);
0efc832d9102 wma encoder
michael
parents:
diff changeset
308 alpha = M_PI / (2.0 * n);
0efc832d9102 wma encoder
michael
parents:
diff changeset
309 for(j=0;j<n;j++) {
4737
99d9dd34903b Optimize by building the mdct window and multipying/adding at the same time.
banan
parents: 4652
diff changeset
310 window[j] = sin((j + 0.5) * alpha);
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
311 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
312 s->windows[i] = window;
0efc832d9102 wma encoder
michael
parents:
diff changeset
313 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
314
0efc832d9102 wma encoder
michael
parents:
diff changeset
315 s->reset_block_lengths = 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
316
0efc832d9102 wma encoder
michael
parents:
diff changeset
317 if (s->use_noise_coding) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
318
0efc832d9102 wma encoder
michael
parents:
diff changeset
319 /* init the noise generator */
0efc832d9102 wma encoder
michael
parents:
diff changeset
320 if (s->use_exp_vlc)
0efc832d9102 wma encoder
michael
parents:
diff changeset
321 s->noise_mult = 0.02;
0efc832d9102 wma encoder
michael
parents:
diff changeset
322 else
0efc832d9102 wma encoder
michael
parents:
diff changeset
323 s->noise_mult = 0.04;
0efc832d9102 wma encoder
michael
parents:
diff changeset
324
0efc832d9102 wma encoder
michael
parents:
diff changeset
325 #ifdef TRACE
0efc832d9102 wma encoder
michael
parents:
diff changeset
326 for(i=0;i<NOISE_TAB_SIZE;i++)
0efc832d9102 wma encoder
michael
parents:
diff changeset
327 s->noise_table[i] = 1.0 * s->noise_mult;
0efc832d9102 wma encoder
michael
parents:
diff changeset
328 #else
0efc832d9102 wma encoder
michael
parents:
diff changeset
329 {
0efc832d9102 wma encoder
michael
parents:
diff changeset
330 unsigned int seed;
0efc832d9102 wma encoder
michael
parents:
diff changeset
331 float norm;
0efc832d9102 wma encoder
michael
parents:
diff changeset
332 seed = 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
333 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
0efc832d9102 wma encoder
michael
parents:
diff changeset
334 for(i=0;i<NOISE_TAB_SIZE;i++) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
335 seed = seed * 314159 + 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
336 s->noise_table[i] = (float)((int)seed) * norm;
0efc832d9102 wma encoder
michael
parents:
diff changeset
337 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
338 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
339 #endif
0efc832d9102 wma encoder
michael
parents:
diff changeset
340 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
341
0efc832d9102 wma encoder
michael
parents:
diff changeset
342 /* choose the VLC tables for the coefficients */
0efc832d9102 wma encoder
michael
parents:
diff changeset
343 coef_vlc_table = 2;
0efc832d9102 wma encoder
michael
parents:
diff changeset
344 if (s->sample_rate >= 32000) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
345 if (bps1 < 0.72)
0efc832d9102 wma encoder
michael
parents:
diff changeset
346 coef_vlc_table = 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
347 else if (bps1 < 1.16)
0efc832d9102 wma encoder
michael
parents:
diff changeset
348 coef_vlc_table = 1;
0efc832d9102 wma encoder
michael
parents:
diff changeset
349 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
350 s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
0efc832d9102 wma encoder
michael
parents:
diff changeset
351 s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
0efc832d9102 wma encoder
michael
parents:
diff changeset
352 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
0efc832d9102 wma encoder
michael
parents:
diff changeset
353 s->coef_vlcs[0]);
0efc832d9102 wma encoder
michael
parents:
diff changeset
354 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
0efc832d9102 wma encoder
michael
parents:
diff changeset
355 s->coef_vlcs[1]);
0efc832d9102 wma encoder
michael
parents:
diff changeset
356
0efc832d9102 wma encoder
michael
parents:
diff changeset
357 return 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
358 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
359
0efc832d9102 wma encoder
michael
parents:
diff changeset
360 int ff_wma_total_gain_to_bits(int total_gain){
0efc832d9102 wma encoder
michael
parents:
diff changeset
361 if (total_gain < 15) return 13;
0efc832d9102 wma encoder
michael
parents:
diff changeset
362 else if (total_gain < 32) return 12;
0efc832d9102 wma encoder
michael
parents:
diff changeset
363 else if (total_gain < 40) return 11;
0efc832d9102 wma encoder
michael
parents:
diff changeset
364 else if (total_gain < 45) return 10;
0efc832d9102 wma encoder
michael
parents:
diff changeset
365 else return 9;
0efc832d9102 wma encoder
michael
parents:
diff changeset
366 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
367
0efc832d9102 wma encoder
michael
parents:
diff changeset
368 int ff_wma_end(AVCodecContext *avctx)
0efc832d9102 wma encoder
michael
parents:
diff changeset
369 {
4601
7b9ce6f729ae Rename WMADecodeContext to WMACodecContext
mbardiaux
parents: 4600
diff changeset
370 WMACodecContext *s = avctx->priv_data;
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
371 int i;
0efc832d9102 wma encoder
michael
parents:
diff changeset
372
0efc832d9102 wma encoder
michael
parents:
diff changeset
373 for(i = 0; i < s->nb_block_sizes; i++)
0efc832d9102 wma encoder
michael
parents:
diff changeset
374 ff_mdct_end(&s->mdct_ctx[i]);
0efc832d9102 wma encoder
michael
parents:
diff changeset
375 for(i = 0; i < s->nb_block_sizes; i++)
0efc832d9102 wma encoder
michael
parents:
diff changeset
376 av_free(s->windows[i]);
0efc832d9102 wma encoder
michael
parents:
diff changeset
377
0efc832d9102 wma encoder
michael
parents:
diff changeset
378 if (s->use_exp_vlc) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
379 free_vlc(&s->exp_vlc);
0efc832d9102 wma encoder
michael
parents:
diff changeset
380 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
381 if (s->use_noise_coding) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
382 free_vlc(&s->hgain_vlc);
0efc832d9102 wma encoder
michael
parents:
diff changeset
383 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
384 for(i = 0;i < 2; i++) {
0efc832d9102 wma encoder
michael
parents:
diff changeset
385 free_vlc(&s->coef_vlc[i]);
0efc832d9102 wma encoder
michael
parents:
diff changeset
386 av_free(s->run_table[i]);
0efc832d9102 wma encoder
michael
parents:
diff changeset
387 av_free(s->level_table[i]);
5369
04a241c308e8 fix mem leak
benoit
parents: 5086
diff changeset
388 av_free(s->int_table[i]);
4490
0efc832d9102 wma encoder
michael
parents:
diff changeset
389 }
0efc832d9102 wma encoder
michael
parents:
diff changeset
390
0efc832d9102 wma encoder
michael
parents:
diff changeset
391 return 0;
0efc832d9102 wma encoder
michael
parents:
diff changeset
392 }