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.

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