H265(HEVC) nal 单元头介绍及rtp发送中的fu分组发送详解

首先来介绍下h265(HEVC)nal单元头,与h264的nal层相比,h265的nal unit header有两个字节构成,如下图所示:


从图中可以看出hHEVC的nal包结构与h264有明显的不同,hevc加入了nal所在的时间层的ID,取去除了nal_ref_idc,此信息合并到了naltype中,通常情况下F为0,layerid为0,TID为1。

        nal单元的类型有如下几种:

     

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.  enum NalUnitType  
  2. {  
  3.   NAL_UNIT_CODED_SLICE_TRAIL_N = 0,   // 0  
  4.   NAL_UNIT_CODED_SLICE_TRAIL_R,   // 1  
  5.     
  6.   NAL_UNIT_CODED_SLICE_TSA_N,     // 2  
  7.   NAL_UNIT_CODED_SLICE_TLA,       // 3   // Current name in the spec: TSA_R  
  8.     
  9.   NAL_UNIT_CODED_SLICE_STSA_N,    // 4  
  10.   NAL_UNIT_CODED_SLICE_STSA_R,    // 5  
  11.   
  12.   NAL_UNIT_CODED_SLICE_RADL_N,    // 6  
  13.   NAL_UNIT_CODED_SLICE_DLP,       // 7 // Current name in the spec: RADL_R  
  14.     
  15.   NAL_UNIT_CODED_SLICE_RASL_N,    // 8  
  16.   NAL_UNIT_CODED_SLICE_TFD,       // 9 // Current name in the spec: RASL_R  
  17.   
  18.   NAL_UNIT_RESERVED_10,  
  19.   NAL_UNIT_RESERVED_11,  
  20.   NAL_UNIT_RESERVED_12,  
  21.   NAL_UNIT_RESERVED_13,  
  22.   NAL_UNIT_RESERVED_14,  
  23.   NAL_UNIT_RESERVED_15, NAL_UNIT_CODED_SLICE_BLA,       // 16   // Current name in the spec: BLA_W_LP  
  24. NAL_UNIT_CODED_SLICE_BLA,       // 16   // Current name in the spec: BLA_W_LP  
  25.   NAL_UNIT_CODED_SLICE_BLANT,     // 17   // Current name in the spec: BLA_W_DLP  
  26.   NAL_UNIT_CODED_SLICE_BLA_N_LP,  // 18  
  27.   NAL_UNIT_CODED_SLICE_IDR,       // 19  // Current name in the spec: IDR_W_DLP  
  28.   NAL_UNIT_CODED_SLICE_IDR_N_LP,  // 20  
  29.   NAL_UNIT_CODED_SLICE_CRA,       // 21  
  30.   NAL_UNIT_RESERVED_22,  
  31.   NAL_UNIT_RESERVED_23,  
  32.   
  33.   NAL_UNIT_RESERVED_24,  
  34.   NAL_UNIT_RESERVED_25,  
  35.   NAL_UNIT_RESERVED_26,  
  36.   NAL_UNIT_RESERVED_27,  
  37.   NAL_UNIT_RESERVED_28,  
  38.   NAL_UNIT_RESERVED_29,  
  39.   NAL_UNIT_RESERVED_30,  
  40.   NAL_UNIT_RESERVED_31,  
  41.   
  42.   NAL_UNIT_VPS,                   // 32  
  43.   NAL_UNIT_SPS,                   // 33  
  44.   NAL_UNIT_PPS,                   // 34  
  45.   NAL_UNIT_ACCESS_UNIT_DELIMITER, // 35  
  46.   NAL_UNIT_EOS,                   // 36  
  47.   NAL_UNIT_EOB,                   // 37  
  48.   NAL_UNIT_FILLER_DATA,           // 38  
  49.   NAL_UNIT_SEI,                   // 39 Prefix SEI  
  50.   NAL_UNIT_SEI_SUFFIX,            // 40 Suffix SEI  
  51.   NAL_UNIT_RESERVED_41,  
  52.   NAL_UNIT_RESERVED_42,  
  53.   NAL_UNIT_RESERVED_43,  
  54.   NAL_UNIT_RESERVED_44,  
  55.   NAL_UNIT_RESERVED_45,  
  56.   NAL_UNIT_RESERVED_46,  
  57.   NAL_UNIT_RESERVED_47,  
  58.   NAL_UNIT_UNSPECIFIED_48,  
  59.   NAL_UNIT_UNSPECIFIED_49,  
  60.   NAL_UNIT_UNSPECIFIED_50,  
  61.   NAL_UNIT_UNSPECIFIED_51,  
  62.   NAL_UNIT_UNSPECIFIED_52,  
  63.   NAL_UNIT_UNSPECIFIED_53,  
  64.   NAL_UNIT_UNSPECIFIED_54,  
  65.   NAL_UNIT_UNSPECIFIED_55,  
  66.   NAL_UNIT_UNSPECIFIED_56,  
  67.   NAL_UNIT_UNSPECIFIED_57,  
  68.   NAL_UNIT_UNSPECIFIED_58,  
  69.   NAL_UNIT_UNSPECIFIED_59,  
  70.   NAL_UNIT_UNSPECIFIED_60,  
  71.   NAL_UNIT_UNSPECIFIED_61,  
  72.   NAL_UNIT_UNSPECIFIED_62,  
  73.   NAL_UNIT_UNSPECIFIED_63,  
  74.   NAL_UNIT_INVALID,  
  75. };  
下面接收下fu分组打包方式,fu分组包头格式如下:


fus包头包含了两个字节的payloadhdr,一个字节的fu header,fu header与h264一样,结构如下图,包含开始位(1b)、停止位(1b)、futype(6b)

