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 
223  if( (unsigned)src_x > FFMAX(h_edge_pos - (motion_x&1) - w, 0)
224  || (unsigned)src_y > FFMAX(v_edge_pos - (motion_y&1) - h, 0)){
225  s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
226  src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
227  src= s->edge_emu_buffer;
228  emu=1;
229  }
230  if(field_select)
231  src += s->linesize;
232  pix_op[dxy](dest, src, stride, h);
233  return emu;
234 }
235 
236 static av_always_inline
238  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
239  int field_based, int bottom_field, int field_select,
240  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
241  int motion_x, int motion_y, int h, int is_mpeg12, int mb_y)
242 {
243  uint8_t *ptr_y, *ptr_cb, *ptr_cr;
244  int dxy, uvdxy, mx, my, src_x, src_y,
245  uvsrc_x, uvsrc_y, v_edge_pos;
246  ptrdiff_t uvlinesize, linesize;
247 
248 #if 0
249 if(s->quarter_sample)
250 {
251  motion_x>>=1;
252  motion_y>>=1;
253 }
254 #endif
255 
256  v_edge_pos = s->v_edge_pos >> field_based;
257  linesize = s->current_picture.f.linesize[0] << field_based;
258  uvlinesize = s->current_picture.f.linesize[1] << field_based;
259 
260  dxy = ((motion_y & 1) << 1) | (motion_x & 1);
261  src_x = s->mb_x* 16 + (motion_x >> 1);
262  src_y =( mb_y<<(4-field_based)) + (motion_y >> 1);
263 
264  if (!is_mpeg12 && s->out_format == FMT_H263) {
265  if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
266  mx = (motion_x>>1)|(motion_x&1);
267  my = motion_y >>1;
268  uvdxy = ((my & 1) << 1) | (mx & 1);
269  uvsrc_x = s->mb_x* 8 + (mx >> 1);
270  uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
271  }else{
272  uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
273  uvsrc_x = src_x>>1;
274  uvsrc_y = src_y>>1;
275  }
276  }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
277  mx = motion_x / 4;
278  my = motion_y / 4;
279  uvdxy = 0;
280  uvsrc_x = s->mb_x*8 + mx;
281  uvsrc_y = mb_y*8 + my;
282  } else {
283  if(s->chroma_y_shift){
284  mx = motion_x / 2;
285  my = motion_y / 2;
286  uvdxy = ((my & 1) << 1) | (mx & 1);
287  uvsrc_x = s->mb_x* 8 + (mx >> 1);
288  uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
289  } else {
290  if(s->chroma_x_shift){
291  //Chroma422
292  mx = motion_x / 2;
293  uvdxy = ((motion_y & 1) << 1) | (mx & 1);
294  uvsrc_x = s->mb_x* 8 + (mx >> 1);
295  uvsrc_y = src_y;
296  } else {
297  //Chroma444
298  uvdxy = dxy;
299  uvsrc_x = src_x;
300  uvsrc_y = src_y;
301  }
302  }
303  }
304 
305  ptr_y = ref_picture[0] + src_y * linesize + src_x;
306  ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
307  ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
308 
309  if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
310  || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&1) - h , 0)){
311  if(is_mpeg12 || s->codec_id == CODEC_ID_MPEG2VIDEO ||
314  "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
315  return;
316  }
317  s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
318  17, 17+field_based,
319  src_x, src_y<<field_based,
320  s->h_edge_pos, s->v_edge_pos);
321  ptr_y = s->edge_emu_buffer;
322  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
323  uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
324  s->dsp.emulated_edge_mc(uvbuf ,
325  ptr_cb, s->uvlinesize,
326  9, 9+field_based,
327  uvsrc_x, uvsrc_y<<field_based,
328  s->h_edge_pos>>1, s->v_edge_pos>>1);
329  s->dsp.emulated_edge_mc(uvbuf+16,
330  ptr_cr, s->uvlinesize,
331  9, 9+field_based,
332  uvsrc_x, uvsrc_y<<field_based,
333  s->h_edge_pos>>1, s->v_edge_pos>>1);
334  ptr_cb= uvbuf;
335  ptr_cr= uvbuf+16;
336  }
337  }
338 
339  if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
340  dest_y += s->linesize;
341  dest_cb+= s->uvlinesize;
342  dest_cr+= s->uvlinesize;
343  }
344 
345  if(field_select){
346  ptr_y += s->linesize;
347  ptr_cb+= s->uvlinesize;
348  ptr_cr+= s->uvlinesize;
349  }
350 
351  pix_op[0][dxy](dest_y, ptr_y, linesize, h);
352 
353  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
354  pix_op[s->chroma_x_shift][uvdxy]
355  (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
356  pix_op[s->chroma_x_shift][uvdxy]
357  (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
358  }
359  if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
360  s->out_format == FMT_H261){
362  }
363 }
364 /* apply one mpeg motion vector to the three components */
365 static av_always_inline
367  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
368  int field_based, int bottom_field, int field_select,
369  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
370  int motion_x, int motion_y, int h, int mb_y)
371 {
372 #if !CONFIG_SMALL
373  if(s->out_format == FMT_MPEG1)
374  mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
375  bottom_field, field_select, ref_picture, pix_op,
376  motion_x, motion_y, h, 1, mb_y);
377  else
378 #endif
379  mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
380  bottom_field, field_select, ref_picture, pix_op,
381  motion_x, motion_y, h, 0, mb_y);
382 }
383 
384 //FIXME move to dsputil, avg variant, 16x16 version
385 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
386  int x;
387  uint8_t * const top = src[1];
388  uint8_t * const left = src[2];
389  uint8_t * const mid = src[0];
390  uint8_t * const right = src[3];
391  uint8_t * const bottom= src[4];
392 #define OBMC_FILTER(x, t, l, m, r, b)\
393  dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
394 #define OBMC_FILTER4(x, t, l, m, r, b)\
395  OBMC_FILTER(x , t, l, m, r, b);\
396  OBMC_FILTER(x+1 , t, l, m, r, b);\
397  OBMC_FILTER(x +stride, t, l, m, r, b);\
398  OBMC_FILTER(x+1+stride, t, l, m, r, b);
399 
400  x=0;
401  OBMC_FILTER (x , 2, 2, 4, 0, 0);
402  OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
403  OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
404  OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
405  OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
406  OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
407  x+= stride;
408  OBMC_FILTER (x , 1, 2, 5, 0, 0);
409  OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
410  OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
411  OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
412  x+= stride;
413  OBMC_FILTER4(x , 1, 2, 5, 0, 0);
414  OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
415  OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
416  OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
417  x+= 2*stride;
418  OBMC_FILTER4(x , 0, 2, 5, 0, 1);
419  OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
420  OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
421  OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
422  x+= 2*stride;
423  OBMC_FILTER (x , 0, 2, 5, 0, 1);
424  OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
425  OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
426  OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
427  OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
428  OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
429  x+= stride;
430  OBMC_FILTER (x , 0, 2, 4, 0, 2);
431  OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
432  OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
433  OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
434 }
435 
436 /* obmc for 1 8x8 luma block */
437 static inline void obmc_motion(MpegEncContext *s,
438  uint8_t *dest, uint8_t *src,
439  int src_x, int src_y,
440  op_pixels_func *pix_op,
441  int16_t mv[5][2]/* mid top left right bottom*/)
442 #define MID 0
443 {
444  int i;
445  uint8_t *ptr[5];
446 
447  assert(s->quarter_sample==0);
448 
449  for(i=0; i<5; i++){
450  if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
451  ptr[i]= ptr[MID];
452  }else{
453  ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
454  hpel_motion(s, ptr[i], src, 0, 0,
455  src_x, src_y,
456  s->width, s->height, s->linesize,
457  s->h_edge_pos, s->v_edge_pos,
458  8, 8, pix_op,
459  mv[i][0], mv[i][1]);
460  }
461  }
462 
463  put_obmc(dest, ptr, s->linesize);
464 }
465 
466 static inline void qpel_motion(MpegEncContext *s,
467  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
468  int field_based, int bottom_field, int field_select,
469  uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
470  qpel_mc_func (*qpix_op)[16],
471  int motion_x, int motion_y, int h)
472 {
473  uint8_t *ptr_y, *ptr_cb, *ptr_cr;
474  int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
475 
476  dxy = ((motion_y & 3) << 2) | (motion_x & 3);
477  src_x = s->mb_x * 16 + (motion_x >> 2);
478  src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
479 
480  v_edge_pos = s->v_edge_pos >> field_based;
481  linesize = s->linesize << field_based;
482  uvlinesize = s->uvlinesize << field_based;
483 
484  if(field_based){
485  mx= motion_x/2;
486  my= motion_y>>1;
488  static const int rtab[8]= {0,0,1,1,0,0,0,1};
489  mx= (motion_x>>1) + rtab[motion_x&7];
490  my= (motion_y>>1) + rtab[motion_y&7];
491  }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
492  mx= (motion_x>>1)|(motion_x&1);
493  my= (motion_y>>1)|(motion_y&1);
494  }else{
495  mx= motion_x/2;
496  my= motion_y/2;
497  }
498  mx= (mx>>1)|(mx&1);
499  my= (my>>1)|(my&1);
500 
501  uvdxy= (mx&1) | ((my&1)<<1);
502  mx>>=1;
503  my>>=1;
504 
505  uvsrc_x = s->mb_x * 8 + mx;
506  uvsrc_y = s->mb_y * (8 >> field_based) + my;
507 
508  ptr_y = ref_picture[0] + src_y * linesize + src_x;
509  ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
510  ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
511 
512  if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
513  || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&3) - h , 0)){
514  s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
515  17, 17+field_based, src_x, src_y<<field_based,
516  s->h_edge_pos, s->v_edge_pos);
517  ptr_y= s->edge_emu_buffer;
518  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
519  uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
520  s->dsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
521  9, 9 + field_based,
522  uvsrc_x, uvsrc_y<<field_based,
523  s->h_edge_pos>>1, s->v_edge_pos>>1);
524  s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
525  9, 9 + field_based,
526  uvsrc_x, uvsrc_y<<field_based,
527  s->h_edge_pos>>1, s->v_edge_pos>>1);
528  ptr_cb= uvbuf;
529  ptr_cr= uvbuf + 16;
530  }
531  }
532 
533  if(!field_based)
534  qpix_op[0][dxy](dest_y, ptr_y, linesize);
535  else{
536  if(bottom_field){
537  dest_y += s->linesize;
538  dest_cb+= s->uvlinesize;
539  dest_cr+= s->uvlinesize;
540  }
541 
542  if(field_select){
543  ptr_y += s->linesize;
544  ptr_cb += s->uvlinesize;
545  ptr_cr += s->uvlinesize;
546  }
547  //damn interlaced mode
548  //FIXME boundary mirroring is not exactly correct here
549  qpix_op[1][dxy](dest_y , ptr_y , linesize);
550  qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
551  }
552  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
553  pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
554  pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
555  }
556 }
557 
561 static inline void chroma_4mv_motion(MpegEncContext *s,
562  uint8_t *dest_cb, uint8_t *dest_cr,
563  uint8_t **ref_picture,
564  op_pixels_func *pix_op,
565  int mx, int my){
566  int dxy, emu=0, src_x, src_y, offset;
567  uint8_t *ptr;
568 
569  /* In case of 8X8, we construct a single chroma motion vector
570  with a special rounding */
571  mx= ff_h263_round_chroma(mx);
572  my= ff_h263_round_chroma(my);
573 
574  dxy = ((my & 1) << 1) | (mx & 1);
575  mx >>= 1;
576  my >>= 1;
577 
578  src_x = s->mb_x * 8 + mx;
579  src_y = s->mb_y * 8 + my;
580  src_x = av_clip(src_x, -8, (s->width >> 1));
581  if (src_x == (s->width >> 1))
582  dxy &= ~1;
583  src_y = av_clip(src_y, -8, (s->height >> 1));
584  if (src_y == (s->height >> 1))
585  dxy &= ~2;
586 
587  offset = src_y * s->uvlinesize + src_x;
588  ptr = ref_picture[1] + offset;
589  if(s->flags&CODEC_FLAG_EMU_EDGE){
590  if( (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
591  || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
593  9, 9, src_x, src_y,
594  s->h_edge_pos>>1, s->v_edge_pos>>1);
595  ptr= s->edge_emu_buffer;
596  emu=1;
597  }
598  }
599  pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
600 
601  ptr = ref_picture[2] + offset;
602  if(emu){
604  9, 9, src_x, src_y,
605  s->h_edge_pos>>1, s->v_edge_pos>>1);
606  ptr= s->edge_emu_buffer;
607  }
608  pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
609 }
610 
611 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
612  /* fetch pixels for estimated mv 4 macroblocks ahead
613  * optimized for 64byte cache lines */
614  const int shift = s->quarter_sample ? 2 : 1;
615  const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
616  const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
617  int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
618  s->dsp.prefetch(pix[0]+off, s->linesize, 4);
619  off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
620  s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
621 }
622 
636  uint8_t *dest_y, uint8_t *dest_cb,
637  uint8_t *dest_cr, int dir,
638  uint8_t **ref_picture,
639  op_pixels_func (*pix_op)[4],
640  qpel_mc_func (*qpix_op)[16], int is_mpeg12)
641 {
642  int dxy, mx, my, src_x, src_y, motion_x, motion_y;
643  int mb_x, mb_y, i;
644  uint8_t *ptr, *dest;
645 
646  mb_x = s->mb_x;
647  mb_y = s->mb_y;
648 
649  prefetch_motion(s, ref_picture, dir);
650 
651  if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
652  int16_t mv_cache[4][4][2];
653  const int xy= s->mb_x + s->mb_y*s->mb_stride;
654  const int mot_stride= s->b8_stride;
655  const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
656 
657  assert(!s->mb_skipped);
658 
659  memcpy(mv_cache[1][1], s->current_picture.f.motion_val[0][mot_xy ], sizeof(int16_t) * 4);
660  memcpy(mv_cache[2][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
661  memcpy(mv_cache[3][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
662 
663  if (mb_y == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - s->mb_stride])) {
664  memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
665  }else{
666  memcpy(mv_cache[0][1], s->current_picture.f.motion_val[0][mot_xy - mot_stride], sizeof(int16_t) * 4);
667  }
668 
669  if (mb_x == 0 || IS_INTRA(s->current_picture.f.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]);
672  }else{
673  AV_COPY32(mv_cache[1][0], s->current_picture.f.motion_val[0][mot_xy - 1]);
674  AV_COPY32(mv_cache[2][0], s->current_picture.f.motion_val[0][mot_xy - 1 + mot_stride]);
675  }
676 
677  if (mb_x + 1 >= s->mb_width || IS_INTRA(s->current_picture.f.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]);
680  }else{
681  AV_COPY32(mv_cache[1][3], s->current_picture.f.motion_val[0][mot_xy + 2]);
682  AV_COPY32(mv_cache[2][3], s->current_picture.f.motion_val[0][mot_xy + 2 + mot_stride]);
683  }
684 
685  mx = 0;
686  my = 0;
687  for(i=0;i<4;i++) {
688  const int x= (i&1)+1;
689  const int y= (i>>1)+1;
690  int16_t mv[5][2]= {
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]}};
696  //FIXME cleanup
697  obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
698  ref_picture[0],
699  mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
700  pix_op[1],
701  mv);
702 
703  mx += mv[0][0];
704  my += mv[0][1];
705  }
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);
708 
709  return;
710  }
711 
712  switch(s->mv_type) {
713  case MV_TYPE_16X16:
714  if(s->mcsel){
715  if(s->real_sprite_warping_points==1){
716  gmc1_motion(s, dest_y, dest_cb, dest_cr,
717  ref_picture);
718  }else{
719  gmc_motion(s, dest_y, dest_cb, dest_cr,
720  ref_picture);
721  }
722  }else if(!is_mpeg12 && s->quarter_sample){
723  qpel_motion(s, dest_y, dest_cb, dest_cr,
724  0, 0, 0,
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 == CODEC_ID_WMV2) {
729  ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
730  ref_picture, pix_op,
731  s->mv[dir][0][0], s->mv[dir][0][1], 16);
732  }else
733  {
734  mpeg_motion(s, dest_y, dest_cb, dest_cr,
735  0, 0, 0,
736  ref_picture, pix_op,
737  s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
738  }
739  break;
740  case MV_TYPE_8X8:
741  if (!is_mpeg12) {
742  mx = 0;
743  my = 0;
744  if(s->quarter_sample){
745  for(i=0;i<4;i++) {
746  motion_x = s->mv[dir][i][0];
747  motion_y = s->mv[dir][i][1];
748 
749  dxy = ((motion_y & 3) << 2) | (motion_x & 3);
750  src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
751  src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
752 
753  /* WARNING: do no forget half pels */
754  src_x = av_clip(src_x, -16, s->width);
755  if (src_x == s->width)
756  dxy &= ~3;
757  src_y = av_clip(src_y, -16, s->height);
758  if (src_y == s->height)
759  dxy &= ~12;
760 
761  ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
762  if(s->flags&CODEC_FLAG_EMU_EDGE){
763  if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
764  || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
766  s->linesize, 9, 9,
767  src_x, src_y,
768  s->h_edge_pos, s->v_edge_pos);
769  ptr= s->edge_emu_buffer;
770  }
771  }
772  dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
773  qpix_op[1][dxy](dest, ptr, s->linesize);
774 
775  mx += s->mv[dir][i][0]/2;
776  my += s->mv[dir][i][1]/2;
777  }
778  }else{
779  for(i=0;i<4;i++) {
780  hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
781  ref_picture[0], 0, 0,
782  mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
783  s->width, s->height, s->linesize,
784  s->h_edge_pos, s->v_edge_pos,
785  8, 8, pix_op[1],
786  s->mv[dir][i][0], s->mv[dir][i][1]);
787 
788  mx += s->mv[dir][i][0];
789  my += s->mv[dir][i][1];
790  }
791  }
792 
793  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
794  chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
795  }
796  break;
797  case MV_TYPE_FIELD:
798  if (s->picture_structure == PICT_FRAME) {
799  if(!is_mpeg12 && s->quarter_sample){
800  for(i=0; i<2; i++){
801  qpel_motion(s, dest_y, dest_cb, dest_cr,
802  1, i, s->field_select[dir][i],
803  ref_picture, pix_op, qpix_op,
804  s->mv[dir][i][0], s->mv[dir][i][1], 8);
805  }
806  }else{
807  /* top field */
808  mpeg_motion(s, dest_y, dest_cb, dest_cr,
809  1, 0, s->field_select[dir][0],
810  ref_picture, pix_op,
811  s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
812  /* bottom field */
813  mpeg_motion(s, dest_y, dest_cb, dest_cr,
814  1, 1, s->field_select[dir][1],
815  ref_picture, pix_op,
816  s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
817  }
818  } else {
819  if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
820  ref_picture = s->current_picture_ptr->f.data;
821  }
822 
823  mpeg_motion(s, dest_y, dest_cb, dest_cr,
824  0, 0, s->field_select[dir][0],
825  ref_picture, pix_op,
826  s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
827  }
828  break;
829  case MV_TYPE_16X8:
830  for(i=0; i<2; i++){
831  uint8_t ** ref2picture;
832 
833  if(s->picture_structure == s->field_select[dir][i] + 1
834  || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
835  ref2picture= ref_picture;
836  }else{
837  ref2picture = s->current_picture_ptr->f.data;
838  }
839 
840  mpeg_motion(s, dest_y, dest_cb, dest_cr,
841  0, 0, s->field_select[dir][i],
842  ref2picture, pix_op,
843  s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
844 
845  dest_y += 16*s->linesize;
846  dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
847  dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
848  }
849  break;
850  case MV_TYPE_DMV:
851  if(s->picture_structure == PICT_FRAME){
852  for(i=0; i<2; i++){
853  int j;
854  for(j=0; j<2; j++){
855  mpeg_motion(s, dest_y, dest_cb, dest_cr,
856  1, j, j^i,
857  ref_picture, pix_op,
858  s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8, mb_y);
859  }
860  pix_op = s->dsp.avg_pixels_tab;
861  }
862  }else{
863  for(i=0; i<2; i++){
864  mpeg_motion(s, dest_y, dest_cb, dest_cr,
865  0, 0, s->picture_structure != i+1,
866  ref_picture, pix_op,
867  s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
868 
869  // after put we make avg of the same block
870  pix_op=s->dsp.avg_pixels_tab;
871 
872  //opposite parity is always in the same frame if this is second field
873  if(!s->first_field){
874  ref_picture = s->current_picture_ptr->f.data;
875  }
876  }
877  }
878  break;
879  default: assert(0);
880  }
881 }
882 
883 static inline void MPV_motion(MpegEncContext *s,
884  uint8_t *dest_y, uint8_t *dest_cb,
885  uint8_t *dest_cr, int dir,
886  uint8_t **ref_picture,
887  op_pixels_func (*pix_op)[4],
888  qpel_mc_func (*qpix_op)[16])
889 {
890 #if !CONFIG_SMALL
891  if(s->out_format == FMT_MPEG1)
892  MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
893  ref_picture, pix_op, qpix_op, 1);
894  else
895 #endif
896  MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
897  ref_picture, pix_op, qpix_op, 0);
898 }
899 #endif /* AVCODEC_MPEGVIDEO_COMMON_H */