armVCM4P10_CompareMotionCostToMV.c revision 0c1bc742181ded4930842b46e9507372f0b1b963
1/**
2 *
3 * File Name:  armVCM4P10_CompareMotionCostToMV.c
4 * OpenMAX DL: v1.0.2
5 * Revision:   9641
6 * Date:       Thursday, February 7, 2008
7 *
8 * (c) Copyright 2007-2008 ARM Limited. All Rights Reserved.
9 *
10 *
11 *
12 * Description:
13 * Contains module for comparing motion vectors and SAD's to decide
14 * the best MV and SAD
15 *
16 */
17
18#include "omxtypes.h"
19#include "armOMX.h"
20
21#include "armVC.h"
22#include "armCOMM.h"
23
24/**
25 * Function: armVCM4P10_ExpGolBitsUsed
26 *
27 * Description:
28 * Performs calculating Exp-Golomb code length for a given values
29 *
30 * Remarks:
31 *
32 * Parameters:
33 * [in]	         val	Signed number for which Exp-Golomb code length has
34 *                      to be calculated
35 *
36 * Return Value:
37 *             Returns the length of the Exp-Golomb code for val
38 */
39
40static OMX_U16 armVCM4P10_ExpGolBitsUsed (OMX_S16 val)
41{
42    OMX_U16 sizeCodeNum, codeNum;
43
44    /* Mapping val to codeNum */
45    codeNum = armAbs (val);
46    if (val > 0)
47    {
48        codeNum = (2 * codeNum) - 1;
49    }
50    else
51    {
52        codeNum = 2 * codeNum;
53    }
54
55    /* Size of the exp-golomb code */
56    sizeCodeNum = (2 * armLogSize (codeNum + 1)) - 1;
57
58    return sizeCodeNum;
59}
60
61
62/**
63 * Function: armVCM4P10_CompareMotionCostToMV
64 *
65 * Description:
66 * Performs comparision of motion vectors and Motion cost to decide the
67 * best MV and best MC
68 *
69 * Remarks:
70 *
71 * Parameters:
72 * [in]	         mvX	x coordinate of the candidate motion vector in 1/4 pel units
73 * [in]	         mvY	y coordinate of the candidate motion vector in 1/4 pel units
74 * [in]	      diffMV	differential MV
75 * [in]	     candSAD	Candidate SAD
76 * [in]	      bestMV	Best MV, contains best MV till the previous interation.
77 * [in]       nLamda    Lamda factor; used to compute motion cost
78 * [in]   *pBestCost    Contains the current best motion cost.
79 * [out]  *pBestCost    pBestCost Motion cost will be associated with the best MV
80 *                      after judgement;
81 *                      computed as SAD+Lamda*BitsUsedByMV, if the candCost is less
82 *                      than the best cost passed then the *pBestCost will be equal to candCost
83 * [out]	  bestMV	Finally will have the best MV after the judgement.
84 *
85 * Return Value:
86 * OMX_INT -- 1 to indicate that the current motion cost is the best
87 *            0 to indicate that it is NOT the best motion cost
88 */
89
90OMX_INT armVCM4P10_CompareMotionCostToMV (
91    OMX_S16  mvX,
92    OMX_S16  mvY,
93    OMXVCMotionVector diffMV,
94    OMX_INT candSAD,
95    OMXVCMotionVector *bestMV,
96    OMX_U32 nLamda,
97    OMX_S32 *pBestCost
98)
99{
100    OMX_S32 candCost;
101    OMX_U16 sizeCodeNum;
102
103    sizeCodeNum = armVCM4P10_ExpGolBitsUsed (diffMV.dx);
104    sizeCodeNum += armVCM4P10_ExpGolBitsUsed (diffMV.dy);
105
106    /* Motion cost = SAD +  lamda * ((bitsused(diffMVx) + (bitsused(diffMVy))*/
107    candCost = candSAD + (nLamda * sizeCodeNum);
108
109    /* Calculate candCost */
110    if (candCost < *pBestCost)
111    {
112        *pBestCost = candCost;
113        bestMV->dx = mvX;
114        bestMV->dy = mvY;
115        return 1;
116    }
117    if (candCost > *pBestCost)
118    {
119        return 0;
120    }
121    /* shorter motion vector */
122    if ( (mvX * mvX + mvY * mvY) < ((bestMV->dx * bestMV->dx) + (bestMV->dy * bestMV->dy)) )
123    {
124        *pBestCost = candCost;
125        bestMV->dx = mvX;
126        bestMV->dy = mvY;
127        return 1;
128    }
129
130    return 0;
131}
132
133/*End of File*/
134