10c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
20c1bc742181ded4930842b46e9507372f0b1b963James Dong *
30c1bc742181ded4930842b46e9507372f0b1b963James Dong * File Name:  armVCM4P10_CompareMotionCostToMV.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 comparing motion vectors and SAD's to decide
140c1bc742181ded4930842b46e9507372f0b1b963James Dong * the best MV and SAD
150c1bc742181ded4930842b46e9507372f0b1b963James Dong *
160c1bc742181ded4930842b46e9507372f0b1b963James Dong */
170c1bc742181ded4930842b46e9507372f0b1b963James Dong
180c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxtypes.h"
190c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armOMX.h"
200c1bc742181ded4930842b46e9507372f0b1b963James Dong
210c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVC.h"
220c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armCOMM.h"
230c1bc742181ded4930842b46e9507372f0b1b963James Dong
240c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
250c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function: armVCM4P10_ExpGolBitsUsed
260c1bc742181ded4930842b46e9507372f0b1b963James Dong *
270c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
280c1bc742181ded4930842b46e9507372f0b1b963James Dong * Performs calculating Exp-Golomb code length for a given values
290c1bc742181ded4930842b46e9507372f0b1b963James Dong *
300c1bc742181ded4930842b46e9507372f0b1b963James Dong * Remarks:
310c1bc742181ded4930842b46e9507372f0b1b963James Dong *
320c1bc742181ded4930842b46e9507372f0b1b963James Dong * Parameters:
330c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	         val	Signed number for which Exp-Golomb code length has
340c1bc742181ded4930842b46e9507372f0b1b963James Dong *                      to be calculated
350c1bc742181ded4930842b46e9507372f0b1b963James Dong *
360c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
370c1bc742181ded4930842b46e9507372f0b1b963James Dong *             Returns the length of the Exp-Golomb code for val
380c1bc742181ded4930842b46e9507372f0b1b963James Dong */
390c1bc742181ded4930842b46e9507372f0b1b963James Dong
400c1bc742181ded4930842b46e9507372f0b1b963James Dongstatic OMX_U16 armVCM4P10_ExpGolBitsUsed (OMX_S16 val)
410c1bc742181ded4930842b46e9507372f0b1b963James Dong{
420c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U16 sizeCodeNum, codeNum;
430c1bc742181ded4930842b46e9507372f0b1b963James Dong
440c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Mapping val to codeNum */
450c1bc742181ded4930842b46e9507372f0b1b963James Dong    codeNum = armAbs (val);
460c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (val > 0)
470c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
480c1bc742181ded4930842b46e9507372f0b1b963James Dong        codeNum = (2 * codeNum) - 1;
490c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
500c1bc742181ded4930842b46e9507372f0b1b963James Dong    else
510c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
520c1bc742181ded4930842b46e9507372f0b1b963James Dong        codeNum = 2 * codeNum;
530c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
540c1bc742181ded4930842b46e9507372f0b1b963James Dong
550c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Size of the exp-golomb code */
560c1bc742181ded4930842b46e9507372f0b1b963James Dong    sizeCodeNum = (2 * armLogSize (codeNum + 1)) - 1;
570c1bc742181ded4930842b46e9507372f0b1b963James Dong
580c1bc742181ded4930842b46e9507372f0b1b963James Dong    return sizeCodeNum;
590c1bc742181ded4930842b46e9507372f0b1b963James Dong}
600c1bc742181ded4930842b46e9507372f0b1b963James Dong
610c1bc742181ded4930842b46e9507372f0b1b963James Dong
620c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
630c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function: armVCM4P10_CompareMotionCostToMV
640c1bc742181ded4930842b46e9507372f0b1b963James Dong *
650c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
660c1bc742181ded4930842b46e9507372f0b1b963James Dong * Performs comparision of motion vectors and Motion cost to decide the
670c1bc742181ded4930842b46e9507372f0b1b963James Dong * best MV and best MC
680c1bc742181ded4930842b46e9507372f0b1b963James Dong *
690c1bc742181ded4930842b46e9507372f0b1b963James Dong * Remarks:
700c1bc742181ded4930842b46e9507372f0b1b963James Dong *
710c1bc742181ded4930842b46e9507372f0b1b963James Dong * Parameters:
720c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	         mvX	x coordinate of the candidate motion vector in 1/4 pel units
730c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	         mvY	y coordinate of the candidate motion vector in 1/4 pel units
740c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	      diffMV	differential MV
750c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	     candSAD	Candidate SAD
760c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	      bestMV	Best MV, contains best MV till the previous interation.
770c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]       nLamda    Lamda factor; used to compute motion cost
780c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]   *pBestCost    Contains the current best motion cost.
790c1bc742181ded4930842b46e9507372f0b1b963James Dong * [out]  *pBestCost    pBestCost Motion cost will be associated with the best MV
800c1bc742181ded4930842b46e9507372f0b1b963James Dong *                      after judgement;
810c1bc742181ded4930842b46e9507372f0b1b963James Dong *                      computed as SAD+Lamda*BitsUsedByMV, if the candCost is less
820c1bc742181ded4930842b46e9507372f0b1b963James Dong *                      than the best cost passed then the *pBestCost will be equal to candCost
830c1bc742181ded4930842b46e9507372f0b1b963James Dong * [out]	  bestMV	Finally will have the best MV after the judgement.
840c1bc742181ded4930842b46e9507372f0b1b963James Dong *
850c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
860c1bc742181ded4930842b46e9507372f0b1b963James Dong * OMX_INT -- 1 to indicate that the current motion cost is the best
870c1bc742181ded4930842b46e9507372f0b1b963James Dong *            0 to indicate that it is NOT the best motion cost
880c1bc742181ded4930842b46e9507372f0b1b963James Dong */
890c1bc742181ded4930842b46e9507372f0b1b963James Dong
900c1bc742181ded4930842b46e9507372f0b1b963James DongOMX_INT armVCM4P10_CompareMotionCostToMV (
910c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S16  mvX,
920c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S16  mvY,
930c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector diffMV,
940c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT candSAD,
950c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMXVCMotionVector *bestMV,
960c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U32 nLamda,
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 *pBestCost
980c1bc742181ded4930842b46e9507372f0b1b963James Dong)
990c1bc742181ded4930842b46e9507372f0b1b963James Dong{
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32 candCost;
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U16 sizeCodeNum;
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong    sizeCodeNum = armVCM4P10_ExpGolBitsUsed (diffMV.dx);
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong    sizeCodeNum += armVCM4P10_ExpGolBitsUsed (diffMV.dy);
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Motion cost = SAD +  lamda * ((bitsused(diffMVx) + (bitsused(diffMVy))*/
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong    candCost = candSAD + (nLamda * sizeCodeNum);
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Calculate candCost */
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (candCost < *pBestCost)
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pBestCost = candCost;
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong        bestMV->dx = mvX;
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong        bestMV->dy = mvY;
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong        return 1;
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (candCost > *pBestCost)
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong        return 0;
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* shorter motion vector */
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ( (mvX * mvX + mvY * mvY) < ((bestMV->dx * bestMV->dx) + (bestMV->dy * bestMV->dy)) )
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pBestCost = candCost;
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong        bestMV->dx = mvX;
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong        bestMV->dy = mvY;
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong        return 1;
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong    return 0;
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong/*End of File*/
134