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.

910 lines
34KB

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