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

📄 bitstream.h

📁 一个优化的H.263解码器,性能达到商用级
💻 H
字号:
// BitStream.h: interface for the CBitStream class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(_BITSTREAM_H__)
#define _BITSTREAM_H__
#define GLOBAL
#define  SACTABLES
#define INDICES

#pragma message("bitstream.h included!")
#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>


#include "tmndec.h"
#include "global.h"
#include "colorspaceconvert.h"
#include "IDCTDecode.h"

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifdef WIN32
#include <io.h>
#endif

class CBitStream
{
private:
	long Actualsize;
	struct ld_struct* ld;
	CColorSpaceConvert* m_ColorSpaceConvert;
	CIDCTDecode*        m_IDCTDecode;
private:
/**************the definition of output formats**************/
/* global variables */
    int De_quiet;
    int De_trace;
    int De_save_frames;
    char De_errortext[256];
    //<<<<<<<<<<<<<
    /*	 int iYUV_RGB_data[522];
	unsigned int m_y2rgb[256];
	unsigned int m_u2rgb[256];
	unsigned int m_v2rgb[256];*/
	unsigned char m_limit_rgb[1024];
	    
	unsigned char *prev_frame[3], *current_frame[3], *bframe[3];
	unsigned char *tmp_f[3], *tmp_b[3];
	unsigned char *edgeframe[3], *edgeframeorig[3], *exnewframe[3];
	unsigned char *nextedgeframe[3], *nextedgeframeorig[3];
	unsigned char *enhancementedgeframe[3], *enhacnementedgeframeorig[3];
	unsigned char *next_I_P_frame[3], *prev_I_P_frame[3], *buffered_frame[3];
	int horizontal_size, vertical_size, mb_width, mb_height;
	int coded_picture_width, coded_picture_height;
	int ref_coded_picture_width, ref_coded_picture_height;
	int chrom_width, chrom_height, blk_cnt;
	int ref_chrom_width, ref_chrom_height;
	int pict_type, newgob, gfid, pgfid;
	int syntax_arith_coding;
	int adv_pred_mode;
	int pb_frame;
	int true_B_frame;
	int scalability_mode;
	int buffered_framenum;
	int long_vectors;
	int fault, expand;
	int verbose;
	int refidct;
	int matrix_coefficients;
	int temp_ref, prev_non_disposable_temp_ref, next_non_disposable_temp_ref;
	int pic_quant, quant, source_format, base_source_format;
	int UFEP;

	int De_overlapping_MC;
	int De_mv_outside_frame;
	int De_use_4mv;

	int De_MV[2][6][MBR + 1][MBC + 2];
	int De_true_B_direct_mode_MV[2][5][MBR + 1][MBC + 2];
	int De_modemap[MBR + 1][MBC + 2];
	int De_anchorframemodemap[MBR + 1][MBC + 2];
	int De_predictionmap[MBR + 1][MBC + 2];
	unsigned char *De_clp;
	unsigned char *De_clp1;

	int De_prev_mv_outside_frame, De_prev_sac, De_prev_adv_pred, De_prev_aic, De_prev_df;
	int De_prev_slice_struct, De_prev_rps, De_prev_isd, De_prev_aivlc, De_prev_mq;
	int De_prev_4mv, De_prev_long_vectors, De_prev_obmc;

	/* Following variables are used in H.263+ bitstream decoding */
	 int plus_type;           /* indicates if extended PTYPE exists or
									 * not. */
	 int optional_custom_PCF;
	 int advanced_intra_coding;
	 int deblocking_filter_mode;
	 int slice_structured_mode;
	 int reference_picture_selection_mode;
	 int independently_segmented_decoding_mode;
	 int alternative_inter_VLC_mode;
	 int modified_quantization_mode;
	 int reduced_resolution_update_mode;
	 int reference_picture_resampling_mode;
	 int De_rtype;
	 int post_filter;
	 int unlimited_unrestricted_motion_vectors;
	 int concealment;
	 int lastwidth;
	 int lastheight;

	/* Scalability globals */
	 unsigned char *De_prev_enhancement_frame[MAX_LAYERS][3], *De_current_enhancement_frame[MAX_LAYERS][3];
	 unsigned char *De_curr_reference_frame[3], *De_upsampled_reference_frame[3];
	 unsigned char *De_enhance_edgeframeorig[MAX_LAYERS][3], *De_enhance_edgeframe[MAX_LAYERS][3];
	 unsigned char *De_tmp_enhance_fwd[MAX_LAYERS][3], *De_tmp_enhance_up[MAX_LAYERS][3];
	 int De_enhancement_layer_init[MAX_LAYERS];
	 int De_enhance_pict;
 ///////////////////////////////////////////
	/* custom format variables */
	 int CP_PAR_code;
	 int CP_picture_width_indication;
	 int CP_picture_width;
	 int CP_picture_height_indication;
	 int CP_picture_height;
	 int PAR_width;
	 int PAR_height;

