wmaenc.c 13.4 KB
Newer Older
Michael Niedermayer's avatar
Michael Niedermayer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * WMA compatible encoder
 * Copyright (c) 2007 Michael Niedermayer
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avcodec.h"
23
#include "internal.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
24
#include "wma.h"
25
#include "libavutil/avassert.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
26 27 28


static int encode_init(AVCodecContext * avctx){
29
    WMACodecContext *s = avctx->priv_data;
30
    int i, flags1, flags2, block_align;
Michael Niedermayer's avatar
Michael Niedermayer committed
31 32
    uint8_t *extradata;

33 34
    s->avctx = avctx;

35
    if(avctx->channels > MAX_CHANNELS) {
36
        av_log(avctx, AV_LOG_ERROR, "too many channels: got %i, need %i or fewer\n",
37 38 39 40
               avctx->channels, MAX_CHANNELS);
        return AVERROR(EINVAL);
    }

41
    if (avctx->sample_rate > 48000) {
42
        av_log(avctx, AV_LOG_ERROR, "sample rate is too high: %d > 48kHz\n",
43 44 45 46
               avctx->sample_rate);
        return AVERROR(EINVAL);
    }

47 48 49 50 51
    if(avctx->bit_rate < 24*1000) {
        av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\n",
               avctx->bit_rate);
        return AVERROR(EINVAL);
    }
52

Michael Niedermayer's avatar
Michael Niedermayer committed
53 54 55
    /* extract flag infos */
    flags1 = 0;
    flags2 = 1;
56
    if (avctx->codec->id == AV_CODEC_ID_WMAV1) {
Michael Niedermayer's avatar
Michael Niedermayer committed
57 58
        extradata= av_malloc(4);
        avctx->extradata_size= 4;
59 60
        AV_WL16(extradata, flags1);
        AV_WL16(extradata+2, flags2);
61
    } else if (avctx->codec->id == AV_CODEC_ID_WMAV2) {
Michael Niedermayer's avatar
Michael Niedermayer committed
62 63
        extradata= av_mallocz(10);
        avctx->extradata_size= 10;
64 65
        AV_WL32(extradata, flags1);
        AV_WL16(extradata+4, flags2);
Michael Niedermayer's avatar
Michael Niedermayer committed
66
    }else
67
        av_assert0(0);
Michael Niedermayer's avatar
Michael Niedermayer committed
68 69 70 71
    avctx->extradata= extradata;
    s->use_exp_vlc = flags2 & 0x0001;
    s->use_bit_reservoir = flags2 & 0x0002;
    s->use_variable_block_len = flags2 & 0x0004;
72 73
    if (avctx->channels == 2)
        s->ms_stereo = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
74 75 76 77 78

    ff_wma_init(avctx, flags2);

    /* init MDCT */
    for(i = 0; i < s->nb_block_sizes; i++)
79
        ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);
Michael Niedermayer's avatar
Michael Niedermayer committed
80

81
    block_align        = avctx->bit_rate * (int64_t)s->frame_len /
82
                         (avctx->sample_rate * 8);
83 84
    block_align        = FFMIN(block_align, MAX_CODED_SUPERFRAME_SIZE);
    avctx->block_align = block_align;
85

86 87 88 89 90 91
    avctx->frame_size = avctx->delay = s->frame_len;

#if FF_API_OLD_ENCODE_AUDIO
    avctx->coded_frame = &s->frame;
    avcodec_get_frame_defaults(avctx->coded_frame);
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
92 93 94 95 96

    return 0;
}


97 98
static void apply_window_and_mdct(AVCodecContext * avctx, const AVFrame *frame)
{
99
    WMACodecContext *s = avctx->priv_data;
100 101
    float **audio      = (float **)frame->extended_data;
    int len            = frame->nb_samples;
Michael Niedermayer's avatar
Michael Niedermayer committed
102
    int window_index= s->frame_len_bits - s->block_len_bits;
103
    FFTContext *mdct = &s->mdct_ctx[window_index];
104
    int ch;
Michael Niedermayer's avatar
Michael Niedermayer committed
105 106
    const float * win = s->windows[window_index];
    int window_len = 1 << s->block_len_bits;
107 108 109 110
    float n = 2.0 * 32768.0 / window_len;

    for (ch = 0; ch < avctx->channels; ch++) {
        memcpy(s->output, s->frame_out[ch], window_len * sizeof(*s->output));
111
        s->fdsp.vector_fmul_scalar(s->frame_out[ch], audio[ch], n, len);
112
        s->fdsp.vector_fmul_reverse(&s->output[window_len], s->frame_out[ch], win, len);
113 114
        s->fdsp.vector_fmul(s->frame_out[ch], s->frame_out[ch], win, len);
        mdct->mdct_calc(mdct, s->coefs[ch], s->output);
Michael Niedermayer's avatar
Michael Niedermayer committed
115 116 117 118
    }
}

