How to get the length and width of the video from the H264 raw data SPS

Source: Internet
Author: User

Have such a demand, give you a h264 raw data file, let you play directly out, how to implement?

The idea is that the H264 raw data format is 0x00000001 back with 0x67 0x68 0x65 0x41 Such data, decoding requires a complete NAL data unit, We need to read the data from each 0x00000001 and the next 0x00000001 before decoding it to the decoder.

Read the file I will not wordy, this article mainly explains how to obtain the video length from the SPS, SPS is 0x67 start data, read to 0x00000001 after a byte is 0x67 after the data is the SPS. Let's look at the structure of the SPS inside a package:


We can see that the bottom 42 start is the SPS data behind 0x67, see the Official document of H264 to know, SPS parameters are stored in the form of bit.

I wrote a class that resolves the SPS myself, with the following code:

Import android.util.log;/* * author:vincent Luo * date:20150615 * Description: Reference H264 Standard syntax to implement the analysis of SPS parameters */public class H264SP SPaser {private static final String TAG = "H264spspaser";p rivate static int startBit = 0;/* * Starts reading from the startBit bit of data stream, reads BITC NT bit, return */public static short U (byte[] Data,int bitcnt,int StartBit) with unsigned reshape {short ret = 0;int start = startbit;for (int i = 0 ; i < bitcnt;i++) {ret<<=1;if ((DATA[START/8] & (0x80 >> (start%8)))! = 0) {ret + = 1;} start++;} return ret;} /* * Unsigned index COLUMBUS code * leadingzerobits =? 1; * for (b = 0;!b; leadingzerobits++) * b = read_bits (1) * Variable Codenum assign values as follows: * Codenum = 2^leadingzerobits? 1 + read_bits (leadingzerobits) * Here the return value of the Read_bits (leadingzerobits) is expressed using a high-level, binary unsigned integer. */public static short UE (byte[] Data,int StartBit) {short ret = 0;int leadingzerobits = -1;int Tempstartbit = (StartBit = = -1) startbit:startbit;//If you pass in-1, then use the last recorded static variable for (int b = 0; b! = 1; leadingzerobits++) {//read to the first number not 0, calculate the number of preceding 0 b = u (data, 1,tempstartbit++); }LOG.D (TAG, "ue leadingzerobits =" + Leadingzerobits + ", Math.pow (2, leadingzerobits) =" + Math.pow (2, leadingzerobits) + ", Tempstartbit =" + tempstartbit); ret = (short) (Math.pow (2, Leadingzerobits)-1 + u (data,leadingzerobits,tempstartbit)); StartBit = Tempstartbit + leadingzerobits; LOG.D (TAG, "ue startBit =" + StartBit); return ret;} /* * SIGNED index Columbus Code * 9.1.1 Signed index Columbus Code mapping process * According to section 9.1, the input of this procedure is codenum. * The output of this procedure is the value of SE (v). * Table 9-3 shows the rules for assigning syntax element values to Codenum, where the values of the syntax elements are arranged in ascending order of absolute value, and negative values are arranged by their absolute * value, but are listed after positive values equal to the absolute value. * Table 9-3-Signed exponential Columbus Code Syntax element SE (v) value corresponds to Codenum *codenum syntax element value *0 0 *11 *2?1 *32 *4?2 *53 *6?3 *k (? 1) ^ (k+1) ceil (k÷2) */public St atic int SE (byte[] data,int StartBit) {int ret = 0;short codenum = UE (data,startbit); ret = (int) (Math.pow ( -1, Codenum + 1) *math.ceil (CODENUM/2)); return ret;}}
Just call the following interface to get a wide height:

int width = (H264spspaser.ue (sps,34) + 1) *16;int height = (H264spspaser.ue (sps,-1) + 1) *16;
Where the parameter SPS is the 42 start of the stream stream, 34 refers to the width of the beginning of the position (bit).


How to get the length and width of the video from the H264 raw data SPS

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.