• FFmpeg源码:ff_h2645_extract_rbsp函数分析


    一、ff_h2645_extract_rbsp函数的声明

    ff_h2645_extract_rbsp函数的声明放在FFmpeg源码(本文演示用的FFmpeg源码版本为5.0.3,该ffmpeg在CentOS 7.5上通过10.2.1版本的gcc编译)的头文件libavcodec/h2645_parse.h中。

    1. /**
    2. * Extract the raw (unescaped) bitstream.
    3. */
    4. int ff_h2645_extract_rbsp(const uint8_t *src, int length, H2645RBSP *rbsp,
    5. H2645NAL *nal, int small_padding);

    该函数在H.264/H.265的解码时被调用。作用是将去掉第一个startcode的H.264/H.265码流(以下全部以H.264码流为例) 中的第一个NALU 提取出来,分别去掉和保留防竞争字节,存贮到形参nal 指向的缓冲区中。关于 NALU和防竞争字节的概念可以参考:音视频入门基础:H.264专题(3)——EBSP, RBSP和SODB

    形参src:输入型参数。指向缓冲区的指针,该缓冲区存放 去掉第一个startcode(起始码)后的H.264码流。

    形参length:输入型参数。指针src指向的缓冲区的长度,单位为字节。

    形参rbsp为H2645RBSP类型,为输出型参数。

    结构体H2645RBSP 定义如下:

    1. typedef struct H2645RBSP {
    2. uint8_t *rbsp_buffer;
    3. AVBufferRef *rbsp_buffer_ref;
    4. int rbsp_buffer_alloc_size;
    5. int rbsp_buffer_size;
    6. } H2645RBSP;

    执行ff_h2645_extract_rbsp函数后,

    rbsp->rbsp_buffer 变为:去掉startcode和防竞争字节后的H.264码流,可能包含多个NALU。

    rbsp->rbsp_buffer_size 变为:rbsp->rbsp_buffer的大小,单位为字节。

    形参nal为H2645NAL类型,为输出型参数。

    结构体H2645NAL定义如下:

    1. typedef struct H2645NAL {
    2. const uint8_t *data;
    3. int size;
    4. /**
    5. * Size, in bits, of just the data, excluding the stop bit and any trailing
    6. * padding. I.e. what HEVC calls SODB.
    7. */
    8. int size_bits;
    9. int raw_size;
    10. const uint8_t *raw_data;
    11. GetBitContext gb;
    12. /**
    13. * NAL unit type
    14. */
    15. int type;
    16. /**
    17. * H.264 only, nal_ref_idc
    18. */
    19. int ref_idc;
    20. /**
    21. * HEVC only, nuh_temporal_id_plus_1 - 1
    22. */
    23. int temporal_id;
    24. /*
    25. * HEVC only, identifier of layer to which nal unit belongs
    26. */
    27. int nuh_layer_id;
    28. int skipped_bytes;
    29. int skipped_bytes_pos_size;
    30. int *skipped_bytes_pos;
    31. } H2645NAL;

    执行ff_h2645_extract_rbsp函数后,

    nal->data变为:指向缓冲区的指针。该缓冲区存放 “指针src指向的缓冲区中的第一个NALU”,该NALU去掉了startcode和防竞争字节,但保留了NALU Header。(可以理解为NALU Header + RBSP)

    nal->size变为:nal->data指向的缓冲区的大小,单位为字节。

    nal->raw_data变为:指向缓冲区的指针。该缓冲区存放 “指针src指向的缓冲区中的第一个NALU”,该NALU去掉了startcode,但保留了防竞争字节和NALU Header。(可以理解为NALU Header + EBSP)

    nal->raw_size变为:nal->raw_data指向的缓冲区的大小,单位为字节。

    形参small_padding:输入型参数。值一般等于1,可以忽略。

    返回值:整形。值等同于nal->raw_size,为nal->raw_data指向的缓冲区的大小,单位为字节

    二、ff_h2645_extract_rbsp函数的定义

    ff_h2645_extract_rbsp函数定义在libavcodec/h2645_parse.c中:

    1. int ff_h2645_extract_rbsp(const uint8_t *src, int length,
    2. H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
    3. {
    4. int i, si, di;
    5. uint8_t *dst;
    6. nal->skipped_bytes = 0;
    7. #define STARTCODE_TEST \
    8. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
    9. if (src[i + 2] != 3 && src[i + 2] != 0) { \
    10. /* startcode, so we must be past the end */ \
    11. length = i; \
    12. } \
    13. break; \
    14. }
    15. #if HAVE_FAST_UNALIGNED
    16. #define FIND_FIRST_ZERO \
    17. if (i > 0 && !src[i]) \
    18. i--; \
    19. while (src[i]) \
    20. i++
    21. #if HAVE_FAST_64BIT
    22. for (i = 0; i + 1 < length; i += 9) {
    23. if (!((~AV_RN64(src + i) &
    24. (AV_RN64(src + i) - 0x0100010001000101ULL)) &
    25. 0x8000800080008080ULL))
    26. continue;
    27. FIND_FIRST_ZERO;
    28. STARTCODE_TEST;
    29. i -= 7;
    30. }
    31. #else
    32. for (i = 0; i + 1 < length; i += 5) {
    33. if (!((~AV_RN32(src + i) &
    34. (AV_RN32(src + i) - 0x01000101U)) &
    35. 0x80008080U))
    36. continue;
    37. FIND_FIRST_ZERO;
    38. STARTCODE_TEST;
    39. i -= 3;
    40. }
    41. #endif /* HAVE_FAST_64BIT */
    42. #else
    43. for (i = 0; i + 1 < length; i += 2) {
    44. if (src[i])
    45. continue;
    46. if (i > 0 && src[i - 1] == 0)
    47. i--;
    48. STARTCODE_TEST;
    49. }
    50. #endif /* HAVE_FAST_UNALIGNED */
    51. if (i >= length - 1 && small_padding) { // no escaped 0
    52. nal->data =
    53. nal->raw_data = src;
    54. nal->size =
    55. nal->raw_size = length;
    56. return length;
    57. } else if (i > length)
    58. i = length;
    59. dst = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size];
    60. memcpy(dst, src, i);
    61. si = di = i;
    62. while (si + 2 < length) {
    63. // remove escapes (very rare 1:2^22)
    64. if (src[si + 2] > 3) {
    65. dst[di++] = src[si++];
    66. dst[di++] = src[si++];
    67. } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
    68. if (src[si + 2] == 3) { // escape
    69. dst[di++] = 0;
    70. dst[di++] = 0;
    71. si += 3;
    72. if (nal->skipped_bytes_pos) {
    73. nal->skipped_bytes++;
    74. if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
    75. nal->skipped_bytes_pos_size *= 2;
    76. av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
    77. av_reallocp_array(&nal->skipped_bytes_pos,
    78. nal->skipped_bytes_pos_size,
    79. sizeof(*nal->skipped_bytes_pos));
    80. if (!nal->skipped_bytes_pos) {
    81. nal->skipped_bytes_pos_size = 0;
    82. return AVERROR(ENOMEM);
    83. }
    84. }
    85. if (nal->skipped_bytes_pos)
    86. nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
    87. }
    88. continue;
    89. } else // next start code
    90. goto nsc;
    91. }
    92. dst[di++] = src[si++];
    93. }
    94. while (si < length)
    95. dst[di++] = src[si++];
    96. nsc:
    97. memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
    98. nal->data = dst;
    99. nal->size = di;
    100. nal->raw_data = src;
    101. nal->raw_size = si;
    102. rbsp->rbsp_buffer_size += si;
    103. return si;
    104. }

    三、ff_h2645_extract_rbsp函数的内部实现原理分析

    ff_h2645_extract_rbsp函数中存在如下代码:

    1. int ff_h2645_extract_rbsp(const uint8_t *src, int length,
    2. H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
    3. {
    4. //...
    5. #define STARTCODE_TEST \
    6. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
    7. if (src[i + 2] != 3 && src[i + 2] != 0) { \
    8. /* startcode, so we must be past the end */ \
    9. length = i; \
    10. } \
    11. break; \
    12. }
    13. //...
    14. for (i = 0; i + 1 < length; i += 2) {
    15. if (src[i])
    16. continue;
    17. if (i > 0 && src[i - 1] == 0)
    18. i--;
    19. STARTCODE_TEST;
    20. }
    21. //...
    22. }

    其中STARTCODE_TEST是宏定义。将宏展开,上述代码相当于:

    1. int ff_h2645_extract_rbsp(const uint8_t *src, int length,
    2. H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
    3. {
    4. //...
    5. for (i = 0; i + 1 < length; i += 2) {
    6. if (src[i])
    7. continue;
    8. if (i > 0 && src[i - 1] == 0)
    9. i--;
    10. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {
    11. if (src[i + 2] != 3 && src[i + 2] != 0) {
    12. /* startcode, so we must be past the end */
    13. length = i;
    14. }
    15. break;
    16. }
    17. }
    18. //...
    19. }

    上述代码中,首先会通过语句:

    1. for (i = 0; i + 1 < length; i += 2) {
    2. if (src[i])
    3. continue;
    4. if (i > 0 && src[i - 1] == 0)
    5. i--;
    6. //...
    7. }

    来判断H.264码流中是否存在ASCII 码为 0 (值为'\0')的的字符,如果存在则表明接下来的数据中可能会出现startcode(起始码)或防竞争字节。然后执行下面代码

    1. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {
    2. if (src[i + 2] != 3 && src[i + 2] != 0) {
    3. /* startcode, so we must be past the end */
    4. length = i;
    5. }
    6. break;
    7. }

    来判断是否是起始码,如果是起始码或防竞争字节就通过break;跳出循环。

    继续执行语句。满足下面条件,说明是防竞争字节:

    1. else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
    2. if (src[si + 2] == 3) { // escape
    3. //...
    4. }

    如果是防竞争字节,通过下面语句去掉防竞争字节:

    1. dst[di++] = 0;
    2. dst[di++] = 0;
    3. si       += 3;
    4. //...

    如果不满足条件if (src[si + 2] == 3),说明遇到下一个起始码,表示这个NALU结束了。执行else语句,跳转到“nsc”:

    1. if (src[si + 2] == 3) { // escape
    2. //...
    3. }
    4. else // next start code
    5. goto nsc;
    6. //...

    跳转到“nsc”后,给输出型参数赋值,并返回。

    1. nsc:
    2. memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
    3. nal->data = dst;
    4. nal->size = di;
    5. nal->raw_data = src;
    6. nal->raw_size = si;
    7. rbsp->rbsp_buffer_size += si;
    8. return si;

    四、通过修改ff_h2645_extract_rbsp函数降低FFmpeg转码时的cpu使用率

    由于ff_h2645_extract_rbsp函数在H.264/H.265的解码时被调用。所以理论上修改该函数(使用算法优化,用空间换时间等策略)可以降低FFmpeg转码时的cpu使用率。具体可以参考:Imagine Computing创新技术大赛赛道2参赛攻略 - 007gzs

  • 相关阅读:
    机器人过程自动化(RPA)入门 4. 数据处理
    CF515E Drazil and Park【思维+线段树】
    EF7创建模型入门篇
    VUE到底有什么好处?
    数据分析与处理 实验二 numpy的基本使用
    GEE19:基于Landsat8的常见的植被指数逐年获取
    【全网最简单】给朋友- 制作,微信公众号推送教程
    开发笔记 | 编译报错 | error: expected identifier or ‘(‘ before ‘return‘
    Linux安装MySQL
    Windows环境中Python应用服务自启动及其监控解决方法
  • 原文地址:https://blog.csdn.net/u014552102/article/details/139823572