假设你已经编译1.3通过并生成了xvid.dll
先贴编码类给你看看
// mediacodec.H File class xvidCode: { public : xvidCode(); ~ xvidCode(); protected : void * m_handle; public : virtual void exit(); virtual bool init( int width, int height, int bits); virtual bool code( char * data, int datalen, bool key); }; // mediacodec.cpp #include " mediacodec.h " #include < xvid.h > #pragma comment(lib,"xvid") /* **************************************************************************** * Quality presets *************************************************************************** */ static const int motion_presets[] = { /* quality 0 */ 0 , /* quality 1 */ XVID_ME_ADVANCEDDIAMOND16, /* quality 2 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16, /* quality 3 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8, /* quality 4 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, /* quality 5 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, /* quality 6 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH8 | XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, }; #define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0])) static const int vop_presets[] = { /* quality 0 */ 0 , /* quality 1 */ 0 , /* quality 2 */ XVID_VOP_HALFPEL, /* quality 3 */ XVID_VOP_HALFPEL | XVID_VOP_INTER4V, /* quality 4 */ XVID_VOP_HALFPEL | XVID_VOP_INTER4V, /* quality 5 */ XVID_VOP_HALFPEL | XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT, /* quality 6 */ XVID_VOP_HALFPEL | XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT | XVID_VOP_HQACPRED, }; #define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0])) #define MAX_ZONES 64 static xvid_enc_zone_t ZONES[MAX_ZONES]; static int NUM_ZONES = 0 ; /* Maximum number of frames to encode */ #define ABS_MAXFRAMENR 9999 static int ARG_STATS = 0 ; static int ARG_DUMP = 0 ; static int ARG_LUMIMASKING = 0 ; static int ARG_BITRATE = 0 ; static int ARG_SINGLE = 0 ; static char * ARG_PASS1 = 0 ; static char * ARG_PASS2 = 0 ; static int ARG_QUALITY = ME_ELEMENTS - 1 ; static float ARG_FRAMERATE = 25.00f ; static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; static int ARG_MAXKEYINTERVAL = 0 ; static char * ARG_INPUTFILE = NULL; static int ARG_INPUTTYPE = 0 ; static int ARG_SAVEMPEGSTREAM = 0 ; static int ARG_SAVEINDIVIDUAL = 0 ; static char * ARG_OUTPUTFILE = NULL; static int XDIM = 0 ; static int YDIM = 0 ; static int ARG_BQRATIO = 150 ; static int ARG_BQOFFSET = 100 ; static int ARG_MAXBFRAMES = 0 ; static int ARG_PACKED = 0 ; static int ARG_DEBUG = 0 ; static int ARG_VOPDEBUG = 0 ; static int ARG_GREYSCALE = 0 ; static int ARG_QTYPE = 0 ; static int ARG_QMATRIX = 0 ; static int ARG_GMC = 0 ; static int ARG_INTERLACING = 0 ; static int ARG_QPEL = 0 ; static int ARG_TURBO = 0 ; static int ARG_VHQMODE = 0 ; static int ARG_BVHQ = 0 ; static int ARG_CLOSED_GOP = 0 ; #ifndef READ_PNM #define IMAGE_SIZE(x,y) ((x)*(y)*3/2) #else #define IMAGE_SIZE(x,y) ((x)*(y)*3) #endif #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) #define SMALL_EPS (1e-10) #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | / (((a) & 0x00ff0000 ) >> 8 ) | (((a) & 0xff000000 ) >> 24 ) ) #define FRAMERATE_INCR 1001 /* *************************************************************************** * Nasty global vars ;-) ************************************************************************** */ static unsigned char qmatrix_intra[ 64 ]; static unsigned char qmatrix_inter[ 64 ]; xvidCode::xvidCode() { m_handle = 0 ; } xvidCode:: ~ xvidCode() { exit(); } void xvidCode::exit() { if (m_handle) xvid_encore(m_handle, XVID_ENC_DESTROY, NULL, NULL); m_handle = 0 ; m_bufferlen = 0 ; m_buffer.reset( 0 ); m_codelen = 0 ; }
bool xvidCode::init( int width, int height, int bits) { exit(); XDIM = width; YDIM = height; xvid_gbl_init_t xvid_gbl_init; memset( & xvid_gbl_init, 0 , sizeof (xvid_gbl_init_t)); xvid_gbl_init.version = XVID_VERSION; xvid_gbl_init.debug = XVID_DEBUG_ERROR; xvid_gbl_init.cpu_flags = 0 ; xvid_global(NULL, XVID_GBL_INIT, & xvid_gbl_init, NULL); // return er xvid_enc_create_t xvid_enc_create; xvid_plugin_single_t single; xvid_plugin_2pass1_t rc2pass1; xvid_plugin_2pass2_t rc2pass2; xvid_enc_plugin_t plugins[ 7 ]; memset( & xvid_enc_create, 0 , sizeof (xvid_enc_create_t)); xvid_enc_create.version = XVID_VERSION; xvid_enc_create.width = XDIM; xvid_enc_create.height = YDIM; xvid_enc_create.profile = XVID_PROFILE_AS_L4; xvid_enc_create.zones = ZONES; xvid_enc_create.num_zones = NUM_ZONES; xvid_enc_create.plugins = plugins; xvid_enc_create.num_plugins = 0 ; if (ARG_SINGLE) { memset( & single, 0 , sizeof (xvid_plugin_single_t)); single.version = XVID_VERSION; single.bitrate = ARG_BITRATE; plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single; plugins[xvid_enc_create.num_plugins].param = & single; xvid_enc_create.num_plugins ++ ; } if (ARG_PASS2) { memset( & rc2pass2, 0 , sizeof (xvid_plugin_2pass2_t)); rc2pass2.version = XVID_VERSION; rc2pass2.filename = ARG_PASS2; rc2pass2.bitrate = ARG_BITRATE; plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; plugins[xvid_enc_create.num_plugins].param = & rc2pass2; xvid_enc_create.num_plugins ++ ; } if (ARG_PASS1) { memset( & rc2pass1, 0 , sizeof (xvid_plugin_2pass1_t)); rc2pass1.version = XVID_VERSION; rc2pass1.filename = ARG_PASS1; plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1; plugins[xvid_enc_create.num_plugins].param = & rc2pass1; xvid_enc_create.num_plugins ++ ; } if (ARG_LUMIMASKING) { plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; plugins[xvid_enc_create.num_plugins].param = NULL; xvid_enc_create.num_plugins ++ ; } if (ARG_DUMP) { plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump; plugins[xvid_enc_create.num_plugins].param = NULL; xvid_enc_create.num_plugins ++ ; } #if 0 if (ARG_DEBUG) { plugins[xvid_enc_create.num_plugins].func = rawenc_debug; plugins[xvid_enc_create.num_plugins].param = NULL; xvid_enc_create.num_plugins ++ ; } #endif xvid_enc_create.num_threads = 0 ; if ((ARG_FRAMERATE - ( int ) ARG_FRAMERATE) < SMALL_EPS) { xvid_enc_create.fincr = 1 ; xvid_enc_create.fbase = ( int ) ARG_FRAMERATE; } else { xvid_enc_create.fincr = FRAMERATE_INCR; xvid_enc_create.fbase = ( int ) (FRAMERATE_INCR * ARG_FRAMERATE); } /* Maximum key frame interval */ if (ARG_MAXKEYINTERVAL > 0 ) { xvid_enc_create.max_key_interval = ARG_MAXKEYINTERVAL; } else { xvid_enc_create.max_key_interval = ( int ) ARG_FRAMERATE * 10 ; } /* Maximum key frame interval */ if (ARG_MAXKEYINTERVAL > 0 ) { xvid_enc_create.max_key_interval = ARG_MAXKEYINTERVAL; } else { xvid_enc_create.max_key_interval = ( int ) ARG_FRAMERATE * 10 ; } /* Bframes settings */ xvid_enc_create.max_bframes = ARG_MAXBFRAMES; xvid_enc_create.bquant_ratio = ARG_BQRATIO; xvid_enc_create.bquant_offset = ARG_BQOFFSET; /* Dropping ratio frame -- we don't need that */ xvid_enc_create.frame_drop_ratio = 0 ; /* Global encoder options */ xvid_enc_create. global = 0 ; if (ARG_PACKED) xvid_enc_create. global |= XVID_GLOBAL_PACKED; if (ARG_CLOSED_GOP) xvid_enc_create. global |= XVID_GLOBAL_CLOSED_GOP; if (ARG_STATS) xvid_enc_create. global |= XVID_GLOBAL_EXTRASTATS_ENABLE; int xerr; xerr = xvid_encore(NULL, XVID_ENC_CREATE, & xvid_enc_create, NULL); m_handle = xvid_enc_create.handle; return xerr == 0 ; }
bool xvidCode::code(char *data, int datalen,bool key) { if(m_buffer.get()==0) { m_bufferlen = IMAGE_SIZE(XDIM, YDIM) * 2; m_buffer.reset((unsigned char *) malloc(m_bufferlen)); } int ret; xvid_enc_frame_t xvid_enc_frame; xvid_enc_stats_t xvid_enc_stats; /* Version for the frame and the stats */ memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); xvid_enc_frame.version = XVID_VERSION; memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); xvid_enc_stats.version = XVID_VERSION; /* Bind output buffer */ xvid_enc_frame.bitstream = m_buffer.get(); xvid_enc_frame.length = -1; xvid_enc_frame.input.csp = XVID_CSP_BGR; xvid_enc_frame.input.plane[0] = data; xvid_enc_frame.input.stride[0] = XDIM*3; /* Set up core's general features */ xvid_enc_frame.vol_flags = 0; if (ARG_STATS) xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS; if (ARG_QTYPE) xvid_enc_frame.vol_flags |= XVID_VOL_MPEGQUANT; if (ARG_QPEL) xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL; if (ARG_GMC) xvid_enc_frame.vol_flags |= XVID_VOL_GMC; if (ARG_INTERLACING) xvid_enc_frame.vol_flags |= XVID_VOL_INTERLACING; /* Set up core's general features */ xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; if (ARG_VOPDEBUG) { xvid_enc_frame.vop_flags |= XVID_VOP_DEBUG; } if (ARG_GREYSCALE) { xvid_enc_frame.vop_flags |= XVID_VOP_GREYSCALE; } /* Frame type -- let core decide for us */ if(key) xvid_enc_frame.type=XVID_TYPE_IVOP; else xvid_enc_frame.type = XVID_TYPE_AUTO; /* Force the right quantizer -- It is internally managed by RC plugins */ xvid_enc_frame.quant = 0; /* Set up motion estimation flags */ xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; if (ARG_GMC) xvid_enc_frame.motion |= XVID_ME_GME_REFINE; if (ARG_QPEL) xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16; if (ARG_QPEL && (xvid_enc_frame.vop_flags & XVID_VOP_INTER4V)) xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8; if (ARG_TURBO) xvid_enc_frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 | XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE | XVID_ME_BFRAME_EARLYSTOP; if (ARG_BVHQ) xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP; switch (ARG_VHQMODE) /* this is the same code as for vfw */ { case 1: /* VHQ_MODE_DECISION */ xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD; break; case 2: /* VHQ_LIMITED_SEARCH */ xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD; xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD; xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; break; case 3: /* VHQ_MEDIUM_SEARCH */ xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD; xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD; xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE8_RD; xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8_RD; xvid_enc_frame.motion |= XVID_ME_CHECKPREDICTION_RD; break; case 4: /* VHQ_WIDE_SEARCH */ xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD; xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD; xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE8_RD; xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8_RD; xvid_enc_frame.motion |= XVID_ME_CHECKPREDICTION_RD; xvid_enc_frame.motion |= XVID_ME_EXTSEARCH_RD; break; default : break; } if (ARG_QMATRIX) { /* We don't use special matrices */ xvid_enc_frame.quant_intra_matrix = qmatrix_intra; xvid_enc_frame.quant_inter_matrix = qmatrix_inter; } else { /* We don't use special matrices */ xvid_enc_frame.quant_intra_matrix = NULL; xvid_enc_frame.quant_inter_matrix = NULL; } ret = xvid_encore(m_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); m_codelen = xvid_enc_stats.length; m_keyframe = xvid_enc_frame.out_flags & XVID_KEYFRAME; return true; }
har * source = 你的视频单帧数据; int sourcelen = 视频单帧长度; xvidCode codec; codec.init(160,120,24);// 160*120 24位 codec.code(data.data.get(),data.datalen,false); char * dst = codec.get_data();//xvid编码以后的数据 int dstlen = codec.get_datalen();//xvid编码以后的数据长度 int keyframe = codec.get_keyframe();//是否为关键帧