annotate motion_est.c @ 853:eacc2dd8fd9d libavcodec

* using DSPContext - so each codec could use its local (sub)set of CPU extension
author kabi
date Mon, 11 Nov 2002 09:40:17 +0000
parents f3c369b8ddca
children 4ba00af12589
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
429
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
3 * Copyright (c) 2000,2001 Fabrice Bellard.
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
4 * Copyright (c) 2002 Michael Niedermayer
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 *
429
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
7 * This library is free software; you can redistribute it and/or
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
9 * License as published by the Free Software Foundation; either
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
10 * version 2 of the License, or (at your option) any later version.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 *
429
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
12 * This library is distributed in the hope that it will be useful,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
429
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
15 * Lesser General Public License for more details.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 *
429
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
18 * License along with this library; if not, write to the Free Software
718a22dc121f license/copyright change
glantau
parents: 376
diff changeset
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
20 *
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
21 * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 #include <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 #include "dsputil.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 #include "mpegvideo.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
28
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
29 #define SQ(a) ((a)*(a))
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
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
32 #define P_LAST P[0]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
33 #define P_LEFT P[1]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
34 #define P_TOP P[2]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
35 #define P_TOPRIGHT P[3]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
36 #define P_MEDIAN P[4]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
37 #define P_LAST_LEFT P[5]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
38 #define P_LAST_RIGHT P[6]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
39 #define P_LAST_TOP P[7]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
40 #define P_LAST_BOTTOM P[8]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
41 #define P_MV1 P[9]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
42
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
43
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
44 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
45 {
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
46 int s, i, j;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
47
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
48 s = 0;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
49 for (i = 0; i < 16; i++) {
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
50 for (j = 0; j < 16; j += 8) {
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
51 s += ABS(pix[0]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
52 s += ABS(pix[1]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
53 s += ABS(pix[2]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
54 s += ABS(pix[3]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
55 s += ABS(pix[4]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
56 s += ABS(pix[5]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
57 s += ABS(pix[6]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
58 s += ABS(pix[7]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
59 pix += 8;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
60 }
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
61 pix += line_size - 16;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
62 }
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
63 return s;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
64 }
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
65
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 UINT32 *sq = squareTbl + 256;
986e461dc072 Initial revision
glantau
parents:
diff changeset
70
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 s += sq[pix1[0] - pix2[0]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 s += sq[pix1[1] - pix2[1]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 s += sq[pix1[2] - pix2[2]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 s += sq[pix1[3] - pix2[3]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 s += sq[pix1[4] - pix2[4]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 s += sq[pix1[5] - pix2[5]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 s += sq[pix1[6] - pix2[6]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 s += sq[pix1[7] - pix2[7]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 pix1 += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 pix2 += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 pix1 += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 pix2 += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
90
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
91 static inline void no_motion_search(MpegEncContext * s,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
92 int *mx_ptr, int *my_ptr)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 *mx_ptr = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 *my_ptr = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
97
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 static int full_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 int *mx_ptr, int *my_ptr, int range,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
100 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 int x1, y1, x2, y2, xx, yy, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 int mx, my, dmin, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
105
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 xx = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 yy = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 x2 = xx + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 y1 = yy - range + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 y2 = yy + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 dmin = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 for (y = y1; y <= y2; y++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 for (x = x1; x <= x2; x++) {
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
126 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x,
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
127 s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 if (d < dmin ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 (d == dmin &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 (abs(x - xx) + abs(y - yy)) <
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 dmin = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
138
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
141
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 return dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
150
986e461dc072 Initial revision
glantau
parents:
diff changeset
151
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 static int log_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 int *mx_ptr, int *my_ptr, int range,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
154 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 int x1, y1, x2, y2, xx, yy, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 int mx, my, dmin, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
159
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 xx = s->mb_x << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 yy = s->mb_y << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
162
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 /* Left limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 x1 = xx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
167
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 /* Right limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 x2 = xx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
172
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 /* Upper limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
177
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 /* Lower limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 y2 = yy + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
182
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 dmin = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
187
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 for (y = y1; y <= y2; y += range) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 for (x = x1; x <= x2; x += range) {
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
191 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
192 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 dmin = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
199
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 range = range >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
201
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 x1 = mx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
205
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 x2 = mx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
209
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 y1 = my - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
213
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 y2 = my + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
217
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 } while (range >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
219
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 return dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
227
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 static int phods_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 int *mx_ptr, int *my_ptr, int range,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
230 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 int mx, my, dminx, dminy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
235
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 xx = s->mb_x << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 yy = s->mb_y << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
238
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 /* Left limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 x1 = xx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
243
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 /* Right limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 x2 = xx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
248
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 /* Upper limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
253
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 /* Lower limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 y2 = yy + 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 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
262
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 x = xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 y = yy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 dminx = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 dminy = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
268
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 lastx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 for (x = x1; x <= x2; x += range) {
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
271 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 dminx = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
277
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 x = lastx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 for (y = y1; y <= y2; y += range) {
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
280 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 dminy = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
286
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 range = range >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
288
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 x = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 y = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 x1 = mx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
294
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 x2 = mx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
298
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 y1 = my - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
302
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 y2 = my + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
306
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 } while (range >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
308
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
312
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 /* half pixel search */
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 return dminy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
318
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
319
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
320 #define Z_THRESHOLD 256
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
321
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
322 #define CHECK_MV(x,y)\
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
323 {\
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
324 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
325 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
326 if(map[index]!=key){\
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
327 d = s->dsp.pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
328 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
329 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
330 map[index]= key;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
331 score_map[index]= d;\
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
332 }\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
333 }
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
334
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
335 #define CHECK_MV_DIR(x,y,new_dir)\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
336 {\
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
337 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
338 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
339 if(map[index]!=key){\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
340 d = pix_abs(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
341 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
342 if(d<dmin){\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
343 best[0]=x;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
344 best[1]=y;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
345 dmin=d;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
346 next_dir= new_dir;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
347 }\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
348 map[index]= key;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
349 score_map[index]= d;\
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
350 }\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
351 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
352
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
353 #define CHECK_MV4(x,y)\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
354 {\
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
355 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
356 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
357 if(map[index]!=key){\
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
358 d = s->dsp.pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
359 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
360 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
361 map[index]= key;\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
362 score_map[index]= d;\
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
363 }\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
364 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
365
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
366 #define check(x,y,S,v)\
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
367 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
368 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
369 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
370 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
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 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
374 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
375 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
376 int xmin, int ymin, int xmax, int ymax, int shift,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
377 uint32_t *map, uint16_t *score_map, int map_generation,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
378 op_pixels_abs_func pix_abs)
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
379 {
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
380 int next_dir=-1;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
381
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
382 for(;;){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
383 int d;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
384 const int dir= next_dir;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
385 const int x= best[0];
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
386 const int y= best[1];
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
387 next_dir=-1;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
388
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
389 //printf("%d", dir);
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
390 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
391 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
392 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
393 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
394
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
395 if(next_dir==-1){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
396 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
397 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
398 }
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
399
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
400 /* for(;;){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
401 int d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
402 const int x= best[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
403 const int y= best[1];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
404 const int last_min=dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
405 if(x>xmin) CHECK_MV(x-1, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
406 if(y>xmin) CHECK_MV(x , y-1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
407 if(x<xmax) CHECK_MV(x+1, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
408 if(y<xmax) CHECK_MV(x , y+1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
409 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
410 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
411 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
412 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
413 if(x-1>xmin) CHECK_MV(x-2, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
414 if(y-1>xmin) CHECK_MV(x , y-2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
415 if(x+1<xmax) CHECK_MV(x+2, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
416 if(y+1<xmax) CHECK_MV(x , y+2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
417 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
418 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
419 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
420 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
421 if(dmin==last_min) return dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
422 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
423 */
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
424 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
425
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
426 #if 1
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
427 #define SNAKE_1 3
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
428 #define SNAKE_2 2
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
429 #else
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
430 #define SNAKE_1 7
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
431 #define SNAKE_2 3
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
432 #endif
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
433 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
434 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
435 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
436 int xmin, int ymin, int xmax, int ymax, int shift,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
437 uint32_t *map, uint16_t *score_map,int map_generation,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
438 op_pixels_abs_func pix_abs)
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
439 {
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
440 int dir=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
441 int c=1;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
442 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
443 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
444 int fails=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
445 int last_d[2]={dmin, dmin};
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
446
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
447 /*static int good=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
448 static int bad=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
449 static int point=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
450
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
451 point++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
452 if(256*256*256*64%point==0)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
453 {
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
454 printf("%d %d %d\n", good, bad, point);
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
455 }*/
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
456
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
457 for(;;){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
458 int x= best[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
459 int y= best[1];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
460 int d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
461 x+=x_dir[dir];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
462 y+=y_dir[dir];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
463 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
464 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
465 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
466 if(map[index]!=key){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
467 d = pix_abs(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
468 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
469 map[index]=key;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
470 score_map[index]=d;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
471 }else
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
472 d= dmin+1;
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
473 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
474 d = dmin + 10000; //FIXME smarter boundary handling
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
475 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
476 if(d<dmin){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
477 best[0]=x;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
478 best[1]=y;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
479 dmin=d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
480
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
481 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
482 dir+=c;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
483
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
484 fails=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
485 //good++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
486 last_d[1]=last_d[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
487 last_d[0]=d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
488 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
489 //bad++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
490 if(fails){
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
491 if(fails>=SNAKE_1+1) return dmin;
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
492 }else{
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
493 if(dir&1) dir-= c*3;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
494 else c= -c;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
495 // c= -c;
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
496 }
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
497 dir+=c*SNAKE_2;
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
498 fails++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
499 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
500 dir&=7;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
501 }
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
502 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
503
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
504 static inline int cross_search(MpegEncContext * s, int *best, int dmin,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
505 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
506 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
507 int xmin, int ymin, int xmax, int ymax, int shift,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
508 uint32_t *map, uint16_t *score_map,int map_generation,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
509 op_pixels_abs_func pix_abs)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
510 {
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
511 static int x_dir[4]= {-1, 0, 1, 0};
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
512 static int y_dir[4]= { 0,-1, 0, 1};
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
513 int improvement[2]={100000, 100000};
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
514 int dirs[2]={2, 3};
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
515 int dir;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
516 int last_dir= -1;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
517
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
518 for(;;){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
519 dir= dirs[ improvement[0] > improvement[1] ? 0 : 1 ];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
520 if(improvement[dir&1]==-1) return dmin;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
521
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
522 {
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
523 const int x= best[0] + x_dir[dir];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
524 const int y= best[1] + y_dir[dir];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
525 const int key= (y<<ME_MAP_MV_BITS) + x + map_generation;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
526 const int index= ((y<<ME_MAP_SHIFT) + x)&(ME_MAP_SIZE-1);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
527 int d;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
528 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
529 if(map[index]!=key){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
530 d = pix_abs(new_pic, old_pic + x + y*pic_stride, pic_stride);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
531 d += (mv_penalty[(x<<shift)-pred_x] + mv_penalty[(y<<shift)-pred_y])*quant;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
532 map[index]=key;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
533 score_map[index]=d;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
534 if(d<dmin){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
535 improvement[dir&1]= dmin-d;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
536 improvement[(dir&1)^1]++;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
537 dmin=d;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
538 best[0]= x;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
539 best[1]= y;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
540 last_dir=dir;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
541 continue;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
542 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
543 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
544 d= score_map[index];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
545 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
546 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
547 d= dmin + 1000; //FIXME is this a good idea?
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
548 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
549 /* evaluated point was cached or checked and worse */
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
550
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
551 if(last_dir==dir){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
552 improvement[dir&1]= -1;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
553 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
554 improvement[dir&1]= d-dmin;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
555 last_dir= dirs[dir&1]= dir^2;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
556 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
557 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
558 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
559 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
560
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
561 static inline int update_map_generation(MpegEncContext * s)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
562 {
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
563 s->me_map_generation+= 1<<(ME_MAP_MV_BITS*2);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
564 if(s->me_map_generation==0){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
565 s->me_map_generation= 1<<(ME_MAP_MV_BITS*2);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
566 memset(s->me_map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
567 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
568 return s->me_map_generation;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
569 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
570
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
571 static int epzs_motion_search(MpegEncContext * s,
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
572 int *mx_ptr, int *my_ptr,
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
573 int P[10][2], int pred_x, int pred_y,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
574 int xmin, int ymin, int xmax, int ymax, uint8_t * ref_picture)
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
575 {
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
576 int best[2]={0, 0};
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
577 int d, dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
578 UINT8 *new_pic, *old_pic;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
579 const int pic_stride= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
580 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
581 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
582 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
583 const int shift= 1+s->quarter_sample;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
584 uint32_t *map= s->me_map;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
585 uint16_t *score_map= s->me_score_map;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
586 int map_generation;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
587
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
588 new_pic = s->new_picture[0] + pic_xy;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
589 old_pic = ref_picture + pic_xy;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
590
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
591 map_generation= update_map_generation(s);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
592
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
593 dmin = s->dsp.pix_abs16x16(new_pic, old_pic, pic_stride);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
594 map[0]= map_generation;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
595 score_map[0]= dmin;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
596
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
597 /* first line */
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
598 if ((s->mb_y == 0 || s->first_slice_line)) {
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
599 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
600 CHECK_MV(P_LAST[0]>>shift, P_LAST[1]>>shift)
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
601 }else{
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
602 if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
603 |P_TOP[0] |P_TOP[1]
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
604 |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
605 *mx_ptr= 0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
606 *my_ptr= 0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
607 s->skip_me=1;
277
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 }
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
610 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
611 if(dmin>256*2){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
612 CHECK_MV(P_LAST[0] >>shift, P_LAST[1] >>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
613 CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
614 CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
615 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
616 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
617 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
618 if(dmin>256*4){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
619 CHECK_MV(P_LAST_RIGHT[0] >>shift, P_LAST_RIGHT[1] >>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
620 CHECK_MV(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift)
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
621 }
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
622 #if 0 //doest only slow things down
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
623 if(dmin>512*3){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
624 int step;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
625 dmin= score_map[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
626 best[0]= best[1]=0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
627 for(step=128; step>0; step>>=1){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
628 const int step2= step;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
629 int y;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
630 for(y=-step2+best[1]; y<=step2+best[1]; y+=step){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
631 int x;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
632 if(y<ymin || y>ymax) continue;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
633
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
634 for(x=-step2+best[0]; x<=step2+best[0]; x+=step){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
635 if(x<xmin || x>xmax) continue;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
636 if(x==best[0] && y==best[1]) continue;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
637 CHECK_MV(x,y)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
638 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
639 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
640 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
641 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
642 #endif
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
643 //check(best[0],best[1],0, b0)
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
644 if(s->me_method==ME_EPZS)
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
645 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride,
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
646 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
647 shift, map, score_map, map_generation, s->dsp.pix_abs16x16);
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
648 else
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
649 dmin= cross_search(s, best, dmin, new_pic, old_pic, pic_stride,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
650 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
651 shift, map, score_map, map_generation, s->dsp.pix_abs16x16);
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
652 //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
653 *mx_ptr= best[0];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
654 *my_ptr= best[1];
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
655
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
656 // 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
657 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
658 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
659
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
660 static int epzs_motion_search4(MpegEncContext * s, int block,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
661 int *mx_ptr, int *my_ptr,
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
662 int P[10][2], int pred_x, int pred_y,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
663 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
664 {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
665 int best[2]={0, 0};
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
666 int d, dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
667 UINT8 *new_pic, *old_pic;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
668 const int pic_stride= s->linesize;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
669 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
670 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
671 int quant= s->qscale; // qscale of the prev frame
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
672 const int shift= 1+s->quarter_sample;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
673 uint32_t *map= s->me_map;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
674 uint16_t *score_map= s->me_score_map;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
675 int map_generation;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
676
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
677 new_pic = s->new_picture[0] + pic_xy;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
678 old_pic = ref_picture + pic_xy;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
679
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
680 map_generation= update_map_generation(s);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
681
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
682 dmin = 1000000;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
683 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
684 /* first line */
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
685 if ((s->mb_y == 0 || s->first_slice_line) && block<2) {
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
686 CHECK_MV4(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
687 CHECK_MV4(P_LAST[0]>>shift, P_LAST[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
688 CHECK_MV4(P_MV1[0]>>shift, P_MV1[1]>>shift)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
689 }else{
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
690 CHECK_MV4(P_MV1[0]>>shift, P_MV1[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
691 //FIXME try some early stop
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
692 if(dmin>64*2){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
693 CHECK_MV4(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
694 CHECK_MV4(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
695 CHECK_MV4(P_TOP[0]>>shift, P_TOP[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
696 CHECK_MV4(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
697 CHECK_MV4(P_LAST[0]>>shift, P_LAST[1]>>shift)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
698 }
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
699 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
700 if(dmin>64*4){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
701 CHECK_MV4(P_LAST_RIGHT[0]>>shift, P_LAST_RIGHT[1]>>shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
702 CHECK_MV4(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
703 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
704
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
705 if(s->me_method==ME_EPZS)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
706 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
707 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
708 shift, map, score_map, map_generation, s->dsp.pix_abs8x8);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
709 else
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
710 dmin= cross_search(s, best, dmin, new_pic, old_pic, pic_stride,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
711 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
712 shift, map, score_map, map_generation, s->dsp.pix_abs8x8);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
713
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
714 *mx_ptr= best[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
715 *my_ptr= best[1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
716
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
717 // printf("%d %d %d \n", best[0], best[1], dmin);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
718 return dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
719 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
720
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
721 #define CHECK_HALF_MV(suffix, x, y) \
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
722 {\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
723 d= pix_abs_ ## 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
724 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
725 COPY3_IF_LT(dminh, d, dx, x, dy, y)\
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
726 }
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
727
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
728
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
729 /* The idea would be to make half pel ME after Inter/Intra decision to
986e461dc072 Initial revision
glantau
parents:
diff changeset
730 save time. */
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
731 static inline int halfpel_motion_search(MpegEncContext * s,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
732 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
733 int xmin, int ymin, int xmax, int ymax,
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
734 int pred_x, int pred_y, uint8_t *ref_picture,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
735 op_pixels_abs_func pix_abs_x2,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
736 op_pixels_abs_func pix_abs_y2, op_pixels_abs_func pix_abs_xy2, int n)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
737 {
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
738 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
739 const int quant= s->qscale;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
740 int mx, my, xx, yy, dminh;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
741 UINT8 *pix, *ptr;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
742
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
743 if(s->skip_me){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
744 *mx_ptr = 0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
745 *my_ptr = 0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
746 return dmin;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
747 }else
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
748
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
749 xx = 16 * s->mb_x + 8*(n&1);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
750 yy = 16 * s->mb_y + 8*(n>>1);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
751 pix = s->new_picture[0] + (yy * s->linesize) + xx;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
752
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
753 mx = *mx_ptr;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
754 my = *my_ptr;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
755 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx);
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
756
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
757 dminh = dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
758
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
759 if (mx > xmin && mx < xmax &&
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
760 my > ymin && my < ymax) {
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
761 int dx=0, dy=0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
762 int d, pen_x, pen_y;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
763
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
764 mx<<=1;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
765 my<<=1;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
766
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
767 pen_x= pred_x + mx;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
768 pen_y= pred_y + my;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
769
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
770 ptr-= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
771 CHECK_HALF_MV(xy2, -1, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
772 CHECK_HALF_MV(y2 , 0, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
773 CHECK_HALF_MV(xy2, +1, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
774
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
775 ptr+= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
776 CHECK_HALF_MV(x2 , -1, 0)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
777 CHECK_HALF_MV(x2 , +1, 0)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
778 CHECK_HALF_MV(xy2, -1, +1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
779 CHECK_HALF_MV(y2 , 0, +1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
780 CHECK_HALF_MV(xy2, +1, +1)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
781
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
782 mx+=dx;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
783 my+=dy;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
784 }else{
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
785 mx<<=1;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
786 my<<=1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
787 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
788
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
789 *mx_ptr = mx;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
790 *my_ptr = my;
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
791 return dminh;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
792 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
793
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
794 static inline int fast_halfpel_motion_search(MpegEncContext * s,
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
795 int *mx_ptr, int *my_ptr, int dmin,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
796 int xmin, int ymin, int xmax, int ymax,
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
797 int pred_x, int pred_y, uint8_t *ref_picture,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
798 op_pixels_abs_func pix_abs_x2,
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
799 op_pixels_abs_func pix_abs_y2, op_pixels_abs_func pix_abs_xy2, int n)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
800 {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
801 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
802 uint16_t *score_map= s->me_score_map;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
803 const int quant= s->qscale;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
804 int mx, my, xx, yy, dminh;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
805 UINT8 *pix, *ptr;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
806
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
807 if(s->skip_me){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
808 // printf("S");
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
809 *mx_ptr = 0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
810 *my_ptr = 0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
811 return dmin;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
812 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
813 // printf("N");
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
814
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
815 xx = 16 * s->mb_x + 8*(n&1);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
816 yy = 16 * s->mb_y + 8*(n>>1);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
817 pix = s->new_picture[0] + (yy * s->linesize) + xx;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
818
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
819 mx = *mx_ptr;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
820 my = *my_ptr;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
821 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
822
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
823 dminh = dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
824
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
825 if (mx > xmin && mx < xmax &&
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
826 my > ymin && my < ymax) {
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
827 int dx=0, dy=0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
828 int d, pen_x, pen_y;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
829 const int index= (my<<ME_MAP_SHIFT) + mx;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
830 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
831 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
832 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
833 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
834 mx<<=1;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
835 my<<=1;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
836
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
837
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
838 pen_x= pred_x + mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
839 pen_y= pred_y + my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
840
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
841 ptr-= s->linesize;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
842 if(t<=b){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
843 CHECK_HALF_MV(y2 , 0, -1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
844 if(l<=r){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
845 CHECK_HALF_MV(xy2, -1, -1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
846 if(t+r<=b+l){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
847 CHECK_HALF_MV(xy2, +1, -1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
848 ptr+= s->linesize;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
849 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
850 ptr+= s->linesize;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
851 CHECK_HALF_MV(xy2, -1, +1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
852 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
853 CHECK_HALF_MV(x2 , -1, 0)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
854 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
855 CHECK_HALF_MV(xy2, +1, -1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
856 if(t+l<=b+r){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
857 CHECK_HALF_MV(xy2, -1, -1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
858 ptr+= s->linesize;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
859 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
860 ptr+= s->linesize;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
861 CHECK_HALF_MV(xy2, +1, +1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
862 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
863 CHECK_HALF_MV(x2 , +1, 0)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
864 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
865 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
866 if(l<=r){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
867 if(t+l<=b+r){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
868 CHECK_HALF_MV(xy2, -1, -1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
869 ptr+= s->linesize;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
870 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
871 ptr+= s->linesize;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
872 CHECK_HALF_MV(xy2, +1, +1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
873 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
874 CHECK_HALF_MV(x2 , -1, 0)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
875 CHECK_HALF_MV(xy2, -1, +1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
876 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
877 if(t+r<=b+l){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
878 CHECK_HALF_MV(xy2, +1, -1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
879 ptr+= s->linesize;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
880 }else{
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
881 ptr+= s->linesize;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
882 CHECK_HALF_MV(xy2, -1, +1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
883 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
884 CHECK_HALF_MV(x2 , +1, 0)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
885 CHECK_HALF_MV(xy2, +1, +1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
886 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
887 CHECK_HALF_MV(y2 , 0, +1)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
888 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
889 mx+=dx;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
890 my+=dy;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
891
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
892 }else{
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
893 mx<<=1;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
894 my<<=1;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
895 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
896
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
897 *mx_ptr = mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
898 *my_ptr = my;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
899 return dminh;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
900 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
901
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
902 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
903 {
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
904 const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
905
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
906 s->p_mv_table[xy][0] = mx;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
907 s->p_mv_table[xy][1] = my;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
908
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
909 /* has allready been set to the 4 MV if 4MV is done */
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
910 if(mv4){
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
911 int mot_xy= s->block_index[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
912
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
913 s->motion_val[mot_xy ][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
914 s->motion_val[mot_xy ][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
915 s->motion_val[mot_xy+1][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
916 s->motion_val[mot_xy+1][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
917
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
918 mot_xy += s->block_wrap[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
919 s->motion_val[mot_xy ][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
920 s->motion_val[mot_xy ][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
921 s->motion_val[mot_xy+1][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
922 s->motion_val[mot_xy+1][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
923 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
924 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
925
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
926 static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
927 {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
928 *range = 8 * (1 << (f_code - 1));
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
929 /* XXX: temporary kludge to avoid overflow for msmpeg4 */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
930 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
931 *range *= 2;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
932
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
933 if (s->unrestricted_mv) {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
934 *xmin = -16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
935 *ymin = -16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
936 if (s->h263_plus)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
937 *range *= 2;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
938 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
939 *xmax = s->mb_width*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
940 *ymax = s->mb_height*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
941 }else {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
942 /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
943 (cuz the drawn edge isnt large enough))*/
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
944 *xmax = s->width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
945 *ymax = s->height;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
946 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
947 } else {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
948 *xmin = 0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
949 *ymin = 0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
950 *xmax = s->mb_width*16 - 16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
951 *ymax = s->mb_height*16 - 16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
952 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
953 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
954
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
955 static inline int mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
956 {
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
957 int block;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
958 int P[10][2];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
959 uint8_t *ref_picture= s->last_picture[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
960 int dmin_sum=0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
961
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
962 for(block=0; block<4; block++){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
963 int mx4, my4;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
964 int pred_x4, pred_y4;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
965 int dmin4;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
966 static const int off[4]= {2, 1, 1, -1};
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
967 const int mot_stride = s->block_wrap[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
968 const int mot_xy = s->block_index[block];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
969 // const int block_x= (block&1);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
970 // const int block_y= (block>>1);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
971 #if 1 // this saves us a bit of cliping work and shouldnt affect compression in a negative way
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
972 const int rel_xmin4= xmin;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
973 const int rel_xmax4= xmax;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
974 const int rel_ymin4= ymin;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
975 const int rel_ymax4= ymax;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
976 #else
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
977 const int rel_xmin4= xmin - block_x*8;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
978 const int rel_xmax4= xmax - block_x*8 + 8;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
979 const int rel_ymin4= ymin - block_y*8;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
980 const int rel_ymax4= ymax - block_y*8 + 8;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
981 #endif
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
982 P_LAST[0] = s->motion_val[mot_xy ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
983 P_LAST[1] = s->motion_val[mot_xy ][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
984 P_LEFT[0] = s->motion_val[mot_xy - 1][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
985 P_LEFT[1] = s->motion_val[mot_xy - 1][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
986 P_LAST_RIGHT[0] = s->motion_val[mot_xy + 1][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
987 P_LAST_RIGHT[1] = s->motion_val[mot_xy + 1][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
988 P_LAST_BOTTOM[0]= s->motion_val[mot_xy + 1*mot_stride][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
989 P_LAST_BOTTOM[1]= s->motion_val[mot_xy + 1*mot_stride][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
990
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
991 if(P_LEFT[0] > (rel_xmax4<<shift)) P_LEFT[0] = (rel_xmax4<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
992 if(P_LAST_RIGHT[0] < (rel_xmin4<<shift)) P_LAST_RIGHT[0] = (rel_xmin4<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
993 if(P_LAST_BOTTOM[1]< (rel_ymin4<<shift)) P_LAST_BOTTOM[1]= (rel_ymin4<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
994
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
995 /* special case for first line */
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
996 if ((s->mb_y == 0 || s->first_slice_line) && block<2) {
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
997 pred_x4= P_LEFT[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
998 pred_y4= P_LEFT[1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
999 } else {
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1000 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1001 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1002 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + off[block]][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1003 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + off[block]][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1004 if(P_TOP[1] > (rel_ymax4<<shift)) P_TOP[1] = (rel_ymax4<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1005 if(P_TOPRIGHT[0] < (rel_xmin4<<shift)) P_TOPRIGHT[0]= (rel_xmin4<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1006 if(P_TOPRIGHT[0] > (rel_xmax4<<shift)) P_TOPRIGHT[0]= (rel_xmax4<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1007 if(P_TOPRIGHT[1] > (rel_ymax4<<shift)) P_TOPRIGHT[1]= (rel_ymax4<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1008
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1009 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1010 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1011
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1012 if(s->out_format == FMT_H263){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1013 pred_x4 = P_MEDIAN[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1014 pred_y4 = P_MEDIAN[1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1015 }else { /* mpeg1 at least */
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1016 pred_x4= P_LEFT[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1017 pred_y4= P_LEFT[1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1018 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1019 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1020 P_MV1[0]= mx;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1021 P_MV1[1]= my;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1022
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1023 dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, ref_picture);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1024
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1025 dmin4= fast_halfpel_motion_search(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4,
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1026 pred_x4, pred_y4, ref_picture, s->dsp.pix_abs8x8_x2,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1027 s->dsp.pix_abs8x8_y2, s->dsp.pix_abs8x8_xy2, block);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1028
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1029 s->motion_val[ s->block_index[block] ][0]= mx4;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1030 s->motion_val[ s->block_index[block] ][1]= my4;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1031 dmin_sum+= dmin4;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1032 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1033 return dmin_sum;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1034 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1035
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1036 void ff_estimate_p_frame_motion(MpegEncContext * s,
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1037 int mb_x, int mb_y)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1038 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1039 UINT8 *pix, *ppix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1040 int sum, varc, vard, mx, my, range, dmin, xx, yy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1041 int xmin, ymin, xmax, ymax;
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
1042 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
1043 int pred_x=0, pred_y=0;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1044 int P[10][2];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
1045 const int shift= 1+s->quarter_sample;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1046 int mb_type=0;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1047 uint8_t *ref_picture= s->last_picture[0];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1048
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1049 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1050 rel_xmin= xmin - mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1051 rel_xmax= xmax - mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1052 rel_ymin= ymin - mb_y*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1053 rel_ymax= ymax - mb_y*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1054 s->skip_me=0;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1055
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1056 switch(s->me_method) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1057 case ME_ZERO:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1058 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1059 no_motion_search(s, &mx, &my);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1060 mx-= mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1061 my-= mb_y*16;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1062 dmin = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1063 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1064 case ME_FULL:
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1065 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1066 mx-= mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1067 my-= mb_y*16;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1068 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1069 case ME_LOG:
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1070 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1071 mx-= mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1072 my-= mb_y*16;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1073 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1074 case ME_PHODS:
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1075 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1076 mx-= mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1077 my-= mb_y*16;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1078 break;
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
1079 case ME_X1:
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
1080 case ME_EPZS:
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
1081 {
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1082 const int mot_stride = s->block_wrap[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1083 const int mot_xy = s->block_index[0];
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
1084
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1085 P_LAST[0] = s->motion_val[mot_xy ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1086 P_LAST[1] = s->motion_val[mot_xy ][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1087 P_LEFT[0] = s->motion_val[mot_xy - 1][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1088 P_LEFT[1] = s->motion_val[mot_xy - 1][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1089 P_LAST_RIGHT[0] = s->motion_val[mot_xy + 2][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1090 P_LAST_RIGHT[1] = s->motion_val[mot_xy + 2][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1091 P_LAST_BOTTOM[0]= s->motion_val[mot_xy + 2*mot_stride][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1092 P_LAST_BOTTOM[1]= s->motion_val[mot_xy + 2*mot_stride][1];
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
1093
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1094 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1095 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1096 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift);
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
1097
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
1098 /* special case for first line */
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1099 if ((mb_y == 0 || s->first_slice_line)) {
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1100 pred_x= P_LEFT[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1101 pred_y= P_LEFT[1];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
1102 } else {
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1103 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1104 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1105 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + 2][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1106 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + 2][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1107 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1] = (rel_ymax<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1108 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1109 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
1110
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1111 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1112 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1113
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1114 if(s->out_format == FMT_H263){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1115 pred_x = P_MEDIAN[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1116 pred_y = P_MEDIAN[1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1117 }else { /* mpeg1 at least */
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1118 pred_x= P_LEFT[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1119 pred_y= P_LEFT[1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1120 }
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
1121 }
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
1122 }
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1123 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
1124
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
1125 break;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1126 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1127
986e461dc072 Initial revision
glantau
parents:
diff changeset
1128 /* intra / predictive decision */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1129 xx = mb_x * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1130 yy = mb_y * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1131
986e461dc072 Initial revision
glantau
parents:
diff changeset
1132 pix = s->new_picture[0] + (yy * s->linesize) + xx;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1133 /* At this point (mx,my) are full-pell and the relative displacement */
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1134 ppix = ref_picture + ((yy+my) * s->linesize) + (xx+mx);
289
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
1135
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1136 sum = s->dsp.pix_sum(pix, s->linesize);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1137
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1138 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1139 // FIXME: MMX OPTIMIZE
289
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
1140 vard = (pix_norm(pix, ppix, s->linesize)+128)>>8;
608
98e39397636b better scene change detection
michaelni
parents: 587
diff changeset
1141
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1142 //printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1143 s->mb_var [s->mb_width * mb_y + mb_x] = varc;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1144 s->mc_mb_var[s->mb_width * mb_y + mb_x] = vard;
809
79de6308c34d fixing another assert q>0.0 issue caused by variance < 0, this fix allso changes the inter/intra decission very slightly -> all regression checksums need to be updated
michaelni
parents: 807
diff changeset
1145 s->mb_mean [s->mb_width * mb_y + mb_x] = (sum+128)>>8;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1146 s->mb_var_sum += varc;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1147 s->mc_mb_var_sum += vard;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1148 //printf("E%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1149
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1150 #if 0
233
3f5b72726118 - More work on preliminary bit rate control, just to be able to get an
pulento
parents: 232
diff changeset
1151 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
1152 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1153 #endif
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1154 if(s->flags&CODEC_FLAG_HQ){
608
98e39397636b better scene change detection
michaelni
parents: 587
diff changeset
1155 if (vard <= 64 || vard < varc)
98e39397636b better scene change detection
michaelni
parents: 587
diff changeset
1156 s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
98e39397636b better scene change detection
michaelni
parents: 587
diff changeset
1157 else
98e39397636b better scene change detection
michaelni
parents: 587
diff changeset
1158 s->scene_change_score+= 20;
98e39397636b better scene change detection
michaelni
parents: 587
diff changeset
1159
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1160 if (vard*2 + 200 > varc)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1161 mb_type|= MB_TYPE_INTRA;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1162 if (varc*2 + 200 > vard){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1163 mb_type|= MB_TYPE_INTER;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1164 if(s->me_method >= ME_EPZS)
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1165 fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1166 pred_x, pred_y, ref_picture, s->dsp.pix_abs16x16_x2,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1167 s->dsp.pix_abs16x16_y2, s->dsp.pix_abs16x16_xy2, 0);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1168 else
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1169 halfpel_motion_search( s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1170 pred_x, pred_y, ref_picture, s->dsp.pix_abs16x16_x2,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1171 s->dsp.pix_abs16x16_y2, s->dsp.pix_abs16x16_xy2, 0);
304
5753d57e7e6b fixing MVs in hq mode
michaelni
parents: 295
diff changeset
1172 }else{
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1173 mx <<=1;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1174 my <<=1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1175 }
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1176 if((s->flags&CODEC_FLAG_4MV)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1177 && !s->skip_me && varc>50 && vard>10){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1178 mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1179 mb_type|=MB_TYPE_INTER4V;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1180
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1181 set_p_mv_tables(s, mx, my, 0);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1182 }else
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1183 set_p_mv_tables(s, mx, my, 1);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1184 }else{
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1185 if (vard <= 64 || vard < varc) {
608
98e39397636b better scene change detection
michaelni
parents: 587
diff changeset
1186 s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1187 mb_type|= MB_TYPE_INTER;
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1188 if (s->me_method != ME_ZERO) {
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1189 if(s->me_method >= ME_EPZS)
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1190 dmin= fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1191 pred_x, pred_y, ref_picture, s->dsp.pix_abs16x16_x2, s->dsp.pix_abs16x16_y2,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1192 s->dsp.pix_abs16x16_xy2, 0);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1193 else
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1194 dmin= halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1195 pred_x, pred_y, ref_picture, s->dsp.pix_abs16x16_x2, s->dsp.pix_abs16x16_y2,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1196 s->dsp.pix_abs16x16_xy2, 0);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1197 if((s->flags&CODEC_FLAG_4MV)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1198 && !s->skip_me && varc>50 && vard>10){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1199 int dmin4= mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1200 if(dmin4 + 128 <dmin)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1201 mb_type= MB_TYPE_INTER4V;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1202 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1203 set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1204
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1205 } else {
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1206 mx <<=1;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1207 my <<=1;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1208 }
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1209 #if 0
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1210 if (vard < 10) {
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1211 skip++;
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1212 fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d",
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1213 skip, vard, varc, dmin);
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1214 }
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1215 #endif
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1216 }else{
608
98e39397636b better scene change detection
michaelni
parents: 587
diff changeset
1217 s->scene_change_score+= 20;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1218 mb_type|= MB_TYPE_INTRA;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1219 mx = 0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1220 my = 0;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1221 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1222 }
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
1223
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1224 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1225 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1226
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1227 int ff_estimate_motion_b(MpegEncContext * s,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1228 int mb_x, int mb_y, int16_t (*mv_table)[2], uint8_t *ref_picture, int f_code)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1229 {
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1230 int mx, my, range, dmin;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1231 int xmin, ymin, xmax, ymax;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1232 int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1233 int pred_x=0, pred_y=0;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1234 int P[10][2];
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1235 const int shift= 1+s->quarter_sample;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1236 const int mot_stride = s->mb_width + 2;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1237 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1238
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1239 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1240 rel_xmin= xmin - mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1241 rel_xmax= xmax - mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1242 rel_ymin= ymin - mb_y*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1243 rel_ymax= ymax - mb_y*16;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1244
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1245 switch(s->me_method) {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1246 case ME_ZERO:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1247 default:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1248 no_motion_search(s, &mx, &my);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1249 dmin = 0;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1250 mx-= mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1251 my-= mb_y*16;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1252 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1253 case ME_FULL:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1254 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1255 mx-= mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1256 my-= mb_y*16;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1257 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1258 case ME_LOG:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1259 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1260 mx-= mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1261 my-= mb_y*16;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1262 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1263 case ME_PHODS:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1264 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1265 mx-= mb_x*16;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1266 my-= mb_y*16;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1267 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1268 case ME_X1:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1269 case ME_EPZS:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1270 {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1271
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1272 P_LAST[0] = mv_table[mot_xy ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1273 P_LAST[1] = mv_table[mot_xy ][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1274 P_LEFT[0] = mv_table[mot_xy - 1][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1275 P_LEFT[1] = mv_table[mot_xy - 1][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1276 P_LAST_RIGHT[0] = mv_table[mot_xy + 1][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1277 P_LAST_RIGHT[1] = mv_table[mot_xy + 1][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1278 P_LAST_BOTTOM[0] = mv_table[mot_xy + mot_stride][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1279 P_LAST_BOTTOM[1] = mv_table[mot_xy + mot_stride][1];
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1280
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1281 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1282 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1283 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift);
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1284
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1285 /* special case for first line */
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1286 if ((mb_y == 0 || s->first_slice_line)) {
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1287 } else {
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1288 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1289 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1290 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1291 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1292 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1]= (rel_ymax<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1293 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1294 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1295
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1296 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1297 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1298 }
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1299 pred_x= P_LEFT[0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1300 pred_y= P_LEFT[1];
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1301 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1302 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1303
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1304 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1305 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1306
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1307 dmin= fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1308 pred_x, pred_y, ref_picture, s->dsp.pix_abs16x16_x2, s->dsp.pix_abs16x16_y2,
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1309 s->dsp.pix_abs16x16_xy2, 0);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1310 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1311 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1312 mv_table[mot_xy][0]= mx;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1313 mv_table[mot_xy][1]= my;
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1314 return dmin;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1315 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1316
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1317
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1318 static inline int check_bidir_mv(MpegEncContext * s,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1319 int mb_x, int mb_y,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1320 int motion_fx, int motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1321 int motion_bx, int motion_by,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1322 int pred_fx, int pred_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1323 int pred_bx, int pred_by)
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1324 {
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1325 //FIXME optimize?
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1326 //FIXME direct mode penalty
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1327 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1328 uint8_t *dest_y = s->me_scratchpad;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1329 uint8_t *ptr;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1330 int dxy;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1331 int src_x, src_y;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1332 int fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1333
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1334 fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->qscale;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1335
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1336 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1337 src_x = mb_x * 16 + (motion_fx >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1338 src_y = mb_y * 16 + (motion_fy >> 1);
765
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1339 src_x = clip(src_x, -16, s->width);
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1340 if (src_x == s->width)
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1341 dxy&= 2;
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1342 src_y = clip(src_y, -16, s->height);
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1343 if (src_y == s->height)
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1344 dxy&= 1;
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1345
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1346 ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1347 s->dsp.put_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1348
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1349 fbmin += (mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->qscale;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1350
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1351 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1352 src_x = mb_x * 16 + (motion_bx >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1353 src_y = mb_y * 16 + (motion_by >> 1);
765
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1354 src_x = clip(src_x, -16, s->width);
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1355 if (src_x == s->width)
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1356 dxy&= 2;
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1357 src_y = clip(src_y, -16, s->height);
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1358 if (src_y == s->height)
7f0d502a42c5 clip MVs for direct mode
michaelni
parents: 691
diff changeset
1359 dxy&= 1;
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1360
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1361 ptr = s->next_picture[0] + (src_y * s->linesize) + src_x;
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1362 s->dsp.avg_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1363
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1364 fbmin += s->dsp.pix_abs16x16(s->new_picture[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize);
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1365 return fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1366 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1367
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1368 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1369 static inline int bidir_refine(MpegEncContext * s,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1370 int mb_x, int mb_y)
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1371 {
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1372 const int mot_stride = s->mb_width + 2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1373 const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1374 int fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1375 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1376 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1377 int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1378 int pred_by= s->b_bidir_back_mv_table[xy-1][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1379 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1380 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1381 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1382 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1383
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1384 //FIXME do refinement and add flag
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1385
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1386 fbmin= check_bidir_mv(s, mb_x, mb_y,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1387 motion_fx, motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1388 motion_bx, motion_by,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1389 pred_fx, pred_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1390 pred_bx, pred_by);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1391
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1392 return fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1393 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1394
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1395 static inline int direct_search(MpegEncContext * s,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1396 int mb_x, int mb_y)
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1397 {
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1398 int P[10][2];
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1399 const int mot_stride = s->mb_width + 2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1400 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1401 int dmin, dmin2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1402 int motion_fx, motion_fy, motion_bx, motion_by, motion_bx0, motion_by0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1403 int motion_dx, motion_dy;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1404 const int motion_px= s->p_mv_table[mot_xy][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1405 const int motion_py= s->p_mv_table[mot_xy][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1406 const int time_pp= s->pp_time;
664
00a882f626bd interlaced mpeg4 b frame decoding
michaelni
parents: 651
diff changeset
1407 const int time_pb= s->pb_time;
00a882f626bd interlaced mpeg4 b frame decoding
michaelni
parents: 651
diff changeset
1408 const int time_bp= time_pp - time_pb;
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1409 int bx, by;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1410 int mx, my, mx2, my2;
587
4d41fe7eb780 b frame direct mode bugfix (bug found by CM <chenm001 at 163 dot com>)
michaelni
parents: 502
diff changeset
1411 uint8_t *ref_picture= s->me_scratchpad - (mb_x - 1 + (mb_y - 1)*s->linesize)*16;
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1412 int16_t (*mv_table)[2]= s->b_direct_mv_table;
620
a5aa53b6e648 warning patch by (Dominik Mierzejewski <dominik at rangers dot eu dot org>)
michaelni
parents: 612
diff changeset
1413 /* uint16_t *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; */ // f_code of the prev frame
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1414
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1415 /* thanks to iso-mpeg the rounding is different for the zero vector, so we need to handle that ... */
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1416 motion_fx= (motion_px*time_pb)/time_pp;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1417 motion_fy= (motion_py*time_pb)/time_pp;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1418 motion_bx0= (-motion_px*time_bp)/time_pp;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1419 motion_by0= (-motion_py*time_bp)/time_pp;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1420 motion_dx= motion_dy=0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1421 dmin2= check_bidir_mv(s, mb_x, mb_y,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1422 motion_fx, motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1423 motion_bx0, motion_by0,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1424 motion_fx, motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1425 motion_bx0, motion_by0) - s->qscale;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1426
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1427 motion_bx= motion_fx - motion_px;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1428 motion_by= motion_fy - motion_py;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1429 for(by=-1; by<2; by++){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1430 for(bx=-1; bx<2; bx++){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1431 uint8_t *dest_y = s->me_scratchpad + (by+1)*s->linesize*16 + (bx+1)*16;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1432 uint8_t *ptr;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1433 int dxy;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1434 int src_x, src_y;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1435 const int width= s->width;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1436 const int height= s->height;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1437
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1438 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1439 src_x = (mb_x + bx) * 16 + (motion_fx >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1440 src_y = (mb_y + by) * 16 + (motion_fy >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1441 src_x = clip(src_x, -16, width);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1442 if (src_x == width) dxy &= ~1;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1443 src_y = clip(src_y, -16, height);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1444 if (src_y == height) dxy &= ~2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1445
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1446 ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1447 s->dsp.put_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1448
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1449 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1450 src_x = (mb_x + bx) * 16 + (motion_bx >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1451 src_y = (mb_y + by) * 16 + (motion_by >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1452 src_x = clip(src_x, -16, width);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1453 if (src_x == width) dxy &= ~1;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1454 src_y = clip(src_y, -16, height);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1455 if (src_y == height) dxy &= ~2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1456
853
eacc2dd8fd9d * using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents: 847
diff changeset
1457 s->dsp.avg_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1458 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1459 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1460
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1461 P_LAST[0] = mv_table[mot_xy ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1462 P_LAST[1] = mv_table[mot_xy ][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1463 P_LEFT[0] = mv_table[mot_xy - 1][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1464 P_LEFT[1] = mv_table[mot_xy - 1][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1465 P_LAST_RIGHT[0] = mv_table[mot_xy + 1][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1466 P_LAST_RIGHT[1] = mv_table[mot_xy + 1][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1467 P_LAST_BOTTOM[0] = mv_table[mot_xy + mot_stride][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1468 P_LAST_BOTTOM[1] = mv_table[mot_xy + mot_stride][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1469 /*
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1470 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1471 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1472 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1473 */
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1474 /* special case for first line */
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1475 if ((mb_y == 0 || s->first_slice_line)) {
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1476 } else {
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1477 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1478 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1479 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1480 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1481
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1482 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1483 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1484 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1485 dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, -16, -16, 15, 15, ref_picture);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1486 if(mx==0 && my==0) dmin=99999999; // not representable, due to rounding stuff
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1487 if(dmin2<dmin){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1488 dmin= dmin2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1489 mx=0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1490 my=0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1491 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1492 #if 1
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1493 mx2= mx= mx*2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1494 my2= my= my*2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1495 for(by=-1; by<2; by++){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1496 if(my2+by < -32) continue;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1497 for(bx=-1; bx<2; bx++){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1498 if(bx==0 && by==0) continue;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1499 if(mx2+bx < -32) continue;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1500 dmin2= check_bidir_mv(s, mb_x, mb_y,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1501 mx2+bx+motion_fx, my2+by+motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1502 mx2+bx+motion_bx, my2+by+motion_by,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1503 mx2+bx+motion_fx, my2+by+motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1504 motion_bx, motion_by) - s->qscale;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1505
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1506 if(dmin2<dmin){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1507 dmin=dmin2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1508 mx= mx2 + bx;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1509 my= my2 + by;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1510 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1511 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1512 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1513 #else
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1514 mx*=2; my*=2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1515 #endif
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1516 if(mx==0 && my==0){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1517 motion_bx= motion_bx0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1518 motion_by= motion_by0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1519 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1520
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1521 s->b_direct_mv_table[mot_xy][0]= mx;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1522 s->b_direct_mv_table[mot_xy][1]= my;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1523 s->b_direct_forw_mv_table[mot_xy][0]= motion_fx + mx;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1524 s->b_direct_forw_mv_table[mot_xy][1]= motion_fy + my;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1525 s->b_direct_back_mv_table[mot_xy][0]= motion_bx + mx;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1526 s->b_direct_back_mv_table[mot_xy][1]= motion_by + my;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1527 return dmin;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1528 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1529
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1530 void ff_estimate_b_frame_motion(MpegEncContext * s,
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1531 int mb_x, int mb_y)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1532 {
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1533 const int quant= s->qscale;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1534 int fmin, bmin, dmin, fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1535 int type=0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1536
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1537 dmin= direct_search(s, mb_x, mb_y);
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1538
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1539 fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, s->last_picture[0], s->f_code);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1540 bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, s->next_picture[0], s->b_code) - quant;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1541 //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1542
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1543 fbmin= bidir_refine(s, mb_x, mb_y);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1544
612
c0005de2be59 new ratecontrol code
michaelni
parents: 608
diff changeset
1545 {
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1546 int score= dmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1547 type=MB_TYPE_DIRECT;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1548
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1549 if(fmin<score){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1550 score=fmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1551 type= MB_TYPE_FORWARD;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1552 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1553 if(bmin<score){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1554 score=bmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1555 type= MB_TYPE_BACKWARD;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1556 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1557 if(fbmin<score){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1558 score=fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1559 type= MB_TYPE_BIDIR;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1560 }
807
0e1d375c537f fixing q>0.0 assert failure caused by overflow of variance for b frames
michaelni
parents: 765
diff changeset
1561 score= ((unsigned)(score*score + 128*256))>>16;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1562 s->mc_mb_var_sum += score;
612
c0005de2be59 new ratecontrol code
michaelni
parents: 608
diff changeset
1563 s->mc_mb_var[mb_y*s->mb_width + mb_x] = score; //FIXME use SSD
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1564 }
612
c0005de2be59 new ratecontrol code
michaelni
parents: 608
diff changeset
1565
c0005de2be59 new ratecontrol code
michaelni
parents: 608
diff changeset
1566 if(s->flags&CODEC_FLAG_HQ){
c0005de2be59 new ratecontrol code
michaelni
parents: 608
diff changeset
1567 type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT; //FIXME something smarter
c0005de2be59 new ratecontrol code
michaelni
parents: 608
diff changeset
1568 }
c0005de2be59 new ratecontrol code
michaelni
parents: 608
diff changeset
1569
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1570 /*
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1571 {
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1572 static int count=0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1573 static int sum=0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1574 if(type==MB_TYPE_DIRECT){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1575 int diff= ABS(s->b_forw_mv_table)
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1576 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1577 }*/
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1578
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1579 s->mb_type[mb_y*s->mb_width + mb_x]= type;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1580 /* if(mb_y==0 && mb_x==0) printf("\n");
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1581 if(mb_x==0) printf("\n");
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1582 printf("%d", av_log2(type));
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1583 */
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1584 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1585
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1586 /* find best f_code for ME which do unlimited searches */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1587 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1588 {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1589 if(s->me_method>=ME_EPZS){
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1590 int score[8];
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1591 int i, y;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1592 UINT8 * fcode_tab= s->fcode_tab;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1593 int best_fcode=-1;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1594 int best_score=-10000000;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1595
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1596 for(i=0; i<8; i++) score[i]= s->mb_num*(8-i); //FIXME *2 and all other too so its the same but nicer
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1597
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1598 for(y=0; y<s->mb_height; y++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1599 int x;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1600 int xy= (y+1)* (s->mb_width+2) + 1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1601 i= y*s->mb_width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1602 for(x=0; x<s->mb_width; x++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1603 if(s->mb_type[i] & type){
847
f3c369b8ddca reversing header game
michaelni
parents: 809
diff changeset
1604 int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV],
f3c369b8ddca reversing header game
michaelni
parents: 809
diff changeset
1605 fcode_tab[mv_table[xy][1] + MAX_MV]);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1606 int j;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1607
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1608 for(j=0; j<fcode && j<8; j++){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1609 if(s->pict_type==B_TYPE || s->mc_mb_var[i] < s->mb_var[i])
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1610 score[j]-= 170;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1611 }
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1612 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1613 i++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1614 xy++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1615 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1616 }
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1617
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1618 for(i=1; i<8; i++){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1619 if(score[i] > best_score){
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1620 best_score= score[i];
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1621 best_fcode= i;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1622 }
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1623 // printf("%d %d\n", i, score[i]);
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1624 }
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1625
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1626 // printf("fcode: %d type: %d\n", i, s->pict_type);
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1627 return best_fcode;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1628 /* for(i=0; i<=MAX_FCODE; i++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1629 printf("%d ", mv_num[i]);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1630 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1631 printf("\n");*/
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1632 }else{
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1633 return 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1634 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1635 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1636
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1637 void ff_fix_long_p_mvs(MpegEncContext * s)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1638 {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1639 const int f_code= s->f_code;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1640 int y;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1641 UINT8 * fcode_tab= s->fcode_tab;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1642 //int clip=0;
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1643 //int noclip=0;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1644 /* clip / convert to intra 16x16 type MVs */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1645 for(y=0; y<s->mb_height; y++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1646 int x;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1647 int xy= (y+1)* (s->mb_width+2)+1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1648 int i= y*s->mb_width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1649 for(x=0; x<s->mb_width; x++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1650 if(s->mb_type[i]&MB_TYPE_INTER){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1651 if( fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1652 || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1653 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1654 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1655 s->mb_type[i] &= ~MB_TYPE_INTER;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1656 s->mb_type[i] |= MB_TYPE_INTRA;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1657 s->p_mv_table[xy][0] = 0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1658 s->p_mv_table[xy][1] = 0;
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1659 //clip++;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1660 }
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1661 //else
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1662 // noclip++;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1663 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1664 xy++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1665 i++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1666 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1667 }
455
7f2d2be2aee9 dont double check vectors
michaelni
parents: 429
diff changeset
1668 //printf("%d no:%d %d//\n", clip, noclip, f_code);
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1669 if(s->flags&CODEC_FLAG_4MV){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1670 const int wrap= 2+ s->mb_width*2;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1671
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1672 /* clip / convert to intra 8x8 type MVs */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1673 for(y=0; y<s->mb_height; y++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1674 int xy= (y*2 + 1)*wrap + 1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1675 int i= y*s->mb_width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1676 int x;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1677
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1678 for(x=0; x<s->mb_width; x++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1679 if(s->mb_type[i]&MB_TYPE_INTER4V){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1680 int block;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1681 for(block=0; block<4; block++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1682 int off= (block& 1) + (block>>1)*wrap;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1683 int mx= s->motion_val[ xy + off ][0];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1684 int my= s->motion_val[ xy + off ][1];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1685
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1686 if( fcode_tab[mx + MAX_MV] > f_code
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1687 || fcode_tab[mx + MAX_MV] == 0
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1688 || fcode_tab[my + MAX_MV] > f_code
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1689 || fcode_tab[my + MAX_MV] == 0 ){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1690 s->mb_type[i] &= ~MB_TYPE_INTER4V;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1691 s->mb_type[i] |= MB_TYPE_INTRA;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1692 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1693 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1694 }
502
f74798aca30e 4mv bugfix
michaelni
parents: 455
diff changeset
1695 xy+=2;
f74798aca30e 4mv bugfix
michaelni
parents: 455
diff changeset
1696 i++;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1697 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1698 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1699 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1700 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1701
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1702 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1703 {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1704 int y;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1705 UINT8 * fcode_tab= s->fcode_tab;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1706
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1707 /* clip / convert to intra 16x16 type MVs */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1708 for(y=0; y<s->mb_height; y++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1709 int x;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1710 int xy= (y+1)* (s->mb_width+2)+1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1711 int i= y*s->mb_width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1712 for(x=0; x<s->mb_width; x++){
691
199b324b2693 fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents: 690
diff changeset
1713 if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code
199b324b2693 fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents: 690
diff changeset
1714 || fcode_tab[mv_table[xy][0] + MAX_MV] == 0){
199b324b2693 fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents: 690
diff changeset
1715 if(mv_table[xy][0]>0) mv_table[xy][0]= (16<<f_code)-1;
199b324b2693 fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents: 690
diff changeset
1716 else mv_table[xy][0]= -(16<<f_code);
199b324b2693 fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents: 690
diff changeset
1717 }
199b324b2693 fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents: 690
diff changeset
1718 if( fcode_tab[mv_table[xy][1] + MAX_MV] > f_code
199b324b2693 fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents: 690
diff changeset
1719 || fcode_tab[mv_table[xy][1] + MAX_MV] == 0){
199b324b2693 fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents: 690
diff changeset
1720 if(mv_table[xy][1]>0) mv_table[xy][1]= (16<<f_code)-1;
199b324b2693 fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents: 690
diff changeset
1721 else mv_table[xy][1]= -(16<<f_code);
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1722 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1723 xy++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1724 i++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1725 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1726 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1727 }