[libav-api] Segmentation Fault avcodec_send_frame()

Hadrien Marquez hadrienmarquez at gmail.com
Fri May 19 16:50:20 CEST 2017


Hello everyone,
I'm  a beginner starting to explore the libav librairies.
In order to understand how it works I've been trying to make a simple
transcoder which decode a video stream from my webcam and then re-encode it.
I'm currently using libavcodec 57. 96.101 / libavformat 57. 72.101 /
libavdevice 57.  7.100 . All taken from the git repository of FFmpeg.

I've successfully decoded the stream.
However when I try to use the avcodec_send_frame() function it results in a
segmentation fault.
I assume it's comming from a packet concept I haven't grasped yet or a bad
initialisation of my encoder.

Here is my encoding initialisation (skipping decoder, frame and packet
init):

...
    AVCodec *p_encoder(NULL);
    AVCodecContext *p_encoderContext(NULL);
...
    p_encoder = avcodec_find_encoder(AV_CODEC_ID_MPEG2VIDEO );
    if(!p_encoder)
        return -1;

    p_encoderContext = avcodec_alloc_context3(p_encoder);
    if(!p_encoderContext)
        return -1;

    p_encoderContext->bit_rate = 300000;
    p_encoderContext->width = p_decoderContext->width;
    p_encoderContext->height = p_decoderContext->height;
    p_encoderContext->time_base = (AVRational){1, 25};
    p_encoderContext->framerate = (AVRational){25, 1};
    p_encoderContext->gop_size = 10;
    p_encoderContext->max_b_frames = 1;
    p_encoderContext->pix_fmt = AV_PIX_FMT_YUV420P;
...
    if (avcodec_open2(p_encoderContext, p_encoder, NULL) < 0)
        return -1;

The loop where I read the frames :

    while(av_read_frame(p_formatContext, p_inPkt) >= 0)
    {
        if(p_inPkt->stream_index == numStreamVideo)
        {
            if (p_inPkt->size)
            {
                decode(p_decoderContext, p_frameCam, p_inPkt, isDecoded);
                av_packet_unref(p_inPkt);
            }
            cout << "Until here i'm good." << endl;
            av_packet_unref(p_outPkt);
            encode(p_encoderContext, p_frameCam, p_outPkt, nameFile);
        }

    }

My function encode() :

static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
string nameFile)
{
    int ret;
    ret = avcodec_send_frame(enc_ctx, frame);
    cout << ret << endl;
    if(ret == AVERROR(EAGAIN))
    {
        cout << "BANG" << endl;
    }
    else if (ret < 0)
    {
        cout << "Error sending frame to encoding\n";
        exit(1);
    }

    ret = avcodec_receive_packet(enc_ctx, pkt);
    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
    {
        cout << ".";
        return;
    }
    else if (ret < 0)
    {
        cout << "Error during encoding\n";
        exit(1);
    }
    SaveFrame(pkt, nameFile);
}

I've attached the source to this mail.

I hope you'll provide me some guidance to solve this segmentation fault
matter. Thank you beforehand and have a nice day!


More information about the libav-api mailing list