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

📄 global.h

📁 h264编解码.用C++实现了图像的编解码功能。
💻 H
字号:
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include <stdio.h>

#define TRUE            1
#define FALSE           0

typedef unsigned char byte;

#define absm(A) ((A)<(0) ? (-A):(A)) /* abs macro, faster than procedure */
#define MAX_VALUE       999999   /* used for start value for some variables */

#define _LUMA_COEFF_COST_       4 //!< threshold for luma coeffs
#define _CHROMA_COEFF_COST_     4 //!< threshold for chroma coeffs, used to be 7

 /* Picture types  */       
#define INTRA_IMG       0         
#define INTER_IMG       1

#define P_SLICE		0
#define I_SLICE		2
    
// Quantization parameter range

#define MIN_QP          0
#define MAX_QP          51
#define SHIFT_QP        12

/* inter MB modes */
#define COPY_MB         0        /* just copy last MB, no motion vectors */  
#define M16x16_MB       1        /* 16 x 16 block */   
#define M16x8_MB        2   
#define M8x16_MB        3
#define M8x8_MB         4
#define M8x4_MB         5   
#define M4x8_MB         6
#define M4x4_MB         7
#define P8x8    8
#define I4MB    9
#define I16MB   10

/* imod constants */
#define INTRA_MB_OLD    0       /* 'old' intra prediction mode                     */
#define INTRA_MB_NEW    1       /* new 16x16 based intra prediction modes          */
#define INTRA_MB_INTER  2       /* Intra MB in inter frame, see "inter MB modes"   */ 

#define BLOCK_SIZE      4
#define MB_BLOCK_SIZE   16

#define NO_INTRA_PMODE  9		     /* #intra prediction modes */
/* 4x4 intra prediction modes */
//#define DC_PRED         0
//#define DIAG_PRED_RL    1 
//#define VERT_PRED       2
//#define DIAG_PRED_LR_45 3
//#define HOR_PRED        4
//#define DIAG_PRED_LR    5 

//#define DC_PRED         2
//#define DIAG_PRED_RL    3 
//#define VERT_PRED       0
//#define DIAG_PRED_LR_45 4
//#define HOR_PRED        1
//#define DIAG_PRED_LR    5 
#define VERT_PRED             0
#define HOR_PRED              1
#define DC_PRED               2
#define DIAG_DOWN_LEFT_PRED   3
#define DIAG_DOWN_RIGHT_PRED  4
#define VERT_RIGHT_PRED       5
#define HOR_DOWN_PRED         6
#define VERT_LEFT_PRED        7
#define HOR_UP_PRED           8
// CAVLC
#define LUMA              0
#define LUMA_INTRA16x16DC 1
#define LUMA_INTRA16x16AC 2

#define LEVEL_NUM      6
#define TOTRUN_NUM    15
#define RUNBEFORE_NUM  7

#define  MAX_NO_POC_FRAMES  10  //size of poc ref array
#define NONREFFRAME 0           // used with push_poc
#define REFFRAME 1

#define ABIPRED
//--- block types for CABAC
#define LUMA_16DC       0
#define LUMA_16AC       1
#define LUMA_8x8        2
#define LUMA_8x4        3
#define LUMA_4x8        4
#define LUMA_4x4        5
#define CHROMA_DC       6
#define CHROMA_AC       7
#define NUM_BLOCK_TYPES 8
/* 16x16 intra prediction modes */
#define VERT_PRED_16    0
#define HOR_PRED_16     1
#define DC_PRED_16      2
#define PLANE_16        3     

/* chroma intra prediction modes */
#define VERT_CHROMA_PRED 0
#define HOR_CHROMA_PRED  1
#define DC_CHROMA_PRED   2
     

/* image formats*/
#define QCIF            0
#define CIF             1

/* QCIF format */
#define IMG_WIDTH       176
#define IMG_HEIGHT      144
#define IMG_WIDTH_CR    88
#define IMG_HEIGHT_CR   72

/* coefficient scan order */
#define SINGLE_SCAN     0
#define DOUBLE_SCAN     1

#define LEN_STARTCODE   31        /* length of start code */
#define MAX_MULT_PRED   5         /* max number of frames to make inter prediction from */

#define EOS             1         /* End Of Sequence */

#define  LAMBDA_ACCURACY_BITS         16
#define  LAMBDA_FACTOR(lambda)        ((int)((double)(1<<LAMBDA_ACCURACY_BITS)*lambda+0.5))
#define  WEIGHTED_COST(factor,bits)   (((factor)*(bits))>>LAMBDA_ACCURACY_BITS)
#define  MV_COST(f,x,y)				  (WEIGHTED_COST(f,img->mv_bituse[(x)]+img->mv_bituse[(y)]))
#define IMG_PAD_SIZE 8


