annotate motion_est.c @ 367:abbed9f28d75 libavcodec

- MPEG-4 B frames coding option for ffmpeg.c - Warning fixes.
author pulento
date Mon, 06 May 2002 19:32:55 +0000
parents d359db02fc90
children ba9c3b8088c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * Motion estimation
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2000,2001 Gerard Lantau.
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
19 *
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
20 * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 #include "dsputil.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 #include "mpegvideo.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
27
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
28 #define ABS(a) ((a)>0 ? (a) : -(a))
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
29 #define MAX(a,b) ((a) > (b) ? (a) : (b))
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
30 #define INTER_BIAS 257
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
31
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
32 static int halfpel_motion_search(MpegEncContext * s,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 int *mx_ptr, int *my_ptr, int dmin,
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
34 int xmin, int ymin, int xmax, int ymax,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
35 int pred_x, int pred_y, uint8_t *ref_picture);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
36
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 static int pix_sum(UINT8 * pix, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
40
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 s += pix[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 s += pix[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 s += pix[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 s += pix[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 s += pix[4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 s += pix[5];
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 s += pix[6];
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 s += pix[7];
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 pix += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 pix += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
58
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
59 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
60 {
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
61 int s, i, j;
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 s = 0;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
64 for (i = 0; i < 16; i++) {
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
65 for (j = 0; j < 16; j += 8) {
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
66 s += ABS(pix[0]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
67 s += ABS(pix[1]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
68 s += ABS(pix[2]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
69 s += ABS(pix[3]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
70 s += ABS(pix[4]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
71 s += ABS(pix[5]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
72 s += ABS(pix[6]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
73 s += ABS(pix[7]-mean);
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
74 pix += 8;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
75 }
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
76 pix += line_size - 16;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
77 }
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
78 return s;
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
79 }
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
80
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 static int pix_norm1(UINT8 * pix, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 UINT32 *sq = squareTbl + 256;
986e461dc072 Initial revision
glantau
parents:
diff changeset
85
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 s += sq[pix[0]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 s += sq[pix[1]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 s += sq[pix[2]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 s += sq[pix[3]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 s += sq[pix[4]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 s += sq[pix[5]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 s += sq[pix[6]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 s += sq[pix[7]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 pix += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 pix += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
103
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 int s, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 UINT32 *sq = squareTbl + 256;
986e461dc072 Initial revision
glantau
parents:
diff changeset
108
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 for (i = 0; i < 16; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 for (j = 0; j < 16; j += 8) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 s += sq[pix1[0] - pix2[0]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 s += sq[pix1[1] - pix2[1]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 s += sq[pix1[2] - pix2[2]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 s += sq[pix1[3] - pix2[3]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 s += sq[pix1[4] - pix2[4]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 s += sq[pix1[5] - pix2[5]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 s += sq[pix1[6] - pix2[6]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 s += sq[pix1[7] - pix2[7]];
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 pix1 += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 pix2 += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 pix1 += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 pix2 += line_size - 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
128
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 static void no_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 int *mx_ptr, int *my_ptr)
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 *mx_ptr = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 *my_ptr = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
135
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 static int full_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 int *mx_ptr, int *my_ptr, int range,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
138 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 int x1, y1, x2, y2, xx, yy, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 int mx, my, dmin, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
143
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 xx = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 yy = 16 * s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 x2 = xx + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 y1 = yy - range + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 y2 = yy + range - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 dmin = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 for (y = y1; y <= y2; y++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 for (x = x1; x <= x2; x++) {
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
164 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x,
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
165 s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 if (d < dmin ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 (d == dmin &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 (abs(x - xx) + abs(y - yy)) <
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 dmin = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
176
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
179
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 return dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
188
986e461dc072 Initial revision
glantau
parents:
diff changeset
189
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 static int log_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
191 int *mx_ptr, int *my_ptr, int range,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
192 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 int x1, y1, x2, y2, xx, yy, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 int mx, my, dmin, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
197
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 xx = s->mb_x << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 yy = s->mb_y << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
200
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 /* Left limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 x1 = xx - 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 /* Right limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 x2 = xx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
210
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 /* Upper limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
215
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 /* Lower limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 y2 = yy + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
220
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 dmin = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
225
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 for (y = y1; y <= y2; y += range) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 for (x = x1; x <= x2; x += range) {
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
229 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 dmin = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
237
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 range = range >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
239
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 x1 = mx - 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 x2 = mx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
247
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 y1 = my - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
251
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 y2 = my + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
253 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
255
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 } while (range >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
257
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 return dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
265
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 static int phods_motion_search(MpegEncContext * s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 int *mx_ptr, int *my_ptr, int range,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
268 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 int mx, my, dminx, dminy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 UINT8 *pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
273
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 xx = s->mb_x << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 yy = s->mb_y << 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
276
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 /* Left limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 x1 = xx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
281
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 /* Right limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 x2 = xx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
286
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 /* Upper limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 y1 = yy - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
291
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 /* Lower limit */
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 y2 = yy + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
296
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
300
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 x = xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 y = yy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 dminx = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 dminy = 0x7fffffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
306
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 lastx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 for (x = x1; x <= x2; x += range) {
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
309 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 dminx = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 mx = x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
315
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 x = lastx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 for (y = y1; y <= y2; y += range) {
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
318 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 dminy = d;
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 my = y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
324
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 range = range >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
326
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 x = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 y = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 x1 = mx - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 if (x1 < xmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 x1 = xmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
332
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 x2 = mx + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 if (x2 > xmax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
335 x2 = xmax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
336
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 y1 = my - range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 if (y1 < ymin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
339 y1 = ymin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
340
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 y2 = my + range;
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 if (y2 > ymax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 y2 = ymax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
344
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 } while (range >= 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
346
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
350
986e461dc072 Initial revision
glantau
parents:
diff changeset
351 /* half pixel search */
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 *mx_ptr = mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 *my_ptr = my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
354 return dminy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
356
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
357
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
358 #define Z_THRESHOLD 256
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
359
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
360 #define CHECK_MV(x,y)\
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
361 {\
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
362 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
363 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
364 if(d<dmin){\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
365 best[0]=x;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
366 best[1]=y;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
367 dmin=d;\
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
368 }\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
369 }
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
370
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
371 #define CHECK_MV_DIR(x,y,new_dir)\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
372 {\
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
373 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
374 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
375 if(d<dmin){\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
376 best[0]=x;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
377 best[1]=y;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
378 dmin=d;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
379 next_dir= new_dir;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
380 }\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
381 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
382
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
383 #define CHECK_MV4(x,y)\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
384 {\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
385 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
386 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
387 if(d<dmin){\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
388 best[0]=x;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
389 best[1]=y;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
390 dmin=d;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
391 }\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
392 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
393
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
394 #define CHECK_MV4_DIR(x,y,new_dir)\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
395 {\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
396 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
397 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
398 if(d<dmin){\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
399 best[0]=x;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
400 best[1]=y;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
401 dmin=d;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
402 next_dir= new_dir;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
403 }\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
404 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
405
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
406
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
407 #define check(x,y,S,v)\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
408 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d xmin" #v, (x), (y), s->mb_x, s->mb_y);\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
409 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d xmax" #v, (x), (y), s->mb_x, s->mb_y);\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
410 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d ymin" #v, (x), (y), s->mb_x, s->mb_y);\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
411 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d ymax" #v, (x), (y), s->mb_x, s->mb_y);\
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
412
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
413
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
414 static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
415 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
416 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
417 int xmin, int ymin, int xmax, int ymax, int shift)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
418 {
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
419 int next_dir=-1;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
420
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
421 for(;;){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
422 int d;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
423 const int dir= next_dir;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
424 const int x= best[0];
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
425 const int y= best[1];
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
426 next_dir=-1;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
427
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
428 //printf("%d", dir);
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
429 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
430 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
431 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
432 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
433
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
434 if(next_dir==-1){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
435 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
436 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
437 }
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
438
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
439 /* for(;;){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
440 int d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
441 const int x= best[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
442 const int y= best[1];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
443 const int last_min=dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
444 if(x>xmin) CHECK_MV(x-1, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
445 if(y>xmin) CHECK_MV(x , y-1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
446 if(x<xmax) CHECK_MV(x+1, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
447 if(y<xmax) CHECK_MV(x , y+1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
448 if(x>xmin && y>ymin) CHECK_MV(x-1, y-1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
449 if(x>xmin && y<ymax) CHECK_MV(x-1, y+1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
450 if(x<xmax && y>ymin) CHECK_MV(x+1, y-1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
451 if(x<xmax && y<ymax) CHECK_MV(x+1, y+1)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
452 if(x-1>xmin) CHECK_MV(x-2, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
453 if(y-1>xmin) CHECK_MV(x , y-2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
454 if(x+1<xmax) CHECK_MV(x+2, y )
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
455 if(y+1<xmax) CHECK_MV(x , y+2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
456 if(x-1>xmin && y-1>ymin) CHECK_MV(x-2, y-2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
457 if(x-1>xmin && y+1<ymax) CHECK_MV(x-2, y+2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
458 if(x+1<xmax && y-1>ymin) CHECK_MV(x+2, y-2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
459 if(x+1<xmax && y+1<ymax) CHECK_MV(x+2, y+2)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
460 if(dmin==last_min) return dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
461 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
462 */
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
463 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
464
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
465 static inline int small_diamond_search4MV(MpegEncContext * s, int *best, int dmin,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
466 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
467 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
468 int xmin, int ymin, int xmax, int ymax, int shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
469 {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
470 int next_dir=-1;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
471
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
472 for(;;){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
473 int d;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
474 const int dir= next_dir;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
475 const int x= best[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
476 const int y= best[1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
477 next_dir=-1;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
478
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
479 //printf("%d", dir);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
480 if(dir!=2 && x>xmin) CHECK_MV4_DIR(x-1, y , 0)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
481 if(dir!=3 && y>ymin) CHECK_MV4_DIR(x , y-1, 1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
482 if(dir!=0 && x<xmax) CHECK_MV4_DIR(x+1, y , 2)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
483 if(dir!=1 && y<ymax) CHECK_MV4_DIR(x , y+1, 3)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
484
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
485 if(next_dir==-1){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
486 return dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
487 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
488 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
489 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
490
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
491 static inline int snake_search(MpegEncContext * s, int *best, int dmin,
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
492 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
493 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
494 int xmin, int ymin, int xmax, int ymax, int shift)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
495 {
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
496 int dir=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
497 int c=1;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
498 static int x_dir[8]= {1,1,0,-1,-1,-1, 0, 1};
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
499 static int y_dir[8]= {0,1,1, 1, 0,-1,-1,-1};
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
500 int fails=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
501 int last_d[2]={dmin, dmin};
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
502
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
503 /*static int good=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
504 static int bad=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
505 static int point=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
506
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
507 point++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
508 if(256*256*256*64%point==0)
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
509 {
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
510 printf("%d %d %d\n", good, bad, point);
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
511 }*/
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
512
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
513 for(;;){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
514 int x= best[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
515 int y= best[1];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
516 int d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
517 x+=x_dir[dir];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
518 y+=y_dir[dir];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
519 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
520 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
521 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
522 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
523 d = dmin + 10000; //FIXME smarter boundary handling
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
524 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
525 if(d<dmin){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
526 best[0]=x;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
527 best[1]=y;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
528 dmin=d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
529
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
530 if(last_d[1] - last_d[0] > last_d[0] - d) c= -c;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
531 dir+=c;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
532
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
533 fails=0;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
534 //good++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
535 last_d[1]=last_d[0];
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
536 last_d[0]=d;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
537 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
538 //bad++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
539 if(fails){
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
540 if(fails>=3) return dmin;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
541 }else{
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
542 c= -c;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
543 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
544 dir+=c*2;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
545 fails++;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
546 }
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
547 dir&=7;
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
548 }
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
549 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
550
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
551 static int epzs_motion_search(MpegEncContext * s,
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
552 int *mx_ptr, int *my_ptr,
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
553 int P[5][2], int pred_x, int pred_y,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
554 int xmin, int ymin, int xmax, int ymax, uint8_t * ref_picture)
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
555 {
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
556 int best[2]={0, 0};
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
557 int d, dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
558 UINT8 *new_pic, *old_pic;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
559 const int pic_stride= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
560 const int pic_xy= (s->mb_y*pic_stride + s->mb_x)*16;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
561 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
562 int quant= s->qscale; // qscale of the prev frame
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
563 const int shift= 1+s->quarter_sample;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
564
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
565 new_pic = s->new_picture[0] + pic_xy;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
566 old_pic = ref_picture + pic_xy;
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
567
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
568 dmin = pix_abs16x16(new_pic, old_pic, pic_stride);
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
569 if(dmin<Z_THRESHOLD){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
570 *mx_ptr= 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
571 *my_ptr= 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
572 //printf("Z");
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
573 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
574 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
575
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
576 /* first line */
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
577 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
578 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
579 }else{
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
580 CHECK_MV(P[4][0]>>shift, P[4][1]>>shift)
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
581 if(dmin<Z_THRESHOLD){
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
582 *mx_ptr= P[4][0]>>shift;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
583 *my_ptr= P[4][1]>>shift;
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
584 //printf("M\n");
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
585 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
586 }
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
587 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
588 CHECK_MV(P[2][0]>>shift, P[2][1]>>shift)
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
589 CHECK_MV(P[3][0]>>shift, P[3][1]>>shift)
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
590 }
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
591 CHECK_MV(P[0][0]>>shift, P[0][1]>>shift)
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
592
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
593 //check(best[0],best[1],0, b0)
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
594 if(s->me_method==ME_EPZS)
281
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
595 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride,
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
596 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
597 else
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
598 dmin= snake_search(s, best, dmin, new_pic, old_pic, pic_stride,
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
599 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
1fc96b02142e mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents: 280
diff changeset
600 //check(best[0],best[1],0, b1)
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
601 *mx_ptr= best[0];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
602 *my_ptr= best[1];
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
603
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
604 // printf("%d %d %d \n", best[0], best[1], dmin);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
605 return dmin;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
606 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
607
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
608 static int epzs_motion_search4(MpegEncContext * s, int block,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
609 int *mx_ptr, int *my_ptr,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
610 int P[6][2], int pred_x, int pred_y,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
611 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
612 {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
613 int best[2]={0, 0};
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
614 int d, dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
615 UINT8 *new_pic, *old_pic;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
616 const int pic_stride= s->linesize;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
617 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
618 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
619 int quant= s->qscale; // qscale of the prev frame
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
620 const int shift= 1+s->quarter_sample;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
621
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
622 new_pic = s->new_picture[0] + pic_xy;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
623 old_pic = ref_picture + pic_xy;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
624
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
625 dmin = pix_abs8x8(new_pic, old_pic, pic_stride);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
626
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
627 /* first line */
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
628 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
629 CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
630 }else{
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
631 CHECK_MV4(P[4][0]>>shift, P[4][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
632 if(dmin<Z_THRESHOLD){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
633 *mx_ptr= P[4][0]>>shift;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
634 *my_ptr= P[4][1]>>shift;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
635 //printf("M\n");
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
636 return dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
637 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
638 CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
639 CHECK_MV4(P[2][0]>>shift, P[2][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
640 CHECK_MV4(P[3][0]>>shift, P[3][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
641 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
642 CHECK_MV4(P[0][0]>>shift, P[0][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
643 CHECK_MV4(P[5][0]>>shift, P[5][1]>>shift)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
644
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
645 //check(best[0],best[1],0, b0)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
646 dmin= small_diamond_search4MV(s, best, dmin, new_pic, old_pic, pic_stride,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
647 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
648 //check(best[0],best[1],0, b1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
649 *mx_ptr= best[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
650 *my_ptr= best[1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
651
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
652 // printf("%d %d %d \n", best[0], best[1], dmin);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
653 return dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
654 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
655
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
656 #define CHECK_HALF_MV(suffix, x, y) \
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
657 d= pix_abs16x16_ ## suffix(pix, ptr+((x)>>1), s->linesize);\
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
658 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
659 if(d<dminh){\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
660 dminh= d;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
661 mx= mx1 + x;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
662 my= my1 + y;\
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
663 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
664
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
665 #define CHECK_HALF_MV4(suffix, x, y) \
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
666 d= pix_abs8x8_ ## suffix(pix, ptr+((x)>>1), s->linesize);\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
667 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
668 if(d<dminh){\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
669 dminh= d;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
670 mx= mx1 + x;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
671 my= my1 + y;\
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
672 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
673
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
674 /* The idea would be to make half pel ME after Inter/Intra decision to
986e461dc072 Initial revision
glantau
parents:
diff changeset
675 save time. */
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
676 static inline int halfpel_motion_search(MpegEncContext * s,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
677 int *mx_ptr, int *my_ptr, int dmin,
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
678 int xmin, int ymin, int xmax, int ymax,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
679 int pred_x, int pred_y, uint8_t *ref_picture)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 {
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
681 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
682 const int quant= s->qscale;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
683 int pen_x, pen_y;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
684 int mx, my, mx1, my1, d, xx, yy, dminh;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
685 UINT8 *pix, *ptr;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
686
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
687 mx = *mx_ptr;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
688 my = *my_ptr;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
689 ptr = ref_picture + (my * s->linesize) + mx;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
690
986e461dc072 Initial revision
glantau
parents:
diff changeset
691 xx = 16 * s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
692 yy = 16 * s->mb_y;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
693 pix = s->new_picture[0] + (yy * s->linesize) + xx;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
694
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
695 dminh = dmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
696
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
697 if (mx > xmin && mx < xmax &&
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
698 my > ymin && my < ymax) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
699
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
700 mx= mx1= 2*(mx - xx);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
701 my= my1= 2*(my - yy);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
702 if(dmin < Z_THRESHOLD && mx==0 && my==0){
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
703 *mx_ptr = 0;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
704 *my_ptr = 0;
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
705 return dmin;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
706 }
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
707
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
708 pen_x= pred_x + mx;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
709 pen_y= pred_y + my;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
710
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
711 ptr-= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
712 CHECK_HALF_MV(xy2, -1, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
713 CHECK_HALF_MV(y2 , 0, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
714 CHECK_HALF_MV(xy2, +1, -1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
715
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
716 ptr+= s->linesize;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
717 CHECK_HALF_MV(x2 , -1, 0)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
718 CHECK_HALF_MV(x2 , +1, 0)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
719 CHECK_HALF_MV(xy2, -1, +1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
720 CHECK_HALF_MV(y2 , 0, +1)
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
721 CHECK_HALF_MV(xy2, +1, +1)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
722
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
723 }else{
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
724 mx= 2*(mx - xx);
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
725 my= 2*(my - yy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
726 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
727
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
728 *mx_ptr = mx;
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
729 *my_ptr = my;
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
730 return dminh;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
731 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
732
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
733 static inline void halfpel_motion_search4(MpegEncContext * s,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
734 int *mx_ptr, int *my_ptr, int dmin,
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
735 int xmin, int ymin, int xmax, int ymax,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
736 int pred_x, int pred_y, int block_x, int block_y,
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
737 uint8_t *ref_picture)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
738 {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
739 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
740 const int quant= s->qscale;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
741 int pen_x, pen_y;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
742 int mx, my, mx1, my1, d, xx, yy, dminh;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
743 UINT8 *pix, *ptr;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
744
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
745 xx = 8 * block_x;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
746 yy = 8 * block_y;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
747 pix = s->new_picture[0] + (yy * s->linesize) + xx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
748
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
749 mx = *mx_ptr;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
750 my = *my_ptr;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
751 ptr = ref_picture + ((yy+my) * s->linesize) + xx + mx;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
752
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
753 dminh = dmin;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
754
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
755 if (mx > xmin && mx < xmax &&
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
756 my > ymin && my < ymax) {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
757
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
758 mx= mx1= 2*mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
759 my= my1= 2*my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
760 if(dmin < Z_THRESHOLD && mx==0 && my==0){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
761 *mx_ptr = 0;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
762 *my_ptr = 0;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
763 return;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
764 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
765
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
766 pen_x= pred_x + mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
767 pen_y= pred_y + my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
768
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
769 ptr-= s->linesize;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
770 CHECK_HALF_MV4(xy2, -1, -1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
771 CHECK_HALF_MV4(y2 , 0, -1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
772 CHECK_HALF_MV4(xy2, +1, -1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
773
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
774 ptr+= s->linesize;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
775 CHECK_HALF_MV4(x2 , -1, 0)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
776 CHECK_HALF_MV4(x2 , +1, 0)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
777 CHECK_HALF_MV4(xy2, -1, +1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
778 CHECK_HALF_MV4(y2 , 0, +1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
779 CHECK_HALF_MV4(xy2, +1, +1)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
780
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
781 }else{
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
782 mx*=2;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
783 my*=2;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
784 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
785
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
786 *mx_ptr = mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
787 *my_ptr = my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
788 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
789
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
790 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my)
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
791 {
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
792 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
793
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
794 s->p_mv_table[xy][0] = mx;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
795 s->p_mv_table[xy][1] = my;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
796
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
797 /* has allready been set to the 4 MV if 4MV is done */
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
798 if(!(s->flags&CODEC_FLAG_4MV)){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
799 int mot_xy= s->block_index[0];
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 s->motion_val[mot_xy ][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
802 s->motion_val[mot_xy ][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
803 s->motion_val[mot_xy+1][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
804 s->motion_val[mot_xy+1][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
805
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
806 mot_xy += s->block_wrap[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
807 s->motion_val[mot_xy ][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
808 s->motion_val[mot_xy ][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
809 s->motion_val[mot_xy+1][0]= mx;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
810 s->motion_val[mot_xy+1][1]= my;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
811 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
812 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
813
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
814 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
815 {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
816 *range = 8 * (1 << (f_code - 1));
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
817 /* XXX: temporary kludge to avoid overflow for msmpeg4 */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
818 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
819 *range *= 2;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
820
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
821 if (s->unrestricted_mv) {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
822 *xmin = -16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
823 *ymin = -16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
824 if (s->h263_plus)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
825 *range *= 2;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
826 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
827 *xmax = s->mb_width*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
828 *ymax = s->mb_height*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
829 }else {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
830 /* 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
831 (cuz the drawn edge isnt large enough))*/
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
832 *xmax = s->width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
833 *ymax = s->height;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
834 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
835 } else {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
836 *xmin = 0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
837 *ymin = 0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
838 *xmax = s->mb_width*16 - 16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
839 *ymax = s->mb_height*16 - 16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
840 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
841 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
842
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
843 void ff_estimate_p_frame_motion(MpegEncContext * s,
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
844 int mb_x, int mb_y)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
845 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
846 UINT8 *pix, *ppix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
847 int sum, varc, vard, mx, my, range, dmin, xx, yy;
986e461dc072 Initial revision
glantau
parents:
diff changeset
848 int xmin, ymin, xmax, ymax;
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
849 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
850 int pred_x=0, pred_y=0;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
851 int P[6][2];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
852 const int shift= 1+s->quarter_sample;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
853 int mb_type=0;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
854 uint8_t *ref_picture= s->last_picture[0];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
855
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
856 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
857
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
858 switch(s->me_method) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
859 case ME_ZERO:
986e461dc072 Initial revision
glantau
parents:
diff changeset
860 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
861 no_motion_search(s, &mx, &my);
986e461dc072 Initial revision
glantau
parents:
diff changeset
862 dmin = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
863 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
864 case ME_FULL:
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
865 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
866 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
867 case ME_LOG:
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
868 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
869 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
870 case ME_PHODS:
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
871 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
872 break;
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
873 case ME_X1:
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
874 case ME_EPZS:
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
875 {
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
876 const int mot_stride = s->block_wrap[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
877 const int mot_xy = s->block_index[0];
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
878
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
879 rel_xmin= xmin - mb_x*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
880 rel_xmax= xmax - mb_x*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
881 rel_ymin= ymin - mb_y*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
882 rel_ymax= ymax - mb_y*16;
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
883
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
884 P[0][0] = s->motion_val[mot_xy ][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
885 P[0][1] = s->motion_val[mot_xy ][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
886 P[1][0] = s->motion_val[mot_xy - 1][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
887 P[1][1] = s->motion_val[mot_xy - 1][1];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
888 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
889
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
890 /* special case for first line */
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
891 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
892 P[4][0] = P[1][0];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
893 P[4][1] = P[1][1];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
894 } else {
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
895 P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
896 P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
897 P[3][0] = s->motion_val[mot_xy - mot_stride + 2 ][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
898 P[3][1] = s->motion_val[mot_xy - mot_stride + 2 ][1];
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
899 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
900 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
901 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
902
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
903 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
904 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
905 }
288
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
906 if(s->out_format == FMT_H263){
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
907 pred_x = P[4][0];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
908 pred_y = P[4][1];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
909 }else { /* mpeg1 at least */
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
910 pred_x= P[1][0];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
911 pred_y= P[1][1];
f82cce6cb182 10l (motion_val was uninitilized)
michaelni
parents: 284
diff changeset
912 }
280
3dc1ca4ba717 fixing epzs & mpeg1 (hopefully now really ...)
michaelni
parents: 277
diff changeset
913 }
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
914 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
915
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
916 mx+= mb_x*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
917 my+= mb_y*16;
277
5cb2978e701f new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents: 275
diff changeset
918 break;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
919 }
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
920
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
921 if(s->flags&CODEC_FLAG_4MV){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
922 int block;
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 mb_type|= MB_TYPE_INTER4V;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
925
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
926 for(block=0; block<4; block++){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
927 int mx4, my4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
928 int pred_x4, pred_y4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
929 int dmin4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
930 static const int off[4]= {2, 1, 1, -1};
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
931 const int mot_stride = s->block_wrap[0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
932 const int mot_xy = s->block_index[block];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
933 const int block_x= mb_x*2 + (block&1);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
934 const int block_y= mb_y*2 + (block>>1);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
935
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
936 const int rel_xmin4= xmin - block_x*8;
295
6622b0fd036c mpeg4 4MV encoding
michaelni
parents: 294
diff changeset
937 const int rel_xmax4= xmax - block_x*8 + 8;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
938 const int rel_ymin4= ymin - block_y*8;
295
6622b0fd036c mpeg4 4MV encoding
michaelni
parents: 294
diff changeset
939 const int rel_ymax4= ymax - block_y*8 + 8;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
940
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
941 P[0][0] = s->motion_val[mot_xy ][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
942 P[0][1] = s->motion_val[mot_xy ][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
943 P[1][0] = s->motion_val[mot_xy - 1][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
944 P[1][1] = s->motion_val[mot_xy - 1][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
945 if(P[1][0] > (rel_xmax4<<shift)) P[1][0]= (rel_xmax4<<shift);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
946
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
947 /* special case for first line */
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
948 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
949 P[4][0] = P[1][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
950 P[4][1] = P[1][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
951 } else {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
952 P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
953 P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
954 P[3][0] = s->motion_val[mot_xy - mot_stride + off[block]][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
955 P[3][1] = s->motion_val[mot_xy - mot_stride + off[block]][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
956 if(P[2][1] > (rel_ymax4<<shift)) P[2][1]= (rel_ymax4<<shift);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
957 if(P[3][0] < (rel_xmin4<<shift)) P[3][0]= (rel_xmin4<<shift);
295
6622b0fd036c mpeg4 4MV encoding
michaelni
parents: 294
diff changeset
958 if(P[3][0] > (rel_xmax4<<shift)) P[3][0]= (rel_xmax4<<shift);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
959 if(P[3][1] > (rel_ymax4<<shift)) P[3][1]= (rel_ymax4<<shift);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
960
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
961 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
962 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
963 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
964 if(s->out_format == FMT_H263){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
965 pred_x4 = P[4][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
966 pred_y4 = P[4][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
967 }else { /* mpeg1 at least */
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
968 pred_x4= P[1][0];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
969 pred_y4= P[1][1];
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
970 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
971 P[5][0]= mx - mb_x*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
972 P[5][1]= my - mb_y*16;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
973
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
974 dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, ref_picture);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
975
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
976 halfpel_motion_search4(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
977 pred_x4, pred_y4, block_x, block_y, ref_picture);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
978
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
979 s->motion_val[ s->block_index[block] ][0]= mx4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
980 s->motion_val[ s->block_index[block] ][1]= my4;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
981 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
982 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
983
986e461dc072 Initial revision
glantau
parents:
diff changeset
984 /* intra / predictive decision */
986e461dc072 Initial revision
glantau
parents:
diff changeset
985 xx = mb_x * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
986 yy = mb_y * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
987
986e461dc072 Initial revision
glantau
parents:
diff changeset
988 pix = s->new_picture[0] + (yy * s->linesize) + xx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
989 /* At this point (mx,my) are full-pell and the absolute displacement */
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
990 ppix = ref_picture + (my * s->linesize) + mx;
289
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
991
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
992 sum = pix_sum(pix, s->linesize);
289
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
993 #if 0
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
994 varc = pix_dev(pix, s->linesize, (sum+128)>>8) + INTER_BIAS;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
995 vard = pix_abs16x16(pix, ppix, s->linesize);
289
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
996 #else
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
997 sum= (sum+8)>>4;
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
998 varc = ((pix_norm1(pix, s->linesize) - sum*sum + 128 + 500)>>8);
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
999 vard = (pix_norm(pix, ppix, s->linesize)+128)>>8;
648e9245546d seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents: 288
diff changeset
1000 #endif
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1001
239
16cd8a9c4da4 - Minor changes on bitrate control
pulento
parents: 233
diff changeset
1002 s->mb_var[s->mb_width * mb_y + mb_x] = varc;
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1003 s->avg_mb_var+= varc;
268
09ae29b27ed9 hopefully better bitrate controll
michaelni
parents: 239
diff changeset
1004 s->mc_mb_var += vard;
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
1005
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1006
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1007 #if 0
233
3f5b72726118 - More work on preliminary bit rate control, just to be able to get an
pulento
parents: 232
diff changeset
1008 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
1009 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1010 #endif
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1011 if(s->flags&CODEC_FLAG_HQ){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1012 if (vard*2 + 200 > varc)
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1013 mb_type|= MB_TYPE_INTRA;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1014 if (varc*2 + 200 > vard){
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1015 mb_type|= MB_TYPE_INTER;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1016 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture);
304
5753d57e7e6b fixing MVs in hq mode
michaelni
parents: 295
diff changeset
1017 }else{
5753d57e7e6b fixing MVs in hq mode
michaelni
parents: 295
diff changeset
1018 mx = mx*2 - mb_x*32;
5753d57e7e6b fixing MVs in hq mode
michaelni
parents: 295
diff changeset
1019 my = my*2 - mb_y*32;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1020 }
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1021 }else{
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1022 if (vard <= 64 || vard < varc) {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1023 mb_type|= MB_TYPE_INTER;
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1024 if (s->me_method != ME_ZERO) {
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1025 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture);
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1026 } else {
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1027 mx -= 16 * mb_x;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1028 my -= 16 * mb_y;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1029 }
320
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1030 #if 0
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1031 if (vard < 10) {
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1032 skip++;
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1033 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
1034 skip, vard, varc, dmin);
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1035 }
cda7d0857baf - ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents: 304
diff changeset
1036 #endif
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1037 }else{
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1038 mb_type|= MB_TYPE_INTRA;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1039 mx = 0;//mx*2 - 32 * mb_x;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1040 my = 0;//my*2 - 32 * mb_y;
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1041 }
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1042 }
284
0778d4e1d584 better inter/intra decission algo (same as xvid)
michaelni
parents: 281
diff changeset
1043
294
944632089814 4MV motion estimation (not finished yet)
michaelni
parents: 289
diff changeset
1044 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1045 set_p_mv_tables(s, mx, my);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1046 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1047
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1048 int ff_estimate_motion_b(MpegEncContext * s,
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1049 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
1050 {
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1051 int mx, my, range, dmin;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1052 int xmin, ymin, xmax, ymax;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1053 int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1054 int pred_x=0, pred_y=0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1055 int P[6][2];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1056 const int shift= 1+s->quarter_sample;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1057 const int mot_stride = s->mb_width + 2;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1058 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1059
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1060 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1061
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1062 switch(s->me_method) {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1063 case ME_ZERO:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1064 default:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1065 no_motion_search(s, &mx, &my);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1066 dmin = 0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1067 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1068 case ME_FULL:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1069 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1070 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1071 case ME_LOG:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1072 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1073 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1074 case ME_PHODS:
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);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1076 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1077 case ME_X1:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1078 case ME_EPZS:
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1079 {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1080
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1081 rel_xmin= xmin - mb_x*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1082 rel_xmax= xmax - mb_x*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1083 rel_ymin= ymin - mb_y*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1084 rel_ymax= ymax - mb_y*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1085
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1086 P[0][0] = mv_table[mot_xy ][0];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1087 P[0][1] = mv_table[mot_xy ][1];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1088 P[1][0] = mv_table[mot_xy - 1][0];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1089 P[1][1] = mv_table[mot_xy - 1][1];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1090 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1091
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1092 /* special case for first line */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1093 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1094 P[4][0] = P[1][0];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1095 P[4][1] = P[1][1];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1096 } else {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1097 P[2][0] = mv_table[mot_xy - mot_stride ][0];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1098 P[2][1] = mv_table[mot_xy - mot_stride ][1];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1099 P[3][0] = mv_table[mot_xy - mot_stride + 1 ][0];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1100 P[3][1] = mv_table[mot_xy - mot_stride + 1 ][1];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1101 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1102 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1103 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1104
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1105 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1106 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1107 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1108 pred_x= P[1][0];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1109 pred_y= P[1][1];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1110 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1111 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
1112
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1113 mx+= mb_x*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1114 my+= mb_y*16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1115 break;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1116 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1117
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1118 /* intra / predictive decision */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1119 // xx = mb_x * 16;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1120 // yy = mb_y * 16;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1121
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1122 // pix = s->new_picture[0] + (yy * s->linesize) + xx;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1123 /* At this point (mx,my) are full-pell and the absolute displacement */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1124 // ppix = ref_picture + (my * s->linesize) + mx;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1125
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1126 dmin= halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture);
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1127
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1128 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1129 mv_table[mot_xy][0]= mx;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1130 mv_table[mot_xy][1]= my;
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1131 return dmin;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1132 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1133
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1134
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1135 static inline int check_bidir_mv(MpegEncContext * s,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1136 int mb_x, int mb_y,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1137 int motion_fx, int motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1138 int motion_bx, int motion_by,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1139 int pred_fx, int pred_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1140 int pred_bx, int pred_by)
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1141 {
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1142 //FIXME optimize?
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1143 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
1144 uint8_t *dest_y = s->me_scratchpad;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1145 uint8_t *ptr;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1146 int dxy;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1147 int src_x, src_y;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1148 int fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1149
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1150 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
1151
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1152 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1153 src_x = mb_x * 16 + (motion_fx >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1154 src_y = mb_y * 16 + (motion_fy >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1155
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1156 ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1157 put_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1158 put_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1159
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1160 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
1161
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1162 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1163 src_x = mb_x * 16 + (motion_bx >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1164 src_y = mb_y * 16 + (motion_by >> 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1165
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1166 ptr = s->next_picture[0] + (src_y * s->linesize) + src_x;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1167 avg_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1168 avg_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1169
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1170 fbmin += pix_abs16x16(s->new_picture[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1171 return fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1172 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1173
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1174 /* 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
1175 static inline int bidir_refine(MpegEncContext * s,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1176 int mb_x, int mb_y)
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1177 {
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1178 const int mot_stride = s->mb_width + 2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1179 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
1180 int fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1181 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
1182 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
1183 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
1184 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
1185 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
1186 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
1187 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
1188 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
1189
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1190 //FIXME do refinement and add flag
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1191
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1192 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
1193 motion_fx, motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1194 motion_bx, motion_by,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1195 pred_fx, pred_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1196 pred_bx, pred_by);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1197
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1198 return fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1199 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1200
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1201 static inline int direct_search(MpegEncContext * s,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1202 int mb_x, int mb_y)
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1203 {
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1204 int P[6][2];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1205 const int mot_stride = s->mb_width + 2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1206 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
1207 int dmin, dmin2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1208 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
1209 int motion_dx, motion_dy;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1210 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
1211 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
1212 const int time_pp= s->pp_time;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1213 const int time_bp= s->bp_time;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1214 const int time_pb= time_pp - time_bp;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1215 int bx, by;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1216 int mx, my, mx2, my2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1217 uint8_t *ref_picture= s->me_scratchpad - (mb_x + 1 + (mb_y + 1)*s->linesize)*16;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1218 int16_t (*mv_table)[2]= s->b_direct_mv_table;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1219 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
1220
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1221 /* 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
1222 motion_fx= (motion_px*time_pb)/time_pp;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1223 motion_fy= (motion_py*time_pb)/time_pp;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1224 motion_bx0= (-motion_px*time_bp)/time_pp;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1225 motion_by0= (-motion_py*time_bp)/time_pp;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1226 motion_dx= motion_dy=0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1227 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
1228 motion_fx, motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1229 motion_bx0, motion_by0,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1230 motion_fx, motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1231 motion_bx0, motion_by0) - s->qscale;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1232
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1233 motion_bx= motion_fx - motion_px;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1234 motion_by= motion_fy - motion_py;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1235 for(by=-1; by<2; by++){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1236 for(bx=-1; bx<2; bx++){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1237 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
1238 uint8_t *ptr;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1239 int dxy;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1240 int src_x, src_y;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1241 const int width= s->width;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1242 const int height= s->height;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1243
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1244 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1245 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
1246 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
1247 src_x = clip(src_x, -16, width);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1248 if (src_x == width) dxy &= ~1;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1249 src_y = clip(src_y, -16, height);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1250 if (src_y == height) dxy &= ~2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1251
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1252 ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1253 put_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1254 put_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1255
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1256 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1257 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
1258 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
1259 src_x = clip(src_x, -16, width);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1260 if (src_x == width) dxy &= ~1;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1261 src_y = clip(src_y, -16, height);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1262 if (src_y == height) dxy &= ~2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1263
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1264 avg_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1265 avg_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1266 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1267 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1268
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1269 P[0][0] = mv_table[mot_xy ][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1270 P[0][1] = mv_table[mot_xy ][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1271 P[1][0] = mv_table[mot_xy - 1][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1272 P[1][1] = mv_table[mot_xy - 1][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1273
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1274 /* special case for first line */
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1275 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1276 P[4][0] = P[1][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1277 P[4][1] = P[1][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1278 } else {
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1279 P[2][0] = mv_table[mot_xy - mot_stride ][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1280 P[2][1] = mv_table[mot_xy - mot_stride ][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1281 P[3][0] = mv_table[mot_xy - mot_stride + 1 ][0];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1282 P[3][1] = mv_table[mot_xy - mot_stride + 1 ][1];
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1283
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1284 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1285 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1286 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1287 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
1288 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
1289 if(dmin2<dmin){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1290 dmin= dmin2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1291 mx=0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1292 my=0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1293 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1294 #if 1
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1295 mx2= mx= mx*2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1296 my2= my= my*2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1297 for(by=-1; by<2; by++){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1298 if(my2+by < -32) continue;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1299 for(bx=-1; bx<2; bx++){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1300 if(bx==0 && by==0) continue;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1301 if(mx2+bx < -32) continue;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1302 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
1303 mx2+bx+motion_fx, my2+by+motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1304 mx2+bx+motion_bx, my2+by+motion_by,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1305 mx2+bx+motion_fx, my2+by+motion_fy,
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1306 motion_bx, motion_by) - s->qscale;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1307
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1308 if(dmin2<dmin){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1309 dmin=dmin2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1310 mx= mx2 + bx;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1311 my= my2 + by;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1312 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1313 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1314 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1315 #else
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1316 mx*=2; my*=2;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1317 #endif
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1318 if(mx==0 && my==0){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1319 motion_bx= motion_bx0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1320 motion_by= motion_by0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1321 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1322
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1323 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
1324 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
1325 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
1326 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
1327 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
1328 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
1329 return dmin;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1330 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1331
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1332 void ff_estimate_b_frame_motion(MpegEncContext * s,
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1333 int mb_x, int mb_y)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1334 {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1335 const int mot_stride = s->mb_width + 2;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1336 const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1337 const int quant= s->qscale;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1338 int fmin, bmin, dmin, fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1339 int type=0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1340 int motion_fx, motion_fy, motion_bx, motion_by;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1341
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1342 dmin= direct_search(s, mb_x, mb_y);
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1343
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1344 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
1345 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
1346 //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
1347
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1348 fbmin= bidir_refine(s, mb_x, mb_y);
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1349
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1350 if(s->flags&CODEC_FLAG_HQ){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1351 type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1352 }else{
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1353 int score= dmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1354 type=MB_TYPE_DIRECT;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1355
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1356 if(fmin<score){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1357 score=fmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1358 type= MB_TYPE_FORWARD;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1359 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1360 if(bmin<score){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1361 score=bmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1362 type= MB_TYPE_BACKWARD;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1363 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1364 if(fbmin<score){
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1365 score=fbmin;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1366 type= MB_TYPE_BIDIR;
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 s->mc_mb_var += score;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1369 }
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1370
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1371 s->mb_type[mb_y*s->mb_width + mb_x]= type;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1372 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1373
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1374 /* find best f_code for ME which do unlimited searches */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1375 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
1376 {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1377 int f_code;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1378
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1379 if(s->me_method>=ME_EPZS){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1380 int mv_num[8];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1381 int i, y;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1382 int loose=0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1383 UINT8 * fcode_tab= s->fcode_tab;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1384
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1385 for(i=0; i<8; i++) mv_num[i]=0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1386
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1387 for(y=0; y<s->mb_height; y++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1388 int x;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1389 int xy= (y+1)* (s->mb_width+2) + 1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1390 i= y*s->mb_width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1391 for(x=0; x<s->mb_width; x++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1392 if(s->mb_type[i] & type){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1393 mv_num[ fcode_tab[mv_table[xy][0] + MAX_MV] ]++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1394 mv_num[ fcode_tab[mv_table[xy][1] + MAX_MV] ]++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1395 //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1396 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1397 i++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1398 xy++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1399 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1400 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1401
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1402 for(i=MAX_FCODE; i>1; i--){
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1403 int threshold;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1404 loose+= mv_num[i];
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1405
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1406 if(s->pict_type==B_TYPE) threshold= 0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1407 else threshold= s->mb_num/20; //FIXME
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1408 if(loose > threshold) break;
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1409 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1410 // printf("fcode: %d type: %d\n", i, s->pict_type);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1411 return i;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1412 /* for(i=0; i<=MAX_FCODE; i++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1413 printf("%d ", mv_num[i]);
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1414 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1415 printf("\n");*/
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1416 }else{
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1417 return 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1418 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1419 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1420
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1421 void ff_fix_long_p_mvs(MpegEncContext * s)
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1422 {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1423 const int f_code= s->f_code;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1424 int y;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1425 UINT8 * fcode_tab= s->fcode_tab;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1426
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1427 /* clip / convert to intra 16x16 type MVs */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1428 for(y=0; y<s->mb_height; y++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1429 int x;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1430 int xy= (y+1)* (s->mb_width+2)+1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1431 int i= y*s->mb_width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1432 for(x=0; x<s->mb_width; x++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1433 if(s->mb_type[i]&MB_TYPE_INTER){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1434 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
1435 || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1436 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1437 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1438 s->mb_type[i] &= ~MB_TYPE_INTER;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1439 s->mb_type[i] |= MB_TYPE_INTRA;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1440 s->p_mv_table[xy][0] = 0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1441 s->p_mv_table[xy][1] = 0;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1442 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1443 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1444 xy++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1445 i++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1446 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1447 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1448
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1449 if(s->flags&CODEC_FLAG_4MV){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1450 const int wrap= 2+ s->mb_width*2;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1451
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1452 /* clip / convert to intra 8x8 type MVs */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1453 for(y=0; y<s->mb_height; y++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1454 int xy= (y*2 + 1)*wrap + 1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1455 int i= y*s->mb_width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1456 int x;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1457
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1458 for(x=0; x<s->mb_width; x++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1459 if(s->mb_type[i]&MB_TYPE_INTER4V){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1460 int block;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1461 for(block=0; block<4; block++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1462 int off= (block& 1) + (block>>1)*wrap;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1463 int mx= s->motion_val[ xy + off ][0];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1464 int my= s->motion_val[ xy + off ][1];
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1465
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1466 if( fcode_tab[mx + MAX_MV] > f_code
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1467 || fcode_tab[mx + MAX_MV] == 0
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1468 || fcode_tab[my + MAX_MV] > f_code
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1469 || fcode_tab[my + MAX_MV] == 0 ){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1470 s->mb_type[i] &= ~MB_TYPE_INTER4V;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1471 s->mb_type[i] |= MB_TYPE_INTRA;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1472 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1473 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1474 xy+=2;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1475 i++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1476 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1477 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1478 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1479 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1480 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1481
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1482 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
1483 {
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1484 int y;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1485 UINT8 * fcode_tab= s->fcode_tab;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1486
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1487 /* clip / convert to intra 16x16 type MVs */
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1488 for(y=0; y<s->mb_height; y++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1489 int x;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1490 int xy= (y+1)* (s->mb_width+2)+1;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1491 int i= y*s->mb_width;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1492 for(x=0; x<s->mb_width; x++){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1493 if(s->mb_type[i]&type){
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1494 if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1495 || fcode_tab[mv_table[xy][0] + MAX_MV] == 0
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1496 || fcode_tab[mv_table[xy][1] + MAX_MV] > f_code
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1497 || fcode_tab[mv_table[xy][1] + MAX_MV] == 0 ){
327
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1498 if(s->mb_type[i]&(~type)) s->mb_type[i] &= ~type;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1499 else{
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1500 mv_table[xy][0] = 0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1501 mv_table[xy][1] = 0;
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1502 //this is certainly bad FIXME
d359db02fc90 much better ME for b frames (a bit slow though)
michaelni
parents: 324
diff changeset
1503 }
324
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1504 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1505 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1506 xy++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1507 i++;
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1508 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1509 }
9c6f056f0e41 fixed mpeg4 time stuff on encoding
michaelni
parents: 320
diff changeset
1510 }