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         return -1;
00038 
00039     if(avctx->bit_rate < 24*1000)
00040         return -1;
00041 
00042     /* extract flag infos */
00043     flags1 = 0;
00044     flags2 = 1;
00045     if (avctx->codec->id == CODEC_ID_WMAV1) {
00046         extradata= av_malloc(4);
00047         avctx->extradata_size= 4;
00048         AV_WL16(extradata, flags1);
00049         AV_WL16(extradata+2, flags2);
00050     } else if (avctx->codec->id == CODEC_ID_WMAV2) {
00051         extradata= av_mallocz(10);
00052         avctx->extradata_size= 10;
00053         AV_WL32(extradata, flags1);
00054         AV_WL16(extradata+4, flags2);
00055     }else
00056         assert(0);
00057     avctx->extradata= extradata;
00058     s->use_exp_vlc = flags2 & 0x0001;
00059     s->use_bit_reservoir = flags2 & 0x0002;
00060     s->use_variable_block_len = flags2 & 0x0004;
00061 
00062     ff_wma_init(avctx, flags2);
00063 
00064     /* init MDCT */
00065     for(i = 0; i < s->nb_block_sizes; i++)
00066         ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0);
00067 
00068     avctx->block_align=
00069     s->block_align= avctx->bit_rate*(int64_t)s->frame_len / (avctx->sample_rate*8);
00070 //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d\n", s->block_align, avctx->bit_rate, s->frame_len, avctx->sample_rate);
00071     avctx->frame_size= s->frame_len;
00072 
00073     return 0;
00074 }
00075 
00076 
00077 static void apply_window_and_mdct(AVCodecContext * avctx, signed short * audio, int len) {
00078     WMACodecContext *s = avctx->priv_data;
00079     int window_index= s->frame_len_bits - s->block_len_bits;
00080     int i, j, channel;
00081     const float * win = s->windows[window_index];
00082     int window_len = 1 << s->block_len_bits;
00083     float n = window_len/2;
00084 
00085     for (channel = 0; channel < avctx->channels; channel++) {
00086         memcpy(s->output, s->frame_out[channel], sizeof(float)*window_len);
00087         j = channel;
00088         for (i = 0; i < len; i++, j += avctx->channels){
00089             s->output[i+window_len]  = audio[j] / n * win[window_len - i - 1];
00090             s->frame_out[channel][i] = audio[j] / n * win[i];
00091         }
00092         ff_mdct_calc(&s->mdct_ctx[window_index], s->coefs[channel], s->output, s->mdct_tmp);
00093     }
00094 }
00095 
00096 //FIXME use for decoding too
00097 static void init_exp(WMACodecContext *s, int ch, const int *exp_param){
00098     int n;
00099     const uint16_t *ptr;
00100     float v, *q, max_scale, *q_end;
00101 
00102     ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
00103     q = s->exponents[ch];
00104     q_end = q + s->block_len;
00105     max_scale = 0;
00106     while (q < q_end) {
00107         /* XXX: use a table */
00108         v = pow(10, *exp_param++ * (1.0 / 16.0));
00109         max_scale= FFMAX(max_scale, v);
00110         n = *ptr++;
00111         do {
00112             *q++ = v;
00113         } while (--n);
00114     }
00115     s->max_exponent[ch] = max_scale;
00116 }
00117 
00118 static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param){
00119     int last_exp;
00120     const uint16_t *ptr;
00121     float *q, *q_end;
00122 
00123     ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
00124     q = s->exponents[ch];
00125     q_end = q + s->block_len;
00126     if (s->version == 1) {
00127         last_exp= *exp_param++;
00128         assert(last_exp-10 >= 0 && last_exp-10 < 32);
00129         put_bits(&s->pb, 5, last_exp - 10);
00130         q+= *ptr++;
00131     }else
00132         last_exp = 36;
00133     while (q < q_end) {
00134         int exp = *exp_param++;
00135         int code = exp - last_exp + 60;
00136         assert(code >= 0 && code < 120);
00137         put_bits(&s->pb, ff_wma_scale_huffbits[code], ff_wma_scale_huffcodes[code]);
00138         /* XXX: use a table */
00139         q+= *ptr++;
00140         last_exp= exp;
00141     }
00142 }
00143 
00144 static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], int total_gain){
00145     int v, bsize, ch, coef_nb_bits, parse_exponents;
00146     float mdct_norm;
00147     int nb_coefs[MAX_CHANNELS];
00148     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};
00149 
00150     //FIXME remove duplication relative to decoder
00151     if (s->use_variable_block_len) {
00152         assert(0); //FIXME not implemented
00153     }else{
00154         /* fixed block len */
00155         s->next_block_len_bits = s->frame_len_bits;
00156         s->prev_block_len_bits = s->frame_len_bits;
00157         s->block_len_bits = s->frame_len_bits;
00158     }
00159 
00160     s->block_len = 1 << s->block_len_bits;
00161 //     assert((s->block_pos + s->block_len) <= s->frame_len);
00162     bsize = s->frame_len_bits - s->block_len_bits;
00163 
00164     //FIXME factor
00165     v = s->coefs_end[bsize] - s->coefs_start;
00166     for(ch = 0; ch < s->nb_channels; ch++)
00167         nb_coefs[ch] = v;
00168     {
00169         int n4 = s->block_len / 2;
00170         mdct_norm = 1.0 / (float)n4;
00171         if (s->version == 1) {
00172             mdct_norm *= sqrt(n4);
00173         }
00174     }
00175 
00176     if (s->nb_channels == 2) {
00177         put_bits(&s->pb, 1, s->ms_stereo= 1);
00178     }
00179 
00180     for(ch = 0; ch < s->nb_channels; ch++) {
00181         if ((s->channel_coded[ch]= 1)) { //FIXME only set channel_coded when needed, instead of always
00182             init_exp(s, ch, fixed_exp);
00183         }
00184     }
00185 
00186     for(ch = 0; ch < s->nb_channels; ch++) {
00187         if (s->channel_coded[ch]) {
00188             int16_t *coefs1;
00189             float *coefs, *exponents, mult;
00190             int i, n;
00191 
00192             coefs1 = s->coefs1[ch];
00193             exponents = s->exponents[ch];
00194             mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
00195             mult *= mdct_norm;
00196             coefs = src_coefs[ch];
00197             if (s->use_noise_coding && 0) {
00198                 assert(0); //FIXME not implemented
00199             } else {
00200                 coefs += s->coefs_start;
00201                 n = nb_coefs[ch];
00202                 for(i = 0;i < n; i++){
00203                     double t= *coefs++ / (exponents[i] * mult);
00204                     if(t<-32768 || t>32767)
00205                         return -1;
00206 
00207                     coefs1[i] = lrint(t);
00208                 }
00209             }
00210         }
00211     }
00212 
00213     v = 0;
00214     for(ch = 0; ch < s->nb_channels; ch++) {
00215         int a = s->channel_coded[ch];
00216         put_bits(&s->pb, 1, a);
00217         v |= a;
00218     }
00219 
00220     if (!v)
00221         return 1;
00222 
00223     for(v= total_gain-1; v>=127; v-= 127)
00224         put_bits(&s->pb, 7, 127);
00225     put_bits(&s->pb, 7, v);
00226 
00227     coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
00228 
00229     if (s->use_noise_coding) {
00230         for(ch = 0; ch < s->nb_channels; ch++) {
00231             if (s->channel_coded[ch]) {
00232                 int i, n;
00233                 n = s->exponent_high_sizes[bsize];
00234                 for(i=0;i<n;i++) {
00235                     put_bits(&s->pb, 1, s->high_band_coded[ch][i]= 0);
00236                     if (0)
00237                         nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
00238                 }
00239             }
00240         }
00241     }
00242 
00243     parse_exponents = 1;
00244     if (s->block_len_bits != s->frame_len_bits) {
00245         put_bits(&s->pb, 1, parse_exponents);
00246     }
00247 
00248     if (parse_exponents) {
00249         for(ch = 0; ch < s->nb_channels; ch++) {
00250             if (s->channel_coded[ch]) {
00251                 if (s->use_exp_vlc) {
00252                     encode_exp_vlc(s, ch, fixed_exp);
00253                 } else {
00254                     assert(0); //FIXME not implemented
00255 //                    encode_exp_lsp(s, ch);
00256                 }
00257             }
00258         }
00259     } else {
00260         assert(0); //FIXME not implemented
00261     }
00262 
00263     for(ch = 0; ch < s->nb_channels; ch++) {
00264         if (s->channel_coded[ch]) {
00265             int run, tindex;
00266             int16_t *ptr, *eptr;
00267             tindex = (ch == 1 && s->ms_stereo);
00268             ptr = &s->coefs1[ch][0];
00269             eptr = ptr + nb_coefs[ch];
00270 
00271             run=0;
00272             for(;ptr < eptr; ptr++){
00273                 if(*ptr){
00274                     int level= *ptr;
00275                     int abs_level= FFABS(level);
00276                     int code= 0;
00277                     if(abs_level <= s->coef_vlcs[tindex]->max_level){
00278                         if(run < s->coef_vlcs[tindex]->levels[abs_level-1])
00279                             code= run + s->int_table[tindex][abs_level-1];
00280                     }
00281 
00282                     assert(code < s->coef_vlcs[tindex]->n);
00283                     put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code], s->coef_vlcs[tindex]->huffcodes[code]);
00284 
00285                     if(code == 0){
00286                         if(1<<coef_nb_bits <= abs_level)
00287                             return -1;
00288 
00289                         put_bits(&s->pb, coef_nb_bits, abs_level);
00290                         put_bits(&s->pb, s->frame_len_bits, run);
00291                     }
00292                     put_bits(&s->pb, 1, level < 0); //FIXME the sign is fliped somewhere
00293                     run=0;
00294                 }else{
00295                     run++;
00296                 }
00297             }
00298             if(run)
00299                 put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]);
00300         }
00301         if (s->version == 1 && s->nb_channels >= 2) {
00302             align_put_bits(&s->pb);
00303         }
00304     }
00305     return 0;
00306 }
00307 
00308 static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], uint8_t *buf, int buf_size, int total_gain){
00309     init_put_bits(&s->pb, buf, buf_size);
00310 
00311     if (s->use_bit_reservoir) {
00312         assert(0);//FIXME not implemented
00313     }else{
00314         if(encode_block(s, src_coefs, total_gain) < 0)
00315             return INT_MAX;
00316     }
00317 
00318     align_put_bits(&s->pb);
00319 
00320     return put_bits_count(&s->pb)/8 - s->block_align;
00321 }
00322 
00323 static int encode_superframe(AVCodecContext *avctx,
00324                             unsigned char *buf, int buf_size, void *data){
00325     WMACodecContext *s = avctx->priv_data;
00326     short *samples = data;
00327     int i, total_gain;
00328 
00329     s->block_len_bits= s->frame_len_bits; //required by non variable block len
00330     s->block_len = 1 << s->block_len_bits;
00331 
00332     apply_window_and_mdct(avctx, samples, avctx->frame_size);
00333 
00334     if (s->ms_stereo) {
00335         float a, b;
00336         int i;
00337 
00338         for(i = 0; i < s->block_len; i++) {
00339             a = s->coefs[0][i]*0.5;
00340             b = s->coefs[1][i]*0.5;
00341             s->coefs[0][i] = a + b;
00342             s->coefs[1][i] = a - b;
00343         }
00344     }
00345 
00346 #if 1
00347     total_gain= 128;
00348     for(i=64; i; i>>=1){
00349         int error= encode_frame(s, s->coefs, buf, buf_size, total_gain-i);
00350         if(error<0)
00351             total_gain-= i;
00352     }
00353 #else
00354     total_gain= 90;
00355     best= encode_frame(s, s->coefs, buf, buf_size, total_gain);
00356     for(i=32; i; i>>=1){
00357         int scoreL= encode_frame(s, s->coefs, buf, buf_size, total_gain-i);
00358         int scoreR= encode_frame(s, s->coefs, buf, buf_size, total_gain+i);
00359         av_log(NULL, AV_LOG_ERROR, "%d %d %d (%d)\n", scoreL, best, scoreR, total_gain);
00360         if(scoreL < FFMIN(best, scoreR)){
00361             best = scoreL;
00362             total_gain -= i;
00363         }else if(scoreR < best){
00364             best = scoreR;
00365             total_gain += i;
00366         }
00367     }
00368 #endif
00369 
00370     encode_frame(s, s->coefs, buf, buf_size, total_gain);
00371     assert((put_bits_count(&s->pb) & 7) == 0);
00372     i= s->block_align - (put_bits_count(&s->pb)+7)/8;
00373     assert(i>=0);
00374     while(i--)
00375         put_bits(&s->pb, 8, 'N');
00376 
00377     flush_put_bits(&s->pb);
00378     return pbBufPtr(&s->pb) - s->pb.buf;
00379 }
00380 
00381 AVCodec wmav1_encoder =
00382 {
00383     "wmav1",
00384     CODEC_TYPE_AUDIO,
00385     CODEC_ID_WMAV1,
00386     sizeof(WMACodecContext),
00387     encode_init,
00388     encode_superframe,
00389     ff_wma_end,
00390     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
00391 };
00392 
00393 AVCodec wmav2_encoder =
00394 {
00395     "wmav2",
00396     CODEC_TYPE_AUDIO,
00397     CODEC_ID_WMAV2,
00398     sizeof(WMACodecContext),
00399     encode_init,
00400     encode_superframe,
00401     ff_wma_end,
00402     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
00403 };

Generated on Thu Dec 4 09:45:18 2008 for libextractor by  doxygen 1.5.1