[FFmpeg-user] Audio encode: [mp2 @ 0x29c52410] Size 1792 invalid

Unga unga888 at yahoo.com
Tue Jun 19 18:20:54 CEST 2012

Hi all

I'm trying out the audio video encoding example provided with the ffmpeg with avcodec_encode_audio2().

I get following error when I try to encode audio:
[mp2 @ 0x29c52410] Size 1792 invalid

Error with the other messages:
Audio Time base: 0.000023
Audio frame_size: 1152
Audio data_size: 4608
[mp2 @ 0x29c52410] Size 1792 invalid
Encode frame failed -22

Appreciate very much if somebody could help me to get the audio encoding to work. If mp2 is old, I would like to try the vorbis.

My encoding function is basically as follows:

static void audio_encode_example(const char *filename)
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int i, j;
    FILE *f;
    short *samples;
    float t, tincr;
    uint8_t *outbuf;
    AVFrame *frame;
    AVPacket outPkt;
    int ret, got_packet = 0;
    double time_base;
    int64_t pts;
    int data_size = 0;

    codec = avcodec_find_encoder(CODEC_ID_MP2);
    c = avcodec_alloc_context3(codec);

    c->bit_rate = 64000;
    c->sample_rate = 44100;
    c->channels = 2;
    c->sample_fmt = AV_SAMPLE_FMT_S16;

    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "could not open codec\n");

    frame= avcodec_alloc_frame();

    frame->nb_samples = c->frame_size;    

    data_size = av_samples_get_buffer_size(NULL, c->channels, frame->nb_samples, c->sample_fmt, 1);
    samples = malloc(data_size);

    // Fill samples here

    outPkt.data = NULL;                
    avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
                             (uint8_t *)samples,
                             data_size, 1);
    if ((ret = avcodec_encode_audio2(c, &outPkt, frame, &got_packet)) < 0)
        fprintf(stderr, "Encode frame failed %d\n", ret);


Many thanks in advance.

Best regards

More information about the ffmpeg-user mailing list