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