//FIXME use for decoding too
Måns Rullgård's avatar
Måns Rullgård committed
119
static void init_exp(WMACodecContext *s, int ch, const int *exp_param){
Michael Niedermayer's avatar
Michael Niedermayer committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    int n;
    const uint16_t *ptr;
    float v, *q, max_scale, *q_end;

    ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
    q = s->exponents[ch];
    q_end = q + s->block_len;
    max_scale = 0;
    while (q < q_end) {
        /* XXX: use a table */
        v = pow(10, *exp_param++ * (1.0 / 16.0));
        max_scale= FFMAX(max_scale, v);
        n = *ptr++;
        do {
            *q++ = v;
        } while (--n);
    }
    s->max_exponent[ch] = max_scale;
}

140
static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param){
Michael Niedermayer's avatar
Michael Niedermayer committed
141 142 143 144 145 146 147 148 149
    int last_exp;
    const uint16_t *ptr;
    float *q, *q_end;

    ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
    q = s->exponents[ch];
    q_end = q + s->block_len;
    if (s->version == 1) {
        last_exp= *exp_param++;
150
        av_assert0(last_exp-10 >= 0 && last_exp-10 < 32);
Michael Niedermayer's avatar
Michael Niedermayer committed
151 152 153 154 155 156 157
        put_bits(&s->pb, 5, last_exp - 10);
        q+= *ptr++;
    }else
        last_exp = 36;
    while (q < q_end) {
        int exp = *exp_param++;
        int code = exp - last_exp + 60;
158
        av_assert1(code >= 0 && code < 120);
159
        put_bits(&s->pb, ff_aac_scalefactor_bits[code], ff_aac_scalefactor_code[code]);
Michael Niedermayer's avatar
Michael Niedermayer committed
160 161 162 163 164 165
        /* XXX: use a table */
        q+= *ptr++;
        last_exp= exp;
    }
}

166
static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], int total_gain){
Michael Niedermayer's avatar
Michael Niedermayer committed
167 168 169 170 171 172 173
    int v, bsize, ch, coef_nb_bits, parse_exponents;
    float mdct_norm;
    int nb_coefs[MAX_CHANNELS];
    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};

    //FIXME remove duplication relative to decoder
    if (s->use_variable_block_len) {
174
        av_assert0(0); //FIXME not implemented
Michael Niedermayer's avatar
Michael Niedermayer committed
175 176 177 178 179 180 181 182 183 184 185 186 187
    }else{
        /* fixed block len */
        s->next_block_len_bits = s->frame_len_bits;
        s->prev_block_len_bits = s->frame_len_bits;
        s->block_len_bits = s->frame_len_bits;
    }

    s->block_len = 1 << s->block_len_bits;
//     assert((s->block_pos + s->block_len) <= s->frame_len);
    bsize = s->frame_len_bits - s->block_len_bits;

    //FIXME factor
    v = s->coefs_end[bsize] - s->coefs_start;
188
    for (ch = 0; ch < s->avctx->channels; ch++)
Michael Niedermayer's avatar
Michael Niedermayer committed
189 190 191 192 193 194 195 196 197
        nb_coefs[ch] = v;
    {
        int n4 = s->block_len / 2;
        mdct_norm = 1.0 / (float)n4;
        if (s->version == 1) {
            mdct_norm *= sqrt(n4);
        }
    }

198
    if (s->avctx->channels == 2) {
199
        put_bits(&s->pb, 1, !!s->ms_stereo);
Michael Niedermayer's avatar
Michael Niedermayer committed
200 201
    }

202
    for (ch = 0; ch < s->avctx->channels; ch++) {
203 204
        s->channel_coded[ch] = 1; //FIXME only set channel_coded when needed, instead of always
        if (s->channel_coded[ch]) {
Michael Niedermayer's avatar
Michael Niedermayer committed
205 206 207 208
            init_exp(s, ch, fixed_exp);
        }
    }

