---
title: "MPEG-2 TS PAT header decoding problem"
author: "hsomesun"
date: "2013-09-10T16:54:36Z"
tags: []
categories: ["编程语言"]
slug: "mpeg-2-ts-pat-header-decoding-problem"
draft: false
lastmod: "2013-09-10T16:54:36Z"
id: 1940209
url: "https://hsomesun.iteye.com/blog/1940209"
---

> 
> 
> Hi,
> I'm trying to develop a basic MPEG-2 Transport Stream decoder in order to extract metadatas about PSI packets. I have a bunch of books and documents (but not the standard itself) describing the stream layout; I also have some consistent TS files for testing and a TS analyser software (Manzanita's MP2TSA) to validate my assertions. The documents I use sometimes disagree, but I was able to filter the correct information up to this point. Now I have the following problem while trying to decode a PAT section: there's an extra byte in the PAT header, undocumented in all documents I've used so far. Below is the beginning of a TS Packet containing a PAT section (the remaining bytes of the packet have a value of FF):
> 
> Code:
> 47 40 00 10 00 00 B0 0D 00 01 C1 00 00 00 02 E1 E0 2F 39 8D 8D  .....
> |TS Header |   PAT Header                 | ? |  Program   |  CRC32      | Stuffing bytes
> According to all the docs I have, the PAT header's length should be equal to 8; but it ended with a mismatch when comparing my results to the ones given by MP2TSA (which is an accurate tool). Adjusting the header's length value to 9 "solves" the problem... but I don't like to use intuitive reasoning! The question is:
> 
> Does someone know the exact layout of a PAT header?
> 
> Another problem, leading to the same question:
> 
> I'm confused about the section_number and last_section_number field values of the PAT header (respectively 0xC1 and 0x00 in the sample above). I found the following description of these fields in the book "Video Demystified, A Handbook for the Digital Engineer", 4th edition (Keith Jack, Newnes, 2005, p.675):
> 
> Section_number
> [...] The section_number of the first section in the PAT must be 0x00. It is incremented by 1 with each additional section in the PAT.
> Last_section_number
> This 8-bit binary number specifies the number of the last section (that is, the section with the highest section_number) of the complete PAT.
> This is obviously wrong, but I can't figure out why... any ideas?
> 
> Thanks for reading
> 
> Jerome
> 
> 




> 
> The problem is solved...
> 
> According to the ITU H.222.0 (05/06) Recommendation (http://www.itu.int/rec/T-REC-H.222.0-200605-I/en), section 2.4.4.2:
> 
> [...] When at least one section begins in a given Transport Stream packet, then the payload_unit_start_indicator (refer to 2.4.3.2) shall be set to '1' and the first byte of the payload of that Transport Stream packet shall contain the pointer. When no section begins in a given Transport Stream packet, then the payload_unit_start_indicator shall be set to '0' and no pointer shall be sent in the payload of that packet.
> The content of the PAT packet is correctly interpreted this way:
> 
> Code:
> 47 40 00 10 00 00 B0 0D 00 01 C1 00 00 00 02 E1 E0 2F 39 8D 8D  .....
> |TS Header | * | PAT Header                   |  Program   |  CRC32      | Stuffing bytes
> * = pointer_field
> The payload_unit_start is effectively set to 1, so the first byte of the payload data refers to a pointer_field which indicates the number of bytes until the first byte of the section. This makes perfect sense now.
> 
> Thanks to myself
> :-)
> 



http://forum.videohelp.com/threads/286326-MPEG-2-TS-PAT-header-decoding-problem