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 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
00215 src_x = av_clip(src_x, -16, width);
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;
00248 ptrdiff_t 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.f.linesize[0] << field_based;
00260 uvlinesize = s->current_picture.f.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 =( mb_y<<(4-field_based)) + (motion_y >> 1);
00265
00266 if (!is_mpeg12 && 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 =( 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(!is_mpeg12 && 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 = 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 =( 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 > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
00312 || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&1) - h , 0)){
00313 if(is_mpeg12 || s->codec_id == CODEC_ID_MPEG2VIDEO ||
00314 s->codec_id == CODEC_ID_MPEG1VIDEO){
00315 av_log(s->avctx,AV_LOG_DEBUG,
00316 "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
00317 return;
00318 }
00319 s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
00320 17, 17+field_based,
00321 src_x, src_y<<field_based,
00322 s->h_edge_pos, s->v_edge_pos);
00323 ptr_y = s->edge_emu_buffer;
00324 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00325 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
00326 s->dsp.emulated_edge_mc(uvbuf ,
00327 ptr_cb, s->uvlinesize,
00328 9, 9+field_based,
00329 uvsrc_x, uvsrc_y<<field_based,
00330 s->h_edge_pos>>1, s->v_edge_pos>>1);
00331 s->dsp.emulated_edge_mc(uvbuf+16,
00332 ptr_cr, s->uvlinesize,
00333 9, 9+field_based,
00334 uvsrc_x, uvsrc_y<<field_based,
00335 s->h_edge_pos>>1, s->v_edge_pos>>1);
00336 ptr_cb= uvbuf;
00337 ptr_cr= uvbuf+16;
00338 }
00339 }
00340
00341 if(bottom_field){
00342 dest_y += s->linesize;
00343 dest_cb+= s->uvlinesize;
00344 dest_cr+= s->uvlinesize;
00345 }
00346
00347 if(field_select){
00348 ptr_y += s->linesize;
00349 ptr_cb+= s->uvlinesize;
00350 ptr_cr+= s->uvlinesize;
00351 }
00352
00353 pix_op[0][dxy](dest_y, ptr_y, linesize, h);
00354
00355 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00356 pix_op[s->chroma_x_shift][uvdxy]
00357 (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
00358 pix_op[s->chroma_x_shift][uvdxy]
00359 (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
00360 }
00361 if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
00362 s->out_format == FMT_H261){
00363 ff_h261_loop_filter(s);
00364 }
00365 }
00366
00367 static av_always_inline
00368 void mpeg_motion(MpegEncContext *s,
00369 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00370 int field_based, int bottom_field, int field_select,
00371 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
00372 int motion_x, int motion_y, int h, int mb_y)
00373 {
00374 #if !CONFIG_SMALL
00375 if(s->out_format == FMT_MPEG1)
00376 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
00377 bottom_field, field_select, ref_picture, pix_op,
00378 motion_x, motion_y, h, 1, mb_y);
00379 else
00380 #endif
00381 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
00382 bottom_field, field_select, ref_picture, pix_op,
00383 motion_x, motion_y, h, 0, mb_y);
00384 }
00385
00386
00387 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
00388 int x;
00389 uint8_t * const top = src[1];
00390 uint8_t * const left = src[2];
00391 uint8_t * const mid = src[0];
00392 uint8_t * const right = src[3];
00393 uint8_t * const bottom= src[4];
00394 #define OBMC_FILTER(x, t, l, m, r, b)\
00395 dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
00396 #define OBMC_FILTER4(x, t, l, m, r, b)\
00397 OBMC_FILTER(x , t, l, m, r, b);\
00398 OBMC_FILTER(x+1 , t, l, m, r, b);\
00399 OBMC_FILTER(x +stride, t, l, m, r, b);\
00400 OBMC_FILTER(x+1+stride, t, l, m, r, b);
00401
00402 x=0;
00403 OBMC_FILTER (x , 2, 2, 4, 0, 0);
00404 OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
00405 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
00406 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
00407 OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
00408 OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
00409 x+= stride;
00410 OBMC_FILTER (x , 1, 2, 5, 0, 0);
00411 OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
00412 OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
00413 OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
00414 x+= stride;
00415 OBMC_FILTER4(x , 1, 2, 5, 0, 0);
00416 OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
00417 OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
00418 OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
00419 x+= 2*stride;
00420 OBMC_FILTER4(x , 0, 2, 5, 0, 1);
00421 OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
00422 OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
00423 OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
00424 x+= 2*stride;
00425 OBMC_FILTER (x , 0, 2, 5, 0, 1);
00426 OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
00427 OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
00428 OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
00429 OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
00430 OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
00431 x+= stride;
00432 OBMC_FILTER (x , 0, 2, 4, 0, 2);
00433 OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
00434 OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
00435 OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
00436 }
00437
00438
00439 static inline void obmc_motion(MpegEncContext *s,
00440 uint8_t *dest, uint8_t *src,
00441 int src_x, int src_y,
00442 op_pixels_func *pix_op,
00443 int16_t mv[5][2])
00444 #define MID 0
00445 {
00446 int i;
00447 uint8_t *ptr[5];
00448
00449 assert(s->quarter_sample==0);
00450
00451 for(i=0; i<5; i++){
00452 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
00453 ptr[i]= ptr[MID];
00454 }else{
00455 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
00456 hpel_motion(s, ptr[i], src, 0, 0,
00457 src_x, src_y,
00458 s->width, s->height, s->linesize,
00459 s->h_edge_pos, s->v_edge_pos,
00460 8, 8, pix_op,
00461 mv[i][0], mv[i][1]);
00462 }
00463 }
00464
00465 put_obmc(dest, ptr, s->linesize);
00466 }
00467
00468 static inline void qpel_motion(MpegEncContext *s,
00469 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00470 int field_based, int bottom_field, int field_select,
00471 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
00472 qpel_mc_func (*qpix_op)[16],
00473 int motion_x, int motion_y, int h)
00474 {
00475 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
00476 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
00477
00478 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
00479 src_x = s->mb_x * 16 + (motion_x >> 2);
00480 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
00481
00482 v_edge_pos = s->v_edge_pos >> field_based;
00483 linesize = s->linesize << field_based;
00484 uvlinesize = s->uvlinesize << field_based;
00485
00486 if(field_based){
00487 mx= motion_x/2;
00488 my= motion_y>>1;
00489 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
00490 static const int rtab[8]= {0,0,1,1,0,0,0,1};
00491 mx= (motion_x>>1) + rtab[motion_x&7];
00492 my= (motion_y>>1) + rtab[motion_y&7];
00493 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
00494 mx= (motion_x>>1)|(motion_x&1);
00495 my= (motion_y>>1)|(motion_y&1);
00496 }else{
00497 mx= motion_x/2;
00498 my= motion_y/2;
00499 }
00500 mx= (mx>>1)|(mx&1);
00501 my= (my>>1)|(my&1);
00502
00503 uvdxy= (mx&1) | ((my&1)<<1);
00504 mx>>=1;
00505 my>>=1;
00506
00507 uvsrc_x = s->mb_x * 8 + mx;
00508 uvsrc_y = s->mb_y * (8 >> field_based) + my;
00509
00510 ptr_y = ref_picture[0] + src_y * linesize + src_x;
00511 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
00512 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
00513
00514 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
00515 || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&3) - h , 0)){
00516 s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
00517 17, 17+field_based, src_x, src_y<<field_based,
00518 s->h_edge_pos, s->v_edge_pos);
00519 ptr_y= s->edge_emu_buffer;
00520 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00521 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
00522 s->dsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
00523 9, 9 + field_based,
00524 uvsrc_x, uvsrc_y<<field_based,
00525 s->h_edge_pos>>1, s->v_edge_pos>>1);
00526 s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
00527 9, 9 + field_based,
00528 uvsrc_x, uvsrc_y<<field_based,
00529 s->h_edge_pos>>1, s->v_edge_pos>>1);
00530 ptr_cb= uvbuf;
00531 ptr_cr= uvbuf + 16;
00532 }
00533 }
00534
00535 if(!field_based)
00536 qpix_op[0][dxy](dest_y, ptr_y, linesize);
00537 else{
00538 if(bottom_field){
00539 dest_y += s->linesize;
00540 dest_cb+= s->uvlinesize;
00541 dest_cr+= s->uvlinesize;
00542 }
00543
00544 if(field_select){
00545 ptr_y += s->linesize;
00546 ptr_cb += s->uvlinesize;
00547 ptr_cr += s->uvlinesize;
00548 }
00549
00550
00551 qpix_op[1][dxy](dest_y , ptr_y , linesize);
00552 qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
00553 }
00554 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00555 pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
00556 pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
00557 }
00558 }
00559
00563 static inline void chroma_4mv_motion(MpegEncContext *s,
00564 uint8_t *dest_cb, uint8_t *dest_cr,
00565 uint8_t **ref_picture,
00566 op_pixels_func *pix_op,
00567 int mx, int my){
00568 int dxy, emu=0, src_x, src_y, offset;
00569 uint8_t *ptr;
00570
00571
00572
00573 mx= ff_h263_round_chroma(mx);
00574 my= ff_h263_round_chroma(my);
00575
00576 dxy = ((my & 1) << 1) | (mx & 1);
00577 mx >>= 1;
00578 my >>= 1;
00579
00580 src_x = s->mb_x * 8 + mx;
00581 src_y = s->mb_y * 8 + my;
00582 src_x = av_clip(src_x, -8, (s->width >> 1));
00583 if (src_x == (s->width >> 1))
00584 dxy &= ~1;
00585 src_y = av_clip(src_y, -8, (s->height >> 1));
00586 if (src_y == (s->height >> 1))
00587 dxy &= ~2;
00588
00589 offset = src_y * s->uvlinesize + src_x;
00590 ptr = ref_picture[1] + offset;
00591 if(s->flags&CODEC_FLAG_EMU_EDGE){
00592 if( (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
00593 || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
00594 s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
00595 9, 9, src_x, src_y,
00596 s->h_edge_pos>>1, s->v_edge_pos>>1);
00597 ptr= s->edge_emu_buffer;
00598 emu=1;
00599 }
00600 }
00601 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
00602
00603 ptr = ref_picture[2] + offset;
00604 if(emu){
00605 s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
00606 9, 9, src_x, src_y,
00607 s->h_edge_pos>>1, s->v_edge_pos>>1);
00608 ptr= s->edge_emu_buffer;
00609 }
00610 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
00611 }
00612
00613 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
00614
00615
00616 const int shift = s->quarter_sample ? 2 : 1;
00617 const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
00618 const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
00619 int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
00620 s->dsp.prefetch(pix[0]+off, s->linesize, 4);
00621 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
00622 s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
00623 }
00624
00637 static av_always_inline void MPV_motion_internal(MpegEncContext *s,
00638 uint8_t *dest_y, uint8_t *dest_cb,
00639 uint8_t *dest_cr, int dir,
00640 uint8_t **ref_picture,
00641 op_pixels_func (*pix_op)[4],
00642 qpel_mc_func (*qpix_op)[16], int is_mpeg12)
00643 {
00644 int dxy, mx, my, src_x, src_y, motion_x, motion_y;
00645 int mb_x, mb_y, i;
00646 uint8_t *ptr, *dest;
00647
00648 mb_x = s->mb_x;
00649 mb_y = s->mb_y;
00650
00651 prefetch_motion(s, ref_picture, dir);
00652
00653 if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
00654 int16_t mv_cache[4][4][2];
00655 const int xy= s->mb_x + s->mb_y*s->mb_stride;
00656 const int mot_stride= s->b8_stride;
00657 const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
00658
00659 assert(!s->mb_skipped);
00660
00661 memcpy(mv_cache[1][1], s->current_picture.f.motion_val[0][mot_xy ], sizeof(int16_t) * 4);
00662 memcpy(mv_cache[2][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
00663 memcpy(mv_cache[3][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
00664
00665 if (mb_y == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - s->mb_stride])) {
00666 memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
00667 }else{
00668 memcpy(mv_cache[0][1], s->current_picture.f.motion_val[0][mot_xy - mot_stride], sizeof(int16_t) * 4);
00669 }
00670
00671 if (mb_x == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - 1])) {
00672 AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
00673 AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
00674 }else{
00675 AV_COPY32(mv_cache[1][0], s->current_picture.f.motion_val[0][mot_xy - 1]);
00676 AV_COPY32(mv_cache[2][0], s->current_picture.f.motion_val[0][mot_xy - 1 + mot_stride]);
00677 }
00678
00679 if (mb_x + 1 >= s->mb_width || IS_INTRA(s->current_picture.f.mb_type[xy + 1])) {
00680 AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
00681 AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
00682 }else{
00683 AV_COPY32(mv_cache[1][3], s->current_picture.f.motion_val[0][mot_xy + 2]);
00684 AV_COPY32(mv_cache[2][3], s->current_picture.f.motion_val[0][mot_xy + 2 + mot_stride]);
00685 }
00686
00687 mx = 0;
00688 my = 0;
00689 for(i=0;i<4;i++) {
00690 const int x= (i&1)+1;
00691 const int y= (i>>1)+1;
00692 int16_t mv[5][2]= {
00693 {mv_cache[y][x ][0], mv_cache[y][x ][1]},
00694 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
00695 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
00696 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
00697 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
00698
00699 obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
00700 ref_picture[0],
00701 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
00702 pix_op[1],
00703 mv);
00704
00705 mx += mv[0][0];
00706 my += mv[0][1];
00707 }
00708 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
00709 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
00710
00711 return;
00712 }
00713
00714 switch(s->mv_type) {
00715 case MV_TYPE_16X16:
00716 if(s->mcsel){
00717 if(s->real_sprite_warping_points==1){
00718 gmc1_motion(s, dest_y, dest_cb, dest_cr,
00719 ref_picture);
00720 }else{
00721 gmc_motion(s, dest_y, dest_cb, dest_cr,
00722 ref_picture);
00723 }
00724 }else if(!is_mpeg12 && s->quarter_sample){
00725 qpel_motion(s, dest_y, dest_cb, dest_cr,
00726 0, 0, 0,
00727 ref_picture, pix_op, qpix_op,
00728 s->mv[dir][0][0], s->mv[dir][0][1], 16);
00729 } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
00730 s->mspel && s->codec_id == CODEC_ID_WMV2) {
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
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 > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
00766 || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
00767 s->dsp.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
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
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 != AV_PICTURE_TYPE_B && !s->first_field){
00822 ref_picture = s->current_picture_ptr->f.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 == AV_PICTURE_TYPE_B || s->first_field){
00837 ref2picture= ref_picture;
00838 }else{
00839 ref2picture = s->current_picture_ptr->f.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
00872 pix_op=s->dsp.avg_pixels_tab;
00873
00874
00875 if(!s->first_field){
00876 ref_picture = s->current_picture_ptr->f.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