[Libav-user] Very slow H264 stream decoding

Дмитрий Бахтияров bakhtiyarov.dima at gmail.com
Thu Oct 3 22:16:31 CEST 2013


Hi, all!

I'm developing network application, which can play H264 video from RTSP
stream. I use live555 for RTSP/RTP processing, libx264 for encoding and
ffmpeg for decoding.
My problem is in the decoder working - I have video stream with 12-15 fps,
but avcodec_decode_video2 set got_image into not-null value very rarely -
average 1 time per second or less. In a GUI its looks like very long
freezes of interface, when one frame drawing on widgets few seconds.
 When I connect to server using vlc all work correct - I have video with
12-15 fps.

I have a such code for decoding:

AVCodec* m_decoder;
AVCodecContext* m_decoderContext;
AVFrame* m_picture;
AVPacket m_packet;

//initialisation of decoder
{
    avcodec_register_all();
    av_init_packet(&m_packet);
    m_decoder = avcodec_find_decoder(CODEC_ID_H264);
    if (!m_decoder)
    {
        QString str = QString("Can't find H264 decoder!");
        emit criticalError(str);
    }
    m_decoderContext = avcodec_alloc_context3(m_decoder);

    m_decoderContext->flags |= CODEC_FLAG_TRUNCATED;

    m_decoderContext->flags |= CODEC_FLAG_LOW_DELAY;
    //we can receive truncated frames
    m_decoderContext->flags2 |= CODEC_FLAG2_CHUNKS;

    AVDictionary* dictionary = nullptr;
    if (avcodec_open2(m_decoderContext, m_decoder, &dictionary) < 0)
    {
        QString str = QString("Failed to open decoder!");
        emit criticalError(str);
    }
    qDebug() << "H264 Decoder successfully opened";
    m_picture = avcodec_alloc_frame();
}



//decoding frame; Frame_t is equal to vector<unsigned char>
{
    Frame_t enc_frame;
    orig_frame >> enc_frame;
    m_packet.size = enc_frame.size();
    m_packet.data = enc_frame.data();

    qDebug() << "H264Decoder: received encoded frame with framesize " <<
enc_frame.size();

    while(m_packet.size > 0)
    {
        int got_picture;
        int len = avcodec_decode_video2(m_decoderContext, m_picture,
&got_picture, &m_packet);
        if (len < 0)
        {
            QString err("Decoding error");
            qDebug() << err;
            return false;
        }
        if (got_picture)
        {
            qDebug() << "H264Decoder: frame decoded!";
            //.......
        }
    }
}

On the server side I have this encoder settings:
{
    m_params = new x264_param_t;
    if (x264_param_default_preset(m_params, "ultrafast", "zerolatency") !=
0)
        qDebug() << "presetting default values for x264 parameters failed!";

    m_params->i_width = m_out_width;
    m_params->i_height = m_out_height;
    m_params->i_threads = 2;
    m_params->i_fps_num = m_fps;  //avg 12-15
    m_params->i_fps_den = 1;

    m_params->i_keyint_max = 24;
    m_params->b_intra_refresh = 1;

    m_params->rc.i_rc_method = X264_RC_CRF;
    m_params->rc.f_rf_constant = 25;
    m_params->rc.f_rf_constant_max = 35;

    m_params->b_repeat_headers = 1;
    m_params->b_annexb = 1;
    x264_param_apply_profile(m_params, "baseline");

    m_encoder = x264_encoder_open(m_params);
    if (m_encoder == nullptr)
    {
        qDebug() << "x264: encoder opening failed!";
        abort();
    }
    m_in_pic = new x264_picture_t;

    if (x264_picture_alloc( m_in_pic, X264_CSP_I420, 640, 480 ) != 0)
        qDebug() << "x264: Picture allocating failed!";

    x264_encoder_parameters(m_encoder, m_params);

    m_out_pic = new x264_picture_t;

    x264_nal_t* headers;
    int pi_nal;
    if (x264_encoder_headers(m_encoder, &headers, &pi_nal) < 0)
        qDebug() << "Error creating NAL headers!";
}


What is the reason of this delay and how I can fix it?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20131004/7555545d/attachment.html>


More information about the Libav-user mailing list