Parent Directory
|
Revision Log
Revision 1.14 - (view) (download)
1 : | edgomez | 1.2 | /***************************************************************************** |
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 : | Isibaar | 1.14 | * $Id: estimation_pvop.c,v 1.13 2005/03/02 20:22:10 Isibaar Exp $ |
25 : | edgomez | 1.2 | * |
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 : | syskin | 1.7 | (int)(1.15582*NEIGH_TEND_8X8 + 0.5), (int)(1.31976*NEIGH_TEND_8X8 + 0.5), |
46 : | edgomez | 1.2 | (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, SearchData * const data, const unsigned int Direction) | ||
64 : | { | ||
65 : | const uint8_t * Reference; | ||
66 : | syskin | 1.4 | int32_t sad, xc, yc; uint32_t t; |
67 : | VECTOR * current; | ||
68 : | edgomez | 1.2 | |
69 : | if ( (x > data->max_dx) || (x < data->min_dx) | ||
70 : | || (y > data->max_dy) || (y < data->min_dy) ) return; | ||
71 : | |||
72 : | syskin | 1.4 | if (data->qpel_precision) { /* x and y are in 1/4 precision */ |
73 : | Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); | ||
74 : | current = data->currentQMV; | ||
75 : | xc = x/2; yc = y/2; | ||
76 : | } else { | ||
77 : | Reference = GetReference(x, y, data); | ||
78 : | current = data->currentMV; | ||
79 : | xc = x; yc = y; | ||
80 : | } | ||
81 : | edgomez | 1.2 | |
82 : | sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp); | ||
83 : | syskin | 1.9 | t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision); |
84 : | edgomez | 1.2 | |
85 : | sad += (data->lambda16 * t * sad)>>10; | ||
86 : | data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10; | ||
87 : | |||
88 : | if (data->chroma) { | ||
89 : | if (sad >= data->iMinSAD[0]) goto no16; | ||
90 : | syskin | 1.4 | sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
91 : | (yc >> 1) + roundtab_79[yc & 0x3], data); | ||
92 : | edgomez | 1.2 | } |
93 : | |||
94 : | if (sad < data->iMinSAD[0]) { | ||
95 : | data->iMinSAD[0] = sad; | ||
96 : | syskin | 1.4 | current[0].x = x; current[0].y = y; |
97 : | edgomez | 1.2 | data->dir = Direction; |
98 : | } | ||
99 : | |||
100 : | no16: | ||
101 : | if (data->temp[0] < data->iMinSAD[1]) { | ||
102 : | syskin | 1.4 | data->iMinSAD[1] = data->temp[0]; current[1].x = x; current[1].y = y; } |
103 : | edgomez | 1.2 | if (data->temp[1] < data->iMinSAD[2]) { |
104 : | syskin | 1.4 | data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; } |
105 : | edgomez | 1.2 | if (data->temp[2] < data->iMinSAD[3]) { |
106 : | syskin | 1.4 | data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; } |
107 : | edgomez | 1.2 | if (data->temp[3] < data->iMinSAD[4]) { |
108 : | syskin | 1.4 | data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } |
109 : | edgomez | 1.2 | } |
110 : | |||
111 : | static void | ||
112 : | CheckCandidate8(const int x, const int y, SearchData * const data, const unsigned int Direction) | ||
113 : | { | ||
114 : | int32_t sad; uint32_t t; | ||
115 : | const uint8_t * Reference; | ||
116 : | VECTOR * current; | ||
117 : | |||
118 : | if ( (x > data->max_dx) || (x < data->min_dx) | ||
119 : | || (y > data->max_dy) || (y < data->min_dy) ) return; | ||
120 : | |||
121 : | if (!data->qpel_precision) { | ||
122 : | Reference = GetReference(x, y, data); | ||
123 : | current = data->currentMV; | ||
124 : | } else { /* x and y are in 1/4 precision */ | ||
125 : | Reference = xvid_me_interpolate8x8qpel(x, y, 0, 0, data); | ||
126 : | current = data->currentQMV; | ||
127 : | } | ||
128 : | |||
129 : | sad = sad8(data->Cur, Reference, data->iEdgedWidth); | ||
130 : | syskin | 1.9 | t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision); |
131 : | edgomez | 1.2 | |
132 : | sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10; | ||
133 : | |||
134 : | if (sad < *(data->iMinSAD)) { | ||
135 : | *(data->iMinSAD) = sad; | ||
136 : | current->x = x; current->y = y; | ||
137 : | data->dir = Direction; | ||
138 : | } | ||
139 : | } | ||
140 : | |||
141 : | int | ||
142 : | xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference, | ||
143 : | const int x, const int y, | ||
144 : | syskin | 1.9 | const uint32_t stride, const uint32_t iQuant) |
145 : | edgomez | 1.2 | { |
146 : | int offset = (x + y*stride)*8; | ||
147 : | syskin | 1.9 | uint32_t sadC = sad8(current->u + offset, |
148 : | reference->u + offset, stride); | ||
149 : | if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; | ||
150 : | sadC += sad8(current->v + offset, | ||
151 : | reference->v + offset, stride); | ||
152 : | if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; | ||
153 : | return 1; | ||
154 : | edgomez | 1.2 | } |
155 : | |||
156 : | /* | ||
157 : | * pmv are filled with: | ||
158 : | * [0]: Median (or whatever is correct in a special case) | ||
159 : | * [1]: left neighbour | ||
160 : | * [2]: top neighbour | ||
161 : | * [3]: topright neighbour | ||
162 : | * psad are filled with: | ||
163 : | * [0]: minimum of [1] to [3] | ||
164 : | * [1]: left neighbour's SAD (NB:[1] to [3] are actually not needed) | ||
165 : | * [2]: top neighbour's SAD | ||
166 : | * [3]: topright neighbour's SAD | ||
167 : | */ | ||
168 : | |||
169 : | static __inline void | ||
170 : | get_pmvdata2(const MACROBLOCK * const mbs, | ||
171 : | const int mb_width, | ||
172 : | const int bound, | ||
173 : | const int x, | ||
174 : | const int y, | ||
175 : | VECTOR * const pmv, | ||
176 : | int32_t * const psad) | ||
177 : | { | ||
178 : | int lx, ly, lz; /* left */ | ||
179 : | int tx, ty, tz; /* top */ | ||
180 : | int rx, ry, rz; /* top-right */ | ||
181 : | int lpos, tpos, rpos; | ||
182 : | int num_cand = 0, last_cand = 1; | ||
183 : | |||
184 : | lx = x - 1; ly = y; lz = 1; | ||
185 : | tx = x; ty = y - 1; tz = 2; | ||
186 : | rx = x + 1; ry = y - 1; rz = 2; | ||
187 : | |||
188 : | lpos = lx + ly * mb_width; | ||
189 : | rpos = rx + ry * mb_width; | ||
190 : | tpos = tx + ty * mb_width; | ||
191 : | |||
192 : | if (lpos >= bound && lx >= 0) { | ||
193 : | num_cand++; | ||
194 : | last_cand = 1; | ||
195 : | pmv[1] = mbs[lpos].mvs[lz]; | ||
196 : | psad[1] = mbs[lpos].sad8[lz]; | ||
197 : | } else { | ||
198 : | pmv[1] = zeroMV; | ||
199 : | psad[1] = MV_MAX_ERROR; | ||
200 : | } | ||
201 : | |||
202 : | if (tpos >= bound) { | ||
203 : | num_cand++; | ||
204 : | last_cand = 2; | ||
205 : | pmv[2]= mbs[tpos].mvs[tz]; | ||
206 : | psad[2] = mbs[tpos].sad8[tz]; | ||
207 : | } else { | ||
208 : | pmv[2] = zeroMV; | ||
209 : | psad[2] = MV_MAX_ERROR; | ||
210 : | } | ||
211 : | |||
212 : | if (rpos >= bound && rx < mb_width) { | ||
213 : | num_cand++; | ||
214 : | last_cand = 3; | ||
215 : | pmv[3] = mbs[rpos].mvs[rz]; | ||
216 : | psad[3] = mbs[rpos].sad8[rz]; | ||
217 : | } else { | ||
218 : | pmv[3] = zeroMV; | ||
219 : | psad[3] = MV_MAX_ERROR; | ||
220 : | } | ||
221 : | |||
222 : | /* original pmvdata() compatibility hack */ | ||
223 : | if (x == 0 && y == 0) { | ||
224 : | pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV; | ||
225 : | psad[0] = 0; | ||
226 : | psad[1] = psad[2] = psad[3] = MV_MAX_ERROR; | ||
227 : | return; | ||
228 : | } | ||
229 : | |||
230 : | /* if only one valid candidate preictor, the invalid candiates are set to the canidate */ | ||
231 : | if (num_cand == 1) { | ||
232 : | pmv[0] = pmv[last_cand]; | ||
233 : | psad[0] = psad[last_cand]; | ||
234 : | return; | ||
235 : | } | ||
236 : | |||
237 : | if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) { | ||
238 : | pmv[0] = pmv[1]; | ||
239 : | psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]); | ||
240 : | return; | ||
241 : | } | ||
242 : | |||
243 : | /* set median, minimum */ | ||
244 : | |||
245 : | pmv[0].x = | ||
246 : | MIN(MAX(pmv[1].x, pmv[2].x), | ||
247 : | MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); | ||
248 : | pmv[0].y = | ||
249 : | MIN(MAX(pmv[1].y, pmv[2].y), | ||
250 : | MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); | ||
251 : | |||
252 : | psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]); | ||
253 : | |||
254 : | } | ||
255 : | |||
256 : | |||
257 : | static void | ||
258 : | ModeDecision_SAD(SearchData * const Data, | ||
259 : | MACROBLOCK * const pMB, | ||
260 : | const MACROBLOCK * const pMBs, | ||
261 : | const int x, const int y, | ||
262 : | const MBParam * const pParam, | ||
263 : | const uint32_t MotionFlags, | ||
264 : | const uint32_t VopFlags, | ||
265 : | const uint32_t VolFlags, | ||
266 : | const IMAGE * const pCurrent, | ||
267 : | const IMAGE * const pRef, | ||
268 : | const IMAGE * const vGMC, | ||
269 : | syskin | 1.5 | const int coding_type, |
270 : | const int skip_sad) | ||
271 : | edgomez | 1.2 | { |
272 : | int mode = MODE_INTER; | ||
273 : | int mcsel = 0; | ||
274 : | int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); | ||
275 : | const uint32_t iQuant = pMB->quant; | ||
276 : | |||
277 : | const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); | ||
278 : | |||
279 : | int sad; | ||
280 : | int InterBias = MV16_INTER_BIAS; | ||
281 : | |||
282 : | pMB->mcsel = 0; | ||
283 : | |||
284 : | if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + | ||
285 : | Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { | ||
286 : | mode = MODE_INTER; | ||
287 : | sad = Data->iMinSAD[0]; | ||
288 : | } else { | ||
289 : | mode = MODE_INTER4V; | ||
290 : | sad = Data->iMinSAD[1] + Data->iMinSAD[2] + | ||
291 : | Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant; | ||
292 : | Data->iMinSAD[0] = sad; | ||
293 : | } | ||
294 : | |||
295 : | /* final skip decision, a.k.a. "the vector you found, really that good?" */ | ||
296 : | syskin | 1.5 | if (skip_possible && (skip_sad < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
297 : | if ( (100*skip_sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) | ||
298 : | syskin | 1.9 | if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant)) { |
299 : | edgomez | 1.2 | mode = MODE_NOT_CODED; |
300 : | sad = 0; | ||
301 : | } | ||
302 : | |||
303 : | /* mcsel */ | ||
304 : | if (coding_type == S_VOP) { | ||
305 : | |||
306 : | int32_t iSAD = sad16(Data->Cur, | ||
307 : | vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); | ||
308 : | |||
309 : | if (Data->chroma) { | ||
310 : | iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); | ||
311 : | iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); | ||
312 : | } | ||
313 : | |||
314 : | if (iSAD <= sad) { /* mode decision GMC */ | ||
315 : | mode = MODE_INTER; | ||
316 : | mcsel = 1; | ||
317 : | sad = iSAD; | ||
318 : | } | ||
319 : | } | ||
320 : | |||
321 : | /* intra decision */ | ||
322 : | |||
323 : | if (iQuant > 10) InterBias += 60 * (iQuant - 10); /* to make high quants work */ | ||
324 : | if (y != 0) | ||
325 : | if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; | ||
326 : | if (x != 0) | ||
327 : | if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; | ||
328 : | |||
329 : | if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */ | ||
330 : | |||
331 : | if (InterBias < sad) { | ||
332 : | syskin | 1.9 | int32_t deviation = dev16(Data->Cur, Data->iEdgedWidth); |
333 : | edgomez | 1.2 | if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
334 : | } | ||
335 : | |||
336 : | pMB->cbp = 63; | ||
337 : | pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; | ||
338 : | |||
339 : | if (mode == MODE_INTER && mcsel == 0) { | ||
340 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; | ||
341 : | |||
342 : | if(Data->qpel) { | ||
343 : | pMB->qmvs[0] = pMB->qmvs[1] | ||
344 : | = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; | ||
345 : | pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; | ||
346 : | pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; | ||
347 : | } else { | ||
348 : | pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; | ||
349 : | pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; | ||
350 : | } | ||
351 : | |||
352 : | } else if (mode == MODE_INTER ) { /* but mcsel == 1 */ | ||
353 : | |||
354 : | pMB->mcsel = 1; | ||
355 : | if (Data->qpel) { | ||
356 : | pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; | ||
357 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; | ||
358 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; | ||
359 : | } else | ||
360 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; | ||
361 : | |||
362 : | } else | ||
363 : | if (mode == MODE_INTER4V) ; /* anything here? */ | ||
364 : | else /* INTRA, NOT_CODED */ | ||
365 : | ZeroMacroblockP(pMB, 0); | ||
366 : | |||
367 : | pMB->mode = mode; | ||
368 : | } | ||
369 : | |||
370 : | static __inline void | ||
371 : | PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount, | ||
372 : | syskin | 1.9 | int iHcount, const MACROBLOCK * const prevMB) |
373 : | edgomez | 1.2 | { |
374 : | |||
375 : | if ( (y != 0) && (x < (iWcount-1)) ) { /* [5] top-right neighbour */ | ||
376 : | pmv[5].x = EVEN(pmv[3].x); | ||
377 : | pmv[5].y = EVEN(pmv[3].y); | ||
378 : | } else pmv[5].x = pmv[5].y = 0; | ||
379 : | |||
380 : | if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }/* pmv[3] is left neighbour */ | ||
381 : | else pmv[3].x = pmv[3].y = 0; | ||
382 : | |||
383 : | if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }/* [4] top neighbour */ | ||
384 : | else pmv[4].x = pmv[4].y = 0; | ||
385 : | |||
386 : | /* [1] median prediction */ | ||
387 : | pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y); | ||
388 : | |||
389 : | pmv[0].x = pmv[0].y = 0; /* [0] is zero; not used in the loop (checked before) but needed here for make_mask */ | ||
390 : | |||
391 : | pmv[2].x = EVEN(prevMB->mvs[0].x); /* [2] is last frame */ | ||
392 : | pmv[2].y = EVEN(prevMB->mvs[0].y); | ||
393 : | |||
394 : | if ((x < iWcount-1) && (y < iHcount-1)) { | ||
395 : | pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */ | ||
396 : | pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y); | ||
397 : | } else pmv[6].x = pmv[6].y = 0; | ||
398 : | } | ||
399 : | |||
400 : | static void | ||
401 : | Search8(SearchData * const OldData, | ||
402 : | const int x, const int y, | ||
403 : | const uint32_t MotionFlags, | ||
404 : | const MBParam * const pParam, | ||
405 : | MACROBLOCK * const pMB, | ||
406 : | const MACROBLOCK * const pMBs, | ||
407 : | const int block, | ||
408 : | SearchData * const Data) | ||
409 : | { | ||
410 : | int i = 0; | ||
411 : | syskin | 1.4 | VECTOR vbest_q; int32_t sbest_q; |
412 : | edgomez | 1.2 | *Data->iMinSAD = *(OldData->iMinSAD + 1 + block); |
413 : | *Data->currentMV = *(OldData->currentMV + 1 + block); | ||
414 : | *Data->currentQMV = *(OldData->currentQMV + 1 + block); | ||
415 : | |||
416 : | if(Data->qpel) { | ||
417 : | Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); | ||
418 : | if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y, | ||
419 : | syskin | 1.9 | Data->predMV, Data->iFcode, 0); |
420 : | edgomez | 1.2 | } else { |
421 : | Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); | ||
422 : | if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y, | ||
423 : | syskin | 1.9 | Data->predMV, Data->iFcode, 0); |
424 : | edgomez | 1.2 | } |
425 : | |||
426 : | *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10; | ||
427 : | |||
428 : | if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) { | ||
429 : | |||
430 : | syskin | 1.4 | vbest_q = Data->currentQMV[0]; |
431 : | sbest_q = Data->iMinSAD[0]; | ||
432 : | |||
433 : | edgomez | 1.2 | |
434 : | syskin | 1.9 | Data->RefP[0] = OldData->RefP[0] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
435 : | Data->RefP[1] = OldData->RefP[1] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); | ||
436 : | Data->RefP[2] = OldData->RefP[2] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); | ||
437 : | Data->RefP[3] = OldData->RefP[3] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); | ||
438 : | edgomez | 1.2 | |
439 : | syskin | 1.9 | Data->Cur = OldData->Cur + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
440 : | edgomez | 1.2 | Data->qpel_precision = 0; |
441 : | |||
442 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, | ||
443 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); |
444 : | edgomez | 1.2 | |
445 : | if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) { | ||
446 : | |||
447 : | MainSearchFunc *MainSearchPtr; | ||
448 : | if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch; | ||
449 : | else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch; | ||
450 : | else MainSearchPtr = xvid_me_DiamondSearch; | ||
451 : | |||
452 : | syskin | 1.9 | MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate8); |
453 : | edgomez | 1.2 | } |
454 : | |||
455 : | syskin | 1.4 | if(!Data->qpel) { |
456 : | /* halfpel mode */ | ||
457 : | if (MotionFlags & XVID_ME_HALFPELREFINE8) | ||
458 : | syskin | 1.8 | /* perform halfpel refine of current best vector */ |
459 : | syskin | 1.9 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0); |
460 : | syskin | 1.4 | } else { |
461 : | /* qpel mode */ | ||
462 : | Data->currentQMV->x = 2*Data->currentMV->x; | ||
463 : | Data->currentQMV->y = 2*Data->currentMV->y; | ||
464 : | |||
465 : | if(MotionFlags & XVID_ME_FASTREFINE8) { | ||
466 : | /* fast */ | ||
467 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, | ||
468 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode, 2); |
469 : | syskin | 1.4 | FullRefine_Fast(Data, CheckCandidate8, 0); |
470 : | } else if(MotionFlags & XVID_ME_QUARTERPELREFINE8) { | ||
471 : | /* full */ | ||
472 : | if (MotionFlags & XVID_ME_HALFPELREFINE8) { | ||
473 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0); /* hpel part */ |
474 : | syskin | 1.4 | Data->currentQMV->x = 2*Data->currentMV->x; |
475 : | Data->currentQMV->y = 2*Data->currentMV->y; | ||
476 : | } | ||
477 : | edgomez | 1.2 | |
478 : | syskin | 1.4 | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, |
479 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode, 2); |
480 : | syskin | 1.4 | Data->qpel_precision = 1; |
481 : | edgomez | 1.2 | |
482 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate8, 0); /* qpel part */ |
483 : | edgomez | 1.2 | } |
484 : | } | ||
485 : | |||
486 : | syskin | 1.4 | if (sbest_q <= Data->iMinSAD[0]) /* we have not found a better match */ |
487 : | Data->currentQMV[0] = vbest_q; | ||
488 : | edgomez | 1.2 | |
489 : | } | ||
490 : | |||
491 : | syskin | 1.9 | if(Data->qpel) { |
492 : | edgomez | 1.2 | pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x; |
493 : | pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y; | ||
494 : | pMB->qmvs[block] = *Data->currentQMV; | ||
495 : | } else { | ||
496 : | pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x; | ||
497 : | pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y; | ||
498 : | } | ||
499 : | |||
500 : | *(OldData->iMinSAD + 1 + block) = *Data->iMinSAD; | ||
501 : | *(OldData->currentMV + 1 + block) = *Data->currentMV; | ||
502 : | *(OldData->currentQMV + 1 + block) = *Data->currentQMV; | ||
503 : | |||
504 : | pMB->mvs[block] = *Data->currentMV; | ||
505 : | pMB->sad8[block] = 4 * *Data->iMinSAD; | ||
506 : | } | ||
507 : | |||
508 : | |||
509 : | |||
510 : | static void | ||
511 : | SearchP(const IMAGE * const pRef, | ||
512 : | const uint8_t * const pRefH, | ||
513 : | const uint8_t * const pRefV, | ||
514 : | const uint8_t * const pRefHV, | ||
515 : | const IMAGE * const pCur, | ||
516 : | const int x, | ||
517 : | const int y, | ||
518 : | const uint32_t MotionFlags, | ||
519 : | const uint32_t VopFlags, | ||
520 : | SearchData * const Data, | ||
521 : | const MBParam * const pParam, | ||
522 : | const MACROBLOCK * const pMBs, | ||
523 : | const MACROBLOCK * const prevMBs, | ||
524 : | MACROBLOCK * const pMB) | ||
525 : | { | ||
526 : | |||
527 : | int i, threshA; | ||
528 : | VECTOR pmv[7]; | ||
529 : | int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); | ||
530 : | CheckFunc * CheckCandidate; | ||
531 : | |||
532 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
533 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); |
534 : | edgomez | 1.2 | |
535 : | get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, pmv, Data->temp); | ||
536 : | |||
537 : | Data->chromaX = Data->chromaY = 0; /* chroma-sad cache */ | ||
538 : | syskin | 1.9 | Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
539 : | Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; | ||
540 : | Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; | ||
541 : | |||
542 : | Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16; | ||
543 : | Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16; | ||
544 : | Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16; | ||
545 : | Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16; | ||
546 : | Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; | ||
547 : | Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; | ||
548 : | edgomez | 1.2 | |
549 : | Data->lambda16 = xvid_me_lambda_vec16[pMB->quant]; | ||
550 : | Data->lambda8 = xvid_me_lambda_vec8[pMB->quant]; | ||
551 : | Data->qpel_precision = 0; | ||
552 : | Data->dir = 0; | ||
553 : | |||
554 : | memset(Data->currentMV, 0, 5*sizeof(VECTOR)); | ||
555 : | |||
556 : | if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); | ||
557 : | else Data->predMV = pmv[0]; | ||
558 : | |||
559 : | syskin | 1.9 | i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0); |
560 : | edgomez | 1.2 | Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10); |
561 : | Data->iMinSAD[1] = pMB->sad8[0] + ((Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS)) >> 10); | ||
562 : | Data->iMinSAD[2] = pMB->sad8[1]; | ||
563 : | Data->iMinSAD[3] = pMB->sad8[2]; | ||
564 : | Data->iMinSAD[4] = pMB->sad8[3]; | ||
565 : | |||
566 : | if ((!(VopFlags & XVID_VOP_MODEDECISION_RD)) && (x | y)) { | ||
567 : | threshA = Data->temp[0]; /* that's where we keep this SAD atm */ | ||
568 : | if (threshA < 512) threshA = 512; | ||
569 : | else if (threshA > 1024) threshA = 1024; | ||
570 : | } else | ||
571 : | threshA = 512; | ||
572 : | |||
573 : | PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, | ||
574 : | syskin | 1.9 | prevMBs + x + y * pParam->mb_width); |
575 : | edgomez | 1.2 | |
576 : | syskin | 1.9 | if (inter4v) CheckCandidate = CheckCandidate16; |
577 : | else CheckCandidate = CheckCandidate16no4v; /* for extra speed */ | ||
578 : | edgomez | 1.2 | |
579 : | /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/ | ||
580 : | |||
581 : | for (i = 1; i < 7; i++) | ||
582 : | if (!vector_repeats(pmv, i)) { | ||
583 : | CheckCandidate(pmv[i].x, pmv[i].y, Data, i); | ||
584 : | if (Data->iMinSAD[0] <= threshA) { i++; break; } | ||
585 : | } | ||
586 : | |||
587 : | if ((Data->iMinSAD[0] <= threshA) || | ||
588 : | (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && | ||
589 : | (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) | ||
590 : | inter4v = 0; | ||
591 : | else { | ||
592 : | |||
593 : | MainSearchFunc * MainSearchPtr; | ||
594 : | int mask = make_mask(pmv, i, Data->dir); /* all vectors pmv[0..i-1] have been checked */ | ||
595 : | |||
596 : | if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; | ||
597 : | else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; | ||
598 : | else MainSearchPtr = xvid_me_DiamondSearch; | ||
599 : | |||
600 : | MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate); | ||
601 : | |||
602 : | /* extended search, diamond starting in 0,0 and in prediction. | ||
603 : | note that this search is/might be done in halfpel positions, | ||
604 : | which makes it more different than the diamond above */ | ||
605 : | |||
606 : | if (MotionFlags & XVID_ME_EXTSEARCH16) { | ||
607 : | int32_t bSAD; | ||
608 : | VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0]; | ||
609 : | if (Data->qpel) { | ||
610 : | startMV.x /= 2; | ||
611 : | startMV.y /= 2; | ||
612 : | } | ||
613 : | if (!(MVequal(startMV, backupMV))) { | ||
614 : | bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; | ||
615 : | |||
616 : | CheckCandidate(startMV.x, startMV.y, Data, 255); | ||
617 : | xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate); | ||
618 : | if (bSAD < Data->iMinSAD[0]) { | ||
619 : | Data->currentMV[0] = backupMV; | ||
620 : | syskin | 1.9 | Data->iMinSAD[0] = bSAD; |
621 : | } | ||
622 : | edgomez | 1.2 | } |
623 : | |||
624 : | backupMV = Data->currentMV[0]; | ||
625 : | startMV.x = startMV.y = 1; | ||
626 : | if (!(MVequal(startMV, backupMV))) { | ||
627 : | bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; | ||
628 : | |||
629 : | CheckCandidate(startMV.x, startMV.y, Data, 255); | ||
630 : | xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate); | ||
631 : | if (bSAD < Data->iMinSAD[0]) { | ||
632 : | Data->currentMV[0] = backupMV; | ||
633 : | Data->iMinSAD[0] = bSAD; | ||
634 : | } | ||
635 : | } | ||
636 : | } | ||
637 : | } | ||
638 : | |||
639 : | |||
640 : | syskin | 1.4 | if(!Data->qpel) { |
641 : | /* halfpel mode */ | ||
642 : | if (MotionFlags & XVID_ME_HALFPELREFINE16) | ||
643 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); |
644 : | syskin | 1.4 | } else { |
645 : | /* qpel mode */ | ||
646 : | |||
647 : | for(i = 0; i < 5; i++) { | ||
648 : | Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */ | ||
649 : | Data->currentQMV[i].y = 2 * Data->currentMV[i].y; | ||
650 : | } | ||
651 : | if(MotionFlags & XVID_ME_FASTREFINE16 && MotionFlags & XVID_ME_QUARTERPELREFINE16) { | ||
652 : | /* fast */ | ||
653 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
654 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode, 2); |
655 : | syskin | 1.4 | FullRefine_Fast(Data, CheckCandidate, 0); |
656 : | } else { | ||
657 : | if(MotionFlags & (XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE16_RD)) { | ||
658 : | /* full */ | ||
659 : | if (MotionFlags & XVID_ME_HALFPELREFINE16) { | ||
660 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); /* hpel part */ |
661 : | syskin | 1.4 | for(i = 0; i < 5; i++) { |
662 : | Data->currentQMV[i].x = 2 * Data->currentMV[i].x; | ||
663 : | Data->currentQMV[i].y = 2 * Data->currentMV[i].y; | ||
664 : | } | ||
665 : | } | ||
666 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
667 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode, 2); |
668 : | syskin | 1.4 | Data->qpel_precision = 1; |
669 : | if(MotionFlags & XVID_ME_QUARTERPELREFINE16) | ||
670 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate, 0); /* qpel part */ |
671 : | syskin | 1.4 | } |
672 : | edgomez | 1.2 | } |
673 : | } | ||
674 : | |||
675 : | syskin | 1.9 | if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30 * ((MotionFlags & XVID_ME_FASTREFINE16) ? 8 : 1)) |
676 : | edgomez | 1.2 | inter4v = 0; |
677 : | |||
678 : | if (inter4v) { | ||
679 : | SearchData Data8; | ||
680 : | memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */ | ||
681 : | |||
682 : | Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); | ||
683 : | Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); | ||
684 : | Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); | ||
685 : | Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); | ||
686 : | |||
687 : | if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) { | ||
688 : | syskin | 1.4 | /* chroma is only used for comparison to INTER. if the comparison will be done in RD domain, it will not be used */ |
689 : | edgomez | 1.2 | int sumx = 0, sumy = 0; |
690 : | |||
691 : | if (Data->qpel) | ||
692 : | for (i = 1; i < 5; i++) { | ||
693 : | sumx += Data->currentQMV[i].x/2; | ||
694 : | sumy += Data->currentQMV[i].y/2; | ||
695 : | } | ||
696 : | else | ||
697 : | for (i = 1; i < 5; i++) { | ||
698 : | sumx += Data->currentMV[i].x; | ||
699 : | sumy += Data->currentMV[i].y; | ||
700 : | } | ||
701 : | |||
702 : | Data->iMinSAD[1] += xvid_me_ChromaSAD((sumx >> 3) + roundtab_76[sumx & 0xf], | ||
703 : | (sumy >> 3) + roundtab_76[sumy & 0xf], Data); | ||
704 : | } | ||
705 : | } else Data->iMinSAD[1] = 4096*256; | ||
706 : | } | ||
707 : | |||
708 : | static __inline uint32_t | ||
709 : | MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags) | ||
710 : | { | ||
711 : | uint32_t Flags = MotionFlags; | ||
712 : | |||
713 : | if (!(VopFlags & XVID_VOP_MODEDECISION_RD)) | ||
714 : | Flags &= ~(XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD+XVID_ME_EXTSEARCH_RD); | ||
715 : | |||
716 : | if (Flags & XVID_ME_EXTSEARCH_RD) | ||
717 : | Flags |= XVID_ME_HALFPELREFINE16_RD; | ||
718 : | |||
719 : | if (Flags & XVID_ME_EXTSEARCH_RD && MotionFlags & XVID_ME_EXTSEARCH8) | ||
720 : | Flags |= XVID_ME_HALFPELREFINE8_RD; | ||
721 : | |||
722 : | if (Flags & XVID_ME_HALFPELREFINE16_RD) | ||
723 : | Flags |= XVID_ME_QUARTERPELREFINE16_RD; | ||
724 : | |||
725 : | if (Flags & XVID_ME_HALFPELREFINE8_RD) { | ||
726 : | Flags |= XVID_ME_QUARTERPELREFINE8_RD; | ||
727 : | Flags &= ~XVID_ME_HALFPELREFINE8; | ||
728 : | } | ||
729 : | |||
730 : | if (Flags & XVID_ME_QUARTERPELREFINE8_RD) | ||
731 : | Flags &= ~XVID_ME_QUARTERPELREFINE8; | ||
732 : | |||
733 : | edgomez | 1.3 | if (Flags & XVID_ME_QUARTERPELREFINE16_RD) |
734 : | Flags &= ~XVID_ME_QUARTERPELREFINE16; | ||
735 : | |||
736 : | edgomez | 1.2 | if (!(VolFlags & XVID_VOL_QUARTERPEL)) |
737 : | Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD); | ||
738 : | |||
739 : | if (!(VopFlags & XVID_VOP_HALFPEL)) | ||
740 : | Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD); | ||
741 : | |||
742 : | syskin | 1.9 | if (VopFlags & XVID_VOP_GREYSCALE) |
743 : | edgomez | 1.2 | Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP); |
744 : | |||
745 : | syskin | 1.4 | if (Flags & XVID_ME_FASTREFINE8) |
746 : | Flags &= ~XVID_ME_HALFPELREFINE8_RD; | ||
747 : | |||
748 : | if (Flags & XVID_ME_FASTREFINE16) | ||
749 : | Flags &= ~XVID_ME_HALFPELREFINE16_RD; | ||
750 : | |||
751 : | edgomez | 1.2 | return Flags; |
752 : | } | ||
753 : | |||
754 : | syskin | 1.10 | static __inline void |
755 : | motionStatsPVOP(int * const MVmax, int * const mvCount, int * const mvSum, | ||
756 : | const MACROBLOCK * const pMB, const int qpel) | ||
757 : | { | ||
758 : | const VECTOR * const mv = qpel ? pMB->qmvs : pMB->mvs; | ||
759 : | int i; | ||
760 : | int max = *MVmax; | ||
761 : | |||
762 : | switch (pMB->mode) { | ||
763 : | case MODE_INTER4V: | ||
764 : | *mvCount += 3; | ||
765 : | for(i = 3; i; i--) { | ||
766 : | if (mv[i].x > max) max = mv[i].x; | ||
767 : | else if (-mv[i].x - 1 > max) max = -mv[i].x - 1; | ||
768 : | *mvSum += mv[i].x * mv[i].x; | ||
769 : | if (mv[i].y > max) max = mv[i].y; | ||
770 : | else if (-mv[i].y - 1 > max) max = -mv[i].y - 1; | ||
771 : | *mvSum += mv[i].y * mv[i].y; | ||
772 : | } | ||
773 : | case MODE_INTER: | ||
774 : | syskin | 1.12 | (*mvCount)++; |
775 : | syskin | 1.10 | *mvSum += mv[0].x * mv[0].x; |
776 : | *mvSum += mv[0].y * mv[0].y; | ||
777 : | syskin | 1.12 | if (mv[0].x > max) max = mv[0].x; |
778 : | else if (-mv[0].x - 1 > max) max = -mv[0].x - 1; | ||
779 : | if (mv[0].y > max) max = mv[0].y; | ||
780 : | else if (-mv[0].y - 1 > max) max = -mv[0].y - 1; | ||
781 : | *MVmax = max; | ||
782 : | syskin | 1.10 | default: |
783 : | break; | ||
784 : | } | ||
785 : | } | ||
786 : | |||
787 : | edgomez | 1.2 | bool |
788 : | MotionEstimation(MBParam * const pParam, | ||
789 : | FRAMEINFO * const current, | ||
790 : | FRAMEINFO * const reference, | ||
791 : | const IMAGE * const pRefH, | ||
792 : | const IMAGE * const pRefV, | ||
793 : | const IMAGE * const pRefHV, | ||
794 : | const IMAGE * const pGMC, | ||
795 : | const uint32_t iLimit) | ||
796 : | { | ||
797 : | MACROBLOCK *const pMBs = current->mbs; | ||
798 : | const IMAGE *const pCurrent = ¤t->image; | ||
799 : | const IMAGE *const pRef = &reference->image; | ||
800 : | |||
801 : | syskin | 1.9 | const uint32_t mb_width = pParam->mb_width; |
802 : | const uint32_t mb_height = pParam->mb_height; | ||
803 : | edgomez | 1.2 | const uint32_t iEdgedWidth = pParam->edged_width; |
804 : | const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags); | ||
805 : | int stat_thresh = 0; | ||
806 : | syskin | 1.10 | int MVmax = 0, mvSum = 0, mvCount = 0; |
807 : | edgomez | 1.2 | |
808 : | uint32_t x, y; | ||
809 : | int32_t sad00; | ||
810 : | int skip_thresh = INITIAL_SKIP_THRESH * \ | ||
811 : | (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1); | ||
812 : | |||
813 : | /* some pre-initialized thingies for SearchP */ | ||
814 : | DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); | ||
815 : | SearchData Data; | ||
816 : | memset(&Data, 0, sizeof(SearchData)); | ||
817 : | Data.iEdgedWidth = iEdgedWidth; | ||
818 : | Data.iFcode = current->fcode; | ||
819 : | Data.rounding = pParam->m_rounding_type; | ||
820 : | Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0); | ||
821 : | Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP; | ||
822 : | Data.dctSpace = dct_space; | ||
823 : | Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); | ||
824 : | Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices; | ||
825 : | |||
826 : | Data.RefQ = pRefV->u; /* a good place, also used in MC (for similar purpose) */ | ||
827 : | if (sadInit) (*sadInit) (); | ||
828 : | |||
829 : | for (y = 0; y < mb_height; y++) { | ||
830 : | for (x = 0; x < mb_width; x++) { | ||
831 : | MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; | ||
832 : | MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width]; | ||
833 : | |||
834 : | syskin | 1.9 | pMB->sad16 = |
835 : | edgomez | 1.2 | sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16, |
836 : | pRef->y + (x + y * iEdgedWidth) * 16, | ||
837 : | syskin | 1.9 | pParam->edged_width, pMB->sad8); |
838 : | syskin | 1.5 | |
839 : | sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3])); | ||
840 : | edgomez | 1.2 | |
841 : | if (Data.chroma) { | ||
842 : | Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, | ||
843 : | pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2) | ||
844 : | + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, | ||
845 : | pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2); | ||
846 : | pMB->sad16 += Data.chromaSAD; | ||
847 : | syskin | 1.5 | sad00 += Data.chromaSAD; |
848 : | edgomez | 1.2 | } |
849 : | |||
850 : | /* initial skip decision */ | ||
851 : | if (current->coding_type != S_VOP) { /* no fast SKIP for S(GMC)-VOPs */ | ||
852 : | syskin | 1.5 | if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) |
853 : | syskin | 1.9 | if (Data.chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant)) { |
854 : | edgomez | 1.2 | ZeroMacroblockP(pMB, sad00); |
855 : | pMB->mode = MODE_NOT_CODED; | ||
856 : | continue; | ||
857 : | } | ||
858 : | } | ||
859 : | |||
860 : | if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) { | ||
861 : | if(x > 0 && y > 0 && x < pParam->mb_width) { | ||
862 : | if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], zeroMV) && | ||
863 : | MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], zeroMV) && | ||
864 : | MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], zeroMV) && | ||
865 : | MVequal(prevMB->mvs[0], zeroMV)) { | ||
866 : | stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16, | ||
867 : | MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16, | ||
868 : | MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16, | ||
869 : | prevMB->sad16))); | ||
870 : | } else { | ||
871 : | stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16, | ||
872 : | MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16, | ||
873 : | MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16, | ||
874 : | prevMB->sad16))); | ||
875 : | } | ||
876 : | } | ||
877 : | } | ||
878 : | |||
879 : | /* favorize (0,0) vector for cartoons */ | ||
880 : | if ((current->vop_flags & XVID_VOP_CARTOON) && | ||
881 : | ((sad00 < pMB->quant * 4 * skip_thresh) || (sad00 < stat_thresh))) { | ||
882 : | ZeroMacroblockP(pMB, sad00); | ||
883 : | Isibaar | 1.14 | pMB->cbp = 0x3f; |
884 : | edgomez | 1.2 | continue; |
885 : | } | ||
886 : | |||
887 : | SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, | ||
888 : | y, MotionFlags, current->vop_flags, | ||
889 : | &Data, pParam, pMBs, reference->mbs, pMB); | ||
890 : | |||
891 : | if (current->vop_flags & XVID_VOP_MODEDECISION_RD) | ||
892 : | xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam, | ||
893 : | MotionFlags, current->vop_flags, current->vol_flags, | ||
894 : | pCurrent, pRef, pGMC, current->coding_type); | ||
895 : | |||
896 : | else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD) | ||
897 : | xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam, | ||
898 : | MotionFlags, current->vop_flags, current->vol_flags, | ||
899 : | pCurrent, pRef, pGMC, current->coding_type); | ||
900 : | else | ||
901 : | ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam, | ||
902 : | MotionFlags, current->vop_flags, current->vol_flags, | ||
903 : | syskin | 1.5 | pCurrent, pRef, pGMC, current->coding_type, sad00); |
904 : | edgomez | 1.2 | |
905 : | |||
906 : | syskin | 1.10 | motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel); |
907 : | edgomez | 1.2 | } |
908 : | } | ||
909 : | |||
910 : | syskin | 1.10 | current->fcode = getMinFcode(MVmax); |
911 : | current->sStat.iMvSum = mvSum; | ||
912 : | current->sStat.iMvCount = mvCount; | ||
913 : | edgomez | 1.2 | |
914 : | syskin | 1.10 | return 0; |
915 : | edgomez | 1.11 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |