00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #ifndef FFMPEG_MPEGVIDEO_COMMON_H
00031 #define FFMPEG_MPEGVIDEO_COMMON_H
00032
00033 #include "avcodec.h"
00034 #include "dsputil.h"
00035 #include "mpegvideo.h"
00036 #include "mjpegenc.h"
00037 #include "msmpeg4.h"
00038 #include "faandct.h"
00039 #include <limits.h>
00040
00041 int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
00042 int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
00043 void denoise_dct_c(MpegEncContext *s, DCTELEM *block);
00044 void copy_picture(Picture *dst, Picture *src);
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(ENABLE_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(ENABLE_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
00217 src_x = av_clip(src_x, -16, width);
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
00241 static av_always_inline void mpeg_motion(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)
00246 {
00247 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
00248 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
00249
00250 #if 0
00251 if(s->quarter_sample)
00252 {
00253 motion_x>>=1;
00254 motion_y>>=1;
00255 }
00256 #endif
00257
00258 v_edge_pos = s->v_edge_pos >> field_based;
00259 linesize = s->current_picture.linesize[0] << field_based;
00260 uvlinesize = s->current_picture.linesize[1] << field_based;
00261
00262 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
00263 src_x = s->mb_x* 16 + (motion_x >> 1);
00264 src_y =(s->mb_y<<(4-field_based)) + (motion_y >> 1);
00265
00266 if (s->out_format == FMT_H263) {
00267 if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
00268 mx = (motion_x>>1)|(motion_x&1);
00269 my = motion_y >>1;
00270 uvdxy = ((my & 1) << 1) | (mx & 1);
00271 uvsrc_x = s->mb_x* 8 + (mx >> 1);
00272 uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
00273 }else{
00274 uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
00275 uvsrc_x = src_x>>1;
00276 uvsrc_y = src_y>>1;
00277 }
00278 }else if(s->out_format == FMT_H261){
00279 mx = motion_x / 4;
00280 my = motion_y / 4;
00281 uvdxy = 0;
00282 uvsrc_x = s->mb_x*8 + mx;
00283 uvsrc_y = s->mb_y*8 + my;
00284 } else {
00285 if(s->chroma_y_shift){
00286 mx = motion_x / 2;
00287 my = motion_y / 2;
00288 uvdxy = ((my & 1) << 1) | (mx & 1);
00289 uvsrc_x = s->mb_x* 8 + (mx >> 1);
00290 uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
00291 } else {
00292 if(s->chroma_x_shift){
00293
00294 mx = motion_x / 2;
00295 uvdxy = ((motion_y & 1) << 1) | (mx & 1);
00296 uvsrc_x = s->mb_x* 8 + (mx >> 1);
00297 uvsrc_y = src_y;
00298 } else {
00299
00300 uvdxy = dxy;
00301 uvsrc_x = src_x;
00302 uvsrc_y = src_y;
00303 }
00304 }
00305 }
00306
00307 ptr_y = ref_picture[0] + src_y * linesize + src_x;
00308 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
00309 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
00310
00311 if( (unsigned)src_x > s->h_edge_pos - (motion_x&1) - 16
00312 || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
00313 if(s->codec_id == CODEC_ID_MPEG2VIDEO ||
00314 s->codec_id == CODEC_ID_MPEG1VIDEO){
00315 av_log(s->avctx,AV_LOG_DEBUG,"MPEG motion vector out of boundary\n");
00316 return ;
00317 }
00318 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
00319 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
00320 ptr_y = s->edge_emu_buffer;
00321 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00322 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
00323 ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
00324 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
00325 ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
00326 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
00327 ptr_cb= uvbuf;
00328 ptr_cr= uvbuf+16;
00329 }
00330 }
00331
00332 if(bottom_field){
00333 dest_y += s->linesize;
00334 dest_cb+= s->uvlinesize;
00335 dest_cr+= s->uvlinesize;
00336 }
00337
00338 if(field_select){
00339 ptr_y += s->linesize;
00340 ptr_cb+= s->uvlinesize;
00341 ptr_cr+= s->uvlinesize;
00342 }
00343
00344 pix_op[0][dxy](dest_y, ptr_y, linesize, h);
00345
00346 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00347 pix_op[s->chroma_x_shift][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
00348 pix_op[s->chroma_x_shift][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
00349 }
00350 if((ENABLE_H261_ENCODER || ENABLE_H261_DECODER) && s->out_format == FMT_H261){
00351 ff_h261_loop_filter(s);
00352 }
00353 }
00354
00355
00356 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
00357 int x;
00358 uint8_t * const top = src[1];
00359 uint8_t * const left = src[2];
00360 uint8_t * const mid = src[0];
00361 uint8_t * const right = src[3];
00362 uint8_t * const bottom= src[4];
00363 #define OBMC_FILTER(x, t, l, m, r, b)\
00364 dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
00365 #define OBMC_FILTER4(x, t, l, m, r, b)\
00366 OBMC_FILTER(x , t, l, m, r, b);\
00367 OBMC_FILTER(x+1 , t, l, m, r, b);\
00368 OBMC_FILTER(x +stride, t, l, m, r, b);\
00369 OBMC_FILTER(x+1+stride, t, l, m, r, b);
00370
00371 x=0;
00372 OBMC_FILTER (x , 2, 2, 4, 0, 0);
00373 OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
00374 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
00375 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
00376 OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
00377 OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
00378 x+= stride;
00379 OBMC_FILTER (x , 1, 2, 5, 0, 0);
00380 OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
00381 OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
00382 OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
00383 x+= stride;
00384 OBMC_FILTER4(x , 1, 2, 5, 0, 0);
00385 OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
00386 OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
00387 OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
00388 x+= 2*stride;
00389 OBMC_FILTER4(x , 0, 2, 5, 0, 1);
00390 OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
00391 OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
00392 OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
00393 x+= 2*stride;
00394 OBMC_FILTER (x , 0, 2, 5, 0, 1);
00395 OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
00396 OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
00397 OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
00398 OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
00399 OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
00400 x+= stride;
00401 OBMC_FILTER (x , 0, 2, 4, 0, 2);
00402 OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
00403 OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
00404 OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
00405 }
00406
00407
00408 static inline void obmc_motion(MpegEncContext *s,
00409 uint8_t *dest, uint8_t *src,
00410 int src_x, int src_y,
00411 op_pixels_func *pix_op,
00412 int16_t mv[5][2])
00413 #define MID 0
00414 {
00415 int i;
00416 uint8_t *ptr[5];
00417
00418 assert(s->quarter_sample==0);
00419
00420 for(i=0; i<5; i++){
00421 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
00422 ptr[i]= ptr[MID];
00423 }else{
00424 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
00425 hpel_motion(s, ptr[i], src, 0, 0,
00426 src_x, src_y,
00427 s->width, s->height, s->linesize,
00428 s->h_edge_pos, s->v_edge_pos,
00429 8, 8, pix_op,
00430 mv[i][0], mv[i][1]);
00431 }
00432 }
00433
00434 put_obmc(dest, ptr, s->linesize);
00435 }
00436
00437 static inline void qpel_motion(MpegEncContext *s,
00438 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00439 int field_based, int bottom_field, int field_select,
00440 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
00441 qpel_mc_func (*qpix_op)[16],
00442 int motion_x, int motion_y, int h)
00443 {
00444 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
00445 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
00446
00447 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
00448 src_x = s->mb_x * 16 + (motion_x >> 2);
00449 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
00450
00451 v_edge_pos = s->v_edge_pos >> field_based;
00452 linesize = s->linesize << field_based;
00453 uvlinesize = s->uvlinesize << field_based;
00454
00455 if(field_based){
00456 mx= motion_x/2;
00457 my= motion_y>>1;
00458 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
00459 static const int rtab[8]= {0,0,1,1,0,0,0,1};
00460 mx= (motion_x>>1) + rtab[motion_x&7];
00461 my= (motion_y>>1) + rtab[motion_y&7];
00462 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
00463 mx= (motion_x>>1)|(motion_x&1);
00464 my= (motion_y>>1)|(motion_y&1);
00465 }else{
00466 mx= motion_x/2;
00467 my= motion_y/2;
00468 }
00469 mx= (mx>>1)|(mx&1);
00470 my= (my>>1)|(my&1);
00471
00472 uvdxy= (mx&1) | ((my&1)<<1);
00473 mx>>=1;
00474 my>>=1;
00475
00476 uvsrc_x = s->mb_x * 8 + mx;
00477 uvsrc_y = s->mb_y * (8 >> field_based) + my;
00478
00479 ptr_y = ref_picture[0] + src_y * linesize + src_x;
00480 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
00481 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
00482
00483 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
00484 || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
00485 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
00486 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
00487 ptr_y= s->edge_emu_buffer;
00488 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00489 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
00490 ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize, 9, 9 + field_based,
00491 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
00492 ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9, 9 + field_based,
00493 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
00494 ptr_cb= uvbuf;
00495 ptr_cr= uvbuf + 16;
00496 }
00497 }
00498
00499 if(!field_based)
00500 qpix_op[0][dxy](dest_y, ptr_y, linesize);
00501 else{
00502 if(bottom_field){
00503 dest_y += s->linesize;
00504 dest_cb+= s->uvlinesize;
00505 dest_cr+= s->uvlinesize;
00506 }
00507
00508 if(field_select){
00509 ptr_y += s->linesize;
00510 ptr_cb += s->uvlinesize;
00511 ptr_cr += s->uvlinesize;
00512 }
00513
00514
00515 qpix_op[1][dxy](dest_y , ptr_y , linesize);
00516 qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
00517 }
00518 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00519 pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
00520 pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
00521 }
00522 }
00523
00527 static inline void chroma_4mv_motion(MpegEncContext *s,
00528 uint8_t *dest_cb, uint8_t *dest_cr,
00529 uint8_t **ref_picture,
00530 op_pixels_func *pix_op,
00531 int mx, int my){
00532 int dxy, emu=0, src_x, src_y, offset;
00533 uint8_t *ptr;
00534
00535
00536
00537 mx= ff_h263_round_chroma(mx);
00538 my= ff_h263_round_chroma(my);
00539
00540 dxy = ((my & 1) << 1) | (mx & 1);
00541 mx >>= 1;
00542 my >>= 1;
00543
00544 src_x = s->mb_x * 8 + mx;
00545 src_y = s->mb_y * 8 + my;
00546 src_x = av_clip(src_x, -8, s->width/2);
00547 if (src_x == s->width/2)
00548 dxy &= ~1;
00549 src_y = av_clip(src_y, -8, s->height/2);
00550 if (src_y == s->height/2)
00551 dxy &= ~2;
00552
00553 offset = (src_y * (s->uvlinesize)) + src_x;
00554 ptr = ref_picture[1] + offset;
00555 if(s->flags&CODEC_FLAG_EMU_EDGE){
00556 if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
00557 || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
00558 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
00559 ptr= s->edge_emu_buffer;
00560 emu=1;
00561 }
00562 }
00563 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
00564
00565 ptr = ref_picture[2] + offset;
00566 if(emu){
00567 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
00568 ptr= s->edge_emu_buffer;
00569 }
00570 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
00571 }
00572
00573 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
00574
00575
00576 const int shift = s->quarter_sample ? 2 : 1;
00577 const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
00578 const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
00579 int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
00580 s->dsp.prefetch(pix[0]+off, s->linesize, 4);
00581 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
00582 s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
00583 }
00584
00597 static inline void MPV_motion(MpegEncContext *s,
00598 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00599 int dir, uint8_t **ref_picture,
00600 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
00601 {
00602 int dxy, mx, my, src_x, src_y, motion_x, motion_y;
00603 int mb_x, mb_y, i;
00604 uint8_t *ptr, *dest;
00605
00606 mb_x = s->mb_x;
00607 mb_y = s->mb_y;
00608
00609 prefetch_motion(s, ref_picture, dir);
00610
00611 if(s->obmc && s->pict_type != B_TYPE){
00612 int16_t mv_cache[4][4][2];
00613 const int xy= s->mb_x + s->mb_y*s->mb_stride;
00614 const int mot_stride= s->b8_stride;
00615 const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
00616
00617 assert(!s->mb_skipped);
00618
00619 memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
00620 memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
00621 memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
00622
00623 if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
00624 memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
00625 }else{
00626 memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
00627 }
00628
00629 if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
00630 *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
00631 *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
00632 }else{
00633 *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
00634 *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
00635 }
00636
00637 if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
00638 *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
00639 *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
00640 }else{
00641 *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
00642 *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
00643 }
00644
00645 mx = 0;
00646 my = 0;
00647 for(i=0;i<4;i++) {
00648 const int x= (i&1)+1;
00649 const int y= (i>>1)+1;
00650 int16_t mv[5][2]= {
00651 {mv_cache[y][x ][0], mv_cache[y][x ][1]},
00652 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
00653 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
00654 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
00655 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
00656
00657 obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
00658 ref_picture[0],
00659 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
00660 pix_op[1],
00661 mv);
00662
00663 mx += mv[0][0];
00664 my += mv[0][1];
00665 }
00666 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY))
00667 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
00668
00669 return;
00670 }
00671
00672 switch(s->mv_type) {
00673 case MV_TYPE_16X16:
00674 if(s->mcsel){
00675 if(s->real_sprite_warping_points==1){
00676 gmc1_motion(s, dest_y, dest_cb, dest_cr,
00677 ref_picture);
00678 }else{
00679 gmc_motion(s, dest_y, dest_cb, dest_cr,
00680 ref_picture);
00681 }
00682 }else if(s->quarter_sample){
00683 qpel_motion(s, dest_y, dest_cb, dest_cr,
00684 0, 0, 0,
00685 ref_picture, pix_op, qpix_op,
00686 s->mv[dir][0][0], s->mv[dir][0][1], 16);
00687 }else if(ENABLE_WMV2 && s->mspel){
00688 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
00689 ref_picture, pix_op,
00690 s->mv[dir][0][0], s->mv[dir][0][1], 16);
00691 }else
00692 {
00693 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00694 0, 0, 0,
00695 ref_picture, pix_op,
00696 s->mv[dir][0][0], s->mv[dir][0][1], 16);
00697 }
00698 break;
00699 case MV_TYPE_8X8:
00700 mx = 0;
00701 my = 0;
00702 if(s->quarter_sample){
00703 for(i=0;i<4;i++) {
00704 motion_x = s->mv[dir][i][0];
00705 motion_y = s->mv[dir][i][1];
00706
00707 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
00708 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
00709 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
00710
00711
00712 src_x = av_clip(src_x, -16, s->width);
00713 if (src_x == s->width)
00714 dxy &= ~3;
00715 src_y = av_clip(src_y, -16, s->height);
00716 if (src_y == s->height)
00717 dxy &= ~12;
00718
00719 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
00720 if(s->flags&CODEC_FLAG_EMU_EDGE){
00721 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
00722 || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
00723 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
00724 ptr= s->edge_emu_buffer;
00725 }
00726 }
00727 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
00728 qpix_op[1][dxy](dest, ptr, s->linesize);
00729
00730 mx += s->mv[dir][i][0]/2;
00731 my += s->mv[dir][i][1]/2;
00732 }
00733 }else{
00734 for(i=0;i<4;i++) {
00735 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
00736 ref_picture[0], 0, 0,
00737 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
00738 s->width, s->height, s->linesize,
00739 s->h_edge_pos, s->v_edge_pos,
00740 8, 8, pix_op[1],
00741 s->mv[dir][i][0], s->mv[dir][i][1]);
00742
00743 mx += s->mv[dir][i][0];
00744 my += s->mv[dir][i][1];
00745 }
00746 }
00747
00748 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY))
00749 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
00750 break;
00751 case MV_TYPE_FIELD:
00752 if (s->picture_structure == PICT_FRAME) {
00753 if(s->quarter_sample){
00754 for(i=0; i<2; i++){
00755 qpel_motion(s, dest_y, dest_cb, dest_cr,
00756 1, i, s->field_select[dir][i],
00757 ref_picture, pix_op, qpix_op,
00758 s->mv[dir][i][0], s->mv[dir][i][1], 8);
00759 }
00760 }else{
00761
00762 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00763 1, 0, s->field_select[dir][0],
00764 ref_picture, pix_op,
00765 s->mv[dir][0][0], s->mv[dir][0][1], 8);
00766
00767 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00768 1, 1, s->field_select[dir][1],
00769 ref_picture, pix_op,
00770 s->mv[dir][1][0], s->mv[dir][1][1], 8);
00771 }
00772 } else {
00773 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
00774 ref_picture= s->current_picture_ptr->data;
00775 }
00776
00777 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00778 0, 0, s->field_select[dir][0],
00779 ref_picture, pix_op,
00780 s->mv[dir][0][0], s->mv[dir][0][1], 16);
00781 }
00782 break;
00783 case MV_TYPE_16X8:
00784 for(i=0; i<2; i++){
00785 uint8_t ** ref2picture;
00786
00787 if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
00788 ref2picture= ref_picture;
00789 }else{
00790 ref2picture= s->current_picture_ptr->data;
00791 }
00792
00793 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00794 0, 0, s->field_select[dir][i],
00795 ref2picture, pix_op,
00796 s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8);
00797
00798 dest_y += 16*s->linesize;
00799 dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
00800 dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
00801 }
00802 break;
00803 case MV_TYPE_DMV:
00804 if(s->picture_structure == PICT_FRAME){
00805 for(i=0; i<2; i++){
00806 int j;
00807 for(j=0; j<2; j++){
00808 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00809 1, j, j^i,
00810 ref_picture, pix_op,
00811 s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8);
00812 }
00813 pix_op = s->dsp.avg_pixels_tab;
00814 }
00815 }else{
00816 for(i=0; i<2; i++){
00817 mpeg_motion(s, dest_y, dest_cb, dest_cr,
00818 0, 0, s->picture_structure != i+1,
00819 ref_picture, pix_op,
00820 s->mv[dir][2*i][0],s->mv[dir][2*i][1],16);
00821
00822
00823 pix_op=s->dsp.avg_pixels_tab;
00824
00825
00826 if(!s->first_field){
00827 ref_picture = s->current_picture_ptr->data;
00828 }
00829 }
00830 }
00831 break;
00832 default: assert(0);
00833 }
00834 }
00835
00836 #endif