• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/wmaenc.c

Go to the documentation of this file.
00001 /*
00002  * WMA compatible encoder
00003  * Copyright (c) 2007 Michael Niedermayer
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avcodec.h"
00023 #include "wma.h"
00024 
00025 #undef NDEBUG
00026 #include <assert.h>
00027 
00028 
00029 static int encode_init(AVCodecContext * avctx){
00030     WMACodecContext *s = avctx->priv_data;
00031     int i, flags1, flags2;
00032     uint8_t *extradata;
00033 
00034     s->avctx = avctx;
00035 
00036     if(avctx->channels > MAX_CHANNELS) {
00037         av_log(avctx, AV_LOG_ERROR, "too many channels: got %i, need %i or fewer",
00038                avctx->channels, MAX_CHANNELS);
00039         return AVERROR(EINVAL);
00040     }
00041 
00042     if (avctx->sample_rate > 48000) {
00043         av_log(avctx, AV_LOG_ERROR, "sample rate is too high: %d > 48kHz",
00044                avctx->sample_rate);
00045         return AVERROR(EINVAL);
00046     }
00047 
00048     if(avctx->bit_rate < 24*1000) {
00049         av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\n",
00050                avctx->bit_rate);
00051         return AVERROR(EINVAL);
00052     }
00053 
00054     /* extract flag infos */
00055     flags1 = 0;
00056     flags2 = 1;
00057     if (avctx->codec->id == CODEC_ID_WMAV1) {
00058         extradata= av_malloc(4);
00059         avctx->extradata_size= 4;
00060         AV_WL16(extradata, flags1);
00061         AV_WL16(extradata+2, flags2);
00062     } else if (avctx->codec->id == CODEC_ID_WMAV2) {
00063         extradata= av_mallocz(10);
00064         avctx->extradata_size= 10;
00065         AV_WL32(extradata, flags1);
00066         AV_WL16(extradata+4, flags2);
00067     }else
00068         assert(0);
00069     avctx->extradata= extradata;
00070     s->use_exp_vlc = flags2 & 0x0001;
00071     s->use_bit_reservoir = flags2 & 0x0002;
00072     s->use_variable_block_len = flags2 & 0x0004;
00073     if (avctx->channels == 2)
00074         s->ms_stereo = 1;
00075 
00076     ff_wma_init(avctx, flags2);
00077 
00078     /* init MDCT */
00079     for(i = 0; i < s->nb_block_sizes; i++)
00080         ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);
00081 
00082     s->block_align     = avctx->bit_rate * (int64_t)s->frame_len /
00083                          (avctx->sample_rate * 8);
00084     s->block_align     = FFMIN(s->block_align, MAX_CODED_SUPERFRAME_SIZE);
00085     avctx->block_align = s->block_align;
00086     avctx->bit_rate    = avctx->block_align * 8LL * avctx->sample_rate /
00087                          s->frame_len;
00088 //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d\n", s->block_align, avctx->bit_rate, s->frame_len, avctx->sample_rate);
00089     avctx->frame_size= s->frame_len;
00090 
00091     return 0;
00092 }
00093 
00094 
00095 static void apply_window_and_mdct(AVCodecContext * avctx, const signed short * audio, int len) {
00096     WMACodecContext *s = avctx->priv_data;
00097     int window_index= s->frame_len_bits - s->block_len_bits;
00098     FFTContext *mdct = &s->mdct_ctx[window_index];
00099     int i, j, channel;
00100     const float * win = s->windows[window_index];
00101     int window_len = 1 << s->block_len_bits;
00102     float n = window_len/2;
00103 
00104     for (channel = 0; channel < avctx->channels; channel++) {
00105         memcpy(s->output, s->frame_out[channel], sizeof(float)*window_len);
00106         j = channel;
00107         for (i = 0; i < len; i++, j += avctx->channels){
00108             s->output[i+window_len]  = audio[j] / n * win[window_len - i - 1];
00109             s->frame_out[channel][i] = audio[j] / n * win[i];
00110         }
00111         mdct->mdct_calc(mdct, s->coefs[channel], s->output);
00112     }
00113 }
00114 
00115 //FIXME use for decoding too
00116 static void init_exp(WMACodecContext *s, int ch, const int *exp_param){
00117     int n;
00118     const uint16_t *ptr;
00119     float v, *q, max_scale, *q_end;
00120 
00121     ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
00122     q = s->exponents[ch];
00123     q_end = q + s->block_len;
00124     max_scale = 0;
00125     while (q < q_end) {
00126         /* XXX: use a table */
00127         v = pow(10, *exp_param++ * (1.0 / 16.0));
00128         max_scale= FFMAX(max_scale, v);
00129         n = *ptr++;
00130         do {
00131             *q++ = v;
00132         } while (--n);
00133     }
00134     s->max_exponent[ch] = max_scale;
00135 }
00136 
00137 static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param){
00138     int last_exp;
00139     const uint16_t *ptr;
00140     float *q, *q_end;
00141 
00142     ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
00143     q = s->exponents[ch];
00144     q_end = q + s->block_len;
00145     if (s->version == 1) {
00146         last_exp= *exp_param++;
00147         assert(last_exp-10 >= 0 && last_exp-10 < 32);
00148         put_bits(&s->pb, 5, last_exp - 10);
00149         q+= *ptr++;
00150     }else
00151         last_exp = 36;
00152     while (q < q_end) {
00153         int exp = *exp_param++;
00154         int code = exp - last_exp + 60;
00155         assert(code >= 0 && code < 120);
00156         put_bits(&s->pb, ff_aac_scalefactor_bits[code], ff_aac_scalefactor_code[code]);
00157         /* XXX: use a table */
00158         q+= *ptr++;
00159         last_exp= exp;
00160     }
00161 }
00162 
00163 static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], int total_gain){
00164     int v, bsize, ch, coef_nb_bits, parse_exponents;
00165     float mdct_norm;
00166     int nb_coefs[MAX_CHANNELS];
00167     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};
00168 
00169     //FIXME remove duplication relative to decoder
00170     if (s->use_variable_block_len) {
00171         assert(0); //FIXME not implemented
00172     }else{
00173         /* fixed block len */
00174         s->next_block_len_bits = s->frame_len_bits;
00175         s->prev_block_len_bits = s->frame_len_bits;
00176         s->block_len_bits = s->frame_len_bits;
00177     }
00178 
00179     s->block_len = 1 << s->block_len_bits;
00180 //     assert((s->block_pos + s->block_len) <= s->frame_len);
00181     bsize = s->frame_len_bits - s->block_len_bits;
00182 
00183     //FIXME factor
00184     v = s->coefs_end[bsize] - s->coefs_start;
00185     for(ch = 0; ch < s->nb_channels; ch++)
00186         nb_coefs[ch] = v;
00187     {
00188         int n4 = s->block_len / 2;
00189         mdct_norm = 1.0 / (float)n4;
00190         if (s->version == 1) {
00191             mdct_norm *= sqrt(n4);
00192         }
00193     }
00194 
00195     if (s->nb_channels == 2) {
00196         put_bits(&s->pb, 1, !!s->ms_stereo);
00197     }
00198 
00199     for(ch = 0; ch < s->nb_channels; ch++) {
00200         s->channel_coded[ch] = 1; //FIXME only set channel_coded when needed, instead of always
00201         if (s->channel_coded[ch]) {
00202             init_exp(s, ch, fixed_exp);
00203         }
00204     }
00205 
00206     for(ch = 0; ch < s->nb_channels; ch++) {
00207         if (s->channel_coded[ch]) {
00208             WMACoef *coefs1;
00209             float *coefs, *exponents, mult;
00210             int i, n;
00211 
00212             coefs1 = s->coefs1[ch];
00213             exponents = s->exponents[ch];
00214             mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
00215             mult *= mdct_norm;
00216             coefs = src_coefs[ch];
00217             if (s->use_noise_coding && 0) {
00218                 assert(0); //FIXME not implemented
00219             } else {
00220                 coefs += s->coefs_start;
00221                 n = nb_coefs[ch];
00222                 for(i = 0;i < n; i++){
00223                     double t= *coefs++ / (exponents[i] * mult);
00224                     if(t<-32768 || t>32767)
00225                         return -1;
00226 
00227                     coefs1[i] = lrint(t);
00228                 }
00229             }
00230         }
00231     }
00232 
00233     v = 0;
00234     for(ch = 0; ch < s->nb_channels; ch++) {
00235         int a = s->channel_coded[ch];
00236         put_bits(&s->pb, 1, a);
00237         v |= a;
00238     }
00239 
00240     if (!v)
00241         return 1;
00242 
00243     for(v= total_gain-1; v>=127; v-= 127)
00244         put_bits(&s->pb, 7, 127);
00245     put_bits(&s->pb, 7, v);
00246 
00247     coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
00248 
00249     if (s->use_noise_coding) {
00250         for(ch = 0; ch < s->nb_channels; ch++) {
00251             if (s->channel_coded[ch]) {
00252                 int i, n;
00253                 n = s->exponent_high_sizes[bsize];
00254                 for(i=0;i<n;i++) {
00255                     put_bits(&s->pb, 1, s->high_band_coded[ch][i]= 0);
00256                     if (0)
00257                         nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
00258                 }
00259             }
00260         }
00261     }
00262 
00263     parse_exponents = 1;
00264     if (s->block_len_bits != s->frame_len_bits) {
00265         put_bits(&s->pb, 1, parse_exponents);
00266     }
00267 
00268     if (parse_exponents) {
00269         for(ch = 0; ch < s->nb_channels; ch++) {
00270             if (s->channel_coded[ch]) {
00271                 if (s->use_exp_vlc) {
00272                     encode_exp_vlc(s, ch, fixed_exp);
00273                 } else {
00274                     assert(0); //FIXME not implemented
00275 //                    encode_exp_lsp(s, ch);
00276                 }
00277             }
00278         }
00279     } else {
00280         assert(0); //FIXME not implemented
00281     }
00282 
00283     for(ch = 0; ch < s->nb_channels; ch++) {
00284         if (s->channel_coded[ch]) {
00285             int run, tindex;
00286             WMACoef *ptr, *eptr;
00287             tindex = (ch == 1 && s->ms_stereo);
00288             ptr = &s->coefs1[ch][0];
00289             eptr = ptr + nb_coefs[ch];
00290 
00291             run=0;
00292             for(;ptr < eptr; ptr++){
00293                 if(*ptr){
00294                     int level= *ptr;
00295                     int abs_level= FFABS(level);
00296                     int code= 0;
00297                     if(abs_level <= s->coef_vlcs[tindex]->max_level){
00298                         if(run < s->coef_vlcs[tindex]->levels[abs_level-1])
00299                             code= run + s->int_table[tindex][abs_level-1];
00300                     }
00301 
00302                     assert(code < s->coef_vlcs[tindex]->n);
00303                     put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code], s->coef_vlcs[tindex]->huffcodes[code]);
00304 
00305                     if(code == 0){
00306                         if(1<<coef_nb_bits <= abs_level)
00307                             return -1;
00308 
00309 
00310                         //Workaround minor rounding differences for the regression tests, FIXME we should find and replace the problematic float by fixpoint for reg tests
00311                         if(abs_level == 0x71B && (s->avctx->flags & CODEC_FLAG_BITEXACT)) abs_level=0x71A;
00312 
00313                         put_bits(&s->pb, coef_nb_bits, abs_level);
00314                         put_bits(&s->pb, s->frame_len_bits, run);
00315                     }
00316                     put_bits(&s->pb, 1, level < 0); //FIXME the sign is fliped somewhere
00317                     run=0;
00318                 }else{
00319                     run++;
00320                 }
00321             }
00322             if(run)
00323                 put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]);
00324         }
00325         if (s->version == 1 && s->nb_channels >= 2) {
00326             align_put_bits(&s->pb);
00327         }
00328     }
00329     return 0;
00330 }
00331 
00332 static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], uint8_t *buf, int buf_size, int total_gain){
00333     init_put_bits(&s->pb, buf, buf_size);
00334 
00335     if (s->use_bit_reservoir) {
00336         assert(0);//FIXME not implemented
00337     }else{
00338         if(encode_block(s, src_coefs, total_gain) < 0)
00339             return INT_MAX;
00340     }
00341 
00342     align_put_bits(&s->pb);
00343 
00344     return put_bits_count(&s->pb)/8 - s->block_align;
00345 }
00346 
00347 static int encode_superframe(AVCodecContext *avctx,
00348                             unsigned char *buf, int buf_size, void *data){
00349     WMACodecContext *s = avctx->priv_data;
00350     const short *samples = data;
00351     int i, total_gain;
00352 
00353     s->block_len_bits= s->frame_len_bits; //required by non variable block len
00354     s->block_len = 1 << s->block_len_bits;
00355 
00356     apply_window_and_mdct(avctx, samples, avctx->frame_size);
00357 
00358     if (s->ms_stereo) {
00359         float a, b;
00360         int i;
00361 
00362         for(i = 0; i < s->block_len; i++) {
00363             a = s->coefs[0][i]*0.5;
00364             b = s->coefs[1][i]*0.5;
00365             s->coefs[0][i] = a + b;
00366             s->coefs[1][i] = a - b;
00367         }
00368     }
00369 
00370     if (buf_size < 2 * MAX_CODED_SUPERFRAME_SIZE) {
00371         av_log(avctx, AV_LOG_ERROR, "output buffer size is too small\n");
00372         return AVERROR(EINVAL);
00373     }
00374 
00375 #if 1
00376     total_gain= 128;
00377     for(i=64; i; i>>=1){
00378         int error= encode_frame(s, s->coefs, buf, buf_size, total_gain-i);
00379         if(error<0)
00380             total_gain-= i;
00381     }
00382 #else
00383     total_gain= 90;
00384     best= encode_frame(s, s->coefs, buf, buf_size, total_gain);
00385     for(i=32; i; i>>=1){
00386         int scoreL= encode_frame(s, s->coefs, buf, buf_size, total_gain-i);
00387         int scoreR= encode_frame(s, s->coefs, buf, buf_size, total_gain+i);
00388         av_log(NULL, AV_LOG_ERROR, "%d %d %d (%d)\n", scoreL, best, scoreR, total_gain);
00389         if(scoreL < FFMIN(best, scoreR)){
00390             best = scoreL;
00391             total_gain -= i;
00392         }else if(scoreR < best){
00393             best = scoreR;
00394             total_gain += i;
00395         }
00396     }
00397 #endif
00398 
00399     encode_frame(s, s->coefs, buf, buf_size, total_gain);
00400     assert((put_bits_count(&s->pb) & 7) == 0);
00401     i= s->block_align - (put_bits_count(&s->pb)+7)/8;
00402     assert(i>=0);
00403     while(i--)
00404         put_bits(&s->pb, 8, 'N');
00405 
00406     flush_put_bits(&s->pb);
00407     return put_bits_ptr(&s->pb) - s->pb.buf;
00408 }
00409 
00410 AVCodec ff_wmav1_encoder =
00411 {
00412     "wmav1",
00413     AVMEDIA_TYPE_AUDIO,
00414     CODEC_ID_WMAV1,
00415     sizeof(WMACodecContext),
00416     encode_init,
00417     encode_superframe,
00418     ff_wma_end,
00419     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00420     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
00421 };
00422 
00423 AVCodec ff_wmav2_encoder =
00424 {
00425     "wmav2",
00426     AVMEDIA_TYPE_AUDIO,
00427     CODEC_ID_WMAV2,
00428     sizeof(WMACodecContext),
00429     encode_init,
00430     encode_superframe,
00431     ff_wma_end,
00432     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00433     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
00434 };

Generated on Wed Apr 11 2012 07:31:35 for FFmpeg by  doxygen 1.7.1