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 "internal.h"
00024 #include "wma.h"
00025 #include "libavutil/avassert.h"
00026
00027 #undef NDEBUG
00028 #include <assert.h>
00029
00030
00031 static int encode_init(AVCodecContext * avctx){
00032 WMACodecContext *s = avctx->priv_data;
00033 int i, flags1, flags2;
00034 uint8_t *extradata;
00035
00036 s->avctx = avctx;
00037
00038 if(avctx->channels > MAX_CHANNELS) {
00039 av_log(avctx, AV_LOG_ERROR, "too many channels: got %i, need %i or fewer",
00040 avctx->channels, MAX_CHANNELS);
00041 return AVERROR(EINVAL);
00042 }
00043
00044 if (avctx->sample_rate > 48000) {
00045 av_log(avctx, AV_LOG_ERROR, "sample rate is too high: %d > 48kHz",
00046 avctx->sample_rate);
00047 return AVERROR(EINVAL);
00048 }
00049
00050 if(avctx->bit_rate < 24*1000) {
00051 av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\n",
00052 avctx->bit_rate);
00053 return AVERROR(EINVAL);
00054 }
00055
00056
00057 flags1 = 0;
00058 flags2 = 1;
00059 if (avctx->codec->id == CODEC_ID_WMAV1) {
00060 extradata= av_malloc(4);
00061 avctx->extradata_size= 4;
00062 AV_WL16(extradata, flags1);
00063 AV_WL16(extradata+2, flags2);
00064 } else if (avctx->codec->id == CODEC_ID_WMAV2) {
00065 extradata= av_mallocz(10);
00066 avctx->extradata_size= 10;
00067 AV_WL32(extradata, flags1);
00068 AV_WL16(extradata+4, flags2);
00069 }else
00070 assert(0);
00071 avctx->extradata= extradata;
00072 s->use_exp_vlc = flags2 & 0x0001;
00073 s->use_bit_reservoir = flags2 & 0x0002;
00074 s->use_variable_block_len = flags2 & 0x0004;
00075 if (avctx->channels == 2)
00076 s->ms_stereo = 1;
00077
00078 ff_wma_init(avctx, flags2);
00079
00080
00081 for(i = 0; i < s->nb_block_sizes; i++)
00082 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);
00083
00084 s->block_align = avctx->bit_rate * (int64_t)s->frame_len /
00085 (avctx->sample_rate * 8);
00086 s->block_align = FFMIN(s->block_align, MAX_CODED_SUPERFRAME_SIZE);
00087 avctx->block_align = s->block_align;
00088
00089 avctx->frame_size = avctx->delay = s->frame_len;
00090
00091 #if FF_API_OLD_ENCODE_AUDIO
00092 avctx->coded_frame = &s->frame;
00093 avcodec_get_frame_defaults(avctx->coded_frame);
00094 #endif
00095
00096 return 0;
00097 }
00098
00099
00100 static void apply_window_and_mdct(AVCodecContext * avctx, const signed short * audio, int len) {
00101 WMACodecContext *s = avctx->priv_data;
00102 int window_index= s->frame_len_bits - s->block_len_bits;
00103 FFTContext *mdct = &s->mdct_ctx[window_index];
00104 int i, j, channel;
00105 const float * win = s->windows[window_index];
00106 int window_len = 1 << s->block_len_bits;
00107 float n = window_len/2;
00108
00109 for (channel = 0; channel < avctx->channels; channel++) {
00110 memcpy(s->output, s->frame_out[channel], sizeof(float)*window_len);
00111 j = channel;
00112 for (i = 0; i < len; i++, j += avctx->channels){
00113 s->output[i+window_len] = audio[j] / n * win[window_len - i - 1];
00114 s->frame_out[channel][i] = audio[j] / n * win[i];
00115 }
00116 mdct->mdct_calc(mdct, s->coefs[channel], s->output);
00117 }
00118 }
00119
00120
00121 static void init_exp(WMACodecContext *s, int ch, const int *exp_param){
00122 int n;
00123 const uint16_t *ptr;
00124 float v, *q, max_scale, *q_end;
00125
00126 ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
00127 q = s->exponents[ch];
00128 q_end = q + s->block_len;
00129 max_scale = 0;
00130 while (q < q_end) {
00131
00132 v = pow(10, *exp_param++ * (1.0 / 16.0));
00133 max_scale= FFMAX(max_scale, v);
00134 n = *ptr++;
00135 do {
00136 *q++ = v;
00137 } while (--n);
00138 }
00139 s->max_exponent[ch] = max_scale;
00140 }
00141
00142 static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param){
00143 int last_exp;
00144 const uint16_t *ptr;
00145 float *q, *q_end;
00146
00147 ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
00148 q = s->exponents[ch];
00149 q_end = q + s->block_len;
00150 if (s->version == 1) {
00151 last_exp= *exp_param++;
00152 assert(last_exp-10 >= 0 && last_exp-10 < 32);
00153 put_bits(&s->pb, 5, last_exp - 10);
00154 q+= *ptr++;
00155 }else
00156 last_exp = 36;
00157 while (q < q_end) {
00158 int exp = *exp_param++;
00159 int code = exp - last_exp + 60;
00160 assert(code >= 0 && code < 120);
00161 put_bits(&s->pb, ff_aac_scalefactor_bits[code], ff_aac_scalefactor_code[code]);
00162
00163 q+= *ptr++;
00164 last_exp= exp;
00165 }
00166 }
00167
00168 static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], int total_gain){
00169 int v, bsize, ch, coef_nb_bits, parse_exponents;
00170 float mdct_norm;
00171 int nb_coefs[MAX_CHANNELS];
00172 static const int fixed_exp[25]={20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20};
00173
00174
00175 if (s->use_variable_block_len) {
00176 assert(0);
00177 }else{
00178
00179 s->next_block_len_bits = s->frame_len_bits;
00180 s->prev_block_len_bits = s->frame_len_bits;
00181 s->block_len_bits = s->frame_len_bits;
00182 }
00183
00184 s->block_len = 1 << s->block_len_bits;
00185
00186 bsize = s->frame_len_bits - s->block_len_bits;
00187
00188
00189 v = s->coefs_end[bsize] - s->coefs_start;
00190 for(ch = 0; ch < s->nb_channels; ch++)
00191 nb_coefs[ch] = v;
00192 {
00193 int n4 = s->block_len / 2;
00194 mdct_norm = 1.0 / (float)n4;
00195 if (s->version == 1) {
00196 mdct_norm *= sqrt(n4);
00197 }
00198 }
00199
00200 if (s->nb_channels == 2) {
00201 put_bits(&s->pb, 1, !!s->ms_stereo);
00202 }
00203
00204 for(ch = 0; ch < s->nb_channels; ch++) {
00205 s->channel_coded[ch] = 1;
00206 if (s->channel_coded[ch]) {
00207 init_exp(s, ch, fixed_exp);
00208 }
00209 }
00210
00211 for(ch = 0; ch < s->nb_channels; ch++) {
00212 if (s->channel_coded[ch]) {
00213 WMACoef *coefs1;
00214 float *coefs, *exponents, mult;
00215 int i, n;
00216
00217 coefs1 = s->coefs1[ch];
00218 exponents = s->exponents[ch];
00219 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
00220 mult *= mdct_norm;
00221 coefs = src_coefs[ch];
00222 if (s->use_noise_coding && 0) {
00223 assert(0);
00224 } else {
00225 coefs += s->coefs_start;
00226 n = nb_coefs[ch];
00227 for(i = 0;i < n; i++){
00228 double t= *coefs++ / (exponents[i] * mult);
00229 if(t<-32768 || t>32767)
00230 return -1;
00231
00232 coefs1[i] = lrint(t);
00233 }
00234 }
00235 }
00236 }
00237
00238 v = 0;
00239 for(ch = 0; ch < s->nb_channels; ch++) {
00240 int a = s->channel_coded[ch];
00241 put_bits(&s->pb, 1, a);
00242 v |= a;
00243 }
00244
00245 if (!v)
00246 return 1;
00247
00248 for(v= total_gain-1; v>=127; v-= 127)
00249 put_bits(&s->pb, 7, 127);
00250 put_bits(&s->pb, 7, v);
00251
00252 coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
00253
00254 if (s->use_noise_coding) {
00255 for(ch = 0; ch < s->nb_channels; ch++) {
00256 if (s->channel_coded[ch]) {
00257 int i, n;
00258 n = s->exponent_high_sizes[bsize];
00259 for(i=0;i<n;i++) {
00260 put_bits(&s->pb, 1, s->high_band_coded[ch][i]= 0);
00261 if (0)
00262 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
00263 }
00264 }
00265 }
00266 }
00267
00268 parse_exponents = 1;
00269 if (s->block_len_bits != s->frame_len_bits) {
00270 put_bits(&s->pb, 1, parse_exponents);
00271 }
00272
00273 if (parse_exponents) {
00274 for(ch = 0; ch < s->nb_channels; ch++) {
00275 if (s->channel_coded[ch]) {
00276 if (s->use_exp_vlc) {
00277 encode_exp_vlc(s, ch, fixed_exp);
00278 } else {
00279 assert(0);
00280
00281 }
00282 }
00283 }
00284 } else {
00285 assert(0);
00286 }
00287
00288 for(ch = 0; ch < s->nb_channels; ch++) {
00289 if (s->channel_coded[ch]) {
00290 int run, tindex;
00291 WMACoef *ptr, *eptr;
00292 tindex = (ch == 1 && s->ms_stereo);
00293 ptr = &s->coefs1[ch][0];
00294 eptr = ptr + nb_coefs[ch];
00295
00296 run=0;
00297 for(;ptr < eptr; ptr++){
00298 if(*ptr){
00299 int level= *ptr;
00300 int abs_level= FFABS(level);
00301 int code= 0;
00302 if(abs_level <= s->coef_vlcs[tindex]->max_level){
00303 if(run < s->coef_vlcs[tindex]->levels[abs_level-1])
00304 code= run + s->int_table[tindex][abs_level-1];
00305 }
00306
00307 assert(code < s->coef_vlcs[tindex]->n);
00308 put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code], s->coef_vlcs[tindex]->huffcodes[code]);
00309
00310 if(code == 0){
00311 if(1<<coef_nb_bits <= abs_level)
00312 return -1;
00313
00314 put_bits(&s->pb, coef_nb_bits, abs_level);
00315 put_bits(&s->pb, s->frame_len_bits, run);
00316 }
00317 put_bits(&s->pb, 1, level < 0);
00318 run=0;
00319 }else{
00320 run++;
00321 }
00322 }
00323 if(run)
00324 put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]);
00325 }
00326 if (s->version == 1 && s->nb_channels >= 2) {
00327 avpriv_align_put_bits(&s->pb);
00328 }
00329 }
00330 return 0;
00331 }
00332
00333 static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], uint8_t *buf, int buf_size, int total_gain){
00334 init_put_bits(&s->pb, buf, buf_size);
00335
00336 if (s->use_bit_reservoir) {
00337 assert(0);
00338 }else{
00339 if(encode_block(s, src_coefs, total_gain) < 0)
00340 return INT_MAX;
00341 }
00342
00343 avpriv_align_put_bits(&s->pb);
00344
00345 return put_bits_count(&s->pb)/8 - s->block_align;
00346 }
00347
00348 static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
00349 const AVFrame *frame, int *got_packet_ptr)
00350 {
00351 WMACodecContext *s = avctx->priv_data;
00352 const int16_t *samples = (const int16_t *)frame->data[0];
00353 int i, total_gain, ret;
00354
00355 s->block_len_bits= s->frame_len_bits;
00356 s->block_len = 1 << s->block_len_bits;
00357
00358 apply_window_and_mdct(avctx, samples, frame->nb_samples);
00359
00360 if (s->ms_stereo) {
00361 float a, b;
00362 int i;
00363
00364 for(i = 0; i < s->block_len; i++) {
00365 a = s->coefs[0][i]*0.5;
00366 b = s->coefs[1][i]*0.5;
00367 s->coefs[0][i] = a + b;
00368 s->coefs[1][i] = a - b;
00369 }
00370 }
00371
00372 if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * MAX_CODED_SUPERFRAME_SIZE)))
00373 return ret;
00374
00375 #if 1
00376 total_gain= 128;
00377 for(i=64; i; i>>=1){
00378 int error = encode_frame(s, s->coefs, avpkt->data, avpkt->size,
00379 total_gain - i);
00380 if(error<0)
00381 total_gain-= i;
00382 }
00383 #else
00384 total_gain= 90;
00385 best = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain);
00386 for(i=32; i; i>>=1){
00387 int scoreL = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain - i);
00388 int scoreR = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain + i);
00389 av_log(NULL, AV_LOG_ERROR, "%d %d %d (%d)\n", scoreL, best, scoreR, total_gain);
00390 if(scoreL < FFMIN(best, scoreR)){
00391 best = scoreL;
00392 total_gain -= i;
00393 }else if(scoreR < best){
00394 best = scoreR;
00395 total_gain += i;
00396 }
00397 }
00398 #endif
00399
00400 encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain);
00401 av_assert0((put_bits_count(&s->pb) & 7) == 0);
00402 i= s->block_align - (put_bits_count(&s->pb)+7)/8;
00403 av_assert0(i>=0);
00404 while(i--)
00405 put_bits(&s->pb, 8, 'N');
00406
00407 flush_put_bits(&s->pb);
00408 av_assert0(put_bits_ptr(&s->pb) - s->pb.buf == s->block_align);
00409
00410 if (frame->pts != AV_NOPTS_VALUE)
00411 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
00412
00413 avpkt->size = s->block_align;
00414 *got_packet_ptr = 1;
00415 return 0;
00416 }
00417
00418 AVCodec ff_wmav1_encoder = {
00419 .name = "wmav1",
00420 .type = AVMEDIA_TYPE_AUDIO,
00421 .id = CODEC_ID_WMAV1,
00422 .priv_data_size = sizeof(WMACodecContext),
00423 .init = encode_init,
00424 .encode2 = encode_superframe,
00425 .close = ff_wma_end,
00426 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00427 AV_SAMPLE_FMT_NONE },
00428 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
00429 };
00430
00431 AVCodec ff_wmav2_encoder = {
00432 .name = "wmav2",
00433 .type = AVMEDIA_TYPE_AUDIO,
00434 .id = CODEC_ID_WMAV2,
00435 .priv_data_size = sizeof(WMACodecContext),
00436 .init = encode_init,
00437 .encode2 = encode_superframe,
00438 .close = ff_wma_end,
00439 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00440 AV_SAMPLE_FMT_NONE },
00441 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
00442 };