📄 p642.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.
*************************************************************/
/*
************************************************************
p64.c
This is the file that has the "main" routine and all of the associated
high-level routines. This has been kludged from the JPEG encoder, so
there is more extensibility than is really necessary.
************************************************************
*/
/*LABEL p64.c */
#include <stdio.h>
#include "globals2.h"
#include "p642.h"
#include <string.h>
#include <stdlib.h>
/*PUBLIC*/
//extern void h261Decoder2(unsigned char * _data, unsigned char * _yuv);
//extern void h261Encoder(unsigned char * _yuv, unsigned char * _data);
//extern void p64EncodeSequence_en();
//extern void p64EncodeFrame_en();
//extern void p64EncodeGOB_en();
extern void p64DecodeSequence2();
extern int p64DecodeGOB2();
//extern void PrintImage();
//extern void PrintFrame();
extern void MakeImage2();
extern void MakeFrame2();
extern void MakeFstore2();
//extern void MakeStat_en();
//extern void MakeRate_en();
extern void SetCCITT2();
//extern void Help();
extern void MakeFileNames2();
//extern void VerifyFiles_en();
//static void ExecuteQuantization_en();
//static void CallOracle();
//static void p64EncodeMDU_en();
//static void ReadCompressMDU_en();
//static void WriteMDU_en();
static void DecodeSaveMDU2();
static int DecompressMDU2();
/*PRIVATE*/
//#define SwapFS_en(fs1,fs2) {FSTORE *ftemp;ftemp = fs1;fs1 = fs2;fs2 = ftemp;}
IMAGE *CImage2=NULL;
FRAME *CFrame2=NULL;
FSTORE *CFS2=NULL;
FSTORE *OFS2=NULL;
STAT *CStat2=NULL;
STAT *RStat2=NULL;
RATE *RCStore2=NULL;
int BlockJ2[] = {0,0,0,0,1,2};
int BlockV2[] = {0,0,1,1,0,0};
int BlockH2[] = {0,1,0,1,0,0};
char *DefaultSuffix2[]={".Y",".U",".V"};
/* CCITT p*64 Marker information */
int TemporalReference2=1;
int TemporalOffset2=0;
int PType2=0x0;
int Type22=0x0;
int MType2=0x0;
int GQuant2=8;
int MQuant2=8;
int MVDH2=0;
int MVDV2=0;
int VAR2=0;
int VAROR2=0;
int MWOR2=0;
int LastMVDV2=0;
int LastMVDH2=0;
int CBP2=0x3f;
int ParityEnable2=0;
int PSpareEnable2=0;
int GSpareEnable2=0;
int Parity2=0;
int PSpare2=0;
int GSpare2=0;
int GRead2=0;
int MBA2=0;
int LastMBA2=0;
int LastMType2=0; /* Last MType2 */
/* System Definitions */
//int ImageType2=IT_NTSC;
int ImageType2=IT_QCIF;
//int ImageType2=IT_QCIF;
int NumberMDU2=0;
int CurrentMDU2=0;
int NumberGOB2=0;
int CurrentGOB2=0;
int CurrentFrame2=0;
int StartFrame2=0;
int LastFrame2=0;
int PreviousFrame2=0;
int NumberFrames2=0;
int TransmittedFrames2=0;
int FrameRate2=30;
int FrameSkip2=1;
/* Stuff for RateControl */
int FileSizeBits2=0;
int Rate2=0;
int BufferOffset2=0; /*Number of bits assumed for initial buffer. */
int QDFact2=1;
int QOffs2=1;
int QUpdateFrequency2=11;
int QUse2=0;
int QSum2=0;
/* Some internal parameters for rate control */
#define DEFAULT_QUANTIZATION 8
int InitialQuant2=0;
/* DCT Coefficient Thresholds for coding blocks */
int CBPThreshold2=1; /* abs threshold before we use CBP2 */
/* MC Threshold for coding blocks through filter*/
#define D_FILTERTHRESHOLD 6
/* Intra forced every so many blocks */
#define SEQUENCE_INTRA_THRESHOLD 131
/* Parser stuff */
extern double Memory2[];
/* Stuff for encoding (temporary integer storage) */
static int inputbuf2[10][64];
static int output2[64];
/* Stuff for Motion Compensation */
extern int SearchLimit2;
extern int bit_set_mask2[];
extern int MeX2[];
extern int MeY2[];
extern int MeVal2[];
extern int MeOVal2[];
extern int MeVAR2[1024];
extern int MeVAROR2[1024];
extern int MeMWOR2[1024];
/* Book-keeping stuff */
int ErrorValue2=0;
int Loud2=MUTE;
//int Trace2=NULL;
int Trace2 = 0;
int Verbose2=0;
int ForceCIF2=0; /* Forces CIF format - superset will hold any other format */
int Oracle2=0;
int MQuantEnable2;
int UseQuant2;
int OracleGQuant2,OracleMQuant2;
int OracleMQuantEnable2,OracleMType2;
/* Statistics_en */
int NumberNZ2=0;
int FirstFrameBits2=0;
int NumberOvfl2=0;
extern int MotionVectorBits2;
extern int MacroAttributeBits2;
extern int CodedBlockBits2;
int YCoefBits2=0;
int UCoefBits2=0;
int VCoefBits2=0;
extern int EOBBits2;
int TotalBits2,LastBits2;
int MacroTypeFrequency2[10];
int YTypeFrequency2[10];
int UVTypeFrequency2[10];
unsigned char **LastIntra2; /* Used for intra forcing (once per 132 frames) */
/* Coding control */
int QuantMType2[] = {0,1,0,1,0,0,1,0,0,1}; /* Quantization used */
int CBPMType2[] = {0,0,1,1,0,1,1,0,1,1}; /* CBP2 used in coding */
int IntraMType2[] = {1,1,0,0,0,0,0,0,0,0}; /* Intra coded macroblock */
int MFMType2[] = {0,0,0,0,1,1,1,1,1,1}; /* Motion forward vector used */
int FilterMType2[] = {0,0,0,0,0,0,0,1,1,1}; /* Filter flags */
int TCoeffMType2[] = {1,1,1,1,0,1,1,0,1,1}; /* Transform coeff. coded */
/* DCT Stuff */
/* Functional Declarations */
//vFunc *UseDct2 = ChenDct_en;
vFunc *UseIDct2 = ChenIDct2;
//#define DefaultDct_en (*UseDct2)
#define DefaultIDct2 (*UseIDct2)
//#define BufferContents_en() (mwtell_en() + BufferOffset2 -\
// (((CurrentGOB2*NumberMDU2)+CurrentMDU2)\
// *Rate2*FrameSkip2\
// /(NumberGOB2*NumberMDU2*FrameRate2)))
//#define BufferSize_en() (Rate2/4) /*In bits */
//bengin
unsigned char *yuv2=NULL;
unsigned char *data2;
unsigned int count2=0;
////end
/*START*/
/*BFUNC
main() is the first routine called by program activation. It parses
the input command line and sets parameters accordingly.
EFUNC*/
//int p64_main(int argc,char **argv)
//{
// BEGIN("main");//static char RoutineName[]= "main"
// int i,p,s;
//
// char str[]="h:\\short.p64";
//
// MakeImage2(); /* Initialize storage */
// MakeFrame2();
// MakeFstore2();
// inithuff2(); /* Put Huffman tables on */
// initmc_en(); /* Put Motion Comp stuff on */
//
// //fp=fopen("h:\\log.txt","w");
// //if(fp==NULL) printf("Error\n");
//
// if (argc==2)
// {//解码
// //beginmy
// CImage2->p64Mode |= P_DECODER;//-d
// CImage2->StreamFileName = str;//-s
// strcpy(CFrame2->ComponentFilePrefix[0],"short");
// //ImageType2 = IT_QCIF;
// //PType2=0x00;
// //endmy
// //Help();
// //exit(-1);
// }
// else if(argc == 1)
// {//编码
// CurrentFrame2 = 0;
// StartFrame2 = CurrentFrame2;
// LastFrame2 = 6;
// strcpy(CFrame2->ComponentFilePrefix[0],"short");
// CImage2->StreamFileName = "qjz.p64";
// //ImageType2 = IT_QCIF;
// //PType2=0x00;
//
// }
// else
// {
// for(s=0,p=0,i=1;i<argc;i++)
// {
// if (!strcmp("-NTSC",argv[i]))
// {
// ImageType2 = IT_NTSC;
// }
// else if (!strcmp("-CIF",argv[i]))
// {
// ImageType2 = IT_CIF;
// }
// else if (!strcmp("-QCIF",argv[i]))
// {
// ImageType2 = IT_QCIF;
// }
// else if (*(argv[i]) == '-')
// {
// switch(*(++argv[i]))
// {
// case 'a':
// CurrentFrame2 = atoi(argv[++i]);
// StartFrame2=CurrentFrame2;
// break;
// case 'b':
// LastFrame2 = atoi(argv[++i]);
// break;
// case 'c':
// ForceCIF2=1;
// break;
// case 'd':
// CImage2->p64Mode |= P_DECODER;
// break;
// case 'f':
// FrameRate2 = atoi(argv[++i]);
// break;
// case 'i':
// SearchLimit2 = atoi(argv[++i]);
// BoundValue(SearchLimit2,1,31,"SearchLimit2");
// break;
// case 'k':
// FrameSkip2 = atoi(argv[++i]);
// break;
// case 'l':
// Loud2 = atoi(argv[++i]);
// break;
// case 'o':
// Oracle2=1;
// break;
// case 'p':
// ParityEnable2=1;
// break;
// case 'q':
// InitialQuant2=atoi(argv[++i]);
// BoundValue(InitialQuant2,1,31,"InitialQuant2");
// break;
// case 'r':
// Rate2 = (atoi(argv[++i]));
// break;
// case 's':
// CImage2->StreamFileName = argv[++i];
// break;
// case 'v':
// Verbose2=1;
// break;
// case 'x':
// FileSizeBits2 = (atoi(argv[++i]));
// break;
// case 'y':
// UseDct2 = ReferenceDct;
// UseIDct2 = ReferenceIDct;
// break;
// case 'z':
// strcpy(CFrame2->ComponentFileSuffix[s++],argv[++i]);
// break;
// default:
// WHEREAMI();
// printf("Illegal Option %c\n",*argv[i]);
// exit(ERROR_BOUNDS);
// break;
// }
// }
// else
// {
// strcpy(CFrame2->ComponentFilePrefix[p++],argv[i]);
// }
// }//end of for
//
// }
//
//
//
//
// if (!CImage2->StreamFileName)
// {
// if (!(CImage2->StreamFileName =(char *) calloc(strlen(CFrame2->ComponentFilePrefix[0])+6, sizeof(char))))
// {
// WHEREAMI();
// printf("Cannot allocate string for StreamFileName.\n");
// exit(ERROR_MEMORY);
// }
// sprintf(CImage2->StreamFileName,"%s.p64",CFrame2->ComponentFilePrefix[0]);
// }
// if (Oracle2)
// {
// initparser();
// parser();
// }
// switch(ImageType2)
// {
// case IT_NTSC:
// PType2=0x04;
// PSpareEnable2=1;
// PSpare2=0x8c;
// break;
// case IT_CIF:
// PType2=0x04;
// break;
// case IT_QCIF:
// PType2=0x00;//ImageType2 = IT_QCIF;
// break;
// default:
// WHEREAMI();
// printf("Image Type not supported: %d\n",ImageType2);
// break;
// }
// if(!(GetFlag(CImage2->p64Mode,P_DECODER)))
// {//编码
// SetCCITT2();
// if (CurrentFrame2>LastFrame2)
// {
// WHEREAMI();
// printf("Need positive number of frames.\n");
// exit(ERROR_BOUNDS);
// }
// NumberFrames2 = LastFrame2-CurrentFrame2+1;
// p64EncodeSequence_en();
// }
// else
// {//解码
// p64DecodeSequence2();
// }
// exit(ErrorValue2);
//}
/*BFUNC
ExecuteQuantization_en() is used to find the appropriate quantization for
the current sequence. It calls the oracle if the flag is set.
EFUNC*/
//
//static void ExecuteQuantization_en()
//{
// BEGIN("ExecuteQuantization_en");
// int CurrentSize;
//
// CurrentSize=BufferContents_en();
// OracleGQuant2 = (CurrentSize/QDFact2) + QOffs2;
// OracleMQuant2=OracleGQuant2;
// if (Verbose2)
// {
// printf("BufferContents_en: %d New Q1: %d\n",
// CurrentSize,GQuant2);
// }
// if (Oracle2) /* If oracle, then consult oracle */
// {
// CallOracle(1);
// OracleGQuant2 = (int) Memory2[L_GQUANT];
// OracleMQuant2 = (int) Memory2[L_MQUANT];
// }
// if (OracleGQuant2<1) OracleGQuant2=1;
// if (OracleGQuant2>31) OracleGQuant2=31;
// if (OracleMQuant2<1) OracleMQuant2=1;
// if (OracleMQuant2>31) OracleMQuant2=31;
//}
/*BFUNC
CallOracle() calls the program interpreter by setting all the relevant
parameters and executing the program designated by value.
EFUNC*/
//
//static void CallOracle(int value)
//{
// BEGIN("CallOracle");
//
// Memory2[L_GQUANT] = (double) GQuant2;
// Memory2[L_MQUANT] = (double) MQuant2;
// Memory2[L_MQUANTENABLE] = (double) MQuantEnable2;
// Memory2[L_MTYPE] = (double) MType2; /* Suggested MType2 */
// Memory2[L_BD] = (double) MeOVal2[Bpos_en(CurrentGOB2,
// CurrentMDU2,0,0)];
// Memory2[L_DBD] = (double) MeVal2[Bpos_en(CurrentGOB2,
// CurrentMDU2,0,0)];
// Memory2[L_VAROR] = (double) MeVAROR2[Bpos_en(CurrentGOB2,
// CurrentMDU2,0,0)];
// Memory2[L_VAR] = (double) MeVAR2[Bpos_en(CurrentGOB2,
// CurrentMDU2,0,0)];
// Memory2[L_MWOR] = (double) MeMWOR2[Bpos_en(CurrentGOB2,
// CurrentMDU2,0,0)];
// Memory2[L_RATE] = (double) Rate2;
// Memory2[L_BUFFERSIZE] = (double) BufferSize_en();
// Memory2[L_BUFFERCONTENTS] = (double) BufferContents_en();
// Memory2[L_QDFACT] = (double) QDFact2;
// Memory2[L_QOFFS] = (double) QOffs2;
// Execute(value);
//}
/*BFUNC
p64EncodeSequence_en() encodes the sequence defined by the CImage2 and
CFrame2 structures.
EFUNC*/
//void p64EncodeSequence_en()
//{
// BEGIN("p64EncodeSequence_en");
//
// MakeStat_en();
// MakeRate_en();
// MakeIob2(READ_IOB);
// InitFS2(CFS2);
// ClearFS2(CFS2);
// InitFS2(OFS2);
// ClearFS2(OFS2);
// //swopen_en(CImage2->StreamFileName);
// swopen_en(" ");
// if (Loud2 > MUTE)
// {
// PrintImage();
// PrintFrame();
// }
// if (FileSizeBits2) /* Rate2 is determined by bits/second. */
// Rate2=(FileSizeBits2*FrameRate2)/(FrameSkip2*(LastFrame2-CurrentFrame2+1));
// if (Rate2)
// {
// QDFact2 = (Rate2/320);
// QOffs2 = 1;
// if (!InitialQuant2)
// {
// InitialQuant2 = 10000000/Rate2;
// if (InitialQuant2>31) InitialQuant2=31;
// else if (InitialQuant2<1) InitialQuant2=1;
// printf("Rate2: %d QDFact2: %d QOffs2: %d\n",Rate2,QDFact2,QOffs2);
// printf("Starting Quantization: %d\n",InitialQuant2);
// }
// }
// if (!InitialQuant2)
// InitialQuant2=DEFAULT_QUANTIZATION;
// GQuant2=MQuant2=InitialQuant2;
// BufferOffset2=0;
// TotalBits2=0;
// NumberOvfl2=0;
// FirstFrameBits2=0;
// printf("START>SEQUENCE\n");
// TransmittedFrames2=0;
// while(CurrentFrame2 <= LastFrame2)
// {
// p64EncodeFrame_en();
// }
// /*limit file growth*/
// if (CurrentFrame2>LastFrame2+1)
// {
// CurrentFrame2=LastFrame2+1;
// }
// TemporalReference2 = CurrentFrame2 % 32;
// WritePictureHeader_en();
// swclose_en();
// /*
// SaveMem(CFS2->fs[0]->mem,"XX");
// SaveMem(OFS2->fs[0]->mem,"YY");
// */
// printf("END>SEQUENCE\n");
// printf("Bits for first frame: %d Number of buffer overflows: %d\n",FirstFrameBits2,NumberOvfl2);
//}
/*BFUNC
p64EncodeFrame_en() encodes a single image frame.
EFUNC*/
//void p64EncodeFrame_en()
//{
// BEGIN("p64EncodeFrame_en");
// int x;
//
// printf("START>Frame: %d\n",CurrentFrame2);
// MakeFileNames2();
// VerifyFiles_en();
// ReadIob_en();
// InstallFS2(0,CFS2);
// if (CurrentFrame2!=StartFrame2)
// GlobalMC_en();
// TemporalReference2 = CurrentFrame2 % 32;
// WritePictureHeader_en();
//
// for(x=0;x<10;x++) /* Initialize Statistics_en */
// {
// MacroTypeFrequency2[x]=0;
// YTypeFrequency2[x]=0;
// UVTypeFrequency2[x]=0;
// }
// MotionVectorBits2=MacroAttributeBits2=0;
// YCoefBits2=UCoefBits2=VCoefBits2=EOBBits2=0;
// QUse2=QSum2=0;
// NumberNZ2=0;
//
// for(CurrentGOB2=0;CurrentGOB2<NumberGOB2;CurrentMDU2=0,CurrentGOB2++)
// p64EncodeGOB_en();
//
// RCStore2[CurrentFrame2-StartFrame2].position=TotalBits2;
// RCStore2[CurrentFrame2-StartFrame2].baseq = GQuant2;
// x = mwtell_en();
// LastBits2 = x - TotalBits2;
// TotalBits2 = x;
// printf("Total No of Bits: %8d Bits for Frame: %8d\n",
// TotalBits2,LastBits2);
// if (Rate2)
// {
// printf("Buffer Contents: %8d out of: %8d\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -