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