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:  omxVCM4P2_FindMVpred.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 module for predicting MV of MB
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 "armCOMM.h"
370c1bc742181ded4930842b46e9507372f0b1b963James Dong
380c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
390c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function:  omxVCM4P2_FindMVpred   (6.2.3.1.1)
400c1bc742181ded4930842b46e9507372f0b1b963James Dong *
410c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
420c1bc742181ded4930842b46e9507372f0b1b963James Dong * Predicts a motion vector for the current block using the procedure
430c1bc742181ded4930842b46e9507372f0b1b963James Dong * specified in [ISO14496-2], subclause 7.6.5.  The resulting predicted MV is
440c1bc742181ded4930842b46e9507372f0b1b963James Dong * returned in pDstMVPred. If the parameter pDstMVPredME if is not NULL then
450c1bc742181ded4930842b46e9507372f0b1b963James Dong * the set of three MV candidates used for prediction is also returned,
460c1bc742181ded4930842b46e9507372f0b1b963James Dong * otherwise pDstMVPredMEis NULL upon return.
470c1bc742181ded4930842b46e9507372f0b1b963James Dong *
480c1bc742181ded4930842b46e9507372f0b1b963James Dong * Input Arguments:
490c1bc742181ded4930842b46e9507372f0b1b963James Dong *
500c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcMVCurMB - pointer to the MV buffer associated with the current Y
510c1bc742181ded4930842b46e9507372f0b1b963James Dong *            macroblock; a value of NULL indicates unavailability.
520c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcCandMV1 - pointer to the MV buffer containing the 4 MVs associated
530c1bc742181ded4930842b46e9507372f0b1b963James Dong *            with the MB located to the left of the current MB; set to NULL
540c1bc742181ded4930842b46e9507372f0b1b963James Dong *            if there is no MB to the left.
550c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcCandMV2 - pointer to the MV buffer containing the 4 MVs associated
560c1bc742181ded4930842b46e9507372f0b1b963James Dong *            with the MB located above the current MB; set to NULL if there
570c1bc742181ded4930842b46e9507372f0b1b963James Dong *            is no MB located above the current MB.
580c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcCandMV3 - pointer to the MV buffer containing the 4 MVs associated
590c1bc742181ded4930842b46e9507372f0b1b963James Dong *            with the MB located to the right and above the current MB; set
600c1bc742181ded4930842b46e9507372f0b1b963James Dong *            to NULL if there is no MB located to the above-right.
610c1bc742181ded4930842b46e9507372f0b1b963James Dong *   iBlk - the index of block in the current macroblock
620c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pDstMVPredME - MV candidate return buffer;  if set to NULL then
630c1bc742181ded4930842b46e9507372f0b1b963James Dong *            prediction candidate MVs are not returned and pDstMVPredME will
640c1bc742181ded4930842b46e9507372f0b1b963James Dong *            be NULL upon function return; if pDstMVPredME is non-NULL then it
650c1bc742181ded4930842b46e9507372f0b1b963James Dong *            must point to a buffer containing sufficient space for three
660c1bc742181ded4930842b46e9507372f0b1b963James Dong *            return MVs.
670c1bc742181ded4930842b46e9507372f0b1b963James Dong *
680c1bc742181ded4930842b46e9507372f0b1b963James Dong * Output Arguments:
690c1bc742181ded4930842b46e9507372f0b1b963James Dong *
700c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pDstMVPred - pointer to the predicted motion vector
710c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pDstMVPredME - if non-NULL upon input then pDstMVPredME  points upon
720c1bc742181ded4930842b46e9507372f0b1b963James Dong *            return to a buffer containing the three motion vector candidates
730c1bc742181ded4930842b46e9507372f0b1b963James Dong *            used for prediction as specified in [ISO14496-2], subclause
740c1bc742181ded4930842b46e9507372f0b1b963James Dong *            7.6.5, otherwise if NULL upon input then pDstMVPredME is NULL
750c1bc742181ded4930842b46e9507372f0b1b963James Dong *            upon output.
760c1bc742181ded4930842b46e9507372f0b1b963James Dong *
770c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
780c1bc742181ded4930842b46e9507372f0b1b963James Dong *
790c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_NoErr - no error
800c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_BadArgErr - bad arguments; returned under any of the following
810c1bc742181ded4930842b46e9507372f0b1b963James Dong *              conditions:
820c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    the pointer pDstMVPred is NULL
830c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    the parameter iBlk does not fall into the range 0 <= iBlk<=3
840c1bc742181ded4930842b46e9507372f0b1b963James Dong *
850c1bc742181ded4930842b46e9507372f0b1b963James Dong */
860c1bc742181ded4930842b46e9507372f0b1b963James Dong
870c1bc742181ded4930842b46e9507372f0b1b963James DongOMXResult omxVCM4P2_FindMVpred(
880c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXVCMotionVector* pSrcMVCurMB,
890c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXVCMotionVector* pSrcCandMV1,
900c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXVCMotionVector* pSrcCandMV2,
910c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMXVCMotionVector* pSrcCandMV3,
920c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMXVCMotionVector* pDstMVPred,
930c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMXVCMotionVector* pDstMVPredME,
940c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_INT iBlk
950c1bc742181ded4930842b46e9507372f0b1b963James Dong )
960c1bc742181ded4930842b46e9507372f0b1b963James Dong{
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector CandMV;
980c1bc742181ded4930842b46e9507372f0b1b963James Dong	const OMXVCMotionVector *pCandMV1;
990c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMXVCMotionVector *pCandMV2;
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong    const OMXVCMotionVector *pCandMV3;
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Argument error checks */
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf(iBlk!=0 && pSrcMVCurMB == NULL, OMX_Sts_BadArgErr);
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pDstMVPred == NULL, OMX_Sts_BadArgErr);
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((iBlk < 0) || (iBlk > 3), OMX_Sts_BadArgErr);
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong    CandMV.dx = CandMV.dy = 0;
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong	/* Based on the position of the block extract the motion vectors and
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong       the tranperancy status */
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Set the default value for these to be used if pSrcCandMV[1|2|3] == NULL */
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong    pCandMV1 = pCandMV2 = pCandMV3 = &CandMV;
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong    switch (iBlk)
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 0:
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong            if(pSrcCandMV1 != NULL)
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong			    pCandMV1 = &pSrcCandMV1[1];
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong			}
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong			if(pSrcCandMV2 != NULL)
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong				pCandMV2 = &pSrcCandMV2[2];
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong			}
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong			if(pSrcCandMV3 != NULL)
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong				pCandMV3 = &pSrcCandMV3[2];
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong			}
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong			if ((pSrcCandMV1 == NULL) && (pSrcCandMV2 == NULL))
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong                pCandMV1 = pCandMV2 = pCandMV3;
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong            else if((pSrcCandMV1 == NULL) && (pSrcCandMV3 == NULL))
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong                pCandMV1 = pCandMV3 = pCandMV2;
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong            else if((pSrcCandMV2 == NULL) && (pSrcCandMV3 == NULL))
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong                pCandMV2 = pCandMV3 = pCandMV1;
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 1:
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong            pCandMV1 = &pSrcMVCurMB[0];
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong			if(pSrcCandMV2 != NULL)
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong				pCandMV2 = &pSrcCandMV2[3];
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong			}
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong			if(pSrcCandMV3 != NULL)
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong				pCandMV3 = &pSrcCandMV3[2];
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong			}
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong			if((pSrcCandMV2 == NULL) && (pSrcCandMV3 == NULL))
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong                pCandMV2 = pCandMV3 = pCandMV1;
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 2:
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong            if(pSrcCandMV1 != NULL)
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong				pCandMV1 = &pSrcCandMV1[3];
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong			}
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong			pCandMV2 = &pSrcMVCurMB[0];
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong			pCandMV3 = &pSrcMVCurMB[1];
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong			break;
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 3:
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong            pCandMV1 = &pSrcMVCurMB[2];
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong			pCandMV2 = &pSrcMVCurMB[0];
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong			pCandMV3 = &pSrcMVCurMB[1];
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong			break;
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Find the median of the 3 candidate MV's */
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDstMVPred->dx = armMedianOf3 (pCandMV1->dx, pCandMV2->dx, pCandMV3->dx);
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDstMVPred->dy = armMedianOf3 (pCandMV1->dy, pCandMV2->dy, pCandMV3->dy);
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pDstMVPredME != NULL)
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Store the candidate MV's into the pDstMVPredME, these can be used
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong           in the fast algorithm if implemented */
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstMVPredME[0].dx = pCandMV1->dx;
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstMVPredME[0].dy = pCandMV1->dy;
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstMVPredME[1].dx = pCandMV2->dx;
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstMVPredME[1].dy = pCandMV2->dy;
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstMVPredME[2].dx = pCandMV3->dx;
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstMVPredME[2].dy = pCandMV3->dy;
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1970c1bc742181ded4930842b46e9507372f0b1b963James Dong
1980c1bc742181ded4930842b46e9507372f0b1b963James Dong    return OMX_Sts_NoErr;
1990c1bc742181ded4930842b46e9507372f0b1b963James Dong}
2000c1bc742181ded4930842b46e9507372f0b1b963James Dong
2010c1bc742181ded4930842b46e9507372f0b1b963James Dong
2020c1bc742181ded4930842b46e9507372f0b1b963James Dong/* End of file */
2030c1bc742181ded4930842b46e9507372f0b1b963James Dong
204