10c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
20c1bc742181ded4930842b46e9507372f0b1b963James Dong *
30c1bc742181ded4930842b46e9507372f0b1b963James Dong * File Name:  omxVCM4P10_BlockMatch_Half.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 half pel Block matching,
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/**
260c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function:  omxVCM4P10_BlockMatch_Half   (6.3.5.2.2)
270c1bc742181ded4930842b46e9507372f0b1b963James Dong *
280c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
290c1bc742181ded4930842b46e9507372f0b1b963James Dong * Performs a half-pel block match using results from a prior integer search.
300c1bc742181ded4930842b46e9507372f0b1b963James Dong *  Returns the best MV and associated cost.  This function estimates the
310c1bc742181ded4930842b46e9507372f0b1b963James Dong * half-pixel motion vector by interpolating the integer resolution motion
320c1bc742181ded4930842b46e9507372f0b1b963James Dong * vector referenced by the input parameter pSrcDstBestMV, i.e., the initial
330c1bc742181ded4930842b46e9507372f0b1b963James Dong * integer MV is generated externally.  The function
340c1bc742181ded4930842b46e9507372f0b1b963James Dong * omxVCM4P10_BlockMatch_Integer may be used for integer motion estimation.
350c1bc742181ded4930842b46e9507372f0b1b963James Dong *
360c1bc742181ded4930842b46e9507372f0b1b963James Dong * Input Arguments:
370c1bc742181ded4930842b46e9507372f0b1b963James Dong *
380c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcOrgY - Pointer to the current position in original picture plane. If
390c1bc742181ded4930842b46e9507372f0b1b963James Dong *            iBlockWidth==4,  4-byte alignment required. If iBlockWidth==8,
400c1bc742181ded4930842b46e9507372f0b1b963James Dong *            8-byte alignment required. If iBlockWidth==16, 16-byte alignment
410c1bc742181ded4930842b46e9507372f0b1b963James Dong *            required.
420c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcRefY - Pointer to the top-left corner of the co-located block in the
430c1bc742181ded4930842b46e9507372f0b1b963James Dong *            reference picture  If iBlockWidth==4,  4-byte alignment
440c1bc742181ded4930842b46e9507372f0b1b963James Dong *            required.  If iBlockWidth==8,  8-byte alignment required.  If
450c1bc742181ded4930842b46e9507372f0b1b963James Dong *            iBlockWidth==16, 16-byte alignment required.
460c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nSrcOrgStep - Stride of the original picture plane in terms of full
470c1bc742181ded4930842b46e9507372f0b1b963James Dong *            pixels; must be a multiple of iBlockWidth.
480c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nSrcRefStep - Stride of the reference picture plane in terms of full
490c1bc742181ded4930842b46e9507372f0b1b963James Dong *            pixels
500c1bc742181ded4930842b46e9507372f0b1b963James Dong *   iBlockWidth - Width of the current block in terms of full pixels; must
510c1bc742181ded4930842b46e9507372f0b1b963James Dong *            be equal to either 4, 8, or 16.
520c1bc742181ded4930842b46e9507372f0b1b963James Dong *   iBlockHeight - Height of the current block in terms of full pixels; must
530c1bc742181ded4930842b46e9507372f0b1b963James Dong *            be equal to either 4, 8, or 16.
540c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nLamda - Lamda factor, used to compute motion cost
550c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pMVPred - Predicted MV, represented in terms of 1/4-pel units; used to
560c1bc742181ded4930842b46e9507372f0b1b963James Dong *            compute motion cost
570c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcDstBestMV - The best MV resulting from a prior integer search,
580c1bc742181ded4930842b46e9507372f0b1b963James Dong *            represented in terms of 1/4-pel units
590c1bc742181ded4930842b46e9507372f0b1b963James Dong *
600c1bc742181ded4930842b46e9507372f0b1b963James Dong * Output Arguments:
610c1bc742181ded4930842b46e9507372f0b1b963James Dong *
620c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcDstBestMV - Best MV resulting from the half-pel search, expressed in
630c1bc742181ded4930842b46e9507372f0b1b963James Dong *            terms of 1/4-pel units
640c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pBestCost - Motion cost associated with the best MV; computed as
650c1bc742181ded4930842b46e9507372f0b1b963James Dong *            SAD+Lamda*BitsUsedByMV
660c1bc742181ded4930842b46e9507372f0b1b963James Dong *
670c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
680c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_NoErr, if the function runs without error.
690c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_BadArgErr - bad arguments: if one of the following cases occurs:
700c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    any of the following pointers is NULL: pSrcOrgY, pSrcRefY,
710c1bc742181ded4930842b46e9507372f0b1b963James Dong *              pSrcDstBestMV, pMVPred, pBestCost
720c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    iBlockWidth or iBlockHeight are equal to values other than 4, 8, or 16.
730c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    Any alignment restrictions are violated
740c1bc742181ded4930842b46e9507372f0b1b963James Dong *
750c1bc742181ded4930842b46e9507372f0b1b963James Dong */
760c1bc742181ded4930842b46e9507372f0b1b963James Dong
770c1bc742181ded4930842b46e9507372f0b1b963James DongOMXResult omxVCM4P10_BlockMatch_Half(
780c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8* pSrcOrgY,
790c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 nSrcOrgStep,
800c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8* pSrcRefY,
810c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 nSrcRefStep,
820c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8 iBlockWidth,
830c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8 iBlockHeight,
840c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U32 nLamda,
850c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMXVCMotionVector* pMVPred,
860c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector* pSrcDstBestMV,
870c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32* pBestCost
880c1bc742181ded4930842b46e9507372f0b1b963James Dong)
890c1bc742181ded4930842b46e9507372f0b1b963James Dong{
900c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Definitions and Initializations*/
910c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT     candSAD;
920c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT     fromX, toX, fromY, toY;
930c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Offset to the reference at the begining of the bounding box */
940c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8      *pTempSrcRefY, *pTempSrcOrgY;
950c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S16     x, y;
960c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector diffMV, candMV, integerMV;
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8      interpolY[256];
980c1bc742181ded4930842b46e9507372f0b1b963James Dong
990c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Argument error checks */
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth ==  4) && (!armIs4ByteAligned(pSrcOrgY)), OMX_Sts_BadArgErr);
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth ==  8) && (!armIs8ByteAligned(pSrcOrgY)), OMX_Sts_BadArgErr);
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth == 16) && (!armIs16ByteAligned(pSrcOrgY)), OMX_Sts_BadArgErr);
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf((iBlockWidth ==  4) && (!armIs4ByteAligned(pSrcRefY)), OMX_Sts_BadArgErr);
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth ==  8) && (!armIs8ByteAligned(pSrcRefY)), OMX_Sts_BadArgErr);
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth == 16) && (!armIs16ByteAligned(pSrcRefY)), OMX_Sts_BadArgErr);
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((nSrcOrgStep % iBlockWidth), OMX_Sts_BadArgErr);
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcOrgY == NULL, OMX_Sts_BadArgErr);
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcRefY == NULL, OMX_Sts_BadArgErr);
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pMVPred == NULL, OMX_Sts_BadArgErr);
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcDstBestMV == NULL, OMX_Sts_BadArgErr);
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pBestCost == NULL, OMX_Sts_BadArgErr);
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(((iBlockWidth!=4)&&(iBlockWidth!=8)&&(iBlockWidth!=16)) , OMX_Sts_BadArgErr);
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(((iBlockHeight!=4)&&(iBlockHeight!=8)&&(iBlockHeight!=16)) , OMX_Sts_BadArgErr);
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Check for valid region */
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong    fromX = 1;
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong    toX   = 1;
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong    fromY = 1;
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong    toY   = 1;
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Initialize to max value as a start point */
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong    *pBestCost = 0x7fffffff;
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong    integerMV.dx = pSrcDstBestMV->dx;
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong    integerMV.dy = pSrcDstBestMV->dy;
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Looping on y- axis */
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (y = -fromY; y <= toY; y++)
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Looping on x- axis */
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong        for (x = -fromX; x <= toX; x++)
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Positioning the pointer */
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong            pTempSrcRefY = pSrcRefY + (nSrcRefStep * (integerMV.dy/4)) + (integerMV.dx/4);
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (x < 0)
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong                pTempSrcRefY = pTempSrcRefY + x;
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (y < 0)
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong                pTempSrcRefY = pTempSrcRefY + (y * nSrcRefStep);
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong            pTempSrcOrgY = pSrcOrgY;
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Prepare cand MV */
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong            candMV.dx = integerMV.dx + x * 2;
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong            candMV.dy = integerMV.dy + y * 2;
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Interpolate half pel for the current position*/
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong            armVCM4P10_Interpolate_Luma(
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pTempSrcRefY,
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong                        nSrcRefStep,
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong                        interpolY,
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockWidth,
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockWidth,
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockHeight,
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong                        armAbs(x) * 2,
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong                        armAbs(y) * 2);
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Calculate the SAD */
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong            armVCCOMM_SAD(
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pTempSrcOrgY,
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong                        nSrcOrgStep,
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong                        interpolY,
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockWidth,
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong                        &candSAD,
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockHeight,
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockWidth);
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong            diffMV.dx = candMV.dx - pMVPred->dx;
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong            diffMV.dy = candMV.dy - pMVPred->dy;
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Result calculations */
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong            armVCM4P10_CompareMotionCostToMV (
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong                        candMV.dx,
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong                        candMV.dy,
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong                        diffMV,
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong                        candSAD,
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pSrcDstBestMV,
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong                        nLamda,
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pBestCost);
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong        } /* End of x- axis */
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong    } /* End of y-axis */
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong    return OMX_Sts_NoErr;
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong/* End of file */
192