int main()
{
	unsigned char *pImages;
	clock_t t_sta,t_end;
	pImages = (unsigned char *)malloc(sizeof(unsigned char)*WIDTH*HEIGHT*FRAMENUM);
	if (pImages == NULL)
	{
		printf("memory alloc error\n");
		return 0;
	}

	int *nMV;
	nMV = (int*)malloc(sizeof(int) * (2 * FRAMENUM - 2));
	if (nMV == NULL)
	{
		free(pImages);
		printf("memory alloc error\n");
		return 0;
	}

	t_sta = clock();
	FILE *fp;
	if ((fp = fopen("../video_6400_3600.yuv", "rb")) == NULL)
	{
		printf("file open error\n");
		return 0;
	}

	int i;
	for (i = 0; i < FRAMENUM; i++)
	{
		fseek(fp, (long int)i*WIDTH*HEIGHT * 3 / 2, SEEK_SET);
		fread(pImages + (long int)WIDTH*HEIGHT * i, 1, WIDTH*HEIGHT, fp);
	}
	fclose(fp);

	t_end = clock();

	printf("Read   time = %f msec\n", (double)(t_end - t_sta)/1000.0);

	t_sta = clock();
	MotionEstimation(pImages, WIDTH, HEIGHT,SCAN, nMV, FRAMENUM);
	t_end = clock();
	printf("Motion time = %f msec\n", (double)(t_end - t_sta)/1000.0);

	if ((fp = fopen("./result.txt", "w")) != NULL)
	{
		for (i = 0; i < FRAMENUM - 1; i++)
		{
			fprintf(fp, "%5d %5d %5d\n",i,nMV[2 * i + 0], nMV[2 * i + 1]);
		}
		fclose(fp);
	}
//	if ((fp = fopen("./result.txt", "rb")) != NULL)
//	{
//		int nErrorCount = 0;
//		int nMVX, nMVY;
//		for (i = 0; i < FRAMENUM - 1; i++)
//		{
//			fscanf(fp, "%d %d\n", &nMVX, &nMVY);
//			if (nMV[2 * i + 0] != nMVX || nMV[2 * i + 1] != nMVY)
//				nErrorCount++;
//		}
//		printf("num of error: %d\n", nErrorCount);
//		fclose(fp);
//	}
	
	free(pImages);
	free(nMV);

    return 0;
}
/* ======================================================================== */
PV_STATUS EncodeVop(VideoEncData *video)
{

    PV_STATUS status;
    Int currLayer = video->currLayer;
    Vol *currVol = video->vol[currLayer];
    Vop *currVop = video->currVop;
//	BitstreamEncVideo *stream=video->bitstream1;
    UChar *Mode = video->headerInfo.Mode;
    rateControl **rc = video->rc;
//	UInt time=0;

    /*******************/
    /* Initialize mode */
    /*******************/

    switch (currVop->predictionType)
    {
        case I_VOP:
            M4VENC_MEMSET(Mode, MODE_INTRA, sizeof(UChar)*currVol->nTotalMB);
            break;
        case P_VOP:
            M4VENC_MEMSET(Mode, MODE_INTER, sizeof(UChar)*currVol->nTotalMB);
            break;
        case B_VOP:
            /*M4VENC_MEMSET(Mode, MODE_INTER_B,sizeof(UChar)*nTotalMB);*/
            return PV_FAIL;
        default:
            return PV_FAIL;
    }

    /*********************/
    /* Motion Estimation */
    /* compute MVs, scene change detection, edge padding, */
    /* intra refresh, compute block activity */
    /*********************/
    MotionEstimation(video);	/* do ME for the whole frame */

    /***************************/
    /* rate Control (assign QP) */
    /* 4/11/01, clean-up, and put into a separate function */
    /***************************/
    status = RC_VopQPSetting(video, rc);
    if (status == PV_FAIL)
        return PV_FAIL;

    /**********************/
    /*     Encode VOP     */
    /**********************/
    if (video->slice_coding) /* end here */
    {
        /* initialize state variable for slice-based APIs */
        video->totalSAD = 0;
        video->mbnum = 0;
        video->sliceNo[0] = 0;
        video->numIntra = 0;
        video->offset = 0;
        video->end_of_buf = 0;
        video->hp_guess = -1;
        return status;
    }

    status = EncodeVop_NoME(video);

    /******************************/
    /* rate control (update stat) */
    /* 6/2/01 separate function */
    /******************************/

    RC_VopUpdateStat(video, rc[currLayer]);

    return status;
}