I want to know what is the start code and how to find the i frame in the H 264 codec. Basically I'm trying to encry开发者_JAVA百科pt the video files using Java; how can I parse the video file to get the i frame using Java?
Based on the comments above here's a simple code snippet you can use:
ByteBuffer bb = ... // Byte buffer with your frame or h.264 NAL stream
int marker = 0xffffffff;
while (bb.hasRemaining()) {
int b = bb.get() & 0xff;
if (marker == 1) {
if ((b & 0x1f) == 5)
System.out.println("IDR slice!! " + (bb.position() - 1));
}
marker = (marker << 8) | b;
}
If you simply want to know whether a given frame is an IDR slice ( key frame ) or not you can use H264Utils.idrSlice from JCodec ( http://jcodec.org ):
ByteBuffer bb = ... // Byte buffer with your frame or h.264 NAL stream
if(H264Utils.idrSlice(bb))
System.out.println("IDR slice!!");
The reason you would want to use library is because IDR NAL unit slice is not the only slice carried in an encoded frame, so you will have to get through the sequence of SPS,PPS and SEI to finally get to your IDR slice.
the previous answer is referring to the nal_unit_type field in the nal_unit(). Detailed syntax for nal_unit is in section 7.3.1() of ISO 14496-10 .
One thing that is different between H.264 and MPEG 2 is that H.264 distinguishes between an I slice and an IDR. The I slice can itself be decoded into a complete picture, but it is possible for slices decoded afterward to refer to slices before the I. Section 3.62 states "After the decoding of an IDR picture all following coded pictures in decoding order can be decoded without inter predition from any picture decoded prior to the IDR picture". It's like an MPEG 2 closed GOP.
Returning to nal_unit_type, see table 7-1 which indicates that an IDR has nal_unit_type 5, but if you're looking for I slices, you can look at the primary_pic_type (table 7-5) in the access_unit_delimiter() (section 7.3.2.4), or you could check the slice_type (table 7-6) of the slice_header() (section 7.3.3)
If the stream is in Annex-B format, you may look for the "start code prefix" which equals to 0x000001, following by the NAL Unit. The NALU type information is provided in the first byte.
精彩评论