简体   繁体   English

FFmpeg无法解码H264流/帧数据

[英]FFmpeg can't decode H264 stream/frame data

Recently I had chance to work with two devices that are streaming the H264 through RTSP. 最近我有机会使用两台通过RTSP流式传输H264的设备。 And I've ran into some problem trying to decompress this stream using FFmpeg library. 我尝试使用FFmpeg库解压缩此流时遇到了一些问题。

Every time the " avcodec_decode_video2 " is called - FFmpeg just says something like: 每次调用“ avcodec_decode_video2 ”时 - FFmpeg只是说:

[h264 @ 00339220] no frame! [h264 @ 00339220]没有框架!

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?) 我的原始H264流I帧数据开始如下:“ 65 88 84 21 3F F8 F8 0D ...”(据我所知,这个0x65表示它是一个IDR帧?)

Other frames for one device starts like: " 41 9A 22 07 F3 4E 48 CC...." 一台设备的其他框架开始如下:“ 41 9A 22 07 F3 4E 48 CC ....”

and for other device - like this: " 61 9A 25 C1 1C 45 62 39...." 和其他设备 - 像这样:“ 61 9A 25 C1 1C 45 62 39 ....”

  • Am I missing some frame data here? 我在这里错过了一些帧数据吗?
  • Does FFmpeg needs to have some extra parameters set up? FFmpeg是否需要设置一些额外的参数?

I was expecting at least " 00 00 00 01 " bytes at the start for the frame data... but this is what I've got.. 我期待帧数据开始时至少有“ 00 00 00 01 ”字节......但这就是我所拥有的......

Ok, managed to make things working. 好的,设法使事情有效。

  • I needed to include the sequence (SPS) and picture parameter sets (PPS) for my frame data before sending frame to the FFmpeg. 在将帧发送到FFmpeg之前,我需要为帧数据包含序列(SPS)和图像参数集(PPS)。
  • I needed to add 4 extra bytes "00 00 00 01" after SPS and PPS data. 我需要在SPS和PPS数据之后添加4个额外字节“00 00 00 01”。

Here is a little picture showing what I mean: 这是一张显示我的意思的小图片: 在此输入图像描述

Bytes "65 88..." is where my original frame data begins. 字节“65 88 ...”是我的原始帧数据开始的地方。

This SPS and PPS information was not included in RTP packet. 此SPS和PPS信息未包含在RTP数据包中。 I'm using Live555 library for RTSP streaming, so I've used subsessions " fmtp_spropparametersets " function to get what I need. 我正在使用Live555库进行RTSP流式传输,因此我使用了子集“ fmtp_spropparametersets ”功能来获得我需要的东西。 This information was Base64 encoded. 此信息是Base64编码的。 (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. (示例: 像这样的东西“Z0KAKNoC0EkQ,aM48gA ==” )请注意,有两个“参数”SPS和PPS由“,”分隔,并且这些参数没有包含“00 00 00 01”,所以你需要添加它们。

Some code sample (I'm using Qt library here): 一些代码示例(我在这里使用Qt库):

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);

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! 一年前我以为我在我的项目中集成了H264流支持,直到我有机会用其他设备测试它...所以你需要记住,有些设备可能会为每个I帧发送SPS和PPS数据。 ..有些人可能不会!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM