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

📄 global.h

📁 经典的MP4编解码核心库
💻 H
字号:
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

//#define _PROFILING_
#include <windows.h>

#define int8_t		char
#define uint8_t		unsigned char
#define int16_t		short
#define uint16_t	unsigned short
#define int32_t		int
#define uint32_t	unsigned int
#define int64_t		__int64
#define uint64_t	unsigned __int64
#define bool		uint32_t
#define EMMS()		__asm {emms}

// vop coding types 
#define I_VOP	0		// intra_coded
#define P_VOP	1		// forward prediction
#define B_VOP	2		// bi-directional prediction
#define S_VOP	3		// sprite plane
#define N_VOP	4		// not_coded

#define MAX_VOP		16
#define MBPRED_SIZE 15

#define ALIGN_WIDTH 16

#define SIGN(X)		(((X)>0)?1:-1)
#define ABS(X)		abs((X))
#define MIN(X, Y)	((X)<(Y)?(X):(Y))
#define MAX(X, Y)	((X)>(Y)?(X):(Y))

#define SCALEBITS	16
#define FIX(X)		((1UL << SCALEBITS) / (X) + 1)

#define FINE_DIV(a, b) ((a)>0) ? ((a)+((b)>>1))/(b) : ((a)-((b)>>1))/(b)

typedef struct
{
	uint8_t * y;
	uint8_t * u;
	uint8_t * v;
	uint8_t * true_addr;	// to make VOP release easier
} VOP;

typedef struct
{
	uint32_t * buf;
	uint32_t reg;
	uint32_t pos;
	uint32_t * buf_ptr;
}
BitWriter;

typedef struct
{
	uint32_t curr;
	uint32_t next;
	uint32_t pos;
	uint32_t * ptr;
	uint32_t * buf;
	uint32_t length;
	uint32_t dec_err;
} 
BitReader;

typedef struct
{
	int *search_history;
	int check_value;
}
SearchHistory;

typedef struct
{
	int x;
	int y;
} VECTOR;

typedef struct
{
	// for both encoder and decoder
	VECTOR	 mvs[4];		// Motion vectors (1MV/4MV)
	uint32_t sad8[4];		// SAD values for inter4v-VECTORst
	uint32_t sad8_mb;		// sum of sad8[0:3]
	uint32_t dev16;			// deviation of MB
	uint32_t mean16;		// mean of luminance sigma
	uint32_t sad16;			// SAD value for inter-VECTOR
	int32_t  mb_complex;	// complexity of MB

    int16_t  pred_values[6][MBPRED_SIZE];
    int32_t  acpred_directions[6];
    
	int32_t  mode;
	uint32_t quant;		// absolute quant

	// for encoder only
	VECTOR  dmvs[4];	// diff of MV of MB with predicted MV
	int32_t dquant;		// diff of quant for consecutive MB
	int32_t cbp;		// coding block pattern

	// interlaced video coding
	int32_t field_dct;
	int32_t field_pred;
	int32_t field_for_top;
	int32_t field_for_bot;

} MACROBLOCK;

// Macroblock for decoding
typedef struct
{
	// for both encoder and decoder
	VECTOR	 mvs[4];	// Motion vectors (1MV/4MV)

	uint32_t acpred_flag;
    int16_t  pred_values[6][MBPRED_SIZE];
    int32_t  acpred_directions[6];
    
	int32_t  mode;
	uint32_t quant;		// absolute quant
	int32_t  dquant;	// diff of quant for consecutive MB
	uint32_t cbp;		// coding block pattern

	// interlaced video coding
	int32_t field_dct;
	int32_t field_pred;
	int32_t field_for_top;
	int32_t field_for_bot;

} Macroblock;


typedef struct
{
	int32_t		mode;				// unlimited buffer size or limited buffer size?
 
	int32_t		act_quant;			// activated quantizer
	int32_t		frames;
	int64_t		total_size;
	float		framerate;
	int32_t		target_rate;
	int16_t		max_quant;
	int16_t		min_quant;
	double		quant_error[32];
	float		avg_framesize;
	float		target_framesize;
	int32_t		reaction_delay_factor;

	double		frame_bytes;

	double		vbv_delay;
	double		vop_delay;
	double		click_per_bit;
	double		decoding_time;
	double		max_vbv_delay;
	double		vbv_delay_upper_th;
	double		vbv_delay_lower_th;
	double		mid_vbv_delay;
	double		low_vbv_delay;

	//double	v;				// velocity;
	int32_t		quant_ip;

	//-------------------------	
	int32_t		target_bits;
	int32_t		BVOP_followed;

	double		total_complex;
	double		global_complexity;
	double		local_complexity;
	double		ref_complex;
	int32_t		complex;

	double		pb_ratio;
	double		rc_constant;
	double		rc_constant_b;

	double		pred_vbv_delay;
	int32_t		b_quant;

	double		Kp;
	double		Kb;

	int32_t		ref_frames;

	int32_t		act_bits;
	int32_t		force_I;

	int32_t		scene_change;

	// members for constant quality vbr
	double		target_quality;
	double		quality;
	double		quality_factor;
	double		unit_quality;
	int32_t		distortion;
	int32_t		bright;
	int32_t		contrast;

}
RateControl;

