首先来介绍下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单元的类型有如下几种:

  1. enum NalUnitType
  2. {
  3. NAL_UNIT_CODED_SLICE_TRAIL_N = 0,   // 0
  4. NAL_UNIT_CODED_SLICE_TRAIL_R,   // 1
  5. NAL_UNIT_CODED_SLICE_TSA_N,     // 2
  6. NAL_UNIT_CODED_SLICE_TLA,       // 3   // Current name in the spec: TSA_R
  7. NAL_UNIT_CODED_SLICE_STSA_N,    // 4
  8. NAL_UNIT_CODED_SLICE_STSA_R,    // 5
  9. NAL_UNIT_CODED_SLICE_RADL_N,    // 6
  10. NAL_UNIT_CODED_SLICE_DLP,       // 7 // Current name in the spec: RADL_R
  11. NAL_UNIT_CODED_SLICE_RASL_N,    // 8
  12. NAL_UNIT_CODED_SLICE_TFD,       // 9 // Current name in the spec: RASL_R
  13. NAL_UNIT_RESERVED_10,
  14. NAL_UNIT_RESERVED_11,
  15. NAL_UNIT_RESERVED_12,
  16. NAL_UNIT_RESERVED_13,
  17. NAL_UNIT_RESERVED_14,
  18. NAL_UNIT_RESERVED_15, NAL_UNIT_CODED_SLICE_BLA,       // 16   // Current name in the spec: BLA_W_LP
  19. NAL_UNIT_CODED_SLICE_BLA,       // 16   // Current name in the spec: BLA_W_LP
  20. NAL_UNIT_CODED_SLICE_BLANT,     // 17   // Current name in the spec: BLA_W_DLP
  21. NAL_UNIT_CODED_SLICE_BLA_N_LP,  // 18
  22. NAL_UNIT_CODED_SLICE_IDR,       // 19  // Current name in the spec: IDR_W_DLP
  23. NAL_UNIT_CODED_SLICE_IDR_N_LP,  // 20
  24. NAL_UNIT_CODED_SLICE_CRA,       // 21
  25. NAL_UNIT_RESERVED_22,
  26. NAL_UNIT_RESERVED_23,
  27. NAL_UNIT_RESERVED_24,
  28. NAL_UNIT_RESERVED_25,
  29. NAL_UNIT_RESERVED_26,
  30. NAL_UNIT_RESERVED_27,
  31. NAL_UNIT_RESERVED_28,
  32. NAL_UNIT_RESERVED_29,
  33. NAL_UNIT_RESERVED_30,
  34. NAL_UNIT_RESERVED_31,
  35. NAL_UNIT_VPS,                   // 32
  36. NAL_UNIT_SPS,                   // 33
  37. NAL_UNIT_PPS,                   // 34
  38. NAL_UNIT_ACCESS_UNIT_DELIMITER, // 35
  39. NAL_UNIT_EOS,                   // 36
  40. NAL_UNIT_EOB,                   // 37
  41. NAL_UNIT_FILLER_DATA,           // 38
  42. NAL_UNIT_SEI,                   // 39 Prefix SEI
  43. NAL_UNIT_SEI_SUFFIX,            // 40 Suffix SEI
  44. NAL_UNIT_RESERVED_41,
  45. NAL_UNIT_RESERVED_42,
  46. NAL_UNIT_RESERVED_43,
  47. NAL_UNIT_RESERVED_44,
  48. NAL_UNIT_RESERVED_45,
  49. NAL_UNIT_RESERVED_46,
  50. NAL_UNIT_RESERVED_47,
  51. NAL_UNIT_UNSPECIFIED_48,
  52. NAL_UNIT_UNSPECIFIED_49,
  53. NAL_UNIT_UNSPECIFIED_50,
  54. NAL_UNIT_UNSPECIFIED_51,
  55. NAL_UNIT_UNSPECIFIED_52,
  56. NAL_UNIT_UNSPECIFIED_53,
  57. NAL_UNIT_UNSPECIFIED_54,
  58. NAL_UNIT_UNSPECIFIED_55,
  59. NAL_UNIT_UNSPECIFIED_56,
  60. NAL_UNIT_UNSPECIFIED_57,
  61. NAL_UNIT_UNSPECIFIED_58,
  62. NAL_UNIT_UNSPECIFIED_59,
  63. NAL_UNIT_UNSPECIFIED_60,
  64. NAL_UNIT_UNSPECIFIED_61,
  65. NAL_UNIT_UNSPECIFIED_62,
  66. NAL_UNIT_UNSPECIFIED_63,
  67. NAL_UNIT_INVALID,
  68. };

下面接收下fu分组打包方式,fu分组包头格式如下:

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

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

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

通过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

最新文章

  1. 执行jar文件生成pdf报错,Unsupported URL &lt;file:///home
  2. MS SQLServer的关键词BETWEEN的一些注意事项
  3. Android studio 相关错误处理
  4. 最小生成树——kruskal算法
  5. Sqlserver 存储过程
  6. Java中唯一数的生成
  7. JSP 服务器响应
  8. 《JS高级程序设计》笔记 —— 解析查询字符串
  9. c#_DropdownList Panel Textbox 控件交互使用,有autopostback和没有的区别
  10. C 这些东西的内存管理
  11. 软件project(六)——需求分析
  12. HDU-1828-Picture(线段树)
  13. APIO2017 懵逼记
  14. Linux 用户与组的基本操作及文件权限位的设置方法
  15. 【题解】狼抓兔子—BZOJ1001。
  16. Supervisor使用教程
  17. 虚拟机模拟SSD用于Ceph测试
  18. layer.js 注册登录切换的问题
  19. HQL-Query接口
  20. Eclipse关联Github

热门文章

  1. 《剑指offer》反转链表
  2. CSS的flex布局和Grid布局
  3. JavaScript设计模式(biaoyansu)
  4. Linux Shell脚本编程-基础2
  5. python 命令行下的命令参数
  6. EXPIREAT
  7. 【Codeforces Round #423 (Div. 2) C】String Reconstruction
  8. 无比强大!Python抓取cssmoban网站的模版并下载
  9. 赵雅智_运用Bitmap和Canvas实现图片显示,缩小,旋转,水印
  10. Web前端之基础知识