分享

FFmpeg的HEVC解码器源代码简单分析:CTU解码(CTU Decode)部分

 托尼虎 2021-04-14

=====================================================

HEVC源代码分析文章列表:

【解码 -libavcodec HEVC 解码器】

FFmpeg的HEVC解码器源代码简单分析:概述

FFmpeg的HEVC解码器源代码简单分析:解析器(Parser)部分

FFmpeg的HEVC解码器源代码简单分析:解码器主干部分

FFmpeg的HEVC解码器源代码简单分析:CTU解码(CTU Decode)部分-PU

FFmpeg的HEVC解码器源代码简单分析:CTU解码(CTU Decode)部分-TU

FFmpeg的HEVC解码器源代码简单分析:环路滤波(LoopFilter)

=====================================================


本文分析FFmpeg的libavcodec中的HEVC解码器的CTU解码(CTU Decode)部分的源代码。FFmpeg的HEVC解码器调用hls_decode_entry()函数完成了Slice解码工作。hls_decode_entry()则调用了hls_coding_quadtree()完成了CTU解码工作。由于CTU解码部分的内容比较多,因此将这一部分内容拆分成两篇文章:一篇文章记录PU的解码,另一篇文章记录TU解码。本文记录PU的解码过程。


函数调用关系图

FFmpeg HEVC解码器的CTU解码(CTU Decoder)部分在整个HEVC解码器中的位置如下图所示。
 

CTU解码(CTU Decoder)部分的函数调用关系如下图所示。
 

从图中可以看出,CTU解码模块对应的函数是hls_coding_quadtree()。该函数是一个递归调用的函数,可以按照四叉树的句法格式解析CTU并获得其中的CU。对于每个CU会调用hls_coding_unit()进行解码。
hls_coding_unit()会调用hls_prediction_unit()对CU中的PU进行处理。hls_prediction_unit()调用luma_mc_uni()对亮度单向预测块进行运动补偿处理,调用chroma_mc_uni()对色度单向预测块进行运动补偿处理,调用luma_mc_bi()对亮度单向预测块进行运动补偿处理。
hls_coding_unit()会调用hls_transform_tree()对CU中的TU进行处理。hls_transform_tree()是一个递归调用的函数,可以按照四叉树的句法格式解析并获得其中的TU。对于每一个TU会调用hls_transform_unit()进行解码。hls_transform_unit()会进行帧内预测,并且调用ff_hevc_hls_residual_coding()解码DCT残差数据。

hls_decode_entry()

hls_decode_entry()是FFmpeg HEVC解码器中Slice解码的入口函数。该函数的定义如下所示。
  1. //解码入口函数
  2. static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
  3. {
  4. HEVCContext *s = avctxt->priv_data;
  5. //CTB尺寸
  6. int ctb_size = 1 << s->sps->log2_ctb_size;
  7. int more_data = 1;
  8. int x_ctb = 0;
  9. int y_ctb = 0;
  10. int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
  11. if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
  12. av_log(s->avctx, AV_LOG_ERROR, 'Impossible initial tile.\n');
  13. return AVERROR_INVALIDDATA;
  14. }
  15. if (s->sh.dependent_slice_segment_flag) {
  16. int prev_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
  17. if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
  18. av_log(s->avctx, AV_LOG_ERROR, 'Previous slice segment missing\n');
  19. return AVERROR_INVALIDDATA;
  20. }
  21. }
  22. while (more_data && ctb_addr_ts < s->sps->ctb_size) {
  23. int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  24. //CTB的位置x和y
  25. x_ctb = (ctb_addr_rs % ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  26. y_ctb = (ctb_addr_rs / ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  27. //初始化周围的参数
  28. hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
  29. //初始化CABAC
  30. ff_hevc_cabac_init(s, ctb_addr_ts);
  31. //样点自适应补偿参数
  32. hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
  33. s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
  34. s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
  35. s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
  36. /*
  37. * CU示意图
  38. *
  39. * 64x64块
  40. *
  41. * 深度d=0
  42. * split_flag=1时候划分为4个32x32
  43. *
  44. * +--------+--------+--------+--------+--------+--------+--------+--------+
  45. * | |
  46. * | | |
  47. * | |
  48. * + | +
  49. * | |
  50. * | | |
  51. * | |
  52. * + | +
  53. * | |
  54. * | | |
  55. * | |
  56. * + | +
  57. * | |
  58. * | | |
  59. * | |
  60. * + -- -- -- -- -- -- -- -- --+ -- -- -- -- -- -- -- -- --+
  61. * | | |
  62. * | |
  63. * | | |
  64. * + +
  65. * | | |
  66. * | |
  67. * | | |
  68. * + +
  69. * | | |
  70. * | |
  71. * | | |
  72. * + +
  73. * | | |
  74. * | |
  75. * | | |
  76. * +--------+--------+--------+--------+--------+--------+--------+--------+
  77. *
  78. *
  79. * 32x32 块
  80. * 深度d=1
  81. * split_flag=1时候划分为4个16x16
  82. *
  83. * +--------+--------+--------+--------+
  84. * | |
  85. * | | |
  86. * | |
  87. * + | +
  88. * | |
  89. * | | |
  90. * | |
  91. * + -- -- -- -- + -- -- -- -- +
  92. * | |
  93. * | | |
  94. * | |
  95. * + | +
  96. * | |
  97. * | | |
  98. * | |
  99. * +--------+--------+--------+--------+
  100. *
  101. *
  102. * 16x16 块
  103. * 深度d=2
  104. * split_flag=1时候划分为4个8x8
  105. *
  106. * +--------+--------+
  107. * | |
  108. * | | |
  109. * | |
  110. * + -- --+ -- -- +
  111. * | |
  112. * | | |
  113. * | |
  114. * +--------+--------+
  115. *
  116. *
  117. * 8x8块
  118. * 深度d=3
  119. * split_flag=1时候划分为4个4x4
  120. *
  121. * +----+----+
  122. * | | |
  123. * + -- + -- +
  124. * | | |
  125. * +----+----+
  126. *
  127. */
  128. /*
  129. * 解析四叉树结构,并且解码
  130. *
  131. * hls_coding_quadtree(HEVCContext *s, int x0, int y0, int log2_cb_size, int cb_depth)中:
  132. * s:HEVCContext上下文结构体
  133. * x_ctb:CB位置的x坐标
  134. * y_ctb:CB位置的y坐标
  135. * log2_cb_size:CB大小取log2之后的值
  136. * cb_depth:深度
  137. *
  138. */
  139. more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
  140. if (more_data < 0) {
  141. s->tab_slice_address[ctb_addr_rs] = -1;
  142. return more_data;
  143. }
  144. ctb_addr_ts++;
  145. //保存解码信息以供下次使用
  146. ff_hevc_save_states(s, ctb_addr_ts);
  147. //去块效应滤波
  148. ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
  149. }
  150. if (x_ctb + ctb_size >= s->sps->width &&
  151. y_ctb + ctb_size >= s->sps->height)
  152. ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
  153. return ctb_addr_ts;
  154. }

从源代码可以看出,hls_decode_entry()主要调用了2个函数进行解码工作:
(1)调用hls_coding_quadtree()解码CTU。其中包含了PU和TU的解码。
(2)调用ff_hevc_hls_filters()进行滤波。其中包含了去块效应滤波和SAO滤波。
本文分析第一步的PU解码过程。

hls_coding_quadtree()

hls_coding_quadtree()用于解析CTU的四叉树句法结构。该函数的定义如下所示。
  1. /*
  2. * 解析四叉树结构,并且解码
  3. * 注意该函数是递归调用
  4. * 注释和处理:雷霄骅
  5. *
  6. *
  7. * s:HEVCContext上下文结构体
  8. * x_ctb:CB位置的x坐标
  9. * y_ctb:CB位置的y坐标
  10. * log2_cb_size:CB大小取log2之后的值
  11. * cb_depth:深度
  12. *
  13. */
  14. static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
  15. int log2_cb_size, int cb_depth)
  16. {
  17. HEVCLocalContext *lc = s->HEVClc;
  18. //CB的大小,split flag=0
  19. //log2_cb_size为CB大小取log之后的结果
  20. const int cb_size = 1 << log2_cb_size;
  21. int ret;
  22. int qp_block_mask = (1<<(s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth)) - 1;
  23. int split_cu;
  24. //确定CU是否还会划分?
  25. lc->ct_depth = cb_depth;
  26. if (x0 + cb_size <= s->sps->width &&
  27. y0 + cb_size <= s->sps->height &&
  28. log2_cb_size > s->sps->log2_min_cb_size) {
  29. split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
  30. } else {
  31. split_cu = (log2_cb_size > s->sps->log2_min_cb_size);
  32. }
  33. if (s->pps->cu_qp_delta_enabled_flag &&
  34. log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth) {
  35. lc->tu.is_cu_qp_delta_coded = 0;
  36. lc->tu.cu_qp_delta = 0;
  37. }
  38. if (s->sh.cu_chroma_qp_offset_enabled_flag &&
  39. log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_chroma_qp_offset_depth) {
  40. lc->tu.is_cu_chroma_qp_offset_coded = 0;
  41. }
  42. if (split_cu) {
  43. //如果CU还可以继续划分,则继续解析划分后的CU
  44. //注意这里是递归调用
  45. //CB的大小,split flag=1
  46. const int cb_size_split = cb_size >> 1;
  47. /*
  48. * (x0, y0) (x1, y0)
  49. * +--------+--------+
  50. * | |
  51. * | | |
  52. * | |
  53. * + -- --+ -- -- +
  54. * (x0, y1) (x1, y1) |
  55. * | | |
  56. * | |
  57. * +--------+--------+
  58. *
  59. */
  60. const int x1 = x0 + cb_size_split;
  61. const int y1 = y0 + cb_size_split;
  62. int more_data = 0;
  63. //注意:
  64. //CU大小减半,log2_cb_size-1
  65. //深度d加1,cb_depth+1
  66. more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
  67. if (more_data < 0)
  68. return more_data;
  69. if (more_data && x1 < s->sps->width) {
  70. more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
  71. if (more_data < 0)
  72. return more_data;
  73. }
  74. if (more_data && y1 < s->sps->height) {
  75. more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
  76. if (more_data < 0)
  77. return more_data;
  78. }
  79. if (more_data && x1 < s->sps->width &&
  80. y1 < s->sps->height) {
  81. more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
  82. if (more_data < 0)
  83. return more_data;
  84. }
  85. if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
  86. ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
  87. lc->qPy_pred = lc->qp_y;
  88. if (more_data)
  89. return ((x1 + cb_size_split) < s->sps->width ||
  90. (y1 + cb_size_split) < s->sps->height);
  91. else
  92. return 0;
  93. } else {
  94. /*
  95. * (x0, y0)
  96. * +--------+--------+
  97. * | |
  98. * | |
  99. * | |
  100. * + +
  101. * | |
  102. * | |
  103. * | |
  104. * +--------+--------+
  105. *
  106. */
  107. //注意处理的是不可划分的CU单元
  108. //处理CU单元-真正的解码
  109. ret = hls_coding_unit(s, x0, y0, log2_cb_size);
  110. if (ret < 0)
  111. return ret;
  112. if ((!((x0 + cb_size) %
  113. (1 << (s->sps->log2_ctb_size))) ||
  114. (x0 + cb_size >= s->sps->width)) &&
  115. (!((y0 + cb_size) %
  116. (1 << (s->sps->log2_ctb_size))) ||
  117. (y0 + cb_size >= s->sps->height))) {
  118. int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
  119. return !end_of_slice_flag;
  120. } else {
  121. return 1;
  122. }
  123. }
  124. return 0;
  125. }

从源代码可以看出,hls_coding_quadtree()首先调用ff_hevc_split_coding_unit_flag_decode()判断当前CU是否还需要划分。如果需要划分的话,就会递归调用4次hls_coding_quadtree()分别对4个子块继续进行四叉树解析;如果不需要划分,就会调用hls_coding_unit()对CU进行解码。总而言之,hls_coding_quadtree()会解析出来一个CTU中的所有CU,并且对每一个CU逐一调用hls_coding_unit()进行解码。一个CTU中CU的解码顺序如下图所示。图中a, b, c …即代表了的先后顺序。



hls_coding_unit()

hls_coding_unit()用于解码一个CU。该函数的定义如下所示。
  1. //处理CU单元-真正的解码
  2. static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
  3. {
  4. //CB大小
  5. int cb_size = 1 << log2_cb_size;
  6. HEVCLocalContext *lc = s->HEVClc;
  7. int log2_min_cb_size = s->sps->log2_min_cb_size;
  8. int length = cb_size >> log2_min_cb_size;
  9. int min_cb_width = s->sps->min_cb_width;
  10. //以最小的CB为单位(例如4x4)的时候,当前CB的位置——x坐标和y坐标
  11. int x_cb = x0 >> log2_min_cb_size;
  12. int y_cb = y0 >> log2_min_cb_size;
  13. int idx = log2_cb_size - 2;
  14. int qp_block_mask = (1<<(s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth)) - 1;
  15. int x, y, ret;
  16. //设置CU的属性值
  17. lc->cu.x = x0;
  18. lc->cu.y = y0;
  19. lc->cu.pred_mode = MODE_INTRA;
  20. lc->cu.part_mode = PART_2Nx2N;
  21. lc->cu.intra_split_flag = 0;
  22. SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
  23. for (x = 0; x < 4; x++)
  24. lc->pu.intra_pred_mode[x] = 1;
  25. if (s->pps->transquant_bypass_enable_flag) {
  26. lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
  27. if (lc->cu.cu_transquant_bypass_flag)
  28. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  29. } else
  30. lc->cu.cu_transquant_bypass_flag = 0;
  31. if (s->sh.slice_type != I_SLICE) {
  32. //Skip类型
  33. uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
  34. //设置到skip_flag缓存中
  35. x = y_cb * min_cb_width + x_cb;
  36. for (y = 0; y < length; y++) {
  37. memset(&s->skip_flag[x], skip_flag, length);
  38. x += min_cb_width;
  39. }
  40. lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
  41. } else {
  42. x = y_cb * min_cb_width + x_cb;
  43. for (y = 0; y < length; y++) {
  44. memset(&s->skip_flag[x], 0, length);
  45. x += min_cb_width;
  46. }
  47. }
  48. if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
  49. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
  50. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  51. if (!s->sh.disable_deblocking_filter_flag)
  52. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  53. } else {
  54. int pcm_flag = 0;
  55. //读取预测模式(非 I Slice)
  56. if (s->sh.slice_type != I_SLICE)
  57. lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
  58. //不是帧内预测模式的时候
  59. //或者已经是最小CB的时候
  60. if (lc->cu.pred_mode != MODE_INTRA ||
  61. log2_cb_size == s->sps->log2_min_cb_size) {
  62. //读取CU分割模式
  63. lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
  64. lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
  65. lc->cu.pred_mode == MODE_INTRA;
  66. }
  67. if (lc->cu.pred_mode == MODE_INTRA) {
  68. //帧内预测模式
  69. //PCM方式编码,不常见
  70. if (lc->cu.part_mode == PART_2Nx2N && s->sps->pcm_enabled_flag &&
  71. log2_cb_size >= s->sps->pcm.log2_min_pcm_cb_size &&
  72. log2_cb_size <= s->sps->pcm.log2_max_pcm_cb_size) {
  73. pcm_flag = ff_hevc_pcm_flag_decode(s);
  74. }
  75. if (pcm_flag) {
  76. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  77. ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
  78. if (s->sps->pcm.loop_filter_disable_flag)
  79. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  80. if (ret < 0)
  81. return ret;
  82. } else {
  83. //获取帧内预测模式
  84. intra_prediction_unit(s, x0, y0, log2_cb_size);
  85. }
  86. } else {
  87. //帧间预测模式
  88. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  89. //帧间模式一共有8种划分模式
  90. switch (lc->cu.part_mode) {
  91. case PART_2Nx2N:
  92. /*
  93. * PART_2Nx2N:
  94. * +--------+--------+
  95. * | |
  96. * | |
  97. * | |
  98. * + + +
  99. * | |
  100. * | |
  101. * | |
  102. * +--------+--------+
  103. */
  104. //处理PU单元-运动补偿
  105. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
  106. break;
  107. case PART_2NxN:
  108. /*
  109. * PART_2NxN:
  110. * +--------+--------+
  111. * | |
  112. * | |
  113. * | |
  114. * +--------+--------+
  115. * | |
  116. * | |
  117. * | |
  118. * +--------+--------+
  119. *
  120. */
  121. /*
  122. * hls_prediction_unit()参数:
  123. * x0 : PU左上角x坐标
  124. * y0 : PU左上角y坐标
  125. * nPbW : PU宽度
  126. * nPbH : PU高度
  127. * log2_cb_size : CB大小取log2()的值
  128. * partIdx : PU的索引号-分成4个块的时候取0-3,分成两个块的时候取0和1
  129. */
  130. //上
  131. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
  132. //下
  133. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
  134. break;
  135. case PART_Nx2N:
  136. /*
  137. * PART_Nx2N:
  138. * +--------+--------+
  139. * | | |
  140. * | | |
  141. * | | |
  142. * + + +
  143. * | | |
  144. * | | |
  145. * | | |
  146. * +--------+--------+
  147. *
  148. */
  149. //左
  150. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
  151. //右
  152. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
  153. break;
  154. case PART_2NxnU:
  155. /*
  156. * PART_2NxnU (Upper) :
  157. * +--------+--------+
  158. * | |
  159. * +--------+--------+
  160. * | |
  161. * + + +
  162. * | |
  163. * | |
  164. * | |
  165. * +--------+--------+
  166. *
  167. */
  168. //上
  169. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
  170. //下
  171. hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
  172. break;
  173. case PART_2NxnD:
  174. /*
  175. * PART_2NxnD (Down) :
  176. * +--------+--------+
  177. * | |
  178. * | |
  179. * | |
  180. * + + +
  181. * | |
  182. * +--------+--------+
  183. * | |
  184. * +--------+--------+
  185. *
  186. */
  187. //上
  188. hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
  189. //下
  190. hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
  191. break;
  192. case PART_nLx2N:
  193. /*
  194. * PART_nLx2N (Left):
  195. * +----+---+--------+
  196. * | | |
  197. * | | |
  198. * | | |
  199. * + + + +
  200. * | | |
  201. * | | |
  202. * | | |
  203. * +----+---+--------+
  204. *
  205. */
  206. //左
  207. hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
  208. //右
  209. hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
  210. break;
  211. case PART_nRx2N:
  212. /*
  213. * PART_nRx2N (Right):
  214. * +--------+---+----+
  215. * | | |
  216. * | | |
  217. * | | |
  218. * + + + +
  219. * | | |
  220. * | | |
  221. * | | |
  222. * +--------+---+----+
  223. *
  224. */
  225. //左
  226. hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
  227. //右
  228. hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
  229. break;
  230. case PART_NxN:
  231. /*
  232. * PART_NxN:
  233. * +--------+--------+
  234. * | | |
  235. * | | |
  236. * | | |
  237. * +--------+--------+
  238. * | | |
  239. * | | |
  240. * | | |
  241. * +--------+--------+
  242. *
  243. */
  244. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
  245. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
  246. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
  247. hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
  248. break;
  249. }
  250. }
  251. if (!pcm_flag) {
  252. int rqt_root_cbf = 1;
  253. if (lc->cu.pred_mode != MODE_INTRA &&
  254. !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
  255. rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
  256. }
  257. if (rqt_root_cbf) {
  258. const static int cbf[2] = { 0 };
  259. lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
  260. s->sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
  261. s->sps->max_transform_hierarchy_depth_inter;
  262. //处理TU四叉树
  263. ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
  264. log2_cb_size,
  265. log2_cb_size, 0, 0, cbf, cbf);
  266. if (ret < 0)
  267. return ret;
  268. } else {
  269. if (!s->sh.disable_deblocking_filter_flag)
  270. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  271. }
  272. }
  273. }
  274. if (s->pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
  275. ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
  276. x = y_cb * min_cb_width + x_cb;
  277. for (y = 0; y < length; y++) {
  278. memset(&s->qp_y_tab[x], lc->qp_y, length);
  279. x += min_cb_width;
  280. }
  281. if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
  282. ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
  283. lc->qPy_pred = lc->qp_y;
  284. }
  285. set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
  286. return 0;
  287. }

从源代码可以看出,hls_coding_unit()主要进行了两个方面的处理:
(1)调用hls_prediction_unit()处理PU。
(2)调用hls_transform_tree()处理TU树。

本文分析第一个函数hls_prediction_unit()中相关的代码。

hls_prediction_unit()

hls_prediction_unit()用于处理PU。该函数的定义如下所示。
  1. /*
  2. * 处理PU单元-运动补偿
  3. *
  4. * hls_prediction_unit()参数:
  5. * x0 : PU左上角x坐标
  6. * y0 : PU左上角y坐标
  7. * nPbW : PU宽度
  8. * nPbH : PU高度
  9. * log2_cb_size : CB大小取log2()的值
  10. * partIdx : PU的索引号-分成4个块的时候取0-3,分成两个块的时候取0和1
  11. *
  12. * [例]
  13. *
  14. * PART_2NxN:
  15. * +--------+--------+
  16. * | |
  17. * | |
  18. * | |
  19. * +--------+--------+
  20. * | |
  21. * | |
  22. * | |
  23. * +--------+--------+
  24. *
  25. * 上方PU:
  26. * hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
  27. * 下方PU:
  28. * hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
  29. *
  30. */
  31. static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
  32. int nPbW, int nPbH,
  33. int log2_cb_size, int partIdx, int idx)
  34. {
  35. #define POS(c_idx, x, y) \
  36. &s->frame->data[c_idx][((y) >> s->sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
  37. (((x) >> s->sps->hshift[c_idx]) << s->sps->pixel_shift)]
  38. HEVCLocalContext *lc = s->HEVClc;
  39. int merge_idx = 0;
  40. struct MvField current_mv = {{{ 0 }}};
  41. int min_pu_width = s->sps->min_pu_width;
  42. MvField *tab_mvf = s->ref->tab_mvf;
  43. RefPicList *refPicList = s->ref->refPicList;
  44. //参考帧
  45. HEVCFrame *ref0, *ref1;
  46. //分别指向Y,U,V分量
  47. uint8_t *dst0 = POS(0, x0, y0);
  48. uint8_t *dst1 = POS(1, x0, y0);
  49. uint8_t *dst2 = POS(2, x0, y0);
  50. int log2_min_cb_size = s->sps->log2_min_cb_size;
  51. int min_cb_width = s->sps->min_cb_width;
  52. int x_cb = x0 >> log2_min_cb_size;
  53. int y_cb = y0 >> log2_min_cb_size;
  54. int x_pu, y_pu;
  55. int i, j;
  56. int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
  57. if (!skip_flag)
  58. lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
  59. if (skip_flag || lc->pu.merge_flag) {
  60. //Merge模式
  61. if (s->sh.max_num_merge_cand > 1)
  62. merge_idx = ff_hevc_merge_idx_decode(s);
  63. else
  64. merge_idx = 0;
  65. ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  66. partIdx, merge_idx, ¤t_mv);
  67. } else {
  68. //AMVP模式
  69. hevc_luma_mv_mpv_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  70. partIdx, merge_idx, ¤t_mv);
  71. }
  72. x_pu = x0 >> s->sps->log2_min_pu_size;
  73. y_pu = y0 >> s->sps->log2_min_pu_size;
  74. for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  75. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  76. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  77. //参考了List0
  78. if (current_mv.pred_flag & PF_L0) {
  79. ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
  80. if (!ref0)
  81. return;
  82. hevc_await_progress(s, ref0, ¤t_mv.mv[0], y0, nPbH);
  83. }
  84. //参考了List1
  85. if (current_mv.pred_flag & PF_L1) {
  86. ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
  87. if (!ref1)
  88. return;
  89. hevc_await_progress(s, ref1, ¤t_mv.mv[1], y0, nPbH);
  90. }
  91. if (current_mv.pred_flag == PF_L0) {
  92. int x0_c = x0 >> s->sps->hshift[1];
  93. int y0_c = y0 >> s->sps->vshift[1];
  94. int nPbW_c = nPbW >> s->sps->hshift[1];
  95. int nPbH_c = nPbH >> s->sps->vshift[1];
  96. //亮度运动补偿-单向
  97. luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
  98. ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
  99. s->sh.luma_weight_l0[current_mv.ref_idx[0]],
  100. s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
  101. //色度运动补偿
  102. chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
  103. 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
  104. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
  105. chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
  106. 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
  107. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
  108. } else if (current_mv.pred_flag == PF_L1) {
  109. int x0_c = x0 >> s->sps->hshift[1];
  110. int y0_c = y0 >> s->sps->vshift[1];
  111. int nPbW_c = nPbW >> s->sps->hshift[1];
  112. int nPbH_c = nPbH >> s->sps->vshift[1];
  113. luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
  114. ¤t_mv.mv[1], x0, y0, nPbW, nPbH,
  115. s->sh.luma_weight_l1[current_mv.ref_idx[1]],
  116. s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
  117. chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
  118. 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
  119. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
  120. chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
  121. 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
  122. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
  123. } else if (current_mv.pred_flag == PF_BI) {
  124. int x0_c = x0 >> s->sps->hshift[1];
  125. int y0_c = y0 >> s->sps->vshift[1];
  126. int nPbW_c = nPbW >> s->sps->hshift[1];
  127. int nPbH_c = nPbH >> s->sps->vshift[1];
  128. //亮度运动补偿-双向
  129. luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
  130. ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
  131. ref1->frame, ¤t_mv.mv[1], ¤t_mv);
  132. chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
  133. x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 0);
  134. chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
  135. x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 1);
  136. }
  137. }

从源代码可以看出,hls_prediction_unit()完成了以下两步工作:
(1)解析码流得到运动矢量。HEVC中包含了Merge和AMVP两种运动矢量预测技术。对于使用Merge的码流,调用ff_hevc_luma_mv_merge_mode();对于使用AMVP的码流,调用hevc_luma_mv_mpv_mode()。
(2)根据运动矢量进行运动补偿。对于单向预测亮度运动补偿,调用luma_mc_uni(),对于单向预测色度运动补偿,调用chroma_mc_uni();对于双向预测亮度运动补偿,调用luma_mc_bi(),对于单向预测色度运动补偿,调用chroma_mc_bi()。

luma_mc_uni()

luma_mc_uni()是单向预测亮度运动补偿函数。该函数的定义如下所示。
  1. /**
  2. * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
  3. *
  4. * @param s HEVC decoding context
  5. * @param dst target buffer for block data at block position
  6. * @param dststride stride of the dst buffer
  7. * @param ref reference picture buffer at origin (0, 0)
  8. * @param mv motion vector (relative to block position) to get pixel data from
  9. * @param x_off horizontal position of block from origin (0, 0)
  10. * @param y_off vertical position of block from origin (0, 0)
  11. * @param block_w width of block
  12. * @param block_h height of block
  13. * @param luma_weight weighting factor applied to the luma prediction
  14. * @param luma_offset additive offset applied to the luma prediction value
  15. */
  16. //亮度运动补偿-单向
  17. static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
  18. AVFrame *ref, const Mv *mv, int x_off, int y_off,
  19. int block_w, int block_h, int luma_weight, int luma_offset)
  20. {
  21. HEVCLocalContext *lc = s->HEVClc;
  22. uint8_t *src = ref->data[0];
  23. ptrdiff_t srcstride = ref->linesize[0];
  24. int pic_width = s->sps->width;
  25. int pic_height = s->sps->height;
  26. //亚像素的运动矢量
  27. //mv0,mv1单位是1/4像素,与00000011相与之后保留后两位
  28. int mx = mv->x & 3;
  29. int my = mv->y & 3;
  30. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  31. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  32. int idx = ff_hevc_pel_weight[block_w];
  33. //整像素的偏移值
  34. //mv0,mv1单位是1/4像素,在这里除以4之后单位变成整像素
  35. x_off += mv->x >> 2;
  36. y_off += mv->y >> 2;
  37. src += y_off * srcstride + (x_off << s->sps->pixel_shift);
  38. //边界处处理
  39. if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
  40. x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  41. y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  42. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  43. int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  44. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  45. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
  46. edge_emu_stride, srcstride,
  47. block_w + QPEL_EXTRA,
  48. block_h + QPEL_EXTRA,
  49. x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
  50. pic_width, pic_height);
  51. src = lc->edge_emu_buffer + buf_offset;
  52. srcstride = edge_emu_stride;
  53. }
  54. //运动补偿
  55. if (!weight_flag)//普通的
  56. s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
  57. block_h, mx, my, block_w);
  58. else//加权的
  59. s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
  60. block_h, s->sh.luma_log2_weight_denom,
  61. luma_weight, luma_offset, mx, my, block_w);
  62. }

