178e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar/*
278e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * Copyright (C) 2007-2008 ARM Limited
378e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *
478e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * Licensed under the Apache License, Version 2.0 (the "License");
578e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * you may not use this file except in compliance with the License.
678e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * You may obtain a copy of the License at
778e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *
878e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *      http://www.apache.org/licenses/LICENSE-2.0
978e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *
1078e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * Unless required by applicable law or agreed to in writing, software
1178e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * distributed under the License is distributed on an "AS IS" BASIS,
1278e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1378e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * See the License for the specific language governing permissions and
1478e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * limitations under the License.
1578e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *
1678e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar */
170c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
180c1bc742181ded4930842b46e9507372f0b1b963James Dong *
190c1bc742181ded4930842b46e9507372f0b1b963James Dong * File Name:  omxVCM4P10_BlockMatch_Quarter.c
200c1bc742181ded4930842b46e9507372f0b1b963James Dong * OpenMAX DL: v1.0.2
210c1bc742181ded4930842b46e9507372f0b1b963James Dong * Revision:   9641
220c1bc742181ded4930842b46e9507372f0b1b963James Dong * Date:       Thursday, February 7, 2008
230c1bc742181ded4930842b46e9507372f0b1b963James Dong *
240c1bc742181ded4930842b46e9507372f0b1b963James Dong *
250c1bc742181ded4930842b46e9507372f0b1b963James Dong *
260c1bc742181ded4930842b46e9507372f0b1b963James Dong *
270c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
280c1bc742181ded4930842b46e9507372f0b1b963James Dong * Contains modules for quater pel Block matching,
290c1bc742181ded4930842b46e9507372f0b1b963James Dong *
300c1bc742181ded4930842b46e9507372f0b1b963James Dong */
310c1bc742181ded4930842b46e9507372f0b1b963James Dong
320c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxtypes.h"
330c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armOMX.h"
340c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxVC.h"
350c1bc742181ded4930842b46e9507372f0b1b963James Dong
360c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVC.h"
370c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armCOMM.h"
380c1bc742181ded4930842b46e9507372f0b1b963James Dong
390c1bc742181ded4930842b46e9507372f0b1b963James Dong
400c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
410c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function:  omxVCM4P10_BlockMatch_Quarter   (6.3.5.2.3)
420c1bc742181ded4930842b46e9507372f0b1b963James Dong *
430c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
440c1bc742181ded4930842b46e9507372f0b1b963James Dong * Performs a quarter-pel block match using results from a prior half-pel
450c1bc742181ded4930842b46e9507372f0b1b963James Dong * search.  Returns the best MV and associated cost.  This function estimates
460c1bc742181ded4930842b46e9507372f0b1b963James Dong * the quarter-pixel motion vector by interpolating the half-pel resolution
470c1bc742181ded4930842b46e9507372f0b1b963James Dong * motion vector referenced by the input parameter pSrcDstBestMV, i.e., the
480c1bc742181ded4930842b46e9507372f0b1b963James Dong * initial half-pel MV is generated externally.  The function
490c1bc742181ded4930842b46e9507372f0b1b963James Dong * omxVCM4P10_BlockMatch_Half may be used for half-pel motion estimation.
500c1bc742181ded4930842b46e9507372f0b1b963James Dong *
510c1bc742181ded4930842b46e9507372f0b1b963James Dong * Input Arguments:
520c1bc742181ded4930842b46e9507372f0b1b963James Dong *
530c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcOrgY - Pointer to the current position in original picture plane. If
540c1bc742181ded4930842b46e9507372f0b1b963James Dong *            iBlockWidth==4,  4-byte alignment required. If iBlockWidth==8,
550c1bc742181ded4930842b46e9507372f0b1b963James Dong *            8-byte alignment required. If iBlockWidth==16, 16-byte alignment
560c1bc742181ded4930842b46e9507372f0b1b963James Dong *            required.
570c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcRefY - Pointer to the top-left corner of the co-located block in the
580c1bc742181ded4930842b46e9507372f0b1b963James Dong *            reference picture  If iBlockWidth==4,  4-byte alignment
590c1bc742181ded4930842b46e9507372f0b1b963James Dong *            required.  If iBlockWidth==8,  8-byte alignment required.  If
600c1bc742181ded4930842b46e9507372f0b1b963James Dong *            iBlockWidth==16, 16-byte alignment required.
610c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nSrcOrgStep - Stride of the original picture plane in terms of full
620c1bc742181ded4930842b46e9507372f0b1b963James Dong *            pixels; must be a multiple of iBlockWidth.
630c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nSrcRefStep - Stride of the reference picture plane in terms of full
640c1bc742181ded4930842b46e9507372f0b1b963James Dong *            pixels
650c1bc742181ded4930842b46e9507372f0b1b963James Dong *   iBlockWidth - Width of the current block in terms of full pixels; must
660c1bc742181ded4930842b46e9507372f0b1b963James Dong *            be equal to either 4, 8, or 16.
670c1bc742181ded4930842b46e9507372f0b1b963James Dong *   iBlockHeight - Height of the current block in terms of full pixels; must
680c1bc742181ded4930842b46e9507372f0b1b963James Dong *            be equal to either 4, 8, or 16.
690c1bc742181ded4930842b46e9507372f0b1b963James Dong *   nLamda - Lamda factor, used to compute motion cost
700c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pMVPred - Predicted MV, represented in terms of 1/4-pel units; used to
710c1bc742181ded4930842b46e9507372f0b1b963James Dong *            compute motion cost
720c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcDstBestMV - The best MV resulting from a prior half-pel search,
730c1bc742181ded4930842b46e9507372f0b1b963James Dong *            represented in terms of 1/4 pel units
740c1bc742181ded4930842b46e9507372f0b1b963James Dong *
750c1bc742181ded4930842b46e9507372f0b1b963James Dong * Output Arguments:
760c1bc742181ded4930842b46e9507372f0b1b963James Dong *
770c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcDstBestMV - Best MV resulting from the quarter-pel search, expressed
780c1bc742181ded4930842b46e9507372f0b1b963James Dong *            in terms of 1/4-pel units
790c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pBestCost - Motion cost associated with the best MV; computed as
800c1bc742181ded4930842b46e9507372f0b1b963James Dong *            SAD+Lamda*BitsUsedByMV
810c1bc742181ded4930842b46e9507372f0b1b963James Dong *
820c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
830c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_NoErr, if the function runs without error.
840c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_BadArgErr - bad arguments: if one of the following cases occurs:
850c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    One of more of the following pointers is NULL:
860c1bc742181ded4930842b46e9507372f0b1b963James Dong *         pSrcOrgY, pSrcRefY, pSrcDstBestMV, pMVPred, pBestCost
870c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    iBlockWidth or iBlockHeight are equal to values other than 4, 8, or 16.
880c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    Any alignment restrictions are violated
890c1bc742181ded4930842b46e9507372f0b1b963James Dong *
900c1bc742181ded4930842b46e9507372f0b1b963James Dong */
910c1bc742181ded4930842b46e9507372f0b1b963James Dong
920c1bc742181ded4930842b46e9507372f0b1b963James DongOMXResult omxVCM4P10_BlockMatch_Quarter(
930c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8* pSrcOrgY,
940c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 nSrcOrgStep,
950c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8* pSrcRefY,
960c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 nSrcRefStep,
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8 iBlockWidth,
980c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8 iBlockHeight,
990c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U32 nLamda,
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMXVCMotionVector* pMVPred,
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector* pSrcDstBestMV,
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32* pBestCost
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong)
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong{
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Definitions and Initializations*/
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT     candSAD;
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT     fromX, toX, fromY, toY;
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Offset to the reference at the begining of the bounding box */
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMX_U8      *pTempSrcRefY, *pTempSrcOrgY;
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S16     x, y;
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector diffMV, candMV, initialMV;
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8      interpolY[256];
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32     pelPosX, pelPosY;
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Argument error checks */
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth ==  4) && (!armIs4ByteAligned(pSrcOrgY)), OMX_Sts_BadArgErr);
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth ==  8) && (!armIs8ByteAligned(pSrcOrgY)), OMX_Sts_BadArgErr);
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth == 16) && (!armIs16ByteAligned(pSrcOrgY)), OMX_Sts_BadArgErr);
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf((iBlockWidth ==  4) && (!armIs4ByteAligned(pSrcRefY)), OMX_Sts_BadArgErr);
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth ==  8) && (!armIs8ByteAligned(pSrcRefY)), OMX_Sts_BadArgErr);
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlockWidth == 16) && (!armIs16ByteAligned(pSrcRefY)), OMX_Sts_BadArgErr);
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((nSrcOrgStep % iBlockWidth), OMX_Sts_BadArgErr);
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcOrgY == NULL, OMX_Sts_BadArgErr);
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcRefY == NULL, OMX_Sts_BadArgErr);
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pMVPred == NULL, OMX_Sts_BadArgErr);
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcDstBestMV == NULL, OMX_Sts_BadArgErr);
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pBestCost == NULL, OMX_Sts_BadArgErr);
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(((iBlockWidth!=4)&&(iBlockWidth!=8)&&(iBlockWidth!=16)) , OMX_Sts_BadArgErr);
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(((iBlockHeight!=4)&&(iBlockHeight!=8)&&(iBlockHeight!=16)) , OMX_Sts_BadArgErr);
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Check for valid region */
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong    fromX = 1;
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong    toX   = 1;
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong    fromY = 1;
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong    toY   = 1;
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Initialize to max value as a start point */
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong    *pBestCost = 0x7fffffff;
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong    initialMV.dx = pSrcDstBestMV->dx;
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong    initialMV.dy = pSrcDstBestMV->dy;
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Looping on y- axis */
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (y = -fromY; y <= toY; y++)
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Looping on x- axis */
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong        for (x = -fromX; x <= toX; x++)
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Positioning the pointer */
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong            pTempSrcRefY = pSrcRefY + (nSrcRefStep * (initialMV.dy/4)) + (initialMV.dx/4);
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Calculating the fract pel position */
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong            pelPosX = (initialMV.dx % 4) + x;
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (pelPosX < 0)
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong                pTempSrcRefY = pTempSrcRefY - 1;
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong                pelPosX += 4;
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong            pelPosY = (initialMV.dy % 4) + y;
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (pelPosY < 0)
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong                pTempSrcRefY = pTempSrcRefY - (1 * nSrcRefStep);
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong                pelPosY += 4;
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong            pTempSrcOrgY = pSrcOrgY;
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Prepare cand MV */
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong            candMV.dx = initialMV.dx + x;
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong            candMV.dy = initialMV.dy + y;
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Interpolate Quater pel for the current position*/
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong            armVCM4P10_Interpolate_Luma(
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pTempSrcRefY,
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong                        nSrcRefStep,
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong                        interpolY,
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockWidth,
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockWidth,
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockHeight,
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pelPosX,
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pelPosY);
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Calculate the SAD */
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong            armVCCOMM_SAD(
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pTempSrcOrgY,
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong                        nSrcOrgStep,
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong                        interpolY,
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockWidth,
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong                        &candSAD,
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockHeight,
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong                        iBlockWidth);
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong            diffMV.dx = candMV.dx - pMVPred->dx;
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong            diffMV.dy = candMV.dy - pMVPred->dy;
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong
1970c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Result calculations */
1980c1bc742181ded4930842b46e9507372f0b1b963James Dong            armVCM4P10_CompareMotionCostToMV (
1990c1bc742181ded4930842b46e9507372f0b1b963James Dong                        candMV.dx,
2000c1bc742181ded4930842b46e9507372f0b1b963James Dong                        candMV.dy,
2010c1bc742181ded4930842b46e9507372f0b1b963James Dong                        diffMV,
2020c1bc742181ded4930842b46e9507372f0b1b963James Dong                        candSAD,
2030c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pSrcDstBestMV,
2040c1bc742181ded4930842b46e9507372f0b1b963James Dong                        nLamda,
2050c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pBestCost);
2060c1bc742181ded4930842b46e9507372f0b1b963James Dong
2070c1bc742181ded4930842b46e9507372f0b1b963James Dong        } /* End of x- axis */
2080c1bc742181ded4930842b46e9507372f0b1b963James Dong    } /* End of y-axis */
2090c1bc742181ded4930842b46e9507372f0b1b963James Dong
2100c1bc742181ded4930842b46e9507372f0b1b963James Dong    return OMX_Sts_NoErr;
2110c1bc742181ded4930842b46e9507372f0b1b963James Dong
2120c1bc742181ded4930842b46e9507372f0b1b963James Dong}
2130c1bc742181ded4930842b46e9507372f0b1b963James Dong
2140c1bc742181ded4930842b46e9507372f0b1b963James Dong/* End of file */
215