FFmpeg 无法解码 H264 流/帧数据 [英] FFmpeg can't decode H264 stream/frame data

查看:43
本文介绍了FFmpeg 无法解码 H264 流/帧数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我有机会使用两个通过 RTSP 流式传输 H264 的设备.我在尝试使用 FFmpeg 库解压缩这个流时遇到了一些问题.

Recently I had chance to work with two devices that are streaming the H264 through RTSP. And I've ran into some problem trying to decompress this stream using FFmpeg library.

每次调用avcodec_decode_video2"时 - FFmpeg 只是说:

Every time the "avcodec_decode_video2" is called - FFmpeg just says something like:

[h264 @ 00339220] 无帧!

[h264 @ 00339220] no frame!

我的原始 H264 流 I 帧数据以这样的方式开始:65 88 84 21 3F F8 F8 0D..."(据我了解,这个 0x65 表示它是一个 IDR 帧?)

My raw H264 stream I frame data starts like this: "65 88 84 21 3F F8 F8 0D..." (as far as I understand this 0x65 indicates that it's a IDR frame?)

一台设备的其他帧开头为:41 9A 22 07 F3 4E 48 CC...."

Other frames for one device starts like: "41 9A 22 07 F3 4E 48 CC...."

对于其他设备 - 像这样:61 9A 25 C1 1C 45 62 39 ...."

and for other device - like this: "61 9A 25 C1 1C 45 62 39...."

  • 我在这里遗漏了一些帧数据吗?
  • FFmpeg 是否需要设置一些额外的参数?

我期望帧数据的开头至少有00 00 00 01"字节......但这就是我所得到的......

I was expecting at least "00 00 00 01" bytes at the start for the frame data... but this is what I've got..

推荐答案

好的,已经成功了.

  • 我需要包含序列 (SPS) 和图片参数集(PPS) 在将帧发送到 FFmpeg 之前用于我的帧数据.
  • 我需要在 SPS 和 PPS 数据之后添加 4 个额外的字节00 00 00 01".

这是一张小图,显示了我的意思:

Here is a little picture showing what I mean:

字节65 88..."是我的原始帧数据开始的地方.

此 SPS 和 PPS 信息未包含在 RTP 数据包中.我正在使用 Live555 库进行 RTSP 流传输,因此我使用了子会话fmtp_spropparametersets"功能来获取我需要的内容.此信息是 Base64 编码的.(示例:类似Z0KAKNoC0EkQ,aM48gA==")请注意,有两个参数"SPS 和 PPS 以,"分隔,并且这些参数没有00 00 00 01"" 包括在内,因此您需要添加它们.

This SPS and PPS information was not included in RTP packet. I'm using Live555 library for RTSP streaming, so I've used subsessions "fmtp_spropparametersets" function to get what I need. This information was Base64 encoded. (Sample: Something like this "Z0KAKNoC0EkQ,aM48gA==") Note that there are two "parameters" SPS and PPS seperated by "," and those parameters doesn't have a "00 00 00 01" included, so you need to add them.

一些代码示例(我在这里使用 Qt 库):

Some code sample (I'm using Qt library here):

QByteArray        ba          = pSubSession->fmtp_spropparametersets();
QList<QByteArray> recordsList = ba.split(',');

for (int i = 0; i < recordsList.size(); i++)
{
   mExtraData.append(char(0x00));
   mExtraData.append(char(0x00));
   mExtraData.append(char(0x00));
   mExtraData.append(char(0x01));

   mExtraData += QByteArray::fromBase64(recordsList.at(i));
}

现在对于每一帧我都做这样的事情:

Now for every frame I do something like this:

QByteArray ba = QByteArray(4, 0); // Prepare the "00 00 00 01"
           ba[3] = 0x01;

mpTrackVideo->buffer.insert(0, mExtraData);
mpTrackVideo->buffer.insert(mExtraData.size(), ba);

一年前,我以为我的项目中集成了 H264 流支持,直到我有机会用其他一些设备对其进行测试...因此,您需要记住,有些设备可能会为每个 I 帧发送 SPS 和 PPS 数据……有些可能不会!

Year ago I thought I had H264 stream support integrated in my project till I've had chance to test it with some other devices... So you need to keep in mind that some devices might send SPS and PPS data for every I frame... and some might not!

这篇关于FFmpeg 无法解码 H264 流/帧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