libavcodec/mpegvideo_common.h
Go to the documentation of this file.
00001 /*
00002  * The simplest mpeg encoder (well, it was the simplest!)
00003  * Copyright (c) 2000,2001 Fabrice Bellard
00004  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
00005  *
00006  * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
00007  *
00008  * This file is part of Libav.
00009  *
00010  * Libav is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * Libav is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with Libav; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00030 #ifndef AVCODEC_MPEGVIDEO_COMMON_H
00031 #define AVCODEC_MPEGVIDEO_COMMON_H
00032 
00033 #include <string.h>
00034 #include "avcodec.h"
00035 #include "dsputil.h"
00036 #include "mpegvideo.h"
00037 #include "mjpegenc.h"
00038 #include "msmpeg4.h"
00039 #include "faandct.h"
00040 #include <limits.h>
00041 
00042 int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
00043 
00048 int alloc_picture(MpegEncContext *s, Picture *pic, int shared);
00049 
00054 void MPV_common_defaults(MpegEncContext *s);
00055 
00056 static inline void gmc1_motion(MpegEncContext *s,
00057                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00058                                uint8_t **ref_picture)
00059 {
00060     uint8_t *ptr;
00061     int offset, src_x, src_y, linesize, uvlinesize;
00062     int motion_x, motion_y;
00063     int emu=0;
00064 
00065     motion_x= s->sprite_offset[0][0];
00066     motion_y= s->sprite_offset[0][1];
00067     src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
00068     src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
00069     motion_x<<=(3-s->sprite_warping_accuracy);
00070     motion_y<<=(3-s->sprite_warping_accuracy);
00071     src_x = av_clip(src_x, -16, s->width);
00072     if (src_x == s->width)
00073         motion_x =0;
00074     src_y = av_clip(src_y, -16, s->height);
00075     if (src_y == s->height)
00076         motion_y =0;
00077 
00078     linesize = s->linesize;
00079     uvlinesize = s->uvlinesize;
00080 
00081     ptr = ref_picture[0] + (src_y * linesize) + src_x;
00082 
00083     if(s->flags&CODEC_FLAG_EMU_EDGE){
00084         if(   (unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0)
00085            || (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)){
00086             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);
00087             ptr= s->edge_emu_buffer;
00088         }
00089     }
00090 
00091     if((motion_x|motion_y)&7){
00092         s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
00093         s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
00094     }else{
00095         int dxy;
00096 
00097         dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
00098         if (s->no_rounding){
00099             s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
00100         }else{
00101             s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
00102         }
00103     }
00104 
00105     if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
00106 
00107     motion_x= s->sprite_offset[1][0];
00108     motion_y= s->sprite_offset[1][1];
00109     src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
00110     src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
00111     motion_x<<=(3-s->sprite_warping_accuracy);
00112     motion_y<<=(3-s->sprite_warping_accuracy);
00113     src_x = av_clip(src_x, -8, s->width>>1);
00114     if (src_x == s->width>>1)
00115         motion_x =0;
00116     src_y = av_clip(src_y, -8, s->height>>1);
00117     if (src_y == s->height>>1)
00118         motion_y =0;
00119 
00120     offset = (src_y * uvlinesize) + src_x;
00121     ptr = ref_picture[1] + offset;
00122     if(s->flags&CODEC_FLAG_EMU_EDGE){
00123         if(   (unsigned)src_x >= FFMAX((s->h_edge_pos>>1) - 9, 0)
00124            || (unsigned)src_y >= FFMAX((s->v_edge_pos>>1) - 9, 0)){
00125             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);
00126             ptr= s->edge_emu_buffer;
00127             emu=1;
00128         }
00129     }
00130     s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
00131 
00132     ptr = ref_picture[2] + offset;
00133     if(emu){
00134         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);
00135         ptr= s->edge_emu_buffer;
00136     }
00137     s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
00138 
00139     return;
00140 }
00141 
00142 static inline void gmc_motion(MpegEncContext *s,
00143                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00144                                uint8_t **ref_picture)
00145 {
00146     uint8_t *ptr;
00147     int linesize, uvlinesize;
00148     const int a= s->sprite_warping_accuracy;
00149     int ox, oy;
00150 
00151     linesize = s->linesize;
00152     uvlinesize = s->uvlinesize;
00153 
00154     ptr = ref_picture[0];
00155 
00156     ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
00157     oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
00158 
00159     s->dsp.gmc(dest_y, ptr, linesize, 16,
00160            ox,
00161            oy,
00162            s->sprite_delta[0][0], s->sprite_delta[0][1],
00163            s->sprite_delta[1][0], s->sprite_delta[1][1],
00164            a+1, (1<<(2*a+1)) - s->no_rounding,
00165            s->h_edge_pos, s->v_edge_pos);
00166     s->dsp.gmc(dest_y+8, ptr, linesize, 16,
00167            ox + s->sprite_delta[0][0]*8,
00168            oy + s->sprite_delta[1][0]*8,
00169            s->sprite_delta[0][0], s->sprite_delta[0][1],
00170            s->sprite_delta[1][0], s->sprite_delta[1][1],
00171            a+1, (1<<(2*a+1)) - s->no_rounding,
00172            s->h_edge_pos, s->v_edge_pos);
00173 
00174     if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
00175 
00176     ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
00177     oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
00178 
00179     ptr = ref_picture[1];
00180     s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
00181            ox,
00182            oy,
00183            s->sprite_delta[0][0], s->sprite_delta[0][1],
00184            s->sprite_delta[1][0], s->sprite_delta[1][1],
00185            a+1, (1<<(2*a+1)) - s->no_rounding,
00186            s->h_edge_pos>>1, s->v_edge_pos>>1);
00187 
00188     ptr = ref_picture[2];
00189     s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
00190            ox,
00191            oy,
00192            s->sprite_delta[0][0], s->sprite_delta[0][1],
00193            s->sprite_delta[1][0], s->sprite_delta[1][1],
00194            a+1, (1<<(2*a+1)) - s->no_rounding,
00195            s->h_edge_pos>>1, s->v_edge_pos>>1);
00196 }
00197 
00198 static inline int hpel_motion(MpegEncContext *s,
00199                                   uint8_t *dest, uint8_t *src,
00200                                   int field_based, int field_select,
00201                                   int src_x, int src_y,
00202                                   int width, int height, int stride,
00203                                   int h_edge_pos, int v_edge_pos,
00204                                   int w, int h, op_pixels_func *pix_op,
00205                                   int motion_x, int motion_y)
00206 {
00207     int dxy;
00208     int emu=0;
00209 
00210     dxy = ((motion_y & 1) << 1) | (motion_x & 1);
00211     src_x += motion_x >> 1;
00212     src_y += motion_y >> 1;
00213 
00214     /* WARNING: do no forget half pels */
00215     src_x = av_clip(src_x, -16, width); //FIXME unneeded for emu?
00216     if (src_x == width)
00217         dxy &= ~1;
00218     src_y = av_clip(src_y, -16, height);
00219     if (src_y == height)
00220         dxy &= ~2;
00221     src += src_y * stride + src_x;
00222 
00223     if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
00224         if(   (unsigned)src_x > FFMAX(h_edge_pos - (motion_x&1) - w, 0)
00225            || (unsigned)src_y > FFMAX(v_edge_pos - (motion_y&1) - h, 0)){
00226             s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
00227                              src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
00228             src= s->edge_emu_buffer;
00229             emu=1;
00230         }
00231     }
00232     if(field_select)
00233         src += s->linesize;
00234     pix_op[dxy](dest, src, stride, h);
00235     return emu;
00236 }
00237 
00238 static av_always_inline
00239 void mpeg_motion_internal(MpegEncContext *s,
00240                  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00241                  int field_based, int bottom_field, int field_select,
00242                  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
00243                  int motion_x, int motion_y, int h, int is_mpeg12, int mb_y)
00244 {
00245     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
00246     int dxy, uvdxy, mx, my, src_x, src_y,
00247         uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
00248 
00249 #if 0
00250 if(s->quarter_sample)
00251 {
00252     motion_x>>=1;
00253     motion_y>>=1;
00254 }
00255 #endif
00256 
00257     v_edge_pos = s->v_edge_pos >> field_based;
00258     linesize   = s->current_picture.f.linesize[0] << field_based;
00259     uvlinesize = s->current_picture.f.linesize[1] << field_based;
00260 
00261     dxy = ((motion_y & 1) << 1) | (motion_x & 1);
00262     src_x = s->mb_x* 16               + (motion_x >> 1);
00263     src_y =(   mb_y<<(4-field_based)) + (motion_y >> 1);
00264 
00265     if (!is_mpeg12 && s->out_format == FMT_H263) {
00266         if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
00267             mx = (motion_x>>1)|(motion_x&1);
00268             my = motion_y >>1;
00269             uvdxy = ((my & 1) << 1) | (mx & 1);
00270             uvsrc_x = s->mb_x* 8               + (mx >> 1);
00271             uvsrc_y =(   mb_y<<(3-field_based))+ (my >> 1);
00272         }else{
00273             uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
00274             uvsrc_x = src_x>>1;
00275             uvsrc_y = src_y>>1;
00276         }
00277     }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
00278         mx = motion_x / 4;
00279         my = motion_y / 4;
00280         uvdxy = 0;
00281         uvsrc_x = s->mb_x*8 + mx;
00282         uvsrc_y =    mb_y*8 + my;
00283     } else {
00284         if(s->chroma_y_shift){
00285             mx = motion_x / 2;
00286             my = motion_y / 2;
00287             uvdxy = ((my & 1) << 1) | (mx & 1);
00288             uvsrc_x = s->mb_x* 8               + (mx >> 1);
00289             uvsrc_y =(   mb_y<<(3-field_based))+ (my >> 1);
00290         } else {
00291             if(s->chroma_x_shift){
00292             //Chroma422
00293                 mx = motion_x / 2;
00294                 uvdxy = ((motion_y & 1) << 1) | (mx & 1);
00295                 uvsrc_x = s->mb_x* 8           + (mx >> 1);
00296                 uvsrc_y = src_y;
00297             } else {
00298             //Chroma444
00299                 uvdxy = dxy;
00300                 uvsrc_x = src_x;
00301                 uvsrc_y = src_y;
00302             }
00303         }
00304     }
00305 
00306     ptr_y  = ref_picture[0] + src_y * linesize + src_x;
00307     ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
00308     ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
00309 
00310     if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
00311        || (unsigned)src_y > FFMAX(   v_edge_pos - (motion_y&1) - h , 0)){
00312             if(is_mpeg12 || s->codec_id == CODEC_ID_MPEG2VIDEO ||
00313                s->codec_id == CODEC_ID_MPEG1VIDEO){
00314                 av_log(s->avctx,AV_LOG_DEBUG,
00315                         "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
00316                 return;
00317             }
00318             s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
00319                                 17, 17+field_based,
00320                                 src_x, src_y<<field_based,
00321                                 s->h_edge_pos, s->v_edge_pos);
00322             ptr_y = s->edge_emu_buffer;
00323             if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00324                 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
00325                 s->dsp.emulated_edge_mc(uvbuf ,
00326                                     ptr_cb, s->uvlinesize,
00327                                     9, 9+field_based,
00328                                     uvsrc_x, uvsrc_y<<field_based,
00329                                     s->h_edge_pos>>1, s->v_edge_pos>>1);
00330                 s->dsp.emulated_edge_mc(uvbuf+16,
00331                                     ptr_cr, s->uvlinesize,
00332                                     9, 9+field_based,
00333                                     uvsrc_x, uvsrc_y<<field_based,
00334                                     s->h_edge_pos>>1, s->v_edge_pos>>1);
00335                 ptr_cb= uvbuf;
00336                 ptr_cr= uvbuf+16;
00337             }
00338     }
00339 
00340     if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
00341         dest_y += s->linesize;
00342         dest_cb+= s->uvlinesize;
00343         dest_cr+= s->uvlinesize;
00344     }
00345 
00346     if(field_select){
00347         ptr_y += s->linesize;
00348         ptr_cb+= s->uvlinesize;
00349         ptr_cr+= s->uvlinesize;
00350     }
00351 
00352     pix_op[0][dxy](dest_y, ptr_y, linesize, h);
00353 
00354     if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00355         pix_op[s->chroma_x_shift][uvdxy]
00356                 (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
00357         pix_op[s->chroma_x_shift][uvdxy]
00358                 (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
00359     }
00360     if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
00361          s->out_format == FMT_H261){
00362         ff_h261_loop_filter(s);
00363     }
00364 }
00365 /* apply one mpeg motion vector to the three components */
00366 static av_always_inline
00367 void mpeg_motion(MpegEncContext *s,
00368                  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00369                  int field_based, int bottom_field, int field_select,
00370                  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
00371                  int motion_x, int motion_y, int h, int mb_y)
00372 {
00373 #if !CONFIG_SMALL
00374     if(s->out_format == FMT_MPEG1)
00375         mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
00376                     bottom_field, field_select, ref_picture, pix_op,
00377                     motion_x, motion_y, h, 1, mb_y);
00378     else
00379 #endif
00380         mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
00381                     bottom_field, field_select, ref_picture, pix_op,
00382                     motion_x, motion_y, h, 0, mb_y);
00383 }
00384 
00385 //FIXME move to dsputil, avg variant, 16x16 version
00386 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
00387     int x;
00388     uint8_t * const top   = src[1];
00389     uint8_t * const left  = src[2];
00390     uint8_t * const mid   = src[0];
00391     uint8_t * const right = src[3];
00392     uint8_t * const bottom= src[4];
00393 #define OBMC_FILTER(x, t, l, m, r, b)\
00394     dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
00395 #define OBMC_FILTER4(x, t, l, m, r, b)\
00396     OBMC_FILTER(x         , t, l, m, r, b);\
00397     OBMC_FILTER(x+1       , t, l, m, r, b);\
00398     OBMC_FILTER(x  +stride, t, l, m, r, b);\
00399     OBMC_FILTER(x+1+stride, t, l, m, r, b);
00400 
00401     x=0;
00402     OBMC_FILTER (x  , 2, 2, 4, 0, 0);
00403     OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
00404     OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
00405     OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
00406     OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
00407     OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
00408     x+= stride;
00409     OBMC_FILTER (x  , 1, 2, 5, 0, 0);
00410     OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
00411     OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
00412     OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
00413     x+= stride;
00414     OBMC_FILTER4(x  , 1, 2, 5, 0, 0);
00415     OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
00416     OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
00417     OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
00418     x+= 2*stride;
00419     OBMC_FILTER4(x  , 0, 2, 5, 0, 1);
00420     OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
00421     OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
00422     OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
00423     x+= 2*stride;
00424     OBMC_FILTER (x  , 0, 2, 5, 0, 1);
00425     OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
00426     OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
00427     OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
00428     OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
00429     OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
00430     x+= stride;
00431     OBMC_FILTER (x  , 0, 2, 4, 0, 2);
00432     OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
00433     OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
00434     OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
00435 }
00436 
00437 /* obmc for 1 8x8 luma block */
00438 static inline void obmc_motion(MpegEncContext *s,
00439                                uint8_t *dest, uint8_t *src,
00440                                int src_x, int src_y,
00441                                op_pixels_func *pix_op,
00442                                int16_t mv[5][2]/* mid top left right bottom*/)
00443 #define MID    0
00444 {
00445     int i;
00446     uint8_t *ptr[5];
00447 
00448     assert(s->quarter_sample==0);
00449 
00450     for(i=0; i<5; i++){
00451         if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
00452             ptr[i]= ptr[MID];
00453         }else{
00454             ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
00455             hpel_motion(s, ptr[i], src, 0, 0,
00456                         src_x, src_y,
00457                         s->width, s->height, s->linesize,
00458                         s->h_edge_pos, s->v_edge_pos,
00459                         8, 8, pix_op,
00460                         mv[i][0], mv[i][1]);
00461         }
00462     }
00463 
00464     put_obmc(dest, ptr, s->linesize);
00465 }
00466 
00467 static inline void qpel_motion(MpegEncContext *s,
00468                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00469                                int field_based, int bottom_field, int field_select,
00470                                uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
00471                                qpel_mc_func (*qpix_op)[16],
00472                                int motion_x, int motion_y, int h)
00473 {
00474     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
00475     int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
00476 
00477     dxy = ((motion_y & 3) << 2) | (motion_x & 3);
00478     src_x = s->mb_x *  16                 + (motion_x >> 2);
00479     src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
00480 
00481     v_edge_pos = s->v_edge_pos >> field_based;
00482     linesize = s->linesize << field_based;
00483     uvlinesize = s->uvlinesize << field_based;
00484 
00485     if(field_based){
00486         mx= motion_x/2;
00487         my= motion_y>>1;
00488     }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
00489         static const int rtab[8]= {0,0,1,1,0,0,0,1};
00490         mx= (motion_x>>1) + rtab[motion_x&7];
00491         my= (motion_y>>1) + rtab[motion_y&7];
00492     }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
00493         mx= (motion_x>>1)|(motion_x&1);
00494         my= (motion_y>>1)|(motion_y&1);
00495     }else{
00496         mx= motion_x/2;
00497         my= motion_y/2;
00498     }
00499     mx= (mx>>1)|(mx&1);
00500     my= (my>>1)|(my&1);
00501 
00502     uvdxy= (mx&1) | ((my&1)<<1);
00503     mx>>=1;
00504     my>>=1;
00505 
00506     uvsrc_x = s->mb_x *  8                 + mx;
00507     uvsrc_y = s->mb_y * (8 >> field_based) + my;
00508 
00509     ptr_y  = ref_picture[0] +   src_y *   linesize +   src_x;
00510     ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
00511     ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
00512 
00513     if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
00514        || (unsigned)src_y > FFMAX(   v_edge_pos - (motion_y&3) - h , 0)){
00515         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
00516                             17, 17+field_based, src_x, src_y<<field_based,
00517                             s->h_edge_pos, s->v_edge_pos);
00518         ptr_y= s->edge_emu_buffer;
00519         if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00520             uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
00521             s->dsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
00522                                 9, 9 + field_based,
00523                                 uvsrc_x, uvsrc_y<<field_based,
00524                                 s->h_edge_pos>>1, s->v_edge_pos>>1);
00525             s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
00526                                 9, 9 + field_based,
00527                                 uvsrc_x, uvsrc_y<<field_based,
00528                                 s->h_edge_pos>>1, s->v_edge_pos>>1);
00529             ptr_cb= uvbuf;
00530             ptr_cr= uvbuf + 16;
00531         }
00532     }
00533 
00534     if(!field_based)
00535         qpix_op[0][dxy](dest_y, ptr_y, linesize);
00536     else{
00537         if(bottom_field){
00538             dest_y += s->linesize;
00539             dest_cb+= s->uvlinesize;
00540             dest_cr+= s->uvlinesize;
00541         }
00542 
00543         if(field_select){
00544             ptr_y  += s->linesize;
00545             ptr_cb += s->uvlinesize;
00546             ptr_cr += s->uvlinesize;
00547         }
00548         //damn interlaced mode
00549         //FIXME boundary mirroring is not exactly correct here
00550         qpix_op[1][dxy](dest_y  , ptr_y  , linesize);
00551         qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
00552     }
00553     if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00554         pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
00555         pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
00556     }
00557 }
00558 
00562 static inline void chroma_4mv_motion(MpegEncContext *s,
00563                                      uint8_t *dest_cb, uint8_t *dest_cr,
00564                                      uint8_t **ref_picture,
00565                                      op_pixels_func *pix_op,
00566                                      int mx, int my){
00567     int dxy, emu=0, src_x, src_y, offset;
00568     uint8_t *ptr;
00569 
00570     /* In case of 8X8, we construct a single chroma motion vector
00571        with a special rounding */
00572     mx= ff_h263_round_chroma(mx);
00573     my= ff_h263_round_chroma(my);
00574 
00575     dxy = ((my & 1) << 1) | (mx & 1);
00576     mx >>= 1;
00577     my >>= 1;
00578 
00579     src_x = s->mb_x * 8 + mx;
00580     src_y = s->mb_y * 8 + my;
00581     src_x = av_clip(src_x, -8, (s->width >> 1));
00582     if (src_x == (s->width >> 1))
00583         dxy &= ~1;
00584     src_y = av_clip(src_y, -8, (s->height >> 1));
00585     if (src_y == (s->height >> 1))
00586         dxy &= ~2;
00587 
00588     offset = src_y * s->uvlinesize + src_x;
00589     ptr = ref_picture[1] + offset;
00590     if(s->flags&CODEC_FLAG_EMU_EDGE){
00591         if(   (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
00592            || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
00593             s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
00594                                 9, 9, src_x, src_y,
00595                                 s->h_edge_pos>>1, s->v_edge_pos>>1);
00596             ptr= s->edge_emu_buffer;
00597             emu=1;
00598         }
00599     }
00600     pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
00601 
00602     ptr = ref_picture[2] + offset;
00603     if(emu){
00604         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
00605                             9, 9, src_x, src_y,
00606                             s->h_edge_pos>>1, s->v_edge_pos>>1);
00607         ptr= s->edge_emu_buffer;
00608     }
00609     pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
00610 }
00611 
00612 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
00613     /* fetch pixels for estimated mv 4 macroblocks ahead
00614      * optimized for 64byte cache lines */
00615     const int shift = s->quarter_sample ? 2 : 1;
00616     const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
00617     const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
00618     int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
00619     s->dsp.prefetch(pix[0]+off, s->linesize, 4);
00620     off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
00621     s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
00622 }
00623 
00636 static av_always_inline void MPV_motion_internal(MpegEncContext *s,
00637                               uint8_t *dest_y, uint8_t *dest_cb,
00638                               uint8_t *dest_cr, int dir,
00639                               uint8_t **ref_picture,
00640                               op_pixels_func (*pix_op)[4],
00641                               qpel_mc_func (*qpix_op)[16], int is_mpeg12)
00642 {
00643     int dxy, mx, my, src_x, src_y, motion_x, motion_y;
00644     int mb_x, mb_y, i;
00645     uint8_t *ptr, *dest;
00646 
00647     mb_x = s->mb_x;
00648     mb_y = s->mb_y;
00649 
00650     prefetch_motion(s, ref_picture, dir);
00651 
00652     if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
00653         int16_t mv_cache[4][4][2];
00654         const int xy= s->mb_x + s->mb_y*s->mb_stride;
00655         const int mot_stride= s->b8_stride;
00656         const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
00657 
00658         assert(!s->mb_skipped);
00659 
00660         memcpy(mv_cache[1][1], s->current_picture.f.motion_val[0][mot_xy             ], sizeof(int16_t) * 4);
00661         memcpy(mv_cache[2][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
00662         memcpy(mv_cache[3][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
00663 
00664         if (mb_y == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - s->mb_stride])) {
00665             memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
00666         }else{
00667             memcpy(mv_cache[0][1], s->current_picture.f.motion_val[0][mot_xy - mot_stride], sizeof(int16_t) * 4);
00668         }
00669 
00670         if (mb_x == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - 1])) {
00671             AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
00672             AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
00673         }else{
00674             AV_COPY32(mv_cache[1][0], s->current_picture.f.motion_val[0][mot_xy - 1]);
00675             AV_COPY32(mv_cache[2][0], s->current_picture.f.motion_val[0][mot_xy - 1 + mot_stride]);
00676         }
00677 
00678         if (mb_x + 1 >= s->mb_width || IS_INTRA(s->current_picture.f.mb_type[xy + 1])) {
00679             AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
00680             AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
00681         }else{
00682             AV_COPY32(mv_cache[1][3], s->current_picture.f.motion_val[0][mot_xy + 2]);
00683             AV_COPY32(mv_cache[2][3], s->current_picture.f.motion_val[0][mot_xy + 2 + mot_stride]);
00684         }
00685 
00686         mx = 0;
00687         my = 0;
00688         for(i=0;i<4;i++) {
00689             const int x= (i&1)+1;
00690             const int y= (i>>1)+1;
00691             int16_t mv[5][2]= {
00692                 {mv_cache[y][x  ][0], mv_cache[y][x  ][1]},
00693                 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
00694                 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
00695                 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
00696                 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
00697             //FIXME cleanup
00698             obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
00699                         ref_picture[0],
00700                         mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
00701                         pix_op[1],
00702                         mv);
00703 
00704             mx += mv[0][0];
00705             my += mv[0][1];
00706         }
00707         if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
00708             chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
00709 
00710         return;
00711     }
00712 
00713     switch(s->mv_type) {
00714     case MV_TYPE_16X16:
00715         if(s->mcsel){
00716             if(s->real_sprite_warping_points==1){
00717                 gmc1_motion(s, dest_y, dest_cb, dest_cr,
00718                             ref_picture);
00719             }else{
00720                 gmc_motion(s, dest_y, dest_cb, dest_cr,
00721                             ref_picture);
00722             }
00723         }else if(!is_mpeg12 && s->quarter_sample){
00724             qpel_motion(s, dest_y, dest_cb, dest_cr,
00725                         0, 0, 0,
00726                         ref_picture, pix_op, qpix_op,
00727                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
00728         } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
00729                    s->mspel && s->codec_id == CODEC_ID_WMV2) {
00730             ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
00731                         ref_picture, pix_op,
00732                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
00733         }else
00734         {
00735             mpeg_motion(s, dest_y, dest_cb, dest_cr,
00736                         0, 0, 0,
00737                         ref_picture, pix_op,
00738                         s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
00739         }
00740         break;
00741     case MV_TYPE_8X8:
00742     if (!is_mpeg12) {
00743         mx = 0;
00744         my = 0;
00745         if(s->quarter_sample){
00746             for(i=0;i<4;i++) {
00747                 motion_x = s->mv[dir][i][0];
00748                 motion_y = s->mv[dir][i][1];
00749 
00750                 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
00751                 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
00752                 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
00753 
00754                 /* WARNING: do no forget half pels */
00755                 src_x = av_clip(src_x, -16, s->width);
00756                 if (src_x == s->width)
00757                     dxy &= ~3;
00758                 src_y = av_clip(src_y, -16, s->height);
00759                 if (src_y == s->height)
00760                     dxy &= ~12;
00761 
00762                 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
00763                 if(s->flags&CODEC_FLAG_EMU_EDGE){
00764                     if(   (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
00765                        || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
00766                         s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
00767                                             s->linesize, 9, 9,
00768                                             src_x, src_y,
00769                                             s->h_edge_pos, s->v_edge_pos);
00770                         ptr= s->edge_emu_buffer;
00771                     }
00772                 }
00773                 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
00774                 qpix_op[1][dxy](dest, ptr, s->linesize);
00775 
00776                 mx += s->mv[dir][i][0]/2;
00777                 my += s->mv[dir][i][1]/2;
00778             }
00779         }else{
00780             for(i=0;i<4;i++) {
00781                 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
00782                             ref_picture[0], 0, 0,
00783                             mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
00784                             s->width, s->height, s->linesize,
00785                             s->h_edge_pos, s->v_edge_pos,
00786                             8, 8, pix_op[1],
00787                             s->mv[dir][i][0], s->mv[dir][i][1]);
00788 
00789                 mx += s->mv[dir][i][0];
00790                 my += s->mv[dir][i][1];
00791             }
00792         }
00793 
00794         if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
00795             chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
00796     }
00797         break;
00798     case MV_TYPE_FIELD:
00799         if (s->picture_structure == PICT_FRAME) {
00800             if(!is_mpeg12 && s->quarter_sample){
00801                 for(i=0; i<2; i++){
00802                     qpel_motion(s, dest_y, dest_cb, dest_cr,
00803                                 1, i, s->field_select[dir][i],
00804                                 ref_picture, pix_op, qpix_op,
00805                                 s->mv[dir][i][0], s->mv[dir][i][1], 8);
00806                 }
00807             }else{
00808                 /* top field */
00809                 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00810                             1, 0, s->field_select[dir][0],
00811                             ref_picture, pix_op,
00812                             s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
00813                 /* bottom field */
00814                 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00815                             1, 1, s->field_select[dir][1],
00816                             ref_picture, pix_op,
00817                             s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
00818             }
00819         } else {
00820             if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
00821                 ref_picture = s->current_picture_ptr->f.data;
00822             }
00823 
00824             mpeg_motion(s, dest_y, dest_cb, dest_cr,
00825                         0, 0, s->field_select[dir][0],
00826                         ref_picture, pix_op,
00827                         s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
00828         }
00829         break;
00830     case MV_TYPE_16X8:
00831         for(i=0; i<2; i++){
00832             uint8_t ** ref2picture;
00833 
00834             if(s->picture_structure == s->field_select[dir][i] + 1
00835                || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
00836                 ref2picture= ref_picture;
00837             }else{
00838                 ref2picture = s->current_picture_ptr->f.data;
00839             }
00840 
00841             mpeg_motion(s, dest_y, dest_cb, dest_cr,
00842                         0, 0, s->field_select[dir][i],
00843                         ref2picture, pix_op,
00844                         s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
00845 
00846             dest_y += 16*s->linesize;
00847             dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
00848             dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
00849         }
00850         break;
00851     case MV_TYPE_DMV:
00852         if(s->picture_structure == PICT_FRAME){
00853             for(i=0; i<2; i++){
00854                 int j;
00855                 for(j=0; j<2; j++){
00856                     mpeg_motion(s, dest_y, dest_cb, dest_cr,
00857                                 1, j, j^i,
00858                                 ref_picture, pix_op,
00859                                 s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8, mb_y);
00860                 }
00861                 pix_op = s->dsp.avg_pixels_tab;
00862             }
00863         }else{
00864             for(i=0; i<2; i++){
00865                 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00866                             0, 0, s->picture_structure != i+1,
00867                             ref_picture, pix_op,
00868                             s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
00869 
00870                 // after put we make avg of the same block
00871                 pix_op=s->dsp.avg_pixels_tab;
00872 
00873                 //opposite parity is always in the same frame if this is second field
00874                 if(!s->first_field){
00875                     ref_picture = s->current_picture_ptr->f.data;
00876                 }
00877             }
00878         }
00879     break;
00880     default: assert(0);
00881     }
00882 }
00883 
00884 static inline void MPV_motion(MpegEncContext *s,
00885                               uint8_t *dest_y, uint8_t *dest_cb,
00886                               uint8_t *dest_cr, int dir,
00887                               uint8_t **ref_picture,
00888                               op_pixels_func (*pix_op)[4],
00889                               qpel_mc_func (*qpix_op)[16])
00890 {
00891 #if !CONFIG_SMALL
00892     if(s->out_format == FMT_MPEG1)
00893         MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
00894                             ref_picture, pix_op, qpix_op, 1);
00895     else
00896 #endif
00897         MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
00898                             ref_picture, pix_op, qpix_op, 0);
00899 }
00900 #endif /* AVCODEC_MPEGVIDEO_COMMON_H */