Parent Directory | Revision Log
Revision 1.21 - (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.21 | * $Id: estimation_pvop.c,v 1.20 2006/02/25 01:20:41 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 : | syskin | 1.19 | #include "motion_smp.h" |
43 : | |||
44 : | edgomez | 1.2 | |
45 : | static const int xvid_me_lambda_vec8[32] = | ||
46 : | Isibaar | 1.16 | { 0 ,(int)(1.0 * NEIGH_TEND_8X8 + 0.5), |
47 : | (int)(2.0*NEIGH_TEND_8X8 + 0.5), (int)(3.0*NEIGH_TEND_8X8 + 0.5), | ||
48 : | (int)(4.0*NEIGH_TEND_8X8 + 0.5), (int)(5.0*NEIGH_TEND_8X8 + 0.5), | ||
49 : | (int)(6.0*NEIGH_TEND_8X8 + 0.5), (int)(7.0*NEIGH_TEND_8X8 + 0.5), | ||
50 : | (int)(8.0*NEIGH_TEND_8X8 + 0.5), (int)(9.0*NEIGH_TEND_8X8 + 0.5), | ||
51 : | (int)(10.0*NEIGH_TEND_8X8 + 0.5), (int)(11.0*NEIGH_TEND_8X8 + 0.5), | ||
52 : | (int)(12.0*NEIGH_TEND_8X8 + 0.5), (int)(13.0*NEIGH_TEND_8X8 + 0.5), | ||
53 : | (int)(14.0*NEIGH_TEND_8X8 + 0.5), (int)(15.0*NEIGH_TEND_8X8 + 0.5), | ||
54 : | (int)(16.0*NEIGH_TEND_8X8 + 0.5), (int)(17.0*NEIGH_TEND_8X8 + 0.5), | ||
55 : | (int)(18.0*NEIGH_TEND_8X8 + 0.5), (int)(19.0*NEIGH_TEND_8X8 + 0.5), | ||
56 : | (int)(20.0*NEIGH_TEND_8X8 + 0.5), (int)(21.0*NEIGH_TEND_8X8 + 0.5), | ||
57 : | (int)(22.0*NEIGH_TEND_8X8 + 0.5), (int)(23.0*NEIGH_TEND_8X8 + 0.5), | ||
58 : | (int)(24.0*NEIGH_TEND_8X8 + 0.5), (int)(25.0*NEIGH_TEND_8X8 + 0.5), | ||
59 : | (int)(26.0*NEIGH_TEND_8X8 + 0.5), (int)(27.0*NEIGH_TEND_8X8 + 0.5), | ||
60 : | (int)(28.0*NEIGH_TEND_8X8 + 0.5), (int)(29.0*NEIGH_TEND_8X8 + 0.5), | ||
61 : | (int)(30.0*NEIGH_TEND_8X8 + 0.5), (int)(31.0*NEIGH_TEND_8X8 + 0.5) | ||
62 : | edgomez | 1.2 | }; |
63 : | |||
64 : | static void | ||
65 : | CheckCandidate16(const int x, const int y, SearchData * const data, const unsigned int Direction) | ||
66 : | { | ||
67 : | const uint8_t * Reference; | ||
68 : | syskin | 1.4 | int32_t sad, xc, yc; uint32_t t; |
69 : | VECTOR * current; | ||
70 : | edgomez | 1.2 | |
71 : | if ( (x > data->max_dx) || (x < data->min_dx) | ||
72 : | || (y > data->max_dy) || (y < data->min_dy) ) return; | ||
73 : | |||
74 : | syskin | 1.4 | if (data->qpel_precision) { /* x and y are in 1/4 precision */ |
75 : | Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); | ||
76 : | current = data->currentQMV; | ||
77 : | xc = x/2; yc = y/2; | ||
78 : | } else { | ||
79 : | Reference = GetReference(x, y, data); | ||
80 : | current = data->currentMV; | ||
81 : | xc = x; yc = y; | ||
82 : | } | ||
83 : | edgomez | 1.2 | |
84 : | sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp); | ||
85 : | syskin | 1.9 | t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision); |
86 : | edgomez | 1.2 | |
87 : | Isibaar | 1.16 | sad += (data->lambda16 * t); |
88 : | data->temp[0] += (data->lambda8 * t); | ||
89 : | edgomez | 1.2 | |
90 : | if (data->chroma) { | ||
91 : | if (sad >= data->iMinSAD[0]) goto no16; | ||
92 : | syskin | 1.4 | sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
93 : | (yc >> 1) + roundtab_79[yc & 0x3], data); | ||
94 : | edgomez | 1.2 | } |
95 : | |||
96 : | if (sad < data->iMinSAD[0]) { | ||
97 : | data->iMinSAD[0] = sad; | ||
98 : | syskin | 1.4 | current[0].x = x; current[0].y = y; |
99 : | edgomez | 1.2 | data->dir = Direction; |
100 : | } | ||
101 : | |||
102 : | no16: | ||
103 : | if (data->temp[0] < data->iMinSAD[1]) { | ||
104 : | syskin | 1.4 | data->iMinSAD[1] = data->temp[0]; current[1].x = x; current[1].y = y; } |
105 : | edgomez | 1.2 | if (data->temp[1] < data->iMinSAD[2]) { |
106 : | syskin | 1.4 | data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; } |
107 : | edgomez | 1.2 | if (data->temp[2] < data->iMinSAD[3]) { |
108 : | syskin | 1.4 | data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; } |
109 : | edgomez | 1.2 | if (data->temp[3] < data->iMinSAD[4]) { |
110 : | syskin | 1.4 | data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } |
111 : | edgomez | 1.2 | } |
112 : | |||
113 : | static void | ||
114 : | CheckCandidate8(const int x, const int y, SearchData * const data, const unsigned int Direction) | ||
115 : | { | ||
116 : | int32_t sad; uint32_t t; | ||
117 : | const uint8_t * Reference; | ||
118 : | VECTOR * current; | ||
119 : | |||
120 : | if ( (x > data->max_dx) || (x < data->min_dx) | ||
121 : | || (y > data->max_dy) || (y < data->min_dy) ) return; | ||
122 : | |||
123 : | if (!data->qpel_precision) { | ||
124 : | Reference = GetReference(x, y, data); | ||
125 : | current = data->currentMV; | ||
126 : | } else { /* x and y are in 1/4 precision */ | ||
127 : | Reference = xvid_me_interpolate8x8qpel(x, y, 0, 0, data); | ||
128 : | current = data->currentQMV; | ||
129 : | } | ||
130 : | |||
131 : | sad = sad8(data->Cur, Reference, data->iEdgedWidth); | ||
132 : | syskin | 1.9 | t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision); |
133 : | edgomez | 1.2 | |
134 : | Isibaar | 1.16 | sad += (data->lambda8 * t); |
135 : | edgomez | 1.2 | |
136 : | if (sad < *(data->iMinSAD)) { | ||
137 : | *(data->iMinSAD) = sad; | ||
138 : | current->x = x; current->y = y; | ||
139 : | data->dir = Direction; | ||
140 : | } | ||
141 : | } | ||
142 : | |||
143 : | int | ||
144 : | xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference, | ||
145 : | const int x, const int y, | ||
146 : | syskin | 1.9 | const uint32_t stride, const uint32_t iQuant) |
147 : | edgomez | 1.2 | { |
148 : | int offset = (x + y*stride)*8; | ||
149 : | syskin | 1.9 | uint32_t sadC = sad8(current->u + offset, |
150 : | reference->u + offset, stride); | ||
151 : | if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; | ||
152 : | sadC += sad8(current->v + offset, | ||
153 : | reference->v + offset, stride); | ||
154 : | if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; | ||
155 : | return 1; | ||
156 : | edgomez | 1.2 | } |
157 : | |||
158 : | /* | ||
159 : | * pmv are filled with: | ||
160 : | * [0]: Median (or whatever is correct in a special case) | ||
161 : | * [1]: left neighbour | ||
162 : | * [2]: top neighbour | ||
163 : | * [3]: topright neighbour | ||
164 : | * psad are filled with: | ||
165 : | * [0]: minimum of [1] to [3] | ||
166 : | * [1]: left neighbour's SAD (NB:[1] to [3] are actually not needed) | ||
167 : | * [2]: top neighbour's SAD | ||
168 : | * [3]: topright neighbour's SAD | ||
169 : | */ | ||
170 : | |||
171 : | static __inline void | ||
172 : | get_pmvdata2(const MACROBLOCK * const mbs, | ||
173 : | const int mb_width, | ||
174 : | const int bound, | ||
175 : | const int x, | ||
176 : | const int y, | ||
177 : | VECTOR * const pmv, | ||
178 : | int32_t * const psad) | ||
179 : | { | ||
180 : | int lx, ly, lz; /* left */ | ||
181 : | int tx, ty, tz; /* top */ | ||
182 : | int rx, ry, rz; /* top-right */ | ||
183 : | int lpos, tpos, rpos; | ||
184 : | int num_cand = 0, last_cand = 1; | ||
185 : | |||
186 : | lx = x - 1; ly = y; lz = 1; | ||
187 : | tx = x; ty = y - 1; tz = 2; | ||
188 : | rx = x + 1; ry = y - 1; rz = 2; | ||
189 : | |||
190 : | lpos = lx + ly * mb_width; | ||
191 : | rpos = rx + ry * mb_width; | ||
192 : | tpos = tx + ty * mb_width; | ||
193 : | |||
194 : | if (lpos >= bound && lx >= 0) { | ||
195 : | num_cand++; | ||
196 : | last_cand = 1; | ||
197 : | pmv[1] = mbs[lpos].mvs[lz]; | ||
198 : | psad[1] = mbs[lpos].sad8[lz]; | ||
199 : | } else { | ||
200 : | pmv[1] = zeroMV; | ||
201 : | psad[1] = MV_MAX_ERROR; | ||
202 : | } | ||
203 : | |||
204 : | if (tpos >= bound) { | ||
205 : | num_cand++; | ||
206 : | last_cand = 2; | ||
207 : | pmv[2]= mbs[tpos].mvs[tz]; | ||
208 : | psad[2] = mbs[tpos].sad8[tz]; | ||
209 : | } else { | ||
210 : | pmv[2] = zeroMV; | ||
211 : | psad[2] = MV_MAX_ERROR; | ||
212 : | } | ||
213 : | |||
214 : | if (rpos >= bound && rx < mb_width) { | ||
215 : | num_cand++; | ||
216 : | last_cand = 3; | ||
217 : | pmv[3] = mbs[rpos].mvs[rz]; | ||
218 : | psad[3] = mbs[rpos].sad8[rz]; | ||
219 : | } else { | ||
220 : | pmv[3] = zeroMV; | ||
221 : | psad[3] = MV_MAX_ERROR; | ||
222 : | } | ||
223 : | |||
224 : | /* original pmvdata() compatibility hack */ | ||
225 : | if (x == 0 && y == 0) { | ||
226 : | pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV; | ||
227 : | psad[0] = 0; | ||
228 : | psad[1] = psad[2] = psad[3] = MV_MAX_ERROR; | ||
229 : | return; | ||
230 : | } | ||
231 : | |||
232 : | /* if only one valid candidate preictor, the invalid candiates are set to the canidate */ | ||
233 : | if (num_cand == 1) { | ||
234 : | pmv[0] = pmv[last_cand]; | ||
235 : | psad[0] = psad[last_cand]; | ||
236 : | return; | ||
237 : | } | ||
238 : | |||
239 : | if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) { | ||
240 : | pmv[0] = pmv[1]; | ||
241 : | psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]); | ||
242 : | return; | ||
243 : | } | ||
244 : | |||
245 : | /* set median, minimum */ | ||
246 : | |||
247 : | pmv[0].x = | ||
248 : | MIN(MAX(pmv[1].x, pmv[2].x), | ||
249 : | MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); | ||
250 : | pmv[0].y = | ||
251 : | MIN(MAX(pmv[1].y, pmv[2].y), | ||
252 : | MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); | ||
253 : | |||
254 : | psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]); | ||
255 : | |||
256 : | } | ||
257 : | |||
258 : | |||
259 : | static void | ||
260 : | ModeDecision_SAD(SearchData * const Data, | ||
261 : | MACROBLOCK * const pMB, | ||
262 : | const MACROBLOCK * const pMBs, | ||
263 : | const int x, const int y, | ||
264 : | const MBParam * const pParam, | ||
265 : | const uint32_t MotionFlags, | ||
266 : | const uint32_t VopFlags, | ||
267 : | const uint32_t VolFlags, | ||
268 : | const IMAGE * const pCurrent, | ||
269 : | const IMAGE * const pRef, | ||
270 : | const IMAGE * const vGMC, | ||
271 : | syskin | 1.5 | const int coding_type, |
272 : | const int skip_sad) | ||
273 : | edgomez | 1.2 | { |
274 : | int mode = MODE_INTER; | ||
275 : | int mcsel = 0; | ||
276 : | int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); | ||
277 : | const uint32_t iQuant = pMB->quant; | ||
278 : | |||
279 : | const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); | ||
280 : | |||
281 : | int sad; | ||
282 : | int InterBias = MV16_INTER_BIAS; | ||
283 : | |||
284 : | pMB->mcsel = 0; | ||
285 : | |||
286 : | if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + | ||
287 : | Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { | ||
288 : | mode = MODE_INTER; | ||
289 : | sad = Data->iMinSAD[0]; | ||
290 : | } else { | ||
291 : | mode = MODE_INTER4V; | ||
292 : | sad = Data->iMinSAD[1] + Data->iMinSAD[2] + | ||
293 : | Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant; | ||
294 : | Data->iMinSAD[0] = sad; | ||
295 : | } | ||
296 : | |||
297 : | /* final skip decision, a.k.a. "the vector you found, really that good?" */ | ||
298 : | syskin | 1.5 | if (skip_possible && (skip_sad < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
299 : | if ( (100*skip_sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) | ||
300 : | syskin | 1.9 | if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant)) { |
301 : | edgomez | 1.2 | mode = MODE_NOT_CODED; |
302 : | sad = 0; | ||
303 : | } | ||
304 : | |||
305 : | /* mcsel */ | ||
306 : | if (coding_type == S_VOP) { | ||
307 : | |||
308 : | int32_t iSAD = sad16(Data->Cur, | ||
309 : | vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); | ||
310 : | |||
311 : | if (Data->chroma) { | ||
312 : | iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); | ||
313 : | iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); | ||
314 : | } | ||
315 : | |||
316 : | if (iSAD <= sad) { /* mode decision GMC */ | ||
317 : | mode = MODE_INTER; | ||
318 : | mcsel = 1; | ||
319 : | sad = iSAD; | ||
320 : | } | ||
321 : | } | ||
322 : | |||
323 : | /* intra decision */ | ||
324 : | |||
325 : | if (iQuant > 10) InterBias += 60 * (iQuant - 10); /* to make high quants work */ | ||
326 : | if (y != 0) | ||
327 : | if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; | ||
328 : | if (x != 0) | ||
329 : | if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; | ||
330 : | |||
331 : | if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */ | ||
332 : | |||
333 : | if (InterBias < sad) { | ||
334 : | syskin | 1.9 | int32_t deviation = dev16(Data->Cur, Data->iEdgedWidth); |
335 : | edgomez | 1.2 | if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
336 : | } | ||
337 : | |||
338 : | pMB->cbp = 63; | ||
339 : | pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; | ||
340 : | |||
341 : | if (mode == MODE_INTER && mcsel == 0) { | ||
342 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; | ||
343 : | |||
344 : | if(Data->qpel) { | ||
345 : | pMB->qmvs[0] = pMB->qmvs[1] | ||
346 : | = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; | ||
347 : | pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; | ||
348 : | pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; | ||
349 : | } else { | ||
350 : | pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; | ||
351 : | pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; | ||
352 : | } | ||
353 : | |||
354 : | } else if (mode == MODE_INTER ) { /* but mcsel == 1 */ | ||
355 : | |||
356 : | pMB->mcsel = 1; | ||
357 : | if (Data->qpel) { | ||
358 : | pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; | ||
359 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; | ||
360 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; | ||
361 : | } else | ||
362 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; | ||
363 : | |||
364 : | } else | ||
365 : | if (mode == MODE_INTER4V) ; /* anything here? */ | ||
366 : | else /* INTRA, NOT_CODED */ | ||
367 : | ZeroMacroblockP(pMB, 0); | ||
368 : | |||
369 : | pMB->mode = mode; | ||
370 : | } | ||
371 : | |||
372 : | static __inline void | ||
373 : | PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount, | ||
374 : | syskin | 1.9 | int iHcount, const MACROBLOCK * const prevMB) |
375 : | edgomez | 1.2 | { |
376 : | |||
377 : | if ( (y != 0) && (x < (iWcount-1)) ) { /* [5] top-right neighbour */ | ||
378 : | pmv[5].x = EVEN(pmv[3].x); | ||
379 : | pmv[5].y = EVEN(pmv[3].y); | ||
380 : | } else pmv[5].x = pmv[5].y = 0; | ||
381 : | |||
382 : | if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }/* pmv[3] is left neighbour */ | ||
383 : | else pmv[3].x = pmv[3].y = 0; | ||
384 : | |||
385 : | if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }/* [4] top neighbour */ | ||
386 : | else pmv[4].x = pmv[4].y = 0; | ||
387 : | |||
388 : | /* [1] median prediction */ | ||
389 : | pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y); | ||
390 : | |||
391 : | pmv[0].x = pmv[0].y = 0; /* [0] is zero; not used in the loop (checked before) but needed here for make_mask */ | ||
392 : | |||
393 : | pmv[2].x = EVEN(prevMB->mvs[0].x); /* [2] is last frame */ | ||
394 : | pmv[2].y = EVEN(prevMB->mvs[0].y); | ||
395 : | |||
396 : | if ((x < iWcount-1) && (y < iHcount-1)) { | ||
397 : | pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */ | ||
398 : | pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y); | ||
399 : | } else pmv[6].x = pmv[6].y = 0; | ||
400 : | } | ||
401 : | |||
402 : | static void | ||
403 : | Search8(SearchData * const OldData, | ||
404 : | const int x, const int y, | ||
405 : | const uint32_t MotionFlags, | ||
406 : | const MBParam * const pParam, | ||
407 : | MACROBLOCK * const pMB, | ||
408 : | const MACROBLOCK * const pMBs, | ||
409 : | const int block, | ||
410 : | SearchData * const Data) | ||
411 : | { | ||
412 : | int i = 0; | ||
413 : | syskin | 1.4 | VECTOR vbest_q; int32_t sbest_q; |
414 : | edgomez | 1.2 | *Data->iMinSAD = *(OldData->iMinSAD + 1 + block); |
415 : | *Data->currentMV = *(OldData->currentMV + 1 + block); | ||
416 : | *Data->currentQMV = *(OldData->currentQMV + 1 + block); | ||
417 : | |||
418 : | if(Data->qpel) { | ||
419 : | Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); | ||
420 : | if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y, | ||
421 : | syskin | 1.9 | Data->predMV, Data->iFcode, 0); |
422 : | edgomez | 1.2 | } else { |
423 : | Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); | ||
424 : | if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y, | ||
425 : | syskin | 1.9 | Data->predMV, Data->iFcode, 0); |
426 : | edgomez | 1.2 | } |
427 : | |||
428 : | Isibaar | 1.16 | *(Data->iMinSAD) += (Data->lambda8 * i); |
429 : | edgomez | 1.2 | |
430 : | if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) { | ||
431 : | |||
432 : | syskin | 1.4 | vbest_q = Data->currentQMV[0]; |
433 : | sbest_q = Data->iMinSAD[0]; | ||
434 : | |||
435 : | edgomez | 1.2 | |
436 : | syskin | 1.9 | Data->RefP[0] = OldData->RefP[0] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
437 : | Data->RefP[1] = OldData->RefP[1] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); | ||
438 : | Data->RefP[2] = OldData->RefP[2] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); | ||
439 : | Data->RefP[3] = OldData->RefP[3] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); | ||
440 : | edgomez | 1.2 | |
441 : | syskin | 1.9 | Data->Cur = OldData->Cur + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
442 : | edgomez | 1.2 | Data->qpel_precision = 0; |
443 : | |||
444 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, | ||
445 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); |
446 : | edgomez | 1.2 | |
447 : | if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) { | ||
448 : | |||
449 : | MainSearchFunc *MainSearchPtr; | ||
450 : | if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch; | ||
451 : | else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch; | ||
452 : | else MainSearchPtr = xvid_me_DiamondSearch; | ||
453 : | |||
454 : | syskin | 1.9 | MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate8); |
455 : | edgomez | 1.2 | } |
456 : | |||
457 : | syskin | 1.4 | if(!Data->qpel) { |
458 : | /* halfpel mode */ | ||
459 : | if (MotionFlags & XVID_ME_HALFPELREFINE8) | ||
460 : | syskin | 1.8 | /* perform halfpel refine of current best vector */ |
461 : | syskin | 1.9 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0); |
462 : | syskin | 1.4 | } else { |
463 : | /* qpel mode */ | ||
464 : | Data->currentQMV->x = 2*Data->currentMV->x; | ||
465 : | Data->currentQMV->y = 2*Data->currentMV->y; | ||
466 : | |||
467 : | if(MotionFlags & XVID_ME_FASTREFINE8) { | ||
468 : | /* fast */ | ||
469 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, | ||
470 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode, 2); |
471 : | syskin | 1.4 | FullRefine_Fast(Data, CheckCandidate8, 0); |
472 : | } else if(MotionFlags & XVID_ME_QUARTERPELREFINE8) { | ||
473 : | /* full */ | ||
474 : | if (MotionFlags & XVID_ME_HALFPELREFINE8) { | ||
475 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0); /* hpel part */ |
476 : | syskin | 1.4 | Data->currentQMV->x = 2*Data->currentMV->x; |
477 : | Data->currentQMV->y = 2*Data->currentMV->y; | ||
478 : | } | ||
479 : | edgomez | 1.2 | |
480 : | syskin | 1.4 | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, |
481 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode, 2); |
482 : | syskin | 1.4 | Data->qpel_precision = 1; |
483 : | edgomez | 1.2 | |
484 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate8, 0); /* qpel part */ |
485 : | edgomez | 1.2 | } |
486 : | } | ||
487 : | |||
488 : | syskin | 1.4 | if (sbest_q <= Data->iMinSAD[0]) /* we have not found a better match */ |
489 : | Data->currentQMV[0] = vbest_q; | ||
490 : | edgomez | 1.2 | |
491 : | } | ||
492 : | |||
493 : | syskin | 1.9 | if(Data->qpel) { |
494 : | edgomez | 1.2 | pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x; |
495 : | pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y; | ||
496 : | pMB->qmvs[block] = *Data->currentQMV; | ||
497 : | } else { | ||
498 : | pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x; | ||
499 : | pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y; | ||
500 : | } | ||
501 : | |||
502 : | *(OldData->iMinSAD + 1 + block) = *Data->iMinSAD; | ||
503 : | *(OldData->currentMV + 1 + block) = *Data->currentMV; | ||
504 : | *(OldData->currentQMV + 1 + block) = *Data->currentQMV; | ||
505 : | |||
506 : | pMB->mvs[block] = *Data->currentMV; | ||
507 : | pMB->sad8[block] = 4 * *Data->iMinSAD; | ||
508 : | } | ||
509 : | |||
510 : | |||
511 : | |||
512 : | static void | ||
513 : | SearchP(const IMAGE * const pRef, | ||
514 : | const uint8_t * const pRefH, | ||
515 : | const uint8_t * const pRefV, | ||
516 : | const uint8_t * const pRefHV, | ||
517 : | const IMAGE * const pCur, | ||
518 : | const int x, | ||
519 : | const int y, | ||
520 : | const uint32_t MotionFlags, | ||
521 : | const uint32_t VopFlags, | ||
522 : | SearchData * const Data, | ||
523 : | const MBParam * const pParam, | ||
524 : | const MACROBLOCK * const pMBs, | ||
525 : | const MACROBLOCK * const prevMBs, | ||
526 : | MACROBLOCK * const pMB) | ||
527 : | { | ||
528 : | |||
529 : | int i, threshA; | ||
530 : | VECTOR pmv[7]; | ||
531 : | int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); | ||
532 : | CheckFunc * CheckCandidate; | ||
533 : | |||
534 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
535 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); |
536 : | edgomez | 1.2 | |
537 : | get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, pmv, Data->temp); | ||
538 : | |||
539 : | Data->chromaX = Data->chromaY = 0; /* chroma-sad cache */ | ||
540 : | syskin | 1.9 | Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
541 : | Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; | ||
542 : | Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; | ||
543 : | |||
544 : | Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16; | ||
545 : | Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16; | ||
546 : | Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16; | ||
547 : | Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16; | ||
548 : | Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; | ||
549 : | Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; | ||
550 : | edgomez | 1.2 | |
551 : | Data->lambda16 = xvid_me_lambda_vec16[pMB->quant]; | ||
552 : | Data->lambda8 = xvid_me_lambda_vec8[pMB->quant]; | ||
553 : | Data->qpel_precision = 0; | ||
554 : | Data->dir = 0; | ||
555 : | |||
556 : | memset(Data->currentMV, 0, 5*sizeof(VECTOR)); | ||
557 : | |||
558 : | if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); | ||
559 : | else Data->predMV = pmv[0]; | ||
560 : | |||
561 : | syskin | 1.9 | i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0); |
562 : | Isibaar | 1.16 | Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i); |
563 : | Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i); | ||
564 : | edgomez | 1.2 | Data->iMinSAD[2] = pMB->sad8[1]; |
565 : | Data->iMinSAD[3] = pMB->sad8[2]; | ||
566 : | Data->iMinSAD[4] = pMB->sad8[3]; | ||
567 : | |||
568 : | if ((!(VopFlags & XVID_VOP_MODEDECISION_RD)) && (x | y)) { | ||
569 : | threshA = Data->temp[0]; /* that's where we keep this SAD atm */ | ||
570 : | if (threshA < 512) threshA = 512; | ||
571 : | else if (threshA > 1024) threshA = 1024; | ||
572 : | } else | ||
573 : | threshA = 512; | ||
574 : | |||
575 : | PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, | ||
576 : | syskin | 1.9 | prevMBs + x + y * pParam->mb_width); |
577 : | edgomez | 1.2 | |
578 : | syskin | 1.9 | if (inter4v) CheckCandidate = CheckCandidate16; |
579 : | else CheckCandidate = CheckCandidate16no4v; /* for extra speed */ | ||
580 : | edgomez | 1.2 | |
581 : | /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/ | ||
582 : | |||
583 : | for (i = 1; i < 7; i++) | ||
584 : | if (!vector_repeats(pmv, i)) { | ||
585 : | CheckCandidate(pmv[i].x, pmv[i].y, Data, i); | ||
586 : | if (Data->iMinSAD[0] <= threshA) { i++; break; } | ||
587 : | } | ||
588 : | |||
589 : | if ((Data->iMinSAD[0] <= threshA) || | ||
590 : | (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && | ||
591 : | (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) | ||
592 : | inter4v = 0; | ||
593 : | else { | ||
594 : | |||
595 : | MainSearchFunc * MainSearchPtr; | ||
596 : | int mask = make_mask(pmv, i, Data->dir); /* all vectors pmv[0..i-1] have been checked */ | ||
597 : | |||
598 : | if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; | ||
599 : | else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; | ||
600 : | else MainSearchPtr = xvid_me_DiamondSearch; | ||
601 : | |||
602 : | MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate); | ||
603 : | |||
604 : | /* extended search, diamond starting in 0,0 and in prediction. | ||
605 : | note that this search is/might be done in halfpel positions, | ||
606 : | which makes it more different than the diamond above */ | ||
607 : | |||
608 : | if (MotionFlags & XVID_ME_EXTSEARCH16) { | ||
609 : | int32_t bSAD; | ||
610 : | VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0]; | ||
611 : | if (Data->qpel) { | ||
612 : | startMV.x /= 2; | ||
613 : | startMV.y /= 2; | ||
614 : | } | ||
615 : | if (!(MVequal(startMV, backupMV))) { | ||
616 : | bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; | ||
617 : | |||
618 : | CheckCandidate(startMV.x, startMV.y, Data, 255); | ||
619 : | xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate); | ||
620 : | if (bSAD < Data->iMinSAD[0]) { | ||
621 : | Data->currentMV[0] = backupMV; | ||
622 : | syskin | 1.9 | Data->iMinSAD[0] = bSAD; |
623 : | } | ||
624 : | edgomez | 1.2 | } |
625 : | |||
626 : | backupMV = Data->currentMV[0]; | ||
627 : | startMV.x = startMV.y = 1; | ||
628 : | if (!(MVequal(startMV, backupMV))) { | ||
629 : | bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; | ||
630 : | |||
631 : | CheckCandidate(startMV.x, startMV.y, Data, 255); | ||
632 : | xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate); | ||
633 : | if (bSAD < Data->iMinSAD[0]) { | ||
634 : | Data->currentMV[0] = backupMV; | ||
635 : | Data->iMinSAD[0] = bSAD; | ||
636 : | } | ||
637 : | } | ||
638 : | } | ||
639 : | } | ||
640 : | |||
641 : | |||
642 : | syskin | 1.4 | if(!Data->qpel) { |
643 : | /* halfpel mode */ | ||
644 : | if (MotionFlags & XVID_ME_HALFPELREFINE16) | ||
645 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); |
646 : | syskin | 1.4 | } else { |
647 : | /* qpel mode */ | ||
648 : | |||
649 : | for(i = 0; i < 5; i++) { | ||
650 : | Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */ | ||
651 : | Data->currentQMV[i].y = 2 * Data->currentMV[i].y; | ||
652 : | } | ||
653 : | if(MotionFlags & XVID_ME_FASTREFINE16 && MotionFlags & XVID_ME_QUARTERPELREFINE16) { | ||
654 : | /* fast */ | ||
655 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
656 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode, 2); |
657 : | syskin | 1.4 | FullRefine_Fast(Data, CheckCandidate, 0); |
658 : | } else { | ||
659 : | if(MotionFlags & (XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE16_RD)) { | ||
660 : | /* full */ | ||
661 : | if (MotionFlags & XVID_ME_HALFPELREFINE16) { | ||
662 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); /* hpel part */ |
663 : | syskin | 1.4 | for(i = 0; i < 5; i++) { |
664 : | Data->currentQMV[i].x = 2 * Data->currentMV[i].x; | ||
665 : | Data->currentQMV[i].y = 2 * Data->currentMV[i].y; | ||
666 : | } | ||
667 : | } | ||
668 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
669 : | syskin | 1.9 | pParam->width, pParam->height, Data->iFcode, 2); |
670 : | syskin | 1.4 | Data->qpel_precision = 1; |
671 : | if(MotionFlags & XVID_ME_QUARTERPELREFINE16) | ||
672 : | syskin | 1.8 | xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate, 0); /* qpel part */ |
673 : | syskin | 1.4 | } |
674 : | edgomez | 1.2 | } |
675 : | } | ||
676 : | |||
677 : | syskin | 1.9 | if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30 * ((MotionFlags & XVID_ME_FASTREFINE16) ? 8 : 1)) |
678 : | edgomez | 1.2 | inter4v = 0; |
679 : | |||
680 : | if (inter4v) { | ||
681 : | SearchData Data8; | ||
682 : | memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */ | ||
683 : | |||
684 : | Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); | ||
685 : | Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); | ||
686 : | Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); | ||
687 : | Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); | ||
688 : | |||
689 : | if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) { | ||
690 : | 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 */ |
691 : | edgomez | 1.2 | int sumx = 0, sumy = 0; |
692 : | |||
693 : | if (Data->qpel) | ||
694 : | for (i = 1; i < 5; i++) { | ||
695 : | sumx += Data->currentQMV[i].x/2; | ||
696 : | sumy += Data->currentQMV[i].y/2; | ||
697 : | } | ||
698 : | else | ||
699 : | for (i = 1; i < 5; i++) { | ||
700 : | sumx += Data->currentMV[i].x; | ||
701 : | sumy += Data->currentMV[i].y; | ||
702 : | } | ||
703 : | |||
704 : | Data->iMinSAD[1] += xvid_me_ChromaSAD((sumx >> 3) + roundtab_76[sumx & 0xf], | ||
705 : | (sumy >> 3) + roundtab_76[sumy & 0xf], Data); | ||
706 : | } | ||
707 : | } else Data->iMinSAD[1] = 4096*256; | ||
708 : | } | ||
709 : | |||
710 : | syskin | 1.18 | static int |
711 : | InitialSkipDecisionP(int sad00, | ||
712 : | const MBParam * pParam, | ||
713 : | const FRAMEINFO * current, | ||
714 : | MACROBLOCK * pMB, | ||
715 : | const MACROBLOCK * prevMB, | ||
716 : | int x, int y, | ||
717 : | const SearchData * Data, | ||
718 : | const IMAGE * const pGMC, | ||
719 : | const IMAGE * const pCurrent, | ||
720 : | const IMAGE * const pRef, | ||
721 : | const uint32_t MotionFlags) | ||
722 : | { | ||
723 : | const unsigned int iEdgedWidth = pParam->edged_width; | ||
724 : | |||
725 : | int skip_thresh = INITIAL_SKIP_THRESH * \ | ||
726 : | (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1); | ||
727 : | int stat_thresh = 0; | ||
728 : | |||
729 : | /* initial skip decision */ | ||
730 : | if (current->coding_type != S_VOP) { /* no fast SKIP for S(GMC)-VOPs */ | ||
731 : | if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) | ||
732 : | if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant)) { | ||
733 : | ZeroMacroblockP(pMB, sad00); | ||
734 : | pMB->mode = MODE_NOT_CODED; | ||
735 : | return 1; | ||
736 : | } | ||
737 : | } | ||
738 : | |||
739 : | if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) { | ||
740 : | VECTOR *cmpMV; | ||
741 : | VECTOR staticMV = { 0, 0 }; | ||
742 : | const MACROBLOCK * pMBs = current->mbs; | ||
743 : | |||
744 : | if (current->coding_type == S_VOP) | ||
745 : | cmpMV = &pMB->amv; | ||
746 : | else | ||
747 : | cmpMV = &staticMV; | ||
748 : | |||
749 : | if(x > 0 && y > 0 && x < pParam->mb_width) { | ||
750 : | if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], *cmpMV) && | ||
751 : | MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) && | ||
752 : | MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) && | ||
753 : | MVequal(prevMB->mvs[0], *cmpMV)) { | ||
754 : | stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16, | ||
755 : | MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16, | ||
756 : | MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16, | ||
757 : | prevMB->sad16))); | ||
758 : | } else { | ||
759 : | stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16, | ||
760 : | MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16, | ||
761 : | MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16, | ||
762 : | prevMB->sad16))); | ||
763 : | } | ||
764 : | } | ||
765 : | } | ||
766 : | |||
767 : | /* favorize (0,0) or global vector for cartoons */ | ||
768 : | if (current->vop_flags & XVID_VOP_CARTOON) { | ||
769 : | if (current->coding_type == S_VOP) { | ||
770 : | int32_t iSAD = sad16(pCurrent->y + (x + y * iEdgedWidth) * 16, | ||
771 : | pGMC->y + 16*y*iEdgedWidth + 16*x, iEdgedWidth, 65536); | ||
772 : | |||
773 : | if (Data->chroma) { | ||
774 : | iSAD += sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, pGMC->u + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2); | ||
775 : | iSAD += sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, pGMC->v + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2); | ||
776 : | } | ||
777 : | |||
778 : | if (iSAD <= stat_thresh) { /* mode decision GMC */ | ||
779 : | pMB->mode = MODE_INTER; | ||
780 : | pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = iSAD; | ||
781 : | pMB->mcsel = 1; | ||
782 : | if (Data->qpel) { | ||
783 : | pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; | ||
784 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; | ||
785 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; | ||
786 : | } else | ||
787 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; | ||
788 : | |||
789 : | return 1; | ||
790 : | } | ||
791 : | } | ||
792 : | else if (sad00 < stat_thresh) { | ||
793 : | VECTOR predMV; | ||
794 : | if (Data->qpel) | ||
795 : | predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); | ||
796 : | else | ||
797 : | predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); | ||
798 : | |||
799 : | ZeroMacroblockP(pMB, sad00); | ||
800 : | pMB->cbp = 0x3f; | ||
801 : | pMB->pmvs[0].x = - predMV.x; | ||
802 : | pMB->pmvs[0].y = - predMV.y; | ||
803 : | return 1; | ||
804 : | } | ||
805 : | } | ||
806 : | |||
807 : | return 0; | ||
808 : | } | ||
809 : | |||
810 : | edgomez | 1.2 | static __inline uint32_t |
811 : | MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags) | ||
812 : | { | ||
813 : | uint32_t Flags = MotionFlags; | ||
814 : | |||
815 : | if (!(VopFlags & XVID_VOP_MODEDECISION_RD)) | ||
816 : | Flags &= ~(XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD+XVID_ME_EXTSEARCH_RD); | ||
817 : | |||
818 : | if (Flags & XVID_ME_EXTSEARCH_RD) | ||
819 : | Flags |= XVID_ME_HALFPELREFINE16_RD; | ||
820 : | |||
821 : | if (Flags & XVID_ME_EXTSEARCH_RD && MotionFlags & XVID_ME_EXTSEARCH8) | ||
822 : | Flags |= XVID_ME_HALFPELREFINE8_RD; | ||
823 : | |||
824 : | if (Flags & XVID_ME_HALFPELREFINE16_RD) | ||
825 : | Flags |= XVID_ME_QUARTERPELREFINE16_RD; | ||
826 : | |||
827 : | if (Flags & XVID_ME_HALFPELREFINE8_RD) { | ||
828 : | Flags |= XVID_ME_QUARTERPELREFINE8_RD; | ||
829 : | Flags &= ~XVID_ME_HALFPELREFINE8; | ||
830 : | } | ||
831 : | |||
832 : | if (Flags & XVID_ME_QUARTERPELREFINE8_RD) | ||
833 : | Flags &= ~XVID_ME_QUARTERPELREFINE8; | ||
834 : | |||
835 : | edgomez | 1.3 | if (Flags & XVID_ME_QUARTERPELREFINE16_RD) |
836 : | Flags &= ~XVID_ME_QUARTERPELREFINE16; | ||
837 : | |||
838 : | edgomez | 1.2 | if (!(VolFlags & XVID_VOL_QUARTERPEL)) |
839 : | Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD); | ||
840 : | |||
841 : | if (!(VopFlags & XVID_VOP_HALFPEL)) | ||
842 : | Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD); | ||
843 : | |||
844 : | syskin | 1.9 | if (VopFlags & XVID_VOP_GREYSCALE) |
845 : | edgomez | 1.2 | Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP); |
846 : | |||
847 : | syskin | 1.4 | if (Flags & XVID_ME_FASTREFINE8) |
848 : | Flags &= ~XVID_ME_HALFPELREFINE8_RD; | ||
849 : | |||
850 : | if (Flags & XVID_ME_FASTREFINE16) | ||
851 : | Flags &= ~XVID_ME_HALFPELREFINE16_RD; | ||
852 : | |||
853 : | edgomez | 1.2 | return Flags; |
854 : | } | ||
855 : | |||
856 : | syskin | 1.10 | static __inline void |
857 : | motionStatsPVOP(int * const MVmax, int * const mvCount, int * const mvSum, | ||
858 : | const MACROBLOCK * const pMB, const int qpel) | ||
859 : | { | ||
860 : | const VECTOR * const mv = qpel ? pMB->qmvs : pMB->mvs; | ||
861 : | int i; | ||
862 : | int max = *MVmax; | ||
863 : | |||
864 : | switch (pMB->mode) { | ||
865 : | case MODE_INTER4V: | ||
866 : | *mvCount += 3; | ||
867 : | for(i = 3; i; i--) { | ||
868 : | if (mv[i].x > max) max = mv[i].x; | ||
869 : | else if (-mv[i].x - 1 > max) max = -mv[i].x - 1; | ||
870 : | *mvSum += mv[i].x * mv[i].x; | ||
871 : | if (mv[i].y > max) max = mv[i].y; | ||
872 : | else if (-mv[i].y - 1 > max) max = -mv[i].y - 1; | ||
873 : | *mvSum += mv[i].y * mv[i].y; | ||
874 : | } | ||
875 : | case MODE_INTER: | ||
876 : | syskin | 1.12 | (*mvCount)++; |
877 : | syskin | 1.10 | *mvSum += mv[0].x * mv[0].x; |
878 : | *mvSum += mv[0].y * mv[0].y; | ||
879 : | syskin | 1.12 | if (mv[0].x > max) max = mv[0].x; |
880 : | else if (-mv[0].x - 1 > max) max = -mv[0].x - 1; | ||
881 : | if (mv[0].y > max) max = mv[0].y; | ||
882 : | else if (-mv[0].y - 1 > max) max = -mv[0].y - 1; | ||
883 : | *MVmax = max; | ||
884 : | syskin | 1.10 | default: |
885 : | break; | ||
886 : | } | ||
887 : | } | ||
888 : | |||
889 : | syskin | 1.18 | void |
890 : | edgomez | 1.2 | MotionEstimation(MBParam * const pParam, |
891 : | FRAMEINFO * const current, | ||
892 : | FRAMEINFO * const reference, | ||
893 : | const IMAGE * const pRefH, | ||
894 : | const IMAGE * const pRefV, | ||
895 : | const IMAGE * const pRefHV, | ||
896 : | syskin | 1.18 | const IMAGE * const pGMC, |
897 : | edgomez | 1.2 | const uint32_t iLimit) |
898 : | { | ||
899 : | MACROBLOCK *const pMBs = current->mbs; | ||
900 : | const IMAGE *const pCurrent = ¤t->image; | ||
901 : | const IMAGE *const pRef = &reference->image; | ||
902 : | |||
903 : | syskin | 1.9 | const uint32_t mb_width = pParam->mb_width; |
904 : | const uint32_t mb_height = pParam->mb_height; | ||
905 : | edgomez | 1.2 | const uint32_t iEdgedWidth = pParam->edged_width; |
906 : | const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags); | ||
907 : | int stat_thresh = 0; | ||
908 : | syskin | 1.10 | int MVmax = 0, mvSum = 0, mvCount = 0; |
909 : | edgomez | 1.2 | |
910 : | uint32_t x, y; | ||
911 : | syskin | 1.18 | int sad00; |
912 : | edgomez | 1.2 | int skip_thresh = INITIAL_SKIP_THRESH * \ |
913 : | (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1); | ||
914 : | syskin | 1.18 | int block = 0; |
915 : | edgomez | 1.2 | |
916 : | /* some pre-initialized thingies for SearchP */ | ||
917 : | DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); | ||
918 : | SearchData Data; | ||
919 : | memset(&Data, 0, sizeof(SearchData)); | ||
920 : | Data.iEdgedWidth = iEdgedWidth; | ||
921 : | Data.iFcode = current->fcode; | ||
922 : | Data.rounding = pParam->m_rounding_type; | ||
923 : | Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0); | ||
924 : | Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP; | ||
925 : | Data.dctSpace = dct_space; | ||
926 : | Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); | ||
927 : | Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices; | ||
928 : | |||
929 : | Data.RefQ = pRefV->u; /* a good place, also used in MC (for similar purpose) */ | ||
930 : | if (sadInit) (*sadInit) (); | ||
931 : | |||
932 : | for (y = 0; y < mb_height; y++) { | ||
933 : | for (x = 0; x < mb_width; x++) { | ||
934 : | syskin | 1.18 | MACROBLOCK *pMB = &pMBs[block]; |
935 : | MACROBLOCK *prevMB = &reference->mbs[block]; | ||
936 : | int skip; | ||
937 : | block++; | ||
938 : | edgomez | 1.2 | |
939 : | syskin | 1.9 | pMB->sad16 = |
940 : | edgomez | 1.2 | sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16, |
941 : | pRef->y + (x + y * iEdgedWidth) * 16, | ||
942 : | syskin | 1.9 | pParam->edged_width, pMB->sad8); |
943 : | syskin | 1.18 | |
944 : | syskin | 1.5 | sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3])); |
945 : | edgomez | 1.2 | |
946 : | if (Data.chroma) { | ||
947 : | Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, | ||
948 : | pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2) | ||
949 : | + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, | ||
950 : | pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2); | ||
951 : | pMB->sad16 += Data.chromaSAD; | ||
952 : | syskin | 1.5 | sad00 += Data.chromaSAD; |
953 : | edgomez | 1.2 | } |
954 : | |||
955 : | syskin | 1.18 | skip = InitialSkipDecisionP(sad00, pParam, current, pMB, prevMB, x, y, &Data, pGMC, |
956 : | pCurrent, pRef, MotionFlags); | ||
957 : | if (skip) continue; | ||
958 : | edgomez | 1.2 | |
959 : | SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, | ||
960 : | y, MotionFlags, current->vop_flags, | ||
961 : | &Data, pParam, pMBs, reference->mbs, pMB); | ||
962 : | |||
963 : | if (current->vop_flags & XVID_VOP_MODEDECISION_RD) | ||
964 : | xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam, | ||
965 : | MotionFlags, current->vop_flags, current->vol_flags, | ||
966 : | pCurrent, pRef, pGMC, current->coding_type); | ||
967 : | |||
968 : | else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD) | ||
969 : | xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam, | ||
970 : | MotionFlags, current->vop_flags, current->vol_flags, | ||
971 : | pCurrent, pRef, pGMC, current->coding_type); | ||
972 : | else | ||
973 : | ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam, | ||
974 : | MotionFlags, current->vop_flags, current->vol_flags, | ||
975 : | syskin | 1.5 | pCurrent, pRef, pGMC, current->coding_type, sad00); |
976 : | edgomez | 1.2 | |
977 : | |||
978 : | syskin | 1.10 | motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel); |
979 : | edgomez | 1.2 | } |
980 : | } | ||
981 : | |||
982 : | syskin | 1.10 | current->fcode = getMinFcode(MVmax); |
983 : | current->sStat.iMvSum = mvSum; | ||
984 : | current->sStat.iMvCount = mvCount; | ||
985 : | edgomez | 1.11 | } |
986 : | syskin | 1.19 | |
987 : | void | ||
988 : | MotionEstimateSMP(SMPmotionData * h) | ||
989 : | { | ||
990 : | const MBParam * const pParam = h->pParam; | ||
991 : | const FRAMEINFO * const current = h->current; | ||
992 : | const FRAMEINFO * const reference = h->reference; | ||
993 : | const IMAGE * const pRefH = h->pRefH; | ||
994 : | const IMAGE * const pRefV = h->pRefV; | ||
995 : | const IMAGE * const pRefHV = h->pRefHV; | ||
996 : | const IMAGE * const pGMC = h->pGMC; | ||
997 : | uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, | ||
998 : | current->vop_flags, | ||
999 : | current->vol_flags); | ||
1000 : | |||
1001 : | MACROBLOCK *const pMBs = current->mbs; | ||
1002 : | const IMAGE *const pCurrent = ¤t->image; | ||
1003 : | const IMAGE *const pRef = &reference->image; | ||
1004 : | |||
1005 : | const uint32_t mb_width = pParam->mb_width; | ||
1006 : | const uint32_t mb_height = pParam->mb_height; | ||
1007 : | const uint32_t iEdgedWidth = pParam->edged_width; | ||
1008 : | int stat_thresh = 0; | ||
1009 : | int MVmax = 0, mvSum = 0, mvCount = 0; | ||
1010 : | int y_step = h->y_step; | ||
1011 : | int start_y = h->start_y; | ||
1012 : | |||
1013 : | uint32_t x, y; | ||
1014 : | int sad00; | ||
1015 : | int skip_thresh = INITIAL_SKIP_THRESH * \ | ||
1016 : | (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1); | ||
1017 : | int block = start_y*mb_width; | ||
1018 : | int * complete_count_self = h->complete_count_self; | ||
1019 : | syskin | 1.21 | const volatile int * complete_count_above = h->complete_count_above; |
1020 : | syskin | 1.19 | int max_mbs; |
1021 : | int current_mb = 0; | ||
1022 : | |||
1023 : | /* some pre-initialized thingies for SearchP */ | ||
1024 : | DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); | ||
1025 : | SearchData Data; | ||
1026 : | memset(&Data, 0, sizeof(SearchData)); | ||
1027 : | Data.iEdgedWidth = iEdgedWidth; | ||
1028 : | Data.iFcode = current->fcode; | ||
1029 : | Data.rounding = pParam->m_rounding_type; | ||
1030 : | Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0); | ||
1031 : | Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP; | ||
1032 : | Data.dctSpace = dct_space; | ||
1033 : | Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); | ||
1034 : | Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices; | ||
1035 : | |||
1036 : | /* todo: sort out temp memory space */ | ||
1037 : | Data.RefQ = h->RefQ; | ||
1038 : | if (sadInit) (*sadInit) (); | ||
1039 : | |||
1040 : | max_mbs = 0; | ||
1041 : | |||
1042 : | for (y = start_y; y < mb_height; y += y_step) { | ||
1043 : | if (y == 0) max_mbs = mb_width; /* we can process all blocks of the first row */ | ||
1044 : | |||
1045 : | for (x = 0; x < mb_width; x++) { | ||
1046 : | |||
1047 : | MACROBLOCK *pMB, *prevMB; | ||
1048 : | int skip; | ||
1049 : | |||
1050 : | if (current_mb >= max_mbs) { | ||
1051 : | /* we ME-ed all macroblocks we safely could. grab next portion */ | ||
1052 : | int above_count = *complete_count_above; /* sync point */ | ||
1053 : | if (above_count == mb_width) { | ||
1054 : | /* full line above is ready */ | ||
1055 : | above_count = mb_width+1; | ||
1056 : | if (y < mb_height-y_step) { | ||
1057 : | /* this is not last line, grab a portion of MBs from the next line too */ | ||
1058 : | above_count += MAX(0, complete_count_above[1] - 1); | ||
1059 : | } | ||
1060 : | } | ||
1061 : | |||
1062 : | max_mbs = current_mb + above_count - x - 1; | ||
1063 : | |||
1064 : | if (current_mb >= max_mbs) { | ||
1065 : | /* current workload is zero */ | ||
1066 : | x--; | ||
1067 : | sched_yield(); | ||
1068 : | continue; | ||
1069 : | } | ||
1070 : | } | ||
1071 : | |||
1072 : | syskin | 1.21 | |
1073 : | pMB = &pMBs[block]; | ||
1074 : | prevMB = &reference->mbs[block]; | ||
1075 : | |||
1076 : | pMB->sad16 = | ||
1077 : | sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16, | ||
1078 : | pRef->y + (x + y * iEdgedWidth) * 16, | ||
1079 : | pParam->edged_width, pMB->sad8); | ||
1080 : | |||
1081 : | sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3])); | ||
1082 : | |||
1083 : | if (Data.chroma) { | ||
1084 : | Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, | ||
1085 : | pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2) | ||
1086 : | + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, | ||
1087 : | pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2); | ||
1088 : | pMB->sad16 += Data.chromaSAD; | ||
1089 : | sad00 += Data.chromaSAD; | ||
1090 : | } | ||
1091 : | |||
1092 : | syskin | 1.19 | skip = InitialSkipDecisionP(sad00, pParam, current, pMB, prevMB, x, y, &Data, pGMC, |
1093 : | pCurrent, pRef, MotionFlags); | ||
1094 : | |||
1095 : | if (skip) { | ||
1096 : | current_mb++; | ||
1097 : | block++; | ||
1098 : | *complete_count_self = x+1; | ||
1099 : | continue; | ||
1100 : | } | ||
1101 : | |||
1102 : | SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, | ||
1103 : | y, MotionFlags, current->vop_flags, | ||
1104 : | &Data, pParam, pMBs, reference->mbs, pMB); | ||
1105 : | |||
1106 : | if (current->vop_flags & XVID_VOP_MODEDECISION_RD) | ||
1107 : | xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam, | ||
1108 : | MotionFlags, current->vop_flags, current->vol_flags, | ||
1109 : | pCurrent, pRef, pGMC, current->coding_type); | ||
1110 : | |||
1111 : | else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD) | ||
1112 : | xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam, | ||
1113 : | MotionFlags, current->vop_flags, current->vol_flags, | ||
1114 : | pCurrent, pRef, pGMC, current->coding_type); | ||
1115 : | else | ||
1116 : | ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam, | ||
1117 : | MotionFlags, current->vop_flags, current->vol_flags, | ||
1118 : | pCurrent, pRef, pGMC, current->coding_type, sad00); | ||
1119 : | |||
1120 : | *complete_count_self = x+1; | ||
1121 : | |||
1122 : | current_mb++; | ||
1123 : | block++; | ||
1124 : | |||
1125 : | motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel); | ||
1126 : | |||
1127 : | } | ||
1128 : | block += (y_step-1)*pParam->mb_width; | ||
1129 : | complete_count_self++; | ||
1130 : | complete_count_above++; | ||
1131 : | } | ||
1132 : | |||
1133 : | syskin | 1.20 | h->minfcode = getMinFcode(MVmax); |
1134 : | |||
1135 : | syskin | 1.19 | h->MVmax = MVmax; |
1136 : | h->mvSum = mvSum; | ||
1137 : | h->mvCount = mvCount; | ||
1138 : | } | ||
1139 : | |||
1140 : |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |