[svn] / branches / dev-api-3 / xvidcore / src / motion / motion_est.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-3/xvidcore/src/motion/motion_est.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 675 - (view) (download)

1 : Isibaar 3 /**************************************************************************
2 :     *
3 : chl 259 * XVID MPEG-4 VIDEO CODEC
4 :     * motion estimation
5 :     *
6 :     * This program is an implementation of a part of one or more MPEG-4
7 :     * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
8 :     * to use this software module in hardware or software products are
9 :     * advised that its use may infringe existing patents or copyrights, and
10 :     * any such use would be at such party's own risk. The original
11 :     * developer of this software module and his/her company, and subsequent
12 :     * editors and their companies, will have no liability for use of this
13 :     * software or modifications or derivatives thereof.
14 :     *
15 :     * This program is free software; you can redistribute it and/or modify
16 :     * it under the terms of the GNU General Public License as published by
17 :     * the Free Software Foundation; either version 2 of the License, or
18 :     * (at your option) any later version.
19 :     *
20 :     * This program is distributed in the hope that it will be useful,
21 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 :     * GNU General Public License for more details.
24 :     *
25 :     * You should have received a copy of the GNU General Public License
26 :     * along with this program; if not, write to the Free Software
27 :     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 :     *
29 :     *************************************************************************/
30 :    
31 : Isibaar 3 #include <assert.h>
32 :     #include <stdio.h>
33 : chl 96 #include <stdlib.h>
34 : Isibaar 3
35 :     #include "../encoder.h"
36 :     #include "../utils/mbfunctions.h"
37 :     #include "../prediction/mbprediction.h"
38 :     #include "../global.h"
39 :     #include "../utils/timer.h"
40 : Isibaar 580 #include "../image/interpolate8x8.h"
41 : chl 530 #include "motion_est.h"
42 : suxen_drol 118 #include "motion.h"
43 : Isibaar 3 #include "sad.h"
44 : Isibaar 539 #include "../utils/emms.h"
45 : Isibaar 3
46 : chl 530 #define INITIAL_SKIP_THRESH (10)
47 :     #define FINAL_SKIP_THRESH (50)
48 :     #define MAX_SAD00_FOR_SKIP (20)
49 : Isibaar 539 #define MAX_CHROMA_SAD_FOR_SKIP (22)
50 : syskin 672 #define SKIP_THRESH_B (15)
51 : Isibaar 3
52 : chl 530 #define CHECK_CANDIDATE(X,Y,D) { \
53 :     (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }
54 : Isibaar 3
55 : chl 530 #define iDiamondSize 2
56 : chl 141
57 : chl 530 static __inline int
58 :     d_mv_bits(int x, int y, const uint32_t iFcode)
59 : Isibaar 3 {
60 : chl 530 int xb, yb;
61 :    
62 :     if (x == 0) xb = 1;
63 :     else {
64 :     if (x < 0) x = -x;
65 :     x += (1 << (iFcode - 1)) - 1;
66 :     x >>= (iFcode - 1);
67 :     if (x > 32) x = 32;
68 :     xb = mvtab[x] + iFcode;
69 : edgomez 195 }
70 : Isibaar 3
71 : chl 530 if (y == 0) yb = 1;
72 :     else {
73 :     if (y < 0) y = -y;
74 :     y += (1 << (iFcode - 1)) - 1;
75 :     y >>= (iFcode - 1);
76 :     if (y > 32) y = 32;
77 :     yb = mvtab[y] + iFcode;
78 :     }
79 :     return xb + yb;
80 : Isibaar 3 }
81 :    
82 : syskin 628 static int32_t
83 :     ChromaSAD(int dx, int dy, const SearchData * const data)
84 : Isibaar 3 {
85 : syskin 628 int sad;
86 :     dx = (dx >> 1) + roundtab_79[dx & 0x3];
87 :     dy = (dy >> 1) + roundtab_79[dy & 0x3];
88 : Isibaar 3
89 : syskin 628 switch (((dx & 1) << 1) + (dy & 1)) { // ((dx%2)?2:0)+((dy%2)?1:0)
90 :     case 0:
91 :     sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);
92 :     sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);
93 :     break;
94 :     case 1:
95 :     dx = dx / 2; dy = (dy - 1) / 2;
96 :     sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);
97 :     sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);
98 :     break;
99 :     case 2:
100 :     dx = (dx - 1) / 2; dy = dy / 2;
101 :     sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);
102 :     sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);
103 :     break;
104 :     default:
105 :     dx = (dx - 1) / 2; dy = (dy - 1) / 2;
106 :     interpolate8x8_halfpel_hv(data->RefQ,
107 :     data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,
108 :     data->rounding);
109 :     sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2);
110 :     interpolate8x8_halfpel_hv(data->RefQ,
111 :     data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,
112 :     data->rounding);
113 :     sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2);
114 :     break;
115 : chl 530 }
116 : syskin 628 return sad;
117 :     }
118 : Isibaar 3
119 : syskin 663 static __inline const uint8_t *
120 :     GetReference(const int x, const int y, const int dir, const SearchData * const data)
121 : Isibaar 606 {
122 : syskin 663 // dir : 0 = forward, 1 = backward
123 :     switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) {
124 :     case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth);
125 :     case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);
126 :     case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);
127 :     case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);
128 :     case 4 : return data->bRef + x/2 + (y/2)*(data->iEdgedWidth);
129 :     case 5 : return data->bRefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);
130 :     case 6 : return data->bRefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);
131 :     default : return data->bRefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);
132 : Isibaar 606
133 :     }
134 :     }
135 :    
136 : syskin 663 static uint8_t *
137 :     Interpolate8x8qpel(const int x, const int y, const int block, const int dir, const SearchData * const data)
138 : chl 530 {
139 : syskin 663 // create or find a qpel-precision reference picture; return pointer to it
140 : syskin 668 uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir;
141 : syskin 663 const int32_t iEdgedWidth = data->iEdgedWidth;
142 :     const uint32_t rounding = data->rounding;
143 :     const int halfpel_x = x/2;
144 :     const int halfpel_y = y/2;
145 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
146 : chl 326
147 : syskin 663 ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases
148 :     ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
149 :     switch( ((x&1)<<1) + (y&1) ) {
150 :     case 0: // pure halfpel position
151 :     Reference = (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data);
152 :     Reference += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
153 :     break;
154 : Isibaar 3
155 : syskin 663 case 1: // x halfpel, y qpel - top or bottom during qpel refinement
156 :     ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
157 :     ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
158 : Isibaar 665 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
159 : syskin 663 break;
160 : Isibaar 3
161 : syskin 663 case 2: // x qpel, y halfpel - left or right during qpel refinement
162 :     ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data);
163 :     ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
164 : Isibaar 665 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
165 : syskin 663 break;
166 : Isibaar 3
167 : syskin 663 default: // x and y in qpel resolution - the "corners" (top left/right and
168 :     // bottom left/right) during qpel refinement
169 :     ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
170 :     ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data);
171 :     ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data);
172 :     ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
173 :     ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
174 :     ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
175 :     interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
176 :     break;
177 :     }
178 :     return Reference;
179 : Isibaar 3 }
180 :    
181 : syskin 663 static uint8_t *
182 :     Interpolate16x16qpel(const int x, const int y, const int dir, const SearchData * const data)
183 : Isibaar 580 {
184 : syskin 663 // create or find a qpel-precision reference picture; return pointer to it
185 : syskin 668 uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir;
186 : syskin 663 const int32_t iEdgedWidth = data->iEdgedWidth;
187 :     const uint32_t rounding = data->rounding;
188 :     const int halfpel_x = x/2;
189 :     const int halfpel_y = y/2;
190 : Isibaar 580 const uint8_t *ref1, *ref2, *ref3, *ref4;
191 :    
192 : syskin 663 ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases
193 :     switch( ((x&1)<<1) + (y&1) ) {
194 :     case 0: // pure halfpel position
195 :     return (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data);
196 : Isibaar 580 case 1: // x halfpel, y qpel - top or bottom during qpel refinement
197 : syskin 663 ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
198 : Isibaar 665 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
199 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
200 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
201 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
202 : Isibaar 580 break;
203 :    
204 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
205 : syskin 663 ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data);
206 : Isibaar 665 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
207 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
208 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
209 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
210 : Isibaar 580 break;
211 :    
212 :     default: // x and y in qpel resolution - the "corners" (top left/right and
213 :     // bottom left/right) during qpel refinement
214 : syskin 663 ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
215 :     ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data);
216 :     ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data);
217 : Isibaar 580 interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
218 :     interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
219 :     interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
220 :     interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
221 :     break;
222 :     }
223 : syskin 663 return Reference;
224 :     }
225 :    
226 :     /* CHECK_CANDIATE FUNCTIONS START */
227 :    
228 :     static void
229 :     CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
230 :     {
231 :     int t, xc, yc;
232 :     const uint8_t * Reference;
233 :     VECTOR * current;
234 :    
235 :     if (( x > data->max_dx) || ( x < data->min_dx)
236 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
237 :    
238 :     if (data->qpel_precision) { // x and y are in 1/4 precision
239 :     Reference = Interpolate16x16qpel(x, y, 0, data);
240 :     t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
241 :     xc = x/2; yc = y/2; //for chroma sad
242 :     current = data->currentQMV;
243 :     } else {
244 :     switch ( ((x&1)<<1) + (y&1) ) {
245 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
246 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
247 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
248 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
249 :     }
250 :     if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode);
251 :     else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
252 :     current = data->currentMV;
253 :     xc = x; yc = y;
254 :     }
255 : Isibaar 580
256 : syskin 663 data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);
257 : Isibaar 580
258 : syskin 628 data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;
259 :     data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;
260 : Isibaar 580
261 : syskin 663 if (data->chroma) data->temp[0] += ChromaSAD(xc, yc, data);
262 : syskin 628
263 : Isibaar 580 if (data->temp[0] < data->iMinSAD[0]) {
264 :     data->iMinSAD[0] = data->temp[0];
265 : syskin 663 current[0].x = x; current[0].y = y;
266 :     *dir = Direction; }
267 : Isibaar 580
268 :     if (data->temp[1] < data->iMinSAD[1]) {
269 : syskin 663 data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y= y; }
270 : Isibaar 580 if (data->temp[2] < data->iMinSAD[2]) {
271 : syskin 663 data->iMinSAD[2] = data->temp[2]; current[2].x = x; current[2].y = y; }
272 : Isibaar 580 if (data->temp[3] < data->iMinSAD[3]) {
273 : syskin 663 data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; }
274 : Isibaar 580 if (data->temp[4] < data->iMinSAD[4]) {
275 : syskin 663 data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; }
276 :    
277 : Isibaar 580 }
278 :    
279 :     static void
280 : syskin 663 CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
281 : syskin 658 {
282 :     int32_t sad;
283 : syskin 663 const uint8_t * Reference;
284 :     int t;
285 :     VECTOR * current;
286 : syskin 658
287 :     if (( x > data->max_dx) || ( x < data->min_dx)
288 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
289 :    
290 : syskin 663 if (data->qpel_precision) { // x and y are in 1/4 precision
291 :     Reference = Interpolate16x16qpel(x, y, 0, data);
292 :     t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
293 :     current = data->currentQMV;
294 :     } else {
295 :     switch ( ((x&1)<<1) + (y&1) ) {
296 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
297 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
298 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
299 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
300 :     }
301 :     if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode);
302 :     else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
303 :     current = data->currentMV;
304 : syskin 658 }
305 :    
306 :     sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
307 : syskin 663 sad += (data->lambda16 * t * sad)/1000;
308 : syskin 658
309 : syskin 663 if (sad < *(data->iMinSAD)) {
310 :     *(data->iMinSAD) = sad;
311 :     current->x = x; current->y = y;
312 :     *dir = Direction; }
313 : syskin 658 }
314 :    
315 :     static void
316 : Isibaar 539 CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
317 :     {
318 : syskin 658 // maximum speed - for P/B/I decision
319 : Isibaar 539 int32_t sad;
320 :    
321 :     if (( x > data->max_dx) || ( x < data->min_dx)
322 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
323 :    
324 : syskin 601 sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth),
325 : Isibaar 539 data->iEdgedWidth, 256*4096);
326 :    
327 :     if (sad < *(data->iMinSAD)) {
328 :     *(data->iMinSAD) = sad;
329 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
330 :     *dir = Direction; }
331 :     }
332 :    
333 :    
334 :     static void
335 : chl 530 CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)
336 :     {
337 :     int32_t sad;
338 : syskin 663 int xb, yb, t;
339 : chl 530 const uint8_t *ReferenceF, *ReferenceB;
340 : syskin 663 VECTOR *current;
341 : Isibaar 3
342 : chl 530 if (( xf > data->max_dx) || ( xf < data->min_dx)
343 :     || ( yf > data->max_dy) || (yf < data->min_dy)) return;
344 : Isibaar 3
345 : syskin 663 if (data->qpel_precision) {
346 :     ReferenceF = Interpolate16x16qpel(xf, yf, 0, data);
347 :     xb = data->currentQMV[1].x; yb = data->currentQMV[1].y;
348 :     current = data->currentQMV;
349 :     ReferenceB = Interpolate16x16qpel(xb, yb, 1, data);
350 :     t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode)
351 :     + d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode);
352 :     } else {
353 :     ReferenceF = Interpolate16x16qpel(2*xf, 2*yf, 0, data);
354 :     xb = data->currentMV[1].x; yb = data->currentMV[1].y;
355 :     ReferenceB = Interpolate16x16qpel(2*xb, 2*yb, 1, data);
356 :     current = data->currentMV;
357 :     if (data->qpel)
358 :     t = d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode)
359 :     + d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode);
360 :     else
361 :     t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode)
362 :     + d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode);
363 : chl 530 }
364 : edgomez 195
365 : syskin 628 sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
366 : syskin 663 sad += (data->lambda16 * t * sad)/1000;
367 : syskin 628
368 : chl 530 if (sad < *(data->iMinSAD)) {
369 :     *(data->iMinSAD) = sad;
370 : syskin 663 current->x = xf; current->y = yf;
371 : chl 530 *dir = Direction; }
372 : Isibaar 3 }
373 :    
374 : chl 530 static void
375 :     CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
376 : Isibaar 3 {
377 : syskin 628 int32_t sad = 0;
378 : chl 530 int k;
379 :     const uint8_t *ReferenceF;
380 :     const uint8_t *ReferenceB;
381 :     VECTOR mvs, b_mvs;
382 : Isibaar 3
383 : chl 530 if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
384 : Isibaar 3
385 : chl 530 for (k = 0; k < 4; k++) {
386 :     mvs.x = data->directmvF[k].x + x;
387 :     b_mvs.x = ((x == 0) ?
388 :     data->directmvB[k].x
389 :     : mvs.x - data->referencemv[k].x);
390 : Isibaar 3
391 : chl 530 mvs.y = data->directmvF[k].y + y;
392 :     b_mvs.y = ((y == 0) ?
393 :     data->directmvB[k].y
394 :     : mvs.y - data->referencemv[k].y);
395 :    
396 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
397 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
398 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
399 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
400 : Isibaar 3
401 : syskin 663 if (!data->qpel) {
402 :     mvs.x *= 2; mvs.y *= 2;
403 :     b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway
404 : chl 530 }
405 : syskin 663 ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);
406 :     ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);
407 : chl 530
408 :     sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
409 : syskin 663 ReferenceF, ReferenceB,
410 : chl 530 data->iEdgedWidth);
411 :     if (sad > *(data->iMinSAD)) return;
412 : edgomez 78 }
413 : chl 530
414 : syskin 628 sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
415 :    
416 : chl 530 if (sad < *(data->iMinSAD)) {
417 :     *(data->iMinSAD) = sad;
418 :     data->currentMV->x = x; data->currentMV->y = y;
419 :     *dir = Direction; }
420 : Isibaar 3 }
421 :    
422 : chl 530 static void
423 :     CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
424 : chl 96 {
425 : chl 530 int32_t sad;
426 :     const uint8_t *ReferenceF;
427 :     const uint8_t *ReferenceB;
428 :     VECTOR mvs, b_mvs;
429 : chl 96
430 : syskin 576 if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
431 : edgomez 195
432 : chl 530 mvs.x = data->directmvF[0].x + x;
433 :     b_mvs.x = ((x == 0) ?
434 :     data->directmvB[0].x
435 :     : mvs.x - data->referencemv[0].x);
436 : edgomez 195
437 : chl 530 mvs.y = data->directmvF[0].y + y;
438 :     b_mvs.y = ((y == 0) ?
439 :     data->directmvB[0].y
440 :     : mvs.y - data->referencemv[0].y);
441 :    
442 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
443 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
444 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
445 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
446 : chl 96
447 : syskin 663 if (!data->qpel) {
448 :     mvs.x *= 2; mvs.y *= 2;
449 :     b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway
450 :     }
451 :     ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data);
452 :     ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data);
453 : chl 96
454 : syskin 628 sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
455 :     sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
456 : chl 96
457 : chl 530 if (sad < *(data->iMinSAD)) {
458 :     *(data->iMinSAD) = sad;
459 :     data->currentMV->x = x; data->currentMV->y = y;
460 :     *dir = Direction; }
461 :     }
462 : chl 96
463 : chl 530 static void
464 :     CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
465 :     {
466 : syskin 628 int32_t sad; int t;
467 : chl 530 const uint8_t * Reference;
468 : edgomez 195
469 : chl 530 if (( x > data->max_dx) || ( x < data->min_dx)
470 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
471 : chl 96
472 : syskin 663 if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data);
473 :     else Reference = Interpolate16x16qpel(2*x, 2*y, 0, data);
474 : edgomez 195
475 : chl 530 sad = sad8(data->Cur, Reference, data->iEdgedWidth);
476 : syskin 663 if (data->qpel) t = d_mv_bits(2 * x - data->predMV.x, 2 * y - data->predMV.y, data->iFcode);
477 : syskin 628 else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
478 : edgomez 195
479 : syskin 628 sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100;
480 : chl 96
481 : Isibaar 606 if (sad < *(data->iMinSAD)) {
482 :     *(data->iMinSAD) = sad;
483 :     data->currentMV->x = x; data->currentMV->y = y;
484 :     *dir = Direction; }
485 :     }
486 :    
487 : Isibaar 539 /* CHECK_CANDIATE FUNCTIONS END */
488 : chl 96
489 : chl 530 /* MAINSEARCH FUNCTIONS START */
490 : edgomez 195
491 : chl 530 static void
492 :     AdvDiamondSearch(int x, int y, const SearchData * const data, int bDirection)
493 : chl 181 {
494 :    
495 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
496 :    
497 : chl 530 int iDirection;
498 : edgomez 195
499 :     do {
500 : chl 181 iDirection = 0;
501 : chl 530 if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
502 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
503 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);
504 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
505 : chl 181
506 :     /* now we're doing diagonal checks near our candidate */
507 :    
508 : chl 530 if (iDirection) { //checking if anything found
509 : chl 181 bDirection = iDirection;
510 :     iDirection = 0;
511 : chl 530 x = data->currentMV->x; y = data->currentMV->y;
512 :     if (bDirection & 3) { //our candidate is left or right
513 :     CHECK_CANDIDATE(x, y + iDiamondSize, 8);
514 :     CHECK_CANDIDATE(x, y - iDiamondSize, 4);
515 :     } else { // what remains here is up or down
516 :     CHECK_CANDIDATE(x + iDiamondSize, y, 2);
517 :     CHECK_CANDIDATE(x - iDiamondSize, y, 1); }
518 : chl 181
519 : edgomez 195 if (iDirection) {
520 :     bDirection += iDirection;
521 : chl 530 x = data->currentMV->x; y = data->currentMV->y; }
522 :     } else { //about to quit, eh? not so fast....
523 : edgomez 195 switch (bDirection) {
524 : chl 181 case 2:
525 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
526 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
527 : chl 181 break;
528 :     case 1:
529 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
530 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
531 : chl 181 break;
532 : edgomez 195 case 2 + 4:
533 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
534 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
535 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
536 : chl 181 break;
537 :     case 4:
538 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
539 :     CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
540 : chl 181 break;
541 :     case 8:
542 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
543 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
544 : chl 181 break;
545 : edgomez 195 case 1 + 4:
546 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
547 :     CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
548 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
549 : chl 181 break;
550 : edgomez 195 case 2 + 8:
551 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
552 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
553 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
554 : chl 181 break;
555 : edgomez 195 case 1 + 8:
556 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
557 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
558 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
559 : chl 181 break;
560 : edgomez 195 default: //1+2+4+8 == we didn't find anything at all
561 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
562 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
563 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
564 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
565 : chl 181 break;
566 :     }
567 : chl 530 if (!iDirection) break; //ok, the end. really
568 :     bDirection = iDirection;
569 :     x = data->currentMV->x; y = data->currentMV->y;
570 : chl 181 }
571 :     }
572 : edgomez 195 while (1); //forever
573 : chl 181 }
574 :    
575 : chl 530 static void
576 :     SquareSearch(int x, int y, const SearchData * const data, int bDirection)
577 : chl 326 {
578 : chl 530 int iDirection;
579 : chl 326
580 : chl 530 do {
581 :     iDirection = 0;
582 :     if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1+16+64);
583 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2+32+128);
584 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4+16+32);
585 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8+64+128);
586 :     if (bDirection & 16) CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1+4+16+32+64);
587 :     if (bDirection & 32) CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2+4+16+32+128);
588 :     if (bDirection & 64) CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1+8+16+64+128);
589 :     if (bDirection & 128) CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2+8+32+64+128);
590 : chl 326
591 : chl 530 bDirection = iDirection;
592 :     x = data->currentMV->x; y = data->currentMV->y;
593 :     } while (iDirection);
594 : chl 346 }
595 :    
596 : chl 530 static void
597 :     DiamondSearch(int x, int y, const SearchData * const data, int bDirection)
598 : chl 346 {
599 :    
600 : chl 181 /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
601 :    
602 : chl 530 int iDirection;
603 : edgomez 195
604 :     do {
605 : chl 181 iDirection = 0;
606 : chl 530 if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
607 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
608 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);
609 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
610 : chl 181
611 :     /* now we're doing diagonal checks near our candidate */
612 :    
613 : chl 530 if (iDirection) { //checking if anything found
614 : chl 181 bDirection = iDirection;
615 :     iDirection = 0;
616 : chl 530 x = data->currentMV->x; y = data->currentMV->y;
617 :     if (bDirection & 3) { //our candidate is left or right
618 :     CHECK_CANDIDATE(x, y + iDiamondSize, 8);
619 :     CHECK_CANDIDATE(x, y - iDiamondSize, 4);
620 :     } else { // what remains here is up or down
621 :     CHECK_CANDIDATE(x + iDiamondSize, y, 2);
622 :     CHECK_CANDIDATE(x - iDiamondSize, y, 1); }
623 : chl 181
624 : chl 530 bDirection += iDirection;
625 :     x = data->currentMV->x; y = data->currentMV->y;
626 : chl 181 }
627 :     }
628 : chl 530 while (iDirection);
629 : chl 181 }
630 :    
631 : chl 530 /* MAINSEARCH FUNCTIONS END */
632 : chl 181
633 : chl 530 /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */
634 :    
635 :     static void
636 : syskin 663 SubpelRefine(const SearchData * const data)
637 : chl 96 {
638 : syskin 663 /* Do a half-pel or q-pel refinement */
639 :     VECTOR backupMV;
640 : chl 530 int iDirection; //not needed
641 : chl 96
642 : syskin 663 if (data->qpel_precision)
643 :     backupMV = *(data->currentQMV);
644 :     else backupMV = *(data->currentMV);
645 : edgomez 195
646 : Isibaar 580 CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);
647 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);
648 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);
649 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);
650 :    
651 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);
652 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);
653 :    
654 :     CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);
655 :     CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);
656 :     }
657 :    
658 : chl 530 static __inline int
659 :     SkipDecisionP(const IMAGE * current, const IMAGE * reference,
660 :     const int x, const int y,
661 :     const uint32_t iEdgedWidth, const uint32_t iQuant)
662 : chl 96
663 : Isibaar 3 {
664 : chl 530 /* keep repeating checks for all b-frames before this P frame,
665 :     to make sure that SKIP is possible (todo)
666 :     how: if skip is not possible set sad00 to a very high value */
667 : Isibaar 3
668 : chl 530 uint32_t sadC = sad8(current->u + x*8 + y*(iEdgedWidth/2)*8,
669 :     reference->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2);
670 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
671 : h 545 sadC += sad8(current->v + (x + y*(iEdgedWidth/2))*8,
672 :     reference->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
673 : chl 530 if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
674 : edgomez 195
675 : chl 530 return 1;
676 :     }
677 : edgomez 195
678 : chl 530 static __inline void
679 :     SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad)
680 :     {
681 :     pMB->mode = MODE_NOT_CODED;
682 : Isibaar 539 pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
683 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
684 : syskin 628
685 : Isibaar 580 pMB->qmvs[0].x = pMB->qmvs[1].x = pMB->qmvs[2].x = pMB->qmvs[3].x = 0;
686 :     pMB->qmvs[0].y = pMB->qmvs[1].y = pMB->qmvs[2].y = pMB->qmvs[3].y = 0;
687 :    
688 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
689 : Isibaar 3 }
690 :    
691 : syskin 628 bool
692 : chl 530 MotionEstimation(MBParam * const pParam,
693 :     FRAMEINFO * const current,
694 :     FRAMEINFO * const reference,
695 :     const IMAGE * const pRefH,
696 :     const IMAGE * const pRefV,
697 :     const IMAGE * const pRefHV,
698 :     const uint32_t iLimit)
699 : Isibaar 3 {
700 : chl 530 MACROBLOCK *const pMBs = current->mbs;
701 :     const IMAGE *const pCurrent = &current->image;
702 :     const IMAGE *const pRef = &reference->image;
703 : Isibaar 3
704 : chl 530 const VECTOR zeroMV = { 0, 0 };
705 : Isibaar 3
706 : chl 530 uint32_t x, y;
707 :     uint32_t iIntra = 0;
708 : syskin 628 int32_t InterBias, quant = current->quant, sad00;
709 : Isibaar 580 uint8_t *qimage;
710 : edgomez 195
711 : Isibaar 539 // some pre-initialized thingies for SearchP
712 :     int32_t temp[5];
713 :     VECTOR currentMV[5];
714 : Isibaar 580 VECTOR currentQMV[5];
715 : Isibaar 539 int32_t iMinSAD[5];
716 :     SearchData Data;
717 :     Data.iEdgedWidth = pParam->edged_width;
718 :     Data.currentMV = currentMV;
719 : Isibaar 580 Data.currentQMV = currentQMV;
720 : Isibaar 539 Data.iMinSAD = iMinSAD;
721 :     Data.temp = temp;
722 :     Data.iFcode = current->fcode;
723 : Isibaar 580 Data.rounding = pParam->m_rounding_type;
724 : syskin 628 Data.qpel = pParam->m_quarterpel;
725 :     Data.chroma = current->global_flags & XVID_ME_COLOUR;
726 : Isibaar 539
727 : Isibaar 580 if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
728 : syskin 594 return 1; // allocate some mem for qpel interpolated blocks
729 : Isibaar 580 // somehow this is dirty since I think we shouldn't use malloc outside
730 :     // encoder_create() - so please fix me!
731 : syskin 628 Data.RefQ = qimage;
732 : chl 530 if (sadInit) (*sadInit) ();
733 : edgomez 195
734 : chl 530 for (y = 0; y < pParam->mb_height; y++) {
735 :     for (x = 0; x < pParam->mb_width; x++) {
736 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
737 : syskin 628
738 :     pMB->sad16
739 : chl 530 = sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16,
740 :     pRef->y + (x + y * pParam->edged_width) * 16,
741 :     pParam->edged_width, pMB->sad8 );
742 : edgomez 195
743 : syskin 628 if (Data.chroma) {
744 :     pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8,
745 :     pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2);
746 :    
747 :     pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8,
748 :     pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2);
749 :     }
750 :    
751 :     sad00 = pMB->sad16; //if no gmc; else sad00 = (..)
752 :    
753 : chl 530 if (!(current->global_flags & XVID_LUMIMASKING)) {
754 :     pMB->dquant = NO_CHANGE;
755 : syskin 601 pMB->quant = current->quant;
756 :     } else {
757 : syskin 574 if (pMB->dquant != NO_CHANGE) {
758 :     quant += DQtab[pMB->dquant];
759 :     if (quant > 31) quant = 31;
760 :     else if (quant < 1) quant = 1;
761 :     }
762 : syskin 601 pMB->quant = quant;
763 :     }
764 : chl 181
765 : syskin 628 //initial skip decision
766 : chl 619 /* no early skip for GMC (global vector = skip vector is unknown!) */
767 :     if (current->coding_type == P_VOP) { /* no fast SKIP for S(GMC)-VOPs */
768 :     if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH)
769 : syskin 628 if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {
770 : chl 619 SkipMacroblockP(pMB, sad00);
771 :     continue;
772 :     }
773 :     }
774 : syskin 628
775 :     SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
776 : chl 530 y, current->motion_flags, pMB->quant,
777 : Isibaar 539 &Data, pParam, pMBs, reference->mbs,
778 : chl 530 current->global_flags & XVID_INTER4V, pMB);
779 : edgomez 195
780 : chl 530 /* final skip decision, a.k.a. "the vector you found, really that good?" */
781 : chl 619 if (current->coding_type == P_VOP) {
782 :     if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)
783 : syskin 601 && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) )
784 : syskin 628 if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {
785 : chl 619 SkipMacroblockP(pMB, sad00);
786 :     continue;
787 :     }
788 :     }
789 : syskin 628
790 : chl 530 /* finally, intra decision */
791 : Isibaar 3
792 : chl 530 InterBias = MV16_INTER_BIAS;
793 : syskin 639 if (pMB->quant > 8) InterBias += 100 * (pMB->quant - 8); // to make high quants work
794 : chl 530 if (y != 0)
795 : syskin 639 if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
796 : chl 530 if (x != 0)
797 : syskin 639 if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
798 : syskin 628
799 :     if (Data.chroma) InterBias += 50; // to compensate bigger SAD
800 : Isibaar 3
801 : chl 530 if (InterBias < pMB->sad16) {
802 :     const int32_t deviation =
803 :     dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,
804 :     pParam->edged_width);
805 : Isibaar 3
806 : chl 530 if (deviation < (pMB->sad16 - InterBias)) {
807 : Isibaar 580 if (++iIntra >= iLimit) { free(qimage); return 1; }
808 : chl 530 pMB->mode = MODE_INTRA;
809 : Isibaar 539 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] =
810 :     pMB->mvs[3] = zeroMV;
811 : Isibaar 580 pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] =
812 :     pMB->qmvs[3] = zeroMV;
813 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] =
814 :     pMB->sad8[3] = 0;
815 :     }
816 :     }
817 :     }
818 : Isibaar 3 }
819 : Isibaar 580 free(qimage);
820 : chl 619
821 :     if (current->coding_type == S_VOP) /* first GMC step only for S(GMC)-VOPs */
822 : syskin 628 current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode );
823 : chl 619 else
824 :     current->GMC_MV = zeroMV;
825 :    
826 : syskin 628 return 0;
827 : chl 530 }
828 : Isibaar 3
829 : edgomez 195
830 : chl 530 #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
831 : Isibaar 3
832 : chl 530 static __inline int
833 :     make_mask(const VECTOR * const pmv, const int i)
834 :     {
835 : Isibaar 539 int mask = 255, j;
836 : chl 530 for (j = 0; j < i; j++) {
837 :     if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already
838 :     if (pmv[i].x == pmv[j].x) {
839 :     if (pmv[i].y == pmv[j].y + iDiamondSize) { mask &= ~4; continue; }
840 :     if (pmv[i].y == pmv[j].y - iDiamondSize) { mask &= ~8; continue; }
841 :     } else
842 :     if (pmv[i].y == pmv[j].y) {
843 :     if (pmv[i].x == pmv[j].x + iDiamondSize) { mask &= ~1; continue; }
844 :     if (pmv[i].x == pmv[j].x - iDiamondSize) { mask &= ~2; continue; }
845 :     }
846 : Isibaar 3 }
847 : chl 530 return mask;
848 :     }
849 : edgomez 195
850 : chl 530 static __inline void
851 :     PreparePredictionsP(VECTOR * const pmv, int x, int y, const int iWcount,
852 :     const int iHcount, const MACROBLOCK * const prevMB)
853 :     {
854 : edgomez 195
855 : chl 530 //this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself
856 : edgomez 195
857 : chl 530 if ( (y != 0) && (x != (iWcount-1)) ) { // [5] top-right neighbour
858 :     pmv[5].x = EVEN(pmv[3].x);
859 : Isibaar 539 pmv[5].y = EVEN(pmv[3].y);
860 :     } else pmv[5].x = pmv[5].y = 0;
861 : chl 169
862 : chl 530 if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }// pmv[3] is left neighbour
863 :     else pmv[3].x = pmv[3].y = 0;
864 : Isibaar 3
865 : chl 530 if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }// [4] top neighbour
866 :     else pmv[4].x = pmv[4].y = 0;
867 : chl 169
868 : chl 530 // [1] median prediction
869 :     pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);
870 : chl 169
871 : chl 530 pmv[0].x = pmv[0].y = 0; // [0] is zero; not used in the loop (checked before) but needed here for make_mask
872 : chl 169
873 : chl 530 pmv[2].x = EVEN(prevMB->mvs[0].x); // [2] is last frame
874 :     pmv[2].y = EVEN(prevMB->mvs[0].y);
875 : chl 169
876 : chl 530 if ((x != iWcount-1) && (y != iHcount-1)) {
877 :     pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); //[6] right-down neighbour in last frame
878 : Isibaar 539 pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
879 :     } else pmv[6].x = pmv[6].y = 0;
880 : chl 530 }
881 : chl 169
882 : chl 530 static void
883 : syskin 628 SearchP(const IMAGE * const pRef,
884 : chl 530 const uint8_t * const pRefH,
885 :     const uint8_t * const pRefV,
886 :     const uint8_t * const pRefHV,
887 :     const IMAGE * const pCur,
888 :     const int x,
889 :     const int y,
890 :     const uint32_t MotionFlags,
891 :     const uint32_t iQuant,
892 : Isibaar 539 SearchData * const Data,
893 : chl 530 const MBParam * const pParam,
894 :     const MACROBLOCK * const pMBs,
895 :     const MACROBLOCK * const prevMBs,
896 :     int inter4v,
897 :     MACROBLOCK * const pMB)
898 :     {
899 : chl 169
900 : chl 530 int i, iDirection = 255, mask, threshA;
901 : Isibaar 539 VECTOR pmv[7];
902 : Isibaar 3
903 : Isibaar 539 get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)()
904 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
905 : Isibaar 580 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
906 : Isibaar 3
907 : Isibaar 539 Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
908 : syskin 628 Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
909 :     Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
910 :    
911 :     Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;
912 : Isibaar 539 Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;
913 :     Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;
914 :     Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
915 : syskin 628 Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
916 :     Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
917 : Isibaar 3
918 : syskin 601 Data->lambda16 = lambda_vec16[iQuant];
919 : Isibaar 606 Data->lambda8 = lambda_vec8[iQuant];
920 : syskin 663 Data->qpel_precision = 0;
921 : chl 169
922 : chl 530 if (!(MotionFlags & PMV_HALFPEL16)) {
923 : Isibaar 539 Data->min_dx = EVEN(Data->min_dx);
924 :     Data->max_dx = EVEN(Data->max_dx);
925 :     Data->min_dy = EVEN(Data->min_dy);
926 :     Data->max_dy = EVEN(Data->max_dy); }
927 : edgomez 568
928 :     if (pMB->dquant != NO_CHANGE) inter4v = 0;
929 : edgomez 195
930 : Isibaar 580 for(i = 0; i < 5; i++)
931 :     Data->currentMV[i].x = Data->currentMV[i].y = 0;
932 : Isibaar 539
933 : syskin 663 if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
934 :     else Data->predMV = pmv[0];
935 : Isibaar 606
936 : syskin 663 i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode);
937 : syskin 628 Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000;
938 :     Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100;
939 : Isibaar 539 Data->iMinSAD[2] = pMB->sad8[1];
940 :     Data->iMinSAD[3] = pMB->sad8[2];
941 :     Data->iMinSAD[4] = pMB->sad8[3];
942 :    
943 : chl 530 if ((x == 0) && (y == 0)) threshA = 512;
944 :     else {
945 : edgomez 568 threshA = Data->temp[0]; // that's when we keep this SAD atm
946 : chl 530 if (threshA < 512) threshA = 512;
947 :     if (threshA > 1024) threshA = 1024; }
948 : Isibaar 3
949 : chl 530 PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
950 :     prevMBs + x + y * pParam->mb_width);
951 : edgomez 195
952 : syskin 663 if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16;
953 :     else CheckCandidate = CheckCandidate16no4v; //for extra speed
954 : edgomez 568
955 : chl 530 /* main loop. checking all predictions */
956 :    
957 :     for (i = 1; i < 7; i++) {
958 :     if (!(mask = make_mask(pmv, i)) ) continue;
959 : edgomez 568 (*CheckCandidate)(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
960 : Isibaar 539 if (Data->iMinSAD[0] <= threshA) break;
961 : chl 530 }
962 :    
963 : Isibaar 539 if ((Data->iMinSAD[0] <= threshA) ||
964 :     (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
965 :     (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {
966 : chl 530 inter4v = 0;
967 : Isibaar 539 } else {
968 : Isibaar 3
969 : Isibaar 539 MainSearchFunc * MainSearchPtr;
970 :     if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
971 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
972 :     else MainSearchPtr = DiamondSearch;
973 : chl 181
974 : Isibaar 539 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
975 : Isibaar 3
976 : chl 530 /* extended search, diamond starting in 0,0 and in prediction.
977 :     note that this search is/might be done in halfpel positions,
978 :     which makes it more different than the diamond above */
979 : chl 259
980 : Isibaar 539 if (MotionFlags & PMV_EXTSEARCH16) {
981 :     int32_t bSAD;
982 :     VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
983 :     if (!(MotionFlags & PMV_HALFPELREFINE16)) // who's gonna use extsearch and no halfpel?
984 :     startMV.x = EVEN(startMV.x); startMV.y = EVEN(startMV.y);
985 :     if (!(MVequal(startMV, backupMV))) {
986 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
987 : Isibaar 3
988 : Isibaar 606 (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);
989 : Isibaar 539 (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
990 :     if (bSAD < Data->iMinSAD[0]) {
991 :     Data->currentMV[0] = backupMV;
992 :     Data->iMinSAD[0] = bSAD; }
993 :     }
994 : Isibaar 3
995 : Isibaar 539 backupMV = Data->currentMV[0];
996 :     if (MotionFlags & PMV_HALFPELREFINE16) startMV.x = startMV.y = 1;
997 :     else startMV.x = startMV.y = 0;
998 :     if (!(MVequal(startMV, backupMV))) {
999 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1000 : edgomez 195
1001 : Isibaar 606 (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);
1002 : Isibaar 539 (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1003 :     if (bSAD < Data->iMinSAD[0]) {
1004 :     Data->currentMV[0] = backupMV;
1005 :     Data->iMinSAD[0] = bSAD; }
1006 :     }
1007 : Isibaar 3 }
1008 :     }
1009 :    
1010 : syskin 663 if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data);
1011 : Isibaar 3
1012 : Isibaar 580 for(i = 0; i < 5; i++) {
1013 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1014 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1015 :     }
1016 :    
1017 :     if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1018 :    
1019 : syskin 663 Data->qpel_precision = 1;
1020 : Isibaar 606 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1021 : syskin 601 pParam->width, pParam->height, Data->iFcode, 0);
1022 :    
1023 : syskin 663 SubpelRefine(Data);
1024 : Isibaar 580 }
1025 :    
1026 : syskin 628 if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0;
1027 : Isibaar 539 if (inter4v) {
1028 :     SearchData Data8;
1029 :     Data8.iFcode = Data->iFcode;
1030 : Isibaar 606 Data8.lambda8 = Data->lambda8;
1031 : Isibaar 539 Data8.iEdgedWidth = Data->iEdgedWidth;
1032 : Isibaar 606 Data8.RefQ = Data->RefQ;
1033 : syskin 628 Data8.qpel = Data->qpel;
1034 : Isibaar 539 Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1035 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
1036 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1037 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1038 : syskin 628
1039 :     if (Data->chroma) {
1040 : syskin 663 int sumx, sumy, dx, dy;
1041 : syskin 628
1042 :     if(pParam->m_quarterpel) {
1043 : syskin 663 sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;
1044 :     sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;
1045 :     } else {
1046 :     sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1047 :     sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1048 :     }
1049 :     dx = (sumx >> 3) + roundtab_76[sumx & 0xf];
1050 :     dy = (sumy >> 3) + roundtab_76[sumy & 0xf];
1051 : syskin 628
1052 :     Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
1053 :     }
1054 : Isibaar 539 }
1055 : Isibaar 3
1056 : chl 530 if (!(inter4v) ||
1057 : Isibaar 539 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
1058 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {
1059 : chl 530 // INTER MODE
1060 :     pMB->mode = MODE_INTER;
1061 : Isibaar 539 pMB->mvs[0] = pMB->mvs[1]
1062 :     = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1063 : Isibaar 3
1064 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1065 : Isibaar 539 pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
1066 : Isibaar 3
1067 : Isibaar 580 if(pParam->m_quarterpel) {
1068 : syskin 663 pMB->qmvs[0] = pMB->qmvs[1]
1069 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1070 :     pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
1071 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
1072 : syskin 628 } else {
1073 : Isibaar 580 pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1074 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1075 :     }
1076 : edgomez 195 } else {
1077 : chl 530 // INTER4V MODE; all other things are already set in Search8
1078 :     pMB->mode = MODE_INTER4V;
1079 : Isibaar 539 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] +
1080 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * iQuant;
1081 : edgomez 78 }
1082 : Isibaar 3 }
1083 :    
1084 : chl 530 static void
1085 :     Search8(const SearchData * const OldData,
1086 :     const int x, const int y,
1087 :     const uint32_t MotionFlags,
1088 :     const MBParam * const pParam,
1089 :     MACROBLOCK * const pMB,
1090 :     const MACROBLOCK * const pMBs,
1091 : Isibaar 539 const int block,
1092 :     SearchData * const Data)
1093 : chl 345 {
1094 : Isibaar 539 Data->iMinSAD = OldData->iMinSAD + 1 + block;
1095 :     Data->currentMV = OldData->currentMV + 1 + block;
1096 : Isibaar 580 Data->currentQMV = OldData->currentQMV + 1 + block;
1097 : chl 345
1098 : syskin 601 if(pParam->m_quarterpel) {
1099 : syskin 663 Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1100 : syskin 628 if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *
1101 : syskin 663 d_mv_bits( Data->currentQMV->x - Data->predMV.x,
1102 :     Data->currentQMV->y - Data->predMV.y,
1103 : syskin 628 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;
1104 : Isibaar 606 } else {
1105 :     Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1106 : syskin 628 if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *
1107 : syskin 601 d_mv_bits( Data->currentMV->x - Data->predMV.x,
1108 :     Data->currentMV->y - Data->predMV.y,
1109 : syskin 628 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;
1110 : Isibaar 606 }
1111 : chl 345
1112 : chl 530 if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) {
1113 : chl 345
1114 : Isibaar 539 Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1));
1115 :     Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1));
1116 :     Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1117 :     Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1118 : chl 345
1119 : Isibaar 539 Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));
1120 : syskin 663 Data->qpel_precision = 0;
1121 : chl 530
1122 : Isibaar 539 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1123 : Isibaar 580 pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);
1124 : syskin 628 CheckCandidate = CheckCandidate8;
1125 : chl 345
1126 : chl 530 if (MotionFlags & PMV_EXTSEARCH8) {
1127 : Isibaar 580 int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1128 :    
1129 : chl 530 MainSearchFunc *MainSearchPtr;
1130 :     if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;
1131 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;
1132 :     else MainSearchPtr = DiamondSearch;
1133 : chl 345
1134 : Isibaar 580 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1135 : chl 345
1136 : syskin 601 if(*(Data->iMinSAD) < temp_sad) {
1137 : Isibaar 580 Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1138 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1139 :     }
1140 :     }
1141 :    
1142 :     if (MotionFlags & PMV_HALFPELREFINE8) {
1143 :     int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1144 :    
1145 : syskin 663 SubpelRefine(Data); // perform halfpel refine of current best vector
1146 : Isibaar 580
1147 :     if(*(Data->iMinSAD) < temp_sad) { // we have found a better match
1148 :     Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1149 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1150 :     }
1151 :     }
1152 :    
1153 : syskin 601 if(pParam->m_quarterpel) {
1154 :     if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&
1155 :     (MotionFlags & PMV_QUARTERPELREFINE8)) {
1156 : syskin 663 Data->qpel_precision = 1;
1157 : Isibaar 606 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1158 : syskin 663 pParam->width, pParam->height, OldData->iFcode, 0);
1159 :     SubpelRefine(Data);
1160 : syskin 601 }
1161 : Isibaar 580 }
1162 : chl 345 }
1163 : chl 530
1164 : Isibaar 580 if(pParam->m_quarterpel) {
1165 : syskin 663 pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;
1166 :     pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;
1167 :     pMB->qmvs[block] = *(Data->currentQMV);
1168 : Isibaar 580 }
1169 :     else {
1170 :     pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
1171 :     pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
1172 :     }
1173 :    
1174 : Isibaar 539 pMB->mvs[block] = *(Data->currentMV);
1175 : syskin 601 pMB->sad8[block] = 4 * (*Data->iMinSAD);
1176 : chl 345 }
1177 :    
1178 : chl 530 /* B-frames code starts here */
1179 : chl 345
1180 : chl 530 static __inline VECTOR
1181 :     ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)
1182 : Isibaar 3 {
1183 : chl 530 /* the stupidiest function ever */
1184 :     if (mode == MODE_FORWARD) return pMB->mvs[0];
1185 :     else return pMB->b_mvs[0];
1186 : Isibaar 3 }
1187 :    
1188 : chl 530 static void __inline
1189 :     PreparePredictionsBF(VECTOR * const pmv, const int x, const int y,
1190 :     const uint32_t iWcount,
1191 :     const MACROBLOCK * const pMB,
1192 :     const uint32_t mode_curr)
1193 : Isibaar 3 {
1194 :    
1195 : chl 530 // [0] is prediction
1196 :     pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);
1197 : Isibaar 3
1198 : chl 530 pmv[1].x = pmv[1].y = 0; // [1] is zero
1199 : Isibaar 3
1200 : chl 530 pmv[2] = ChoosePred(pMB, mode_curr);
1201 :     pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);
1202 : edgomez 195
1203 : chl 530 if ((y != 0)&&(x != (int)(iWcount+1))) { // [3] top-right neighbour
1204 :     pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);
1205 : Isibaar 539 pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);
1206 :     } else pmv[3].x = pmv[3].y = 0;
1207 : Isibaar 3
1208 : chl 530 if (y != 0) {
1209 :     pmv[4] = ChoosePred(pMB-iWcount, mode_curr);
1210 :     pmv[4].x = EVEN(pmv[4].x); pmv[4].y = EVEN(pmv[4].y);
1211 :     } else pmv[4].x = pmv[4].y = 0;
1212 : Isibaar 3
1213 : chl 530 if (x != 0) {
1214 :     pmv[5] = ChoosePred(pMB-1, mode_curr);
1215 :     pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
1216 :     } else pmv[5].x = pmv[5].y = 0;
1217 : Isibaar 3
1218 : chl 530 if ((x != 0)&&(y != 0)) {
1219 :     pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr);
1220 :     pmv[6].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
1221 :     } else pmv[6].x = pmv[6].y = 0;
1222 : edgomez 195
1223 : chl 530 // more?
1224 :     }
1225 : chl 181
1226 : edgomez 170
1227 : chl 530 /* search backward or forward, for b-frames */
1228 :     static void
1229 :     SearchBF( const uint8_t * const pRef,
1230 :     const uint8_t * const pRefH,
1231 :     const uint8_t * const pRefV,
1232 :     const uint8_t * const pRefHV,
1233 :     const IMAGE * const pCur,
1234 :     const int x, const int y,
1235 :     const uint32_t MotionFlags,
1236 :     const uint32_t iFcode,
1237 :     const MBParam * const pParam,
1238 :     MACROBLOCK * const pMB,
1239 :     const VECTOR * const predMV,
1240 :     int32_t * const best_sad,
1241 : h 545 const int32_t mode_current,
1242 :     SearchData * const Data)
1243 : chl 530 {
1244 : Isibaar 3
1245 : chl 530 const int32_t iEdgedWidth = pParam->edged_width;
1246 :    
1247 :     int i, iDirection, mask;
1248 : h 545 VECTOR pmv[7];
1249 : chl 530 MainSearchFunc *MainSearchPtr;
1250 : h 545 *Data->iMinSAD = MV_MAX_ERROR;
1251 :     Data->iFcode = iFcode;
1252 : syskin 663 Data->qpel_precision = 0;
1253 : Isibaar 3
1254 : h 545 Data->Ref = pRef + (x + y * iEdgedWidth) * 16;
1255 :     Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;
1256 :     Data->RefV = pRefV + (x + y * iEdgedWidth) * 16;
1257 :     Data->RefHV = pRefHV + (x + y * iEdgedWidth) * 16;
1258 : Isibaar 3
1259 : h 545 Data->predMV = *predMV;
1260 : edgomez 195
1261 : h 545 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1262 : Isibaar 580 pParam->width, pParam->height, iFcode, pParam->m_quarterpel);
1263 : Isibaar 3
1264 : h 545 pmv[0] = Data->predMV;
1265 : syskin 658 if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }
1266 : h 545 PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);
1267 : edgomez 195
1268 : h 545 Data->currentMV->x = Data->currentMV->y = 0;
1269 : chl 530 CheckCandidate = CheckCandidate16no4v;
1270 : chl 181
1271 : chl 530 // main loop. checking all predictions
1272 :     for (i = 0; i < 8; i++) {
1273 :     if (!(mask = make_mask(pmv, i)) ) continue;
1274 : h 545 CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1275 : edgomez 78 }
1276 : Isibaar 3
1277 : chl 530 if (MotionFlags & PMV_USESQUARES16)
1278 :     MainSearchPtr = SquareSearch;
1279 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1280 :     MainSearchPtr = AdvDiamondSearch;
1281 :     else MainSearchPtr = DiamondSearch;
1282 : chl 96
1283 : h 545 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1284 : chl 169
1285 : syskin 663 SubpelRefine(Data);
1286 : syskin 658
1287 :     if (Data->qpel) {
1288 :     Data->currentQMV->x = 2*Data->currentMV->x;
1289 :     Data->currentQMV->y = 2*Data->currentMV->y;
1290 : syskin 663 Data->qpel_precision = 1;
1291 : syskin 658 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1292 : syskin 663 pParam->width, pParam->height, iFcode, 0);
1293 :     SubpelRefine(Data);
1294 : syskin 658 }
1295 : chl 169
1296 : chl 530 // three bits are needed to code backward mode. four for forward
1297 :     // we treat the bits just like they were vector's
1298 : syskin 628 if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16;
1299 :     else *Data->iMinSAD += 3 * Data->lambda16;
1300 : chl 169
1301 : h 545 if (*Data->iMinSAD < *best_sad) {
1302 :     *best_sad = *Data->iMinSAD;
1303 : chl 530 pMB->mode = mode_current;
1304 : syskin 658 if (Data->qpel) {
1305 :     pMB->pmvs[0].x = Data->currentQMV->x - predMV->x;
1306 :     pMB->pmvs[0].y = Data->currentQMV->y - predMV->y;
1307 :     if (mode_current == MODE_FORWARD)
1308 :     pMB->qmvs[0] = *Data->currentQMV;
1309 :     else
1310 :     pMB->b_qmvs[0] = *Data->currentQMV;
1311 :     } else {
1312 :     pMB->pmvs[0].x = Data->currentMV->x - predMV->x;
1313 :     pMB->pmvs[0].y = Data->currentMV->y - predMV->y;
1314 :     }
1315 :     if (mode_current == MODE_FORWARD)
1316 :     pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV;
1317 :     else
1318 :     pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search
1319 :    
1320 : chl 169 }
1321 : chl 530
1322 :     }
1323 : chl 169
1324 : chl 530 static int32_t
1325 : Isibaar 539 SearchDirect(const IMAGE * const f_Ref,
1326 : chl 530 const uint8_t * const f_RefH,
1327 :     const uint8_t * const f_RefV,
1328 :     const uint8_t * const f_RefHV,
1329 : Isibaar 539 const IMAGE * const b_Ref,
1330 : chl 530 const uint8_t * const b_RefH,
1331 :     const uint8_t * const b_RefV,
1332 :     const uint8_t * const b_RefHV,
1333 :     const IMAGE * const pCur,
1334 :     const int x, const int y,
1335 :     const uint32_t MotionFlags,
1336 :     const int32_t TRB, const int32_t TRD,
1337 :     const MBParam * const pParam,
1338 :     MACROBLOCK * const pMB,
1339 :     const MACROBLOCK * const b_mb,
1340 : h 545 int32_t * const best_sad,
1341 :     SearchData * const Data)
1342 : edgomez 195
1343 : chl 530 {
1344 : h 545 int32_t skip_sad;
1345 : chl 530 int k;
1346 : h 545
1347 : chl 530 MainSearchFunc *MainSearchPtr;
1348 : Isibaar 3
1349 : h 545 *Data->iMinSAD = 256*4096;
1350 : chl 140
1351 : h 545 Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16;
1352 :     Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16;
1353 :     Data->RefV = f_RefV + (x + Data->iEdgedWidth*y) * 16;
1354 :     Data->RefHV = f_RefHV + (x + Data->iEdgedWidth*y) * 16;
1355 :     Data->bRef = b_Ref->y + (x + Data->iEdgedWidth*y) * 16;
1356 :     Data->bRefH = b_RefH + (x + Data->iEdgedWidth*y) * 16;
1357 :     Data->bRefV = b_RefV + (x + Data->iEdgedWidth*y) * 16;
1358 :     Data->bRefHV = b_RefHV + (x + Data->iEdgedWidth*y) * 16;
1359 : chl 140
1360 : h 545 Data->max_dx = 2 * pParam->width - 2 * (x) * 16;
1361 :     Data->max_dy = 2 * pParam->height - 2 * (y) * 16;
1362 :     Data->min_dx = -(2 * 16 + 2 * (x) * 16);
1363 :     Data->min_dy = -(2 * 16 + 2 * (y) * 16);
1364 : syskin 658 if (Data->qpel) { //we measure in qpixels
1365 :     Data->max_dx *= 2;
1366 :     Data->max_dy *= 2;
1367 :     Data->min_dx *= 2;
1368 :     Data->min_dy *= 2;
1369 :     Data->referencemv = b_mb->qmvs;
1370 :     } else Data->referencemv = b_mb->mvs;
1371 : syskin 672 Data->qpel_precision = 0; // it's a trick. it's 1 not 0, but we need 0 here
1372 : Isibaar 3
1373 : chl 530 for (k = 0; k < 4; k++) {
1374 : h 545 pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD);
1375 :     pMB->b_mvs[k].x = Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD;
1376 :     pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD);
1377 :     pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD;
1378 : Isibaar 3
1379 : h 545 if ( ( pMB->b_mvs[k].x > Data->max_dx ) || ( pMB->b_mvs[k].x < Data->min_dx )
1380 :     || ( pMB->b_mvs[k].y > Data->max_dy ) || ( pMB->b_mvs[k].y < Data->min_dy )) {
1381 : Isibaar 3
1382 : Isibaar 539 *best_sad = 256*4096; // in that case, we won't use direct mode
1383 :     pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV"
1384 :     pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;
1385 :     return 0;
1386 :     }
1387 :     if (b_mb->mode != MODE_INTER4V) {
1388 :     pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0];
1389 :     pMB->b_mvs[1] = pMB->b_mvs[2] = pMB->b_mvs[3] = pMB->b_mvs[0];
1390 : h 545 Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0];
1391 :     Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0];
1392 : Isibaar 539 break;
1393 :     }
1394 :     }
1395 : Isibaar 3
1396 : syskin 663
1397 :     if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect;
1398 :     else CheckCandidate = CheckCandidateDirectno4v;
1399 : syskin 658
1400 : h 545 (*CheckCandidate)(0, 0, 255, &k, Data);
1401 : Isibaar 3
1402 : chl 530 // skip decision
1403 : syskin 628 if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) {
1404 : syskin 594 //possible skip - checking chroma. everything copied from MC
1405 : Isibaar 539 //this is not full chroma compensation, only it's fullpel approximation. should work though
1406 :     int sum, dx, dy, b_dx, b_dy;
1407 : Isibaar 3
1408 : syskin 658 if (Data->qpel) {
1409 :     sum = pMB->mvs[0].y/2 + pMB->mvs[1].y/2 + pMB->mvs[2].y/2 + pMB->mvs[3].y/2;
1410 :     dy = (sum >> 3) + roundtab_76[sum & 0xf];
1411 :     sum = pMB->mvs[0].x/2 + pMB->mvs[1].x/2 + pMB->mvs[2].x/2 + pMB->mvs[3].x/2;
1412 :     dx = (sum >> 3) + roundtab_76[sum & 0xf];
1413 : Isibaar 539
1414 : syskin 658 sum = pMB->b_mvs[0].y/2 + pMB->b_mvs[1].y/2 + pMB->b_mvs[2].y/2 + pMB->b_mvs[3].y/2;
1415 :     b_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1416 :     sum = pMB->b_mvs[0].x/2 + pMB->b_mvs[1].x/2 + pMB->b_mvs[2].x/2 + pMB->b_mvs[3].x/2;
1417 :     b_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1418 : Isibaar 539
1419 : syskin 658 } else {
1420 :     sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1421 :     dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1422 :     sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1423 :     dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1424 : Isibaar 539
1425 : syskin 658 sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1426 :     b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1427 :     sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1428 :     b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1429 :     }
1430 : h 545 sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2),
1431 :     f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,
1432 :     b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,
1433 :     Data->iEdgedWidth/2);
1434 :     sum += sad8bi(pCur->v + 8*x + 8*y*(Data->iEdgedWidth/2),
1435 :     f_Ref->v + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,
1436 :     b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,
1437 :     Data->iEdgedWidth/2);
1438 : Isibaar 539
1439 : syskin 601 if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {
1440 : Isibaar 539 pMB->mode = MODE_DIRECT_NONE_MV;
1441 : h 545 return *Data->iMinSAD;
1442 : Isibaar 539 }
1443 :     }
1444 :    
1445 : h 545 skip_sad = *Data->iMinSAD;
1446 : edgomez 195
1447 : chl 530 // DIRECT MODE DELTA VECTOR SEARCH.
1448 :     // This has to be made more effective, but at the moment I'm happy it's running at all
1449 : Isibaar 3
1450 : chl 530 if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1451 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1452 :     else MainSearchPtr = DiamondSearch;
1453 : edgomez 195
1454 : h 545 (*MainSearchPtr)(0, 0, Data, 255);
1455 : Isibaar 3
1456 : syskin 663 SubpelRefine(Data);
1457 : Isibaar 3
1458 : syskin 628 *Data->iMinSAD += 1 * Data->lambda16; // one bit is needed to code direct mode
1459 : h 545 *best_sad = *Data->iMinSAD;
1460 : Isibaar 3
1461 : syskin 672 if (b_mb->mode == MODE_INTER4V)
1462 : chl 530 pMB->mode = MODE_DIRECT;
1463 : syskin 672 else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation
1464 : edgomez 195
1465 : h 545 pMB->pmvs[3] = *Data->currentMV;
1466 : edgomez 195
1467 : chl 530 for (k = 0; k < 4; k++) {
1468 : h 545 pMB->mvs[k].x = Data->directmvF[k].x + Data->currentMV->x;
1469 : syskin 658 pMB->b_mvs[k].x = ( (Data->currentMV->x == 0)
1470 : h 545 ? Data->directmvB[k].x
1471 : syskin 658 :pMB->mvs[k].x - Data->referencemv[k].x);
1472 : h 545 pMB->mvs[k].y = (Data->directmvF[k].y + Data->currentMV->y);
1473 :     pMB->b_mvs[k].y = ((Data->currentMV->y == 0)
1474 :     ? Data->directmvB[k].y
1475 :     : pMB->mvs[k].y - Data->referencemv[k].y);
1476 : syskin 658 if (Data->qpel) {
1477 :     pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2;
1478 :     pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2;
1479 :     pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2;
1480 :     pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2;
1481 :     }
1482 :    
1483 : chl 530 if (b_mb->mode != MODE_INTER4V) {
1484 :     pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];
1485 :     pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];
1486 : syskin 658 pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0];
1487 :     pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0];
1488 : chl 530 break;
1489 :     }
1490 :     }
1491 : h 545 return skip_sad;
1492 : Isibaar 3 }
1493 : chl 96
1494 : h 545
1495 : chl 530 static __inline void
1496 :     SearchInterpolate(const uint8_t * const f_Ref,
1497 :     const uint8_t * const f_RefH,
1498 :     const uint8_t * const f_RefV,
1499 :     const uint8_t * const f_RefHV,
1500 :     const uint8_t * const b_Ref,
1501 :     const uint8_t * const b_RefH,
1502 :     const uint8_t * const b_RefV,
1503 :     const uint8_t * const b_RefHV,
1504 :     const IMAGE * const pCur,
1505 :     const int x, const int y,
1506 :     const uint32_t fcode,
1507 :     const uint32_t bcode,
1508 :     const uint32_t MotionFlags,
1509 :     const MBParam * const pParam,
1510 :     const VECTOR * const f_predMV,
1511 :     const VECTOR * const b_predMV,
1512 :     MACROBLOCK * const pMB,
1513 : h 545 int32_t * const best_sad,
1514 :     SearchData * const fData)
1515 : chl 530
1516 : chl 96 {
1517 :    
1518 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1519 : chl 530 int iDirection, i, j;
1520 : h 545 SearchData bData;
1521 : chl 96
1522 : syskin 601 *(bData.iMinSAD = fData->iMinSAD) = 4096*256;
1523 : h 545 bData.Cur = fData->Cur;
1524 :     fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth;
1525 : syskin 658 bData.currentMV = fData->currentMV + 1; bData.currentQMV = fData->currentQMV + 1;
1526 : syskin 601 bData.lambda16 = fData->lambda16;
1527 : h 545 fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;
1528 : edgomez 195
1529 : h 545 bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16;
1530 :     bData.bRefH = fData->RefH = f_RefH + (x + y * iEdgedWidth) * 16;
1531 :     bData.bRefV = fData->RefV = f_RefV + (x + y * iEdgedWidth) * 16;
1532 :     bData.bRefHV = fData->RefHV = f_RefHV + (x + y * iEdgedWidth) * 16;
1533 :     bData.Ref = fData->bRef = b_Ref + (x + y * iEdgedWidth) * 16;
1534 :     bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16;
1535 :     bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;
1536 :     bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;
1537 : syskin 658 bData.RefQ = fData->RefQ;
1538 : syskin 675 fData->qpel_precision = bData.qpel_precision = 0; bData.qpel = fData->qpel;
1539 : syskin 672 bData.rounding = 0;
1540 : chl 96
1541 : h 545 bData.bpredMV = fData->predMV = *f_predMV;
1542 :     fData->bpredMV = bData.predMV = *b_predMV;
1543 : edgomez 195
1544 : syskin 661 fData->currentMV[0] = fData->currentMV[2];
1545 : Isibaar 580 get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel);
1546 :     get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel);
1547 : chl 96
1548 : h 545 if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;
1549 : syskin 675 if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dx;
1550 :     if (fData->currentMV[0].y > fData->max_dy) fData->currentMV[0].y = fData->max_dy;
1551 :     if (fData->currentMV[0].y < fData->min_dy) fData->currentMV[0].y = fData->min_dy;
1552 : Isibaar 539
1553 : h 545 if (fData->currentMV[1].x > bData.max_dx) fData->currentMV[1].x = bData.max_dx;
1554 : syskin 675 if (fData->currentMV[1].x < bData.min_dx) fData->currentMV[1].x = bData.min_dx;
1555 :     if (fData->currentMV[1].y > bData.max_dy) fData->currentMV[1].y = bData.max_dy;
1556 :     if (fData->currentMV[1].y < bData.min_dy) fData->currentMV[1].y = bData.min_dy;
1557 : Isibaar 539
1558 : h 545 CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);
1559 : chl 96
1560 : chl 530 //diamond. I wish we could use normal mainsearch functions (square, advdiamond)
1561 : chl 96
1562 : chl 530 do {
1563 :     iDirection = 255;
1564 :     // forward MV moves
1565 : h 545 i = fData->currentMV[0].x; j = fData->currentMV[0].y;
1566 : chl 96
1567 : h 545 CheckCandidateInt(i + 1, j, 0, &iDirection, fData);
1568 :     CheckCandidateInt(i, j + 1, 0, &iDirection, fData);
1569 :     CheckCandidateInt(i - 1, j, 0, &iDirection, fData);
1570 :     CheckCandidateInt(i, j - 1, 0, &iDirection, fData);
1571 : chl 96
1572 : chl 530 // backward MV moves
1573 : h 545 i = fData->currentMV[1].x; j = fData->currentMV[1].y;
1574 :     fData->currentMV[2] = fData->currentMV[0];
1575 : Isibaar 539 CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);
1576 :     CheckCandidateInt(i, j + 1, 0, &iDirection, &bData);
1577 :     CheckCandidateInt(i - 1, j, 0, &iDirection, &bData);
1578 :     CheckCandidateInt(i, j - 1, 0, &iDirection, &bData);
1579 : chl 96
1580 : chl 530 } while (!(iDirection));
1581 : edgomez 195
1582 : syskin 628 *fData->iMinSAD += 2 * fData->lambda16; // two bits are needed to code interpolate mode.
1583 : Isibaar 606
1584 : syskin 658 if (fData->qpel) {
1585 : syskin 675 CheckCandidate = CheckCandidateInt;
1586 : syskin 663 fData->qpel_precision = bData.qpel_precision = 1;
1587 : syskin 658 get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 0);
1588 :     get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 0);
1589 :     fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x;
1590 :     fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y;
1591 :     fData->currentQMV[1].x = 2 * fData->currentMV[1].x;
1592 :     fData->currentQMV[1].y = 2 * fData->currentMV[1].y;
1593 : syskin 663 SubpelRefine(fData);
1594 : syskin 658 fData->currentQMV[2] = fData->currentQMV[0];
1595 : syskin 663 SubpelRefine(&bData);
1596 : syskin 658 }
1597 :    
1598 : h 545 if (*fData->iMinSAD < *best_sad) {
1599 :     *best_sad = *fData->iMinSAD;
1600 :     pMB->mvs[0] = fData->currentMV[0];
1601 :     pMB->b_mvs[0] = fData->currentMV[1];
1602 : chl 530 pMB->mode = MODE_INTERPOLATE;
1603 : syskin 658 if (fData->qpel) {
1604 :     pMB->qmvs[0] = fData->currentQMV[0];
1605 :     pMB->b_qmvs[0] = fData->currentQMV[1];
1606 :     pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x;
1607 :     pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y;
1608 :     pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x;
1609 :     pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y;
1610 :     } else {
1611 :     pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;
1612 :     pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;
1613 :     pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;
1614 :     pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;
1615 :     }
1616 : chl 530 }
1617 :     }
1618 : chl 96
1619 : chl 530 void
1620 :     MotionEstimationBVOP(MBParam * const pParam,
1621 :     FRAMEINFO * const frame,
1622 :     const int32_t time_bp,
1623 :     const int32_t time_pp,
1624 :     // forward (past) reference
1625 :     const MACROBLOCK * const f_mbs,
1626 :     const IMAGE * const f_ref,
1627 :     const IMAGE * const f_refH,
1628 :     const IMAGE * const f_refV,
1629 :     const IMAGE * const f_refHV,
1630 :     // backward (future) reference
1631 : syskin 644 const FRAMEINFO * const b_reference,
1632 : chl 530 const IMAGE * const b_ref,
1633 :     const IMAGE * const b_refH,
1634 :     const IMAGE * const b_refV,
1635 :     const IMAGE * const b_refHV)
1636 :     {
1637 :     uint32_t i, j;
1638 :     int32_t best_sad, skip_sad;
1639 :     int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0;
1640 :     static const VECTOR zeroMV={0,0};
1641 : syskin 644 const MACROBLOCK * const b_mbs = b_reference->mbs;
1642 : chl 96
1643 : chl 530 VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/
1644 : chl 96
1645 : chl 530 const int32_t TRB = time_pp - time_bp;
1646 :     const int32_t TRD = time_pp;
1647 : syskin 658 uint8_t * qimage;
1648 : chl 96
1649 : h 545 // some pre-inintialized data for the rest of the search
1650 :    
1651 :     SearchData Data;
1652 :     int32_t iMinSAD;
1653 :     VECTOR currentMV[3];
1654 : syskin 658 VECTOR currentQMV[3];
1655 : h 545 Data.iEdgedWidth = pParam->edged_width;
1656 : syskin 658 Data.currentMV = currentMV; Data.currentQMV = currentQMV;
1657 : h 545 Data.iMinSAD = &iMinSAD;
1658 : syskin 601 Data.lambda16 = lambda_vec16[frame->quant];
1659 : syskin 658 Data.qpel = pParam->m_quarterpel;
1660 : syskin 663 Data.rounding = 0;
1661 : h 545
1662 : syskin 658 if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
1663 :     return; // allocate some mem for qpel interpolated blocks
1664 :     // somehow this is dirty since I think we shouldn't use malloc outside
1665 :     // encoder_create() - so please fix me!
1666 :     Data.RefQ = qimage;
1667 :    
1668 : chl 530 // note: i==horizontal, j==vertical
1669 :     for (j = 0; j < pParam->mb_height; j++) {
1670 : chl 96
1671 : chl 530 f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */
1672 : edgomez 195
1673 : chl 530 for (i = 0; i < pParam->mb_width; i++) {
1674 :     MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
1675 :     const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
1676 : edgomez 195
1677 : chl 619 /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
1678 : syskin 644 if (b_reference->coding_type != S_VOP)
1679 :     if (b_mb->mode == MODE_NOT_CODED) {
1680 :     pMB->mode = MODE_NOT_CODED;
1681 :     continue;
1682 :     }
1683 : chl 96
1684 : h 545 Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
1685 : syskin 601 pMB->quant = frame->quant;
1686 : syskin 628
1687 : chl 530 /* direct search comes first, because it (1) checks for SKIP-mode
1688 :     and (2) sets very good predictions for forward and backward search */
1689 : Isibaar 539 skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y,
1690 :     b_ref, b_refH->y, b_refV->y, b_refHV->y,
1691 : chl 530 &frame->image,
1692 :     i, j,
1693 :     frame->motion_flags,
1694 :     TRB, TRD,
1695 :     pParam,
1696 :     pMB, b_mb,
1697 : h 545 &best_sad,
1698 :     &Data);
1699 : chl 96
1700 : Isibaar 539 if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }
1701 : syskin 658
1702 : chl 530 // forward search
1703 :     SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
1704 :     &frame->image, i, j,
1705 :     frame->motion_flags,
1706 : h 545 frame->fcode, pParam,
1707 : chl 530 pMB, &f_predMV, &best_sad,
1708 : h 545 MODE_FORWARD, &Data);
1709 : chl 96
1710 : chl 530 // backward search
1711 :     SearchBF(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
1712 :     &frame->image, i, j,
1713 :     frame->motion_flags,
1714 : h 545 frame->bcode, pParam,
1715 : chl 530 pMB, &b_predMV, &best_sad,
1716 : h 545 MODE_BACKWARD, &Data);
1717 : chl 96
1718 : chl 530 // interpolate search comes last, because it uses data from forward and backward as prediction
1719 : syskin 662
1720 : chl 530 SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
1721 :     b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
1722 :     &frame->image,
1723 :     i, j,
1724 :     frame->fcode, frame->bcode,
1725 :     frame->motion_flags,
1726 : h 545 pParam,
1727 : chl 530 &f_predMV, &b_predMV,
1728 : h 545 pMB, &best_sad,
1729 :     &Data);
1730 : syskin 662
1731 : chl 530 switch (pMB->mode) {
1732 :     case MODE_FORWARD:
1733 :     f_count++;
1734 : syskin 658 if (pParam->m_quarterpel) f_predMV = pMB->qmvs[0];
1735 :     else f_predMV = pMB->mvs[0];
1736 : chl 530 break;
1737 :     case MODE_BACKWARD:
1738 :     b_count++;
1739 : syskin 658 if (pParam->m_quarterpel) b_predMV = pMB->b_qmvs[0];
1740 :     else b_predMV = pMB->b_mvs[0];
1741 : chl 530 break;
1742 :     case MODE_INTERPOLATE:
1743 :     i_count++;
1744 : syskin 658 if (pParam->m_quarterpel) {
1745 :     f_predMV = pMB->qmvs[0];
1746 :     b_predMV = pMB->b_qmvs[0];
1747 :     } else {
1748 :     f_predMV = pMB->mvs[0];
1749 :     b_predMV = pMB->b_mvs[0];
1750 :     }
1751 : chl 530 break;
1752 :     case MODE_DIRECT:
1753 :     case MODE_DIRECT_NO4V:
1754 :     d_count++;
1755 :     break;
1756 :     default:
1757 :     break;
1758 : chl 96 }
1759 :     }
1760 :     }
1761 : syskin 658 free(qimage);
1762 : chl 96 }
1763 :    
1764 : chl 530 /* Hinted ME starts here */
1765 : chl 96
1766 : syskin 594 static void
1767 : syskin 628 SearchPhinted ( const IMAGE * const pRef,
1768 : chl 289 const uint8_t * const pRefH,
1769 :     const uint8_t * const pRefV,
1770 :     const uint8_t * const pRefHV,
1771 :     const IMAGE * const pCur,
1772 :     const int x,
1773 :     const int y,
1774 :     const uint32_t MotionFlags,
1775 :     const uint32_t iQuant,
1776 :     const MBParam * const pParam,
1777 :     const MACROBLOCK * const pMBs,
1778 : chl 530 int inter4v,
1779 : h 545 MACROBLOCK * const pMB,
1780 : edgomez 568 SearchData * const Data)
1781 : chl 289 {
1782 : chl 530
1783 : edgomez 568 int i, t;
1784 : chl 530 MainSearchFunc * MainSearchPtr;
1785 : suxen_drol 118
1786 : h 545 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1787 : Isibaar 580 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
1788 : suxen_drol 118
1789 : syskin 628 Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
1790 :     Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1791 :     Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1792 : Isibaar 600
1793 : syskin 628 Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;
1794 :     Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;
1795 :     Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;
1796 :     Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
1797 :     Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1798 :     Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1799 : syskin 663 Data->qpel_precision = 0;
1800 : syskin 628
1801 : chl 530 if (!(MotionFlags & PMV_HALFPEL16)) {
1802 : h 545 Data->min_dx = EVEN(Data->min_dx);
1803 :     Data->max_dx = EVEN(Data->max_dx);
1804 :     Data->min_dy = EVEN(Data->min_dy);
1805 :     Data->max_dy = EVEN(Data->max_dy);
1806 : chl 530 }
1807 : syskin 663 if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1808 :     else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1809 : chl 289
1810 : h 545 for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
1811 : chl 289
1812 : chl 530 if (pMB->dquant != NO_CHANGE) inter4v = 0;
1813 : chl 289
1814 : syskin 663 if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16;
1815 : syskin 628 else CheckCandidate = CheckCandidate16no4v;
1816 : chl 289
1817 : chl 530 pMB->mvs[0].x = EVEN(pMB->mvs[0].x);
1818 :     pMB->mvs[0].y = EVEN(pMB->mvs[0].y);
1819 : h 545 if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed
1820 :     if (pMB->mvs[0].x < Data->min_dx) pMB->mvs[0].x = Data->min_dx;
1821 :     if (pMB->mvs[0].y > Data->max_dy) pMB->mvs[0].y = Data->max_dy;
1822 :     if (pMB->mvs[0].y < Data->min_dy) pMB->mvs[0].y = Data->min_dy;
1823 : chl 370
1824 : edgomez 568 (*CheckCandidate)(pMB->mvs[0].x, pMB->mvs[0].y, 0, &t, Data);
1825 : chl 289
1826 : chl 530 if (pMB->mode == MODE_INTER4V)
1827 :     for (i = 1; i < 4; i++) { // all four vectors will be used as four predictions for 16x16 search
1828 :     pMB->mvs[i].x = EVEN(pMB->mvs[i].x);
1829 :     pMB->mvs[i].y = EVEN(pMB->mvs[i].y);
1830 :     if (!(make_mask(pMB->mvs, i)))
1831 : edgomez 568 (*CheckCandidate)(pMB->mvs[i].x, pMB->mvs[i].y, 0, &t, Data);
1832 : chl 289 }
1833 :    
1834 :     if (MotionFlags & PMV_USESQUARES16)
1835 : chl 530 MainSearchPtr = SquareSearch;
1836 : chl 289 else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1837 : chl 530 MainSearchPtr = AdvDiamondSearch;
1838 :     else MainSearchPtr = DiamondSearch;
1839 : chl 289
1840 : h 545 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1841 : chl 289
1842 : syskin 663 if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data);
1843 : chl 289
1844 : syskin 594 for(i = 0; i < 5; i++) {
1845 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1846 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1847 :     }
1848 : chl 289
1849 : syskin 594 if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1850 : Isibaar 606 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1851 :     pParam->width, pParam->height, Data->iFcode, 0);
1852 : syskin 663 Data->qpel_precision = 1;
1853 :     SubpelRefine(Data);
1854 : syskin 594 }
1855 :    
1856 :     if (inter4v) {
1857 :     SearchData Data8;
1858 :     Data8.iFcode = Data->iFcode;
1859 : Isibaar 606 Data8.lambda8 = Data->lambda8;
1860 : syskin 594 Data8.iEdgedWidth = Data->iEdgedWidth;
1861 : Isibaar 606 Data8.RefQ = Data->RefQ;
1862 : syskin 628 Data8.qpel = Data->qpel;
1863 :     Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1864 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
1865 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1866 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1867 :    
1868 :     if (Data->chroma) {
1869 : syskin 663 int sumx, sumy, dx, dy;
1870 : syskin 628
1871 : syskin 663 if(pParam->m_quarterpel) {
1872 :     sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;
1873 :     sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;
1874 :     } else {
1875 :     sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1876 :     sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1877 :     }
1878 :     dx = (sumx >> 3) + roundtab_76[sumx & 0xf];
1879 :     dy = (sumy >> 3) + roundtab_76[sumy & 0xf];
1880 :    
1881 : syskin 628 Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
1882 :     }
1883 : syskin 594 }
1884 :    
1885 : chl 530 if (!(inter4v) ||
1886 : h 545 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] +
1887 :     Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {
1888 : chl 530 // INTER MODE
1889 :     pMB->mode = MODE_INTER;
1890 : Isibaar 539 pMB->mvs[0] = pMB->mvs[1]
1891 : h 545 = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1892 : chl 289
1893 : Isibaar 606 pMB->qmvs[0] = pMB->qmvs[1]
1894 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1895 :    
1896 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1897 : h 545 pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
1898 : chl 289
1899 : Isibaar 606 if(pParam->m_quarterpel) {
1900 : syskin 663 pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
1901 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
1902 : syskin 628 } else {
1903 : Isibaar 606 pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1904 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1905 :     }
1906 : chl 530 } else {
1907 : syskin 628 // INTER4V MODE; all other things are already set in Search8
1908 : chl 530 pMB->mode = MODE_INTER4V;
1909 : h 545 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3]
1910 :     + Data->iMinSAD[4] + IMV16X16 * iQuant;
1911 : chl 289 }
1912 :    
1913 :     }
1914 :    
1915 : edgomez 195 void
1916 : chl 530 MotionEstimationHinted( MBParam * const pParam,
1917 :     FRAMEINFO * const current,
1918 :     FRAMEINFO * const reference,
1919 :     const IMAGE * const pRefH,
1920 :     const IMAGE * const pRefV,
1921 :     const IMAGE * const pRefHV)
1922 : suxen_drol 118 {
1923 : chl 530 MACROBLOCK *const pMBs = current->mbs;
1924 :     const IMAGE *const pCurrent = &current->image;
1925 :     const IMAGE *const pRef = &reference->image;
1926 : suxen_drol 118
1927 : chl 530 uint32_t x, y;
1928 : syskin 601 uint8_t * qimage;
1929 : syskin 574 int32_t temp[5], quant = current->quant;
1930 : h 545 int32_t iMinSAD[5];
1931 : Isibaar 606 VECTOR currentMV[5], currentQMV[5];
1932 : h 545 SearchData Data;
1933 :     Data.iEdgedWidth = pParam->edged_width;
1934 :     Data.currentMV = currentMV;
1935 : Isibaar 606 Data.currentQMV = currentQMV;
1936 : h 545 Data.iMinSAD = iMinSAD;
1937 :     Data.temp = temp;
1938 :     Data.iFcode = current->fcode;
1939 : syskin 594 Data.rounding = pParam->m_rounding_type;
1940 : syskin 628 Data.qpel = pParam->m_quarterpel;
1941 :     Data.chroma = current->global_flags & XVID_ME_COLOUR;
1942 : h 545
1943 : syskin 594 if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
1944 :     return; // allocate some mem for qpel interpolated blocks
1945 :     // somehow this is dirty since I think we shouldn't use malloc outside
1946 :     // encoder_create() - so please fix me!
1947 :    
1948 : syskin 601 Data.RefQ = qimage;
1949 :    
1950 : chl 530 if (sadInit) (*sadInit) ();
1951 : suxen_drol 118
1952 : chl 530 for (y = 0; y < pParam->mb_height; y++) {
1953 :     for (x = 0; x < pParam->mb_width; x++) {
1954 : chl 312
1955 : chl 530 MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
1956 : suxen_drol 118
1957 : chl 530 //intra mode is copied from the first pass. At least for the time being
1958 :     if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue;
1959 : chl 346
1960 : chl 530 if (!(current->global_flags & XVID_LUMIMASKING)) {
1961 :     pMB->dquant = NO_CHANGE;
1962 :     pMB->quant = current->quant; }
1963 : Isibaar 606 else {
1964 : syskin 574 if (pMB->dquant != NO_CHANGE) {
1965 :     quant += DQtab[pMB->dquant];
1966 :     if (quant > 31) quant = 31;
1967 :     else if (quant < 1) quant = 1;
1968 :     }
1969 : Isibaar 606 pMB->quant = quant;
1970 :     }
1971 : chl 341
1972 : syskin 628 SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1973 : Isibaar 539 y, current->motion_flags, pMB->quant,
1974 : h 545 pParam, pMBs, current->global_flags & XVID_INTER4V, pMB,
1975 :     &Data);
1976 : chl 317
1977 : Isibaar 539 }
1978 :     }
1979 : syskin 594 free(qimage);
1980 : Isibaar 539 }
1981 : suxen_drol 118
1982 : Isibaar 539 static __inline int
1983 :     MEanalyzeMB ( const uint8_t * const pRef,
1984 :     const uint8_t * const pCur,
1985 :     const int x,
1986 :     const int y,
1987 :     const MBParam * const pParam,
1988 :     const MACROBLOCK * const pMBs,
1989 : h 545 MACROBLOCK * const pMB,
1990 :     SearchData * const Data)
1991 : Isibaar 539 {
1992 : chl 317
1993 : Isibaar 606 int i = 255, mask;
1994 : h 545 VECTOR pmv[3];
1995 : syskin 640 *(Data->iMinSAD) = MV_MAX_ERROR;
1996 : suxen_drol 118
1997 : syskin 640 //median is only used as prediction. it doesn't have to be real
1998 :     if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;
1999 :     else
2000 :     if (x == 1) //left macroblock does not have any vector now
2001 :     Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median
2002 :     else if (y == 1) // top macroblock don't have it's vector
2003 :     Data->predMV = (pMB - 1)->mvs[0]; // left instead of median
2004 :     else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median
2005 :    
2006 : h 545 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2007 : Isibaar 580 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
2008 : chl 326
2009 : h 545 Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2010 :     Data->Ref = pRef + (x + y * pParam->edged_width) * 16;
2011 : Isibaar 539
2012 :     pmv[1].x = EVEN(pMB->mvs[0].x);
2013 :     pmv[1].y = EVEN(pMB->mvs[0].y);
2014 : syskin 640 pmv[2].x = EVEN(Data->predMV.x);
2015 :     pmv[2].y = EVEN(Data->predMV.y);
2016 :     pmv[0].x = pmv[0].y = 0;
2017 : chl 326
2018 : syskin 640 (*CheckCandidate)(0, 0, 255, &i, Data);
2019 :    
2020 :     //early skip for 0,0
2021 :     if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) {
2022 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
2023 :     pMB->mode = MODE_NOT_CODED;
2024 :     return 0;
2025 :     }
2026 :    
2027 : Isibaar 539 if (!(mask = make_mask(pmv, 1)))
2028 : syskin 640 (*CheckCandidate)(pmv[1].x, pmv[1].y, mask, &i, Data);
2029 : Isibaar 539 if (!(mask = make_mask(pmv, 2)))
2030 : syskin 640 (*CheckCandidate)(pmv[2].x, pmv[2].y, mask, &i, Data);
2031 : Isibaar 539
2032 : syskin 640 if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 4) // diamond only if needed
2033 :     DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
2034 : Isibaar 539
2035 : syskin 640 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
2036 : syskin 628 pMB->mode = MODE_INTER;
2037 : h 545 return *(Data->iMinSAD);
2038 : Isibaar 539 }
2039 :    
2040 :     #define INTRA_THRESH 1350
2041 : syskin 672 #define INTER_THRESH 1200
2042 : Isibaar 539
2043 : syskin 644
2044 : Isibaar 539 int
2045 :     MEanalysis( const IMAGE * const pRef,
2046 : syskin 644 FRAMEINFO * const Current,
2047 : Isibaar 539 MBParam * const pParam,
2048 : syskin 644 int maxIntra, //maximum number if non-I frames
2049 :     int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame
2050 :     int bCount) // number if B frames in a row
2051 : Isibaar 539 {
2052 :     uint32_t x, y, intra = 0;
2053 :     int sSAD = 0;
2054 : syskin 644 MACROBLOCK * const pMBs = Current->mbs;
2055 :     const IMAGE * const pCurrent = &Current->image;
2056 :     int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH;
2057 : h 545
2058 :     VECTOR currentMV;
2059 :     int32_t iMinSAD;
2060 :     SearchData Data;
2061 :     Data.iEdgedWidth = pParam->edged_width;
2062 :     Data.currentMV = &currentMV;
2063 :     Data.iMinSAD = &iMinSAD;
2064 : syskin 644 Data.iFcode = Current->fcode;
2065 : syskin 640 CheckCandidate = CheckCandidate16no4vI;
2066 : h 545
2067 : syskin 658 if (intraCount < 10) // we're right after an I frame
2068 :     IntraThresh += 4 * (intraCount - 10) * (intraCount - 10);
2069 : syskin 644 else
2070 :     if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec
2071 :     IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;
2072 :    
2073 :    
2074 : syskin 672 InterThresh += 400 * (1 - bCount);
2075 : syskin 644 if (InterThresh < 200) InterThresh = 200;
2076 :    
2077 : Isibaar 539 if (sadInit) (*sadInit) ();
2078 :    
2079 : syskin 601 for (y = 1; y < pParam->mb_height-1; y++) {
2080 :     for (x = 1; x < pParam->mb_width-1; x++) {
2081 : Isibaar 539 int sad, dev;
2082 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
2083 :    
2084 :     sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y,
2085 : h 545 pParam, pMBs, pMB, &Data);
2086 : Isibaar 539
2087 : syskin 644 if (sad > IntraThresh) {
2088 : syskin 601 dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,
2089 :     pParam->edged_width);
2090 : syskin 644 if (dev + IntraThresh < sad) {
2091 :     pMB->mode = MODE_INTRA;
2092 :     if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame
2093 : Isibaar 539 }
2094 : syskin 644 }
2095 : syskin 601 sSAD += sad;
2096 : suxen_drol 118 }
2097 :     }
2098 : Isibaar 539 sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);
2099 : syskin 644 if (sSAD > InterThresh ) return 1; //P frame
2100 : Isibaar 539 emms();
2101 :     return 0; // B frame
2102 :    
2103 : suxen_drol 118 }
2104 : syskin 576
2105 :     int
2106 :     FindFcode( const MBParam * const pParam,
2107 :     const FRAMEINFO * const current)
2108 :     {
2109 :     uint32_t x, y;
2110 :     int max = 0, min = 0, i;
2111 :    
2112 :     for (y = 0; y < pParam->mb_height; y++) {
2113 :     for (x = 0; x < pParam->mb_width; x++) {
2114 :    
2115 :     MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
2116 :     for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4:1); i++) {
2117 :     if (pMB->mvs[i].x > max) max = pMB->mvs[i].x;
2118 :     if (pMB->mvs[i].y > max) max = pMB->mvs[i].y;
2119 :    
2120 :     if (pMB->mvs[i].x < min) min = pMB->mvs[i].x;
2121 :     if (pMB->mvs[i].y < min) min = pMB->mvs[i].y;
2122 :     }
2123 :     }
2124 :     }
2125 :    
2126 :     min = -min;
2127 :     max += 1;
2128 :     if (min > max) max = min;
2129 : Isibaar 606 if (pParam->m_quarterpel) max *= 2;
2130 : syskin 576
2131 :     for (i = 1; (max > 32 << (i - 1)); i++);
2132 :     return i;
2133 :     }
2134 : chl 619
2135 : syskin 628 static void
2136 :     CheckGMC(int x, int y, const int dir, int * iDirection,
2137 :     const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC,
2138 :     const MBParam * const pParam)
2139 :     {
2140 :     uint32_t mx, my, a, count = 0;
2141 : chl 619
2142 : syskin 628 for (my = 1; my < pParam->mb_height-1; my++)
2143 :     for (mx = 1; mx < pParam->mb_width-1; mx++) {
2144 :     VECTOR mv;
2145 :     const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];
2146 :     if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue;
2147 :     mv = pMB->mvs[0];
2148 :     a = ABS(mv.x - x) + ABS(mv.y - y);
2149 :     if (a < 6) count += 6 - a;
2150 :     }
2151 : chl 619
2152 : syskin 628 if (count > *bestcount) {
2153 :     *bestcount = count;
2154 :     *iDirection = dir;
2155 :     GMC->x = x; GMC->y = y;
2156 :     }
2157 :     }
2158 :    
2159 :    
2160 : chl 619 static VECTOR
2161 :     GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode)
2162 :     {
2163 :    
2164 : syskin 628 uint32_t count, bestcount = 0;
2165 : chl 619 int x, y;
2166 : syskin 628 VECTOR gmc = {0,0};
2167 : chl 619 int step, min_x, max_x, min_y, max_y;
2168 :     uint32_t mx, my;
2169 : syskin 628 int iDirection, bDirection;
2170 : chl 619
2171 :     min_x = min_y = -32<<iFcode;
2172 :     max_x = max_y = 32<<iFcode;
2173 :    
2174 : syskin 628 //step1: let's find a rough camera panning
2175 : chl 619 for (step = 32; step >= 2; step /= 2) {
2176 :     bestcount = 0;
2177 :     for (y = min_y; y <= max_y; y += step)
2178 :     for (x = min_x ; x <= max_x; x += step) {
2179 :     count = 0;
2180 :     //for all macroblocks
2181 :     for (my = 1; my < pParam->mb_height-1; my++)
2182 :     for (mx = 1; mx < pParam->mb_width-1; mx++) {
2183 :     const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];
2184 :     VECTOR mv;
2185 :    
2186 :     if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)
2187 :     continue;
2188 :    
2189 :     mv = pMB->mvs[0];
2190 :     if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step ) /* GMC translation is always halfpel-res */
2191 :     count++;
2192 :     }
2193 :     if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; }
2194 :     }
2195 :     min_x = gmc.x - step;
2196 :     max_x = gmc.x + step;
2197 :     min_y = gmc.y - step;
2198 :     max_y = gmc.y + step;
2199 :    
2200 :     }
2201 : syskin 628
2202 :     if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10)
2203 :     gmc.x = gmc.y = 0; //no camara pan, no GMC
2204 :    
2205 :     // step2: let's refine camera panning using gradiend-descent approach.
2206 :     // TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond)
2207 :     bestcount = 0;
2208 :     CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam);
2209 :     do {
2210 :     x = gmc.x; y = gmc.y;
2211 :     bDirection = iDirection; iDirection = 0;
2212 :     if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2213 :     if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2214 :     if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam);
2215 :     if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2216 :    
2217 :     } while (iDirection);
2218 :    
2219 : chl 619 if (pParam->m_quarterpel) {
2220 :     gmc.x *= 2;
2221 :     gmc.y *= 2; /* we store the halfpel value as pseudo-qpel to make comparison easier */
2222 :     }
2223 :    
2224 :     return gmc;
2225 :     }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4