annotate motion_est.c @ 281:1fc96b02142e libavcodec

mpeg4 aspect_ratio_info in AVCodecContext (requested by alex) experimental (& faster) motion estimation squished a dirty uninitialized var bug mpeg1 fcode>1 support
author michaelni
date Fri, 22 Mar 2002 23:22:08 +0000
parents 3dc1ca4ba717
children 0778d4e1d584
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * Motion estimation
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2000,2001 Gerard Lantau.
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
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
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 #include "dsputil.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 #include "mpegvideo.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
27
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 static void halfpel_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
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
986e461dc072 Initial revision
glantau
parents:
diff changeset
32
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 /* config it to test motion vector encoding (send random vectors) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
34 //#define CONFIG_TEST_MV_ENCODE
986e461dc072 Initial revision
glantau
parents:
diff changeset
35
986e461dc072 Initial revision
glantau
parents:
diff changeset
36 static int pix_sum(UINT8 * pix, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
39
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 s += pix[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 s += pix[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 s += pix[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 s += pix[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 s += pix[4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 s += pix[5];
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 s += pix[6];
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 s += pix[7];
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 pix += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 pix += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
57
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 static int pix_norm1(UINT8 * pix, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 UINT32 *sq = squareTbl + 256;
986e461dc072 Initial revision
glantau
parents:
diff changeset
62
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 s += sq[pix[0]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 s += sq[pix[1]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 s += sq[pix[2]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 s += sq[pix[3]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 s += sq[pix[4]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 s += sq[pix[5]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 s += sq[pix[6]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 s += sq[pix[7]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 pix += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 pix += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
80
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 UINT32 *sq = squareTbl + 256;
986e461dc072 Initial revision
glantau
parents:
diff changeset
85
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 s += sq[pix1[0] - pix2[0]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 s += sq[pix1[1] - pix2[1]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 s += sq[pix1[2] - pix2[2]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 s += sq[pix1[3] - pix2[3]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 s += sq[pix1[4] - pix2[4]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 s += sq[pix1[5] - pix2[5]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 s += sq[pix1[6] - pix2[6]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 s += sq[pix1[7] - pix2[7]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 pix1 += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 pix2 += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 pix1 += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 pix2 += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
105
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 static void no_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 int *mx_ptr, int *my_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 *mx_ptr = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 *my_ptr = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
112
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 static int full_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 int *mx_ptr, int *my_ptr, int range,
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 int xmin, int ymin, int xmax, int ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 int x1, y1, x2, y2, xx, yy, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 int mx, my, dmin, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
120
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 xx = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 yy = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 x2 = xx + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 y1 = yy - range + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 y2 = yy + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 dmin = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 for (y = y1; y <= y2; y++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 for (x = x1; x <= x2; x++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x,
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 s->linesize, 16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 if (d < dmin ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 (d == dmin &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 (abs(x - xx) + abs(y - yy)) <
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 dmin = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
153
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
156
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 return dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
165
986e461dc072 Initial revision
glantau
parents:
diff changeset
166
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 static int log_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 int *mx_ptr, int *my_ptr, int range,
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 int xmin, int ymin, int xmax, int ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 int x1, y1, x2, y2, xx, yy, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 int mx, my, dmin, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
174
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 xx = s->mb_x << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 yy = s->mb_y << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
177
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 /* Left limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 x1 = xx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
182
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 /* Right limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 x2 = xx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
187
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 /* Upper limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
191 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
192
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 /* Lower limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 y2 = yy + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
197
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 dmin = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
202
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 for (y = y1; y <= y2; y += range) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 for (x = x1; x <= x2; x += range) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize, 16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 dmin = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
214
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 range = range >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
216
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 x1 = mx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
220
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 x2 = mx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
224
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 y1 = my - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
228
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 y2 = my + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
232
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 } while (range >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
234
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 return dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
242
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 static int phods_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 int *mx_ptr, int *my_ptr, int range,
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 int xmin, int ymin, int xmax, int ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 int mx, my, dminx, dminy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
250
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 xx = s->mb_x << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 yy = s->mb_y << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
253
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 /* Left limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 x1 = xx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
258
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 /* Right limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 x2 = xx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
263
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 /* Upper limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
268
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 /* Lower limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 y2 = yy + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
273
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
277
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 x = xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 y = yy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 dminx = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 dminy = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
283
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 lastx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 for (x = x1; x <= x2; x += range) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize, 16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 dminx = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
292
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 x = lastx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 for (y = y1; y <= y2; y += range) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize, 16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 dminy = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
301
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 range = range >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
303
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 x = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 y = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 x1 = mx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
309
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 x2 = mx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
313
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 y1 = my - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
317
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 y2 = my + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
321
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 } while (range >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
323
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
327
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 /* half pixel search */
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 return dminy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
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)\
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
338 {\
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
339 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
340 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
341 if(d<dmin){\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
342 best[0]=x;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
343 best[1]=y;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
344 dmin=d;\
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
345 }\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
346 }
277
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 #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
349 {\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
350 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
351 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
352 if(d<dmin){\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
353 best[0]=x;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
354 best[1]=y;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
355 dmin=d;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
356 next_dir= new_dir;\
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 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
359
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
360 #define check(x,y,S,v)\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
361 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
362 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
363 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
364 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
365
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
366
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
367 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
368 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
369 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
370 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
371 {
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
372 int next_dir=-1;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
373
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
374 for(;;){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
375 int d;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
376 const int dir= next_dir;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
377 const int x= best[0];
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
378 const int y= best[1];
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
379 next_dir=-1;
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 //printf("%d", dir);
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
382 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
383 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
384 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
385 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
386
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
387 if(next_dir==-1){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
388 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
389 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
390 }
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
391
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
392 /* for(;;){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
393 int d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
394 const int x= best[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
395 const int y= best[1];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
396 const int last_min=dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
397 if(x>xmin) CHECK_MV(x-1, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
398 if(y>xmin) CHECK_MV(x , y-1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
399 if(x<xmax) CHECK_MV(x+1, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
400 if(y<xmax) CHECK_MV(x , y+1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
401 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
402 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
403 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
404 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
405 if(x-1>xmin) CHECK_MV(x-2, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
406 if(y-1>xmin) CHECK_MV(x , y-2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
407 if(x+1<xmax) CHECK_MV(x+2, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
408 if(y+1<xmax) CHECK_MV(x , y+2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
409 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
410 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
411 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
412 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
413 if(dmin==last_min) return dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
414 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
415 */
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
416 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
417
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
418 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
419 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
420 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
421 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
422 {
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
423 int dir=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
424 int c=1;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
425 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
426 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
427 int fails=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
428 int last_d[2]={dmin, dmin};
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
429
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
430 /*static int good=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
431 static int bad=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
432 static int point=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
433
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
434 point++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
435 if(256*256*256*64%point==0)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
436 {
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
437 printf("%d %d %d\n", good, bad, point);
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
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
440 for(;;){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
441 int x= best[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
442 int y= best[1];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
443 int d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
444 x+=x_dir[dir];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
445 y+=y_dir[dir];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
446 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
447 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride, 16);
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
448 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
449 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
450 d = dmin + 10000; //FIXME smarter boundary handling
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
451 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
452 if(d<dmin){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
453 best[0]=x;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
454 best[1]=y;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
455 dmin=d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
456
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
457 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
458 dir+=c;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
459
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
460 fails=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
461 //good++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
462 last_d[1]=last_d[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
463 last_d[0]=d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
464 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
465 //bad++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
466 if(fails){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
467 if(fails>=3) return dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
468 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
469 c= -c;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
470 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
471 dir+=c*2;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
472 fails++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
473 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
474 dir&=7;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
475 }
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
476 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
477
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
478 static int epzs_motion_search(MpegEncContext * s,
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
479 int *mx_ptr, int *my_ptr,
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
480 int P[5][2], int pred_x, int pred_y,
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
481 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
482 {
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
483 int best[2]={0, 0};
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
484 int d, dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
485 UINT8 *new_pic, *old_pic;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
486 const int pic_stride= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
487 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
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 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
490 const int shift= 1+s->quarter_sample;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
491
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
492 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
493 old_pic = s->last_picture[0] + pic_xy;
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
494
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
495 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
496 if(dmin<Z_THRESHOLD){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
497 *mx_ptr= 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
498 *my_ptr= 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
499 //printf("Z");
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
500 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
501 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
502
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
503 /* first line */
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
504 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
505 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
506 }else{
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
507 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
508 if(dmin<Z_THRESHOLD){
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
509 *mx_ptr= P[4][0]>>shift;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
510 *my_ptr= P[4][1]>>shift;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
511 //printf("M\n");
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
512 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
513 }
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
514 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
515 CHECK_MV(P[2][0]>>shift, P[2][1]>>shift)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
516 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
517 }
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
518 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
519
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
520 //check(best[0],best[1],0, b0)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
521 if(s->full_search==ME_EPZS)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
522 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
523 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
524 else
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
525 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
526 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
527 //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
528 *mx_ptr= best[0];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
529 *my_ptr= best[1];
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
530
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
531 // 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
532 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
533 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
534
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
535 #define CHECK_HALF_MV(suffix, x, y) \
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
536 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
537 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
538 if(d<dminh){\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
539 dminh= d;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
540 mx= mx1 + x;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
541 my= my1 + y;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
542 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
543
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
544 /* The idea would be to make half pel ME after Inter/Intra decision to
986e461dc072 Initial revision
glantau
parents:
diff changeset
545 save time. */
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
546 static inline void halfpel_motion_search(MpegEncContext * s,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
547 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
548 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
549 int pred_x, int pred_y)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 {
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
551 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
552 const int quant= s->qscale;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
553 int pen_x, pen_y;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
554 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
555 UINT8 *pix, *ptr;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
556
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
557
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
558 mx = *mx_ptr;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
559 my = *my_ptr;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
560 ptr = s->last_picture[0] + (my * s->linesize) + mx;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
561
986e461dc072 Initial revision
glantau
parents:
diff changeset
562 xx = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
563 yy = 16 * s->mb_y;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
564 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
565
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
566 dminh = dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
567
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
568 if (mx > xmin && mx < xmax &&
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
569 my > ymin && my < ymax) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
570
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
571 mx= mx1= 2*(mx - xx);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
572 my= my1= 2*(my - yy);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
573 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
574 *mx_ptr = 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
575 *my_ptr = 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
576 return;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
577 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
578
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
579 pen_x= pred_x + mx;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
580 pen_y= pred_y + my;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
581
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
582 ptr-= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
583 CHECK_HALF_MV(xy2, -1, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
584 CHECK_HALF_MV(y2 , 0, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
585 CHECK_HALF_MV(xy2, +1, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
586
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
587 ptr+= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
588 CHECK_HALF_MV(x2 , -1, 0)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
589 CHECK_HALF_MV(x2 , +1, 0)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
590 CHECK_HALF_MV(xy2, -1, +1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
591 CHECK_HALF_MV(y2 , 0, +1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
592 CHECK_HALF_MV(xy2, +1, +1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
593 }else{
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
594 mx= 2*(mx - xx);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
595 my= 2*(my - yy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
596 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
597
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
598 *mx_ptr = mx;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
599 *my_ptr = my;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
600 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
601
986e461dc072 Initial revision
glantau
parents:
diff changeset
602 #ifndef CONFIG_TEST_MV_ENCODE
986e461dc072 Initial revision
glantau
parents:
diff changeset
603
986e461dc072 Initial revision
glantau
parents:
diff changeset
604 int estimate_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
605 int mb_x, int mb_y,
986e461dc072 Initial revision
glantau
parents:
diff changeset
606 int *mx_ptr, int *my_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
608 UINT8 *pix, *ppix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 int sum, varc, vard, mx, my, range, dmin, xx, yy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
610 int xmin, ymin, xmax, ymax;
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
611 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
612 int pred_x=0, pred_y=0;
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
613 int P[5][2];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
614 const int shift= 1+s->quarter_sample;
233
3f5b72726118 - More work on preliminary bit rate control, just to be able to get an
pulento
parents: 232
diff changeset
615
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 range = 8 * (1 << (s->f_code - 1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
617 /* XXX: temporary kludge to avoid overflow for msmpeg4 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
619 range = range * 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
620
986e461dc072 Initial revision
glantau
parents:
diff changeset
621 if (s->unrestricted_mv) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
622 xmin = -16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
623 ymin = -16;
275
7ebb3f9aaf3b - Added video coding statistics for ffmpeg. Needs more work.
pulento
parents: 268
diff changeset
624 if (s->h263_plus)
7ebb3f9aaf3b - Added video coding statistics for ffmpeg. Needs more work.
pulento
parents: 268
diff changeset
625 range *= 2;
218
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
626 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
627 xmax = s->mb_width*16;
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
628 ymax = s->mb_height*16;
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
629 }else {
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
630 /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
631 (cuz the drawn edge isnt large enough))*/
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
632 xmax = s->width;
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
633 ymax = s->height;
232
b640ec5948b0 - Now the ME is done for the entire picture when enconding, the
pulento
parents: 218
diff changeset
634 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
635 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
636 xmin = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 ymin = 0;
218
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
638 xmax = s->mb_width*16 - 16;
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
639 ymax = s->mb_height*16 - 16;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
640 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
641 switch(s->full_search) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
642 case ME_ZERO:
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 no_motion_search(s, &mx, &my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
645 dmin = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
646 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 case ME_FULL:
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax);
986e461dc072 Initial revision
glantau
parents:
diff changeset
649 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
650 case ME_LOG:
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax);
986e461dc072 Initial revision
glantau
parents:
diff changeset
652 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
653 case ME_PHODS:
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax);
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 break;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
656 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
657 case ME_EPZS:
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
658 rel_xmin= xmin - s->mb_x*16;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
659 rel_xmax= xmax - s->mb_x*16;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
660 rel_ymin= ymin - s->mb_y*16;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
661 rel_ymax= ymax - s->mb_y*16;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
662 if(s->out_format == FMT_H263){
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
663 static const int off[4]= {2, 1, 1, -1};
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
664 const int mot_stride = s->mb_width*2 + 2;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
665 const int mot_xy = (s->mb_y*2 + 1)*mot_stride + s->mb_x*2 + 1;
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
666
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
667 P[0][0] = s->motion_val[mot_xy ][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
668 P[0][1] = s->motion_val[mot_xy ][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
669 P[1][0] = s->motion_val[mot_xy - 1][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
670 P[1][1] = s->motion_val[mot_xy - 1][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
671 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
672
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
673 /* special case for first line */
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
674 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
675 pred_x = P[1][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
676 pred_y = P[1][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
677 } else {
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
678 P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
679 P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
680 P[3][0] = s->motion_val[mot_xy - mot_stride + off[0] ][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
681 P[3][1] = s->motion_val[mot_xy - mot_stride + off[0] ][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
682 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
683 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
684 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
685
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
686 P[4][0]= pred_x = mid_pred(P[1][0], P[2][0], P[3][0]);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
687 P[4][1]= pred_y = mid_pred(P[1][1], P[2][1], P[3][1]);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
688 }
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
689 }else {
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
690 const int xy= s->mb_y*s->mb_width + s->mb_x;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
691 pred_x= s->last_mv[0][0][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
692 pred_y= s->last_mv[0][0][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
693
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
694 P[0][0]= s->mv_table[0][xy ];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
695 P[0][1]= s->mv_table[1][xy ];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
696 if(s->mb_x == 0){
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
697 P[1][0]= 0;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
698 P[1][1]= 0;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
699 }else{
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
700 P[1][0]= s->mv_table[0][xy-1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
701 P[1][1]= s->mv_table[1][xy-1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
702 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
703 }
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
704
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
705 if (!(s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
706 P[2][0] = s->mv_table[0][xy - s->mb_width];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
707 P[2][1] = s->mv_table[1][xy - s->mb_width];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
708 P[3][0] = s->mv_table[0][xy - s->mb_width+1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
709 P[3][1] = s->mv_table[1][xy - s->mb_width+1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
710 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
711 if(P[3][0] > (rel_xmax<<shift)) P[3][0]= (rel_xmax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
712 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
713 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
714
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
715 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
716 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
717 }
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
718 }
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
719 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax);
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
720
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
721 mx+= s->mb_x*16;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
722 my+= s->mb_y*16;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
723 break;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
724 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
725
986e461dc072 Initial revision
glantau
parents:
diff changeset
726 /* intra / predictive decision */
986e461dc072 Initial revision
glantau
parents:
diff changeset
727 xx = mb_x * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
728 yy = mb_y * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
729
986e461dc072 Initial revision
glantau
parents:
diff changeset
730 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
731 /* At this point (mx,my) are full-pell and the absolute displacement */
986e461dc072 Initial revision
glantau
parents:
diff changeset
732 ppix = s->last_picture[0] + (my * s->linesize) + mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
733
986e461dc072 Initial revision
glantau
parents:
diff changeset
734 sum = pix_sum(pix, s->linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
735 varc = pix_norm1(pix, s->linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
736 vard = pix_norm(pix, ppix, s->linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
737
986e461dc072 Initial revision
glantau
parents:
diff changeset
738 vard = vard >> 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
739 sum = sum >> 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
740 varc = (varc >> 8) - (sum * sum);
239
16cd8a9c4da4 - Minor changes on bitrate control
pulento
parents: 233
diff changeset
741 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
742 s->avg_mb_var += varc;
268
09ae29b27ed9 hopefully better bitrate controll
michaelni
parents: 239
diff changeset
743 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
744
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
745 #if 0
233
3f5b72726118 - More work on preliminary bit rate control, just to be able to get an
pulento
parents: 232
diff changeset
746 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
747 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
748 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
749 if (vard <= 64 || vard < varc) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
750 if (s->full_search != ME_ZERO) {
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
751 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
752 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
753 mx -= 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
754 my -= 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
755 }
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
756 // check(mx + 32*s->mb_x, my + 32*s->mb_y, 1, end)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
757
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
758 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
759 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
760 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
761 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
762 *mx_ptr = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
763 *my_ptr = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
764 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
765 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
766 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
767
986e461dc072 Initial revision
glantau
parents:
diff changeset
768 #else
986e461dc072 Initial revision
glantau
parents:
diff changeset
769
986e461dc072 Initial revision
glantau
parents:
diff changeset
770 /* test version which generates valid random vectors */
986e461dc072 Initial revision
glantau
parents:
diff changeset
771 int estimate_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
772 int mb_x, int mb_y,
986e461dc072 Initial revision
glantau
parents:
diff changeset
773 int *mx_ptr, int *my_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
774 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
775 int xx, yy, x1, y1, x2, y2, range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
776
986e461dc072 Initial revision
glantau
parents:
diff changeset
777 if ((random() % 10) >= 5) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
778 range = 8 * (1 << (s->f_code - 1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
779 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
780 range = range * 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
781
986e461dc072 Initial revision
glantau
parents:
diff changeset
782 xx = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
783 yy = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
784 x1 = xx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
785 if (x1 < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
786 x1 = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
787 x2 = xx + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
788 if (x2 > (s->width - 16))
986e461dc072 Initial revision
glantau
parents:
diff changeset
789 x2 = s->width - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
790 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
791 if (y1 < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
792 y1 = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
793 y2 = yy + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
794 if (y2 > (s->height - 16))
986e461dc072 Initial revision
glantau
parents:
diff changeset
795 y2 = s->height - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
796
986e461dc072 Initial revision
glantau
parents:
diff changeset
797 *mx_ptr = (random() % (2 * (x2 - x1 + 1))) + 2 * (x1 - xx);
986e461dc072 Initial revision
glantau
parents:
diff changeset
798 *my_ptr = (random() % (2 * (y2 - y1 + 1))) + 2 * (y1 - yy);
986e461dc072 Initial revision
glantau
parents:
diff changeset
799 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
800 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
801 *mx_ptr = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
802 *my_ptr = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
803 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
804 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
805 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
806
986e461dc072 Initial revision
glantau
parents:
diff changeset
807 #endif