omxVCM4P2_MotionEstimationMB.c revision 0c1bc742181ded4930842b46e9507372f0b1b963
10c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
20c1bc742181ded4930842b46e9507372f0b1b963James Dong *
30c1bc742181ded4930842b46e9507372f0b1b963James Dong * File Name:  omxVCM4P2_MotionEstimationMB.c
40c1bc742181ded4930842b46e9507372f0b1b963James Dong * OpenMAX DL: v1.0.2
50c1bc742181ded4930842b46e9507372f0b1b963James Dong * Revision:   9641
60c1bc742181ded4930842b46e9507372f0b1b963James Dong * Date:       Thursday, February 7, 2008
70c1bc742181ded4930842b46e9507372f0b1b963James Dong *
80c1bc742181ded4930842b46e9507372f0b1b963James Dong * (c) Copyright 2007-2008 ARM Limited. All Rights Reserved.
90c1bc742181ded4930842b46e9507372f0b1b963James Dong *
100c1bc742181ded4930842b46e9507372f0b1b963James Dong *
110c1bc742181ded4930842b46e9507372f0b1b963James Dong *
120c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
130c1bc742181ded4930842b46e9507372f0b1b963James Dong * Contains module for motion search 16x16 macroblock
140c1bc742181ded4930842b46e9507372f0b1b963James Dong *
150c1bc742181ded4930842b46e9507372f0b1b963James Dong */
160c1bc742181ded4930842b46e9507372f0b1b963James Dong
170c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxtypes.h"
180c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armOMX.h"
190c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxVC.h"
200c1bc742181ded4930842b46e9507372f0b1b963James Dong
210c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVC.h"
220c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armCOMM.h"
230c1bc742181ded4930842b46e9507372f0b1b963James Dong
240c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
250c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function: armVCM4P2_BlockMatch_16x16
260c1bc742181ded4930842b46e9507372f0b1b963James Dong *
270c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
280c1bc742181ded4930842b46e9507372f0b1b963James Dong * 16x16 block match wrapper function, calls omxVCM4P2_BlockMatch_Integer_16x16.
290c1bc742181ded4930842b46e9507372f0b1b963James Dong * If half pel search is enabled it also calls omxVCM4P2_BlockMatch_Half_16x16
300c1bc742181ded4930842b46e9507372f0b1b963James Dong *
310c1bc742181ded4930842b46e9507372f0b1b963James Dong * Remarks:
320c1bc742181ded4930842b46e9507372f0b1b963James Dong *
330c1bc742181ded4930842b46e9507372f0b1b963James Dong * Parameters:
340c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	pSrcRefBuf	  pointer to the reference Y plane; points to the reference MB that
350c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    corresponds to the location of the current macroblock in the current
360c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    plane.
370c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	srcRefStep	  width of the reference plane
380c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	pRefRect	  pointer to the valid rectangular in reference plane. Relative to image origin.
390c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    It's not limited to the image boundary, but depended on the padding. For example,
400c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    if you pad 4 pixels outside the image border, then the value for left border
410c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    can be -4
420c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	pSrcCurrBuf	  pointer to the current macroblock extracted from original plane (linear array,
430c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    256 entries); must be aligned on an 16-byte boundary.
440c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in] pCurrPointPos position of the current macroblock in the current plane
450c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in] pSrcPreMV	  pointer to predicted motion vector; NULL indicates no predicted MV
460c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in] pSrcPreSAD	  pointer to SAD associated with the predicted MV (referenced by pSrcPreMV); may be set to NULL if unavailable.
470c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in] pMESpec		  vendor-specific motion estimation specification structure; must have been allocated
480c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    and then initialized using omxVCM4P2_MEInit prior to calling the block matching
490c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    function.
500c1bc742181ded4930842b46e9507372f0b1b963James Dong * [out] pDstMV	      pointer to estimated MV
510c1bc742181ded4930842b46e9507372f0b1b963James Dong * [out] pDstSAD	  pointer to minimum SAD
520c1bc742181ded4930842b46e9507372f0b1b963James Dong * *
530c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
540c1bc742181ded4930842b46e9507372f0b1b963James Dong * OMX_Sts_NoErr - no error
550c1bc742181ded4930842b46e9507372f0b1b963James Dong * OMX_Sts_BadArgErr - bad arguments
560c1bc742181ded4930842b46e9507372f0b1b963James Dong *
570c1bc742181ded4930842b46e9507372f0b1b963James Dong */
580c1bc742181ded4930842b46e9507372f0b1b963James Dongstatic OMXResult armVCM4P2_BlockMatch_16x16(
590c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMX_U8 *pSrcRefBuf,
600c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMX_INT srcRefStep,
610c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXRect *pRefRect,
620c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMX_U8 *pSrcCurrBuf,
630c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXVCM4P2Coordinate *pCurrPointPos,
640c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMXVCMotionVector *pSrcPreMV,
650c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_INT *pSrcPreSAD,
660c1bc742181ded4930842b46e9507372f0b1b963James Dong     void *pMESpec,
670c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMXVCMotionVector *pDstMV,
680c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_INT *pDstSAD
690c1bc742181ded4930842b46e9507372f0b1b963James Dong)
700c1bc742181ded4930842b46e9507372f0b1b963James Dong{
710c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCM4P2MEParams *pMEParams = (OMXVCM4P2MEParams *)pMESpec;
720c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT rndVal;
730c1bc742181ded4930842b46e9507372f0b1b963James Dong
740c1bc742181ded4930842b46e9507372f0b1b963James Dong    rndVal = pMEParams->rndVal;
750c1bc742181ded4930842b46e9507372f0b1b963James Dong
760c1bc742181ded4930842b46e9507372f0b1b963James Dong    omxVCM4P2_BlockMatch_Integer_16x16(
770c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcRefBuf,
780c1bc742181ded4930842b46e9507372f0b1b963James Dong        srcRefStep,
790c1bc742181ded4930842b46e9507372f0b1b963James Dong        pRefRect,
800c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcCurrBuf,
810c1bc742181ded4930842b46e9507372f0b1b963James Dong        pCurrPointPos,
820c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcPreMV,
830c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcPreSAD,
840c1bc742181ded4930842b46e9507372f0b1b963James Dong        pMEParams,
850c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstMV,
860c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstSAD);
870c1bc742181ded4930842b46e9507372f0b1b963James Dong
880c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pMEParams->halfPelSearchEnable)
890c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
900c1bc742181ded4930842b46e9507372f0b1b963James Dong        omxVCM4P2_BlockMatch_Half_16x16(
910c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSrcRefBuf,
920c1bc742181ded4930842b46e9507372f0b1b963James Dong            srcRefStep,
930c1bc742181ded4930842b46e9507372f0b1b963James Dong            pRefRect,
940c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSrcCurrBuf,
950c1bc742181ded4930842b46e9507372f0b1b963James Dong            pCurrPointPos,
960c1bc742181ded4930842b46e9507372f0b1b963James Dong            rndVal,
970c1bc742181ded4930842b46e9507372f0b1b963James Dong            pDstMV,
980c1bc742181ded4930842b46e9507372f0b1b963James Dong            pDstSAD);
990c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    return OMX_Sts_NoErr;
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function: armVCM4P2_BlockMatch_8x8
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong *
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong * 8x8 block match wrapper function, calls omxVCM4P2_BlockMatch_Integer_8x8.
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong * If half pel search is enabled it also calls omxVCM4P2_BlockMatch_Half_8x8
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong *
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong * Remarks:
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong *
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong * Parameters:
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	pSrcRefBuf	  pointer to the reference Y plane; points to the reference MB that
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    corresponds to the location of the current macroblock in the current
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    plane.
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	srcRefStep	  width of the reference plane
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	pRefRect	  pointer to the valid rectangular in reference plane. Relative to image origin.
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    It's not limited to the image boundary, but depended on the padding. For example,
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    if you pad 4 pixels outside the image border, then the value for left border
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    can be -4
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	pSrcCurrBuf	  pointer to the current macroblock extracted from original plane (linear array,
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    256 entries); must be aligned on an 16-byte boundary.
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in] pCurrPointPos position of the current macroblock in the current plane
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in] pSrcPreMV	  pointer to predicted motion vector; NULL indicates no predicted MV
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in] pSrcPreSAD	  pointer to SAD associated with the predicted MV (referenced by pSrcPreMV); may be set to NULL if unavailable.
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in] pMESpec		  vendor-specific motion estimation specification structure; must have been allocated
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    and then initialized using omxVCM4P2_MEInit prior to calling the block matching
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong *                    function.
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong * [out] pDstMV	      pointer to estimated MV
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong * [out] pDstSAD	  pointer to minimum SAD
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong * *
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong * OMX_Sts_NoErr - no error
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong * OMX_Sts_BadArgErr - bad arguments
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong *
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong */
1380c1bc742181ded4930842b46e9507372f0b1b963James Dongstatic OMXResult armVCM4P2_BlockMatch_8x8(
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMX_U8 *pSrcRefBuf,
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_INT srcRefStep,
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXRect *pRefRect,
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMX_U8 *pSrcCurrBuf,
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXVCM4P2Coordinate *pCurrPointPos,
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMXVCMotionVector *pSrcPreMV,
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_INT *pSrcPreSAD,
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong     void *pMESpec,
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMXVCMotionVector *pSrcDstMV,
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_INT *pDstSAD
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong)
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong{
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCM4P2MEParams *pMEParams = (OMXVCM4P2MEParams *)pMESpec;
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT rndVal;
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong    rndVal = pMEParams->rndVal;
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong    omxVCM4P2_BlockMatch_Integer_8x8(
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcRefBuf,
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong        srcRefStep,
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong        pRefRect,
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcCurrBuf,
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong        pCurrPointPos,
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcPreMV,
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcPreSAD,
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong        pMEParams,
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcDstMV,
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstSAD);
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pMEParams->halfPelSearchEnable)
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong        omxVCM4P2_BlockMatch_Half_8x8(
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSrcRefBuf,
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong            srcRefStep,
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong            pRefRect,
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSrcCurrBuf,
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong            pCurrPointPos,
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong            rndVal,
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSrcDstMV,
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong            pDstSAD);
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong    return OMX_Sts_NoErr;
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function:  omxVCM4P2_MotionEstimationMB   (6.2.4.3.1)
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong *
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong * Performs motion search for a 16x16 macroblock.  Selects best motion search
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong * strategy from among inter-1MV, inter-4MV, and intra modes.  Supports
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong * integer and half pixel resolution.
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong *
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong * Input Arguments:
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong *
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcCurrBuf - pointer to the top-left corner of the current MB in the
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong *            original picture plane; must be aligned on a 16-byte boundary.
1970c1bc742181ded4930842b46e9507372f0b1b963James Dong *            The function does not expect source data outside the region
1980c1bc742181ded4930842b46e9507372f0b1b963James Dong *            bounded by the MB to be available; for example it is not
1990c1bc742181ded4930842b46e9507372f0b1b963James Dong *            necessary for the caller to guarantee the availability of
2000c1bc742181ded4930842b46e9507372f0b1b963James Dong *            pSrcCurrBuf[-SrcCurrStep], i.e., the row of pixels above the MB
2010c1bc742181ded4930842b46e9507372f0b1b963James Dong *            to be processed.
2020c1bc742181ded4930842b46e9507372f0b1b963James Dong *   srcCurrStep - width of the original picture plane, in terms of full
2030c1bc742181ded4930842b46e9507372f0b1b963James Dong *            pixels; must be a multiple of 16.
2040c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcRefBuf - pointer to the reference Y plane; points to the reference
2050c1bc742181ded4930842b46e9507372f0b1b963James Dong *            plane location corresponding to the location of the current
2060c1bc742181ded4930842b46e9507372f0b1b963James Dong *            macroblock in the current plane; must be aligned on a 16-byte
2070c1bc742181ded4930842b46e9507372f0b1b963James Dong *            boundary.
2080c1bc742181ded4930842b46e9507372f0b1b963James Dong *   srcRefStep - width of the reference picture plane, in terms of full
2090c1bc742181ded4930842b46e9507372f0b1b963James Dong *            pixels; must be a multiple of 16.
2100c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pRefRect - reference plane valid region rectangle, specified relative to
2110c1bc742181ded4930842b46e9507372f0b1b963James Dong *            the image origin
2120c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pCurrPointPos - position of the current macroblock in the current plane
2130c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pMESpec - pointer to the vendor-specific motion estimation specification
2140c1bc742181ded4930842b46e9507372f0b1b963James Dong *            structure; must be allocated and then initialized using
2150c1bc742181ded4930842b46e9507372f0b1b963James Dong *            omxVCM4P2_MEInit prior to calling this function.
2160c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pMBInfo - array, of dimension four, containing pointers to information
2170c1bc742181ded4930842b46e9507372f0b1b963James Dong *            associated with four nearby MBs:
2180c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -   pMBInfo[0] - pointer to left MB information
2190c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -   pMBInfo[1] - pointer to top MB information
2200c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -   pMBInfo[2] - pointer to top-left MB information
2210c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -   pMBInfo[3] - pointer to top-right MB information
2220c1bc742181ded4930842b46e9507372f0b1b963James Dong *            Any pointer in the array may be set equal to NULL if the
2230c1bc742181ded4930842b46e9507372f0b1b963James Dong *            corresponding MB doesn't exist.  For each MB, the following structure
2240c1bc742181ded4930842b46e9507372f0b1b963James Dong *            members are used:
2250c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -   mbType - macroblock type, either OMX_VC_INTRA, OMX_VC_INTER, or
2260c1bc742181ded4930842b46e9507372f0b1b963James Dong *                OMX_VC_INTER4V
2270c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -   pMV0[2][2] - estimated motion vectors; represented
2280c1bc742181ded4930842b46e9507372f0b1b963James Dong *                in 1/2 pixel units
2290c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -   sliceID - number of the slice to which the MB belongs
2300c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcDstMBCurr - pointer to information structure for the current MB.
2310c1bc742181ded4930842b46e9507372f0b1b963James Dong *            The following entries should be set prior to calling the
2320c1bc742181ded4930842b46e9507372f0b1b963James Dong *            function: sliceID - the number of the slice the to which the
2330c1bc742181ded4930842b46e9507372f0b1b963James Dong *            current MB belongs.  The structure elements cbpy and cbpc are
2340c1bc742181ded4930842b46e9507372f0b1b963James Dong *            ignored.
2350c1bc742181ded4930842b46e9507372f0b1b963James Dong *
2360c1bc742181ded4930842b46e9507372f0b1b963James Dong * Output Arguments:
2370c1bc742181ded4930842b46e9507372f0b1b963James Dong *
2380c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcDstMBCurr - pointer to updated information structure for the current
2390c1bc742181ded4930842b46e9507372f0b1b963James Dong *            MB after MB-level motion estimation has been completed.  The
2400c1bc742181ded4930842b46e9507372f0b1b963James Dong *            following structure members are updated by the ME function:
2410c1bc742181ded4930842b46e9507372f0b1b963James Dong *              -  mbType - macroblock type: OMX_VC_INTRA, OMX_VC_INTER, or
2420c1bc742181ded4930842b46e9507372f0b1b963James Dong *                 OMX_VC_INTER4V.
2430c1bc742181ded4930842b46e9507372f0b1b963James Dong *              -  pMV0[2][2] - estimated motion vectors; represented in
2440c1bc742181ded4930842b46e9507372f0b1b963James Dong *                 terms of 1/2 pel units.
2450c1bc742181ded4930842b46e9507372f0b1b963James Dong *              -  pMVPred[2][2] - predicted motion vectors; represented
2460c1bc742181ded4930842b46e9507372f0b1b963James Dong *                 in terms of 1/2 pel units.
2470c1bc742181ded4930842b46e9507372f0b1b963James Dong *            The structure members cbpy and cbpc are not updated by the function.
2480c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pDstSAD - pointer to the minimum SAD for INTER1V, or sum of minimum SADs
2490c1bc742181ded4930842b46e9507372f0b1b963James Dong *            for INTER4V
2500c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pDstBlockSAD - pointer to an array of SAD values for each of the four
2510c1bc742181ded4930842b46e9507372f0b1b963James Dong *            8x8 luma blocks in the MB.  The block SADs are in scan order for
2520c1bc742181ded4930842b46e9507372f0b1b963James Dong *            each MB.
2530c1bc742181ded4930842b46e9507372f0b1b963James Dong *
2540c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
2550c1bc742181ded4930842b46e9507372f0b1b963James Dong *
2560c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_NoErr - no error
2570c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_BadArgErr - bad arguments.  Returned if one or more of the
2580c1bc742181ded4930842b46e9507372f0b1b963James Dong *              following conditions is true:
2590c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    at least one of the following pointers is NULL: pSrcCurrBuf,
2600c1bc742181ded4930842b46e9507372f0b1b963James Dong *              pSrcRefBuf, pRefRect, pCurrPointPos, pMBInter, pMBIntra,
2610c1bc742181ded4930842b46e9507372f0b1b963James Dong *              pSrcDstMBCurr, or pDstSAD.
2620c1bc742181ded4930842b46e9507372f0b1b963James Dong *
2630c1bc742181ded4930842b46e9507372f0b1b963James Dong */
2640c1bc742181ded4930842b46e9507372f0b1b963James Dong
2650c1bc742181ded4930842b46e9507372f0b1b963James DongOMXResult omxVCM4P2_MotionEstimationMB (
2660c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8 *pSrcCurrBuf,
2670c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 srcCurrStep,
2680c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8 *pSrcRefBuf,
2690c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 srcRefStep,
2700c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMXRect*pRefRect,
2710c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMXVCM4P2Coordinate *pCurrPointPos,
2720c1bc742181ded4930842b46e9507372f0b1b963James Dong    void *pMESpec,
2730c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMXVCM4P2MBInfoPtr *pMBInfo,
2740c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCM4P2MBInfo *pSrcDstMBCurr,
2750c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U16 *pDstSAD,
2760c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U16 *pDstBlockSAD
2770c1bc742181ded4930842b46e9507372f0b1b963James Dong)
2780c1bc742181ded4930842b46e9507372f0b1b963James Dong{
2790c1bc742181ded4930842b46e9507372f0b1b963James Dong
2800c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT intraSAD, average, count, index, x, y;
2810c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector dstMV16x16;
2820c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT           dstSAD16x16;
2830c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT           dstSAD8x8;
2840c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCM4P2MEParams  *pMEParams;
2850c1bc742181ded4930842b46e9507372f0b1b963James Dong	OMXVCM4P2Coordinate TempCurrPointPos;
2860c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCM4P2Coordinate *pTempCurrPointPos;
2870c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8 aTempSrcCurrBuf[271];
2880c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8 *pTempSrcCurrBuf;
2890c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8 *pDst;
2900c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8 aDst[71];
2910c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 dstStep = 8;
2920c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT predictType;
2930c1bc742181ded4930842b46e9507372f0b1b963James Dong	OMX_S32 Sad;
2940c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8 *pTempSrcRefBuf;
2950c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector* pSrcCandMV1[4];
2960c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector* pSrcCandMV2[4];
2970c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector* pSrcCandMV3[4];
2980c1bc742181ded4930842b46e9507372f0b1b963James Dong
2990c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Argument error checks */
3000c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(!armIs16ByteAligned(pSrcCurrBuf), OMX_Sts_BadArgErr);
3010c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(!armIs16ByteAligned(pSrcRefBuf), OMX_Sts_BadArgErr);
3020c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(((srcCurrStep % 16) || (srcRefStep % 16)), OMX_Sts_BadArgErr);
3030c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(pSrcCurrBuf == NULL, OMX_Sts_BadArgErr);
3040c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(pSrcRefBuf == NULL, OMX_Sts_BadArgErr);
3050c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pRefRect == NULL, OMX_Sts_BadArgErr);
3060c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pCurrPointPos == NULL, OMX_Sts_BadArgErr);
3070c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcDstMBCurr == NULL, OMX_Sts_BadArgErr);
3080c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pDstSAD == NULL, OMX_Sts_BadArgErr);
3090c1bc742181ded4930842b46e9507372f0b1b963James Dong
3100c1bc742181ded4930842b46e9507372f0b1b963James Dong
3110c1bc742181ded4930842b46e9507372f0b1b963James Dong    pTempCurrPointPos = &(TempCurrPointPos);
3120c1bc742181ded4930842b46e9507372f0b1b963James Dong    pTempSrcCurrBuf = armAlignTo16Bytes(aTempSrcCurrBuf);
3130c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMEParams = (OMXVCM4P2MEParams *)pMESpec;
3140c1bc742181ded4930842b46e9507372f0b1b963James Dong    pTempCurrPointPos->x = pCurrPointPos->x;
3150c1bc742181ded4930842b46e9507372f0b1b963James Dong    pTempCurrPointPos->y = pCurrPointPos->y;
3160c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSrcDstMBCurr->mbType = OMX_VC_INTER;
3170c1bc742181ded4930842b46e9507372f0b1b963James Dong
3180c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Preparing a linear buffer for block match */
3190c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (y = 0, index = count = 0; y < 16; y++, index += srcCurrStep - 16)
3200c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3210c1bc742181ded4930842b46e9507372f0b1b963James Dong        for(x = 0; x < 16; x++, count++, index++)
3220c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
3230c1bc742181ded4930842b46e9507372f0b1b963James Dong            pTempSrcCurrBuf[count] = pSrcCurrBuf[index];
3240c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
3250c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3260c1bc742181ded4930842b46e9507372f0b1b963James Dong    for(y = 0, index = 0; y < 2; y++)
3270c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3280c1bc742181ded4930842b46e9507372f0b1b963James Dong        for(x = 0; x < 2; x++,index++)
3290c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
3300c1bc742181ded4930842b46e9507372f0b1b963James Dong            if((pMBInfo[0] != NULL) && (pMBInfo[0]->mbType != OMX_VC_INTRA))
3310c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
3320c1bc742181ded4930842b46e9507372f0b1b963James Dong               pSrcCandMV1[index] = &(pMBInfo[0]->pMV0[y][x]);
3330c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
3340c1bc742181ded4930842b46e9507372f0b1b963James Dong            else
3350c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
3360c1bc742181ded4930842b46e9507372f0b1b963James Dong               pSrcCandMV1[index] = NULL;
3370c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
3380c1bc742181ded4930842b46e9507372f0b1b963James Dong            if((pMBInfo[1] != NULL) && (pMBInfo[1]->mbType != OMX_VC_INTRA))
3390c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
3400c1bc742181ded4930842b46e9507372f0b1b963James Dong               pSrcCandMV2[index] = &(pMBInfo[1]->pMV0[y][x]);
3410c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
3420c1bc742181ded4930842b46e9507372f0b1b963James Dong            else
3430c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
3440c1bc742181ded4930842b46e9507372f0b1b963James Dong               pSrcCandMV2[index] = NULL;
3450c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
3460c1bc742181ded4930842b46e9507372f0b1b963James Dong            if((pMBInfo[3] != NULL) && (pMBInfo[3]->mbType != OMX_VC_INTRA))
3470c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
3480c1bc742181ded4930842b46e9507372f0b1b963James Dong               pSrcCandMV3[index] = &(pMBInfo[3]->pMV0[y][x]);
3490c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
3500c1bc742181ded4930842b46e9507372f0b1b963James Dong            else
3510c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
3520c1bc742181ded4930842b46e9507372f0b1b963James Dong               pSrcCandMV3[index] = NULL;
3530c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
3540c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
3550c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3560c1bc742181ded4930842b46e9507372f0b1b963James Dong	/* Calculating SAD at MV(0,0) */
3570c1bc742181ded4930842b46e9507372f0b1b963James Dong	armVCCOMM_SAD(pTempSrcCurrBuf,
3580c1bc742181ded4930842b46e9507372f0b1b963James Dong					  16,
3590c1bc742181ded4930842b46e9507372f0b1b963James Dong					  pSrcRefBuf,
3600c1bc742181ded4930842b46e9507372f0b1b963James Dong					  srcRefStep,
3610c1bc742181ded4930842b46e9507372f0b1b963James Dong					  &Sad,
3620c1bc742181ded4930842b46e9507372f0b1b963James Dong					  16,
3630c1bc742181ded4930842b46e9507372f0b1b963James Dong					  16);
3640c1bc742181ded4930842b46e9507372f0b1b963James Dong	*pDstSAD = Sad;
3650c1bc742181ded4930842b46e9507372f0b1b963James Dong
3660c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Mode decision for NOT_CODED MB */
3670c1bc742181ded4930842b46e9507372f0b1b963James Dong	if(*pDstSAD == 0)
3680c1bc742181ded4930842b46e9507372f0b1b963James Dong	{
3690c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcDstMBCurr->pMV0[0][0].dx = 0;
3700c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcDstMBCurr->pMV0[0][0].dy = 0;
3710c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pDstSAD   = 0;
3720c1bc742181ded4930842b46e9507372f0b1b963James Dong		return OMX_Sts_NoErr;
3730c1bc742181ded4930842b46e9507372f0b1b963James Dong	}
3740c1bc742181ded4930842b46e9507372f0b1b963James Dong
3750c1bc742181ded4930842b46e9507372f0b1b963James Dong    omxVCM4P2_FindMVpred(
3760c1bc742181ded4930842b46e9507372f0b1b963James Dong                    &(pSrcDstMBCurr->pMV0[0][0]),
3770c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV1[0],
3780c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV2[0],
3790c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV3[0],
3800c1bc742181ded4930842b46e9507372f0b1b963James Dong                    &(pSrcDstMBCurr->pMVPred[0][0]),
3810c1bc742181ded4930842b46e9507372f0b1b963James Dong                    NULL,
3820c1bc742181ded4930842b46e9507372f0b1b963James Dong                    0);
3830c1bc742181ded4930842b46e9507372f0b1b963James Dong
3840c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Inter 1 MV */
3850c1bc742181ded4930842b46e9507372f0b1b963James Dong    armVCM4P2_BlockMatch_16x16(
3860c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcRefBuf,
3870c1bc742181ded4930842b46e9507372f0b1b963James Dong        srcRefStep,
3880c1bc742181ded4930842b46e9507372f0b1b963James Dong        pRefRect,
3890c1bc742181ded4930842b46e9507372f0b1b963James Dong        pTempSrcCurrBuf,
3900c1bc742181ded4930842b46e9507372f0b1b963James Dong        pCurrPointPos,
3910c1bc742181ded4930842b46e9507372f0b1b963James Dong        &(pSrcDstMBCurr->pMVPred[0][0]),
3920c1bc742181ded4930842b46e9507372f0b1b963James Dong        NULL,
3930c1bc742181ded4930842b46e9507372f0b1b963James Dong        pMEParams,
3940c1bc742181ded4930842b46e9507372f0b1b963James Dong        &dstMV16x16,
3950c1bc742181ded4930842b46e9507372f0b1b963James Dong        &dstSAD16x16);
3960c1bc742181ded4930842b46e9507372f0b1b963James Dong
3970c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Initialize all with 1 MV values */
3980c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSrcDstMBCurr->pMV0[0][0].dx = dstMV16x16.dx;
3990c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSrcDstMBCurr->pMV0[0][0].dy = dstMV16x16.dy;
4000c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSrcDstMBCurr->pMV0[0][1].dx = dstMV16x16.dx;
4010c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSrcDstMBCurr->pMV0[0][1].dy = dstMV16x16.dy;
4020c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSrcDstMBCurr->pMV0[1][0].dx = dstMV16x16.dx;
4030c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSrcDstMBCurr->pMV0[1][0].dy = dstMV16x16.dy;
4040c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSrcDstMBCurr->pMV0[1][1].dx = dstMV16x16.dx;
4050c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSrcDstMBCurr->pMV0[1][1].dy = dstMV16x16.dy;
4060c1bc742181ded4930842b46e9507372f0b1b963James Dong
4070c1bc742181ded4930842b46e9507372f0b1b963James Dong    *pDstSAD   = dstSAD16x16;
4080c1bc742181ded4930842b46e9507372f0b1b963James Dong
4090c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pMEParams->searchEnable8x8)
4100c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
4110c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Inter 4MV */
4120c1bc742181ded4930842b46e9507372f0b1b963James Dong        armVCM4P2_BlockMatch_8x8 (pSrcRefBuf,
4130c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      srcRefStep, pRefRect,
4140c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pTempSrcCurrBuf, pTempCurrPointPos,
4150c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      &(pSrcDstMBCurr->pMVPred[0][0]), NULL,
4160c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pMEParams, &(pSrcDstMBCurr->pMV0[0][0]),
4170c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      &dstSAD8x8
4180c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      );
4190c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstBlockSAD[0] = dstSAD8x8;
4200c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pDstSAD = dstSAD8x8;
4210c1bc742181ded4930842b46e9507372f0b1b963James Dong        pTempCurrPointPos->x += 8;
4220c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcRefBuf += 8;
4230c1bc742181ded4930842b46e9507372f0b1b963James Dong        omxVCM4P2_FindMVpred(
4240c1bc742181ded4930842b46e9507372f0b1b963James Dong                    &(pSrcDstMBCurr->pMV0[0][1]),
4250c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV1[1],
4260c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV2[1],
4270c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV3[1],
4280c1bc742181ded4930842b46e9507372f0b1b963James Dong                    &(pSrcDstMBCurr->pMVPred[0][1]),
4290c1bc742181ded4930842b46e9507372f0b1b963James Dong                    NULL,
4300c1bc742181ded4930842b46e9507372f0b1b963James Dong                    1);
4310c1bc742181ded4930842b46e9507372f0b1b963James Dong
4320c1bc742181ded4930842b46e9507372f0b1b963James Dong        armVCM4P2_BlockMatch_8x8 (pSrcRefBuf,
4330c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      srcRefStep, pRefRect,
4340c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pTempSrcCurrBuf, pTempCurrPointPos,
4350c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      &(pSrcDstMBCurr->pMVPred[0][1]), NULL,
4360c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pMEParams, &(pSrcDstMBCurr->pMV0[0][1]),
4370c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      &dstSAD8x8
4380c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      );
4390c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstBlockSAD[1] = dstSAD8x8;
4400c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pDstSAD += dstSAD8x8;
4410c1bc742181ded4930842b46e9507372f0b1b963James Dong        pTempCurrPointPos->x -= 8;
4420c1bc742181ded4930842b46e9507372f0b1b963James Dong        pTempCurrPointPos->y += 8;
4430c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcRefBuf += (srcRefStep * 8) - 8;
4440c1bc742181ded4930842b46e9507372f0b1b963James Dong
4450c1bc742181ded4930842b46e9507372f0b1b963James Dong        omxVCM4P2_FindMVpred(
4460c1bc742181ded4930842b46e9507372f0b1b963James Dong                    &(pSrcDstMBCurr->pMV0[1][0]),
4470c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV1[2],
4480c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV2[2],
4490c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV3[2],
4500c1bc742181ded4930842b46e9507372f0b1b963James Dong                    &(pSrcDstMBCurr->pMVPred[1][0]),
4510c1bc742181ded4930842b46e9507372f0b1b963James Dong                    NULL,
4520c1bc742181ded4930842b46e9507372f0b1b963James Dong                    2);
4530c1bc742181ded4930842b46e9507372f0b1b963James Dong        armVCM4P2_BlockMatch_8x8 (pSrcRefBuf,
4540c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      srcRefStep, pRefRect,
4550c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pTempSrcCurrBuf, pTempCurrPointPos,
4560c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      &(pSrcDstMBCurr->pMVPred[1][0]), NULL,
4570c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pMEParams, &(pSrcDstMBCurr->pMV0[1][0]),
4580c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      &dstSAD8x8
4590c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      );
4600c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstBlockSAD[2] = dstSAD8x8;
4610c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pDstSAD += dstSAD8x8;
4620c1bc742181ded4930842b46e9507372f0b1b963James Dong        pTempCurrPointPos->x += 8;
4630c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcRefBuf += 8;
4640c1bc742181ded4930842b46e9507372f0b1b963James Dong        omxVCM4P2_FindMVpred(
4650c1bc742181ded4930842b46e9507372f0b1b963James Dong                    &(pSrcDstMBCurr->pMV0[1][1]),
4660c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV1[3],
4670c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV2[3],
4680c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSrcCandMV3[3],
4690c1bc742181ded4930842b46e9507372f0b1b963James Dong                    &(pSrcDstMBCurr->pMVPred[1][1]),
4700c1bc742181ded4930842b46e9507372f0b1b963James Dong                    NULL,
4710c1bc742181ded4930842b46e9507372f0b1b963James Dong                    3);
4720c1bc742181ded4930842b46e9507372f0b1b963James Dong        armVCM4P2_BlockMatch_8x8 (pSrcRefBuf,
4730c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      srcRefStep, pRefRect,
4740c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pTempSrcCurrBuf, pTempCurrPointPos,
4750c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      &(pSrcDstMBCurr->pMVPred[1][1]), NULL,
4760c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pMEParams, &(pSrcDstMBCurr->pMV0[1][1]),
4770c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      &dstSAD8x8
4780c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      );
4790c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstBlockSAD[3] = dstSAD8x8;
4800c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pDstSAD += dstSAD8x8;
4810c1bc742181ded4930842b46e9507372f0b1b963James Dong
4820c1bc742181ded4930842b46e9507372f0b1b963James Dong
4830c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Checking if 4MV is equal to 1MV */
4840c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (
4850c1bc742181ded4930842b46e9507372f0b1b963James Dong            (pSrcDstMBCurr->pMV0[0][0].dx != dstMV16x16.dx) ||
4860c1bc742181ded4930842b46e9507372f0b1b963James Dong            (pSrcDstMBCurr->pMV0[0][0].dy != dstMV16x16.dy) ||
4870c1bc742181ded4930842b46e9507372f0b1b963James Dong            (pSrcDstMBCurr->pMV0[0][1].dx != dstMV16x16.dx) ||
4880c1bc742181ded4930842b46e9507372f0b1b963James Dong            (pSrcDstMBCurr->pMV0[0][1].dy != dstMV16x16.dy) ||
4890c1bc742181ded4930842b46e9507372f0b1b963James Dong            (pSrcDstMBCurr->pMV0[1][0].dx != dstMV16x16.dx) ||
4900c1bc742181ded4930842b46e9507372f0b1b963James Dong            (pSrcDstMBCurr->pMV0[1][0].dy != dstMV16x16.dy) ||
4910c1bc742181ded4930842b46e9507372f0b1b963James Dong            (pSrcDstMBCurr->pMV0[1][1].dx != dstMV16x16.dx) ||
4920c1bc742181ded4930842b46e9507372f0b1b963James Dong            (pSrcDstMBCurr->pMV0[1][1].dy != dstMV16x16.dy)
4930c1bc742181ded4930842b46e9507372f0b1b963James Dong           )
4940c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
4950c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* select the 4 MV */
4960c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSrcDstMBCurr->mbType = OMX_VC_INTER4V;
4970c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
4980c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
4990c1bc742181ded4930842b46e9507372f0b1b963James Dong
5000c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* finding the error in intra mode */
5010c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (count = 0, average = 0; count < 256 ; count++)
5020c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
5030c1bc742181ded4930842b46e9507372f0b1b963James Dong        average = average + pTempSrcCurrBuf[count];
5040c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
5050c1bc742181ded4930842b46e9507372f0b1b963James Dong    average = average/256;
5060c1bc742181ded4930842b46e9507372f0b1b963James Dong
5070c1bc742181ded4930842b46e9507372f0b1b963James Dong	intraSAD = 0;
5080c1bc742181ded4930842b46e9507372f0b1b963James Dong
5090c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Intra SAD calculation */
5100c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (count = 0; count < 256 ; count++)
5110c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
5120c1bc742181ded4930842b46e9507372f0b1b963James Dong        intraSAD += armAbs ((pTempSrcCurrBuf[count]) - (average));
5130c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
5140c1bc742181ded4930842b46e9507372f0b1b963James Dong
5150c1bc742181ded4930842b46e9507372f0b1b963James Dong	/* Using the MPEG4 VM formula for intra/inter mode decision
5160c1bc742181ded4930842b46e9507372f0b1b963James Dong	   Var < (SAD - 2*NB) where NB = N^2 is the number of pixels
5170c1bc742181ded4930842b46e9507372f0b1b963James Dong	   of the macroblock.*/
5180c1bc742181ded4930842b46e9507372f0b1b963James Dong
5190c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (intraSAD <= (*pDstSAD - 512))
5200c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
5210c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcDstMBCurr->mbType = OMX_VC_INTRA;
5220c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcDstMBCurr->pMV0[0][0].dx = 0;
5230c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSrcDstMBCurr->pMV0[0][0].dy = 0;
5240c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pDstSAD   = intraSAD;
5250c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstBlockSAD[0] = 0xFFFF;
5260c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstBlockSAD[1] = 0xFFFF;
5270c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstBlockSAD[2] = 0xFFFF;
5280c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstBlockSAD[3] = 0xFFFF;
5290c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
5300c1bc742181ded4930842b46e9507372f0b1b963James Dong
5310c1bc742181ded4930842b46e9507372f0b1b963James Dong    if(pSrcDstMBCurr->mbType == OMX_VC_INTER)
5320c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
5330c1bc742181ded4930842b46e9507372f0b1b963James Dong      pTempSrcRefBuf = pSrcRefBuf + (srcRefStep * dstMV16x16.dy) + dstMV16x16.dx;
5340c1bc742181ded4930842b46e9507372f0b1b963James Dong
5350c1bc742181ded4930842b46e9507372f0b1b963James Dong      if((dstMV16x16.dx & 0x1) && (dstMV16x16.dy & 0x1))
5360c1bc742181ded4930842b46e9507372f0b1b963James Dong      {
5370c1bc742181ded4930842b46e9507372f0b1b963James Dong        predictType = OMX_VC_HALF_PIXEL_XY;
5380c1bc742181ded4930842b46e9507372f0b1b963James Dong      }
5390c1bc742181ded4930842b46e9507372f0b1b963James Dong      else if(dstMV16x16.dx & 0x1)
5400c1bc742181ded4930842b46e9507372f0b1b963James Dong      {
5410c1bc742181ded4930842b46e9507372f0b1b963James Dong        predictType = OMX_VC_HALF_PIXEL_X;
5420c1bc742181ded4930842b46e9507372f0b1b963James Dong      }
5430c1bc742181ded4930842b46e9507372f0b1b963James Dong      else if(dstMV16x16.dy & 0x1)
5440c1bc742181ded4930842b46e9507372f0b1b963James Dong      {
5450c1bc742181ded4930842b46e9507372f0b1b963James Dong        predictType = OMX_VC_HALF_PIXEL_Y;
5460c1bc742181ded4930842b46e9507372f0b1b963James Dong      }
5470c1bc742181ded4930842b46e9507372f0b1b963James Dong      else
5480c1bc742181ded4930842b46e9507372f0b1b963James Dong      {
5490c1bc742181ded4930842b46e9507372f0b1b963James Dong        predictType = OMX_VC_INTEGER_PIXEL;
5500c1bc742181ded4930842b46e9507372f0b1b963James Dong      }
5510c1bc742181ded4930842b46e9507372f0b1b963James Dong
5520c1bc742181ded4930842b46e9507372f0b1b963James Dong      pDst = armAlignTo8Bytes(&(aDst[0]));
5530c1bc742181ded4930842b46e9507372f0b1b963James Dong      /* Calculating Block SAD at MV(dstMV16x16.dx,dstMV16x16.dy) */
5540c1bc742181ded4930842b46e9507372f0b1b963James Dong	  /* Block 0 */
5550c1bc742181ded4930842b46e9507372f0b1b963James Dong      omxVCM4P2_MCReconBlock(pTempSrcRefBuf,
5560c1bc742181ded4930842b46e9507372f0b1b963James Dong	                             srcRefStep,
5570c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 NULL,
5580c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 pDst,
5590c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 dstStep,
5600c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 predictType,
5610c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 pMEParams->rndVal);
5620c1bc742181ded4930842b46e9507372f0b1b963James Dong
5630c1bc742181ded4930842b46e9507372f0b1b963James Dong      armVCCOMM_SAD(pTempSrcCurrBuf,
5640c1bc742181ded4930842b46e9507372f0b1b963James Dong                        16,
5650c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pDst,
5660c1bc742181ded4930842b46e9507372f0b1b963James Dong                        dstStep,
5670c1bc742181ded4930842b46e9507372f0b1b963James Dong                        &Sad,
5680c1bc742181ded4930842b46e9507372f0b1b963James Dong                        8,
5690c1bc742181ded4930842b46e9507372f0b1b963James Dong                        8);
5700c1bc742181ded4930842b46e9507372f0b1b963James Dong      pDstBlockSAD[0] = Sad;
5710c1bc742181ded4930842b46e9507372f0b1b963James Dong
5720c1bc742181ded4930842b46e9507372f0b1b963James Dong      /* Block 1 */
5730c1bc742181ded4930842b46e9507372f0b1b963James Dong      omxVCM4P2_MCReconBlock(pTempSrcRefBuf + 8,
5740c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 srcRefStep,
5750c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 NULL,
5760c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 pDst,
5770c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 dstStep,
5780c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 predictType,
5790c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 pMEParams->rndVal);
5800c1bc742181ded4930842b46e9507372f0b1b963James Dong
5810c1bc742181ded4930842b46e9507372f0b1b963James Dong      armVCCOMM_SAD(pTempSrcCurrBuf + 8,
5820c1bc742181ded4930842b46e9507372f0b1b963James Dong                        16,
5830c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pDst,
5840c1bc742181ded4930842b46e9507372f0b1b963James Dong                        dstStep,
5850c1bc742181ded4930842b46e9507372f0b1b963James Dong                        &Sad,
5860c1bc742181ded4930842b46e9507372f0b1b963James Dong                        8,
5870c1bc742181ded4930842b46e9507372f0b1b963James Dong                        8);
5880c1bc742181ded4930842b46e9507372f0b1b963James Dong      pDstBlockSAD[1] = Sad;
5890c1bc742181ded4930842b46e9507372f0b1b963James Dong
5900c1bc742181ded4930842b46e9507372f0b1b963James Dong      /* Block 2 */
5910c1bc742181ded4930842b46e9507372f0b1b963James Dong      omxVCM4P2_MCReconBlock(pTempSrcRefBuf + (srcRefStep*8),
5920c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 srcRefStep,
5930c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 NULL,
5940c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 pDst,
5950c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 dstStep,
5960c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 predictType,
5970c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 pMEParams->rndVal);
5980c1bc742181ded4930842b46e9507372f0b1b963James Dong
5990c1bc742181ded4930842b46e9507372f0b1b963James Dong      armVCCOMM_SAD(pTempSrcCurrBuf + (16*8),
6000c1bc742181ded4930842b46e9507372f0b1b963James Dong                        16,
6010c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pDst,
6020c1bc742181ded4930842b46e9507372f0b1b963James Dong                        dstStep,
6030c1bc742181ded4930842b46e9507372f0b1b963James Dong                        &Sad,
6040c1bc742181ded4930842b46e9507372f0b1b963James Dong                        8,
6050c1bc742181ded4930842b46e9507372f0b1b963James Dong                        8);
6060c1bc742181ded4930842b46e9507372f0b1b963James Dong      pDstBlockSAD[2] = Sad;
6070c1bc742181ded4930842b46e9507372f0b1b963James Dong
6080c1bc742181ded4930842b46e9507372f0b1b963James Dong	  /* Block 3 */
6090c1bc742181ded4930842b46e9507372f0b1b963James Dong      omxVCM4P2_MCReconBlock(pTempSrcRefBuf + (srcRefStep*8) + 8,
6100c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 srcRefStep,
6110c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 NULL,
6120c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 pDst,
6130c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 dstStep,
6140c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 predictType,
6150c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 pMEParams->rndVal);
6160c1bc742181ded4930842b46e9507372f0b1b963James Dong
6170c1bc742181ded4930842b46e9507372f0b1b963James Dong      armVCCOMM_SAD(pTempSrcCurrBuf + (16*8) + 8,
6180c1bc742181ded4930842b46e9507372f0b1b963James Dong                        16,
6190c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pDst,
6200c1bc742181ded4930842b46e9507372f0b1b963James Dong                        dstStep,
6210c1bc742181ded4930842b46e9507372f0b1b963James Dong                        &Sad,
6220c1bc742181ded4930842b46e9507372f0b1b963James Dong                        8,
6230c1bc742181ded4930842b46e9507372f0b1b963James Dong                        8);
6240c1bc742181ded4930842b46e9507372f0b1b963James Dong      pDstBlockSAD[3] = Sad;
6250c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
6260c1bc742181ded4930842b46e9507372f0b1b963James Dong    return OMX_Sts_NoErr;
6270c1bc742181ded4930842b46e9507372f0b1b963James Dong}
6280c1bc742181ded4930842b46e9507372f0b1b963James Dong
6290c1bc742181ded4930842b46e9507372f0b1b963James Dong/* End of file */
6300c1bc742181ded4930842b46e9507372f0b1b963James Dong
631