#include #include #include #define MAXPID 0x1fff int main (int argc, char **argv) { int pids[MAXPID+1]; int pid_count = 0; int i; int count = 0; for (i = 0; i <= MAXPID; i++) pids[i] = -1; while (1) { unsigned char buffer[188]; int bytes_read; int n; bytes_read = 0; do { n = read (0, buffer + bytes_read, sizeof (buffer) - bytes_read); if (n < 0) { perror ("read"); exit (1); } bytes_read += n; } while (bytes_read < sizeof (buffer) && n > 0); if (n == 0) { if (bytes_read && bytes_read < sizeof (buffer)) { fprintf (stderr, "incomplete ts packet read, size = %d\n", bytes_read); } exit (0); } // printf ("%02x%02x%02x%02x\n", buffer[0], buffer[1], buffer[2], buffer[3]); { int sync; int pid; int cc; char outbuf[1]; int pidtype; sync = buffer[0]; pid = ((buffer[1] & 0x1f) << 8) | buffer[2]; cc = buffer[3] & 0x0f; // fprintf (stderr, "pid = 0x%04x, cc = 0x%x\n", pid, cc); if (sync != 0x47) { // TODO, skip to next package write (1, "#", 1); goto char_written; } pidtype = pids[pid]; if (pidtype == -1) { if (pid == 0) { fprintf (stderr, "# = pid 0x0000 (PAT)\n"); pidtype = pids[pid] = 0; } else if (pid == 0x1fff) { fprintf (stderr, "\" ' pid 0x1fff (invalid)\n"); pidtype = pids[pid] = 0; } else { if (pid_count < 26) pid_count++; pidtype = pids[pid] = pid_count; fprintf (stderr, "%c %c pid 0x%04x\n", 0x40 + pidtype, 0x60 + pidtype, pid); } } if (pid == 0) outbuf[0] = cc ? '=' : '#'; else if (pid == 0x1fff) outbuf[0] = cc ? '\'' : '"'; else outbuf[0] = 0x40 + pidtype + (cc ? 0x20 : 0); write (1, outbuf, 1); char_written: count++; if ((count % 64) == 0) write (1, "\n", 1); } } }