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

📄 复件 h264t.h

📁 dm642上的h264源码
💻 H
字号:
/*****************************************************************************
*  H264 AVC CODEC
*
*  Copyright(C) whu 2006
*
*  Authors: Xiao JS, Chen GX, Lin KT
****************************************************************************/
// H264t.h 
//

#ifndef _H264T_H_
#define _H264T_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include "portab.h"
//x  

#define T_STRIDE        16  //t->stride
#define T_STRIDE_EDGED  16
    
//x
    
#define WIDTH_Y     352
#define HEIGHT_Y    288
#define WIDTH_UV    176
#define HEIGHT_UV   144

#define H264_MAJOR     ((uint32_t)0 << 16)
#define H264_MINOR     ((uint32_t)14)
#define H264_VER       (H264_MAJOR | H264_MINOR)
// flags field
#define USE_INTRA16x16        0x1      // use intra 16x16 pred
#define USE_INTRA4x4          0x2      // use intra 4x4   pred
#define USE_SUBBLOCK          0x4      // use inter 8x8   pred
#define USE_HALFPEL           0x8      // half pel search
#define USE_QUARTPEL          0x10     // quarter pel search
#define USE_FULLSEARCH        0x20     // full search
#define USE_DIAMONDSEACH      0x40
#define USE_FORCEBLOCKSIZE    0x80 // search specify block size
#define USE_FASTINTERPOLATE   0x100  // fast interpolate
#define USE_SAD               0x10000  // use sad
#define USE_SATD              0x20000  // use satd
#define USE_INTRAININTER      0x40000 // inter will try intra mode
#define USE_EXTRASUBPELSEARCH 0x80000 // search 8 points vs 4 points (qulity vs. speed)
#define USE_SCENEDETECT       0x100000

// custom flag
#define CUSTOM_FASTINTERPOLATE (1 << 1)

#define SEARCH_16x16P 0x1
#define SEARCH_16x8P  0x2
#define SEARCH_8x16P  0x4
#define SEARCH_8x8P   0x8
#define SEARCH_8x4P   0x10
#define SEARCH_4x8P   0x20
#define SEARCH_4x4P   0x40

// mb_neighbour
#define MB_LEFT        0x1
#define MB_TOP         0x2
#define MB_TOPRIGHT    0x4

//#define MAX_REFFRAMES  16
#define MAX_REFFRAMES  2
#define MAX_BREFNUMS   5
#define MAX_PLUGINS    5
#define CACHE_SIZE     16
#define EDGED_WIDTH    16//(32 + CACHE_SIZE)
#define EDGED_HEIGHT   EDGED_WIDTH

#define H264_MIN(a,b) ( (a)<(b) ? (a) : (b) )
#define H264_MAX(a,b) ( (a)>(b) ? (a) : (b) )

/* plugins state */
#define STATE_BEFORESEQ 0   /* before sequence */
#define STATE_BEFOREGOP 1   /* before gop header */
#define STATE_BEFOREPIC 2   /* before picture */
#define STATE_AFTERPIC  3   /* after picture encoded */

/* stat control */
#define DISPLAY_PSNR    1
#define DISPLAY_BLOCK   2

typedef enum 
{
    DEC_STATE_BUFFER = -1,
    DEC_STATE_OK,
    DEC_STATE_SEQ,
    DEC_STATE_PIC,
    DEC_STATE_SLICE,
    DEC_STATE_CUSTOM_SET,
    DEC_STATE_UNKOWN
} decoder_state_t;

typedef struct H264_t H264_t;
//typedef struct H264_search_context_t H264_search_context_t;

typedef struct  
{
    int32_t width, height;
    int32_t qp;
    int32_t bitrate;
    int32_t framerate;
//    int32_t adapt_framerate;
    int32_t iframe;                     // I 帧间距
    int32_t idrframe;                   // idr 间距
    int32_t ref_num;                    // 参考帧数目
    int32_t b_num;                      // b frame num
    int32_t flags;
    int32_t cpu;
    int32_t search_x;
    int32_t search_y;
    int32_t block_size;
    int32_t disable_filter;
    int32_t aspect_ratio;
    int32_t video_format;
    int32_t luma_coeff_cost;
    int32_t min_qp;
    int32_t max_qp;
    int32_t enable_rc;
    int32_t enable_stat;
    int32_t direct_flag;
	//for CABAC
//	int32_t cabac;
    void*   rec_name;
    int32_t stride_y;   //xjs add for output
    int32_t stride_uv;  //xjs add for output
} H264_param_t;

typedef struct  
{
    int32_t i_block_num[2];
    int32_t p_block_num[8];
    int32_t b_block_num[2];
    int32_t skip_block_num;
} H264_stat_t;