209
    for (ch = 0; ch < s->avctx->channels; ch++) {
Michael Niedermayer's avatar
Michael Niedermayer committed
210
        if (s->channel_coded[ch]) {
211
            WMACoef *coefs1;
Michael Niedermayer's avatar
Michael Niedermayer committed
212 213 214 215 216 217 218 219 220
            float *coefs, *exponents, mult;
            int i, n;

            coefs1 = s->coefs1[ch];
            exponents = s->exponents[ch];
            mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
            mult *= mdct_norm;
            coefs = src_coefs[ch];
            if (s->use_noise_coding && 0) {
221
                av_assert0(0); //FIXME not implemented
Michael Niedermayer's avatar
Michael Niedermayer committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            } else {
                coefs += s->coefs_start;
                n = nb_coefs[ch];
                for(i = 0;i < n; i++){
                    double t= *coefs++ / (exponents[i] * mult);
                    if(t<-32768 || t>32767)
                        return -1;

                    coefs1[i] = lrint(t);
                }
            }
        }
    }

    v = 0;
237
    for (ch = 0; ch < s->avctx->channels; ch++) {
238
        int a = s->channel_coded[ch];
Michael Niedermayer's avatar
Michael Niedermayer committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252
        put_bits(&s->pb, 1, a);
        v |= a;
    }

    if (!v)
        return 1;

    for(v= total_gain-1; v>=127; v-= 127)
        put_bits(&s->pb, 7, 127);
    put_bits(&s->pb, 7, v);

    coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);

    if (s->use_noise_coding) {
253
        for (ch = 0; ch < s->avctx->channels; ch++) {
Michael Niedermayer's avatar
Michael Niedermayer committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
            if (s->channel_coded[ch]) {
                int i, n;
                n = s->exponent_high_sizes[bsize];
                for(i=0;i<n;i++) {
                    put_bits(&s->pb, 1, s->high_band_coded[ch][i]= 0);
                    if (0)
                        nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
                }
            }
        }
    }

    parse_exponents = 1;
    if (s->block_len_bits != s->frame_len_bits) {
        put_bits(&s->pb, 1, parse_exponents);
    }

    if (parse_exponents) {
272
        for (ch = 0; ch < s->avctx->channels; ch++) {
Michael Niedermayer's avatar
Michael Niedermayer committed
273 274 275 276
            if (s->channel_coded[ch]) {
                if (s->use_exp_vlc) {
                    encode_exp_vlc(s, ch, fixed_exp);
                } else {
277
                    av_assert0(0); //FIXME not implemented
Michael Niedermayer's avatar
Michael Niedermayer committed
278 279 280 281 282
//                    encode_exp_lsp(s, ch);
                }
            }
        }
    } else {
283
        av_assert0(0); //FIXME not implemented
Michael Niedermayer's avatar
Michael Niedermayer committed
284 285
    }

286
    for (ch = 0; ch < s->avctx->channels; ch++) {
Michael Niedermayer's avatar
Michael Niedermayer committed
287 288
        if (s->channel_coded[ch]) {
            int run, tindex;
289
            WMACoef *ptr, *eptr;
Michael Niedermayer's avatar
Michael Niedermayer committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            tindex = (ch == 1 && s->ms_stereo);
            ptr = &s->coefs1[ch][0];
            eptr = ptr + nb_coefs[ch];

            run=0;
            for(;ptr < eptr; ptr++){
                if(*ptr){
                    int level= *ptr;
                    int abs_level= FFABS(level);
                    int code= 0;
                    if(abs_level <= s->coef_vlcs[tindex]->max_level){
                        if(run < s->coef_vlcs[tindex]->levels[abs_level-1])
                            code= run + s->int_table[tindex][abs_level-1];
                    }

305
                    av_assert2(code < s->coef_vlcs[tindex]->n);
Michael Niedermayer's avatar
Michael Niedermayer committed
306 307 308 309 310 311 312 313 314
                    put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code], s->coef_vlcs[tindex]->huffcodes[code]);

                    if(code == 0){
                        if(1<<coef_nb_bits <= abs_level)
                            return -1;

                        put_bits(&s->pb, coef_nb_bits, abs_level);
                        put_bits(&s->pb, s->frame_len_bits, run);
                    }
315
                    put_bits(&s->pb, 1, level < 0); //FIXME the sign is fliped somewhere
Michael Niedermayer's avatar
Michael Niedermayer committed
316 317 318 319 320 321 322 323
                    run=0;
                }else{
                    run++;
                }
            }
            if(run)
                put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]);
        }
