Annotation of multiplexer/showts.c, revision 1.2

1.2     ! oskar       1: /*
        !             2:  * tiny debugging tool.
        !             3:  * when fed with a transport stream to stdin, produces one char
        !             4:  * per packet to stdout, and a few informative messages to stderr.
        !             5:  * Upper case letters denote a continuity counter mismatch.
        !             6:  *
        !             7:  * Author: Jacob Schroeder (jacob@convergence.de)
        !             8:  */
        !             9: 
1.1       jacob      10: #include <stdlib.h>
                     11: #include <stdio.h>
                     12: #include <unistd.h>
                     13: 
                     14: #define MAXPID 0x1fff
                     15: 
                     16: int main (int argc, char **argv)
                     17: {
                     18: 
                     19:   int           pids[MAXPID+1];
1.2     ! oskar      20:   int           ccs[MAXPID+1];
1.1       jacob      21:   int           pid_count = 0;
                     22:   int           i;
                     23:       int count = 0;
                     24: 
                     25:   for (i = 0; i <= MAXPID; i++)
1.2     ! oskar      26:     {
        !            27:       pids[i] = -1;
        !            28:       ccs[i] = -1;
        !            29:     }
1.1       jacob      30: 
                     31:   while (1)
                     32:     {
                     33:       unsigned char buffer[188];
                     34:       int           bytes_read;
                     35: 
                     36:       int n;
                     37: 
                     38: 
                     39:       bytes_read = 0;
                     40: 
                     41:       do 
                     42:         {
                     43:           n = read (0, buffer + bytes_read, sizeof (buffer) - bytes_read);
                     44: 
                     45:           if (n < 0)
                     46:             {
                     47:               perror ("read");
                     48:               exit (1);
                     49:             }
                     50: 
                     51:           bytes_read += n;
                     52:         }
                     53:       while (bytes_read < sizeof (buffer) && n > 0);
                     54: 
                     55:       if (n == 0)
                     56:         {
                     57:           if (bytes_read && bytes_read < sizeof (buffer))
                     58:             {
                     59:               fprintf (stderr, "incomplete ts packet read, size = %d\n", bytes_read);
                     60:             }
                     61:           exit (0);
                     62:         }
                     63: 
                     64:       // printf ("%02x%02x%02x%02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
                     65:       {
                     66:         int   sync;
                     67:         int   pid;
                     68:         int   cc;
1.2     ! oskar      69:         int   ccok;
1.1       jacob      70: 
                     71:         char  outbuf[1];
                     72: 
                     73:         int   pidtype;
                     74: 
                     75:         sync = buffer[0];
                     76:         pid  = ((buffer[1] & 0x1f) << 8) | buffer[2];
                     77:         cc   = buffer[3] & 0x0f;
                     78: 
                     79:         // fprintf (stderr, "pid = 0x%04x, cc = 0x%x\n", pid, cc);
                     80: 
                     81:         if (sync != 0x47)
                     82:           {
                     83:             // TODO, skip to next package
                     84:             write (1, "#", 1);
                     85:           }
1.2     ! oskar      86:         else
        !            87:           {
        !            88:             pidtype = pids[pid];
1.1       jacob      89: 
1.2     ! oskar      90:             if (pidtype == -1)
1.1       jacob      91:               {
1.2     ! oskar      92:                 if (pid == 0)
        !            93:                   {
        !            94:                     fprintf (stderr, "# =  pid 0x0000 (PAT)\n");
        !            95:                     pidtype = pids[pid] = 0;
        !            96:                   }
        !            97:                 else if (pid == 0x1fff)
        !            98:                   {
        !            99:                     fprintf (stderr, "\" '  pid 0x1fff (invalid)\n");
        !           100:                     pidtype = pids[pid] = 0;
        !           101:                   }
        !           102:                 else
        !           103:                   {
        !           104:                     if (pid_count < 26)
        !           105:                       pid_count++;
        !           106: 
        !           107:                     pidtype = pids[pid] = pid_count;
        !           108:                     fprintf (stderr, "%c %c  pid 0x%04x\n", 
        !           109:                              0x40 + pidtype, 0x60 + pidtype,  pid);
        !           110:                   }
        !           111:                 ccok = 1;
1.1       jacob     112:               }
1.2     ! oskar     113:             else
1.1       jacob     114:               {
1.2     ! oskar     115:                 ccok = ccs[pid] == cc ? 1 : 0;
1.1       jacob     116:               }
1.2     ! oskar     117: 
        !           118:             if (pid == 0)
        !           119:               outbuf[0] = ccok ? '=' : '#';
        !           120:             else if (pid == 0x1fff)
        !           121:               outbuf[0] = ccok ? '\'' : '"';
1.1       jacob     122:             else
1.2     ! oskar     123:               outbuf[0] = 0x40 + pidtype + (ccok ? 0x20 : 0);
1.1       jacob     124: 
1.2     ! oskar     125:             ccs[pid] = (cc + 1) & 0x0F;
        !           126:                 
        !           127:             write (1, outbuf, 1);
1.1       jacob     128:           }
                    129: 
                    130:         count++;
                    131:         if ((count % 64) == 0)
                    132:           write (1, "\n", 1);
                    133:       }
                    134:     }
                    135: }

LinuxTV legacy CVS <linuxtv.org/cvs>