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