FFmpeg  1.2.12
motion_est.c
Go to the documentation of this file.
1 /*
2  * Motion estimation
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer
5  *
6  * new motion estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <limits.h>
33 
34 #include "avcodec.h"
35 #include "mathops.h"
36 #include "mpegvideo.h"
37 
38 #undef NDEBUG
39 #include <assert.h>
40 
41 #define P_LEFT P[1]
42 #define P_TOP P[2]
43 #define P_TOPRIGHT P[3]
44 #define P_MEDIAN P[4]
45 #define P_MV1 P[9]
46 
48  int *mx_ptr, int *my_ptr, int dmin,
49  int src_index, int ref_index,
50  int size, int h);
51 
52 static inline unsigned update_map_generation(MotionEstContext *c)
53 {
54  c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
55  if(c->map_generation==0){
56  c->map_generation= 1<<(ME_MAP_MV_BITS*2);
57  memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
58  }
59  return c->map_generation;
60 }
61 
62 /* shape adaptive search stuff */
63 typedef struct Minima{
64  int height;
65  int x, y;
66  int checked;
67 }Minima;
68 
69 static int minima_cmp(const void *a, const void *b){
70  const Minima *da = (const Minima *) a;
71  const Minima *db = (const Minima *) b;
72 
73  return da->height - db->height;
74 }
75 
76 #define FLAG_QPEL 1 //must be 1
77 #define FLAG_CHROMA 2
78 #define FLAG_DIRECT 4
79 
80 static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
81  const int offset[3]= {
82  y*c-> stride + x,
83  ((y*c->uvstride + x)>>1),
84  ((y*c->uvstride + x)>>1),
85  };
86  int i;
87  for(i=0; i<3; i++){
88  c->src[0][i]= src [i] + offset[i];
89  c->ref[0][i]= ref [i] + offset[i];
90  }
91  if(ref_index){
92  for(i=0; i<3; i++){
93  c->ref[ref_index][i]= ref2[i] + offset[i];
94  }
95  }
96 }
97 
98 static int get_flags(MotionEstContext *c, int direct, int chroma){
99  return ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
100  + (direct ? FLAG_DIRECT : 0)
101  + (chroma ? FLAG_CHROMA : 0);
102 }
103 
104 static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
105  const int size, const int h, int ref_index, int src_index,
106  me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){
107  MotionEstContext * const c= &s->me;
108  const int stride= c->stride;
109  const int hx= subx + (x<<(1+qpel));
110  const int hy= suby + (y<<(1+qpel));
111  uint8_t * const * const ref= c->ref[ref_index];
112  uint8_t * const * const src= c->src[src_index];
113  int d;
114  //FIXME check chroma 4mv, (no crashes ...)
115  av_assert2(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
116  if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
117  const int time_pp= s->pp_time;
118  const int time_pb= s->pb_time;
119  const int mask= 2*qpel+1;
120  if(s->mv_type==MV_TYPE_8X8){
121  int i;
122  for(i=0; i<4; i++){
123  int fx = c->direct_basis_mv[i][0] + hx;
124  int fy = c->direct_basis_mv[i][1] + hy;
125  int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4));
126  int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4));
127  int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
128  int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
129 
130  uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
131  if(qpel){
132  c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
133  c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
134  }else{
135  c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
136  c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
137  }
138  }
139  }else{
140  int fx = c->direct_basis_mv[0][0] + hx;
141  int fy = c->direct_basis_mv[0][1] + hy;
142  int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
143  int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
144  int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
145  int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
146 
147  if(qpel){
148  c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);
149  c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);
150  c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);
151  c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
152  c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);
153  c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);
154  c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);
155  c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
156  }else{
157  av_assert2((fx>>1) + 16*s->mb_x >= -16);
158  av_assert2((fy>>1) + 16*s->mb_y >= -16);
159  av_assert2((fx>>1) + 16*s->mb_x <= s->width);
160  av_assert2((fy>>1) + 16*s->mb_y <= s->height);
161  av_assert2((bx>>1) + 16*s->mb_x >= -16);
162  av_assert2((by>>1) + 16*s->mb_y >= -16);
163  av_assert2((bx>>1) + 16*s->mb_x <= s->width);
164  av_assert2((by>>1) + 16*s->mb_y <= s->height);
165 
166  c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
167  c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
168  }
169  }
170  d = cmp_func(s, c->temp, src[0], stride, 16);
171  }else
172  d= 256*256*256*32;
173  return d;
174 }
175 
176 static av_always_inline int cmp_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
177  const int size, const int h, int ref_index, int src_index,
178  me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel, int chroma){
179  MotionEstContext * const c= &s->me;
180  const int stride= c->stride;
181  const int uvstride= c->uvstride;
182  const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
183  const int hx= subx + (x<<(1+qpel));
184  const int hy= suby + (y<<(1+qpel));
185  uint8_t * const * const ref= c->ref[ref_index];
186  uint8_t * const * const src= c->src[src_index];
187  int d;
188  //FIXME check chroma 4mv, (no crashes ...)
189  int uvdxy; /* no, it might not be used uninitialized */
190  if(dxy){
191  if(qpel){
192  if (h << size == 16) {
193  c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h)
194  } else if (size == 0 && h == 8) {
195  c->qpel_put[1][dxy](c->temp , ref[0] + x + y*stride , stride);
196  c->qpel_put[1][dxy](c->temp + 8, ref[0] + x + y*stride + 8, stride);
197  } else
198  av_assert2(0);
199  if(chroma){
200  int cx= hx/2;
201  int cy= hy/2;
202  cx= (cx>>1)|(cx&1);
203  cy= (cy>>1)|(cy&1);
204  uvdxy= (cx&1) + 2*(cy&1);
205  //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264
206  }
207  }else{
208  c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
209  if(chroma)
210  uvdxy= dxy | (x&1) | (2*(y&1));
211  }
212  d = cmp_func(s, c->temp, src[0], stride, h);
213  }else{
214  d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
215  if(chroma)
216  uvdxy= (x&1) + 2*(y&1);
217  }
218  if(chroma){
219  uint8_t * const uvtemp= c->temp + 16*stride;
220  c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
221  c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
222  d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1);
223  d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
224  }
225  return d;
226 }
227 
228 static int cmp_simple(MpegEncContext *s, const int x, const int y,
229  int ref_index, int src_index,
230  me_cmp_func cmp_func, me_cmp_func chroma_cmp_func){
231  return cmp_inline(s,x,y,0,0,0,16,ref_index,src_index, cmp_func, chroma_cmp_func, 0, 0);
232 }
233 
234 static int cmp_fpel_internal(MpegEncContext *s, const int x, const int y,
235  const int size, const int h, int ref_index, int src_index,
236  me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
237  if(flags&FLAG_DIRECT){
238  return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
239  }else{
240  return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
241  }
242 }
243 
244 static int cmp_internal(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
245  const int size, const int h, int ref_index, int src_index,
246  me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
247  if(flags&FLAG_DIRECT){
248  return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
249  }else{
250  return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL, flags&FLAG_CHROMA);
251  }
252 }
253 
257 static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
258  const int size, const int h, int ref_index, int src_index,
259  me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
262  && flags==0 && h==16 && size==0 && subx==0 && suby==0){
263  return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);
264  }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
265  && subx==0 && suby==0){
266  return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);
267  }else{
268  return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);
269  }
270 }
271 
272 static int cmp_hpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
273  const int size, const int h, int ref_index, int src_index,
274  me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
275  if(flags&FLAG_DIRECT){
276  return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0);
277  }else{
278  return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
279  }
280 }
281 
282 static int cmp_qpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
283  const int size, const int h, int ref_index, int src_index,
284  me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
285  if(flags&FLAG_DIRECT){
286  return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1);
287  }else{
288  return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1, flags&FLAG_CHROMA);
289  }
290 }
291 
292 #include "motion_est_template.c"
293 
294 static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){
295  return 0;
296 }
297 
298 static void zero_hpel(uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h){
299 }
300 
302  MotionEstContext * const c= &s->me;
303  int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
304  int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
305 
307  av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
308  return -1;
309  }
310  //special case of snow is needed because snow uses its own iterative ME code
312  av_log(s->avctx, AV_LOG_ERROR, "me_method is only allowed to be set to zero and epzs; for hex,umh,full and others see dia_size\n");
313  return -1;
314  }
315 
316  c->avctx= s->avctx;
317 
318  if(cache_size < 2*dia_size && !c->stride){
319  av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
320  }
321 
323  ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
325  ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
326 
327  c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
329  c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
330 
331 /*FIXME s->no_rounding b_type*/
332  if(s->flags&CODEC_FLAG_QPEL){
336  else c->qpel_put= s->dsp.put_qpel_pixels_tab;
337  }else{
340  else if( c->avctx->me_sub_cmp == FF_CMP_SAD
341  && c->avctx-> me_cmp == FF_CMP_SAD
342  && c->avctx-> mb_cmp == FF_CMP_SAD)
343  c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
344  else
346  }
347  c->hpel_avg= s->dsp.avg_pixels_tab;
349  else c->hpel_put= s->dsp.put_pixels_tab;
350 
351  if(s->linesize){
352  c->stride = s->linesize;
353  c->uvstride= s->uvlinesize;
354  }else{
355  c->stride = 16*s->mb_width + 32;
356  c->uvstride= 8*s->mb_width + 16;
357  }
358 
359  /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
360  * not have yet, and even if we had, the motion estimation code
361  * does not expect it. */
362  if(s->codec_id != AV_CODEC_ID_SNOW){
363  if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
364  s->dsp.me_cmp[2]= zero_cmp;
365  }
366  if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
367  s->dsp.me_sub_cmp[2]= zero_cmp;
368  }
369  c->hpel_put[2][0]= c->hpel_put[2][1]=
370  c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
371  }
372 
373  if(s->codec_id == AV_CODEC_ID_H261){
375  }
376 
377  return 0;
378 }
379 
380 #define CHECK_SAD_HALF_MV(suffix, x, y) \
381 {\
382  d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
383  d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
384  COPY3_IF_LT(dminh, d, dx, x, dy, y)\
385 }
386 
388  int *mx_ptr, int *my_ptr, int dmin,
389  int src_index, int ref_index,
390  int size, int h)
391 {
392  MotionEstContext * const c= &s->me;
393  const int penalty_factor= c->sub_penalty_factor;
394  int mx, my, dminh;
395  uint8_t *pix, *ptr;
396  int stride= c->stride;
397  const int flags= c->sub_flags;
399 
400  av_assert2(flags == 0);
401 
402  if(c->skip){
403  *mx_ptr = 0;
404  *my_ptr = 0;
405  return dmin;
406  }
407 
408  pix = c->src[src_index][0];
409 
410  mx = *mx_ptr;
411  my = *my_ptr;
412  ptr = c->ref[ref_index][0] + (my * stride) + mx;
413 
414  dminh = dmin;
415 
416  if (mx > xmin && mx < xmax &&
417  my > ymin && my < ymax) {
418  int dx=0, dy=0;
419  int d, pen_x, pen_y;
420  const int index= (my<<ME_MAP_SHIFT) + mx;
421  const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
422  const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
423  const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
424  const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
425  mx<<=1;
426  my<<=1;
427 
428 
429  pen_x= pred_x + mx;
430  pen_y= pred_y + my;
431 
432  ptr-= stride;
433  if(t<=b){
434  CHECK_SAD_HALF_MV(y2 , 0, -1)
435  if(l<=r){
436  CHECK_SAD_HALF_MV(xy2, -1, -1)
437  if(t+r<=b+l){
438  CHECK_SAD_HALF_MV(xy2, +1, -1)
439  ptr+= stride;
440  }else{
441  ptr+= stride;
442  CHECK_SAD_HALF_MV(xy2, -1, +1)
443  }
444  CHECK_SAD_HALF_MV(x2 , -1, 0)
445  }else{
446  CHECK_SAD_HALF_MV(xy2, +1, -1)
447  if(t+l<=b+r){
448  CHECK_SAD_HALF_MV(xy2, -1, -1)
449  ptr+= stride;
450  }else{
451  ptr+= stride;
452  CHECK_SAD_HALF_MV(xy2, +1, +1)
453  }
454  CHECK_SAD_HALF_MV(x2 , +1, 0)
455  }
456  }else{
457  if(l<=r){
458  if(t+l<=b+r){
459  CHECK_SAD_HALF_MV(xy2, -1, -1)
460  ptr+= stride;
461  }else{
462  ptr+= stride;
463  CHECK_SAD_HALF_MV(xy2, +1, +1)
464  }
465  CHECK_SAD_HALF_MV(x2 , -1, 0)
466  CHECK_SAD_HALF_MV(xy2, -1, +1)
467  }else{
468  if(t+r<=b+l){
469  CHECK_SAD_HALF_MV(xy2, +1, -1)
470  ptr+= stride;
471  }else{
472  ptr+= stride;
473  CHECK_SAD_HALF_MV(xy2, -1, +1)
474  }
475  CHECK_SAD_HALF_MV(x2 , +1, 0)
476  CHECK_SAD_HALF_MV(xy2, +1, +1)
477  }
478  CHECK_SAD_HALF_MV(y2 , 0, +1)
479  }
480  mx+=dx;
481  my+=dy;
482 
483  }else{
484  mx<<=1;
485  my<<=1;
486  }
487 
488  *mx_ptr = mx;
489  *my_ptr = my;
490  return dminh;
491 }
492 
493 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
494 {
495  const int xy= s->mb_x + s->mb_y*s->mb_stride;
496 
497  s->p_mv_table[xy][0] = mx;
498  s->p_mv_table[xy][1] = my;
499 
500  /* has already been set to the 4 MV if 4MV is done */
501  if(mv4){
502  int mot_xy= s->block_index[0];
503 
504  s->current_picture.f.motion_val[0][mot_xy ][0] = mx;
505  s->current_picture.f.motion_val[0][mot_xy ][1] = my;
506  s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
507  s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
508 
509  mot_xy += s->b8_stride;
510  s->current_picture.f.motion_val[0][mot_xy ][0] = mx;
511  s->current_picture.f.motion_val[0][mot_xy ][1] = my;
512  s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
513  s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
514  }
515 }
516 
520 static inline void get_limits(MpegEncContext *s, int x, int y)
521 {
522  MotionEstContext * const c= &s->me;
523  int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
524  int max_range = MAX_MV >> (1 + !!(c->flags&FLAG_QPEL));
525 /*
526  if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
527  else c->range= 16;
528 */
529  if (s->unrestricted_mv) {
530  c->xmin = - x - 16;
531  c->ymin = - y - 16;
532  c->xmax = - x + s->width;
533  c->ymax = - y + s->height;
534  } else if (s->out_format == FMT_H261){
535  // Search range of H261 is different from other codec standards
536  c->xmin = (x > 15) ? - 15 : 0;
537  c->ymin = (y > 15) ? - 15 : 0;
538  c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
539  c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
540  } else {
541  c->xmin = - x;
542  c->ymin = - y;
543  c->xmax = - x + s->mb_width *16 - 16;
544  c->ymax = - y + s->mb_height*16 - 16;
545  }
546  if(!range || range > max_range)
547  range = max_range;
548  if(range){
549  c->xmin = FFMAX(c->xmin,-range);
550  c->xmax = FFMIN(c->xmax, range);
551  c->ymin = FFMAX(c->ymin,-range);
552  c->ymax = FFMIN(c->ymax, range);
553  }
554 }
555 
556 static inline void init_mv4_ref(MotionEstContext *c){
557  const int stride= c->stride;
558 
559  c->ref[1][0] = c->ref[0][0] + 8;
560  c->ref[2][0] = c->ref[0][0] + 8*stride;
561  c->ref[3][0] = c->ref[2][0] + 8;
562  c->src[1][0] = c->src[0][0] + 8;
563  c->src[2][0] = c->src[0][0] + 8*stride;
564  c->src[3][0] = c->src[2][0] + 8;
565 }
566 
567 static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
568 {
569  MotionEstContext * const c= &s->me;
570  const int size= 1;
571  const int h=8;
572  int block;
573  int P[10][2];
574  int dmin_sum=0, mx4_sum=0, my4_sum=0, i;
575  int same=1;
576  const int stride= c->stride;
578  int saftey_cliping= s->unrestricted_mv && (s->width&15) && (s->height&15);
579 
580  init_mv4_ref(c);
581 
582  for(block=0; block<4; block++){
583  int mx4, my4;
584  int pred_x4, pred_y4;
585  int dmin4;
586  static const int off[4]= {2, 1, 1, -1};
587  const int mot_stride = s->b8_stride;
588  const int mot_xy = s->block_index[block];
589 
590  if(saftey_cliping){
591  c->xmax = - 16*s->mb_x + s->width - 8*(block &1);
592  c->ymax = - 16*s->mb_y + s->height - 8*(block>>1);
593  }
594 
595  P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
596  P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
597 
598  if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
599 
600  /* special case for first line */
601  if (s->first_slice_line && block<2) {
602  c->pred_x= pred_x4= P_LEFT[0];
603  c->pred_y= pred_y4= P_LEFT[1];
604  } else {
605  P_TOP[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][0];
606  P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
607  P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][0];
608  P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][1];
609  if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
610  if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
611  if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
612  if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
613 
614  P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
615  P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
616 
617  c->pred_x= pred_x4 = P_MEDIAN[0];
618  c->pred_y= pred_y4 = P_MEDIAN[1];
619  }
620  P_MV1[0]= mx;
621  P_MV1[1]= my;
622  if(saftey_cliping)
623  for(i=0; i<10; i++){
624  if(P[i][0] > (c->xmax<<shift)) P[i][0]= (c->xmax<<shift);
625  if(P[i][1] > (c->ymax<<shift)) P[i][1]= (c->ymax<<shift);
626  }
627 
628  dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
629 
630  dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
631 
632  if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
633  int dxy;
634  const int offset= ((block&1) + (block>>1)*stride)*8;
635  uint8_t *dest_y = c->scratchpad + offset;
636  if(s->quarter_sample){
637  uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
638  dxy = ((my4 & 3) << 2) | (mx4 & 3);
639 
640  if(s->no_rounding)
641  s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , stride);
642  else
643  s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , stride);
644  }else{
645  uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
646  dxy = ((my4 & 1) << 1) | (mx4 & 1);
647 
648  if(s->no_rounding)
649  s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h);
650  else
651  s->dsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h);
652  }
653  dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
654  }else
655  dmin_sum+= dmin4;
656 
657  if(s->quarter_sample){
658  mx4_sum+= mx4/2;
659  my4_sum+= my4/2;
660  }else{
661  mx4_sum+= mx4;
662  my4_sum+= my4;
663  }
664 
665  s->current_picture.f.motion_val[0][s->block_index[block]][0] = mx4;
666  s->current_picture.f.motion_val[0][s->block_index[block]][1] = my4;
667 
668  if(mx4 != mx || my4 != my) same=0;
669  }
670 
671  if(same)
672  return INT_MAX;
673 
674  if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
675  dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*16*stride, c->scratchpad, stride, 16);
676  }
677 
678  if(c->avctx->mb_cmp&FF_CMP_CHROMA){
679  int dxy;
680  int mx, my;
681  int offset;
682 
683  mx= ff_h263_round_chroma(mx4_sum);
684  my= ff_h263_round_chroma(my4_sum);
685  dxy = ((my & 1) << 1) | (mx & 1);
686 
687  offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
688 
689  if(s->no_rounding){
690  s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
691  s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
692  }else{
693  s->dsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
694  s->dsp.put_pixels_tab [1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
695  }
696 
697  dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.f.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad , s->uvlinesize, 8);
698  dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.f.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad+8, s->uvlinesize, 8);
699  }
700 
701  c->pred_x= mx;
702  c->pred_y= my;
703 
704  switch(c->avctx->mb_cmp&0xFF){
705  /*case FF_CMP_SSE:
706  return dmin_sum+ 32*s->qscale*s->qscale;*/
707  case FF_CMP_RD:
708  return dmin_sum;
709  default:
710  return dmin_sum+ 11*c->mb_penalty_factor;
711  }
712 }
713 
714 static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
715  MotionEstContext * const c= &s->me;
716 
717  c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
718  c->src[1][0] = c->src[0][0] + s->linesize;
719  if(c->flags & FLAG_CHROMA){
720  c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
721  c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
722  c->src[1][1] = c->src[0][1] + s->uvlinesize;
723  c->src[1][2] = c->src[0][2] + s->uvlinesize;
724  }
725 }
726 
727 static int interlaced_search(MpegEncContext *s, int ref_index,
728  int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
729 {
730  MotionEstContext * const c= &s->me;
731  const int size=0;
732  const int h=8;
733  int block;
734  int P[10][2];
736  int same=1;
737  const int stride= 2*s->linesize;
738  int dmin_sum= 0;
739  const int mot_stride= s->mb_stride;
740  const int xy= s->mb_x + s->mb_y*mot_stride;
741 
742  c->ymin>>=1;
743  c->ymax>>=1;
744  c->stride<<=1;
745  c->uvstride<<=1;
746  init_interlaced_ref(s, ref_index);
747 
748  for(block=0; block<2; block++){
749  int field_select;
750  int best_dmin= INT_MAX;
751  int best_field= -1;
752 
753  for(field_select=0; field_select<2; field_select++){
754  int dmin, mx_i, my_i;
755  int16_t (*mv_table)[2]= mv_tables[block][field_select];
756 
757  if(user_field_select){
758  av_assert1(field_select==0 || field_select==1);
759  av_assert1(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
760  if(field_select_tables[block][xy] != field_select)
761  continue;
762  }
763 
764  P_LEFT[0] = mv_table[xy - 1][0];
765  P_LEFT[1] = mv_table[xy - 1][1];
766  if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);
767 
768  c->pred_x= P_LEFT[0];
769  c->pred_y= P_LEFT[1];
770 
771  if(!s->first_slice_line){
772  P_TOP[0] = mv_table[xy - mot_stride][0];
773  P_TOP[1] = mv_table[xy - mot_stride][1];
774  P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
775  P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
776  if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);
777  if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
778  if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
779  if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
780 
781  P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
782  P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
783  }
784  P_MV1[0]= mx; //FIXME not correct if block != field_select
785  P_MV1[1]= my / 2;
786 
787  dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
788 
789  dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
790 
791  mv_table[xy][0]= mx_i;
792  mv_table[xy][1]= my_i;
793 
794  if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
795  int dxy;
796 
797  //FIXME chroma ME
798  uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
799  dxy = ((my_i & 1) << 1) | (mx_i & 1);
800 
801  if(s->no_rounding){
802  s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);
803  }else{
804  s->dsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);
805  }
806  dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
807  dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
808  }else
809  dmin+= c->mb_penalty_factor; //field_select bits
810 
811  dmin += field_select != block; //slightly prefer same field
812 
813  if(dmin < best_dmin){
814  best_dmin= dmin;
815  best_field= field_select;
816  }
817  }
818  {
819  int16_t (*mv_table)[2]= mv_tables[block][best_field];
820 
821  if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
822  if(mv_table[xy][1]&1) same=0;
823  if(mv_table[xy][1]*2 != my) same=0;
824  if(best_field != block) same=0;
825  }
826 
827  field_select_tables[block][xy]= best_field;
828  dmin_sum += best_dmin;
829  }
830 
831  c->ymin<<=1;
832  c->ymax<<=1;
833  c->stride>>=1;
834  c->uvstride>>=1;
835 
836  if(same)
837  return INT_MAX;
838 
839  switch(c->avctx->mb_cmp&0xFF){
840  /*case FF_CMP_SSE:
841  return dmin_sum+ 32*s->qscale*s->qscale;*/
842  case FF_CMP_RD:
843  return dmin_sum;
844  default:
845  return dmin_sum+ 11*c->mb_penalty_factor;
846  }
847 }
848 
849 static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
850  int ymax= s->me.ymax>>interlaced;
851  int ymin= s->me.ymin>>interlaced;
852 
853  if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
854  if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
855  if(mv[1] < ymin) mv[1] = ymin;
856  if(mv[1] > ymax) mv[1] = ymax;
857 }
858 
859 static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
860  MotionEstContext * const c= &s->me;
862  int mb_xy= mb_x + mb_y*s->mb_stride;
863  int xy= 2*mb_x + 2*mb_y*s->b8_stride;
864  int mb_type= s->current_picture.f.mb_type[mb_xy];
865  int flags= c->flags;
866  int shift= (flags&FLAG_QPEL) + 1;
867  int mask= (1<<shift)-1;
868  int x, y, i;
869  int d=0;
870  me_cmp_func cmpf= s->dsp.sse[0];
871  me_cmp_func chroma_cmpf= s->dsp.sse[1];
872 
873  if(p_type && USES_LIST(mb_type, 1)){
874  av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
875  return INT_MAX/2;
876  }
877  av_assert0(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
878 
879  for(i=0; i<4; i++){
880  int xy= s->block_index[i];
881  clip_input_mv(s, p->f.motion_val[0][xy], !!IS_INTERLACED(mb_type));
882  clip_input_mv(s, p->f.motion_val[1][xy], !!IS_INTERLACED(mb_type));
883  }
884 
885  if(IS_INTERLACED(mb_type)){
886  int xy2= xy + s->b8_stride;
888  c->stride<<=1;
889  c->uvstride<<=1;
890 
891  if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
892  av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
893  return INT_MAX/2;
894  }
895 
896  if(USES_LIST(mb_type, 0)){
897  int field_select0= p->f.ref_index[0][4*mb_xy ];
898  int field_select1= p->f.ref_index[0][4*mb_xy+2];
899  av_assert0(field_select0==0 ||field_select0==1);
900  av_assert0(field_select1==0 ||field_select1==1);
901  init_interlaced_ref(s, 0);
902 
903  if(p_type){
904  s->p_field_select_table[0][mb_xy]= field_select0;
905  s->p_field_select_table[1][mb_xy]= field_select1;
906  *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
907  *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
909  }else{
910  s->b_field_select_table[0][0][mb_xy]= field_select0;
911  s->b_field_select_table[0][1][mb_xy]= field_select1;
912  *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
913  *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
915  }
916 
917  x = p->f.motion_val[0][xy ][0];
918  y = p->f.motion_val[0][xy ][1];
919  d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
920  x = p->f.motion_val[0][xy2][0];
921  y = p->f.motion_val[0][xy2][1];
922  d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
923  }
924  if(USES_LIST(mb_type, 1)){
925  int field_select0 = p->f.ref_index[1][4 * mb_xy ];
926  int field_select1 = p->f.ref_index[1][4 * mb_xy + 2];
927  av_assert0(field_select0==0 ||field_select0==1);
928  av_assert0(field_select1==0 ||field_select1==1);
929  init_interlaced_ref(s, 2);
930 
931  s->b_field_select_table[1][0][mb_xy]= field_select0;
932  s->b_field_select_table[1][1][mb_xy]= field_select1;
933  *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy ];
934  *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy2];
935  if(USES_LIST(mb_type, 0)){
937  }else{
939  }
940 
941  x = p->f.motion_val[1][xy ][0];
942  y = p->f.motion_val[1][xy ][1];
943  d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
944  x = p->f.motion_val[1][xy2][0];
945  y = p->f.motion_val[1][xy2][1];
946  d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
947  //FIXME bidir scores
948  }
949  c->stride>>=1;
950  c->uvstride>>=1;
951  }else if(IS_8X8(mb_type)){
952  if(!(s->flags & CODEC_FLAG_4MV)){
953  av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
954  return INT_MAX/2;
955  }
956  cmpf= s->dsp.sse[1];
957  chroma_cmpf= s->dsp.sse[1];
958  init_mv4_ref(c);
959  for(i=0; i<4; i++){
960  xy= s->block_index[i];
961  x= p->f.motion_val[0][xy][0];
962  y= p->f.motion_val[0][xy][1];
963  d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
964  }
966  }else{
967  if(USES_LIST(mb_type, 0)){
968  if(p_type){
969  *(uint32_t*)s->p_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
971  }else if(USES_LIST(mb_type, 1)){
972  *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
973  *(uint32_t*)s->b_bidir_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
975  }else{
976  *(uint32_t*)s->b_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
978  }
979  x = p->f.motion_val[0][xy][0];
980  y = p->f.motion_val[0][xy][1];
981  d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
982  }else if(USES_LIST(mb_type, 1)){
983  *(uint32_t*)s->b_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
985 
986  x = p->f.motion_val[1][xy][0];
987  y = p->f.motion_val[1][xy][1];
988  d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
989  }else
991  }
992  return d;
993 }
994 
995 static inline int get_penalty_factor(int lambda, int lambda2, int type){
996  switch(type&0xFF){
997  default:
998  case FF_CMP_SAD:
999  return lambda>>FF_LAMBDA_SHIFT;
1000  case FF_CMP_DCT:
1001  return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
1002  case FF_CMP_W53:
1003  return (4*lambda)>>(FF_LAMBDA_SHIFT);
1004  case FF_CMP_W97:
1005  return (2*lambda)>>(FF_LAMBDA_SHIFT);
1006  case FF_CMP_SATD:
1007  case FF_CMP_DCT264:
1008  return (2*lambda)>>FF_LAMBDA_SHIFT;
1009  case FF_CMP_RD:
1010  case FF_CMP_PSNR:
1011  case FF_CMP_SSE:
1012  case FF_CMP_NSSE:
1013  return lambda2>>FF_LAMBDA_SHIFT;
1014  case FF_CMP_BIT:
1015  return 1;
1016  }
1017 }
1018 
1020  int mb_x, int mb_y)
1021 {
1022  MotionEstContext * const c= &s->me;
1023  uint8_t *pix, *ppix;
1024  int sum, mx, my, dmin;
1025  int varc;
1026  int vard;
1027  int P[10][2];
1028  const int shift= 1+s->quarter_sample;
1029  int mb_type=0;
1030  Picture * const pic= &s->current_picture;
1031 
1032  init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
1033 
1034  av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
1035  av_assert0(s->linesize == c->stride);
1036  av_assert0(s->uvlinesize == c->uvstride);
1037 
1042 
1043  get_limits(s, 16*mb_x, 16*mb_y);
1044  c->skip=0;
1045 
1046  /* intra / predictive decision */
1047  pix = c->src[0][0];
1048  sum = s->dsp.pix_sum(pix, s->linesize);
1049  varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500;
1050 
1051  pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
1052  pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
1053  c->mb_var_sum_temp += (varc+128)>>8;
1054 
1055  if(c->avctx->me_threshold){
1056  vard= check_input_motion(s, mb_x, mb_y, 1);
1057 
1058  if((vard+128)>>8 < c->avctx->me_threshold){
1059  int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1060  int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1061  pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
1062  c->mc_mb_var_sum_temp += (vard+128)>>8;
1063  c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1064  return;
1065  }
1066  if((vard+128)>>8 < c->avctx->mb_threshold)
1067  mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
1068  }
1069 
1070  switch(s->me_method) {
1071  case ME_ZERO:
1072  default:
1073  mx = 0;
1074  my = 0;
1075  dmin = 0;
1076  break;
1077  case ME_X1:
1078  case ME_EPZS:
1079  {
1080  const int mot_stride = s->b8_stride;
1081  const int mot_xy = s->block_index[0];
1082 
1083  P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
1084  P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
1085 
1086  if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
1087 
1088  if(!s->first_slice_line) {
1089  P_TOP[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][0];
1090  P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
1091  P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][0];
1092  P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][1];
1093  if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
1094  if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
1095  if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
1096 
1097  P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1098  P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1099 
1100  if(s->out_format == FMT_H263){
1101  c->pred_x = P_MEDIAN[0];
1102  c->pred_y = P_MEDIAN[1];
1103  }else { /* mpeg1 at least */
1104  c->pred_x= P_LEFT[0];
1105  c->pred_y= P_LEFT[1];
1106  }
1107  }else{
1108  c->pred_x= P_LEFT[0];
1109  c->pred_y= P_LEFT[1];
1110  }
1111 
1112  }
1113  dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
1114 
1115  break;
1116  }
1117 
1118  /* At this point (mx,my) are full-pell and the relative displacement */
1119  ppix = c->ref[0][0] + (my * s->linesize) + mx;
1120 
1121  vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
1122 
1123  pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
1124  c->mc_mb_var_sum_temp += (vard+128)>>8;
1125 
1126  if(mb_type){
1127  int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1128  int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1129  c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1130 
1131  if(mb_type == CANDIDATE_MB_TYPE_INTER){
1132  c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1133  set_p_mv_tables(s, mx, my, 1);
1134  }else{
1135  mx <<=shift;
1136  my <<=shift;
1137  }
1138  if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
1139  h263_mv4_search(s, mx, my, shift);
1140 
1141  set_p_mv_tables(s, mx, my, 0);
1142  }
1143  if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
1144  interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
1145  }
1146  }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1147  int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1148  int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1149  c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1150 
1151  if (vard*2 + 200*256 > varc)
1152  mb_type|= CANDIDATE_MB_TYPE_INTRA;
1153  if (varc*2 + 200*256 > vard || s->qscale > 24){
1154 // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
1155  mb_type|= CANDIDATE_MB_TYPE_INTER;
1156  c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1157  if(s->flags&CODEC_FLAG_MV0)
1158  if(mx || my)
1159  mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
1160  }else{
1161  mx <<=shift;
1162  my <<=shift;
1163  }
1164  if((s->flags&CODEC_FLAG_4MV)
1165  && !c->skip && varc>50<<8 && vard>10<<8){
1166  if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
1167  mb_type|=CANDIDATE_MB_TYPE_INTER4V;
1168 
1169  set_p_mv_tables(s, mx, my, 0);
1170  }else
1171  set_p_mv_tables(s, mx, my, 1);
1173  && !c->skip){ //FIXME varc/d checks
1174  if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
1175  mb_type |= CANDIDATE_MB_TYPE_INTER_I;
1176  }
1177  }else{
1178  int intra_score, i;
1179  mb_type= CANDIDATE_MB_TYPE_INTER;
1180 
1181  dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1182  if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1183  dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1184 
1185  if((s->flags&CODEC_FLAG_4MV)
1186  && !c->skip && varc>50<<8 && vard>10<<8){
1187  int dmin4= h263_mv4_search(s, mx, my, shift);
1188  if(dmin4 < dmin){
1189  mb_type= CANDIDATE_MB_TYPE_INTER4V;
1190  dmin=dmin4;
1191  }
1192  }
1194  && !c->skip){ //FIXME varc/d checks
1195  int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
1196  if(dmin_i < dmin){
1197  mb_type = CANDIDATE_MB_TYPE_INTER_I;
1198  dmin= dmin_i;
1199  }
1200  }
1201 
1202  set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
1203 
1204  /* get intra luma score */
1205  if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
1206  intra_score= varc - 500;
1207  }else{
1208  unsigned mean = (sum+128)>>8;
1209  mean*= 0x01010101;
1210 
1211  for(i=0; i<16; i++){
1212  *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
1213  *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
1214  *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
1215  *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
1216  }
1217 
1218  intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
1219  }
1220  intra_score += c->mb_penalty_factor*16;
1221 
1222  if(intra_score < dmin){
1223  mb_type= CANDIDATE_MB_TYPE_INTRA;
1224  s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
1225  }else
1226  s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = 0;
1227 
1228  {
1229  int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1230  int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1231  c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1232  }
1233  }
1234 
1235  s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
1236 }
1237 
1239  int mb_x, int mb_y)
1240 {
1241  MotionEstContext * const c= &s->me;
1242  int mx, my, dmin;
1243  int P[10][2];
1244  const int shift= 1+s->quarter_sample;
1245  const int xy= mb_x + mb_y*s->mb_stride;
1246  init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
1247 
1248  av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
1249 
1252 
1253  get_limits(s, 16*mb_x, 16*mb_y);
1254  c->skip=0;
1255 
1256  P_LEFT[0] = s->p_mv_table[xy + 1][0];
1257  P_LEFT[1] = s->p_mv_table[xy + 1][1];
1258 
1259  if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
1260 
1261  /* special case for first line */
1262  if (s->first_slice_line) {
1263  c->pred_x= P_LEFT[0];
1264  c->pred_y= P_LEFT[1];
1265  P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1266  P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
1267  } else {
1268  P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
1269  P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
1270  P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
1271  P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
1272  if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
1273  if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
1274  if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
1275 
1276  P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1277  P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1278 
1279  c->pred_x = P_MEDIAN[0];
1280  c->pred_y = P_MEDIAN[1];
1281  }
1282 
1283  dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
1284 
1285  s->p_mv_table[xy][0] = mx<<shift;
1286  s->p_mv_table[xy][1] = my<<shift;
1287 
1288  return dmin;
1289 }
1290 
1292  int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
1293 {
1294  MotionEstContext * const c= &s->me;
1295  int mx, my, dmin;
1296  int P[10][2];
1297  const int shift= 1+s->quarter_sample;
1298  const int mot_stride = s->mb_stride;
1299  const int mot_xy = mb_y*mot_stride + mb_x;
1300  uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
1301  int mv_scale;
1302 
1307 
1308  get_limits(s, 16*mb_x, 16*mb_y);
1309 
1310  switch(s->me_method) {
1311  case ME_ZERO:
1312  default:
1313  mx = 0;
1314  my = 0;
1315  dmin = 0;
1316  break;
1317  case ME_X1:
1318  case ME_EPZS:
1319  P_LEFT[0] = mv_table[mot_xy - 1][0];
1320  P_LEFT[1] = mv_table[mot_xy - 1][1];
1321 
1322  if (P_LEFT[0] > (c->xmax << shift)) P_LEFT[0] = (c->xmax << shift);
1323 
1324  /* special case for first line */
1325  if (!s->first_slice_line) {
1326  P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1327  P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1328  P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
1329  P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
1330  if (P_TOP[1] > (c->ymax << shift)) P_TOP[1] = (c->ymax << shift);
1331  if (P_TOPRIGHT[0] < (c->xmin << shift)) P_TOPRIGHT[0] = (c->xmin << shift);
1332  if (P_TOPRIGHT[1] > (c->ymax << shift)) P_TOPRIGHT[1] = (c->ymax << shift);
1333 
1334  P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1335  P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1336  }
1337  c->pred_x = P_LEFT[0];
1338  c->pred_y = P_LEFT[1];
1339 
1340  if(mv_table == s->b_forw_mv_table){
1341  mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
1342  }else{
1343  mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
1344  }
1345 
1346  dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
1347 
1348  break;
1349  }
1350 
1351  dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
1352 
1353  if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1354  dmin= get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
1355 
1356 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1357  mv_table[mot_xy][0]= mx;
1358  mv_table[mot_xy][1]= my;
1359 
1360  return dmin;
1361 }
1362 
1363 static inline int check_bidir_mv(MpegEncContext * s,
1364  int motion_fx, int motion_fy,
1365  int motion_bx, int motion_by,
1366  int pred_fx, int pred_fy,
1367  int pred_bx, int pred_by,
1368  int size, int h)
1369 {
1370  //FIXME optimize?
1371  //FIXME better f_code prediction (max mv & distance)
1372  //FIXME pointers
1373  MotionEstContext * const c= &s->me;
1374  uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
1375  uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame
1376  int stride= c->stride;
1377  uint8_t *dest_y = c->scratchpad;
1378  uint8_t *ptr;
1379  int dxy;
1380  int src_x, src_y;
1381  int fbmin;
1382  uint8_t **src_data= c->src[0];
1383  uint8_t **ref_data= c->ref[0];
1384  uint8_t **ref2_data= c->ref[2];
1385 
1386  if(s->quarter_sample){
1387  dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1388  src_x = motion_fx >> 2;
1389  src_y = motion_fy >> 2;
1390 
1391  ptr = ref_data[0] + (src_y * stride) + src_x;
1392  s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , stride);
1393 
1394  dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1395  src_x = motion_bx >> 2;
1396  src_y = motion_by >> 2;
1397 
1398  ptr = ref2_data[0] + (src_y * stride) + src_x;
1399  s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride);
1400  }else{
1401  dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1402  src_x = motion_fx >> 1;
1403  src_y = motion_fy >> 1;
1404 
1405  ptr = ref_data[0] + (src_y * stride) + src_x;
1406  s->dsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1407 
1408  dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1409  src_x = motion_bx >> 1;
1410  src_y = motion_by >> 1;
1411 
1412  ptr = ref2_data[0] + (src_y * stride) + src_x;
1413  s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1414  }
1415 
1416  fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
1417  +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
1418  + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
1419 
1420  if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1421  }
1422  //FIXME CHROMA !!!
1423 
1424  return fbmin;
1425 }
1426 
1427 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1428 static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
1429 {
1430  MotionEstContext * const c= &s->me;
1431  const int mot_stride = s->mb_stride;
1432  const int xy = mb_y *mot_stride + mb_x;
1433  int fbmin;
1434  int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1435  int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1436  int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1437  int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1438  int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1439  int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1440  int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1441  int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1442  const int flags= c->sub_flags;
1443  const int qpel= flags&FLAG_QPEL;
1444  const int shift= 1+qpel;
1445  const int xmin= c->xmin<<shift;
1446  const int ymin= c->ymin<<shift;
1447  const int xmax= c->xmax<<shift;
1448  const int ymax= c->ymax<<shift;
1449 #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
1450 #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
1451  int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
1452  uint8_t map[256] = { 0 };
1453 
1454  map[hashidx&255] = 1;
1455 
1456  fbmin= check_bidir_mv(s, motion_fx, motion_fy,
1457  motion_bx, motion_by,
1458  pred_fx, pred_fy,
1459  pred_bx, pred_by,
1460  0, 16);
1461 
1462  if(s->avctx->bidir_refine){
1463  int end;
1464  static const uint8_t limittab[5]={0,8,32,64,80};
1465  const int limit= limittab[s->avctx->bidir_refine];
1466  static const int8_t vect[][4]={
1467 { 0, 0, 0, 1}, { 0, 0, 0,-1}, { 0, 0, 1, 0}, { 0, 0,-1, 0}, { 0, 1, 0, 0}, { 0,-1, 0, 0}, { 1, 0, 0, 0}, {-1, 0, 0, 0},
1468 
1469 { 0, 0, 1, 1}, { 0, 0,-1,-1}, { 0, 1, 1, 0}, { 0,-1,-1, 0}, { 1, 1, 0, 0}, {-1,-1, 0, 0}, { 1, 0, 0, 1}, {-1, 0, 0,-1},
1470 { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
1471 { 0, 0,-1, 1}, { 0, 0, 1,-1}, { 0,-1, 1, 0}, { 0, 1,-1, 0}, {-1, 1, 0, 0}, { 1,-1, 0, 0}, { 1, 0, 0,-1}, {-1, 0, 0, 1},
1472 { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
1473 
1474 { 0, 1, 1, 1}, { 0,-1,-1,-1}, { 1, 1, 1, 0}, {-1,-1,-1, 0}, { 1, 1, 0, 1}, {-1,-1, 0,-1}, { 1, 0, 1, 1}, {-1, 0,-1,-1},
1475 { 0,-1, 1, 1}, { 0, 1,-1,-1}, {-1, 1, 1, 0}, { 1,-1,-1, 0}, { 1, 1, 0,-1}, {-1,-1, 0, 1}, { 1, 0,-1, 1}, {-1, 0, 1,-1},
1476 { 0, 1,-1, 1}, { 0,-1, 1,-1}, { 1,-1, 1, 0}, {-1, 1,-1, 0}, {-1, 1, 0, 1}, { 1,-1, 0,-1}, { 1, 0, 1,-1}, {-1, 0,-1, 1},
1477 { 0, 1, 1,-1}, { 0,-1,-1, 1}, { 1, 1,-1, 0}, {-1,-1, 1, 0}, { 1,-1, 0, 1}, {-1, 1, 0,-1}, {-1, 0, 1, 1}, { 1, 0,-1,-1},
1478 
1479 { 1, 1, 1, 1}, {-1,-1,-1,-1},
1480 { 1, 1, 1,-1}, {-1,-1,-1, 1}, { 1, 1,-1, 1}, {-1,-1, 1,-1}, { 1,-1, 1, 1}, {-1, 1,-1,-1}, {-1, 1, 1, 1}, { 1,-1,-1,-1},
1481 { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
1482  };
1483  static const uint8_t hash[]={
1484 HASH8( 0, 0, 0, 1), HASH8( 0, 0, 0,-1), HASH8( 0, 0, 1, 0), HASH8( 0, 0,-1, 0), HASH8( 0, 1, 0, 0), HASH8( 0,-1, 0, 0), HASH8( 1, 0, 0, 0), HASH8(-1, 0, 0, 0),
1485 
1486 HASH8( 0, 0, 1, 1), HASH8( 0, 0,-1,-1), HASH8( 0, 1, 1, 0), HASH8( 0,-1,-1, 0), HASH8( 1, 1, 0, 0), HASH8(-1,-1, 0, 0), HASH8( 1, 0, 0, 1), HASH8(-1, 0, 0,-1),
1487 HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
1488 HASH8( 0, 0,-1, 1), HASH8( 0, 0, 1,-1), HASH8( 0,-1, 1, 0), HASH8( 0, 1,-1, 0), HASH8(-1, 1, 0, 0), HASH8( 1,-1, 0, 0), HASH8( 1, 0, 0,-1), HASH8(-1, 0, 0, 1),
1489 HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
1490 
1491 HASH8( 0, 1, 1, 1), HASH8( 0,-1,-1,-1), HASH8( 1, 1, 1, 0), HASH8(-1,-1,-1, 0), HASH8( 1, 1, 0, 1), HASH8(-1,-1, 0,-1), HASH8( 1, 0, 1, 1), HASH8(-1, 0,-1,-1),
1492 HASH8( 0,-1, 1, 1), HASH8( 0, 1,-1,-1), HASH8(-1, 1, 1, 0), HASH8( 1,-1,-1, 0), HASH8( 1, 1, 0,-1), HASH8(-1,-1, 0, 1), HASH8( 1, 0,-1, 1), HASH8(-1, 0, 1,-1),
1493 HASH8( 0, 1,-1, 1), HASH8( 0,-1, 1,-1), HASH8( 1,-1, 1, 0), HASH8(-1, 1,-1, 0), HASH8(-1, 1, 0, 1), HASH8( 1,-1, 0,-1), HASH8( 1, 0, 1,-1), HASH8(-1, 0,-1, 1),
1494 HASH8( 0, 1, 1,-1), HASH8( 0,-1,-1, 1), HASH8( 1, 1,-1, 0), HASH8(-1,-1, 1, 0), HASH8( 1,-1, 0, 1), HASH8(-1, 1, 0,-1), HASH8(-1, 0, 1, 1), HASH8( 1, 0,-1,-1),
1495 
1496 HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1),
1497 HASH8( 1, 1, 1,-1), HASH8(-1,-1,-1, 1), HASH8( 1, 1,-1, 1), HASH8(-1,-1, 1,-1), HASH8( 1,-1, 1, 1), HASH8(-1, 1,-1,-1), HASH8(-1, 1, 1, 1), HASH8( 1,-1,-1,-1),
1498 HASH8( 1, 1,-1,-1), HASH8(-1,-1, 1, 1), HASH8( 1,-1,-1, 1), HASH8(-1, 1, 1,-1), HASH8( 1,-1, 1,-1), HASH8(-1, 1,-1, 1),
1499 };
1500 
1501 #define CHECK_BIDIR(fx,fy,bx,by)\
1502  if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
1503  &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
1504  &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
1505  int score;\
1506  map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
1507  score= check_bidir_mv(s, motion_fx+fx, motion_fy+fy, motion_bx+bx, motion_by+by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);\
1508  if(score < fbmin){\
1509  hashidx += HASH(fx,fy,bx,by);\
1510  fbmin= score;\
1511  motion_fx+=fx;\
1512  motion_fy+=fy;\
1513  motion_bx+=bx;\
1514  motion_by+=by;\
1515  end=0;\
1516  }\
1517  }
1518 #define CHECK_BIDIR2(a,b,c,d)\
1519 CHECK_BIDIR(a,b,c,d)\
1520 CHECK_BIDIR(-(a),-(b),-(c),-(d))
1521 
1522  do{
1523  int i;
1524  int borderdist=0;
1525  end=1;
1526 
1527  CHECK_BIDIR2(0,0,0,1)
1528  CHECK_BIDIR2(0,0,1,0)
1529  CHECK_BIDIR2(0,1,0,0)
1530  CHECK_BIDIR2(1,0,0,0)
1531 
1532  for(i=8; i<limit; i++){
1533  int fx= motion_fx+vect[i][0];
1534  int fy= motion_fy+vect[i][1];
1535  int bx= motion_bx+vect[i][2];
1536  int by= motion_by+vect[i][3];
1537  if(borderdist<=0){
1538  int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
1539  int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
1540  if((a|b) < 0)
1541  map[(hashidx+hash[i])&255] = 1;
1542  }
1543  if(!map[(hashidx+hash[i])&255]){
1544  int score;
1545  map[(hashidx+hash[i])&255] = 1;
1546  score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
1547  if(score < fbmin){
1548  hashidx += hash[i];
1549  fbmin= score;
1550  motion_fx=fx;
1551  motion_fy=fy;
1552  motion_bx=bx;
1553  motion_by=by;
1554  end=0;
1555  borderdist--;
1556  if(borderdist<=0){
1557  int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
1558  int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
1559  borderdist= FFMIN(a,b);
1560  }
1561  }
1562  }
1563  }
1564  }while(!end);
1565  }
1566 
1567  s->b_bidir_forw_mv_table[xy][0]= motion_fx;
1568  s->b_bidir_forw_mv_table[xy][1]= motion_fy;
1569  s->b_bidir_back_mv_table[xy][0]= motion_bx;
1570  s->b_bidir_back_mv_table[xy][1]= motion_by;
1571 
1572  return fbmin;
1573 }
1574 
1575 static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
1576 {
1577  MotionEstContext * const c= &s->me;
1578  int P[10][2];
1579  const int mot_stride = s->mb_stride;
1580  const int mot_xy = mb_y*mot_stride + mb_x;
1581  const int shift= 1+s->quarter_sample;
1582  int dmin, i;
1583  const int time_pp= s->pp_time;
1584  const int time_pb= s->pb_time;
1585  int mx, my, xmin, xmax, ymin, ymax;
1586  int16_t (*mv_table)[2]= s->b_direct_mv_table;
1587 
1588  c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
1589  ymin= xmin=(-32)>>shift;
1590  ymax= xmax= 31>>shift;
1591 
1592  if (IS_8X8(s->next_picture.f.mb_type[mot_xy])) {
1593  s->mv_type= MV_TYPE_8X8;
1594  }else{
1595  s->mv_type= MV_TYPE_16X16;
1596  }
1597 
1598  for(i=0; i<4; i++){
1599  int index= s->block_index[i];
1600  int min, max;
1601 
1602  c->co_located_mv[i][0] = s->next_picture.f.motion_val[0][index][0];
1603  c->co_located_mv[i][1] = s->next_picture.f.motion_val[0][index][1];
1604  c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
1605  c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
1606 // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
1607 // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
1608 
1609  max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1610  min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1611  max+= 16*mb_x + 1; // +-1 is for the simpler rounding
1612  min+= 16*mb_x - 1;
1613  xmax= FFMIN(xmax, s->width - max);
1614  xmin= FFMAX(xmin, - 16 - min);
1615 
1616  max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1617  min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1618  max+= 16*mb_y + 1; // +-1 is for the simpler rounding
1619  min+= 16*mb_y - 1;
1620  ymax= FFMIN(ymax, s->height - max);
1621  ymin= FFMAX(ymin, - 16 - min);
1622 
1623  if(s->mv_type == MV_TYPE_16X16) break;
1624  }
1625 
1626  av_assert2(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1627 
1628  if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
1629  s->b_direct_mv_table[mot_xy][0]= 0;
1630  s->b_direct_mv_table[mot_xy][1]= 0;
1631 
1632  return 256*256*256*64;
1633  }
1634 
1635  c->xmin= xmin;
1636  c->ymin= ymin;
1637  c->xmax= xmax;
1638  c->ymax= ymax;
1639  c->flags |= FLAG_DIRECT;
1640  c->sub_flags |= FLAG_DIRECT;
1641  c->pred_x=0;
1642  c->pred_y=0;
1643 
1644  P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
1645  P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
1646 
1647  /* special case for first line */
1648  if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
1649  P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
1650  P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
1651  P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
1652  P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
1653 
1654  P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1655  P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1656  }
1657 
1658  dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
1659  if(c->sub_flags&FLAG_QPEL)
1660  dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1661  else
1662  dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1663 
1664  if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1665  dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1666 
1667  get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
1668 
1669  mv_table[mot_xy][0]= mx;
1670  mv_table[mot_xy][1]= my;
1671  c->flags &= ~FLAG_DIRECT;
1672  c->sub_flags &= ~FLAG_DIRECT;
1673 
1674  return dmin;
1675 }
1676 
1678  int mb_x, int mb_y)
1679 {
1680  MotionEstContext * const c= &s->me;
1681  const int penalty_factor= c->mb_penalty_factor;
1682  int fmin, bmin, dmin, fbmin, bimin, fimin;
1683  int type=0;
1684  const int xy = mb_y*s->mb_stride + mb_x;
1686  s->next_picture.f.data, 16 * mb_x, 16 * mb_y, 2);
1687 
1688  get_limits(s, 16*mb_x, 16*mb_y);
1689 
1690  c->skip=0;
1691 
1692  if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.f.mbskip_table[xy]) {
1693  int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
1694 
1695  score= ((unsigned)(score*score + 128*256))>>16;
1696  c->mc_mb_var_sum_temp += score;
1697  s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1698  s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
1699 
1700  return;
1701  }
1702 
1703  if(c->avctx->me_threshold){
1704  int vard= check_input_motion(s, mb_x, mb_y, 0);
1705 
1706  if((vard+128)>>8 < c->avctx->me_threshold){
1707 // pix = c->src[0][0];
1708 // sum = s->dsp.pix_sum(pix, s->linesize);
1709 // varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
1710 
1711 // pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
1712  s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
1713 /* pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
1714  c->mb_var_sum_temp += (varc+128)>>8;*/
1715  c->mc_mb_var_sum_temp += (vard+128)>>8;
1716 /* if (vard <= 64<<8 || vard < varc) {
1717  c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1718  }else{
1719  c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
1720  }*/
1721  return;
1722  }
1723  if((vard+128)>>8 < c->avctx->mb_threshold){
1724  type= s->mb_type[mb_y*s->mb_stride + mb_x];
1725  if(type == CANDIDATE_MB_TYPE_DIRECT){
1726  direct_search(s, mb_x, mb_y);
1727  }
1728  if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
1729  c->skip=0;
1730  ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
1731  }
1732  if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
1733  c->skip=0;
1734  ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
1735  }
1737  c->skip=0;
1739  interlaced_search(s, 0,
1741  s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
1742  }
1744  c->skip=0;
1746  interlaced_search(s, 2,
1748  s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
1749  }
1750  return;
1751  }
1752  }
1753 
1754  if (s->codec_id == AV_CODEC_ID_MPEG4)
1755  dmin= direct_search(s, mb_x, mb_y);
1756  else
1757  dmin= INT_MAX;
1758 //FIXME penalty stuff for non mpeg4
1759  c->skip=0;
1760  fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
1761 
1762  c->skip=0;
1763  bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
1764  av_dlog(s, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1765 
1766  c->skip=0;
1767  fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
1768  av_dlog(s, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1769 
1771 //FIXME mb type penalty
1772  c->skip=0;
1774  fimin= interlaced_search(s, 0,
1776  s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
1778  bimin= interlaced_search(s, 2,
1780  s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
1781  }else
1782  fimin= bimin= INT_MAX;
1783 
1784  {
1785  int score= fmin;
1787 
1788  if (dmin <= score){
1789  score = dmin;
1790  type = CANDIDATE_MB_TYPE_DIRECT;
1791  }
1792  if(bmin<score){
1793  score=bmin;
1795  }
1796  if(fbmin<score){
1797  score=fbmin;
1799  }
1800  if(fimin<score){
1801  score=fimin;
1803  }
1804  if(bimin<score){
1805  score=bimin;
1807  }
1808 
1809  score= ((unsigned)(score*score + 128*256))>>16;
1810  c->mc_mb_var_sum_temp += score;
1811  s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1812  }
1813 
1816  if(fimin < INT_MAX)
1818  if(bimin < INT_MAX)
1820  if(fimin < INT_MAX && bimin < INT_MAX){
1821  type |= CANDIDATE_MB_TYPE_BIDIR_I;
1822  }
1823  //FIXME something smarter
1824  if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
1825  if(s->codec_id == AV_CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
1826  type |= CANDIDATE_MB_TYPE_DIRECT0;
1827  }
1828 
1829  s->mb_type[mb_y*s->mb_stride + mb_x]= type;
1830 }
1831 
1832 /* find best f_code for ME which do unlimited searches */
1833 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
1834 {
1835  if(s->me_method>=ME_EPZS){
1836  int score[8];
1837  int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
1838  uint8_t * fcode_tab= s->fcode_tab;
1839  int best_fcode=-1;
1840  int best_score=-10000000;
1841 
1842  if(s->msmpeg4_version)
1843  range= FFMIN(range, 16);
1845  range= FFMIN(range, 256);
1846 
1847  for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
1848 
1849  for(y=0; y<s->mb_height; y++){
1850  int x;
1851  int xy= y*s->mb_stride;
1852  for(x=0; x<s->mb_width; x++){
1853  if(s->mb_type[xy] & type){
1854  int mx= mv_table[xy][0];
1855  int my= mv_table[xy][1];
1856  int fcode= FFMAX(fcode_tab[mx + MAX_MV],
1857  fcode_tab[my + MAX_MV]);
1858  int j;
1859 
1860  if(mx >= range || mx < -range ||
1861  my >= range || my < -range)
1862  continue;
1863 
1864  for(j=0; j<fcode && j<8; j++){
1866  score[j]-= 170;
1867  }
1868  }
1869  xy++;
1870  }
1871  }
1872 
1873  for(i=1; i<8; i++){
1874  if(score[i] > best_score){
1875  best_score= score[i];
1876  best_fcode= i;
1877  }
1878  }
1879 
1880  return best_fcode;
1881  }else{
1882  return 1;
1883  }
1884 }
1885 
1887 {
1888  MotionEstContext * const c= &s->me;
1889  const int f_code= s->f_code;
1890  int y, range;
1892 
1893  range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
1894 
1895  av_assert0(range <= 16 || !s->msmpeg4_version);
1897 
1898  if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1899 
1900  if(s->flags&CODEC_FLAG_4MV){
1901  const int wrap= s->b8_stride;
1902 
1903  /* clip / convert to intra 8x8 type MVs */
1904  for(y=0; y<s->mb_height; y++){
1905  int xy= y*2*wrap;
1906  int i= y*s->mb_stride;
1907  int x;
1908 
1909  for(x=0; x<s->mb_width; x++){
1911  int block;
1912  for(block=0; block<4; block++){
1913  int off= (block& 1) + (block>>1)*wrap;
1914  int mx = s->current_picture.f.motion_val[0][ xy + off ][0];
1915  int my = s->current_picture.f.motion_val[0][ xy + off ][1];
1916 
1917  if( mx >=range || mx <-range
1918  || my >=range || my <-range){
1919  s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
1922  }
1923  }
1924  }
1925  xy+=2;
1926  i++;
1927  }
1928  }
1929  }
1930 }
1931 
1936 void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
1937  int16_t (*mv_table)[2], int f_code, int type, int truncate)
1938 {
1939  MotionEstContext * const c= &s->me;
1940  int y, h_range, v_range;
1941 
1942  // RAL: 8 in MPEG-1, 16 in MPEG-4
1943  int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
1944 
1945  if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1946 
1947  h_range= range;
1948  v_range= field_select_table ? range>>1 : range;
1949 
1950  /* clip / convert to intra 16x16 type MVs */
1951  for(y=0; y<s->mb_height; y++){
1952  int x;
1953  int xy= y*s->mb_stride;
1954  for(x=0; x<s->mb_width; x++){
1955  if (s->mb_type[xy] & type){ // RAL: "type" test added...
1956  if(field_select_table==NULL || field_select_table[xy] == field_select){
1957  if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
1958  || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
1959 
1960  if(truncate){
1961  if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
1962  else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
1963  if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
1964  else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
1965  }else{
1966  s->mb_type[xy] &= ~type;
1967  s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
1968  mv_table[xy][0]=
1969  mv_table[xy][1]= 0;
1970  }
1971  }
1972  }
1973  }
1974  xy++;
1975  }
1976  }
1977 }