Mercurial > libavcodec.hg
annotate motion_est.c @ 277:5cb2978e701f libavcodec
new motion estimation (epzs) not complete yet but allready pretty good :)
unlimited mv search range
minor bugfix in the mpeg4 header parser
reset picture in gop counter if scene change is detected
author | michaelni |
---|---|
date | Fri, 22 Mar 2002 02:21:17 +0000 |
parents | 7ebb3f9aaf3b |
children | 3dc1ca4ba717 |
rev | line source |
---|---|
0 | 1 /* |
2 * Motion estimation | |
3 * Copyright (c) 2000,2001 Gerard Lantau. | |
4 * | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
19 * |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
20 * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at> |
0 | 21 */ |
22 #include <stdlib.h> | |
23 #include <stdio.h> | |
24 #include "avcodec.h" | |
25 #include "dsputil.h" | |
26 #include "mpegvideo.h" | |
27 | |
28 static void halfpel_motion_search(MpegEncContext * s, | |
29 int *mx_ptr, int *my_ptr, int dmin, | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
30 int xmin, int ymin, int xmax, int ymax, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
31 int pred_x, int pred_y); |
0 | 32 |
33 /* config it to test motion vector encoding (send random vectors) */ | |
34 //#define CONFIG_TEST_MV_ENCODE | |
35 | |
36 static int pix_sum(UINT8 * pix, int line_size) | |
37 { | |
38 int s, i, j; | |
39 | |
40 s = 0; | |
41 for (i = 0; i < 16; i++) { | |
42 for (j = 0; j < 16; j += 8) { | |
43 s += pix[0]; | |
44 s += pix[1]; | |
45 s += pix[2]; | |
46 s += pix[3]; | |
47 s += pix[4]; | |
48 s += pix[5]; | |
49 s += pix[6]; | |
50 s += pix[7]; | |
51 pix += 8; | |
52 } | |
53 pix += line_size - 16; | |
54 } | |
55 return s; | |
56 } | |
57 | |
58 static int pix_norm1(UINT8 * pix, int line_size) | |
59 { | |
60 int s, i, j; | |
61 UINT32 *sq = squareTbl + 256; | |
62 | |
63 s = 0; | |
64 for (i = 0; i < 16; i++) { | |
65 for (j = 0; j < 16; j += 8) { | |
66 s += sq[pix[0]]; | |
67 s += sq[pix[1]]; | |
68 s += sq[pix[2]]; | |
69 s += sq[pix[3]]; | |
70 s += sq[pix[4]]; | |
71 s += sq[pix[5]]; | |
72 s += sq[pix[6]]; | |
73 s += sq[pix[7]]; | |
74 pix += 8; | |
75 } | |
76 pix += line_size - 16; | |
77 } | |
78 return s; | |
79 } | |
80 | |
81 static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size) | |
82 { | |
83 int s, i, j; | |
84 UINT32 *sq = squareTbl + 256; | |
85 | |
86 s = 0; | |
87 for (i = 0; i < 16; i++) { | |
88 for (j = 0; j < 16; j += 8) { | |
89 s += sq[pix1[0] - pix2[0]]; | |
90 s += sq[pix1[1] - pix2[1]]; | |
91 s += sq[pix1[2] - pix2[2]]; | |
92 s += sq[pix1[3] - pix2[3]]; | |
93 s += sq[pix1[4] - pix2[4]]; | |
94 s += sq[pix1[5] - pix2[5]]; | |
95 s += sq[pix1[6] - pix2[6]]; | |
96 s += sq[pix1[7] - pix2[7]]; | |
97 pix1 += 8; | |
98 pix2 += 8; | |
99 } | |
100 pix1 += line_size - 16; | |
101 pix2 += line_size - 16; | |
102 } | |
103 return s; | |
104 } | |
105 | |
106 static void no_motion_search(MpegEncContext * s, | |
107 int *mx_ptr, int *my_ptr) | |
108 { | |
109 *mx_ptr = 16 * s->mb_x; | |
110 *my_ptr = 16 * s->mb_y; | |
111 } | |
112 | |
113 static int full_motion_search(MpegEncContext * s, | |
114 int *mx_ptr, int *my_ptr, int range, | |
115 int xmin, int ymin, int xmax, int ymax) | |
116 { | |
117 int x1, y1, x2, y2, xx, yy, x, y; | |
118 int mx, my, dmin, d; | |
119 UINT8 *pix; | |
120 | |
121 xx = 16 * s->mb_x; | |
122 yy = 16 * s->mb_y; | |
123 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */ | |
124 if (x1 < xmin) | |
125 x1 = xmin; | |
126 x2 = xx + range - 1; | |
127 if (x2 > xmax) | |
128 x2 = xmax; | |
129 y1 = yy - range + 1; | |
130 if (y1 < ymin) | |
131 y1 = ymin; | |
132 y2 = yy + range - 1; | |
133 if (y2 > ymax) | |
134 y2 = ymax; | |
135 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
136 dmin = 0x7fffffff; | |
137 mx = 0; | |
138 my = 0; | |
139 for (y = y1; y <= y2; y++) { | |
140 for (x = x1; x <= x2; x++) { | |
141 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, | |
142 s->linesize, 16); | |
143 if (d < dmin || | |
144 (d == dmin && | |
145 (abs(x - xx) + abs(y - yy)) < | |
146 (abs(mx - xx) + abs(my - yy)))) { | |
147 dmin = d; | |
148 mx = x; | |
149 my = y; | |
150 } | |
151 } | |
152 } | |
153 | |
154 *mx_ptr = mx; | |
155 *my_ptr = my; | |
156 | |
157 #if 0 | |
158 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) || | |
159 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) { | |
160 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr); | |
161 } | |
162 #endif | |
163 return dmin; | |
164 } | |
165 | |
166 | |
167 static int log_motion_search(MpegEncContext * s, | |
168 int *mx_ptr, int *my_ptr, int range, | |
169 int xmin, int ymin, int xmax, int ymax) | |
170 { | |
171 int x1, y1, x2, y2, xx, yy, x, y; | |
172 int mx, my, dmin, d; | |
173 UINT8 *pix; | |
174 | |
175 xx = s->mb_x << 4; | |
176 yy = s->mb_y << 4; | |
177 | |
178 /* Left limit */ | |
179 x1 = xx - range; | |
180 if (x1 < xmin) | |
181 x1 = xmin; | |
182 | |
183 /* Right limit */ | |
184 x2 = xx + range; | |
185 if (x2 > xmax) | |
186 x2 = xmax; | |
187 | |
188 /* Upper limit */ | |
189 y1 = yy - range; | |
190 if (y1 < ymin) | |
191 y1 = ymin; | |
192 | |
193 /* Lower limit */ | |
194 y2 = yy + range; | |
195 if (y2 > ymax) | |
196 y2 = ymax; | |
197 | |
198 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
199 dmin = 0x7fffffff; | |
200 mx = 0; | |
201 my = 0; | |
202 | |
203 do { | |
204 for (y = y1; y <= y2; y += range) { | |
205 for (x = x1; x <= x2; x += range) { | |
206 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize, 16); | |
207 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { | |
208 dmin = d; | |
209 mx = x; | |
210 my = y; | |
211 } | |
212 } | |
213 } | |
214 | |
215 range = range >> 1; | |
216 | |
217 x1 = mx - range; | |
218 if (x1 < xmin) | |
219 x1 = xmin; | |
220 | |
221 x2 = mx + range; | |
222 if (x2 > xmax) | |
223 x2 = xmax; | |
224 | |
225 y1 = my - range; | |
226 if (y1 < ymin) | |
227 y1 = ymin; | |
228 | |
229 y2 = my + range; | |
230 if (y2 > ymax) | |
231 y2 = ymax; | |
232 | |
233 } while (range >= 1); | |
234 | |
235 #ifdef DEBUG | |
236 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my); | |
237 #endif | |
238 *mx_ptr = mx; | |
239 *my_ptr = my; | |
240 return dmin; | |
241 } | |
242 | |
243 static int phods_motion_search(MpegEncContext * s, | |
244 int *mx_ptr, int *my_ptr, int range, | |
245 int xmin, int ymin, int xmax, int ymax) | |
246 { | |
247 int x1, y1, x2, y2, xx, yy, x, y, lastx, d; | |
248 int mx, my, dminx, dminy; | |
249 UINT8 *pix; | |
250 | |
251 xx = s->mb_x << 4; | |
252 yy = s->mb_y << 4; | |
253 | |
254 /* Left limit */ | |
255 x1 = xx - range; | |
256 if (x1 < xmin) | |
257 x1 = xmin; | |
258 | |
259 /* Right limit */ | |
260 x2 = xx + range; | |
261 if (x2 > xmax) | |
262 x2 = xmax; | |
263 | |
264 /* Upper limit */ | |
265 y1 = yy - range; | |
266 if (y1 < ymin) | |
267 y1 = ymin; | |
268 | |
269 /* Lower limit */ | |
270 y2 = yy + range; | |
271 if (y2 > ymax) | |
272 y2 = ymax; | |
273 | |
274 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
275 mx = 0; | |
276 my = 0; | |
277 | |
278 x = xx; | |
279 y = yy; | |
280 do { | |
281 dminx = 0x7fffffff; | |
282 dminy = 0x7fffffff; | |
283 | |
284 lastx = x; | |
285 for (x = x1; x <= x2; x += range) { | |
286 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize, 16); | |
287 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { | |
288 dminx = d; | |
289 mx = x; | |
290 } | |
291 } | |
292 | |
293 x = lastx; | |
294 for (y = y1; y <= y2; y += range) { | |
295 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize, 16); | |
296 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { | |
297 dminy = d; | |
298 my = y; | |
299 } | |
300 } | |
301 | |
302 range = range >> 1; | |
303 | |
304 x = mx; | |
305 y = my; | |
306 x1 = mx - range; | |
307 if (x1 < xmin) | |
308 x1 = xmin; | |
309 | |
310 x2 = mx + range; | |
311 if (x2 > xmax) | |
312 x2 = xmax; | |
313 | |
314 y1 = my - range; | |
315 if (y1 < ymin) | |
316 y1 = ymin; | |
317 | |
318 y2 = my + range; | |
319 if (y2 > ymax) | |
320 y2 = ymax; | |
321 | |
322 } while (range >= 1); | |
323 | |
324 #ifdef DEBUG | |
325 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my); | |
326 #endif | |
327 | |
328 /* half pixel search */ | |
329 *mx_ptr = mx; | |
330 *my_ptr = my; | |
331 return dminy; | |
332 } | |
333 | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
334 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
335 #define Z_THRESHOLD 256 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
336 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
337 #define CHECK_MV(x,y)\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
338 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride, 16);\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
339 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
340 if(d<dmin){\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
341 best[0]=x;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
342 best[1]=y;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
343 dmin=d;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
344 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
345 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
346 #define CHECK_MV_DIR(x,y,new_dir)\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
347 {\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
348 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride, 16);\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
349 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
350 if(d<dmin){\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
351 best[0]=x;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
352 best[1]=y;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
353 dmin=d;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
354 next_dir= new_dir;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
355 }\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
356 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
357 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
358 static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
359 UINT8 *new_pic, UINT8 *old_pic, int pic_stride, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
360 int pred_x, int pred_y, UINT16 *mv_penalty, int quant, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
361 int xmin, int ymin, int xmax, int ymax, int shift) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
362 { |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
363 int next_dir=-1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
364 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
365 for(;;){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
366 int d; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
367 const int dir= next_dir; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
368 const int x= best[0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
369 const int y= best[1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
370 next_dir=-1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
371 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
372 //printf("%d", dir); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
373 if(dir!=2 && x-1>=xmin) CHECK_MV_DIR(x-1, y , 0) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
374 if(dir!=3 && y-1>=ymin) CHECK_MV_DIR(x , y-1, 1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
375 if(dir!=0 && x+1<=xmax) CHECK_MV_DIR(x+1, y , 2) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
376 if(dir!=1 && y+1<=ymax) CHECK_MV_DIR(x , y+1, 3) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
377 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
378 if(next_dir==-1){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
379 return dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
380 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
381 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
382 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
383 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
384 static int epzs_motion_search(MpegEncContext * s, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
385 int *mx_ptr, int *my_ptr, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
386 int *px_ptr, int *py_ptr, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
387 int xmin, int ymin, int xmax, int ymax) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
388 { |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
389 INT16 P_left[2], P_top[2], P_topright[2], P_last[2]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
390 static const int off[4]= {2, 1, 1, -1}; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
391 int best[2]={0, 0}; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
392 int d, dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
393 UINT8 *new_pic, *old_pic; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
394 const int pic_stride= s->linesize; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
395 const int pic_xy= (s->mb_y*pic_stride + s->mb_x)*16; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
396 const int mot_stride = s->block_wrap[0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
397 const int mot_xy = s->block_index[0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
398 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
399 int quant= s->qscale; // qscale of the prev frame |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
400 int pred_x, pred_y; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
401 const int shift= 1+s->quarter_sample; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
402 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
403 new_pic = s->new_picture[0] + pic_xy; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
404 old_pic = s->last_picture[0] + pic_xy; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
405 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
406 xmin-=s->mb_x*16; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
407 xmax-=s->mb_x*16; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
408 ymin-=s->mb_y*16; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
409 ymax-=s->mb_y*16; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
410 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
411 dmin = pix_abs16x16(new_pic, old_pic, pic_stride, 16); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
412 if(dmin<Z_THRESHOLD){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
413 *mx_ptr= 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
414 *my_ptr= 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
415 //printf("Z"); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
416 return dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
417 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
418 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
419 P_last[0] = s->motion_val[mot_xy ][0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
420 P_last[1] = s->motion_val[mot_xy ][1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
421 P_left[0] = s->motion_val[mot_xy - 1][0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
422 P_left[1] = s->motion_val[mot_xy - 1][1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
423 if(P_left[0] > (xmax<<shift)) P_left[0]= (xmax<<shift); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
424 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
425 /* special case for first line */ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
426 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) { |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
427 *px_ptr= pred_x = P_left[0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
428 *py_ptr= pred_y = P_left[1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
429 CHECK_MV(pred_x>>shift, pred_y>>shift) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
430 if(dmin<Z_THRESHOLD){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
431 *mx_ptr= pred_x>>shift; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
432 *my_ptr= pred_y>>shift; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
433 //printf("M"); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
434 return dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
435 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
436 } else { |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
437 P_top [0] = s->motion_val[mot_xy - mot_stride ][0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
438 P_top [1] = s->motion_val[mot_xy - mot_stride ][1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
439 P_topright[0] = s->motion_val[mot_xy - mot_stride + off[0] ][0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
440 P_topright[1] = s->motion_val[mot_xy - mot_stride + off[0] ][1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
441 if(P_top [1] > (ymax<<shift)) P_top [1]= (ymax<<shift); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
442 if(P_topright[0] < (xmin<<shift)) P_topright[0]= (xmin<<shift); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
443 if(P_topright[1] > (ymax<<shift)) P_topright[1]= (ymax<<shift); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
444 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
445 *px_ptr= pred_x = mid_pred(P_left[0], P_top[0], P_topright[0]); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
446 *py_ptr= pred_y = mid_pred(P_left[1], P_top[1], P_topright[1]); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
447 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
448 CHECK_MV(pred_x>>shift, pred_y>>shift) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
449 if(dmin<Z_THRESHOLD){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
450 *mx_ptr= pred_x>>shift; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
451 *my_ptr= pred_y>>shift; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
452 //printf("M"); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
453 return dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
454 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
455 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
456 CHECK_MV(P_left [0]>>shift, P_left [1]>>shift) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
457 CHECK_MV(P_top [0]>>shift, P_top [1]>>shift) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
458 CHECK_MV(P_topright[0]>>shift, P_topright[1]>>shift) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
459 CHECK_MV(P_last [0]>>shift, P_last [1]>>shift) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
460 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
461 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
462 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
463 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
464 *mx_ptr= best[0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
465 *my_ptr= best[1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
466 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
467 // printf("%d %d %d \n", best[0], best[1], dmin); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
468 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
469 return dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
470 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
471 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
472 #define CHECK_HALF_MV(suffix, x, y) \ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
473 d= pix_abs16x16_ ## suffix(pix, ptr+((x)>>1), s->linesize, 16);\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
474 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
475 if(d<dminh){\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
476 dminh= d;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
477 mx= mx1 + x;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
478 my= my1 + y;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
479 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
480 |
0 | 481 /* The idea would be to make half pel ME after Inter/Intra decision to |
482 save time. */ | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
483 static inline void halfpel_motion_search(MpegEncContext * s, |
0 | 484 int *mx_ptr, int *my_ptr, int dmin, |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
485 int xmin, int ymin, int xmax, int ymax, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
486 int pred_x, int pred_y) |
0 | 487 { |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
488 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
489 const int quant= s->qscale; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
490 int pen_x, pen_y; |
0 | 491 int mx, my, mx1, my1, d, xx, yy, dminh; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
492 UINT8 *pix, *ptr; |
0 | 493 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
494 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
495 mx = *mx_ptr; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
496 my = *my_ptr; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
497 ptr = s->last_picture[0] + (my * s->linesize) + mx; |
0 | 498 |
499 xx = 16 * s->mb_x; | |
500 yy = 16 * s->mb_y; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
501 pix = s->new_picture[0] + (yy * s->linesize) + xx; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
502 |
0 | 503 dminh = dmin; |
504 | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
505 if (mx > xmin && mx < xmax && |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
506 my > ymin && my < ymax) { |
0 | 507 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
508 mx= mx1= 2*(mx - xx); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
509 my= my1= 2*(my - yy); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
510 if(dmin < Z_THRESHOLD && mx==0 && my==0){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
511 *mx_ptr = 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
512 *my_ptr = 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
513 return; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
514 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
515 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
516 pen_x= pred_x + mx; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
517 pen_y= pred_y + my; |
0 | 518 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
519 ptr-= s->linesize; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
520 CHECK_HALF_MV(xy2, -1, -1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
521 CHECK_HALF_MV(y2 , 0, -1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
522 CHECK_HALF_MV(xy2, +1, -1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
523 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
524 ptr+= s->linesize; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
525 CHECK_HALF_MV(x2 , -1, 0) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
526 CHECK_HALF_MV(x2 , +1, 0) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
527 CHECK_HALF_MV(xy2, -1, +1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
528 CHECK_HALF_MV(y2 , 0, +1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
529 CHECK_HALF_MV(xy2, +1, +1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
530 }else{ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
531 mx= 2*(mx - xx); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
532 my= 2*(my - yy); |
0 | 533 } |
534 | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
535 *mx_ptr = mx; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
536 *my_ptr = my; |
0 | 537 } |
538 | |
539 #ifndef CONFIG_TEST_MV_ENCODE | |
540 | |
541 int estimate_motion(MpegEncContext * s, | |
542 int mb_x, int mb_y, | |
543 int *mx_ptr, int *my_ptr) | |
544 { | |
545 UINT8 *pix, *ppix; | |
546 int sum, varc, vard, mx, my, range, dmin, xx, yy; | |
547 int xmin, ymin, xmax, ymax; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
548 int pred_x=0, pred_y=0; |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
549 |
0 | 550 range = 8 * (1 << (s->f_code - 1)); |
551 /* XXX: temporary kludge to avoid overflow for msmpeg4 */ | |
552 if (s->out_format == FMT_H263 && !s->h263_msmpeg4) | |
553 range = range * 2; | |
554 | |
555 if (s->unrestricted_mv) { | |
556 xmin = -16; | |
557 ymin = -16; | |
275
7ebb3f9aaf3b
- Added video coding statistics for ffmpeg. Needs more work.
pulento
parents:
268
diff
changeset
|
558 if (s->h263_plus) |
7ebb3f9aaf3b
- Added video coding statistics for ffmpeg. Needs more work.
pulento
parents:
268
diff
changeset
|
559 range *= 2; |
218 | 560 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){ |
561 xmax = s->mb_width*16; | |
562 ymax = s->mb_height*16; | |
563 }else { | |
564 /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise | |
565 (cuz the drawn edge isnt large enough))*/ | |
566 xmax = s->width; | |
567 ymax = s->height; | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
218
diff
changeset
|
568 } |
0 | 569 } else { |
570 xmin = 0; | |
571 ymin = 0; | |
218 | 572 xmax = s->mb_width*16 - 16; |
573 ymax = s->mb_height*16 - 16; | |
0 | 574 } |
575 switch(s->full_search) { | |
576 case ME_ZERO: | |
577 default: | |
578 no_motion_search(s, &mx, &my); | |
579 dmin = 0; | |
580 break; | |
581 case ME_FULL: | |
582 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax); | |
583 break; | |
584 case ME_LOG: | |
585 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax); | |
586 break; | |
587 case ME_PHODS: | |
588 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax); | |
589 break; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
590 case ME_X1: // just reserving some space for experiments ... |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
591 case ME_EPZS: |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
592 dmin = epzs_motion_search(s, &mx, &my, &pred_x, &pred_y, xmin, ymin, xmax, ymax); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
593 mx+= s->mb_x*16; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
594 my+= s->mb_y*16; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
595 break; |
0 | 596 } |
597 | |
598 /* intra / predictive decision */ | |
599 xx = mb_x * 16; | |
600 yy = mb_y * 16; | |
601 | |
602 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
603 /* At this point (mx,my) are full-pell and the absolute displacement */ | |
604 ppix = s->last_picture[0] + (my * s->linesize) + mx; | |
605 | |
606 sum = pix_sum(pix, s->linesize); | |
607 varc = pix_norm1(pix, s->linesize); | |
608 vard = pix_norm(pix, ppix, s->linesize); | |
609 | |
610 vard = vard >> 8; | |
611 sum = sum >> 8; | |
612 varc = (varc >> 8) - (sum * sum); | |
239 | 613 s->mb_var[s->mb_width * mb_y + mb_x] = varc; |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
614 s->avg_mb_var += varc; |
268 | 615 s->mc_mb_var += vard; |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
616 |
0 | 617 #if 0 |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
618 printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n", |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
619 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy); |
0 | 620 #endif |
621 if (vard <= 64 || vard < varc) { | |
622 if (s->full_search != ME_ZERO) { | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
623 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y); |
0 | 624 } else { |
625 mx -= 16 * s->mb_x; | |
626 my -= 16 * s->mb_y; | |
627 } | |
628 *mx_ptr = mx; | |
629 *my_ptr = my; | |
630 return 0; | |
631 } else { | |
632 *mx_ptr = 0; | |
633 *my_ptr = 0; | |
634 return 1; | |
635 } | |
636 } | |
637 | |
638 #else | |
639 | |
640 /* test version which generates valid random vectors */ | |
641 int estimate_motion(MpegEncContext * s, | |
642 int mb_x, int mb_y, | |
643 int *mx_ptr, int *my_ptr) | |
644 { | |
645 int xx, yy, x1, y1, x2, y2, range; | |
646 | |
647 if ((random() % 10) >= 5) { | |
648 range = 8 * (1 << (s->f_code - 1)); | |
649 if (s->out_format == FMT_H263 && !s->h263_msmpeg4) | |
650 range = range * 2; | |
651 | |
652 xx = 16 * s->mb_x; | |
653 yy = 16 * s->mb_y; | |
654 x1 = xx - range; | |
655 if (x1 < 0) | |
656 x1 = 0; | |
657 x2 = xx + range - 1; | |
658 if (x2 > (s->width - 16)) | |
659 x2 = s->width - 16; | |
660 y1 = yy - range; | |
661 if (y1 < 0) | |
662 y1 = 0; | |
663 y2 = yy + range - 1; | |
664 if (y2 > (s->height - 16)) | |
665 y2 = s->height - 16; | |
666 | |
667 *mx_ptr = (random() % (2 * (x2 - x1 + 1))) + 2 * (x1 - xx); | |
668 *my_ptr = (random() % (2 * (y2 - y1 + 1))) + 2 * (y1 - yy); | |
669 return 0; | |
670 } else { | |
671 *mx_ptr = 0; | |
672 *my_ptr = 0; | |
673 return 1; | |
674 } | |
675 } | |
676 | |
677 #endif |