www.pudn.com > MPEG2systemsrc.rar > OutputPort.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.
*/

/* OutputPort implementation */

#include "Utilities.H"
#include "OutputPort.H"

extern "C"
{
#include 
}

OutputPort::OutputPort (int tf)
{
  textflag = tf;
  crc_flag = FALSE;
  poly = NULL;
}

void OutputPort::write_bit (char c)
{
  output_bit(c);
  if (crc_flag && poly) poly->push_bit(c);
}

void OutputPort::write_byte (char c)
{
  for (int i = 7; i >= 0; i--)
    output_bit((c & (1 << i)) ? '1' : '0');
  if (poly) sys_message("write_byte called with crc on");
}

void OutputPort::flush ()
{
  output_flush();
}

void OutputPort::write_pattern (char* pattern)
{
  int n = strlen(pattern);

  for (int i = 0; i < n; i++)
    {
      write_bit(pattern[i]);
    }
}

void OutputPort::write_uimsbf(int value, int nbits)
{
  char rep[nbits];
  int i;

  for (i = 0; i < nbits; i++)
    {

      rep[i] = ((((int) value / 2) * 2) == value) ? '0' : '1';
      value = value / 2;
    }
  
  for (i = nbits-1; i >=0; i--)
    {
      (rep[i] == '0') ? write_bit('0') : write_bit('1');
    }
}

void OutputPort::write_tcimsbf(int value, int nbits)
{
  if (value >= 0)
    {
      write_uimsbf(value, nbits);
      return;
    }
  else
    {
      int vcomp = - value;
      char rep[nbits];
      int i;

      // calculate 1's comp of base 2 rep for -value
      for (i = 0; i < nbits; i++)
	{
	  rep[i] = ((((int) vcomp / 2) * 2) == vcomp) ? '1' : '0';
	  vcomp = vcomp / 2;
	}
      // add 1
      for (i = 0; i < nbits; i++)
	{
	  if (rep[i] == '1')
	    {
	      rep[i] = '0';
	    }
	  else
	    {
	      rep[i] = '1';
	      break;
	    }
	}
      // output bits
      for (i = nbits-1; i >=0; i--)
	{
	  (rep[i] == '0') ? write_bit('0') : write_bit('1');
	}
    }

}

void OutputPort::write_markerbit ()
{
  write_bit('1');
}

void OutputPort::write_reserved_bits (int nbits)
{
  for (int i = 0; i < nbits; i++)
    {
      write_bit('1');
    }
}

void OutputPort::write_timestamp90 (TimeStamp90* ts)
{
  write_uimsbf(ts->bit32, 1);
  write_uimsbf(ts->bits0_31 >> 30, 2);
  write_markerbit();
  write_uimsbf(ts->bits0_31 >> 15, 15);
  write_markerbit();
  write_uimsbf(ts->bits0_31, 15);
  write_markerbit();  
}

void OutputPort::write_timestamp27_pes_format (TimeStamp27* ts)
{
  write_uimsbf(ts->bit32, 1);
  write_uimsbf(ts->bits0_31 >> 30, 2);
  write_markerbit();
  write_uimsbf(ts->bits0_31 >> 15, 15);
  write_markerbit();
  write_uimsbf(ts->bits0_31, 15);
  write_markerbit();
  write_uimsbf(ts->ext, 9);  
  write_markerbit();
}

void OutputPort::write_timestamp27_ts_format (TimeStamp27* ts)
{
  write_uimsbf(ts->bit32, 1);
  write_uimsbf(ts->bits0_31, 32);
  write_reserved_bits(6);
  write_uimsbf(ts->ext, 9);  
}

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

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

void OutputPort::write_crc ()
{
  // write the complement of the crc
  for (int i = 31; i >= 0 ; --i)
    {
      if (poly->shift_reg[i])
	{
	  write_bit('0');
	}
      else
	{
	  write_bit('1');     
	}
    }
}