Mpeg2/ts Get PMT PID

Source: Internet
Author: User

A simple function to get the PMT PID from the specified mpeg2/ts file,

Note the comments in the code.

#define MAKE_PID (P) (((p[0]<<8) + p[1]) & 0X1FFF)[HTML]  View Plain  copy ushort getpmtpid (const char* filename)    {   #define  TSPACKET_SIZE     188         ushort  pid;       uchar buf[TSPACKET_SIZE];        uchar* p = buf;       FILE*  fp;           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) & nbsp;== tspacket_size)  {           if  (Buf[0]  != mpeg_ts_sync_byte)  {                fprintf (stderr,  "no sync byte where one was expected!\n") ;               break;            }               p = buf;           pid = make_pid (( p+1));           if  ( pid != 0 )  {               //  not Pat pack                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 for point field            p += 1 + p[0];               // 8  is the length of the PAT header, and if a table header is defined, use the sizeof operator             p += 8;               // nit program exist??            if ( * ((ushort*) p)  == 0)  {                p += 4; //  pat's program structure is 4 bytes            }               p += 2; //  program number is two bytes               fclose (FP);    &Nbsp;      return make_pid (p);       }           fclose (FP);       return 0;   }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.