struct inp_par					 /* all input parameters                */
{
  int no_frames;                /* number of frames to be encoded                                              */
  int qp0;                      /* QP of first frame                                                           */
  int qpN;                      /* QP of remaining frames                                                      */
  int jumpd;                    /* number of frames to skip in input sequence (e.g 2 takes frame 0,3,6,9...)   */ 
  int hadamard;                 /* 0: 'normal' SAD in 1/3 pixel search.  1: use 4x4 Haphazard transform and '  */
                                /* Sum of absolute transform difference' in 1/3 pixel search                   */
  int search_range;             /* search range - integer pel search and 16x16 blocks.  The search window is   */ 
                                /* generally around the predicted vector. Max vector is 2xmcrange.  For 8x8    */
                                /* and 4x4 block sizes the search range is 1/2 of that for 16x16 blocks.       */
  int no_multpred;              /* 1: prediction from the last frame only. 2: prediction from the last or      */
                                /* second last frame etc.  Maximum 5 frames                                    */
  int postfilter;               /* Not used                                      */
  byte img_format;               /* 0: QCIF. 1: CIF                                                             */
  int intra_upd;                /* For error robustness. 0: no special action. 1: One GOB/frame is intra coded */   
                                /* as regular 'update'. 2: One GOB every 2 frames is intra coded etc.          */ 
                                /* In connection with this intra update, restrictions is put on motion vectors */
                                /* to prevent errors to propagate from the past*/
  int write_dec;                /* write decoded image to file */
  int blc_size[8][2];           /* array for different block sizes */
  char infile[100];             /* YUV 4:2:0 input format*/      
  char outfile[100];            /* H.26L compressed output bitstream*/
};

struct img_par
{   
  int number;                  /* current image number to be encoded*/
  int type;
  int multframe_no;
  int qp;                      /* quant for the current frame */   
  int width;                   /* Number of pels */
  int width_cr;                /* Number of pels chroma */
  int height;                  /* Number of lines */
  int height_cr;               /* Number of lines  chroma */
  int mb_y;                    /* current MB vertical   */ 
  int mb_x;                    /* current MB horizontal */ 
  int block_y;                 /* current block vertical   */ 
  int block_x;                 /* current block horizontal */ 
  int pix_y;                   /* current pixel vertical   */ 
  int pix_x;                   /* current pixel horizontal */ 
  int mb_mode;                 /* MB mode relevant for inter images, for intra images are all MB intra*/
  int imod;                    /* new/old intra modes + inter */
  int pix_c_y;                 /* current pixel chroma vertical   */
  int block_c_x;               /* current block chroma vertical   */
  int pix_c_x;                 /* current pixel chroma horizontal   */
  int blc_size_h;              /* block size for motion search, horizontal */ 
  int blc_size_v;              /* block size for motion search, vertical */
  int spiral_search[2][6561];  /* To obtain spiral search */ 
  int ipredmode[90][74];       /* prediction mode for inter frames */ 
  int intra_pred_modes[16];
  int cbp;
  int mprr[9][16];           /* all 5 prediction modes */
  int mprr_2[5][256];       //!< all 4 new intra prediction modes/*yummy*/
  int intra_mpr[16];
  int s[4];
  int intra_m7[16];
  int mv[2];       /* motion vectors for all block types and all reference frames */
  int comp;
  int offset;
  int mpr[16][16];             /* current best prediction mode */ 
  int m7[16][16];              /* the diff pixel values between orginal image and prediction */ 
  int cof[4][6][18][2];     /* coefficients */ 
  int i16offset;		/*yummy*/
  int cofy[16][2];     /* DC coefficients */ /*yummy*/
  int cofu[5][2][2];           /* coefficients chroma */  
  int mv_bituse[512];  

  int nz_coeff[90][74][4][6];            //!< number of coefficients per block (CAVLC)
  int cod_counter;             //!< Current count of number of skipped macroblocks in a row
  int current_mb_nr;
  int total_number_mb;

  int offset1[2];
  int comp1[2];
  int mv1[2][2];
  int offset2[2];
  int comp2[2];
  int mv2[2][2];
  int bSkipMode;

  int offset3[4][16];
  int comp3[4][16];
  int mv3[4][16][2];
  int SubMBType1;
  int SubMBType2;
  int SubMBType3;
  int SubMBType4;
};

