summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/on2/h264dec/omxdl/reference/vc/m4p10/src/armVCM4P10_CompareMotionCostToMV.c
blob: 6611a37d66d5bf9685c08e15ba6ecc5cf2a9df77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) 2007-2008 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
/**
 * 
 * File Name:  armVCM4P10_CompareMotionCostToMV.c
 * OpenMAX DL: v1.0.2
 * Revision:   9641
 * Date:       Thursday, February 7, 2008
 * 
 * 
 * 
 * 
 * Description:
 * Contains module for comparing motion vectors and SAD's to decide 
 * the best MV and SAD
 *
 */
  
#include "omxtypes.h"
#include "armOMX.h"

#include "armVC.h"
#include "armCOMM.h"

/**
 * Function: armVCM4P10_ExpGolBitsUsed
 *
 * Description:
 * Performs calculating Exp-Golomb code length for a given values
 *
 * Remarks:
 *
 * Parameters:
 * [in]	         val	Signed number for which Exp-Golomb code length has
 *                      to be calculated
 *
 * Return Value: 
 *             Returns the length of the Exp-Golomb code for val
 */

static OMX_U16 armVCM4P10_ExpGolBitsUsed (OMX_S16 val)
{
    OMX_U16 sizeCodeNum, codeNum;
    
    /* Mapping val to codeNum */
    codeNum = armAbs (val);
    if (val > 0)
    {
        codeNum = (2 * codeNum) - 1;
    }
    else
    {
        codeNum = 2 * codeNum;
    }
    
    /* Size of the exp-golomb code */
    sizeCodeNum = (2 * armLogSize (codeNum + 1)) - 1;
    
    return sizeCodeNum;
}
                

/**
 * Function: armVCM4P10_CompareMotionCostToMV
 *
 * Description:
 * Performs comparision of motion vectors and Motion cost to decide the 
 * best MV and best MC
 *
 * Remarks:
 *
 * Parameters:
 * [in]	         mvX	x coordinate of the candidate motion vector in 1/4 pel units
 * [in]	         mvY	y coordinate of the candidate motion vector in 1/4 pel units
 * [in]	      diffMV	differential MV
 * [in]	     candSAD	Candidate SAD
 * [in]	      bestMV	Best MV, contains best MV till the previous interation.
 * [in]       nLamda    Lamda factor; used to compute motion cost 
 * [in]   *pBestCost    Contains the current best motion cost.
 * [out]  *pBestCost    pBestCost Motion cost will be associated with the best MV 
 *                      after judgement; 
 *                      computed as SAD+Lamda*BitsUsedByMV, if the candCost is less 
 *                      than the best cost passed then the *pBestCost will be equal to candCost
 * [out]	  bestMV	Finally will have the best MV after the judgement.
 *
 * Return Value:
 * OMX_INT -- 1 to indicate that the current motion cost is the best 
 *            0 to indicate that it is NOT the best motion cost
 */

OMX_INT armVCM4P10_CompareMotionCostToMV (
    OMX_S16  mvX,
    OMX_S16  mvY,
    OMXVCMotionVector diffMV, 
    OMX_INT candSAD, 
    OMXVCMotionVector *bestMV, 
    OMX_U32 nLamda,
    OMX_S32 *pBestCost
) 
{
    OMX_S32 candCost;
    OMX_U16 sizeCodeNum;
    
    sizeCodeNum = armVCM4P10_ExpGolBitsUsed (diffMV.dx);
    sizeCodeNum += armVCM4P10_ExpGolBitsUsed (diffMV.dy);
    
    /* Motion cost = SAD +  lamda * ((bitsused(diffMVx) + (bitsused(diffMVy))*/
    candCost = candSAD + (nLamda * sizeCodeNum);
        
    /* Calculate candCost */
    if (candCost < *pBestCost)
    {
        *pBestCost = candCost;
        bestMV->dx = mvX;
        bestMV->dy = mvY;
        return 1;
    }
    if (candCost > *pBestCost)
    {
        return 0;
    }
    /* shorter motion vector */
    if ( (mvX * mvX + mvY * mvY) < ((bestMV->dx * bestMV->dx) + (bestMV->dy * bestMV->dy)) )
    {
        *pBestCost = candCost;
        bestMV->dx = mvX;
        bestMV->dy = mvY;
        return 1;
    }
    
    return 0;
}

/*End of File*/