[svn] / branches / dev-api-4 / xvidcore / src / motion / estimation_pvop.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/motion/estimation_pvop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1195 - (view) (download)

1 : edgomez 1142 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Motion Estimation for P- and S- VOPs -
5 :     *
6 :     * Copyright(C) 2002 Christoph Lampert <gruel@web.de>
7 :     * 2002 Michael Militzer <michael@xvid.org>
8 :     * 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net>
9 :     *
10 :     * This program is free software ; you can redistribute it and/or modify
11 :     * it under the terms of the GNU General Public License as published by
12 :     * the Free Software Foundation ; either version 2 of the License, or
13 :     * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
17 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 :     * GNU General Public License for more details.
19 :     *
20 :     * You should have received a copy of the GNU General Public License
21 :     * along with this program ; if not, write to the Free Software
22 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : edgomez 1195 * $Id: estimation_pvop.c,v 1.1.2.6 2003-10-29 12:41:41 edgomez Exp $
25 : edgomez 1142 *
26 :     ****************************************************************************/
27 :    
28 :     #include <assert.h>
29 :     #include <stdio.h>
30 :     #include <stdlib.h>
31 :     #include <string.h> /* memcpy */
32 :    
33 :     #include "../encoder.h"
34 :     #include "../prediction/mbprediction.h"
35 :     #include "../global.h"
36 :     #include "../utils/timer.h"
37 :     #include "../image/interpolate8x8.h"
38 :     #include "estimation.h"
39 :     #include "motion.h"
40 :     #include "sad.h"
41 :     #include "motion_inlines.h"
42 :    
43 :     static const int xvid_me_lambda_vec8[32] =
44 :     { 0 ,(int)(1.00235 * NEIGH_TEND_8X8 + 0.5),
45 :     (int)(1.15582 + NEIGH_TEND_8X8 + 0.5), (int)(1.31976*NEIGH_TEND_8X8 + 0.5),
46 :     (int)(1.49591*NEIGH_TEND_8X8 + 0.5), (int)(1.68601*NEIGH_TEND_8X8 + 0.5),
47 :     (int)(1.89187*NEIGH_TEND_8X8 + 0.5), (int)(2.11542*NEIGH_TEND_8X8 + 0.5),
48 :     (int)(2.35878*NEIGH_TEND_8X8 + 0.5), (int)(2.62429*NEIGH_TEND_8X8 + 0.5),
49 :     (int)(2.91455*NEIGH_TEND_8X8 + 0.5), (int)(3.23253*NEIGH_TEND_8X8 + 0.5),
50 :     (int)(3.58158*NEIGH_TEND_8X8 + 0.5), (int)(3.96555*NEIGH_TEND_8X8 + 0.5),
51 :     (int)(4.38887*NEIGH_TEND_8X8 + 0.5), (int)(4.85673*NEIGH_TEND_8X8 + 0.5),
52 :     (int)(5.37519*NEIGH_TEND_8X8 + 0.5), (int)(5.95144*NEIGH_TEND_8X8 + 0.5),
53 :     (int)(6.59408*NEIGH_TEND_8X8 + 0.5), (int)(7.31349*NEIGH_TEND_8X8 + 0.5),
54 :     (int)(8.12242*NEIGH_TEND_8X8 + 0.5), (int)(9.03669*NEIGH_TEND_8X8 + 0.5),
55 :     (int)(10.0763*NEIGH_TEND_8X8 + 0.5), (int)(11.2669*NEIGH_TEND_8X8 + 0.5),
56 :     (int)(12.6426*NEIGH_TEND_8X8 + 0.5), (int)(14.2493*NEIGH_TEND_8X8 + 0.5),
57 :     (int)(16.1512*NEIGH_TEND_8X8 + 0.5), (int)(18.442*NEIGH_TEND_8X8 + 0.5),
58 :     (int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5),
59 :     (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5)
60 :     };
61 :    
62 :     static void
63 :     CheckCandidate16(const int x, const int y, const SearchData * const data, const unsigned int Direction)
64 :     {
65 :     const uint8_t * Reference;
66 :     int32_t sad; uint32_t t;
67 :    
68 :     if ( (x > data->max_dx) || (x < data->min_dx)
69 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
70 :    
71 : syskin 1163 Reference = GetReference(x, y, data);
72 : edgomez 1142
73 :     sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);
74 : syskin 1163 t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel, 0);
75 : edgomez 1142
76 :     sad += (data->lambda16 * t * sad)>>10;
77 :     data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;
78 :    
79 : syskin 1163 if (data->chroma) {
80 :     if (sad >= data->iMinSAD[0]) goto no16;
81 :     sad += xvid_me_ChromaSAD((x >> 1) + roundtab_79[x & 0x3],
82 :     (y >> 1) + roundtab_79[y & 0x3], data);
83 :     }
84 : edgomez 1142
85 :     if (sad < data->iMinSAD[0]) {
86 :     data->iMinSAD[0] = sad;
87 : syskin 1163 data->currentMV[0].x = x; data->currentMV[0].y = y;
88 : edgomez 1142 *data->dir = Direction;
89 :     }
90 :    
91 : syskin 1163 no16:
92 : edgomez 1142 if (data->temp[0] < data->iMinSAD[1]) {
93 : syskin 1163 data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
94 : edgomez 1142 if (data->temp[1] < data->iMinSAD[2]) {
95 : syskin 1163 data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
96 : edgomez 1142 if (data->temp[2] < data->iMinSAD[3]) {
97 : syskin 1163 data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
98 : edgomez 1142 if (data->temp[3] < data->iMinSAD[4]) {
99 : syskin 1163 data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
100 : edgomez 1142 }
101 :    
102 :     static void
103 : syskin 1163 CheckCandidate16_qpel(const int x, const int y, const SearchData * const data, const unsigned int Direction)
104 : edgomez 1142 {
105 :     const uint8_t *Reference;
106 :     int32_t sad; uint32_t t;
107 :    
108 :     if ( (x > data->max_dx) || (x < data->min_dx)
109 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
110 :    
111 : syskin 1163 Reference = xvid_me_interpolate16x16qpel(x, y, 0, data);
112 : edgomez 1142
113 :     sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);
114 : syskin 1163 t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 0);
115 : edgomez 1142
116 :     sad += (data->lambda16 * t * sad)>>10;
117 :     data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;
118 :    
119 : syskin 1163 if (data->chroma && (sad < data->iMinSAD[0] || sad < data->iMinSAD2[0]) )
120 :     sad += xvid_me_ChromaSAD(((x/2) >> 1) + roundtab_79[(x/2) & 0x3],
121 :     ((y/2) >> 1) + roundtab_79[(y/2) & 0x3], data);
122 : edgomez 1142
123 :     if (data->temp[0] < data->iMinSAD[1]) {
124 : syskin 1163 data->iMinSAD[1] = data->temp[0]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; }
125 : edgomez 1142 if (data->temp[1] < data->iMinSAD[2]) {
126 : syskin 1163 data->iMinSAD[2] = data->temp[1]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; }
127 : edgomez 1142 if (data->temp[2] < data->iMinSAD[3]) {
128 : syskin 1163 data->iMinSAD[3] = data->temp[2]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; }
129 : edgomez 1142 if (data->temp[3] < data->iMinSAD[4]) {
130 : syskin 1163 data->iMinSAD[4] = data->temp[3]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; }
131 : edgomez 1142
132 :     if (sad < data->iMinSAD[0]) {
133 :     *(data->iMinSAD2) = *(data->iMinSAD);
134 : syskin 1163 data->currentQMV2->x = data->currentQMV->x;
135 :     data->currentQMV2->y = data->currentQMV->y;
136 : edgomez 1142
137 :     data->iMinSAD[0] = sad;
138 : syskin 1163 data->currentQMV[0].x = x; data->currentQMV[0].y = y;
139 :     } else if (sad < *(data->iMinSAD2)) {
140 : edgomez 1142 *(data->iMinSAD2) = sad;
141 : syskin 1163 data->currentQMV2->x = x; data->currentQMV2->y = y;
142 : edgomez 1142 }
143 :     }
144 :    
145 :     static void
146 :     CheckCandidate8(const int x, const int y, const SearchData * const data, const unsigned int Direction)
147 :     {
148 :     int32_t sad; uint32_t t;
149 :     const uint8_t * Reference;
150 :     VECTOR * current;
151 :    
152 :     if ( (x > data->max_dx) || (x < data->min_dx)
153 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
154 :    
155 :     if (!data->qpel_precision) {
156 :     Reference = GetReference(x, y, data);
157 :     current = data->currentMV;
158 :     } else { /* x and y are in 1/4 precision */
159 :     Reference = xvid_me_interpolate8x8qpel(x, y, 0, 0, data);
160 :     current = data->currentQMV;
161 :     }
162 :    
163 :     sad = sad8(data->Cur, Reference, data->iEdgedWidth);
164 :     t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
165 :    
166 :     sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10;
167 :    
168 :     if (sad < *(data->iMinSAD)) {
169 :     *(data->iMinSAD) = sad;
170 :     current->x = x; current->y = y;
171 :     *data->dir = Direction;
172 :     }
173 :     }
174 :    
175 :     static void
176 :     CheckCandidate32(const int x, const int y, const SearchData * const data, const unsigned int Direction)
177 :     {
178 :     uint32_t t;
179 :     const uint8_t * Reference;
180 :     int sad;
181 :    
182 :     if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || /* non-zero even value */
183 :     (x > data->max_dx) || (x < data->min_dx)
184 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
185 :    
186 :     Reference = GetReference(x, y, data);
187 :     t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1);
188 :    
189 :     sad = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp);
190 :    
191 :     sad += (data->lambda16 * t * sad) >> 10;
192 :     data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;
193 :    
194 :     if (sad < data->iMinSAD[0]) {
195 :     data->iMinSAD[0] = sad;
196 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
197 :     *data->dir = Direction;
198 :     }
199 :    
200 :     if (data->temp[0] < data->iMinSAD[1]) {
201 :     data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
202 :     if (data->temp[1] < data->iMinSAD[2]) {
203 :     data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
204 :     if (data->temp[2] < data->iMinSAD[3]) {
205 :     data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
206 :     if (data->temp[3] < data->iMinSAD[4]) {
207 :     data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
208 :     }
209 :    
210 :     static void
211 :     SubpelRefine_Fast(SearchData * data, CheckFunc * CheckCandidate)
212 :     {
213 : syskin 1163 /* Do a fast q-pel refinement */
214 : edgomez 1142 VECTOR centerMV;
215 :     VECTOR second_best;
216 :     int best_sad = *data->iMinSAD;
217 :     int xo, yo, xo2, yo2;
218 :     int size = 2;
219 : syskin 1163 *data->iMinSAD2 = 0;
220 : edgomez 1142
221 : syskin 1163 /* check all halfpixel positions near our best halfpel position */
222 :     centerMV = *data->currentQMV;
223 : edgomez 1142 *data->iMinSAD = 256 * 4096;
224 :    
225 :     CHECK_CANDIDATE(centerMV.x, centerMV.y - size, 0);
226 :     CHECK_CANDIDATE(centerMV.x + size, centerMV.y - size, 0);
227 :     CHECK_CANDIDATE(centerMV.x + size, centerMV.y, 0);
228 :     CHECK_CANDIDATE(centerMV.x + size, centerMV.y + size, 0);
229 :    
230 :     CHECK_CANDIDATE(centerMV.x, centerMV.y + size, 0);
231 :     CHECK_CANDIDATE(centerMV.x - size, centerMV.y + size, 0);
232 :     CHECK_CANDIDATE(centerMV.x - size, centerMV.y, 0);
233 :     CHECK_CANDIDATE(centerMV.x - size, centerMV.y - size, 0);
234 :    
235 : syskin 1163 second_best = *data->currentQMV;
236 : edgomez 1142
237 : syskin 1163 /* after second_best has been found, go back to the vector we began with */
238 : edgomez 1142
239 : syskin 1163 data->currentQMV[0] = centerMV;
240 : edgomez 1142 *data->iMinSAD = best_sad;
241 :    
242 :     xo = centerMV.x;
243 :     yo = centerMV.y;
244 :     xo2 = second_best.x;
245 :     yo2 = second_best.y;
246 :    
247 :     *data->iMinSAD2 = 256 * 4096;
248 :    
249 : syskin 1163 if (yo == yo2) {
250 : edgomez 1142 CHECK_CANDIDATE((xo+xo2)>>1, yo, 0);
251 :     CHECK_CANDIDATE(xo, yo-1, 0);
252 :     CHECK_CANDIDATE(xo, yo+1, 0);
253 :    
254 : syskin 1163 if(best_sad <= *data->iMinSAD2) return;
255 : edgomez 1142
256 :     if(data->currentQMV[0].x == data->currentQMV2[0].x) {
257 :     CHECK_CANDIDATE((xo+xo2)>>1, yo-1, 0);
258 :     CHECK_CANDIDATE((xo+xo2)>>1, yo+1, 0);
259 : syskin 1163 } else {
260 : edgomez 1142 CHECK_CANDIDATE((xo+xo2)>>1,
261 : syskin 1163 (data->currentQMV[0].x == xo) ? data->currentQMV[0].y : data->currentQMV2[0].y, 0);
262 : edgomez 1142 }
263 : syskin 1163 return;
264 : edgomez 1142 }
265 :    
266 : syskin 1163 if (xo == xo2) {
267 : edgomez 1142 CHECK_CANDIDATE(xo, (yo+yo2)>>1, 0);
268 :     CHECK_CANDIDATE(xo-1, yo, 0);
269 :     CHECK_CANDIDATE(xo+1, yo, 0);
270 :    
271 : syskin 1163 if(best_sad < *data->iMinSAD2) return;
272 : edgomez 1142
273 :     if(data->currentQMV[0].y == data->currentQMV2[0].y) {
274 :     CHECK_CANDIDATE(xo-1, (yo+yo2)>>1, 0);
275 :     CHECK_CANDIDATE(xo+1, (yo+yo2)>>1, 0);
276 : syskin 1163 } else {
277 : edgomez 1142 CHECK_CANDIDATE((data->currentQMV[0].y == yo) ? data->currentQMV[0].x : data->currentQMV2[0].x, (yo+yo2)>>1, 0);
278 :     }
279 : syskin 1163 return;
280 : edgomez 1142 }
281 :    
282 :     CHECK_CANDIDATE(xo, (yo+yo2)>>1, 0);
283 :     CHECK_CANDIDATE((xo+xo2)>>1, yo, 0);
284 :    
285 : syskin 1163 if(best_sad <= *data->iMinSAD2) return;
286 : edgomez 1142
287 :     CHECK_CANDIDATE((xo+xo2)>>1, (yo+yo2)>>1, 0);
288 :     }
289 :    
290 :     int
291 :     xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference,
292 :     const int x, const int y,
293 :     const uint32_t stride, const uint32_t iQuant, int rrv)
294 :     {
295 :     int offset = (x + y*stride)*8;
296 :     if(!rrv) {
297 :     uint32_t sadC = sad8(current->u + offset,
298 :     reference->u + offset, stride);
299 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
300 :     sadC += sad8(current->v + offset,
301 :     reference->v + offset, stride);
302 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
303 :     return 1;
304 :    
305 :     } else {
306 :     uint32_t sadC = sad16(current->u + 2*offset,
307 :     reference->u + 2*offset, stride, 256*4096);
308 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
309 :     sadC += sad16(current->v + 2*offset,
310 :     reference->v + 2*offset, stride, 256*4096);
311 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
312 :     return 1;
313 :     }
314 :     }
315 :    
316 :     /*
317 :     * pmv are filled with:
318 :     * [0]: Median (or whatever is correct in a special case)
319 :     * [1]: left neighbour
320 :     * [2]: top neighbour
321 :     * [3]: topright neighbour
322 :     * psad are filled with:
323 :     * [0]: minimum of [1] to [3]
324 :     * [1]: left neighbour's SAD (NB:[1] to [3] are actually not needed)
325 :     * [2]: top neighbour's SAD
326 :     * [3]: topright neighbour's SAD
327 :     */
328 :    
329 : syskin 1163 static __inline void
330 : edgomez 1142 get_pmvdata2(const MACROBLOCK * const mbs,
331 :     const int mb_width,
332 :     const int bound,
333 :     const int x,
334 :     const int y,
335 :     VECTOR * const pmv,
336 :     int32_t * const psad)
337 :     {
338 :     int lx, ly, lz; /* left */
339 :     int tx, ty, tz; /* top */
340 :     int rx, ry, rz; /* top-right */
341 :     int lpos, tpos, rpos;
342 :     int num_cand = 0, last_cand = 1;
343 :    
344 : syskin 1163 lx = x - 1; ly = y; lz = 1;
345 :     tx = x; ty = y - 1; tz = 2;
346 :     rx = x + 1; ry = y - 1; rz = 2;
347 : edgomez 1142
348 :     lpos = lx + ly * mb_width;
349 :     rpos = rx + ry * mb_width;
350 :     tpos = tx + ty * mb_width;
351 :    
352 :     if (lpos >= bound && lx >= 0) {
353 :     num_cand++;
354 :     last_cand = 1;
355 :     pmv[1] = mbs[lpos].mvs[lz];
356 :     psad[1] = mbs[lpos].sad8[lz];
357 :     } else {
358 :     pmv[1] = zeroMV;
359 :     psad[1] = MV_MAX_ERROR;
360 :     }
361 :    
362 :     if (tpos >= bound) {
363 :     num_cand++;
364 :     last_cand = 2;
365 :     pmv[2]= mbs[tpos].mvs[tz];
366 :     psad[2] = mbs[tpos].sad8[tz];
367 :     } else {
368 :     pmv[2] = zeroMV;
369 :     psad[2] = MV_MAX_ERROR;
370 :     }
371 :    
372 :     if (rpos >= bound && rx < mb_width) {
373 :     num_cand++;
374 :     last_cand = 3;
375 :     pmv[3] = mbs[rpos].mvs[rz];
376 :     psad[3] = mbs[rpos].sad8[rz];
377 :     } else {
378 :     pmv[3] = zeroMV;
379 :     psad[3] = MV_MAX_ERROR;
380 :     }
381 :    
382 :     /* original pmvdata() compatibility hack */
383 : syskin 1163 if (x == 0 && y == 0) {
384 : edgomez 1142 pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;
385 :     psad[0] = 0;
386 :     psad[1] = psad[2] = psad[3] = MV_MAX_ERROR;
387 : syskin 1185 return;
388 : edgomez 1142 }
389 :    
390 :     /* if only one valid candidate preictor, the invalid candiates are set to the canidate */
391 :     if (num_cand == 1) {
392 :     pmv[0] = pmv[last_cand];
393 :     psad[0] = psad[last_cand];
394 : syskin 1185 return;
395 : edgomez 1142 }
396 :    
397 :     if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) {
398 :     pmv[0] = pmv[1];
399 :     psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
400 : syskin 1185 return;
401 : edgomez 1142 }
402 :    
403 :     /* set median, minimum */
404 :    
405 :     pmv[0].x =
406 :     MIN(MAX(pmv[1].x, pmv[2].x),
407 :     MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
408 :     pmv[0].y =
409 :     MIN(MAX(pmv[1].y, pmv[2].y),
410 :     MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
411 :    
412 :     psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
413 :    
414 :     }
415 :    
416 :    
417 :     static void
418 :     ModeDecision_SAD(SearchData * const Data,
419 :     MACROBLOCK * const pMB,
420 :     const MACROBLOCK * const pMBs,
421 :     const int x, const int y,
422 :     const MBParam * const pParam,
423 :     const uint32_t MotionFlags,
424 :     const uint32_t VopFlags,
425 :     const uint32_t VolFlags,
426 :     const IMAGE * const pCurrent,
427 :     const IMAGE * const pRef,
428 :     const IMAGE * const vGMC,
429 :     const int coding_type)
430 :     {
431 :     int mode = MODE_INTER;
432 :     int mcsel = 0;
433 :     int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
434 :     const uint32_t iQuant = pMB->quant;
435 :    
436 :     const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0);
437 :    
438 :     int sad;
439 :     int InterBias = MV16_INTER_BIAS;
440 :    
441 :     pMB->mcsel = 0;
442 :    
443 :     if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
444 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {
445 :     mode = MODE_INTER;
446 :     sad = Data->iMinSAD[0];
447 :     } else {
448 :     mode = MODE_INTER4V;
449 :     sad = Data->iMinSAD[1] + Data->iMinSAD[2] +
450 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant;
451 :     Data->iMinSAD[0] = sad;
452 :     }
453 :    
454 :     /* final skip decision, a.k.a. "the vector you found, really that good?" */
455 :     if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP))
456 :     if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)
457 :     if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {
458 :     mode = MODE_NOT_CODED;
459 :     sad = 0;
460 :     }
461 :    
462 :     /* mcsel */
463 :     if (coding_type == S_VOP) {
464 :    
465 :     int32_t iSAD = sad16(Data->Cur,
466 :     vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536);
467 :    
468 :     if (Data->chroma) {
469 :     iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
470 :     iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
471 :     }
472 :    
473 :     if (iSAD <= sad) { /* mode decision GMC */
474 :     mode = MODE_INTER;
475 :     mcsel = 1;
476 :     sad = iSAD;
477 :     }
478 :     }
479 :    
480 :     /* intra decision */
481 :    
482 :     if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */
483 :     if (y != 0)
484 :     if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
485 :     if (x != 0)
486 :     if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
487 :    
488 :     if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */
489 :     if (Data->rrv) InterBias *= 4;
490 :    
491 :     if (InterBias < sad) {
492 :     int32_t deviation;
493 :     if (!Data->rrv)
494 :     deviation = dev16(Data->Cur, Data->iEdgedWidth);
495 :     else
496 :     deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */
497 :     dev16(Data->Cur+16, Data->iEdgedWidth) +
498 :     dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) +
499 :     dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth);
500 :    
501 :     if (deviation < (sad - InterBias)) mode = MODE_INTRA;
502 :     }
503 :    
504 :     pMB->cbp = 63;
505 :     pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
506 :    
507 :     if (Data->rrv) {
508 :     Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);
509 :     Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);
510 :     }
511 :    
512 :     if (mode == MODE_INTER && mcsel == 0) {
513 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
514 :    
515 :     if(Data->qpel) {
516 :     pMB->qmvs[0] = pMB->qmvs[1]
517 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
518 :     pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
519 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
520 :     } else {
521 :     pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
522 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
523 :     }
524 :    
525 : edgomez 1160 } else if (mode == MODE_INTER ) { /* but mcsel == 1 */
526 : edgomez 1142
527 :     pMB->mcsel = 1;
528 :     if (Data->qpel) {
529 :     pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
530 :     pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2;
531 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2;
532 :     } else
533 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
534 :    
535 :     } else
536 :     if (mode == MODE_INTER4V) ; /* anything here? */
537 :     else /* INTRA, NOT_CODED */
538 :     ZeroMacroblockP(pMB, 0);
539 :    
540 :     pMB->mode = mode;
541 :     }
542 :    
543 :     static __inline void
544 :     PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,
545 :     int iHcount, const MACROBLOCK * const prevMB, int rrv)
546 :     {
547 :     /* this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself */
548 :     if (rrv) { iWcount /= 2; iHcount /= 2; }
549 :    
550 :     if ( (y != 0) && (x < (iWcount-1)) ) { /* [5] top-right neighbour */
551 :     pmv[5].x = EVEN(pmv[3].x);
552 :     pmv[5].y = EVEN(pmv[3].y);
553 :     } else pmv[5].x = pmv[5].y = 0;
554 :    
555 :     if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }/* pmv[3] is left neighbour */
556 :     else pmv[3].x = pmv[3].y = 0;
557 :    
558 :     if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }/* [4] top neighbour */
559 :     else pmv[4].x = pmv[4].y = 0;
560 :    
561 :     /* [1] median prediction */
562 :     pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);
563 :    
564 :     pmv[0].x = pmv[0].y = 0; /* [0] is zero; not used in the loop (checked before) but needed here for make_mask */
565 :    
566 :     pmv[2].x = EVEN(prevMB->mvs[0].x); /* [2] is last frame */
567 :     pmv[2].y = EVEN(prevMB->mvs[0].y);
568 :    
569 :     if ((x < iWcount-1) && (y < iHcount-1)) {
570 :     pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */
571 :     pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
572 :     } else pmv[6].x = pmv[6].y = 0;
573 :    
574 :     if (rrv) {
575 :     int i;
576 :     for (i = 0; i < 7; i++) {
577 :     pmv[i].x = RRV_MV_SCALEUP(pmv[i].x);
578 :     pmv[i].y = RRV_MV_SCALEUP(pmv[i].y);
579 :     }
580 :     }
581 :     }
582 :    
583 :     static void
584 :     Search8(const SearchData * const OldData,
585 :     const int x, const int y,
586 :     const uint32_t MotionFlags,
587 :     const MBParam * const pParam,
588 :     MACROBLOCK * const pMB,
589 :     const MACROBLOCK * const pMBs,
590 :     const int block,
591 :     SearchData * const Data)
592 :     {
593 :     int i = 0;
594 :     CheckFunc * CheckCandidate;
595 :     Data->iMinSAD = OldData->iMinSAD + 1 + block;
596 :     Data->currentMV = OldData->currentMV + 1 + block;
597 :     Data->currentQMV = OldData->currentQMV + 1 + block;
598 :    
599 :     if(Data->qpel) {
600 :     Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);
601 :     if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y,
602 :     Data->predMV, Data->iFcode, 0, 0);
603 :     } else {
604 :     Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);
605 :     if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y,
606 :     Data->predMV, Data->iFcode, 0, Data->rrv);
607 :     }
608 :    
609 :     *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;
610 :    
611 :     if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
612 :    
613 :     if (Data->rrv) i = 16; else i = 8;
614 :    
615 :     Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
616 :     Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
617 :     Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
618 :     Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
619 :    
620 :     Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));
621 :     Data->qpel_precision = 0;
622 :    
623 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
624 :     pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
625 :    
626 :     if (!Data->rrv) CheckCandidate = CheckCandidate8;
627 :     else CheckCandidate = CheckCandidate16no4v;
628 :    
629 :     if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) {
630 :     int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */
631 :    
632 :     MainSearchFunc *MainSearchPtr;
633 :     if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch;
634 :     else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch;
635 :     else MainSearchPtr = xvid_me_DiamondSearch;
636 :    
637 :     MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate);
638 :    
639 :     if(*(Data->iMinSAD) < temp_sad) {
640 :     Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */
641 :     Data->currentQMV->y = 2 * Data->currentMV->y;
642 :     }
643 :     }
644 :    
645 :     if (MotionFlags & XVID_ME_HALFPELREFINE8) {
646 :     int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */
647 :    
648 :     xvid_me_SubpelRefine(Data, CheckCandidate); /* perform halfpel refine of current best vector */
649 :    
650 :     if(*(Data->iMinSAD) < temp_sad) { /* we have found a better match */
651 :     Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */
652 :     Data->currentQMV->y = 2 * Data->currentMV->y;
653 :     }
654 :     }
655 :    
656 :     if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {
657 :     Data->qpel_precision = 1;
658 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
659 :     pParam->width, pParam->height, Data->iFcode, 2, 0);
660 :     xvid_me_SubpelRefine(Data, CheckCandidate);
661 :     }
662 :     }
663 :    
664 :     if (Data->rrv) {
665 :     Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x);
666 :     Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y);
667 :     }
668 :    
669 :     if(Data->qpel) {
670 :     pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;
671 :     pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;
672 :     pMB->qmvs[block] = *Data->currentQMV;
673 :     } else {
674 :     pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
675 :     pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
676 :     }
677 :    
678 :     pMB->mvs[block] = *Data->currentMV;
679 :     pMB->sad8[block] = 4 * *Data->iMinSAD;
680 :     }
681 :    
682 :    
683 :    
684 :     static void
685 :     SearchP(const IMAGE * const pRef,
686 :     const uint8_t * const pRefH,
687 :     const uint8_t * const pRefV,
688 :     const uint8_t * const pRefHV,
689 :     const IMAGE * const pCur,
690 :     const int x,
691 :     const int y,
692 :     const uint32_t MotionFlags,
693 :     const uint32_t VopFlags,
694 :     SearchData * const Data,
695 :     const MBParam * const pParam,
696 :     const MACROBLOCK * const pMBs,
697 :     const MACROBLOCK * const prevMBs,
698 :     MACROBLOCK * const pMB)
699 :     {
700 :    
701 :     int i, threshA;
702 :     VECTOR pmv[7];
703 :     int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
704 :     CheckFunc * CheckCandidate;
705 :    
706 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
707 :     pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
708 :    
709 : syskin 1163 get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, pmv, Data->temp);
710 : edgomez 1142
711 :     Data->temp[5] = Data->temp[6] = 0; /* chroma-sad cache */
712 :     i = Data->rrv ? 2 : 1;
713 :     Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;
714 :     Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
715 :     Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
716 :    
717 :     Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i;
718 :     Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i;
719 :     Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i;
720 :     Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i;
721 :     Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
722 :     Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
723 :    
724 :     Data->lambda16 = xvid_me_lambda_vec16[pMB->quant];
725 :     Data->lambda8 = xvid_me_lambda_vec8[pMB->quant];
726 :     Data->qpel_precision = 0;
727 :     *Data->dir = 0;
728 :    
729 :     memset(Data->currentMV, 0, 5*sizeof(VECTOR));
730 :    
731 :     if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
732 :     else Data->predMV = pmv[0];
733 :    
734 :     i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0);
735 :     Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10);
736 :     Data->iMinSAD[1] = pMB->sad8[0] + ((Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS)) >> 10);
737 :     Data->iMinSAD[2] = pMB->sad8[1];
738 :     Data->iMinSAD[3] = pMB->sad8[2];
739 :     Data->iMinSAD[4] = pMB->sad8[3];
740 :    
741 :     if ((!(VopFlags & XVID_VOP_MODEDECISION_RD)) && (x | y)) {
742 :     threshA = Data->temp[0]; /* that's where we keep this SAD atm */
743 :     if (threshA < 512) threshA = 512;
744 :     else if (threshA > 1024) threshA = 1024;
745 :     } else
746 :     threshA = 512;
747 :    
748 :     PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
749 :     prevMBs + x + y * pParam->mb_width, Data->rrv);
750 :    
751 :     if (!Data->rrv) {
752 :     if (inter4v) CheckCandidate = CheckCandidate16;
753 :     else CheckCandidate = CheckCandidate16no4v; /* for extra speed */
754 :     } else CheckCandidate = CheckCandidate32;
755 :    
756 :     /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/
757 :    
758 :     for (i = 1; i < 7; i++)
759 :     if (!vector_repeats(pmv, i)) {
760 :     CheckCandidate(pmv[i].x, pmv[i].y, Data, i);
761 :     if (Data->iMinSAD[0] <= threshA) { i++; break; }
762 :     }
763 :    
764 :     if ((Data->iMinSAD[0] <= threshA) ||
765 :     (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
766 :     (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16)))
767 :     inter4v = 0;
768 :     else {
769 :    
770 :     MainSearchFunc * MainSearchPtr;
771 : edgomez 1160 int mask = make_mask(pmv, i, *Data->dir); /* all vectors pmv[0..i-1] have been checked */
772 : edgomez 1142
773 :     if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
774 :     else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
775 :     else MainSearchPtr = xvid_me_DiamondSearch;
776 :    
777 :     MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate);
778 :    
779 :     /* extended search, diamond starting in 0,0 and in prediction.
780 :     note that this search is/might be done in halfpel positions,
781 :     which makes it more different than the diamond above */
782 :    
783 :     if (MotionFlags & XVID_ME_EXTSEARCH16) {
784 :     int32_t bSAD;
785 :     VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
786 : syskin 1163 if (Data->qpel) {
787 :     startMV.x /= 2;
788 :     startMV.y /= 2;
789 :     } else if (Data->rrv) {
790 : edgomez 1142 startMV.x = RRV_MV_SCALEUP(startMV.x);
791 :     startMV.y = RRV_MV_SCALEUP(startMV.y);
792 :     }
793 :     if (!(MVequal(startMV, backupMV))) {
794 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
795 :    
796 :     CheckCandidate(startMV.x, startMV.y, Data, 255);
797 :     MainSearchPtr(startMV.x, startMV.y, Data, 255, CheckCandidate);
798 :     if (bSAD < Data->iMinSAD[0]) {
799 :     Data->currentMV[0] = backupMV;
800 :     Data->iMinSAD[0] = bSAD; }
801 :     }
802 :    
803 :     backupMV = Data->currentMV[0];
804 :     startMV.x = startMV.y = 1;
805 :     if (!(MVequal(startMV, backupMV))) {
806 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
807 :    
808 :     CheckCandidate(startMV.x, startMV.y, Data, 255);
809 :     MainSearchPtr(startMV.x, startMV.y, Data, 255, CheckCandidate);
810 :     if (bSAD < Data->iMinSAD[0]) {
811 :     Data->currentMV[0] = backupMV;
812 :     Data->iMinSAD[0] = bSAD;
813 :     }
814 :     }
815 :     }
816 :     }
817 :    
818 :     if (MotionFlags & XVID_ME_HALFPELREFINE16)
819 :     xvid_me_SubpelRefine(Data, CheckCandidate);
820 :    
821 :     for(i = 0; i < 5; i++) {
822 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */
823 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
824 :     }
825 :    
826 :     if (Data->qpel) {
827 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
828 :     pParam->width, pParam->height, Data->iFcode, 2, 0);
829 :     Data->qpel_precision = 1;
830 :     if (MotionFlags & XVID_ME_QUARTERPELREFINE16) {
831 :     if(MotionFlags & XVID_ME_FASTREFINE16)
832 : syskin 1163 SubpelRefine_Fast(Data, CheckCandidate16_qpel);
833 : edgomez 1142 else
834 : syskin 1163 xvid_me_SubpelRefine(Data, CheckCandidate16_qpel);
835 : edgomez 1142 }
836 :     }
837 :    
838 :     if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30)
839 :     inter4v = 0;
840 :    
841 :     if (inter4v) {
842 :     SearchData Data8;
843 :     memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */
844 :    
845 :     Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
846 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
847 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
848 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
849 :    
850 :     if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) {
851 :     /* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */
852 :     int sumx = 0, sumy = 0;
853 :    
854 :     if (Data->qpel)
855 :     for (i = 1; i < 5; i++) {
856 :     sumx += Data->currentQMV[i].x/2;
857 :     sumy += Data->currentQMV[i].y/2;
858 :     }
859 :     else
860 :     for (i = 1; i < 5; i++) {
861 :     sumx += Data->currentMV[i].x;
862 :     sumy += Data->currentMV[i].y;
863 :     }
864 :    
865 :     Data->iMinSAD[1] += xvid_me_ChromaSAD((sumx >> 3) + roundtab_76[sumx & 0xf],
866 :     (sumy >> 3) + roundtab_76[sumy & 0xf], Data);
867 :     }
868 :     } else Data->iMinSAD[1] = 4096*256;
869 :     }
870 :    
871 :     static __inline uint32_t
872 :     MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags)
873 :     {
874 :     uint32_t Flags = MotionFlags;
875 :    
876 :     if (!(VopFlags & XVID_VOP_MODEDECISION_RD))
877 :     Flags &= ~(XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD+XVID_ME_EXTSEARCH_RD);
878 :    
879 :     if (Flags & XVID_ME_EXTSEARCH_RD)
880 :     Flags |= XVID_ME_HALFPELREFINE16_RD;
881 :    
882 :     if (Flags & XVID_ME_EXTSEARCH_RD && MotionFlags & XVID_ME_EXTSEARCH8)
883 :     Flags |= XVID_ME_HALFPELREFINE8_RD;
884 :    
885 :     if (Flags & XVID_ME_HALFPELREFINE16_RD)
886 :     Flags |= XVID_ME_QUARTERPELREFINE16_RD;
887 :    
888 :     if (Flags & XVID_ME_HALFPELREFINE8_RD) {
889 :     Flags |= XVID_ME_QUARTERPELREFINE8_RD;
890 :     Flags &= ~XVID_ME_HALFPELREFINE8;
891 :     }
892 :    
893 :     if (Flags & XVID_ME_QUARTERPELREFINE8_RD)
894 :     Flags &= ~XVID_ME_QUARTERPELREFINE8;
895 :    
896 :     if (!(VolFlags & XVID_VOL_QUARTERPEL))
897 :     Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD);
898 :    
899 :     if (!(VopFlags & XVID_VOP_HALFPEL))
900 :     Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD);
901 :    
902 :     if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED))
903 :     Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP);
904 :    
905 :     return Flags;
906 :     }
907 :    
908 :     bool
909 :     MotionEstimation(MBParam * const pParam,
910 :     FRAMEINFO * const current,
911 :     FRAMEINFO * const reference,
912 :     const IMAGE * const pRefH,
913 :     const IMAGE * const pRefV,
914 :     const IMAGE * const pRefHV,
915 :     const IMAGE * const pGMC,
916 :     const uint32_t iLimit)
917 :     {
918 :     MACROBLOCK *const pMBs = current->mbs;
919 :     const IMAGE *const pCurrent = &current->image;
920 :     const IMAGE *const pRef = &reference->image;
921 :    
922 :     uint32_t mb_width = pParam->mb_width;
923 :     uint32_t mb_height = pParam->mb_height;
924 :     const uint32_t iEdgedWidth = pParam->edged_width;
925 :     const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags);
926 :     int stat_thresh = 0;
927 :    
928 :     uint32_t x, y;
929 :     uint32_t iIntra = 0;
930 :     int32_t sad00;
931 :     int skip_thresh = INITIAL_SKIP_THRESH * \
932 :     (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \
933 :     (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
934 :    
935 :     /* some pre-initialized thingies for SearchP */
936 :     int32_t temp[8]; uint32_t dir;
937 :     VECTOR currentMV[5];
938 :     VECTOR currentQMV[5];
939 : syskin 1163 VECTOR currentQMV2;
940 : edgomez 1142 int32_t iMinSAD[5];
941 : syskin 1163 int32_t iMinSAD2;
942 : edgomez 1142 DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
943 :     SearchData Data;
944 :     memset(&Data, 0, sizeof(SearchData));
945 :     Data.iEdgedWidth = iEdgedWidth;
946 :     Data.currentMV = currentMV;
947 :     Data.currentQMV = currentQMV;
948 : syskin 1163 Data.currentQMV2 = &currentQMV2;
949 : edgomez 1142 Data.iMinSAD = iMinSAD;
950 : syskin 1163 Data.iMinSAD2 = &iMinSAD2;
951 : edgomez 1142 Data.temp = temp;
952 :     Data.dir = &dir;
953 :     Data.iFcode = current->fcode;
954 :     Data.rounding = pParam->m_rounding_type;
955 :     Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
956 :     Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;
957 :     Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0;
958 :     Data.dctSpace = dct_space;
959 :     Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
960 :    
961 :     if ((current->vop_flags & XVID_VOP_REDUCED)) {
962 :     mb_width = (pParam->width + 31) / 32;
963 :     mb_height = (pParam->height + 31) / 32;
964 :     Data.qpel = 0;
965 :     }
966 :    
967 :     Data.RefQ = pRefV->u; /* a good place, also used in MC (for similar purpose) */
968 :     if (sadInit) (*sadInit) ();
969 :    
970 :     for (y = 0; y < mb_height; y++) {
971 :     for (x = 0; x < mb_width; x++) {
972 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
973 :     MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width];
974 :    
975 :     if (!Data.rrv) pMB->sad16 =
976 :     sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,
977 :     pRef->y + (x + y * iEdgedWidth) * 16,
978 :     pParam->edged_width, pMB->sad8 );
979 :    
980 :     else pMB->sad16 =
981 :     sad32v_c(pCurrent->y + (x + y * iEdgedWidth) * 32,
982 :     pRef->y + (x + y * iEdgedWidth) * 32,
983 :     pParam->edged_width, pMB->sad8 );
984 :    
985 :     if (Data.chroma) {
986 :     Data.temp[7] = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,
987 :     pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)
988 :     + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,
989 :     pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
990 :     pMB->sad16 += Data.temp[7];
991 :     }
992 :    
993 :     sad00 = pMB->sad16;
994 :    
995 :     /* initial skip decision */
996 :     /* no early skip for GMC (global vector = skip vector is unknown!) */
997 :     if (current->coding_type != S_VOP) { /* no fast SKIP for S(GMC)-VOPs */
998 :     if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
999 :     if (Data.chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {
1000 :     ZeroMacroblockP(pMB, sad00);
1001 :     pMB->mode = MODE_NOT_CODED;
1002 :     continue;
1003 :     }
1004 :     }
1005 :    
1006 :     if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) {
1007 :     if(x > 0 && y > 0 && x < pParam->mb_width) {
1008 :     if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], zeroMV) &&
1009 :     MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&
1010 :     MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&
1011 :     MVequal(prevMB->mvs[0], zeroMV)) {
1012 :     stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
1013 :     MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
1014 :     MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
1015 :     prevMB->sad16)));
1016 : edgomez 1195 } else {
1017 : Isibaar 1194 stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
1018 : edgomez 1142 MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
1019 :     MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
1020 :     prevMB->sad16)));
1021 : Isibaar 1194 }
1022 : edgomez 1142 }
1023 :     }
1024 :    
1025 :     /* favorize (0,0) vector for cartoons */
1026 :     if ((current->vop_flags & XVID_VOP_CARTOON) &&
1027 :     ((sad00 < pMB->quant * 4 * skip_thresh) || (sad00 < stat_thresh))) {
1028 :     ZeroMacroblockP(pMB, sad00);
1029 :     continue;
1030 :     }
1031 :    
1032 :     SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1033 :     y, MotionFlags, current->vop_flags,
1034 :     &Data, pParam, pMBs, reference->mbs, pMB);
1035 :    
1036 :     if (current->vop_flags & XVID_VOP_MODEDECISION_RD)
1037 :     xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam,
1038 :     MotionFlags, current->vop_flags, current->vol_flags,
1039 :     pCurrent, pRef, pGMC, current->coding_type);
1040 :    
1041 :     else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD)
1042 :     xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam,
1043 :     MotionFlags, current->vop_flags, current->vol_flags,
1044 :     pCurrent, pRef, pGMC, current->coding_type);
1045 :     else
1046 :     ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam,
1047 :     MotionFlags, current->vop_flags, current->vol_flags,
1048 :     pCurrent, pRef, pGMC, current->coding_type);
1049 :    
1050 :    
1051 :     if (pMB->mode == MODE_INTRA)
1052 :     if (++iIntra > iLimit) return 1;
1053 :     }
1054 :     }
1055 :     return 0;
1056 :     }
1057 :    
1058 :    

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