JM86中encode_one_macroblock函数注释

  有一小部分是别人写的,大部分是我在看代码的过程中根据自己的理解所写的,如有什么不对的地方,还望大家指教~~~~
  1. /*!
  2. *************************************************************************************
  3. * /brief
  4. * Mode Decision for a macroblock
  5. * //+++该函数的作用是编码一个宏块(包括帧间、帧内、帧内预测的方式)。
  6. * NOTE:从上面程序段中可以看出JM8.5中对7种宏块模式是采用全部遍历的方式,所以导致的计算复杂度很高。
  7. *************************************************************************************
  8. */
  9. void encode_one_macroblock ()
  10. {
  11. static const int b8_mode_table[6] = {0, 4, 5, 6, 7}; // DO NOT CHANGE ORDER !!!
  12. // //++ B片子宏块类型。0:Direct_8*8,4:8*8,5:8*4,6:4*8,7:4*4
  13. static const int mb_mode_table[7] = {0, 1, 2, 3, P8x8, I16MB, I4MB}; // DO NOT CHANGE ORDER !!!
  14. //+++// 0:16X16 Direct模式,在B帧中有效
  15. // 1:Inter16X16,在帧间有效
  16. // 2:Inter16X8,在帧间有效
  17. // 3:Inter8X16,在帧间有效
  18. // P8X8:帧间有效:包括Inter8x8,Inter8x4,Inter4x8,Inter4x4
  19. // I16MB:Intra16X16帧内有效
  20. // I4MB:Intra有效
  21. // I8MB:Intra有效
  22. // IPCM:Intra有效,不要预测,直接对RAW数据编码.
  23. int valid[MAXMODE];
  24. int rerun, block, index, mode, i0, i1, j0, j1, pdir, ref, i, j, k, ctr16x16, dummy;
  25. double qp, lambda_mode, lambda_motion, min_rdcost, rdcost = 0, max_rdcost=1e30;
  26. int lambda_motion_factor;
  27. int fw_mcost, bw_mcost, bid_mcost, mcost, max_mcost=(1<<30);
  28. int curr_cbp_blk/*+++当前宏块每个 4*4 块是否有非零系数,跟CBP功能类似,CBP 是表示的每个8*8 块*/,
  29. cnt_nonz = 0, best_cnt_nonz = 0, best_fw_ref = 0, best_pdir;
  30. int cost=0;
  31. int min_cost = max_mcost, min_cost8x8, cost8x8, cost_direct=0, have_direct=0, i16mode;
  32. //+++根据对下面代码的阅读,这儿有比较要说明一下上面声明的一些变量,这对于理解代码很重要.
  33. //+++首先,要提到best_mode,我本来以为这个量是在本函数中声明的一个变量,然后仔细在上面寻找,都没有找到,后来才发现,
  34. //+++wow,原来best_mode在这个文件(rdopt.c)的开头声明过,是本文件的一个全局变量,yes,终于明白这个了
  35. //+++其次,对于上面的一些变量,介绍一下:
  36. //+++min_rdcost是用来保存RDO模式下的最小代价;min_cost则是非RDO模式下的代价
  37. //+++min_cost8x8是亚宏块级(P8x8)非RDO模式下最小模式代价;cost8x8是宏块(16x16)中4个8x8块的代价和(每一个8x8都是最佳代价min_cost8x8)
  38. //+++fw_mcost,bw_mcost和bid_mcost是中间变量用来记录(前向,后向和双向)最佳参考帧对应的代价
  39. //+++cost是一个中间变量,主要是在非RDO模式下,(帧间预测)记录宏块级或亚宏块级中某一个模式中所有块(16x16是1块,16x8是2块...)的所有代价之和(参考帧代价,运动估计)
  40. //+++mcost也是一个中间变量,也是非RDO模式下,在进行最优参考帧时,用于累加代价之和(参考帧代价,运动估计)
  41. int intra1 = 0;
  42. int intra = (((img->type==P_SLICE||img->type==SP_SLICE) && img->mb_y==img->mb_y_upd && img->mb_y_upd!=img->mb_y_intra) || img->type==I_SLICE);
  43. int spframe = (img->type==SP_SLICE);
  44. int siframe = (img->type==SI_SLICE);
  45. int bframe = (img->type==B_SLICE);
  46. int runs = (input->RestrictRef==1 && input->rdopt==2 && (img->type==P_SLICE || img->type==SP_SLICE || (img->type==B_SLICE && img->nal_reference_idc>0)) ? 2 : 1);
  47. int checkref = (input->rdopt && input->RestrictRef && (img->type==P_SLICE || img->type==SP_SLICE));
  48. Macroblock* currMB = &img->mb_data[img->current_mb_nr];
  49. Macroblock* prevMB = img->current_mb_nr ? &img->mb_data[img->current_mb_nr-1]:NULL ;
  50. int **ipredmodes = img->ipredmode;//+++ipredmode帧内预测模式
  51. int best_bw_ref = -1;
  52. int ******allmvs = img->all_mv;
  53. int l,list_offset;
  54. int curr_mb_field = ((img->MbaffFrameFlag)&&(currMB->mb_field));
  55. // find out the correct list offsets
  56. if (curr_mb_field)
  57. {
  58. if(img->current_mb_nr%2)
  59. list_offset = 4; // bottom field mb
  60. else
  61. list_offset = 2; // top field mb
  62. }
  63. else
  64. {
  65. list_offset = 0; // no mb aff or frame mb
  66. }
  67. if(input->FMEnable)
  68. decide_intrabk_SAD();
  69. intra |= RandomIntra (img->current_mb_nr); // Forced Pseudo-Random Intra
  70. //===== SET VALID MODES =====
  71. valid[I4MB] = 1; //++ 宏块可以采用intra4*4模式编码
  72. valid[I16MB] = 1; //++ 宏块可以采用intra16*16模式编码
  73. valid[0] = (!intra ); //++ 宏块是否采用inter模式编码
  74. valid[1] = (!intra && input->InterSearch16x16); //++ 宏块是否采用inter16*16模式编码
  75. valid[2] = (!intra && input->InterSearch16x8); //++ 宏块是否采用inter16*8模式编码
  76. valid[3] = (!intra && input->InterSearch8x16); //++ 宏块是否采用inter8*16模式编码
  77. valid[4] = (!intra && input->InterSearch8x8); //++ 宏块是否采用inter8*8模式编码
  78. valid[5] = (!intra && input->InterSearch8x4); //++ 宏块是否采用inter8*4模式编码
  79. valid[6] = (!intra && input->InterSearch4x8); //++ 宏块是否采用inter4*8模式编码
  80. valid[7] = (!intra && input->InterSearch4x4); //++ 宏块是否采用inter4*4模式编码
  81. valid[P8x8] = (valid[4] || valid[5] || valid[6] || valid[7]); //++ 宏块是否采用亚宏块模式编码 +++P8x8=8
  82. valid[12] = (siframe); //++ 宏块是否采用SI模式编码
  83. if (!img->MbaffFrameFlag)//+++//在非宏块级帧场自适应且是场模式下 进行色度矢量校正
  84. {
  85. for (l=0+list_offset;l<(2+list_offset);l++)
  86. {
  87. for(k = 0; k < listXsize[l]; k++)
  88. {
  89. listX[l][k]->chroma_vector_adjustment= 0;
  90. if(img->structure == TOP_FIELD && img->structure != listX[l][k]->structure)
  91. listX[l][k]->chroma_vector_adjustment = -2;
  92. if(img->structure == BOTTOM_FIELD && img->structure != listX[l][k]->structure)
  93. listX[l][k]->chroma_vector_adjustment = 2;
  94. }
  95. }
  96. }
  97. else
  98. {
  99. if (curr_mb_field)
  100. {
  101. for (l=0+list_offset;l<(2+list_offset);l++)
  102. {
  103. for(k = 0; k < listXsize[l]; k++)
  104. {
  105. listX[l][k]->chroma_vector_adjustment= 0;
  106. if(img->current_mb_nr % 2 == 0 && listX[l][k]->structure == BOTTOM_FIELD)
  107. listX[l][k]->chroma_vector_adjustment = -2;
  108. if(img->current_mb_nr % 2 == 1 && listX[l][k]->structure == TOP_FIELD)
  109. listX[l][k]->chroma_vector_adjustment = 2;
  110. }
  111. }
  112. }
  113. else
  114. {
  115. for (l=0+list_offset;l<(2+list_offset);l++)
  116. {
  117. for(k = 0; k < listXsize[l]; k++)
  118. {
  119. listX[l][k]->chroma_vector_adjustment= 0;
  120. }
  121. }
  122. }
  123. }
  124. //+++ 设置RDO与非RDO下的lambda系数
  125. //===== SET LAGRANGE PARAMETERS =====
  126. if (input->rdopt)
  127. {
  128. qp = (double)img->qp - SHIFT_QP; //++ ???
  129. if (input->successive_Bframe>0)
  130. lambda_mode = 0.68 * pow (2, qp/3.0) * (img->type==B_SLICE? max(2.00,min(4.00,(qp / 6.0))):spframe?max(1.4,min(3.0,(qp / 12.0))):1.0);
  131. else
  132. lambda_mode = 0.85 * pow (2, qp/3.0) * (img->type==B_SLICE? 4.0:spframe?max(1.4,min(3.0,(qp / 12.0))):1.0);
  133. lambda_motion = sqrt (lambda_mode);
  134. }
  135. else
  136. {
  137. lambda_mode = lambda_motion = QP2QUANT[max(0,img->qp-SHIFT_QP)];
  138. }
  139. lambda_motion_factor = LAMBDA_FACTOR (lambda_motion); //++ 使用该变量的原因主要是为了在运算过程中使用定点运算但又能提高精度
  140. for (rerun=0; rerun<runs; rerun++) //++ runs=2是针对loss rdo模式,其余的情况runs=1
  141. {
  142. if (runs==2)
  143. {
  144. if (rerun==0) input->rdopt=1;
  145. else input->rdopt=2;
  146. }
  147. // reset chroma intra predictor to default
  148. currMB->c_ipred_mode = DC_PRED_8;
  149. //+++++++++++++++++++++++++++
  150. //+++帧间预测,计算其选择合适模式后的开销
  151. //+++(根据拉格朗日率失真准则)
  152. if (!intra)
  153. {
  154. //+++B帧的直接模式预测
  155. //===== set direct motion vectors =====
  156. if (bframe)//+++P帧无direct模式 但有skip模式
  157. {
  158. Get_Direct_Motion_Vectors ();//+++不返回direct模式代价
  159. }
  160. //+++//宏块级运动估计
  161. //===== MOTION ESTIMATION FOR 16x16, 16x8, 8x16 BLOCKS =====
  162. for (min_cost=1<<20, best_mode=1, mode=1; mode<4; mode++)
  163. {
  164. if (valid[mode])//+++//对应于程序外部(即CFG文件中)的设置
  165. {
  166. //+++//对于16x16,MB只分一个块;对于16x8和8x16,MB被分成两个块
  167. for (cost=0, block=0; block<(mode==1?1:2); block++) //++ 16*16 分割方式只需要计算一次,16*8 和 8*16 分割方式需要计算两次
  168. {
  169. //+++/块匹配!!!lambda_motion用来求运动矢量消耗的码率
  170. PartitionMotionSearch (mode, block, lambda_motion); //++ 对 16*16、16*8、8*16 分割方式进行 ME
  171. //--- set 4x4 block indizes (for getting MV) ---
  172. j = (block==1 && mode==2 ? 2 : 0); //++ 如果现在处理的是 16*8 分割方式的第 2 个分割,则 j=2
  173. i = (block==1 && mode==3 ? 2 : 0); //++ 如果现在处理的是 8*16 分割方式的第 2 个分割,则 i=2
  174. //+++通过循环在list0中寻找前向预测的最佳参考帧(best_fw_ref),代价为fw_mcost,
  175. //--- get cost and reference frame for forward prediction ---
  176. for (fw_mcost=max_mcost, ref=0; ref<listXsize[LIST_0+list_offset]; ref++)
  177. {
  178. if (!checkref || ref==0 || CheckReliabilityOfRef (block, LIST_0, ref, mode))
  179. {
  180. //++ 参考帧索引号编码所需要使用的比特数作为编码代价的一部分
  181. //+++这个地方就是利用拉格朗日进行计算开销的函数,事实上,这个地方要考虑很多东西的开销,可以发现,下面的代码中还要加上其他的
  182. //+++mcost->(1)REF_COST参考帧代价
  183. mcost = (input->rdopt ? REF_COST (lambda_motion_factor, ref, LIST_0 + list_offset) : (int)(2*lambda_motion*min(ref,1)));
  184. //+++motion_cost(运动搜索代价)是一个全局变量,其值在PartitionMotionSearch函数内进行过改变
  185. //+++so 此时motion_cost就是保存的相应的运动搜索代价
  186. //+++那么,mcost->(2)motion_cost运动搜索代价
  187. mcost += motion_cost[mode][LIST_0][ref][block];//+++可以发现motion_cost数组的参数mode,block和
  188. //+++PartitionMotionSearch(PMS)函数的参数是一致的,并且在PMS函数中
  189. //+++计算motion_cost时也是对ref(参考帧)进行循环的,所以将两者联系起来
  190. //+++我们就可以理解这儿motion_cost的数组中参数的含义了.
  191. //+++另外在[JM86核心函数研究]中有对motion_cost的详细介绍
  192. //+++该数组存储了一个宏块的分块在不同帧间模式和参考帧下的运动估计的开销值。
  193. //+++函数PartitionMotionSearch的主要任务就是更新该数组。同时,该数组也在决定最佳参考帧时被引用。
  194. if (mcost < fw_mcost)//+++根据参考代价选出该模式下的最佳参考帧
  195. {
  196. //+++记录最佳参考帧及预测代价
  197. fw_mcost = mcost;
  198. best_fw_ref = ref;
  199. }
  200. }
  201. }
  202. //+++B帧还需记录向后及双向预测的各个参考帧与参考代价
  203. if (bframe)
  204. {
  205. //--- get cost for bidirectional prediction ---
  206. for (bw_mcost=max_mcost, ref=0; ref<listXsize[LIST_1 + list_offset]; ref++)
  207. {//+++B帧的计算方法与I,P帧不一样
  208. mcost = (input->rdopt ? REF_COST (lambda_motion_factor, ref, LIST_1 + list_offset) : (int)(2*lambda_motion*min(ref,1)));
  209. mcost += motion_cost[mode][LIST_1][ref][block];
  210. if (mcost < bw_mcost)
  211. {
  212. bw_mcost = mcost;
  213. best_bw_ref = ref;
  214. }
  215. }
  216. // search bidirectional between best forward and ref_idx=0 backward
  217. bid_mcost = (input->rdopt ? (REF_COST (lambda_motion_factor, best_fw_ref,LIST_0+list_offset)+REF_COST (lambda_motion_factor, 0,LIST_1+list_offset)) : (int)(2*lambda_motion*min(best_fw_ref,1)));
  218. bid_mcost += BIDPartitionCost (mode, block, best_fw_ref, 0, lambda_motion_factor);
  219. //--- get prediction direction ----
  220. if (fw_mcost<=bw_mcost && fw_mcost<=bid_mcost)
  221. {
  222. best_pdir = 0;
  223. best_bw_ref = 0;
  224. cost += fw_mcost;
  225. }
  226. else if (bw_mcost<=fw_mcost && bw_mcost<=bid_mcost)
  227. {
  228. best_pdir = 1;
  229. cost += bw_mcost;
  230. best_fw_ref = 0;
  231. }
  232. else
  233. {
  234. best_pdir = 2;
  235. cost += bid_mcost;
  236. best_bw_ref = 0;
  237. }
  238. }
  239. else // if (bframe)
  240. {
  241. best_pdir = 0;//+++best_pdir是最佳预测方向,对于非B帧(P帧)预测方向就一个直接设置为0,而对于
  242. //+++B帧要从三种方式(前向,后向,双向)中选择一个,从上面if中的代码中我们就可以看出来
  243. cost += fw_mcost;
  244. }
  245. //+++下面要记录参考帧和MV信息,说明一下:这些全局变量是作为BlockMotionSearch()中运动搜索时运动向量预测的依据
  246. //+++问:下一宏块的运动向量等信息也用当前宏块的运动向量预测吗?答:ME 算法是开放部分,你想怎么做就怎么做。JM 在 ME 时会用到相邻块的 MV;
  247. //+++对于MB是以4*4的块为单位来记录mv和ref的
  248. if (mode==1)//+++16x16
  249. {
  250. if (best_pdir==1)//+++后向参考
  251. {
  252. for (j=0; j<4; j++)
  253. {
  254. for (i=0; i<4; i++)
  255. {
  256. //+++保存参考信息至重建单元,B帧多做后项参考
  257. //+++enc_picture 中好像是最终数据,img 中存的是中间数据
  258. enc_picture->ref_idx[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = -1;
  259. enc_picture->ref_pic_id [LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = -1;
  260. enc_picture->mv[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j][0] = 0;
  261. enc_picture->mv[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j][1] = 0;
  262. }
  263. }
  264. }
  265. else
  266. {
  267. for (j=0; j<4; j++)
  268. {
  269. for (i=0; i<4; i++)
  270. {
  271. enc_picture->ref_idx[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = best_fw_ref;
  272. enc_picture->ref_pic_id [LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = enc_picture->ref_pic_num[LIST_0 + list_offset][enc_picture->ref_idx[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j]];
  273. enc_picture->mv[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j][0] = img->all_mv[i][j][LIST_0][best_fw_ref][mode][0];
  274. enc_picture->mv[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j][1] = img->all_mv[i][j][LIST_0][best_fw_ref][mode][1];
  275. }
  276. }
  277. }
  278. if (bframe)
  279. {
  280. if (best_pdir==0)
  281. {
  282. for (j=0; j<4; j++)
  283. {
  284. for (i=0; i<4; i++)
  285. {
  286. enc_picture->ref_idx[LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = -1;
  287. enc_picture->ref_pic_id [LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = -1;
  288. enc_picture->mv[LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j][0] = 0;
  289. enc_picture->mv[LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j][1] = 0;
  290. }
  291. }
  292. }
  293. else
  294. {
  295. for (j=0; j<4; j++)
  296. {
  297. for (i=0; i<4; i++)
  298. {
  299. enc_picture->ref_idx[LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = best_bw_ref;
  300. enc_picture->ref_pic_id [LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = enc_picture->ref_pic_num[LIST_1 + list_offset][enc_picture->ref_idx[LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j]];
  301. if(best_bw_ref>=0)
  302. {
  303. enc_picture->mv[LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j][0] = img->all_mv[i][j][LIST_1][best_bw_ref][mode][0];
  304. enc_picture->mv[LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j][1] = img->all_mv[i][j][LIST_1][best_bw_ref][mode][1];
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. else if (mode==2)//+++16x8
  312. {
  313. for (j=0; j<2; j++)
  314. {
  315. for (i=0; i<4; i++)
  316. {
  317. if (best_pdir==1)
  318. {
  319. enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+block*2+j] = -1;
  320. enc_picture->ref_pic_id [LIST_0][img->block_x+i][img->block_y+block*2+j] = -1;
  321. enc_picture->mv[LIST_0][img->block_x+i][img->block_y+block*2+j][0] = 0;
  322. enc_picture->mv[LIST_0][img->block_x+i][img->block_y+block*2+j][1] = 0;
  323. }
  324. else
  325. {
  326. enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+block*2+j] = best_fw_ref;
  327. enc_picture->ref_pic_id [LIST_0][img->block_x+i][img->block_y+block*2+j] = enc_picture->ref_pic_num[LIST_0 + list_offset][enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+block*2+j]];
  328. enc_picture->mv[LIST_0][img->block_x+i][img->block_y+block*2+j][0] = img->all_mv[i][j+block*2][LIST_0][best_fw_ref][mode][0];
  329. enc_picture->mv[LIST_0][img->block_x+i][img->block_y+block*2+j][1] = img->all_mv[i][j+block*2][LIST_0][best_fw_ref][mode][1];
  330. }
  331. if (bframe)
  332. {
  333. if (best_pdir==0)
  334. {
  335. enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+block*2+j] = -1;
  336. enc_picture->ref_pic_id [LIST_1][img->block_x+i][img->block_y+block*2+j] = -1;
  337. enc_picture->mv[LIST_1][img->block_x+i][img->block_y+block*2+j][0] = 0;
  338. enc_picture->mv[LIST_1][img->block_x+i][img->block_y+block*2+j][1] = 0;
  339. }
  340. else
  341. {
  342. enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+block*2+j] = best_bw_ref;
  343. enc_picture->ref_pic_id [LIST_1][img->block_x+i][img->block_y+block*2+j] = enc_picture->ref_pic_num[LIST_1 + list_offset][enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+block*2+j]];
  344. if(best_bw_ref>=0)
  345. {
  346. enc_picture->mv[LIST_1][img->block_x+i][img->block_y+block*2+j][0] = img->all_mv[i][j][LIST_1][best_bw_ref][mode][0];
  347. enc_picture->mv[LIST_1][img->block_x+i][img->block_y+block*2+j][1] = img->all_mv[i][j][LIST_1][best_bw_ref][mode][1];
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. else//+++8x16
  355. {
  356. for (j=0; j<4; j++)
  357. {
  358. for (i=0; i<2; i++)
  359. {
  360. if (best_pdir==1)
  361. {
  362. enc_picture->ref_idx[LIST_0][img->block_x+block*2+i][img->block_y+j] = -1;
  363. enc_picture->ref_pic_id [LIST_0][img->block_x+block*2+i][img->block_y+j] = -1;
  364. enc_picture->mv[LIST_0][img->block_x+block*2+i][img->block_y+j][0] = 0;
  365. enc_picture->mv[LIST_0][img->block_x+block*2+i][img->block_y+j][1] = 0;
  366. }
  367. else
  368. {
  369. enc_picture->ref_idx[LIST_0][img->block_x+block*2+i][img->block_y+j] = best_fw_ref;
  370. enc_picture->ref_pic_id [LIST_0][img->block_x+block*2+i][img->block_y+j] = enc_picture->ref_pic_num[LIST_0 + list_offset][enc_picture->ref_idx[LIST_0][img->block_x+block*2+i][img->block_y+j]];
  371. enc_picture->mv[LIST_0][img->block_x+block*2+i][img->block_y+j][0] = img->all_mv[i][j][LIST_0][best_fw_ref][mode][0];
  372. enc_picture->mv[LIST_0][img->block_x+block*2+i][img->block_y+j][1] = img->all_mv[i][j][LIST_0][best_fw_ref][mode][1];
  373. }
  374. if (bframe)
  375. {
  376. if (best_pdir==0)
  377. {
  378. enc_picture->ref_idx[LIST_1][img->block_x+block*2+i][img->block_y+j] = -1;
  379. enc_picture->ref_pic_id [LIST_1][img->block_x+block*2+i][img->block_y+j] = -1;
  380. enc_picture->mv[LIST_1][img->block_x+block*2+i][img->block_y+j][0] = 0;
  381. enc_picture->mv[LIST_1][img->block_x+block*2+i][img->block_y+j][1] = 0;
  382. }
  383. else
  384. {
  385. enc_picture->ref_idx[LIST_1][img->block_x+block*2+i][img->block_y+j] = best_bw_ref;
  386. enc_picture->ref_pic_id [LIST_1][img->block_x+block*2+i][img->block_y+j] = enc_picture->ref_pic_num[LIST_1 + list_offset][enc_picture->ref_idx[LIST_1][img->block_x+block*2+i][img->block_y+j]];
  387. if(best_bw_ref>=0)
  388. {
  389. enc_picture->mv[LIST_1][img->block_x+block*2+i][img->block_y+j][0] = img->all_mv[i][j][LIST_1][best_bw_ref][mode][0];
  390. enc_picture->mv[LIST_1][img->block_x+block*2+i][img->block_y+j][1] = img->all_mv[i][j][LIST_1][best_bw_ref][mode][1];
  391. }
  392. }
  393. }
  394. }
  395. }
  396. }
  397. //+++以8x8块记录各个模式的预测信息 每个分割的8x8相同
  398. //+++此处解释一下best8x8fwref:: 该2维数组存储了一个宏块的4个8x8block在各个帧间预测模式下的最佳前向参考帧。
  399. //+++上面的代码完成了最佳参考帧的选择,所以这儿将最佳参考帧的信息存储在这个数组中。在计算预测值时,程序会引用该数组作为参考。
  400. //----- set reference frame and direction parameters -----
  401. if (mode==3)//+++两个16x8:如果以8x8为单位,分别拥有(0,2)和(1,3)
  402. {
  403. best8x8fwref [3][block] = best8x8fwref [3][block+2] = best_fw_ref;
  404. best8x8pdir [3][block] = best8x8pdir [3][block+2] = best_pdir;
  405. best8x8bwref [3][block] = best8x8bwref [3][block+2] = best_bw_ref;
  406. }
  407. else if (mode==2)//+++两个8x16:如果以8x8为单位,分别拥有(0,1)和(2,3)
  408. {
  409. best8x8fwref [2][2*block] = best8x8fwref [2][2*block+1] = best_fw_ref;
  410. best8x8pdir [2][2*block] = best8x8pdir [2][2*block+1] = best_pdir;
  411. best8x8bwref [2][2*block] = best8x8bwref [2][2*block+1] = best_bw_ref;
  412. }
  413. else//+++一个16x16:如果以8x8为单位,(0,1,2,3)的设置是一样的
  414. {
  415. best8x8fwref [1][0] = best8x8fwref [1][1] = best8x8fwref [1][2] = best8x8fwref [1][3] = best_fw_ref;
  416. best8x8pdir [1][0] = best8x8pdir [1][1] = best8x8pdir [1][2] = best8x8pdir [1][3] = best_pdir;
  417. best8x8bwref [1][0] = best8x8bwref [1][1] = best8x8bwref [1][2] = best8x8bwref [1][3] = best_bw_ref;
  418. }
  419. //--- set reference frames and motion vectors ---
  420. if (mode>1 && block==0) //+++mode>1,说明mode=2和3的时候执行,block==0,说明是对第一个分块.
  421. //+++这里说明一下:对于16x8,8x16来说,只有两个宏块分割,这里保存第一块分割的运动信息,目的是给下一个分割做运动矢量预测使用
  422. //+++由于前一个分割不需要下一个分割的运动矢量信息,因此只需要保存前一个分割即可.
  423. //+++另外,在这之前,事实上已经保存过,所以这里是重复保存了
  424. SetRefAndMotionVectors (block, mode, best_pdir, best_fw_ref, best_bw_ref);
  425. } // for (block=0; block<(mode==1?1:2); block++)
  426. //+++我在这个地方解释一下cost与min_cost:min_cost是为了寻找三种宏块模式(16x16,16x8,8x16)中代价最小的模式
  427. //+++由于宏块模式在计算的时候是进一步分成小块计算的,所以cost就是用来保存一种宏块模式下对应所有小块的代价之和
  428. //+++模式16x16下:cost对应一个16x16大小的块;模式16x8模式下:cost对应两个16x8大小的块......
  429. //+++所以下面这一个if语句就是在寻找代价最小的宏块模式(best_mode)
  430. if (cost < min_cost)
  431. {
  432. best_mode = mode;
  433. min_cost = cost;
  434. }
  435. } // if (valid[mode])
  436. } // for (mode=1; mode<4; mode++)//+++宏块级模式结束
  437. //+++现在宏块级运动估计结束,best_mode保存了最佳的宏块模式,min_cost记录其对应的代价
  438. //+++亚宏块级运动估计
  439. if (valid[P8x8])
  440. {
  441. cost8x8 = 0;
  442. //===== store coding state of macroblock =====
  443. store_coding_state (cs_mb);
  444. //+++遍历16x16宏块的4个8x8块
  445. //===== LOOP OVER 8x8 SUB-PARTITIONS (Motion Estimation & Mode Decision) =====
  446. for (cbp8x8=cbp_blk8x8=cnt_nonz_8x8=0, block=0; block<4; block++)
  447. {
  448. //--- set coordinates ---
  449. j0 = ((block/2)<<3); j1 = (j0>>2);
  450. i0 = ((block%2)<<3); i1 = (i0>>2);
  451. //+++对宏块中的每个8x8块的模式循环
  452. //+++这个地方解释一下:min_cost8x8用于记录非RDO模式下的最小代价,而min_rdcost则是RDO模式下的最小代价
  453. //===== LOOP OVER POSSIBLE CODING MODES FOR 8x8 SUB-PARTITION =====
  454. for (min_cost8x8=(1<<20), min_rdcost=1e30, index=(bframe?0:1); index<5; index++)
  455. {
  456. if (valid[mode=b8_mode_table[index]])//+++b8_mode_table[6] = {0, 4, 5, 6, 7};
  457. {
  458. curr_cbp_blk = 0;//+++初始化当前cbp_blk
  459. if (mode==0)
  460. {
  461. //--- Direct Mode ---
  462. if (!input->rdopt)
  463. {
  464. cost_direct += (cost = Get_Direct_Cost8x8 ( block, lambda_mode ));
  465. if (cost==1<<30)
  466. cost_direct = (1<<30);
  467. have_direct ++;//+++记录是否做过direct模式的标记
  468. }
  469. best_fw_ref = direct_ref_idx[LIST_0][img->block_x+(block&1)*2][img->block_y+(block&2)];
  470. best_bw_ref = direct_ref_idx[LIST_1][img->block_x+(block&1)*2][img->block_y+(block&2)];
  471. best_pdir = direct_pdir[img->block_x+(block&1)*2][img->block_y+(block&2)];
  472. } // if (mode==0)
  473. else//+++非direct模式下
  474. {
  475. //--- motion estimation for all reference frames ---
  476. PartitionMotionSearch (mode, block, lambda_motion);
  477. //+++这一部分与宏块级的类似
  478. //--- get cost and reference frame for forward prediction ---
  479. for (fw_mcost=max_mcost, ref=0; ref<listXsize[LIST_0+list_offset]; ref++)
  480. {
  481. if (!checkref || ref==0 || CheckReliabilityOfRef (block, LIST_0, ref, mode))
  482. {
  483. //+++(1)参考帧代价
  484. mcost = (input->rdopt ? REF_COST(lambda_motion_factor,ref,LIST_0+list_offset) : (int)(2*lambda_motion*min(ref,1)));
  485. //+++(2)MV代价
  486. mcost += motion_cost[mode][LIST_0][ref][block];
  487. if (mcost < fw_mcost)
  488. {
  489. fw_mcost = mcost;
  490. best_fw_ref = ref;
  491. }
  492. }
  493. }
  494. //store forward reference index for every block
  495. for (j=0; j<2; j++)
  496. for (i=0; i<2; i++)
  497. {
  498. enc_picture->ref_idx[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = best_fw_ref;
  499. enc_picture->ref_pic_id [LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = enc_picture->ref_pic_num[LIST_0 + list_offset][enc_picture->ref_idx[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j]];
  500. }
  501. //+++B帧有所不同,还要考虑后向和双向模式,其实与上面的代码是相似的
  502. if (bframe)
  503. {
  504. for (bw_mcost=max_mcost, ref=0; ref<listXsize[LIST_1+list_offset]; ref++)
  505. {
  506. mcost = (input->rdopt ? REF_COST(lambda_motion_factor,ref,LIST_1+list_offset) : (int)(2*lambda_motion*min(ref,1)));
  507. mcost += motion_cost[mode][LIST_1][ref][block];
  508. if (mcost < bw_mcost)
  509. {
  510. bw_mcost = mcost;
  511. best_bw_ref = ref;
  512. }
  513. }
  514. // bidirectional uses best forward and zero backward reference
  515. bid_mcost = (input->rdopt ? (REF_COST (lambda_motion_factor, best_fw_ref, LIST_0 + list_offset)+REF_COST (lambda_motion_factor, 0, LIST_1 + list_offset)) : (int)(2*lambda_motion*min(best_fw_ref,1)));
  516. bid_mcost += BIDPartitionCost (mode, block, best_fw_ref, 0, lambda_motion_factor );
  517. //--- get prediction direction ----
  518. if (fw_mcost<=bw_mcost && fw_mcost<=bid_mcost)
  519. {
  520. best_pdir = 0;
  521. cost = fw_mcost;
  522. best_bw_ref = -1;
  523. }
  524. else if (bw_mcost<=fw_mcost && bw_mcost<=bid_mcost)
  525. {
  526. best_pdir = 1;
  527. cost = bw_mcost;
  528. best_fw_ref = -1;
  529. }
  530. else
  531. {
  532. best_pdir = 2;
  533. cost = bid_mcost;
  534. best_bw_ref = 0;
  535. }
  536. //store backward reference index for every block
  537. for (j=0; j<2; j++)
  538. for (i=0; i<2; i++)
  539. {
  540. enc_picture->ref_idx[LIST_0][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = best_fw_ref;
  541. enc_picture->ref_idx[LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = best_bw_ref;
  542. //enc_picture->ref_pic_id [LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j] = enc_picture->ref_pic_num[LIST_1 + list_offset][enc_picture->ref_idx[LIST_1][img->block_x+(block&1)*2+i][img->block_y+(block&2)+j]];
  543. }
  544. } // if (bframe)
  545. else
  546. {
  547. best_pdir = 0;
  548. cost = fw_mcost;
  549. }
  550. } // if (mode!=0)
  551. //--- store coding state before coding with current mode ---
  552. store_coding_state (cs_cm);
  553. //+++计算RDO模式代价rdcost或非RDO模式代价cost
  554. if (input->rdopt)
  555. {
  556. //+++RDCost_for_8x8blocks是在4种亚宏块模式中选择最佳的模式(代价)
  557. //+++准确来说RDCost_for_8x8blocks主要是得到编号为block的8x8块,在mode模式下的代价
  558. //--- get and check rate-distortion cost ---
  559. rdcost = RDCost_for_8x8blocks (&cnt_nonz, &curr_cbp_blk, lambda_mode,
  560. block, mode, best_pdir, best_fw_ref, best_bw_ref);
  561. //+++block:8x8块在16x16大小的宏块中的位置
  562. //+++mode:亚宏块模式
  563. }
  564. else
  565. {
  566. cost += (REF_COST (lambda_motion_factor, B8Mode2Value (mode, best_pdir), list_offset + (best_pdir<1?0:1)) - 1);
  567. }
  568. //+++根据cost(非RDO模式)或rdcost(RDO模式)选择最佳模式
  569. //--- set variables if best mode has changed ---
  570. if (( input->rdopt && rdcost < min_rdcost) ||
  571. (!input->rdopt && cost < min_cost8x8 ) )
  572. {
  573. min_cost8x8 = cost;//+++非RDO代价
  574. min_rdcost = rdcost;//+++RDO代价
  575. best8x8mode [block] = mode;//最佳的亚宏块模式
  576. best8x8pdir [P8x8][block] = best_pdir;
  577. best8x8fwref [P8x8][block] = best_fw_ref;
  578. best8x8bwref [P8x8][block] = best_bw_ref;
  579. //--- store number of nonzero coefficients ---
  580. best_cnt_nonz = cnt_nonz;
  581. //+++存储cbp、残差AC系数和重建帧中的预测值
  582. if (input->rdopt)
  583. {
  584. //--- store block cbp ---
  585. cbp_blk8x8 &= (~(0x33 << (((block>>1)<<3)+((block%2)<<1)))); // delete bits for block
  586. cbp_blk8x8 |= curr_cbp_blk;
  587. //--- store coefficients ---
  588. for (k=0; k< 4; k++)
  589. for (j=0; j< 2; j++)
  590. for (i=0; i<65; i++) cofAC8x8[block][k][j][i] = img->cofAC[block][k][j][i]; // 18->65 for ABT
  591. //--- store reconstruction and prediction ---
  592. for (j=j0; j<j0+8; j++)
  593. for (i=i0; i<i0+8; i++)
  594. {
  595. rec_mbY8x8[j][i] = enc_picture->imgY[img->pix_y+j][img->pix_x+i];
  596. mpr8x8 [j][i] = img->mpr[i][j];
  597. }
  598. }
  599. //--- store coding state ---
  600. store_coding_state (cs_b8);
  601. } // if (rdcost <= min_rdcost)
  602. //--- re-set coding state as it was before coding with current mode was performed ---
  603. reset_coding_state (cs_cm);
  604. } // if (valid[mode=b8_mode_table[index]])
  605. } // for (min_rdcost=1e30, index=(bframe?0:1); index<6; index++)//+++5种8x8模式循环结束
  606. //+++cost8x8:非RDO模式下的代价,其实cost8x8是一个宏块中的4个8x8块的代价和,
  607. //+++换句话说,其实cost8x8就是一个宏块(16x16)在亚宏块模式下的总的代价和.
  608. //+++这个量主要用来和上面计算的最佳宏块级模式(非RDO情况下)对应的最小代价min_cost来进行比较,
  609. //+++看宏块级模式或亚宏块级模式哪个更好,从下面的代码中我们也可以看出来.
  610. cost8x8 += min_cost8x8;
  611. // if (!input->rdopt) cost8x8+= min_cost8x8;
  612. // else cost8x8 += min_rdcost;
  613. if (!input->rdopt)
  614. {
  615. mode = best8x8mode[block];
  616. pdir = best8x8pdir[P8x8][block];
  617. //+++编码残差系数
  618. curr_cbp_blk = 0;
  619. //+++cnt_nonz表示非零系数的编码代价;
  620. best_cnt_nonz = LumaResidualCoding8x8 (&dummy, &curr_cbp_blk, block, pdir,
  621. (pdir==0||pdir==2?mode:0),
  622. (pdir==1||pdir==2?mode:0),
  623. (best8x8fwref[P8x8][block]),
  624. (best8x8bwref[P8x8][block]));
  625. cbp_blk8x8 &= (~(0x33 << (((block>>1)<<3)+((block%2)<<1)))); // delete bits for block
  626. cbp_blk8x8 |= curr_cbp_blk;
  627. //--- store coefficients ---
  628. for (k=0; k< 4; k++)
  629. for (j=0; j< 2; j++)
  630. for (i=0; i<65; i++) cofAC8x8[block][k][j][i] = img->cofAC[block][k][j][i]; // 18->65 for ABT
  631. //--- store reconstruction and prediction ---
  632. for (j=j0; j<j0+8; j++)
  633. for (i=i0; i<i0+8; i++)
  634. {
  635. rec_mbY8x8[j][i] = enc_picture->imgY[img->pix_y+j][img->pix_x+i];
  636. mpr8x8 [j][i] = img->mpr[i][j];
  637. }
  638. }
  639. //----- set cbp and count of nonzero coefficients ---
  640. if (best_cnt_nonz)
  641. {
  642. cbp8x8 |= (1<<block);
  643. cnt_nonz_8x8 += best_cnt_nonz;
  644. }
  645. mode=best8x8mode[block];//第2396行会用到mode,best8x8mode[block]应该是第block号8x8块所选取的最佳亚宏块级模式
  646. //===== reset intra prediction modes (needed for prediction, must be stored after 8x8 mode dec.) =====
  647. j0 = img->block_y+2*(block/2);
  648. i0 = img->block_x+2*(block%2);
  649. for (j=j0; j<j0+2; j++)
  650. for (i=i0; i<i0+2; i++)
  651. ipredmodes[i][j] = DC_PRED;
  652. i0 = 4*block;
  653. for (i=i0; i<i0+4; i++) currMB->intra_pred_modes[i] = DC_PRED;
  654. if (block<3)
  655. {
  656. //===== re-set reconstructed block =====
  657. j0 = 8*(block/2);
  658. i0 = 8*(block%2);
  659. for (j=j0; j<j0+8; j++)
  660. for (i=i0; i<i0+8; i++)
  661. enc_picture->imgY[img->pix_y+j][img->pix_x+i] = rec_mbY8x8[j][i];
  662. } // if (block<3)
  663. //+++这儿与宏块级类似,要设置MV和参考帧
  664. //===== set motion vectors and reference frames (prediction) =====
  665. SetRefAndMotionVectors (block, mode, best8x8pdir[P8x8][block], best8x8fwref[P8x8][block], best8x8bwref[P8x8][block]);
  666. //===== set the coding state after current block =====
  667. reset_coding_state (cs_b8);
  668. } // for (cbp8x8=cbp_blk8x8=cnt_nonz_8x8=0, block=0; block<4; block++)
  669. //+++此刻完成了对一个宏块(16x16)在亚宏块模式下的计算,或者说对宏块中每个8x8块完成亚宏块模式的选择
  670. //+++并且在亚宏块模式下的代价(4个8x8块代价之和)也计算完毕,在cost8x8中,下面会用到,和宏块级模式(min_cost)进行比较
  671. //===== store intra prediction modes for 8x8+ macroblock mode =====
  672. for (k=0, j=img->block_y; j<img->block_y+4; j++)
  673. {
  674. for ( i=img->block_x; i<img->block_x+4; i++, k++)
  675. {
  676. b8_ipredmode [k] = ipredmodes [i][j];
  677. b8_intra_pred_modes[k] = currMB->intra_pred_modes[k];
  678. }
  679. }
  680. //--- re-set coding state (as it was before 8x8 block coding) ---
  681. reset_coding_state (cs_mb);
  682. for (i=0; i<16; i++)
  683. for(j=0; j<16; j++)
  684. //++ 原代码中这里 img->mpr 的下标写反了
  685. // diffy[j][i] = imgY_org[img->opix_y+j][img->opix_x+i]-img->mpr[j][i];
  686. diffy[j][i] = imgY_org[img->opix_y+j][img->opix_x+i]-img->mpr[i][j];
  687. //+++到这儿应该是一个结点了啊
  688. //+++(在非RDO模式下)对一个宏块(当前宏块),此时宏块级(16x16,16x8,8x16)的最小开销已经计算出来,即min_cost
  689. //+++子宏块级(8x8,8x4,4x8和4x4)的开销也计算出来(4个8x8块代价之和),即cost8x8
  690. //+++这样可以比较这两种方式,选择开销更小的那种宏块分割方式
  691. if(cost8x8 < min_cost)//+++// 帧间8x8模式与帧间16x16代价相比
  692. {
  693. best_mode = P8x8;
  694. min_cost = cost8x8;
  695. }
  696. //+++到这儿我们也可以看到,在非RDO模式下,best_mode保存的是一个宏块(当前宏块)帧间预测的最优模式,是(16x16,16x8,8x16和P8x8)之一.
  697. //+++(16x16,16x8,8x16)这三种宏块级的模式是选择最优模式后再与P8x8模式比较.min_cost现在保存的是一个宏块(当前宏块)帧间预测的最小代价
  698. //+++从下面帧内预测的代码我们也可以看到,min_cost作为帧间最小代价是要和帧内两种模式I4MB和I16MB进行比较的.
  699. }
  700. else // if (valid[P8x8])
  701. {
  702. //+++这个地方是不用子宏块划分的方法时,将其开销设为很大
  703. cost8x8 = (1<<20);
  704. }
  705. // Find a motion vector for the Skip mode
  706. if((img->type == P_SLICE)||(img->type == SP_SLICE))
  707. FindSkipModeMotionVector ();//+++用于计算MV预测值
  708. }
  709. else // if (img->type!=I_SLICE)//+++不进行帧间预测
  710. {
  711. min_cost = (1<<20);
  712. }
  713. //+++帧间预测的相关开销计算完毕
  714. //++++++++++++++++++++++++++++
  715. //===================================
  716. //+++下面是对帧内预测进行求其开销
  717. if (input->rdopt)
  718. {//+++使用RDO准则的情况
  719. int mb_available_up;
  720. int mb_available_left;
  721. int mb_available_up_left;
  722. min_rdcost = max_rdcost;
  723. // precompute all new chroma intra prediction modes
  724. //++ 对色度进行帧内预测,并求出最优模式,
  725. //+++RDO模式下,需要色度预测,保存在currMB->c_ipred_mode中
  726. IntraChromaPrediction8x8(&mb_available_up, &mb_available_left, &mb_available_up_left);
  727. //+++阅读完IntraChromaPrediction8x8函数的代码,并且结合下面的代码,我们可以看到:
  728. //+++这个函数不做任何运动估计和补偿,而仅仅是求出要进行帧内编码的色度宏块需要的参考块
  729. //+++本来在currMB->c_ipred_mode保存了最佳的模式,但是下面却被覆盖掉了
  730. //++ 分别在四种色度模式下进行 RDO 计算,如果是 inter 模式,因为色度预测模式与 SSD 计算
  731. //++ 无关,因此只需要计算一次(利用 currMB->c_ipred_mode == DC_PRED_8 条件限制来实现)
  732. for (currMB->c_ipred_mode=DC_PRED_8; currMB->c_ipred_mode<=PLANE_8; currMB->c_ipred_mode++)
  733. {
  734. // bypass if c_ipred_mode is not allowed
  735. //+++若当前 c_ipred_mode 模式不被允许,则考虑下一个 c_ipred_mode 模式;
  736. if ((currMB->c_ipred_mode==VERT_PRED_8 && !mb_available_up) ||
  737. (currMB->c_ipred_mode==HOR_PRED_8 && !mb_available_left) ||
  738. (currMB->c_ipred_mode==PLANE_8 && (!mb_available_left || !mb_available_up || !mb_available_up_left)))
  739. continue;
  740. //===== GET BEST MACROBLOCK MODE =====
  741. for (ctr16x16=0, index=0; index<7; index++)
  742. {
  743. //+++mb_mode_table[7] = {0, 1, 2, 3, P8x8, I16MB, I4MB};
  744. mode = mb_mode_table[index];
  745. //--- for INTER16x16 check all prediction directions ---
  746. if (mode==1 && img->type==B_SLICE)
  747. {
  748. best8x8pdir[1][0] = best8x8pdir[1][1] = best8x8pdir[1][2] = best8x8pdir[1][3] = ctr16x16;
  749. if (ctr16x16 < 2) index--;
  750. ctr16x16++;
  751. }
  752. img->NoResidueDirect = 0;
  753. if (valid[mode])
  754. {
  755. // bypass if c_ipred_mode not used
  756. //++ 设置当前宏块类型以及其中每个8*8块的分割方式和预测方向,每个4*4块的参考帧索引
  757. //++ 该函数在下面的 RDCost_for_macroblocks 函数内再次调用,进行了重复操作
  758. SetModesAndRefframeForBlocks (mode);
  759. if (currMB->c_ipred_mode == DC_PRED_8 || //++ 利用这个条件限制来实现 inter 模式时只计算一次 RDO
  760. (IS_INTRA(currMB) ))
  761. {
  762. //+++此刻我的理解是:RDCost_for_macroblocks是在模式0-3,P8x8,Intra4x4和Intra16x16之间选择一个最佳模式
  763. //+++这是利用RDO的方法.上面再帧间预测时其实已经将在1-3中选择出了最佳模式,但那是在非RDO模式下的
  764. //+++关于这个地方的RDCost_for_macroblocks我要说明一下:该函数是求出对应mode模式下的RDO代价,然后将这个代价与min_rdcost进行比较
  765. //+++如果此时RDO的代价比较小,返回1,执行if,保存相关参数,并且修改min_rdcost,否则返回0,这样下面的if语句不执行,接着再比较下一个模式.
  766. //+++所以我们可以看到(0-3,P8x8,Intra4x4和Intra16x16)这些模式的比较是随着外面的这个for循环,依次在RDCost_fo...函数中完成的,
  767. if (RDCost_for_macroblocks (lambda_mode, mode, &min_rdcost)) //++ 帧内模式时亮度存在重复计算情况:因为色度预测模式与亮度预测模式无关,所以在该色度模
  768. { //++ 式循环中每次计算得到的 intra16*16 和 intra4*4 宏块类型的最佳亮度模式都是完全相同的
  769. //Rate control
  770. if(mode == P8x8)
  771. {
  772. for (i=0; i<16; i++)
  773. for(j=0; j<16; j++)
  774. diffy[j][i] = imgY_org[img->opix_y+j][img->opix_x+i] - mpr8x8[j][i];
  775. }else
  776. {
  777. for (i=0; i<16; i++)
  778. for(j=0; j<16; j++)
  779. diffy[j][i] = imgY_org[img->opix_y+j][img->opix_x+i] - pred[j][i];
  780. }
  781. //+++存储该模式下的一些参数:因为RDCost_for_macroblocks的计算结果是mode模式的预测开销小于min_rdcost,
  782. //+++所以mode模式其实更好,所以现在要保存该模式的一些参数.
  783. store_macroblock_parameters (mode);
  784. }
  785. }
  786. }
  787. if (valid[0] && bframe && mode == 0 && currMB->cbp && (currMB->cbp&15) != 15) //g050
  788. {
  789. img->NoResidueDirect = 1;
  790. if (RDCost_for_macroblocks (lambda_mode, mode, &min_rdcost))
  791. {
  792. //Rate control
  793. for (i=0; i<16; i++)
  794. for(j=0; j<16; j++)
  795. diffy[j][i] = imgY_org[img->opix_y+j][img->opix_x+i] - pred[j][i];
  796. store_macroblock_parameters (mode);
  797. }
  798. }
  799. }
  800. }
  801. }
  802. else//+++不使用RDO准则的情况
  803. {
  804. //+++从下面的代码可以看到,在不同模式下计算出的cost都要和min_cost(帧间预测代价)进行比较
  805. //+++然后决定是否更新min_cost和best_mode,所以我们也可以看到,最终的代价也是保存在min_cost中,
  806. //+++best_mode对应保存最佳模式,从而用于最后的熵编码
  807. //+++下面对Direct,I4MB和I16MB三种模式进行依次比较,代码是类似的.
  808. if (valid[0] && bframe) // check DIRECT MODE
  809. {
  810. cost = (have_direct?cost_direct:Get_Direct_CostMB (lambda_mode));
  811. cost -= (int)floor(16*lambda_motion+0.4999);
  812. if (cost <= min_cost)
  813. {
  814. //Rate control
  815. for (i=0; i<16; i++)
  816. for(j=0; j<16; j++)
  817. diffy[j][i] = imgY_org[img->pix_y+j][img->pix_x+i]-img->mpr[i][j];
  818. //+++更新min_cost和best_mode
  819. min_cost = cost;
  820. best_mode = 0;
  821. }
  822. }
  823. if (valid[I4MB]) // check INTRA4x4
  824. {
  825. //+++从Intra4x4的9中模式中选择最佳的模式,函数Mode_Dec..中的cost就是当前宏块的代价
  826. //+++追踪各个函数,我们可以看到函数调用顺序如下:在非RDO模式下调用
  827. //+++Mode_Decision_for_Intra4x4Macroblock(得到一个宏块的代价)
  828. //+++->Mode_Decision_for_8x8IntraBlocks(得到一个8x8块代价)
  829. //+++->Mode_Decision_for_Intra4x4Macroblock(得到一个4x4块代价),
  830. //+++其实最后一个函数才是真正的Intra4x4模式选择的核心,在这个函数中包含9中模式的比较(RDO和非RDO)
  831. currMB->cbp = Mode_Decision_for_Intra4x4Macroblock (lambda_mode, &cost);//++ 计算当前宏块采用intra4*4编码时的代价和CBP
  832. if (cost <= min_cost)
  833. {
  834. //Rate control
  835. for (i=0; i<16; i++)
  836. for(j=0; j<16; j++)
  837. diffy[j][i] = imgY_org[img->pix_y+j][img->pix_x+i]-img->mpr[i][j];//++ 保存采用intra4*4编码时最佳模式的残差块
  838. //+++更新min_cost和best_mode
  839. min_cost = cost;
  840. best_mode = I4MB;
  841. }
  842. }
  843. if (valid[I16MB]) // check INTRA16x16
  844. {
  845. //+++比较与RDO模式下代码可以发现,其实在RDO模式下进行Intra16x16预测时,调用的函数Intra16x16_Mode_Decision的代码
  846. //+++与这儿类似:也是先intrapred_luma_16x16,再find_sad_16x16,最后dct_luma_16x16
  847. //+++同时这也说明:intra16*16 在进行4种模式选择时候,无论是否在RDO情况下,其选择过程是相同的。
  848. intrapred_luma_16x16 (); //++ 分别计算当前宏块在4种intra16*16帧内预测模式下的预测块
  849. //+++intrapred_luma_16x16执行完后,预测的结果保存在数组img->mprr_2中
  850. cost = find_sad_16x16 (&i16mode); //++ 计算intra16*16类型的代价(以SATD作为判断标准,因为该段代码是不采用RDO的处理过程)
  851. //+++此时i16mode保存的是Intra16x16的最佳模式,cost是对应的代价
  852. if (cost < min_cost) //++ 如果采用intra16*16类型编码的代价小于采用intra4*4类型编码的代价,则对当前宏块采用intra16*16类型编码
  853. {
  854. //Rate control
  855. for (i=0; i<16; i++)
  856. for(j=0; j<16; j++)
  857. diffy[j][i] = imgY_org[img->pix_y+j][img->pix_x+i]-img->mprr_2[i16mode][j][i];
  858. //+++更新min_cost和best_mode
  859. best_mode = I16MB;
  860. //+++这里说明一下:find_sad_16x16函数中求sad时已经做过DCT变换,但在dct_luma_16x16函数中又要重新做一遍呢?
  861. //+++答:前者是对每种预测模式都要做,后者是对最佳模式才做。每种模式做了之后并没有把结果都保存起来,所以要对最佳模式重新再做一次。
  862. currMB->cbp = dct_luma_16x16 (i16mode); //++ 对当前宏块采用intra16*16类型进行编码,并计算CBP
  863. }
  864. }
  865. }
  866. //+++通过比较RDO模式与非RDO模式我们可以发现,
  867. //+++在intra4*4与intra16*16之间做选择时,RDO情况下色度要参与计算,而非RDO情况下色度不参与计算
  868. //+++并且我们可以看到在非RDO模式下,具体的帧内预测还是帧间预测的选择在这儿完成(可以在这儿看到具体的代码)
  869. //+++但是对于RDO模式,具体的帧内还是帧间的选择却是在函数RDCost_for_macroblocks中完成的.
  870. //+++帧内预测结束
  871. //===================================
  872. if (rerun==0)
  873. {
  874. intra1 = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
  875. }
  876. } // for (rerun=0; rerun<runs; rerun++)
  877. //+++到现在,帧内和帧间模式的选择终于结束
  878. //+++下面的要进行存储编码参数,分RDO和非RDO
  879. //+++印证了[h264率失真RDO研究]一文中的一句:
  880. //+++"如果用RDO代价函数,因为计算代价时已经编码,择优后仅需对编码进行存储;
  881. //+++如果用非RDO代价函数,则择优后还要进行编码过程,编码后存储参数."
  882. //+++从下面的代码我们也可清楚的看到
  883. if (input->rdopt)
  884. {
  885. if ((cbp!=0 || best_mode==I16MB ))
  886. currMB->prev_cbp = 1;
  887. else if (cbp==0 && !input->RCEnable)
  888. {
  889. currMB->delta_qp = 0;
  890. currMB->qp = currMB->prev_qp;
  891. img->qp = currMB->qp;
  892. currMB->prev_cbp = 0;
  893. }
  894. //+++存储编码参数
  895. set_stored_macroblock_parameters ();
  896. //+++ (1)保存重建图像
  897. //+++ rdopt->rec_mbY[j][i] = rec_mbY[j][i];
  898. //+++ rdopt->rec_mbU[j][i] = rec_mbU[j][i];
  899. //+++ rdopt->rec_mbV[j][i] = rec_mbV[j][i];
  900. //+++ (2)变换系数和cbp
  901. //+++ img->cofAC=i4p(cofAC);
  902. //+++ img->cofDC=i3p(cofDC);
  903. //+++ currMB->cbp = cbp;
  904. //+++ currMB->cbp_blk = cbp_blk;
  905. //+++ (3)宏块模式
  906. //+++ currMB->mb_type = mode(best_mode);
  907. //+++ (4)参考帧保存
  908. //+++ rdopt.c 1511行,对前向参考,后向参考帧分别进行了保存
  909. //+++ (5)帧内预测模式
  910. //+++ currMB->c_ipred_mode = best_c_imode;
  911. //+++ (6)为当前块保存运动向量
  912. //+++ SetMotionVectorsMB (currMB, bframe);
  913. //+++ 从上面其实可以看到"="右边的量在rdopt.c中都是全局变量,在完成encode_one_macroblock之前,对这些参数进行一下保存.
  914. }
  915. else
  916. {
  917. //===== set parameters for chosen mode =====
  918. SetModesAndRefframeForBlocks (best_mode); //++ 设置当前宏块的参数,包括:宏块类型(mb_type)、4个8*8块的分割模式和预测方向(b8mode,b8pdir)、16个4*4块的参考帧索引(ref_idx,ref_pic_id)
  919. if (best_mode==P8x8)
  920. {
  921. SetCoeffAndReconstruction8x8 (currMB);
  922. }
  923. else
  924. {
  925. if (best_mode!=I4MB)
  926. {
  927. for (k=0, j=img->block_y; j<img->block_y+4; j++)
  928. for ( i=img->block_x; i<img->block_x+4; i++, k++)
  929. {
  930. ipredmodes [i][j] = DC_PRED;
  931. currMB->intra_pred_modes[k] = DC_PRED;
  932. }
  933. if (best_mode!=I16MB)
  934. {
  935. LumaResidualCoding ();
  936. //Rate control
  937. for (i=0; i<16; i++)
  938. for(j=0; j<16; j++)
  939. diffy[j][i] = imgY_org[img->pix_y+j][img->pix_x+i]-img->mpr[i][j];
  940. }
  941. }
  942. }
  943. //+++
  944. // precompute all chroma intra prediction modes
  945. IntraChromaPrediction8x8(NULL, NULL, NULL); //++ 对色度块进行处理,包括:分别计算4种色度帧内预测模式下的预测块、代价(该分支未采用RDO,因此以SATD作为判断标准)、最佳预测模式
  946. img->i16offset = 0;
  947. dummy = 0;
  948. ChromaResidualCoding (&dummy);
  949. if (best_mode==I16MB)
  950. {
  951. img->i16offset = I16Offset (currMB->cbp, i16mode);
  952. }
  953. SetMotionVectorsMB (currMB, bframe);
  954. //===== check for SKIP mode =====
  955. if ((img->type==P_SLICE || img->type==SP_SLICE) && best_mode==1 && currMB->cbp==0 &&
  956. enc_picture->ref_idx[LIST_0][img->block_x][img->block_y]==0 &&
  957. enc_picture->mv[LIST_0][img->block_x][img->block_y][0]==allmvs[0][0][0][0][0][0] &&
  958. enc_picture->mv[LIST_0][img->block_x][img->block_y][1]==allmvs[0][0][0][0][0][1] )
  959. {
  960. currMB->mb_type=currMB->b8mode[0]=currMB->b8mode[1]=currMB->b8mode[2]=currMB->b8mode[3]=0;
  961. }
  962. if(img->MbaffFrameFlag)
  963. set_mbaff_parameters();
  964. }
  965. // Rate control
  966. if(input->RCEnable)
  967. {
  968. if(img->type==P_SLICE)
  969. {
  970. img->MADofMB[img->current_mb_nr] = calc_MAD();
  971. if(input->basicunit<img->Frame_Total_Number_MB)
  972. {
  973. img->TotalMADBasicUnit +=img->MADofMB[img->current_mb_nr];
  974. /* delta_qp is present only for non-skipped macroblocks*/
  975. if ((cbp!=0 || best_mode==I16MB))
  976. currMB->prev_cbp = 1;
  977. else
  978. {
  979. img->qp -= currMB->delta_qp;
  980. currMB->delta_qp = 0;
  981. currMB->qp = img->qp;
  982. currMB->prev_cbp = 0;
  983. }
  984. /* When MBAFF is used, delta_qp is only present for the first non-skipped macroblock of each
  985. macroblock pair*/
  986. if (input->MbInterlace)
  987. {
  988. if(!currMB->mb_field)
  989. {
  990. DELTA_QP = currMB->delta_qp;
  991. QP = currMB->qp;
  992. }
  993. else
  994. {
  995. DELTA_QP2 = currMB->delta_qp;
  996. QP2 = currMB->qp;
  997. }
  998. }
  999. }
  1000. }
  1001. }
  1002. //+++保存最佳代价在rdopt->min_rdcost中
  1003. if(input->rdopt)
  1004. rdopt->min_rdcost = min_rdcost;
  1005. else
  1006. rdopt->min_rdcost = min_cost;
  1007. if(img->MbaffFrameFlag)
  1008. {
  1009. if (img->current_mb_nr%2) //bottom
  1010. {
  1011. if ((currMB->mb_type ? 0:((img->type == B_SLICE) ? !currMB->cbp:1)) // bottom is skip
  1012. &&(prevMB->mb_type ? 0:((img->type == B_SLICE) ? !prevMB->cbp:1))) // top is skip
  1013. {
  1014. if (!(field_flag_inference() == curr_mb_field))
  1015. {
  1016. rdopt->min_rdcost = 1e30; // don't allow coding of an MB pair as skip if wrong inference
  1017. }
  1018. }
  1019. }
  1020. }
  1021. //+++RestrictRefFrames应该是和容错有关系的。我的理解是,如果某个区域在后面帧里被intra方式update(从而可以避免错误传播),
  1022. //+++这个区域很可能是出现错误的区域,因此,不使用这个区域作参考以避免错误传播。请大家指正
  1023. //===== Decide if this MB will restrict the reference frames =====
  1024. if (input->RestrictRef==1)
  1025. {
  1026. if (input->rdopt==1)
  1027. {
  1028. refresh_map[2*img->mb_y ][2*img->mb_x ] = (intra ? 1 : 0);
  1029. refresh_map[2*img->mb_y ][2*img->mb_x+1] = (intra ? 1 : 0);
  1030. refresh_map[2*img->mb_y+1][2*img->mb_x ] = (intra ? 1 : 0);
  1031. refresh_map[2*img->mb_y+1][2*img->mb_x+1] = (intra ? 1 : 0);
  1032. }
  1033. else if (input->rdopt==2)
  1034. {
  1035. refresh_map[2*img->mb_y ][2*img->mb_x ] = (intra1==0 && (currMB->mb_type==I16MB || currMB->mb_type==I4MB) ? 1 : 0);
  1036. refresh_map[2*img->mb_y ][2*img->mb_x+1] = (intra1==0 && (currMB->mb_type==I16MB || currMB->mb_type==I4MB) ? 1 : 0);
  1037. refresh_map[2*img->mb_y+1][2*img->mb_x ] = (intra1==0 && (currMB->mb_type==I16MB || currMB->mb_type==I4MB) ? 1 : 0);
  1038. refresh_map[2*img->mb_y+1][2*img->mb_x+1] = (intra1==0 && (currMB->mb_type==I16MB || currMB->mb_type==I4MB) ? 1 : 0);
  1039. }
  1040. }
  1041. else if (input->RestrictRef==2)
  1042. {
  1043. refresh_map[2*img->mb_y ][2*img->mb_x ] = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
  1044. refresh_map[2*img->mb_y ][2*img->mb_x+1] = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
  1045. refresh_map[2*img->mb_y+1][2*img->mb_x ] = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
  1046. refresh_map[2*img->mb_y+1][2*img->mb_x+1] = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
  1047. }
  1048. if(input->FMEnable)
  1049. skip_intrabk_SAD(best_mode, listXsize[LIST_0+list_offset]);
  1050. }//+++void encode_one_macroblock(),该函数终于完成了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值