Annotation of multiplexer/showts.c, revision 1.3

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)
1.3     ! oskar       8:  *
        !             9:  * This program is free software; you can redistribute it and/or modify
        !            10:  * it under the terms of the GNU General Public License as published by
        !            11:  * the Free Software Foundation; either version 2 of the License, or
        !            12:  * (at your option) any later version.
        !            13:  *
        !            14:  * This program is distributed in the hope that it will be useful,
        !            15:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            16:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        !            17:  * GNU General Public License for more details.
        !            18:  *
        !            19:  * You should have received a copy of the GNU General Public License
        !            20:  * along with this program; if not, write to the Free Software
        !            21:  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1.2       oskar      22:  */
                     23: 
1.1       jacob      24: #include <stdlib.h>
                     25: #include <stdio.h>
                     26: #include <unistd.h>
                     27: 
                     28: #define MAXPID 0x1fff
                     29: 
                     30: int main (int argc, char **argv)
                     31: {
                     32: 
                     33:   int           pids[MAXPID+1];
1.2       oskar      34:   int           ccs[MAXPID+1];
1.1       jacob      35:   int           pid_count = 0;
                     36:   int           i;
                     37:       int count = 0;
                     38: 
                     39:   for (i = 0; i <= MAXPID; i++)
1.2       oskar      40:     {
                     41:       pids[i] = -1;
                     42:       ccs[i] = -1;
                     43:     }
1.1       jacob      44: 
                     45:   while (1)
                     46:     {
                     47:       unsigned char buffer[188];
                     48:       int           bytes_read;
                     49: 
                     50:       int n;
                     51: 
                     52: 
                     53:       bytes_read = 0;
                     54: 
                     55:       do 
                     56:         {
                     57:           n = read (0, buffer + bytes_read, sizeof (buffer) - bytes_read);
                     58: 
                     59:           if (n < 0)
                     60:             {
                     61:               perror ("read");
                     62:               exit (1);
                     63:             }
                     64: 
                     65:           bytes_read += n;
                     66:         }
                     67:       while (bytes_read < sizeof (buffer) && n > 0);
                     68: 
                     69:       if (n == 0)
                     70:         {
                     71:           if (bytes_read && bytes_read < sizeof (buffer))
                     72:             {
                     73:               fprintf (stderr, "incomplete ts packet read, size = %d\n", bytes_read);
                     74:             }
                     75:           exit (0);
                     76:         }
                     77: 
                     78:       // printf ("%02x%02x%02x%02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
                     79:       {
                     80:         int   sync;
                     81:         int   pid;
                     82:         int   cc;
1.2       oskar      83:         int   ccok;
1.1       jacob      84: 
                     85:         char  outbuf[1];
                     86: 
                     87:         int   pidtype;
                     88: 
                     89:         sync = buffer[0];
                     90:         pid  = ((buffer[1] & 0x1f) << 8) | buffer[2];
                     91:         cc   = buffer[3] & 0x0f;
                     92: 
                     93:         // fprintf (stderr, "pid = 0x%04x, cc = 0x%x\n", pid, cc);
                     94: 
                     95:         if (sync != 0x47)
                     96:           {
                     97:             // TODO, skip to next package
                     98:             write (1, "#", 1);
                     99:           }
1.2       oskar     100:         else
                    101:           {
                    102:             pidtype = pids[pid];
1.1       jacob     103: 
1.2       oskar     104:             if (pidtype == -1)
1.1       jacob     105:               {
1.2       oskar     106:                 if (pid == 0)
                    107:                   {
                    108:                     fprintf (stderr, "# =  pid 0x0000 (PAT)\n");
                    109:                     pidtype = pids[pid] = 0;
                    110:                   }
                    111:                 else if (pid == 0x1fff)
                    112:                   {
                    113:                     fprintf (stderr, "\" '  pid 0x1fff (invalid)\n");
                    114:                     pidtype = pids[pid] = 0;
                    115:                   }
                    116:                 else
                    117:                   {
                    118:                     if (pid_count < 26)
                    119:                       pid_count++;
                    120: 
                    121:                     pidtype = pids[pid] = pid_count;
                    122:                     fprintf (stderr, "%c %c  pid 0x%04x\n", 
                    123:                              0x40 + pidtype, 0x60 + pidtype,  pid);
                    124:                   }
                    125:                 ccok = 1;
1.1       jacob     126:               }
1.2       oskar     127:             else
1.1       jacob     128:               {
1.2       oskar     129:                 ccok = ccs[pid] == cc ? 1 : 0;
1.1       jacob     130:               }
1.2       oskar     131: 
                    132:             if (pid == 0)
                    133:               outbuf[0] = ccok ? '=' : '#';
                    134:             else if (pid == 0x1fff)
                    135:               outbuf[0] = ccok ? '\'' : '"';
1.1       jacob     136:             else
1.2       oskar     137:               outbuf[0] = 0x40 + pidtype + (ccok ? 0x20 : 0);
1.1       jacob     138: 
1.2       oskar     139:             ccs[pid] = (cc + 1) & 0x0F;
                    140:                 
                    141:             write (1, outbuf, 1);
1.1       jacob     142:           }
                    143: 
                    144:         count++;
                    145:         if ((count % 64) == 0)
                    146:           write (1, "\n", 1);
                    147:       }
                    148:     }
                    149: }

LinuxTV legacy CVS <linuxtv.org/cvs>