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 "sinewin.h"
00024 #include "wma.h"
00025 #include "wmadata.h"
00026
00027 #undef NDEBUG
00028 #include <assert.h>
00029
00030
00031
00032 static void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
00033 float **plevel_table, uint16_t **pint_table,
00034 const CoefVLCTable *vlc_table)
00035 {
00036 int n = vlc_table->n;
00037 const uint8_t *table_bits = vlc_table->huffbits;
00038 const uint32_t *table_codes = vlc_table->huffcodes;
00039 const uint16_t *levels_table = vlc_table->levels;
00040 uint16_t *run_table, *level_table, *int_table;
00041 float *flevel_table;
00042 int i, l, j, k, level;
00043
00044 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
00045
00046 run_table = av_malloc(n * sizeof(uint16_t));
00047 level_table = av_malloc(n * sizeof(uint16_t));
00048 flevel_table= av_malloc(n * sizeof(*flevel_table));
00049 int_table = av_malloc(n * sizeof(uint16_t));
00050 i = 2;
00051 level = 1;
00052 k = 0;
00053 while (i < n) {
00054 int_table[k] = i;
00055 l = levels_table[k++];
00056 for (j = 0; j < l; j++) {
00057 run_table[i] = j;
00058 level_table[i] = level;
00059 flevel_table[i]= level;
00060 i++;
00061 }
00062 level++;
00063 }
00064 *prun_table = run_table;
00065 *plevel_table = flevel_table;
00066 *pint_table = int_table;
00067 av_free(level_table);
00068 }
00069
00077 int av_cold ff_wma_get_frame_len_bits(int sample_rate, int version,
00078 unsigned int decode_flags)
00079 {
00080
00081 int frame_len_bits;
00082
00083 if (sample_rate <= 16000) {
00084 frame_len_bits = 9;
00085 } else if (sample_rate <= 22050 ||
00086 (sample_rate <= 32000 && version == 1)) {
00087 frame_len_bits = 10;
00088 } else if (sample_rate <= 48000 || version < 3) {
00089 frame_len_bits = 11;
00090 } else if (sample_rate <= 96000) {
00091 frame_len_bits = 12;
00092 } else {
00093 frame_len_bits = 13;
00094 }
00095
00096 if (version == 3) {
00097 int tmp = decode_flags & 0x6;
00098 if (tmp == 0x2) {
00099 ++frame_len_bits;
00100 } else if (tmp == 0x4) {
00101 --frame_len_bits;
00102 } else if (tmp == 0x6) {
00103 frame_len_bits -= 2;
00104 }
00105 }
00106
00107 return frame_len_bits;
00108 }
00109
00110 int ff_wma_init(AVCodecContext *avctx, int flags2)
00111 {
00112 WMACodecContext *s = avctx->priv_data;
00113 int i;
00114 float bps1, high_freq;
00115 volatile float bps;
00116 int sample_rate1;
00117 int coef_vlc_table;
00118
00119 if ( avctx->sample_rate <= 0 || avctx->sample_rate > 50000
00120 || avctx->channels <= 0 || avctx->channels > 8
00121 || avctx->bit_rate <= 0)
00122 return -1;
00123
00124 s->sample_rate = avctx->sample_rate;
00125 s->nb_channels = avctx->channels;
00126 s->bit_rate = avctx->bit_rate;
00127 s->block_align = avctx->block_align;
00128
00129 dsputil_init(&s->dsp, avctx);
00130 ff_fmt_convert_init(&s->fmt_conv, avctx);
00131
00132 if (avctx->codec->id == CODEC_ID_WMAV1) {
00133 s->version = 1;
00134 } else {
00135 s->version = 2;
00136 }
00137
00138
00139 s->frame_len_bits = ff_wma_get_frame_len_bits(s->sample_rate, s->version, 0);
00140 s->next_block_len_bits = s->frame_len_bits;
00141 s->prev_block_len_bits = s->frame_len_bits;
00142 s->block_len_bits = s->frame_len_bits;
00143
00144 s->frame_len = 1 << s->frame_len_bits;
00145 if (s->use_variable_block_len) {
00146 int nb_max, nb;
00147 nb = ((flags2 >> 3) & 3) + 1;
00148 if ((s->bit_rate / s->nb_channels) >= 32000)
00149 nb += 2;
00150 nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
00151 if (nb > nb_max)
00152 nb = nb_max;
00153 s->nb_block_sizes = nb + 1;
00154 } else {
00155 s->nb_block_sizes = 1;
00156 }
00157
00158
00159 s->use_noise_coding = 1;
00160 high_freq = s->sample_rate * 0.5;
00161
00162
00163 sample_rate1 = s->sample_rate;
00164 if (s->version == 2) {
00165 if (sample_rate1 >= 44100) {
00166 sample_rate1 = 44100;
00167 } else if (sample_rate1 >= 22050) {
00168 sample_rate1 = 22050;
00169 } else if (sample_rate1 >= 16000) {
00170 sample_rate1 = 16000;
00171 } else if (sample_rate1 >= 11025) {
00172 sample_rate1 = 11025;
00173 } else if (sample_rate1 >= 8000) {
00174 sample_rate1 = 8000;
00175 }
00176 }
00177
00178 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
00179 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
00180
00181
00182
00183 bps1 = bps;
00184 if (s->nb_channels == 2)
00185 bps1 = bps * 1.6;
00186 if (sample_rate1 == 44100) {
00187 if (bps1 >= 0.61) {
00188 s->use_noise_coding = 0;
00189 } else {
00190 high_freq = high_freq * 0.4;
00191 }
00192 } else if (sample_rate1 == 22050) {
00193 if (bps1 >= 1.16) {
00194 s->use_noise_coding = 0;
00195 } else if (bps1 >= 0.72) {
00196 high_freq = high_freq * 0.7;
00197 } else {
00198 high_freq = high_freq * 0.6;
00199 }
00200 } else if (sample_rate1 == 16000) {
00201 if (bps > 0.5) {
00202 high_freq = high_freq * 0.5;
00203 } else {
00204 high_freq = high_freq * 0.3;
00205 }
00206 } else if (sample_rate1 == 11025) {
00207 high_freq = high_freq * 0.7;
00208 } else if (sample_rate1 == 8000) {
00209 if (bps <= 0.625) {
00210 high_freq = high_freq * 0.5;
00211 } else if (bps > 0.75) {
00212 s->use_noise_coding = 0;
00213 } else {
00214 high_freq = high_freq * 0.65;
00215 }
00216 } else {
00217 if (bps >= 0.8) {
00218 high_freq = high_freq * 0.75;
00219 } else if (bps >= 0.6) {
00220 high_freq = high_freq * 0.6;
00221 } else {
00222 high_freq = high_freq * 0.5;
00223 }
00224 }
00225 av_dlog(s->avctx, "flags2=0x%x\n", flags2);
00226 av_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
00227 s->version, s->nb_channels, s->sample_rate, s->bit_rate,
00228 s->block_align);
00229 av_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
00230 bps, bps1, high_freq, s->byte_offset_bits);
00231 av_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
00232 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
00233
00234
00235 {
00236 int a, b, pos, lpos, k, block_len, i, j, n;
00237 const uint8_t *table;
00238
00239 if (s->version == 1) {
00240 s->coefs_start = 3;
00241 } else {
00242 s->coefs_start = 0;
00243 }
00244 for (k = 0; k < s->nb_block_sizes; k++) {
00245 block_len = s->frame_len >> k;
00246
00247 if (s->version == 1) {
00248 lpos = 0;
00249 for (i = 0; i < 25; i++) {
00250 a = ff_wma_critical_freqs[i];
00251 b = s->sample_rate;
00252 pos = ((block_len * 2 * a) + (b >> 1)) / b;
00253 if (pos > block_len)
00254 pos = block_len;
00255 s->exponent_bands[0][i] = pos - lpos;
00256 if (pos >= block_len) {
00257 i++;
00258 break;
00259 }
00260 lpos = pos;
00261 }
00262 s->exponent_sizes[0] = i;
00263 } else {
00264
00265 table = NULL;
00266 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
00267 if (a < 3) {
00268 if (s->sample_rate >= 44100) {
00269 table = exponent_band_44100[a];
00270 } else if (s->sample_rate >= 32000) {
00271 table = exponent_band_32000[a];
00272 } else if (s->sample_rate >= 22050) {
00273 table = exponent_band_22050[a];
00274 }
00275 }
00276 if (table) {
00277 n = *table++;
00278 for (i = 0; i < n; i++)
00279 s->exponent_bands[k][i] = table[i];
00280 s->exponent_sizes[k] = n;
00281 } else {
00282 j = 0;
00283 lpos = 0;
00284 for (i = 0; i < 25; i++) {
00285 a = ff_wma_critical_freqs[i];
00286 b = s->sample_rate;
00287 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
00288 pos <<= 2;
00289 if (pos > block_len)
00290 pos = block_len;
00291 if (pos > lpos)
00292 s->exponent_bands[k][j++] = pos - lpos;
00293 if (pos >= block_len)
00294 break;
00295 lpos = pos;
00296 }
00297 s->exponent_sizes[k] = j;
00298 }
00299 }
00300
00301
00302 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
00303
00304 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
00305 s->sample_rate + 0.5);
00306 n = s->exponent_sizes[k];
00307 j = 0;
00308 pos = 0;
00309 for (i = 0; i < n; i++) {
00310 int start, end;
00311 start = pos;
00312 pos += s->exponent_bands[k][i];
00313 end = pos;
00314 if (start < s->high_band_start[k])
00315 start = s->high_band_start[k];
00316 if (end > s->coefs_end[k])
00317 end = s->coefs_end[k];
00318 if (end > start)
00319 s->exponent_high_bands[k][j++] = end - start;
00320 }
00321 s->exponent_high_sizes[k] = j;
00322 #if 0
00323 tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
00324 s->frame_len >> k,
00325 s->coefs_end[k],
00326 s->high_band_start[k],
00327 s->exponent_high_sizes[k]);
00328 for (j = 0; j < s->exponent_high_sizes[k]; j++)
00329 tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
00330 tprintf(s->avctx, "\n");
00331 #endif
00332 }
00333 }
00334
00335 #ifdef TRACE
00336 {
00337 int i, j;
00338 for (i = 0; i < s->nb_block_sizes; i++) {
00339 tprintf(s->avctx, "%5d: n=%2d:",
00340 s->frame_len >> i,
00341 s->exponent_sizes[i]);
00342 for (j = 0; j < s->exponent_sizes[i]; j++)
00343 tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
00344 tprintf(s->avctx, "\n");
00345 }
00346 }
00347 #endif
00348
00349
00350 for (i = 0; i < s->nb_block_sizes; i++) {
00351 ff_init_ff_sine_windows(s->frame_len_bits - i);
00352 s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
00353 }
00354
00355 s->reset_block_lengths = 1;
00356
00357 if (s->use_noise_coding) {
00358
00359
00360 if (s->use_exp_vlc) {
00361 s->noise_mult = 0.02;
00362 } else {
00363 s->noise_mult = 0.04;
00364 }
00365
00366 #ifdef TRACE
00367 for (i = 0; i < NOISE_TAB_SIZE; i++)
00368 s->noise_table[i] = 1.0 * s->noise_mult;
00369 #else
00370 {
00371 unsigned int seed;
00372 float norm;
00373 seed = 1;
00374 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
00375 for (i = 0; i < NOISE_TAB_SIZE; i++) {
00376 seed = seed * 314159 + 1;
00377 s->noise_table[i] = (float)((int)seed) * norm;
00378 }
00379 }
00380 #endif
00381 }
00382
00383
00384 coef_vlc_table = 2;
00385 if (s->sample_rate >= 32000) {
00386 if (bps1 < 0.72) {
00387 coef_vlc_table = 0;
00388 } else if (bps1 < 1.16) {
00389 coef_vlc_table = 1;
00390 }
00391 }
00392 s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
00393 s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
00394 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
00395 s->coef_vlcs[0]);
00396 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
00397 s->coef_vlcs[1]);
00398
00399 return 0;
00400 }
00401
00402 int ff_wma_total_gain_to_bits(int total_gain)
00403 {
00404 if (total_gain < 15) return 13;
00405 else if (total_gain < 32) return 12;
00406 else if (total_gain < 40) return 11;
00407 else if (total_gain < 45) return 10;
00408 else return 9;
00409 }
00410
00411 int ff_wma_end(AVCodecContext *avctx)
00412 {
00413 WMACodecContext *s = avctx->priv_data;
00414 int i;
00415
00416 for (i = 0; i < s->nb_block_sizes; i++)
00417 ff_mdct_end(&s->mdct_ctx[i]);
00418
00419 if (s->use_exp_vlc) {
00420 ff_free_vlc(&s->exp_vlc);
00421 }
00422 if (s->use_noise_coding) {
00423 ff_free_vlc(&s->hgain_vlc);
00424 }
00425 for (i = 0; i < 2; i++) {
00426 ff_free_vlc(&s->coef_vlc[i]);
00427 av_free(s->run_table[i]);
00428 av_free(s->level_table[i]);
00429 av_free(s->int_table[i]);
00430 }
00431
00432 return 0;
00433 }
00434
00440 unsigned int ff_wma_get_large_val(GetBitContext* gb)
00441 {
00443 int n_bits = 8;
00445 if (get_bits1(gb)) {
00446 n_bits += 8;
00447 if (get_bits1(gb)) {
00448 n_bits += 8;
00449 if (get_bits1(gb)) {
00450 n_bits += 7;
00451 }
00452 }
00453 }
00454 return get_bits_long(gb, n_bits);
00455 }
00456
00473 int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,
00474 VLC *vlc,
00475 const float *level_table, const uint16_t *run_table,
00476 int version, WMACoef *ptr, int offset,
00477 int num_coefs, int block_len, int frame_len_bits,
00478 int coef_nb_bits)
00479 {
00480 int code, level, sign;
00481 const uint32_t *ilvl = (const uint32_t*)level_table;
00482 uint32_t *iptr = (uint32_t*)ptr;
00483 const unsigned int coef_mask = block_len - 1;
00484 for (; offset < num_coefs; offset++) {
00485 code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
00486 if (code > 1) {
00488 offset += run_table[code];
00489 sign = get_bits1(gb) - 1;
00490 iptr[offset & coef_mask] = ilvl[code] ^ sign<<31;
00491 } else if (code == 1) {
00493 break;
00494 } else {
00496 if (!version) {
00497 level = get_bits(gb, coef_nb_bits);
00500 offset += get_bits(gb, frame_len_bits);
00501 } else {
00502 level = ff_wma_get_large_val(gb);
00504 if (get_bits1(gb)) {
00505 if (get_bits1(gb)) {
00506 if (get_bits1(gb)) {
00507 av_log(avctx,AV_LOG_ERROR,
00508 "broken escape sequence\n");
00509 return -1;
00510 } else
00511 offset += get_bits(gb, frame_len_bits) + 4;
00512 } else
00513 offset += get_bits(gb, 2) + 1;
00514 }
00515 }
00516 sign = get_bits1(gb) - 1;
00517 ptr[offset & coef_mask] = (level^sign) - sign;
00518 }
00519 }
00521 if (offset > num_coefs) {
00522 av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
00523 return -1;
00524 }
00525
00526 return 0;
00527 }
00528