typedef struct
{
	BitWriter		bs;
	SearchHistory * sh;		// for motion estimation

	// macroblock
	MACROBLOCK	  * pMBs;
	MACROBLOCK	  * pMBs_B;

	RateControl   * rc;

	// Number of VOP allocaed in Encoder
	uint8_t * vop_list[MAX_VOP];
	int32_t vop_count;

    int32_t rounding_type; 			// value is alternated between 0-1 after each PVOP 
	int32_t interlaced;		// interlaced video coding
	int32_t alternate_scan;	// use zigzag or alternative vertical scan?
	int32_t top_field_first;

    int32_t width;
    int32_t height;

	int32_t ext_width;
	int32_t ext_height;
	int32_t mb_width;
	int32_t mb_height;

    int32_t coding_type;
	int32_t BVOP_enable;

	int32_t  quant_type;
    uint32_t f_code;
    uint32_t quality;
    uint32_t quant;
	int32_t  mean;					// mean of luminance signal

	uint32_t iFrameEncoded;			// to locate B_VOP
    int32_t	 iFrameNum;
    int32_t	 iMaxKeyInterval;
	int32_t	 bitrate;

	int32_t  enable_deinterlacing;
	int32_t  enable_bidirectional_encoding;
	int32_t  adapt_quant;			// Adaptive quantization. 

	int32_t  dev16_done;			// to avoid computation of dev16 twice for same VOP

	// for BVOP encoding,			
	int32_t	 TRB;
	int32_t	 TRD;
	uint32_t B_quant[3];
	VOP		 sBVOP[3];
	VOP		 vCompensated;

	// to avoid unnecessary interpolation operations
	int32_t	 interpolated;

	// use for B_VOP motion vectors' coding
	int32_t	 Px_for;
	int32_t  Py_for;
	int32_t  Px_back;
	int32_t  Py_back;
	int32_t  Prev_j;	

    VOP		 sCurrent;
    VOP		 sReference;
	VOP		 sBackup;

    VOP		 vInterH;
    VOP		 vInterV;
    VOP		 vInterHV;

	// interpolated images for B_VOP encoding
	VOP		 vNextH;
	VOP		 vNextV;
	VOP		 vNextHV;
	// end of preparation for BVOP

	// information to be written into VOL header
	int32_t  fbase;				// 
	int32_t  fincr;				// 
	int32_t  time_res_bits;		//
	int32_t  modulo_time_base[2];

	// information used for rate control and other control.
	int32_t  act_p;				// activity of P_VOP
	int32_t  act_b;				// activity of B_VOP

	int32_t  *filtered_complex;

	int32_t  p_match_err;
	int32_t  b_match_err;

	int32_t	 iMvPrevVOP;
	int32_t	 iMvCurVOP;
	int32_t	 iMvCount;

	// mpeg quant type
	int16_t intra_m[64];
	int16_t inter_m[64];

}
Encoder;

typedef struct
{
	int32_t coding_type;

	// Number of VOP allocaed in Decoder
	uint8_t * vop_list[MAX_VOP];
	int32_t vop_count;
	
	uint32_t shape;
	uint32_t time_inc_bits;
	uint32_t quant_bits;
	uint32_t quant_type;
	uint32_t quarterpel;

	// vop

	uint32_t width;
	uint32_t height;
	uint32_t ext_width;
	uint32_t ext_height;
	
	VOP cur;
	VOP ref;
	VOP refh;
	VOP refv;
	VOP refhv;

	int32_t TRB;
	int32_t TRD;
	int32_t time_resolution;
	int32_t time_incr;
	int32_t ref_time_incr;
	int32_t prev_ref_time_incr;
	VOP		bvop;
	VECTOR	mv_B[2];
	int32_t prev_row;
	int32_t cur_edge_set;
	int32_t refn_edge_set;
	
	int32_t interlaced;
	int32_t top_field_first;
	int32_t alternative_scan;

	// macroblock

	uint32_t mb_width;
	uint32_t mb_height;
	Macroblock * mbs;
	Macroblock * mbs_B;

	BitReader bs;

	uint32_t rounding;
	uint32_t quant;
	uint32_t fcode;

	// To support BVOP decoding
	uint32_t fcode_backward;
	uint32_t intra_dc_th;

	// mpeg quant type
	int16_t intra_m[64];
	int16_t inter_m[64];
	
} Decoder;

// needed for bitstream.h
uint32_t BSWAP(uint32_t);

#endif // _GLOBAL_H_

⌨️ 快捷键说明

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