⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stream.c

📁 h261协议的h261编码码,lib_261.h里面提供了详细的接口和说明.
💻 C
字号:
/*************************************************************
Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
Group. If you use this software, you agree to the following: This
program package is purely experimental, and is licensed "as is".
Permission is granted to use, modify, and distribute this program
without charge for any purpose, provided this license/ disclaimer
notice appears in the copies.  No warranty or maintenance is given,
either expressed or implied.  In no event shall the author(s) be
liable to you or a third party for any special, incidental,
consequential, or other damages, arising out of the use or inability
to use the program for any purpose (or the loss of data), even if we
have been advised of such possibilities.  Any public reference or
advertisement of this source code should refer to it as the Portable
Video Research Group (PVRG) code, and not by any author(s) (or
Stanford University) name.
*************************************************************/

/*
************************************************************
stream.c

This file handles all of the bit-level stream commands.

************************************************************
*/

/*LABEL stream.c */

#include <stdio.h>
#include "globals.h"

/*PUBLIC*/

//extern void mropen();
//extern void mrclose();
extern void mwopen_en();
extern void mwclose_en();
//extern int mgetb();
extern void mputv_en();
//extern int mgetv();
extern long mwtell_en();
//extern long mrtell();
//extern void mwseek();
//extern void mrseek();
//extern int seof();

/*PRIVATE*/

extern int ErrorValue;


//begin
extern unsigned char *data;
extern unsigned int	count;
static unsigned char *swout_data;
static unsigned char *srin_data;
static unsigned int r_count;
//end
//
static FILE *swout;
static FILE *srin;
static int current_write_byte;
static int current_read_byte;
static int read_position;
static int write_position;

int bit_set_mask[] =
{0x00000001,0x00000002,0x00000004,0x00000008,
0x00000010,0x00000020,0x00000040,0x00000080,
0x00000100,0x00000200,0x00000400,0x00000800,
0x00001000,0x00002000,0x00004000,0x00008000,
0x00010000,0x00020000,0x00040000,0x00080000,
0x00100000,0x00200000,0x00400000,0x00800000,
0x01000000,0x02000000,0x04000000,0x08000000,
0x10000000,0x20000000,0x40000000,0x80000000};

#define mput1()\
{current_write_byte|=bit_set_mask[write_position--];\
	if (write_position<0) {\
	swout_data[count]= current_write_byte; count++; \
	write_position=7;current_write_byte=0;}}

#define mput0()\
{write_position--;if(write_position<0){\
	swout_data[count]= current_write_byte; count++;\
	write_position=7;current_write_byte=0;}}


/*START*/

/*BFUNC

mropen() opens up the stream for reading on a bit level.

EFUNC*/

//void mropen(filename)
//     char *filename;
//void mropen(char *filename)
//{	
//  BEGIN("mropen");
//  current_read_byte=0;
//  read_position = -1;
//  //begin
//  srin_data = data;
//  r_count=0;
//  //end
//  //if ((srin = fopen(filename,"rb")) == NULL)
//  //  {
//  //    fprintf(stderr,"Cannot Read Input File\n");
//  //    exit(ERROR_INIT_FILE);
//  //  }
//}

/*BFUNC

mrclose() closes a read bit-stream.

EFUNC*/

//void mrclose()
//{
//  BEGIN("mrclose");
//
//  //fclose(srin);
//}

/*BFUNC

mwopen_en() opens a bit stream for writing.

EFUNC*/

//void mwopen_en(filename)
//     char *filename;
void mwopen_en(char *filename)
{
  BEGIN("mwopen_en");

  current_write_byte=0;
  write_position=7;

  //begin
  swout_data = &data[0];
  count = 0;
  //end
  //if ((swout = fopen(filename,"wb+")) == NULL)
  //  {
  //    WHEREAMI();
  //    printf("Cannot Open Output File\n");
  //    exit(ERROR_INIT_FILE);
  //  }
}


/*BFUNC

mwclose_en() closes the write bit-stream. It flushes the remaining byte
with ones, consistent with -1 returned on EOF.

EFUNC*/

void mwclose_en()
{
  BEGIN("mwclose_en");

  while(write_position!=7)
    {
      mput1();
    }
  //fclose(swout);
}

/*BFUNC

mputb_en() puts a bit to the write stream.

EFUNC*/

