]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideo_motion.c
cavsdec: check dimensions being valid.
[ffmpeg] / libavcodec / mpegvideo_motion.c
1 /*
2  * Copyright (c) 2000,2001 Fabrice Bellard
3  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <string.h>
25 #include "libavutil/avassert.h"
26 #include "avcodec.h"
27 #include "dsputil.h"
28 #include "mpegvideo.h"
29 #include "mjpegenc.h"
30 #include "msmpeg4.h"
31 #include <limits.h>
32
33 static inline void gmc1_motion(MpegEncContext *s,
34                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
35                                uint8_t **ref_picture)
36 {
37     uint8_t *ptr;
38     int offset, src_x, src_y, linesize, uvlinesize;
39     int motion_x, motion_y;
40     int emu=0;
41
42     motion_x= s->sprite_offset[0][0];
43     motion_y= s->sprite_offset[0][1];
44     src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
45     src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
46     motion_x<<=(3-s->sprite_warping_accuracy);
47     motion_y<<=(3-s->sprite_warping_accuracy);
48     src_x = av_clip(src_x, -16, s->width);
49     if (src_x == s->width)
50         motion_x =0;
51     src_y = av_clip(src_y, -16, s->height);
52     if (src_y == s->height)
53         motion_y =0;
54
55     linesize = s->linesize;
56     uvlinesize = s->uvlinesize;
57
58     ptr = ref_picture[0] + (src_y * linesize) + src_x;
59
60     if(s->flags&CODEC_FLAG_EMU_EDGE){
61         if(   (unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0)
62            || (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)){
63             s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
64             ptr= s->edge_emu_buffer;
65         }
66     }
67
68     if((motion_x|motion_y)&7){
69         s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
70         s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
71     }else{
72         int dxy;
73
74         dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
75         if (s->no_rounding){
76             s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
77         }else{
78             s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
79         }
80     }
81
82     if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
83
84     motion_x= s->sprite_offset[1][0];
85     motion_y= s->sprite_offset[1][1];
86     src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
87     src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
88     motion_x<<=(3-s->sprite_warping_accuracy);
89     motion_y<<=(3-s->sprite_warping_accuracy);
90     src_x = av_clip(src_x, -8, s->width>>1);
91     if (src_x == s->width>>1)
92         motion_x =0;
93     src_y = av_clip(src_y, -8, s->height>>1);
94     if (src_y == s->height>>1)
95         motion_y =0;
96
97     offset = (src_y * uvlinesize) + src_x;
98     ptr = ref_picture[1] + offset;
99     if(s->flags&CODEC_FLAG_EMU_EDGE){
100         if(   (unsigned)src_x >= FFMAX((s->h_edge_pos>>1) - 9, 0)
101            || (unsigned)src_y >= FFMAX((s->v_edge_pos>>1) - 9, 0)){
102             s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
103             ptr= s->edge_emu_buffer;
104             emu=1;
105         }
106     }
107     s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
108
109     ptr = ref_picture[2] + offset;
110     if(emu){
111         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
112         ptr= s->edge_emu_buffer;
113     }
114     s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
115
116     return;
117 }
118
119 static inline void gmc_motion(MpegEncContext *s,
120                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
121                                uint8_t **ref_picture)
122 {
123     uint8_t *ptr;
124     int linesize, uvlinesize;
125     const int a= s->sprite_warping_accuracy;
126     int ox, oy;
127
128     linesize = s->linesize;
129     uvlinesize = s->uvlinesize;
130
131     ptr = ref_picture[0];
132
133     ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
134     oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
135
136     s->dsp.gmc(dest_y, ptr, linesize, 16,
137            ox,
138            oy,
139            s->sprite_delta[0][0], s->sprite_delta[0][1],
140            s->sprite_delta[1][0], s->sprite_delta[1][1],
141            a+1, (1<<(2*a+1)) - s->no_rounding,
142            s->h_edge_pos, s->v_edge_pos);
143     s->dsp.gmc(dest_y+8, ptr, linesize, 16,
144            ox + s->sprite_delta[0][0]*8,
145            oy + s->sprite_delta[1][0]*8,
146            s->sprite_delta[0][0], s->sprite_delta[0][1],
147            s->sprite_delta[1][0], s->sprite_delta[1][1],
148            a+1, (1<<(2*a+1)) - s->no_rounding,
149            s->h_edge_pos, s->v_edge_pos);
150
151     if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
152
153     ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
154     oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
155
156     ptr = ref_picture[1];
157     s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
158            ox,
159            oy,
160            s->sprite_delta[0][0], s->sprite_delta[0][1],
161            s->sprite_delta[1][0], s->sprite_delta[1][1],
162            a+1, (1<<(2*a+1)) - s->no_rounding,
163            s->h_edge_pos>>1, s->v_edge_pos>>1);
164
165     ptr = ref_picture[2];
166     s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
167            ox,
168            oy,
169            s->sprite_delta[0][0], s->sprite_delta[0][1],
170            s->sprite_delta[1][0], s->sprite_delta[1][1],
171            a+1, (1<<(2*a+1)) - s->no_rounding,
172            s->h_edge_pos>>1, s->v_edge_pos>>1);
173 }
174
175 static inline int hpel_motion(MpegEncContext *s,
176                                   uint8_t *dest, uint8_t *src,
177                                   int field_based, int field_select,
178                                   int src_x, int src_y,
179                                   int width, int height, int stride,
180                                   int h_edge_pos, int v_edge_pos,
181                                   int w, int h, op_pixels_func *pix_op,
182                                   int motion_x, int motion_y)
183 {
184     int dxy;
185     int emu=0;
186
187     dxy = ((motion_y & 1) << 1) | (motion_x & 1);
188     src_x += motion_x >> 1;
189     src_y += motion_y >> 1;
190
191     /* WARNING: do no forget half pels */
192     src_x = av_clip(src_x, -16, width); //FIXME unneeded for emu?
193     if (src_x == width)
194         dxy &= ~1;
195     src_y = av_clip(src_y, -16, height);
196     if (src_y == height)
197         dxy &= ~2;
198     src += src_y * stride + src_x;
199
200     if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
201         if(   (unsigned)src_x > FFMAX(h_edge_pos - (motion_x&1) - w, 0)
202            || (unsigned)src_y > FFMAX(v_edge_pos - (motion_y&1) - h, 0)){
203             s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
204                              src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
205             src= s->edge_emu_buffer;
206             emu=1;
207         }
208     }
209     if(field_select)
210         src += s->linesize;
211     pix_op[dxy](dest, src, stride, h);
212     return emu;
213 }
214
215 static av_always_inline
216 void mpeg_motion_internal(MpegEncContext *s,
217                  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
218                  int field_based, int bottom_field, int field_select,
219                  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
220                  int motion_x, int motion_y, int h, int is_mpeg12, int mb_y)
221 {
222     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
223     int dxy, uvdxy, mx, my, src_x, src_y,
224         uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
225
226 #if 0
227 if(s->quarter_sample)
228 {
229     motion_x>>=1;
230     motion_y>>=1;
231 }
232 #endif
233
234     v_edge_pos = s->v_edge_pos >> field_based;
235     linesize   = s->current_picture.f.linesize[0] << field_based;
236     uvlinesize = s->current_picture.f.linesize[1] << field_based;
237
238     dxy = ((motion_y & 1) << 1) | (motion_x & 1);
239     src_x = s->mb_x* 16               + (motion_x >> 1);
240     src_y =(   mb_y<<(4-field_based)) + (motion_y >> 1);
241
242     if (!is_mpeg12 && s->out_format == FMT_H263) {
243         if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
244             mx = (motion_x>>1)|(motion_x&1);
245             my = motion_y >>1;
246             uvdxy = ((my & 1) << 1) | (mx & 1);
247             uvsrc_x = s->mb_x* 8               + (mx >> 1);
248             uvsrc_y =(   mb_y<<(3-field_based))+ (my >> 1);
249         }else{
250             uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
251             uvsrc_x = src_x>>1;
252             uvsrc_y = src_y>>1;
253         }
254     }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
255         mx = motion_x / 4;
256         my = motion_y / 4;
257         uvdxy = 0;
258         uvsrc_x = s->mb_x*8 + mx;
259         uvsrc_y =    mb_y*8 + my;
260     } else {
261         if(s->chroma_y_shift){
262             mx = motion_x / 2;
263             my = motion_y / 2;
264             uvdxy = ((my & 1) << 1) | (mx & 1);
265             uvsrc_x = s->mb_x* 8               + (mx >> 1);
266             uvsrc_y =(   mb_y<<(3-field_based))+ (my >> 1);
267         } else {
268             if(s->chroma_x_shift){
269             //Chroma422
270                 mx = motion_x / 2;
271                 uvdxy = ((motion_y & 1) << 1) | (mx & 1);
272                 uvsrc_x = s->mb_x* 8           + (mx >> 1);
273                 uvsrc_y = src_y;
274             } else {
275             //Chroma444
276                 uvdxy = dxy;
277                 uvsrc_x = src_x;
278                 uvsrc_y = src_y;
279             }
280         }
281     }
282
283     ptr_y  = ref_picture[0] + src_y * linesize + src_x;
284     ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
285     ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
286
287     if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
288        || (unsigned)src_y > FFMAX(   v_edge_pos - (motion_y&1) - h , 0)){
289             if(is_mpeg12 || s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
290                s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
291                 av_log(s->avctx,AV_LOG_DEBUG,
292                         "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
293                 return;
294             }
295             s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
296                                 17, 17+field_based,
297                                 src_x, src_y<<field_based,
298                                 s->h_edge_pos, s->v_edge_pos);
299             ptr_y = s->edge_emu_buffer;
300             if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
301                 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
302                 s->dsp.emulated_edge_mc(uvbuf ,
303                                     ptr_cb, s->uvlinesize,
304                                     9, 9+field_based,
305                                     uvsrc_x, uvsrc_y<<field_based,
306                                     s->h_edge_pos>>1, s->v_edge_pos>>1);
307                 s->dsp.emulated_edge_mc(uvbuf+16,
308                                     ptr_cr, s->uvlinesize,
309                                     9, 9+field_based,
310                                     uvsrc_x, uvsrc_y<<field_based,
311                                     s->h_edge_pos>>1, s->v_edge_pos>>1);
312                 ptr_cb= uvbuf;
313                 ptr_cr= uvbuf+16;
314             }
315     }
316
317     if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
318         dest_y += s->linesize;
319         dest_cb+= s->uvlinesize;
320         dest_cr+= s->uvlinesize;
321     }
322
323     if(field_select){
324         ptr_y += s->linesize;
325         ptr_cb+= s->uvlinesize;
326         ptr_cr+= s->uvlinesize;
327     }
328
329     pix_op[0][dxy](dest_y, ptr_y, linesize, h);
330
331     if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
332         pix_op[s->chroma_x_shift][uvdxy]
333                 (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
334         pix_op[s->chroma_x_shift][uvdxy]
335                 (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
336     }
337     if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
338          s->out_format == FMT_H261){
339         ff_h261_loop_filter(s);
340     }
341 }
342 /* apply one mpeg motion vector to the three components */
343 static void mpeg_motion(MpegEncContext *s,
344                         uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
345                         int field_select, uint8_t **ref_picture,
346                         op_pixels_func (*pix_op)[4],
347                         int motion_x, int motion_y, int h, int mb_y)
348 {
349 #if !CONFIG_SMALL
350     if(s->out_format == FMT_MPEG1)
351         mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
352                     field_select, ref_picture, pix_op,
353                     motion_x, motion_y, h, 1, mb_y);
354     else
355 #endif
356         mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
357                     field_select, ref_picture, pix_op,
358                     motion_x, motion_y, h, 0, mb_y);
359 }
360
361 static void mpeg_motion_field(MpegEncContext *s, uint8_t *dest_y,
362                               uint8_t *dest_cb, uint8_t *dest_cr,
363                               int bottom_field, int field_select,
364                               uint8_t **ref_picture,
365                               op_pixels_func (*pix_op)[4],
366                               int motion_x, int motion_y, int h, int mb_y)
367 {
368 #if !CONFIG_SMALL
369     if(s->out_format == FMT_MPEG1)
370         mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
371                     bottom_field, field_select, ref_picture, pix_op,
372                     motion_x, motion_y, h, 1, mb_y);
373     else
374 #endif
375         mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
376                     bottom_field, field_select, ref_picture, pix_op,
377                     motion_x, motion_y, h, 0, mb_y);
378 }
379
380 //FIXME move to dsputil, avg variant, 16x16 version
381 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
382     int x;
383     uint8_t * const top   = src[1];
384     uint8_t * const left  = src[2];
385     uint8_t * const mid   = src[0];
386     uint8_t * const right = src[3];
387     uint8_t * const bottom= src[4];
388 #define OBMC_FILTER(x, t, l, m, r, b)\
389     dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
390 #define OBMC_FILTER4(x, t, l, m, r, b)\
391     OBMC_FILTER(x         , t, l, m, r, b);\
392     OBMC_FILTER(x+1       , t, l, m, r, b);\
393     OBMC_FILTER(x  +stride, t, l, m, r, b);\
394     OBMC_FILTER(x+1+stride, t, l, m, r, b);
395
396     x=0;
397     OBMC_FILTER (x  , 2, 2, 4, 0, 0);
398     OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
399     OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
400     OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
401     OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
402     OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
403     x+= stride;
404     OBMC_FILTER (x  , 1, 2, 5, 0, 0);
405     OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
406     OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
407     OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
408     x+= stride;
409     OBMC_FILTER4(x  , 1, 2, 5, 0, 0);
410     OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
411     OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
412     OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
413     x+= 2*stride;
414     OBMC_FILTER4(x  , 0, 2, 5, 0, 1);
415     OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
416     OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
417     OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
418     x+= 2*stride;
419     OBMC_FILTER (x  , 0, 2, 5, 0, 1);
420     OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
421     OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
422     OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
423     OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
424     OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
425     x+= stride;
426     OBMC_FILTER (x  , 0, 2, 4, 0, 2);
427     OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
428     OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
429     OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
430 }
431
432 /* obmc for 1 8x8 luma block */
433 static inline void obmc_motion(MpegEncContext *s,
434                                uint8_t *dest, uint8_t *src,
435                                int src_x, int src_y,
436                                op_pixels_func *pix_op,
437                                int16_t mv[5][2]/* mid top left right bottom*/)
438 #define MID    0
439 {
440     int i;
441     uint8_t *ptr[5];
442
443     av_assert2(s->quarter_sample==0);
444
445     for(i=0; i<5; i++){
446         if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
447             ptr[i]= ptr[MID];
448         }else{
449             ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
450             hpel_motion(s, ptr[i], src, 0, 0,
451                         src_x, src_y,
452                         s->width, s->height, s->linesize,
453                         s->h_edge_pos, s->v_edge_pos,
454                         8, 8, pix_op,
455                         mv[i][0], mv[i][1]);
456         }
457     }
458
459     put_obmc(dest, ptr, s->linesize);
460 }
461
462 static inline void qpel_motion(MpegEncContext *s,
463                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
464                                int field_based, int bottom_field, int field_select,
465                                uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
466                                qpel_mc_func (*qpix_op)[16],
467                                int motion_x, int motion_y, int h)
468 {
469     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
470     int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
471
472     dxy = ((motion_y & 3) << 2) | (motion_x & 3);
473     src_x = s->mb_x *  16                 + (motion_x >> 2);
474     src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
475
476     v_edge_pos = s->v_edge_pos >> field_based;
477     linesize = s->linesize << field_based;
478     uvlinesize = s->uvlinesize << field_based;
479
480     if(field_based){
481         mx= motion_x/2;
482         my= motion_y>>1;
483     }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
484         static const int rtab[8]= {0,0,1,1,0,0,0,1};
485         mx= (motion_x>>1) + rtab[motion_x&7];
486         my= (motion_y>>1) + rtab[motion_y&7];
487     }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
488         mx= (motion_x>>1)|(motion_x&1);
489         my= (motion_y>>1)|(motion_y&1);
490     }else{
491         mx= motion_x/2;
492         my= motion_y/2;
493     }
494     mx= (mx>>1)|(mx&1);
495     my= (my>>1)|(my&1);
496
497     uvdxy= (mx&1) | ((my&1)<<1);
498     mx>>=1;
499     my>>=1;
500
501     uvsrc_x = s->mb_x *  8                 + mx;
502     uvsrc_y = s->mb_y * (8 >> field_based) + my;
503
504     ptr_y  = ref_picture[0] +   src_y *   linesize +   src_x;
505     ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
506     ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
507
508     if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
509        || (unsigned)src_y > FFMAX(   v_edge_pos - (motion_y&3) - h , 0)){
510         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
511                             17, 17+field_based, src_x, src_y<<field_based,
512                             s->h_edge_pos, s->v_edge_pos);
513         ptr_y= s->edge_emu_buffer;
514         if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
515             uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
516             s->dsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
517                                 9, 9 + field_based,
518                                 uvsrc_x, uvsrc_y<<field_based,
519                                 s->h_edge_pos>>1, s->v_edge_pos>>1);
520             s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
521                                 9, 9 + field_based,
522                                 uvsrc_x, uvsrc_y<<field_based,
523                                 s->h_edge_pos>>1, s->v_edge_pos>>1);
524             ptr_cb= uvbuf;
525             ptr_cr= uvbuf + 16;
526         }
527     }
528
529     if(!field_based)
530         qpix_op[0][dxy](dest_y, ptr_y, linesize);
531     else{
532         if(bottom_field){
533             dest_y += s->linesize;
534             dest_cb+= s->uvlinesize;
535             dest_cr+= s->uvlinesize;
536         }
537
538         if(field_select){
539             ptr_y  += s->linesize;
540             ptr_cb += s->uvlinesize;
541             ptr_cr += s->uvlinesize;
542         }
543         //damn interlaced mode
544         //FIXME boundary mirroring is not exactly correct here
545         qpix_op[1][dxy](dest_y  , ptr_y  , linesize);
546         qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
547     }
548     if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
549         pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
550         pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
551     }
552 }
553
554 /**
555  * h263 chroma 4mv motion compensation.
556  */
557 static inline void chroma_4mv_motion(MpegEncContext *s,
558                                      uint8_t *dest_cb, uint8_t *dest_cr,
559                                      uint8_t **ref_picture,
560                                      op_pixels_func *pix_op,
561                                      int mx, int my){
562     int dxy, emu=0, src_x, src_y, offset;
563     uint8_t *ptr;
564
565     /* In case of 8X8, we construct a single chroma motion vector
566        with a special rounding */
567     mx= ff_h263_round_chroma(mx);
568     my= ff_h263_round_chroma(my);
569
570     dxy = ((my & 1) << 1) | (mx & 1);
571     mx >>= 1;
572     my >>= 1;
573
574     src_x = s->mb_x * 8 + mx;
575     src_y = s->mb_y * 8 + my;
576     src_x = av_clip(src_x, -8, (s->width >> 1));
577     if (src_x == (s->width >> 1))
578         dxy &= ~1;
579     src_y = av_clip(src_y, -8, (s->height >> 1));
580     if (src_y == (s->height >> 1))
581         dxy &= ~2;
582
583     offset = src_y * s->uvlinesize + src_x;
584     ptr = ref_picture[1] + offset;
585     if(s->flags&CODEC_FLAG_EMU_EDGE){
586         if(   (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
587            || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
588             s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
589                                 9, 9, src_x, src_y,
590                                 s->h_edge_pos>>1, s->v_edge_pos>>1);
591             ptr= s->edge_emu_buffer;
592             emu=1;
593         }
594     }
595     pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
596
597     ptr = ref_picture[2] + offset;
598     if(emu){
599         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
600                             9, 9, src_x, src_y,
601                             s->h_edge_pos>>1, s->v_edge_pos>>1);
602         ptr= s->edge_emu_buffer;
603     }
604     pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
605 }
606
607 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
608     /* fetch pixels for estimated mv 4 macroblocks ahead
609      * optimized for 64byte cache lines */
610     const int shift = s->quarter_sample ? 2 : 1;
611     const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
612     const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
613     int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
614     s->dsp.prefetch(pix[0]+off, s->linesize, 4);
615     off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
616     s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
617 }
618
619 /**
620  * motion compensation of a single macroblock
621  * @param s context
622  * @param dest_y luma destination pointer
623  * @param dest_cb chroma cb/u destination pointer
624  * @param dest_cr chroma cr/v destination pointer
625  * @param dir direction (0->forward, 1->backward)
626  * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
627  * @param pix_op halfpel motion compensation function (average or put normally)
628  * @param qpix_op qpel motion compensation function (average or put normally)
629  * the motion vectors are taken from s->mv and the MV type from s->mv_type
630  */
631 static av_always_inline void MPV_motion_internal(MpegEncContext *s,
632                               uint8_t *dest_y, uint8_t *dest_cb,
633                               uint8_t *dest_cr, int dir,
634                               uint8_t **ref_picture,
635                               op_pixels_func (*pix_op)[4],
636                               qpel_mc_func (*qpix_op)[16], int is_mpeg12)
637 {
638     int dxy, mx, my, src_x, src_y, motion_x, motion_y;
639     int mb_x, mb_y, i;
640     uint8_t *ptr, *dest;
641
642     mb_x = s->mb_x;
643     mb_y = s->mb_y;
644
645     prefetch_motion(s, ref_picture, dir);
646
647     if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
648         int16_t mv_cache[4][4][2];
649         const int xy= s->mb_x + s->mb_y*s->mb_stride;
650         const int mot_stride= s->b8_stride;
651         const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
652
653         av_assert2(!s->mb_skipped);
654
655         memcpy(mv_cache[1][1], s->current_picture.f.motion_val[0][mot_xy             ], sizeof(int16_t) * 4);
656         memcpy(mv_cache[2][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
657         memcpy(mv_cache[3][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
658
659         if (mb_y == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - s->mb_stride])) {
660             memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
661         }else{
662             memcpy(mv_cache[0][1], s->current_picture.f.motion_val[0][mot_xy - mot_stride], sizeof(int16_t) * 4);
663         }
664
665         if (mb_x == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - 1])) {
666             AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
667             AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
668         }else{
669             AV_COPY32(mv_cache[1][0], s->current_picture.f.motion_val[0][mot_xy - 1]);
670             AV_COPY32(mv_cache[2][0], s->current_picture.f.motion_val[0][mot_xy - 1 + mot_stride]);
671         }
672
673         if (mb_x + 1 >= s->mb_width || IS_INTRA(s->current_picture.f.mb_type[xy + 1])) {
674             AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
675             AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
676         }else{
677             AV_COPY32(mv_cache[1][3], s->current_picture.f.motion_val[0][mot_xy + 2]);
678             AV_COPY32(mv_cache[2][3], s->current_picture.f.motion_val[0][mot_xy + 2 + mot_stride]);
679         }
680
681         mx = 0;
682         my = 0;
683         for(i=0;i<4;i++) {
684             const int x= (i&1)+1;
685             const int y= (i>>1)+1;
686             int16_t mv[5][2]= {
687                 {mv_cache[y][x  ][0], mv_cache[y][x  ][1]},
688                 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
689                 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
690                 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
691                 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
692             //FIXME cleanup
693             obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
694                         ref_picture[0],
695                         mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
696                         pix_op[1],
697                         mv);
698
699             mx += mv[0][0];
700             my += mv[0][1];
701         }
702         if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
703             chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
704
705         return;
706     }
707
708     switch(s->mv_type) {
709     case MV_TYPE_16X16:
710         if(s->mcsel){
711             if(s->real_sprite_warping_points==1){
712                 gmc1_motion(s, dest_y, dest_cb, dest_cr,
713                             ref_picture);
714             }else{
715                 gmc_motion(s, dest_y, dest_cb, dest_cr,
716                             ref_picture);
717             }
718         }else if(!is_mpeg12 && s->quarter_sample){
719             qpel_motion(s, dest_y, dest_cb, dest_cr,
720                         0, 0, 0,
721                         ref_picture, pix_op, qpix_op,
722                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
723         } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
724                    s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
725             ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
726                         ref_picture, pix_op,
727                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
728         }else
729         {
730             mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
731                         ref_picture, pix_op,
732                         s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
733         }
734         break;
735     case MV_TYPE_8X8:
736     if (!is_mpeg12) {
737         mx = 0;
738         my = 0;
739         if(s->quarter_sample){
740             for(i=0;i<4;i++) {
741                 motion_x = s->mv[dir][i][0];
742                 motion_y = s->mv[dir][i][1];
743
744                 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
745                 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
746                 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
747
748                 /* WARNING: do no forget half pels */
749                 src_x = av_clip(src_x, -16, s->width);
750                 if (src_x == s->width)
751                     dxy &= ~3;
752                 src_y = av_clip(src_y, -16, s->height);
753                 if (src_y == s->height)
754                     dxy &= ~12;
755
756                 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
757                 if(s->flags&CODEC_FLAG_EMU_EDGE){
758                     if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
759                        || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
760                         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
761                                             s->linesize, 9, 9,
762                                             src_x, src_y,
763                                             s->h_edge_pos, s->v_edge_pos);
764                         ptr= s->edge_emu_buffer;
765                     }
766                 }
767                 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
768                 qpix_op[1][dxy](dest, ptr, s->linesize);
769
770                 mx += s->mv[dir][i][0]/2;
771                 my += s->mv[dir][i][1]/2;
772             }
773         }else{
774             for(i=0;i<4;i++) {
775                 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
776                             ref_picture[0], 0, 0,
777                             mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
778                             s->width, s->height, s->linesize,
779                             s->h_edge_pos, s->v_edge_pos,
780                             8, 8, pix_op[1],
781                             s->mv[dir][i][0], s->mv[dir][i][1]);
782
783                 mx += s->mv[dir][i][0];
784                 my += s->mv[dir][i][1];
785             }
786         }
787
788         if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
789             chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
790     }
791         break;
792     case MV_TYPE_FIELD:
793         if (s->picture_structure == PICT_FRAME) {
794             if(!is_mpeg12 && s->quarter_sample){
795                 for(i=0; i<2; i++){
796                     qpel_motion(s, dest_y, dest_cb, dest_cr,
797                                 1, i, s->field_select[dir][i],
798                                 ref_picture, pix_op, qpix_op,
799                                 s->mv[dir][i][0], s->mv[dir][i][1], 8);
800                 }
801             }else{
802                 /* top field */
803                 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
804                                   0, s->field_select[dir][0],
805                                   ref_picture, pix_op,
806                                   s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
807                 /* bottom field */
808                 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
809                                   1, s->field_select[dir][1],
810                                   ref_picture, pix_op,
811                                   s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
812             }
813         } else {
814             if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
815                 ref_picture = s->current_picture_ptr->f.data;
816             }
817
818             mpeg_motion(s, dest_y, dest_cb, dest_cr,
819                         s->field_select[dir][0],
820                         ref_picture, pix_op,
821                         s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
822         }
823         break;
824     case MV_TYPE_16X8:
825         for(i=0; i<2; i++){
826             uint8_t ** ref2picture;
827
828             if(s->picture_structure == s->field_select[dir][i] + 1
829                || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
830                 ref2picture= ref_picture;
831             }else{
832                 ref2picture = s->current_picture_ptr->f.data;
833             }
834
835             mpeg_motion(s, dest_y, dest_cb, dest_cr,
836                         s->field_select[dir][i],
837                         ref2picture, pix_op,
838                         s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
839
840             dest_y += 16*s->linesize;
841             dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
842             dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
843         }
844         break;
845     case MV_TYPE_DMV:
846         if(s->picture_structure == PICT_FRAME){
847             for(i=0; i<2; i++){
848                 int j;
849                 for(j=0; j<2; j++){
850                     mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
851                                       j, j^i, ref_picture, pix_op,
852                                       s->mv[dir][2*i + j][0],
853                                       s->mv[dir][2*i + j][1], 8, mb_y);
854                 }
855                 pix_op = s->dsp.avg_pixels_tab;
856             }
857         }else{
858             for(i=0; i<2; i++){
859                 mpeg_motion(s, dest_y, dest_cb, dest_cr,
860                             s->picture_structure != i+1,
861                             ref_picture, pix_op,
862                             s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
863
864                 // after put we make avg of the same block
865                 pix_op=s->dsp.avg_pixels_tab;
866
867                 //opposite parity is always in the same frame if this is second field
868                 if(!s->first_field){
869                     ref_picture = s->current_picture_ptr->f.data;
870                 }
871             }
872         }
873     break;
874     default: av_assert2(0);
875     }
876 }
877
878 void ff_MPV_motion(MpegEncContext *s,
879                    uint8_t *dest_y, uint8_t *dest_cb,
880                    uint8_t *dest_cr, int dir,
881                    uint8_t **ref_picture,
882                    op_pixels_func (*pix_op)[4],
883                    qpel_mc_func (*qpix_op)[16])
884 {
885 #if !CONFIG_SMALL
886     if(s->out_format == FMT_MPEG1)
887         MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
888                             ref_picture, pix_op, qpix_op, 1);
889     else
890 #endif
891         MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
892                             ref_picture, pix_op, qpix_op, 0);
893 }