从源代码可以看出,luma_mc_uni()调用了HEVCDSPContext的put_hevc_qpel_uni()汇编函数完成了运动补偿。

luma_mc_bi()

luma_mc_bi()是双向预测亮度运动补偿函数。该函数的定义如下所示。
  1. /**
  2. * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
  3. *
  4. * @param s HEVC decoding context
  5. * @param dst target buffer for block data at block position
  6. * @param dststride stride of the dst buffer
  7. * @param ref0 reference picture0 buffer at origin (0, 0)
  8. * @param mv0 motion vector0 (relative to block position) to get pixel data from
  9. * @param x_off horizontal position of block from origin (0, 0)
  10. * @param y_off vertical position of block from origin (0, 0)
  11. * @param block_w width of block
  12. * @param block_h height of block
  13. * @param ref1 reference picture1 buffer at origin (0, 0)
  14. * @param mv1 motion vector1 (relative to block position) to get pixel data from
  15. * @param current_mv current motion vector structure
  16. */
  17. //亮度运动补偿-双向
  18. static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
  19. AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
  20. int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
  21. {
  22. HEVCLocalContext *lc = s->HEVClc;
  23. ptrdiff_t src0stride = ref0->linesize[0];
  24. ptrdiff_t src1stride = ref1->linesize[0];
  25. int pic_width = s->sps->width;
  26. int pic_height = s->sps->height;
  27. //亚像素的运动矢量
  28. //mv0,mv1单位是1/4像素,与00000011相与之后保留后两位
  29. int mx0 = mv0->x & 3;
  30. int my0 = mv0->y & 3;
  31. int mx1 = mv1->x & 3;
  32. int my1 = mv1->y & 3;
  33. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  34. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  35. //整像素的偏移值
  36. //mv0,mv1单位是1/4像素,在这里除以4之后单位变成整像素
  37. int x_off0 = x_off + (mv0->x >> 2);
  38. int y_off0 = y_off + (mv0->y >> 2);
  39. int x_off1 = x_off + (mv1->x >> 2);
  40. int y_off1 = y_off + (mv1->y >> 2);
  41. int idx = ff_hevc_pel_weight[block_w];
  42. //匹配块数据(整像素精度,没有进行差值)
  43. //list0
  44. uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->sps->pixel_shift);
  45. //list1
  46. uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->sps->pixel_shift);
  47. //边界位置的处理
  48. if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
  49. x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  50. y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  51. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  52. int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  53. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  54. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
  55. edge_emu_stride, src0stride,
  56. block_w + QPEL_EXTRA,
  57. block_h + QPEL_EXTRA,
  58. x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
  59. pic_width, pic_height);
  60. src0 = lc->edge_emu_buffer + buf_offset;
  61. src0stride = edge_emu_stride;
  62. }
  63. if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
  64. x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  65. y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  66. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  67. int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  68. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  69. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
  70. edge_emu_stride, src1stride,
  71. block_w + QPEL_EXTRA,
  72. block_h + QPEL_EXTRA,
  73. x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
  74. pic_width, pic_height);
  75. src1 = lc->edge_emu_buffer2 + buf_offset;
  76. src1stride = edge_emu_stride;
  77. }
  78. //双向预测分成2步:
  79. // (1)使用list0中的匹配块进行单向预测
  80. // (2)使用list1中的匹配块再次进行单向预测,然后与第1次预测的结果求平均
  81. //第1步
  82. s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
  83. block_h, mx0, my0, block_w);
  84. //第2步
  85. if (!weight_flag)
  86. s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
  87. block_h, mx1, my1, block_w);
  88. else
  89. s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
  90. block_h, s->sh.luma_log2_weight_denom,
  91. s->sh.luma_weight_l0[current_mv->ref_idx[0]],
  92. s->sh.luma_weight_l1[current_mv->ref_idx[1]],
  93. s->sh.luma_offset_l0[current_mv->ref_idx[0]],
  94. s->sh.luma_offset_l1[current_mv->ref_idx[1]],
  95. mx1, my1, block_w);
  96. }

从源代码可以看出,luma_mc_bi()调用了HEVCDSPContext的put_hevc_qpel()和put_hevc_qpel_bi()汇编函数完成了运动补偿。后文将会对这些运动补偿汇编函数进行分析。

运动补偿相关知识

本部分简单总结运动补偿相关的知识,并举几个例子。

运动补偿小知识

本节简单回顾一下《HEVC标准》中有关运动补偿的知识。

PU的划分

HEVC中CU支持如下几种划分方式。可以看出帧内CU只有2种划分模式,而帧间CU支持8种划分模式(其中后4种是非对称划分模式)。

1/4像素运动估计

运动估计的理论基础就是活动图像邻近帧中的景物存在着一定的相关性。因此在压缩编码中不需要传递每一帧的所有信息,而只需要传递帧与帧之间差值就可以了(可以想象,如果画面背景是静止的,那么只需要传递很少的数据)。
在视频编码的运动估计步骤中,会查找与当前宏块或者子宏块“长得像”的宏块作为“匹配块”,然后编码传输匹配块的位置(运动矢量,参考帧)和当前宏块与匹配块之间的微小差别(残差数据)。例如下图中,当前宏块中一个“车轮”在参考帧中找到了形状同样为一个“轮子”的匹配块。
 
最早视频编码标准中都是以整像素的方式进行运动估计的。这样处理的好处是计算简单,坏处是不够精确。随着硬件技术的进步,比较新的视频编码标准(例如MPEG2)中使用1/2像素精度的方式进行运动估计。这样做计算相对复杂,但是计算也相对准确。1/2像素精度运动估计如下图所示。
 
H.264中对运动估计的精度要求又有了提升,变成了1/4像素精度。HEVC在运动估计方面同样使用了1/4精度。在H.264 / HEVC编码和解码的过程中,需要将画面中的像素进行插值——简单地说就是把原先的1个像素点拓展成4x4一共16个点。下图显示了HEVC编码和解码过程中像素插值情况。可以看出原先的A3点的右下方通过插值的方式产生了q1--q15一共15个点。
 
一些实验证明,1/4像素精度基本上达到了运动估计性能提升的极限。更高精度的运动估计并不能更明显的提升性能,却会导致计算复杂度的显著提升。因此现存主流的编解码标准在运动估计方面都采用了1/4精度。曾经有人压缩对比过1/2、1/4、1/8精度的运动估计下编码的视频质量,如下图所示。
 
从图中可以看出:1/4精度相比于1/2精度来说有显著的提升,但是1/8精度实际上和1/4精度是差不多的。

四分之一像素内插方式

HEVC的1/4像素内插的方法和H.264是不一样的。H.264首先通过6抽头的滤波器获得半像素点,然后通过线性内插的方式获得1/4像素点。HEVC则在半像素点使用了8抽头的滤波器,在1/4像素点使用了7抽头的滤波器。以上面四分之一像素插值示意图为例,分别记录一下H.264和HEVC各个差值点的计算方法。
【H.264像素插值方式】
H.264的水平半像素点q2插值公式为:
q2= round( (A1 – 5*A2 + 20*A3 + 20*A4 – 5*A5 + A6 ) / 32)
PS:垂直半像素点q8,对角线半像素点q10的计算方法是类似的。
H.264的1/4像素点q1插值公式为:
q1=round((A3+q2)/2)
PS:其它1/4像素点的计算方法也是类似的,都是通过整像素点和半像素点线性内插获得。
【HEVC像素插值方式】
HEVC的半像素点q2插值公式为:
q2= round( (–A0 + 4*A1 – 10*A2 + 58*A3 + 17*A4 – 5*A5 + A6 ) / 32)
HEVC的1/4像素点q1插值公式为:
q1= round( (–A0 + 4*A1 – 11*A2 + 40*A3+ 40*A4 – 11*A5 + 4*A6 – A7 ) / 32)
HEVC的3/4像素点q3插值公式为:
q3= round( (A1 – 5*A2 + 17*A3 + 58*A4 – 10*A5 + 4*A6 – A7 ) / 32)
PS:其它1/4像素点的计算方法也是类似的。

单向预测与双向预测

在运动估计的过程中,不仅仅只可以选择一个图像作为参考帧(P帧),而且还可以选择两张图片作为参考帧(B帧)。使用一张图像作为参考帧称为单向预测,而使用一张图像作为参考帧称为双向预测。使用单向预测的时候,直接将参考帧上的匹配块的数据“搬移下来”作后续的处理(“赋值”),而使用双向预测的时候,需要首先将两个参考帧上的匹配块的数据求平均值(“求平均”),然后再做后续处理。毫无疑问双向预测可以得到更好的压缩效果,但是也会使码流变得复杂一些。双向预测的示意图如下所示。
 

