"H.264 package MPEG-TS stream
- author amour wang
Brief description
This article mainly introduces the key part of the H264 packages into MPEG-TS streams, and some problems encountered in the middle. As for the relevant standards of H264 and TS streams, this is no longer described in detail.
2.H264 packaging TS flow process
The TS stream composition is described (here, the case in this article, other situations refer to TS standards):
TS flow is in units, each package size is 188, mainly including several different types of package 1. PAT table: This table defines all the programs in the current TS stream, which PID is 0x0000, which is PSI The root node, check the search for the program must start from the PAT table. The TS stream ID, program channel number, PMT ID 2. PMT table: program map table, including all PID information in the channel, can filter the corresponding video or audio data from the package according to PID 3. PES: Briefly, PES is the standard for some information about H264.
Package process:
1. TS Header: All packages start with 4 bytes, which indicates that the load unit begins with a more difficult part: It is very official to write on this document, in fact, it is a size of more than one package must be Divided into several packages, the first package is 1, other packages are 0. PID: It is used to represent the type of this package, this is a good understanding, but it is an important sign, so it emphasizes adaptive zone control: current Us only 0x3 and 0x1, indicating that there is and without an adaptive region continuous counter: This place should pay attention to the count of each type of package is the 2. PAT construct of each count, most of this part is fixed, Define the PMT ID, others are basically filled in, this PAT is relatively simple, so a package load is enough.
INT TS_PAT_HEADER (Char * BUF) {
Bits_buffer_s bits;
IF (! buf) {
Return 0;
}
BITS_INITWRITE (& BITS, 32, (UNSIGNED Char *) BUF);
BITS_WRITE (& BITS, 8, 0x00); // Table ID, fixed to 0x00
Bits_WRITE (& BITS, 1, 1); // Section Syntax Indicator, fixed to 1
BITS_WRITE (& BITS, 1, 0); // Zero, 0
BITS_WRITE (& BITS, 2, 0x03); // Reserved1, fixed to 0x03
Bits_WRITE (& BITS, 12, 0x0D); // section length, indicating that the number of bytes after this byte, including CRC32
Bits_WRITE (& BITS, 16, 0x0001); // Transport Stream ID, used to distinguish other TS streams
BITS_WRITE (& BITS, 2, 0x03); // reserved2, fixed to 0x03
BITS_WRITE (& BITS, 5, 0x00); // Version Number, Range 0-31
Bits_WRITE (& BITS, 1, 1); // Current Next Indicator, 0 Next Table is valid, 1 Currently transmitted PAT table can be used
Bits_WRITE (& BITS, 8, 0x00); // section Number, PAT may be divided into multi-segment transmission, the first paragraph is 00
BITS_WRITE (& BITS, 8, 0x00); // Last Section Number
BITS_WRITE (& BITS, 16, 0x0001); // Program Number
Bits_WRITE (& BITS, 3, 0x07); // Reserved3 and PMT_PID are a group, a total of several channels by Program Number
Bits_WRITE (& BITS, 13, TS_PID_PMT); // PMT of Pid in Ts Head This can define the ID of the PMT
CHAR * CRC = CRC32 (BUF); // Calculate CRC
Bits_WRITE (& BITS, 8, CRC [0]);
Bits_WRITE (& BITS, 8, CRC [1]);
Bits_WRITE (& BITS, 8, CRC [2]);
Bits_WRITE (& BITS, 8, CRC [3]);
Bits_Align (& Bits);
Return bits.i_data;
} 1234567891011121314151617181920212223242526272829
3. PPMT construct: This part is not much problem, most of which are filled in according to the standard, define the VIDEO PID. There is still a problem with each frame, not every frame of H264 must have PAT and PMT. Because there are many frame data in each H264, there may be 1 package data, some need to take hundreds of packages, and PAT and PMT appear in the TS stream in the segmentation time. So I am here. A PAT and PMT are inserted every 40 TS packs in the way in FFMPEG.
INT TS_PMT_HEADER (CHAR * BUF) {
Bits_buffer_s bits;
IF (! buf) {
Return 0;
}
BITS_INITWRITE (& BITS, 32, (UNSIGNED Char *) BUF);
Bits_WRITE (& BITS, 8, 0x02); // Table ID, fixed to 0x02
Bits_WRITE (& BITS, 1, 1); // Section Syntax Indicator, fixed to 1
BITS_WRITE (& BITS, 1, 0); // Zero, 0
BITS_WRITE (& BITS, 2, 0x03); // Reserved1, fixed to 0x03
Bits_WRITE (& BITS, 12, 0X12); // section length, indicating the number of bytes after this byte, including CRC32
Bits_WRITE (& BITS, 16, 0x0001); // Program Number, indicating the current PMT associated channel number
BITS_WRITE (& BITS, 2, 0x03); // reserved2, fixed to 0x03
BITS_WRITE (& BITS, 5, 0x00); // Version Number, Range 0-31
Bits_WRITE (& BITS, 1, 1); // Current Next Indicator, 0 Next Table is valid, 1 Currently transmitted PAT table can be used
Bits_WRITE (& BITS, 8, 0x00); // section Number, PAT may be divided into multi-segment transmission, the first paragraph is 00
BITS_WRITE (& BITS, 8, 0x00); // Last Section Number
Bits_WRITE (& BITS, 3, 0x07); // Reserved3, fixed to 0x07
Bits_WRITE (& BITS, 13,
TS_PID_Video); // PCR OF PID IN TS HEAD, if the programs definition from the private data stream is independent of PCR, this domain will be 0x1FFF
Bits_WRITE (& BITS, 4, 0x0F); // Reserved4, fixed to 0x0f
Bits_WRITE (& BITS, 12, 0x00); // Program Info Length, the first two bits 00
Bits_WRITE (& BITS, 8, TS_PMT_STREAMTYPE_H264_VIDEO); // Stream Type, the flag is VIDEO or AUDIO or other data
Bits_WRITE (& BITS, 3, 0x07); // Reserved, fixed to 0x07
Bits_WRITE (& BITS, 13, TS_PID_VIDEO); // Elementary Of Pid in Ts Head, this can be defined
BITS_WRITE (& BITS, 4, 0x0F); // Reserved, fixed to 0x0f
Bits_WRITE (& BITS, 12, 0x00); // Elementary Stream Info Length, the first two bits 00
CHAR * CRC = CRC32 (BUF); // Calculate CRC
Bits_WRITE (& BITS, 8, CRC [0]);
Bits_WRITE (& BITS, 8, CRC [1]);
Bits_WRITE (& BITS, 8, CRC [2]);
Bits_WRITE (& BITS, 8, CRC [3]);
Bits_Align (& Bits);
Return bits.i_data;
} 12345678910111213141516171819202122232425262728293033233343536
4. Pes constructed: This part is the most troublesome, and the largest place. <1> PES data is not a package, do not fill ff behind, but the package where PES is located, followed by H264 data <2> When the H264 data is less than a package, or when the end data is not enough, it is not the first to fill the data and then make up 0xFF, but the first to make up 0xFF to the remaining air space that is just enough to fill the remaining data. <3> PCR / PTS conversion problem: Since the PCR and PTS have 33 positions, if only 4 bytes are directly used, only 8 bytes are available, and there is 8 bytes, pay attention to the size of the size, and the test is small-end mode under ARM, so it needs to be converted < 4> PTS calculation method:
STATIC uint32_t video_frame_rate = 30;
Static uint32_t video_pts_increment = 90000 / video_frame_rate; // removed by a frame rate, how much is the time consumption of each frame, unit is Timescale Unit
Static uint64_t video_pts = 0; 123
<5>
INT mk_pes_packet (Char * Buf, Int BVideo, Int Length, Int Bdtsen, Unsigned Int PTS,
Unsigned int dts) {PES_HEAD_S PESHEAD; // PES header
PES_OPTION_S PESOTION; / / Sign
PES_PTS_S PESPTS; // PTS
PES_PTS_S PESDTS; // DTS
IF (! buf) {
Return 0;
}
MEMSET (& Peshead, 0, Sizeof (Peshead);
Memset (& Pesoption, 0, Sizeof (Pesoption);
MEMSET (& PESPTS, 0, SIZEOF (PESPTS);
MEMSET (& PESDTS, 0, SIZEOF (PESDTS));
Peshead.startcode = htonl (0x000001) >> 8; // Daxous
Peshead.stream_id = bvideo? 0xE0: 0xc0; // If it is video, E0
// if (pes_max_size
> 30) & 0x07;
PESPTS.TS2 = (PTS >> 22) & 0xFF;
PESPTS.TS3 = (PTS >> 15) & 0x7f;
PESPTS.TS4 = (PTS >> 7) & 0xFF;
PESPTS.TS5 = PTS & 0x7F;
PESDTS.FIXED1 = PESDTS.FIXED2 = pestts.fixed3 = pestts.fixed4 = 0x01;
PESDTS.TS1 = (DTS >> 30) & 0x07;
PESDTS.TS2 = (DTS >> 22) & 0xFF;
PESDTS.TS3 = (DTS >> 15) & 0x7F;
PESDTS.TS4 = (DTS >> 7) & 0xFF;
PESDTS.TS5 = DTS & 0X7F;
CHAR * HEAD = BUF;
Memcpy (Head, & Peshead, Sizeof (Peshead);
HEAD + = SizeOf (Peshead);
Memcpy (Head, & Pesoption, Sizeof (Pesoption);
HEAD + = SizeOf (Pesoption);
Memcpy (Head, & PESPTS, SIZEOF (PESPTS);
HEAD + = SizeOf (PESPTS);
IF (bdtsen) {
Memcpy (Head, & PESDTS, SIZEOF (PESDTS);
HEAD + = SizeOf (PESPTS);
}
Return (Head - BUF);
} 12 12 12
references:
http://www.cnblogs.com/lifan3a/articles/6214419.html http://blog.csdn.net/u013354805/Article/details/51591229 http://blog.csdn.net/u013354805/Article/details/ 51578457 http://blog.csdn.net/u013354805/Article/details/51586086 "
Our other product: