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