/* System wide init, do it once for all */ void xvid4_init (void) { MMSET (xvid_gbl_init2); MMSET (xvid_gbl_info); printf ("[xvid] Initializing global Xvid 4\n"); xvid_gbl_init2.version = XVID_VERSION; xvid_global (NULL, XVID_GBL_INIT, &xvid_gbl_init2, NULL); xvid_gbl_info.version = XVID_VERSION; xvid_global (NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL); if (xvid_gbl_info.build) printf ("[xvid] Build: %s\n", xvid_gbl_info.build); printf ("[xvid] SIMD supported: (%x)\n", xvid_gbl_info.cpu_flags); #define CPUF(x) if(xvid_gbl_info.cpu_flags & XVID_CPU_##x) printf("\t\t"#x"\n"); #if defined( ARCH_X86) || defined(ARCH_X86_64) CPUF (MMX); CPUF (MMXEXT); CPUF (SSE); CPUF (SSE2); CPUF (3DNOW); CPUF (3DNOWEXT); #endif #ifdef USE_ALTIVEC CPUF (ALTIVEC); #endif }
// ************************************************* // ************************************************* // Pass1 // ************************************************* // ************************************************* uint8_t xvid4EncoderPass1::init (uint32_t br, uint32_t fps1000, xvid4EncParam * param) { int xerr; printf ("[xvid] Pass 1 of 2 (log: %s)\n", (char *) param->logName); _fps1000 = fps1000; memcpy (&_param, param, sizeof (_param)); dump (); // Here we go... MMSET (xvid_enc_create); MMSET (pass1); xvid_enc_create.version = XVID_VERSION; xvid_enc_create.width = _w; xvid_enc_create.height = _h; plugins[0].func = xvid_plugin_2pass1; plugins[0].param = &pass1; pass1.version = XVID_VERSION; pass1.filename = (char *) _param.logName; xvid_enc_create.plugins = plugins; xvid_enc_create.num_plugins = 1; //Framerate xvid_enc_create.fincr = 1000; xvid_enc_create.fbase = _fps1000; // createUpdate (); // Force Qz=2 ! xerr = xvid_encore (NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); if (xerr < 0) { printf ("[xvid] Init error: %d\n", xerr); return 0; } _handle = xvid_enc_create.handle; printf ("[xvid] Pass 1 init Ok\n"); return 1; }
int FFV1_XvidCompress(unsigned char *in,unsigned char *out,uint32_t *outlen) { int ret; MMSET(xvid_enc_frame); MMSET(xvid_enc_stats); xvid_enc_frame.version = XVID_VERSION; xvid_enc_stats.version = XVID_VERSION; xvid_enc_frame.length = 0; xvid_enc_frame.vop_flags|=XVID_VOP_HALFPEL; if(vinfo.quant) xvid_enc_frame.vol_flags|=XVID_VOL_MPEGQUANT; xvid_enc_frame.input.csp = XVID_CSP_YV12; xvid_enc_frame.input.stride[0] = _w; xvid_enc_frame.input.stride[1] = _w>>1; xvid_enc_frame.input.stride[2] = _w>>1; xvid_enc_frame.type = XVID_TYPE_AUTO; /* Set up motion estimation flags */ xvid_enc_frame.input.plane[0] = in; xvid_enc_frame.input.plane[2] = in+(_w*_h); xvid_enc_frame.input.plane[1] = in+((_w*_h*5)>>2); xvid_enc_frame.bitstream = out; xvid_enc_frame.quant = vinfo.quality; xvid_enc_frame.motion = X4motion_presets[vinfo.me]; if(nextIsKF) { xvid_enc_frame.type = XVID_TYPE_IVOP; // let the codec decide between I-frame (1) and P-frame (0) nextIsKF=0; } else { xvid_enc_frame.type = XVID_TYPE_VOL; // let the codec decide between I-frame (1) and P-frame (0) } ret = xvid_encore(xvid_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); *outlen = ret; #ifdef ADM_DEBUG printf("Outlen: %lu\n",*outlen); #endif return 1; }
// ************************************************* // ************************************************* // CBR // ************************************************* // ************************************************* uint8_t xvid4EncoderCBR::init (uint32_t br, uint32_t fps1000, xvid4EncParam * param) { int xerr; printf ("[xvid] CBR mode = %lu kbps\n", br); _bitrate = br; _fps1000 = fps1000; memcpy (&_param, param, sizeof (_param)); dump (); // Here we go... MMSET (single); MMSET (xvid_enc_create); xvid_enc_create.version = XVID_VERSION; xvid_enc_create.width = _w; xvid_enc_create.height = _h; plugins[0].func = xvid_plugin_single; plugins[0].param = &single; plugins[1].func = adm_hook; plugins[1].param = NULL; single.version = XVID_VERSION; single.bitrate = _bitrate * 1000; // Kb->bit xvid_enc_create.plugins = plugins; xvid_enc_create.num_plugins = 2; //Framerate xvid_enc_create.fincr = 1000; xvid_enc_create.fbase = _fps1000; // createUpdate (); xerr = xvid_encore (NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); if (xerr < 0) { printf ("[xvid] Init error: %d\n", xerr); return 0; } _handle = xvid_enc_create.handle; printf ("[xvid] CBR init Ok\n"); return 1; }
/** \fn preAmble \fn prepare a frame to be encoded */ bool x264Encoder::preAmble (ADMImage * in) { MMSET(pic); pic.img.i_csp = X264_CSP_I420; pic.img.i_plane = 3; pic.img.plane[0] = YPLANE(in); pic.img.plane[2] = UPLANE(in); pic.img.plane[1] = VPLANE(in); pic.img.i_stride[0] = in->GetPitch(PLANAR_Y); pic.img.i_stride[1] = in->GetPitch(PLANAR_U); pic.img.i_stride[2] = in->GetPitch(PLANAR_V); pic.i_type = X264_TYPE_AUTO; pic.i_pts = in->Pts; return true; }
/** \fn setup */ bool x265Encoder::setup(void) { ADM_info("=============x265, setting up==============\n"); MMSET(param); x265_param_default( ¶m); firstIdr=true; image=new ADMImageDefault(getWidth(),getHeight()); // -------------- preset, tune, idc ------------ if(!x265Settings.useAdvancedConfiguration) { char tune[200] = {0}; strcat(tune, x265Settings.general.tuning); x265_param_default_preset(¶m, x265Settings.general.preset, tune); } param.logLevel=x265Settings.level; // Threads.. #if X265_BUILD < 47 switch(x265Settings.general.poolThreads) { case 0: case 1: case 2: param.poolNumThreads = x265Settings.general.poolThreads; break; case 99: break; //auto default: ADM_error("UNKNOWN NB OF THREADS\n"); break; } #endif switch(x265Settings.general.frameThreads) { case 0: case 1: case 2: param.frameNumThreads = x265Settings.general.frameThreads; break; case 99: break; //auto default: ADM_error("UNKNOWN NB OF THREADS\n"); break; } param.sourceWidth = getWidth(); param.sourceHeight = getHeight(); param.internalCsp = X265_CSP_I420; param.internalBitDepth = 8; param.logLevel=X265_LOG_INFO; //DEBUG; //INFO; //Framerate int n,d; uint64_t f=source->getInfo()->frameIncrement; usSecondsToFrac(f,&n,&d); param.fpsNum = d; param.fpsDenom = n; // -------------- vui------------ #undef MKPARAM #undef MKPARAMD #undef MKPARAMB #define MKPARAM(x,y) {param.vui.x = x265Settings.vui.y;aprintf("[x265] vui."#x" = %d\n",param.vui.x);} #define MKPARAMD(x,y) {param.vui.x = (double)x265Settings.vui.y; aprintf("[x265] vui."#x" = %.2f\n",param.vui.x);} #define MKPARAMB(x,y) {param.vui.x = x265Settings.vui.y ;aprintf("[x265] vui."#x" = %s\n",TrueFalse[param.vui.x&1]);} MKPARAM (sarWidth,sar_width) MKPARAM (sarHeight,sar_height) // -------------- rate control------------ switch(x265Settings.general.params.mode) { case COMPRESS_2PASS: case COMPRESS_2PASS_BITRATE: uint32_t bitrate; if(passNumber!=1 && passNumber!=2) { ADM_error("No pass number specified! (%d)\n",(int)passNumber); return false; } ADM_info("Starting pass :%d\n",passNumber); if(x265Settings.general.params.mode==COMPRESS_2PASS) { uint64_t duration=source->getInfo()->totalDuration; // in us ADM_info("Source duration :%s\n",ADM_us2plain(duration)); ADM_info("Target size :%d\n",(int)x265Settings.general.params.finalsize); uint32_t avg; if(false==ADM_computeAverageBitrateFromDuration(duration, x265Settings.general.params.finalsize, &avg)) { ADM_error("[x265] No source duration!\n"); return false; } bitrate=(uint32_t)avg; } else bitrate=x265Settings.general.params.avg_bitrate; ADM_info("Using average bitrate of %d kb/s\n",(int)bitrate); param.rc.rateControlMode = X265_RC_ABR; param.rc.bitrate = bitrate; if(passNumber==1) { param.rc.bStatWrite=1; param.rc.bStatRead=0; param.rc.statFileName=strdup(logFile); } else { param.rc.bStatWrite=0; param.rc.bStatRead=1; param.rc.statFileName=strdup(logFile); if(!ADM_fileExist(logFile)) { ADM_error("Logfile %s does not exist \n",logFile); return false; } } break; case COMPRESS_AQ: param.rc.rateControlMode = X265_RC_CRF; param.rc.rfConstant = x265Settings.general.params.qz; break; case COMPRESS_CQ: param.rc.rateControlMode = X265_RC_CQP; param.rc.qp = x265Settings.general.params.qz; break; case COMPRESS_CBR: param.rc.rateControlMode = X265_RC_ABR; param.rc.bitrate = x265Settings.general.params.bitrate; param.rc.qp = 0; param.rc.rfConstant = 0; break; default: GUI_Error_HIG("Not coded","this mode has notbeen implemented\n"); return false; break; } if(globalHeader) param.bRepeatHeaders=0; else param.bRepeatHeaders=1; if(x265Settings.useAdvancedConfiguration) { #undef MKPARAM #undef MKPARAMD #undef MKPARAMB #define MKPARAM(x,y) {param.x = x265Settings.y;aprintf("[x265] "#x" = %d\n",param.x);} #define MKPARAMD(x,y) {param.x = (double)x265Settings.y; aprintf("[x265] "#x" = %.2f\n",param.x);} #define MKPARAMB(x,y) {param.x = x265Settings.y ;aprintf("[x265] "#x" = %s\n",TrueFalse[param.x&1]);} MKPARAM(maxNumReferences,MaxRefFrames); MKPARAM(keyframeMin,MinIdr); MKPARAM(keyframeMax,MaxIdr); MKPARAM(scenecutThreshold,i_scenecut_threshold); MKPARAM(bframes,MaxBFrame); MKPARAM(bFrameAdaptive,i_bframe_adaptive); MKPARAM(bFrameBias,i_bframe_bias); MKPARAM(bBPyramid,i_bframe_pyramid); MKPARAMB(bEnableLoopFilter,b_deblocking_filter); MKPARAMB(interlaceMode,interlaced_mode); MKPARAMB(bEnableConstrainedIntra,constrained_intra); MKPARAM(lookaheadDepth,lookahead); MKPARAMB(bEnableWeightedBiPred,weighted_bipred) MKPARAM (bEnableWeightedPred,weighted_pred) MKPARAM (cbQpOffset,cb_chroma_offset) MKPARAM (crQpOffset,cr_chroma_offset) MKPARAM (searchMethod,me_method) MKPARAM (searchRange,me_range) MKPARAM (subpelRefine,subpel_refine) MKPARAM (bFrameAdaptive,trellis) MKPARAMB(bEnableEarlySkip,fast_pskip) MKPARAMB(bEnableTSkipFast,dct_decimate) MKPARAMD(psyRd,psy_rd) #if X265_BUILD >= 40 MKPARAM (noiseReductionIntra,noise_reduction_intra) MKPARAM (noiseReductionInter,noise_reduction_inter) #else MKPARAM (noiseReduction,noise_reduction) #endif //---------------- ratecontrol ------------------- #undef MKPARAM #undef MKPARAMD #undef MKPARAMB #define MKPARAM(x,y) {param.rc.x = x265Settings.ratecontrol.y;aprintf("[x265] rc."#x" = %d\n",param.rc.x);} #define MKPARAMD(x,y) {param.rc.x = (double)x265Settings.ratecontrol.y; aprintf("[x265] rc."#x" = %.2f\n",param.rc.x);} #define MKPARAMB(x,y) {param.rc.x = x265Settings.ratecontrol.y ;aprintf("[x265] rc."#x" = %s\n",TrueFalse[param.rc.x&1]);} MKPARAM(qpStep,qp_step); #if X265_BUILD >= 41 MKPARAMB(bStrictCbr,strict_cbr); #else MKPARAM(rateTolerance,rate_tolerance); #endif MKPARAM(ipFactor,ip_factor); MKPARAM(pbFactor,pb_factor); MKPARAMB(cuTree,cu_tree); MKPARAM(aqMode,aq_mode); MKPARAMD(aqStrength,aq_strength); } if(!param.bframes) encoderDelay=0; else { if(2>=param.maxNumReferences) { encoderDelay=f*2*2; } else { encoderDelay=2*f*(x265Settings.MaxRefFrames-1); } } if(!x265Settings.useAdvancedConfiguration) { x265_param_apply_profile(¶m, x265Settings.general.profile); } dumpx265Setup(¶m); ADM_info("Creating x265 encoder\n"); handle = x265_encoder_open (¶m); if (!handle) { ADM_error("Cannot initialize x265\n"); return 0; } ADM_info("x265, setup ok\n"); if (globalHeader) { ADM_info("Creating global header\n"); return createHeader (); } else ADM_info("No need for global header\n"); return true; }
int FFV1_XvidInit(v4linfo *info) { int err; printf("Initializing Xvid4 with width =%d, height = %d in YUV420P format\n",info->width,info->height); MMSET(xvid_gbl_init2); MMSET(xvid_gbl_info); printf("Initializing global xvid 4\n"); xvid_gbl_init2.version = XVID_VERSION; xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init2, NULL); xvid_gbl_info.version = XVID_VERSION; xvid_global(NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL); if(xvid_gbl_info.build) { printf("\txvid build:%s\n",xvid_gbl_info.build); } printf("\txvid thread:%d\n",xvid_gbl_info.num_threads); printf("\txvid SIMD supported:(%x)\n",xvid_gbl_info.cpu_flags); // MMSET(xvid_enc_create); xvid_enc_create.version = XVID_VERSION; xvid_enc_create.width = info->width; xvid_enc_create.height = info->height; MMSET(single); plugins[0].func = xvid_plugin_single; plugins[0].param = &single; single.version = XVID_VERSION; single.bitrate = 1500; xvid_enc_create.plugins = plugins; xvid_enc_create.num_plugins = 1; if(info->ntsc) xvid_enc_create.fbase =29970; else xvid_enc_create.fbase =25000; //Framerate xvid_enc_create.fincr = 1000; // // err = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); if(err<0) { printf("Xvid-4 init error :%d\n",err); return 0; } xvid_handle = xvid_enc_create.handle; printf("Xvid-4 CQ init Ok\n"); return 1; }
// ************************************************* // ************************************************* // Pass2 // ************************************************* // ************************************************* uint8_t xvid4EncoderPass2::init (uint32_t br, uint32_t fps1000, xvid4EncParam * param) { int xerr; printf ("[xvid] Pass 2 of 2, average bitrate = %lu\n", br); _bitrate = br; _fps1000 = fps1000; memcpy (&_param, param, sizeof (_param)); dump (); // Here we go... MMSET (xvid_enc_create); MMSET (pass2); xvid_enc_create.version = XVID_VERSION; xvid_enc_create.width = _w; xvid_enc_create.height = _h; plugins[0].func = xvid_plugin_2pass2; plugins[0].param = &pass2; plugins[1].func = adm_hook; plugins[1].param = NULL; pass2.version = XVID_VERSION; pass2.filename = (char *) _param.logName; pass2.bitrate = br; // Average bitrate #define CPY(x) pass2.x=_param.x;printf("[xvid] "#x"=%d\n",pass2.x); CPY (keyframe_boost); CPY (curve_compression_high); CPY (curve_compression_low); CPY (overflow_control_strength); CPY (max_overflow_improvement); CPY (max_overflow_degradation); CPY (kfreduction); CPY (kfthreshold); CPY (container_frame_overhead); xvid_enc_create.plugins = plugins; xvid_enc_create.num_plugins = 2; //Framerate xvid_enc_create.fincr = 1000; xvid_enc_create.fbase = _fps1000; // createUpdate (); xerr = xvid_encore (NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); if (xerr < 0) { printf ("[xvid] Init error: %d\n", xerr); return 0; } _handle = xvid_enc_create.handle; printf ("[xvid] Init Ok\n"); return 1; }
uint8_t xvid4Encoder::preAmble (uint8_t * in) { /* Version for the frame and the stats */ //MMSET (xvid_enc_frame); MMSET (xvid_enc_stats); xvid_enc_frame.version = XVID_VERSION; xvid_enc_stats.version = XVID_VERSION; /* Bind output buffer */ xvid_enc_frame.length = 0; if (_param.mpegQuantizer) xvid_enc_frame.vol_flags |= XVID_VOL_MPEGQUANT; if (_param.interlaced) xvid_enc_frame.vol_flags |= XVID_VOL_INTERLACING; #define SVOP(x,y) if(_param.x) xvid_enc_frame.vop_flags|=XVID_VOP_##y xvid_enc_frame.motion = motion_presets[_param.guiLevel]; SVOP (inter4mv, INTER4V); SVOP (trellis, TRELLISQUANT); SVOP (hqac, HQACPRED); #if XVID_API==XVID_MAKE_API(4, 1) SVOP (bvhq, RD_BVOP); #endif SVOP (greyscale, GREYSCALE); SVOP (cartoon, CARTOON); SVOP (chroma_opt, CHROMAOPT); //SVOP( 1 ,HALFPEL); xvid_enc_frame.vop_flags |= XVID_VOP_HALFPEL; // ME if (_param.chroma_me) { xvid_enc_frame.motion |= XVID_ME_CHROMA_BVOP; xvid_enc_frame.motion |= XVID_ME_CHROMA_PVOP; } if (_param.cartoon) { xvid_enc_frame.motion |= XVID_ME_DETECT_STATIC_MOTION; } //printf("Vhq mode : %d\n",_param.vhqmode); #define VHME(x) xvid_enc_frame.motion|= XVID_ME_##x switch (_param.vhqmode) { case 4: VHME (EXTSEARCH_RD); case 3: VHME (HALFPELREFINE8_RD); VHME (QUARTERPELREFINE8_RD); VHME (CHECKPREDICTION_RD); case 2: VHME (HALFPELREFINE16_RD); VHME (QUARTERPELREFINE16_RD); case 1: SVOP (vhqmode, MODEDECISION_RD); case 0: break; default: ADM_assert (0); } if (_param.qpel) { xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL; xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16; xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8; } if (_param.gmc) { xvid_enc_frame.vol_flags |= XVID_VOL_GMC; xvid_enc_frame.motion |= XVID_ME_GME_REFINE; } if (_param.turbo) { xvid_enc_frame.motion |= XVID_ME_FASTREFINE16; xvid_enc_frame.motion |= XVID_ME_FASTREFINE8; xvid_enc_frame.motion |= XVID_ME_SKIP_DELTASEARCH; xvid_enc_frame.motion |= XVID_ME_FAST_MODEINTERPOLATE; xvid_enc_frame.motion |= XVID_ME_BFRAME_EARLYSTOP; } xvid_enc_frame.bframe_threshold = _param.bframe_threshold; xvid_enc_frame.input.csp = XVID_CSP_YV12; xvid_enc_frame.input.stride[0] = _w; xvid_enc_frame.input.stride[1] = _w >> 1; xvid_enc_frame.input.stride[2] = _w >> 1; xvid_enc_frame.type = XVID_TYPE_AUTO; /* Set up motion estimation flags */ xvid_enc_frame.input.plane[0] = in; xvid_enc_frame.input.plane[1] = in + (_w * _h); xvid_enc_frame.input.plane[2] = in + ((_w * _h * 5) >> 2); xvid_enc_frame.par_width = _param.par_width; xvid_enc_frame.par_height = _param.par_height; //printf("Using AR : %u x %u\n",xvid_enc_frame.par_width,xvid_enc_frame.par_height ); if (xvid_enc_frame.par_width != xvid_enc_frame.par_height) xvid_enc_frame.par = XVID_PAR_EXT; else xvid_enc_frame.par = XVID_PAR_11_VGA; /* Custome matrices */ if(_param.useCustomIntra) { if(!xvid_enc_frame.quant_intra_matrix) printf("[xvid] Using custom intra matrix\n"); xvid_enc_frame.quant_intra_matrix=_param.intraMatrix; } if(_param.useCustomInter) { if(!xvid_enc_frame.quant_inter_matrix) printf("[xvid] Using custom inter matrix\n"); xvid_enc_frame.quant_inter_matrix=_param.interMatrix; } return 1; }
void xvid4Encoder::createUpdate (void) { uint32_t threads = 0; prefs->get(FEATURE_THREADING_XVID, &threads); if (threads == 0) xvid_enc_create.num_threads = xvid_gbl_info.num_threads; else if (threads > 1) xvid_enc_create.num_threads = threads; else xvid_enc_create.num_threads = 0; if (xvid_enc_create.num_threads) printf ("[xvid] using %u threads\n", xvid_enc_create.num_threads); xvid_enc_create.max_bframes = _param.bframes; xvid_enc_create.max_key_interval = _param.max_key_interval; xvid_enc_create.bquant_ratio = _param.bquant_ratio; xvid_enc_create.bquant_offset = _param.bquant_offset; if (_param.packed) xvid_enc_create.global |=XVID_GLOBAL_PACKED; if (_param.closed_gop) xvid_enc_create.global |=XVID_GLOBAL_CLOSED_GOP; for (uint32_t i = 0; i < 3; i++) { xvid_enc_create.min_quant[i] = _param.qmin[i]; xvid_enc_create.max_quant[i] = _param.qmax[i]; } preAmble (NULL); #define C_VOP(x) if(xvid_enc_frame.vop_flags& XVID_VOP_##x) printf("[xvid] Vop: "#x" is set\n"); #define C_VOL(x) if(xvid_enc_frame.vol_flags& XVID_VOL_##x) printf("[xvid] Vol: "#x" is set\n"); #define C_ME(x) if(xvid_enc_frame.motion& XVID_ME_##x) printf("[xvid] ME: "#x" is set\n"); C_VOP (DEBUG); C_VOP (CHROMAOPT); C_VOP (HALFPEL); C_VOP (INTER4V); C_VOP (TRELLISQUANT); C_VOP (CHROMAOPT); C_VOP (CARTOON); C_VOP (GREYSCALE); C_VOP (HQACPRED); C_VOP (MODEDECISION_RD); C_VOL (MPEGQUANT); C_VOL (EXTRASTATS); C_VOL (GMC); C_VOL (QUARTERPEL); C_VOL (INTERLACING); C_ME (ADVANCEDDIAMOND16); C_ME (ADVANCEDDIAMOND8); C_ME (USESQUARES16); C_ME (USESQUARES8); C_ME (HALFPELREFINE16); C_ME (HALFPELREFINE8); // Turbo Mode C_ME (FASTREFINE16); C_ME (FASTREFINE8); C_ME (SKIP_DELTASEARCH); C_ME (FAST_MODEINTERPOLATE); C_ME (BFRAME_EARLYSTOP); C_ME (GME_REFINE); C_ME (QUARTERPELREFINE8); C_ME (QUARTERPELREFINE16); C_ME (EXTSEARCH16); C_ME (EXTSEARCH8); C_ME (CHROMA_BVOP); C_ME (CHROMA_PVOP); MMSET (xvid_enc_frame); }