Labels

Friday, 6 March 2015

How to Wrap H264 Frames in to FLV

First, the analysis of the FLV data
We acquire a flv file to a simple analysis of the data format flv
the flv Standard Document Download
the flv file analyzer flvprase Download
This is not the focus of this article, this is skipped. I believe we control the flv standard documents can understand the the flv data format, and I want to learn this knowledge friend first completion of this step is also highly recommended.
RTMP uplink h264 video stream
Uplink video server will be saved as binary files (note 16 data, binary data must use binary form to save the data) as shown (the picture is displayed per line, that is, from 0 to f, such as incomplete, separate Open the picture to view)
The tools I use is Notepad + + (and install the binary view plug-in)
If you have to do the first step, it is not difficult to see that rtmp flv video stream is the one after the Video tag - FLV tag in the removal of the head, leaving only the video tag content.
Our control flv standard document-by-analysis
17:1-keyframe 7-avc
00: AVC sequence header - AVC packet type
00 00 00: composition time, AVC when all 0s, meaningless
Because AVC packet type = AVC sequence header, next step is AVCDecoderConfigurationRecord of the contents of the
configurationVersion = 01
AVCProfileIndication = 42
profile_compatibility = 00
AVCLevelIndication = 1f
lengthSizeMinusOne = ff - the number of bytes FLV NALU packet length data (lengthSizeMinusOne & 3) +1, the actual test found a total of FF evaluates to 4, hereinafter also referred to this data
numOfSequenceParameterSets = E1 - SPS number, numOfSequenceParameterSets & 0x1F, the actual test total E1, evaluates to 1
sequenceParameterSetLength = 00 31 - SPS length of 2 bytes, and the calculation result 49
sequenceParameterSetNALUnits = 67 42 80 1f 96 54 05 01 ed 80 a8 40 00 00 03 00 40 00 00 07 b8 00 00 20 00 00 03 01 00 01 fc 63 8c 00 00 10 00 00 03 00 80 00 fe 31 c3 b4 24 4d 40 - SPS just calculated for the 49 bytes, SPS contains the video length, width information
numOfPictureParameterSets = 01 - the number of PPS, the actual test found that total E1, the results
pictureParameterSetLength = 00 04 - PPS length
pictureParameterSetNALUnits = 68 ce 35 20 - PPS
Followed by another new a videotag package data
17:1-keyframe 7-avc
01: AVC NALU
00 00 00: composition time, AVC when all 0s, meaningless
Because AVCPacket type = AVC NALU, the next step is one or more NALU
Each NALU package front has (lengthSizeMinusOne & 3) +1 bytes of NAL packet length description (previously mentioned, remember), previously calculated for the four bytes
00 00 00 02: 2 ??- NALU length
09 10: NAL package
Insert NALU knowledge, the first five of the first byte of each NALU marked the NAL package type, NAL nal_unit_type
  # Define NALU_TYPE_SLICE 1 
  # Define NALU_TYPE_DPA a 2 
  # Define NALU_TYPE_DPB a 3 
  The # define NALU_TYPE_DPC 4 
  # Define NALU_TYPE_IDR a 5 
  The # define NALU_TYPE_SEI 6 
  # Define NALU_TYPE_SPS a 7 
  The # define NALU_TYPE_PPS 8 
  The # define NALU_TYPE_AUD 9 / / access delimiters # define NALU_TYPE_EOSEQ 10 
  # Define NALU_TYPE_EOSTREAM a 11 
  The # define NALU_TYPE_FILL 12 
09 & 0x1f = 9, the access unit delimiter
We resolve SPS header bytes 67, 67 & 0x1F = 7, pps head byte 68,68 & 0x1f = 8, just to correspond on.
00000029: Description next NAL package length 41
06 00 11 80 00 af c8 00 00 03 00 00 03 00 00 af c8 00 00 03 00 00 40 01 0c 00 00 03 00 00 03 00 90 80 08 00 00 03 00 08 80:06 & 0x1f = 6 - SEI
00 00 3c D0: next NAL packet length
65 88 80 ......: 65 & 0x1f = 5 - I frame data
Packet video tag analysis ends, the following will be followed by the P frame data corresponding to the I frame, as shown below
See 00003d80 line, in front of up to 53 4f 7f are on a video tag content, pull 658,880 said earlier that I frame data, 27 is a video tag
27:2-inter frame, P frame ,7-CodecID = AVC
01: AVCPacket type = AVC NALU
00 00 00: composition time, AVC when all 0s, meaningless
000000020930: with the above analysis of the same pull 2 bytes nal package, access unit delimiter
11:17 000000 bytes NAL package
06 01 0c 00 00 80 00 00 90 80 18 00 00 03 00 08 80:06 & 0x1f = 6-SEI
00 00 46 85: NAL packet data length
41 9a 02 ......: 41 & 0x1f = 1, P frame data

Third, the conversion
Roughly summarized under flv h264 stream, according to the order
1, a video tag, the information contained in: SPS, PPS, access unit delimiter, SEI, I frame packet
2, one or a plurality of video tag, containing information: access unit delimiter, SEI, P-frame packet for a plurality of
Cycle 1, 2
It should be noted, do this step conversion, we need from videotag, get to, a NAL package can, it is the I-frames, P-frames and other types, in fact, we do not need to care about here just to better analyze the data.
h264 NALU and NALU between 000,001 (00,000,001) separated, composition h264 format
1,00 00 00 01 SPS 00 00 00 01 PPS 00 00 00 01 Access Unit separator character 00 00 00 01 SEI 00 00 00 01 I frame 00 00 00 01 P frame 00 00 00 01 P frame ...... (P frame variable number )
Cycle 1

Access unit delimiter and SEI is not necessary, h264 in binary form write to a file, use the Elecard StreamEye can broadcast, download address

No comments:

Post a Comment