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.

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