]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideo_motion.c
Merge remote-tracking branch 'qatar/master'
[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 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 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 src_x, int src_y,
178                                   op_pixels_func *pix_op,
179                                   int motion_x, int motion_y)
180 {
181     int dxy;
182     int emu=0;
183
184     dxy = ((motion_y & 1) << 1) | (motion_x & 1);
185     src_x += motion_x >> 1;
186     src_y += motion_y >> 1;
187
188     /* WARNING: do no forget half pels */
189     src_x = av_clip(src_x, -16, s->width); //FIXME unneeded for emu?
190     if (src_x == s->width)
191         dxy &= ~1;
192     src_y = av_clip(src_y, -16, s->height);
193     if (src_y == s->height)
194         dxy &= ~2;
195     src += src_y * s->linesize + src_x;
196
197     if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
198         if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 8, 0)
199            || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&1) - 8, 0)){
200             s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, 9, 9,
201                              src_x, src_y, s->h_edge_pos, s->v_edge_pos);
202             src= s->edge_emu_buffer;
203             emu=1;
204         }
205     }
206     pix_op[dxy](dest, src, s->linesize, 8);
207     return emu;
208 }
209
210 static av_always_inline
211 void mpeg_motion_internal(MpegEncContext *s,
212                  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
213                  int field_based, int bottom_field, int field_select,
214                  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
215                  int motion_x, int motion_y, int h, int is_mpeg12, int mb_y)
216 {
217     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
218     int dxy, uvdxy, mx, my, src_x, src_y,
219         uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
220
221 #if 0
222 if(s->quarter_sample)
223 {
224     motion_x>>=1;
225     motion_y>>=1;
226 }
227 #endif
228
229     v_edge_pos = s->v_edge_pos >> field_based;
230     linesize   = s->current_picture.f.linesize[0] << field_based;
231     uvlinesize = s->current_picture.f.linesize[1] << field_based;
232
233     dxy = ((motion_y & 1) << 1) | (motion_x & 1);
234     src_x = s->mb_x* 16               + (motion_x >> 1);
235     src_y =(   mb_y<<(4-field_based)) + (motion_y >> 1);
236
237     if (!is_mpeg12 && s->out_format == FMT_H263) {
238         if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
239             mx = (motion_x>>1)|(motion_x&1);
240             my = motion_y >>1;
241             uvdxy = ((my & 1) << 1) | (mx & 1);
242             uvsrc_x = s->mb_x* 8               + (mx >> 1);
243             uvsrc_y =(   mb_y<<(3-field_based))+ (my >> 1);
244         }else{
245             uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
246             uvsrc_x = src_x>>1;
247             uvsrc_y = src_y>>1;
248         }
249     }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
250         mx = motion_x / 4;
251         my = motion_y / 4;
252         uvdxy = 0;
253         uvsrc_x = s->mb_x*8 + mx;
254         uvsrc_y =    mb_y*8 + my;
255     } else {
256         if(s->chroma_y_shift){
257             mx = motion_x / 2;
258             my = motion_y / 2;
259             uvdxy = ((my & 1) << 1) | (mx & 1);
260             uvsrc_x = s->mb_x* 8               + (mx >> 1);
261             uvsrc_y =(   mb_y<<(3-field_based))+ (my >> 1);
262         } else {
263             if(s->chroma_x_shift){
264             //Chroma422
265                 mx = motion_x / 2;
266                 uvdxy = ((motion_y & 1) << 1) | (mx & 1);
267                 uvsrc_x = s->mb_x* 8           + (mx >> 1);
268                 uvsrc_y = src_y;
269             } else {
270             //Chroma444
271                 uvdxy = dxy;
272                 uvsrc_x = src_x;
273                 uvsrc_y = src_y;
274             }
275         }
276     }
277
278     ptr_y  = ref_picture[0] + src_y * linesize + src_x;
279     ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
280     ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
281
282     if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
283        || (unsigned)src_y > FFMAX(   v_edge_pos - (motion_y&1) - h , 0)){
284             if(is_mpeg12 || s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
285                s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
286                 av_log(s->avctx,AV_LOG_DEBUG,
287                         "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
288                 return;
289             }
290             s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
291                                 17, 17+field_based,
292                                 src_x, src_y<<field_based,
293                                 s->h_edge_pos, s->v_edge_pos);
294             ptr_y = s->edge_emu_buffer;
295             if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
296                 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
297                 s->dsp.emulated_edge_mc(uvbuf ,
298                                     ptr_cb, s->uvlinesize,
299                                     9, 9+field_based,
300                                     uvsrc_x, uvsrc_y<<field_based,
301                                     s->h_edge_pos>>1, s->v_edge_pos>>1);
302                 s->dsp.emulated_edge_mc(uvbuf+16,
303                                     ptr_cr, 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                 ptr_cb= uvbuf;
308                 ptr_cr= uvbuf+16;
309             }
310     }
311
312     if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
313         dest_y += s->linesize;
314         dest_cb+= s->uvlinesize;
315         dest_cr+= s->uvlinesize;
316     }
317
318     if(field_select){
319         ptr_y += s->linesize;
320         ptr_cb+= s->uvlinesize;
321         ptr_cr+= s->uvlinesize;
322     }
323
324     pix_op[0][dxy](dest_y, ptr_y, linesize, h);
325
326     if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
327         pix_op[s->chroma_x_shift][uvdxy]
328                 (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
329         pix_op[s->chroma_x_shift][uvdxy]
330                 (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
331     }
332     if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
333          s->out_format == FMT_H261){
334         ff_h261_loop_filter(s);
335     }
336 }
337 /* apply one mpeg motion vector to the three components */
338 static void mpeg_motion(MpegEncContext *s,
339                         uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
340                         int field_select, uint8_t **ref_picture,
341                         op_pixels_func (*pix_op)[4],
342                         int motion_x, int motion_y, int h, int mb_y)
343 {
344 #if !CONFIG_SMALL
345     if(s->out_format == FMT_MPEG1)
346         mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
347                     field_select, ref_picture, pix_op,
348                     motion_x, motion_y, h, 1, mb_y);
349     else
350 #endif
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, 0, mb_y);
354 }
355
356 static void mpeg_motion_field(MpegEncContext *s, uint8_t *dest_y,
357                               uint8_t *dest_cb, uint8_t *dest_cr,
358                               int bottom_field, int field_select,
359                               uint8_t **ref_picture,
360                               op_pixels_func (*pix_op)[4],
361                               int motion_x, int motion_y, int h, int mb_y)
362 {
363 #if !CONFIG_SMALL
364     if(s->out_format == FMT_MPEG1)
365         mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
366                     bottom_field, field_select, ref_picture, pix_op,
367                     motion_x, motion_y, h, 1, mb_y);
368     else
369 #endif
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, 0, mb_y);
373 }
374
375 //FIXME move to dsputil, avg variant, 16x16 version
376 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
377     int x;
378     uint8_t * const top   = src[1];
379     uint8_t * const left  = src[2];
380     uint8_t * const mid   = src[0];
381     uint8_t * const right = src[3];
382     uint8_t * const bottom= src[4];
383 #define OBMC_FILTER(x, t, l, m, r, b)\
384     dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
385 #define OBMC_FILTER4(x, t, l, m, r, b)\
386     OBMC_FILTER(x         , t, l, m, r, b);\
387     OBMC_FILTER(x+1       , t, l, m, r, b);\
388     OBMC_FILTER(x  +stride, t, l, m, r, b);\
389     OBMC_FILTER(x+1+stride, t, l, m, r, b);
390
391     x=0;
392     OBMC_FILTER (x  , 2, 2, 4, 0, 0);
393     OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
394     OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
395     OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
396     OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
397     OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
398     x+= stride;
399     OBMC_FILTER (x  , 1, 2, 5, 0, 0);
400     OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
401     OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
402     OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
403     x+= stride;
404     OBMC_FILTER4(x  , 1, 2, 5, 0, 0);
405     OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
406     OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
407     OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
408     x+= 2*stride;
409     OBMC_FILTER4(x  , 0, 2, 5, 0, 1);
410     OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
411     OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
412     OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
413     x+= 2*stride;
414     OBMC_FILTER (x  , 0, 2, 5, 0, 1);
415     OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
416     OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
417     OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
418     OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
419     OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
420     x+= stride;
421     OBMC_FILTER (x  , 0, 2, 4, 0, 2);
422     OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
423     OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
424     OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
425 }
426
427 /* obmc for 1 8x8 luma block */
428 static inline void obmc_motion(MpegEncContext *s,
429                                uint8_t *dest, uint8_t *src,
430                                int src_x, int src_y,
431                                op_pixels_func *pix_op,
432                                int16_t mv[5][2]/* mid top left right bottom*/)
433 #define MID    0
434 {
435     int i;
436     uint8_t *ptr[5];
437
438     av_assert2(s->quarter_sample==0);
439
440     for(i=0; i<5; i++){
441         if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
442             ptr[i]= ptr[MID];
443         }else{
444             ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
445             hpel_motion(s, ptr[i], src,
446                         src_x, src_y,
447                         pix_op,
448                         mv[i][0], mv[i][1]);
449         }
450     }
451
452     put_obmc(dest, ptr, s->linesize);
453 }
454
455 static inline void qpel_motion(MpegEncContext *s,
456                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
457                                int field_based, int bottom_field, int field_select,
458                                uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
459                                qpel_mc_func (*qpix_op)[16],
460                                int motion_x, int motion_y, int h)
461 {
462     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
463     int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
464
465     dxy = ((motion_y & 3) << 2) | (motion_x & 3);
466     src_x = s->mb_x *  16                 + (motion_x >> 2);
467     src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
468
469     v_edge_pos = s->v_edge_pos >> field_based;
470     linesize = s->linesize << field_based;
471     uvlinesize = s->uvlinesize << field_based;
472
473     if(field_based){
474         mx= motion_x/2;
475         my= motion_y>>1;
476     }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
477         static const int rtab[8]= {0,0,1,1,0,0,0,1};
478         mx= (motion_x>>1) + rtab[motion_x&7];
479         my= (motion_y>>1) + rtab[motion_y&7];
480     }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
481         mx= (motion_x>>1)|(motion_x&1);
482         my= (motion_y>>1)|(motion_y&1);
483     }else{
484         mx= motion_x/2;
485         my= motion_y/2;
486     }
487     mx= (mx>>1)|(mx&1);
488     my= (my>>1)|(my&1);
489
490     uvdxy= (mx&1) | ((my&1)<<1);
491     mx>>=1;
492     my>>=1;
493
494     uvsrc_x = s->mb_x *  8                 + mx;
495     uvsrc_y = s->mb_y * (8 >> field_based) + my;
496
497     ptr_y  = ref_picture[0] +   src_y *   linesize +   src_x;
498     ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
499     ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
500
501     if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
502        || (unsigned)src_y > FFMAX(   v_edge_pos - (motion_y&3) - h , 0)){
503         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
504                             17, 17+field_based, src_x, src_y<<field_based,
505                             s->h_edge_pos, s->v_edge_pos);
506         ptr_y= s->edge_emu_buffer;
507         if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
508             uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
509             s->dsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
510                                 9, 9 + field_based,
511                                 uvsrc_x, uvsrc_y<<field_based,
512                                 s->h_edge_pos>>1, s->v_edge_pos>>1);
513             s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
514                                 9, 9 + field_based,
515                                 uvsrc_x, uvsrc_y<<field_based,
516                                 s->h_edge_pos>>1, s->v_edge_pos>>1);
517             ptr_cb= uvbuf;
518             ptr_cr= uvbuf + 16;
519         }
520     }
521
522     if(!field_based)
523         qpix_op[0][dxy](dest_y, ptr_y, linesize);
524     else{
525         if(bottom_field){
526             dest_y += s->linesize;
527             dest_cb+= s->uvlinesize;
528             dest_cr+= s->uvlinesize;
529         }
530
531         if(field_select){
532             ptr_y  += s->linesize;
533             ptr_cb += s->uvlinesize;
534             ptr_cr += s->uvlinesize;
535         }
536         //damn interlaced mode
537         //FIXME boundary mirroring is not exactly correct here
538         qpix_op[1][dxy](dest_y  , ptr_y  , linesize);
539         qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
540     }
541     if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
542         pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
543         pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
544     }
545 }
546
547 /**
548  * h263 chroma 4mv motion compensation.
549  */
550 static void chroma_4mv_motion(MpegEncContext *s,
551                               uint8_t *dest_cb, uint8_t *dest_cr,
552                               uint8_t **ref_picture,
553                               op_pixels_func *pix_op,
554                               int mx, int my)
555 {
556     int dxy, emu=0, src_x, src_y, offset;
557     uint8_t *ptr;
558
559     /* In case of 8X8, we construct a single chroma motion vector
560        with a special rounding */
561     mx= ff_h263_round_chroma(mx);
562     my= ff_h263_round_chroma(my);
563
564     dxy = ((my & 1) << 1) | (mx & 1);
565     mx >>= 1;
566     my >>= 1;
567
568     src_x = s->mb_x * 8 + mx;
569     src_y = s->mb_y * 8 + my;
570     src_x = av_clip(src_x, -8, (s->width >> 1));
571     if (src_x == (s->width >> 1))
572         dxy &= ~1;
573     src_y = av_clip(src_y, -8, (s->height >> 1));
574     if (src_y == (s->height >> 1))
575         dxy &= ~2;
576
577     offset = src_y * s->uvlinesize + src_x;
578     ptr = ref_picture[1] + offset;
579     if(s->flags&CODEC_FLAG_EMU_EDGE){
580         if(   (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
581            || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
582             s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
583                                 9, 9, src_x, src_y,
584                                 s->h_edge_pos>>1, s->v_edge_pos>>1);
585             ptr= s->edge_emu_buffer;
586             emu=1;
587         }
588     }
589     pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
590
591     ptr = ref_picture[2] + offset;
592     if(emu){
593         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
594                             9, 9, src_x, src_y,
595                             s->h_edge_pos>>1, s->v_edge_pos>>1);
596         ptr= s->edge_emu_buffer;
597     }
598     pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
599 }
600
601 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
602     /* fetch pixels for estimated mv 4 macroblocks ahead
603      * optimized for 64byte cache lines */
604     const int shift = s->quarter_sample ? 2 : 1;
605     const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
606     const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
607     int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
608     s->dsp.prefetch(pix[0]+off, s->linesize, 4);
609     off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
610     s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
611 }
612
613 /**
614  * motion compensation of a single macroblock
615  * @param s context
616  * @param dest_y luma destination pointer
617  * @param dest_cb chroma cb/u destination pointer
618  * @param dest_cr chroma cr/v destination pointer
619  * @param dir direction (0->forward, 1->backward)
620  * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
621  * @param pix_op halfpel motion compensation function (average or put normally)
622  * @param qpix_op qpel motion compensation function (average or put normally)
623  * the motion vectors are taken from s->mv and the MV type from s->mv_type
624  */
625 static av_always_inline void MPV_motion_internal(MpegEncContext *s,
626                               uint8_t *dest_y, uint8_t *dest_cb,
627                               uint8_t *dest_cr, int dir,
628                               uint8_t **ref_picture,
629                               op_pixels_func (*pix_op)[4],
630                               qpel_mc_func (*qpix_op)[16], int is_mpeg12)
631 {
632     int dxy, mx, my, src_x, src_y, motion_x, motion_y;
633     int mb_x, mb_y, i;
634     uint8_t *ptr, *dest;
635
636     mb_x = s->mb_x;
637     mb_y = s->mb_y;
638
639     prefetch_motion(s, ref_picture, dir);
640
641     if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
642         int16_t mv_cache[4][4][2];
643         const int xy= s->mb_x + s->mb_y*s->mb_stride;
644         const int mot_stride= s->b8_stride;
645         const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
646
647         av_assert2(!s->mb_skipped);
648
649         memcpy(mv_cache[1][1], s->current_picture.f.motion_val[0][mot_xy             ], sizeof(int16_t) * 4);
650         memcpy(mv_cache[2][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
651         memcpy(mv_cache[3][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
652
653         if (mb_y == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - s->mb_stride])) {
654             memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
655         }else{
656             memcpy(mv_cache[0][1], s->current_picture.f.motion_val[0][mot_xy - mot_stride], sizeof(int16_t) * 4);
657         }
658
659         if (mb_x == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - 1])) {
660             AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
661             AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
662         }else{
663             AV_COPY32(mv_cache[1][0], s->current_picture.f.motion_val[0][mot_xy - 1]);
664             AV_COPY32(mv_cache[2][0], s->current_picture.f.motion_val[0][mot_xy - 1 + mot_stride]);
665         }
666
667         if (mb_x + 1 >= s->mb_width || IS_INTRA(s->current_picture.f.mb_type[xy + 1])) {
668             AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
669             AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
670         }else{
671             AV_COPY32(mv_cache[1][3], s->current_picture.f.motion_val[0][mot_xy + 2]);
672             AV_COPY32(mv_cache[2][3], s->current_picture.f.motion_val[0][mot_xy + 2 + mot_stride]);
673         }
674
675         mx = 0;
676         my = 0;
677         for(i=0;i<4;i++) {
678             const int x= (i&1)+1;
679             const int y= (i>>1)+1;
680             int16_t mv[5][2]= {
681                 {mv_cache[y][x  ][0], mv_cache[y][x  ][1]},
682                 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
683                 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
684                 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
685                 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
686             //FIXME cleanup
687             obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
688                         ref_picture[0],
689                         mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
690                         pix_op[1],
691                         mv);
692
693             mx += mv[0][0];
694             my += mv[0][1];
695         }
696         if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
697             chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
698
699         return;
700     }
701
702     switch(s->mv_type) {
703     case MV_TYPE_16X16:
704         if(s->mcsel){
705             if(s->real_sprite_warping_points==1){
706                 gmc1_motion(s, dest_y, dest_cb, dest_cr,
707                             ref_picture);
708             }else{
709                 gmc_motion(s, dest_y, dest_cb, dest_cr,
710                             ref_picture);
711             }
712         }else if(!is_mpeg12 && s->quarter_sample){
713             qpel_motion(s, dest_y, dest_cb, dest_cr,
714                         0, 0, 0,
715                         ref_picture, pix_op, qpix_op,
716                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
717         } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
718                    s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
719             ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
720                         ref_picture, pix_op,
721                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
722         }else
723         {
724             mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
725                         ref_picture, pix_op,
726                         s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
727         }
728         break;
729     case MV_TYPE_8X8:
730     if (!is_mpeg12) {
731         mx = 0;
732         my = 0;
733         if(s->quarter_sample){
734             for(i=0;i<4;i++) {
735                 motion_x = s->mv[dir][i][0];
736                 motion_y = s->mv[dir][i][1];
737
738                 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
739                 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
740                 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
741
742                 /* WARNING: do no forget half pels */
743                 src_x = av_clip(src_x, -16, s->width);
744                 if (src_x == s->width)
745                     dxy &= ~3;
746                 src_y = av_clip(src_y, -16, s->height);
747                 if (src_y == s->height)
748                     dxy &= ~12;
749
750                 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
751                 if(s->flags&CODEC_FLAG_EMU_EDGE){
752                     if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
753                        || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
754                         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
755                                             s->linesize, 9, 9,
756                                             src_x, src_y,
757                                             s->h_edge_pos, s->v_edge_pos);
758                         ptr= s->edge_emu_buffer;
759                     }
760                 }
761                 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
762                 qpix_op[1][dxy](dest, ptr, s->linesize);
763
764                 mx += s->mv[dir][i][0]/2;
765                 my += s->mv[dir][i][1]/2;
766             }
767         }else{
768             for(i=0;i<4;i++) {
769                 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
770                             ref_picture[0],
771                             mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
772                             pix_op[1],
773                             s->mv[dir][i][0], s->mv[dir][i][1]);
774
775                 mx += s->mv[dir][i][0];
776                 my += s->mv[dir][i][1];
777             }
778         }
779
780         if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
781             chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
782     }
783         break;
784     case MV_TYPE_FIELD:
785         if (s->picture_structure == PICT_FRAME) {
786             if(!is_mpeg12 && s->quarter_sample){
787                 for(i=0; i<2; i++){
788                     qpel_motion(s, dest_y, dest_cb, dest_cr,
789                                 1, i, s->field_select[dir][i],
790                                 ref_picture, pix_op, qpix_op,
791                                 s->mv[dir][i][0], s->mv[dir][i][1], 8);
792                 }
793             }else{
794                 /* top field */
795                 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
796                                   0, s->field_select[dir][0],
797                                   ref_picture, pix_op,
798                                   s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
799                 /* bottom field */
800                 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
801                                   1, s->field_select[dir][1],
802                                   ref_picture, pix_op,
803                                   s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
804             }
805         } else {
806             if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
807                 ref_picture = s->current_picture_ptr->f.data;
808             }
809
810             mpeg_motion(s, dest_y, dest_cb, dest_cr,
811                         s->field_select[dir][0],
812                         ref_picture, pix_op,
813                         s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
814         }
815         break;
816     case MV_TYPE_16X8:
817         for(i=0; i<2; i++){
818             uint8_t ** ref2picture;
819
820             if(s->picture_structure == s->field_select[dir][i] + 1
821                || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
822                 ref2picture= ref_picture;
823             }else{
824                 ref2picture = s->current_picture_ptr->f.data;
825             }
826
827             mpeg_motion(s, dest_y, dest_cb, dest_cr,
828                         s->field_select[dir][i],
829                         ref2picture, pix_op,
830                         s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
831
832             dest_y += 16*s->linesize;
833             dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
834             dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
835         }
836         break;
837     case MV_TYPE_DMV:
838         if(s->picture_structure == PICT_FRAME){
839             for(i=0; i<2; i++){
840                 int j;
841                 for(j=0; j<2; j++){
842                     mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
843                                       j, j^i, ref_picture, pix_op,
844                                       s->mv[dir][2*i + j][0],
845                                       s->mv[dir][2*i + j][1], 8, mb_y);
846                 }
847                 pix_op = s->dsp.avg_pixels_tab;
848             }
849         }else{
850             for(i=0; i<2; i++){
851                 mpeg_motion(s, dest_y, dest_cb, dest_cr,
852                             s->picture_structure != i+1,
853                             ref_picture, pix_op,
854                             s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
855
856                 // after put we make avg of the same block
857                 pix_op=s->dsp.avg_pixels_tab;
858
859                 //opposite parity is always in the same frame if this is second field
860                 if(!s->first_field){
861                     ref_picture = s->current_picture_ptr->f.data;
862                 }
863             }
864         }
865     break;
866     default: av_assert2(0);
867     }
868 }
869
870 void ff_MPV_motion(MpegEncContext *s,
871                    uint8_t *dest_y, uint8_t *dest_cb,
872                    uint8_t *dest_cr, int dir,
873                    uint8_t **ref_picture,
874                    op_pixels_func (*pix_op)[4],
875                    qpel_mc_func (*qpix_op)[16])
876 {
877 #if !CONFIG_SMALL
878     if(s->out_format == FMT_MPEG1)
879         MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
880                             ref_picture, pix_op, qpix_op, 1);
881     else
882 #endif
883         MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
884                             ref_picture, pix_op, qpix_op, 0);
885 }