//
// nal_unit
//
typedef struct
{
    int32_t nal_ref_idc;
    int32_t nal_unit_type;
    int32_t nal_size;
} H264_nal_t;

typedef struct
{
    int32_t profile_idc;
    int32_t level_idc;
    int32_t seq_id;
    int32_t log2_max_frame_num_minus4;
    int32_t pic_order_cnt_type;
    int32_t max_pic_order;
    int32_t num_ref_frames;
    int32_t pic_width_in_mbs_minus1;
    int32_t pic_height_in_mbs_minus1;
    int32_t frame_mbs_only_flag;
} H264_seq_set_t;

typedef struct
{
    int32_t pic_id;
    int32_t seq_id;
    int32_t entroy_coding_mode_flag;
    int32_t pic_order_present_flag;
    int32_t num_slice_groups_minus1;
    int32_t num_ref_idx_l0_active_minus1;
    int32_t num_ref_idx_l1_active_minus1;
    int32_t weighted_pred_flag;
    int32_t weighted_bipred_idc;
    int32_t pic_init_qp_minus26;
    int32_t pic_init_qs_minus26;
    int32_t chroma_qp_index_offset;
    int32_t deblocking_filter_control_present_flag;
} H264_pic_set_t;

typedef struct
{
    int32_t first_mb_in_slice;
    int32_t slice_type;
    int32_t pic_id;
    int32_t frame_num;
    int32_t idr_pic_id;
    /* pic_oder_cnt_type == 0*/
    int32_t pic_order_cnt_lsb;
    int32_t delta_pic_order_cnt_bottom;
    int32_t direct_spatial_mv_pred_flag;
    /*P frame*/
    int32_t num_ref_idx_active_override_flag;
    int32_t slice_qp_delta;
    int32_t ref_pic_list_reordering_flag_l0;
    int32_t no_output_of_prior_pics_flag;
    int32_t long_term_reference_flag;
    int32_t adaptive_ref_pic_marking_mode_flag;
	/* for CABAC */
	int32_t cabac_init_idc;
} H264_slice_t;

typedef struct
{
    int32_t ver;
    int32_t flags;
} H264_custom_set_t;

typedef struct
{
    void (*proc)(H264_t* t, void* data, int32_t state);
    void (*close)(H264_t* t, void* data);
    void* handle;
    int32_t ret;    // ret value, optional
} H264_plugin_t;

typedef struct
{
    int8_t  refno;
    int16_t x;
    int16_t y;
} H264_vector_t;

// this struct should keep 16 bytes align
typedef struct
{
    // intra 4x4 mode
    uint32_t    mode_i4x4_all;
    // inter 4x4 block mv
    H264_vector_t vec[4 * 4];
	
    // non zero count
    uint8_t     nnz[16 + 4 + 4];
	// sad
	uint32_t    sad;

    // I16x16, I4x4, P_MODE, ....
    uint8_t     mb_mode;
    uint8_t     mb_mode_uv;
    // intra 16x16 mode
    uint8_t     mode_i16x16;
    // inter
    uint8_t     mb_part;
    // rate control
    int8_t      mb_qp_delta; 

	uint16_t    cbp_y;
	uint16_t    cbp_c;
	uint16_t	cbp;
    // just pad to 16 bytes aligned
    uint8_t     pad[CACHE_SIZE - 7];
} H264_mb_context_t;

typedef struct
{
    uint8_t*  Y[4], *U, *V;
    int32_t   poc;
    H264_mb_context_t *mb;
	H264_mb_context_t *mb2;
    int32_t   qp;
} H264_frame_t;

typedef struct H264_search_context_t
{
    // all candidate
    H264_vector_t* vec;
    int32_t        vec_num;
    H264_vector_t  vec_best;
    int32_t height;
    int32_t width;
    int8_t  limit_x;
    int8_t  limit_y;
    int32_t offset;
    int32_t mb_part;
    int32_t list_index;
} H264_search_context_t;


#ifndef _PREDICT_H_1
#define _PREDICT_H_1
typedef void (*H264_predict_16x16_mode_t)(uint8_t* dst, int32_t dst_stride, uint8_t* top, uint8_t* left);
typedef uint32_t (*H264_I_pred_16x16_mode_t)(uint8_t*src, uint8_t *top, uint8_t *left, uint32_t *sad_m, uint16_t src_stride);

typedef void (*H264_predict_4x4_mode_t)  (uint8_t* dst, int32_t dst_stride, uint8_t* top, uint8_t* left);
typedef uint32_t (*H264_I_pred_4x4_mode_t)(uint8_t*src, uint8_t *top, uint8_t *left, uint16_t src_stride);

