📄 sim.h
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include <windows.h>
#include <io.h>
#include<math.h>
#include "config.h"
#include"macros.h"
/* This should not be changed */
#define MB_SIZE 16
#define MBR 144
#define MBC 176
/* If someone forgets to define a coding format, QCIF is default */
#if (!defined SQCIF && !defined QCIF && !defined CIF && !defined CIF4 && !defined CIF16)
#define QCIF
#endif
/* The coding format is decided here. */
/* Parameters from TMN */
#define PREF_NULL_VEC 100
#define PREF_16_VEC 200
#define PREF_PBDELTA_NULL_VEC 50
int headerlength; /* Global variables */
int widthA , heightA , widthA2 ;
int user_lines , user_pels ;
int pels;
int cpels;
int lines;
int mbc;
int mbr;
long lsize;
long csize;
int advanced;
int syntax_arith_coding;
float target_framerate;
long bit_count;
int qbase[384];
int MAX_MVx , MAX_MVy;
int gMAX_MVx , gMAX_MVy;
/****************************/
#define PSC 1
#define PSC_LENGTH 17
#define ESCAPE 7167
#define PCT_INTER 1
#define PCT_INTRA 0
#define ON 1
#define OFF 0
#define SF_SQCIF 1 /* 001 */
#define SF_QCIF 2 /* 010 */
#define SF_CIF 3 /* 011 */
#define SF_4CIF 4 /* 100 */
#define SF_16CIF 5 /* 101 */
#define MODE_INTER 0
#define MODE_INTER_Q 1
#define MODE_INTER4V 2
#define MODE_INTRA 3
#define MODE_INTRA_Q 4
#define MODE_INTER0 5
#define PBMODE_NORMAL 0
#define PBMODE_MVDB 1
#define PBMODE_CBPB_MVDB 2
#define NO_VEC 999
/* Motionvector structure */
typedef struct motionvector {
int x; /* Horizontal comp. of mv */
int y; /* Vertical comp. of mv */
int x_half; /* Horizontal half-pel acc. */
int y_half; /* Vertical half-pel acc. */
int min_error; /* Min error for this vector */
int Mode; /* Necessary for adv. pred. mode */
} MotionVector;
/* Point structure */
typedef struct point {
int x;
int y;
} Point;
/* Group of pictures structure. */
typedef struct pict_image {
unsigned char _huge *lum; /* Luminance plane */
unsigned char _huge *Cr; /* Cr plane */
unsigned char _huge *Cb; /* Cb plane */
} PictImage;
/* Picture structure */
typedef struct pict {
int prev;
int curr;
int TR; /* Time reference */
int bit_rate;
int src_frame_rate;
float target_frame_rate;
int source_format;
int picture_coding_type;
int spare;
int unrestricted_mv_mode;
int PB;
int QUANT;
int DQUANT;
int MB;
int seek_dist; /* Motion vector search window */
int use_gobsync; /* flag for gob_sync */
int MODB; /* B-frame mode */
int BQUANT; /* which quantizer to use for B-MBs in PB-frame */
int TRB; /* Time reference for B-picture */
float QP_mean; /* mean quantizer */
} Pict;
/* Slice structure */
typedef struct slice {
unsigned int vert_pos; /* Vertical position of slice */
unsigned int quant_scale; /* Quantization scale */
} Slice;
/* Macroblock structure */
typedef struct macroblock {
int mb_address; /* Macroblock address */
int macroblock_type; /* Macroblock type */
int skipped; /* 1 if skipped */
MotionVector motion; /* Motion Vector */
} Macroblock;
/* Structure for macroblock data */
typedef struct mb_structure
{
int lum[16][16];
int Cr[8][8];
int Cb[8][8];
} MB_Structure;
/* Structure for counted bits */
typedef struct bits_counted
{
long Y;
long C;
int vec;
int CBPY;
int CBPCM;
int MODB;
int CBPB;
int COD;
int header;
int DQUANT;
long total;
int no_inter;
int no_inter4v;
int no_intra;
/* NB: Remember to change AddBits(), ZeroBits() and AddBitsPicture()
when entries are added here */
} Bits;
/* Structure for data for data from previous macroblock */
/* Structure for average results and virtal buffer data */
typedef struct results {
float SNR_l; /* SNR for luminance */
float SNR_Cr; /* SNR for chrominance */
float SNR_Cb;
float QP_mean; /* Mean quantizer */
} Results;
HGLOBAL hStreamBuf ;
unsigned char * InternalEncoderOutput ;
unsigned char * EncoderRGBInput ;
HGLOBAL hCurr_image, hRecon_image, hPr_edge, hBitBuf, hMV , hCurrRGB;
PictImage curr_image, recon_image, pr_edge, prev_recon;
char _huge *bit_buf;
MotionVector *pMV[6][MBR+1][MBC+2];
int B;
MB_Structure *recon_data_P ;
MB_Structure *diff ;
HFILE hStreamfile , hOut;
float DelayBetweenFramesInSeconds;
int CommBacklog;
float mean_frame_rate, ref_frame_rate, frame_rate, seconds;
int total_frames_passed,first_frameskip, orig_frameskip, chosen_frameskip, targetrate;
int start, end, frames, frame_no, frameskip;
int QP,QPI , ChangableQP, ChangableQP;
Bits *bits;
Bits *total_bits ;
Bits *intra_bits ;
Results *res ;
Results *total_res ;
Pict *pic ;
int VersionNum ;
void Help();
void PrintSNR(Results *res, int num);
void PrintResult(Bits *bits, int num_units, int num);
unsigned char *ReadImage(char *filename, int frame_no, int headerlength);
void WriteImage(PictImage *image, HFILE);
void FreeImage(PictImage *image);
void MB_Encode(MB_Structure *mb_orig, int QP, int I);
void MB_Decode(int *qcoeff, MB_Structure *mb_recon, int QP, int I);
void dct( int *block);
void FillBlock( int x, int y, PictImage *image, MB_Structure *data);
void ReconImage (int i, int j, MB_Structure *data, PictImage *recon);
void CodeOneOrTwo(PictImage _huge *curr, PictImage _huge *prev_recon, int QP, int frameskip, Bits _huge *bits,
Pict _huge *pic);
void CodeOneIntra(PictImage *curr, PictImage *recon, int QP, Bits *bits, Pict *pic);
void Dequant(int *qcoeff, int *rcoeff, int QP, int I);
void Quant(int *coeff, int *qcoeff, int QP, int I);
void CountBitsCoeff(int *qcoeff, int I, int CBP, Bits *bits, int ncoeffs);
int CodeCoeff(int Mode, int *qcoeff, int block, int ncoeffs);
int FindCBP(int *qcoeff, int Mode, int ncoeffs);
void CountBitsVectors(MotionVector *MV[5][MBR+1][MBC+2], Bits *bits,
int i, int j, int Mode, int newgob, Pict *pic);
void FindPMV(MotionVector *MV[5][MBR+1][MBC+2], int x, int y,
int *p0, int *p1, int block, int newgob, int half_pel);
void ZeroBits(Bits *bits);
void ZeroRes(Results *res);
void ZeroVec(MotionVector *MV);
void MarkVec(MotionVector *MV);
void CopyVec(MotionVector *MV1, MotionVector *MV2);
int EqualVec(MotionVector *MV2, MotionVector *MV1);
void AddBits(Bits *total, Bits *bits);
void AddRes(Results *total, Results *res, Pict *pic);
void AddBitsPicture(Bits *bits);
void FindMB(int x, int y, unsigned char _huge *image, unsigned char MB[16][16]);
MB_Structure *MB_Recon(PictImage *prev_recon, MB_Structure *diff,
int x_curr, int y_curr, MotionVector *MV);
extern _inline unsigned char InterpolatePel(unsigned char _huge *image,int xcurr , int ycurr );
void MotionEstimatePicture(unsigned char *curr_mb, unsigned char _huge *prev,
int seek_dist,MotionVector *MV[5][MBR+1][MBC+2]);
void MotionEstimation(unsigned char _huge *curr, unsigned char _huge *prev, int x_curr,
int y_curr, int xoff, int yoff, int seek_dist,
MotionVector *MV[5][MBR+1][MBC+2], int *sad_0 );
unsigned char *LoadArea(unsigned char _huge *im, int x, int y,
int x_size, int y_size, int lx);
int SAD_Macroblock(unsigned char _huge *ii, unsigned char _huge *act_block,
int h_length, int Min_FRAME, int type );
int SAD_Block(unsigned char _huge *ii, unsigned char _huge *act_block,
int h_length, int min_sofar);
MB_Structure *Predict_P(PictImage *curr_image, PictImage *prev_image,
unsigned char _huge *prev_ipol,int x_curr, int y_curr,
MotionVector *fr[5][MBR+1][MBC+2]);
void Clip(MB_Structure *data);
void DoPredChrom_P(int x_curr, int y_curr, int dx, int dy,
PictImage *curr, PictImage *prev,
MB_Structure *pred_error);
void FindHalfPel(MotionVector *, unsigned char *,unsigned char *, int, int);
void FindPred(int x, int y, MotionVector *fr, unsigned char _huge *prev,
int *pred, int bs, int comp);
void FindPredOBMC(unsigned char _huge *image_edge,int x, int y, MotionVector *MV[5][MBR+1][MBC+2],
int *pred, int comp);
MB_Structure *MB_Recon_P(PictImage *prev_image, unsigned char _huge *prev_ipol,
MB_Structure *diff, int x_curr, int y_curr,
MotionVector *MV[5][MBR+1][MBC+2]);
void MB_Pred(unsigned char _huge *pre_edge,PictImage *prev_image,
MB_Structure *recon_data, int x_curr, int y_curr,
MotionVector *MV[6][MBR+1][MBC+2]);
void ReconLumBlock_P(int x, int y, MotionVector *fr,
unsigned char _huge *prev, int *data,int bs,int comp);
void ReconChromBlock_P(int x_curr, int y_curr, int dx, int dy,
PictImage *prev, MB_Structure *data);
void FindChromBlock_P(int x_curr, int y_curr, int dx, int dy,
PictImage *prev, MB_Structure *data);
void ComputeSNR(PictImage *im1, PictImage *im2, Results *res, int write);
void ZeroMBlock(MB_Structure *data);
_inline void DiffMB(MB_Structure *diff, MB_Structure *orig, int symbol);
int CountBitsPicture(Pict *pic);
void CountBitsMB(int Mode, int COD, int CBP, int CBPB, Pict *pic, Bits *bits);
int CountBitsSlice(int slice, int quant);
int ChooseMode(unsigned char *curr, int min_SAD);
int ModifyMode(int Mode, int dquant);
void MakeEdgeImage(unsigned char _huge *src, unsigned char _huge *dst, int width,
int height, int edge);
void error_handler(char *);
/* The following routine is added for Rate Control */
#ifdef OFFLINE_RATE_CONTROL
int FrameUpdateQP(int buf, int bits, int frames_left, int QP, int B,
float seconds);
#else
int UpdateQuantizer(int mb, float QP_mean, int pict_type, float bit_rate,
int mb_width, int mb_height, int bitcount) ;
int InitializeQuantizer(int pict_type, float bit_rate,
float target_frame_rate, float QP_mean);
void InitializeRateControl();
void UpdateRateControl(int bits);
#endif
/* global declarations for idctref */
void init_idctref (void);
void idctref (int *coeff);
void init_idct (void);
void idct(int *coeff);
/* Syntax based arithmetic coding routines */
void Count_sac_BitsCoeff(int *qcoeff, int I, int CBP, Bits *bits, int ncoeffs);
int Code_sac_Coeff(int Mode, int *qcoeff, int block, int ncoeffs);
int CodeTCoef(int mod_index, int position, int intra);
void Count_sac_BitsVectors(MotionVector *MV[5][MBR+1][MBC+2], Bits *bits,
int i, int j, int Mode, int newgob, Pict *pic);
void Count_sac_BitsMB(int Mode,int COD,int CBP,int CBPB,Pict *pic,Bits *bits);
int AR_Encode(int index, int cumul_freq[]);
int indexfn(int value, int table[], int max);
int bit_opp_bits(int);
int bit_in_psc_layer(int);
int encoder_flush();
void putbits (int, int);
void initbits();
int alignbits ();
extern _inline void newFindHalfPel(MotionVector *fr, int m[5]);
/* Fix broken header-files on suns to avoid compiler warnings */
/* #define BROKEN_SUN_HEADERS here or in Makefile */
#ifdef BROKEN_SUN_HEADERS
extern int time();
extern int fclose();
extern int rewind();
extern int fseek();
extern int fread();
extern int fwrite();
extern int fflush();
extern int fscanf();
extern int _flsbuf();
extern int _filbuf();
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -