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