帧间预测实例

本节以一段《Sintel》动画的码流为例,看一下HEVC码流中的运动补偿相关的信息。

【示例1-P帧】

下图为一个P帧解码后的图像。
 
下图为该帧CTU的划分方式。其中蓝色的是P类型CU,而红色的是I类型CU,透明的是Skip类型的CU。可以看出画面复杂的地方CTU划分比较细。
 
下图为每个P类型的CU的运动矢量信息。
 
下图显示了运动矢量与图像内容变化之间的关系。
 
下图为经过运动补偿,没有进行残差叠加处理的视频内容。
 
下图为该帧的残差信息。
 

【示例2-B帧】

下图为一个B帧解码后的图像。
 
下图为该帧CTU的划分方式。其中蓝色的是P类型CU,而绿色的是B类型CU,透明的是Skip类型的CU。可以看出画面复杂的地方CTU划分比较细。
 
下图为每个CU的运动矢量信息。
 
下图显示了运动矢量与图像内容变化之间的关系。
 

【示例3】

本节以一段《Sintel》动画的码流为例,看一下HEVC码流中帧间预测具体的信息。下图为一个P帧解码后的图像。
 
下图显示了该帧图像的帧间预测模式(部分CU采用了帧内预测模式,在这里不分析)。在这里我们选择一个8x8 CU(图中以紫色方框标出)看一下其中具体的信息。该CU采用了AMVP帧间预测方式,运动矢量为(8,-10)。
 
该8x8CU的像素值如下图所示。
 
下图显示了该CU中一个像素的计算方式。从图中可以看出,每个像素值通过匹配块周围的8x8=64个像素内插获得结果。图中的像素内插后的结果是“220”。



运动补偿汇编函数源代码

运动补偿相关的汇编函数位于HEVCDSPContext中。HEVCDSPContext的初始化函数是ff_hevc_dsp_init()。该函数对HEVCDSPContext结构体中的函数指针进行了赋值。FFmpeg HEVC解码器运行的过程中只要调用HEVCDSPContext的函数指针就可以完成相应的功能。

ff_hevc_dsp_init()

ff_hevc_dsp_init()用于初始化HEVCDSPContext结构体中的汇编函数指针。该函数的定义如下所示。
  1. void ff_hevc_dsp_init(HEVCDSPContext *hevcdsp, int bit_depth)
  2. {
  3. #undef FUNC
  4. #define FUNC(a, depth) a ## _ ## depth
  5. #undef PEL_FUNC
  6. #define PEL_FUNC(dst1, idx1, idx2, a, depth) \
  7. for(i = 0 ; i < 10 ; i++) \
  8. { \
  9. hevcdsp->dst1[i][idx1][idx2] = a ## _ ## depth; \
  10. }
  11. #undef EPEL_FUNCS
  12. #define EPEL_FUNCS(depth) \
  13. PEL_FUNC(put_hevc_epel, 0, 0, put_hevc_pel_pixels, depth); \
  14. PEL_FUNC(put_hevc_epel, 0, 1, put_hevc_epel_h, depth); \
  15. PEL_FUNC(put_hevc_epel, 1, 0, put_hevc_epel_v, depth); \
  16. PEL_FUNC(put_hevc_epel, 1, 1, put_hevc_epel_hv, depth)
  17. #undef EPEL_UNI_FUNCS
  18. #define EPEL_UNI_FUNCS(depth) \
  19. PEL_FUNC(put_hevc_epel_uni, 0, 0, put_hevc_pel_uni_pixels, depth); \
  20. PEL_FUNC(put_hevc_epel_uni, 0, 1, put_hevc_epel_uni_h, depth); \
  21. PEL_FUNC(put_hevc_epel_uni, 1, 0, put_hevc_epel_uni_v, depth); \
  22. PEL_FUNC(put_hevc_epel_uni, 1, 1, put_hevc_epel_uni_hv, depth); \
  23. PEL_FUNC(put_hevc_epel_uni_w, 0, 0, put_hevc_pel_uni_w_pixels, depth); \
  24. PEL_FUNC(put_hevc_epel_uni_w, 0, 1, put_hevc_epel_uni_w_h, depth); \
  25. PEL_FUNC(put_hevc_epel_uni_w, 1, 0, put_hevc_epel_uni_w_v, depth); \
  26. PEL_FUNC(put_hevc_epel_uni_w, 1, 1, put_hevc_epel_uni_w_hv, depth)
  27. #undef EPEL_BI_FUNCS
  28. #define EPEL_BI_FUNCS(depth) \
  29. PEL_FUNC(put_hevc_epel_bi, 0, 0, put_hevc_pel_bi_pixels, depth); \
  30. PEL_FUNC(put_hevc_epel_bi, 0, 1, put_hevc_epel_bi_h, depth); \
  31. PEL_FUNC(put_hevc_epel_bi, 1, 0, put_hevc_epel_bi_v, depth); \
  32. PEL_FUNC(put_hevc_epel_bi, 1, 1, put_hevc_epel_bi_hv, depth); \
  33. PEL_FUNC(put_hevc_epel_bi_w, 0, 0, put_hevc_pel_bi_w_pixels, depth); \
  34. PEL_FUNC(put_hevc_epel_bi_w, 0, 1, put_hevc_epel_bi_w_h, depth); \
  35. PEL_FUNC(put_hevc_epel_bi_w, 1, 0, put_hevc_epel_bi_w_v, depth); \
  36. PEL_FUNC(put_hevc_epel_bi_w, 1, 1, put_hevc_epel_bi_w_hv, depth)
  37. #undef QPEL_FUNCS
  38. #define QPEL_FUNCS(depth) \
  39. PEL_FUNC(put_hevc_qpel, 0, 0, put_hevc_pel_pixels, depth); \
  40. PEL_FUNC(put_hevc_qpel, 0, 1, put_hevc_qpel_h, depth); \
  41. PEL_FUNC(put_hevc_qpel, 1, 0, put_hevc_qpel_v, depth); \
  42. PEL_FUNC(put_hevc_qpel, 1, 1, put_hevc_qpel_hv, depth)
  43. #undef QPEL_UNI_FUNCS
  44. #define QPEL_UNI_FUNCS(depth) \
  45. PEL_FUNC(put_hevc_qpel_uni, 0, 0, put_hevc_pel_uni_pixels, depth); \
  46. PEL_FUNC(put_hevc_qpel_uni, 0, 1, put_hevc_qpel_uni_h, depth); \
  47. PEL_FUNC(put_hevc_qpel_uni, 1, 0, put_hevc_qpel_uni_v, depth); \
  48. PEL_FUNC(put_hevc_qpel_uni, 1, 1, put_hevc_qpel_uni_hv, depth); \
  49. PEL_FUNC(put_hevc_qpel_uni_w, 0, 0, put_hevc_pel_uni_w_pixels, depth); \
  50. PEL_FUNC(put_hevc_qpel_uni_w, 0, 1, put_hevc_qpel_uni_w_h, depth); \
  51. PEL_FUNC(put_hevc_qpel_uni_w, 1, 0, put_hevc_qpel_uni_w_v, depth); \
  52. PEL_FUNC(put_hevc_qpel_uni_w, 1, 1, put_hevc_qpel_uni_w_hv, depth)
  53. #undef QPEL_BI_FUNCS
  54. #define QPEL_BI_FUNCS(depth) \
  55. PEL_FUNC(put_hevc_qpel_bi, 0, 0, put_hevc_pel_bi_pixels, depth); \
  56. PEL_FUNC(put_hevc_qpel_bi, 0, 1, put_hevc_qpel_bi_h, depth); \
  57. PEL_FUNC(put_hevc_qpel_bi, 1, 0, put_hevc_qpel_bi_v, depth); \
  58. PEL_FUNC(put_hevc_qpel_bi, 1, 1, put_hevc_qpel_bi_hv, depth); \
  59. PEL_FUNC(put_hevc_qpel_bi_w, 0, 0, put_hevc_pel_bi_w_pixels, depth); \
  60. PEL_FUNC(put_hevc_qpel_bi_w, 0, 1, put_hevc_qpel_bi_w_h, depth); \
  61. PEL_FUNC(put_hevc_qpel_bi_w, 1, 0, put_hevc_qpel_bi_w_v, depth); \
  62. PEL_FUNC(put_hevc_qpel_bi_w, 1, 1, put_hevc_qpel_bi_w_hv, depth)
  63. #define HEVC_DSP(depth) \
  64. hevcdsp->put_pcm = FUNC(put_pcm, depth); \
  65. hevcdsp->transform_add[0] = FUNC(transform_add4x4, depth); \
  66. hevcdsp->transform_add[1] = FUNC(transform_add8x8, depth); \
  67. hevcdsp->transform_add[2] = FUNC(transform_add16x16, depth); \
  68. hevcdsp->transform_add[3] = FUNC(transform_add32x32, depth); \
  69. hevcdsp->transform_skip = FUNC(transform_skip, depth); \
  70. hevcdsp->transform_rdpcm = FUNC(transform_rdpcm, depth); \
  71. hevcdsp->idct_4x4_luma = FUNC(transform_4x4_luma, depth); \
  72. hevcdsp->idct[0] = FUNC(idct_4x4, depth); \
  73. hevcdsp->idct[1] = FUNC(idct_8x8, depth); \
  74. hevcdsp->idct[2] = FUNC(idct_16x16, depth); \
  75. hevcdsp->idct[3] = FUNC(idct_32x32, depth); \
  76. \
  77. hevcdsp->idct_dc[0] = FUNC(idct_4x4_dc, depth); \
  78. hevcdsp->idct_dc[1] = FUNC(idct_8x8_dc, depth); \
  79. hevcdsp->idct_dc[2] = FUNC(idct_16x16_dc, depth); \
  80. hevcdsp->idct_dc[3] = FUNC(idct_32x32_dc, depth); \
  81. \
  82. hevcdsp->sao_band_filter = FUNC(sao_band_filter_0, depth); \
  83. hevcdsp->sao_edge_filter[0] = FUNC(sao_edge_filter_0, depth); \
  84. hevcdsp->sao_edge_filter[1] = FUNC(sao_edge_filter_1, depth); \
  85. \
  86. QPEL_FUNCS(depth); \
  87. QPEL_UNI_FUNCS(depth); \
  88. QPEL_BI_FUNCS(depth); \
  89. EPEL_FUNCS(depth); \
  90. EPEL_UNI_FUNCS(depth); \
  91. EPEL_BI_FUNCS(depth); \
  92. \
  93. hevcdsp->hevc_h_loop_filter_luma = FUNC(hevc_h_loop_filter_luma, depth); \
  94. hevcdsp->hevc_v_loop_filter_luma = FUNC(hevc_v_loop_filter_luma, depth); \
  95. hevcdsp->hevc_h_loop_filter_chroma = FUNC(hevc_h_loop_filter_chroma, depth); \
  96. hevcdsp->hevc_v_loop_filter_chroma = FUNC(hevc_v_loop_filter_chroma, depth); \
  97. hevcdsp->hevc_h_loop_filter_luma_c = FUNC(hevc_h_loop_filter_luma, depth); \
  98. hevcdsp->hevc_v_loop_filter_luma_c = FUNC(hevc_v_loop_filter_luma, depth); \
  99. hevcdsp->hevc_h_loop_filter_chroma_c = FUNC(hevc_h_loop_filter_chroma, depth); \
  100. hevcdsp->hevc_v_loop_filter_chroma_c = FUNC(hevc_v_loop_filter_chroma, depth)
  101. int i = 0;
  102. switch (bit_depth) {
  103. case 9:
  104. HEVC_DSP(9);
  105. break;
  106. case 10:
  107. HEVC_DSP(10);
  108. break;
  109. case 12:
  110. HEVC_DSP(12);
  111. break;
  112. default:
  113. HEVC_DSP(8);
  114. break;
  115. }
  116. if (ARCH_X86)
  117. ff_hevc_dsp_init_x86(hevcdsp, bit_depth);
  118. }

