H264 detects whether frame I or frame P solves the mosaic problem in the first frame of the video.

Source: Internet
Author: User

Recently, we used h264 code stream data for video recording, but the first frame was mosaic. The reason is that the first frame of the video is not a key frame, therefore, video recording is required to determine whether the first frame is a key frame. There are two methods: the first is to search based on the original code stream, and the second is to pass the original code to FFMPEG for FFMPEG to judge the key_frame, the second method is relatively simple, but because this method is too close to the video decoding, It is troublesome to modify the video later, so select the first method, view Note 1 to understand how to detect the key frames of the h264 code stream. Below are the key frames and P frames of the original code stream.


0000000: SPS

0000000: PPS

0000000: Frame Type

Key Frame Type:

0000000: 0000 0001 6742 401f 9654 0501 ed00 f39e  ....gB@..T......0000010: a000 0000 0168 ce38 8000 0000 0165 8880  .....h.8.....e..0000020: 4001 8231 2000 4f11 d84d 5fff fb3b c28a  @..1 .O..M_..;..0000030: 00bc fc83 03db b3e3 8603 9c59 fa0f a82c  ...........Y...,0000040: df55 fdf6 8414 032a e766 bd4b fbea 05af  .U.....*.f.K....

P Frame Type:

0000000: 0000 0001 6742 401f 9654 0501 ed00 f39e  ....gB@..T......0000010: a000 0000 0168 ce38 8000 0000 0141 9a02  .....h.8.....A..0000020: 0586 7cb9 9125 5788 8f90 7f1f 1930 7eef  ..|..%W......0~.0000030: 6383 bebd 2cc5 3627 92c3 390b 46dc d4a5  c...,.6'..9.F...0000040: 774b 3484 57f8 9840 fba3 1dd6 800f 2242  wK4.W..@......"B0000050: 8816 080f 8f8d 84c6 09aa cda6 363d 00da  ............6=..0000060: b563 4392 bc65 93e2 63bb 6d30 472e 3ef1  .cC..e..c.m0G.>.0000070: 545d 6a3f 36c3 2f7d 6b1e 3c91 d15d d687  T]j?6./}k.<..]..

Therefore, you need to retrieve 29th bytes in the code to determine whether it is 65 or 41,

Public static string bytetohexstring (byte SRC) {stringbuilder = new stringbuilder (""); int v = SRC & 0xff; string HV = integer. tohexstring (V); If (HV. length () <2) {stringbuilder. append (0);} stringbuilder. append (HV); Return stringbuilder. tostring ();} private Boolean isfirstiframe = true; private string IFRAME = "65 "; // The key frame is 0x65 // barrayimage is the byte array of the h264 source code stream if (in video recording) {If (isfirstiframe) {string type = bytetohexstring (barrayimage [29]); If (type. equals (IFRAME) {// 29th characters indicates the Frame Type isfirstiframe = false; shootingvideodata (barrayimage, video_data_ivideolen); // the first frame of recording: key Frame }}else {shootingvideodata (barrayimage, video_data_ivideolen );}}

In this way, there will be no mosaic for the first recorded video.

Note 1: I frame and P frame in h264 are detected.

Link: http://blog.csdn.net/zgyulongfei/article/details/7558031

I found some information on the internet today and learned how to detect the Frame Type in h264. I will record it here.

First, describe the definition of the nal unit type (from H.264, the next generation video compression coding standard ):

Assume that the code stream of an h264 segment is: 00 00 00 01 41 E6 60 ......

The 00 00 01 is the start code, and the next byte after the start code can detect the frame type.

In the preceding code stream, the bitwise 0x41 after the start code is converted into a binary value of 0100 0001.

Note: The order of interpretation is from left to right.

(1) A 1st-bit prohibition bit. A value of 1 indicates a syntax error.

(2) 2nd ~ The third digit is the reference level.

(3) 4th ~ 8 is the nal unit type, where 0 0001 is converted to 1 in decimal format. As you can see, this code stream is a piece of non-partitioned, non-IDR image. In baseline, it is a p frame, because baseline does not have B frames.

For another code stream: 00 00 00 01
65 E8 ......

Then, convert the last five digits to 5 in decimal format based on 0x65 bytes (0110 0101), that is, the part in the IDR image, that is, the I frame.


The code can be written in this way. Int type = 0x65 & 0x1f, and then you can find the expected result in the table based on the type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.