[关闭]
@snuffles 2015-10-20T15:56:13.000000Z 字数 8385 阅读 2067

整像素运动估计代码分析

大作业


作业三
1. 找出ITM软件中整像素运动估计模块代码,并对搜索过程做简要分析
2. 分析openCV全局运动代码,描述其过程

reference
UMHexagonS算法代码有自己的注释


  1. 整像素运动估计的函数的定义在fastme.c中
  2. 其中fastme.c包括整像素估计和分像素估计算法(Fast integer pel motion estimation and fractional pel motion estimation)。主要包括
    1. get_mem_FME() and free_mem_FME() are functions for allocation and release
      of memories about motion estimation
    2. FME_BlockMotionSearch() is the function for fast integer pel motion
      estimation and fractional pel motion estimation
    3. DefineThreshold() defined thresholds for early termination
  3. 其中整像素估计的算法是UMHexagonS算法。分为4个步骤
    1. 步骤一:起始搜索点的预测,五种预测模式求预测运动矢量
    (1)中值预测:利用空间相关性,取已求出的、当前帧的左、上、右上邻块的运动矢量的中间值;
    (2)原点预测:取原点处;
    (3)上层预测:采用从模式1(16*16)到模式7(4*4)的分级搜索顺序,将已求出的当前位置的上一级模式运动向量的作为当前模式的运动向量的预测值
    (4)对应块预测:将前一帧相应位置的运动向量作为当前块的运动向量的预测值;
    (5)相邻参考帧预测:利用时间相关性,将前面参考帧的运动向量按时间进行缩放。
    2. 步骤二:不甚满意区块搜索(对应代码里面first_step)
    (1)以最佳起始搜索点为中心,用非对称十字型搜索模板进行搜索,获得当前最佳点,判断此处是否属于满意或者很满意区,跳到相应的步骤三或步骤四,或继续搜索;
    (2)以当前最佳点为中心,在[-2,2]的方形区域(5*5)中进行全搜索,获得当前最佳点,判断是否属于满意或很满意区,跳到相应的步骤三或步骤四,或继续搜索;
    (3)用大六边形模板(16点)进行搜索,直至搜索到能符合相应阈值而进入步骤三或步骤四的点为止,否则也结束步骤二的搜索而进入步骤三。
    3. 步骤三:满意区的块搜索(对应代码里面sec_step)
    以当前最佳点为中心进行六边形搜索,直至最佳点为六边形中心。
    4. 步骤四:很满意区的搜索(对应代码里面third_step)
    以当前最佳点为中心进行菱形搜索,直至最佳点为菱形中心。

  4. 代码如下

  1. /*
  2. *************************************************************************
  3. * Function: FastIntegerPelBlockMotionSearch: fast pixel block motion search
  4. this algrithm is called UMHexagonS which includes
  5. four steps with different kinds of search patterns
  6. * Input:
  7. pel_t** orig_pic, // <-- original picture
  8. int ref, // <-- reference frame (0... or -1 (backward))
  9. int pic_pix_x, // <-- absolute x-coordinate of regarded AxB block
  10. int pic_pix_y, // <-- absolute y-coordinate of regarded AxB block
  11. int blocktype, // <-- block type (1-16x16 ... 7-4x4)
  12. int pred_mv_x, // <-- motion vector predictor (x) in sub-pel units
  13. int pred_mv_y, // <-- motion vector predictor (y) in sub-pel units
  14. int* mv_x, // --> motion vector (x) - in pel units
  15. int* mv_y, // --> motion vector (y) - in pel units
  16. int search_range, // <-- 1-d search range in pel units
  17. int min_mcost, // <-- minimum motion cost (cost for center or huge value)
  18. double lambda // <-- lagrangian parameter for determining motion cost
  19. * Output:
  20. * Return:
  21. * Attention: in this function, three macro definitions is gives,
  22. SEARCH_ONE_PIXEL: search one pixel in search range
  23. SEARCH_ONE_PIXEL1(value_iAbort): search one pixel in search range,
  24. but give a parameter to show if mincost refeshed
  25. *************************************************************************
  1. int // ==> minimum motion cost after search
  2. FastIntegerPelBlockMotionSearch (pel_t** orig_pic, // <-- not used
  3. int ref, // <-- reference frame (0... or -1 (backward))
  4. int pic_pix_x, // <-- absolute x-coordinate of regarded AxB block
  5. int pic_pix_y, // <-- absolute y-coordinate of regarded AxB block
  6. int blocktype, // <-- block type (1-16x16 ... 7-4x4)
  7. int pred_mv_x, // <-- motion vector predictor (x) in sub-pel units
  8. int pred_mv_y, // <-- motion vector predictor (y) in sub-pel units
  9. int* mv_x, // --> motion vector (x) - in pel units
  10. int* mv_y, // --> motion vector (y) - in pel units
  11. int search_range, // <-- 1-d search range in pel units
  12. int min_mcost, // <-- minimum motion cost (cost for center or huge value)
  13. double lambda) // <-- lagrangian parameter for determining motion cost
  14. {
  15. static int Diamond_x[4] = {-1, 0, 1, 0};
  16. static int Diamond_y[4] = {0, 1, 0, -1};
  17. static int Hexagon_x[6] = {2, 1, -1, -2, -1, 1};
  18. static int Hexagon_y[6] = {0, -2, -2, 0, 2, 2};
  19. static int Big_Hexagon_x[16] = {0,-2, -4,-4,-4, -4, -4, -2, 0, 2, 4, 4, 4, 4, 4, 2};
  20. static int Big_Hexagon_y[16] = {4, 3, 2, 1, 0, -1, -2, -3, -4, -3, -2, -1, 0, 1, 2, 3};
  21. int pos, cand_x, cand_y, mcost;
  22. pel_t *(*get_ref_line)(int, pel_t*, int, int);
  23. pel_t* ref_pic = img->type==B_IMG? Refbuf11 [ref+1] : Refbuf11[ref];
  24. int best_pos = 0; // position with minimum motion cost
  25. int max_pos = (2*search_range+1)*(2*search_range+1); // number of search positions
  26. int lambda_factor = LAMBDA_FACTOR (lambda); // factor for determining lagragian motion cost
  27. int mvshift = 2; // motion vector shift for getting sub-pel units
  28. int blocksize_y = input->blc_size[blocktype][1]; // vertical block size
  29. int blocksize_x = input->blc_size[blocktype][0]; // horizontal block size
  30. int blocksize_x4 = blocksize_x >> 2; // horizontal block size in 4-pel units
  31. int pred_x = (pic_pix_x << mvshift) + pred_mv_x; // predicted position x (in sub-pel units)
  32. int pred_y = (pic_pix_y << mvshift) + pred_mv_y; // predicted position y (in sub-pel units)
  33. int center_x = pic_pix_x + *mv_x; // center position x (in pel units)
  34. int center_y = pic_pix_y + *mv_y; // center position y (in pel units)
  35. int best_x, best_y;
  36. int check_for_00 = (blocktype==1 && !input->rdopt && img->type!=B_IMG && ref==0);
  37. int search_step,iYMinNow, iXMinNow;
  38. int i,m, iSADLayer;
  39. int iAbort;
  40. float betaSec,betaThird;
  41. int height = img->height;
  42. //===== set function for getting reference picture lines =====
  43. if ((center_x > search_range) && (center_x < img->width -1-search_range-blocksize_x) &&
  44. (center_y > search_range) && (center_y < height-1-search_range-blocksize_y) )
  45. {
  46. get_ref_line = FastLineX;
  47. }
  48. else
  49. {
  50. get_ref_line = UMVLineX;
  51. }
  52. memset(McostState[0],0,(2*search_range+1)*(2*search_range+1)*4);
  53. ///////////////////////////////////////////////////////////////
  54. #ifdef MRF_HYU
  55. #ifdef MRF_RAND_P
  56. if(img->type == B_IMG && ref > 0)
  57. #else
  58. if((input->low_delay && img->type != INTER_IMG && ref>0)||(input->low_delay == 0 && ref > 0))
  59. #endif
  60. #else
  61. if(ref>0)
  62. #endif
  63. {
  64. if(pred_SAD_ref!=0)
  65. {
  66. betaSec = Bsize[blocktype]/(pred_SAD_ref*pred_SAD_ref)-AlphaSec[blocktype];
  67. betaThird = Bsize[blocktype]/(pred_SAD_ref*pred_SAD_ref)-AlphaThird[blocktype];
  68. }
  69. else
  70. {
  71. betaSec = 0;
  72. betaThird = 0;
  73. }
  74. }
  75. else
  76. {
  77. if(blocktype==1)
  78. {
  79. if(pred_SAD_space !=0)
  80. {
  81. betaSec = Bsize[blocktype]/(pred_SAD_space*pred_SAD_space)-AlphaSec[blocktype];
  82. betaThird = Bsize[blocktype]/(pred_SAD_space*pred_SAD_space)-AlphaThird[blocktype];
  83. }
  84. else
  85. {
  86. betaSec = 0;
  87. betaThird = 0;
  88. }
  89. }
  90. else
  91. {
  92. if(pred_SAD_uplayer !=0)
  93. {
  94. betaSec = Bsize[blocktype]/(pred_SAD_uplayer*pred_SAD_uplayer)-AlphaSec[blocktype];
  95. betaThird = Bsize[blocktype]/(pred_SAD_uplayer*pred_SAD_uplayer)-AlphaThird[blocktype];
  96. }
  97. else
  98. {
  99. betaSec = 0;
  100. betaThird = 0;
  101. }
  102. }
  103. }
  104. /*****************************/
  105. ////////////search around the predictor and (0,0)
  106. //check the center median predictor
  107. cand_x = center_x ;
  108. cand_y = center_y ;
  109. mcost = MV_COST (lambda_factor, mvshift, cand_x, cand_y, pred_x, pred_y);
  110. mcost = PartCalMad(ref_pic, orig_pic, get_ref_line,blocksize_y,blocksize_x,blocksize_x4,mcost,min_mcost,cand_x,cand_y);
  111. McostState[search_range][search_range] = mcost;
  112. if (mcost < min_mcost)
  113. {
  114. min_mcost = mcost;
  115. best_x = cand_x;
  116. best_y = cand_y;
  117. }
  118. iXMinNow = best_x;
  119. iYMinNow = best_y;
  120. for (m = 0; m < 4; m++)
  121. {
  122. cand_x = iXMinNow + Diamond_x[m];
  123. cand_y = iYMinNow + Diamond_y[m];
  124. SEARCH_ONE_PIXEL
  125. }
  126. if(center_x != pic_pix_x || center_y != pic_pix_y)
  127. {
  128. cand_x = pic_pix_x ;
  129. cand_y = pic_pix_y ;
  130. SEARCH_ONE_PIXEL
  131. iXMinNow = best_x;
  132. iYMinNow = best_y;
  133. for (m = 0; m < 4; m++)
  134. {
  135. cand_x = iXMinNow + Diamond_x[m];
  136. cand_y = iYMinNow + Diamond_y[m];
  137. SEARCH_ONE_PIXEL
  138. }
  139. }
  140. if(blocktype>1)
  141. {
  142. cand_x = pic_pix_x + (pred_MV_uplayer[0]/4);
  143. cand_y = pic_pix_y + (pred_MV_uplayer[1]/4);
  144. SEARCH_ONE_PIXEL
  145. if ((min_mcost-pred_SAD_uplayer)<pred_SAD_uplayer*betaThird)
  146. goto third_step;
  147. else if((min_mcost-pred_SAD_uplayer)<pred_SAD_uplayer*betaSec)
  148. goto sec_step;
  149. }
  150. //coordinate position prediction
  151. if ((img->number > 1 + ref && ref!=-1) || (ref == -1 && Bframe_ctr > 1))
  152. {
  153. cand_x = pic_pix_x + pred_MV_time[0]/4;
  154. cand_y = pic_pix_y + pred_MV_time[1]/4;
  155. SEARCH_ONE_PIXEL
  156. }
  157. //prediciton using mV of last ref moiton vector
  158. if ((ref > 0) || (img->type == B_IMG && ref == 0))
  159. {
  160. cand_x = pic_pix_x + pred_MV_ref[0]/4;
  161. cand_y = pic_pix_y + pred_MV_ref[1]/4;
  162. SEARCH_ONE_PIXEL
  163. }
  164. //strengthen local search
  165. iXMinNow = best_x;
  166. iYMinNow = best_y;
  167. for (m = 0; m < 4; m++)
  168. {
  169. cand_x = iXMinNow + Diamond_x[m];
  170. cand_y = iYMinNow + Diamond_y[m];
  171. SEARCH_ONE_PIXEL
  172. }
  173. EARLY_TERMINATION
  174. if(blocktype>6)
  175. goto sec_step;
  176. else
  177. goto first_step;
  178. first_step: //Unsymmetrical-cross search
  179. iXMinNow = best_x;
  180. iYMinNow = best_y;
  181. for(i=1;i<=search_range/2;i++)
  182. {
  183. search_step = 2*i - 1;
  184. cand_x = iXMinNow + search_step;
  185. cand_y = iYMinNow ;
  186. SEARCH_ONE_PIXEL
  187. cand_x = iXMinNow - search_step;
  188. cand_y = iYMinNow ;
  189. SEARCH_ONE_PIXEL
  190. }
  191. for(i=1;i<=search_range/4;i++)
  192. {
  193. search_step = 2*i - 1;
  194. cand_x = iXMinNow ;
  195. cand_y = iYMinNow + search_step;
  196. SEARCH_ONE_PIXEL
  197. cand_x = iXMinNow ;
  198. cand_y = iYMinNow - search_step;
  199. SEARCH_ONE_PIXEL
  200. }
  201. EARLY_TERMINATION
  202. iXMinNow = best_x;
  203. iYMinNow = best_y;
  204. // Uneven Multi-Hexagon-grid Search
  205. for(pos=1;pos<25;pos++)
  206. {
  207. cand_x = iXMinNow + spiral_search_x[pos];
  208. cand_y = iYMinNow + spiral_search_y[pos];
  209. SEARCH_ONE_PIXEL
  210. }
  211. EARLY_TERMINATION
  212. for(i=1;i<=search_range/4; i++)
  213. {
  214. iAbort = 0;
  215. for (m = 0; m < 16; m++)
  216. {
  217. cand_x = iXMinNow + Big_Hexagon_x[m]*i;
  218. cand_y = iYMinNow + Big_Hexagon_y[m]*i;
  219. SEARCH_ONE_PIXEL1(1)
  220. }
  221. if (iAbort)
  222. {
  223. EARLY_TERMINATION
  224. }
  225. }
  226. sec_step: //Extended Hexagon-based Search
  227. iXMinNow = best_x;
  228. iYMinNow = best_y;
  229. for(i=0;i<search_range;i++)
  230. {
  231. iAbort = 1;
  232. for (m = 0; m < 6; m++)
  233. {
  234. cand_x = iXMinNow + Hexagon_x[m];
  235. cand_y = iYMinNow + Hexagon_y[m];
  236. SEARCH_ONE_PIXEL1(0)
  237. }
  238. if(iAbort)
  239. break;
  240. iXMinNow = best_x;
  241. iYMinNow = best_y;
  242. }
  243. third_step: // the third step with a small search pattern
  244. iXMinNow = best_x;
  245. iYMinNow = best_y;
  246. for(i=0;i<search_range;i++)
  247. {
  248. iSADLayer = 65536;
  249. iAbort = 1;
  250. for (m = 0; m < 4; m++)
  251. {
  252. cand_x = iXMinNow + Diamond_x[m];
  253. cand_y = iYMinNow + Diamond_y[m];
  254. SEARCH_ONE_PIXEL1(0)
  255. }
  256. if(iAbort)
  257. break;
  258. iXMinNow = best_x;
  259. iYMinNow = best_y;
  260. }
  261. *mv_x = best_x - pic_pix_x;
  262. *mv_y = best_y - pic_pix_y;
  263. return min_mcost;
  264. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注