从源代码可以看出,ff_hevc_dsp_init()函数中包含一个名为“HEVC_DSP(depth)”的很长的宏定义。该宏定义中包含了C语言版本的帧内预测函数的初始化代码。ff_hevc_dsp_init()会根据系统的颜色位深bit_depth初始化相应的C语言版本的帧内预测函数。在函数的末尾则包含了汇编函数的初始化函数:如果系统是X86架构的,则会调用ff_hevc_dsp_init_x86()初始化X86平台下经过汇编优化的函数。下面以8bit颜色位深为例,看一下“HEVC_DSP(8)”的展开结果。
  1. hevcdsp->put_pcm = put_pcm_8;
  2. hevcdsp->transform_add[0] = transform_add4x4_8;
  3. hevcdsp->transform_add[1] = transform_add8x8_8;
  4. hevcdsp->transform_add[2] = transform_add16x16_8;
  5. hevcdsp->transform_add[3] = transform_add32x32_8;
  6. hevcdsp->transform_skip = transform_skip_8;
  7. hevcdsp->transform_rdpcm = transform_rdpcm_8;
  8. hevcdsp->idct_4x4_luma = transform_4x4_luma_8;
  9. hevcdsp->idct[0] = idct_4x4_8;
  10. hevcdsp->idct[1] = idct_8x8_8;
  11. hevcdsp->idct[2] = idct_16x16_8;
  12. hevcdsp->idct[3] = idct_32x32_8;
  13. hevcdsp->idct_dc[0] = idct_4x4_dc_8;
  14. hevcdsp->idct_dc[1] = idct_8x8_dc_8;
  15. hevcdsp->idct_dc[2] = idct_16x16_dc_8;
  16. hevcdsp->idct_dc[3] = idct_32x32_dc_8;
  17. hevcdsp->sao_band_filter = sao_band_filter_0_8;
  18. hevcdsp->sao_edge_filter[0] = sao_edge_filter_0_8;
  19. hevcdsp->sao_edge_filter[1] = sao_edge_filter_1_8;
  20. for(i = 0 ; i < 10 ; i++)
  21. {
  22. hevcdsp->put_hevc_qpel[i][0][0] = put_hevc_pel_pixels_8;
  23. };
  24. for(i = 0 ; i < 10 ; i++)
  25. {
  26. hevcdsp->put_hevc_qpel[i][0][1] = put_hevc_qpel_h_8;
  27. };
  28. for(i = 0 ; i < 10 ; i++)
  29. {
  30. hevcdsp->put_hevc_qpel[i][1][0] = put_hevc_qpel_v_8;
  31. };
  32. for(i = 0 ; i < 10 ; i++)
  33. {
  34. hevcdsp->put_hevc_qpel[i][1][1] = put_hevc_qpel_hv_8;
  35. };
  36. for(i = 0 ; i < 10 ; i++)
  37. {
  38. hevcdsp->put_hevc_qpel_uni[i][0][0] = put_hevc_pel_uni_pixels_8;
  39. };
  40. for(i = 0 ; i < 10 ; i++)
  41. {
  42. hevcdsp->put_hevc_qpel_uni[i][0][1] = put_hevc_qpel_uni_h_8;
  43. };
  44. for(i = 0 ; i < 10 ; i++)
  45. {
  46. hevcdsp->put_hevc_qpel_uni[i][1][0] = put_hevc_qpel_uni_v_8;
  47. };
  48. for(i = 0 ; i < 10 ; i++)
  49. {
  50. hevcdsp->put_hevc_qpel_uni[i][1][1] = put_hevc_qpel_uni_hv_8;
  51. };
  52. for(i = 0 ; i < 10 ; i++)
  53. {
  54. hevcdsp->put_hevc_qpel_uni_w[i][0][0] = put_hevc_pel_uni_w_pixels_8;
  55. };
  56. for(i = 0 ; i < 10 ; i++)
  57. {
  58. hevcdsp->put_hevc_qpel_uni_w[i][0][1] = put_hevc_qpel_uni_w_h_8;
  59. };
  60. for(i = 0 ; i < 10 ; i++)
  61. {
  62. hevcdsp->put_hevc_qpel_uni_w[i][1][0] = put_hevc_qpel_uni_w_v_8;
  63. };
  64. for(i = 0 ; i < 10 ; i++)
  65. {
  66. hevcdsp->put_hevc_qpel_uni_w[i][1][1] = put_hevc_qpel_uni_w_hv_8;
  67. };
  68. for(i = 0 ; i < 10 ; i++)
  69. {
  70. hevcdsp->put_hevc_qpel_bi[i][0][0] = put_hevc_pel_bi_pixels_8;
  71. };
  72. for(i = 0 ; i < 10 ; i++)
  73. {
  74. hevcdsp->put_hevc_qpel_bi[i][0][1] = put_hevc_qpel_bi_h_8;
  75. };
  76. for(i = 0 ; i < 10 ; i++)
  77. {
  78. hevcdsp->put_hevc_qpel_bi[i][1][0] = put_hevc_qpel_bi_v_8;
  79. };
  80. for(i = 0 ; i < 10 ; i++)
  81. {
  82. hevcdsp->put_hevc_qpel_bi[i][1][1] = put_hevc_qpel_bi_hv_8;
  83. };
  84. for(i = 0 ; i < 10 ; i++)
  85. {
  86. hevcdsp->put_hevc_qpel_bi_w[i][0][0] = put_hevc_pel_bi_w_pixels_8;
  87. };
  88. for(i = 0 ; i < 10 ; i++)
  89. {
  90. hevcdsp->put_hevc_qpel_bi_w[i][0][1] = put_hevc_qpel_bi_w_h_8;
  91. };
  92. for(i = 0 ; i < 10 ; i++)
  93. {
  94. hevcdsp->put_hevc_qpel_bi_w[i][1][0] = put_hevc_qpel_bi_w_v_8;
  95. };
  96. for(i = 0 ; i < 10 ; i++)
  97. {
  98. hevcdsp->put_hevc_qpel_bi_w[i][1][1] = put_hevc_qpel_bi_w_hv_8;
  99. };
  100. for(i = 0 ; i < 10 ; i++)
  101. {
  102. hevcdsp->put_hevc_epel[i][0][0] = put_hevc_pel_pixels_8;
  103. };
  104. for(i = 0 ; i < 10 ; i++)
  105. {
  106. hevcdsp->put_hevc_epel[i][0][1] = put_hevc_epel_h_8;
  107. };
  108. for(i = 0 ; i < 10 ; i++)
  109. {
  110. hevcdsp->put_hevc_epel[i][1][0] = put_hevc_epel_v_8;
  111. };
  112. for(i = 0 ; i < 10 ; i++)
  113. {
  114. hevcdsp->put_hevc_epel[i][1][1] = put_hevc_epel_hv_8;
  115. };
  116. for(i = 0 ; i < 10 ; i++)
  117. {
  118. hevcdsp->put_hevc_epel_uni[i][0][0] = put_hevc_pel_uni_pixels_8;
  119. };
  120. for(i = 0 ; i < 10 ; i++)
  121. {
  122. hevcdsp->put_hevc_epel_uni[i][0][1] = put_hevc_epel_uni_h_8;
  123. };
  124. for(i = 0 ; i < 10 ; i++)
  125. {
  126. hevcdsp->put_hevc_epel_uni[i][1][0] = put_hevc_epel_uni_v_8;
  127. };
  128. for(i = 0 ; i < 10 ; i++)
  129. {
  130. hevcdsp->put_hevc_epel_uni[i][1][1] = put_hevc_epel_uni_hv_8;
  131. };
  132. for(i = 0 ; i < 10 ; i++)
  133. {
  134. hevcdsp->put_hevc_epel_uni_w[i][0][0] = put_hevc_pel_uni_w_pixels_8;
  135. };
  136. for(i = 0 ; i < 10 ; i++)
  137. {
  138. hevcdsp->put_hevc_epel_uni_w[i][0][1] = put_hevc_epel_uni_w_h_8;
  139. };
  140. for(i = 0 ; i < 10 ; i++)
  141. {
  142. hevcdsp->put_hevc_epel_uni_w[i][1][0] = put_hevc_epel_uni_w_v_8;
  143. };
  144. for(i = 0 ; i < 10 ; i++)
  145. {
  146. hevcdsp->put_hevc_epel_uni_w[i][1][1] = put_hevc_epel_uni_w_hv_8;
  147. };
  148. for(i = 0 ; i < 10 ; i++)
  149. {
  150. hevcdsp->put_hevc_epel_bi[i][0][0] = put_hevc_pel_bi_pixels_8;
  151. };
  152. for(i = 0 ; i < 10 ; i++)
  153. {
  154. hevcdsp->put_hevc_epel_bi[i][0][1] = put_hevc_epel_bi_h_8;
  155. };
  156. for(i = 0 ; i < 10 ; i++)
  157. {
  158. hevcdsp->put_hevc_epel_bi[i][1][0] = put_hevc_epel_bi_v_8;
  159. };
  160. for(i = 0 ; i < 10 ; i++)
  161. {
  162. hevcdsp->put_hevc_epel_bi[i][1][1] = put_hevc_epel_bi_hv_8;
  163. };
  164. for(i = 0 ; i < 10 ; i++)
  165. {
  166. hevcdsp->put_hevc_epel_bi_w[i][0][0] = put_hevc_pel_bi_w_pixels_8;
  167. };
  168. for(i = 0 ; i < 10 ; i++)
  169. {
  170. hevcdsp->put_hevc_epel_bi_w[i][0][1] = put_hevc_epel_bi_w_h_8;
  171. };
  172. for(i = 0 ; i < 10 ; i++)
  173. {
  174. hevcdsp->put_hevc_epel_bi_w[i][1][0] = put_hevc_epel_bi_w_v_8;
  175. };
  176. for(i = 0 ; i < 10 ; i++)
  177. {
  178. hevcdsp->put_hevc_epel_bi_w[i][1][1] = put_hevc_epel_bi_w_hv_8;
  179. };
  180. hevcdsp->hevc_h_loop_filter_luma = hevc_h_loop_filter_luma_8;
  181. hevcdsp->hevc_v_loop_filter_luma = hevc_v_loop_filter_luma_8;
  182. hevcdsp->hevc_h_loop_filter_chroma = hevc_h_loop_filter_chroma_8;
  183. hevcdsp->hevc_v_loop_filter_chroma = hevc_v_loop_filter_chroma_8;
  184. hevcdsp->hevc_h_loop_filter_luma_c = hevc_h_loop_filter_luma_8;
  185. hevcdsp->hevc_v_loop_filter_luma_c = hevc_v_loop_filter_luma_8;
  186. hevcdsp->hevc_h_loop_filter_chroma_c = hevc_h_loop_filter_chroma_8;
  187. hevcdsp->hevc_v_loop_filter_chroma_c = hevc_v_loop_filter_chroma_8

