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.

886 lines
33KB

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