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

📄 encoder.c

📁 mpeg4代码,比较具体
💻 C
📖 第 1 页 / 共 2 页
字号:
		} else {			pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);		}	} else {		if (pFrame->intra == 1) {			pFrame->intra = FrameCodeI(pEnc, &bs, &bits);		} else {			pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);		}	}	/* Fix from CVS_HEAD (2003-03-17)	 *	 * Relic from OpenDivX - now disabled	 *	 * BitstreamPutBits(&bs, 0xFFFF, 16);	 * BitstreamPutBits(&bs, 0xFFFF, 16);	 */	BitstreamPadAlways(&bs); /* next_start_code() at the end of Video Object Plane */	pFrame->length = BitstreamLength(&bs);	if (pResult) {		pResult->quant = pEnc->current->quant;		pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);		pResult->kblks = pEnc->sStat.kblks;		pResult->mblks = pEnc->sStat.mblks;		pResult->ublks = pEnc->sStat.ublks;	}	emms();	if (pFrame->quant == 0) {		RateControlUpdate(&pEnc->rate_control, (int16_t)pEnc->current->quant,						  pFrame->length, pFrame->intra);	}#ifdef _DEBUG_PSNR	psnr =		image_psnr(&pEnc->sOriginal, &pEnc->current->image,				   pEnc->mbParam.edged_width, pEnc->mbParam.width,				   pEnc->mbParam.height);	snprintf(temp, 127, "PSNR: %f\n", psnr);	DEBUG(temp);#endif	inc_frame_num(pEnc);	pEnc->iFrameNum++;	stop_global_timer();	write_timer();	return XVID_ERR_OK;}static __inline voidCodeIntraMB(Encoder * pEnc,			MACROBLOCK * pMB){	pMB->mode = MODE_INTRA;	/* zero mv statistics */	pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;	pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;	pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;	pMB->sad16 = 0;	if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {		if (pMB->dquant != NO_CHANGE) {			pMB->mode = MODE_INTRA_Q;			pEnc->current->quant += DQtab[pMB->dquant];			if (pEnc->current->quant > 31)				pEnc->current->quant = 31;			if (pEnc->current->quant < 1)				pEnc->current->quant = 1;		}	}	pMB->quant = pEnc->current->quant;}#define FCODEBITS	3#define MODEBITS	5voidHintedMESet(Encoder * pEnc,			int *intra){	HINTINFO *hint;	Bitstream bs;	int length, high;	uint32_t x, y;	hint = pEnc->mbParam.hint;	if (hint->rawhints) {		*intra = hint->mvhint.intra;	} else {		BitstreamInit(&bs, hint->hintstream, hint->hintlength);		*intra = BitstreamGetBit(&bs);	}	if (*intra) {		return;	}	pEnc->current->fcode = (hint->rawhints != 0) ?		(uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);	length = pEnc->current->fcode + 5;	high = 1 << (length - 1);	for (y = 0; y < pEnc->mbParam.mb_height; ++y) {		for (x = 0; x < pEnc->mbParam.mb_width; ++x) {			MACROBLOCK *pMB =				&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];			MVBLOCKHINT *bhint =				&hint->mvhint.block[x + y * pEnc->mbParam.mb_width];			VECTOR pred;			VECTOR tmp;			int vec;			pMB->mode = (hint->rawhints != 0) ?				(uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);			pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;			pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;			if (pMB->mode == MODE_INTER) {				tmp.x = (hint->rawhints) ?					bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);				tmp.y =	(hint->rawhints) ?					bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);				tmp.x -= (tmp.x >= high) ? high * 2 : 0;				tmp.y -= (tmp.y >= high) ? high * 2 : 0;				pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);				for (vec = 0; vec < 4; ++vec) {					pMB->mvs[vec].x = tmp.x;					pMB->mvs[vec].y = tmp.y;					pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;					pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;				}			} else if (pMB->mode == MODE_INTER4V) {				for (vec = 0; vec < 4; ++vec) {					tmp.x = (hint->rawhints) ?						bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);					tmp.y = (hint->rawhints) ?						bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);					tmp.x -= (tmp.x >= high) ? high * 2 : 0;					tmp.y -= (tmp.y >= high) ? high * 2 : 0;					pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);					pMB->mvs[vec].x = tmp.x;					pMB->mvs[vec].y = tmp.y;					pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;					pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;				}			} else				/* intra / stuffing / not_coded */			{				for (vec = 0; vec < 4; ++vec) {					pMB->mvs[vec].x = pMB->mvs[vec].y = 0;				}			}			if (pMB->mode == MODE_INTER4V &&				(pEnc->current->global_flags & XVID_LUMIMASKING)				&& pMB->dquant != NO_CHANGE) {				pMB->mode = MODE_INTRA;				for (vec = 0; vec < 4; ++vec) {					pMB->mvs[vec].x = pMB->mvs[vec].y = 0;				}			}		}	}}voidHintedMEGet(Encoder * pEnc,			int intra){	HINTINFO *hint;	Bitstream bs;	uint32_t x, y;	int length, high;	hint = pEnc->mbParam.hint;	if (hint->rawhints) {		hint->mvhint.intra = intra;	} else {		BitstreamInit(&bs, hint->hintstream, 0);		BitstreamPutBit(&bs, intra);	}	if (intra) {		if (!hint->rawhints) {			BitstreamPad(&bs);			hint->hintlength = BitstreamLength(&bs);		}		return;	}	length = pEnc->current->fcode + 5;	high = 1 << (length - 1);	if (hint->rawhints) {		hint->mvhint.fcode = pEnc->current->fcode;	} else {		BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);	}	for (y = 0; y < pEnc->mbParam.mb_height; ++y) {		for (x = 0; x < pEnc->mbParam.mb_width; ++x) {			MACROBLOCK *pMB =				&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];			MVBLOCKHINT *bhint =				&hint->mvhint.block[x + y * pEnc->mbParam.mb_width];			VECTOR tmp;			if (hint->rawhints) {				bhint->mode = pMB->mode;			} else {				BitstreamPutBits(&bs, pMB->mode, MODEBITS);			}			if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {				tmp.x = pMB->mvs[0].x;				tmp.y = pMB->mvs[0].y;				tmp.x += (tmp.x < 0) ? high * 2 : 0;				tmp.y += (tmp.y < 0) ? high * 2 : 0;				if (hint->rawhints) {					bhint->mvs[0].x = tmp.x;					bhint->mvs[0].y = tmp.y;				} else {					BitstreamPutBits(&bs, tmp.x, length);					BitstreamPutBits(&bs, tmp.y, length);				}			} else if (pMB->mode == MODE_INTER4V) {				int vec;				for (vec = 0; vec < 4; ++vec) {					tmp.x = pMB->mvs[vec].x;					tmp.y = pMB->mvs[vec].y;					tmp.x += (tmp.x < 0) ? high * 2 : 0;					tmp.y += (tmp.y < 0) ? high * 2 : 0;					if (hint->rawhints) {						bhint->mvs[vec].x = tmp.x;						bhint->mvs[vec].y = tmp.y;					} else {						BitstreamPutBits(&bs, tmp.x, length);						BitstreamPutBits(&bs, tmp.y, length);					}				}			}		}	}	if (!hint->rawhints) {		BitstreamPad(&bs);		hint->hintlength = BitstreamLength(&bs);	}}static intFrameCodeI(Encoder * pEnc,		   Bitstream * bs,		   uint32_t * pBits){	DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);	DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);	uint16_t x, y;	pEnc->iFrameNum = 0;	pEnc->mbParam.m_rounding_type = 1;	pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;	pEnc->current->coding_type = I_VOP;	BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);	BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);	*pBits = BitstreamPos(bs);	pEnc->sStat.iTextBits = 0;	pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;	pEnc->sStat.mblks = pEnc->sStat.ublks = 0;	for (y = 0; y < pEnc->mbParam.mb_height; y++)		for (x = 0; x < pEnc->mbParam.mb_width; x++) {			MACROBLOCK *pMB =				&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];			CodeIntraMB(pEnc, pMB);			MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,							  dct_codes, qcoeff);			start_timer();			MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);			stop_prediction_timer();			start_timer();			if (pEnc->current->global_flags & XVID_GREYSCALE)			{	pMB->cbp &= 0x3C;		/* keep only bits 5-2 */				qcoeff[4*64+0]=0;		/* zero, because for INTRA MBs DC value is saved */				qcoeff[5*64+0]=0;			}			MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);			stop_coding_timer();		}	emms();	*pBits = BitstreamPos(bs) - *pBits;	pEnc->sStat.fMvPrevSigma = -1;	pEnc->sStat.iMvSum = 0;	pEnc->sStat.iMvCount = 0;	pEnc->mbParam.m_fcode = 2;	if (pEnc->current->global_flags & XVID_HINTEDME_GET) {		HintedMEGet(pEnc, 1);	}	return 1;					/* intra */}#define INTRA_THRESHOLD 0.5static intFrameCodeP(Encoder * pEnc,		   Bitstream * bs,		   uint32_t * pBits,		   bool force_inter,		   bool vol_header){	float fSigma;	DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);	DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);	int iLimit;	unsigned int x, y;	int iSearchRange;	int bIntra;		/* IMAGE *pCurrent = &pEnc->current->image; */	IMAGE *pRef = &pEnc->reference->image;	start_timer();	image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,				   pEnc->mbParam.width, pEnc->mbParam.height);	stop_edges_timer();	pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;	pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;	pEnc->current->fcode = pEnc->mbParam.m_fcode;	if (!force_inter)		iLimit =			(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *				   INTRA_THRESHOLD);	else		iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;	if ((pEnc->current->global_flags & XVID_HALFPEL)) {		start_timer();		image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,						  &pEnc->vInterHV, pEnc->mbParam.edged_width,						  pEnc->mbParam.edged_height,						  pEnc->current->rounding_type);		stop_inter_timer();	}	start_timer();	if (pEnc->current->global_flags & XVID_HINTEDME_SET) {		HintedMESet(pEnc, &bIntra);	} else {		bIntra =			MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                         &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                         iLimit);				}	stop_motion_timer();	if (bIntra == 1) {		return FrameCodeI(pEnc, bs, pBits);	}	pEnc->current->coding_type = P_VOP;	if (vol_header)		BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);	BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);	*pBits = BitstreamPos(bs);	pEnc->sStat.iTextBits = 0;	pEnc->sStat.iMvSum = 0;	pEnc->sStat.iMvCount = 0;	pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;	for (y = 0; y < pEnc->mbParam.mb_height; y++) {		for (x = 0; x < pEnc->mbParam.mb_width; x++) {			MACROBLOCK *pMB =				&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];			bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);			if (!bIntra) {				start_timer();				MBMotionCompensation(pMB, x, y, &pEnc->reference->image,									 &pEnc->vInterH, &pEnc->vInterV,									 &pEnc->vInterHV, &pEnc->current->image,									 dct_codes, pEnc->mbParam.width,									 pEnc->mbParam.height,									 pEnc->mbParam.edged_width,									 pEnc->current->rounding_type);				stop_comp_timer();				if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {					if (pMB->dquant != NO_CHANGE) {						pMB->mode = MODE_INTER_Q;						pEnc->current->quant += DQtab[pMB->dquant];						if (pEnc->current->quant > 31)							pEnc->current->quant = 31;						else if (pEnc->current->quant < 1)							pEnc->current->quant = 1;					}				}				pMB->quant = pEnc->current->quant;				pMB->field_pred = 0;				pMB->cbp =					MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,									  dct_codes, qcoeff);			} else {				CodeIntraMB(pEnc, pMB);				MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,								  dct_codes, qcoeff);				start_timer();				MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);				stop_prediction_timer();			}			if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {				pEnc->sStat.kblks++;			} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||					   pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||					   pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {				pEnc->sStat.mblks++;			}  else {				pEnc->sStat.ublks++;			} 			start_timer();			/* Finished processing the MB, now check if to CODE or SKIP */			if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&				pMB->mvs[0].y == 0) {					MBSkip(bs);	/* without B-frames, no precautions are needed */			}			else {				if (pEnc->current->global_flags & XVID_GREYSCALE) {					pMB->cbp &= 0x3C;		/* keep only bits 5-2 */					qcoeff[4*64+0]=0;		/* zero, because DC for INTRA MBs DC value is saved */					qcoeff[5*64+0]=0;				}				MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);			}			stop_coding_timer();		}	}	emms();	if (pEnc->current->global_flags & XVID_HINTEDME_GET) {		HintedMEGet(pEnc, 0);	}	if (pEnc->sStat.iMvCount == 0)		pEnc->sStat.iMvCount = 1;	fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);	iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);	if ((fSigma > iSearchRange / 3)		&& (pEnc->mbParam.m_fcode <= 3))	/* maximum search range 128 */	{		pEnc->mbParam.m_fcode++;		iSearchRange *= 2;	} else if ((fSigma < iSearchRange / 6)			   && (pEnc->sStat.fMvPrevSigma >= 0)			   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)			   && (pEnc->mbParam.m_fcode >= 2))	/* minimum search range 16 */	{		pEnc->mbParam.m_fcode--;		iSearchRange /= 2;	}	pEnc->sStat.fMvPrevSigma = fSigma;	*pBits = BitstreamPos(bs) - *pBits;	return 0;					/* inter */}

⌨️ 快捷键说明

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