📄 xvid_encraw.c
字号:
u += XDIM / 2; v += XDIM / 2; } /* I don't know why, but this seems needed */ fread(&dummy, 1, 1, handle); return (0);}#elsestatic intread_pnmheader(FILE * handle){ int bytes, xsize, ysize, depth; char dummy[2]; bytes = fread(dummy, 1, 2, handle); if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '6')) return (1); fscanf(handle, "%d %d %d", &xsize, &ysize, &depth); if ((xsize > 1440) || (ysize > 2880) || (depth != 255)) { fprintf(stderr, "%d %d %d\n", xsize, ysize, depth); return (2); } XDIM = xsize; YDIM = ysize; return (0);}static intread_pnmdata(FILE * handle, unsigned char *image){ int i; char dummy; /* read Y component of picture */ fread(image, 1, XDIM * YDIM * 3, handle); /* I don't know why, but this seems needed */ fread(&dummy, 1, 1, handle); return (0);}#endifstatic intread_yuvdata(FILE * handle, unsigned char *image){ if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int) IMAGE_SIZE(XDIM, YDIM)) return (1); else return (0);}/***************************************************************************** * Routines for encoding: init encoder, frame step, release encoder ****************************************************************************//* sample plugin */intrawenc_debug(void *handle, int opt, void *param1, void *param2){ switch (opt) { case XVID_PLG_INFO: { xvid_plg_info_t *info = (xvid_plg_info_t *) param1; info->flags = XVID_REQDQUANTS; return 0; } case XVID_PLG_CREATE: case XVID_PLG_DESTROY: case XVID_PLG_BEFORE: return 0; case XVID_PLG_AFTER: { xvid_plg_data_t *data = (xvid_plg_data_t *) param1; int i, j; printf("---[ frame: %5i quant: %2i length: %6i ]---\n", data->frame_num, data->quant, data->length); for (j = 0; j < data->mb_height; j++) { for (i = 0; i < data->mb_width; i++) printf("%2i ", data->dquant[j * data->dquant_stride + i]); printf("\n"); } return 0; } } return XVID_ERR_FAIL;}#define FRAMERATE_INCR 1001/* Initialize encoder for first use, pass all needed parameters to the codec */static intenc_init(int use_assembler){ int xerr; //xvid_plugin_cbr_t cbr; xvid_plugin_single_t single; xvid_plugin_2pass1_t rc2pass1; xvid_plugin_2pass2_t rc2pass2; //xvid_plugin_fixed_t rcfixed; xvid_enc_plugin_t plugins[7]; xvid_gbl_init_t xvid_gbl_init; xvid_enc_create_t xvid_enc_create; /*------------------------------------------------------------------------ * XviD core initialization *----------------------------------------------------------------------*/ /* Set version -- version checking will done by xvidcore */ memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); xvid_gbl_init.version = XVID_VERSION; xvid_gbl_init.debug = ARG_DEBUG; /* Do we have to enable ASM optimizations ? */ if (use_assembler) {#ifdef ARCH_IS_IA64 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ASM;#else xvid_gbl_init.cpu_flags = 0;#endif } else { xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; } /* Initialize XviD core -- Should be done once per __process__ */ xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); /*------------------------------------------------------------------------ * XviD encoder initialization *----------------------------------------------------------------------*/ /* Version again */ memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); xvid_enc_create.version = XVID_VERSION; /* Width and Height of input frames */ xvid_enc_create.width = XDIM; xvid_enc_create.height = YDIM; xvid_enc_create.profile = XVID_PROFILE_AS_L4; /* init plugins */ 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;/* An example of activating VBV could look like this rc2pass2.vbv_size = 3145728; rc2pass2.vbv_initial = 2359296; rc2pass2.vbv_maxrate = 4000000; rc2pass2.vbv_peakrate = 10000000;*/ 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 /* No fancy thread tests */ xvid_enc_create.num_threads = 0; /* Frame rate - Do some quick float fps = fincr/fbase hack */ 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; } /* 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; /* I use a small value here, since will not encode whole movies, but short clips */ xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); /* Retrieve the encoder instance from the structure */ enc_handle = xvid_enc_create.handle; return (xerr);}static intenc_stop(){ int xerr; /* Destroy the encoder instance */ xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); return (xerr);}static intenc_main(unsigned char *image, unsigned char *bitstream, int *key, int *stats_type, int *stats_quant, int *stats_length, int sse[3]){ 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 = bitstream; xvid_enc_frame.length = -1; /* Initialize input image fields */ if (image) { xvid_enc_frame.input.plane[0] = image;#ifndef READ_PNM if (ARG_INPUTTYPE==2) xvid_enc_frame.input.csp = XVID_CSP_YV12; else xvid_enc_frame.input.csp = XVID_CSP_I420; xvid_enc_frame.input.stride[0] = XDIM;#else xvid_enc_frame.input.csp = XVID_CSP_BGR; xvid_enc_frame.input.stride[0] = XDIM*3;#endif } else { xvid_enc_frame.input.csp = XVID_CSP_NULL; } /* 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 */ 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; } /* Encode the frame */ ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME); *stats_type = xvid_enc_stats.type; *stats_quant = xvid_enc_stats.quant; *stats_length = xvid_enc_stats.length; sse[0] = xvid_enc_stats.sse_y; sse[1] = xvid_enc_stats.sse_u; sse[2] = xvid_enc_stats.sse_v; return (ret);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -