I want clarification aboutPID in TS

Me2019H

Registered
Messages
101
hello,

ransport Packets (188 bytes each):
---------------------------
|Header | Payload |
---------------------------
the header contains 0x47 (Sync Byte) witch means begininng of all packets

but if the Sync Byte contains
0x0000 PAT
or
0x0050 PMT
or
0x0200 AC-3 Audio
or
0x0300 MPEG Video, PCR
How do we determine the begininng of the packet?
 

cayoenrique

Member
Messages
475
Me2019H

Google search &Wiki are your friend. Star reading here:

*Download:

_https://tsduck.io/download/docs/mpegts-introduction.pdf

Print the following:

_https://en.wikipedia.org/wiki/MPEG_transport_stream
_https://en.wikipedia.org/wiki/Program-specific_information
_http://colibri.bplaced.net/

*And for Specs & standards look in

_https://en.wikipedia.org/wiki/MPEG-2

==========================================

Now here a litle C program for you to understand. I am not sure who wrote it but I beleive was K2TSET

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char *argv[])
{
    FILE *fp = NULL;
    FILE *fpout = NULL;
    char *PUSI_data;
    char PUSI_data_A[1024] = "";
    char *in_file_name;
    char *out_file_name;
    unsigned char hex[1024] = "";
    int each = 0;
    int af_length = 0;
    size_t bytes = 0;
    size_t used = 0;
    size_t offset = 0;
    unsigned int PID = 0x200;

// get PID argument
    PID = strtol(argv[2],0,16);
  
// open input ts file  
    if ( ( fp = fopen (argv[1], "rb")) == NULL) {
        printf ( "could not open file\n");
        return 1;
    }

// compute outout filename
    in_file_name=argv[1];
    printf("InFile %s\n",in_file_name);
    char *token = strtok(in_file_name, ".");
    out_file_name= strcat(token ,"_PUSI.txt");
    printf("OutFile %s\n",out_file_name);
  
//open output txt file
    fpout = fopen(out_file_name, "w");  
   // exiting program
    if (fpout == NULL) {
        printf("Error!");
        exit(1);
    }  
  
//    PID = strtol(argv[2],0,16);
    printf ( "Scanning for PUSI in PID: 0x%04X\n",PID);

    //loop as long as there are bytes to read
    while ( ( bytes = fread ( &hex, 1, 188, fp)) > 0) {
    PUSI_data="";
    // look for 0x47 Sync
            if ( hex[0] == 0x47)

        // look for Pusi flag and PID
            {
                if (((hex[1] & 0x40) == 0x40) & ((hex[1] & 0x3f) == (PID >> 8 & 0xff)) & ((hex[2] & 0xff) == (PID & 0xff)) )
              
            {
            if ((hex[3] & 0xC0) == 0x00) { fprintf (fpout, "C ");  }
            if ((hex[3] & 0xC0) == 0x40) { fprintf (fpout, "X ");  }
            if ((hex[3] & 0xC0) == 0x80) { fprintf (fpout, "E ");  }
            if ((hex[3] & 0xC0) == 0xC0) { fprintf (fpout, "O ");  }

          
// check for Adaption Field 0x80              
            af_length=0;
            if ((hex[3] & 0x30) == 0x00) { fprintf (fpout, "X ");}
            if ((hex[3] & 0x30) == 0x10) { fprintf (fpout, "Payld: ");}
            if ((hex[3] & 0x30) == 0x20) { fprintf (fpout, "AF: %02X ",hex[4] ); af_length=hex[4]+1;}
            if ((hex[3] & 0x30) == 0x30) { fprintf (fpout, "AF: %02X ",hex[4] ); af_length=hex[4]+1;}

            int j;
               fprintf (fpout, "PUSI:");
               for(j=0; j<4; j++){fprintf (fpout,"%02x ", hex[j]); } 

               fprintf (fpout, "CB0: ");
               for(j=4; j<12; j++){fprintf (fpout,"%02x ", hex[j+af_length]); } 

               fprintf (fpout, "CB1: ");
               for(j=12; j<15; j++){fprintf (fpout,"%02x ", hex[j+af_length]); } 

               fprintf (fpout, "\n");
            }
        }      
    }
    printf ( "End scanning file\n");
    fclose ( fp);
    fclose( fpout);
   return 0;
}

Name it Find_PUSI.c & compile it

Code:
gcc -o Find_PUSI.exe Find_PUSI.c

to use it

Code:
Find_PUSI.exe example_biss.ts 0x0200
 
Top