2 * Copyright (c) 2000,2001 Fabrice Bellard
3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/avassert.h"
27 #include "libavutil/internal.h"
30 #include "mpegvideo.h"
35 static void gmc1_motion(MpegEncContext *s,
36 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
37 uint8_t **ref_picture)
40 int offset, src_x, src_y, linesize, uvlinesize;
41 int motion_x, motion_y;
44 motion_x= s->sprite_offset[0][0];
45 motion_y= s->sprite_offset[0][1];
46 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
47 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
48 motion_x<<=(3-s->sprite_warping_accuracy);
49 motion_y<<=(3-s->sprite_warping_accuracy);
50 src_x = av_clip(src_x, -16, s->width);
51 if (src_x == s->width)
53 src_y = av_clip(src_y, -16, s->height);
54 if (src_y == s->height)
57 linesize = s->linesize;
58 uvlinesize = s->uvlinesize;
60 ptr = ref_picture[0] + (src_y * linesize) + src_x;
62 if(s->flags&CODEC_FLAG_EMU_EDGE){
63 if( (unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0)
64 || (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)){
65 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
66 ptr= s->edge_emu_buffer;
70 if((motion_x|motion_y)&7){
71 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
72 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
76 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
78 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
80 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
84 if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
86 motion_x= s->sprite_offset[1][0];
87 motion_y= s->sprite_offset[1][1];
88 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
89 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
90 motion_x<<=(3-s->sprite_warping_accuracy);
91 motion_y<<=(3-s->sprite_warping_accuracy);
92 src_x = av_clip(src_x, -8, s->width>>1);
93 if (src_x == s->width>>1)
95 src_y = av_clip(src_y, -8, s->height>>1);
96 if (src_y == s->height>>1)
99 offset = (src_y * uvlinesize) + src_x;
100 ptr = ref_picture[1] + offset;
101 if(s->flags&CODEC_FLAG_EMU_EDGE){
102 if( (unsigned)src_x >= FFMAX((s->h_edge_pos>>1) - 9, 0)
103 || (unsigned)src_y >= FFMAX((s->v_edge_pos>>1) - 9, 0)){
104 s->vdsp.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);
105 ptr= s->edge_emu_buffer;
109 s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
111 ptr = ref_picture[2] + offset;
113 s->vdsp.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);
114 ptr= s->edge_emu_buffer;
116 s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
121 static void gmc_motion(MpegEncContext *s,
122 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
123 uint8_t **ref_picture)
126 int linesize, uvlinesize;
127 const int a= s->sprite_warping_accuracy;
130 linesize = s->linesize;
131 uvlinesize = s->uvlinesize;
133 ptr = ref_picture[0];
135 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
136 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
138 s->dsp.gmc(dest_y, ptr, linesize, 16,
141 s->sprite_delta[0][0], s->sprite_delta[0][1],
142 s->sprite_delta[1][0], s->sprite_delta[1][1],
143 a+1, (1<<(2*a+1)) - s->no_rounding,
144 s->h_edge_pos, s->v_edge_pos);
145 s->dsp.gmc(dest_y+8, ptr, linesize, 16,
146 ox + s->sprite_delta[0][0]*8,
147 oy + s->sprite_delta[1][0]*8,
148 s->sprite_delta[0][0], s->sprite_delta[0][1],
149 s->sprite_delta[1][0], s->sprite_delta[1][1],
150 a+1, (1<<(2*a+1)) - s->no_rounding,
151 s->h_edge_pos, s->v_edge_pos);
153 if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
155 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
156 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
158 ptr = ref_picture[1];
159 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
162 s->sprite_delta[0][0], s->sprite_delta[0][1],
163 s->sprite_delta[1][0], s->sprite_delta[1][1],
164 a+1, (1<<(2*a+1)) - s->no_rounding,
165 s->h_edge_pos>>1, s->v_edge_pos>>1);
167 ptr = ref_picture[2];
168 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
171 s->sprite_delta[0][0], s->sprite_delta[0][1],
172 s->sprite_delta[1][0], s->sprite_delta[1][1],
173 a+1, (1<<(2*a+1)) - s->no_rounding,
174 s->h_edge_pos>>1, s->v_edge_pos>>1);
177 static inline int hpel_motion(MpegEncContext *s,
178 uint8_t *dest, uint8_t *src,
179 int src_x, int src_y,
180 op_pixels_func *pix_op,
181 int motion_x, int motion_y)
186 src_x += motion_x >> 1;
187 src_y += motion_y >> 1;
189 /* WARNING: do no forget half pels */
190 src_x = av_clip(src_x, -16, s->width); //FIXME unneeded for emu?
191 if (src_x != s->width)
193 src_y = av_clip(src_y, -16, s->height);
194 if (src_y != s->height)
195 dxy |= (motion_y & 1) << 1;
196 src += src_y * s->linesize + src_x;
198 if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
199 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 8, 0)
200 || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&1) - 8, 0)){
201 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, 9, 9,
202 src_x, src_y, s->h_edge_pos, s->v_edge_pos);
203 src= s->edge_emu_buffer;
207 pix_op[dxy](dest, src, s->linesize, 8);
211 static av_always_inline
212 void mpeg_motion_internal(MpegEncContext *s,
213 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
214 int field_based, int bottom_field, int field_select,
215 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
216 int motion_x, int motion_y, int h, int is_mpeg12, int mb_y)
218 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
219 int dxy, uvdxy, mx, my, src_x, src_y,
220 uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
223 if(s->quarter_sample)
230 v_edge_pos = s->v_edge_pos >> field_based;
231 linesize = s->current_picture.f.linesize[0] << field_based;
232 uvlinesize = s->current_picture.f.linesize[1] << field_based;
234 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
235 src_x = s->mb_x* 16 + (motion_x >> 1);
236 src_y =( mb_y<<(4-field_based)) + (motion_y >> 1);
238 if (!is_mpeg12 && s->out_format == FMT_H263) {
239 if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
240 mx = (motion_x>>1)|(motion_x&1);
242 uvdxy = ((my & 1) << 1) | (mx & 1);
243 uvsrc_x = s->mb_x* 8 + (mx >> 1);
244 uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
246 uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
250 }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
254 uvsrc_x = s->mb_x*8 + mx;
255 uvsrc_y = mb_y*8 + my;
257 if(s->chroma_y_shift){
260 uvdxy = ((my & 1) << 1) | (mx & 1);
261 uvsrc_x = s->mb_x* 8 + (mx >> 1);
262 uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
264 if(s->chroma_x_shift){
267 uvdxy = ((motion_y & 1) << 1) | (mx & 1);
268 uvsrc_x = s->mb_x* 8 + (mx >> 1);
279 ptr_y = ref_picture[0] + src_y * linesize + src_x;
280 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
281 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
283 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
284 || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&1) - h , 0)){
285 if(is_mpeg12 || s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
286 s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
287 av_log(s->avctx,AV_LOG_DEBUG,
288 "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
291 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
293 src_x, src_y<<field_based,
294 s->h_edge_pos, s->v_edge_pos);
295 ptr_y = s->edge_emu_buffer;
296 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
297 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
298 s->vdsp.emulated_edge_mc(uvbuf ,
299 ptr_cb, s->uvlinesize,
301 uvsrc_x, uvsrc_y<<field_based,
302 s->h_edge_pos>>1, s->v_edge_pos>>1);
303 s->vdsp.emulated_edge_mc(uvbuf+16,
304 ptr_cr, s->uvlinesize,
306 uvsrc_x, uvsrc_y<<field_based,
307 s->h_edge_pos>>1, s->v_edge_pos>>1);
313 if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
314 dest_y += s->linesize;
315 dest_cb+= s->uvlinesize;
316 dest_cr+= s->uvlinesize;
320 ptr_y += s->linesize;
321 ptr_cb+= s->uvlinesize;
322 ptr_cr+= s->uvlinesize;
325 pix_op[0][dxy](dest_y, ptr_y, linesize, h);
327 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
328 pix_op[s->chroma_x_shift][uvdxy]
329 (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
330 pix_op[s->chroma_x_shift][uvdxy]
331 (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
333 if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
334 s->out_format == FMT_H261){
335 ff_h261_loop_filter(s);
338 /* apply one mpeg motion vector to the three components */
339 static void mpeg_motion(MpegEncContext *s,
340 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
341 int field_select, uint8_t **ref_picture,
342 op_pixels_func (*pix_op)[4],
343 int motion_x, int motion_y, int h, int mb_y)
346 if(s->out_format == FMT_MPEG1)
347 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
348 field_select, ref_picture, pix_op,
349 motion_x, motion_y, h, 1, mb_y);
352 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
353 field_select, ref_picture, pix_op,
354 motion_x, motion_y, h, 0, mb_y);
357 static void mpeg_motion_field(MpegEncContext *s, uint8_t *dest_y,
358 uint8_t *dest_cb, uint8_t *dest_cr,
359 int bottom_field, int field_select,
360 uint8_t **ref_picture,
361 op_pixels_func (*pix_op)[4],
362 int motion_x, int motion_y, int h, int mb_y)
365 if(s->out_format == FMT_MPEG1)
366 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
367 bottom_field, field_select, ref_picture, pix_op,
368 motion_x, motion_y, h, 1, mb_y);
371 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
372 bottom_field, field_select, ref_picture, pix_op,
373 motion_x, motion_y, h, 0, mb_y);
376 //FIXME move to dsputil, avg variant, 16x16 version
377 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
379 uint8_t * const top = src[1];
380 uint8_t * const left = src[2];
381 uint8_t * const mid = src[0];
382 uint8_t * const right = src[3];
383 uint8_t * const bottom= src[4];
384 #define OBMC_FILTER(x, t, l, m, r, b)\
385 dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
386 #define OBMC_FILTER4(x, t, l, m, r, b)\
387 OBMC_FILTER(x , t, l, m, r, b);\
388 OBMC_FILTER(x+1 , t, l, m, r, b);\
389 OBMC_FILTER(x +stride, t, l, m, r, b);\
390 OBMC_FILTER(x+1+stride, t, l, m, r, b);
393 OBMC_FILTER (x , 2, 2, 4, 0, 0);
394 OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
395 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
396 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
397 OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
398 OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
400 OBMC_FILTER (x , 1, 2, 5, 0, 0);
401 OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
402 OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
403 OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
405 OBMC_FILTER4(x , 1, 2, 5, 0, 0);
406 OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
407 OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
408 OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
410 OBMC_FILTER4(x , 0, 2, 5, 0, 1);
411 OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
412 OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
413 OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
415 OBMC_FILTER (x , 0, 2, 5, 0, 1);
416 OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
417 OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
418 OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
419 OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
420 OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
422 OBMC_FILTER (x , 0, 2, 4, 0, 2);
423 OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
424 OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
425 OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
428 /* obmc for 1 8x8 luma block */
429 static inline void obmc_motion(MpegEncContext *s,
430 uint8_t *dest, uint8_t *src,
431 int src_x, int src_y,
432 op_pixels_func *pix_op,
433 int16_t mv[5][2]/* mid top left right bottom*/)
439 av_assert2(s->quarter_sample==0);
442 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
445 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
446 hpel_motion(s, ptr[i], src,
453 put_obmc(dest, ptr, s->linesize);
456 static inline void qpel_motion(MpegEncContext *s,
457 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
458 int field_based, int bottom_field, int field_select,
459 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
460 qpel_mc_func (*qpix_op)[16],
461 int motion_x, int motion_y, int h)
463 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
464 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
466 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
467 src_x = s->mb_x * 16 + (motion_x >> 2);
468 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
470 v_edge_pos = s->v_edge_pos >> field_based;
471 linesize = s->linesize << field_based;
472 uvlinesize = s->uvlinesize << field_based;
477 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
478 static const int rtab[8]= {0,0,1,1,0,0,0,1};
479 mx= (motion_x>>1) + rtab[motion_x&7];
480 my= (motion_y>>1) + rtab[motion_y&7];
481 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
482 mx= (motion_x>>1)|(motion_x&1);
483 my= (motion_y>>1)|(motion_y&1);
491 uvdxy= (mx&1) | ((my&1)<<1);
495 uvsrc_x = s->mb_x * 8 + mx;
496 uvsrc_y = s->mb_y * (8 >> field_based) + my;
498 ptr_y = ref_picture[0] + src_y * linesize + src_x;
499 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
500 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
502 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
503 || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&3) - h , 0)){
504 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
505 17, 17+field_based, src_x, src_y<<field_based,
506 s->h_edge_pos, s->v_edge_pos);
507 ptr_y= s->edge_emu_buffer;
508 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
509 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
510 s->vdsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
512 uvsrc_x, uvsrc_y<<field_based,
513 s->h_edge_pos>>1, s->v_edge_pos>>1);
514 s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
516 uvsrc_x, uvsrc_y<<field_based,
517 s->h_edge_pos>>1, s->v_edge_pos>>1);
524 qpix_op[0][dxy](dest_y, ptr_y, linesize);
527 dest_y += s->linesize;
528 dest_cb+= s->uvlinesize;
529 dest_cr+= s->uvlinesize;
533 ptr_y += s->linesize;
534 ptr_cb += s->uvlinesize;
535 ptr_cr += s->uvlinesize;
537 //damn interlaced mode
538 //FIXME boundary mirroring is not exactly correct here
539 qpix_op[1][dxy](dest_y , ptr_y , linesize);
540 qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
542 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
543 pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
544 pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
549 * h263 chroma 4mv motion compensation.
551 static void chroma_4mv_motion(MpegEncContext *s,
552 uint8_t *dest_cb, uint8_t *dest_cr,
553 uint8_t **ref_picture,
554 op_pixels_func *pix_op,
557 int dxy, emu=0, src_x, src_y, offset;
560 /* In case of 8X8, we construct a single chroma motion vector
561 with a special rounding */
562 mx= ff_h263_round_chroma(mx);
563 my= ff_h263_round_chroma(my);
565 dxy = ((my & 1) << 1) | (mx & 1);
569 src_x = s->mb_x * 8 + mx;
570 src_y = s->mb_y * 8 + my;
571 src_x = av_clip(src_x, -8, (s->width >> 1));
572 if (src_x == (s->width >> 1))
574 src_y = av_clip(src_y, -8, (s->height >> 1));
575 if (src_y == (s->height >> 1))
578 offset = src_y * s->uvlinesize + src_x;
579 ptr = ref_picture[1] + offset;
580 if(s->flags&CODEC_FLAG_EMU_EDGE){
581 if( (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
582 || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
583 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
585 s->h_edge_pos>>1, s->v_edge_pos>>1);
586 ptr= s->edge_emu_buffer;
590 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
592 ptr = ref_picture[2] + offset;
594 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
596 s->h_edge_pos>>1, s->v_edge_pos>>1);
597 ptr= s->edge_emu_buffer;
599 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
602 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
603 /* fetch pixels for estimated mv 4 macroblocks ahead
604 * optimized for 64byte cache lines */
605 const int shift = s->quarter_sample ? 2 : 1;
606 const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
607 const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
608 int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
609 s->vdsp.prefetch(pix[0]+off, s->linesize, 4);
610 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
611 s->vdsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
615 * motion compensation of a single macroblock
617 * @param dest_y luma destination pointer
618 * @param dest_cb chroma cb/u destination pointer
619 * @param dest_cr chroma cr/v destination pointer
620 * @param dir direction (0->forward, 1->backward)
621 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
622 * @param pix_op halfpel motion compensation function (average or put normally)
623 * @param qpix_op qpel motion compensation function (average or put normally)
624 * the motion vectors are taken from s->mv and the MV type from s->mv_type
626 static av_always_inline void MPV_motion_internal(MpegEncContext *s,
627 uint8_t *dest_y, uint8_t *dest_cb,
628 uint8_t *dest_cr, int dir,
629 uint8_t **ref_picture,
630 op_pixels_func (*pix_op)[4],
631 qpel_mc_func (*qpix_op)[16], int is_mpeg12)
633 int dxy, mx, my, src_x, src_y, motion_x, motion_y;
640 prefetch_motion(s, ref_picture, dir);
642 if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
643 LOCAL_ALIGNED_8(int16_t, mv_cache, [4], [4][2]);
644 AVFrame *cur_frame = &s->current_picture.f;
645 const int xy= s->mb_x + s->mb_y*s->mb_stride;
646 const int mot_stride= s->b8_stride;
647 const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
649 av_assert2(!s->mb_skipped);
651 AV_COPY32(mv_cache[1][1], cur_frame->motion_val[0][mot_xy ]);
652 AV_COPY32(mv_cache[1][2], cur_frame->motion_val[0][mot_xy + 1]);
654 AV_COPY32(mv_cache[2][1], cur_frame->motion_val[0][mot_xy + mot_stride ]);
655 AV_COPY32(mv_cache[2][2], cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
657 AV_COPY32(mv_cache[3][1], cur_frame->motion_val[0][mot_xy + mot_stride ]);
658 AV_COPY32(mv_cache[3][2], cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
660 if (mb_y == 0 || IS_INTRA(cur_frame->mb_type[xy - s->mb_stride])) {
661 AV_COPY32(mv_cache[0][1], mv_cache[1][1]);
662 AV_COPY32(mv_cache[0][2], mv_cache[1][2]);
664 AV_COPY32(mv_cache[0][1], cur_frame->motion_val[0][mot_xy - mot_stride ]);
665 AV_COPY32(mv_cache[0][2], cur_frame->motion_val[0][mot_xy - mot_stride + 1]);
668 if (mb_x == 0 || IS_INTRA(cur_frame->mb_type[xy - 1])) {
669 AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
670 AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
672 AV_COPY32(mv_cache[1][0], cur_frame->motion_val[0][mot_xy - 1]);
673 AV_COPY32(mv_cache[2][0], cur_frame->motion_val[0][mot_xy - 1 + mot_stride]);
676 if (mb_x + 1 >= s->mb_width || IS_INTRA(cur_frame->mb_type[xy + 1])) {
677 AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
678 AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
680 AV_COPY32(mv_cache[1][3], cur_frame->motion_val[0][mot_xy + 2]);
681 AV_COPY32(mv_cache[2][3], cur_frame->motion_val[0][mot_xy + 2 + mot_stride]);
687 const int x= (i&1)+1;
688 const int y= (i>>1)+1;
690 {mv_cache[y][x ][0], mv_cache[y][x ][1]},
691 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
692 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
693 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
694 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
696 obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
698 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
705 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
706 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
714 if(s->real_sprite_warping_points==1){
715 gmc1_motion(s, dest_y, dest_cb, dest_cr,
718 gmc_motion(s, dest_y, dest_cb, dest_cr,
721 }else if(!is_mpeg12 && s->quarter_sample){
722 qpel_motion(s, dest_y, dest_cb, dest_cr,
724 ref_picture, pix_op, qpix_op,
725 s->mv[dir][0][0], s->mv[dir][0][1], 16);
726 } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
727 s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
728 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
730 s->mv[dir][0][0], s->mv[dir][0][1], 16);
733 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
735 s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
742 if(s->quarter_sample){
744 motion_x = s->mv[dir][i][0];
745 motion_y = s->mv[dir][i][1];
747 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
748 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
749 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
751 /* WARNING: do no forget half pels */
752 src_x = av_clip(src_x, -16, s->width);
753 if (src_x == s->width)
755 src_y = av_clip(src_y, -16, s->height);
756 if (src_y == s->height)
759 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
760 if(s->flags&CODEC_FLAG_EMU_EDGE){
761 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
762 || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
763 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
766 s->h_edge_pos, s->v_edge_pos);
767 ptr= s->edge_emu_buffer;
770 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
771 qpix_op[1][dxy](dest, ptr, s->linesize);
773 mx += s->mv[dir][i][0]/2;
774 my += s->mv[dir][i][1]/2;
778 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
780 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
782 s->mv[dir][i][0], s->mv[dir][i][1]);
784 mx += s->mv[dir][i][0];
785 my += s->mv[dir][i][1];
789 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
790 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
794 if (s->picture_structure == PICT_FRAME) {
795 if(!is_mpeg12 && s->quarter_sample){
797 qpel_motion(s, dest_y, dest_cb, dest_cr,
798 1, i, s->field_select[dir][i],
799 ref_picture, pix_op, qpix_op,
800 s->mv[dir][i][0], s->mv[dir][i][1], 8);
804 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
805 0, s->field_select[dir][0],
807 s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
809 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
810 1, s->field_select[dir][1],
812 s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
815 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
816 ref_picture = s->current_picture_ptr->f.data;
819 mpeg_motion(s, dest_y, dest_cb, dest_cr,
820 s->field_select[dir][0],
822 s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
827 uint8_t ** ref2picture;
829 if(s->picture_structure == s->field_select[dir][i] + 1
830 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
831 ref2picture= ref_picture;
833 ref2picture = s->current_picture_ptr->f.data;
836 mpeg_motion(s, dest_y, dest_cb, dest_cr,
837 s->field_select[dir][i],
839 s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
841 dest_y += 16*s->linesize;
842 dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
843 dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
847 if(s->picture_structure == PICT_FRAME){
851 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
852 j, j^i, ref_picture, pix_op,
853 s->mv[dir][2*i + j][0],
854 s->mv[dir][2*i + j][1], 8, mb_y);
856 pix_op = s->dsp.avg_pixels_tab;
860 mpeg_motion(s, dest_y, dest_cb, dest_cr,
861 s->picture_structure != i+1,
863 s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
865 // after put we make avg of the same block
866 pix_op=s->dsp.avg_pixels_tab;
868 //opposite parity is always in the same frame if this is second field
870 ref_picture = s->current_picture_ptr->f.data;
875 default: av_assert2(0);
879 void ff_MPV_motion(MpegEncContext *s,
880 uint8_t *dest_y, uint8_t *dest_cb,
881 uint8_t *dest_cr, int dir,
882 uint8_t **ref_picture,
883 op_pixels_func (*pix_op)[4],
884 qpel_mc_func (*qpix_op)[16])
887 if(s->out_format == FMT_MPEG1)
888 MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
889 ref_picture, pix_op, qpix_op, 1);
892 MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
893 ref_picture, pix_op, qpix_op, 0);