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.

894 lines
34KB

  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 inline 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 inline 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 field_based, int field_select,
  155. int src_x, int src_y,
  156. int width, int height, int stride,
  157. int h_edge_pos, int v_edge_pos,
  158. int w, int h, op_pixels_func *pix_op,
  159. int motion_x, int motion_y)
  160. {
  161. int dxy;
  162. int emu=0;
  163. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  164. src_x += motion_x >> 1;
  165. src_y += motion_y >> 1;
  166. /* WARNING: do no forget half pels */
  167. src_x = av_clip(src_x, -16, width); //FIXME unneeded for emu?
  168. if (src_x == width)
  169. dxy &= ~1;
  170. src_y = av_clip(src_y, -16, height);
  171. if (src_y == height)
  172. dxy &= ~2;
  173. src += src_y * stride + src_x;
  174. if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
  175. if( (unsigned)src_x > FFMAX(h_edge_pos - (motion_x&1) - w, 0)
  176. || (unsigned)src_y > FFMAX(v_edge_pos - (motion_y&1) - h, 0)){
  177. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  178. src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
  179. src= s->edge_emu_buffer;
  180. emu=1;
  181. }
  182. }
  183. if(field_select)
  184. src += s->linesize;
  185. pix_op[dxy](dest, src, stride, h);
  186. return emu;
  187. }
  188. static av_always_inline
  189. void mpeg_motion_internal(MpegEncContext *s,
  190. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  191. int field_based, int bottom_field, int field_select,
  192. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  193. int motion_x, int motion_y, int h, int is_mpeg12, int mb_y)
  194. {
  195. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  196. int dxy, uvdxy, mx, my, src_x, src_y,
  197. uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
  198. #if 0
  199. if(s->quarter_sample)
  200. {
  201. motion_x>>=1;
  202. motion_y>>=1;
  203. }
  204. #endif
  205. v_edge_pos = s->v_edge_pos >> field_based;
  206. linesize = s->current_picture.f.linesize[0] << field_based;
  207. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  208. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  209. src_x = s->mb_x* 16 + (motion_x >> 1);
  210. src_y =( mb_y<<(4-field_based)) + (motion_y >> 1);
  211. if (!is_mpeg12 && s->out_format == FMT_H263) {
  212. if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
  213. mx = (motion_x>>1)|(motion_x&1);
  214. my = motion_y >>1;
  215. uvdxy = ((my & 1) << 1) | (mx & 1);
  216. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  217. uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
  218. }else{
  219. uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
  220. uvsrc_x = src_x>>1;
  221. uvsrc_y = src_y>>1;
  222. }
  223. }else if(!is_mpeg12 && s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  224. mx = motion_x / 4;
  225. my = motion_y / 4;
  226. uvdxy = 0;
  227. uvsrc_x = s->mb_x*8 + mx;
  228. uvsrc_y = mb_y*8 + my;
  229. } else {
  230. if(s->chroma_y_shift){
  231. mx = motion_x / 2;
  232. my = motion_y / 2;
  233. uvdxy = ((my & 1) << 1) | (mx & 1);
  234. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  235. uvsrc_y =( mb_y<<(3-field_based))+ (my >> 1);
  236. } else {
  237. if(s->chroma_x_shift){
  238. //Chroma422
  239. mx = motion_x / 2;
  240. uvdxy = ((motion_y & 1) << 1) | (mx & 1);
  241. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  242. uvsrc_y = src_y;
  243. } else {
  244. //Chroma444
  245. uvdxy = dxy;
  246. uvsrc_x = src_x;
  247. uvsrc_y = src_y;
  248. }
  249. }
  250. }
  251. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  252. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  253. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  254. if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&1) - 16, 0)
  255. || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&1) - h , 0)){
  256. if(is_mpeg12 || s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
  257. s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
  258. av_log(s->avctx,AV_LOG_DEBUG,
  259. "MPEG motion vector out of boundary (%d %d)\n", src_x, src_y);
  260. return;
  261. }
  262. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
  263. 17, 17+field_based,
  264. src_x, src_y<<field_based,
  265. s->h_edge_pos, s->v_edge_pos);
  266. ptr_y = s->edge_emu_buffer;
  267. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  268. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  269. s->dsp.emulated_edge_mc(uvbuf ,
  270. ptr_cb, 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. s->dsp.emulated_edge_mc(uvbuf+16,
  275. ptr_cr, s->uvlinesize,
  276. 9, 9+field_based,
  277. uvsrc_x, uvsrc_y<<field_based,
  278. s->h_edge_pos>>1, s->v_edge_pos>>1);
  279. ptr_cb= uvbuf;
  280. ptr_cr= uvbuf+16;
  281. }
  282. }
  283. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  284. dest_y += s->linesize;
  285. dest_cb+= s->uvlinesize;
  286. dest_cr+= s->uvlinesize;
  287. }
  288. if(field_select){
  289. ptr_y += s->linesize;
  290. ptr_cb+= s->uvlinesize;
  291. ptr_cr+= s->uvlinesize;
  292. }
  293. pix_op[0][dxy](dest_y, ptr_y, linesize, h);
  294. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  295. pix_op[s->chroma_x_shift][uvdxy]
  296. (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
  297. pix_op[s->chroma_x_shift][uvdxy]
  298. (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
  299. }
  300. if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
  301. s->out_format == FMT_H261){
  302. ff_h261_loop_filter(s);
  303. }
  304. }
  305. /* apply one mpeg motion vector to the three components */
  306. static void mpeg_motion(MpegEncContext *s,
  307. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  308. int field_select, uint8_t **ref_picture,
  309. op_pixels_func (*pix_op)[4],
  310. int motion_x, int motion_y, int h, int mb_y)
  311. {
  312. #if !CONFIG_SMALL
  313. if(s->out_format == FMT_MPEG1)
  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, 1, mb_y);
  317. else
  318. #endif
  319. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
  320. field_select, ref_picture, pix_op,
  321. motion_x, motion_y, h, 0, mb_y);
  322. }
  323. static void mpeg_motion_field(MpegEncContext *s, uint8_t *dest_y,
  324. uint8_t *dest_cb, uint8_t *dest_cr,
  325. int bottom_field, int field_select,
  326. uint8_t **ref_picture,
  327. op_pixels_func (*pix_op)[4],
  328. int motion_x, int motion_y, int h, int mb_y)
  329. {
  330. #if !CONFIG_SMALL
  331. if(s->out_format == FMT_MPEG1)
  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, 1, mb_y);
  335. else
  336. #endif
  337. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
  338. bottom_field, field_select, ref_picture, pix_op,
  339. motion_x, motion_y, h, 0, mb_y);
  340. }
  341. //FIXME move to dsputil, avg variant, 16x16 version
  342. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
  343. int x;
  344. uint8_t * const top = src[1];
  345. uint8_t * const left = src[2];
  346. uint8_t * const mid = src[0];
  347. uint8_t * const right = src[3];
  348. uint8_t * const bottom= src[4];
  349. #define OBMC_FILTER(x, t, l, m, r, b)\
  350. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  351. #define OBMC_FILTER4(x, t, l, m, r, b)\
  352. OBMC_FILTER(x , t, l, m, r, b);\
  353. OBMC_FILTER(x+1 , t, l, m, r, b);\
  354. OBMC_FILTER(x +stride, t, l, m, r, b);\
  355. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  356. x=0;
  357. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  358. OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
  359. OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
  360. OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
  361. OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
  362. OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
  363. x+= stride;
  364. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  365. OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
  366. OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
  367. OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
  368. x+= stride;
  369. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  370. OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
  371. OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
  372. OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
  373. x+= 2*stride;
  374. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  375. OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
  376. OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
  377. OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
  378. x+= 2*stride;
  379. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  380. OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
  381. OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
  382. OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
  383. OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
  384. OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
  385. x+= stride;
  386. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  387. OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
  388. OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
  389. OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
  390. }
  391. /* obmc for 1 8x8 luma block */
  392. static inline void obmc_motion(MpegEncContext *s,
  393. uint8_t *dest, uint8_t *src,
  394. int src_x, int src_y,
  395. op_pixels_func *pix_op,
  396. int16_t mv[5][2]/* mid top left right bottom*/)
  397. #define MID 0
  398. {
  399. int i;
  400. uint8_t *ptr[5];
  401. av_assert2(s->quarter_sample==0);
  402. for(i=0; i<5; i++){
  403. if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
  404. ptr[i]= ptr[MID];
  405. }else{
  406. ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
  407. hpel_motion(s, ptr[i], src, 0, 0,
  408. src_x, src_y,
  409. s->width, s->height, s->linesize,
  410. s->h_edge_pos, s->v_edge_pos,
  411. 8, 8, pix_op,
  412. mv[i][0], mv[i][1]);
  413. }
  414. }
  415. put_obmc(dest, ptr, s->linesize);
  416. }
  417. static inline void qpel_motion(MpegEncContext *s,
  418. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  419. int field_based, int bottom_field, int field_select,
  420. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  421. qpel_mc_func (*qpix_op)[16],
  422. int motion_x, int motion_y, int h)
  423. {
  424. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  425. int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
  426. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  427. src_x = s->mb_x * 16 + (motion_x >> 2);
  428. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  429. v_edge_pos = s->v_edge_pos >> field_based;
  430. linesize = s->linesize << field_based;
  431. uvlinesize = s->uvlinesize << field_based;
  432. if(field_based){
  433. mx= motion_x/2;
  434. my= motion_y>>1;
  435. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  436. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  437. mx= (motion_x>>1) + rtab[motion_x&7];
  438. my= (motion_y>>1) + rtab[motion_y&7];
  439. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  440. mx= (motion_x>>1)|(motion_x&1);
  441. my= (motion_y>>1)|(motion_y&1);
  442. }else{
  443. mx= motion_x/2;
  444. my= motion_y/2;
  445. }
  446. mx= (mx>>1)|(mx&1);
  447. my= (my>>1)|(my&1);
  448. uvdxy= (mx&1) | ((my&1)<<1);
  449. mx>>=1;
  450. my>>=1;
  451. uvsrc_x = s->mb_x * 8 + mx;
  452. uvsrc_y = s->mb_y * (8 >> field_based) + my;
  453. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  454. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  455. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  456. if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 16, 0)
  457. || (unsigned)src_y > FFMAX( v_edge_pos - (motion_y&3) - h , 0)){
  458. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
  459. 17, 17+field_based, src_x, src_y<<field_based,
  460. s->h_edge_pos, s->v_edge_pos);
  461. ptr_y= s->edge_emu_buffer;
  462. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  463. uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
  464. s->dsp.emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
  465. 9, 9 + field_based,
  466. uvsrc_x, uvsrc_y<<field_based,
  467. s->h_edge_pos>>1, s->v_edge_pos>>1);
  468. s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
  469. 9, 9 + field_based,
  470. uvsrc_x, uvsrc_y<<field_based,
  471. s->h_edge_pos>>1, s->v_edge_pos>>1);
  472. ptr_cb= uvbuf;
  473. ptr_cr= uvbuf + 16;
  474. }
  475. }
  476. if(!field_based)
  477. qpix_op[0][dxy](dest_y, ptr_y, linesize);
  478. else{
  479. if(bottom_field){
  480. dest_y += s->linesize;
  481. dest_cb+= s->uvlinesize;
  482. dest_cr+= s->uvlinesize;
  483. }
  484. if(field_select){
  485. ptr_y += s->linesize;
  486. ptr_cb += s->uvlinesize;
  487. ptr_cr += s->uvlinesize;
  488. }
  489. //damn interlaced mode
  490. //FIXME boundary mirroring is not exactly correct here
  491. qpix_op[1][dxy](dest_y , ptr_y , linesize);
  492. qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
  493. }
  494. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  495. pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
  496. pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
  497. }
  498. }
  499. /**
  500. * h263 chroma 4mv motion compensation.
  501. */
  502. static inline void chroma_4mv_motion(MpegEncContext *s,
  503. uint8_t *dest_cb, uint8_t *dest_cr,
  504. uint8_t **ref_picture,
  505. op_pixels_func *pix_op,
  506. int mx, int my){
  507. int dxy, emu=0, src_x, src_y, offset;
  508. uint8_t *ptr;
  509. /* In case of 8X8, we construct a single chroma motion vector
  510. with a special rounding */
  511. mx= ff_h263_round_chroma(mx);
  512. my= ff_h263_round_chroma(my);
  513. dxy = ((my & 1) << 1) | (mx & 1);
  514. mx >>= 1;
  515. my >>= 1;
  516. src_x = s->mb_x * 8 + mx;
  517. src_y = s->mb_y * 8 + my;
  518. src_x = av_clip(src_x, -8, (s->width >> 1));
  519. if (src_x == (s->width >> 1))
  520. dxy &= ~1;
  521. src_y = av_clip(src_y, -8, (s->height >> 1));
  522. if (src_y == (s->height >> 1))
  523. dxy &= ~2;
  524. offset = src_y * s->uvlinesize + src_x;
  525. ptr = ref_picture[1] + offset;
  526. if(s->flags&CODEC_FLAG_EMU_EDGE){
  527. if( (unsigned)src_x > FFMAX((s->h_edge_pos>>1) - (dxy &1) - 8, 0)
  528. || (unsigned)src_y > FFMAX((s->v_edge_pos>>1) - (dxy>>1) - 8, 0)){
  529. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  530. 9, 9, src_x, src_y,
  531. s->h_edge_pos>>1, s->v_edge_pos>>1);
  532. ptr= s->edge_emu_buffer;
  533. emu=1;
  534. }
  535. }
  536. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  537. ptr = ref_picture[2] + offset;
  538. if(emu){
  539. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  540. 9, 9, src_x, src_y,
  541. s->h_edge_pos>>1, s->v_edge_pos>>1);
  542. ptr= s->edge_emu_buffer;
  543. }
  544. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  545. }
  546. static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
  547. /* fetch pixels for estimated mv 4 macroblocks ahead
  548. * optimized for 64byte cache lines */
  549. const int shift = s->quarter_sample ? 2 : 1;
  550. const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
  551. const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
  552. int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
  553. s->dsp.prefetch(pix[0]+off, s->linesize, 4);
  554. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  555. s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
  556. }
  557. /**
  558. * motion compensation of a single macroblock
  559. * @param s context
  560. * @param dest_y luma destination pointer
  561. * @param dest_cb chroma cb/u destination pointer
  562. * @param dest_cr chroma cr/v destination pointer
  563. * @param dir direction (0->forward, 1->backward)
  564. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  565. * @param pix_op halfpel motion compensation function (average or put normally)
  566. * @param qpix_op qpel motion compensation function (average or put normally)
  567. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  568. */
  569. static av_always_inline void MPV_motion_internal(MpegEncContext *s,
  570. uint8_t *dest_y, uint8_t *dest_cb,
  571. uint8_t *dest_cr, int dir,
  572. uint8_t **ref_picture,
  573. op_pixels_func (*pix_op)[4],
  574. qpel_mc_func (*qpix_op)[16], int is_mpeg12)
  575. {
  576. int dxy, mx, my, src_x, src_y, motion_x, motion_y;
  577. int mb_x, mb_y, i;
  578. uint8_t *ptr, *dest;
  579. mb_x = s->mb_x;
  580. mb_y = s->mb_y;
  581. prefetch_motion(s, ref_picture, dir);
  582. if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
  583. int16_t mv_cache[4][4][2];
  584. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  585. const int mot_stride= s->b8_stride;
  586. const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
  587. av_assert2(!s->mb_skipped);
  588. memcpy(mv_cache[1][1], s->current_picture.f.motion_val[0][mot_xy ], sizeof(int16_t) * 4);
  589. memcpy(mv_cache[2][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
  590. memcpy(mv_cache[3][1], s->current_picture.f.motion_val[0][mot_xy + mot_stride], sizeof(int16_t) * 4);
  591. if (mb_y == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - s->mb_stride])) {
  592. memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
  593. }else{
  594. memcpy(mv_cache[0][1], s->current_picture.f.motion_val[0][mot_xy - mot_stride], sizeof(int16_t) * 4);
  595. }
  596. if (mb_x == 0 || IS_INTRA(s->current_picture.f.mb_type[xy - 1])) {
  597. AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
  598. AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
  599. }else{
  600. AV_COPY32(mv_cache[1][0], s->current_picture.f.motion_val[0][mot_xy - 1]);
  601. AV_COPY32(mv_cache[2][0], s->current_picture.f.motion_val[0][mot_xy - 1 + mot_stride]);
  602. }
  603. if (mb_x + 1 >= s->mb_width || IS_INTRA(s->current_picture.f.mb_type[xy + 1])) {
  604. AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
  605. AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
  606. }else{
  607. AV_COPY32(mv_cache[1][3], s->current_picture.f.motion_val[0][mot_xy + 2]);
  608. AV_COPY32(mv_cache[2][3], s->current_picture.f.motion_val[0][mot_xy + 2 + mot_stride]);
  609. }
  610. mx = 0;
  611. my = 0;
  612. for(i=0;i<4;i++) {
  613. const int x= (i&1)+1;
  614. const int y= (i>>1)+1;
  615. int16_t mv[5][2]= {
  616. {mv_cache[y][x ][0], mv_cache[y][x ][1]},
  617. {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
  618. {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
  619. {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
  620. {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
  621. //FIXME cleanup
  622. obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  623. ref_picture[0],
  624. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  625. pix_op[1],
  626. mv);
  627. mx += mv[0][0];
  628. my += mv[0][1];
  629. }
  630. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  631. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  632. return;
  633. }
  634. switch(s->mv_type) {
  635. case MV_TYPE_16X16:
  636. if(s->mcsel){
  637. if(s->real_sprite_warping_points==1){
  638. gmc1_motion(s, dest_y, dest_cb, dest_cr,
  639. ref_picture);
  640. }else{
  641. gmc_motion(s, dest_y, dest_cb, dest_cr,
  642. ref_picture);
  643. }
  644. }else if(!is_mpeg12 && s->quarter_sample){
  645. qpel_motion(s, dest_y, dest_cb, dest_cr,
  646. 0, 0, 0,
  647. ref_picture, pix_op, qpix_op,
  648. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  649. } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
  650. s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
  651. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  652. ref_picture, pix_op,
  653. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  654. }else
  655. {
  656. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  657. ref_picture, pix_op,
  658. s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
  659. }
  660. break;
  661. case MV_TYPE_8X8:
  662. if (!is_mpeg12) {
  663. mx = 0;
  664. my = 0;
  665. if(s->quarter_sample){
  666. for(i=0;i<4;i++) {
  667. motion_x = s->mv[dir][i][0];
  668. motion_y = s->mv[dir][i][1];
  669. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  670. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  671. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  672. /* WARNING: do no forget half pels */
  673. src_x = av_clip(src_x, -16, s->width);
  674. if (src_x == s->width)
  675. dxy &= ~3;
  676. src_y = av_clip(src_y, -16, s->height);
  677. if (src_y == s->height)
  678. dxy &= ~12;
  679. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  680. if(s->flags&CODEC_FLAG_EMU_EDGE){
  681. if( (unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x&3) - 8, 0)
  682. || (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y&3) - 8, 0)){
  683. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  684. s->linesize, 9, 9,
  685. src_x, src_y,
  686. s->h_edge_pos, s->v_edge_pos);
  687. ptr= s->edge_emu_buffer;
  688. }
  689. }
  690. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  691. qpix_op[1][dxy](dest, ptr, s->linesize);
  692. mx += s->mv[dir][i][0]/2;
  693. my += s->mv[dir][i][1]/2;
  694. }
  695. }else{
  696. for(i=0;i<4;i++) {
  697. hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  698. ref_picture[0], 0, 0,
  699. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  700. s->width, s->height, s->linesize,
  701. s->h_edge_pos, s->v_edge_pos,
  702. 8, 8, pix_op[1],
  703. s->mv[dir][i][0], s->mv[dir][i][1]);
  704. mx += s->mv[dir][i][0];
  705. my += s->mv[dir][i][1];
  706. }
  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. break;
  712. case MV_TYPE_FIELD:
  713. if (s->picture_structure == PICT_FRAME) {
  714. if(!is_mpeg12 && s->quarter_sample){
  715. for(i=0; i<2; i++){
  716. qpel_motion(s, dest_y, dest_cb, dest_cr,
  717. 1, i, s->field_select[dir][i],
  718. ref_picture, pix_op, qpix_op,
  719. s->mv[dir][i][0], s->mv[dir][i][1], 8);
  720. }
  721. }else{
  722. /* top field */
  723. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  724. 0, s->field_select[dir][0],
  725. ref_picture, pix_op,
  726. s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
  727. /* bottom field */
  728. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  729. 1, s->field_select[dir][1],
  730. ref_picture, pix_op,
  731. s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
  732. }
  733. } else {
  734. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
  735. ref_picture = s->current_picture_ptr->f.data;
  736. }
  737. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  738. s->field_select[dir][0],
  739. ref_picture, pix_op,
  740. s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y>>1);
  741. }
  742. break;
  743. case MV_TYPE_16X8:
  744. for(i=0; i<2; i++){
  745. uint8_t ** ref2picture;
  746. if(s->picture_structure == s->field_select[dir][i] + 1
  747. || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
  748. ref2picture= ref_picture;
  749. }else{
  750. ref2picture = s->current_picture_ptr->f.data;
  751. }
  752. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  753. s->field_select[dir][i],
  754. ref2picture, pix_op,
  755. s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8, mb_y>>1);
  756. dest_y += 16*s->linesize;
  757. dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
  758. dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
  759. }
  760. break;
  761. case MV_TYPE_DMV:
  762. if(s->picture_structure == PICT_FRAME){
  763. for(i=0; i<2; i++){
  764. int j;
  765. for(j=0; j<2; j++){
  766. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  767. j, j^i, ref_picture, pix_op,
  768. s->mv[dir][2*i + j][0],
  769. s->mv[dir][2*i + j][1], 8, mb_y);
  770. }
  771. pix_op = s->dsp.avg_pixels_tab;
  772. }
  773. }else{
  774. for(i=0; i<2; i++){
  775. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  776. s->picture_structure != i+1,
  777. ref_picture, pix_op,
  778. s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
  779. // after put we make avg of the same block
  780. pix_op=s->dsp.avg_pixels_tab;
  781. //opposite parity is always in the same frame if this is second field
  782. if(!s->first_field){
  783. ref_picture = s->current_picture_ptr->f.data;
  784. }
  785. }
  786. }
  787. break;
  788. default: av_assert2(0);
  789. }
  790. }
  791. void ff_MPV_motion(MpegEncContext *s,
  792. uint8_t *dest_y, uint8_t *dest_cb,
  793. uint8_t *dest_cr, int dir,
  794. uint8_t **ref_picture,
  795. op_pixels_func (*pix_op)[4],
  796. qpel_mc_func (*qpix_op)[16])
  797. {
  798. #if !CONFIG_SMALL
  799. if(s->out_format == FMT_MPEG1)
  800. MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
  801. ref_picture, pix_op, qpix_op, 1);
  802. else
  803. #endif
  804. MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
  805. ref_picture, pix_op, qpix_op, 0);
  806. }