使用FFmpeg和分离的AVCodecContext解码H264 RTSP

6

我需要帮助解码视频的rtsp流。这些流来自AXIS IP摄像头,我使用ffmpeg库来处理。必须单独创建AVCodecContext,而不是从AVFormatContext->streams[...]->codec中获取。

因此,我创建了AVCodec、AVCodecContext并尝试初始化它们。

AVCodec *codec=avcodec_find_decoder(codec_id);
if(!codec)
{
    qDebug()<<"FFMPEG failed to create codec"<<codec_id;
    return false; //-->
}

AVCodecContext *context=avcodec_alloc_context3(codec);
if(!context)
{
    qDebug()<<"FFMPEG failed to allocate codec context";
    return false; //-->
}
avcodec_open2(context, codec, NULL);

然后在应用程序的主循环中,我获取帧数据并尝试进行解码:

_preallocatedFrame = avcodec_alloc_frame();
avcodec_decode_video2(_context, _preallocatedFrame, &got_picture, &_packet);

我在控制台中收到了很多消息:

[h264 @ 1f177720] decode_slice_header error
[h264 @ 1f177720] no frame!
[h264 @ 1f177720] non-existing PPS 0 referenced
[h264 @ 1f177720] decode_slice_header error
[h264 @ 1f177720] no frame!
[h264 @ 1f177720] non-existing PPS 0 referenced
[h264 @ 1f177720] decode_slice_header error
[h264 @ 1f177720] no frame!
[h264 @ 1f177720] non-existing PPS 0 referenced
[h264 @ 1f177720] decode_slice_header error
[h264 @ 1f177720] no frame!
[h264 @ 1f177720] non-existing PPS 0 referenced
[h264 @ 1f177720] decode_slice_header error
[h264 @ 1f177720] no frame!
[h264 @ 1f177720] non-existing PPS 0 referenced
[h264 @ 1f177720] decode_slice_header error
[h264 @ 1f177720] no frame!

你能否给我一些建议,如何正确初始化AVCodecContext或其他相关的操作?

1个回答

4

如果您想解码h.264流,需要进行一些额外的工作。您需要将“sps pps”数据传递给解码器。这些数据可以在rtp流本身中找到(见此)或者在SDP中的rtsp协商中找到。在成功将这些数据提供给解码器后,应该能够进行解码。


好的,但是有什么区别呢?当我从AVFormatContext->streams[...]->codec获取我的AVCodecContext并执行完全相似的操作时,它会无错误解码。但是我需要使用分离的codeccontext(当我不能拥有格式上下文时,可能需要在从归档中获取数据并保存在磁盘上后进行解码)。 - mmmaaak
是的,它们之间有20多个不同之处。我试图将所有不同的字段设置为我的上下文,除了AVCOdecContext->priv_data和其他指针字段-这并没有起作用。 - mmmaaak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接