paylodhdr两个自己的赋值,其实就是把hevc帧数据的nal unit header的naltype替换为49即可,下面是从ffmpeg源码中截取出来的fu打包方式代码片段:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. static void nal_send(AVFormatContext *ctx, const uint8_t *buf, int len, int last_packet_of_frame)  
  2. {  
  3.      RTPMuxContext *rtp_ctx = ctx->priv_data;  
  4.      int rtp_payload_size = rtp_ctx->max_payload_size - RTP_HEVC_HEADERS_SIZE;  
  5.      int nal_type = (buf[0] >> 1) & 0x3F;  
  6.   
  7.         /* send it as one single NAL unit? */  
  8.         if (len <= rtp_ctx->max_payload_size) //小于对定的最大值时,直接发送(最大值一般小于mtu)  
  9.      {  
  10.          /* use the original NAL unit buffer and transmit it as RTP payload */  
  11.         ff_rtp_send_data(ctx, buf, len, last_packet_of_frame);  
  12.           
  13.     }  
  14.     else //大于最大值时进行fu分组发送  
  15.     {  
  16.         /* 
  17.                     create the HEVC payload header and transmit the buffer as fragmentation units (FU) 
  18.                        
  19.                     0                   1 
  20.                     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 
  21.                     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  22.                     |F|   Type    |  LayerId  | TID | 
  23.                     +-------------+-----------------+ 
  24.                        
  25.                         F       = 0 
  26.                         Type    = 49 (fragmentation unit (FU)) 
  27.                         LayerId = 0 
  28.                         TID     = 1 
  29.         */  
  30.         rtp_ctx->buf[0] = 49 << 1;  
  31.         rtp_ctx->buf[1] = 1;  
  32.            //此处为paylaodhdr,规范赋值应该是替换hevc数据nal 的payloadhdr的type  
  33.             //rtp_ctx->buf[0] = (buf[0] &0x81) | (49<<1);  
  34.        //rtp_ctx->buf[1] = buf[1]  
  35.         /* 
  36.             create the FU header 
  37.                            
  38.             0 1 2 3 4 5 6 7 
  39.             +-+-+-+-+-+-+-+-+ 
  40.             |S|E|  FuType   | 
  41.             +---------------+ 
  42.                            
  43.                 S       = variable 
  44.                 E       = variable 
  45.                 FuType  = NAL unit type 
  46.         */  
  47.         rtp_ctx->buf[2] = nal_type;  
  48.         /* set the S bit: mark as start fragment */  
  49.         rtp_ctx->buf[2] |= 1 << 7;       
  50.         /* pass the original NAL header */  
  51.         //此处要注意,当是分组的第一报数据时,应该覆盖掉前两个字节的数据,h264要覆盖前一个字节的数据,即是第一包要去除hevc帧数据的paylaodhdr  
  52.         buf += 2;  
  53.         len -= 2;     
  54.         while (len > rtp_payload_size)   
  55.         {  
  56.             /* complete and send current RTP packet */  
  57.                 memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, rtp_payload_size);  
  58.             ff_rtp_send_data(ctx, rtp_ctx->buf, rtp_ctx->max_payload_size, 0);  
  59.               
  60.                 buf += rtp_payload_size;  
  61.             len -= rtp_payload_size;  
  62.               
  63.                 /* reset the S bit */  
  64.                 rtp_ctx->buf[2] &= ~(1 << 7);  
  65.               
  66.         }  
  67.       
  68.          /* set the E bit: mark as last fragment */  
  69.          rtp_ctx->buf[2] |= 1 << 6;  
  70.       
  71.          /* complete and send last RTP packet */  
  72.          memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, len);  
  73.         ff_rtp_send_data(ctx, rtp_ctx->buf, len + 2, last_packet_of_frame);  
  74.   
  75.     }  
  76. }  



通过rtp发送hevc视频数据,当hevc帧数据大于mtu时,应该进行fu分组发送,从上面代码流程就是对超过max_payload_size数据进行fu分组的流程,这个h264 fu-A很类似,很容易理解。


参考规范:

https://tools.ietf.org/html/draft-ietf-payload-rtp-h265-14

ffmpeg相关代码

https://www.ffmpeg.org/doxygen/2.5/rtpenc__hevc_8c_source.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
H.265HEVC)是一种视频编码标准,用于压缩和编码视频数据。RTP(Real-time Transport Protocol)是一种用于实时传输音视频数据的协议,常用于实时通信应用。 在使用RTP传输H.265视频数据时,初始化RTP的过程与传输其他类型的数据类似,但需要注意以下几点: 1. 设置RTP版本:RTP的版本字段表示RTP协议的版本号。当前常用的版本号为2。 2. 设置RTP负载类型:RTP负载类型字段用于标识传输的数据类型。对于H.265RTP负载类型通常设置为265。 3. 设置序列号:序列号字段表示RTP包的序列号,用于标识传输的顺序。每发送一个RTP包,序列号就会递增。 4. 设置时间戳:时间戳字段用于指示RTP数据的时间戳信息。对于H.265,可以根据需要设置帧的解码时间戳(decode timestamp)或显示时间戳(presentation timestamp)。 5. 设置同步源标识符:同步源标识符(SSRC)是一个32位的标识符,用于区分不同的RTP流。每个RTP流都应具有唯一的SSRC值。 这些初始化RTP的步骤可以通过编程语言或使用相应的库来实现。具体实现方式可能因编程语言或库的不同而有所差异。例如,使用Python语言和相应的库(如`python-rtplib`)可以通过设置RTP的字段来初始化RTP。 需要注意的是,RTP的具体格式和字段信息可以参考RFC 3550RTP标准)和H.265HEVC)标准的相关规定和说明,以确保正确地初始化和解析RTP

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值