	 int CP_clock_frequency;
	 int SSS_rectangular_slices;
	 int SSS_arbitary_slice_ordering;
	 int enhancement_layer_num;
	 int reference_layer_number;
	 int MF_of_reference_picture_selection;
	 int TRI, TRPI;
	 int temporal_reference_for_prediction;

	 #ifdef USE_TIME
	 int framerate;
	 #ifndef WIN32
	 struct timeval tftarget;
	 #else
	 unsigned int targetTime;
	 #endif
	 #endif


	 int De_trd, De_trb, De_bscan, De_bquant, De_true_b_trb;
	/* output */
	 char outputname[100];
	 char trace_file_name[100];
	 FILE *trace_file;
	 char recon_file_name[100];
	 FILE *recon_file_ptr;
	 char enhance_recon_file_name[MAX_LAYERS][100];
	 FILE *enhance_recon_file_ptr[MAX_LAYERS];

	 int outtype;
	 
	 /* For reference picture selection Mode  (Annex N) */
 	 int   ring_pointer;               /* pointer - to last picture */
	 void *ring_lum[RING_SIZE];    /* luminance */
	 void *ring_Cr[RING_SIZE];     /* chominance */
	 void *ring_Cb[RING_SIZE];
	 int   ring_temporal_reference[RING_SIZE];     /* TR values for the pictures */
	 int   ring_quality[RING_SIZE];     /* picture quality */
	 int   stop_decoder;           /* flag if we completely out of sync */

	 unsigned char* rgbTemp[5];
	 int  Testflag,framenum,localframenum;
	 long  Maxsize;  //Max size of input buffer,just used for testing.
	 int first;

private:
	short iclip[1024];       /* clipping table */
    short *iclp;
///////use in SAC ,initial these value at class construction//////////////////
    long low, high, code_value, bit, length, sacindex, cum, zerorun;
//////use in Getpic.c function such as get_B_MBs,get_EI_EP_MBs,get_I_P_MBs///
    int coded_map[MBR + 1][MBC + 1];
    int quant_map[MBR + 1][MBC + 1];


public:
	CBitStream();
	virtual ~CBitStream();
	void Setld(struct ld_struct* ldp);
	
	int  ATInitH263Decode();
	void ATDestroyH263Decode() ;
	int  ATDecodeH263Frame(unsigned char *buffer, int length, unsigned char **rgb, int *width, int *height);
	
	void InitOptionModeZero (void);
	void Allocmemory(int iWidth,int iHeight);
	bool IsKeyFrame(unsigned char *buffer);


private:
	void SetActualsize(long iActualsize);
   /* getbits.c */
	void De_initbits(void);
	void fillbfr(void);
	unsigned int showbits(int n);
	unsigned int getbits1(void);
	void flushbits(int n);
	unsigned int getbits(int n);

	/* getblk.c */
	void getblock(int comp, int mode, int INTRA_AC_DC, int Mode);
	void get_sac_block(int comp, int mode, int INTRA_AC_DC, int Mode);
	/* local prototypes */
	De_RunCoef vlc_word_decode (int symbol_word, int *last);
	De_RunCoef Decode_Escape_Char (int intra, int *last);
	int DecodeTCoef (int position, int intra);
	/* gethdr.c */
	void startcode(void);
	void getgobheader(void);
	/* getpic.c */
	void putlast(int framenum);
	void PictureDisplay(int *framenum);
	/* scalability funcitons */
	void De_UpsampleReferenceLayerPicture(void);
	void De_UpsampleComponent(unsigned char *enhanced, unsigned char *base,
							int horiz, int vert);
	/* advanced intra coding functions */
	void De_Intra_AC_DC_Decode(short *store_qcoeff, int INTRA_AC_DC, int MBA, int xpos, int ypos, int comp, int newgob);
	void De_fill_null(int pred[]);
	void De_fill_A(int pred[], short *store_qcoeff, int xpos, int ypos, int block);
	void De_fill_B(int pred[], short *store_qcoeff, int xpos, int ypos, int block);
	int De_oddifyclipDC(int x);
	int De_clipAC(int x);
	int De_clipDC(int x);
	void interpolate_image(unsigned char *in, unsigned char
										 *out, int width, int height);
	/* getvlc.c */
	int getTMNMV(void);
	int getRVLC (void);
	int getMCBPC(void);
	int getMODB(void);
	int getMCBPCintra(void);
	int getCBPY(void);
	int getMBTYPE(int *true_B_cbp, int *true_B_quant);
	int getscalabilityCBPC ();
	/* idct.c */
//	void De_idct(short *block);
//	void De_init_idct(void);
	void De_decode(short *block, int comp, int bx, int by, int addflag);
	/* tmndec.c */