typedef void (*H264_predict_8x8_mode_t)  (uint8_t* dst, int32_t dst_stride, uint8_t* top, uint8_t* left);
typedef uint32_t (*H264_I_pred_8x8_mode_t)(uint8_t*src, uint8_t *top, uint8_t *left, uint32_t *sad_m, uint16_t src_stride);
#endif

//typedef void (*expand8to16_t)(uint8_t* src, int32_t src_stride, int32_t quarter_width, int32_t quarter_height, int16_t* dst);
typedef void (*expand8to16sub_t)(uint8_t* pred, int32_t quarter_width, int32_t quarter_height, int16_t* dst, uint8_t* src, int32_t src_stride);
//typedef void (*contract16to8_t)(int16_t* src, int32_t quarter_width, int32_t quarter_height, uint8_t* dst, int32_t dst_stride);
typedef void (*contract16to8add_t)(int16_t* src, int32_t quarter_width, int32_t quarter_height, uint8_t* org, uint8_t* dst, int32_t dst_stride);
typedef void (*memcpy_stride_u_t)(void* src, int32_t width, int32_t height, int32_t src_stride, void* dst, int32_t dst_stride);
typedef uint32_t (*H264_cmp_t)(uint8_t* src, int32_t src_stride, uint8_t* data, int32_t dst_stride);
typedef void (*H264_pia_t)(uint8_t* p1, uint8_t* p2, int32_t p1_stride, int32_t p2_stride, uint8_t* dst, int32_t dst_stride);
typedef uint32_t (*H264_satd_i16x16_u_t)(uint8_t* src, int32_t src_stride, uint8_t* data, int32_t dst_stride);

typedef void (*H264_eighth_pixel_mc_u_t)(uint8_t* src, int32_t src_stride, uint8_t* dst, int16_t mvx, int16_t mvy, int32_t width, int32_t height);
typedef void (*H264_interpolate_halfpel_t)(uint8_t* src, int32_t src_stride, uint8_t* dst, int32_t dst_stride, int32_t width, int32_t height);
typedef void (*H264_pixel_avg_t)(uint8_t* p1, uint8_t* p2, int32_t p1_stride, int32_t p2_stride, uint8_t* dst, int32_t dst_stride, int32_t w, int32_t h);
typedef uint32_t (*H264_search_t)(H264_t* t, H264_search_context_t* context);
//typedef void (*H264_emms_t)();
typedef decoder_state_t (*action_t)(H264_t* t);

typedef struct
{
    // COPY BEGIN
    // NOTE: copied from H264_mb_cache_t except padding words!!!
    // intra 4x4 mode
    uint32_t    mode_i4x4_all;
    // inter 4x4 block mv
    H264_vector_t vec[4 * 4];
    // non zero count
    uint8_t     nnz[16 + 4 + 4];
    // sad
    uint32_t    sad;

    // I16x16, I4x4, P_MODE, ....
    uint8_t     mb_mode;
    uint8_t     mb_mode_uv;
    // intra 16x16 mode
    uint8_t     mode_i16x16;
    // inter
    uint8_t     mb_part;
    // rate control
    int8_t      mb_qp_delta; 
	uint16_t    cbp_y;
  	uint16_t    cbp_c;
  	uint16_t	cbp;
    // COPY END

    H264_mb_context_t* context;

    int32_t     mb_neighbour;
    int16_t     mb_x;
    int16_t     mb_y;
    int16_t		mb_xy;
    int32_t     lambda;

    uint8_t*    src_y;
    uint8_t*    src_u;
    uint8_t*    src_v;
    uint8_t*    dst_y;	
    uint8_t*    dst_u;
    uint8_t*    dst_v;

    // save the predict value for intra encode
    DECLARE_ALIGNED_MATRIX_H(pred_i16x16, 16, 16, uint8_t, CACHE_SIZE);
    DECLARE_ALIGNED_MATRIX_H(pred_i8x8u, 8, 8, uint8_t, CACHE_SIZE);
    DECLARE_ALIGNED_MATRIX_H(pred_i8x8v, 8, 8, uint8_t, CACHE_SIZE);
    DECLARE_ALIGNED_MATRIX_H(pred_p16x16, 16, 16, uint8_t, CACHE_SIZE);

    H264_vector_t vec_pred[5];                  //帧间vec预测
    int16_t     dct_y_z[16][4*4];               // 块进行Z扫描后的系数
    int16_t     dct_uv_z[2][4][4*4];
    int16_t     dc4x4_z[16];                    // Z扫描后的16个DC系数
    int16_t     dc2x2_z[2][4];                  // Z扫描后的4个DC系数
	
  } H264_mb_t;

