annotate motion_est.c @ 308:072c4ff05e62 libavcodec

b frames & slices bugfix
author michaelni
date Fri, 05 Apr 2002 04:23:01 +0000
parents 5753d57e7e6b
children cda7d0857baf
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
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
28 #define ABS(a) ((a)>0 ? (a) : -(a))
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
29 #define MAX(a,b) ((a) > (b) ? (a) : (b))
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
30 #define INTER_BIAS 257
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
31
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 static void halfpel_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
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,
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
35 int pred_x, int pred_y);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
36
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 /* config it to test motion vector encoding (send random vectors) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 //#define CONFIG_TEST_MV_ENCODE
986e461dc072 Initial revision
glantau
parents:
diff changeset
39
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 static int pix_sum(UINT8 * pix, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
43
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 s += pix[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 s += pix[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 s += pix[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 s += pix[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 s += pix[4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 s += pix[5];
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 s += pix[6];
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 s += pix[7];
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 pix += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 pix += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
61
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
62 static int pix_dev(UINT8 * pix, int line_size, int mean)
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
63 {
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
64 int s, i, j;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
65
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
66 s = 0;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
67 for (i = 0; i < 16; i++) {
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
68 for (j = 0; j < 16; j += 8) {
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
69 s += ABS(pix[0]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
70 s += ABS(pix[1]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
71 s += ABS(pix[2]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
72 s += ABS(pix[3]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
73 s += ABS(pix[4]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
74 s += ABS(pix[5]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
75 s += ABS(pix[6]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
76 s += ABS(pix[7]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
77 pix += 8;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
78 }
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
79 pix += line_size - 16;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
80 }
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
81 return s;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
82 }
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
83
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 static int pix_norm1(UINT8 * pix, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 UINT32 *sq = squareTbl + 256;
986e461dc072 Initial revision
glantau
parents:
diff changeset
88
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 s += sq[pix[0]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 s += sq[pix[1]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 s += sq[pix[2]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 s += sq[pix[3]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 s += sq[pix[4]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 s += sq[pix[5]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 s += sq[pix[6]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 s += sq[pix[7]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 pix += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 pix += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
106
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 UINT32 *sq = squareTbl + 256;
986e461dc072 Initial revision
glantau
parents:
diff changeset
111
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 s += sq[pix1[0] - pix2[0]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 s += sq[pix1[1] - pix2[1]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 s += sq[pix1[2] - pix2[2]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 s += sq[pix1[3] - pix2[3]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 s += sq[pix1[4] - pix2[4]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 s += sq[pix1[5] - pix2[5]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 s += sq[pix1[6] - pix2[6]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 s += sq[pix1[7] - pix2[7]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 pix1 += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 pix2 += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 pix1 += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 pix2 += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
131
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 static void no_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 int *mx_ptr, int *my_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 *mx_ptr = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 *my_ptr = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
138
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 static int full_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 int *mx_ptr, int *my_ptr, int range,
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 int xmin, int ymin, int xmax, int ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 int x1, y1, x2, y2, xx, yy, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 int mx, my, dmin, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
146
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 xx = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 yy = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 x2 = xx + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 y1 = yy - range + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 y2 = yy + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 dmin = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 for (y = y1; y <= y2; y++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 for (x = x1; x <= x2; x++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x,
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
168 s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 if (d < dmin ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 (d == dmin &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 (abs(x - xx) + abs(y - yy)) <
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 dmin = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
179
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
182
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 return dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
191
986e461dc072 Initial revision
glantau
parents:
diff changeset
192
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 static int log_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 int *mx_ptr, int *my_ptr, int range,
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 int xmin, int ymin, int xmax, int ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 int x1, y1, x2, y2, xx, yy, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 int mx, my, dmin, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
200
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 xx = s->mb_x << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 yy = s->mb_y << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
203
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 /* Left limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 x1 = xx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
208
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 /* Right limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 x2 = xx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
213
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 /* Upper limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
218
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 /* Lower limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 y2 = yy + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
223
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 dmin = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
228
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 for (y = y1; y <= y2; y += range) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 for (x = x1; x <= x2; x += range) {
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
232 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 dmin = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
240
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 range = range >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
242
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 x1 = mx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
246
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 x2 = mx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
250
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 y1 = my - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
253 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
254
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 y2 = my + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
258
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 } while (range >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
260
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 return dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
268
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 static int phods_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 int *mx_ptr, int *my_ptr, int range,
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 int xmin, int ymin, int xmax, int ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 int mx, my, dminx, dminy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
276
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 xx = s->mb_x << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 yy = s->mb_y << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
279
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 /* Left limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 x1 = xx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
284
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 /* Right limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 x2 = xx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
289
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 /* Upper limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
294
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 /* Lower limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 y2 = yy + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
299
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
303
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 x = xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 y = yy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 dminx = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 dminy = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
309
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 lastx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 for (x = x1; x <= x2; x += range) {
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
312 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 dminx = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
318
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 x = lastx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 for (y = y1; y <= y2; y += range) {
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
321 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 dminy = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
327
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 range = range >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
329
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 x = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 y = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 x1 = mx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
335
986e461dc072 Initial revision
glantau
parents:
diff changeset
336 x2 = mx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
339
986e461dc072 Initial revision
glantau
parents:
diff changeset
340 y1 = my - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
343
986e461dc072 Initial revision
glantau
parents:
diff changeset
344 y2 = my + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
347
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 } while (range >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
349
986e461dc072 Initial revision
glantau
parents:
diff changeset
350 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
351 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
353
986e461dc072 Initial revision
glantau
parents:
diff changeset
354 /* half pixel search */
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 return dminy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
359
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
360
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
361 #define Z_THRESHOLD 256
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
362
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
363 #define CHECK_MV(x,y)\
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
364 {\
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
365 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
366 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
367 if(d<dmin){\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
368 best[0]=x;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
369 best[1]=y;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
370 dmin=d;\
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
371 }\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
372 }
277
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 #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
375 {\
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
376 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
377 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
378 if(d<dmin){\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
379 best[0]=x;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
380 best[1]=y;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
381 dmin=d;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
382 next_dir= new_dir;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
383 }\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
384 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
385
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
386 #define CHECK_MV4(x,y)\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
387 {\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
388 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
389 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
390 if(d<dmin){\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
391 best[0]=x;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
392 best[1]=y;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
393 dmin=d;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
394 }\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
395 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
396
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
397 #define CHECK_MV4_DIR(x,y,new_dir)\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
398 {\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
399 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
400 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
401 if(d<dmin){\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
402 best[0]=x;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
403 best[1]=y;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
404 dmin=d;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
405 next_dir= new_dir;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
406 }\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
407 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
408
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
409
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
410 #define check(x,y,S,v)\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
411 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
412 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
413 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
414 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
415
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
416
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
417 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
418 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
419 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
420 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
421 {
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
422 int next_dir=-1;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
423
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
424 for(;;){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
425 int d;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
426 const int dir= next_dir;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
427 const int x= best[0];
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
428 const int y= best[1];
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
429 next_dir=-1;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
430
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
431 //printf("%d", dir);
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
432 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
433 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
434 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
435 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
436
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
437 if(next_dir==-1){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
438 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
439 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
440 }
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
441
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
442 /* for(;;){
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 const int x= best[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
445 const int y= best[1];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
446 const int last_min=dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
447 if(x>xmin) CHECK_MV(x-1, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
448 if(y>xmin) CHECK_MV(x , y-1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
449 if(x<xmax) CHECK_MV(x+1, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
450 if(y<xmax) CHECK_MV(x , y+1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
451 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
452 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
453 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
454 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
455 if(x-1>xmin) CHECK_MV(x-2, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
456 if(y-1>xmin) CHECK_MV(x , y-2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
457 if(x+1<xmax) CHECK_MV(x+2, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
458 if(y+1<xmax) CHECK_MV(x , y+2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
459 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
460 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
461 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
462 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
463 if(dmin==last_min) return dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
464 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
465 */
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
466 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
467
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
468 static inline int small_diamond_search4MV(MpegEncContext * s, int *best, int dmin,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
469 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
470 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
471 int xmin, int ymin, int xmax, int ymax, int shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
472 {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
473 int next_dir=-1;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
474
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
475 for(;;){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
476 int d;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
477 const int dir= next_dir;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
478 const int x= best[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
479 const int y= best[1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
480 next_dir=-1;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
481
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
482 //printf("%d", dir);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
483 if(dir!=2 && x>xmin) CHECK_MV4_DIR(x-1, y , 0)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
484 if(dir!=3 && y>ymin) CHECK_MV4_DIR(x , y-1, 1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
485 if(dir!=0 && x<xmax) CHECK_MV4_DIR(x+1, y , 2)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
486 if(dir!=1 && y<ymax) CHECK_MV4_DIR(x , y+1, 3)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
487
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
488 if(next_dir==-1){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
489 return dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
490 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
491 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
492 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
493
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
494 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
495 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
496 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
497 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
498 {
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
499 int dir=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
500 int c=1;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
501 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
502 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
503 int fails=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
504 int last_d[2]={dmin, dmin};
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
505
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
506 /*static int good=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
507 static int bad=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
508 static int 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 point++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
511 if(256*256*256*64%point==0)
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 printf("%d %d %d\n", good, bad, point);
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
514 }*/
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
515
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
516 for(;;){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
517 int x= best[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
518 int y= best[1];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
519 int d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
520 x+=x_dir[dir];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
521 y+=y_dir[dir];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
522 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
523 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
524 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
525 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
526 d = dmin + 10000; //FIXME smarter boundary handling
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
527 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
528 if(d<dmin){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
529 best[0]=x;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
530 best[1]=y;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
531 dmin=d;
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 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
534 dir+=c;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
535
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
536 fails=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
537 //good++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
538 last_d[1]=last_d[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
539 last_d[0]=d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
540 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
541 //bad++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
542 if(fails){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
543 if(fails>=3) return dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
544 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
545 c= -c;
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+=c*2;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
548 fails++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
549 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
550 dir&=7;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
551 }
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
552 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
553
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
554 static int epzs_motion_search(MpegEncContext * s,
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
555 int *mx_ptr, int *my_ptr,
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
556 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
557 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
558 {
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
559 int best[2]={0, 0};
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
560 int d, dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
561 UINT8 *new_pic, *old_pic;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
562 const int pic_stride= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
563 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
564 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
565 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
566 const int shift= 1+s->quarter_sample;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
567
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
568 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
569 old_pic = s->last_picture[0] + pic_xy;
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
570
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
571 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
572 if(dmin<Z_THRESHOLD){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
573 *mx_ptr= 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
574 *my_ptr= 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
575 //printf("Z");
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
576 return dmin;
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
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
579 /* first line */
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
580 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
581 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
582 }else{
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
583 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
584 if(dmin<Z_THRESHOLD){
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
585 *mx_ptr= P[4][0]>>shift;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
586 *my_ptr= P[4][1]>>shift;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
587 //printf("M\n");
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
588 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
589 }
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
590 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
591 CHECK_MV(P[2][0]>>shift, P[2][1]>>shift)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
592 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
593 }
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
594 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
595
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
596 //check(best[0],best[1],0, b0)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
597 if(s->full_search==ME_EPZS)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
598 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
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 else
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
601 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
602 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
603 //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
604 *mx_ptr= best[0];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
605 *my_ptr= best[1];
277
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 // 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
608 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
609 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
610
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
611 static int epzs_motion_search4(MpegEncContext * s, int block,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
612 int *mx_ptr, int *my_ptr,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
613 int P[6][2], int pred_x, int pred_y,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
614 int xmin, int ymin, int xmax, int ymax)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
615 {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
616 int best[2]={0, 0};
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
617 int d, dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
618 UINT8 *new_pic, *old_pic;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
619 const int pic_stride= s->linesize;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
620 const int pic_xy= ((s->mb_y*2 + (block>>1))*pic_stride + s->mb_x*2 + (block&1))*8;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
621 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
622 int quant= s->qscale; // qscale of the prev frame
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
623 const int shift= 1+s->quarter_sample;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
624
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
625 new_pic = s->new_picture[0] + pic_xy;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
626 old_pic = s->last_picture[0] + pic_xy;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
627
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
628 dmin = pix_abs8x8(new_pic, old_pic, pic_stride);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
629
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
630 /* first line */
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
631 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
632 CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
633 }else{
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
634 CHECK_MV4(P[4][0]>>shift, P[4][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
635 if(dmin<Z_THRESHOLD){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
636 *mx_ptr= P[4][0]>>shift;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
637 *my_ptr= P[4][1]>>shift;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
638 //printf("M\n");
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
639 return dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
640 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
641 CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
642 CHECK_MV4(P[2][0]>>shift, P[2][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
643 CHECK_MV4(P[3][0]>>shift, P[3][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
644 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
645 CHECK_MV4(P[0][0]>>shift, P[0][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
646 CHECK_MV4(P[5][0]>>shift, P[5][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
647
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
648 //check(best[0],best[1],0, b0)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
649 dmin= small_diamond_search4MV(s, best, dmin, new_pic, old_pic, pic_stride,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
650 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
651 //check(best[0],best[1],0, b1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
652 *mx_ptr= best[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
653 *my_ptr= best[1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
654
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
655 // printf("%d %d %d \n", best[0], best[1], dmin);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
656 return dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
657 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
658
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
659 #define CHECK_HALF_MV(suffix, x, y) \
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
660 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
661 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
662 if(d<dminh){\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
663 dminh= d;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
664 mx= mx1 + x;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
665 my= my1 + y;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
666 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
667
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
668 #define CHECK_HALF_MV4(suffix, x, y) \
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
669 d= pix_abs8x8_ ## suffix(pix, ptr+((x)>>1), s->linesize);\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
670 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
671 if(d<dminh){\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
672 dminh= d;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
673 mx= mx1 + x;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
674 my= my1 + y;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
675 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
676
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
677 /* The idea would be to make half pel ME after Inter/Intra decision to
986e461dc072 Initial revision
glantau
parents:
diff changeset
678 save time. */
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
679 static inline void halfpel_motion_search(MpegEncContext * s,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 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
681 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
682 int pred_x, int pred_y)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
683 {
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
684 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
685 const int quant= s->qscale;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
686 int pen_x, pen_y;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
687 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
688 UINT8 *pix, *ptr;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
689
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
690 mx = *mx_ptr;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
691 my = *my_ptr;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
692 ptr = s->last_picture[0] + (my * s->linesize) + mx;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
693
986e461dc072 Initial revision
glantau
parents:
diff changeset
694 xx = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
695 yy = 16 * s->mb_y;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
696 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
697
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
698 dminh = dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
699
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
700 if (mx > xmin && mx < xmax &&
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
701 my > ymin && my < ymax) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
702
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
703 mx= mx1= 2*(mx - xx);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
704 my= my1= 2*(my - yy);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
705 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
706 *mx_ptr = 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
707 *my_ptr = 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
708 return;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
709 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
710
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
711 pen_x= pred_x + mx;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
712 pen_y= pred_y + my;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
713
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
714 ptr-= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
715 CHECK_HALF_MV(xy2, -1, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
716 CHECK_HALF_MV(y2 , 0, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
717 CHECK_HALF_MV(xy2, +1, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
718
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
719 ptr+= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
720 CHECK_HALF_MV(x2 , -1, 0)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
721 CHECK_HALF_MV(x2 , +1, 0)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
722 CHECK_HALF_MV(xy2, -1, +1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
723 CHECK_HALF_MV(y2 , 0, +1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
724 CHECK_HALF_MV(xy2, +1, +1)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
725
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
726 }else{
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
727 mx= 2*(mx - xx);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
728 my= 2*(my - yy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
729 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
730
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
731 *mx_ptr = mx;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
732 *my_ptr = my;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
733 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
734
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
735 static inline void halfpel_motion_search4(MpegEncContext * s,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
736 int *mx_ptr, int *my_ptr, int dmin,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
737 int xmin, int ymin, int xmax, int ymax,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
738 int pred_x, int pred_y, int block_x, int block_y)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
739 {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
740 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
741 const int quant= s->qscale;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
742 int pen_x, pen_y;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
743 int mx, my, mx1, my1, d, xx, yy, dminh;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
744 UINT8 *pix, *ptr;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
745
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
746 xx = 8 * block_x;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
747 yy = 8 * block_y;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
748 pix = s->new_picture[0] + (yy * s->linesize) + xx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
749
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
750 mx = *mx_ptr;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
751 my = *my_ptr;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
752 ptr = s->last_picture[0] + ((yy+my) * s->linesize) + xx + mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
753
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
754 dminh = dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
755
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
756 if (mx > xmin && mx < xmax &&
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
757 my > ymin && my < ymax) {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
758
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
759 mx= mx1= 2*mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
760 my= my1= 2*my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
761 if(dmin < Z_THRESHOLD && mx==0 && my==0){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
762 *mx_ptr = 0;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
763 *my_ptr = 0;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
764 return;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
765 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
766
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
767 pen_x= pred_x + mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
768 pen_y= pred_y + my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
769
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
770 ptr-= s->linesize;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
771 CHECK_HALF_MV4(xy2, -1, -1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
772 CHECK_HALF_MV4(y2 , 0, -1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
773 CHECK_HALF_MV4(xy2, +1, -1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
774
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
775 ptr+= s->linesize;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
776 CHECK_HALF_MV4(x2 , -1, 0)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
777 CHECK_HALF_MV4(x2 , +1, 0)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
778 CHECK_HALF_MV4(xy2, -1, +1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
779 CHECK_HALF_MV4(y2 , 0, +1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
780 CHECK_HALF_MV4(xy2, +1, +1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
781
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
782 }else{
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
783 mx*=2;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
784 my*=2;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
785 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
786
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
787 *mx_ptr = mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
788 *my_ptr = my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
789 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
790
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
791 static inline void set_mv_tables(MpegEncContext * s, int mx, int my)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
792 {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
793 const int xy= s->mb_x + s->mb_y*s->mb_width;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
794
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
795 s->mv_table[0][xy] = mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
796 s->mv_table[1][xy] = my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
797
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
798 /* has allready been set to the 4 MV if 4MV is done */
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
799 if(!(s->flags&CODEC_FLAG_4MV)){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
800 int mot_xy= s->block_index[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
801
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
802 s->motion_val[mot_xy ][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
803 s->motion_val[mot_xy ][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
804 s->motion_val[mot_xy+1][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
805 s->motion_val[mot_xy+1][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
806
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
807 mot_xy += s->block_wrap[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
808 s->motion_val[mot_xy ][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
809 s->motion_val[mot_xy ][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
810 s->motion_val[mot_xy+1][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
811 s->motion_val[mot_xy+1][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
812 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
813 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
814
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
815 #ifndef CONFIG_TEST_MV_ENCODE
986e461dc072 Initial revision
glantau
parents:
diff changeset
816
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
817 void estimate_motion(MpegEncContext * s,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
818 int mb_x, int mb_y)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
819 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
820 UINT8 *pix, *ppix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
821 int sum, varc, vard, mx, my, range, dmin, xx, yy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
822 int xmin, ymin, xmax, ymax;
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
823 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
824 int pred_x=0, pred_y=0;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
825 int P[6][2];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
826 const int shift= 1+s->quarter_sample;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
827 int mb_type=0;
233
3f5b72726118 - More work on preliminary bit rate control, just to be able to get an
pulento
parents: 232
diff changeset
828
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
829 range = 8 * (1 << (s->f_code - 1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
830 /* XXX: temporary kludge to avoid overflow for msmpeg4 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
831 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
832 range = range * 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
833
986e461dc072 Initial revision
glantau
parents:
diff changeset
834 if (s->unrestricted_mv) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
835 xmin = -16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
836 ymin = -16;
275
7ebb3f9aaf3b - Added video coding statistics for ffmpeg. Needs more work.
pulento
parents: 268
diff changeset
837 if (s->h263_plus)
7ebb3f9aaf3b - Added video coding statistics for ffmpeg. Needs more work.
pulento
parents: 268
diff changeset
838 range *= 2;
218
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
839 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
840 xmax = s->mb_width*16;
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
841 ymax = s->mb_height*16;
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
842 }else {
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
843 /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
844 (cuz the drawn edge isnt large enough))*/
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
845 xmax = s->width;
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
846 ymax = s->height;
232
b640ec5948b0 - Now the ME is done for the entire picture when enconding, the
pulento
parents: 218
diff changeset
847 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
848 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
849 xmin = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
850 ymin = 0;
218
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
851 xmax = s->mb_width*16 - 16;
9df2d13e64b2 (commit by michael)
arpi_esp
parents: 6
diff changeset
852 ymax = s->mb_height*16 - 16;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
853 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
854 switch(s->full_search) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
855 case ME_ZERO:
986e461dc072 Initial revision
glantau
parents:
diff changeset
856 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
857 no_motion_search(s, &mx, &my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
858 dmin = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
859 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
860 case ME_FULL:
986e461dc072 Initial revision
glantau
parents:
diff changeset
861 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax);
986e461dc072 Initial revision
glantau
parents:
diff changeset
862 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
863 case ME_LOG:
986e461dc072 Initial revision
glantau
parents:
diff changeset
864 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax);
986e461dc072 Initial revision
glantau
parents:
diff changeset
865 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
866 case ME_PHODS:
986e461dc072 Initial revision
glantau
parents:
diff changeset
867 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax);
986e461dc072 Initial revision
glantau
parents:
diff changeset
868 break;
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
869 case ME_X1:
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
870 case ME_EPZS:
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
871 {
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
872 const int mot_stride = s->block_wrap[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
873 const int mot_xy = s->block_index[0];
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
874
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
875 rel_xmin= xmin - mb_x*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
876 rel_xmax= xmax - mb_x*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
877 rel_ymin= ymin - mb_y*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
878 rel_ymax= ymax - mb_y*16;
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
879
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
880 P[0][0] = s->motion_val[mot_xy ][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
881 P[0][1] = s->motion_val[mot_xy ][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
882 P[1][0] = s->motion_val[mot_xy - 1][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
883 P[1][1] = s->motion_val[mot_xy - 1][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
884 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
885
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
886 /* special case for first line */
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
887 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
888 P[4][0] = P[1][0];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
889 P[4][1] = P[1][1];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
890 } else {
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
891 P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
892 P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
893 P[3][0] = s->motion_val[mot_xy - mot_stride + 2 ][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
894 P[3][1] = s->motion_val[mot_xy - mot_stride + 2 ][1];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
895 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
896 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
897 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
898
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
899 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
900 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
901 }
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
902 if(s->out_format == FMT_H263){
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
903 pred_x = P[4][0];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
904 pred_y = P[4][1];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
905 }else { /* mpeg1 at least */
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
906 pred_x= P[1][0];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
907 pred_y= P[1][1];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
908 }
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
909 }
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
910 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
911
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
912 mx+= mb_x*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
913 my+= mb_y*16;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
914 break;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
915 }
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
916
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
917 if(s->flags&CODEC_FLAG_4MV){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
918 int block;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
919
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
920 mb_type|= MB_TYPE_INTER4V;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
921
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
922 for(block=0; block<4; block++){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
923 int mx4, my4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
924 int pred_x4, pred_y4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
925 int dmin4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
926 static const int off[4]= {2, 1, 1, -1};
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
927 const int mot_stride = s->block_wrap[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
928 const int mot_xy = s->block_index[block];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
929 const int block_x= mb_x*2 + (block&1);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
930 const int block_y= mb_y*2 + (block>>1);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
931
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
932 const int rel_xmin4= xmin - block_x*8;
295
6622b0fd036c mpeg4 4MV encoding
michaelni
parents: 294
diff changeset
933 const int rel_xmax4= xmax - block_x*8 + 8;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
934 const int rel_ymin4= ymin - block_y*8;
295
6622b0fd036c mpeg4 4MV encoding
michaelni
parents: 294
diff changeset
935 const int rel_ymax4= ymax - block_y*8 + 8;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
936
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
937 P[0][0] = s->motion_val[mot_xy ][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
938 P[0][1] = s->motion_val[mot_xy ][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
939 P[1][0] = s->motion_val[mot_xy - 1][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
940 P[1][1] = s->motion_val[mot_xy - 1][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
941 if(P[1][0] > (rel_xmax4<<shift)) P[1][0]= (rel_xmax4<<shift);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
942
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
943 /* special case for first line */
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
944 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
945 P[4][0] = P[1][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
946 P[4][1] = P[1][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
947 } else {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
948 P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
949 P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
950 P[3][0] = s->motion_val[mot_xy - mot_stride + off[block]][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
951 P[3][1] = s->motion_val[mot_xy - mot_stride + off[block]][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
952 if(P[2][1] > (rel_ymax4<<shift)) P[2][1]= (rel_ymax4<<shift);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
953 if(P[3][0] < (rel_xmin4<<shift)) P[3][0]= (rel_xmin4<<shift);
295
6622b0fd036c mpeg4 4MV encoding
michaelni
parents: 294
diff changeset
954 if(P[3][0] > (rel_xmax4<<shift)) P[3][0]= (rel_xmax4<<shift);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
955 if(P[3][1] > (rel_ymax4<<shift)) P[3][1]= (rel_ymax4<<shift);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
956
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
957 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
958 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
959 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
960 if(s->out_format == FMT_H263){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
961 pred_x4 = P[4][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
962 pred_y4 = P[4][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
963 }else { /* mpeg1 at least */
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
964 pred_x4= P[1][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
965 pred_y4= P[1][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
966 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
967 P[5][0]= mx - mb_x*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
968 P[5][1]= my - mb_y*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
969
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
970 dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
971
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
972 halfpel_motion_search4(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
973 pred_x4, pred_y4, block_x, block_y);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
974
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
975 s->motion_val[ s->block_index[block] ][0]= mx4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
976 s->motion_val[ s->block_index[block] ][1]= my4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
977 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
978 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
979
986e461dc072 Initial revision
glantau
parents:
diff changeset
980 /* intra / predictive decision */
986e461dc072 Initial revision
glantau
parents:
diff changeset
981 xx = mb_x * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
982 yy = mb_y * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
983
986e461dc072 Initial revision
glantau
parents:
diff changeset
984 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
985 /* At this point (mx,my) are full-pell and the absolute displacement */
986e461dc072 Initial revision
glantau
parents:
diff changeset
986 ppix = s->last_picture[0] + (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
987
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
988 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
989 #if 0
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
990 varc = pix_dev(pix, s->linesize, (sum+128)>>8) + INTER_BIAS;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
991 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
992 #else
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
993 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
994 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
995 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
996 #endif
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
997
239
16cd8a9c4da4 - Minor changes on bitrate control
pulento
parents: 233
diff changeset
998 s->mb_var[s->mb_width * mb_y + mb_x] = varc;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
999 s->avg_mb_var+= varc;
268
09ae29b27ed9 hopefully better bitrate controll
michaelni
parents: 239
diff changeset
1000 s->mc_mb_var += vard;
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
1001
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1002 #if 0
233
3f5b72726118 - More work on preliminary bit rate control, just to be able to get an
pulento
parents: 232
diff changeset
1003 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
1004 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1005 #endif
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1006 if(s->flags&CODEC_FLAG_HQ){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1007 if (vard*2 + 200 > varc)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1008 mb_type|= MB_TYPE_INTRA;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1009 if (varc*2 + 200 > vard){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1010 mb_type|= MB_TYPE_INTER;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
1011 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y);
304
5753d57e7e6b fixing MVs in hq mode
michaelni
parents: 295
diff changeset
1012 }else{
5753d57e7e6b fixing MVs in hq mode
michaelni
parents: 295
diff changeset
1013 mx = mx*2 - mb_x*32;
5753d57e7e6b fixing MVs in hq mode
michaelni
parents: 295
diff changeset
1014 my = my*2 - mb_y*32;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1015 }
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1016 }else{
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1017 if (vard <= 64 || vard < varc) {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1018 mb_type|= MB_TYPE_INTER;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1019 if (s->full_search != ME_ZERO) {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1020 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1021 } else {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1022 mx -= 16 * mb_x;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1023 my -= 16 * mb_y;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1024 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1025 }else{
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1026 mb_type|= MB_TYPE_INTRA;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1027 mx = 0;//mx*2 - 32 * mb_x;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1028 my = 0;//my*2 - 32 * mb_y;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1029 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1030 }
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
1031
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1032 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1033 set_mv_tables(s, mx, my);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1034 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1035
986e461dc072 Initial revision
glantau
parents:
diff changeset
1036 #else
986e461dc072 Initial revision
glantau
parents:
diff changeset
1037
986e461dc072 Initial revision
glantau
parents:
diff changeset
1038 /* test version which generates valid random vectors */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1039 int estimate_motion(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1040 int mb_x, int mb_y,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1041 int *mx_ptr, int *my_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1042 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1043 int xx, yy, x1, y1, x2, y2, range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1044
986e461dc072 Initial revision
glantau
parents:
diff changeset
1045 if ((random() % 10) >= 5) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1046 range = 8 * (1 << (s->f_code - 1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
1047 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1048 range = range * 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1049
986e461dc072 Initial revision
glantau
parents:
diff changeset
1050 xx = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1051 yy = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1052 x1 = xx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1053 if (x1 < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1054 x1 = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1055 x2 = xx + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1056 if (x2 > (s->width - 16))
986e461dc072 Initial revision
glantau
parents:
diff changeset
1057 x2 = s->width - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1058 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1059 if (y1 < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1060 y1 = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1061 y2 = yy + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1062 if (y2 > (s->height - 16))
986e461dc072 Initial revision
glantau
parents:
diff changeset
1063 y2 = s->height - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1064
986e461dc072 Initial revision
glantau
parents:
diff changeset
1065 *mx_ptr = (random() % (2 * (x2 - x1 + 1))) + 2 * (x1 - xx);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1066 *my_ptr = (random() % (2 * (y2 - y1 + 1))) + 2 * (y1 - yy);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1067 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1068 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1069 *mx_ptr = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1070 *my_ptr = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1071 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1072 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1073 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1074
986e461dc072 Initial revision
glantau
parents:
diff changeset
1075 #endif