You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

866 lines
32KB

  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 FFmpeg.
  9. *
  10. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file mpegvideo_common.h
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #ifndef FFMPEG_MPEGVIDEO_COMMON_H
  29. #define FFMPEG_MPEGVIDEO_COMMON_H
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "mpegvideo.h"
  33. #include "mjpegenc.h"
  34. #include "msmpeg4.h"
  35. #include "faandct.h"
  36. #include <limits.h>
  37. int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  38. int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  39. void denoise_dct_c(MpegEncContext *s, DCTELEM *block);
  40. void copy_picture(Picture *dst, Picture *src);
  41. /**
  42. * allocates a Picture
  43. * The pixels are allocated/set by calling get_buffer() if shared=0
  44. */
  45. int alloc_picture(MpegEncContext *s, Picture *pic, int shared);
  46. /**
  47. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  48. * the changed fields will not depend upon the prior state of the MpegEncContext.
  49. */
  50. void MPV_common_defaults(MpegEncContext *s);
  51. static inline void gmc1_motion(MpegEncContext *s,
  52. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  53. uint8_t **ref_picture)
  54. {
  55. uint8_t *ptr;
  56. int offset, src_x, src_y, linesize, uvlinesize;
  57. int motion_x, motion_y;
  58. int emu=0;
  59. motion_x= s->sprite_offset[0][0];
  60. motion_y= s->sprite_offset[0][1];
  61. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  62. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  63. motion_x<<=(3-s->sprite_warping_accuracy);
  64. motion_y<<=(3-s->sprite_warping_accuracy);
  65. src_x = av_clip(src_x, -16, s->width);
  66. if (src_x == s->width)
  67. motion_x =0;
  68. src_y = av_clip(src_y, -16, s->height);
  69. if (src_y == s->height)
  70. motion_y =0;
  71. linesize = s->linesize;
  72. uvlinesize = s->uvlinesize;
  73. ptr = ref_picture[0] + (src_y * linesize) + src_x;
  74. if(s->flags&CODEC_FLAG_EMU_EDGE){
  75. if( (unsigned)src_x >= s->h_edge_pos - 17
  76. || (unsigned)src_y >= s->v_edge_pos - 17){
  77. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  78. ptr= s->edge_emu_buffer;
  79. }
  80. }
  81. if((motion_x|motion_y)&7){
  82. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  83. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  84. }else{
  85. int dxy;
  86. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  87. if (s->no_rounding){
  88. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  89. }else{
  90. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  91. }
  92. }
  93. if(ENABLE_GRAY && s->flags&CODEC_FLAG_GRAY) return;
  94. motion_x= s->sprite_offset[1][0];
  95. motion_y= s->sprite_offset[1][1];
  96. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  97. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  98. motion_x<<=(3-s->sprite_warping_accuracy);
  99. motion_y<<=(3-s->sprite_warping_accuracy);
  100. src_x = av_clip(src_x, -8, s->width>>1);
  101. if (src_x == s->width>>1)
  102. motion_x =0;
  103. src_y = av_clip(src_y, -8, s->height>>1);
  104. if (src_y == s->height>>1)
  105. motion_y =0;
  106. offset = (src_y * uvlinesize) + src_x;
  107. ptr = ref_picture[1] + offset;
  108. if(s->flags&CODEC_FLAG_EMU_EDGE){
  109. if( (unsigned)src_x >= (s->h_edge_pos>>1) - 9
  110. || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
  111. ff_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);
  112. ptr= s->edge_emu_buffer;
  113. emu=1;
  114. }
  115. }
  116. s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  117. ptr = ref_picture[2] + offset;
  118. if(emu){
  119. ff_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);
  120. ptr= s->edge_emu_buffer;
  121. }
  122. s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  123. return;
  124. }
  125. static inline void gmc_motion(MpegEncContext *s,
  126. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  127. uint8_t **ref_picture)
  128. {
  129. uint8_t *ptr;
  130. int linesize, uvlinesize;
  131. const int a= s->sprite_warping_accuracy;
  132. int ox, oy;
  133. linesize = s->linesize;
  134. uvlinesize = s->uvlinesize;
  135. ptr = ref_picture[0];
  136. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  137. 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,
  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. if(ENABLE_GRAY && s->flags&CODEC_FLAG_GRAY) return;
  153. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  154. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  155. ptr = ref_picture[1];
  156. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  157. ox,
  158. oy,
  159. s->sprite_delta[0][0], s->sprite_delta[0][1],
  160. s->sprite_delta[1][0], s->sprite_delta[1][1],
  161. a+1, (1<<(2*a+1)) - s->no_rounding,
  162. s->h_edge_pos>>1, s->v_edge_pos>>1);
  163. ptr = ref_picture[2];
  164. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  165. ox,
  166. oy,
  167. s->sprite_delta[0][0], s->sprite_delta[0][1],
  168. s->sprite_delta[1][0], s->sprite_delta[1][1],
  169. a+1, (1<<(2*a+1)) - s->no_rounding,
  170. s->h_edge_pos>>1, s->v_edge_pos>>1);
  171. }
  172. static inline int hpel_motion(MpegEncContext *s,
  173. uint8_t *dest, uint8_t *src,
  174. int field_based, int field_select,
  175. int src_x, int src_y,
  176. int width, int height, int stride,
  177. int h_edge_pos, int v_edge_pos,
  178. int w, int h, op_pixels_func *pix_op,
  179. int motion_x, int motion_y)
  180. {
  181. int dxy;
  182. int emu=0;
  183. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  184. src_x += motion_x >> 1;
  185. src_y += motion_y >> 1;
  186. /* WARNING: do no forget half pels */
  187. src_x = av_clip(src_x, -16, width); //FIXME unneeded for emu?
  188. if (src_x == width)
  189. dxy &= ~1;
  190. src_y = av_clip(src_y, -16, height);
  191. if (src_y == height)
  192. dxy &= ~2;
  193. src += src_y * stride + src_x;
  194. if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
  195. if( (unsigned)src_x > h_edge_pos - (motion_x&1) - w
  196. || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
  197. ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  198. src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
  199. src= s->edge_emu_buffer;
  200. emu=1;
  201. }
  202. }
  203. if(field_select)
  204. src += s->linesize;
  205. pix_op[dxy](dest, src, stride, h);
  206. return emu;
  207. }
  208. /* apply one mpeg motion vector to the three components */
  209. static av_always_inline
  210. void mpeg_motion(MpegEncContext *s,
  211. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  212. int field_based, int bottom_field, int field_select,
  213. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  214. int motion_x, int motion_y, int h)
  215. {
  216. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  217. int dxy, uvdxy, mx, my, src_x, src_y,
  218. uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
  219. #if 0
  220. if(s->quarter_sample)
  221. {
  222. motion_x>>=1;
  223. motion_y>>=1;
  224. }
  225. #endif
  226. v_edge_pos = s->v_edge_pos >> field_based;
  227. linesize = s->current_picture.linesize[0] << field_based;
  228. uvlinesize = s->current_picture.linesize[1] << field_based;
  229. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  230. src_x = s->mb_x* 16 + (motion_x >> 1);
  231. src_y =(s->mb_y<<(4-field_based)) + (motion_y >> 1);
  232. if (s->out_format == FMT_H263) {
  233. if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
  234. mx = (motion_x>>1)|(motion_x&1);
  235. my = motion_y >>1;
  236. uvdxy = ((my & 1) << 1) | (mx & 1);
  237. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  238. uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
  239. }else{
  240. uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
  241. uvsrc_x = src_x>>1;
  242. uvsrc_y = src_y>>1;
  243. }
  244. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  245. mx = motion_x / 4;
  246. my = motion_y / 4;
  247. uvdxy = 0;
  248. uvsrc_x = s->mb_x*8 + mx;
  249. uvsrc_y = s->mb_y*8 + my;
  250. } else {
  251. if(s->chroma_y_shift){
  252. mx = motion_x / 2;
  253. my = motion_y / 2;
  254. uvdxy = ((my & 1) << 1) | (mx & 1);
  255. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  256. uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
  257. } else {
  258. if(s->chroma_x_shift){
  259. //Chroma422
  260. mx = motion_x / 2;
  261. uvdxy = ((motion_y & 1) << 1) | (mx & 1);
  262. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  263. uvsrc_y = src_y;
  264. } else {
  265. //Chroma444
  266. uvdxy = dxy;
  267. uvsrc_x = src_x;
  268. uvsrc_y = src_y;
  269. }
  270. }
  271. }
  272. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  273. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  274. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  275. if( (unsigned)src_x > s->h_edge_pos - (motion_x&1) - 16
  276. || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
  277. if(s->codec_id == CODEC_ID_MPEG2VIDEO ||
  278. s->codec_id == CODEC_ID_MPEG1VIDEO){
  279. av_log(s->avctx,AV_LOG_DEBUG,
  280. "MPEG motion vector out of boundary\n");
  281. return ;
  282. }
  283. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
  284. 17, 17+field_based,
  285. src_x, src_y<<field_based,
  286. s->h_edge_pos, s->v_edge_pos);
  287. ptr_y = s->edge_emu_buffer;
  288. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  289. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  290. ff_emulated_edge_mc(uvbuf ,
  291. ptr_cb, s->uvlinesize,
  292. 9, 9+field_based,
  293. uvsrc_x, uvsrc_y<<field_based,
  294. s->h_edge_pos>>1, s->v_edge_pos>>1);
  295. ff_emulated_edge_mc(uvbuf+16,
  296. ptr_cr, s->uvlinesize,
  297. 9, 9+field_based,
  298. uvsrc_x, uvsrc_y<<field_based,
  299. s->h_edge_pos>>1, s->v_edge_pos>>1);
  300. ptr_cb= uvbuf;
  301. ptr_cr= uvbuf+16;
  302. }
  303. }
  304. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  305. dest_y += s->linesize;
  306. dest_cb+= s->uvlinesize;
  307. dest_cr+= s->uvlinesize;
  308. }
  309. if(field_select){
  310. ptr_y += s->linesize;
  311. ptr_cb+= s->uvlinesize;
  312. ptr_cr+= s->uvlinesize;
  313. }
  314. pix_op[0][dxy](dest_y, ptr_y, linesize, h);
  315. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  316. pix_op[s->chroma_x_shift][uvdxy]
  317. (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
  318. pix_op[s->chroma_x_shift][uvdxy]
  319. (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
  320. }
  321. if((ENABLE_H261_ENCODER || ENABLE_H261_DECODER) &&
  322. s->out_format == FMT_H261){
  323. ff_h261_loop_filter(s);
  324. }
  325. }
  326. //FIXME move to dsputil, avg variant, 16x16 version
  327. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
  328. int x;
  329. uint8_t * const top = src[1];
  330. uint8_t * const left = src[2];
  331. uint8_t * const mid = src[0];
  332. uint8_t * const right = src[3];
  333. uint8_t * const bottom= src[4];
  334. #define OBMC_FILTER(x, t, l, m, r, b)\
  335. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  336. #define OBMC_FILTER4(x, t, l, m, r, b)\
  337. OBMC_FILTER(x , t, l, m, r, b);\
  338. OBMC_FILTER(x+1 , t, l, m, r, b);\
  339. OBMC_FILTER(x +stride, t, l, m, r, b);\
  340. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  341. x=0;
  342. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  343. OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
  344. OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
  345. OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
  346. OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
  347. OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
  348. x+= stride;
  349. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  350. OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
  351. OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
  352. OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
  353. x+= stride;
  354. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  355. OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
  356. OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
  357. OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
  358. x+= 2*stride;
  359. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  360. OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
  361. OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
  362. OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
  363. x+= 2*stride;
  364. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  365. OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
  366. OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
  367. OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
  368. OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
  369. OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
  370. x+= stride;
  371. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  372. OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
  373. OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
  374. OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
  375. }
  376. /* obmc for 1 8x8 luma block */
  377. static inline void obmc_motion(MpegEncContext *s,
  378. uint8_t *dest, uint8_t *src,
  379. int src_x, int src_y,
  380. op_pixels_func *pix_op,
  381. int16_t mv[5][2]/* mid top left right bottom*/)
  382. #define MID 0
  383. {
  384. int i;
  385. uint8_t *ptr[5];
  386. assert(s->quarter_sample==0);
  387. for(i=0; i<5; i++){
  388. if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
  389. ptr[i]= ptr[MID];
  390. }else{
  391. ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
  392. hpel_motion(s, ptr[i], src, 0, 0,
  393. src_x, src_y,
  394. s->width, s->height, s->linesize,
  395. s->h_edge_pos, s->v_edge_pos,
  396. 8, 8, pix_op,
  397. mv[i][0], mv[i][1]);
  398. }
  399. }
  400. put_obmc(dest, ptr, s->linesize);
  401. }
  402. static inline void qpel_motion(MpegEncContext *s,
  403. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  404. int field_based, int bottom_field, int field_select,
  405. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  406. qpel_mc_func (*qpix_op)[16],
  407. int motion_x, int motion_y, int h)
  408. {
  409. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  410. int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
  411. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  412. src_x = s->mb_x * 16 + (motion_x >> 2);
  413. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  414. v_edge_pos = s->v_edge_pos >> field_based;
  415. linesize = s->linesize << field_based;
  416. uvlinesize = s->uvlinesize << field_based;
  417. if(field_based){
  418. mx= motion_x/2;
  419. my= motion_y>>1;
  420. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  421. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  422. mx= (motion_x>>1) + rtab[motion_x&7];
  423. my= (motion_y>>1) + rtab[motion_y&7];
  424. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  425. mx= (motion_x>>1)|(motion_x&1);
  426. my= (motion_y>>1)|(motion_y&1);
  427. }else{
  428. mx= motion_x/2;
  429. my= motion_y/2;
  430. }
  431. mx= (mx>>1)|(mx&1);
  432. my= (my>>1)|(my&1);
  433. uvdxy= (mx&1) | ((my&1)<<1);
  434. mx>>=1;
  435. my>>=1;
  436. uvsrc_x = s->mb_x * 8 + mx;
  437. uvsrc_y = s->mb_y * (8 >> field_based) + my;
  438. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  439. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  440. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  441. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
  442. || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
  443. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
  444. 17, 17+field_based, src_x, src_y<<field_based,
  445. s->h_edge_pos, s->v_edge_pos);
  446. ptr_y= s->edge_emu_buffer;
  447. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  448. uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
  449. ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
  450. 9, 9 + field_based,
  451. uvsrc_x, uvsrc_y<<field_based,
  452. s->h_edge_pos>>1, s->v_edge_pos>>1);
  453. ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
  454. 9, 9 + field_based,
  455. uvsrc_x, uvsrc_y<<field_based,
  456. s->h_edge_pos>>1, s->v_edge_pos>>1);
  457. ptr_cb= uvbuf;
  458. ptr_cr= uvbuf + 16;
  459. }
  460. }
  461. if(!field_based)
  462. qpix_op[0][dxy](dest_y, ptr_y, linesize);
  463. else{
  464. if(bottom_field){
  465. dest_y += s->linesize;
  466. dest_cb+= s->uvlinesize;
  467. dest_cr+= s->uvlinesize;
  468. }
  469. if(field_select){
  470. ptr_y += s->linesize;
  471. ptr_cb += s->uvlinesize;
  472. ptr_cr += s->uvlinesize;
  473. }
  474. //damn interlaced mode
  475. //FIXME boundary mirroring is not exactly correct here
  476. qpix_op[1][dxy](dest_y , ptr_y , linesize);
  477. qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
  478. }
  479. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  480. pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
  481. pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
  482. }
  483. }
  484. /**
  485. * h263 chroma 4mv motion compensation.
  486. */
  487. static inline void chroma_4mv_motion(MpegEncContext *s,
  488. uint8_t *dest_cb, uint8_t *dest_cr,
  489. uint8_t **ref_picture,
  490. op_pixels_func *pix_op,
  491. int mx, int my){
  492. int dxy, emu=0, src_x, src_y, offset;
  493. uint8_t *ptr;
  494. /* In case of 8X8, we construct a single chroma motion vector
  495. with a special rounding */
  496. mx= ff_h263_round_chroma(mx);
  497. my= ff_h263_round_chroma(my);
  498. dxy = ((my & 1) << 1) | (mx & 1);
  499. mx >>= 1;
  500. my >>= 1;
  501. src_x = s->mb_x * 8 + mx;
  502. src_y = s->mb_y * 8 + my;
  503. src_x = av_clip(src_x, -8, s->width/2);
  504. if (src_x == s->width/2)
  505. dxy &= ~1;
  506. src_y = av_clip(src_y, -8, s->height/2);
  507. if (src_y == s->height/2)
  508. dxy &= ~2;
  509. offset = (src_y * (s->uvlinesize)) + src_x;
  510. ptr = ref_picture[1] + offset;
  511. if(s->flags&CODEC_FLAG_EMU_EDGE){
  512. if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
  513. || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
  514. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  515. 9, 9, src_x, src_y,
  516. s->h_edge_pos>>1, s->v_edge_pos>>1);
  517. ptr= s->edge_emu_buffer;
  518. emu=1;
  519. }
  520. }
  521. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  522. ptr = ref_picture[2] + offset;
  523. if(emu){
  524. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  525. 9, 9, src_x, src_y,
  526. s->h_edge_pos>>1, s->v_edge_pos>>1);
  527. ptr= s->edge_emu_buffer;
  528. }
  529. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  530. }
  531. static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
  532. /* fetch pixels for estimated mv 4 macroblocks ahead
  533. * optimized for 64byte cache lines */
  534. const int shift = s->quarter_sample ? 2 : 1;
  535. const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
  536. const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
  537. int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
  538. s->dsp.prefetch(pix[0]+off, s->linesize, 4);
  539. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  540. s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
  541. }
  542. /**
  543. * motion compensation of a single macroblock
  544. * @param s context
  545. * @param dest_y luma destination pointer
  546. * @param dest_cb chroma cb/u destination pointer
  547. * @param dest_cr chroma cr/v destination pointer
  548. * @param dir direction (0->forward, 1->backward)
  549. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  550. * @param pic_op halfpel motion compensation function (average or put normally)
  551. * @param pic_op qpel motion compensation function (average or put normally)
  552. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  553. */
  554. static inline void MPV_motion(MpegEncContext *s,
  555. uint8_t *dest_y, uint8_t *dest_cb,
  556. uint8_t *dest_cr, int dir,
  557. uint8_t **ref_picture,
  558. op_pixels_func (*pix_op)[4],
  559. qpel_mc_func (*qpix_op)[16])
  560. {
  561. int dxy, mx, my, src_x, src_y, motion_x, motion_y;
  562. int mb_x, mb_y, i;
  563. uint8_t *ptr, *dest;
  564. mb_x = s->mb_x;
  565. mb_y = s->mb_y;
  566. prefetch_motion(s, ref_picture, dir);
  567. if(s->obmc && s->pict_type != FF_B_TYPE){
  568. int16_t mv_cache[4][4][2];
  569. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  570. const int mot_stride= s->b8_stride;
  571. const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
  572. assert(!s->mb_skipped);
  573. memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
  574. memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  575. memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  576. if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
  577. memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
  578. }else{
  579. memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
  580. }
  581. if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
  582. *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
  583. *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
  584. }else{
  585. *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
  586. *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
  587. }
  588. if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
  589. *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
  590. *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
  591. }else{
  592. *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
  593. *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
  594. }
  595. mx = 0;
  596. my = 0;
  597. for(i=0;i<4;i++) {
  598. const int x= (i&1)+1;
  599. const int y= (i>>1)+1;
  600. int16_t mv[5][2]= {
  601. {mv_cache[y][x ][0], mv_cache[y][x ][1]},
  602. {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
  603. {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
  604. {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
  605. {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
  606. //FIXME cleanup
  607. obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  608. ref_picture[0],
  609. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  610. pix_op[1],
  611. mv);
  612. mx += mv[0][0];
  613. my += mv[0][1];
  614. }
  615. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  616. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  617. return;
  618. }
  619. switch(s->mv_type) {
  620. case MV_TYPE_16X16:
  621. if(s->mcsel){
  622. if(s->real_sprite_warping_points==1){
  623. gmc1_motion(s, dest_y, dest_cb, dest_cr,
  624. ref_picture);
  625. }else{
  626. gmc_motion(s, dest_y, dest_cb, dest_cr,
  627. ref_picture);
  628. }
  629. }else if(s->quarter_sample){
  630. qpel_motion(s, dest_y, dest_cb, dest_cr,
  631. 0, 0, 0,
  632. ref_picture, pix_op, qpix_op,
  633. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  634. }else if(ENABLE_WMV2 && s->mspel){
  635. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  636. ref_picture, pix_op,
  637. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  638. }else
  639. {
  640. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  641. 0, 0, 0,
  642. ref_picture, pix_op,
  643. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  644. }
  645. break;
  646. case MV_TYPE_8X8:
  647. mx = 0;
  648. my = 0;
  649. if(s->quarter_sample){
  650. for(i=0;i<4;i++) {
  651. motion_x = s->mv[dir][i][0];
  652. motion_y = s->mv[dir][i][1];
  653. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  654. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  655. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  656. /* WARNING: do no forget half pels */
  657. src_x = av_clip(src_x, -16, s->width);
  658. if (src_x == s->width)
  659. dxy &= ~3;
  660. src_y = av_clip(src_y, -16, s->height);
  661. if (src_y == s->height)
  662. dxy &= ~12;
  663. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  664. if(s->flags&CODEC_FLAG_EMU_EDGE){
  665. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
  666. || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
  667. ff_emulated_edge_mc(s->edge_emu_buffer, ptr,
  668. s->linesize, 9, 9,
  669. src_x, src_y,
  670. s->h_edge_pos, s->v_edge_pos);
  671. ptr= s->edge_emu_buffer;
  672. }
  673. }
  674. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  675. qpix_op[1][dxy](dest, ptr, s->linesize);
  676. mx += s->mv[dir][i][0]/2;
  677. my += s->mv[dir][i][1]/2;
  678. }
  679. }else{
  680. for(i=0;i<4;i++) {
  681. hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  682. ref_picture[0], 0, 0,
  683. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  684. s->width, s->height, s->linesize,
  685. s->h_edge_pos, s->v_edge_pos,
  686. 8, 8, pix_op[1],
  687. s->mv[dir][i][0], s->mv[dir][i][1]);
  688. mx += s->mv[dir][i][0];
  689. my += s->mv[dir][i][1];
  690. }
  691. }
  692. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  693. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  694. break;
  695. case MV_TYPE_FIELD:
  696. if (s->picture_structure == PICT_FRAME) {
  697. if(s->quarter_sample){
  698. for(i=0; i<2; i++){
  699. qpel_motion(s, dest_y, dest_cb, dest_cr,
  700. 1, i, s->field_select[dir][i],
  701. ref_picture, pix_op, qpix_op,
  702. s->mv[dir][i][0], s->mv[dir][i][1], 8);
  703. }
  704. }else{
  705. /* top field */
  706. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  707. 1, 0, s->field_select[dir][0],
  708. ref_picture, pix_op,
  709. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  710. /* bottom field */
  711. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  712. 1, 1, s->field_select[dir][1],
  713. ref_picture, pix_op,
  714. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  715. }
  716. } else {
  717. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != FF_B_TYPE && !s->first_field){
  718. ref_picture= s->current_picture_ptr->data;
  719. }
  720. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  721. 0, 0, s->field_select[dir][0],
  722. ref_picture, pix_op,
  723. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  724. }
  725. break;
  726. case MV_TYPE_16X8:
  727. for(i=0; i<2; i++){
  728. uint8_t ** ref2picture;
  729. if(s->picture_structure == s->field_select[dir][i] + 1
  730. || s->pict_type == FF_B_TYPE || s->first_field){
  731. ref2picture= ref_picture;
  732. }else{
  733. ref2picture= s->current_picture_ptr->data;
  734. }
  735. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  736. 0, 0, s->field_select[dir][i],
  737. ref2picture, pix_op,
  738. s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8);
  739. dest_y += 16*s->linesize;
  740. dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
  741. dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
  742. }
  743. break;
  744. case MV_TYPE_DMV:
  745. if(s->picture_structure == PICT_FRAME){
  746. for(i=0; i<2; i++){
  747. int j;
  748. for(j=0; j<2; j++){
  749. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  750. 1, j, j^i,
  751. ref_picture, pix_op,
  752. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8);
  753. }
  754. pix_op = s->dsp.avg_pixels_tab;
  755. }
  756. }else{
  757. for(i=0; i<2; i++){
  758. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  759. 0, 0, s->picture_structure != i+1,
  760. ref_picture, pix_op,
  761. s->mv[dir][2*i][0],s->mv[dir][2*i][1],16);
  762. // after put we make avg of the same block
  763. pix_op=s->dsp.avg_pixels_tab;
  764. //opposite parity is always in the same frame if this is second field
  765. if(!s->first_field){
  766. ref_picture = s->current_picture_ptr->data;
  767. }
  768. }
  769. }
  770. break;
  771. default: assert(0);
  772. }
  773. }
  774. #endif /* FFMPEG_MPEGVIDEO_COMMON_H */