struct H264_t
{
    H264_frame_t  refn[MAX_REFFRAMES];
    H264_frame_t* ref;
    int32_t refl0_num;
    H264_frame_t  cur;
    H264_frame_t* rec;

    int32_t     width;
    int32_t     height;
    int32_t     stride;
    int32_t     stride_uv;
    int32_t     edged_stride;
    int32_t     edged_stride_uv;
    int32_t     edged_width;
    int32_t     edged_height;
    int32_t     qp_y, qp_uv;
    void        *bs;
    uint8_t     *bs_buf;
    uint32_t    flags;
    int32_t     mb_width, mb_height;
    int32_t		mb_stride;
    uint32_t    idr_pic_id;
    /* the frame unique id in the whole encoding session, for statistic(rc uses it too) */
    uint32_t    frame_id;
    /* the frame_num in the bitstream semantic, for generating bitstream only */
    uint32_t    frame_num;
    /* the frame unique id in the current encoding gop, for deciding the slice type usage */
    uint32_t    frame_no;
    /* the frame unique id that the last key frame id */
    uint32_t    last_i_frame_id;
    uint32_t    poc;
    uint32_t    slice_type;
    int32_t     skip;
    uint32_t    sad_all;
    int32_t     header_bits;
    int32_t     frame_bits;

    /* ++ decoder section ++ */
    /* source section */
    uint8_t* src_buf;   /* source buffer start ptr */ 
    uint8_t* src_end;
    uint8_t* nal_buf;   /* buffer one whole nal unit */
    int32_t  nal_len;    /* one nal unit length */

    uint32_t shift;     /* nal decode use */
    uint32_t shift1;

    action_t action;

    int32_t need_deblock;
//    H264_frame_t* cur_frame;
    /* frame rate info */
    int32_t aspect_ratio;
    int32_t video_format;
//    H264_frame_t output;
    /* -- decoder section -- */

    H264_param_t   param;
    H264_nal_t     nal;
    H264_seq_set_t ss;
    H264_pic_set_t ps;
    H264_slice_t   slice;
    H264_stat_t    stat;

    H264_mb_t      mb;
    int16_t        subpel_pts;
    H264_plugin_t  plugins[MAX_PLUGINS];

    int32_t        plug_num;
    H264_predict_16x16_mode_t pred16x16[4 + 3];
    H264_I_pred_16x16_mode_t  Ipred16x16[4 + 3];
	H264_predict_8x8_mode_t   pred8x8[4 + 3];
    H264_I_pred_8x8_mode_t    Ipred8x8[4 + 3];
  	H264_predict_4x4_mode_t   pred4x4[9 + 3];
    H264_I_pred_4x4_mode_t    Ipred4x4[12];

  	H264_cmp_t cmp[8];
    H264_cmp_t sad[8];
    H264_pia_t pia[9];  //for pixel avearage func

    expand8to16sub_t   expand8to16sub;
    contract16to8add_t contract16to8add;
    memcpy_stride_u_t  memcpy_stride_u;
    H264_eighth_pixel_mc_u_t   eighth_pixel_mc_u;
    H264_interpolate_halfpel_t interpolate_halfpel_h;
    H264_interpolate_halfpel_t interpolate_halfpel_v;
    H264_interpolate_halfpel_t interpolate_halfpel_hv;
    H264_pixel_avg_t     pixel_avg;
    H264_satd_i16x16_u_t H264_satd_16x16_u;
    H264_search_t        search;
//    H264_emms_t emms;
};

/* private func(for encoder & decoder share) */
void H264_init_cpu(H264_t* t);
void H264_mb_load_context(H264_t* t, int32_t mb_y, int32_t mb_x);
void H264_extend_border(H264_t* t, H264_frame_t* f);
void H264_interpolate_halfpel(H264_t* t, H264_frame_t* f);

/* extern api */
H264_t* H264enc_open(H264_param_t* para);
void H264enc_close(H264_t* t);
int32_t H264_encode(H264_t* t, uint8_t* src, uint8_t* dst, int32_t dst_size);
int32_t H264_encode3(H264_t* t, uint32_t* srcY, uint32_t* srcU, uint32_t* srcV, uint8_t* dst, int32_t dst_size);

H264_t* H264dec_open(H264_param_t* para);
void H264dec_close(H264_t* t);
int32_t H264_decode(H264_t* t, uint8_t* src, int32_t src_size,uint8_t bMark);
int32_t H264_decode3(H264_t* t, uint8_t* src, int32_t src_size, uint8_t bMark, uint32_t* dstY, uint32_t* dstU, uint32_t* dstV);

H264_frame_t* H264dec_flush_frame(H264_t* t);

#ifdef __cplusplus
};
#endif

#endif

⌨️ 快捷键说明

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