Mercurial > libavcodec.hg
annotate motion_est.c @ 324:9c6f056f0e41 libavcodec
fixed mpeg4 time stuff on encoding
mpeg4 b-frame enoding support
removed old, out-commented ratecontrol
reuse motion compensation code between encoding & decoding
prefix newly added global functions with ff_ to reduce namespace polution
b-frame ME (unfinished, but working)
added some comments to mpegvideo.h
do MC on encoding only once if possible
bugs? ;)
author | michaelni |
---|---|
date | Wed, 17 Apr 2002 04:32:12 +0000 |
parents | cda7d0857baf |
children | d359db02fc90 |
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 | |
284 | 28 #define ABS(a) ((a)>0 ? (a) : -(a)) |
294 | 29 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
284 | 30 #define INTER_BIAS 257 |
31 | |
0 | 32 static void halfpel_motion_search(MpegEncContext * s, |
33 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
|
34 int xmin, int ymin, int xmax, int ymax, |
324 | 35 int pred_x, int pred_y, uint8_t *ref_picture); |
0 | 36 |
37 static int pix_sum(UINT8 * pix, int line_size) | |
38 { | |
39 int s, i, j; | |
40 | |
41 s = 0; | |
42 for (i = 0; i < 16; i++) { | |
43 for (j = 0; j < 16; j += 8) { | |
44 s += pix[0]; | |
45 s += pix[1]; | |
46 s += pix[2]; | |
47 s += pix[3]; | |
48 s += pix[4]; | |
49 s += pix[5]; | |
50 s += pix[6]; | |
51 s += pix[7]; | |
52 pix += 8; | |
53 } | |
54 pix += line_size - 16; | |
55 } | |
56 return s; | |
57 } | |
58 | |
284 | 59 static int pix_dev(UINT8 * pix, int line_size, int mean) |
60 { | |
61 int s, i, j; | |
62 | |
63 s = 0; | |
64 for (i = 0; i < 16; i++) { | |
65 for (j = 0; j < 16; j += 8) { | |
66 s += ABS(pix[0]-mean); | |
67 s += ABS(pix[1]-mean); | |
68 s += ABS(pix[2]-mean); | |
69 s += ABS(pix[3]-mean); | |
70 s += ABS(pix[4]-mean); | |
71 s += ABS(pix[5]-mean); | |
72 s += ABS(pix[6]-mean); | |
73 s += ABS(pix[7]-mean); | |
74 pix += 8; | |
75 } | |
76 pix += line_size - 16; | |
77 } | |
78 return s; | |
79 } | |
80 | |
0 | 81 static int pix_norm1(UINT8 * pix, 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[pix[0]]; | |
90 s += sq[pix[1]]; | |
91 s += sq[pix[2]]; | |
92 s += sq[pix[3]]; | |
93 s += sq[pix[4]]; | |
94 s += sq[pix[5]]; | |
95 s += sq[pix[6]]; | |
96 s += sq[pix[7]]; | |
97 pix += 8; | |
98 } | |
99 pix += line_size - 16; | |
100 } | |
101 return s; | |
102 } | |
103 | |
104 static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size) | |
105 { | |
106 int s, i, j; | |
107 UINT32 *sq = squareTbl + 256; | |
108 | |
109 s = 0; | |
110 for (i = 0; i < 16; i++) { | |
111 for (j = 0; j < 16; j += 8) { | |
112 s += sq[pix1[0] - pix2[0]]; | |
113 s += sq[pix1[1] - pix2[1]]; | |
114 s += sq[pix1[2] - pix2[2]]; | |
115 s += sq[pix1[3] - pix2[3]]; | |
116 s += sq[pix1[4] - pix2[4]]; | |
117 s += sq[pix1[5] - pix2[5]]; | |
118 s += sq[pix1[6] - pix2[6]]; | |
119 s += sq[pix1[7] - pix2[7]]; | |
120 pix1 += 8; | |
121 pix2 += 8; | |
122 } | |
123 pix1 += line_size - 16; | |
124 pix2 += line_size - 16; | |
125 } | |
126 return s; | |
127 } | |
128 | |
129 static void no_motion_search(MpegEncContext * s, | |
130 int *mx_ptr, int *my_ptr) | |
131 { | |
132 *mx_ptr = 16 * s->mb_x; | |
133 *my_ptr = 16 * s->mb_y; | |
134 } | |
135 | |
136 static int full_motion_search(MpegEncContext * s, | |
137 int *mx_ptr, int *my_ptr, int range, | |
324 | 138 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
0 | 139 { |
140 int x1, y1, x2, y2, xx, yy, x, y; | |
141 int mx, my, dmin, d; | |
142 UINT8 *pix; | |
143 | |
144 xx = 16 * s->mb_x; | |
145 yy = 16 * s->mb_y; | |
146 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */ | |
147 if (x1 < xmin) | |
148 x1 = xmin; | |
149 x2 = xx + range - 1; | |
150 if (x2 > xmax) | |
151 x2 = xmax; | |
152 y1 = yy - range + 1; | |
153 if (y1 < ymin) | |
154 y1 = ymin; | |
155 y2 = yy + range - 1; | |
156 if (y2 > ymax) | |
157 y2 = ymax; | |
158 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
159 dmin = 0x7fffffff; | |
160 mx = 0; | |
161 my = 0; | |
162 for (y = y1; y <= y2; y++) { | |
163 for (x = x1; x <= x2; x++) { | |
324 | 164 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, |
294 | 165 s->linesize); |
0 | 166 if (d < dmin || |
167 (d == dmin && | |
168 (abs(x - xx) + abs(y - yy)) < | |
169 (abs(mx - xx) + abs(my - yy)))) { | |
170 dmin = d; | |
171 mx = x; | |
172 my = y; | |
173 } | |
174 } | |
175 } | |
176 | |
177 *mx_ptr = mx; | |
178 *my_ptr = my; | |
179 | |
180 #if 0 | |
181 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) || | |
182 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) { | |
183 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr); | |
184 } | |
185 #endif | |
186 return dmin; | |
187 } | |
188 | |
189 | |
190 static int log_motion_search(MpegEncContext * s, | |
191 int *mx_ptr, int *my_ptr, int range, | |
324 | 192 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
0 | 193 { |
194 int x1, y1, x2, y2, xx, yy, x, y; | |
195 int mx, my, dmin, d; | |
196 UINT8 *pix; | |
197 | |
198 xx = s->mb_x << 4; | |
199 yy = s->mb_y << 4; | |
200 | |
201 /* Left limit */ | |
202 x1 = xx - range; | |
203 if (x1 < xmin) | |
204 x1 = xmin; | |
205 | |
206 /* Right limit */ | |
207 x2 = xx + range; | |
208 if (x2 > xmax) | |
209 x2 = xmax; | |
210 | |
211 /* Upper limit */ | |
212 y1 = yy - range; | |
213 if (y1 < ymin) | |
214 y1 = ymin; | |
215 | |
216 /* Lower limit */ | |
217 y2 = yy + range; | |
218 if (y2 > ymax) | |
219 y2 = ymax; | |
220 | |
221 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
222 dmin = 0x7fffffff; | |
223 mx = 0; | |
224 my = 0; | |
225 | |
226 do { | |
227 for (y = y1; y <= y2; y += range) { | |
228 for (x = x1; x <= x2; x += range) { | |
324 | 229 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); |
0 | 230 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { |
231 dmin = d; | |
232 mx = x; | |
233 my = y; | |
234 } | |
235 } | |
236 } | |
237 | |
238 range = range >> 1; | |
239 | |
240 x1 = mx - range; | |
241 if (x1 < xmin) | |
242 x1 = xmin; | |
243 | |
244 x2 = mx + range; | |
245 if (x2 > xmax) | |
246 x2 = xmax; | |
247 | |
248 y1 = my - range; | |
249 if (y1 < ymin) | |
250 y1 = ymin; | |
251 | |
252 y2 = my + range; | |
253 if (y2 > ymax) | |
254 y2 = ymax; | |
255 | |
256 } while (range >= 1); | |
257 | |
258 #ifdef DEBUG | |
259 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my); | |
260 #endif | |
261 *mx_ptr = mx; | |
262 *my_ptr = my; | |
263 return dmin; | |
264 } | |
265 | |
266 static int phods_motion_search(MpegEncContext * s, | |
267 int *mx_ptr, int *my_ptr, int range, | |
324 | 268 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
0 | 269 { |
270 int x1, y1, x2, y2, xx, yy, x, y, lastx, d; | |
271 int mx, my, dminx, dminy; | |
272 UINT8 *pix; | |
273 | |
274 xx = s->mb_x << 4; | |
275 yy = s->mb_y << 4; | |
276 | |
277 /* Left limit */ | |
278 x1 = xx - range; | |
279 if (x1 < xmin) | |
280 x1 = xmin; | |
281 | |
282 /* Right limit */ | |
283 x2 = xx + range; | |
284 if (x2 > xmax) | |
285 x2 = xmax; | |
286 | |
287 /* Upper limit */ | |
288 y1 = yy - range; | |
289 if (y1 < ymin) | |
290 y1 = ymin; | |
291 | |
292 /* Lower limit */ | |
293 y2 = yy + range; | |
294 if (y2 > ymax) | |
295 y2 = ymax; | |
296 | |
297 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
298 mx = 0; | |
299 my = 0; | |
300 | |
301 x = xx; | |
302 y = yy; | |
303 do { | |
304 dminx = 0x7fffffff; | |
305 dminy = 0x7fffffff; | |
306 | |
307 lastx = x; | |
308 for (x = x1; x <= x2; x += range) { | |
324 | 309 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); |
0 | 310 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { |
311 dminx = d; | |
312 mx = x; | |
313 } | |
314 } | |
315 | |
316 x = lastx; | |
317 for (y = y1; y <= y2; y += range) { | |
324 | 318 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); |
0 | 319 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { |
320 dminy = d; | |
321 my = y; | |
322 } | |
323 } | |
324 | |
325 range = range >> 1; | |
326 | |
327 x = mx; | |
328 y = my; | |
329 x1 = mx - range; | |
330 if (x1 < xmin) | |
331 x1 = xmin; | |
332 | |
333 x2 = mx + range; | |
334 if (x2 > xmax) | |
335 x2 = xmax; | |
336 | |
337 y1 = my - range; | |
338 if (y1 < ymin) | |
339 y1 = ymin; | |
340 | |
341 y2 = my + range; | |
342 if (y2 > ymax) | |
343 y2 = ymax; | |
344 | |
345 } while (range >= 1); | |
346 | |
347 #ifdef DEBUG | |
348 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my); | |
349 #endif | |
350 | |
351 /* half pixel search */ | |
352 *mx_ptr = mx; | |
353 *my_ptr = my; | |
354 return dminy; | |
355 } | |
356 | |
277
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 #define Z_THRESHOLD 256 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
359 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
360 #define CHECK_MV(x,y)\ |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
361 {\ |
294 | 362 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
363 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
|
364 if(d<dmin){\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
365 best[0]=x;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
366 best[1]=y;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
367 dmin=d;\ |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
368 }\ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
369 } |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
370 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
371 #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
|
372 {\ |
294 | 373 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
374 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
|
375 if(d<dmin){\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
376 best[0]=x;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
377 best[1]=y;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
378 dmin=d;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
379 next_dir= new_dir;\ |
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 |
294 | 383 #define CHECK_MV4(x,y)\ |
384 {\ | |
385 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ | |
386 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ | |
387 if(d<dmin){\ | |
388 best[0]=x;\ | |
389 best[1]=y;\ | |
390 dmin=d;\ | |
391 }\ | |
392 } | |
393 | |
394 #define CHECK_MV4_DIR(x,y,new_dir)\ | |
395 {\ | |
396 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ | |
397 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ | |
398 if(d<dmin){\ | |
399 best[0]=x;\ | |
400 best[1]=y;\ | |
401 dmin=d;\ | |
402 next_dir= new_dir;\ | |
403 }\ | |
404 } | |
405 | |
406 | |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
407 #define check(x,y,S,v)\ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
408 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d xmin" #v, (x), (y), s->mb_x, s->mb_y);\ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
409 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d xmax" #v, (x), (y), s->mb_x, s->mb_y);\ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
410 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d ymin" #v, (x), (y), s->mb_x, s->mb_y);\ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
411 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d ymax" #v, (x), (y), s->mb_x, s->mb_y);\ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
412 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
413 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
414 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
|
415 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
|
416 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
|
417 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
|
418 { |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
419 int next_dir=-1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
420 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
421 for(;;){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
422 int d; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
423 const int dir= next_dir; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
424 const int x= best[0]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
425 const int y= best[1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
426 next_dir=-1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
427 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
428 //printf("%d", dir); |
280 | 429 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0) |
430 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1) | |
431 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2) | |
432 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3) | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
433 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
434 if(next_dir==-1){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
435 return dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
436 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
437 } |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
438 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
439 /* for(;;){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
440 int d; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
441 const int x= best[0]; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
442 const int y= best[1]; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
443 const int last_min=dmin; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
444 if(x>xmin) CHECK_MV(x-1, y ) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
445 if(y>xmin) CHECK_MV(x , y-1) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
446 if(x<xmax) CHECK_MV(x+1, y ) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
447 if(y<xmax) CHECK_MV(x , y+1) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
448 if(x>xmin && y>ymin) CHECK_MV(x-1, y-1) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
449 if(x>xmin && y<ymax) CHECK_MV(x-1, y+1) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
450 if(x<xmax && y>ymin) CHECK_MV(x+1, y-1) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
451 if(x<xmax && y<ymax) CHECK_MV(x+1, y+1) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
452 if(x-1>xmin) CHECK_MV(x-2, y ) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
453 if(y-1>xmin) CHECK_MV(x , y-2) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
454 if(x+1<xmax) CHECK_MV(x+2, y ) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
455 if(y+1<xmax) CHECK_MV(x , y+2) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
456 if(x-1>xmin && y-1>ymin) CHECK_MV(x-2, y-2) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
457 if(x-1>xmin && y+1<ymax) CHECK_MV(x-2, y+2) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
458 if(x+1<xmax && y-1>ymin) CHECK_MV(x+2, y-2) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
459 if(x+1<xmax && y+1<ymax) CHECK_MV(x+2, y+2) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
460 if(dmin==last_min) return dmin; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
461 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
462 */ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
463 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
464 |
294 | 465 static inline int small_diamond_search4MV(MpegEncContext * s, int *best, int dmin, |
466 UINT8 *new_pic, UINT8 *old_pic, int pic_stride, | |
467 int pred_x, int pred_y, UINT16 *mv_penalty, int quant, | |
468 int xmin, int ymin, int xmax, int ymax, int shift) | |
469 { | |
470 int next_dir=-1; | |
471 | |
472 for(;;){ | |
473 int d; | |
474 const int dir= next_dir; | |
475 const int x= best[0]; | |
476 const int y= best[1]; | |
477 next_dir=-1; | |
478 | |
479 //printf("%d", dir); | |
480 if(dir!=2 && x>xmin) CHECK_MV4_DIR(x-1, y , 0) | |
481 if(dir!=3 && y>ymin) CHECK_MV4_DIR(x , y-1, 1) | |
482 if(dir!=0 && x<xmax) CHECK_MV4_DIR(x+1, y , 2) | |
483 if(dir!=1 && y<ymax) CHECK_MV4_DIR(x , y+1, 3) | |
484 | |
485 if(next_dir==-1){ | |
486 return dmin; | |
487 } | |
488 } | |
489 } | |
490 | |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
491 static inline int snake_search(MpegEncContext * s, int *best, int dmin, |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
492 UINT8 *new_pic, UINT8 *old_pic, int pic_stride, |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
493 int pred_x, int pred_y, UINT16 *mv_penalty, int quant, |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
494 int xmin, int ymin, int xmax, int ymax, int shift) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
495 { |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
496 int dir=0; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
497 int c=1; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
498 static int x_dir[8]= {1,1,0,-1,-1,-1, 0, 1}; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
499 static int y_dir[8]= {0,1,1, 1, 0,-1,-1,-1}; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
500 int fails=0; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
501 int last_d[2]={dmin, dmin}; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
502 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
503 /*static int good=0; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
504 static int bad=0; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
505 static int point=0; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
506 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
507 point++; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
508 if(256*256*256*64%point==0) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
509 { |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
510 printf("%d %d %d\n", good, bad, point); |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
511 }*/ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
512 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
513 for(;;){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
514 int x= best[0]; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
515 int y= best[1]; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
516 int d; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
517 x+=x_dir[dir]; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
518 y+=y_dir[dir]; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
519 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){ |
294 | 520 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride); |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
521 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
522 }else{ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
523 d = dmin + 10000; //FIXME smarter boundary handling |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
524 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
525 if(d<dmin){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
526 best[0]=x; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
527 best[1]=y; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
528 dmin=d; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
529 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
530 if(last_d[1] - last_d[0] > last_d[0] - d) c= -c; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
531 dir+=c; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
532 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
533 fails=0; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
534 //good++; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
535 last_d[1]=last_d[0]; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
536 last_d[0]=d; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
537 }else{ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
538 //bad++; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
539 if(fails){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
540 if(fails>=3) return dmin; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
541 }else{ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
542 c= -c; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
543 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
544 dir+=c*2; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
545 fails++; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
546 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
547 dir&=7; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
548 } |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
549 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
550 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
551 static int epzs_motion_search(MpegEncContext * s, |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
552 int *mx_ptr, int *my_ptr, |
280 | 553 int P[5][2], int pred_x, int pred_y, |
324 | 554 int xmin, int ymin, int xmax, int ymax, uint8_t * ref_picture) |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
555 { |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
556 int best[2]={0, 0}; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
557 int d, dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
558 UINT8 *new_pic, *old_pic; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
559 const int pic_stride= s->linesize; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
560 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
|
561 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
|
562 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
|
563 const int shift= 1+s->quarter_sample; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
564 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
565 new_pic = s->new_picture[0] + pic_xy; |
324 | 566 old_pic = ref_picture + pic_xy; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
567 |
294 | 568 dmin = pix_abs16x16(new_pic, old_pic, pic_stride); |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
569 if(dmin<Z_THRESHOLD){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
570 *mx_ptr= 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
571 *my_ptr= 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
572 //printf("Z"); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
573 return dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
574 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
575 |
280 | 576 /* first line */ |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
577 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) { |
280 | 578 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift) |
579 }else{ | |
580 CHECK_MV(P[4][0]>>shift, P[4][1]>>shift) | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
581 if(dmin<Z_THRESHOLD){ |
280 | 582 *mx_ptr= P[4][0]>>shift; |
583 *my_ptr= P[4][1]>>shift; | |
584 //printf("M\n"); | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
585 return dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
586 } |
280 | 587 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift) |
588 CHECK_MV(P[2][0]>>shift, P[2][1]>>shift) | |
589 CHECK_MV(P[3][0]>>shift, P[3][1]>>shift) | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
590 } |
280 | 591 CHECK_MV(P[0][0]>>shift, P[0][1]>>shift) |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
592 |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
593 //check(best[0],best[1],0, b0) |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
594 if(s->me_method==ME_EPZS) |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
595 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride, |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
596 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift); |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
597 else |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
598 dmin= snake_search(s, best, dmin, new_pic, old_pic, pic_stride, |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
599 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift); |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
600 //check(best[0],best[1],0, b1) |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
601 *mx_ptr= best[0]; |
280 | 602 *my_ptr= best[1]; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
603 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
604 // 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
|
605 return dmin; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
606 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
607 |
294 | 608 static int epzs_motion_search4(MpegEncContext * s, int block, |
609 int *mx_ptr, int *my_ptr, | |
610 int P[6][2], int pred_x, int pred_y, | |
324 | 611 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
294 | 612 { |
613 int best[2]={0, 0}; | |
614 int d, dmin; | |
615 UINT8 *new_pic, *old_pic; | |
616 const int pic_stride= s->linesize; | |
617 const int pic_xy= ((s->mb_y*2 + (block>>1))*pic_stride + s->mb_x*2 + (block&1))*8; | |
618 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame | |
619 int quant= s->qscale; // qscale of the prev frame | |
620 const int shift= 1+s->quarter_sample; | |
621 | |
622 new_pic = s->new_picture[0] + pic_xy; | |
324 | 623 old_pic = ref_picture + pic_xy; |
294 | 624 |
625 dmin = pix_abs8x8(new_pic, old_pic, pic_stride); | |
626 | |
627 /* first line */ | |
628 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) { | |
629 CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift) | |
630 }else{ | |
631 CHECK_MV4(P[4][0]>>shift, P[4][1]>>shift) | |
632 if(dmin<Z_THRESHOLD){ | |
633 *mx_ptr= P[4][0]>>shift; | |
634 *my_ptr= P[4][1]>>shift; | |
635 //printf("M\n"); | |
636 return dmin; | |
637 } | |
638 CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift) | |
639 CHECK_MV4(P[2][0]>>shift, P[2][1]>>shift) | |
640 CHECK_MV4(P[3][0]>>shift, P[3][1]>>shift) | |
641 } | |
642 CHECK_MV4(P[0][0]>>shift, P[0][1]>>shift) | |
643 CHECK_MV4(P[5][0]>>shift, P[5][1]>>shift) | |
644 | |
645 //check(best[0],best[1],0, b0) | |
646 dmin= small_diamond_search4MV(s, best, dmin, new_pic, old_pic, pic_stride, | |
647 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift); | |
648 //check(best[0],best[1],0, b1) | |
649 *mx_ptr= best[0]; | |
650 *my_ptr= best[1]; | |
651 | |
652 // printf("%d %d %d \n", best[0], best[1], dmin); | |
653 return dmin; | |
654 } | |
655 | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
656 #define CHECK_HALF_MV(suffix, x, y) \ |
294 | 657 d= pix_abs16x16_ ## suffix(pix, ptr+((x)>>1), s->linesize);\ |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
658 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
|
659 if(d<dminh){\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
660 dminh= d;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
661 mx= mx1 + x;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
662 my= my1 + y;\ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
663 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
664 |
294 | 665 #define CHECK_HALF_MV4(suffix, x, y) \ |
666 d= pix_abs8x8_ ## suffix(pix, ptr+((x)>>1), s->linesize);\ | |
667 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\ | |
668 if(d<dminh){\ | |
669 dminh= d;\ | |
670 mx= mx1 + x;\ | |
671 my= my1 + y;\ | |
672 } | |
673 | |
0 | 674 /* The idea would be to make half pel ME after Inter/Intra decision to |
675 save time. */ | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
676 static inline void halfpel_motion_search(MpegEncContext * s, |
0 | 677 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
|
678 int xmin, int ymin, int xmax, int ymax, |
324 | 679 int pred_x, int pred_y, uint8_t *ref_picture) |
0 | 680 { |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
681 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
|
682 const int quant= s->qscale; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
683 int pen_x, pen_y; |
0 | 684 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
|
685 UINT8 *pix, *ptr; |
0 | 686 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
687 mx = *mx_ptr; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
688 my = *my_ptr; |
324 | 689 ptr = ref_picture + (my * s->linesize) + mx; |
0 | 690 |
691 xx = 16 * s->mb_x; | |
692 yy = 16 * s->mb_y; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
693 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
|
694 |
0 | 695 dminh = dmin; |
696 | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
697 if (mx > xmin && mx < xmax && |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
698 my > ymin && my < ymax) { |
0 | 699 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
700 mx= mx1= 2*(mx - xx); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
701 my= my1= 2*(my - yy); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
702 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
|
703 *mx_ptr = 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
704 *my_ptr = 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
705 return; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
706 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
707 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
708 pen_x= pred_x + mx; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
709 pen_y= pred_y + my; |
0 | 710 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
711 ptr-= s->linesize; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
712 CHECK_HALF_MV(xy2, -1, -1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
713 CHECK_HALF_MV(y2 , 0, -1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
714 CHECK_HALF_MV(xy2, +1, -1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
715 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
716 ptr+= s->linesize; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
717 CHECK_HALF_MV(x2 , -1, 0) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
718 CHECK_HALF_MV(x2 , +1, 0) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
719 CHECK_HALF_MV(xy2, -1, +1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
720 CHECK_HALF_MV(y2 , 0, +1) |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
721 CHECK_HALF_MV(xy2, +1, +1) |
294 | 722 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
723 }else{ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
724 mx= 2*(mx - xx); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
725 my= 2*(my - yy); |
0 | 726 } |
727 | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
728 *mx_ptr = mx; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
729 *my_ptr = my; |
0 | 730 } |
731 | |
294 | 732 static inline void halfpel_motion_search4(MpegEncContext * s, |
733 int *mx_ptr, int *my_ptr, int dmin, | |
734 int xmin, int ymin, int xmax, int ymax, | |
324 | 735 int pred_x, int pred_y, int block_x, int block_y, |
736 uint8_t *ref_picture) | |
294 | 737 { |
738 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame | |
739 const int quant= s->qscale; | |
740 int pen_x, pen_y; | |
741 int mx, my, mx1, my1, d, xx, yy, dminh; | |
742 UINT8 *pix, *ptr; | |
743 | |
744 xx = 8 * block_x; | |
745 yy = 8 * block_y; | |
746 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
747 | |
748 mx = *mx_ptr; | |
749 my = *my_ptr; | |
324 | 750 ptr = ref_picture + ((yy+my) * s->linesize) + xx + mx; |
294 | 751 |
752 dminh = dmin; | |
753 | |
754 if (mx > xmin && mx < xmax && | |
755 my > ymin && my < ymax) { | |
756 | |
757 mx= mx1= 2*mx; | |
758 my= my1= 2*my; | |
759 if(dmin < Z_THRESHOLD && mx==0 && my==0){ | |
760 *mx_ptr = 0; | |
761 *my_ptr = 0; | |
762 return; | |
763 } | |
764 | |
765 pen_x= pred_x + mx; | |
766 pen_y= pred_y + my; | |
767 | |
768 ptr-= s->linesize; | |
769 CHECK_HALF_MV4(xy2, -1, -1) | |
770 CHECK_HALF_MV4(y2 , 0, -1) | |
771 CHECK_HALF_MV4(xy2, +1, -1) | |
772 | |
773 ptr+= s->linesize; | |
774 CHECK_HALF_MV4(x2 , -1, 0) | |
775 CHECK_HALF_MV4(x2 , +1, 0) | |
776 CHECK_HALF_MV4(xy2, -1, +1) | |
777 CHECK_HALF_MV4(y2 , 0, +1) | |
778 CHECK_HALF_MV4(xy2, +1, +1) | |
779 | |
780 }else{ | |
781 mx*=2; | |
782 my*=2; | |
783 } | |
784 | |
785 *mx_ptr = mx; | |
786 *my_ptr = my; | |
787 } | |
788 | |
324 | 789 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my) |
294 | 790 { |
324 | 791 const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2); |
294 | 792 |
324 | 793 s->p_mv_table[xy][0] = mx; |
794 s->p_mv_table[xy][1] = my; | |
294 | 795 |
796 /* has allready been set to the 4 MV if 4MV is done */ | |
797 if(!(s->flags&CODEC_FLAG_4MV)){ | |
798 int mot_xy= s->block_index[0]; | |
799 | |
800 s->motion_val[mot_xy ][0]= mx; | |
801 s->motion_val[mot_xy ][1]= my; | |
802 s->motion_val[mot_xy+1][0]= mx; | |
803 s->motion_val[mot_xy+1][1]= my; | |
804 | |
805 mot_xy += s->block_wrap[0]; | |
806 s->motion_val[mot_xy ][0]= mx; | |
807 s->motion_val[mot_xy ][1]= my; | |
808 s->motion_val[mot_xy+1][0]= mx; | |
809 s->motion_val[mot_xy+1][1]= my; | |
810 } | |
811 } | |
812 | |
324 | 813 static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code) |
814 { | |
815 *range = 8 * (1 << (f_code - 1)); | |
816 /* XXX: temporary kludge to avoid overflow for msmpeg4 */ | |
817 if (s->out_format == FMT_H263 && !s->h263_msmpeg4) | |
818 *range *= 2; | |
0 | 819 |
324 | 820 if (s->unrestricted_mv) { |
821 *xmin = -16; | |
822 *ymin = -16; | |
823 if (s->h263_plus) | |
824 *range *= 2; | |
825 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){ | |
826 *xmax = s->mb_width*16; | |
827 *ymax = s->mb_height*16; | |
828 }else { | |
829 /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise | |
830 (cuz the drawn edge isnt large enough))*/ | |
831 *xmax = s->width; | |
832 *ymax = s->height; | |
833 } | |
834 } else { | |
835 *xmin = 0; | |
836 *ymin = 0; | |
837 *xmax = s->mb_width*16 - 16; | |
838 *ymax = s->mb_height*16 - 16; | |
839 } | |
840 } | |
841 | |
842 void ff_estimate_p_frame_motion(MpegEncContext * s, | |
843 int mb_x, int mb_y) | |
0 | 844 { |
845 UINT8 *pix, *ppix; | |
846 int sum, varc, vard, mx, my, range, dmin, xx, yy; | |
847 int xmin, ymin, xmax, ymax; | |
280 | 848 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
849 int pred_x=0, pred_y=0; |
294 | 850 int P[6][2]; |
280 | 851 const int shift= 1+s->quarter_sample; |
294 | 852 int mb_type=0; |
324 | 853 uint8_t *ref_picture= s->last_picture[0]; |
0 | 854 |
324 | 855 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code); |
856 | |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
857 switch(s->me_method) { |
0 | 858 case ME_ZERO: |
859 default: | |
860 no_motion_search(s, &mx, &my); | |
861 dmin = 0; | |
862 break; | |
863 case ME_FULL: | |
324 | 864 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); |
0 | 865 break; |
866 case ME_LOG: | |
324 | 867 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
0 | 868 break; |
869 case ME_PHODS: | |
324 | 870 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
0 | 871 break; |
288 | 872 case ME_X1: |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
873 case ME_EPZS: |
288 | 874 { |
294 | 875 const int mot_stride = s->block_wrap[0]; |
876 const int mot_xy = s->block_index[0]; | |
288 | 877 |
294 | 878 rel_xmin= xmin - mb_x*16; |
879 rel_xmax= xmax - mb_x*16; | |
880 rel_ymin= ymin - mb_y*16; | |
881 rel_ymax= ymax - mb_y*16; | |
288 | 882 |
280 | 883 P[0][0] = s->motion_val[mot_xy ][0]; |
884 P[0][1] = s->motion_val[mot_xy ][1]; | |
885 P[1][0] = s->motion_val[mot_xy - 1][0]; | |
886 P[1][1] = s->motion_val[mot_xy - 1][1]; | |
887 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift); | |
888 | |
889 /* special case for first line */ | |
294 | 890 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) { |
288 | 891 P[4][0] = P[1][0]; |
892 P[4][1] = P[1][1]; | |
280 | 893 } else { |
894 P[2][0] = s->motion_val[mot_xy - mot_stride ][0]; | |
895 P[2][1] = s->motion_val[mot_xy - mot_stride ][1]; | |
294 | 896 P[3][0] = s->motion_val[mot_xy - mot_stride + 2 ][0]; |
897 P[3][1] = s->motion_val[mot_xy - mot_stride + 2 ][1]; | |
280 | 898 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift); |
899 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift); | |
900 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift); | |
901 | |
902 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]); | |
903 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]); | |
904 } | |
288 | 905 if(s->out_format == FMT_H263){ |
906 pred_x = P[4][0]; | |
907 pred_y = P[4][1]; | |
908 }else { /* mpeg1 at least */ | |
909 pred_x= P[1][0]; | |
910 pred_y= P[1][1]; | |
911 } | |
280 | 912 } |
324 | 913 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture); |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
914 |
294 | 915 mx+= mb_x*16; |
916 my+= mb_y*16; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
917 break; |
0 | 918 } |
294 | 919 |
920 if(s->flags&CODEC_FLAG_4MV){ | |
921 int block; | |
922 | |
923 mb_type|= MB_TYPE_INTER4V; | |
924 | |
925 for(block=0; block<4; block++){ | |
926 int mx4, my4; | |
927 int pred_x4, pred_y4; | |
928 int dmin4; | |
929 static const int off[4]= {2, 1, 1, -1}; | |
930 const int mot_stride = s->block_wrap[0]; | |
931 const int mot_xy = s->block_index[block]; | |
932 const int block_x= mb_x*2 + (block&1); | |
933 const int block_y= mb_y*2 + (block>>1); | |
934 | |
935 const int rel_xmin4= xmin - block_x*8; | |
295 | 936 const int rel_xmax4= xmax - block_x*8 + 8; |
294 | 937 const int rel_ymin4= ymin - block_y*8; |
295 | 938 const int rel_ymax4= ymax - block_y*8 + 8; |
294 | 939 |
940 P[0][0] = s->motion_val[mot_xy ][0]; | |
941 P[0][1] = s->motion_val[mot_xy ][1]; | |
942 P[1][0] = s->motion_val[mot_xy - 1][0]; | |
943 P[1][1] = s->motion_val[mot_xy - 1][1]; | |
944 if(P[1][0] > (rel_xmax4<<shift)) P[1][0]= (rel_xmax4<<shift); | |
945 | |
946 /* special case for first line */ | |
947 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) { | |
948 P[4][0] = P[1][0]; | |
949 P[4][1] = P[1][1]; | |
950 } else { | |
951 P[2][0] = s->motion_val[mot_xy - mot_stride ][0]; | |
952 P[2][1] = s->motion_val[mot_xy - mot_stride ][1]; | |
953 P[3][0] = s->motion_val[mot_xy - mot_stride + off[block]][0]; | |
954 P[3][1] = s->motion_val[mot_xy - mot_stride + off[block]][1]; | |
955 if(P[2][1] > (rel_ymax4<<shift)) P[2][1]= (rel_ymax4<<shift); | |
956 if(P[3][0] < (rel_xmin4<<shift)) P[3][0]= (rel_xmin4<<shift); | |
295 | 957 if(P[3][0] > (rel_xmax4<<shift)) P[3][0]= (rel_xmax4<<shift); |
294 | 958 if(P[3][1] > (rel_ymax4<<shift)) P[3][1]= (rel_ymax4<<shift); |
959 | |
960 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]); | |
961 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]); | |
962 } | |
963 if(s->out_format == FMT_H263){ | |
964 pred_x4 = P[4][0]; | |
965 pred_y4 = P[4][1]; | |
966 }else { /* mpeg1 at least */ | |
967 pred_x4= P[1][0]; | |
968 pred_y4= P[1][1]; | |
969 } | |
970 P[5][0]= mx - mb_x*16; | |
971 P[5][1]= my - mb_y*16; | |
972 | |
324 | 973 dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, ref_picture); |
294 | 974 |
975 halfpel_motion_search4(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, | |
324 | 976 pred_x4, pred_y4, block_x, block_y, ref_picture); |
294 | 977 |
978 s->motion_val[ s->block_index[block] ][0]= mx4; | |
979 s->motion_val[ s->block_index[block] ][1]= my4; | |
980 } | |
981 } | |
0 | 982 |
983 /* intra / predictive decision */ | |
984 xx = mb_x * 16; | |
985 yy = mb_y * 16; | |
986 | |
987 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
988 /* At this point (mx,my) are full-pell and the absolute displacement */ | |
324 | 989 ppix = ref_picture + (my * s->linesize) + mx; |
289
648e9245546d
seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents:
288
diff
changeset
|
990 |
0 | 991 sum = pix_sum(pix, s->linesize); |
289
648e9245546d
seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents:
288
diff
changeset
|
992 #if 0 |
648e9245546d
seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents:
288
diff
changeset
|
993 varc = pix_dev(pix, s->linesize, (sum+128)>>8) + INTER_BIAS; |
294 | 994 vard = pix_abs16x16(pix, ppix, s->linesize); |
289
648e9245546d
seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents:
288
diff
changeset
|
995 #else |
648e9245546d
seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents:
288
diff
changeset
|
996 sum= (sum+8)>>4; |
648e9245546d
seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents:
288
diff
changeset
|
997 varc = ((pix_norm1(pix, s->linesize) - sum*sum + 128 + 500)>>8); |
648e9245546d
seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents:
288
diff
changeset
|
998 vard = (pix_norm(pix, ppix, s->linesize)+128)>>8; |
648e9245546d
seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents:
288
diff
changeset
|
999 #endif |
0 | 1000 |
239 | 1001 s->mb_var[s->mb_width * mb_y + mb_x] = varc; |
294 | 1002 s->avg_mb_var+= varc; |
268 | 1003 s->mc_mb_var += vard; |
284 | 1004 |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1005 |
0 | 1006 #if 0 |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1007 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
|
1008 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy); |
0 | 1009 #endif |
294 | 1010 if(s->flags&CODEC_FLAG_HQ){ |
1011 if (vard*2 + 200 > varc) | |
1012 mb_type|= MB_TYPE_INTRA; | |
1013 if (varc*2 + 200 > vard){ | |
1014 mb_type|= MB_TYPE_INTER; | |
324 | 1015 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture); |
304 | 1016 }else{ |
1017 mx = mx*2 - mb_x*32; | |
1018 my = my*2 - mb_y*32; | |
0 | 1019 } |
294 | 1020 }else{ |
1021 if (vard <= 64 || vard < varc) { | |
1022 mb_type|= MB_TYPE_INTER; | |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1023 if (s->me_method != ME_ZERO) { |
324 | 1024 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture); |
294 | 1025 } else { |
1026 mx -= 16 * mb_x; | |
1027 my -= 16 * mb_y; | |
1028 } | |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1029 #if 0 |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1030 if (vard < 10) { |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1031 skip++; |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1032 fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d", |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1033 skip, vard, varc, dmin); |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1034 } |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1035 #endif |
294 | 1036 }else{ |
1037 mb_type|= MB_TYPE_INTRA; | |
1038 mx = 0;//mx*2 - 32 * mb_x; | |
1039 my = 0;//my*2 - 32 * mb_y; | |
1040 } | |
1041 } | |
284 | 1042 |
294 | 1043 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; |
324 | 1044 set_p_mv_tables(s, mx, my); |
0 | 1045 } |
1046 | |
324 | 1047 void ff_estimate_motion_b(MpegEncContext * s, |
1048 int mb_x, int mb_y, int16_t (*mv_table)[2], uint8_t *ref_picture, int f_code) | |
0 | 1049 { |
324 | 1050 UINT8 *pix, *ppix; |
1051 int sum, varc, vard, mx, my, range, dmin, xx, yy; | |
1052 int xmin, ymin, xmax, ymax; | |
1053 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; | |
1054 int pred_x=0, pred_y=0; | |
1055 int P[6][2]; | |
1056 const int shift= 1+s->quarter_sample; | |
1057 int mb_type=0; | |
1058 const int mot_stride = s->mb_width + 2; | |
1059 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1; | |
1060 | |
1061 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code); | |
1062 | |
1063 switch(s->me_method) { | |
1064 case ME_ZERO: | |
1065 default: | |
1066 no_motion_search(s, &mx, &my); | |
1067 dmin = 0; | |
1068 break; | |
1069 case ME_FULL: | |
1070 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); | |
1071 break; | |
1072 case ME_LOG: | |
1073 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | |
1074 break; | |
1075 case ME_PHODS: | |
1076 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | |
1077 break; | |
1078 case ME_X1: | |
1079 case ME_EPZS: | |
1080 { | |
0 | 1081 |
324 | 1082 rel_xmin= xmin - mb_x*16; |
1083 rel_xmax= xmax - mb_x*16; | |
1084 rel_ymin= ymin - mb_y*16; | |
1085 rel_ymax= ymax - mb_y*16; | |
1086 | |
1087 P[0][0] = mv_table[mot_xy ][0]; | |
1088 P[0][1] = mv_table[mot_xy ][1]; | |
1089 P[1][0] = mv_table[mot_xy - 1][0]; | |
1090 P[1][1] = mv_table[mot_xy - 1][1]; | |
1091 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift); | |
1092 | |
1093 /* special case for first line */ | |
1094 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) { | |
1095 P[4][0] = P[1][0]; | |
1096 P[4][1] = P[1][1]; | |
1097 } else { | |
1098 P[2][0] = mv_table[mot_xy - mot_stride ][0]; | |
1099 P[2][1] = mv_table[mot_xy - mot_stride ][1]; | |
1100 P[3][0] = mv_table[mot_xy - mot_stride + 1 ][0]; | |
1101 P[3][1] = mv_table[mot_xy - mot_stride + 1 ][1]; | |
1102 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift); | |
1103 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift); | |
1104 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift); | |
1105 | |
1106 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]); | |
1107 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]); | |
1108 } | |
1109 pred_x= P[1][0]; | |
1110 pred_y= P[1][1]; | |
1111 } | |
1112 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture); | |
1113 | |
1114 mx+= mb_x*16; | |
1115 my+= mb_y*16; | |
1116 break; | |
1117 } | |
1118 | |
1119 /* intra / predictive decision */ | |
1120 // xx = mb_x * 16; | |
1121 // yy = mb_y * 16; | |
0 | 1122 |
324 | 1123 // pix = s->new_picture[0] + (yy * s->linesize) + xx; |
1124 /* At this point (mx,my) are full-pell and the absolute displacement */ | |
1125 // ppix = ref_picture + (my * s->linesize) + mx; | |
1126 | |
1127 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture); | |
1128 | |
1129 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; | |
1130 mv_table[mot_xy][0]= mx; | |
1131 mv_table[mot_xy][1]= my; | |
1132 } | |
1133 | |
1134 | |
1135 int ff_decide_type(MpegEncContext * s, | |
1136 int mb_x, int mb_y) | |
1137 { | |
1138 | |
1139 } | |
1140 | |
1141 void ff_estimate_b_frame_motion(MpegEncContext * s, | |
1142 int mb_x, int mb_y) | |
1143 { | |
1144 const int mot_stride = s->mb_width + 2; | |
1145 const int xy = (mb_y + 1)*mot_stride + mb_x + 1; | |
1146 | |
1147 ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, s->last_picture[0], s->f_code); | |
1148 ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, s->next_picture[0], s->b_code); | |
1149 //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]); | |
1150 s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0]; | |
1151 s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1]; | |
1152 s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0]; | |
1153 s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1]; | |
1154 | |
1155 s->mb_type[mb_y*s->mb_width + mb_x]= MB_TYPE_FORWARD; //FIXME | |
1156 } | |
0 | 1157 |
324 | 1158 /* find best f_code for ME which do unlimited searches */ |
1159 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) | |
1160 { | |
1161 int f_code; | |
1162 | |
1163 if(s->me_method>=ME_EPZS){ | |
1164 int mv_num[8]; | |
1165 int i, y; | |
1166 int loose=0; | |
1167 UINT8 * fcode_tab= s->fcode_tab; | |
1168 | |
1169 for(i=0; i<8; i++) mv_num[i]=0; | |
1170 | |
1171 for(y=0; y<s->mb_height; y++){ | |
1172 int x; | |
1173 int xy= (y+1)* (s->mb_width+2) + 1; | |
1174 i= y*s->mb_width; | |
1175 for(x=0; x<s->mb_width; x++){ | |
1176 if(s->mb_type[i] & type){ | |
1177 mv_num[ fcode_tab[mv_table[xy][0] + MAX_MV] ]++; | |
1178 mv_num[ fcode_tab[mv_table[xy][1] + MAX_MV] ]++; | |
1179 //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i); | |
1180 } | |
1181 i++; | |
1182 xy++; | |
1183 } | |
1184 } | |
1185 | |
1186 for(i=MAX_FCODE; i>1; i--){ | |
1187 loose+= mv_num[i]; | |
1188 if(loose > s->mb_num/20) break; //FIXME this is pretty ineffective | |
1189 } | |
1190 // printf("fcode: %d type: %d\n", i, s->pict_type); | |
1191 return i; | |
1192 /* for(i=0; i<=MAX_FCODE; i++){ | |
1193 printf("%d ", mv_num[i]); | |
1194 } | |
1195 printf("\n");*/ | |
1196 }else{ | |
1197 return 1; | |
0 | 1198 } |
1199 } | |
1200 | |
324 | 1201 void ff_fix_long_p_mvs(MpegEncContext * s) |
1202 { | |
1203 const int f_code= s->f_code; | |
1204 int y; | |
1205 UINT8 * fcode_tab= s->fcode_tab; | |
1206 | |
1207 /* clip / convert to intra 16x16 type MVs */ | |
1208 for(y=0; y<s->mb_height; y++){ | |
1209 int x; | |
1210 int xy= (y+1)* (s->mb_width+2)+1; | |
1211 int i= y*s->mb_width; | |
1212 for(x=0; x<s->mb_width; x++){ | |
1213 if(s->mb_type[i]&MB_TYPE_INTER){ | |
1214 if( fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code | |
1215 || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0 | |
1216 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code | |
1217 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){ | |
1218 s->mb_type[i] &= ~MB_TYPE_INTER; | |
1219 s->mb_type[i] |= MB_TYPE_INTRA; | |
1220 s->p_mv_table[xy][0] = 0; | |
1221 s->p_mv_table[xy][1] = 0; | |
1222 } | |
1223 } | |
1224 xy++; | |
1225 i++; | |
1226 } | |
1227 } | |
1228 | |
1229 if(s->flags&CODEC_FLAG_4MV){ | |
1230 const int wrap= 2+ s->mb_width*2; | |
1231 | |
1232 /* clip / convert to intra 8x8 type MVs */ | |
1233 for(y=0; y<s->mb_height; y++){ | |
1234 int xy= (y*2 + 1)*wrap + 1; | |
1235 int i= y*s->mb_width; | |
1236 int x; | |
1237 | |
1238 for(x=0; x<s->mb_width; x++){ | |
1239 if(s->mb_type[i]&MB_TYPE_INTER4V){ | |
1240 int block; | |
1241 for(block=0; block<4; block++){ | |
1242 int off= (block& 1) + (block>>1)*wrap; | |
1243 int mx= s->motion_val[ xy + off ][0]; | |
1244 int my= s->motion_val[ xy + off ][1]; | |
1245 | |
1246 if( fcode_tab[mx + MAX_MV] > f_code | |
1247 || fcode_tab[mx + MAX_MV] == 0 | |
1248 || fcode_tab[my + MAX_MV] > f_code | |
1249 || fcode_tab[my + MAX_MV] == 0 ){ | |
1250 s->mb_type[i] &= ~MB_TYPE_INTER4V; | |
1251 s->mb_type[i] |= MB_TYPE_INTRA; | |
1252 } | |
1253 } | |
1254 xy+=2; | |
1255 i++; | |
1256 } | |
1257 } | |
1258 } | |
1259 } | |
1260 } | |
1261 | |
1262 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type) | |
1263 { | |
1264 int y; | |
1265 UINT8 * fcode_tab= s->fcode_tab; | |
1266 | |
1267 /* clip / convert to intra 16x16 type MVs */ | |
1268 for(y=0; y<s->mb_height; y++){ | |
1269 int x; | |
1270 int xy= (y+1)* (s->mb_width+2)+1; | |
1271 int i= y*s->mb_width; | |
1272 for(x=0; x<s->mb_width; x++){ | |
1273 if(s->mb_type[i]&type){ | |
1274 if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code | |
1275 || fcode_tab[mv_table[xy][0] + MAX_MV] == 0 | |
1276 || fcode_tab[mv_table[xy][1] + MAX_MV] > f_code | |
1277 || fcode_tab[mv_table[xy][1] + MAX_MV] == 0 ){ | |
1278 s->mb_type[i] &= ~type; | |
1279 if(s->mb_type[i]==0) s->mb_type[i]= MB_TYPE_FORWARD; //FIXME | |
1280 mv_table[xy][0] = 0; | |
1281 mv_table[xy][1] = 0; | |
1282 //this is certainly bad FIXME | |
1283 } | |
1284 } | |
1285 xy++; | |
1286 i++; | |
1287 } | |
1288 } | |
1289 } |