/* prototypes */
void intrapred_luma(struct img_par *img,int CurrPixX,int CurrPixY);
void init(byte iformat,struct img_par *img);
int image(struct img_par *img,struct inp_par *inp);
int  find_sad(struct img_par *img,int hadamard);
int  dct_luma(byte *ii, int *pp, int pos_mb1,int pos_mb2,struct img_par *img);
int dct_luma_16x16(byte *ii, int new_intra_mode,struct img_par *img);/*yummy*/
int inter_dct_luma1(int block_x,int block_y,int *coeff_cost, struct img_par *img);
int intra_dct_chroma(int uv,int cr_cbp,byte *in,int *pp, struct img_par *img);
int  dct_chroma(int uv,int i11,struct img_par *img);
int  motion_search(struct img_par *img, int isi);
int motion_search_16x8(struct img_par *img,int MinCost);
int motion_search_8x16(struct img_par *img,int MinCost);
/***************by guoxq*******************/
int ue_v (int value);
int se_v (int value);
int u_1 (int value);
int u_v (int n, int value);
void InitBits();
void PutBits(int n, int val);
void AlignBits();
int Write_Intra4x4PredictionMode(int mode);
int writeSyntaxElement_NumCoeffTrailingOnes(int numcoeff_vlc, int numcoeff, int numtrailingones);
int writeSyntaxElement_NumCoeffTrailingOnesChromaDC(int numcoeff_vlc, int numcoeff, int numtrailingones);
int writeSyntaxElement_Level_VLC1(int value1);
int writeSyntaxElement_Level_VLCN(int value1, int vlc);
int writeSyntaxElement_TotalZeros(int value1, int length);
int writeSyntaxElement_TotalZerosChromaDC(int value1, int length);
int writeSyntaxElement_Run(int value1, int length);

void WriteMb_CAVLC(struct img_par *img,int *intra_pred_modes,int cbp);
/***************by guoxq*******************/
int Intra_Sad(struct img_par *img,byte *ii, int *pp);
int IntraMB_Sad(struct img_par *img,byte *ii, int *pp);
int Intra_Sad_16x16(struct img_par *img,byte *ii, int *pp);

void read_input(struct codpar parameter,struct inp_par *inp);
void read_input1(struct codpar parameter,struct inp_par *inp);
void intrapred_chroma(struct img_par *img,int,int,int uv);
void ReadImage(struct img_par *img);
void InitLookupTable();
int RGB2YUV (int x_dim, int y_dim, void *bmp, void *y_out, void *u_out, void *v_out);
void oneforthpix(struct img_par *img);

//mb
int macroblock_intra_topleft(struct img_par *img);
int macroblock_intra_top(struct img_par *img);
int macroblock_intra_left(struct img_par *img);
int macroblock_intra_right(struct img_par *img);
int macroblock_intra_other(struct img_par *img);

void IntraMb_Chroma(struct img_par *img);
int Motion_Estimation(struct img_par *img,int tot_intra_sad);
void macroblock_inter(struct img_par *img,int tot_intra_sad);

int inter_dct_luma(int *ii, int *coeff, int block_x,int block_y,int *coeff_cost, struct img_par *img);
void inter_idct_luma(int *outD, byte *pp, int block_x,int block_y,struct img_par *img,int width);
void Predict(struct img_par *img);
void Predict_16x8(struct img_par *img, int index);
void Predict_8x16(struct img_par *img, int index);

int motion_search_8x8(struct img_par *img,int BlockType, int index, int MB_x, int MB_y);
int SmallBlock_ME(struct img_par *img, int Min_Cost);
void macroblock_inter_SmallBlock(struct img_par *img);
void Copy_tmpmv(struct img_par *img);

void InitInfoTable();
void freeInfo();

void DeblockFrame(struct img_par *img);


typedef struct __BIT2BYTE__
{
	union
	{
		unsigned int ii;
		char cc[4];
	} data;
	int cExist;
	int bExist;
} BIT2BYTE;

#define SAD_TABLE_SIZE 49
typedef struct __SAD_TABLE__
{
	int dx,dy;
	int offset;
} SAD_TABLE;

typedef struct point
{
	int x;
	int y;
	int comp,offset;
	int type;
} Point;
/*
int *NumCoeffTable;
int *NumCoeffDCTable;//chroma DC
int *TotalZerosTable;
int *TotalZerosDCTable;//chroma DC
int *RunTable;
int *LevelTable;*/
#endif







⌨️ 快捷键说明

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