Obtain SPS and PPS through h264

Source: Internet
Author: User

If you want to transmit video streams in real time when using Android phones for h264 hard encoding, you need to know the sequence parameter sets (SPS) and picture parameter set (PPS) of the video stream ).

Today, I understand how to obtain SPS and PPS. I will record them here. I hope you can get some help here.

First, let's take a look at the prerequisites. The video recording parameters I set are:

Mmediarecorder. setoutputformat (mediarecorder. outputformat. three_gpp );

Mmediarecorder. setvideoencoder (mediarecorder. videoencoder. h264 );

To make everyone better understand, I first paste the avcc data structure:

aligned(8) class AVCDecoderConfigurationRecord {  unsigned int(8) configurationVersion = 1;  unsigned int(8) AVCProfileIndication;  unsigned int(8) profile_compatibility;  unsigned int(8) AVCLevelIndication;  bit(6) reserved = '111111'b;  unsigned int(2) lengthSizeMinusOne;  bit(3) reserved = '111'b;  unsigned int(5) numOfSequenceParameterSets; for (i=0; i< numOfSequenceParameterSetsispan> unsigned int(16) sequenceParameterSetLength ;  bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit;  }  unsigned int(8) numOfPictureParameterSets; for (i=0; i< numOfPictureParameterSetsispan> unsigned int(16) pictureParameterSetLength;  bit(8*pictureParameterSetLength) pictureParameterSetNALUnit;  } } 

OK. The data structure is pasted. I then paste the recorded 3GP output type h264 code stream segment.

The Shadow part is all the data of avcc.

Where: 0x61 0x76 0x63 0x43 is the character avcc

0x01 is configurationversion

0x42 is avcprofileindication

0x00 is profile_compatibility

0x1f is avclevelindication

0xff is a 6bit reserved and a 2bit lengthsizeminusone

0xe1 is a 3bit reserved and a 5bit numofsequenceparametersets.

0x00 0x09 is the length of SPS to 9 bytes.

Therefore, the content of SPS is the next 9 Bytes: 67 42 00 1f E9 02 C1 2C 80

Next: 01 is numofpictureparametersets

0x00 and 0x04 are 4 bytes in PPS length.

Therefore, the content of PPS is the next 4 Bytes: 68 ce 06 F2

Through this data segment, SPS and PPS can be obtained.

Below I will post the SPS and PPS code for obtaining the h264 code stream with the output format 3GP using Java code:

Package cn.edu. xmu. zgy; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; publicclass obtainspsandpps {publicvoid getspsandpps (string filename) throws ioexception {file = new file (filename); fileinputstream FD = new fileinputstream (File); int filelength = (INT) file. length (); byte [] filedata = newbyte [filelength]; FCM. read (filedata); // 'A' = 0x61, 'V' = 0x76, 'C' = 0x63, 'c' = 0x43byte [] avcc = newbyte [] {0x61, 0x76, 0x63, 0x43 }; // start position of avcc: int avcrecord = 0; For (int ix = 0; IX <filelengthixspan> If (filedata [ix] = avcc [0] & filedata [ix + 1] = avcc [1] & filedata [ix + 2] = avcc [2] & filedata [ix + 3] = avcc [3]) {// If avcc is found, the starting position of avcrecord is recorded and the loop is exited. Avcrecord = IX + 4; break;} If (0 = avcrecord) {system. out. println ("avcc not found, please check whether the file format is correct"); Return ;}// add 7 to skip // (1) 8-byte configurationversion // (2) 8-byte avcprofileindication // (3) 8-byte profile_compatibility // (4) 8-byte avclevelindication // (5) 6-bit reserved // (6) 2-bit lengthsizeminusone // (7) 3-bit reserved // (8) 5-bit numofsequenceparametersets // 6 bytes in total, then the sequenceparametersetlength is reached at int spsstartpos = avcrecord + 6; byte [] spsbt = newbyte [] {filedata [spsstartpos], filedata [spsstartpos + 1]}; int spslength = bytes2int (spsbt); byte [] SPS = newbyte [spslength]; // skip the sequenceparametersetlength spsstartpos + = 2; system. arraycopy (filedata, spsstartpos, SPS, 0, spslength); printresult ("SPs", SPS, spslength ); // The bottom part is to get PPS // spsstartpos + spslength can jump to the PPS position // Add 1 to skip 1 byte numofpictureparametersetsint ppsstartpos = spsstartpos + spslength + 1; byte [] ppsbt = newbyte [] {filedata [ppsstartpos], filedata [ppsstartpos + 1]}; int ppslength = bytes2int (ppsbt); byte [] PPS = newbyte [ppslength]; ppsstartpos + = 2; system. arraycopy (filedata, ppsstartpos, PPS, 0, ppslength); printresult ("PPS", PPS, ppslength);} privateint bytes2int (byte [] BT) {int ret = BT [0]; RET <span style = 'font-size: 12px; font-style: normal; font-weight: normal; font-family: 'courier new monospacecolorrgb> 8; RET | = BT [1]; return ret;} privatevoid printresult (string type, byte [] BT, int Len) {system. out. println (Type + "Length:" + Len); string cont = type + "content:"; system. out. print (cont); For (int ix = 0; ix <lenixspan> system. out. printf ("% 02x", BT [ix]);} system. out. println ("\ n ----------");} publicstaticvoid main (string [] ARGs) throws ioexception {New obtainspsandpps (). getspsandpps ("C: \ zgy. h264 ");}}

The running result is as follows:

SPS length: 9
SPS: 67 42 00 1f E9 02 C1 2C 80
----------
PPS length: 4
PPS content: 68 ce 06 F2

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.