lavc: Convert some remaining strides to ptrdiff_t
[libav.git] / libavcodec / mpegvideo_motion.c
CommitLineData
3ada94ba 1/*
406792e7 2 * Copyright (c) 2000,2001 Fabrice Bellard
3ada94ba
BF
3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4 *
7b94177e
DB
5 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
6 *
2912e87a 7 * This file is part of Libav.
3ada94ba 8 *
2912e87a 9 * Libav is free software; you can redistribute it and/or
3ada94ba
BF
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.
13 *
2912e87a 14 * Libav is distributed in the hope that it will be useful,
3ada94ba
BF
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.
18 *
19 * You should have received a copy of the GNU Lesser General Public
2912e87a 20 * License along with Libav; if not, write to the Free Software
3ada94ba 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3ada94ba
BF
22 */
23
c60208e7 24#include <string.h>
218aefce
DB
25
26#include "libavutil/internal.h"
3ada94ba
BF
27#include "avcodec.h"
28#include "dsputil.h"
66ac3dbf 29#include "h261.h"
3ada94ba
BF
30#include "mpegvideo.h"
31#include "mjpegenc.h"
32#include "msmpeg4.h"
3ada94ba
BF
33#include <limits.h>
34
66f5a6b4
MR
35static void gmc1_motion(MpegEncContext *s,
36 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
37 uint8_t **ref_picture)
3ada94ba
BF
38{
39 uint8_t *ptr;
93f30547
RB
40 int src_x, src_y, motion_x, motion_y;
41 ptrdiff_t offset, linesize, uvlinesize;
3ada94ba
BF
42 int emu=0;
43
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)
52 motion_x =0;
53 src_y = av_clip(src_y, -16, s->height);
54 if (src_y == s->height)
55 motion_y =0;
56
57 linesize = s->linesize;
58 uvlinesize = s->uvlinesize;
59
60 ptr = ref_picture[0] + (src_y * linesize) + src_x;
61
62 if(s->flags&CODEC_FLAG_EMU_EDGE){
9b6aafba
JB
63 if( (unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0)
64 || (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)){
8c53d39e 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);
3ada94ba
BF
66 ptr= s->edge_emu_buffer;
67 }
68 }
69
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);
73 }else{
74 int dxy;
75
76 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
77 if (s->no_rounding){
f4fed5a2 78 s->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
3ada94ba 79 }else{
f4fed5a2 80 s->hdsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
3ada94ba
BF
81 }
82 }
83
49fb20cb 84 if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
3ada94ba
BF
85
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)
94 motion_x =0;
95 src_y = av_clip(src_y, -8, s->height>>1);
96 if (src_y == s->height>>1)
97 motion_y =0;
98
99 offset = (src_y * uvlinesize) + src_x;
100 ptr = ref_picture[1] + offset;
101 if(s->flags&CODEC_FLAG_EMU_EDGE){
9b6aafba
JB
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)){
8c53d39e 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);
3ada94ba
BF
105 ptr= s->edge_emu_buffer;
106 emu=1;
107 }
108 }
109 s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
110
111 ptr = ref_picture[2] + offset;
112 if(emu){
8c53d39e 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);
3ada94ba
BF
114 ptr= s->edge_emu_buffer;
115 }
116 s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
117
118 return;
119}
120
66f5a6b4
MR
121static void gmc_motion(MpegEncContext *s,
122 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
123 uint8_t **ref_picture)
3ada94ba
BF
124{
125 uint8_t *ptr;
126 int linesize, uvlinesize;
127 const int a= s->sprite_warping_accuracy;
128 int ox, oy;
129
130 linesize = s->linesize;
131 uvlinesize = s->uvlinesize;
132
133 ptr = ref_picture[0];
134
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;
137
138 s->dsp.gmc(dest_y, ptr, linesize, 16,
139 ox,
140 oy,
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);
152
49fb20cb 153 if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
3ada94ba
BF
154
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;
157
158 ptr = ref_picture[1];
159 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
160 ox,
161 oy,
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);
166
167 ptr = ref_picture[2];
168 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
169 ox,
170 oy,
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);
175}
176
177static inline int hpel_motion(MpegEncContext *s,
178 uint8_t *dest, uint8_t *src,
3ada94ba 179 int src_x, int src_y,
2568646a 180 op_pixels_func *pix_op,
3ada94ba
BF
181 int motion_x, int motion_y)
182{
5e39bb07 183 int dxy = 0;
3ada94ba
BF
184 int emu=0;
185
3ada94ba
BF
186 src_x += motion_x >> 1;
187 src_y += motion_y >> 1;
188
189 /* WARNING: do no forget half pels */
2568646a 190 src_x = av_clip(src_x, -16, s->width); //FIXME unneeded for emu?
5e39bb07
MR
191 if (src_x != s->width)
192 dxy |= motion_x & 1;
2568646a 193 src_y = av_clip(src_y, -16, s->height);
5e39bb07
MR
194 if (src_y != s->height)
195 dxy |= (motion_y & 1) << 1;
2568646a 196 src += src_y * s->linesize + src_x;
3ada94ba
BF
197
198 if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
2568646a
MR
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)){
8c53d39e 201 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, 9, 9,
2568646a 202 src_x, src_y, s->h_edge_pos, s->v_edge_pos);
3ada94ba
BF
203 src= s->edge_emu_buffer;
204 emu=1;
205 }
206 }
2568646a 207 pix_op[dxy](dest, src, s->linesize, 8);
3ada94ba
BF
208 return emu;
209}
210
08c9bfba 211static av_always_inline
bd7c626a 212void mpeg_motion_internal(MpegEncContext *s,
08c9bfba
KC
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],
078cdecf 216 int motion_x, int motion_y, int h, int is_mpeg12, int mb_y)
3ada94ba
BF
217{
218 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
08c9bfba 219 int dxy, uvdxy, mx, my, src_x, src_y,
a711a2cb
MS
220 uvsrc_x, uvsrc_y, v_edge_pos;
221 ptrdiff_t uvlinesize, linesize;
3ada94ba
BF
222
223#if 0
224if(s->quarter_sample)
225{
226 motion_x>>=1;
227 motion_y>>=1;
228}
229#endif
230
231 v_edge_pos = s->v_edge_pos >> field_based;
657ccb5a
DB
232 linesize = s->current_picture.f.linesize[0] << field_based;
233 uvlinesize = s->current_picture.f.linesize[1] << field_based;
3ada94ba
BF
234
235 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
236 src_x = s->mb_x* 16 + (motion_x >> 1);
078cdecf 237 src_y =( mb_y<<(4-field_based)) + (motion_y >> 1);
3ada94ba 238
bd7c626a 239 if (!is_mpeg12 && s->out_format == FMT_H263) {
3ada94ba
BF
240 if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
241 mx = (motion_x>>1)|(motion_x&1);
242 my = motion_y >>1;
243 uvdxy = ((my & 1) << 1) | (mx & 1);
244 uvsrc_x = s->mb_x* 8 + (mx >> 1);
078cdecf 245 uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
3ada94ba
BF
246 }else{
247 uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
248 uvsrc_x = src_x>>1;
249 uvsrc_y = src_y>>1;
250 }
bd7c626a 251 }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
3ada94ba
BF
252 mx = motion_x / 4;
253 my = motion_y / 4;
254 uvdxy = 0;
255 uvsrc_x = s->mb_x*8 + mx;
078cdecf 256 uvsrc_y = mb_y*8 + my;
3ada94ba
BF
257 } else {
258 if(s->chroma_y_shift){
259 mx = motion_x / 2;
260 my = motion_y / 2;
261 uvdxy = ((my & 1) << 1) | (mx & 1);
262 uvsrc_x = s->mb_x* 8 + (mx >> 1);
078cdecf 263 uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
3ada94ba
BF
264 } else {
265 if(s->chroma_x_shift){
266 //Chroma422
267 mx = motion_x / 2;
268 uvdxy = ((motion_y & 1) << 1) | (mx & 1);
269 uvsrc_x = s->mb_x* 8 + (mx >> 1);
270 uvsrc_y = src_y;
271 } else {
272 //Chroma444
273 uvdxy = dxy;
274 uvsrc_x = src_x;
275 uvsrc_y = src_y;
276 }
277 }
278 }
279
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;
283
9b6aafba
JB
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)){
36ef5369
AK
286 if(is_mpeg12 || s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
287 s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
08c9bfba 288 av_log(s->avctx,AV_LOG_DEBUG,
22d4f213 289 "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
81b44c42 290 return;
3ada94ba 291 }
8c53d39e 292 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
08c9bfba
KC
293 17, 17+field_based,
294 src_x, src_y<<field_based,
295 s->h_edge_pos, s->v_edge_pos);
3ada94ba 296 ptr_y = s->edge_emu_buffer;
49fb20cb 297 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
3ada94ba 298 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
8c53d39e 299 s->vdsp.emulated_edge_mc(uvbuf ,
08c9bfba
KC
300 ptr_cb, s->uvlinesize,
301 9, 9+field_based,
302 uvsrc_x, uvsrc_y<<field_based,
303 s->h_edge_pos>>1, s->v_edge_pos>>1);
8c53d39e 304 s->vdsp.emulated_edge_mc(uvbuf+16,
08c9bfba
KC
305 ptr_cr, s->uvlinesize,
306 9, 9+field_based,
307 uvsrc_x, uvsrc_y<<field_based,
308 s->h_edge_pos>>1, s->v_edge_pos>>1);
3ada94ba
BF
309 ptr_cb= uvbuf;
310 ptr_cr= uvbuf+16;
311 }
312 }
313
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;
318 }
319
320 if(field_select){
321 ptr_y += s->linesize;
322 ptr_cb+= s->uvlinesize;
323 ptr_cr+= s->uvlinesize;
324 }
325
326 pix_op[0][dxy](dest_y, ptr_y, linesize, h);
327
49fb20cb 328 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
08c9bfba
KC
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);
3ada94ba 333 }
49fb20cb 334 if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
08c9bfba 335 s->out_format == FMT_H261){
3ada94ba
BF
336 ff_h261_loop_filter(s);
337 }
338}
bd7c626a 339/* apply one mpeg motion vector to the three components */
f69f4036
MR
340static 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)
bd7c626a 345{
b250f9c6 346#if !CONFIG_SMALL
bd7c626a 347 if(s->out_format == FMT_MPEG1)
f69f4036
MR
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);
351 else
352#endif
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);
356}
357
358static 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)
364{
365#if !CONFIG_SMALL
366 if(s->out_format == FMT_MPEG1)
367 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
bd7c626a 368 bottom_field, field_select, ref_picture, pix_op,
078cdecf 369 motion_x, motion_y, h, 1, mb_y);
bd7c626a
KC
370 else
371#endif
f69f4036 372 mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
bd7c626a 373 bottom_field, field_select, ref_picture, pix_op,
078cdecf 374 motion_x, motion_y, h, 0, mb_y);
bd7c626a 375}
3ada94ba
BF
376
377//FIXME move to dsputil, avg variant, 16x16 version
378static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
379 int x;
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);
392
393 x=0;
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);
400 x+= stride;
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);
405 x+= stride;
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);
410 x+= 2*stride;
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);
415 x+= 2*stride;
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);
422 x+= stride;
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);
427}
428
429/* obmc for 1 8x8 luma block */
430static 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*/)
435#define MID 0
436{
437 int i;
438 uint8_t *ptr[5];
439
440 assert(s->quarter_sample==0);
441
442 for(i=0; i<5; i++){
443 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
444 ptr[i]= ptr[MID];
445 }else{
446 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
2568646a 447 hpel_motion(s, ptr[i], src,
3ada94ba 448 src_x, src_y,
2568646a 449 pix_op,
3ada94ba
BF
450 mv[i][0], mv[i][1]);
451 }
452 }
453
454 put_obmc(dest, ptr, s->linesize);
455}
456
457static 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)
463{
464 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
93f30547
RB
465 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos;
466 ptrdiff_t linesize, uvlinesize;
3ada94ba
BF
467
468 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
469 src_x = s->mb_x * 16 + (motion_x >> 2);
470 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
471
472 v_edge_pos = s->v_edge_pos >> field_based;
473 linesize = s->linesize << field_based;
474 uvlinesize = s->uvlinesize << field_based;
475
476 if(field_based){
477 mx= motion_x/2;
478 my= motion_y>>1;
479 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
480 static const int rtab[8]= {0,0,1,1,0,0,0,1};
481 mx= (motion_x>>1) + rtab[motion_x&7];
482 my= (motion_y>>1) + rtab[motion_y&7];
483 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
484 mx= (motion_x>>1)|(motion_x&1);
485 my= (motion_y>>1)|(motion_y&1);
486 }else{
487 mx= motion_x/2;
488 my= motion_y/2;
489 }
490 mx= (mx>>1)|(mx&1);
491 my= (my>>1)|(my&1);
492
493 uvdxy= (mx&1) | ((my&1)<<1);
494 mx>>=1;
495 my>>=1;
496
497 uvsrc_x = s->mb_x * 8 + mx;
498 uvsrc_y = s->mb_y * (8 >> field_based) + my;
499
500 ptr_y = ref_picture[0] + src_y * linesize + src_x;
501 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
502 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
503
9b6aafba
JB
504 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
505 || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&3) - h , 0)){
8c53d39e 506 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
cb9501f4
KC
507 17, 17+field_based, src_x, src_y<<field_based,
508 s->h_edge_pos, s->v_edge_pos);
3ada94ba 509 ptr_y= s->edge_emu_buffer;
49fb20cb 510 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
3ada94ba 511 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
8c53d39e 512 s->vdsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
cb9501f4
KC
513 9, 9 + field_based,
514 uvsrc_x, uvsrc_y<<field_based,
515 s->h_edge_pos>>1, s->v_edge_pos>>1);
8c53d39e 516 s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
cb9501f4
KC
517 9, 9 + field_based,
518 uvsrc_x, uvsrc_y<<field_based,
519 s->h_edge_pos>>1, s->v_edge_pos>>1);
3ada94ba
BF
520 ptr_cb= uvbuf;
521 ptr_cr= uvbuf + 16;
522 }
523 }
524
525 if(!field_based)
526 qpix_op[0][dxy](dest_y, ptr_y, linesize);
527 else{
528 if(bottom_field){
529 dest_y += s->linesize;
530 dest_cb+= s->uvlinesize;
531 dest_cr+= s->uvlinesize;
532 }
533
534 if(field_select){
535 ptr_y += s->linesize;
536 ptr_cb += s->uvlinesize;
537 ptr_cr += s->uvlinesize;
538 }
539 //damn interlaced mode
540 //FIXME boundary mirroring is not exactly correct here
541 qpix_op[1][dxy](dest_y , ptr_y , linesize);
542 qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
543 }
49fb20cb 544 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
3ada94ba
BF
545 pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
546 pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
547 }
548}
549
550/**
b9ca2042 551 * h263 chroma 4mv motion compensation.
3ada94ba 552 */
66f5a6b4
MR
553static void chroma_4mv_motion(MpegEncContext *s,
554 uint8_t *dest_cb, uint8_t *dest_cr,
555 uint8_t **ref_picture,
556 op_pixels_func *pix_op,
557 int mx, int my)
558{
3ada94ba 559 uint8_t *ptr;
93f30547
RB
560 int src_x, src_y, dxy, emu = 0;
561 ptrdiff_t offset;
3ada94ba
BF
562
563 /* In case of 8X8, we construct a single chroma motion vector
564 with a special rounding */
565 mx= ff_h263_round_chroma(mx);
566 my= ff_h263_round_chroma(my);
567
568 dxy = ((my & 1) << 1) | (mx & 1);
569 mx >>= 1;
570 my >>= 1;
571
572 src_x = s->mb_x * 8 + mx;
573 src_y = s->mb_y * 8 + my;
c31a25e7
JGG
574 src_x = av_clip(src_x, -8, (s->width >> 1));
575 if (src_x == (s->width >> 1))
3ada94ba 576 dxy &= ~1;
c31a25e7
JGG
577 src_y = av_clip(src_y, -8, (s->height >> 1));
578 if (src_y == (s->height >> 1))
3ada94ba
BF
579 dxy &= ~2;
580
ee41963f 581 offset = src_y * s->uvlinesize + src_x;
3ada94ba
BF
582 ptr = ref_picture[1] + offset;
583 if(s->flags&CODEC_FLAG_EMU_EDGE){
9b6aafba
JB
584 if( (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
585 || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
8c53d39e 586 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
cb9501f4
KC
587 9, 9, src_x, src_y,
588 s->h_edge_pos>>1, s->v_edge_pos>>1);
3ada94ba
BF
589 ptr= s->edge_emu_buffer;
590 emu=1;
591 }
592 }
593 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
594
595 ptr = ref_picture[2] + offset;
596 if(emu){
8c53d39e 597 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
cb9501f4
KC
598 9, 9, src_x, src_y,
599 s->h_edge_pos>>1, s->v_edge_pos>>1);
3ada94ba
BF
600 ptr= s->edge_emu_buffer;
601 }
602 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
603}
604
605static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
606 /* fetch pixels for estimated mv 4 macroblocks ahead
607 * optimized for 64byte cache lines */
608 const int shift = s->quarter_sample ? 2 : 1;
609 const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
610 const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
611 int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
8c53d39e 612 s->vdsp.prefetch(pix[0]+off, s->linesize, 4);
3ada94ba 613 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
8c53d39e 614 s->vdsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
3ada94ba
BF
615}
616
617/**
618 * motion compensation of a single macroblock
619 * @param s context
620 * @param dest_y luma destination pointer
621 * @param dest_cb chroma cb/u destination pointer
622 * @param dest_cr chroma cr/v destination pointer
623 * @param dir direction (0->forward, 1->backward)
624 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
9a58234f
DB
625 * @param pix_op halfpel motion compensation function (average or put normally)
626 * @param qpix_op qpel motion compensation function (average or put normally)
3ada94ba
BF
627 * the motion vectors are taken from s->mv and the MV type from s->mv_type
628 */
b1d31e2f 629static av_always_inline void MPV_motion_internal(MpegEncContext *s,
cb9501f4
KC
630 uint8_t *dest_y, uint8_t *dest_cb,
631 uint8_t *dest_cr, int dir,
632 uint8_t **ref_picture,
633 op_pixels_func (*pix_op)[4],
bd7c626a 634 qpel_mc_func (*qpix_op)[16], int is_mpeg12)
3ada94ba
BF
635{
636 int dxy, mx, my, src_x, src_y, motion_x, motion_y;
637 int mb_x, mb_y, i;
638 uint8_t *ptr, *dest;
639
640 mb_x = s->mb_x;
641 mb_y = s->mb_y;
642
643 prefetch_motion(s, ref_picture, dir);
644
975a1447 645 if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
154ff818 646 LOCAL_ALIGNED_8(int16_t, mv_cache, [4], [4][2]);
759001c5 647 Picture *cur_frame = &s->current_picture;
3ada94ba
BF
648 const int xy= s->mb_x + s->mb_y*s->mb_stride;
649 const int mot_stride= s->b8_stride;
650 const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
651
652 assert(!s->mb_skipped);
653
154ff818
JG
654 AV_COPY32(mv_cache[1][1], cur_frame->motion_val[0][mot_xy ]);
655 AV_COPY32(mv_cache[1][2], cur_frame->motion_val[0][mot_xy + 1]);
3ada94ba 656
154ff818
JG
657 AV_COPY32(mv_cache[2][1], cur_frame->motion_val[0][mot_xy + mot_stride ]);
658 AV_COPY32(mv_cache[2][2], cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
659
660 AV_COPY32(mv_cache[3][1], cur_frame->motion_val[0][mot_xy + mot_stride ]);
661 AV_COPY32(mv_cache[3][2], cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
662
663 if (mb_y == 0 || IS_INTRA(cur_frame->mb_type[xy - s->mb_stride])) {
664 AV_COPY32(mv_cache[0][1], mv_cache[1][1]);
665 AV_COPY32(mv_cache[0][2], mv_cache[1][2]);
3ada94ba 666 }else{
154ff818
JG
667 AV_COPY32(mv_cache[0][1], cur_frame->motion_val[0][mot_xy - mot_stride ]);
668 AV_COPY32(mv_cache[0][2], cur_frame->motion_val[0][mot_xy - mot_stride + 1]);
3ada94ba
BF
669 }
670
154ff818 671 if (mb_x == 0 || IS_INTRA(cur_frame->mb_type[xy - 1])) {
99267eb7
EF
672 AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
673 AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
3ada94ba 674 }else{
154ff818
JG
675 AV_COPY32(mv_cache[1][0], cur_frame->motion_val[0][mot_xy - 1]);
676 AV_COPY32(mv_cache[2][0], cur_frame->motion_val[0][mot_xy - 1 + mot_stride]);
3ada94ba
BF
677 }
678
154ff818 679 if (mb_x + 1 >= s->mb_width || IS_INTRA(cur_frame->mb_type[xy + 1])) {
99267eb7
EF
680 AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
681 AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
3ada94ba 682 }else{
154ff818
JG
683 AV_COPY32(mv_cache[1][3], cur_frame->motion_val[0][mot_xy + 2]);
684 AV_COPY32(mv_cache[2][3], cur_frame->motion_val[0][mot_xy + 2 + mot_stride]);
3ada94ba
BF
685 }
686
687 mx = 0;
688 my = 0;
689 for(i=0;i<4;i++) {
690 const int x= (i&1)+1;
691 const int y= (i>>1)+1;
692 int16_t mv[5][2]= {
693 {mv_cache[y][x ][0], mv_cache[y][x ][1]},
694 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
695 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
696 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
697 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
698 //FIXME cleanup
699 obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
700 ref_picture[0],
701 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
702 pix_op[1],
703 mv);
704
705 mx += mv[0][0];
706 my += mv[0][1];
707 }
49fb20cb 708 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
3ada94ba
BF
709 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
710
711 return;
712 }
713
714 switch(s->mv_type) {
715 case MV_TYPE_16X16:
716 if(s->mcsel){
717 if(s->real_sprite_warping_points==1){
718 gmc1_motion(s, dest_y, dest_cb, dest_cr,
719 ref_picture);
720 }else{
721 gmc_motion(s, dest_y, dest_cb, dest_cr,
722 ref_picture);
723 }
bd7c626a 724 }else if(!is_mpeg12 && s->quarter_sample){
3ada94ba
BF
725 qpel_motion(s, dest_y, dest_cb, dest_cr,
726 0, 0, 0,
727 ref_picture, pix_op, qpix_op,
728 s->mv[dir][0][0], s->mv[dir][0][1], 16);
18f2d5cb 729 } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
36ef5369 730 s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
3ada94ba
BF
731 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
732 ref_picture, pix_op,
733 s->mv[dir][0][0], s->mv[dir][0][1], 16);
734 }else
735 {
f69f4036 736 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
3ada94ba 737 ref_picture, pix_op,
078cdecf 738 s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
3ada94ba
BF
739 }
740 break;
741 case MV_TYPE_8X8:
bd7c626a 742 if (!is_mpeg12) {
3ada94ba
BF
743 mx = 0;
744 my = 0;
745 if(s->quarter_sample){
746 for(i=0;i<4;i++) {
747 motion_x = s->mv[dir][i][0];
748 motion_y = s->mv[dir][i][1];
749
750 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
751 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
752 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
753
754 /* WARNING: do no forget half pels */
755 src_x = av_clip(src_x, -16, s->width);
756 if (src_x == s->width)
757 dxy &= ~3;
758 src_y = av_clip(src_y, -16, s->height);
759 if (src_y == s->height)
760 dxy &= ~12;
761
762 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
763 if(s->flags&CODEC_FLAG_EMU_EDGE){
9b6aafba
JB
764 if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
765 || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
8c53d39e 766 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
cb9501f4
KC
767 s->linesize, 9, 9,
768 src_x, src_y,
769 s->h_edge_pos, s->v_edge_pos);
3ada94ba
BF
770 ptr= s->edge_emu_buffer;
771 }
772 }
773 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
774 qpix_op[1][dxy](dest, ptr, s->linesize);
775
776 mx += s->mv[dir][i][0]/2;
777 my += s->mv[dir][i][1]/2;
778 }
779 }else{
780 for(i=0;i<4;i++) {
781 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
2568646a 782 ref_picture[0],
3ada94ba 783 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
2568646a 784 pix_op[1],
3ada94ba
BF
785 s->mv[dir][i][0], s->mv[dir][i][1]);
786
787 mx += s->mv[dir][i][0];
788 my += s->mv[dir][i][1];
789 }
790 }
791
49fb20cb 792 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
3ada94ba 793 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
bd7c626a 794 }
3ada94ba
BF
795 break;
796 case MV_TYPE_FIELD:
797 if (s->picture_structure == PICT_FRAME) {
bd7c626a 798 if(!is_mpeg12 && s->quarter_sample){
3ada94ba
BF
799 for(i=0; i<2; i++){
800 qpel_motion(s, dest_y, dest_cb, dest_cr,
801 1, i, s->field_select[dir][i],
802 ref_picture, pix_op, qpix_op,
803 s->mv[dir][i][0], s->mv[dir][i][1], 8);
804 }
805 }else{
806 /* top field */
f69f4036
MR
807 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
808 0, s->field_select[dir][0],
809 ref_picture, pix_op,
810 s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
3ada94ba 811 /* bottom field */
f69f4036
MR
812 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
813 1, s->field_select[dir][1],
814 ref_picture, pix_op,
815 s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
3ada94ba
BF
816 }
817 } else {
975a1447 818 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
657ccb5a 819 ref_picture = s->current_picture_ptr->f.data;
3ada94ba
BF
820 }
821
822 mpeg_motion(s, dest_y, dest_cb, dest_cr,
f69f4036 823 s->field_select[dir][0],
3ada94ba 824 ref_picture, pix_op,
078cdecf 825 s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
3ada94ba
BF
826 }
827 break;
828 case MV_TYPE_16X8:
829 for(i=0; i<2; i++){
830 uint8_t ** ref2picture;
831
cb9501f4 832 if(s->picture_structure == s->field_select[dir][i] + 1
975a1447 833 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
3ada94ba
BF
834 ref2picture= ref_picture;
835 }else{
657ccb5a 836 ref2picture = s->current_picture_ptr->f.data;
3ada94ba
BF
837 }
838
839 mpeg_motion(s, dest_y, dest_cb, dest_cr,
f69f4036 840 s->field_select[dir][i],
3ada94ba 841 ref2picture, pix_op,
078cdecf 842 s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
3ada94ba
BF
843
844 dest_y += 16*s->linesize;
845 dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
846 dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
847 }
848 break;
849 case MV_TYPE_DMV:
850 if(s->picture_structure == PICT_FRAME){
851 for(i=0; i<2; i++){
852 int j;
853 for(j=0; j<2; j++){
f69f4036
MR
854 mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
855 j, j^i, ref_picture, pix_op,
856 s->mv[dir][2*i + j][0],
857 s->mv[dir][2*i + j][1], 8, mb_y);
3ada94ba 858 }
f4fed5a2 859 pix_op = s->hdsp.avg_pixels_tab;
3ada94ba
BF
860 }
861 }else{
862 for(i=0; i<2; i++){
863 mpeg_motion(s, dest_y, dest_cb, dest_cr,
f69f4036 864 s->picture_structure != i+1,
3ada94ba 865 ref_picture, pix_op,
078cdecf 866 s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
3ada94ba
BF
867
868 // after put we make avg of the same block
f4fed5a2 869 pix_op=s->hdsp.avg_pixels_tab;
3ada94ba
BF
870
871 //opposite parity is always in the same frame if this is second field
872 if(!s->first_field){
657ccb5a 873 ref_picture = s->current_picture_ptr->f.data;
3ada94ba
BF
874 }
875 }
876 }
877 break;
878 default: assert(0);
879 }
880}
881
7a851153
MR
882void ff_MPV_motion(MpegEncContext *s,
883 uint8_t *dest_y, uint8_t *dest_cb,
884 uint8_t *dest_cr, int dir,
885 uint8_t **ref_picture,
886 op_pixels_func (*pix_op)[4],
887 qpel_mc_func (*qpix_op)[16])
bd7c626a 888{
b250f9c6 889#if !CONFIG_SMALL
bd7c626a
KC
890 if(s->out_format == FMT_MPEG1)
891 MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
892 ref_picture, pix_op, qpix_op, 1);
893 else
894#endif
895 MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
896 ref_picture, pix_op, qpix_op, 0);
897}