www.pudn.com > MPEG2systemsrc.rar > InputPort.C


/* Copyright (C) 1995, Tektronix Inc. All Rights Reserved.
 *
 *   Usage Restrictions
 *
 * License is granted to copy, to use, and to make and to use derivative
 * works for research and evaluation purposes only.
 *
 *   Disclaimer of Warranty
 *
 * These software programs are available to the user without any license
 * fee or royalty on an "as is" basis.  Tektronix Inc. disclaims any and
 * all warranties, whether express, implied, or statuary, including any
 * implied warranties or merchantability or of fitness for a particular
 * purpose.  In no event shall the copyright-holder be liable for any 
 * incidental, punitive, or consequential damages of any kind whatsoever
 * arising from the use of these programs.
 *
 * This disclaimer of warranty extends to the user of these programs and
 * user's customers, employees, agents, transferees, successors, and
 * assigns.
 *
 * The Tektronix Inc. does not represent or warrant that the programs
 * furnished hereunder are free of infringement of any third-party
 * patents.
*/

/* InputPort implementation */

#include "InputPort.H"
#include "Utilities.H"

extern "C"
{
#include 
#include 
}

InputPort::InputPort ()
{
  crc_flag = FALSE;
  poly = NULL;
}

char InputPort::read_bit ()
{
  char c = input_bit();
  if (crc_flag && poly) poly->push_bit(c);  
  return c;
}

char InputPort::read_byte ()
{
  int i;
  int bit;
  char byte =  input_byte ();
  if (crc_flag && poly) {
    for (i=0; i < 8; i++) {
      bit = ((0x80 >> i) & byte);
      if (bit) bit = ONE; else bit = ZERO;
      poly->push_bit( bit);
    }
  }
  return byte;
}

int InputPort::read_pattern (char* pattern)
{
  // doesn't scan yet 
  int n = strlen(pattern);
  int i;
  char b[32];
  int result = TRUE;
  char str[128];
  
  i = 0;
  while (i < n)
    {
      b[i] = read_bit();
      if (b[i] != pattern[i])
	{
	  result = FALSE;
	}
      i++;
    }

  if (result == FALSE)
      {
	b[i] = '\0';
	sprintf(str,
		"(%s) does not match pattern (%s) InputPort::read_pattern",
		b, pattern);
	sys_message(str);
      }
      
  return result;
}

int InputPort::read_markerbit ()
{
  char c = read_bit();
  if (c == ONE)
    {
      return ONE;
    }
  else
    {
      sys_message("markerbit missed in InputPort::read_markerbit");
      return BADBIT;
    }
}

char InputPort::read_reserved_bits (int nbits)
// returns last bit read
{
  return read_nbits(nbits);
}

char InputPort::read_nbits (int n)
// returns last bit read
{
  char b;
  
  for (int i = 0; i < n; i++)
    {
      b = read_bit();
      if (b == BADBIT)
	{
	  sys_error("bad bit found\n");
	}

    }
  return b;
}

int InputPort::read_uimsbf(int nbits, char* message)
{
  int value = 0;
  int i = 0;
  char b;

  while (i < nbits)
    {
      b = read_bit();
      if (b == '1')
	value = 2 * value + 1;
      else if (b == '0')
	value = 2 * value;
      else
	{
          sys_message(message);
	  return value;
	}
      i++;
    }
  return value;
}

int power (int b, int n)
{
  if (n < 0)
    {
      sys_message("negative exponent in power - returning 0\n");
      return 0;
    }
  int res;
  res = 1;
  while (n > 0)
    {
      res = b * res;
      n--;
    }
  return res;
}

int InputPort::read_tcimsbf(int nbits, char* message)
{
  if (nbits > 8)
    {
      sys_message("can't read 2's comp with more than 8 bits; reading unsigned");
      return read_uimsbf(nbits, message);
    }
  else
    {
      int pos = read_uimsbf(nbits, message);
      int pow = power(2, nbits);
      if (pos > pow / 2)
	return -(pow - pos);
      else
        return pos;
    }
}

TimeStamp90* InputPort::read_timestamp90 ()
{
  int bit32 = read_uimsbf(1, "reading bit32");
  int bits30_31 = read_uimsbf(2, "reading bits 30..31");
  read_markerbit();
  int bits15_29 = read_uimsbf(15, "reading bits 15..29");
  read_markerbit();
  int bits0_14 = read_uimsbf(15, "reading bits 0..14");
  read_markerbit();
  return new TimeStamp90((bits30_31 << 30) + (bits15_29 << 15) + bits0_14,
			 bit32);
}

TimeStamp27* InputPort::read_timestamp27_pes_format ()
{
  int bit32 = read_uimsbf(1, "reading bit32");
  int bits30_31 = read_uimsbf(2, "reading bits 30..31");
  read_markerbit();
  int bits15_29 = read_uimsbf(15, "reading bits 15..29");
  read_markerbit();
  int bits0_14 = read_uimsbf(15, "reading bits 0..14");
  read_markerbit();
  int ext = read_uimsbf(9, "reading extension bits");
  read_markerbit();
  return new TimeStamp27((bits30_31 << 30) + (bits15_29 << 15) + bits0_14,
			 bit32,
			 ext);
}

TimeStamp27* InputPort::read_timestamp27_ts_format ()
{
  int bit32 = read_uimsbf(1, "reading bit32");
  int bits0_31 = read_uimsbf(32, "reading bits 0..31");
  read_reserved_bits(6);
  int ext = read_uimsbf(9, "reading extension bits");
  return new TimeStamp27(bits0_31, bit32, ext);
}

void InputPort::start_crc ()
{
  if (poly) delete poly;
  poly = new Poly();
  crc_flag = TRUE;
}

void InputPort::stop_crc ()
{
  crc_flag = FALSE;
}

int InputPort::check_crc ()
{
  // first turn off crc_flag
  stop_crc();
  char buf[32];
  for (int i = 31; i >=0; i--)
    {
      buf[i] = read_bit();
    }
  return poly->poly_cmp(buf);
}

void InputPort::print_crc ()
{
  if (!poly) return;
  printf("poly: ");
  poly->print_bits();
  printf("\n");
  printf("data: ");  
  for (int i = 0; i < 32; i++)
    {
      char c = input_bit();
      putc(c, stdout);
    }
  printf("\n");
}