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