10c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
20c1bc742181ded4930842b46e9507372f0b1b963James Dong *
30c1bc742181ded4930842b46e9507372f0b1b963James Dong * File Name:  omxVCM4P10_BlockMatch_Integer.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 modules for Block matching, a full search algorithm
140c1bc742181ded4930842b46e9507372f0b1b963James Dong * is implemented
150c1bc742181ded4930842b46e9507372f0b1b963James Dong *
160c1bc742181ded4930842b46e9507372f0b1b963James Dong */
170c1bc742181ded4930842b46e9507372f0b1b963James Dong
180c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxtypes.h"
190c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armOMX.h"
200c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxVC.h"
210c1bc742181ded4930842b46e9507372f0b1b963James Dong
220c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVC.h"
230c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armCOMM.h"
240c1bc742181ded4930842b46e9507372f0b1b963James Dong
250c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
260c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function:  omxVCM4P10_BlockMatch_Integer   (6.3.5.2.1)
270c1bc742181ded4930842b46e9507372f0b1b963James Dong *
280c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
290c1bc742181ded4930842b46e9507372f0b1b963James Dong * Performs integer block match.  Returns best MV and associated cost.
300c1bc742181ded4930842b46e9507372f0b1b963James Dong *
310c1bc742181ded4930842b46e9507372f0b1b963James Dong * Input Arguments:
320c1bc742181ded4930842b46e9507372f0b1b963James Dong *
330c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcOrgY - Pointer to the top-left corner of the current block. If
340c1bc742181ded4930842b46e9507372f0b1b963James Dong *            iBlockWidth==4,  4-byte alignment required. If iBlockWidth==8,
350c1bc742181ded4930842b46e9507372f0b1b963James Dong *            8-byte alignment required. If iBlockWidth==16, 16-byte alignment
360c1bc742181ded4930842b46e9507372f0b1b963James Dong *            required.
370c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcRefY - Pointer to the top-left corner of the co-located block in the
380c1bc742181ded4930842b46e9507372f0b1b963James Dong *            reference picture. If iBlockWidth==4,  4-byte alignment
390c1bc742181ded4930842b46e9507372f0b1b963James Dong *            required.  If iBlockWidth==8,  8-byte alignment required.  If
400c1bc742181ded4930842b46e9507372f0b1b963James Dong *            iBlockWidth==16, 16-byte alignment required.
410c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nSrcOrgStep - Stride of the original picture plane, expressed in terms
420c1bc742181ded4930842b46e9507372f0b1b963James Dong *            of integer pixels; must be a multiple of iBlockWidth.
430c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nSrcRefStep - Stride of the reference picture plane, expressed in terms
440c1bc742181ded4930842b46e9507372f0b1b963James Dong *            of integer pixels
450c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pRefRect - pointer to the valid reference rectangle inside the reference
460c1bc742181ded4930842b46e9507372f0b1b963James Dong *            picture plane
470c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nCurrPointPos - position of the current block in the current plane
480c1bc742181ded4930842b46e9507372f0b1b963James Dong *   iBlockWidth - Width of the current block, expressed in terms of integer
490c1bc742181ded4930842b46e9507372f0b1b963James Dong *            pixels; must be equal to either 4, 8, or 16.
500c1bc742181ded4930842b46e9507372f0b1b963James Dong *   iBlockHeight - Height of the current block, expressed in terms of
510c1bc742181ded4930842b46e9507372f0b1b963James Dong *            integer pixels; must be equal to either 4, 8, or 16.
520c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nLamda - Lamda factor; used to compute motion cost
530c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pMVPred - Predicted MV; used to compute motion cost, expressed in terms
540c1bc742181ded4930842b46e9507372f0b1b963James Dong *            of 1/4-pel units
550c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pMVCandidate - Candidate MV; used to initialize the motion search,
560c1bc742181ded4930842b46e9507372f0b1b963James Dong *            expressed in terms of integer pixels
570c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pMESpec - pointer to the ME specification structure
580c1bc742181ded4930842b46e9507372f0b1b963James Dong *
590c1bc742181ded4930842b46e9507372f0b1b963James Dong * Output Arguments:
600c1bc742181ded4930842b46e9507372f0b1b963James Dong *
610c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pDstBestMV - Best MV resulting from integer search, expressed in terms
620c1bc742181ded4930842b46e9507372f0b1b963James Dong *            of 1/4-pel units
630c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pBestCost - Motion cost associated with the best MV; computed as
640c1bc742181ded4930842b46e9507372f0b1b963James Dong *            SAD+Lamda*BitsUsedByMV
650c1bc742181ded4930842b46e9507372f0b1b963James Dong *
660c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
670c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_NoErr, if the function runs without error.
680c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_BadArgErr - bad arguments: if one of the following cases occurs:
690c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    any of the following poitners are NULL:
700c1bc742181ded4930842b46e9507372f0b1b963James Dong *         pSrcOrgY, pSrcRefY, pRefRect, pMVPred, pMVCandidate, or pMESpec.
710c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    Either iBlockWidth or iBlockHeight are values other than 4, 8, or 16.
720c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    Any alignment restrictions are violated
730c1bc742181ded4930842b46e9507372f0b1b963James Dong *
740c1bc742181ded4930842b46e9507372f0b1b963James Dong */
750c1bc742181ded4930842b46e9507372f0b1b963James Dong
760c1bc742181ded4930842b46e9507372f0b1b963James Dong OMXResult omxVCM4P10_BlockMatch_Integer (
770c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMX_U8 *pSrcOrgY,
780c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_S32 nSrcOrgStep,
790c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMX_U8 *pSrcRefY,
800c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_S32 nSrcRefStep,
810c1bc742181ded4930842b46e9507372f0b1b963James Dong	 const OMXRect *pRefRect,
820c1bc742181ded4930842b46e9507372f0b1b963James Dong	 const OMXVCM4P2Coordinate *pCurrPointPos,
830c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_U8 iBlockWidth,
840c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_U8 iBlockHeight,
850c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_U32 nLamda,
860c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXVCMotionVector *pMVPred,
870c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXVCMotionVector *pMVCandidate,
880c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMXVCMotionVector *pBestMV,
890c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_S32 *pBestCost,
900c1bc742181ded4930842b46e9507372f0b1b963James Dong     void *pMESpec
910c1bc742181ded4930842b46e9507372f0b1b963James Dong)
920c1bc742181ded4930842b46e9507372f0b1b963James Dong{
930c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Definitions and Initializations*/
940c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT candSAD;
950c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT fromX, toX, fromY, toY;
960c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Offset to the reference at the begining of the bounding box */
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8 *pTempSrcRefY, *pTempSrcOrgY;
980c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S16 x, y;
990c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector diffMV;
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 nSearchRange;
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    ARMVCM4P10_MESpec *armMESpec = (ARMVCM4P10_MESpec *) pMESpec;
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Argument error checks */
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth ==  4) && (!armIs4ByteAligned(pSrcOrgY)), OMX_Sts_BadArgErr);
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth ==  8) && (!armIs8ByteAligned(pSrcOrgY)), OMX_Sts_BadArgErr);
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth == 16) && (!armIs16ByteAligned(pSrcOrgY)), OMX_Sts_BadArgErr);
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf((iBlockWidth ==  4) && (!armIs4ByteAligned(pSrcRefY)), OMX_Sts_BadArgErr);
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth ==  8) && (!armIs8ByteAligned(pSrcRefY)), OMX_Sts_BadArgErr);
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth == 16) && (!armIs16ByteAligned(pSrcRefY)), OMX_Sts_BadArgErr);
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcOrgY == NULL, OMX_Sts_BadArgErr);
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcRefY == NULL, OMX_Sts_BadArgErr);
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pMVPred == NULL, OMX_Sts_BadArgErr);
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pMVCandidate == NULL, OMX_Sts_BadArgErr);
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pBestMV == NULL, OMX_Sts_BadArgErr);
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pBestCost == NULL, OMX_Sts_BadArgErr);
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(((iBlockWidth!=4)&&(iBlockWidth!=8)&&(iBlockWidth!=16)) , OMX_Sts_BadArgErr);
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(((iBlockHeight!=4)&&(iBlockHeight!=8)&&(iBlockHeight!=16)) , OMX_Sts_BadArgErr);
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong    armIgnore (pMESpec);
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong    if(iBlockWidth == 4)
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong        nSearchRange = armMESpec->MEParams.searchRange4x4;
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong    else if(iBlockWidth == 8)
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong        nSearchRange = armMESpec->MEParams.searchRange8x8;
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong    else
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong        nSearchRange = armMESpec->MEParams.searchRange16x16;
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Check for valid region */
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong    fromX = nSearchRange;
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong    toX   = nSearchRange;
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong    fromY = nSearchRange;
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong    toY   = nSearchRange;
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((pCurrPointPos->x - nSearchRange) < pRefRect->x)
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong        fromX =  pCurrPointPos->x - pRefRect->x;
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((pCurrPointPos->x + iBlockWidth + nSearchRange) > (pRefRect->x + pRefRect->width))
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong        toX   = pRefRect->width - (pCurrPointPos->x - pRefRect->x) - iBlockWidth;
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((pCurrPointPos->y - nSearchRange) < pRefRect->y)
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong        fromY = pCurrPointPos->y - pRefRect->y;
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((pCurrPointPos->y + iBlockWidth + nSearchRange) > (pRefRect->y + pRefRect->height))
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong        toY   = pRefRect->width - (pCurrPointPos->y - pRefRect->y) - iBlockWidth;
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong    pBestMV->dx = -fromX * 4;
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong    pBestMV->dy = -fromY * 4;
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Initialize to max value as a start point */
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong    *pBestCost = 0x7fffffff;
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Looping on y- axis */
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (y = -fromY; y <= toY; y++)
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Looping on x- axis */
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong        for (x = -fromX; x <= toX; x++)
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Positioning the pointer */
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong            pTempSrcRefY = pSrcRefY + (nSrcRefStep * y) + x;
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong            pTempSrcOrgY = pSrcOrgY;
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Calculate the SAD */
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong            armVCCOMM_SAD(
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong    	        pTempSrcOrgY,
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong    	        nSrcOrgStep,
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong    	        pTempSrcRefY,
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong    	        nSrcRefStep,
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong    	        &candSAD,
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong    	        iBlockHeight,
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong    	        iBlockWidth);
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong            diffMV.dx = (x * 4) - pMVPred->dx;
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong            diffMV.dy = (y * 4) - pMVPred->dy;
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Result calculations */
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong            armVCM4P10_CompareMotionCostToMV ((x * 4), (y * 4), diffMV, candSAD, pBestMV, nLamda, pBestCost);
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong        } /* End of x- axis */
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong    } /* End of y-axis */
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong    return OMX_Sts_NoErr;
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong/* End of file */
197