Parent Directory | Revision Log
Revision 1.9 - (view) (download)
1 : | edgomez | 1.2 | /***************************************************************************** |
2 : | * | ||
3 : | * XVID MPEG-4 VIDEO CODEC | ||
4 : | * - Motion Estimation for B-VOPs - | ||
5 : | * | ||
6 : | * Copyright(C) 2002 Christoph Lampert <gruel@web.de> | ||
7 : | * 2002 Michael Militzer <michael@xvid.org> | ||
8 : | * 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net> | ||
9 : | * | ||
10 : | * This program is free software ; you can redistribute it and/or modify | ||
11 : | * it under the terms of the GNU General Public License as published by | ||
12 : | * the Free Software Foundation ; either version 2 of the License, or | ||
13 : | * (at your option) any later version. | ||
14 : | * | ||
15 : | * This program is distributed in the hope that it will be useful, | ||
16 : | * but WITHOUT ANY WARRANTY ; without even the implied warranty of | ||
17 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 : | * GNU General Public License for more details. | ||
19 : | * | ||
20 : | * You should have received a copy of the GNU General Public License | ||
21 : | * along with this program ; if not, write to the Free Software | ||
22 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
23 : | * | ||
24 : | edgomez | 1.9 | * $Id$ |
25 : | edgomez | 1.2 | * |
26 : | ****************************************************************************/ | ||
27 : | |||
28 : | |||
29 : | #include <assert.h> | ||
30 : | #include <stdio.h> | ||
31 : | #include <stdlib.h> | ||
32 : | #include <string.h> /* memcpy */ | ||
33 : | |||
34 : | #include "../encoder.h" | ||
35 : | #include "../global.h" | ||
36 : | #include "../image/interpolate8x8.h" | ||
37 : | #include "estimation.h" | ||
38 : | #include "motion.h" | ||
39 : | #include "sad.h" | ||
40 : | #include "motion_inlines.h" | ||
41 : | |||
42 : | static int32_t | ||
43 : | ChromaSAD2(const int fx, const int fy, const int bx, const int by, | ||
44 : | SearchData * const data) | ||
45 : | { | ||
46 : | int sad; | ||
47 : | const uint32_t stride = data->iEdgedWidth/2; | ||
48 : | uint8_t *f_refu, *f_refv, *b_refu, *b_refv; | ||
49 : | syskin | 1.3 | int offset, filter; |
50 : | edgomez | 1.2 | |
51 : | const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = { | ||
52 : | NULL, | ||
53 : | interpolate8x8_halfpel_v, | ||
54 : | interpolate8x8_halfpel_h, | ||
55 : | interpolate8x8_halfpel_hv | ||
56 : | }; | ||
57 : | syskin | 1.3 | |
58 : | syskin | 1.4 | if (data->chromaX == fx && data->chromaY == fy && |
59 : | data->b_chromaX == bx && data->b_chromaY == by) | ||
60 : | return data->chromaSAD; | ||
61 : | |||
62 : | syskin | 1.3 | offset = (fx>>1) + (fy>>1)*stride; |
63 : | filter = ((fx & 1) << 1) | (fy & 1); | ||
64 : | edgomez | 1.2 | |
65 : | if (filter != 0) { | ||
66 : | syskin | 1.6 | f_refu = data->RefQ + 64; |
67 : | f_refv = data->RefQ + 64 + 8; | ||
68 : | syskin | 1.4 | if (data->chromaX != fx || data->chromaY != fy) { |
69 : | interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding); | ||
70 : | interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding); | ||
71 : | } | ||
72 : | edgomez | 1.2 | } else { |
73 : | f_refu = (uint8_t*)data->RefP[4] + offset; | ||
74 : | f_refv = (uint8_t*)data->RefP[5] + offset; | ||
75 : | } | ||
76 : | syskin | 1.4 | data->chromaX = fx; data->chromaY = fy; |
77 : | edgomez | 1.2 | |
78 : | offset = (bx>>1) + (by>>1)*stride; | ||
79 : | filter = ((bx & 1) << 1) | (by & 1); | ||
80 : | |||
81 : | if (filter != 0) { | ||
82 : | syskin | 1.6 | b_refu = data->RefQ + 64 + 16; |
83 : | b_refv = data->RefQ + 64 + 24; | ||
84 : | syskin | 1.4 | if (data->b_chromaX != bx || data->b_chromaY != by) { |
85 : | interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding); | ||
86 : | interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding); | ||
87 : | } | ||
88 : | edgomez | 1.2 | } else { |
89 : | b_refu = (uint8_t*)data->b_RefP[4] + offset; | ||
90 : | b_refv = (uint8_t*)data->b_RefP[5] + offset; | ||
91 : | } | ||
92 : | syskin | 1.4 | data->b_chromaX = bx; data->b_chromaY = by; |
93 : | edgomez | 1.2 | |
94 : | sad = sad8bi(data->CurU, b_refu, f_refu, stride); | ||
95 : | sad += sad8bi(data->CurV, b_refv, f_refv, stride); | ||
96 : | |||
97 : | syskin | 1.4 | data->chromaSAD = sad; |
98 : | edgomez | 1.2 | return sad; |
99 : | } | ||
100 : | |||
101 : | static void | ||
102 : | CheckCandidateInt(const int x, const int y, SearchData * const data, const unsigned int Direction) | ||
103 : | { | ||
104 : | int32_t sad, xf, yf, xb, yb, xcf, ycf, xcb, ycb; | ||
105 : | uint32_t t; | ||
106 : | syskin | 1.3 | |
107 : | edgomez | 1.2 | const uint8_t *ReferenceF, *ReferenceB; |
108 : | VECTOR *current; | ||
109 : | |||
110 : | if ((x > data->max_dx) || (x < data->min_dx) || | ||
111 : | (y > data->max_dy) || (y < data->min_dy)) | ||
112 : | return; | ||
113 : | |||
114 : | if (Direction == 1) { /* x and y mean forward vector */ | ||
115 : | VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1]; | ||
116 : | xb = backward.x; | ||
117 : | yb = backward.y; | ||
118 : | xf = x; yf = y; | ||
119 : | } else { /* x and y mean backward vector */ | ||
120 : | VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0]; | ||
121 : | xf = forward.x; | ||
122 : | yf = forward.y; | ||
123 : | xb = x; yb = y; | ||
124 : | } | ||
125 : | syskin | 1.7 | |
126 : | edgomez | 1.2 | if (!data->qpel_precision) { |
127 : | ReferenceF = GetReference(xf, yf, data); | ||
128 : | ReferenceB = GetReferenceB(xb, yb, 1, data); | ||
129 : | current = data->currentMV + Direction - 1; | ||
130 : | xcf = xf; ycf = yf; | ||
131 : | xcb = xb; ycb = yb; | ||
132 : | } else { | ||
133 : | ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data); | ||
134 : | current = data->currentQMV + Direction - 1; | ||
135 : | ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data); | ||
136 : | xcf = xf/2; ycf = yf/2; | ||
137 : | xcb = xb/2; ycb = yb/2; | ||
138 : | } | ||
139 : | |||
140 : | t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) | ||
141 : | + d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0); | ||
142 : | |||
143 : | sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); | ||
144 : | sad += (data->lambda16 * t * sad)>>10; | ||
145 : | |||
146 : | if (data->chroma && sad < *data->iMinSAD) | ||
147 : | sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], | ||
148 : | (ycf >> 1) + roundtab_79[ycf & 0x3], | ||
149 : | (xcb >> 1) + roundtab_79[xcb & 0x3], | ||
150 : | (ycb >> 1) + roundtab_79[ycb & 0x3], data); | ||
151 : | |||
152 : | if (sad < *(data->iMinSAD)) { | ||
153 : | *data->iMinSAD = sad; | ||
154 : | current->x = x; current->y = y; | ||
155 : | data->dir = Direction; | ||
156 : | } | ||
157 : | } | ||
158 : | |||
159 : | static void | ||
160 : | CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) | ||
161 : | { | ||
162 : | int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; | ||
163 : | uint32_t k; | ||
164 : | const uint8_t *ReferenceF; | ||
165 : | const uint8_t *ReferenceB; | ||
166 : | VECTOR mvs, b_mvs; | ||
167 : | syskin | 1.7 | const int blocks[4] = {0, 8, 8*data->iEdgedWidth, 8*data->iEdgedWidth+8}; |
168 : | edgomez | 1.2 | |
169 : | if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; | ||
170 : | |||
171 : | for (k = 0; k < 4; k++) { | ||
172 : | mvs.x = data->directmvF[k].x + x; | ||
173 : | b_mvs.x = ((x == 0) ? | ||
174 : | data->directmvB[k].x | ||
175 : | : mvs.x - data->referencemv[k].x); | ||
176 : | |||
177 : | mvs.y = data->directmvF[k].y + y; | ||
178 : | b_mvs.y = ((y == 0) ? | ||
179 : | data->directmvB[k].y | ||
180 : | : mvs.y - data->referencemv[k].y); | ||
181 : | |||
182 : | if ((mvs.x > data->max_dx) || (mvs.x < data->min_dx) || | ||
183 : | (mvs.y > data->max_dy) || (mvs.y < data->min_dy) || | ||
184 : | (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) || | ||
185 : | (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) | ||
186 : | return; | ||
187 : | |||
188 : | if (data->qpel) { | ||
189 : | xcf += mvs.x/2; ycf += mvs.y/2; | ||
190 : | xcb += b_mvs.x/2; ycb += b_mvs.y/2; | ||
191 : | syskin | 1.7 | if (data->qpel_precision) { |
192 : | ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data); | ||
193 : | ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data); | ||
194 : | goto done; | ||
195 : | } | ||
196 : | mvs.x >>=1; mvs.y >>=1; b_mvs.x >>=1; b_mvs.y >>=1; // qpel->hpel | ||
197 : | edgomez | 1.2 | } else { |
198 : | xcf += mvs.x; ycf += mvs.y; | ||
199 : | xcb += b_mvs.x; ycb += b_mvs.y; | ||
200 : | } | ||
201 : | syskin | 1.7 | ReferenceF = GetReference(mvs.x, mvs.y, data) + blocks[k]; |
202 : | ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data) + blocks[k]; | ||
203 : | done: | ||
204 : | syskin | 1.4 | sad += data->iMinSAD[k+1] = |
205 : | syskin | 1.7 | sad8bi(data->Cur + blocks[k], |
206 : | syskin | 1.4 | ReferenceF, ReferenceB, data->iEdgedWidth); |
207 : | edgomez | 1.2 | if (sad > *(data->iMinSAD)) return; |
208 : | } | ||
209 : | |||
210 : | sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; | ||
211 : | |||
212 : | if (data->chroma && sad < *data->iMinSAD) | ||
213 : | sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], | ||
214 : | (ycf >> 3) + roundtab_76[ycf & 0xf], | ||
215 : | (xcb >> 3) + roundtab_76[xcb & 0xf], | ||
216 : | (ycb >> 3) + roundtab_76[ycb & 0xf], data); | ||
217 : | |||
218 : | if (sad < *(data->iMinSAD)) { | ||
219 : | data->iMinSAD[0] = sad; | ||
220 : | data->currentMV->x = x; data->currentMV->y = y; | ||
221 : | data->dir = Direction; | ||
222 : | } | ||
223 : | } | ||
224 : | |||
225 : | static void | ||
226 : | CheckCandidateDirectno4v(const int x, const int y, SearchData * const data, const unsigned int Direction) | ||
227 : | { | ||
228 : | int32_t sad, xcf, ycf, xcb, ycb; | ||
229 : | const uint8_t *ReferenceF; | ||
230 : | const uint8_t *ReferenceB; | ||
231 : | VECTOR mvs, b_mvs; | ||
232 : | |||
233 : | if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; | ||
234 : | |||
235 : | mvs.x = data->directmvF[0].x + x; | ||
236 : | b_mvs.x = ((x == 0) ? | ||
237 : | data->directmvB[0].x | ||
238 : | : mvs.x - data->referencemv[0].x); | ||
239 : | |||
240 : | mvs.y = data->directmvF[0].y + y; | ||
241 : | b_mvs.y = ((y == 0) ? | ||
242 : | data->directmvB[0].y | ||
243 : | : mvs.y - data->referencemv[0].y); | ||
244 : | |||
245 : | if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx) | ||
246 : | || (mvs.y > data->max_dy) || (mvs.y < data->min_dy) | ||
247 : | || (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) | ||
248 : | || (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) return; | ||
249 : | |||
250 : | if (data->qpel) { | ||
251 : | xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2); | ||
252 : | xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2); | ||
253 : | syskin | 1.7 | if (data->qpel_precision) { |
254 : | ReferenceF = xvid_me_interpolate16x16qpel(mvs.x, mvs.y, 0, data); | ||
255 : | ReferenceB = xvid_me_interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); | ||
256 : | goto done; | ||
257 : | } | ||
258 : | mvs.x >>=1; mvs.y >>=1; b_mvs.x >>=1; b_mvs.y >>=1; // qpel->hpel | ||
259 : | edgomez | 1.2 | } else { |
260 : | xcf = 4*mvs.x; ycf = 4*mvs.y; | ||
261 : | xcb = 4*b_mvs.x; ycb = 4*b_mvs.y; | ||
262 : | } | ||
263 : | syskin | 1.7 | ReferenceF = GetReference(mvs.x, mvs.y, data); |
264 : | ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data); | ||
265 : | edgomez | 1.2 | |
266 : | syskin | 1.7 | done: |
267 : | edgomez | 1.2 | sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
268 : | sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; | ||
269 : | |||
270 : | if (data->chroma && sad < *data->iMinSAD) | ||
271 : | sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], | ||
272 : | (ycf >> 3) + roundtab_76[ycf & 0xf], | ||
273 : | (xcb >> 3) + roundtab_76[xcb & 0xf], | ||
274 : | (ycb >> 3) + roundtab_76[ycb & 0xf], data); | ||
275 : | |||
276 : | if (sad < *(data->iMinSAD)) { | ||
277 : | *(data->iMinSAD) = sad; | ||
278 : | data->currentMV->x = x; data->currentMV->y = y; | ||
279 : | data->dir = Direction; | ||
280 : | } | ||
281 : | } | ||
282 : | |||
283 : | void | ||
284 : | CheckCandidate16no4v(const int x, const int y, SearchData * const data, const unsigned int Direction) | ||
285 : | { | ||
286 : | int32_t sad, xc, yc; | ||
287 : | const uint8_t * Reference; | ||
288 : | uint32_t t; | ||
289 : | VECTOR * current; | ||
290 : | |||
291 : | if ( (x > data->max_dx) || ( x < data->min_dx) | ||
292 : | || (y > data->max_dy) || (y < data->min_dy) ) return; | ||
293 : | |||
294 : | if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */ | ||
295 : | |||
296 : | if (data->qpel_precision) { /* x and y are in 1/4 precision */ | ||
297 : | Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); | ||
298 : | current = data->currentQMV; | ||
299 : | xc = x/2; yc = y/2; | ||
300 : | } else { | ||
301 : | Reference = GetReference(x, y, data); | ||
302 : | current = data->currentMV; | ||
303 : | xc = x; yc = y; | ||
304 : | } | ||
305 : | t = d_mv_bits(x, y, data->predMV, data->iFcode, | ||
306 : | data->qpel^data->qpel_precision, data->rrv); | ||
307 : | |||
308 : | sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); | ||
309 : | sad += (data->lambda16 * t * sad)>>10; | ||
310 : | |||
311 : | if (data->chroma && sad < *data->iMinSAD) | ||
312 : | sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], | ||
313 : | (yc >> 1) + roundtab_79[yc & 0x3], data); | ||
314 : | |||
315 : | if (sad < *(data->iMinSAD)) { | ||
316 : | *(data->iMinSAD) = sad; | ||
317 : | current->x = x; current->y = y; | ||
318 : | data->dir = Direction; | ||
319 : | } | ||
320 : | } | ||
321 : | |||
322 : | syskin | 1.7 | |
323 : | static void | ||
324 : | initialize_searchData(SearchData * Data_d, | ||
325 : | SearchData * Data_f, | ||
326 : | SearchData * Data_b, | ||
327 : | SearchData * Data_i, | ||
328 : | int x, int y, | ||
329 : | const IMAGE * const f_Ref, | ||
330 : | const uint8_t * const f_RefH, | ||
331 : | const uint8_t * const f_RefV, | ||
332 : | const uint8_t * const f_RefHV, | ||
333 : | const IMAGE * const b_Ref, | ||
334 : | const uint8_t * const b_RefH, | ||
335 : | const uint8_t * const b_RefV, | ||
336 : | const uint8_t * const b_RefHV, | ||
337 : | const IMAGE * const pCur, | ||
338 : | const MACROBLOCK * const b_mb) | ||
339 : | { | ||
340 : | |||
341 : | /* per-macroblock SearchData initialization - too many things would be repeated 4 times */ | ||
342 : | const uint8_t * RefP[6], * b_RefP[6], * Cur[3]; | ||
343 : | const uint32_t iEdgedWidth = Data_d->iEdgedWidth; | ||
344 : | unsigned int lambda; | ||
345 : | int i; | ||
346 : | |||
347 : | /* luma */ | ||
348 : | int offset = (x + iEdgedWidth*y) * 16; | ||
349 : | RefP[0] = f_Ref->y + offset; | ||
350 : | RefP[2] = f_RefH + offset; | ||
351 : | RefP[1] = f_RefV + offset; | ||
352 : | RefP[3] = f_RefHV + offset; | ||
353 : | b_RefP[0] = b_Ref->y + offset; | ||
354 : | b_RefP[2] = b_RefH + offset; | ||
355 : | b_RefP[1] = b_RefV + offset; | ||
356 : | b_RefP[3] = b_RefHV + offset; | ||
357 : | Cur[0] = pCur->y + offset; | ||
358 : | |||
359 : | /* chroma */ | ||
360 : | offset = (x + (iEdgedWidth/2)*y) * 8; | ||
361 : | RefP[4] = f_Ref->u + offset; | ||
362 : | RefP[5] = f_Ref->v + offset; | ||
363 : | b_RefP[4] = b_Ref->u + offset; | ||
364 : | b_RefP[5] = b_Ref->v + offset; | ||
365 : | Cur[1] = pCur->u + offset; | ||
366 : | Cur[2] = pCur->v + offset; | ||
367 : | |||
368 : | lambda = xvid_me_lambda_vec16[b_mb->quant]; | ||
369 : | |||
370 : | for (i = 0; i < 6; i++) { | ||
371 : | Data_d->RefP[i] = Data_f->RefP[i] = Data_i->RefP[i] = RefP[i]; | ||
372 : | Data_d->b_RefP[i] = Data_b->RefP[i] = Data_i->b_RefP[i] = b_RefP[i]; | ||
373 : | } | ||
374 : | Data_d->Cur = Data_f->Cur = Data_b->Cur = Data_i->Cur = Cur[0]; | ||
375 : | Data_d->CurU = Data_f->CurU = Data_b->CurU = Data_i->CurU = Cur[1]; | ||
376 : | Data_d->CurV = Data_f->CurV = Data_b->CurV = Data_i->CurV = Cur[2]; | ||
377 : | |||
378 : | Data_d->lambda16 = lambda/4; | ||
379 : | Data_f->lambda16 = Data_b->lambda16 = Data_i->lambda16 = lambda; | ||
380 : | |||
381 : | /* reset chroma-sad cache */ | ||
382 : | Data_d->b_chromaX = Data_d->b_chromaY = Data_d->chromaX = Data_d->chromaY = Data_d->chromaSAD = 256*4096; | ||
383 : | Data_i->b_chromaX = Data_i->b_chromaY = Data_i->chromaX = Data_i->chromaY = Data_i->chromaSAD = 256*4096; | ||
384 : | Data_f->chromaX = Data_f->chromaY = Data_f->chromaSAD = 256*4096; | ||
385 : | Data_b->chromaX = Data_b->chromaY = Data_b->chromaSAD = 256*4096; | ||
386 : | |||
387 : | *Data_d->iMinSAD = *Data_b->iMinSAD = *Data_f->iMinSAD = *Data_i->iMinSAD = 4096*256; | ||
388 : | } | ||
389 : | |||
390 : | edgomez | 1.2 | static __inline VECTOR |
391 : | ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode) | ||
392 : | { | ||
393 : | /* the stupidiest function ever */ | ||
394 : | return (mode == MODE_FORWARD ? pMB->mvs[0] : pMB->b_mvs[0]); | ||
395 : | } | ||
396 : | |||
397 : | static void __inline | ||
398 : | PreparePredictionsBF(VECTOR * const pmv, const int x, const int y, | ||
399 : | const uint32_t iWcount, | ||
400 : | const MACROBLOCK * const pMB, | ||
401 : | syskin | 1.7 | const uint32_t mode_curr, |
402 : | const VECTOR hint) | ||
403 : | edgomez | 1.2 | { |
404 : | |||
405 : | /* [0] is prediction */ | ||
406 : | syskin | 1.7 | pmv[0].x = (pmv[0].x); pmv[0].y = (pmv[0].y); |
407 : | edgomez | 1.2 | |
408 : | pmv[1].x = pmv[1].y = 0; /* [1] is zero */ | ||
409 : | |||
410 : | syskin | 1.7 | pmv[2].x = hint.x; pmv[2].y = hint.y; |
411 : | edgomez | 1.2 | |
412 : | if ((y != 0)&&(x != (int)(iWcount+1))) { /* [3] top-right neighbour */ | ||
413 : | pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr); | ||
414 : | } else pmv[3].x = pmv[3].y = 0; | ||
415 : | |||
416 : | if (y != 0) { | ||
417 : | pmv[4] = ChoosePred(pMB-iWcount, mode_curr); | ||
418 : | } else pmv[4].x = pmv[4].y = 0; | ||
419 : | |||
420 : | if (x != 0) { | ||
421 : | pmv[5] = ChoosePred(pMB-1, mode_curr); | ||
422 : | } else pmv[5].x = pmv[5].y = 0; | ||
423 : | |||
424 : | if (x != 0 && y != 0) { | ||
425 : | pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr); | ||
426 : | } else pmv[6].x = pmv[6].y = 0; | ||
427 : | } | ||
428 : | |||
429 : | /* search backward or forward */ | ||
430 : | static void | ||
431 : | syskin | 1.7 | SearchBF_initial(const int x, const int y, |
432 : | edgomez | 1.2 | const uint32_t MotionFlags, |
433 : | const uint32_t iFcode, | ||
434 : | const MBParam * const pParam, | ||
435 : | MACROBLOCK * const pMB, | ||
436 : | const VECTOR * const predMV, | ||
437 : | int32_t * const best_sad, | ||
438 : | const int32_t mode_current, | ||
439 : | syskin | 1.7 | SearchData * const Data, |
440 : | VECTOR hint) | ||
441 : | edgomez | 1.2 | { |
442 : | |||
443 : | int i; | ||
444 : | VECTOR pmv[7]; | ||
445 : | *Data->iMinSAD = MV_MAX_ERROR; | ||
446 : | Data->qpel_precision = 0; | ||
447 : | |||
448 : | Data->predMV = *predMV; | ||
449 : | |||
450 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
451 : | pParam->width, pParam->height, iFcode - Data->qpel, 1, 0); | ||
452 : | |||
453 : | pmv[0] = Data->predMV; | ||
454 : | syskin | 1.7 | if (Data->qpel) { |
455 : | pmv[0].x /= 2; pmv[0].y /= 2; | ||
456 : | hint.x /= 2; hint.y /= 2; | ||
457 : | } | ||
458 : | edgomez | 1.2 | |
459 : | syskin | 1.7 | PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current, hint); |
460 : | edgomez | 1.2 | |
461 : | Data->currentMV->x = Data->currentMV->y = 0; | ||
462 : | |||
463 : | /* main loop. checking all predictions */ | ||
464 : | for (i = 0; i < 7; i++) | ||
465 : | if (!vector_repeats(pmv, i) ) | ||
466 : | CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i); | ||
467 : | |||
468 : | if (*Data->iMinSAD > 512) { | ||
469 : | unsigned int mask = make_mask(pmv, 7, Data->dir); | ||
470 : | |||
471 : | MainSearchFunc *MainSearchPtr; | ||
472 : | if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; | ||
473 : | else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; | ||
474 : | else MainSearchPtr = xvid_me_DiamondSearch; | ||
475 : | |||
476 : | MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v); | ||
477 : | } | ||
478 : | |||
479 : | syskin | 1.7 | if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0]; |
480 : | } | ||
481 : | edgomez | 1.2 | |
482 : | syskin | 1.7 | static void |
483 : | SearchBF_final(const int x, const int y, | ||
484 : | const uint32_t MotionFlags, | ||
485 : | const MBParam * const pParam, | ||
486 : | int32_t * const best_sad, | ||
487 : | SearchData * const Data) | ||
488 : | { | ||
489 : | syskin | 1.3 | if(!Data->qpel) { |
490 : | /* halfpel mode */ | ||
491 : | if (MotionFlags & XVID_ME_HALFPELREFINE16) | ||
492 : | syskin | 1.7 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate16no4v, 0); |
493 : | syskin | 1.3 | } else { |
494 : | /* qpel mode */ | ||
495 : | if(MotionFlags & XVID_ME_FASTREFINE16) { | ||
496 : | /* fast */ | ||
497 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
498 : | pParam->width, pParam->height, Data->iFcode, 2, 0); | ||
499 : | syskin | 1.7 | FullRefine_Fast(Data, CheckCandidate16no4v, 0); |
500 : | |||
501 : | syskin | 1.3 | } else { |
502 : | syskin | 1.7 | |
503 : | syskin | 1.3 | Data->currentQMV->x = 2*Data->currentMV->x; |
504 : | Data->currentQMV->y = 2*Data->currentMV->y; | ||
505 : | if(MotionFlags & XVID_ME_QUARTERPELREFINE16) { | ||
506 : | /* full */ | ||
507 : | if (MotionFlags & XVID_ME_HALFPELREFINE16) { | ||
508 : | syskin | 1.7 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate16no4v, 0); /* hpel part */ |
509 : | syskin | 1.3 | Data->currentQMV->x = 2*Data->currentMV->x; |
510 : | Data->currentQMV->y = 2*Data->currentMV->y; | ||
511 : | } | ||
512 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
513 : | pParam->width, pParam->height, Data->iFcode, 2, 0); | ||
514 : | Data->qpel_precision = 1; | ||
515 : | syskin | 1.7 | xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate16no4v, 0); /* qpel part */ |
516 : | syskin | 1.3 | } |
517 : | edgomez | 1.2 | } |
518 : | } | ||
519 : | syskin | 1.7 | if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0]; |
520 : | edgomez | 1.2 | |
521 : | } | ||
522 : | |||
523 : | static void | ||
524 : | syskin | 1.7 | SkipDecisionB(MACROBLOCK * const pMB, const SearchData * const Data) |
525 : | edgomez | 1.2 | { |
526 : | int k; | ||
527 : | |||
528 : | syskin | 1.8 | pMB->mode = MODE_DIRECT; /* just to initialize it */ |
529 : | |||
530 : | edgomez | 1.2 | if (!Data->chroma) { |
531 : | int dx = 0, dy = 0, b_dx = 0, b_dy = 0; | ||
532 : | int32_t sum; | ||
533 : | const uint32_t stride = Data->iEdgedWidth/2; | ||
534 : | /* this is not full chroma compensation, only it's fullpel approximation. should work though */ | ||
535 : | |||
536 : | for (k = 0; k < 4; k++) { | ||
537 : | dy += Data->directmvF[k].y >> Data->qpel; | ||
538 : | dx += Data->directmvF[k].x >> Data->qpel; | ||
539 : | b_dy += Data->directmvB[k].y >> Data->qpel; | ||
540 : | b_dx += Data->directmvB[k].x >> Data->qpel; | ||
541 : | } | ||
542 : | |||
543 : | dy = (dy >> 3) + roundtab_76[dy & 0xf]; | ||
544 : | dx = (dx >> 3) + roundtab_76[dx & 0xf]; | ||
545 : | b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf]; | ||
546 : | b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; | ||
547 : | |||
548 : | syskin | 1.7 | sum = sad8bi(Data->CurU, |
549 : | Data->RefP[4] + (dy/2) * stride + dx/2, | ||
550 : | Data->b_RefP[4] + (b_dy/2) * stride + b_dx/2, | ||
551 : | edgomez | 1.2 | stride); |
552 : | |||
553 : | if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ | ||
554 : | |||
555 : | syskin | 1.7 | sum += sad8bi(Data->CurV, |
556 : | Data->RefP[5] + (dy/2) * stride + dx/2, | ||
557 : | Data->b_RefP[5] + (b_dy/2) * stride + b_dx/2, | ||
558 : | edgomez | 1.2 | stride); |
559 : | syskin | 1.8 | |
560 : | if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ | ||
561 : | } else { | ||
562 : | int sum = Data->chromaSAD; /* chroma-sad SAD caching keeps it there */ | ||
563 : | |||
564 : | edgomez | 1.2 | if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
565 : | } | ||
566 : | |||
567 : | /* skip */ | ||
568 : | pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */ | ||
569 : | for (k = 0; k < 4; k++) { | ||
570 : | pMB->qmvs[k] = pMB->mvs[k] = Data->directmvF[k]; | ||
571 : | pMB->b_qmvs[k] = pMB->b_mvs[k] = Data->directmvB[k]; | ||
572 : | } | ||
573 : | } | ||
574 : | |||
575 : | static uint32_t | ||
576 : | syskin | 1.7 | SearchDirect_initial(const int x, const int y, |
577 : | edgomez | 1.2 | const uint32_t MotionFlags, |
578 : | const int32_t TRB, const int32_t TRD, | ||
579 : | const MBParam * const pParam, | ||
580 : | MACROBLOCK * const pMB, | ||
581 : | const MACROBLOCK * const b_mb, | ||
582 : | int32_t * const best_sad, | ||
583 : | SearchData * const Data) | ||
584 : | |||
585 : | { | ||
586 : | int32_t skip_sad; | ||
587 : | int k = (x + Data->iEdgedWidth*y) * 16; | ||
588 : | |||
589 : | k = Data->qpel ? 4 : 2; | ||
590 : | Data->max_dx = k * (pParam->width - x * 16); | ||
591 : | Data->max_dy = k * (pParam->height - y * 16); | ||
592 : | Data->min_dx = -k * (16 + x * 16); | ||
593 : | Data->min_dy = -k * (16 + y * 16); | ||
594 : | |||
595 : | Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs; | ||
596 : | |||
597 : | for (k = 0; k < 4; k++) { | ||
598 : | syskin | 1.7 | Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
599 : | Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD; | ||
600 : | Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD); | ||
601 : | Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD; | ||
602 : | |||
603 : | if ( (Data->directmvB[k].x > Data->max_dx) | (Data->directmvB[k].x < Data->min_dx) | ||
604 : | | (Data->directmvB[k].y > Data->max_dy) | (Data->directmvB[k].y < Data->min_dy) ) { | ||
605 : | |||
606 : | Data->iMinSAD[0] = *best_sad = 256*4096; /* in that case, we won't use direct mode */ | ||
607 : | edgomez | 1.2 | return 256*4096; |
608 : | } | ||
609 : | if (b_mb->mode != MODE_INTER4V) { | ||
610 : | Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0]; | ||
611 : | Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0]; | ||
612 : | break; | ||
613 : | } | ||
614 : | } | ||
615 : | syskin | 1.7 | Data->qpel_precision = Data->qpel; /* this initial check is done with full precision, to find real |
616 : | SKIP sad */ | ||
617 : | edgomez | 1.2 | |
618 : | syskin | 1.4 | CheckCandidateDirect(0, 0, Data, 255); /* will also fill iMinSAD[1..4] with 8x8 SADs */ |
619 : | edgomez | 1.2 | |
620 : | syskin | 1.7 | |
621 : | edgomez | 1.2 | /* initial (fast) skip decision */ |
622 : | syskin | 1.4 | if (Data->iMinSAD[1] < (int)Data->iQuant * INITIAL_SKIP_THRESH |
623 : | && Data->iMinSAD[2] < (int)Data->iQuant * INITIAL_SKIP_THRESH | ||
624 : | && Data->iMinSAD[3] < (int)Data->iQuant * INITIAL_SKIP_THRESH | ||
625 : | && Data->iMinSAD[4] < (int)Data->iQuant * INITIAL_SKIP_THRESH) { | ||
626 : | edgomez | 1.2 | /* possible skip */ |
627 : | syskin | 1.7 | SkipDecisionB(pMB, Data); |
628 : | syskin | 1.4 | if (pMB->mode == MODE_DIRECT_NONE_MV) |
629 : | return *Data->iMinSAD; /* skipped */ | ||
630 : | edgomez | 1.2 | } |
631 : | |||
632 : | syskin | 1.4 | skip_sad = 4*MAX(MAX(Data->iMinSAD[1],Data->iMinSAD[2]), MAX(Data->iMinSAD[3],Data->iMinSAD[4])); |
633 : | if (Data->chroma) skip_sad += Data->chromaSAD; | ||
634 : | |||
635 : | syskin | 1.7 | Data->currentMV[1].x = Data->directmvF[0].x + Data->currentMV->x; /* hints for forward and backward searches */ |
636 : | Data->currentMV[1].y = Data->directmvF[0].y + Data->currentMV->y; | ||
637 : | edgomez | 1.2 | |
638 : | syskin | 1.7 | Data->currentMV[2].x = ((Data->currentMV->x == 0) ? |
639 : | Data->directmvB[0].x | ||
640 : | : Data->currentMV[1].x - Data->referencemv[0].x); | ||
641 : | |||
642 : | Data->currentMV[2].y = ((Data->currentMV->y == 0) ? | ||
643 : | Data->directmvB[0].y | ||
644 : | : Data->currentMV[1].y - Data->referencemv[0].y); | ||
645 : | |||
646 : | return skip_sad; | ||
647 : | } | ||
648 : | edgomez | 1.2 | |
649 : | syskin | 1.7 | static void |
650 : | SearchDirect_final( const uint32_t MotionFlags, | ||
651 : | const MACROBLOCK * const b_mb, | ||
652 : | int32_t * const best_sad, | ||
653 : | SearchData * const Data) | ||
654 : | edgomez | 1.2 | |
655 : | syskin | 1.7 | { |
656 : | CheckFunc * CheckCandidate = b_mb->mode == MODE_INTER4V ? | ||
657 : | CheckCandidateDirect : CheckCandidateDirectno4v; | ||
658 : | MainSearchFunc *MainSearchPtr; | ||
659 : | edgomez | 1.2 | |
660 : | syskin | 1.7 | if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; |
661 : | else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; | ||
662 : | else MainSearchPtr = xvid_me_DiamondSearch; | ||
663 : | edgomez | 1.2 | |
664 : | syskin | 1.7 | Data->qpel_precision = 0; |
665 : | MainSearchPtr(0, 0, Data, 255, CheckCandidate); | ||
666 : | edgomez | 1.2 | |
667 : | syskin | 1.7 | Data->qpel_precision = Data->qpel; |
668 : | if(Data->qpel) { | ||
669 : | *Data->iMinSAD = 256*4096; /* this old SAD was not real, it was in hpel precision */ | ||
670 : | CheckCandidate(Data->currentMV->x, Data->currentMV->y, Data, 255); | ||
671 : | } | ||
672 : | edgomez | 1.2 | |
673 : | syskin | 1.7 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); |
674 : | edgomez | 1.2 | |
675 : | syskin | 1.7 | if (Data->iMinSAD[0] < *best_sad) { |
676 : | *best_sad = Data->iMinSAD[0]; | ||
677 : | edgomez | 1.2 | } |
678 : | syskin | 1.7 | |
679 : | edgomez | 1.2 | } |
680 : | |||
681 : | |||
682 : | syskin | 1.7 | static __inline void |
683 : | set_range(int * range, SearchData * Data) | ||
684 : | edgomez | 1.2 | { |
685 : | Data->min_dx = range[0]; | ||
686 : | Data->max_dx = range[1]; | ||
687 : | Data->min_dy = range[2]; | ||
688 : | Data->max_dy = range[3]; | ||
689 : | } | ||
690 : | |||
691 : | static void | ||
692 : | syskin | 1.7 | SearchInterpolate_initial( |
693 : | edgomez | 1.2 | const int x, const int y, |
694 : | const uint32_t MotionFlags, | ||
695 : | const MBParam * const pParam, | ||
696 : | const VECTOR * const f_predMV, | ||
697 : | const VECTOR * const b_predMV, | ||
698 : | int32_t * const best_sad, | ||
699 : | syskin | 1.7 | SearchData * const Data, |
700 : | const VECTOR startF, | ||
701 : | const VECTOR startB) | ||
702 : | edgomez | 1.2 | |
703 : | { | ||
704 : | int b_range[4], f_range[4]; | ||
705 : | |||
706 : | Data->qpel_precision = 0; | ||
707 : | syskin | 1.4 | |
708 : | edgomez | 1.2 | Data->predMV = *f_predMV; |
709 : | Data->bpredMV = *b_predMV; | ||
710 : | |||
711 : | syskin | 1.7 | Data->currentMV[0] = startF; |
712 : | Data->currentMV[1] = startB; | ||
713 : | edgomez | 1.2 | |
714 : | syskin | 1.7 | get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, 0); |
715 : | get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1, 0); | ||
716 : | edgomez | 1.2 | |
717 : | if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1]; | ||
718 : | if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0]; | ||
719 : | if (Data->currentMV[0].y > f_range[3]) Data->currentMV[0].y = f_range[3]; | ||
720 : | if (Data->currentMV[0].y < f_range[2]) Data->currentMV[0].y = f_range[2]; | ||
721 : | |||
722 : | if (Data->currentMV[1].x > b_range[1]) Data->currentMV[1].x = b_range[1]; | ||
723 : | if (Data->currentMV[1].x < b_range[0]) Data->currentMV[1].x = b_range[0]; | ||
724 : | if (Data->currentMV[1].y > b_range[3]) Data->currentMV[1].y = b_range[3]; | ||
725 : | if (Data->currentMV[1].y < b_range[2]) Data->currentMV[1].y = b_range[2]; | ||
726 : | |||
727 : | set_range(f_range, Data); | ||
728 : | |||
729 : | CheckCandidateInt(Data->currentMV[0].x, Data->currentMV[0].y, Data, 1); | ||
730 : | |||
731 : | syskin | 1.7 | if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0]; |
732 : | } | ||
733 : | |||
734 : | static void | ||
735 : | SearchInterpolate_final(const int x, const int y, | ||
736 : | const uint32_t MotionFlags, | ||
737 : | const MBParam * const pParam, | ||
738 : | int32_t * const best_sad, | ||
739 : | SearchData * const Data) | ||
740 : | { | ||
741 : | int i, j; | ||
742 : | int b_range[4], f_range[4]; | ||
743 : | |||
744 : | get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, 0); | ||
745 : | get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1, 0); | ||
746 : | |||
747 : | edgomez | 1.2 | /* diamond */ |
748 : | do { | ||
749 : | Data->dir = 0; | ||
750 : | /* forward MV moves */ | ||
751 : | i = Data->currentMV[0].x; j = Data->currentMV[0].y; | ||
752 : | |||
753 : | CheckCandidateInt(i + 1, j, Data, 1); | ||
754 : | CheckCandidateInt(i, j + 1, Data, 1); | ||
755 : | CheckCandidateInt(i - 1, j, Data, 1); | ||
756 : | CheckCandidateInt(i, j - 1, Data, 1); | ||
757 : | |||
758 : | /* backward MV moves */ | ||
759 : | set_range(b_range, Data); | ||
760 : | i = Data->currentMV[1].x; j = Data->currentMV[1].y; | ||
761 : | |||
762 : | CheckCandidateInt(i + 1, j, Data, 2); | ||
763 : | CheckCandidateInt(i, j + 1, Data, 2); | ||
764 : | CheckCandidateInt(i - 1, j, Data, 2); | ||
765 : | CheckCandidateInt(i, j - 1, Data, 2); | ||
766 : | |||
767 : | set_range(f_range, Data); | ||
768 : | |||
769 : | } while (Data->dir != 0); | ||
770 : | |||
771 : | /* qpel refinement */ | ||
772 : | if (Data->qpel) { | ||
773 : | Data->qpel_precision = 1; | ||
774 : | syskin | 1.7 | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
775 : | x, y, 4, pParam->width, pParam->height, Data->iFcode, 2, 0); | ||
776 : | edgomez | 1.2 | |
777 : | Data->currentQMV[0].x = 2 * Data->currentMV[0].x; | ||
778 : | Data->currentQMV[0].y = 2 * Data->currentMV[0].y; | ||
779 : | Data->currentQMV[1].x = 2 * Data->currentMV[1].x; | ||
780 : | Data->currentQMV[1].y = 2 * Data->currentMV[1].y; | ||
781 : | |||
782 : | syskin | 1.7 | if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
783 : | xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidateInt, 1); | ||
784 : | edgomez | 1.2 | |
785 : | syskin | 1.7 | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
786 : | x, y, 4, pParam->width, pParam->height, Data->bFcode, 2, 0); | ||
787 : | edgomez | 1.2 | |
788 : | syskin | 1.7 | xvid_me_SubpelRefine(Data->currentQMV[1], Data, CheckCandidateInt, 2); |
789 : | } | ||
790 : | edgomez | 1.2 | } |
791 : | |||
792 : | syskin | 1.7 | if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0]; |
793 : | } | ||
794 : | |||
795 : | static void | ||
796 : | ModeDecision_BVOP_SAD(const SearchData * const Data_d, | ||
797 : | const SearchData * const Data_b, | ||
798 : | const SearchData * const Data_f, | ||
799 : | const SearchData * const Data_i, | ||
800 : | MACROBLOCK * const pMB, | ||
801 : | const MACROBLOCK * const b_mb, | ||
802 : | VECTOR * f_predMV, | ||
803 : | VECTOR * b_predMV) | ||
804 : | { | ||
805 : | int mode = MODE_DIRECT, k; | ||
806 : | int best_sad, f_sad, b_sad, i_sad; | ||
807 : | const int qpel = Data_d->qpel; | ||
808 : | |||
809 : | /* evaluate cost of all modes - quite simple in SAD */ | ||
810 : | best_sad = Data_d->iMinSAD[0] + 1*Data_d->lambda16; | ||
811 : | b_sad = Data_b->iMinSAD[0] + 3*Data_d->lambda16; | ||
812 : | f_sad = Data_f->iMinSAD[0] + 4*Data_d->lambda16; | ||
813 : | i_sad = Data_i->iMinSAD[0] + 2*Data_d->lambda16; | ||
814 : | |||
815 : | if (b_sad < best_sad) { | ||
816 : | mode = MODE_BACKWARD; | ||
817 : | best_sad = b_sad; | ||
818 : | } | ||
819 : | |||
820 : | if (f_sad < best_sad) { | ||
821 : | mode = MODE_FORWARD; | ||
822 : | best_sad = f_sad; | ||
823 : | } | ||
824 : | |||
825 : | if (i_sad < best_sad) { | ||
826 : | mode = MODE_INTERPOLATE; | ||
827 : | best_sad = i_sad; | ||
828 : | } | ||
829 : | |||
830 : | pMB->sad16 = best_sad; | ||
831 : | pMB->mode = mode; | ||
832 : | |||
833 : | switch (mode) { | ||
834 : | |||
835 : | case MODE_DIRECT: | ||
836 : | if (!qpel && b_mb->mode != MODE_INTER4V) pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */ | ||
837 : | |||
838 : | pMB->pmvs[3] = Data_d->currentMV[0]; | ||
839 : | |||
840 : | for (k = 0; k < 4; k++) { | ||
841 : | pMB->mvs[k].x = Data_d->directmvF[k].x + Data_d->currentMV->x; | ||
842 : | pMB->b_mvs[k].x = ( (Data_d->currentMV->x == 0) | ||
843 : | ? Data_d->directmvB[k].x | ||
844 : | :pMB->mvs[k].x - Data_d->referencemv[k].x); | ||
845 : | pMB->mvs[k].y = (Data_d->directmvF[k].y + Data_d->currentMV->y); | ||
846 : | pMB->b_mvs[k].y = ((Data_d->currentMV->y == 0) | ||
847 : | ? Data_d->directmvB[k].y | ||
848 : | : pMB->mvs[k].y - Data_d->referencemv[k].y); | ||
849 : | if (qpel) { | ||
850 : | pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2; | ||
851 : | pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2; | ||
852 : | pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2; | ||
853 : | pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2; | ||
854 : | } | ||
855 : | |||
856 : | if (b_mb->mode != MODE_INTER4V) { | ||
857 : | pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0]; | ||
858 : | pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0]; | ||
859 : | pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0]; | ||
860 : | pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0]; | ||
861 : | break; | ||
862 : | } | ||
863 : | } | ||
864 : | break; | ||
865 : | |||
866 : | case MODE_FORWARD: | ||
867 : | if (qpel) { | ||
868 : | pMB->pmvs[0].x = Data_f->currentQMV->x - f_predMV->x; | ||
869 : | pMB->pmvs[0].y = Data_f->currentQMV->y - f_predMV->y; | ||
870 : | pMB->qmvs[0] = *Data_f->currentQMV; | ||
871 : | *f_predMV = Data_f->currentQMV[0]; | ||
872 : | } else { | ||
873 : | pMB->pmvs[0].x = Data_f->currentMV->x - f_predMV->x; | ||
874 : | pMB->pmvs[0].y = Data_f->currentMV->y - f_predMV->y; | ||
875 : | *f_predMV = Data_f->currentMV[0]; | ||
876 : | } | ||
877 : | pMB->mvs[0] = *Data_f->currentMV; | ||
878 : | break; | ||
879 : | |||
880 : | case MODE_BACKWARD: | ||
881 : | if (qpel) { | ||
882 : | pMB->pmvs[0].x = Data_b->currentQMV->x - b_predMV->x; | ||
883 : | pMB->pmvs[0].y = Data_b->currentQMV->y - b_predMV->y; | ||
884 : | pMB->b_qmvs[0] = *Data_b->currentQMV; | ||
885 : | *b_predMV = Data_b->currentQMV[0]; | ||
886 : | } else { | ||
887 : | pMB->pmvs[0].x = Data_b->currentMV->x - b_predMV->x; | ||
888 : | pMB->pmvs[0].y = Data_b->currentMV->y - b_predMV->y; | ||
889 : | *b_predMV = Data_b->currentMV[0]; | ||
890 : | } | ||
891 : | pMB->b_mvs[0] = *Data_b->currentMV; | ||
892 : | break; | ||
893 : | |||
894 : | |||
895 : | case MODE_INTERPOLATE: | ||
896 : | pMB->mvs[0] = Data_i->currentMV[0]; | ||
897 : | pMB->b_mvs[0] = Data_i->currentMV[1]; | ||
898 : | if (qpel) { | ||
899 : | pMB->qmvs[0] = Data_i->currentQMV[0]; | ||
900 : | pMB->b_qmvs[0] = Data_i->currentQMV[1]; | ||
901 : | edgomez | 1.2 | pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x; |
902 : | pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y; | ||
903 : | pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x; | ||
904 : | pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y; | ||
905 : | syskin | 1.7 | *f_predMV = Data_i->currentQMV[0]; |
906 : | *b_predMV = Data_i->currentQMV[1]; | ||
907 : | edgomez | 1.2 | } else { |
908 : | pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x; | ||
909 : | pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y; | ||
910 : | pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x; | ||
911 : | pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y; | ||
912 : | syskin | 1.7 | *f_predMV = Data_i->currentMV[0]; |
913 : | *b_predMV = Data_i->currentMV[1]; | ||
914 : | edgomez | 1.2 | } |
915 : | syskin | 1.7 | break; |
916 : | edgomez | 1.2 | } |
917 : | } | ||
918 : | |||
919 : | void | ||
920 : | MotionEstimationBVOP(MBParam * const pParam, | ||
921 : | FRAMEINFO * const frame, | ||
922 : | const int32_t time_bp, | ||
923 : | const int32_t time_pp, | ||
924 : | /* forward (past) reference */ | ||
925 : | const MACROBLOCK * const f_mbs, | ||
926 : | const IMAGE * const f_ref, | ||
927 : | const IMAGE * const f_refH, | ||
928 : | const IMAGE * const f_refV, | ||
929 : | const IMAGE * const f_refHV, | ||
930 : | /* backward (future) reference */ | ||
931 : | const FRAMEINFO * const b_reference, | ||
932 : | const IMAGE * const b_ref, | ||
933 : | const IMAGE * const b_refH, | ||
934 : | const IMAGE * const b_refV, | ||
935 : | const IMAGE * const b_refHV) | ||
936 : | { | ||
937 : | uint32_t i, j; | ||
938 : | syskin | 1.7 | int32_t best_sad, sad2; |
939 : | edgomez | 1.2 | uint32_t skip_sad; |
940 : | |||
941 : | const MACROBLOCK * const b_mbs = b_reference->mbs; | ||
942 : | |||
943 : | VECTOR f_predMV, b_predMV; | ||
944 : | |||
945 : | const int32_t TRB = time_pp - time_bp; | ||
946 : | const int32_t TRD = time_pp; | ||
947 : | |||
948 : | /* some pre-inintialized data for the rest of the search */ | ||
949 : | syskin | 1.7 | SearchData Data_d, Data_f, Data_b, Data_i; |
950 : | memset(&Data_d, 0, sizeof(SearchData)); | ||
951 : | edgomez | 1.2 | |
952 : | syskin | 1.7 | Data_d.iEdgedWidth = pParam->edged_width; |
953 : | Data_d.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0; | ||
954 : | Data_d.rounding = 0; | ||
955 : | Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; | ||
956 : | Data_d.iQuant = frame->quant; | ||
957 : | |||
958 : | Data_d.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */ | ||
959 : | |||
960 : | memcpy(&Data_f, &Data_d, sizeof(SearchData)); | ||
961 : | memcpy(&Data_b, &Data_d, sizeof(SearchData)); | ||
962 : | memcpy(&Data_i, &Data_d, sizeof(SearchData)); | ||
963 : | edgomez | 1.2 | |
964 : | syskin | 1.7 | Data_f.iFcode = Data_i.iFcode = frame->fcode; |
965 : | Data_b.iFcode = Data_i.bFcode = frame->bcode; | ||
966 : | edgomez | 1.2 | |
967 : | |||
968 : | for (j = 0; j < pParam->mb_height; j++) { | ||
969 : | |||
970 : | f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ | ||
971 : | |||
972 : | for (i = 0; i < pParam->mb_width; i++) { | ||
973 : | MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; | ||
974 : | const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; | ||
975 : | syskin | 1.7 | pMB->mode = -1; |
976 : | |||
977 : | initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i, | ||
978 : | i, j, f_ref, f_refH->y, f_refV->y, f_refHV->y, | ||
979 : | b_ref, b_refH->y, b_refV->y, b_refHV->y, | ||
980 : | &frame->image, b_mb); | ||
981 : | edgomez | 1.2 | |
982 : | /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */ | ||
983 : | if (b_reference->coding_type != S_VOP) | ||
984 : | if (b_mb->mode == MODE_NOT_CODED) { | ||
985 : | pMB->mode = MODE_NOT_CODED; | ||
986 : | pMB->mvs[0] = pMB->b_mvs[0] = zeroMV; | ||
987 : | pMB->sad16 = 0; | ||
988 : | continue; | ||
989 : | } | ||
990 : | syskin | 1.3 | |
991 : | edgomez | 1.2 | /* direct search comes first, because it (1) checks for SKIP-mode |
992 : | and (2) sets very good predictions for forward and backward search */ | ||
993 : | syskin | 1.7 | skip_sad = SearchDirect_initial(i, j, frame->motion_flags, TRB, TRD, pParam, pMB, |
994 : | b_mb, &best_sad, &Data_d); | ||
995 : | edgomez | 1.2 | |
996 : | if (pMB->mode == MODE_DIRECT_NONE_MV) { | ||
997 : | pMB->sad16 = best_sad; | ||
998 : | continue; | ||
999 : | } | ||
1000 : | syskin | 1.7 | |
1001 : | SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB, | ||
1002 : | &f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1]); | ||
1003 : | |||
1004 : | SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, | ||
1005 : | &b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]); | ||
1006 : | |||
1007 : | sad2 = best_sad; | ||
1008 : | |||
1009 : | if (Data_f.iMinSAD[0] < 2*sad2+1500) | ||
1010 : | SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f); | ||
1011 : | |||
1012 : | if (Data_b.iMinSAD[0] < 2*sad2+1500) | ||
1013 : | SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b); | ||
1014 : | edgomez | 1.2 | |
1015 : | syskin | 1.7 | SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad, |
1016 : | &Data_i, Data_f.currentMV[0], Data_b.currentMV[0]); | ||
1017 : | edgomez | 1.2 | |
1018 : | edgomez | 1.9 | if (((Data_i.iMinSAD[0] < 2*best_sad+2000) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE)) |
1019 : | || Data_i.iMinSAD[0] <= best_sad) | ||
1020 : | edgomez | 1.2 | |
1021 : | syskin | 1.7 | SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i); |
1022 : | edgomez | 1.2 | |
1023 : | syskin | 1.7 | |
1024 : | if ((Data_d.iMinSAD[0] <= best_sad) && (!(frame->motion_flags&XVID_ME_SKIP_DELTASEARCH))) | ||
1025 : | SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d); | ||
1026 : | edgomez | 1.2 | |
1027 : | |||
1028 : | /* final skip decision */ | ||
1029 : | syskin | 1.7 | if ( (skip_sad < Data_d.iQuant * MAX_SAD00_FOR_SKIP ) |
1030 : | && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) { | ||
1031 : | edgomez | 1.2 | |
1032 : | syskin | 1.7 | SkipDecisionB(pMB, &Data_d); |
1033 : | if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */ | ||
1034 : | edgomez | 1.2 | pMB->sad16 = best_sad; |
1035 : | syskin | 1.7 | continue; |
1036 : | } | ||
1037 : | edgomez | 1.2 | } |
1038 : | syskin | 1.7 | |
1039 : | ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV); | ||
1040 : | |||
1041 : | edgomez | 1.2 | } |
1042 : | } | ||
1043 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |