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