• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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

Generated on Fri Sep 16 2011 17:17:40 for FFmpeg by  doxygen 1.7.1