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 Libav.
9 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #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->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
80 s->hdsp.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;
221 ptrdiff_t uvlinesize, linesize;
224 if(s->quarter_sample)
231 v_edge_pos = s->v_edge_pos >> field_based;
232 linesize = s->current_picture.f.linesize[0] << field_based;
233 uvlinesize = s->current_picture.f.linesize[1] << field_based;
235 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
236 src_x = s->mb_x* 16 + (motion_x >> 1);
237 src_y =( mb_y<<(4-field_based)) + (motion_y >> 1);
239 if (!is_mpeg12 && s->out_format == FMT_H263) {
240 if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
241 mx = (motion_x>>1)|(motion_x&1);
243 uvdxy = ((my & 1) << 1) | (mx & 1);
244 uvsrc_x = s->mb_x* 8 + (mx >> 1);
245 uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
247 uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
251 }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
255 uvsrc_x = s->mb_x*8 + mx;
256 uvsrc_y = mb_y*8 + my;
258 if(s->chroma_y_shift){
261 uvdxy = ((my & 1) << 1) | (mx & 1);
262 uvsrc_x = s->mb_x* 8 + (mx >> 1);
263 uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
265 if(s->chroma_x_shift){
268 uvdxy = ((motion_y & 1) << 1) | (mx & 1);
269 uvsrc_x = s->mb_x* 8 + (mx >> 1);
280 ptr_y = ref_picture[0] + src_y * linesize + src_x;
281 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
282 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
284 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
285 || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&1) - h , 0)){
286 if(is_mpeg12 || s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
287 s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
288 av_log(s->avctx,AV_LOG_DEBUG,
289 "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
292 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
294 src_x, src_y<<field_based,
295 s->h_edge_pos, s->v_edge_pos);
296 ptr_y = s->edge_emu_buffer;
297 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
298 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
299 s->vdsp.emulated_edge_mc(uvbuf ,
300 ptr_cb, s->uvlinesize,
302 uvsrc_x, uvsrc_y<<field_based,
303 s->h_edge_pos>>1, s->v_edge_pos>>1);
304 s->vdsp.emulated_edge_mc(uvbuf+16,
305 ptr_cr, s->uvlinesize,
307 uvsrc_x, uvsrc_y<<field_based,
308 s->h_edge_pos>>1, s->v_edge_pos>>1);
314 if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
315 dest_y += s->linesize;
316 dest_cb+= s->uvlinesize;
317 dest_cr+= s->uvlinesize;
321 ptr_y += s->linesize;
322 ptr_cb+= s->uvlinesize;
323 ptr_cr+= s->uvlinesize;
326 pix_op[0][dxy](dest_y, ptr_y, linesize, h);
328 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
329 pix_op[s->chroma_x_shift][uvdxy]
330 (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
331 pix_op[s->chroma_x_shift][uvdxy]
332 (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
334 if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
335 s->out_format == FMT_H261){
336 ff_h261_loop_filter(s);
339 /* apply one mpeg motion vector to the three components */
340 static void mpeg_motion(MpegEncContext *s,
341 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
342 int field_select, uint8_t **ref_picture,
343 op_pixels_func (*pix_op)[4],
344 int motion_x, int motion_y, int h, int mb_y)
347 if(s->out_format == FMT_MPEG1)
348 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
349 field_select, ref_picture, pix_op,
350 motion_x, motion_y, h, 1, mb_y);
353 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
354 field_select, ref_picture, pix_op,
355 motion_x, motion_y, h, 0, mb_y);
358 static void mpeg_motion_field(MpegEncContext *s, uint8_t *dest_y,
359 uint8_t *dest_cb, uint8_t *dest_cr,
360 int bottom_field, int field_select,
361 uint8_t **ref_picture,
362 op_pixels_func (*pix_op)[4],
363 int motion_x, int motion_y, int h, int mb_y)
366 if(s->out_format == FMT_MPEG1)
367 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
368 bottom_field, field_select, ref_picture, pix_op,
369 motion_x, motion_y, h, 1, mb_y);
372 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
373 bottom_field, field_select, ref_picture, pix_op,
374 motion_x, motion_y, h, 0, mb_y);
377 //FIXME move to dsputil, avg variant, 16x16 version
378 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
380 uint8_t * const top = src[1];
381 uint8_t * const left = src[2];
382 uint8_t * const mid = src[0];
383 uint8_t * const right = src[3];
384 uint8_t * const bottom= src[4];
385 #define OBMC_FILTER(x, t, l, m, r, b)\
386 dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
387 #define OBMC_FILTER4(x, t, l, m, r, b)\
388 OBMC_FILTER(x , t, l, m, r, b);\
389 OBMC_FILTER(x+1 , t, l, m, r, b);\
390 OBMC_FILTER(x +stride, t, l, m, r, b);\
391 OBMC_FILTER(x+1+stride, t, l, m, r, b);
394 OBMC_FILTER (x , 2, 2, 4, 0, 0);
395 OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
396 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
397 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
398 OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
399 OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
401 OBMC_FILTER (x , 1, 2, 5, 0, 0);
402 OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
403 OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
404 OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
406 OBMC_FILTER4(x , 1, 2, 5, 0, 0);
407 OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
408 OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
409 OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
411 OBMC_FILTER4(x , 0, 2, 5, 0, 1);
412 OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
413 OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
414 OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
416 OBMC_FILTER (x , 0, 2, 5, 0, 1);
417 OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
418 OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
419 OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
420 OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
421 OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
423 OBMC_FILTER (x , 0, 2, 4, 0, 2);
424 OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
425 OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
426 OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
429 /* obmc for 1 8x8 luma block */
430 static inline void obmc_motion(MpegEncContext *s,
431 uint8_t *dest, uint8_t *src,
432 int src_x, int src_y,
433 op_pixels_func *pix_op,
434 int16_t mv[5][2]/* mid top left right bottom*/)
440 assert(s->quarter_sample==0);
443 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
446 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
447 hpel_motion(s, ptr[i], src,
454 put_obmc(dest, ptr, s->linesize);
457 static inline void qpel_motion(MpegEncContext *s,
458 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
459 int field_based, int bottom_field, int field_select,
460 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
461 qpel_mc_func (*qpix_op)[16],
462 int motion_x, int motion_y, int h)
464 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
465 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
467 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
468 src_x = s->mb_x * 16 + (motion_x >> 2);
469 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
471 v_edge_pos = s->v_edge_pos >> field_based;
472 linesize = s->linesize << field_based;
473 uvlinesize = s->uvlinesize << field_based;
478 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
479 static const int rtab[8]= {0,0,1,1,0,0,0,1};
480 mx= (motion_x>>1) + rtab[motion_x&7];
481 my= (motion_y>>1) + rtab[motion_y&7];
482 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
483 mx= (motion_x>>1)|(motion_x&1);
484 my= (motion_y>>1)|(motion_y&1);
492 uvdxy= (mx&1) | ((my&1)<<1);
496 uvsrc_x = s->mb_x * 8 + mx;
497 uvsrc_y = s->mb_y * (8 >> field_based) + my;
499 ptr_y = ref_picture[0] + src_y * linesize + src_x;
500 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
501 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
503 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
504 || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&3) - h , 0)){
505 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
506 17, 17+field_based, src_x, src_y<<field_based,
507 s->h_edge_pos, s->v_edge_pos);
508 ptr_y= s->edge_emu_buffer;
509 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
510 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
511 s->vdsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
513 uvsrc_x, uvsrc_y<<field_based,
514 s->h_edge_pos>>1, s->v_edge_pos>>1);
515 s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
517 uvsrc_x, uvsrc_y<<field_based,
518 s->h_edge_pos>>1, s->v_edge_pos>>1);
525 qpix_op[0][dxy](dest_y, ptr_y, linesize);
528 dest_y += s->linesize;
529 dest_cb+= s->uvlinesize;
530 dest_cr+= s->uvlinesize;
534 ptr_y += s->linesize;
535 ptr_cb += s->uvlinesize;
536 ptr_cr += s->uvlinesize;
538 //damn interlaced mode
539 //FIXME boundary mirroring is not exactly correct here
540 qpix_op[1][dxy](dest_y , ptr_y , linesize);
541 qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
543 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
544 pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
545 pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
550 * h263 chroma 4mv motion compensation.
552 static void chroma_4mv_motion(MpegEncContext *s,
553 uint8_t *dest_cb, uint8_t *dest_cr,
554 uint8_t **ref_picture,
555 op_pixels_func *pix_op,
558 int dxy, emu=0, src_x, src_y, offset;
561 /* In case of 8X8, we construct a single chroma motion vector
562 with a special rounding */
563 mx= ff_h263_round_chroma(mx);
564 my= ff_h263_round_chroma(my);
566 dxy = ((my & 1) << 1) | (mx & 1);
570 src_x = s->mb_x * 8 + mx;
571 src_y = s->mb_y * 8 + my;
572 src_x = av_clip(src_x, -8, (s->width >> 1));
573 if (src_x == (s->width >> 1))
575 src_y = av_clip(src_y, -8, (s->height >> 1));
576 if (src_y == (s->height >> 1))
579 offset = src_y * s->uvlinesize + src_x;
580 ptr = ref_picture[1] + offset;
581 if(s->flags&CODEC_FLAG_EMU_EDGE){
582 if( (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
583 || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
584 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
586 s->h_edge_pos>>1, s->v_edge_pos>>1);
587 ptr= s->edge_emu_buffer;
591 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
593 ptr = ref_picture[2] + offset;
595 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
597 s->h_edge_pos>>1, s->v_edge_pos>>1);
598 ptr= s->edge_emu_buffer;
600 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
603 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
604 /* fetch pixels for estimated mv 4 macroblocks ahead
605 * optimized for 64byte cache lines */
606 const int shift = s->quarter_sample ? 2 : 1;
607 const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
608 const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
609 int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
610 s->vdsp.prefetch(pix[0]+off, s->linesize, 4);
611 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
612 s->vdsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
616 * motion compensation of a single macroblock
618 * @param dest_y luma destination pointer
619 * @param dest_cb chroma cb/u destination pointer
620 * @param dest_cr chroma cr/v destination pointer
621 * @param dir direction (0->forward, 1->backward)
622 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
623 * @param pix_op halfpel motion compensation function (average or put normally)
624 * @param qpix_op qpel motion compensation function (average or put normally)
625 * the motion vectors are taken from s->mv and the MV type from s->mv_type
627 static av_always_inline void MPV_motion_internal(MpegEncContext *s,
628 uint8_t *dest_y, uint8_t *dest_cb,
629 uint8_t *dest_cr, int dir,
630 uint8_t **ref_picture,
631 op_pixels_func (*pix_op)[4],
632 qpel_mc_func (*qpix_op)[16], int is_mpeg12)
634 int dxy, mx, my, src_x, src_y, motion_x, motion_y;
641 prefetch_motion(s, ref_picture, dir);
643 if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
644 LOCAL_ALIGNED_8(int16_t, mv_cache, [4], [4][2]);
645 Picture *cur_frame = &s->current_picture;
646 const int xy= s->mb_x + s->mb_y*s->mb_stride;
647 const int mot_stride= s->b8_stride;
648 const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
650 assert(!s->mb_skipped);
652 AV_COPY32(mv_cache[1][1], cur_frame->motion_val[0][mot_xy ]);
653 AV_COPY32(mv_cache[1][2], cur_frame->motion_val[0][mot_xy + 1]);
655 AV_COPY32(mv_cache[2][1], cur_frame->motion_val[0][mot_xy + mot_stride ]);
656 AV_COPY32(mv_cache[2][2], cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
658 AV_COPY32(mv_cache[3][1], cur_frame->motion_val[0][mot_xy + mot_stride ]);
659 AV_COPY32(mv_cache[3][2], cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
661 if (mb_y == 0 || IS_INTRA(cur_frame->mb_type[xy - s->mb_stride])) {
662 AV_COPY32(mv_cache[0][1], mv_cache[1][1]);
663 AV_COPY32(mv_cache[0][2], mv_cache[1][2]);
665 AV_COPY32(mv_cache[0][1], cur_frame->motion_val[0][mot_xy - mot_stride ]);
666 AV_COPY32(mv_cache[0][2], cur_frame->motion_val[0][mot_xy - mot_stride + 1]);
669 if (mb_x == 0 || IS_INTRA(cur_frame->mb_type[xy - 1])) {
670 AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
671 AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
673 AV_COPY32(mv_cache[1][0], cur_frame->motion_val[0][mot_xy - 1]);
674 AV_COPY32(mv_cache[2][0], cur_frame->motion_val[0][mot_xy - 1 + mot_stride]);
677 if (mb_x + 1 >= s->mb_width || IS_INTRA(cur_frame->mb_type[xy + 1])) {
678 AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
679 AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
681 AV_COPY32(mv_cache[1][3], cur_frame->motion_val[0][mot_xy + 2]);
682 AV_COPY32(mv_cache[2][3], cur_frame->motion_val[0][mot_xy + 2 + mot_stride]);
688 const int x= (i&1)+1;
689 const int y= (i>>1)+1;
691 {mv_cache[y][x ][0], mv_cache[y][x ][1]},
692 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
693 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
694 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
695 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
697 obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
699 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
706 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
707 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
715 if(s->real_sprite_warping_points==1){
716 gmc1_motion(s, dest_y, dest_cb, dest_cr,
719 gmc_motion(s, dest_y, dest_cb, dest_cr,
722 }else if(!is_mpeg12 && s->quarter_sample){
723 qpel_motion(s, dest_y, dest_cb, dest_cr,
725 ref_picture, pix_op, qpix_op,
726 s->mv[dir][0][0], s->mv[dir][0][1], 16);
727 } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
728 s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
729 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
731 s->mv[dir][0][0], s->mv[dir][0][1], 16);
734 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
736 s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
743 if(s->quarter_sample){
745 motion_x = s->mv[dir][i][0];
746 motion_y = s->mv[dir][i][1];
748 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
749 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
750 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
752 /* WARNING: do no forget half pels */
753 src_x = av_clip(src_x, -16, s->width);
754 if (src_x == s->width)
756 src_y = av_clip(src_y, -16, s->height);
757 if (src_y == s->height)
760 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
761 if(s->flags&CODEC_FLAG_EMU_EDGE){
762 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
763 || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
764 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
767 s->h_edge_pos, s->v_edge_pos);
768 ptr= s->edge_emu_buffer;
771 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
772 qpix_op[1][dxy](dest, ptr, s->linesize);
774 mx += s->mv[dir][i][0]/2;
775 my += s->mv[dir][i][1]/2;
779 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
781 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
783 s->mv[dir][i][0], s->mv[dir][i][1]);
785 mx += s->mv[dir][i][0];
786 my += s->mv[dir][i][1];
790 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
791 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
795 if (s->picture_structure == PICT_FRAME) {
796 if(!is_mpeg12 && s->quarter_sample){
798 qpel_motion(s, dest_y, dest_cb, dest_cr,
799 1, i, s->field_select[dir][i],
800 ref_picture, pix_op, qpix_op,
801 s->mv[dir][i][0], s->mv[dir][i][1], 8);
805 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
806 0, s->field_select[dir][0],
808 s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
810 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
811 1, s->field_select[dir][1],
813 s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
816 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
817 ref_picture = s->current_picture_ptr->f.data;
820 mpeg_motion(s, dest_y, dest_cb, dest_cr,
821 s->field_select[dir][0],
823 s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
828 uint8_t ** ref2picture;
830 if(s->picture_structure == s->field_select[dir][i] + 1
831 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
832 ref2picture= ref_picture;
834 ref2picture = s->current_picture_ptr->f.data;
837 mpeg_motion(s, dest_y, dest_cb, dest_cr,
838 s->field_select[dir][i],
840 s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
842 dest_y += 16*s->linesize;
843 dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
844 dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
848 if(s->picture_structure == PICT_FRAME){
852 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
853 j, j^i, ref_picture, pix_op,
854 s->mv[dir][2*i + j][0],
855 s->mv[dir][2*i + j][1], 8, mb_y);
857 pix_op = s->hdsp.avg_pixels_tab;
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;
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);