Search code examples
videompeg-2mpeg2-ts

PCR based Seek for MpegTSFile


I want to implement seek functionality for MpegTs file playback. Could any one suggest me how to convert PCR to time(in mescs). I know that PCR is used for seek(I have seen in VLC) but would like to know how this can be done.

Kind Regards, Ven


Solution

  • The PCR is a clock that represents 27 Mhz clock at the encoder side. Hence each tick of PCR clock represents the 1/27 Microseconds.

    The first step is to be able to parse the PCR packet. The PCR ticks are maintained by a specific PID (mostly video but can be audio or alternative packets). When the PCR is present in the packet, it is of Adaptation field, type 2 or 3. You can refer to this or wiki for understanding how to parse PCR.

    Once you get the PCR value of the packet - (use a 64 bit integer) you have a timestamp P0 for that packet. Now you can seek to exactly say 10 seceonds, when you get next PCR packet, with a time stamp, P1 where

    P1 = P0 + 10 * 27 * 10^6

    So when you see another such packet containing timestamp P1 or more you can be sure of elapsing 10 seconds.

    Please, note that in some situation, PCR may find discontinuity hence, more calculation is needed at the point PCR base shifts.