A simple function to get the PCR PID from the specified mpeg2/ts file,
The head structure of the PMT has a PCR PID, we first get the PMT PID, and then analyze the PMT package, you can get the PCR pid.
Implementation of the GETPMTPID function see: http://blog.csdn.net/myaccella/article/details/6982596
Make_pid:
#define MAKE_PID (P) (((p[0]<<8) + p[1]) & 0X1FFF)
[HTML] View Plain copy ushort getpcrpid (const char* filename) { #define TSPACKET_SIZE 188 ushort pid, pmtpid, pcrpid = 0; uchar buf[TSPACKET_SIZE]; uchar* p = buf; file* fp; pmtpid = Getpmtpid (filename); if (pmtpid == 0) { fprintf (stderr, "unable to get pmt pid!\n "); return 0; } fp = fopen (filename, "RB"); if (fp == null) { fprintf (stderr, "failed to open the file %s: %s\n", filename, strerror (errno)); return 0; } while (fread (BUF, &NBSP;1,&NBSP;TSPACKET_SIZE,&NBSP;FP) == tspacket_size) { if (buf[0] != mpeg_ts_sync_byte) { // @MPEG_TS_SYNC_BYTE = 0x47 fprintf ( stderr, "no sync byte!\n"); break; } p = buf; pid = make_pid ((p+1)); if ( pid != pmtpid ) { // Not a PMT packet continue; } // payload_unit_start_indicator must be 1 if (! BUF[1]&NBSP;>>&NBSP;6&NBSP;&&NBSP;0X01)) { fprintf (stderr, "no payload_unit_start_indicator!\n"); continue; } // Skip the TS header p += mpeg_ts_header_size; //@MPEG_TS_ header_size = 4 // adaptation field exist or not if ((buf[3] >> 4 & 0x03) & 0x2) { fprintf (stderr, "Adaptation field exist\n "); p += 1 + p[0]; // p[0] is Adaptation field length, 1 for length field } // p[0] for Point field length, 1 to point field p += 1 + p[0]; // 8 Offset for pcr pid in PMT header p += 8; // Pcr pid If it's 0x1fff, will need to continue to fetch pcrpid = make_pid (p);