Ts is composed of a TS packet. Each packet is fixed with 188 bytes. Each packet starts with a 4-byte packet header. the first byte of the packet header is fixed with 0x47. What are the other 184 bytes, which basically contain audio or video decoding data. If a TS file is given, how can we find the decoded audio/video decoding data?
Each TS packet contains a PID in the header of the first 4 bytes. First, we traverse the TS packets one by one and find the TS packet whose PID is 0. This packet is called pat, this Pat package contains the PID of PMT, so we can traverse the TS package and find the TS package named PMT. What is in PMT? PMT contains the PID of the video ts package and its codec, the PID of the audio ts package and its codec. With codec, we know what decoder to choose. with PID, we can obtain the decoded data.
Let's talk about how video data and audio data are scattered in ts packages. Both video and audio are organized in the form of PES (packetized elementary stream. A video frame is a PES package. We all know that a TS packet contains only 188 bytes, and the packet header contains 184 bytes. This is impossible to put down one frame. In fact, a PES package is allocated to several consecutive ts packages, so if we want to get a frame of data, then we need to extract all the data in several consecutive ts packages to form a PES. So how do we know the beginning and end of a PES? Then we traverse each ts package one by one and look for payload_unit_start_indicator in the packet header to set to 1. This flag represents the beginning of a PES. From here on, until the next payload_unit_start_indicator is 1, the intermediate ts package is composed of PES.
We can see that the pmt_pid value is 4096, so we can find the TS package with PID 4096, that is, PMT. in PMT, we can find that the video PID is 265, and the audio PID is 257.
Reprinted please indicate the source http://blog.csdn.net/tung214/article/details/39502457
The actual sequence of TS packets. The value of paload 1 indicates the beginning of a PES packet.
Discussion on Demux of MPEG2-TS in Vernacular