	/* recon.c */
	void reconstruct(int bx, int by, int P, int bdx, int bdy, int MODB, int newgob);
	void reconstruct_true_B(int bx, int by, int true_B_prediction_type);
	void reconstruct_ei_ep(int bx, int by, int ei_ep_prediction_type);
	void recon_bidir_average (int bx, int by) ;// called in reconstruct_true_B
	void recon_comp (unsigned char *src, unsigned char *dst,
					 int lx, int lx2, int w, int h, int x, int y, int dx, int dy, int flag);
    void recon_comp_obmc(unsigned char *src, unsigned char *dst, 
		             int lx, int lx2, int comp, int w, int h, int x, int y, int newgob);

	void rec (unsigned char *s, unsigned char *d, int lx, int lx2, int h);
//	void recon_bidir_average (int bx, int by);
	void recc (unsigned char *s, unsigned char *d, int lx, int lx2, int h);
	void reco (unsigned char *s, int *d, int lx, int lx2, int addflag, int c, int xa, int xb, int ya, int yb);
	void rech (unsigned char *s, unsigned char *d, int lx, int lx2, int h);
	void rechc (unsigned char *s, unsigned char *d, int lx, int lx2, int h);
	void recho (unsigned char *s, int *d, int lx, int lx2, int addflag, int c, int xa, int xb, int ya, int yb);
	void recv (unsigned char *s, unsigned char *d, int lx, int lx2, int h);
	void recvc (unsigned char *s, unsigned char *d, int lx, int lx2, int h);
	void recvo(unsigned char *s, int *d, int lx, int lx2, int addflag, int c, int xa, int xb, int ya, int yb);
	void rec4(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
	void rec4c(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
	void rec4o(unsigned char *s, int *d, int lx, int lx2, int addflag, int c, int xa, int xb, int ya, int yb);

	/* store.c */
	void store_one(char *outname, unsigned char *src[], int offset, int incr, 
								 int height);
	void store_rgb (char *outname, unsigned char *src,
                                     int offset, int incr, int height);
	void store_rgb1 (char *name, unsigned char *src,
               int offset, int incr, int width, int height, int append);
	void store_yuv (char *outname, unsigned char *src[],
                                     int offset, int incr, int height);
	void store_yuv1 (char *name, unsigned char *src,
               int offset, int incr, int width, int height, int append);
	/* sac.c */
	int decode_a_symbol(int cumul_freq[]);
	void decoder_reset ();
	void bit_out_psc_layer ();
	/* ring_buf.c */
	int  get_reference_picture(void);
	void store_picture(int quality);

	void getpicture (int *framenum, int gob,int *iWidth,int *iHeight);
	void getpicturehdr (int *ImageWidth,int *ImageHeight);
	int  getheader (int *iWidth,int *iHeight);

	void get_I_P_MBs (int framenum, int gob,int *iWidth,int *iHeight);
	void get_B_MBs (int framenum,int *iWidth,int *iHeight);
	void get_EI_EP_MBs (int framenum,int *iWidth,int *iHeight);
private:
	/* private prototypes */
	void clearblock (int comp);
	int motion_decode (int vec, int pmv);
	int find_pmv (int x, int y, int block, int comp);
    void addblock (int comp, int bx, int by, int addflag);
    void reconblock_b (int comp, int bx, int by, int mode, int bdx, int bdy);
    void find_bidir_limits (int vec, int *start, int *stop, int nhv);
    void find_bidir_chroma_limits (int vec, int *start, int *stop);
    void make_edge_image (unsigned char *src, unsigned char *dst, int width, int height, int edge);
    void init_enhancement_layer (int layer);
    void edge_filter (unsigned char *lum, unsigned char *Cb, unsigned char *Cr, int width, int height);
	void horiz_edge_filter (unsigned char *rec, int width, int height, int chr);
	void vert_edge_filter (unsigned char *rec, int width, int height, int chr);
	void vert_post_filter (unsigned char *rec, int width, int height, int chr);
	void horiz_post_filter (unsigned char *rec, int width, int height, int chr);
	void PostFilter (unsigned char *lum, unsigned char *Cb, unsigned char *Cr, int width, int height);
	/* reference picture selection */
	//int  get_reference_picture(void);
	//void store_picture(int);

	/* error concealment */
	void conceal_missing_gobs(int start_mb_row_missing, int number_of_mb_rows_missing);
	void flushdisplaybuffer (int framenum);  
};

#endif 

⌨️ 快捷键说明

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