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