检测 RTP 流中的 MPEG4/H264 I-Frame (IDR) [英] Detect MPEG4/H264 I-Frame (IDR) in RTP stream
问题描述
我需要检测 RTP 数据包中的 MPEG4 I 帧.我知道如何删除 RTP 标头并在其中获取 MPEG4 帧,但我不知道如何识别 I 帧.
I need to detect MPEG4 I-Frame in RTP packet. I know how to remove RTP header and get the MPEG4 frame in it, but I can't figure out how to identify the I-Frame.
它有特定的签名/标题吗?
Does it have a specific signature/header?
推荐答案
好的,所以我想出了 h264 流.
Ok so I figured it out for h264 stream.
如何检测 I 帧:
- 删除 RTP 标头
- 检查 h264 有效载荷中第一个字节的值
- 如果值为 124 (0x7C),则为 I 帧
我无法弄清楚 MPEG4-ES 流...有什么建议吗?
I cant figure it out for the MPEG4-ES stream... any suggestions?
H264 IDR
这适用于我的 h264 流(fmtp:96 packetization-mode=1; profile-level-id=420029;
).您只需传递表示通过 RTP 接收的 h264 片段的字节数组.如果您想传递整个 RTP,只需更正 RTPHeaderBytes
值以跳过 RTP 标头.我总是得到 I-Frame,因为它是唯一可以分段的帧,请参阅 here.我在我的服务器中使用了这段(简化的)代码,它就像一个魅力!!!!如果 I 帧 (IDR) 未分段,fragment_type
将为 5,因此此代码将为分段和未分段 IDR 返回 true
.
This works for my h264 stream (fmtp:96 packetization-mode=1; profile-level-id=420029;
). You just pass byte array that represents the h264 fragment received through RTP. If you want to pass whole RTP, just correct the RTPHeaderBytes
value to skip RTP header. I always get the I-Frame, because it is the only frame that can be fragmented, see here. I use this (simplified) piece of code in my server, and it works like a charm!!!! If the I-Frame (IDR) is not fragmented, the fragment_type
would be 5, so this code would return true
for the fragmented and not fragmented IDRs.
public static bool isH264iFrame(byte[] paket)
{
int RTPHeaderBytes = 0;
int fragment_type = paket[RTPHeaderBytes + 0] & 0x1F;
int nal_type = paket[RTPHeaderBytes + 1] & 0x1F;
int start_bit = paket[RTPHeaderBytes + 1] & 0x80;
if (((fragment_type == 28 || fragment_type == 29) && nal_type == 5 && start_bit == 128) || fragment_type == 5)
{
return true;
}
return false;
}
这是 NAL 单元类型的表格:
Here's the table of NAL unit types:
Type Name
0 [unspecified]
1 Coded slice
2 Data Partition A
3 Data Partition B
4 Data Partition C
5 IDR (Instantaneous Decoding Refresh) Picture
6 SEI (Supplemental Enhancement Information)
7 SPS (Sequence Parameter Set)
8 PPS (Picture Parameter Set)
9 Access Unit Delimiter
10 EoS (End of Sequence)
11 EoS (End of Stream)
12 Filter Data
13-23 [extended]
24-31 [unspecified]
编辑 2:MPEG4 I-VOP
我忘记更新了...感谢 Che 和 ISO IEC 14496-2 文档,我设法解决了这个问题!Che 很有仪式感,但他的回答不是那么精确......所以这里是如何找到 I、P 和 B 帧(I-VOP、P-VOP、B-VOP)的简而言之:
I forgot to update this... Thanx to Che and ISO IEC 14496-2 document, I managed to work this out! Che was rite, but not so precise in his answer... so here is how to find I, P and B frames (I-VOP, P-VOP, B-VOP) in short:
- VOP(视频对象平面 -- 帧)以代码
000001B6
(十六进制)开头.所有 MPEG4 帧 (I,P,B) 都是一样的 接下来是更多信息,我不打算在这里描述(参见 IEC 文档),但我们只(如 che 所说)需要来自以下字节的高 2 位(在值为
B6
的字节).这 2 位告诉你 VOP_CODING_TYPE,见表格:
- VOP (Video Object Plane -- frame) starts with a code
000001B6
(hex). It is the same for all MPEG4 frames (I,P,B) Next follows many more info, that I am not going to describe here (see the IEC doc), but we only (as che said) need the higher 2 bits from the following byte (next two bits after the byte with the value
B6
). Those 2 bits tell you the VOP_CODING_TYPE, see the table:
VOP_CODING_TYPE (binary) Coding method
00 intra-coded (I)
01 predictive-coded (P)
10 bidirectionally-predictive-coded (B)
11 sprite (S)
因此,要找到 I-Frame,请找到以四个字节 000001B6
开头并具有下一个字节 00
的高两位的数据包.这将在 MPEG4 流中找到具有简单视频对象类型的 I 帧(不确定高级简单).
So, to find I-Frame find the packet starting with four bytes 000001B6
and having the higher two bits of the next byte 00
. This will find I frame in MPEG4 stream with a simple video object type (not sure for advanced simple).
如有其他问题,您可以查看提供的文档(ISO IEC 14496-2),您想了解的关于 MPEG4 都有.:)
For any other problems, you can check the document provided (ISO IEC 14496-2), there is all you want to know about MPEG4. :)
这篇关于检测 RTP 流中的 MPEG4/H264 I-Frame (IDR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!