Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPEG2 Presentation Time Stamps (PTS) calculation

I have an MPEG2 TS file and now I am interested in extracting PTS information from each picture frame. I know that PTS is described in 33 bits including 3 marker bits. But I don't know how this bitfield can be converted to more understandable form (seconds, milliseconds). Can anybody help me please

like image 489
Nabijon Avatar asked Nov 28 '12 13:11

Nabijon


People also ask

What is the unit of the MPEG2 transport stream clock?

The MPEG2 transport stream clocks (PCR, PTS, DTS) all have units of 1/90000 second. The PTS and DTS have three marker bits which you need to skip over. The pattern is always (from most to least significant bit) 3 bits, marker, 15 bits, marker, 15 bits, marker.

What is a presentation timestamps?

Presentation timestamp. The presentation timestamp (PTS) is a timestamp metadata field in an MPEG transport stream or MPEG program stream that is used to achieve synchronization of programs' separate elementary streams (for example Video, Audio, Subtitles) when presented to the viewer.

How many marker bits are there in pts and DTS?

The PTS and DTS have three marker bits which you need to skip over. The pattern is always (from most to least significant bit) 3 bits, marker, 15 bits, marker, 15 bits, marker.

Is there a way to skip over the markers in MPEG2?

The MPEG2 transport stream clocks (PCR, PTS, DTS) all have units of 1/90000 second. The PTS and DTS have three marker bits which you need to skip over. The pattern is always (from most to least significant bit) 3 bits, marker, 15 bits, marker, 15 bits, marker. The markers must be equal to 1. In C, removing the markers would work like this:


Video Answer


2 Answers

The MPEG2 transport stream clocks (PCR, PTS, DTS) all have units of 1/90000 second. The PTS and DTS have three marker bits which you need to skip over. The pattern is always (from most to least significant bit) 3 bits, marker, 15 bits, marker, 15 bits, marker. The markers must be equal to 1. In C, removing the markers would work like this:

uint64_t v; // this is a 64bit integer, lowest 36 bits contain a timestamp with markers
uint64_t pts = 0;
pts |= (v >> 3) & (0x0007 << 30); // top 3 bits, shifted left by 3, other bits zeroed out
pts |= (v >> 2) & (0x7fff << 15); // middle 15 bits
pts |= (v >> 1) & (0x7fff <<  0); // bottom 15 bits
// pts now has correct timestamp without markers in lowest 33 bits 

They also have an extension field of 9bits, forming a 42bit integer in which the extension is the least significant bits. The units for the base+extension are 1/27000000 second. Many implementations leave the extension as all zeros.

like image 198
Alex I Avatar answered Nov 16 '22 00:11

Alex I


24hours/day * 60min/hr * 60secs/min *90k/sec (clock) = 7962624000, which needs 33 bits to be represented; You can extract your time from the clock using this info;

like image 36
RGovindan Avatar answered Nov 16 '22 00:11

RGovindan