You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.4KB

  1. /*
  2. * RTP packetizer for HEVC/H.265 payload format (draft version 6)
  3. * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avc.h"
  22. #include "avformat.h"
  23. #include "rtpenc.h"
  24. #define RTP_HEVC_HEADERS_SIZE 3
  25. static void nal_send(AVFormatContext *ctx, const uint8_t *buf, int len, int last_packet_of_frame)
  26. {
  27. RTPMuxContext *rtp_ctx = ctx->priv_data;
  28. int rtp_payload_size = rtp_ctx->max_payload_size - RTP_HEVC_HEADERS_SIZE;
  29. int nal_type = (buf[0] >> 1) & 0x3F;
  30. /* send it as one single NAL unit? */
  31. if (len <= rtp_ctx->max_payload_size) {
  32. /* use the original NAL unit buffer and transmit it as RTP payload */
  33. ff_rtp_send_data(ctx, buf, len, last_packet_of_frame);
  34. } else {
  35. /*
  36. create the HEVC payload header and transmit the buffer as fragmentation units (FU)
  37. 0 1
  38. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  39. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  40. |F| Type | LayerId | TID |
  41. +-------------+-----------------+
  42. F = 0
  43. Type = 49 (fragmentation unit (FU))
  44. LayerId = 0
  45. TID = 1
  46. */
  47. rtp_ctx->buf[0] = 49 << 1;
  48. rtp_ctx->buf[1] = 1;
  49. /*
  50. create the FU header
  51. 0 1 2 3 4 5 6 7
  52. +-+-+-+-+-+-+-+-+
  53. |S|E| FuType |
  54. +---------------+
  55. S = variable
  56. E = variable
  57. FuType = NAL unit type
  58. */
  59. rtp_ctx->buf[2] = nal_type;
  60. /* set the S bit: mark as start fragment */
  61. rtp_ctx->buf[2] |= 1 << 7;
  62. /* pass the original NAL header */
  63. buf += 2;
  64. len -= 2;
  65. while (len > rtp_payload_size) {
  66. /* complete and send current RTP packet */
  67. memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, rtp_payload_size);
  68. ff_rtp_send_data(ctx, rtp_ctx->buf, rtp_ctx->max_payload_size, 0);
  69. buf += rtp_payload_size;
  70. len -= rtp_payload_size;
  71. /* reset the S bit */
  72. rtp_ctx->buf[2] &= ~(1 << 7);
  73. }
  74. /* set the E bit: mark as last fragment */
  75. rtp_ctx->buf[2] |= 1 << 6;
  76. /* complete and send last RTP packet */
  77. memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, len);
  78. ff_rtp_send_data(ctx, rtp_ctx->buf, len + 2, last_packet_of_frame);
  79. }
  80. }
  81. void ff_rtp_send_hevc(AVFormatContext *ctx, const uint8_t *frame_buf, int frame_size)
  82. {
  83. const uint8_t *next_NAL_unit;
  84. const uint8_t *buf_ptr, *buf_end = frame_buf + frame_size;
  85. RTPMuxContext *rtp_ctx = ctx->priv_data;
  86. /* use the default 90 KHz time stamp */
  87. rtp_ctx->timestamp = rtp_ctx->cur_timestamp;
  88. if (rtp_ctx->nal_length_size)
  89. buf_ptr = ff_avc_mp4_find_startcode(frame_buf, buf_end, rtp_ctx->nal_length_size) ? frame_buf : buf_end;
  90. else
  91. buf_ptr = ff_avc_find_startcode(frame_buf, buf_end);
  92. /* find all NAL units and send them as separate packets */
  93. while (buf_ptr < buf_end) {
  94. if (rtp_ctx->nal_length_size) {
  95. next_NAL_unit = ff_avc_mp4_find_startcode(buf_ptr, buf_end, rtp_ctx->nal_length_size);
  96. if (!next_NAL_unit)
  97. next_NAL_unit = buf_end;
  98. buf_ptr += rtp_ctx->nal_length_size;
  99. } else {
  100. while (!*(buf_ptr++))
  101. ;
  102. next_NAL_unit = ff_avc_find_startcode(buf_ptr, buf_end);
  103. }
  104. /* send the next NAL unit */
  105. nal_send(ctx, buf_ptr, next_NAL_unit - buf_ptr, next_NAL_unit == buf_end);
  106. /* jump to the next NAL unit */
  107. buf_ptr = next_NAL_unit;
  108. }
  109. }