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

📄 encoder.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 2 页
字号:
	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->mbParam.fixed_code = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);

	length	= pEnc->mbParam.fixed_code + 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->pMBs[x + y * pEnc->mbParam.mb_width];
			MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
			VECTOR pred[4];
			VECTOR tmp;
			int dummy[4];
			int vec;

			pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);

			if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)
			{
				tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);
				tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);
				tmp.x -= (tmp.x >= high) ? high*2 : 0;
				tmp.y -= (tmp.y >= high) ? high*2 : 0;

				get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);

				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[0].x;
					pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;
				}
			}
			else if (pMB->mode == MODE_INTER4V)
			{
				for (vec=0 ; vec<4 ; ++vec)
				{
					tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);
					tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);
					tmp.x -= (tmp.x >= high) ? high*2 : 0;
					tmp.y -= (tmp.y >= high) ? high*2 : 0;

					get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);

					pMB->mvs[vec].x  = tmp.x;
					pMB->mvs[vec].y  = tmp.y;
					pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;
					pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;
				}
			}
			else	// intra / intra_q / stuffing / not_coded
			{
				for (vec=0 ; vec<4 ; ++vec)
				{
					pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
				}
			}
		}
	}
}

static
void HintedMEGet(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->mbParam.fixed_code + 5;
	high	= 1 << (length - 1);

	if (hint->rawhints)
	{
		hint->mvhint.fcode = pEnc->mbParam.fixed_code;
	}
	else
	{
		BitstreamPutBits(&bs, pEnc->mbParam.fixed_code, FCODEBITS);
	}

	for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)
	{
		for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)
		{
			MACROBLOCK * pMB = &pEnc->pMBs[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 int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits
#ifdef MPEG4IP
	, bool vol_header
#endif
	)
{

	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.rounding_type = 1;
	pEnc->mbParam.coding_type = I_VOP;

#ifdef MPEG4IP
	if (vol_header) {
		BitstreamWriteVolHeader(bs, &pEnc->mbParam);
	}
#else
	BitstreamWriteVolHeader(bs, &pEnc->mbParam);
#endif
	BitstreamWriteVopHeader(bs, &pEnc->mbParam);

	*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->pMBs[x + y * pEnc->mbParam.mb_width];

			CodeIntraMB(pEnc, pMB);

			MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent);

			start_timer();
			MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);
			stop_prediction_timer();

			start_timer();
			MBCoding(&pEnc->mbParam, 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.fixed_code = 2;

	if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET)
	{
		HintedMEGet(pEnc, 1);
	}

	return 1;					 // intra
}


#define INTRA_THRESHOLD 0.5

static int FrameCodeP(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;
	uint32_t x, y;
	int iSearchRange;
	bool bIntra;

	IMAGE *pCurrent = &pEnc->sCurrent;
	IMAGE *pRef = &pEnc->sReference;

	start_timer();
	image_setedges(pRef,
		       pEnc->mbParam.edged_width,
		       pEnc->mbParam.edged_height,
		       pEnc->mbParam.width,
		       pEnc->mbParam.height,
		       pEnc->mbParam.global_flags & XVID_INTERLACING);
	stop_edges_timer();

	pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;

	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->mbParam.global_flags & XVID_HALFPEL) > 0) {
		start_timer();
		image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
				  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
				  pEnc->mbParam.rounding_type);
		stop_inter_timer();
	}

	start_timer();
	if (pEnc->mbParam.global_flags & XVID_HINTEDME_SET)
	{
		HintedMESet(pEnc, &bIntra);
	}
	else
	{
		bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,
					  &pEnc->vInterH, &pEnc->vInterV,
					  &pEnc->vInterHV, &pEnc->sCurrent, iLimit);
	}
	stop_motion_timer();

	if (bIntra == 1)
	{
#ifdef MPEG4IP
		return FrameCodeI(pEnc, bs, pBits, vol_header);
#else
		return FrameCodeI(pEnc, bs, pBits);
#endif
	}

	pEnc->mbParam.coding_type = P_VOP;

	if(vol_header)
		BitstreamWriteVolHeader(bs, &pEnc->mbParam);

	BitstreamWriteVopHeader(bs, &pEnc->mbParam);

	*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->pMBs[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->sReference,
						     &pEnc->vInterH,
						     &pEnc->vInterV,
						     &pEnc->vInterHV,
						     &pEnc->sCurrent,
						     dct_codes,
						     pEnc->mbParam.width,
						     pEnc->mbParam.height,
						     pEnc->mbParam.edged_width,
						     pEnc->mbParam.rounding_type);
				stop_comp_timer();

				if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {
					if(pMB->dquant != NO_CHANGE) {
						pMB->mode = MODE_INTER_Q;
						pEnc->mbParam.quant += DQtab[pMB->dquant];
						if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;
						else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;
					}
				}
				pMB->quant = pEnc->mbParam.quant;

				pMB->field_pred = 0;

				pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);
			}
			else 
			{
				CodeIntraMB(pEnc, pMB);
				MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);
			}

			start_timer();
			MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);
			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();
			MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);
			stop_coding_timer();
		}
	}

	emms();

	if (pEnc->mbParam.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.fixed_code);

	if ((fSigma > iSearchRange / 3) 
	    && (pEnc->mbParam.fixed_code <= 3))	// maximum search range 128
	{
		pEnc->mbParam.fixed_code++;
		iSearchRange *= 2;
	}
	else if ((fSigma < iSearchRange / 6)
		 && (pEnc->sStat.fMvPrevSigma >= 0)
		 && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
		 && (pEnc->mbParam.fixed_code >= 2))	// minimum search range 16
	{
		pEnc->mbParam.fixed_code--;
		iSearchRange /= 2;
	}

	pEnc->sStat.fMvPrevSigma = fSigma;
    
	*pBits = BitstreamPos(bs) - *pBits;

	return 0;					 // inter
}

⌨️ 快捷键说明

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