可以看出“HEVC_DSP(8)”中包含了DCT、IDCT、1/4像素运动补偿、SAO滤波器、去块效应滤波器等模块的C语言版本函数。本文关注其中的1/4像素运动补偿函数。通过上述代码可以总结出下面几个用于像素插值的函数:
HEVCDSPContext->put_hevc_qpel_uni[i][0][1]():单向预测水平像素插值函数。C语言版本函数为put_hevc_qpel_uni_h_8()
HEVCDSPContext->put_hevc_qpel_uni[i][1][0]():单向预测垂直像素插值函数。C语言版本函数为put_hevc_qpel_uni_v_8()
HEVCDSPContext->put_hevc_qpel_uni[i][1][1]():单向预测中心像素插值函数。C语言版本函数为put_hevc_qpel_uni_hv_8()
HEVCDSPContext->put_hevc_qpel_bi[i][0][1]():双向预测水平像素插值函数。C语言版本函数为put_hevc_qpel_bi_h_8()
HEVCDSPContext->put_hevc_qpel_bi[i][1][0]():双向预测垂直像素插值函数。C语言版本函数为put_hevc_qpel_bi_v_8()
HEVCDSPContext->put_hevc_qpel_bi[i][1][1]():双向预测中心像素插值函数。C语言版本函数为put_hevc_qpel_bi_hv_8()
下文例举其中的几个函数进行分析。

put_hevc_qpel_uni_h_8()

put_hevc_qpel_uni_h_8()是单向预测水平像素插值函数。该函数的定义如下所示。

  1. /*
  2. * 单向预测
  3. * 水平(Horizontal)滤波像素插值
  4. *
  5. *
  6. * A B C D X E F G H
  7. *
  8. *
  9. * 参数:
  10. * _dst:输出的插值后像素
  11. * _dststride:输出一行像素数据的大小
  12. * _src:输入的整像素
  13. * _srcstride:输入一行像素数据的大小
  14. * height:像素的宽
  15. * width:像素的高
  16. * mx:运动矢量亚像素x方向取值。以1/4像素为基本单位
  17. * my:运动矢量亚像素x方向取值。以1/4像素为基本单位
  18. *
  19. */
  20. static void FUNC(put_hevc_qpel_uni_h)(uint8_t *_dst, ptrdiff_t _dststride,
  21. uint8_t *_src, ptrdiff_t _srcstride,
  22. int height, intptr_t mx, intptr_t my, int width)
  23. {
  24. int x, y;
  25. pixel *src = (pixel*)_src;
  26. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  27. pixel *dst = (pixel *)_dst;
  28. ptrdiff_t dststride = _dststride / sizeof(pixel);
  29. //ff_hevc_qpel_filters[]是滤波器插值系数数组
  30. //[0]为1/4像素点插值;[1]为半像素点插值;[2]为3/4像素点插值
  31. const int8_t *filter = ff_hevc_qpel_filters[mx - 1];
  32. int shift = 14 - BIT_DEPTH;
  33. #if BIT_DEPTH < 14
  34. int offset = 1 << (shift - 1);
  35. #else
  36. int offset = 0;
  37. #endif
  38. //处理x*y个像素
  39. //注意,调用了QPEL_FILTER(),其中使用filter[]中的系数进行滤波。
  40. //QPEL_FILTER()的参数是(src, 1)
  41. //其中第2个参数stride代表用于滤波的点之前的间距。取1的话是水平滤波,取srcstride的话是垂直滤波
  42. for (y = 0; y < height; y++) {
  43. for (x = 0; x < width; x++)
  44. dst[x] = av_clip_pixel(((QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) + offset) >> shift);
  45. src += srcstride;
  46. dst += dststride;
  47. }
  48. }

put_hevc_qpel_uni_h_8()源代码中的filter[]用于从静态数组ff_hevc_qpel_filters[3][]中选择一组滤波参数。该数组中一共有3组参数可以选择,分别对应着1/4像素插值点、半像素插值点、3/4像素插值点。ff_hevc_qpel_filters[3][]定义如下所示。
  1. //滤波器插值系数数组
  2. //[0]为1/4像素点插值;[1]为半像素点插值;[2]为3/4像素点插值
  3. DECLARE_ALIGNED(16, const int8_t, ff_hevc_qpel_filters[3][16]) = {
  4. //1/4像素位置
  5. { -1, 4,-10, 58, 17, -5, 1, 0, -1, 4,-10, 58, 17, -5, 1, 0},
  6. //半像素位置
  7. { -1, 4,-11, 40, 40,-11, 4, -1, -1, 4,-11, 40, 40,-11, 4, -1},
  8. //3/4像素位置
  9. { 0, 1, -5, 17, 58,-10, 4, -1, 0, 1, -5, 17, 58,-10, 4, -1}
  10. };
在选定了滤波参数后,put_hevc_qpel_uni_h_8()就开始逐点对wxh的像素块进行插值。每个点在插值的时候会调用一个宏“QPEL_FILTER(src, 1)”用于进行具体的滤波工作。“QPEL_FILTER(src, stride)”是一个用于滤波的宏,定义如下所示。
  1. //半像素插值滤波器
  2. //8个点
  3. //filter[]中存储了系数
  4. #define QPEL_FILTER(src, stride) \
  5. (filter[0] * src[x - 3 * stride] + \
  6. filter[1] * src[x - 2 * stride] + \
  7. filter[2] * src[x - stride] + \
  8. filter[3] * src[x ] + \
  9. filter[4] * src[x + stride] + \
  10. filter[5] * src[x + 2 * stride] + \
  11. filter[6] * src[x + 3 * stride] + \
  12. filter[7] * src[x + 4 * stride])
“QPEL_FILTER(src, 1)”展开后的结果如下图所示。
  1. av_clip_uint8_c((((filter[0] * src[x - 3 * 1] +
  2. filter[1] * src[x - 2 * 1] +
  3. filter[2] * src[x - 1] +
  4. filter[3] * src[x ] +
  5. filter[4] * src[x + 1] +
  6. filter[5] * src[x + 2 * 1] +
  7. filter[6] * src[x + 3 * 1] +
  8. filter[7] * src[x + 4 * 1]) >> (8 - 8)) + offset) >> shift)
可以看出QPEL_FILTER()在滤波的点左右共取了8个点进行滤波处理。

put_hevc_qpel_uni_v_8()

put_hevc_qpel_uni_v_8()是单向预测垂直像素插值函数。该函数的定义如下所示。
  1. /*
  2. * 单向预测
  3. * 垂直(Vertical)滤波像素插值
  4. *
  5. * A
  6. *
  7. * B
  8. *
  9. * C
  10. *
  11. * D
  12. * X
  13. * E
  14. *
  15. * F
  16. *
  17. * G
  18. *
  19. * H
  20. *
  21. * 参数:
  22. * _dst:输出的插值后像素
  23. * _dststride:输出一行像素数据的大小
  24. * _src:输入的整像素
  25. * _srcstride:输入一行像素数据的大小
  26. * height:像素的宽
  27. * width:像素的高
  28. * mx:运动矢量亚像素x方向取值。以1/4像素为基本单位
  29. * my:运动矢量亚像素x方向取值。以1/4像素为基本单位
  30. *
  31. */
  32. static void FUNC(put_hevc_qpel_uni_v)(uint8_t *_dst, ptrdiff_t _dststride,
  33. uint8_t *_src, ptrdiff_t _srcstride,
  34. int height, intptr_t mx, intptr_t my, int width)
  35. {
  36. int x, y;
  37. pixel *src = (pixel*)_src;
  38. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  39. pixel *dst = (pixel *)_dst;
  40. ptrdiff_t dststride = _dststride / sizeof(pixel);
  41. //ff_hevc_qpel_filters[]是滤波器插值系数数组
  42. //[0]为1/4像素点插值;[1]为半像素点插值;[2]为3/4像素点插值
  43. const int8_t *filter = ff_hevc_qpel_filters[my - 1];
  44. int shift = 14 - BIT_DEPTH;
  45. #if BIT_DEPTH < 14
  46. int offset = 1 << (shift - 1);
  47. #else
  48. int offset = 0;
  49. #endif
  50. //处理x*y个像素
  51. //注意,调用了QPEL_FILTER(),其中使用filter[]中的系数进行滤波。
  52. //QPEL_FILTER()的参数是(src, srcstride)
  53. //其中第2个参数stride代表用于滤波的点之前的间距。取1的话是水平滤波,取srcstride的话是垂直滤波
  54. for (y = 0; y < height; y++) {
  55. for (x = 0; x < width; x++)
  56. dst[x] = av_clip_pixel(((QPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) + offset) >> shift);
  57. src += srcstride;
  58. dst += dststride;
  59. }
  60. }

从源代码可以看出,put_hevc_qpel_uni_v_8()的流程和put_hevc_qpel_uni_h_8()基本上是一模一样的。同样也是先选择一组系数存于filter[]中,然后调用“QPEL_FILTER()”进行滤波。它们之间的区别在于put_hevc_qpel_uni_v_8()中滤波的宏是“QPEL_FILTER(src, srcstride)”而put_hevc_qpel_uni_h_8()中滤波的宏是“QPEL_FILTER(src, 1)”。如此一来就选择了垂直的8个点进行滤波。

put_hevc_qpel_uni_hv_8()

put_hevc_qpel_uni_hv_8()是单向预测中间位置像素插值函数。该函数的定义如下所示。
  1. /*
  2. * 单向预测
  3. * 中间(hv)滤波像素插值
  4. *
  5. * 需要水平滤波和垂直滤波
  6. *
  7. */
  8. static void FUNC(put_hevc_qpel_uni_hv)(uint8_t *_dst, ptrdiff_t _dststride,
  9. uint8_t *_src, ptrdiff_t _srcstride,
  10. int height, intptr_t mx, intptr_t my, int width)
  11. {
  12. int x, y;
  13. const int8_t *filter;
  14. pixel *src = (pixel*)_src;
  15. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  16. pixel *dst = (pixel *)_dst;
  17. ptrdiff_t dststride = _dststride / sizeof(pixel);
  18. int16_t tmp_array[(MAX_PB_SIZE + QPEL_EXTRA) * MAX_PB_SIZE];
  19. int16_t *tmp = tmp_array;
  20. int shift = 14 - BIT_DEPTH;
  21. #if BIT_DEPTH < 14
  22. int offset = 1 << (shift - 1);
  23. #else
  24. int offset = 0;
  25. #endif
  26. src -= QPEL_EXTRA_BEFORE * srcstride;
  27. filter = ff_hevc_qpel_filters[mx - 1];
  28. //先水平像素插值
  29. for (y = 0; y < height + QPEL_EXTRA; y++) {
  30. for (x = 0; x < width; x++)
  31. tmp[x] = QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
  32. src += srcstride;
  33. tmp += MAX_PB_SIZE;
  34. }
  35. tmp = tmp_array + QPEL_EXTRA_BEFORE * MAX_PB_SIZE;
  36. filter = ff_hevc_qpel_filters[my - 1];
  37. //处理x*y个像素
  38. for (y = 0; y < height; y++) {
  39. for (x = 0; x < width; x++)
  40. dst[x] = av_clip_pixel(((QPEL_FILTER(tmp, MAX_PB_SIZE) >> 6) + offset) >> shift);
  41. tmp += MAX_PB_SIZE;
  42. dst += dststride;
  43. }
  44. }

从源代码可以看出,put_hevc_qpel_uni_hv_8()是“水平+垂直”的结合。这样就完成了中间位置像素插值的工作。

put_hevc_qpel_bi_h_8()

put_hevc_qpel_bi_h_8()是双向预测水平像素插值函数。该函数的定义如下所示。
  1. /*
  2. * 双向预测
  3. * 水平(Horizontal)滤波像素插值
  4. * 注:双向预测要求将滤波后的像素叠加到另一部分像素(单向运动补偿得到的像素)上后求平均
  5. *
  6. *
  7. * A B C D X E F G H
  8. *
  9. *
  10. * 参数:
  11. * _dst:输出的插值后像素
  12. * _dststride:输出一行像素数据的大小
  13. * _src:输入的整像素
  14. * _srcstride:输入一行像素数据的大小
  15. *
  16. * src2:需要叠加的像素。该像素与滤波后的像素叠加后求平均
  17. *
  18. * height:像素的宽
  19. * width:像素的高
  20. * mx:运动矢量亚像素x方向取值。以1/4像素为基本单位
  21. * my:运动矢量亚像素x方向取值。以1/4像素为基本单位
  22. *
  23. */
  24. static void FUNC(put_hevc_qpel_bi_h)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
  25. int16_t *src2,
  26. int height, intptr_t mx, intptr_t my, int width)
  27. {
  28. int x, y;
  29. pixel *src = (pixel*)_src;
  30. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  31. pixel *dst = (pixel *)_dst;
  32. ptrdiff_t dststride = _dststride / sizeof(pixel);
  33. //ff_hevc_qpel_filters[]是滤波器插值系数数组
  34. //[0]为1/4像素点插值;[1]为半像素点插值;[2]为3/4像素点插值
  35. const int8_t *filter = ff_hevc_qpel_filters[mx - 1];
  36. //注意和单向预测相比多了“+1”,即在后面代码中多右移一位,实现了“除以2”功能
  37. int shift = 14 + 1 - BIT_DEPTH;
  38. #if BIT_DEPTH < 14
  39. int offset = 1 << (shift - 1);
  40. #else
  41. int offset = 0;
  42. #endif
  43. //处理x*y个像素
  44. //注意,在这里使用QPEL_FILTER[]插值后的像素叠加了src2[]然后求平均
  45. //这里求平均是通过把shift变量加1实现的(等同于除以2)
  46. for (y = 0; y < height; y++) {
  47. for (x = 0; x < width; x++)
  48. dst[x] = av_clip_pixel(((QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) + src2[x] + offset) >> shift);
  49. src += srcstride;
  50. dst += dststride;
  51. src2 += MAX_PB_SIZE;
  52. }
  53. }

从源代码可以看出,put_hevc_qpel_bi_h_8()的流程和put_hevc_qpel_uni_h_8()的流程基本上是一样的。由于该函数用于双向预测,所以在求结果的时候是和输入像素“求平均”而不是“赋值”。具体的代码中就是通过将滤波结果与src2[x]相加后除以2实现的(除以2是通过在前面代码中将shift加1实现的)。

剩下的几个插值函数的原理基本一样,在这里不再重复叙述。至此有关FFmpeg HEVC解码器中PU解码部分的代码就分析完毕了。




雷霄骅
leixiaohua1020@126.com
http://blog.csdn.net/leixiaohua1020




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多