Annotation of multiplexer/showts.c, revision 1.1

1.1     ! jacob       1: #include <stdlib.h>
        !             2: #include <stdio.h>
        !             3: #include <unistd.h>
        !             4: 
        !             5: #define MAXPID 0x1fff
        !             6: 
        !             7: int main (int argc, char **argv)
        !             8: {
        !             9: 
        !            10:   int           pids[MAXPID+1];
        !            11:   int           pid_count = 0;
        !            12:   int           i;
        !            13:       int count = 0;
        !            14: 
        !            15:   for (i = 0; i <= MAXPID; i++)
        !            16:     pids[i] = -1;
        !            17: 
        !            18:   while (1)
        !            19:     {
        !            20:       unsigned char buffer[188];
        !            21:       int           bytes_read;
        !            22: 
        !            23:       int n;
        !            24: 
        !            25: 
        !            26:       bytes_read = 0;
        !            27: 
        !            28:       do 
        !            29:         {
        !            30:           n = read (0, buffer + bytes_read, sizeof (buffer) - bytes_read);
        !            31: 
        !            32:           if (n < 0)
        !            33:             {
        !            34:               perror ("read");
        !            35:               exit (1);
        !            36:             }
        !            37: 
        !            38:           bytes_read += n;
        !            39:         }
        !            40:       while (bytes_read < sizeof (buffer) && n > 0);
        !            41: 
        !            42:       if (n == 0)
        !            43:         {
        !            44:           if (bytes_read && bytes_read < sizeof (buffer))
        !            45:             {
        !            46:               fprintf (stderr, "incomplete ts packet read, size = %d\n", bytes_read);
        !            47:             }
        !            48:           exit (0);
        !            49:         }
        !            50: 
        !            51:       // printf ("%02x%02x%02x%02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
        !            52:       {
        !            53:         int   sync;
        !            54:         int   pid;
        !            55:         int   cc;
        !            56: 
        !            57:         char  outbuf[1];
        !            58: 
        !            59:         int   pidtype;
        !            60: 
        !            61:         sync = buffer[0];
        !            62:         pid  = ((buffer[1] & 0x1f) << 8) | buffer[2];
        !            63:         cc   = buffer[3] & 0x0f;
        !            64: 
        !            65:         // fprintf (stderr, "pid = 0x%04x, cc = 0x%x\n", pid, cc);
        !            66: 
        !            67:         if (sync != 0x47)
        !            68:           {
        !            69:             // TODO, skip to next package
        !            70:             write (1, "#", 1);
        !            71:             goto char_written;
        !            72:           }
        !            73: 
        !            74:         pidtype = pids[pid];
        !            75: 
        !            76:         if (pidtype == -1)
        !            77:           {
        !            78:             if (pid == 0)
        !            79:               {
        !            80:                 fprintf (stderr, "# =  pid 0x0000 (PAT)\n");
        !            81:                 pidtype = pids[pid] = 0;
        !            82:               }
        !            83:             else if (pid == 0x1fff)
        !            84:               {
        !            85:                 fprintf (stderr, "\" '  pid 0x1fff (invalid)\n");
        !            86:                 pidtype = pids[pid] = 0;
        !            87:               }
        !            88:             else
        !            89:               {
        !            90:                 if (pid_count < 26)
        !            91:                   pid_count++;
        !            92: 
        !            93:                 pidtype = pids[pid] = pid_count;
        !            94:                 fprintf (stderr, "%c %c  pid 0x%04x\n", 
        !            95:                          0x40 + pidtype, 0x60 + pidtype,  pid);
        !            96:               }
        !            97:           }
        !            98: 
        !            99:         if (pid == 0)
        !           100:           outbuf[0] = cc ? '=' : '#';
        !           101:         else if (pid == 0x1fff)
        !           102:           outbuf[0] = cc ? '\'' : '"';
        !           103:         else
        !           104:           outbuf[0] = 0x40 + pidtype + (cc ? 0x20 : 0);
        !           105:             
        !           106:         write (1, outbuf, 1);
        !           107: 
        !           108: char_written:
        !           109:         count++;
        !           110:         if ((count % 64) == 0)
        !           111:           write (1, "\n", 1);
        !           112:       }
        !           113:     }
        !           114: }

LinuxTV legacy CVS <linuxtv.org/cvs>