File:  [DVB] / multiplexer / showts.c
Revision 1.3: download - view: text, annotated - select for diffs
Mon Aug 25 21:05:23 2003 UTC (20 years, 9 months ago) by oskar
Branches: MAIN
CVS tags: HEAD
add copyright notice

/*
 * tiny debugging tool.
 * when fed with a transport stream to stdin, produces one char
 * per packet to stdout, and a few informative messages to stderr.
 * Upper case letters denote a continuity counter mismatch.
 *
 * Author: Jacob Schroeder (jacob@convergence.de)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define MAXPID 0x1fff

int main (int argc, char **argv)
{

  int           pids[MAXPID+1];
  int           ccs[MAXPID+1];
  int           pid_count = 0;
  int           i;
      int count = 0;

  for (i = 0; i <= MAXPID; i++)
    {
      pids[i] = -1;
      ccs[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;
        int   ccok;

        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);
          }
        else
          {
            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);
                  }
                ccok = 1;
              }
            else
              {
                ccok = ccs[pid] == cc ? 1 : 0;
              }

            if (pid == 0)
              outbuf[0] = ccok ? '=' : '#';
            else if (pid == 0x1fff)
              outbuf[0] = ccok ? '\'' : '"';
            else
              outbuf[0] = 0x40 + pidtype + (ccok ? 0x20 : 0);

            ccs[pid] = (cc + 1) & 0x0F;
                
            write (1, outbuf, 1);
          }

        count++;
        if ((count % 64) == 0)
          write (1, "\n", 1);
      }
    }
}

LinuxTV legacy CVS <linuxtv.org/cvs>