[cvs] / xvidcore / src / motion / motion_est.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/motion/motion_est.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.44.2.14 - (view) (download)

1 : Isibaar 1.1 /**************************************************************************
2 :     *
3 : chl 1.26 * XVID MPEG-4 VIDEO CODEC
4 :     * motion estimation
5 :     *
6 :     * This program is an implementation of a part of one or more MPEG-4
7 :     * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
8 :     * to use this software module in hardware or software products are
9 :     * advised that its use may infringe existing patents or copyrights, and
10 :     * any such use would be at such party's own risk. The original
11 :     * developer of this software module and his/her company, and subsequent
12 :     * editors and their companies, will have no liability for use of this
13 :     * software or modifications or derivatives thereof.
14 :     *
15 :     * This program is free software; you can redistribute it and/or modify
16 :     * it under the terms of the GNU General Public License as published by
17 :     * the Free Software Foundation; either version 2 of the License, or
18 :     * (at your option) any later version.
19 :     *
20 :     * This program is distributed in the hope that it will be useful,
21 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 :     * GNU General Public License for more details.
24 :     *
25 :     * You should have received a copy of the GNU General Public License
26 :     * along with this program; if not, write to the Free Software
27 :     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 :     *
29 :     *************************************************************************/
30 :    
31 : Isibaar 1.1 #include <assert.h>
32 :     #include <stdio.h>
33 : chl 1.3 #include <stdlib.h>
34 : Isibaar 1.1
35 :     #include "../encoder.h"
36 :     #include "../utils/mbfunctions.h"
37 :     #include "../prediction/mbprediction.h"
38 :     #include "../global.h"
39 :     #include "../utils/timer.h"
40 : Isibaar 1.44.2.7 #include "../image/interpolate8x8.h"
41 : chl 1.44.2.1 #include "motion_est.h"
42 : suxen_drol 1.6 #include "motion.h"
43 : Isibaar 1.1 #include "sad.h"
44 : Isibaar 1.44.2.2 #include "../utils/emms.h"
45 : Isibaar 1.1
46 : chl 1.44.2.1 #define INITIAL_SKIP_THRESH (10)
47 :     #define FINAL_SKIP_THRESH (50)
48 :     #define MAX_SAD00_FOR_SKIP (20)
49 : Isibaar 1.44.2.2 #define MAX_CHROMA_SAD_FOR_SKIP (22)
50 :     #define SKIP_THRESH_B (25)
51 : chl 1.44.2.1
52 :     #define CHECK_CANDIDATE(X,Y,D) { \
53 :     (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }
54 : edgomez 1.20
55 : Isibaar 1.44.2.7 #define GET_REFERENCE(X, Y, REF) { \
56 : chl 1.44.2.9 switch ( (((X)&1)<<1) + ((Y)&1) ) \
57 : Isibaar 1.44.2.7 { \
58 : syskin 1.44.2.11 case 0 : REF = (uint8_t *)data->Ref + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \
59 :     case 1 : REF = (uint8_t *)data->RefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \
60 :     case 2 : REF = (uint8_t *)data->RefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \
61 :     default : REF = (uint8_t *)data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \
62 : Isibaar 1.44.2.7 } \
63 :     }
64 :    
65 : chl 1.44.2.1 #define iDiamondSize 2
66 : edgomez 1.20
67 : chl 1.44.2.1 static __inline int
68 :     d_mv_bits(int x, int y, const uint32_t iFcode)
69 :     {
70 :     int xb, yb;
71 :    
72 :     if (x == 0) xb = 1;
73 :     else {
74 :     if (x < 0) x = -x;
75 :     x += (1 << (iFcode - 1)) - 1;
76 :     x >>= (iFcode - 1);
77 :     if (x > 32) x = 32;
78 :     xb = mvtab[x] + iFcode;
79 :     }
80 :    
81 :     if (y == 0) yb = 1;
82 :     else {
83 :     if (y < 0) y = -y;
84 :     y += (1 << (iFcode - 1)) - 1;
85 :     y >>= (iFcode - 1);
86 :     if (y > 32) y = 32;
87 :     yb = mvtab[y] + iFcode;
88 :     }
89 :     return xb + yb;
90 :     }
91 :    
92 : syskin 1.44.2.14 static int32_t
93 :     ChromaSAD(int dx, int dy, const SearchData * const data)
94 : chl 1.44.2.1 {
95 : syskin 1.44.2.14 int sad;
96 :     dx = (dx >> 1) + roundtab_79[dx & 0x3];
97 :     dy = (dy >> 1) + roundtab_79[dy & 0x3];
98 :    
99 :     switch (((dx & 1) << 1) + (dy & 1)) { // ((dx%2)?2:0)+((dy%2)?1:0)
100 :     case 0:
101 :     sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);
102 :     sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);
103 :     break;
104 :     case 1:
105 :     dx = dx / 2; dy = (dy - 1) / 2;
106 :     sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);
107 :     sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);
108 :     break;
109 :     case 2:
110 :     dx = (dx - 1) / 2; dy = dy / 2;
111 :     sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);
112 :     sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);
113 :     break;
114 :     default:
115 :     dx = (dx - 1) / 2; dy = (dy - 1) / 2;
116 :     interpolate8x8_halfpel_hv(data->RefQ,
117 :     data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,
118 :     data->rounding);
119 :     sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2);
120 :     interpolate8x8_halfpel_hv(data->RefQ,
121 :     data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,
122 :     data->rounding);
123 :     sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2);
124 :     break;
125 : chl 1.44.2.1 }
126 : syskin 1.44.2.14 return sad;
127 :     }
128 : chl 1.10
129 :    
130 : syskin 1.44.2.14 /* CHECK_CANDIATE FUNCTIONS START */
131 : Isibaar 1.1
132 :    
133 : chl 1.44.2.1 static void
134 : syskin 1.44.2.14 CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
135 : Isibaar 1.44.2.12 {
136 :     int t;
137 :     const uint8_t * Reference;
138 :    
139 :     if (( x > data->max_dx) || ( x < data->min_dx)
140 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
141 :    
142 :     switch ( ((x&1)<<1) + (y&1) ) {
143 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
144 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
145 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
146 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
147 :     }
148 :    
149 :     data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);
150 :    
151 : syskin 1.44.2.14 if (data->qpel) t = d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode);
152 :     else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
153 :    
154 :     data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;
155 :     data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;
156 :    
157 :     if (data->chroma) data->temp[0] += ChromaSAD(x, y, data);
158 : Isibaar 1.44.2.12
159 :     if (data->temp[0] < data->iMinSAD[0]) {
160 :     data->iMinSAD[0] = data->temp[0];
161 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
162 :     *dir = Direction; }
163 :    
164 :     if (data->temp[1] < data->iMinSAD[1]) {
165 :     data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
166 :     if (data->temp[2] < data->iMinSAD[2]) {
167 :     data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
168 :     if (data->temp[3] < data->iMinSAD[3]) {
169 :     data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
170 :     if (data->temp[4] < data->iMinSAD[4]) {
171 :     data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
172 :    
173 :     }
174 :    
175 :     static void
176 : chl 1.44.2.1 CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
177 : Isibaar 1.1 {
178 : chl 1.44.2.1 int32_t sad;
179 :     const uint8_t * Reference;
180 : Isibaar 1.1
181 : chl 1.44.2.1 if (( x > data->max_dx) || ( x < data->min_dx)
182 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
183 : Isibaar 1.1
184 : chl 1.44.2.1 switch ( ((x&1)<<1) + (y&1) )
185 :     {
186 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
187 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
188 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
189 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
190 : edgomez 1.20 }
191 : Isibaar 1.1
192 : syskin 1.44.2.14 sad = sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR);
193 :     sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000;
194 : Isibaar 1.1
195 : chl 1.44.2.1 if (sad < *(data->iMinSAD)) {
196 :     *(data->iMinSAD) = sad;
197 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
198 :     *dir = Direction; }
199 : Isibaar 1.1 }
200 :    
201 : chl 1.44.2.1 static void
202 : Isibaar 1.44.2.7 CheckCandidate16_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
203 :    
204 :     // CheckCandidate16 variant which expects x and y in quarter pixel resolution
205 :     // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)
206 :     // around currentMV!
207 :     {
208 :     int t;
209 : syskin 1.44.2.11 uint8_t * Reference = (uint8_t *)data->RefQ;
210 : Isibaar 1.44.2.7 const uint8_t *ref1, *ref2, *ref3, *ref4;
211 :     VECTOR halfpelMV = *(data->currentMV);
212 :    
213 :     int32_t iEdgedWidth = data->iEdgedWidth;
214 :     uint32_t rounding = data->rounding;
215 :    
216 :     if (( x > data->max_dx) || ( x < data->min_dx)
217 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
218 :    
219 : Isibaar 1.44.2.12 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases
220 : Isibaar 1.44.2.7 switch( ((x&1)<<1) + (y&1) )
221 :     {
222 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
223 : syskin 1.44.2.11 GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);
224 : Isibaar 1.44.2.7 break;
225 :    
226 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
227 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
228 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
229 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);
230 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);
231 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);
232 :     break;
233 :    
234 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
235 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);
236 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
237 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);
238 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);
239 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);
240 :     break;
241 :    
242 :     default: // x and y in qpel resolution - the "corners" (top left/right and
243 :     // bottom left/right) during qpel refinement
244 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
245 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);
246 :     GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);
247 :    
248 :     interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
249 :     interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
250 :     interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
251 :     interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
252 :     break;
253 :     }
254 :    
255 : syskin 1.44.2.8 data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1);
256 : Isibaar 1.44.2.7
257 :     t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode);
258 : syskin 1.44.2.14 data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;
259 :     data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;
260 :    
261 :     if (data->chroma)
262 :     data->temp[0] += ChromaSAD(x/2, y/2, data);
263 : Isibaar 1.44.2.7
264 :     if (data->temp[0] < data->iMinSAD[0]) {
265 :     data->iMinSAD[0] = data->temp[0];
266 :     data->currentQMV[0].x = x; data->currentQMV[0].y = y;
267 : syskin 1.44.2.8 /* *dir = Direction;*/ }
268 : Isibaar 1.44.2.7
269 :     if (data->temp[1] < data->iMinSAD[1]) {
270 :     data->iMinSAD[1] = data->temp[1]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; }
271 :     if (data->temp[2] < data->iMinSAD[2]) {
272 :     data->iMinSAD[2] = data->temp[2]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; }
273 :     if (data->temp[3] < data->iMinSAD[3]) {
274 :     data->iMinSAD[3] = data->temp[3]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; }
275 :     if (data->temp[4] < data->iMinSAD[4]) {
276 :     data->iMinSAD[4] = data->temp[4]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; }
277 :     }
278 :    
279 :     static void
280 : Isibaar 1.44.2.2 CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
281 :     {
282 :     int32_t sad;
283 :    
284 :     if (( x > data->max_dx) || ( x < data->min_dx)
285 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
286 :    
287 : syskin 1.44.2.11 sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth),
288 : Isibaar 1.44.2.2 data->iEdgedWidth, 256*4096);
289 :    
290 :     if (sad < *(data->iMinSAD)) {
291 :     *(data->iMinSAD) = sad;
292 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
293 :     *dir = Direction; }
294 :     }
295 :    
296 :    
297 :     static void
298 : chl 1.44.2.1 CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)
299 : Isibaar 1.1 {
300 : chl 1.44.2.1 int32_t sad;
301 :     const int xb = data->currentMV[1].x;
302 :     const int yb = data->currentMV[1].y;
303 :     const uint8_t *ReferenceF, *ReferenceB;
304 : suxen_drol 1.8
305 : chl 1.44.2.1 if (( xf > data->max_dx) || ( xf < data->min_dx)
306 :     || ( yf > data->max_dy) || (yf < data->min_dy)) return;
307 : suxen_drol 1.8
308 : chl 1.44.2.1 switch ( ((xf&1)<<1) + (yf&1) ) {
309 :     case 0 : ReferenceF = data->Ref + xf/2 + (yf/2)*(data->iEdgedWidth); break;
310 :     case 1 : ReferenceF = data->RefV + xf/2 + ((yf-1)/2)*(data->iEdgedWidth); break;
311 :     case 2 : ReferenceF = data->RefH + (xf-1)/2 + (yf/2)*(data->iEdgedWidth); break;
312 :     default : ReferenceF = data->RefHV + (xf-1)/2 + ((yf-1)/2)*(data->iEdgedWidth); break;
313 :     }
314 : chl 1.19
315 : chl 1.44.2.1 switch ( ((xb&1)<<1) + (yb&1) ) {
316 :     case 0 : ReferenceB = data->bRef + xb/2 + (yb/2)*(data->iEdgedWidth); break;
317 :     case 1 : ReferenceB = data->bRefV + xb/2 + ((yb-1)/2)*(data->iEdgedWidth); break;
318 :     case 2 : ReferenceB = data->bRefH + (xb-1)/2 + (yb/2)*(data->iEdgedWidth); break;
319 :     default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break;
320 :     }
321 : Isibaar 1.1
322 : syskin 1.44.2.14 sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
323 : chl 1.19
324 : syskin 1.44.2.14 sad += (data->lambda16 *
325 :     ( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) +
326 :     d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000;
327 : edgomez 1.20
328 : chl 1.44.2.1 if (sad < *(data->iMinSAD)) {
329 :     *(data->iMinSAD) = sad;
330 :     data->currentMV->x = xf; data->currentMV->y = yf;
331 :     *dir = Direction; }
332 :     }
333 : edgomez 1.20
334 : chl 1.44.2.1 static void
335 :     CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
336 :     {
337 : syskin 1.44.2.14 int32_t sad = 0;
338 : chl 1.44.2.1 int k;
339 :     const uint8_t *ReferenceF;
340 :     const uint8_t *ReferenceB;
341 :     VECTOR mvs, b_mvs;
342 : edgomez 1.20
343 : chl 1.44.2.1 if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
344 : edgomez 1.20
345 : chl 1.44.2.1 for (k = 0; k < 4; k++) {
346 :     mvs.x = data->directmvF[k].x + x;
347 :     b_mvs.x = ((x == 0) ?
348 :     data->directmvB[k].x
349 :     : mvs.x - data->referencemv[k].x);
350 : edgomez 1.20
351 : chl 1.44.2.1 mvs.y = data->directmvF[k].y + y;
352 :     b_mvs.y = ((y == 0) ?
353 :     data->directmvB[k].y
354 :     : mvs.y - data->referencemv[k].y);
355 :    
356 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
357 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
358 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
359 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
360 :    
361 :     switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {
362 :     case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;
363 :     case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
364 :     case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;
365 :     default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
366 :     }
367 :    
368 :     switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {
369 :     case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
370 :     case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
371 :     case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
372 :     default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
373 :     }
374 :    
375 :     sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
376 :     ReferenceF + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
377 :     ReferenceB + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
378 :     data->iEdgedWidth);
379 :     if (sad > *(data->iMinSAD)) return;
380 :     }
381 : chl 1.17
382 : syskin 1.44.2.14 sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
383 :    
384 : chl 1.44.2.1 if (sad < *(data->iMinSAD)) {
385 :     *(data->iMinSAD) = sad;
386 :     data->currentMV->x = x; data->currentMV->y = y;
387 :     *dir = Direction; }
388 : Isibaar 1.1 }
389 :    
390 : chl 1.44.2.1 static void
391 :     CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
392 :     {
393 :     int32_t sad;
394 :     const uint8_t *ReferenceF;
395 :     const uint8_t *ReferenceB;
396 :     VECTOR mvs, b_mvs;
397 : chl 1.35
398 : syskin 1.44.2.6 if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
399 : Isibaar 1.1
400 : chl 1.44.2.1 mvs.x = data->directmvF[0].x + x;
401 :     b_mvs.x = ((x == 0) ?
402 :     data->directmvB[0].x
403 :     : mvs.x - data->referencemv[0].x);
404 :    
405 :     mvs.y = data->directmvF[0].y + y;
406 :     b_mvs.y = ((y == 0) ?
407 :     data->directmvB[0].y
408 :     : mvs.y - data->referencemv[0].y);
409 :    
410 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
411 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
412 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
413 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
414 :    
415 :     switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {
416 :     case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;
417 :     case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
418 :     case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;
419 :     default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
420 :     }
421 :    
422 :     switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {
423 :     case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
424 :     case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
425 :     case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
426 :     default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
427 :     }
428 :    
429 : syskin 1.44.2.14 sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
430 :     sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
431 : Isibaar 1.1
432 : chl 1.44.2.1 if (sad < *(data->iMinSAD)) {
433 :     *(data->iMinSAD) = sad;
434 :     data->currentMV->x = x; data->currentMV->y = y;
435 :     *dir = Direction; }
436 :     }
437 : Isibaar 1.1
438 : chl 1.44.2.1 static void
439 :     CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
440 :     {
441 : syskin 1.44.2.14 int32_t sad; int t;
442 : chl 1.44.2.1 const uint8_t * Reference;
443 : Isibaar 1.1
444 : chl 1.44.2.1 if (( x > data->max_dx) || ( x < data->min_dx)
445 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
446 : Isibaar 1.1
447 : chl 1.44.2.1 switch ( ((x&1)<<1) + (y&1) )
448 :     {
449 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
450 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
451 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
452 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
453 : edgomez 1.2 }
454 : Isibaar 1.1
455 : chl 1.44.2.1 sad = sad8(data->Cur, Reference, data->iEdgedWidth);
456 : syskin 1.44.2.14 if (data->qpel) t = d_mv_bits(2 * x - data->predQMV.x, 2 * y - data->predQMV.y, data->iFcode);
457 :     else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
458 : Isibaar 1.44.2.12
459 : syskin 1.44.2.14 sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100;
460 : Isibaar 1.44.2.12
461 :     if (sad < *(data->iMinSAD)) {
462 :     *(data->iMinSAD) = sad;
463 :     data->currentMV->x = x; data->currentMV->y = y;
464 :     *dir = Direction; }
465 :     }
466 :    
467 :     static void
468 : Isibaar 1.44.2.7 CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
469 :     // CheckCandidate16no4v variant which expects x and y in quarter pixel resolution
470 :     // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)
471 :     // around currentMV!
472 :    
473 :     {
474 :     int32_t sad;
475 :     uint8_t *Reference = (uint8_t *) data->RefQ;
476 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
477 :     VECTOR halfpelMV = *(data->currentMV);
478 :    
479 :     int32_t iEdgedWidth = data->iEdgedWidth;
480 :     uint32_t rounding = data->rounding;
481 :    
482 :     if (( x > data->max_dx) || ( x < data->min_dx)
483 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
484 :    
485 : Isibaar 1.44.2.12 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
486 : Isibaar 1.44.2.7 switch( ((x&1)<<1) + (y&1) )
487 :     {
488 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
489 : syskin 1.44.2.11 GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);
490 : Isibaar 1.44.2.7 break;
491 :    
492 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
493 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
494 :    
495 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
496 :     break;
497 :    
498 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
499 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);
500 :    
501 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
502 :     break;
503 :    
504 :     default: // x and y in qpel resolution - the "corners" (top left/right and
505 :     // bottom left/right) during qpel refinement
506 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
507 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);
508 :     GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);
509 :    
510 :     interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
511 :     break;
512 :     }
513 :    
514 :     sad = sad8(data->Cur, Reference, data->iEdgedWidth);
515 : syskin 1.44.2.14 sad += (data->lambda8 * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode) * (sad+NEIGH_8X8_BIAS))/100;
516 : Isibaar 1.44.2.7
517 :     if (sad < *(data->iMinSAD)) {
518 :     *(data->iMinSAD) = sad;
519 :     data->currentQMV->x = x; data->currentQMV->y = y;
520 :     *dir = Direction; }
521 :     }
522 :    
523 : Isibaar 1.44.2.2 /* CHECK_CANDIATE FUNCTIONS END */
524 : chl 1.3
525 : chl 1.44.2.1 /* MAINSEARCH FUNCTIONS START */
526 : chl 1.18
527 : chl 1.44.2.1 static void
528 :     AdvDiamondSearch(int x, int y, const SearchData * const data, int bDirection)
529 :     {
530 : chl 1.18
531 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
532 :    
533 : chl 1.44.2.1 int iDirection;
534 : edgomez 1.20
535 :     do {
536 : chl 1.18 iDirection = 0;
537 : chl 1.44.2.1 if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
538 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
539 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);
540 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
541 : chl 1.18
542 :     /* now we're doing diagonal checks near our candidate */
543 :    
544 : chl 1.44.2.1 if (iDirection) { //checking if anything found
545 : chl 1.18 bDirection = iDirection;
546 :     iDirection = 0;
547 : chl 1.44.2.1 x = data->currentMV->x; y = data->currentMV->y;
548 :     if (bDirection & 3) { //our candidate is left or right
549 :     CHECK_CANDIDATE(x, y + iDiamondSize, 8);
550 :     CHECK_CANDIDATE(x, y - iDiamondSize, 4);
551 :     } else { // what remains here is up or down
552 :     CHECK_CANDIDATE(x + iDiamondSize, y, 2);
553 :     CHECK_CANDIDATE(x - iDiamondSize, y, 1); }
554 : chl 1.18
555 : edgomez 1.20 if (iDirection) {
556 :     bDirection += iDirection;
557 : chl 1.44.2.1 x = data->currentMV->x; y = data->currentMV->y; }
558 :     } else { //about to quit, eh? not so fast....
559 : edgomez 1.20 switch (bDirection) {
560 : chl 1.18 case 2:
561 : chl 1.44.2.1 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
562 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
563 : chl 1.18 break;
564 :     case 1:
565 : chl 1.44.2.1 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
566 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
567 : edgomez 1.20 break;
568 :     case 2 + 4:
569 : chl 1.44.2.1 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
570 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
571 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
572 : chl 1.18 break;
573 :     case 4:
574 : chl 1.44.2.1 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
575 :     CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
576 : chl 1.18 break;
577 :     case 8:
578 : chl 1.44.2.1 CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
579 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
580 : edgomez 1.20 break;
581 :     case 1 + 4:
582 : chl 1.44.2.1 CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
583 :     CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
584 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
585 : edgomez 1.20 break;
586 :     case 2 + 8:
587 : chl 1.44.2.1 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
588 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
589 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
590 : edgomez 1.20 break;
591 :     case 1 + 8:
592 : chl 1.44.2.1 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
593 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
594 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
595 : edgomez 1.20 break;
596 :     default: //1+2+4+8 == we didn't find anything at all
597 : chl 1.44.2.1 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
598 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
599 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
600 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
601 : chl 1.18 break;
602 :     }
603 : chl 1.44.2.1 if (!iDirection) break; //ok, the end. really
604 :     bDirection = iDirection;
605 :     x = data->currentMV->x; y = data->currentMV->y;
606 : chl 1.18 }
607 :     }
608 : edgomez 1.20 while (1); //forever
609 : chl 1.18 }
610 :    
611 : chl 1.44.2.1 static void
612 :     SquareSearch(int x, int y, const SearchData * const data, int bDirection)
613 :     {
614 :     int iDirection;
615 : chl 1.35
616 : chl 1.44.2.1 do {
617 :     iDirection = 0;
618 :     if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1+16+64);
619 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2+32+128);
620 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4+16+32);
621 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8+64+128);
622 :     if (bDirection & 16) CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1+4+16+32+64);
623 :     if (bDirection & 32) CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2+4+16+32+128);
624 :     if (bDirection & 64) CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1+8+16+64+128);
625 :     if (bDirection & 128) CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2+8+32+64+128);
626 :    
627 :     bDirection = iDirection;
628 :     x = data->currentMV->x; y = data->currentMV->y;
629 :     } while (iDirection);
630 : chl 1.35 }
631 :    
632 : chl 1.44.2.1 static void
633 :     DiamondSearch(int x, int y, const SearchData * const data, int bDirection)
634 : chl 1.18 {
635 :    
636 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
637 :    
638 : chl 1.44.2.1 int iDirection;
639 : edgomez 1.20
640 :     do {
641 : chl 1.18 iDirection = 0;
642 : chl 1.44.2.1 if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
643 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
644 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);
645 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
646 : chl 1.18
647 :     /* now we're doing diagonal checks near our candidate */
648 :    
649 : chl 1.44.2.1 if (iDirection) { //checking if anything found
650 : chl 1.18 bDirection = iDirection;
651 :     iDirection = 0;
652 : chl 1.44.2.1 x = data->currentMV->x; y = data->currentMV->y;
653 :     if (bDirection & 3) { //our candidate is left or right
654 :     CHECK_CANDIDATE(x, y + iDiamondSize, 8);
655 :     CHECK_CANDIDATE(x, y - iDiamondSize, 4);
656 :     } else { // what remains here is up or down
657 :     CHECK_CANDIDATE(x + iDiamondSize, y, 2);
658 :     CHECK_CANDIDATE(x - iDiamondSize, y, 1); }
659 : chl 1.18
660 : chl 1.44.2.1 bDirection += iDirection;
661 :     x = data->currentMV->x; y = data->currentMV->y;
662 : chl 1.18 }
663 :     }
664 : chl 1.44.2.1 while (iDirection);
665 : chl 1.18 }
666 :    
667 : chl 1.44.2.1 /* MAINSEARCH FUNCTIONS END */
668 :    
669 :     /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */
670 : chl 1.18
671 : chl 1.44.2.1 static void
672 :     HalfpelRefine(const SearchData * const data)
673 : Isibaar 1.1 {
674 :     /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
675 :    
676 : chl 1.44.2.1 VECTOR backupMV = *(data->currentMV);
677 :     int iDirection; //not needed
678 : edgomez 1.20
679 : chl 1.44.2.1 CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);
680 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);
681 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);
682 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);
683 : edgomez 1.20
684 : chl 1.44.2.1 CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);
685 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);
686 :    
687 :     CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);
688 :     CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);
689 : Isibaar 1.1 }
690 :    
691 : Isibaar 1.44.2.7
692 :     static void
693 :     QuarterpelRefine(const SearchData * const data)
694 :     {
695 :     /* Perform quarter pixel refinement*/
696 :    
697 :     VECTOR backupMV = *(data->currentQMV);
698 :     int iDirection; //not needed
699 :    
700 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);
701 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);
702 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);
703 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);
704 :    
705 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);
706 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);
707 :    
708 :     CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);
709 :     CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);
710 :    
711 :     }
712 :    
713 : chl 1.44.2.1 static __inline int
714 :     SkipDecisionP(const IMAGE * current, const IMAGE * reference,
715 :     const int x, const int y,
716 :     const uint32_t iEdgedWidth, const uint32_t iQuant)
717 : Isibaar 1.1
718 : chl 1.44.2.1 {
719 :     /* keep repeating checks for all b-frames before this P frame,
720 :     to make sure that SKIP is possible (todo)
721 :     how: if skip is not possible set sad00 to a very high value */
722 : chl 1.3
723 : chl 1.44.2.1 uint32_t sadC = sad8(current->u + x*8 + y*(iEdgedWidth/2)*8,
724 :     reference->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2);
725 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
726 : h 1.44.2.3 sadC += sad8(current->v + (x + y*(iEdgedWidth/2))*8,
727 :     reference->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
728 : chl 1.44.2.1 if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
729 : chl 1.35
730 : chl 1.44.2.1 return 1;
731 :     }
732 : Isibaar 1.1
733 : chl 1.44.2.1 static __inline void
734 :     SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad)
735 :     {
736 :     pMB->mode = MODE_NOT_CODED;
737 : Isibaar 1.44.2.2 pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
738 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
739 : syskin 1.44.2.14
740 : Isibaar 1.44.2.7 pMB->qmvs[0].x = pMB->qmvs[1].x = pMB->qmvs[2].x = pMB->qmvs[3].x = 0;
741 :     pMB->qmvs[0].y = pMB->qmvs[1].y = pMB->qmvs[2].y = pMB->qmvs[3].y = 0;
742 :    
743 : chl 1.44.2.1 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
744 :     }
745 :    
746 : syskin 1.44.2.14 bool
747 : chl 1.44.2.1 MotionEstimation(MBParam * const pParam,
748 :     FRAMEINFO * const current,
749 :     FRAMEINFO * const reference,
750 :     const IMAGE * const pRefH,
751 :     const IMAGE * const pRefV,
752 :     const IMAGE * const pRefHV,
753 :     const uint32_t iLimit)
754 :     {
755 :     MACROBLOCK *const pMBs = current->mbs;
756 :     const IMAGE *const pCurrent = &current->image;
757 :     const IMAGE *const pRef = &reference->image;
758 : Isibaar 1.1
759 : syskin 1.44.2.14 FILE * debug;
760 : chl 1.44.2.1 const VECTOR zeroMV = { 0, 0 };
761 : edgomez 1.20
762 : chl 1.44.2.1 uint32_t x, y;
763 :     uint32_t iIntra = 0;
764 : syskin 1.44.2.14 int32_t InterBias, quant = current->quant, sad00;
765 : Isibaar 1.44.2.7 uint8_t *qimage;
766 : chl 1.44.2.1
767 : Isibaar 1.44.2.2 // some pre-initialized thingies for SearchP
768 :     int32_t temp[5];
769 :     VECTOR currentMV[5];
770 : Isibaar 1.44.2.7 VECTOR currentQMV[5];
771 : Isibaar 1.44.2.2 int32_t iMinSAD[5];
772 :     SearchData Data;
773 :     Data.iEdgedWidth = pParam->edged_width;
774 :     Data.currentMV = currentMV;
775 : Isibaar 1.44.2.7 Data.currentQMV = currentQMV;
776 : Isibaar 1.44.2.2 Data.iMinSAD = iMinSAD;
777 :     Data.temp = temp;
778 :     Data.iFcode = current->fcode;
779 : Isibaar 1.44.2.7 Data.rounding = pParam->m_rounding_type;
780 : syskin 1.44.2.14 Data.qpel = pParam->m_quarterpel;
781 :     Data.chroma = current->global_flags & XVID_ME_COLOUR;
782 : Isibaar 1.44.2.7
783 :     if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
784 : syskin 1.44.2.8 return 1; // allocate some mem for qpel interpolated blocks
785 : Isibaar 1.44.2.7 // somehow this is dirty since I think we shouldn't use malloc outside
786 :     // encoder_create() - so please fix me!
787 : syskin 1.44.2.14 Data.RefQ = qimage;
788 : chl 1.44.2.1 if (sadInit) (*sadInit) ();
789 :    
790 :     for (y = 0; y < pParam->mb_height; y++) {
791 :     for (x = 0; x < pParam->mb_width; x++) {
792 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
793 : syskin 1.44.2.14
794 :     pMB->sad16
795 : chl 1.44.2.1 = sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16,
796 :     pRef->y + (x + y * pParam->edged_width) * 16,
797 :     pParam->edged_width, pMB->sad8 );
798 :    
799 : syskin 1.44.2.14 if (Data.chroma) {
800 :     pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8,
801 :     pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2);
802 :    
803 :     pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8,
804 :     pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2);
805 :     }
806 :    
807 :     sad00 = pMB->sad16; //if no gmc; else sad00 = (..)
808 :    
809 : chl 1.44.2.1 if (!(current->global_flags & XVID_LUMIMASKING)) {
810 :     pMB->dquant = NO_CHANGE;
811 : syskin 1.44.2.11 pMB->quant = current->quant;
812 :     } else {
813 : syskin 1.44.2.5 if (pMB->dquant != NO_CHANGE) {
814 :     quant += DQtab[pMB->dquant];
815 :     if (quant > 31) quant = 31;
816 :     else if (quant < 1) quant = 1;
817 :     }
818 : syskin 1.44.2.11 pMB->quant = quant;
819 :     }
820 : chl 1.44.2.1
821 : syskin 1.44.2.14 //initial skip decision
822 : chl 1.44.2.13 /* no early skip for GMC (global vector = skip vector is unknown!) */
823 :     if (current->coding_type == P_VOP) { /* no fast SKIP for S(GMC)-VOPs */
824 :     if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH)
825 : syskin 1.44.2.14 if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {
826 : chl 1.44.2.13 SkipMacroblockP(pMB, sad00);
827 :     continue;
828 :     }
829 :     }
830 : syskin 1.44.2.14
831 :     SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
832 : chl 1.44.2.1 y, current->motion_flags, pMB->quant,
833 : Isibaar 1.44.2.2 &Data, pParam, pMBs, reference->mbs,
834 : chl 1.44.2.1 current->global_flags & XVID_INTER4V, pMB);
835 :    
836 :     /* final skip decision, a.k.a. "the vector you found, really that good?" */
837 : chl 1.44.2.13 if (current->coding_type == P_VOP) {
838 :     if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)
839 : syskin 1.44.2.11 && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) )
840 : syskin 1.44.2.14 if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {
841 : chl 1.44.2.13 SkipMacroblockP(pMB, sad00);
842 :     continue;
843 :     }
844 :     }
845 : syskin 1.44.2.14
846 : chl 1.44.2.1 /* finally, intra decision */
847 : Isibaar 1.1
848 : chl 1.44.2.1 InterBias = MV16_INTER_BIAS;
849 : syskin 1.44.2.14 if (pMB->quant > 8) InterBias += 80 * (pMB->quant - 8); // to make high quants work
850 : chl 1.44.2.1 if (y != 0)
851 : syskin 1.44.2.14 if ((pMB - pParam->mb_width)->mode == MODE_INTER ) InterBias -= 80;
852 : chl 1.44.2.1 if (x != 0)
853 : syskin 1.44.2.14 if ((pMB - 1)->mode == MODE_INTER ) InterBias -= 80;
854 :    
855 :     if (Data.chroma) InterBias += 50; // to compensate bigger SAD
856 : chl 1.44.2.1
857 :     if (InterBias < pMB->sad16) {
858 :     const int32_t deviation =
859 :     dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,
860 :     pParam->edged_width);
861 : edgomez 1.20
862 : chl 1.44.2.1 if (deviation < (pMB->sad16 - InterBias)) {
863 : Isibaar 1.44.2.7 if (++iIntra >= iLimit) { free(qimage); return 1; }
864 : chl 1.44.2.1 pMB->mode = MODE_INTRA;
865 : Isibaar 1.44.2.2 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] =
866 :     pMB->mvs[3] = zeroMV;
867 : Isibaar 1.44.2.7 pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] =
868 :     pMB->qmvs[3] = zeroMV;
869 : chl 1.44.2.1 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] =
870 :     pMB->sad8[3] = 0;
871 :     }
872 :     }
873 :     }
874 :     }
875 : Isibaar 1.44.2.7 free(qimage);
876 : chl 1.44.2.13
877 :     if (current->coding_type == S_VOP) /* first GMC step only for S(GMC)-VOPs */
878 : syskin 1.44.2.14 current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode );
879 : chl 1.44.2.13 else
880 :     current->GMC_MV = zeroMV;
881 :    
882 : syskin 1.44.2.14 return 0;
883 : chl 1.44.2.1 }
884 : chl 1.18
885 : edgomez 1.20
886 : chl 1.44.2.1 #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
887 : edgomez 1.20
888 : chl 1.44.2.1 static __inline int
889 :     make_mask(const VECTOR * const pmv, const int i)
890 :     {
891 : Isibaar 1.44.2.2 int mask = 255, j;
892 : chl 1.44.2.1 for (j = 0; j < i; j++) {
893 :     if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already
894 :     if (pmv[i].x == pmv[j].x) {
895 :     if (pmv[i].y == pmv[j].y + iDiamondSize) { mask &= ~4; continue; }
896 :     if (pmv[i].y == pmv[j].y - iDiamondSize) { mask &= ~8; continue; }
897 :     } else
898 :     if (pmv[i].y == pmv[j].y) {
899 :     if (pmv[i].x == pmv[j].x + iDiamondSize) { mask &= ~1; continue; }
900 :     if (pmv[i].x == pmv[j].x - iDiamondSize) { mask &= ~2; continue; }
901 :     }
902 :     }
903 :     return mask;
904 :     }
905 :    
906 :     static __inline void
907 :     PreparePredictionsP(VECTOR * const pmv, int x, int y, const int iWcount,
908 :     const int iHcount, const MACROBLOCK * const prevMB)
909 :     {
910 :    
911 :     //this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself
912 :    
913 :     if ( (y != 0) && (x != (iWcount-1)) ) { // [5] top-right neighbour
914 :     pmv[5].x = EVEN(pmv[3].x);
915 : Isibaar 1.44.2.2 pmv[5].y = EVEN(pmv[3].y);
916 :     } else pmv[5].x = pmv[5].y = 0;
917 : chl 1.44.2.1
918 :     if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }// pmv[3] is left neighbour
919 :     else pmv[3].x = pmv[3].y = 0;
920 :    
921 :     if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }// [4] top neighbour
922 :     else pmv[4].x = pmv[4].y = 0;
923 :    
924 :     // [1] median prediction
925 :     pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);
926 :    
927 :     pmv[0].x = pmv[0].y = 0; // [0] is zero; not used in the loop (checked before) but needed here for make_mask
928 :    
929 :     pmv[2].x = EVEN(prevMB->mvs[0].x); // [2] is last frame
930 :     pmv[2].y = EVEN(prevMB->mvs[0].y);
931 :    
932 :     if ((x != iWcount-1) && (y != iHcount-1)) {
933 :     pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); //[6] right-down neighbour in last frame
934 : Isibaar 1.44.2.2 pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
935 :     } else pmv[6].x = pmv[6].y = 0;
936 : chl 1.44.2.1 }
937 :    
938 :     static void
939 : syskin 1.44.2.14 SearchP(const IMAGE * const pRef,
940 : chl 1.44.2.1 const uint8_t * const pRefH,
941 :     const uint8_t * const pRefV,
942 :     const uint8_t * const pRefHV,
943 :     const IMAGE * const pCur,
944 :     const int x,
945 :     const int y,
946 :     const uint32_t MotionFlags,
947 :     const uint32_t iQuant,
948 : Isibaar 1.44.2.2 SearchData * const Data,
949 : chl 1.44.2.1 const MBParam * const pParam,
950 :     const MACROBLOCK * const pMBs,
951 :     const MACROBLOCK * const prevMBs,
952 :     int inter4v,
953 :     MACROBLOCK * const pMB)
954 :     {
955 : Isibaar 1.1
956 : chl 1.44.2.1 int i, iDirection = 255, mask, threshA;
957 : Isibaar 1.44.2.2 VECTOR pmv[7];
958 : chl 1.44.2.1
959 : Isibaar 1.44.2.2 get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)()
960 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
961 : Isibaar 1.44.2.7 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
962 : Isibaar 1.44.2.2
963 :     Data->predMV = pmv[0];
964 : Isibaar 1.44.2.7
965 : Isibaar 1.44.2.2 Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
966 : syskin 1.44.2.14 Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
967 :     Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
968 :    
969 :     Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;
970 : Isibaar 1.44.2.2 Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;
971 :     Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;
972 :     Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
973 : syskin 1.44.2.14 Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
974 :     Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
975 : Isibaar 1.1
976 : syskin 1.44.2.11 Data->lambda16 = lambda_vec16[iQuant];
977 : Isibaar 1.44.2.12 Data->lambda8 = lambda_vec8[iQuant];
978 : Isibaar 1.1
979 : edgomez 1.20 if (!(MotionFlags & PMV_HALFPEL16)) {
980 : Isibaar 1.44.2.2 Data->min_dx = EVEN(Data->min_dx);
981 :     Data->max_dx = EVEN(Data->max_dx);
982 :     Data->min_dy = EVEN(Data->min_dy);
983 :     Data->max_dy = EVEN(Data->max_dy); }
984 : edgomez 1.44.2.4
985 :     if (pMB->dquant != NO_CHANGE) inter4v = 0;
986 : Isibaar 1.44.2.2
987 : Isibaar 1.44.2.7 for(i = 0; i < 5; i++)
988 :     Data->currentMV[i].x = Data->currentMV[i].y = 0;
989 : chl 1.44.2.1
990 : Isibaar 1.44.2.12 if (pParam->m_quarterpel) {
991 :     Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
992 :     i = d_mv_bits(Data->predQMV.x, Data->predQMV.y, Data->iFcode);
993 :     } else i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode);
994 :    
995 : syskin 1.44.2.14 Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000;
996 :     Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100;
997 : Isibaar 1.44.2.2 Data->iMinSAD[2] = pMB->sad8[1];
998 :     Data->iMinSAD[3] = pMB->sad8[2];
999 :     Data->iMinSAD[4] = pMB->sad8[3];
1000 : chl 1.44.2.1
1001 :     if ((x == 0) && (y == 0)) threshA = 512;
1002 :     else {
1003 : edgomez 1.44.2.4 threshA = Data->temp[0]; // that's when we keep this SAD atm
1004 : chl 1.44.2.1 if (threshA < 512) threshA = 512;
1005 :     if (threshA > 1024) threshA = 1024; }
1006 :    
1007 :     PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
1008 :     prevMBs + x + y * pParam->mb_width);
1009 :    
1010 : syskin 1.44.2.14 if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16;
1011 :     else CheckCandidate = CheckCandidate16no4v;
1012 : edgomez 1.44.2.4
1013 : chl 1.44.2.1 /* main loop. checking all predictions */
1014 :    
1015 :     for (i = 1; i < 7; i++) {
1016 :     if (!(mask = make_mask(pmv, i)) ) continue;
1017 : edgomez 1.44.2.4 (*CheckCandidate)(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1018 : Isibaar 1.44.2.2 if (Data->iMinSAD[0] <= threshA) break;
1019 : chl 1.44.2.1 }
1020 :    
1021 : Isibaar 1.44.2.2 if ((Data->iMinSAD[0] <= threshA) ||
1022 :     (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
1023 :     (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {
1024 : chl 1.44.2.1 inter4v = 0;
1025 : Isibaar 1.44.2.2 } else {
1026 : edgomez 1.20
1027 : Isibaar 1.44.2.2 MainSearchFunc * MainSearchPtr;
1028 :     if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1029 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1030 :     else MainSearchPtr = DiamondSearch;
1031 : chl 1.35
1032 : Isibaar 1.44.2.2 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
1033 : chl 1.13
1034 : chl 1.44.2.1 /* extended search, diamond starting in 0,0 and in prediction.
1035 :     note that this search is/might be done in halfpel positions,
1036 :     which makes it more different than the diamond above */
1037 : Isibaar 1.1
1038 : Isibaar 1.44.2.2 if (MotionFlags & PMV_EXTSEARCH16) {
1039 :     int32_t bSAD;
1040 :     VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
1041 :     if (!(MotionFlags & PMV_HALFPELREFINE16)) // who's gonna use extsearch and no halfpel?
1042 :     startMV.x = EVEN(startMV.x); startMV.y = EVEN(startMV.y);
1043 :     if (!(MVequal(startMV, backupMV))) {
1044 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1045 :    
1046 : Isibaar 1.44.2.12 (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);
1047 : Isibaar 1.44.2.2 (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1048 :     if (bSAD < Data->iMinSAD[0]) {
1049 :     Data->currentMV[0] = backupMV;
1050 :     Data->iMinSAD[0] = bSAD; }
1051 :     }
1052 : chl 1.13
1053 : Isibaar 1.44.2.2 backupMV = Data->currentMV[0];
1054 :     if (MotionFlags & PMV_HALFPELREFINE16) startMV.x = startMV.y = 1;
1055 :     else startMV.x = startMV.y = 0;
1056 :     if (!(MVequal(startMV, backupMV))) {
1057 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1058 :    
1059 : Isibaar 1.44.2.12 (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);
1060 : Isibaar 1.44.2.2 (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1061 :     if (bSAD < Data->iMinSAD[0]) {
1062 :     Data->currentMV[0] = backupMV;
1063 :     Data->iMinSAD[0] = bSAD; }
1064 :     }
1065 : chl 1.44.2.1 }
1066 :     }
1067 : chl 1.13
1068 : Isibaar 1.44.2.2 if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);
1069 : Isibaar 1.1
1070 : Isibaar 1.44.2.7 for(i = 0; i < 5; i++) {
1071 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1072 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1073 :     }
1074 :    
1075 :     if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1076 :    
1077 : Isibaar 1.44.2.12 CheckCandidate = CheckCandidate16_qpel;
1078 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1079 : syskin 1.44.2.11 pParam->width, pParam->height, Data->iFcode, 0);
1080 : syskin 1.44.2.8
1081 : Isibaar 1.44.2.7 QuarterpelRefine(Data);
1082 :     }
1083 :    
1084 : syskin 1.44.2.14 if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0;
1085 : Isibaar 1.44.2.2 if (inter4v) {
1086 :     SearchData Data8;
1087 :     Data8.iFcode = Data->iFcode;
1088 : Isibaar 1.44.2.12 Data8.lambda8 = Data->lambda8;
1089 : Isibaar 1.44.2.2 Data8.iEdgedWidth = Data->iEdgedWidth;
1090 : Isibaar 1.44.2.12 Data8.RefQ = Data->RefQ;
1091 : syskin 1.44.2.14 Data8.qpel = Data->qpel;
1092 : Isibaar 1.44.2.2 Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1093 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
1094 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1095 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1096 : syskin 1.44.2.14
1097 :     if (Data->chroma) {
1098 :     int sum, dx, dy;
1099 :    
1100 :     if(pParam->m_quarterpel) {
1101 :     sum = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;
1102 :     } else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1103 :     dy = (sum >> 3) + roundtab_76[sum & 0xf];
1104 :    
1105 :     if(pParam->m_quarterpel) {
1106 :     sum = pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;
1107 :     } else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1108 :     dx = (sum >> 3) + roundtab_76[sum & 0xf];
1109 :    
1110 :     Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
1111 :     }
1112 : Isibaar 1.44.2.2 }
1113 : chl 1.13
1114 : chl 1.44.2.1 if (!(inter4v) ||
1115 : Isibaar 1.44.2.2 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
1116 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {
1117 : chl 1.44.2.1 // INTER MODE
1118 :     pMB->mode = MODE_INTER;
1119 : Isibaar 1.44.2.2 pMB->mvs[0] = pMB->mvs[1]
1120 :     = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1121 : edgomez 1.20
1122 : Isibaar 1.44.2.7 pMB->qmvs[0] = pMB->qmvs[1]
1123 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1124 :    
1125 : chl 1.44.2.1 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1126 : Isibaar 1.44.2.2 pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
1127 : chl 1.13
1128 : Isibaar 1.44.2.7 if(pParam->m_quarterpel) {
1129 :     pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;
1130 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;
1131 : syskin 1.44.2.14 } else {
1132 : Isibaar 1.44.2.7 pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1133 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1134 :     }
1135 : chl 1.44.2.1 } else {
1136 :     // INTER4V MODE; all other things are already set in Search8
1137 :     pMB->mode = MODE_INTER4V;
1138 : Isibaar 1.44.2.2 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] +
1139 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * iQuant;
1140 : chl 1.44.2.1 }
1141 :     }
1142 : edgomez 1.20
1143 : chl 1.44.2.1 static void
1144 :     Search8(const SearchData * const OldData,
1145 :     const int x, const int y,
1146 :     const uint32_t MotionFlags,
1147 :     const MBParam * const pParam,
1148 :     MACROBLOCK * const pMB,
1149 :     const MACROBLOCK * const pMBs,
1150 : Isibaar 1.44.2.2 const int block,
1151 :     SearchData * const Data)
1152 : chl 1.44.2.1 {
1153 : Isibaar 1.44.2.2 Data->iMinSAD = OldData->iMinSAD + 1 + block;
1154 :     Data->currentMV = OldData->currentMV + 1 + block;
1155 : Isibaar 1.44.2.7 Data->currentQMV = OldData->currentQMV + 1 + block;
1156 :    
1157 : syskin 1.44.2.11 if(pParam->m_quarterpel) {
1158 : Isibaar 1.44.2.12 Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1159 : syskin 1.44.2.14 if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *
1160 : syskin 1.44.2.11 d_mv_bits( Data->currentQMV->x - Data->predQMV.x,
1161 :     Data->currentQMV->y - Data->predQMV.y,
1162 : syskin 1.44.2.14 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;
1163 : Isibaar 1.44.2.12 } else {
1164 :     Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1165 : syskin 1.44.2.14 if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *
1166 : syskin 1.44.2.11 d_mv_bits( Data->currentMV->x - Data->predMV.x,
1167 :     Data->currentMV->y - Data->predMV.y,
1168 : syskin 1.44.2.14 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;
1169 : Isibaar 1.44.2.12 }
1170 : Isibaar 1.1
1171 : chl 1.44.2.1 if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) {
1172 : edgomez 1.20
1173 : Isibaar 1.44.2.2 Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1));
1174 :     Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1));
1175 :     Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1176 :     Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1177 : Isibaar 1.1
1178 : Isibaar 1.44.2.2 Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));
1179 : chl 1.44.2.1
1180 : Isibaar 1.44.2.2 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1181 : Isibaar 1.44.2.7 pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);
1182 : syskin 1.44.2.14 CheckCandidate = CheckCandidate8;
1183 : Isibaar 1.1
1184 : chl 1.44.2.1 if (MotionFlags & PMV_EXTSEARCH8) {
1185 : Isibaar 1.44.2.7 int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1186 :    
1187 : chl 1.44.2.1 MainSearchFunc *MainSearchPtr;
1188 :     if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;
1189 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;
1190 :     else MainSearchPtr = DiamondSearch;
1191 : Isibaar 1.1
1192 : Isibaar 1.44.2.7 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1193 : Isibaar 1.1
1194 : syskin 1.44.2.11 if(*(Data->iMinSAD) < temp_sad) {
1195 : Isibaar 1.44.2.7 Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1196 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1197 :     }
1198 :     }
1199 :    
1200 :     if (MotionFlags & PMV_HALFPELREFINE8) {
1201 :     int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1202 :    
1203 :     HalfpelRefine(Data); // perform halfpel refine of current best vector
1204 :    
1205 :     if(*(Data->iMinSAD) < temp_sad) { // we have found a better match
1206 :     Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1207 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1208 :     }
1209 :     }
1210 :    
1211 : syskin 1.44.2.11 if(pParam->m_quarterpel) {
1212 :     if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&
1213 :     (MotionFlags & PMV_QUARTERPELREFINE8)) {
1214 : Isibaar 1.44.2.10 CheckCandidate = CheckCandidate8_qpel;
1215 : Isibaar 1.44.2.12 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1216 :     pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);
1217 :     QuarterpelRefine(Data);
1218 : syskin 1.44.2.11 }
1219 : Isibaar 1.44.2.7 }
1220 :     }
1221 :    
1222 :     if(pParam->m_quarterpel) {
1223 :     pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x;
1224 :     pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y;
1225 :     }
1226 :     else {
1227 :     pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
1228 :     pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
1229 : Isibaar 1.1 }
1230 :    
1231 : Isibaar 1.44.2.2 pMB->mvs[block] = *(Data->currentMV);
1232 : Isibaar 1.44.2.7 pMB->qmvs[block] = *(Data->currentQMV);
1233 : Isibaar 1.44.2.2
1234 : syskin 1.44.2.11 pMB->sad8[block] = 4 * (*Data->iMinSAD);
1235 : Isibaar 1.1 }
1236 :    
1237 : chl 1.44.2.1 /* B-frames code starts here */
1238 : Isibaar 1.1
1239 : chl 1.44.2.1 static __inline VECTOR
1240 :     ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)
1241 :     {
1242 :     /* the stupidiest function ever */
1243 :     if (mode == MODE_FORWARD) return pMB->mvs[0];
1244 :     else return pMB->b_mvs[0];
1245 :     }
1246 : Isibaar 1.1
1247 : chl 1.44.2.1 static void __inline
1248 :     PreparePredictionsBF(VECTOR * const pmv, const int x, const int y,
1249 :     const uint32_t iWcount,
1250 :     const MACROBLOCK * const pMB,
1251 :     const uint32_t mode_curr)
1252 :     {
1253 : Isibaar 1.1
1254 : chl 1.44.2.1 // [0] is prediction
1255 :     pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);
1256 : Isibaar 1.1
1257 : chl 1.44.2.1 pmv[1].x = pmv[1].y = 0; // [1] is zero
1258 : Isibaar 1.1
1259 : chl 1.44.2.1 pmv[2] = ChoosePred(pMB, mode_curr);
1260 :     pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);
1261 : Isibaar 1.1
1262 : chl 1.44.2.1 if ((y != 0)&&(x != (int)(iWcount+1))) { // [3] top-right neighbour
1263 :     pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);
1264 : Isibaar 1.44.2.2 pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);
1265 :     } else pmv[3].x = pmv[3].y = 0;
1266 : chl 1.39
1267 : chl 1.44.2.1 if (y != 0) {
1268 :     pmv[4] = ChoosePred(pMB-iWcount, mode_curr);
1269 :     pmv[4].x = EVEN(pmv[4].x); pmv[4].y = EVEN(pmv[4].y);
1270 :     } else pmv[4].x = pmv[4].y = 0;
1271 : chl 1.39
1272 : chl 1.44.2.1 if (x != 0) {
1273 :     pmv[5] = ChoosePred(pMB-1, mode_curr);
1274 :     pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
1275 :     } else pmv[5].x = pmv[5].y = 0;
1276 : chl 1.39
1277 : chl 1.44.2.1 if ((x != 0)&&(y != 0)) {
1278 :     pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr);
1279 :     pmv[6].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
1280 :     } else pmv[6].x = pmv[6].y = 0;
1281 : chl 1.39
1282 : chl 1.44.2.1 // more?
1283 : chl 1.39 }
1284 :    
1285 :    
1286 : chl 1.44.2.1 /* search backward or forward, for b-frames */
1287 :     static void
1288 :     SearchBF( const uint8_t * const pRef,
1289 :     const uint8_t * const pRefH,
1290 :     const uint8_t * const pRefV,
1291 :     const uint8_t * const pRefHV,
1292 :     const IMAGE * const pCur,
1293 :     const int x, const int y,
1294 :     const uint32_t MotionFlags,
1295 :     const uint32_t iFcode,
1296 :     const MBParam * const pParam,
1297 :     MACROBLOCK * const pMB,
1298 :     const VECTOR * const predMV,
1299 :     int32_t * const best_sad,
1300 : h 1.44.2.3 const int32_t mode_current,
1301 :     SearchData * const Data)
1302 : Isibaar 1.1 {
1303 :    
1304 : chl 1.44.2.1 const int32_t iEdgedWidth = pParam->edged_width;
1305 :    
1306 :     int i, iDirection, mask;
1307 : h 1.44.2.3 VECTOR pmv[7];
1308 : chl 1.44.2.1 MainSearchFunc *MainSearchPtr;
1309 : h 1.44.2.3 *Data->iMinSAD = MV_MAX_ERROR;
1310 :     Data->iFcode = iFcode;
1311 : chl 1.44.2.1
1312 : h 1.44.2.3 Data->Ref = pRef + (x + y * iEdgedWidth) * 16;
1313 :     Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;
1314 :     Data->RefV = pRefV + (x + y * iEdgedWidth) * 16;
1315 :     Data->RefHV = pRefHV + (x + y * iEdgedWidth) * 16;
1316 : chl 1.44.2.1
1317 : h 1.44.2.3 Data->predMV = *predMV;
1318 : edgomez 1.20
1319 : h 1.44.2.3 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1320 : Isibaar 1.44.2.7 pParam->width, pParam->height, iFcode, pParam->m_quarterpel);
1321 : Isibaar 1.1
1322 : h 1.44.2.3 pmv[0] = Data->predMV;
1323 :     PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);
1324 : Isibaar 1.1
1325 : h 1.44.2.3 Data->currentMV->x = Data->currentMV->y = 0;
1326 : Isibaar 1.1
1327 : chl 1.44.2.1 CheckCandidate = CheckCandidate16no4v;
1328 : edgomez 1.20
1329 : chl 1.44.2.1 // main loop. checking all predictions
1330 :     for (i = 0; i < 8; i++) {
1331 :     if (!(mask = make_mask(pmv, i)) ) continue;
1332 : h 1.44.2.3 CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1333 : edgomez 1.20 }
1334 :    
1335 : chl 1.44.2.1 if (MotionFlags & PMV_USESQUARES16)
1336 :     MainSearchPtr = SquareSearch;
1337 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1338 :     MainSearchPtr = AdvDiamondSearch;
1339 :     else MainSearchPtr = DiamondSearch;
1340 : Isibaar 1.1
1341 : h 1.44.2.3 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1342 : Isibaar 1.1
1343 : h 1.44.2.3 HalfpelRefine(Data);
1344 : Isibaar 1.1
1345 : chl 1.44.2.1 // three bits are needed to code backward mode. four for forward
1346 :     // we treat the bits just like they were vector's
1347 : syskin 1.44.2.14 if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16;
1348 :     else *Data->iMinSAD += 3 * Data->lambda16;
1349 : chl 1.44.2.1
1350 : h 1.44.2.3 if (*Data->iMinSAD < *best_sad) {
1351 :     *best_sad = *Data->iMinSAD;
1352 : chl 1.44.2.1 pMB->mode = mode_current;
1353 : h 1.44.2.3 pMB->pmvs[0].x = Data->currentMV->x - predMV->x;
1354 :     pMB->pmvs[0].y = Data->currentMV->y - predMV->y;
1355 : syskin 1.44.2.11 if (mode_current == MODE_FORWARD) pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV;
1356 :     else pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search
1357 : Isibaar 1.1 }
1358 : chl 1.44.2.1
1359 :     }
1360 :    
1361 :     static int32_t
1362 : Isibaar 1.44.2.2 SearchDirect(const IMAGE * const f_Ref,
1363 : chl 1.44.2.1 const uint8_t * const f_RefH,
1364 :     const uint8_t * const f_RefV,
1365 :     const uint8_t * const f_RefHV,
1366 : Isibaar 1.44.2.2 const IMAGE * const b_Ref,
1367 : chl 1.44.2.1 const uint8_t * const b_RefH,
1368 :     const uint8_t * const b_RefV,
1369 :     const uint8_t * const b_RefHV,
1370 :     const IMAGE * const pCur,
1371 :     const int x, const int y,
1372 :     const uint32_t MotionFlags,
1373 :     const int32_t TRB, const int32_t TRD,
1374 :     const MBParam * const pParam,
1375 :     MACROBLOCK * const pMB,
1376 :     const MACROBLOCK * const b_mb,
1377 : h 1.44.2.3 int32_t * const best_sad,
1378 :     SearchData * const Data)
1379 : chl 1.44.2.1
1380 :     {
1381 : h 1.44.2.3 int32_t skip_sad;
1382 : chl 1.44.2.1 int k;
1383 : h 1.44.2.3
1384 : chl 1.44.2.1 MainSearchFunc *MainSearchPtr;
1385 :    
1386 : h 1.44.2.3 *Data->iMinSAD = 256*4096;
1387 :     Data->referencemv = b_mb->mvs;
1388 : chl 1.44.2.1
1389 : h 1.44.2.3 Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16;
1390 :     Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16;
1391 :     Data->RefV = f_RefV + (x + Data->iEdgedWidth*y) * 16;
1392 :     Data->RefHV = f_RefHV + (x + Data->iEdgedWidth*y) * 16;
1393 :     Data->bRef = b_Ref->y + (x + Data->iEdgedWidth*y) * 16;
1394 :     Data->bRefH = b_RefH + (x + Data->iEdgedWidth*y) * 16;
1395 :     Data->bRefV = b_RefV + (x + Data->iEdgedWidth*y) * 16;
1396 :     Data->bRefHV = b_RefHV + (x + Data->iEdgedWidth*y) * 16;
1397 :    
1398 :     Data->max_dx = 2 * pParam->width - 2 * (x) * 16;
1399 :     Data->max_dy = 2 * pParam->height - 2 * (y) * 16;
1400 :     Data->min_dx = -(2 * 16 + 2 * (x) * 16);
1401 :     Data->min_dy = -(2 * 16 + 2 * (y) * 16);
1402 : chl 1.44.2.1
1403 :     for (k = 0; k < 4; k++) {
1404 : h 1.44.2.3 pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD);
1405 :     pMB->b_mvs[k].x = Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD;
1406 :     pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD);
1407 :     pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD;
1408 :    
1409 :     if ( ( pMB->b_mvs[k].x > Data->max_dx ) || ( pMB->b_mvs[k].x < Data->min_dx )
1410 :     || ( pMB->b_mvs[k].y > Data->max_dy ) || ( pMB->b_mvs[k].y < Data->min_dy )) {
1411 : chl 1.44.2.1
1412 : Isibaar 1.44.2.2 *best_sad = 256*4096; // in that case, we won't use direct mode
1413 :     pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV"
1414 :     pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;
1415 :     return 0;
1416 :     }
1417 :     if (b_mb->mode != MODE_INTER4V) {
1418 :     pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0];
1419 :     pMB->b_mvs[1] = pMB->b_mvs[2] = pMB->b_mvs[3] = pMB->b_mvs[0];
1420 : h 1.44.2.3 Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0];
1421 :     Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0];
1422 : Isibaar 1.44.2.2 break;
1423 :     }
1424 :     }
1425 : chl 1.44.2.1
1426 :     if (b_mb->mode == MODE_INTER4V)
1427 :     CheckCandidate = CheckCandidateDirect;
1428 :     else CheckCandidate = CheckCandidateDirectno4v;
1429 :    
1430 : h 1.44.2.3 (*CheckCandidate)(0, 0, 255, &k, Data);
1431 : Isibaar 1.44.2.2
1432 :     // skip decision
1433 : syskin 1.44.2.14 if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) {
1434 : syskin 1.44.2.8 //possible skip - checking chroma. everything copied from MC
1435 : Isibaar 1.44.2.2 //this is not full chroma compensation, only it's fullpel approximation. should work though
1436 :     int sum, dx, dy, b_dx, b_dy;
1437 :    
1438 :     sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1439 :     dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1440 :    
1441 :     sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1442 :     dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1443 :    
1444 :     sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1445 :     b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1446 :    
1447 :     sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1448 :     b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1449 :    
1450 : h 1.44.2.3 sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2),
1451 :     f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,
1452 :     b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,
1453 :     Data->iEdgedWidth/2);
1454 :     sum += sad8bi(pCur->v + 8*x + 8*y*(Data->iEdgedWidth/2),
1455 :     f_Ref->v + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,
1456 :     b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,
1457 :     Data->iEdgedWidth/2);
1458 : Isibaar 1.44.2.2
1459 : syskin 1.44.2.11 if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {
1460 : Isibaar 1.44.2.2 pMB->mode = MODE_DIRECT_NONE_MV;
1461 : h 1.44.2.3 return *Data->iMinSAD;
1462 : Isibaar 1.44.2.2 }
1463 :     }
1464 :    
1465 : h 1.44.2.3 skip_sad = *Data->iMinSAD;
1466 : Isibaar 1.44.2.2
1467 : chl 1.44.2.1 // DIRECT MODE DELTA VECTOR SEARCH.
1468 :     // This has to be made more effective, but at the moment I'm happy it's running at all
1469 :    
1470 :     if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1471 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1472 :     else MainSearchPtr = DiamondSearch;
1473 :    
1474 : h 1.44.2.3 (*MainSearchPtr)(0, 0, Data, 255);
1475 : chl 1.44.2.1
1476 : h 1.44.2.3 HalfpelRefine(Data);
1477 : chl 1.44.2.1
1478 : syskin 1.44.2.14 *Data->iMinSAD += 1 * Data->lambda16; // one bit is needed to code direct mode
1479 : h 1.44.2.3 *best_sad = *Data->iMinSAD;
1480 : chl 1.44.2.1
1481 :     if (b_mb->mode == MODE_INTER4V)
1482 :     pMB->mode = MODE_DIRECT;
1483 :     else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation
1484 :    
1485 : h 1.44.2.3 pMB->pmvs[3] = *Data->currentMV;
1486 : chl 1.44.2.1
1487 :     for (k = 0; k < 4; k++) {
1488 : h 1.44.2.3 pMB->mvs[k].x = Data->directmvF[k].x + Data->currentMV->x;
1489 :     pMB->b_mvs[k].x = ((Data->currentMV->x == 0)
1490 :     ? Data->directmvB[k].x
1491 :     : pMB->mvs[k].x - Data->referencemv[k].x);
1492 :     pMB->mvs[k].y = (Data->directmvF[k].y + Data->currentMV->y);
1493 :     pMB->b_mvs[k].y = ((Data->currentMV->y == 0)
1494 :     ? Data->directmvB[k].y
1495 :     : pMB->mvs[k].y - Data->referencemv[k].y);
1496 : chl 1.44.2.1 if (b_mb->mode != MODE_INTER4V) {
1497 :     pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];
1498 :     pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];
1499 :     break;
1500 :     }
1501 :     }
1502 : h 1.44.2.3 return skip_sad;
1503 : chl 1.44.2.1 }
1504 :    
1505 : h 1.44.2.3
1506 : chl 1.44.2.1 static __inline void
1507 :     SearchInterpolate(const uint8_t * const f_Ref,
1508 :     const uint8_t * const f_RefH,
1509 :     const uint8_t * const f_RefV,
1510 :     const uint8_t * const f_RefHV,
1511 :     const uint8_t * const b_Ref,
1512 :     const uint8_t * const b_RefH,
1513 :     const uint8_t * const b_RefV,
1514 :     const uint8_t * const b_RefHV,
1515 :     const IMAGE * const pCur,
1516 :     const int x, const int y,
1517 :     const uint32_t fcode,
1518 :     const uint32_t bcode,
1519 :     const uint32_t MotionFlags,
1520 :     const MBParam * const pParam,
1521 :     const VECTOR * const f_predMV,
1522 :     const VECTOR * const b_predMV,
1523 :     MACROBLOCK * const pMB,
1524 : h 1.44.2.3 int32_t * const best_sad,
1525 :     SearchData * const fData)
1526 : edgomez 1.20
1527 : chl 1.3 {
1528 :    
1529 : edgomez 1.20 const int32_t iEdgedWidth = pParam->edged_width;
1530 : chl 1.3
1531 : chl 1.44.2.1 int iDirection, i, j;
1532 : h 1.44.2.3 SearchData bData;
1533 : edgomez 1.20
1534 : syskin 1.44.2.11 *(bData.iMinSAD = fData->iMinSAD) = 4096*256;
1535 : h 1.44.2.3 bData.Cur = fData->Cur;
1536 :     fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth;
1537 :     bData.currentMV = fData->currentMV + 1;
1538 : syskin 1.44.2.11 bData.lambda16 = fData->lambda16;
1539 : h 1.44.2.3 fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;
1540 :    
1541 :     bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16;
1542 :     bData.bRefH = fData->RefH = f_RefH + (x + y * iEdgedWidth) * 16;
1543 :     bData.bRefV = fData->RefV = f_RefV + (x + y * iEdgedWidth) * 16;
1544 :     bData.bRefHV = fData->RefHV = f_RefHV + (x + y * iEdgedWidth) * 16;
1545 :     bData.Ref = fData->bRef = b_Ref + (x + y * iEdgedWidth) * 16;
1546 :     bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16;
1547 :     bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;
1548 :     bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;
1549 :    
1550 :     bData.bpredMV = fData->predMV = *f_predMV;
1551 :     fData->bpredMV = bData.predMV = *b_predMV;
1552 :    
1553 : syskin 1.44.2.11 fData->currentMV[0] = fData->currentMV[3]; //forward search stored it's vector here. backward stored it in the place it's needed
1554 : Isibaar 1.44.2.7 get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel);
1555 :     get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel);
1556 : chl 1.3
1557 : h 1.44.2.3 if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;
1558 :     if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dy;
1559 :     if (fData->currentMV[0].y > fData->max_dy) fData->currentMV[0].y = fData->max_dx;
1560 :     if (fData->currentMV[0].y > fData->min_dy) fData->currentMV[0].y = fData->min_dy;
1561 :    
1562 :     if (fData->currentMV[1].x > bData.max_dx) fData->currentMV[1].x = bData.max_dx;
1563 :     if (fData->currentMV[1].x < bData.min_dx) fData->currentMV[1].x = bData.min_dy;
1564 :     if (fData->currentMV[1].y > bData.max_dy) fData->currentMV[1].y = bData.max_dx;
1565 :     if (fData->currentMV[1].y > bData.min_dy) fData->currentMV[1].y = bData.min_dy;
1566 : Isibaar 1.44.2.2
1567 : h 1.44.2.3 CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);
1568 : chl 1.3
1569 : chl 1.44.2.1 //diamond. I wish we could use normal mainsearch functions (square, advdiamond)
1570 : chl 1.3
1571 : chl 1.44.2.1 do {
1572 :     iDirection = 255;
1573 :     // forward MV moves
1574 : h 1.44.2.3 i = fData->currentMV[0].x; j = fData->currentMV[0].y;
1575 : chl 1.35
1576 : h 1.44.2.3 CheckCandidateInt(i + 1, j, 0, &iDirection, fData);
1577 :     CheckCandidateInt(i, j + 1, 0, &iDirection, fData);
1578 :     CheckCandidateInt(i - 1, j, 0, &iDirection, fData);
1579 :     CheckCandidateInt(i, j - 1, 0, &iDirection, fData);
1580 : edgomez 1.20
1581 : chl 1.44.2.1 // backward MV moves
1582 : h 1.44.2.3 i = fData->currentMV[1].x; j = fData->currentMV[1].y;
1583 :     fData->currentMV[2] = fData->currentMV[0];
1584 : chl 1.3
1585 : Isibaar 1.44.2.2 CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);
1586 :     CheckCandidateInt(i, j + 1, 0, &iDirection, &bData);
1587 :     CheckCandidateInt(i - 1, j, 0, &iDirection, &bData);
1588 :     CheckCandidateInt(i, j - 1, 0, &iDirection, &bData);
1589 : chl 1.3
1590 : chl 1.44.2.1 } while (!(iDirection));
1591 : chl 1.3
1592 : syskin 1.44.2.14 *fData->iMinSAD += 2 * fData->lambda16; // two bits are needed to code interpolate mode.
1593 : Isibaar 1.44.2.12
1594 : h 1.44.2.3 if (*fData->iMinSAD < *best_sad) {
1595 :     *best_sad = *fData->iMinSAD;
1596 :     pMB->mvs[0] = fData->currentMV[0];
1597 :     pMB->b_mvs[0] = fData->currentMV[1];
1598 : chl 1.44.2.1 pMB->mode = MODE_INTERPOLATE;
1599 : edgomez 1.20
1600 : chl 1.44.2.1 pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;
1601 :     pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;
1602 :     pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;
1603 :     pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;
1604 : edgomez 1.20 }
1605 : chl 1.3 }
1606 :    
1607 : chl 1.44.2.1 void
1608 :     MotionEstimationBVOP(MBParam * const pParam,
1609 :     FRAMEINFO * const frame,
1610 :     const int32_t time_bp,
1611 :     const int32_t time_pp,
1612 :     // forward (past) reference
1613 :     const MACROBLOCK * const f_mbs,
1614 :     const IMAGE * const f_ref,
1615 :     const IMAGE * const f_refH,
1616 :     const IMAGE * const f_refV,
1617 :     const IMAGE * const f_refHV,
1618 :     // backward (future) reference
1619 :     const MACROBLOCK * const b_mbs,
1620 :     const IMAGE * const b_ref,
1621 :     const IMAGE * const b_refH,
1622 :     const IMAGE * const b_refV,
1623 :     const IMAGE * const b_refHV)
1624 :     {
1625 :     uint32_t i, j;
1626 :     int32_t best_sad, skip_sad;
1627 :     int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0;
1628 :     static const VECTOR zeroMV={0,0};
1629 : chl 1.3
1630 : chl 1.44.2.1 VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/
1631 : chl 1.3
1632 : chl 1.44.2.1 const int32_t TRB = time_pp - time_bp;
1633 :     const int32_t TRD = time_pp;
1634 : chl 1.3
1635 : h 1.44.2.3 // some pre-inintialized data for the rest of the search
1636 :    
1637 :     SearchData Data;
1638 :     int32_t iMinSAD;
1639 :     VECTOR currentMV[3];
1640 :     Data.iEdgedWidth = pParam->edged_width;
1641 :     Data.currentMV = currentMV;
1642 :     Data.iMinSAD = &iMinSAD;
1643 : syskin 1.44.2.11 Data.lambda16 = lambda_vec16[frame->quant];
1644 : h 1.44.2.3
1645 : chl 1.44.2.1 // note: i==horizontal, j==vertical
1646 : edgomez 1.20
1647 : chl 1.44.2.1 for (j = 0; j < pParam->mb_height; j++) {
1648 : chl 1.3
1649 : chl 1.44.2.1 f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */
1650 : edgomez 1.20
1651 : chl 1.44.2.1 for (i = 0; i < pParam->mb_width; i++) {
1652 :     MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
1653 :     const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
1654 : edgomez 1.20
1655 : chl 1.44.2.13 /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
1656 : syskin 1.44.2.14 if (b_mb->mode == MODE_NOT_CODED) {
1657 : chl 1.44.2.1 pMB->mode = MODE_NOT_CODED;
1658 :     continue;
1659 :     }
1660 : edgomez 1.20
1661 : h 1.44.2.3 Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
1662 : syskin 1.44.2.11 pMB->quant = frame->quant;
1663 : syskin 1.44.2.14
1664 : chl 1.44.2.1 /* direct search comes first, because it (1) checks for SKIP-mode
1665 :     and (2) sets very good predictions for forward and backward search */
1666 : Isibaar 1.44.2.2 skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y,
1667 :     b_ref, b_refH->y, b_refV->y, b_refHV->y,
1668 : chl 1.44.2.1 &frame->image,
1669 :     i, j,
1670 :     frame->motion_flags,
1671 :     TRB, TRD,
1672 :     pParam,
1673 :     pMB, b_mb,
1674 : h 1.44.2.3 &best_sad,
1675 :     &Data);
1676 : edgomez 1.20
1677 : Isibaar 1.44.2.2 if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }
1678 : chl 1.3
1679 : chl 1.44.2.1 // forward search
1680 :     SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
1681 :     &frame->image, i, j,
1682 :     frame->motion_flags,
1683 : h 1.44.2.3 frame->fcode, pParam,
1684 : chl 1.44.2.1 pMB, &f_predMV, &best_sad,
1685 : h 1.44.2.3 MODE_FORWARD, &Data);
1686 : chl 1.10
1687 : chl 1.44.2.1 // backward search
1688 :     SearchBF(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
1689 :     &frame->image, i, j,
1690 :     frame->motion_flags,
1691 : h 1.44.2.3 frame->bcode, pParam,
1692 : chl 1.44.2.1 pMB, &b_predMV, &best_sad,
1693 : h 1.44.2.3 MODE_BACKWARD, &Data);
1694 : chl 1.10
1695 : chl 1.44.2.1 // interpolate search comes last, because it uses data from forward and backward as prediction
1696 : edgomez 1.20
1697 : chl 1.44.2.1 SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
1698 :     b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
1699 :     &frame->image,
1700 :     i, j,
1701 :     frame->fcode, frame->bcode,
1702 :     frame->motion_flags,
1703 : h 1.44.2.3 pParam,
1704 : chl 1.44.2.1 &f_predMV, &b_predMV,
1705 : h 1.44.2.3 pMB, &best_sad,
1706 :     &Data);
1707 : edgomez 1.20
1708 : chl 1.44.2.1 switch (pMB->mode) {
1709 :     case MODE_FORWARD:
1710 :     f_count++;
1711 :     f_predMV = pMB->mvs[0];
1712 :     break;
1713 :     case MODE_BACKWARD:
1714 :     b_count++;
1715 :     b_predMV = pMB->b_mvs[0];
1716 :     break;
1717 :     case MODE_INTERPOLATE:
1718 :     i_count++;
1719 :     f_predMV = pMB->mvs[0];
1720 :     b_predMV = pMB->b_mvs[0];
1721 :     break;
1722 :     case MODE_DIRECT:
1723 :     case MODE_DIRECT_NO4V:
1724 :     d_count++;
1725 :     break;
1726 :     default:
1727 :     break;
1728 : chl 1.10 }
1729 :     }
1730 :     }
1731 : chl 1.44.2.1 }
1732 : chl 1.3
1733 : chl 1.44.2.1 /* Hinted ME starts here */
1734 : chl 1.18
1735 : syskin 1.44.2.8 static void
1736 : syskin 1.44.2.14 SearchPhinted ( const IMAGE * const pRef,
1737 : chl 1.30 const uint8_t * const pRefH,
1738 :     const uint8_t * const pRefV,
1739 :     const uint8_t * const pRefHV,
1740 :     const IMAGE * const pCur,
1741 :     const int x,
1742 :     const int y,
1743 :     const uint32_t MotionFlags,
1744 :     const uint32_t iQuant,
1745 :     const MBParam * const pParam,
1746 :     const MACROBLOCK * const pMBs,
1747 : chl 1.44.2.1 int inter4v,
1748 : h 1.44.2.3 MACROBLOCK * const pMB,
1749 : edgomez 1.44.2.4 SearchData * const Data)
1750 : chl 1.44.2.1 {
1751 : chl 1.30
1752 : edgomez 1.44.2.4 int i, t;
1753 : chl 1.44.2.1 MainSearchFunc * MainSearchPtr;
1754 :    
1755 : h 1.44.2.3 Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1756 : syskin 1.44.2.14 Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1757 : h 1.44.2.3 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1758 : Isibaar 1.44.2.7 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
1759 : chl 1.44.2.1
1760 : syskin 1.44.2.14 Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
1761 :     Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1762 :     Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1763 :    
1764 :     Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;
1765 :     Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;
1766 :     Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;
1767 :     Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
1768 :     Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1769 :     Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1770 : edgomez 1.44.2.4
1771 : chl 1.44.2.1 if (!(MotionFlags & PMV_HALFPEL16)) {
1772 : h 1.44.2.3 Data->min_dx = EVEN(Data->min_dx);
1773 :     Data->max_dx = EVEN(Data->max_dx);
1774 :     Data->min_dy = EVEN(Data->min_dy);
1775 :     Data->max_dy = EVEN(Data->max_dy);
1776 : chl 1.30 }
1777 :    
1778 : h 1.44.2.3 for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
1779 : chl 1.30
1780 : chl 1.44.2.1 if (pMB->dquant != NO_CHANGE) inter4v = 0;
1781 : chl 1.30
1782 : syskin 1.44.2.14 if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16;
1783 :     else CheckCandidate = CheckCandidate16no4v;
1784 : chl 1.30
1785 : chl 1.44.2.1 pMB->mvs[0].x = EVEN(pMB->mvs[0].x);
1786 :     pMB->mvs[0].y = EVEN(pMB->mvs[0].y);
1787 : h 1.44.2.3 if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed
1788 :     if (pMB->mvs[0].x < Data->min_dx) pMB->mvs[0].x = Data->min_dx;
1789 :     if (pMB->mvs[0].y > Data->max_dy) pMB->mvs[0].y = Data->max_dy;
1790 :     if (pMB->mvs[0].y < Data->min_dy) pMB->mvs[0].y = Data->min_dy;
1791 : chl 1.30
1792 : edgomez 1.44.2.4 (*CheckCandidate)(pMB->mvs[0].x, pMB->mvs[0].y, 0, &t, Data);
1793 : chl 1.30
1794 : chl 1.44.2.1 if (pMB->mode == MODE_INTER4V)
1795 :     for (i = 1; i < 4; i++) { // all four vectors will be used as four predictions for 16x16 search
1796 :     pMB->mvs[i].x = EVEN(pMB->mvs[i].x);
1797 :     pMB->mvs[i].y = EVEN(pMB->mvs[i].y);
1798 :     if (!(make_mask(pMB->mvs, i)))
1799 : edgomez 1.44.2.4 (*CheckCandidate)(pMB->mvs[i].x, pMB->mvs[i].y, 0, &t, Data);
1800 : chl 1.44.2.1 }
1801 : chl 1.30
1802 :     if (MotionFlags & PMV_USESQUARES16)
1803 : chl 1.44.2.1 MainSearchPtr = SquareSearch;
1804 : chl 1.30 else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1805 : chl 1.44.2.1 MainSearchPtr = AdvDiamondSearch;
1806 :     else MainSearchPtr = DiamondSearch;
1807 : chl 1.30
1808 : h 1.44.2.3 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1809 : chl 1.30
1810 : h 1.44.2.3 if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);
1811 : chl 1.30
1812 : syskin 1.44.2.8 for(i = 0; i < 5; i++) {
1813 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1814 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1815 :     }
1816 :    
1817 :     if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1818 : Isibaar 1.44.2.12 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1819 :     pParam->width, pParam->height, Data->iFcode, 0);
1820 :     CheckCandidate = CheckCandidate16_qpel;
1821 : syskin 1.44.2.8 QuarterpelRefine(Data);
1822 :     }
1823 :    
1824 :     if (inter4v) {
1825 :     SearchData Data8;
1826 :     Data8.iFcode = Data->iFcode;
1827 : Isibaar 1.44.2.12 Data8.lambda8 = Data->lambda8;
1828 : syskin 1.44.2.8 Data8.iEdgedWidth = Data->iEdgedWidth;
1829 : Isibaar 1.44.2.12 Data8.RefQ = Data->RefQ;
1830 : syskin 1.44.2.14 Data8.qpel = Data->qpel;
1831 :     Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1832 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
1833 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1834 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1835 :    
1836 :     if (Data->chroma) {
1837 :     int sum, dx, dy;
1838 :    
1839 :     if(pParam->m_quarterpel)
1840 :     sum = (pMB->qmvs[0].y + pMB->qmvs[1].y + pMB->qmvs[2].y + pMB->qmvs[3].y)/2;
1841 :     else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1842 :     dy = (sum ? SIGN(sum) *
1843 :     (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0);
1844 :    
1845 :     if(pParam->m_quarterpel)
1846 :     sum = (pMB->qmvs[0].x + pMB->qmvs[1].x + pMB->qmvs[2].x + pMB->qmvs[3].x)/2;
1847 :     else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1848 :     dx = (sum ? SIGN(sum) *
1849 :     (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0);
1850 :     Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
1851 :     }
1852 : syskin 1.44.2.8 }
1853 : chl 1.30
1854 : chl 1.44.2.1 if (!(inter4v) ||
1855 : h 1.44.2.3 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] +
1856 :     Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {
1857 : chl 1.44.2.1 // INTER MODE
1858 :     pMB->mode = MODE_INTER;
1859 : Isibaar 1.44.2.2 pMB->mvs[0] = pMB->mvs[1]
1860 : h 1.44.2.3 = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1861 : chl 1.30
1862 : Isibaar 1.44.2.12 pMB->qmvs[0] = pMB->qmvs[1]
1863 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1864 :    
1865 : chl 1.44.2.1 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1866 : h 1.44.2.3 pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
1867 : chl 1.30
1868 : Isibaar 1.44.2.12 if(pParam->m_quarterpel) {
1869 :     pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;
1870 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;
1871 : syskin 1.44.2.14 } else {
1872 : Isibaar 1.44.2.12 pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1873 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1874 :     }
1875 : chl 1.44.2.1 } else {
1876 : syskin 1.44.2.14 // INTER4V MODE; all other things are already set in Search8
1877 : chl 1.44.2.1 pMB->mode = MODE_INTER4V;
1878 : h 1.44.2.3 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3]
1879 :     + Data->iMinSAD[4] + IMV16X16 * iQuant;
1880 : chl 1.44.2.1 }
1881 : chl 1.30
1882 :     }
1883 : suxen_drol 1.6
1884 : edgomez 1.20 void
1885 : chl 1.44.2.1 MotionEstimationHinted( MBParam * const pParam,
1886 :     FRAMEINFO * const current,
1887 :     FRAMEINFO * const reference,
1888 :     const IMAGE * const pRefH,
1889 :     const IMAGE * const pRefV,
1890 :     const IMAGE * const pRefHV)
1891 : suxen_drol 1.6 {
1892 : chl 1.44.2.1 MACROBLOCK *const pMBs = current->mbs;
1893 :     const IMAGE *const pCurrent = &current->image;
1894 :     const IMAGE *const pRef = &reference->image;
1895 : chl 1.32
1896 : chl 1.44.2.1 uint32_t x, y;
1897 : syskin 1.44.2.11 uint8_t * qimage;
1898 : syskin 1.44.2.5 int32_t temp[5], quant = current->quant;
1899 : h 1.44.2.3 int32_t iMinSAD[5];
1900 : Isibaar 1.44.2.12 VECTOR currentMV[5], currentQMV[5];
1901 : h 1.44.2.3 SearchData Data;
1902 :     Data.iEdgedWidth = pParam->edged_width;
1903 :     Data.currentMV = currentMV;
1904 : Isibaar 1.44.2.12 Data.currentQMV = currentQMV;
1905 : h 1.44.2.3 Data.iMinSAD = iMinSAD;
1906 :     Data.temp = temp;
1907 :     Data.iFcode = current->fcode;
1908 : syskin 1.44.2.8 Data.rounding = pParam->m_rounding_type;
1909 : syskin 1.44.2.14 Data.qpel = pParam->m_quarterpel;
1910 :     Data.chroma = current->global_flags & XVID_ME_COLOUR;
1911 : syskin 1.44.2.8
1912 :     if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
1913 :     return; // allocate some mem for qpel interpolated blocks
1914 :     // somehow this is dirty since I think we shouldn't use malloc outside
1915 :     // encoder_create() - so please fix me!
1916 :    
1917 : syskin 1.44.2.11 Data.RefQ = qimage;
1918 :    
1919 : chl 1.44.2.1 if (sadInit) (*sadInit) ();
1920 : chl 1.32
1921 : chl 1.44.2.1 for (y = 0; y < pParam->mb_height; y++) {
1922 :     for (x = 0; x < pParam->mb_width; x++) {
1923 : suxen_drol 1.6
1924 : chl 1.44.2.1 MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
1925 : suxen_drol 1.6
1926 : chl 1.44.2.1 //intra mode is copied from the first pass. At least for the time being
1927 :     if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue;
1928 : chl 1.37
1929 : chl 1.44.2.1 if (!(current->global_flags & XVID_LUMIMASKING)) {
1930 :     pMB->dquant = NO_CHANGE;
1931 :     pMB->quant = current->quant; }
1932 : Isibaar 1.44.2.12 else {
1933 : syskin 1.44.2.5 if (pMB->dquant != NO_CHANGE) {
1934 :     quant += DQtab[pMB->dquant];
1935 :     if (quant > 31) quant = 31;
1936 :     else if (quant < 1) quant = 1;
1937 :     }
1938 : Isibaar 1.44.2.12 pMB->quant = quant;
1939 :     }
1940 : chl 1.33
1941 : syskin 1.44.2.14 SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1942 : Isibaar 1.44.2.2 y, current->motion_flags, pMB->quant,
1943 : h 1.44.2.3 pParam, pMBs, current->global_flags & XVID_INTER4V, pMB,
1944 :     &Data);
1945 : edgomez 1.20
1946 : Isibaar 1.44.2.2 }
1947 :     }
1948 : syskin 1.44.2.8 free(qimage);
1949 : Isibaar 1.44.2.2 }
1950 : chl 1.33
1951 : Isibaar 1.44.2.2 static __inline int
1952 :     MEanalyzeMB ( const uint8_t * const pRef,
1953 :     const uint8_t * const pCur,
1954 :     const int x,
1955 :     const int y,
1956 :     const MBParam * const pParam,
1957 :     const MACROBLOCK * const pMBs,
1958 : h 1.44.2.3 MACROBLOCK * const pMB,
1959 :     SearchData * const Data)
1960 : Isibaar 1.44.2.2 {
1961 : chl 1.32
1962 : Isibaar 1.44.2.12 int i = 255, mask;
1963 : h 1.44.2.3 VECTOR pmv[3];
1964 : suxen_drol 1.22
1965 : h 1.44.2.3 *(Data->iMinSAD) = MV_MAX_ERROR;
1966 :     Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1967 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1968 : Isibaar 1.44.2.7 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
1969 : chl 1.35
1970 : h 1.44.2.3 Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
1971 :     Data->Ref = pRef + (x + y * pParam->edged_width) * 16;
1972 : Isibaar 1.44.2.2
1973 :     CheckCandidate = CheckCandidate16no4vI;
1974 : chl 1.44.2.1
1975 : Isibaar 1.44.2.2 pmv[1].x = EVEN(pMB->mvs[0].x);
1976 :     pmv[1].y = EVEN(pMB->mvs[0].y);
1977 : h 1.44.2.3 pmv[0].x = EVEN(Data->predMV.x);
1978 :     pmv[0].y = EVEN(Data->predMV.y);
1979 : Isibaar 1.44.2.2 pmv[2].x = pmv[2].y = 0;
1980 :    
1981 : h 1.44.2.3 CheckCandidate16no4vI(pmv[0].x, pmv[0].y, 255, &i, Data);
1982 : Isibaar 1.44.2.2 if (!(mask = make_mask(pmv, 1)))
1983 : h 1.44.2.3 CheckCandidate16no4vI(pmv[1].x, pmv[1].y, mask, &i, Data);
1984 : Isibaar 1.44.2.2 if (!(mask = make_mask(pmv, 2)))
1985 : h 1.44.2.3 CheckCandidate16no4vI(0, 0, mask, &i, Data);
1986 : Isibaar 1.44.2.2
1987 : Isibaar 1.44.2.12 DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
1988 : Isibaar 1.44.2.2
1989 : Isibaar 1.44.2.12 pMB->mvs[0] = *Data->currentMV;
1990 : syskin 1.44.2.14 pMB->mode = MODE_INTER;
1991 : Isibaar 1.44.2.2
1992 : h 1.44.2.3 return *(Data->iMinSAD);
1993 : Isibaar 1.44.2.2 }
1994 :    
1995 :     #define INTRA_THRESH 1350
1996 :     #define INTER_THRESH 900
1997 :    
1998 :     int
1999 :     MEanalysis( const IMAGE * const pRef,
2000 :     const IMAGE * const pCurrent,
2001 :     MBParam * const pParam,
2002 :     MACROBLOCK * const pMBs,
2003 :     const uint32_t iFcode)
2004 :     {
2005 :     uint32_t x, y, intra = 0;
2006 :     int sSAD = 0;
2007 : h 1.44.2.3
2008 :     VECTOR currentMV;
2009 :     int32_t iMinSAD;
2010 :     SearchData Data;
2011 :     Data.iEdgedWidth = pParam->edged_width;
2012 :     Data.currentMV = &currentMV;
2013 :     Data.iMinSAD = &iMinSAD;
2014 :     Data.iFcode = iFcode;
2015 :    
2016 : Isibaar 1.44.2.2 if (sadInit) (*sadInit) ();
2017 :    
2018 : syskin 1.44.2.11 for (y = 1; y < pParam->mb_height-1; y++) {
2019 :     for (x = 1; x < pParam->mb_width-1; x++) {
2020 : Isibaar 1.44.2.2 int sad, dev;
2021 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
2022 :    
2023 :     sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y,
2024 : h 1.44.2.3 pParam, pMBs, pMB, &Data);
2025 : Isibaar 1.44.2.2
2026 : syskin 1.44.2.11 if (sad > INTRA_THRESH) {
2027 :     dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,
2028 :     pParam->edged_width);
2029 : syskin 1.44.2.14 if (dev + INTRA_THRESH < sad) { intra++; pMB->mode = MODE_INTRA; }
2030 : syskin 1.44.2.11 if (intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame
2031 : Isibaar 1.44.2.2 }
2032 : syskin 1.44.2.11 sSAD += sad;
2033 : suxen_drol 1.6 }
2034 :     }
2035 : Isibaar 1.44.2.2 sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);
2036 :     if (sSAD > INTER_THRESH ) return 1; //P frame
2037 :     emms();
2038 :     return 0; // B frame
2039 : chl 1.44.2.1
2040 : syskin 1.44.2.6 }
2041 :    
2042 :     int
2043 :     FindFcode( const MBParam * const pParam,
2044 :     const FRAMEINFO * const current)
2045 :     {
2046 :     uint32_t x, y;
2047 :     int max = 0, min = 0, i;
2048 :    
2049 :     for (y = 0; y < pParam->mb_height; y++) {
2050 :     for (x = 0; x < pParam->mb_width; x++) {
2051 :    
2052 :     MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
2053 :     for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4:1); i++) {
2054 :     if (pMB->mvs[i].x > max) max = pMB->mvs[i].x;
2055 :     if (pMB->mvs[i].y > max) max = pMB->mvs[i].y;
2056 :    
2057 :     if (pMB->mvs[i].x < min) min = pMB->mvs[i].x;
2058 :     if (pMB->mvs[i].y < min) min = pMB->mvs[i].y;
2059 :     }
2060 :     }
2061 :     }
2062 :    
2063 :     min = -min;
2064 :     max += 1;
2065 :     if (min > max) max = min;
2066 : Isibaar 1.44.2.12 if (pParam->m_quarterpel) max *= 2;
2067 : syskin 1.44.2.6
2068 :     for (i = 1; (max > 32 << (i - 1)); i++);
2069 :     return i;
2070 : chl 1.44.2.13 }
2071 :    
2072 : syskin 1.44.2.14 static void
2073 :     CheckGMC(int x, int y, const int dir, int * iDirection,
2074 :     const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC,
2075 :     const MBParam * const pParam)
2076 :     {
2077 :     uint32_t mx, my, a, count = 0;
2078 :    
2079 :     for (my = 1; my < pParam->mb_height-1; my++)
2080 :     for (mx = 1; mx < pParam->mb_width-1; mx++) {
2081 :     VECTOR mv;
2082 :     const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];
2083 :     if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue;
2084 :     mv = pMB->mvs[0];
2085 :     a = ABS(mv.x - x) + ABS(mv.y - y);
2086 :     if (a < 6) count += 6 - a;
2087 :     }
2088 :    
2089 :     if (count > *bestcount) {
2090 :     *bestcount = count;
2091 :     *iDirection = dir;
2092 :     GMC->x = x; GMC->y = y;
2093 :     }
2094 :     }
2095 : chl 1.44.2.13
2096 :    
2097 :     static VECTOR
2098 :     GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode)
2099 :     {
2100 :    
2101 : syskin 1.44.2.14 uint32_t count, bestcount = 0;
2102 : chl 1.44.2.13 int x, y;
2103 : syskin 1.44.2.14 VECTOR gmc = {0,0};
2104 : chl 1.44.2.13 int step, min_x, max_x, min_y, max_y;
2105 :     uint32_t mx, my;
2106 : syskin 1.44.2.14 int iDirection, bDirection;
2107 : chl 1.44.2.13
2108 :     min_x = min_y = -32<<iFcode;
2109 :     max_x = max_y = 32<<iFcode;
2110 :    
2111 : syskin 1.44.2.14 //step1: let's find a rough camera panning
2112 : chl 1.44.2.13 for (step = 32; step >= 2; step /= 2) {
2113 :     bestcount = 0;
2114 :     for (y = min_y; y <= max_y; y += step)
2115 :     for (x = min_x ; x <= max_x; x += step) {
2116 :     count = 0;
2117 :     //for all macroblocks
2118 :     for (my = 1; my < pParam->mb_height-1; my++)
2119 :     for (mx = 1; mx < pParam->mb_width-1; mx++) {
2120 :     const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];
2121 :     VECTOR mv;
2122 :    
2123 :     if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)
2124 :     continue;
2125 :    
2126 :     mv = pMB->mvs[0];
2127 :     if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step ) /* GMC translation is always halfpel-res */
2128 :     count++;
2129 :     }
2130 :     if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; }
2131 :     }
2132 :     min_x = gmc.x - step;
2133 :     max_x = gmc.x + step;
2134 :     min_y = gmc.y - step;
2135 :     max_y = gmc.y + step;
2136 :    
2137 :     }
2138 : syskin 1.44.2.14
2139 :     if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10)
2140 :     gmc.x = gmc.y = 0; //no camara pan, no GMC
2141 :    
2142 :     // step2: let's refine camera panning using gradiend-descent approach.
2143 :     // TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond)
2144 :     bestcount = 0;
2145 :     CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam);
2146 :     do {
2147 :     x = gmc.x; y = gmc.y;
2148 :     bDirection = iDirection; iDirection = 0;
2149 :     if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2150 :     if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2151 :     if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam);
2152 :     if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2153 :    
2154 :     } while (iDirection);
2155 :    
2156 : chl 1.44.2.13 if (pParam->m_quarterpel) {
2157 :     gmc.x *= 2;
2158 :     gmc.y *= 2; /* we store the halfpel value as pseudo-qpel to make comparison easier */
2159 :     }
2160 :    
2161 :     return gmc;
2162 : Isibaar 1.44.2.2 }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4