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"
31 #include "mpegvideo.h"
36 static void gmc1_motion(MpegEncContext *s,
37 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
38 uint8_t **ref_picture)
41 int offset, src_x, src_y, linesize, uvlinesize;
42 int motion_x, motion_y;
45 motion_x= s->sprite_offset[0][0];
46 motion_y= s->sprite_offset[0][1];
47 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
48 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
49 motion_x<<=(3-s->sprite_warping_accuracy);
50 motion_y<<=(3-s->sprite_warping_accuracy);
51 src_x = av_clip(src_x, -16, s->width);
52 if (src_x == s->width)
54 src_y = av_clip(src_y, -16, s->height);
55 if (src_y == s->height)
58 linesize = s->linesize;
59 uvlinesize = s->uvlinesize;
61 ptr = ref_picture[0] + (src_y * linesize) + src_x;
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;
69 if((motion_x|motion_y)&7){
70 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
71 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
75 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
77 s->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
79 s->hdsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
83 if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
85 motion_x= s->sprite_offset[1][0];
86 motion_y= s->sprite_offset[1][1];
87 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
88 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
89 motion_x<<=(3-s->sprite_warping_accuracy);
90 motion_y<<=(3-s->sprite_warping_accuracy);
91 src_x = av_clip(src_x, -8, s->width>>1);
92 if (src_x == s->width>>1)
94 src_y = av_clip(src_y, -8, s->height>>1);
95 if (src_y == s->height>>1)
98 offset = (src_y * uvlinesize) + src_x;
99 ptr = ref_picture[1] + offset;
100 if( (unsigned)src_x >= FFMAX((s->h_edge_pos>>1) - 9, 0)
101 || (unsigned)src_y >= FFMAX((s->v_edge_pos>>1) - 9, 0)){
102 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);
103 ptr= s->edge_emu_buffer;
106 s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
108 ptr = ref_picture[2] + offset;
110 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);
111 ptr= s->edge_emu_buffer;
113 s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
118 static void gmc_motion(MpegEncContext *s,
119 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
120 uint8_t **ref_picture)
123 int linesize, uvlinesize;
124 const int a= s->sprite_warping_accuracy;
127 linesize = s->linesize;
128 uvlinesize = s->uvlinesize;
130 ptr = ref_picture[0];
132 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
133 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
135 s->dsp.gmc(dest_y, ptr, linesize, 16,
138 s->sprite_delta[0][0], s->sprite_delta[0][1],
139 s->sprite_delta[1][0], s->sprite_delta[1][1],
140 a+1, (1<<(2*a+1)) - s->no_rounding,
141 s->h_edge_pos, s->v_edge_pos);
142 s->dsp.gmc(dest_y+8, ptr, linesize, 16,
143 ox + s->sprite_delta[0][0]*8,
144 oy + s->sprite_delta[1][0]*8,
145 s->sprite_delta[0][0], s->sprite_delta[0][1],
146 s->sprite_delta[1][0], s->sprite_delta[1][1],
147 a+1, (1<<(2*a+1)) - s->no_rounding,
148 s->h_edge_pos, s->v_edge_pos);
150 if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
152 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
153 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
155 ptr = ref_picture[1];
156 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
159 s->sprite_delta[0][0], s->sprite_delta[0][1],
160 s->sprite_delta[1][0], s->sprite_delta[1][1],
161 a+1, (1<<(2*a+1)) - s->no_rounding,
162 s->h_edge_pos>>1, s->v_edge_pos>>1);
164 ptr = ref_picture[2];
165 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
168 s->sprite_delta[0][0], s->sprite_delta[0][1],
169 s->sprite_delta[1][0], s->sprite_delta[1][1],
170 a+1, (1<<(2*a+1)) - s->no_rounding,
171 s->h_edge_pos>>1, s->v_edge_pos>>1);
174 static inline int hpel_motion(MpegEncContext *s,
175 uint8_t *dest, uint8_t *src,
176 int src_x, int src_y,
177 op_pixels_func *pix_op,
178 int motion_x, int motion_y)
183 src_x += motion_x >> 1;
184 src_y += motion_y >> 1;
186 /* WARNING: do no forget half pels */
187 src_x = av_clip(src_x, -16, s->width); //FIXME unneeded for emu?
188 if (src_x != s->width)
190 src_y = av_clip(src_y, -16, s->height);
191 if (src_y != s->height)
192 dxy |= (motion_y & 1) << 1;
193 src += src_y * s->linesize + src_x;
195 if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
196 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 8, 0)
197 || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&1) - 8, 0)){
198 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, 9, 9,
199 src_x, src_y, s->h_edge_pos, s->v_edge_pos);
200 src= s->edge_emu_buffer;
204 pix_op[dxy](dest, src, s->linesize, 8);
208 static av_always_inline
209 void mpeg_motion_internal(MpegEncContext *s,
210 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
211 int field_based, int bottom_field, int field_select,
212 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
213 int motion_x, int motion_y, int h, int is_mpeg12, int mb_y)
215 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
216 int dxy, uvdxy, mx, my, src_x, src_y,
217 uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
220 if(s->quarter_sample)
227 v_edge_pos = s->v_edge_pos >> field_based;
228 linesize = s->current_picture.f.linesize[0] << field_based;
229 uvlinesize = s->current_picture.f.linesize[1] << field_based;
231 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
232 src_x = s->mb_x* 16 + (motion_x >> 1);
233 src_y =( mb_y<<(4-field_based)) + (motion_y >> 1);
235 if (!is_mpeg12 && s->out_format == FMT_H263) {
236 if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
237 mx = (motion_x>>1)|(motion_x&1);
239 uvdxy = ((my & 1) << 1) | (mx & 1);
240 uvsrc_x = s->mb_x* 8 + (mx >> 1);
241 uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
243 uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
247 }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
251 uvsrc_x = s->mb_x*8 + mx;
252 uvsrc_y = mb_y*8 + my;
254 if(s->chroma_y_shift){
257 uvdxy = ((my & 1) << 1) | (mx & 1);
258 uvsrc_x = s->mb_x* 8 + (mx >> 1);
259 uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
261 if(s->chroma_x_shift){
264 uvdxy = ((motion_y & 1) << 1) | (mx & 1);
265 uvsrc_x = s->mb_x* 8 + (mx >> 1);
276 ptr_y = ref_picture[0] + src_y * linesize + src_x;
277 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
278 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
280 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
281 || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&1) - h , 0)){
282 if(is_mpeg12 || s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
283 s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
284 av_log(s->avctx,AV_LOG_DEBUG,
285 "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
288 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
290 src_x, src_y<<field_based,
291 s->h_edge_pos, s->v_edge_pos);
292 ptr_y = s->edge_emu_buffer;
293 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
294 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
295 s->vdsp.emulated_edge_mc(uvbuf ,
296 ptr_cb, s->uvlinesize,
298 uvsrc_x, uvsrc_y<<field_based,
299 s->h_edge_pos>>1, s->v_edge_pos>>1);
300 s->vdsp.emulated_edge_mc(uvbuf+16,
301 ptr_cr, s->uvlinesize,
303 uvsrc_x, uvsrc_y<<field_based,
304 s->h_edge_pos>>1, s->v_edge_pos>>1);
310 if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
311 dest_y += s->linesize;
312 dest_cb+= s->uvlinesize;
313 dest_cr+= s->uvlinesize;
317 ptr_y += s->linesize;
318 ptr_cb+= s->uvlinesize;
319 ptr_cr+= s->uvlinesize;
322 pix_op[0][dxy](dest_y, ptr_y, linesize, h);
324 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
325 pix_op[s->chroma_x_shift][uvdxy]
326 (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
327 pix_op[s->chroma_x_shift][uvdxy]
328 (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
330 if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
331 s->out_format == FMT_H261){
332 ff_h261_loop_filter(s);
335 /* apply one mpeg motion vector to the three components */
336 static void mpeg_motion(MpegEncContext *s,
337 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
338 int field_select, uint8_t **ref_picture,
339 op_pixels_func (*pix_op)[4],
340 int motion_x, int motion_y, int h, int mb_y)
343 if(s->out_format == FMT_MPEG1)
344 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
345 field_select, ref_picture, pix_op,
346 motion_x, motion_y, h, 1, mb_y);
349 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
350 field_select, ref_picture, pix_op,
351 motion_x, motion_y, h, 0, mb_y);
354 static void mpeg_motion_field(MpegEncContext *s, uint8_t *dest_y,
355 uint8_t *dest_cb, uint8_t *dest_cr,
356 int bottom_field, int field_select,
357 uint8_t **ref_picture,
358 op_pixels_func (*pix_op)[4],
359 int motion_x, int motion_y, int h, int mb_y)
362 if(s->out_format == FMT_MPEG1)
363 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
364 bottom_field, field_select, ref_picture, pix_op,
365 motion_x, motion_y, h, 1, mb_y);
368 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
369 bottom_field, field_select, ref_picture, pix_op,
370 motion_x, motion_y, h, 0, mb_y);
373 //FIXME move to dsputil, avg variant, 16x16 version
374 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
376 uint8_t * const top = src[1];
377 uint8_t * const left = src[2];
378 uint8_t * const mid = src[0];
379 uint8_t * const right = src[3];
380 uint8_t * const bottom= src[4];
381 #define OBMC_FILTER(x, t, l, m, r, b)\
382 dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
383 #define OBMC_FILTER4(x, t, l, m, r, b)\
384 OBMC_FILTER(x , t, l, m, r, b);\
385 OBMC_FILTER(x+1 , t, l, m, r, b);\
386 OBMC_FILTER(x +stride, t, l, m, r, b);\
387 OBMC_FILTER(x+1+stride, t, l, m, r, b);
390 OBMC_FILTER (x , 2, 2, 4, 0, 0);
391 OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
392 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
393 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
394 OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
395 OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
397 OBMC_FILTER (x , 1, 2, 5, 0, 0);
398 OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
399 OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
400 OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
402 OBMC_FILTER4(x , 1, 2, 5, 0, 0);
403 OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
404 OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
405 OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
407 OBMC_FILTER4(x , 0, 2, 5, 0, 1);
408 OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
409 OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
410 OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
412 OBMC_FILTER (x , 0, 2, 5, 0, 1);
413 OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
414 OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
415 OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
416 OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
417 OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
419 OBMC_FILTER (x , 0, 2, 4, 0, 2);
420 OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
421 OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
422 OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
425 /* obmc for 1 8x8 luma block */
426 static inline void obmc_motion(MpegEncContext *s,
427 uint8_t *dest, uint8_t *src,
428 int src_x, int src_y,
429 op_pixels_func *pix_op,
430 int16_t mv[5][2]/* mid top left right bottom*/)
436 av_assert2(s->quarter_sample==0);
439 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
442 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
443 hpel_motion(s, ptr[i], src,
450 put_obmc(dest, ptr, s->linesize);
453 static inline void qpel_motion(MpegEncContext *s,
454 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
455 int field_based, int bottom_field, int field_select,
456 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
457 qpel_mc_func (*qpix_op)[16],
458 int motion_x, int motion_y, int h)
460 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
461 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
463 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
464 src_x = s->mb_x * 16 + (motion_x >> 2);
465 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
467 v_edge_pos = s->v_edge_pos >> field_based;
468 linesize = s->linesize << field_based;
469 uvlinesize = s->uvlinesize << field_based;
474 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
475 static const int rtab[8]= {0,0,1,1,0,0,0,1};
476 mx= (motion_x>>1) + rtab[motion_x&7];
477 my= (motion_y>>1) + rtab[motion_y&7];
478 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
479 mx= (motion_x>>1)|(motion_x&1);
480 my= (motion_y>>1)|(motion_y&1);
488 uvdxy= (mx&1) | ((my&1)<<1);
492 uvsrc_x = s->mb_x * 8 + mx;
493 uvsrc_y = s->mb_y * (8 >> field_based) + my;
495 ptr_y = ref_picture[0] + src_y * linesize + src_x;
496 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
497 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
499 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
500 || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&3) - h , 0)){
501 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
502 17, 17+field_based, src_x, src_y<<field_based,
503 s->h_edge_pos, s->v_edge_pos);
504 ptr_y= s->edge_emu_buffer;
505 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
506 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
507 s->vdsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
509 uvsrc_x, uvsrc_y<<field_based,
510 s->h_edge_pos>>1, s->v_edge_pos>>1);
511 s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
513 uvsrc_x, uvsrc_y<<field_based,
514 s->h_edge_pos>>1, s->v_edge_pos>>1);
521 qpix_op[0][dxy](dest_y, ptr_y, linesize);
524 dest_y += s->linesize;
525 dest_cb+= s->uvlinesize;
526 dest_cr+= s->uvlinesize;
530 ptr_y += s->linesize;
531 ptr_cb += s->uvlinesize;
532 ptr_cr += s->uvlinesize;
534 //damn interlaced mode
535 //FIXME boundary mirroring is not exactly correct here
536 qpix_op[1][dxy](dest_y , ptr_y , linesize);
537 qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
539 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
540 pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
541 pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
546 * h263 chroma 4mv motion compensation.
548 static void chroma_4mv_motion(MpegEncContext *s,
549 uint8_t *dest_cb, uint8_t *dest_cr,
550 uint8_t **ref_picture,
551 op_pixels_func *pix_op,
554 int dxy, emu=0, src_x, src_y, offset;
557 /* In case of 8X8, we construct a single chroma motion vector
558 with a special rounding */
559 mx= ff_h263_round_chroma(mx);
560 my= ff_h263_round_chroma(my);
562 dxy = ((my & 1) << 1) | (mx & 1);
566 src_x = s->mb_x * 8 + mx;
567 src_y = s->mb_y * 8 + my;
568 src_x = av_clip(src_x, -8, (s->width >> 1));
569 if (src_x == (s->width >> 1))
571 src_y = av_clip(src_y, -8, (s->height >> 1));
572 if (src_y == (s->height >> 1))
575 offset = src_y * s->uvlinesize + src_x;
576 ptr = ref_picture[1] + offset;
577 if(s->flags&CODEC_FLAG_EMU_EDGE){
578 if( (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
579 || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
580 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
582 s->h_edge_pos>>1, s->v_edge_pos>>1);
583 ptr= s->edge_emu_buffer;
587 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
589 ptr = ref_picture[2] + offset;
591 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
593 s->h_edge_pos>>1, s->v_edge_pos>>1);
594 ptr= s->edge_emu_buffer;
596 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
599 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
600 /* fetch pixels for estimated mv 4 macroblocks ahead
601 * optimized for 64byte cache lines */
602 const int shift = s->quarter_sample ? 2 : 1;
603 const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
604 const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
605 int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
606 s->vdsp.prefetch(pix[0]+off, s->linesize, 4);
607 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
608 s->vdsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
612 * motion compensation of a single macroblock
614 * @param dest_y luma destination pointer
615 * @param dest_cb chroma cb/u destination pointer
616 * @param dest_cr chroma cr/v destination pointer
617 * @param dir direction (0->forward, 1->backward)
618 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
619 * @param pix_op halfpel motion compensation function (average or put normally)
620 * @param qpix_op qpel motion compensation function (average or put normally)
621 * the motion vectors are taken from s->mv and the MV type from s->mv_type
623 static av_always_inline void MPV_motion_internal(MpegEncContext *s,
624 uint8_t *dest_y, uint8_t *dest_cb,
625 uint8_t *dest_cr, int dir,
626 uint8_t **ref_picture,
627 op_pixels_func (*pix_op)[4],
628 qpel_mc_func (*qpix_op)[16], int is_mpeg12)
630 int dxy, mx, my, src_x, src_y, motion_x, motion_y;
637 prefetch_motion(s, ref_picture, dir);
639 if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
640 LOCAL_ALIGNED_8(int16_t, mv_cache, [4], [4][2]);
641 Picture *cur_frame = &s->current_picture;
642 const int xy= s->mb_x + s->mb_y*s->mb_stride;
643 const int mot_stride= s->b8_stride;
644 const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
646 av_assert2(!s->mb_skipped);
648 AV_COPY32(mv_cache[1][1], cur_frame->motion_val[0][mot_xy ]);
649 AV_COPY32(mv_cache[1][2], cur_frame->motion_val[0][mot_xy + 1]);
651 AV_COPY32(mv_cache[2][1], cur_frame->motion_val[0][mot_xy + mot_stride ]);
652 AV_COPY32(mv_cache[2][2], cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
654 AV_COPY32(mv_cache[3][1], cur_frame->motion_val[0][mot_xy + mot_stride ]);
655 AV_COPY32(mv_cache[3][2], cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
657 if (mb_y == 0 || IS_INTRA(cur_frame->mb_type[xy - s->mb_stride])) {
658 AV_COPY32(mv_cache[0][1], mv_cache[1][1]);
659 AV_COPY32(mv_cache[0][2], mv_cache[1][2]);
661 AV_COPY32(mv_cache[0][1], cur_frame->motion_val[0][mot_xy - mot_stride ]);
662 AV_COPY32(mv_cache[0][2], cur_frame->motion_val[0][mot_xy - mot_stride + 1]);
665 if (mb_x == 0 || IS_INTRA(cur_frame->mb_type[xy - 1])) {
666 AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
667 AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
669 AV_COPY32(mv_cache[1][0], cur_frame->motion_val[0][mot_xy - 1]);
670 AV_COPY32(mv_cache[2][0], cur_frame->motion_val[0][mot_xy - 1 + mot_stride]);
673 if (mb_x + 1 >= s->mb_width || IS_INTRA(cur_frame->mb_type[xy + 1])) {
674 AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
675 AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
677 AV_COPY32(mv_cache[1][3], cur_frame->motion_val[0][mot_xy + 2]);
678 AV_COPY32(mv_cache[2][3], cur_frame->motion_val[0][mot_xy + 2 + mot_stride]);
684 const int x= (i&1)+1;
685 const int y= (i>>1)+1;
687 {mv_cache[y][x ][0], mv_cache[y][x ][1]},
688 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
689 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
690 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
691 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
693 obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
695 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
702 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
703 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
711 if(s->real_sprite_warping_points==1){
712 gmc1_motion(s, dest_y, dest_cb, dest_cr,
715 gmc_motion(s, dest_y, dest_cb, dest_cr,
718 }else if(!is_mpeg12 && s->quarter_sample){
719 qpel_motion(s, dest_y, dest_cb, dest_cr,
721 ref_picture, pix_op, qpix_op,
722 s->mv[dir][0][0], s->mv[dir][0][1], 16);
723 } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
724 s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
725 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
727 s->mv[dir][0][0], s->mv[dir][0][1], 16);
730 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
732 s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
739 if(s->quarter_sample){
741 motion_x = s->mv[dir][i][0];
742 motion_y = s->mv[dir][i][1];
744 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
745 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
746 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
748 /* WARNING: do no forget half pels */
749 src_x = av_clip(src_x, -16, s->width);
750 if (src_x == s->width)
752 src_y = av_clip(src_y, -16, s->height);
753 if (src_y == s->height)
756 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
757 if(s->flags&CODEC_FLAG_EMU_EDGE){
758 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
759 || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
760 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
763 s->h_edge_pos, s->v_edge_pos);
764 ptr= s->edge_emu_buffer;
767 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
768 qpix_op[1][dxy](dest, ptr, s->linesize);
770 mx += s->mv[dir][i][0]/2;
771 my += s->mv[dir][i][1]/2;
775 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
777 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
779 s->mv[dir][i][0], s->mv[dir][i][1]);
781 mx += s->mv[dir][i][0];
782 my += s->mv[dir][i][1];
786 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
787 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
791 if (s->picture_structure == PICT_FRAME) {
792 if(!is_mpeg12 && s->quarter_sample){
794 qpel_motion(s, dest_y, dest_cb, dest_cr,
795 1, i, s->field_select[dir][i],
796 ref_picture, pix_op, qpix_op,
797 s->mv[dir][i][0], s->mv[dir][i][1], 8);
801 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
802 0, s->field_select[dir][0],
804 s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
806 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
807 1, s->field_select[dir][1],
809 s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
812 if( s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field
814 ref_picture = s->current_picture_ptr->f.data;
817 mpeg_motion(s, dest_y, dest_cb, dest_cr,
818 s->field_select[dir][0],
820 s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
825 uint8_t ** ref2picture;
827 if((s->picture_structure == s->field_select[dir][i] + 1
828 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field) && ref_picture[0]){
829 ref2picture= ref_picture;
831 ref2picture = s->current_picture_ptr->f.data;
834 mpeg_motion(s, dest_y, dest_cb, dest_cr,
835 s->field_select[dir][i],
837 s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
839 dest_y += 16*s->linesize;
840 dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
841 dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
845 if(s->picture_structure == PICT_FRAME){
849 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
850 j, j^i, ref_picture, pix_op,
851 s->mv[dir][2*i + j][0],
852 s->mv[dir][2*i + j][1], 8, mb_y);
854 pix_op = s->hdsp.avg_pixels_tab;
857 if (!ref_picture[0]) {
858 ref_picture = s->current_picture_ptr->f.data;
861 mpeg_motion(s, dest_y, dest_cb, dest_cr,
862 s->picture_structure != i+1,
864 s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
866 // after put we make avg of the same block
867 pix_op=s->hdsp.avg_pixels_tab;
869 //opposite parity is always in the same frame if this is second field
871 ref_picture = s->current_picture_ptr->f.data;
876 default: av_assert2(0);
880 void ff_MPV_motion(MpegEncContext *s,
881 uint8_t *dest_y, uint8_t *dest_cb,
882 uint8_t *dest_cr, int dir,
883 uint8_t **ref_picture,
884 op_pixels_func (*pix_op)[4],
885 qpel_mc_func (*qpix_op)[16])
888 if(s->out_format == FMT_MPEG1)
889 MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
890 ref_picture, pix_op, qpix_op, 1);
893 MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
894 ref_picture, pix_op, qpix_op, 0);