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.

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