//void mputb_en(b)
//     int b;
void mputb_en(int b)
{
  BEGIN("mputb_en");

  if (b) {mput1();}

  else {mput0();} 
  //begin
  //end
}

/*BFUNC

mgetb() returns a bit from the read stream.

EFUNC*/

//int mgetb()
//{
//  BEGIN("mgetb");
//  //fprintf(fp,"read_position = %d\tcurrent_read_bye=%d\n",read_position,current_read_byte);
//  
//  if (read_position<0)
//    {
//		//if(current_read_byte==231)
//		//	printf("\n");
//      //current_read_byte=getc(srin);
//	  //begin
//	  current_read_byte = srin_data[r_count];
//	  r_count++;
//	  //end
//      read_position = 7;
//	  if(current_read_byte<0) 
//		  printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
//    }
//  if (current_read_byte&bit_set_mask[read_position--]) {return(1);}
//  return(0);
//}

/*BFUNC

mputv_en() puts a n bits to the stream from byte b.

EFUNC*/

//void mputv_en(n,b)
//     int n;
//     int b;
void mputv_en(int n,int b)
{
  BEGIN("mputv_en");

  while(n--)
    {
      if(b&bit_set_mask[n]) 
	  {
		  mput1();
		  //begin
		  //current_write_byte|=bit_set_mask[write_position--];
		  //if (write_position<0)
		  //{			 
			 // swout_data[count]= current_write_byte;
			 // count++; 
			 // write_position=7;
			 // current_write_byte=0;
		  //}
		  //end
	  }
      else 
	  {
		  mput0();
		  //begin
		  //write_position--;
		  //if(write_position<0)
		  //{
			 // swout_data[count]= current_write_byte;
			 // count++;
			 // write_position=7;
			 // current_write_byte=0;
		  //}
		  //end
	  }
    }
}

/*BFUNC

mgetv() returns n bits read off of the read stream.

EFUNC*/

//int mgetv(n)
//     int n;
//int mgetv(int n)
//{
//  BEGIN("mgetv");
//  int b=0;
//
//  while(n--)
//    {
//      b <<= 1;
//      if (mgetb()) {b |= 1;}
//    }
//  return(b);
//}
//

/*BFUNC

mwtell_en() returns the position in bits of the write stream.

EFUNC*/

long mwtell_en()
{
  BEGIN("mwtell_en");

  //return((ftell(swout)<<3) + (7 - write_position));
  //begin
  return( (count<<3) + (7 - write_position) );
  //end
}

/*BFUNC

mrtell() returns the position in read bits of the read stream.

EFUNC*/

//long mrtell()
//{
//  BEGIN("mrtell");
//
//  //return((ftell(srin)<<3) - (read_position+1));
//  return ((r_count<<3) - (read_position+1));
//}

/*BFUNC

mwseek() seeks to a specific bit position on the write stream.

EFUNC*/

//void mwseek(distance)
//     long distance;
//void mwseek(long distance)
//{
//	BEGIN("mwseek");
//	int Length;
//    
//	if (write_position != 7) {putc(current_write_byte,swout);}
//	fseek(swout,0,2L);
//	Length = ftell(swout);
//	fseek(swout,(distance+7)>>3,0L);
//	if ((Length << 3) <= distance) /* Make sure we read clean stuff */
//    {
//		current_write_byte = 0;
//		write_position = 7 - (distance & 0x7);
//	}
//	else
//    {
//		current_write_byte = getc(swout);
//		write_position = 7 - (distance & 0x7);
//		fseek(swout,(distance+7)>>3,0L);
//    }
//}
//

/*BFUNC

mrseek() seeks to a specific bit position on the read stream.

EFUNC*/

//void mrseek(distance)
//     long distance;
//void mrseek(long distance)
//{
//  BEGIN("mrseek");
//
//  fseek(srin,distance>>3,0L);
//  current_read_byte = getc(srin);
//  read_position = 7 - (distance % 8);
//}
//

/*BFUNC

seof() returns a -1 if at the end of stream. It mimics the {\tt feof}
command.

EFUNC*/

//int seof()
//{
//  BEGIN("seof");
//
//  //begin
//  return (r_count < count ? 0 : 1);
//  //end
//  //return(feof(srin));
//}

/*END*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -