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.

892 lines
34KB

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