1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Motion Estimation shared functions - |
5 |
|
* |
6 |
|
* Copyright(C) 2002 Christoph Lampert <gruel@web.de> |
7 |
|
* 2002 Michael Militzer <michael@xvid.org> |
8 |
|
* 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
9 |
|
* |
10 |
|
* This program is free software ; you can redistribute it and/or modify |
11 |
|
* it under the terms of the GNU General Public License as published by |
12 |
|
* the Free Software Foundation ; either version 2 of the License, or |
13 |
|
* (at your option) any later version. |
14 |
|
* |
15 |
|
* This program is distributed in the hope that it will be useful, |
16 |
|
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
17 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
* GNU General Public License for more details. |
19 |
|
* |
20 |
|
* You should have received a copy of the GNU General Public License |
21 |
|
* along with this program ; if not, write to the Free Software |
22 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
|
* |
24 |
|
* $Id$ |
25 |
|
* |
26 |
|
****************************************************************************/ |
27 |
|
|
28 |
|
#ifndef _MOTION_INLINES_ |
29 |
|
#define _MOTION_INLINES_ |
30 |
|
|
31 |
|
#include <stdlib.h> |
32 |
|
|
33 |
|
/* |
34 |
|
* Calculate the min/max range |
35 |
|
* relative to the _MACROBLOCK_ position |
36 |
|
*/ |
37 |
|
static void __inline |
38 |
|
get_range(int32_t * const min_dx, |
39 |
|
int32_t * const max_dx, |
40 |
|
int32_t * const min_dy, |
41 |
|
int32_t * const max_dy, |
42 |
|
const uint32_t x, |
43 |
|
const uint32_t y, |
44 |
|
uint32_t block_sz, /* block dimension, 3(8) or 4(16) */ |
45 |
|
const uint32_t width, |
46 |
|
const uint32_t height, |
47 |
|
const uint32_t fcode, |
48 |
|
const int precision, /* 2 for qpel, 1 for halfpel */ |
49 |
|
const int rrv) |
50 |
|
{ |
51 |
|
int k; |
52 |
|
const int search_range = 16 << fcode; |
53 |
|
int high = search_range - 1; |
54 |
|
int low = -search_range; |
55 |
|
|
56 |
|
if (rrv) { |
57 |
|
high = RRV_MV_SCALEUP(high); |
58 |
|
low = RRV_MV_SCALEUP(low); |
59 |
|
block_sz++; |
60 |
|
} |
61 |
|
|
62 |
|
k = (int)(width - (x<<block_sz))<<precision; |
63 |
|
*max_dx = MIN(high, k); |
64 |
|
k = (int)(height - (y<<block_sz))<<precision; |
65 |
|
*max_dy = MIN(high, k); |
66 |
|
|
67 |
|
k = (-(int)((x+1)<<block_sz))<<precision; |
68 |
|
*min_dx = MAX(low, k); |
69 |
|
k = (-(int)((y+1)<<block_sz))<<precision; |
70 |
|
*min_dy = MAX(low, k); |
71 |
|
} |
72 |
|
|
73 |
|
/* reversed mv.length table */ |
74 |
|
static const int r_mvtab[64] = { |
75 |
|
12, 12, 12, 12, 12, 12, 12, 12, |
76 |
|
12, 12, 12, 12, 12, 12, 12, 12, |
77 |
|
12, 12, 12, 12, 12, 12, 12, 12, |
78 |
|
12, 12, 12, 12, 12, 12, 12, 12, |
79 |
|
12, 11, 11, 11, 11, 11, 11, 10, |
80 |
|
10, 10, 10, 10, 10, 10, 10, 10, |
81 |
|
10, 10, 10, 10, 10, 9, 9, 9, |
82 |
|
7, 7, 7, 6, 4, 3, 2, 1 |
83 |
|
}; |
84 |
|
|
85 |
|
static __inline uint32_t |
86 |
|
d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv) |
87 |
|
{ |
88 |
|
unsigned int bits; |
89 |
|
|
90 |
|
x <<= qpel; |
91 |
|
y <<= qpel; |
92 |
|
if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
93 |
|
|
94 |
|
x -= pred.x; |
95 |
|
bits = (x != 0 ? iFcode:0); |
96 |
|
x = -abs(x); |
97 |
|
x >>= (iFcode - 1); |
98 |
|
bits += r_mvtab[x+63]; |
99 |
|
|
100 |
|
y -= pred.y; |
101 |
|
bits += (y != 0 ? iFcode:0); |
102 |
|
y = -abs(y); |
103 |
|
y >>= (iFcode - 1); |
104 |
|
bits += r_mvtab[y+63]; |
105 |
|
|
106 |
|
return bits; |
107 |
|
} |
108 |
|
|
109 |
|
static __inline const uint8_t * |
110 |
|
GetReference(const int x, const int y, const SearchData * const data) |
111 |
|
{ |
112 |
|
const int picture = ((x&1)<<1) | (y&1); |
113 |
|
const int offset = (x>>1) + (y>>1)*data->iEdgedWidth; |
114 |
|
return data->RefP[picture] + offset; |
115 |
|
} |
116 |
|
|
117 |
|
static __inline const uint8_t * |
118 |
|
GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data) |
119 |
|
{ |
120 |
|
/* dir : 0 = forward, 1 = backward */ |
121 |
|
const uint8_t *const *const direction = ( dir == 0 ? data->RefP : data->b_RefP ); |
122 |
|
const int picture = ((x&1)<<1) | (y&1); |
123 |
|
const int offset = (x>>1) + (y>>1)*data->iEdgedWidth; |
124 |
|
return direction[picture] + offset; |
125 |
|
} |
126 |
|
|
127 |
|
static __inline void |
128 |
|
ZeroMacroblockP(MACROBLOCK *pMB, const int32_t sad) |
129 |
|
{ |
130 |
|
pMB->mode = MODE_INTER; |
131 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = zeroMV; |
132 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = zeroMV; |
133 |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
134 |
|
pMB->mcsel = 0; |
135 |
|
pMB->cbp = 0; |
136 |
|
} |
137 |
|
|
138 |
|
/* check if given vector is equal to any vector checked before */ |
139 |
|
static __inline int |
140 |
|
vector_repeats(const VECTOR * const pmv, const unsigned int i) |
141 |
|
{ |
142 |
|
unsigned int j; |
143 |
|
for (j = 0; j < i; j++) |
144 |
|
if (MVequal(pmv[i], pmv[j])) return 1; /* same vector has been checked already */ |
145 |
|
return 0; |
146 |
|
} |
147 |
|
|
148 |
|
/* make a binary mask that prevents diamonds/squares |
149 |
|
from checking a vector which has been checked as a prediction */ |
150 |
|
static __inline int |
151 |
|
make_mask(const VECTOR * const pmv, const unsigned int i, const unsigned int current) |
152 |
|
{ |
153 |
|
unsigned int mask = 255, j; |
154 |
|
for (j = 0; j < i; j++) { |
155 |
|
if (pmv[current].x == pmv[j].x) { |
156 |
|
if (pmv[current].y == pmv[j].y + iDiamondSize) mask &= ~4; |
157 |
|
else if (pmv[current].y == pmv[j].y - iDiamondSize) mask &= ~8; |
158 |
|
} else |
159 |
|
if (pmv[current].y == pmv[j].y) { |
160 |
|
if (pmv[current].x == pmv[j].x + iDiamondSize) mask &= ~1; |
161 |
|
else if (pmv[current].x == pmv[j].x - iDiamondSize) mask &= ~2; |
162 |
|
} |
163 |
|
} |
164 |
|
return mask; |
165 |
|
} |
166 |
|
|
167 |
|
#endif /* _MOTION_INLINES_ */ |