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