View Full Version : h.264 nal unit
chia0176
11th January 2011, 16:22
Hi,
Is there a standard length field for NAL unit in h.264?
I would like to write a program to determine if a file type is h.264.
I understand that in a NAL unit, these three-byte sequences cannot occur at any byte-aligned position:
-0x000000
-0x000001
-0x000002
Hence, i would like to use this property to ascertain whether it is a h.264 file type.
But to use this property, i need to know the length field for NAL unit in H.264 for me to do a search.
I tried searching manually using a hex editor, but the byte difference in the two start prefix is always different.
Any help is appreciated. Thanks.
roozhou
11th January 2011, 17:06
No. Each NAL starts with 00 00 01 startcode and ends before next 00 00 01 startcode.
chia0176
12th January 2011, 08:24
i see. ok thanks. Is there a way for me to distinguish h.264 file types from other files? I wish to write a C++ program to distinguish them. Thanks.
kieranrk
12th January 2011, 08:50
i see. ok thanks. Is there a way for me to distinguish h.264 file types from other files? I wish to write a C++ program to distinguish them. Thanks.
See how GNU file does its parsing.
Shevach
12th January 2011, 12:26
I use the following algorithm for detection h264 stream:
Set nal_unit_cnt=0
labelA:
Find 0x000001 pattern (must be byte-aligned)
Read next byte nal_byte
Extract forbidden_zero_bit = nal_byte>>7
Extract nal_unit_type = nal_byte &0x1F
If forbidden_zero_bit==0 and nal_unit_type<=19
nal_unit_cnt ++
else
abort: this is not h264 stream
endif
if nal_unit_cnt < 20 (or other threshold)
goto labeA
endif
This is h264 stream - 20 start codes followed by forbidden_zero_bit flag and correct nal_unit_type
roozhou
12th January 2011, 15:25
Look at MediaInfo's H264 parser. I think it's enough for you.
chia0176
13th January 2011, 17:41
Thanks Kieranrk, Shevach and roozhou for your help. I am a beginner to h.264 material. So far i have not heard of GNU and mediainfo. I will do some research on it.
Shevach, thank you very much for your algorithm. i think that is what i am looking for. May i know where you got this info from? Is this something that you have analysed and created it?
nm
13th January 2011, 18:04
So far i have not heard of GNU
kieranrk meant the file command and its libmagic library: http://www.darwinsys.com/file/
But it doesn't seem to detect H.264 elementary streams.
Shevach
16th January 2011, 08:36
Thanks Kieranrk, Shevach and roozhou for your help. I am a beginner to h.264 material. So far i have not heard of GNU and mediainfo. I will do some research on it.
Shevach, thank you very much for your algorithm. i think that is what i am looking for. May i know where you got this info from? Is this something that you have analysed and created it?
In commercial decoders video data always arrives encapsulated in any container (e.g. TS where stream type is indicated). Therefore the issue how detect elementary stream type is not so popular.
Several years ago I implemented the above algorithm to determine whether the given stream is H.264 or not. The algorithm worked fine although it is not absolute reliable.
Trahald
16th January 2011, 19:43
<snip>I understand that in a NAL unit, these three-byte sequences cannot occur at any byte-aligned position:
-0x000000
-0x000001
-0x000002<snip>
FYI - That is referred to as emulation prevention. (A system used to avoid start codes accidentally appearing to be in the middle of a header by tossing in x03's before the last byte.) .. it also includes 0x000003. As you probably figured by now, looking for them wont help u.
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.