00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "wma.h"
00024 #include "wmadata.h"
00025
00026 #undef NDEBUG
00027 #include <assert.h>
00028
00029
00030
00031 static void init_coef_vlc(VLC *vlc,
00032 uint16_t **prun_table, uint16_t **plevel_table, uint16_t **pint_table,
00033 const CoefVLCTable *vlc_table)
00034 {
00035 int n = vlc_table->n;
00036 const uint8_t *table_bits = vlc_table->huffbits;
00037 const uint32_t *table_codes = vlc_table->huffcodes;
00038 const uint16_t *levels_table = vlc_table->levels;
00039 uint16_t *run_table, *level_table, *int_table;
00040 int i, l, j, k, level;
00041
00042 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
00043
00044 run_table = av_malloc(n * sizeof(uint16_t));
00045 level_table = av_malloc(n * sizeof(uint16_t));
00046 int_table = av_malloc(n * sizeof(uint16_t));
00047 i = 2;
00048 level = 1;
00049 k = 0;
00050 while (i < n) {
00051 int_table[k]= i;
00052 l = levels_table[k++];
00053 for(j=0;j<l;j++) {
00054 run_table[i] = j;
00055 level_table[i] = level;
00056 i++;
00057 }
00058 level++;
00059 }
00060 *prun_table = run_table;
00061 *plevel_table = level_table;
00062 *pint_table= int_table;
00063 }
00064
00065 int ff_wma_init(AVCodecContext * avctx, int flags2)
00066 {
00067 WMACodecContext *s = avctx->priv_data;
00068 int i;
00069 float bps1, high_freq;
00070 volatile float bps;
00071 int sample_rate1;
00072 int coef_vlc_table;
00073
00074 if( avctx->sample_rate<=0 || avctx->sample_rate>50000
00075 || avctx->channels<=0 || avctx->channels>8
00076 || avctx->bit_rate<=0)
00077 return -1;
00078
00079 s->sample_rate = avctx->sample_rate;
00080 s->nb_channels = avctx->channels;
00081 s->bit_rate = avctx->bit_rate;
00082 s->block_align = avctx->block_align;
00083
00084 dsputil_init(&s->dsp, avctx);
00085
00086 if (avctx->codec->id == CODEC_ID_WMAV1) {
00087 s->version = 1;
00088 } else {
00089 s->version = 2;
00090 }
00091
00092
00093 if (s->sample_rate <= 16000) {
00094 s->frame_len_bits = 9;
00095 } else if (s->sample_rate <= 22050 ||
00096 (s->sample_rate <= 32000 && s->version == 1)) {
00097 s->frame_len_bits = 10;
00098 } else {
00099 s->frame_len_bits = 11;
00100 }
00101 s->frame_len = 1 << s->frame_len_bits;
00102 if (s->use_variable_block_len) {
00103 int nb_max, nb;
00104 nb = ((flags2 >> 3) & 3) + 1;
00105 if ((s->bit_rate / s->nb_channels) >= 32000)
00106 nb += 2;
00107 nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
00108 if (nb > nb_max)
00109 nb = nb_max;
00110 s->nb_block_sizes = nb + 1;
00111 } else {
00112 s->nb_block_sizes = 1;
00113 }
00114
00115
00116 s->use_noise_coding = 1;
00117 high_freq = s->sample_rate * 0.5;
00118
00119
00120 sample_rate1 = s->sample_rate;
00121 if (s->version == 2) {
00122 if (sample_rate1 >= 44100)
00123 sample_rate1 = 44100;
00124 else if (sample_rate1 >= 22050)
00125 sample_rate1 = 22050;
00126 else if (sample_rate1 >= 16000)
00127 sample_rate1 = 16000;
00128 else if (sample_rate1 >= 11025)
00129 sample_rate1 = 11025;
00130 else if (sample_rate1 >= 8000)
00131 sample_rate1 = 8000;
00132 }
00133
00134 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
00135 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
00136
00137
00138
00139 bps1 = bps;
00140 if (s->nb_channels == 2)
00141 bps1 = bps * 1.6;
00142 if (sample_rate1 == 44100) {
00143 if (bps1 >= 0.61)
00144 s->use_noise_coding = 0;
00145 else
00146 high_freq = high_freq * 0.4;
00147 } else if (sample_rate1 == 22050) {
00148 if (bps1 >= 1.16)
00149 s->use_noise_coding = 0;
00150 else if (bps1 >= 0.72)
00151 high_freq = high_freq * 0.7;
00152 else
00153 high_freq = high_freq * 0.6;
00154 } else if (sample_rate1 == 16000) {
00155 if (bps > 0.5)
00156 high_freq = high_freq * 0.5;
00157 else
00158 high_freq = high_freq * 0.3;
00159 } else if (sample_rate1 == 11025) {
00160 high_freq = high_freq * 0.7;
00161 } else if (sample_rate1 == 8000) {
00162 if (bps <= 0.625) {
00163 high_freq = high_freq * 0.5;
00164 } else if (bps > 0.75) {
00165 s->use_noise_coding = 0;
00166 } else {
00167 high_freq = high_freq * 0.65;
00168 }
00169 } else {
00170 if (bps >= 0.8) {
00171 high_freq = high_freq * 0.75;
00172 } else if (bps >= 0.6) {
00173 high_freq = high_freq * 0.6;
00174 } else {
00175 high_freq = high_freq * 0.5;
00176 }
00177 }
00178 dprintf(s->avctx, "flags2=0x%x\n", flags2);
00179 dprintf(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
00180 s->version, s->nb_channels, s->sample_rate, s->bit_rate,
00181 s->block_align);
00182 dprintf(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
00183 bps, bps1, high_freq, s->byte_offset_bits);
00184 dprintf(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
00185 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
00186
00187
00188 {
00189 int a, b, pos, lpos, k, block_len, i, j, n;
00190 const uint8_t *table;
00191
00192 if (s->version == 1) {
00193 s->coefs_start = 3;
00194 } else {
00195 s->coefs_start = 0;
00196 }
00197 for(k = 0; k < s->nb_block_sizes; k++) {
00198 block_len = s->frame_len >> k;
00199
00200 if (s->version == 1) {
00201 lpos = 0;
00202 for(i=0;i<25;i++) {
00203 a = wma_critical_freqs[i];
00204 b = s->sample_rate;
00205 pos = ((block_len * 2 * a) + (b >> 1)) / b;
00206 if (pos > block_len)
00207 pos = block_len;
00208 s->exponent_bands[0][i] = pos - lpos;
00209 if (pos >= block_len) {
00210 i++;
00211 break;
00212 }
00213 lpos = pos;
00214 }
00215 s->exponent_sizes[0] = i;
00216 } else {
00217
00218 table = NULL;
00219 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
00220 if (a < 3) {
00221 if (s->sample_rate >= 44100)
00222 table = exponent_band_44100[a];
00223 else if (s->sample_rate >= 32000)
00224 table = exponent_band_32000[a];
00225 else if (s->sample_rate >= 22050)
00226 table = exponent_band_22050[a];
00227 }
00228 if (table) {
00229 n = *table++;
00230 for(i=0;i<n;i++)
00231 s->exponent_bands[k][i] = table[i];
00232 s->exponent_sizes[k] = n;
00233 } else {
00234 j = 0;
00235 lpos = 0;
00236 for(i=0;i<25;i++) {
00237 a = wma_critical_freqs[i];
00238 b = s->sample_rate;
00239 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
00240 pos <<= 2;
00241 if (pos > block_len)
00242 pos = block_len;
00243 if (pos > lpos)
00244 s->exponent_bands[k][j++] = pos - lpos;
00245 if (pos >= block_len)
00246 break;
00247 lpos = pos;
00248 }
00249 s->exponent_sizes[k] = j;
00250 }
00251 }
00252
00253
00254 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
00255
00256 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
00257 s->sample_rate + 0.5);
00258 n = s->exponent_sizes[k];
00259 j = 0;
00260 pos = 0;
00261 for(i=0;i<n;i++) {
00262 int start, end;
00263 start = pos;
00264 pos += s->exponent_bands[k][i];
00265 end = pos;
00266 if (start < s->high_band_start[k])
00267 start = s->high_band_start[k];
00268 if (end > s->coefs_end[k])
00269 end = s->coefs_end[k];
00270 if (end > start)
00271 s->exponent_high_bands[k][j++] = end - start;
00272 }
00273 s->exponent_high_sizes[k] = j;
00274 #if 0
00275 tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
00276 s->frame_len >> k,
00277 s->coefs_end[k],
00278 s->high_band_start[k],
00279 s->exponent_high_sizes[k]);
00280 for(j=0;j<s->exponent_high_sizes[k];j++)
00281 tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
00282 tprintf(s->avctx, "\n");
00283 #endif
00284 }
00285 }
00286
00287 #ifdef TRACE
00288 {
00289 int i, j;
00290 for(i = 0; i < s->nb_block_sizes; i++) {
00291 tprintf(s->avctx, "%5d: n=%2d:",
00292 s->frame_len >> i,
00293 s->exponent_sizes[i]);
00294 for(j=0;j<s->exponent_sizes[i];j++)
00295 tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
00296 tprintf(s->avctx, "\n");
00297 }
00298 }
00299 #endif
00300
00301
00302 for(i = 0; i < s->nb_block_sizes; i++) {
00303 int n;
00304 n = 1 << (s->frame_len_bits - i);
00305 ff_sine_window_init(ff_sine_windows[s->frame_len_bits - i - 7], n);
00306 s->windows[i] = ff_sine_windows[s->frame_len_bits - i - 7];
00307 }
00308
00309 s->reset_block_lengths = 1;
00310
00311 if (s->use_noise_coding) {
00312
00313
00314 if (s->use_exp_vlc)
00315 s->noise_mult = 0.02;
00316 else
00317 s->noise_mult = 0.04;
00318
00319 #ifdef TRACE
00320 for(i=0;i<NOISE_TAB_SIZE;i++)
00321 s->noise_table[i] = 1.0 * s->noise_mult;
00322 #else
00323 {
00324 unsigned int seed;
00325 float norm;
00326 seed = 1;
00327 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
00328 for(i=0;i<NOISE_TAB_SIZE;i++) {
00329 seed = seed * 314159 + 1;
00330 s->noise_table[i] = (float)((int)seed) * norm;
00331 }
00332 }
00333 #endif
00334 }
00335
00336
00337 coef_vlc_table = 2;
00338 if (s->sample_rate >= 32000) {
00339 if (bps1 < 0.72)
00340 coef_vlc_table = 0;
00341 else if (bps1 < 1.16)
00342 coef_vlc_table = 1;
00343 }
00344 s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
00345 s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
00346 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
00347 s->coef_vlcs[0]);
00348 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
00349 s->coef_vlcs[1]);
00350
00351 return 0;
00352 }
00353
00354 int ff_wma_total_gain_to_bits(int total_gain){
00355 if (total_gain < 15) return 13;
00356 else if (total_gain < 32) return 12;
00357 else if (total_gain < 40) return 11;
00358 else if (total_gain < 45) return 10;
00359 else return 9;
00360 }
00361
00362 int ff_wma_end(AVCodecContext *avctx)
00363 {
00364 WMACodecContext *s = avctx->priv_data;
00365 int i;
00366
00367 for(i = 0; i < s->nb_block_sizes; i++)
00368 ff_mdct_end(&s->mdct_ctx[i]);
00369
00370 if (s->use_exp_vlc) {
00371 free_vlc(&s->exp_vlc);
00372 }
00373 if (s->use_noise_coding) {
00374 free_vlc(&s->hgain_vlc);
00375 }
00376 for(i = 0;i < 2; i++) {
00377 free_vlc(&s->coef_vlc[i]);
00378 av_free(s->run_table[i]);
00379 av_free(s->level_table[i]);
00380 av_free(s->int_table[i]);
00381 }
00382
00383 return 0;
00384 }