324
        if (s->version == 1 && s->avctx->channels >= 2) {
325
            avpriv_align_put_bits(&s->pb);
Michael Niedermayer's avatar
Michael Niedermayer committed
326 327 328 329 330
        }
    }
    return 0;
}

331
static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], uint8_t *buf, int buf_size, int total_gain){
Michael Niedermayer's avatar
Michael Niedermayer committed
332 333 334
    init_put_bits(&s->pb, buf, buf_size);

    if (s->use_bit_reservoir) {
335
        av_assert0(0);//FIXME not implemented
Michael Niedermayer's avatar
Michael Niedermayer committed
336 337 338 339 340
    }else{
        if(encode_block(s, src_coefs, total_gain) < 0)
            return INT_MAX;
    }

341
    avpriv_align_put_bits(&s->pb);
Michael Niedermayer's avatar
Michael Niedermayer committed
342

343
    return put_bits_count(&s->pb) / 8 - s->avctx->block_align;
Michael Niedermayer's avatar
Michael Niedermayer committed
344 345
}

346 347 348
static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
                             const AVFrame *frame, int *got_packet_ptr)
{
349
    WMACodecContext *s = avctx->priv_data;
350
    int i, total_gain, ret, error;
Michael Niedermayer's avatar
Michael Niedermayer committed
351 352 353 354

    s->block_len_bits= s->frame_len_bits; //required by non variable block len
    s->block_len = 1 << s->block_len_bits;

355
    apply_window_and_mdct(avctx, frame);
Michael Niedermayer's avatar
Michael Niedermayer committed
356 357 358 359 360 361 362 363 364 365 366 367 368

    if (s->ms_stereo) {
        float a, b;
        int i;

        for(i = 0; i < s->block_len; i++) {
            a = s->coefs[0][i]*0.5;
            b = s->coefs[1][i]*0.5;
            s->coefs[0][i] = a + b;
            s->coefs[1][i] = a - b;
        }
    }

369
    if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * MAX_CODED_SUPERFRAME_SIZE)))
370
        return ret;
371

Michael Niedermayer's avatar
Michael Niedermayer committed
372 373
    total_gain= 128;
    for(i=64; i; i>>=1){
374
        error = encode_frame(s, s->coefs, avpkt->data, avpkt->size,
375
                                 total_gain - i);
376
        if(error<=0)
Michael Niedermayer's avatar
Michael Niedermayer committed
377 378 379
            total_gain-= i;
    }

380 381
    while(total_gain <= 128 && error > 0)
        error = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain++);
382
    av_assert0((put_bits_count(&s->pb) & 7) == 0);
383
    i= avctx->block_align - (put_bits_count(&s->pb)+7)/8;
384
    av_assert0(i>=0);
385
    while(i--)
Michael Niedermayer's avatar
Michael Niedermayer committed
386 387 388
        put_bits(&s->pb, 8, 'N');

    flush_put_bits(&s->pb);
389
    av_assert0(put_bits_ptr(&s->pb) - s->pb.buf == avctx->block_align);
390 391 392 393

    if (frame->pts != AV_NOPTS_VALUE)
        avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);

394
    avpkt->size = avctx->block_align;
395 396
    *got_packet_ptr = 1;
    return 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
397 398
}

399
#if CONFIG_WMAV1_ENCODER
400 401 402
AVCodec ff_wmav1_encoder = {
    .name           = "wmav1",
    .type           = AVMEDIA_TYPE_AUDIO,
403
    .id             = AV_CODEC_ID_WMAV1,
404 405
    .priv_data_size = sizeof(WMACodecContext),
    .init           = encode_init,
406
    .encode2        = encode_superframe,
407
    .close          = ff_wma_end,
408
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
409
                                                     AV_SAMPLE_FMT_NONE },
410
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
Michael Niedermayer's avatar
Michael Niedermayer committed
411
};
412 413
#endif
#if CONFIG_WMAV2_ENCODER
414 415 416
AVCodec ff_wmav2_encoder = {
    .name           = "wmav2",
    .type           = AVMEDIA_TYPE_AUDIO,
417
    .id             = AV_CODEC_ID_WMAV2,
418 419
    .priv_data_size = sizeof(WMACodecContext),
    .init           = encode_init,
420
    .encode2        = encode_superframe,
421
    .close          = ff_wma_end,
422
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
423
                                                     AV_SAMPLE_FMT_NONE },
424
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
Michael Niedermayer's avatar
Michael Niedermayer committed
425
};
426
#endif