mpegvideo_common.h
Go to the documentation of this file.
1 /*
2  * The simplest mpeg encoder (well, it was the simplest!)
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
30 #ifndef AVCODEC_MPEGVIDEO_COMMON_H
31 #define AVCODEC_MPEGVIDEO_COMMON_H
32 
33 #include <string.h>
34 #include "avcodec.h"
35 #include "dsputil.h"
36 #include "mpegvideo.h"
37 #include "mjpegenc.h"
38 #include "msmpeg4.h"
39 #include "faandct.h"
40 #include <limits.h>
41 
42 int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
43 
48 int alloc_picture(MpegEncContext *s, Picture *pic, int shared);
49 
55 
56 static inline void gmc1_motion(MpegEncContext *s,
57  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
58  uint8_t **ref_picture)
59 {
60  uint8_t *ptr;
61  int offset, src_x, src_y, linesize, uvlinesize;
62  int motion_x, motion_y;
63  int emu=0;
64 
65  motion_x= s->sprite_offset[0][0];
66  motion_y= s->sprite_offset[0][1];
67  src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
68  src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
69  motion_x<<=(3-s->sprite_warping_accuracy);
70  motion_y<<=(3-s->sprite_warping_accuracy);
71  src_x = av_clip(src_x, -16, s->width);
72  if (src_x == s->width)
73  motion_x =0;
74  src_y = av_clip(src_y, -16, s->height);
75  if (src_y == s->height)
76  motion_y =0;
77 
78  linesize = s->linesize;
79  uvlinesize = s->uvlinesize;
80 
81  ptr = ref_picture[0] + (src_y * linesize) + src_x;
82 
84  if( (unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0)
85  || (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)){
86  s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
87  ptr= s->edge_emu_buffer;
88  }
89  }
90 
91  if((motion_x|motion_y)&7){
92  s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
93  s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
94  }else{
95  int dxy;
96 
97  dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
98  if (s->no_rounding){
99  s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
100  }else{
101  s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
102  }
103  }
104 
105  if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
106 
107  motion_x= s->sprite_offset[1][0];
108  motion_y= s->sprite_offset[1][1];
109  src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
110  src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
111  motion_x<<=(3-s->sprite_warping_accuracy);
112  motion_y<<=(3-s->sprite_warping_accuracy);
113  src_x = av_clip(src_x, -8, s->width>>1);
114  if (src_x == s->width>>1)
115  motion_x =0;
116  src_y = av_clip(src_y, -8, s->height>>1);
117  if (src_y == s->height>>1)
118  motion_y =0;
119 
120  offset = (src_y * uvlinesize) + src_x;
121  ptr = ref_picture[1] + offset;
122  if(s->flags&CODEC_FLAG_EMU_EDGE){
123  if( (unsigned)src_x >= FFMAX((s->h_edge_pos>>1) - 9, 0)
124  || (unsigned)src_y >= FFMAX((s->v_edge_pos>>1) - 9, 0)){
125  s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
126  ptr= s->edge_emu_buffer;
127  emu=1;
128  }
129  }
130  s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
131 
132  ptr = ref_picture[2] + offset;
133  if(emu){
134  s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
135  ptr= s->edge_emu_buffer;
136  }
137  s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
138 
139  return;
140 }
141 
142 static inline void gmc_motion(MpegEncContext *s,
143  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
144  uint8_t **ref_picture)
145 {
146  uint8_t *ptr;
147  int linesize, uvlinesize;
148  const int a= s->sprite_warping_accuracy;
149  int ox, oy;
150 
151  linesize = s->linesize;
152  uvlinesize = s->uvlinesize;
153 
154  ptr = ref_picture[0];
155 
156  ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
157  oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
158 
159  s->dsp.gmc(dest_y, ptr, linesize, 16,
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, s->v_edge_pos);
166  s->dsp.gmc(dest_y+8, ptr, linesize, 16,
167  ox + s->sprite_delta[0][0]*8,
168  oy + s->sprite_delta[1][0]*8,
169  s->sprite_delta[0][0], s->sprite_delta[0][1],
170  s->sprite_delta[1][0], s->sprite_delta[1][1],
171  a+1, (1<<(2*a+1)) - s->no_rounding,
172  s->h_edge_pos, s->v_edge_pos);
173 
174  if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
175 
176  ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
177  oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
178 
179  ptr = ref_picture[1];
180  s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
181  ox,
182  oy,
183  s->sprite_delta[0][0], s->sprite_delta[0][1],
184  s->sprite_delta[1][0], s->sprite_delta[1][1],
185  a+1, (1<<(2*a+1)) - s->no_rounding,
186  s->h_edge_pos>>1, s->v_edge_pos>>1);
187 
188  ptr = ref_picture[2];
189  s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
190  ox,
191  oy,
192  s->sprite_delta[0][0], s->sprite_delta[0][1],
193  s->sprite_delta[1][0], s->sprite_delta[1][1],
194  a+1, (1<<(2*a+1)) - s->no_rounding,
195  s->h_edge_pos>>1, s->v_edge_pos>>1);
196 }
197 
198 static inline int hpel_motion(MpegEncContext *s,
199  uint8_t *dest, uint8_t *src,
200  int field_based, int field_select,
201  int src_x, int src_y,
202  int width, int height, int stride,
203  int h_edge_pos, int v_edge_pos,
204  int w, int h, op_pixels_func *pix_op,
205  int motion_x, int motion_y)
206 {
207  int dxy;
208  int emu=0;
209 
210  dxy = ((motion_y & 1) << 1) | (motion_x & 1);
211  src_x += motion_x >> 1;
212  src_y += motion_y >> 1;
213 
214  /* WARNING: do no forget half pels */
215  src_x = av_clip(src_x, -16, width); //FIXME unneeded for emu?
216  if (src_x == width)
217  dxy &= ~1;
218  src_y = av_clip(src_y, -16, height);
219  if (src_y == height)
220  dxy &= ~2;
221  src += src_y * stride + src_x;
222 
224  if( (unsigned)src_x > FFMAX(h_edge_pos - (motion_x&1) - w, 0)
225  || (unsigned)src_y > FFMAX(v_edge_pos - (motion_y&1) - h, 0)){
226  s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
227  src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
228  src= s->edge_emu_buffer;
229  emu=1;
230  }
231  }
232  if(field_select)
233  src += s->linesize;
234  pix_op[dxy](dest, src, stride, h);
235  return emu;
236 }
237 
238 static av_always_inline
240  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
241  int field_based, int bottom_field, int field_select,
242  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
243  int motion_x, int motion_y, int h, int is_mpeg12, int mb_y)
244 {
245  uint8_t *ptr_y, *ptr_cb, *ptr_cr;
246  int dxy, uvdxy, mx, my, src_x, src_y,
247  uvsrc_x, uvsrc_y, v_edge_pos;
248  ptrdiff_t uvlinesize, linesize;
249 
250 #if 0
251 if(s->quarter_sample)
252 {
253  motion_x>>=1;
254  motion_y>>=1;
255 }
256 #endif
257 
258  v_edge_pos = s->v_edge_pos >> field_based;
259  linesize = s->current_picture.f.linesize[0] << field_based;
260  uvlinesize = s->current_picture.f.linesize[1] << field_based;
261 
262  dxy = ((motion_y & 1) << 1) | (motion_x & 1);
263  src_x = s->mb_x* 16 + (motion_x >> 1);
264  src_y =( mb_y<<(4-field_based)) + (motion_y >> 1);
265 
266  if (!is_mpeg12 && s->out_format == FMT_H263) {
267  if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
268  mx = (motion_x>>1)|(motion_x&1);
269  my = motion_y >>1;
270  uvdxy = ((my & 1) << 1) | (mx & 1);
271  uvsrc_x = s->mb_x* 8 + (mx >> 1);
272  uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
273  }else{
274  uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
275  uvsrc_x = src_x>>1;
276  uvsrc_y = src_y>>1;
277  }
278  }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
279  mx = motion_x / 4;
280  my = motion_y / 4;
281  uvdxy = 0;
282  uvsrc_x = s->mb_x*8 + mx;
283  uvsrc_y = mb_y*8 + my;
284  } else {
285  if(s->chroma_y_shift){
286  mx = motion_x / 2;
287  my = motion_y / 2;
288  uvdxy = ((my & 1) << 1) | (mx & 1);
289  uvsrc_x = s->mb_x* 8 + (mx >> 1);
290  uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
291  } else {
292  if(s->chroma_x_shift){
293  //Chroma422
294  mx = motion_x / 2;
295  uvdxy = ((motion_y & 1) << 1) | (mx & 1);
296  uvsrc_x = s->mb_x* 8 + (mx >> 1);
297  uvsrc_y = src_y;
298  } else {
299  //Chroma444
300  uvdxy = dxy;
301  uvsrc_x = src_x;
302  uvsrc_y = src_y;
303  }
304  }
305  }
306 
307  ptr_y = ref_picture[0] + src_y * linesize + src_x;
308  ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
309  ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
310 
311  if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
312  || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&1) - h , 0)){
313  if(is_mpeg12 || s->codec_id == CODEC_ID_MPEG2VIDEO ||
316  "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
317  return;
318  }
319  s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
320  17, 17+field_based,
321  src_x, src_y<<field_based,
322  s->h_edge_pos, s->v_edge_pos);
323  ptr_y = s->edge_emu_buffer;
324  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
325  uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
326  s->dsp.emulated_edge_mc(uvbuf ,
327  ptr_cb, s->uvlinesize,
328  9, 9+field_based,
329  uvsrc_x, uvsrc_y<<field_based,
330  s->h_edge_pos>>1, s->v_edge_pos>>1);
331  s->dsp.emulated_edge_mc(uvbuf+16,
332  ptr_cr, s->uvlinesize,
333  9, 9+field_based,
334  uvsrc_x, uvsrc_y<<field_based,
335  s->h_edge_pos>>1, s->v_edge_pos>>1);
336  ptr_cb= uvbuf;
337  ptr_cr= uvbuf+16;
338  }
339  }
340 
341  if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
342  dest_y += s->linesize;
343  dest_cb+= s->uvlinesize;
344  dest_cr+= s->uvlinesize;
345  }
346 
347  if(field_select){
348  ptr_y += s->linesize;
349  ptr_cb+= s->uvlinesize;
350  ptr_cr+= s->uvlinesize;
351  }
352 
353  pix_op[0][dxy](dest_y, ptr_y, linesize, h);
354 
355  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
356  pix_op[s->chroma_x_shift][uvdxy]
357  (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
358  pix_op[s->chroma_x_shift][uvdxy]
359  (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
360  }
361  if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
362  s->out_format == FMT_H261){
364  }
365 }
366 /* apply one mpeg motion vector to the three components */
367 static av_always_inline
369  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
370  int field_based, int bottom_field, int field_select,
371  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
372  int motion_x, int motion_y, int h, int mb_y)
373 {
374 #if !CONFIG_SMALL
375  if(s->out_format == FMT_MPEG1)
376  mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
377  bottom_field, field_select, ref_picture, pix_op,
378  motion_x, motion_y, h, 1, mb_y);
379  else
380 #endif
381  mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
382  bottom_field, field_select, ref_picture, pix_op,
383  motion_x, motion_y, h, 0, mb_y);
384 }
385 
386 //FIXME move to dsputil, avg variant, 16x16 version
387 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
388  int x;
389  uint8_t * const top = src[1];
390  uint8_t * const left = src[2];
391  uint8_t * const mid = src[0];
392  uint8_t * const right = src[3];
393  uint8_t * const bottom= src[4];
394 #define OBMC_FILTER(x, t, l, m, r, b)\
395  dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
396 #define OBMC_FILTER4(x, t, l, m, r, b)\
397  OBMC_FILTER(x , t, l, m, r, b);\
398  OBMC_FILTER(x+1 , t, l, m, r, b);\
399  OBMC_FILTER(x +stride, t, l, m, r, b);\
400  OBMC_FILTER(x+1+stride, t, l, m, r, b);
401 
402  x=0;
403  OBMC_FILTER (x , 2, 2, 4, 0, 0);
404  OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
405  OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
406  OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
407  OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
408  OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
409  x+= stride;
410  OBMC_FILTER (x , 1, 2, 5, 0, 0);
411  OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
412  OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
413  OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
414  x+= stride;
415  OBMC_FILTER4(x , 1, 2, 5, 0, 0);
416  OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
417  OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
418  OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
419  x+= 2*stride;
420  OBMC_FILTER4(x , 0, 2, 5, 0, 1);
421  OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
422  OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
423  OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
424  x+= 2*stride;
425  OBMC_FILTER (x , 0, 2, 5, 0, 1);
426  OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
427  OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
428  OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
429  OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
430  OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
431  x+= stride;
432  OBMC_FILTER (x , 0, 2, 4, 0, 2);
433  OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
434  OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
435  OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
436 }
437 
438 /* obmc for 1 8x8 luma block */
439 static inline void obmc_motion(MpegEncContext *s,
440  uint8_t *dest, uint8_t *src,
441  int src_x, int src_y,
442  op_pixels_func *pix_op,
443  int16_t mv[5][2]/* mid top left right bottom*/)
444 #define MID 0
445 {
446  int i;
447  uint8_t *ptr[5];
448 
449  assert(s->quarter_sample==0);
450 
451  for(i=0; i<5; i++){
452  if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
453  ptr[i]= ptr[MID];
454  }else{
455  ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
456  hpel_motion(s, ptr[i], src, 0, 0,
457  src_x, src_y,
458  s->width, s->height, s->linesize,
459  s->h_edge_pos, s->v_edge_pos,
460  8, 8, pix_op,
461  mv[i][0], mv[i][1]);
462  }
463  }
464 
465  put_obmc(dest, ptr, s->linesize);
466 }
467 
468 static inline void qpel_motion(MpegEncContext *s,
469  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
470  int field_based, int bottom_field, int field_select,
471  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
472  qpel_mc_func (*qpix_op)[16],
473  int motion_x, int motion_y, int h)
474 {
475  uint8_t *ptr_y, *ptr_cb, *ptr_cr;
476  int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
477 
478  dxy = ((motion_y & 3) << 2) | (motion_x & 3);
479  src_x = s->mb_x * 16 + (motion_x >> 2);
480  src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
481 
482  v_edge_pos = s->v_edge_pos >> field_based;
483  linesize = s->linesize << field_based;
484  uvlinesize = s->uvlinesize << field_based;
485 
486  if(field_based){
487  mx= motion_x/2;
488  my= motion_y>>1;
490  static const int rtab[8]= {0,0,1,1,0,0,0,1};
491  mx= (motion_x>>1) + rtab[motion_x&7];
492  my= (motion_y>>1) + rtab[motion_y&7];
493  }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
494  mx= (motion_x>>1)|(motion_x&1);
495  my= (motion_y>>1)|(motion_y&1);
496  }else{
497  mx= motion_x/2;
498  my= motion_y/2;
499  }
500  mx= (mx>>1)|(mx&1);
501  my= (my>>1)|(my&1);
502 
503  uvdxy= (mx&1) | ((my&1)<<1);
504  mx>>=1;
505  my>>=1;
506 
507  uvsrc_x = s->mb_x * 8 + mx;
508  uvsrc_y = s->mb_y * (8 >> field_based) + my;
509 
510  ptr_y = ref_picture[0] + src_y * linesize + src_x;
511  ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
512  ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
513 
514  if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
515  || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&3) - h , 0)){
516  s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
517  17, 17+field_based, src_x, src_y<<field_based,
518  s->h_edge_pos, s->v_edge_pos);
519  ptr_y= s->edge_emu_buffer;
520  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
521  uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
522  s->dsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
523  9, 9 + field_based,
524  uvsrc_x, uvsrc_y<<field_based,
525  s->h_edge_pos>>1, s->v_edge_pos>>1);
526  s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
527  9, 9 + field_based,
528  uvsrc_x, uvsrc_y<<field_based,
529  s->h_edge_pos>>1, s->v_edge_pos>>1);
530  ptr_cb= uvbuf;
531  ptr_cr= uvbuf + 16;
532  }
533  }
534 
535  if(!field_based)
536  qpix_op[0][dxy](dest_y, ptr_y, linesize);
537  else{
538  if(bottom_field){
539  dest_y += s->linesize;
540  dest_cb+= s->uvlinesize;
541  dest_cr+= s->uvlinesize;
542  }
543 
544  if(field_select){
545  ptr_y += s->linesize;
546  ptr_cb += s->uvlinesize;
547  ptr_cr += s->uvlinesize;
548  }
549  //damn interlaced mode
550  //FIXME boundary mirroring is not exactly correct here
551  qpix_op[1][dxy](dest_y , ptr_y , linesize);
552  qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
553  }
554  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
555  pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
556  pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
557  }
558 }
559 
563 static inline void chroma_4mv_motion(MpegEncContext *s,
564  uint8_t *dest_cb, uint8_t *dest_cr,
565  uint8_t **ref_picture,
566  op_pixels_func *pix_op,
567  int mx, int my){
568  int dxy, emu=0, src_x, src_y, offset;
569  uint8_t *ptr;
570 
571  /* In case of 8X8, we construct a single chroma motion vector
572  with a special rounding */
573  mx= ff_h263_round_chroma(mx);
574  my= ff_h263_round_chroma(my);
575 
576  dxy = ((my & 1) << 1) | (mx & 1);
577  mx >>= 1;
578  my >>= 1;
579 
580  src_x = s->mb_x * 8 + mx;
581  src_y = s->mb_y * 8 + my;
582  src_x = av_clip(src_x, -8, (s->width >> 1));
583  if (src_x == (s->width >> 1))
584  dxy &= ~1;
585  src_y = av_clip(src_y, -8, (s->height >> 1));
586  if (src_y == (s->height >> 1))
587  dxy &= ~2;
588 
589  offset = src_y * s->uvlinesize + src_x;
590  ptr = ref_picture[1] + offset;
591  if(s->flags&CODEC_FLAG_EMU_EDGE){
592  if( (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
593  || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
595  9, 9, src_x, src_y,
596  s->h_edge_pos>>1, s->v_edge_pos>>1);
597  ptr= s->edge_emu_buffer;
598  emu=1;
599  }
600  }
601  pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
602 
603  ptr = ref_picture[2] + offset;
604  if(emu){
606  9, 9, src_x, src_y,
607  s->h_edge_pos>>1, s->v_edge_pos>>1);
608  ptr= s->edge_emu_buffer;
609  }
610  pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
611 }
612 
613 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
614  /* fetch pixels for estimated mv 4 macroblocks ahead
615  * optimized for 64byte cache lines */
616  const int shift = s->quarter_sample ? 2 : 1;
617  const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
618  const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
619  int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
620  s->dsp.prefetch(pix[0]+off, s->linesize, 4);
621  off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
622  s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
623 }
624 
638  uint8_t *dest_y, uint8_t *dest_cb,
639  uint8_t *dest_cr, int dir,
640  uint8_t **ref_picture,
641  op_pixels_func (*pix_op)[4],
642  qpel_mc_func (*qpix_op)[16], int is_mpeg12)
643 {
644  int dxy, mx, my, src_x, src_y, motion_x, motion_y;
645  int mb_x, mb_y, i;
646  uint8_t *ptr, *dest;
647 
648  mb_x = s->mb_x;
649  mb_y = s->mb_y;
650 
651  prefetch_motion(s, ref_picture, dir);
652 
653  if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
654  int16_t mv_cache[4][4][2];
655  const int xy= s->mb_x + s->mb_y*s->mb_stride;
656  const int mot_stride= s->b8_stride;
657  const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
658 
659  assert(!s->mb_skipped);
660 
661  memcpy(mv_cache[1][1], s->current_picture.f.motion_val[0][mot_xy ], sizeof(int16_t) * 4);
662  memcpy(mv_cache[2][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
663  memcpy(mv_cache[3][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
664 
665  if (mb_y == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - s->mb_stride])) {
666  memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
667  }else{
668  memcpy(mv_cache[0][1], s->current_picture.f.motion_val[0][mot_xy - mot_stride], sizeof(int16_t) * 4);
669  }
670 
671  if (mb_x == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - 1])) {
672  AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
673  AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
674  }else{
675  AV_COPY32(mv_cache[1][0], s->current_picture.f.motion_val[0][mot_xy - 1]);
676  AV_COPY32(mv_cache[2][0], s->current_picture.f.motion_val[0][mot_xy - 1 + mot_stride]);
677  }
678 
679  if (mb_x + 1 >= s->mb_width || IS_INTRA(s->current_picture.f.mb_type[xy + 1])) {
680  AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
681  AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
682  }else{
683  AV_COPY32(mv_cache[1][3], s->current_picture.f.motion_val[0][mot_xy + 2]);
684  AV_COPY32(mv_cache[2][3], s->current_picture.f.motion_val[0][mot_xy + 2 + mot_stride]);
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  }
708  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
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  }
724  }else if(!is_mpeg12 && s->quarter_sample){
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);
729  } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
730  s->mspel && s->codec_id == CODEC_ID_WMV2) {
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  {
736  mpeg_motion(s, dest_y, dest_cb, dest_cr,
737  0, 0, 0,
738  ref_picture, pix_op,
739  s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
740  }
741  break;
742  case MV_TYPE_8X8:
743  if (!is_mpeg12) {
744  mx = 0;
745  my = 0;
746  if(s->quarter_sample){
747  for(i=0;i<4;i++) {
748  motion_x = s->mv[dir][i][0];
749  motion_y = s->mv[dir][i][1];
750 
751  dxy = ((motion_y & 3) << 2) | (motion_x & 3);
752  src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
753  src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
754 
755  /* WARNING: do no forget half pels */
756  src_x = av_clip(src_x, -16, s->width);
757  if (src_x == s->width)
758  dxy &= ~3;
759  src_y = av_clip(src_y, -16, s->height);
760  if (src_y == s->height)
761  dxy &= ~12;
762 
763  ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
764  if(s->flags&CODEC_FLAG_EMU_EDGE){
765  if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
766  || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
768  s->linesize, 9, 9,
769  src_x, src_y,
770  s->h_edge_pos, s->v_edge_pos);
771  ptr= s->edge_emu_buffer;
772  }
773  }
774  dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
775  qpix_op[1][dxy](dest, ptr, s->linesize);
776 
777  mx += s->mv[dir][i][0]/2;
778  my += s->mv[dir][i][1]/2;
779  }
780  }else{
781  for(i=0;i<4;i++) {
782  hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
783  ref_picture[0], 0, 0,
784  mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
785  s->width, s->height, s->linesize,
786  s->h_edge_pos, s->v_edge_pos,
787  8, 8, pix_op[1],
788  s->mv[dir][i][0], s->mv[dir][i][1]);
789 
790  mx += s->mv[dir][i][0];
791  my += s->mv[dir][i][1];
792  }
793  }
794 
795  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
796  chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
797  }
798  break;
799  case MV_TYPE_FIELD:
800  if (s->picture_structure == PICT_FRAME) {
801  if(!is_mpeg12 && s->quarter_sample){
802  for(i=0; i<2; i++){
803  qpel_motion(s, dest_y, dest_cb, dest_cr,
804  1, i, s->field_select[dir][i],
805  ref_picture, pix_op, qpix_op,
806  s->mv[dir][i][0], s->mv[dir][i][1], 8);
807  }
808  }else{
809  /* top field */
810  mpeg_motion(s, dest_y, dest_cb, dest_cr,
811  1, 0, s->field_select[dir][0],
812  ref_picture, pix_op,
813  s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
814  /* bottom field */
815  mpeg_motion(s, dest_y, dest_cb, dest_cr,
816  1, 1, s->field_select[dir][1],
817  ref_picture, pix_op,
818  s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
819  }
820  } else {
821  if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
822  ref_picture = s->current_picture_ptr->f.data;
823  }
824 
825  mpeg_motion(s, dest_y, dest_cb, dest_cr,
826  0, 0, s->field_select[dir][0],
827  ref_picture, pix_op,
828  s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
829  }
830  break;
831  case MV_TYPE_16X8:
832  for(i=0; i<2; i++){
833  uint8_t ** ref2picture;
834 
835  if(s->picture_structure == s->field_select[dir][i] + 1
836  || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
837  ref2picture= ref_picture;
838  }else{
839  ref2picture = s->current_picture_ptr->f.data;
840  }
841 
842  mpeg_motion(s, dest_y, dest_cb, dest_cr,
843  0, 0, s->field_select[dir][i],
844  ref2picture, pix_op,
845  s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
846 
847  dest_y += 16*s->linesize;
848  dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
849  dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
850  }
851  break;
852  case MV_TYPE_DMV:
853  if(s->picture_structure == PICT_FRAME){
854  for(i=0; i<2; i++){
855  int j;
856  for(j=0; j<2; j++){
857  mpeg_motion(s, dest_y, dest_cb, dest_cr,
858  1, j, j^i,
859  ref_picture, pix_op,
860  s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8, mb_y);
861  }
862  pix_op = s->dsp.avg_pixels_tab;
863  }
864  }else{
865  for(i=0; i<2; i++){
866  mpeg_motion(s, dest_y, dest_cb, dest_cr,
867  0, 0, s->picture_structure != i+1,
868  ref_picture, pix_op,
869  s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
870 
871  // after put we make avg of the same block
872  pix_op=s->dsp.avg_pixels_tab;
873 
874  //opposite parity is always in the same frame if this is second field
875  if(!s->first_field){
876  ref_picture = s->current_picture_ptr->f.data;
877  }
878  }
879  }
880  break;
881  default: assert(0);
882  }
883 }
884 
885 static inline void MPV_motion(MpegEncContext *s,
886  uint8_t *dest_y, uint8_t *dest_cb,
887  uint8_t *dest_cr, int dir,
888  uint8_t **ref_picture,
889  op_pixels_func (*pix_op)[4],
890  qpel_mc_func (*qpix_op)[16])
891 {
892 #if !CONFIG_SMALL
893  if(s->out_format == FMT_MPEG1)
894  MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
895  ref_picture, pix_op, qpix_op, 1);
896  else
897 #endif
898  MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
899  ref_picture, pix_op, qpix_op, 0);
900 }
901 #endif /* AVCODEC_MPEGVIDEO_COMMON_H */