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

📄 codec.c

📁 TMS320C6713 视频压缩算法源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	create.min_quant[0] = codec->config.min_iquant;	create.max_quant[0] = codec->config.max_iquant;	create.min_quant[1] = codec->config.min_pquant;	create.max_quant[1] = codec->config.max_pquant;	create.min_quant[2] = codec->config.min_bquant;	create.max_quant[2] = codec->config.max_bquant;	if ((profiles[codec->config.profile].flags & PROFILE_BVOP) && codec->config.use_bvop) {		create.max_bframes = codec->config.max_bframes;		create.bquant_ratio = codec->config.bquant_ratio;		create.bquant_offset = codec->config.bquant_offset;		if (codec->config.packed) 			create.global |= XVID_GLOBAL_PACKED;		if (codec->config.closed_gov) 			create.global |= XVID_GLOBAL_CLOSED_GOP;	}	create.frame_drop_ratio = codec->config.frame_drop_ratio;	create.num_threads = codec->config.num_threads;	switch(codec->xvid_encore_func(0, XVID_ENC_CREATE, &create, NULL))	{	case XVID_ERR_FAIL :			return ICERR_ERROR;	case XVID_ERR_MEMORY :		return ICERR_MEMORY;	case XVID_ERR_FORMAT :		return ICERR_BADFORMAT;	case XVID_ERR_VERSION :		return ICERR_UNSUPPORTED;	}	free(create.zones);	codec->ehandle = create.handle;	codec->framenum = 0;	codec->keyspacing = 0;	if (codec->config.display_status) {		status_destroy_always(&codec->status);		status_create(&codec->status, codec->fincr, codec->fbase);	}	return ICERR_OK;}LRESULT compress_end(CODEC * codec){	if (codec->m_hdll != NULL) {		if (codec->ehandle != NULL) {			codec->xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);			codec->ehandle = NULL;		}	}	if (codec->config.display_status)		status_destroy(&codec->status);	return ICERR_OK;}static void apply_zone_modifiers(xvid_enc_frame_t * frame, CONFIG * config, int framenum){	int i;	for (i=0; i<config->num_zones && config->zones[i].frame <= framenum; i++) ;	if (--i < 0) return; /* there are no zones, or we're before the first zone */	if (framenum == config->zones[i].frame)		frame->type = config->zones[i].type;	if (config->zones[i].greyscale) {		frame->vop_flags |= XVID_VOP_GREYSCALE;	}	if (config->zones[i].chroma_opt) {		frame->vop_flags |= XVID_VOP_CHROMAOPT;	}	if ((profiles[config->profile].flags & PROFILE_BVOP) && config->use_bvop) {		frame->bframe_threshold = config->zones[i].bvop_threshold;	}}LRESULT compress(CODEC * codec, ICCOMPRESS * icc){	BITMAPINFOHEADER * inhdr = icc->lpbiInput;	BITMAPINFOHEADER * outhdr = icc->lpbiOutput;	xvid_enc_frame_t frame;	xvid_enc_stats_t stats;	int length;		memset(&frame, 0, sizeof(frame));	frame.version = XVID_VERSION;	frame.type = XVID_TYPE_AUTO;	/* vol stuff */	if ((profiles[codec->config.profile].flags & PROFILE_MPEGQUANT) && 		codec->config.quant_type != QUANT_MODE_H263)	{		frame.vol_flags |= XVID_VOL_MPEGQUANT;		if (codec->config.quant_type == QUANT_MODE_CUSTOM) {			frame.quant_intra_matrix = codec->config.qmatrix_intra;			frame.quant_inter_matrix = codec->config.qmatrix_inter;		}else{			frame.quant_intra_matrix = NULL;			frame.quant_inter_matrix = NULL;		}	}	if ((profiles[codec->config.profile].flags & PROFILE_REDUCED) &&		codec->config.reduced_resolution) {		frame.vol_flags |= XVID_VOL_REDUCED_ENABLE;		frame.vop_flags |= XVID_VOP_REDUCED;	/* XXX: need auto decion mode */	}	if ((profiles[codec->config.profile].flags & PROFILE_QPEL) && codec->config.qpel) {		frame.vol_flags |= XVID_VOL_QUARTERPEL;		frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8;	}	if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc) {		frame.vol_flags |= XVID_VOL_GMC;		frame.motion |= XVID_ME_GME_REFINE;	}	if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing)		frame.vol_flags |= XVID_VOL_INTERLACING;	if (codec->config.ar_mode == 0) { /* PAR */		if (codec->config.display_aspect_ratio != 5) {			frame.par = codec->config.display_aspect_ratio + 1;		} else {			frame.par = XVID_PAR_EXT;			frame.par_width = codec->config.par_x;			frame.par_height= codec->config.par_y;		}	} else { /* AR */		/* custom pixel aspect ratio -> calculated from DAR */		frame.par = XVID_PAR_EXT;		frame.par_width = (100 * inhdr->biHeight) / codec->config.ar_y;		frame.par_height= (100 * inhdr->biWidth) / codec->config.ar_x;	}	/* vop stuff */	frame.vop_flags |= XVID_VOP_HALFPEL;	frame.vop_flags |= XVID_VOP_HQACPRED;	if (codec->config.vop_debug) 		frame.vop_flags |= XVID_VOP_DEBUG;	if (codec->config.trellis_quant) {		frame.vop_flags |= XVID_VOP_TRELLISQUANT;	}	if (codec->config.motion_search > 4)		frame.vop_flags |= XVID_VOP_INTER4V;	if (codec->config.chromame)		frame.motion |= XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP;	if (codec->config.cartoon_mode) {		frame.vop_flags |= XVID_VOP_CARTOON;		frame.motion |= XVID_ME_DETECT_STATIC_MOTION;	}	if (codec->config.turbo) 		frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 | 						XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE | 						XVID_ME_BFRAME_EARLYSTOP;	frame.motion |= pmvfast_presets[codec->config.motion_search];	switch (codec->config.vhq_mode)	{	case VHQ_MODE_DECISION :		frame.vop_flags |= XVID_VOP_MODEDECISION_RD;		break;	case VHQ_LIMITED_SEARCH :		frame.vop_flags |= XVID_VOP_MODEDECISION_RD;		frame.motion |= XVID_ME_HALFPELREFINE16_RD;		frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;		break;	case VHQ_MEDIUM_SEARCH :		frame.vop_flags |= XVID_VOP_MODEDECISION_RD;		frame.motion |= XVID_ME_HALFPELREFINE16_RD;		frame.motion |= XVID_ME_HALFPELREFINE8_RD;		frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;		frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;		frame.motion |= XVID_ME_CHECKPREDICTION_RD;		break;	case VHQ_WIDE_SEARCH :		frame.vop_flags |= XVID_VOP_MODEDECISION_RD;		frame.motion |= XVID_ME_HALFPELREFINE16_RD;		frame.motion |= XVID_ME_HALFPELREFINE8_RD;		frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;		frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;		frame.motion |= XVID_ME_CHECKPREDICTION_RD;		frame.motion |= XVID_ME_EXTSEARCH_RD;		break;	default :		break;	}	frame.input.plane[0] = icc->lpInput;	frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3;	if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL)		return ICERR_BADFORMAT;	if (frame.input.csp == XVID_CSP_I420 || frame.input.csp == XVID_CSP_YV12) {		frame.input.stride[0] = (4 * icc->lpbiInput->biWidth + 3) / 4;		frame.input.stride[1] = frame.input.stride[2] = frame.input.stride[0] / 2 ;	}	frame.bitstream = icc->lpOutput;	frame.length = icc->lpbiOutput->biSizeImage;	frame.quant = 0;	if (codec->config.mode == RC_MODE_NULL) {		outhdr->biSizeImage = 0;		*icc->lpdwFlags = AVIIF_KEYFRAME;		return ICERR_OK;	}	// force keyframe spacing in 2-pass 1st pass	if (codec->config.motion_search == 0)		frame.type = XVID_TYPE_IVOP;	/* frame-based stuff */	apply_zone_modifiers(&frame, &codec->config, codec->framenum);	/* call encore */	memset(&stats, 0, sizeof(stats));	stats.version = XVID_VERSION;	length = codec->xvid_encore_func(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats);	switch (length) 	{	case XVID_ERR_FAIL :			return ICERR_ERROR;	case XVID_ERR_MEMORY :		return ICERR_MEMORY;	case XVID_ERR_FORMAT :		return ICERR_BADFORMAT;		case XVID_ERR_VERSION :		return ICERR_UNSUPPORTED;	}	if (codec->config.display_status && stats.type>0) {		status_update(&codec->status, stats.type, stats.length, stats.quant);	}	DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length);	if (length == 0)	/* no encoder output */	{		*icc->lpdwFlags = 0;		((char*)icc->lpOutput)[0] = 0x7f;	/* virtual dub skip frame */		outhdr->biSizeImage = 1;			}else{		if (frame.out_flags & XVID_KEYFRAME)		{			codec->keyspacing = 0;			*icc->lpdwFlags = AVIIF_KEYFRAME;		}		else		{			 *icc->lpdwFlags = 0;		}		outhdr->biSizeImage = length;		if (codec->config.mode == RC_MODE_2PASS1 && codec->config.discard1pass)		{			outhdr->biSizeImage = 0;		}	}	codec->framenum++;	codec->keyspacing++;	return ICERR_OK;}/* decompressor */LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput){	BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;	BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;	if (lpbiInput == NULL) 	{		return ICERR_ERROR;	}	if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX && inhdr->biCompression != FOURCC_DX50 && get_colorspace(inhdr) == XVID_CSP_NULL)	{		return ICERR_BADFORMAT;	}	if (lpbiOutput == NULL) 	{		return ICERR_OK;	}	if (inhdr->biWidth != outhdr->biWidth ||		inhdr->biHeight != outhdr->biHeight ||		get_colorspace(outhdr) == XVID_CSP_NULL) 	{		return ICERR_BADFORMAT;	}	return ICERR_OK;}LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput){	BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;	BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;	LRESULT result;	if (lpbiOutput == NULL) 	{		return sizeof(BITMAPINFOHEADER);	}	/* --- yv12 --- */	if (get_colorspace(inhdr) != XVID_CSP_NULL) {		memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));		/* XXX: should we set outhdr->biSize ?? */		return ICERR_OK;	}	/* --- yv12 --- */	result = decompress_query(codec, lpbiInput, lpbiOutput);	if (result != ICERR_OK) 	{		return result;	}	outhdr->biSize = sizeof(BITMAPINFOHEADER);	outhdr->biWidth = inhdr->biWidth;	outhdr->biHeight = inhdr->biHeight;	outhdr->biPlanes = 1;	outhdr->biBitCount = 24;	outhdr->biCompression = BI_RGB;	/* sonic foundry vegas video v3 only supports BI_RGB */	outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount / 8;	outhdr->biXPelsPerMeter = 0;	outhdr->biYPelsPerMeter = 0;	outhdr->biClrUsed = 0;	outhdr->biClrImportant = 0;	return ICERR_OK;}#define REG_GET_N(X, Y, Z) \{ \	DWORD size = sizeof(int); \	if (RegQueryValueEx(hKey, X, 0, 0, (LPBYTE)&Y, &size) != ERROR_SUCCESS) { \		Y=Z; \	} \}while(0)LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput){	xvid_gbl_init_t init;	xvid_dec_create_t create;	HKEY hKey;	if (init_dll(codec) != 0) return ICERR_ERROR;	memset(&init, 0, sizeof(init));	init.version = XVID_VERSION;	init.cpu_flags = codec->config.cpu;	codec->xvid_global_func(0, XVID_GBL_INIT, &init, NULL);	memset(&create, 0, sizeof(create));	create.version = XVID_VERSION;	create.width = lpbiInput->bmiHeader.biWidth;	create.height = lpbiInput->bmiHeader.biHeight;	switch(codec->xvid_decore_func(0, XVID_DEC_CREATE, &create, NULL)) 	{	case XVID_ERR_FAIL :		return ICERR_ERROR;	case XVID_ERR_MEMORY :		return ICERR_MEMORY;	case XVID_ERR_FORMAT :		return ICERR_BADFORMAT;	case XVID_ERR_VERSION :		return ICERR_UNSUPPORTED;	}	codec->dhandle = create.handle;	RegOpenKeyEx(XVID_REG_KEY, XVID_REG_PARENT "\\" XVID_REG_CHILD, 0, KEY_READ, &hKey);	REG_GET_N("Deblock_Y",  pp_dy, 0)	REG_GET_N("Deblock_UV", pp_duv, 0)	REG_GET_N("Dering",  pp_dr, 0)	REG_GET_N("FilmEffect", pp_fe, 0)	RegCloseKey(hKey);	return ICERR_OK;}LRESULT decompress_end(CODEC * codec){	if (codec->m_hdll != NULL) {		if (codec->dhandle != NULL) {			codec->xvid_decore_func(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL);			codec->dhandle = NULL;		}	}	return ICERR_OK;}LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd){	xvid_dec_frame_t frame;		/* --- yv12 --- */		if (icd->lpbiInput->biCompression != FOURCC_XVID &&		 icd->lpbiInput->biCompression != FOURCC_DIVX &&		 icd->lpbiInput->biCompression != FOURCC_DX50)	{		xvid_gbl_convert_t convert;		DPRINTF("input=%c%c%c%c output=%c%c%c%c", 			icd->lpbiInput->biCompression&0xff,			(icd->lpbiInput->biCompression>>8)&0xff,			(icd->lpbiInput->biCompression>>16)&0xff,			(icd->lpbiInput->biCompression>>24)&0xff,			icd->lpbiOutput->biCompression&0xff,			(icd->lpbiOutput->biCompression>>8)&0xff,			(icd->lpbiOutput->biCompression>>16)&0xff,			(icd->lpbiOutput->biCompression>>24)&0xff);		memset(&convert, 0, sizeof(convert));		convert.version = XVID_VERSION;		convert.input.csp = get_colorspace(icd->lpbiInput);		convert.input.plane[0] = icd->lpInput;		convert.input.stride[0] = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3;  		if (convert.input.csp == XVID_CSP_I420 || convert.input.csp == XVID_CSP_YV12)			convert.input.stride[0] = (convert.input.stride[0]*2)/3;		convert.output.csp = get_colorspace(icd->lpbiOutput);		convert.output.plane[0] = icd->lpOutput;		convert.output.stride[0] = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;		if (convert.output.csp == XVID_CSP_I420 || convert.output.csp == XVID_CSP_YV12)			convert.output.stride[0] = (convert.output.stride[0]*2)/3;		convert.width = icd->lpbiInput->biWidth;		convert.height = icd->lpbiInput->biHeight;		convert.interlacing = 0;		if (convert.input.csp == XVID_CSP_NULL ||			convert.output.csp == XVID_CSP_NULL ||			codec->xvid_global_func(0, XVID_GBL_CONVERT, &convert, NULL) < 0)		{			 return ICERR_BADFORMAT;		}		return ICERR_OK;	}	/* --- yv12 --- */		memset(&frame, 0, sizeof(frame));	frame.version = XVID_VERSION;	frame.general = XVID_LOWDELAY;	/* force low_delay_default mode */	frame.bitstream = icd->lpInput;	frame.length = icd->lpbiInput->biSizeImage;		if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL)))	{		if ((frame.output.csp = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL) 		{			return ICERR_BADFORMAT;		}		frame.output.plane[0] = icd->lpOutput;		frame.output.stride[0] = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;		if (frame.output.csp == XVID_CSP_I420 || frame.output.csp == XVID_CSP_YV12)			frame.output.stride[0] = (frame.output.stride[0]*2)/3;	}	else	{		frame.output.csp = XVID_CSP_NULL;	}	if (pp_dy)frame.general |= XVID_DEBLOCKY;	if (pp_duv) frame.general |= XVID_DEBLOCKUV;/*	if (pp_dr) frame.general |= XVID_DERING; */	if (pp_fe) frame.general |= XVID_FILMEFFECT;	switch (codec->xvid_decore_func(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) 	{	case XVID_ERR_FAIL :			return ICERR_ERROR;	case XVID_ERR_MEMORY :		return ICERR_MEMORY;	case XVID_ERR_FORMAT :		return ICERR_BADFORMAT;		case XVID_ERR_VERSION :		return ICERR_UNSUPPORTED;	}	return ICERR_OK;}

⌨️ 快捷键说明

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