[Libav-user] avcodec_open2 returns -22

Talgorn François-Xavier fxtalgorn at yahoo.fr
Thu Jun 18 17:17:06 CEST 2015


Hi Kevin,

I don't know about a general reference for codec params but
here is how it is done for MP2 encoder in the FFMPEG decoding_encoding.c example.
They generate a tone and encode it in mp2.
Maybe it could be a starting point.
All I've done with libav has been from examples. There is very little to no documentation online.
Good luck.

____________________________________________
static void audio_encode_example(const char *filename)
{
    AVCodec *codec;
    AVCodecContext *c= NULL;
    AVFrame *frame;
    AVPacket pkt;
    int i, j, k, ret, got_output;
    int buffer_size;
    FILE *f;
    uint16_t *samples;
    float t, tincr;

    printf("Encode audio file %s\n", filename);

    /* find the MP2 encoder */
    codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }

    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate audio codec context\n");
        exit(1);
    }

    /* put sample parameters */
    c->bit_rate = 64000;

    /* check that the encoder supports s16 pcm input */
    c->sample_fmt = AV_SAMPLE_FMT_S16;
    if (!check_sample_fmt(codec, c->sample_fmt)) {
        fprintf(stderr, "Encoder does not support sample format %s",
                av_get_sample_fmt_name(c->sample_fmt));
        exit(1);
    }

    /* select other audio parameters supported by the encoder */
    c->sample_rate    = select_sample_rate(codec);
    c->channel_layout = select_channel_layout(codec);
    c->channels       = av_get_channel_layout_nb_channels(c->channel_layout);

    /* open it */
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }

    f = fopen(filename, "wb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }

    /* frame containing input raw audio */
    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate audio frame\n");
        exit(1);
    }

    frame->nb_samples     = c->frame_size;
    frame->format         = c->sample_fmt;
    frame->channel_layout = c->channel_layout;

    /* the codec gives us the frame size, in samples,
     * we calculate the size of the samples buffer in bytes */
    buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
                                             c->sample_fmt, 0);
    if (buffer_size < 0) {
        fprintf(stderr, "Could not get sample buffer size\n");
        exit(1);
    }
    samples = av_malloc(buffer_size);
    if (!samples) {
        fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
                buffer_size);
        exit(1);
    }
    /* setup the data pointers in the AVFrame */
    ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
                                   (const uint8_t*)samples, buffer_size, 0);
    if (ret < 0) {
        fprintf(stderr, "Could not setup audio frame\n");
        exit(1);
    }

    /* encode a single tone sound */
    t = 0;
    tincr = 2 * M_PI * 440.0 / c->sample_rate;
    for (i = 0; i < 200; i++) {
        av_init_packet(&pkt);
        pkt.data = NULL; // packet data will be allocated by the encoder
        pkt.size = 0;

        for (j = 0; j < c->frame_size; j++) {
            samples[2*j] = (int)(sin(t) * 10000);

            for (k = 1; k < c->channels; k++)
                samples[2*j + k] = samples[2*j];
            t += tincr;
        }
        /* encode the samples */
        ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding audio frame\n");
            exit(1);
        }
        if (got_output) {
            fwrite(pkt.data, 1, pkt.size, f);
            av_free_packet(&pkt);
        }
    }

    /* get the delayed frames */
    for (got_output = 1; got_output; i++) {
        ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding frame\n");
            exit(1);
        }

        if (got_output) {
            fwrite(pkt.data, 1, pkt.size, f);
            av_free_packet(&pkt);
        }
    }
    fclose(f);

    av_freep(&samples);
    av_frame_free(&frame);
    avcodec_close(c);
    av_free(c);
}

Le 18 juin 2015 à 16:04, Kevin J. Brooks <kbrooks at r2c-ss.com> a écrit :

> Can someone point me to a reference where I can learn how to set up the codec parameters?  Even if it is only setting up for WMAV2, but I do need to record both audio and video.
> 
> On 6/17/2015 5:53 PM, Kevin J. Brooks wrote:
>> I changed the Audio codec to WMAV2, and left the rest of the AudioCodec settings the same Now the program crashes at this line in the QtMel code:
>> 
>> int outSize = avcodec_encode_audio(m_audioStream->codec, m_audioOutputBuffer, m_audioOutputBufferSize, (short *)samples.data());
>> 
>> In debug the error staates "Stopped in thread 2 by:Exception at 0x66364c62, cod:0xc0000005: write access violation at:0x1, flags=0x0 (first chance).
>> 
>> I suspect I don't have the audio settings set write, but I am not sure how to set them for WMAV2.
>> 
>> 
>> 
>> On 6/17/2015 4:30 PM, Kevin J. Brooks wrote:
>>> I am setting the AudioCodec to MP3,  I am using the pre-built libraries the code I have the causes the issue is from the code for the QtMEL library.  What I am posting here is the Audio Codec setup for the QtMel object.  I hope it helps.
>>> 
>>> BTW I am using H264 for the Video Codec.  If I record video only, it does work, it is only if I try to add sound that it fails.
>>> 
>>> CR2CAudioFormat format;
>>>     format.setChannelCount(2);
>>>     format.setSampleRate(44100);
>>>     format.setFormat(CR2CAudioFormat::SignedInt16);
>>> 
>>> I really don't have a preference for the codec, I just need to know how to set it up properly. I am totally new at recording video and audio.
>>> 
>>> On 6/17/2015 1:08 AM, Taha Ansari wrote:
>>>> On Wed, Jun 17, 2015 at 2:17 AM, Gonzalo Garramuno <ggarra13 at gmail.com> wrote:
>>>> On 16/06/15 09:06, Kevin J. Brooks wrote:
>>>> Yes I am.  For more information, I am developing on Windoze 7.
>>>> avcodec_open2 can fail if you lack the library for the codec you want to decode or encode.  For example, libx264.  The libraries in ffmpeg.zeranoe.com contain all codecs, but they are GPL only.  You should try them first and see if the problem goes away.  If it does, you know you have to compile ffmpeg with different flags.
>>>> 
>>>> Since he mentions Windows 7, I assume he is using pre-built packages from zeranoe site (as you mention). These builds contain proprietary codec(s) like x264 (non GPL).
>>>> 
>>>> @Kevin: maybe you have some bare minimum code with you that can produce this error, which people can have a look? 
>>>> 
>>>> 
>>>> _______________________________________________
>>>> Libav-user mailing list
>>>> Libav-user at ffmpeg.org
>>>> http://ffmpeg.org/mailman/listinfo/libav-user
>>> 
>>> 
>>> 
>>> _______________________________________________
>>> Libav-user mailing list
>>> Libav-user at ffmpeg.org
>>> http://ffmpeg.org/mailman/listinfo/libav-user
>> 
>> 
>> 
>> _______________________________________________
>> Libav-user mailing list
>> Libav-user at ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/libav-user
> 
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20150618/321c528f/attachment.html>


More information about the Libav-user mailing list