2

我有网络上的 H264 RTP 流,我需要对其进行解码。我使用 libx264 编码流和 ffmpeg 解码。当我使用 VLC 连接到服务器时,它可以正确播放视频而没有任何问题。但是当我使用我的应用程序连接到服务器时,我有很长一段时间,当从这个流中绘制视频的小部件只绘制一个图像时。

我检查了日志文件,发现 avcodec_decode_video2() 很少将 got_image 设置为 1!解码器每 1-2 秒给我一个新的解码帧平均值,但在服务器上我的编码器有 12-15 fps!

解码器延迟的原因是什么,我该如何解决?

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_LOW_DELAY;
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();
...
    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;
            //emit criticalError(err);
            return false;
        }
        if (got_picture)
        {
                //create from frame QImage and send it into GUI thread
            qDebug() << "H264Decoder: frame decoded!";

我尝试更改 m_decoderContext 的一些选项(即 thread_count),但这并没有改变任何东西。

4

0 回答 0