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.

171 lines
5.8KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/intreadwrite.h"
  22. #include "avc.h"
  23. #include "avformat.h"
  24. #include "rtpenc.h"
  25. #define RTP_HEVC_HEADERS_SIZE 3
  26. static void flush_buffered(AVFormatContext *s1, int last)
  27. {
  28. RTPMuxContext *s = s1->priv_data;
  29. if (s->buf_ptr != s->buf) {
  30. // If only sending one single NAL unit, skip the aggregation framing
  31. if (s->buffered_nals == 1)
  32. ff_rtp_send_data(s1, s->buf + 4, s->buf_ptr - s->buf - 4, last);
  33. else
  34. ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, last);
  35. }
  36. s->buf_ptr = s->buf;
  37. s->buffered_nals = 0;
  38. }
  39. static void nal_send(AVFormatContext *ctx, const uint8_t *buf, int len, int last_packet_of_frame)
  40. {
  41. RTPMuxContext *rtp_ctx = ctx->priv_data;
  42. int rtp_payload_size = rtp_ctx->max_payload_size - RTP_HEVC_HEADERS_SIZE;
  43. int nal_type = (buf[0] >> 1) & 0x3F;
  44. /* send it as one single NAL unit? */
  45. if (len <= rtp_ctx->max_payload_size) {
  46. int buffered_size = rtp_ctx->buf_ptr - rtp_ctx->buf;
  47. /* Flush buffered NAL units if the current unit doesn't fit */
  48. if (buffered_size + 2 + len > rtp_ctx->max_payload_size) {
  49. flush_buffered(ctx, 0);
  50. buffered_size = 0;
  51. }
  52. /* If the NAL unit fits including the framing, write the unit
  53. * to the buffer as an aggregate packet, otherwise flush and
  54. * send as single NAL. */
  55. if (buffered_size + 4 + len <= rtp_ctx->max_payload_size) {
  56. if (buffered_size == 0) {
  57. *rtp_ctx->buf_ptr++ = 48 << 1;
  58. *rtp_ctx->buf_ptr++ = 1;
  59. }
  60. AV_WB16(rtp_ctx->buf_ptr, len);
  61. rtp_ctx->buf_ptr += 2;
  62. memcpy(rtp_ctx->buf_ptr, buf, len);
  63. rtp_ctx->buf_ptr += len;
  64. rtp_ctx->buffered_nals++;
  65. } else {
  66. flush_buffered(ctx, 0);
  67. ff_rtp_send_data(ctx, buf, len, last_packet_of_frame);
  68. }
  69. } else {
  70. flush_buffered(ctx, 0);
  71. /*
  72. create the HEVC payload header and transmit the buffer as fragmentation units (FU)
  73. 0 1
  74. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  75. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  76. |F| Type | LayerId | TID |
  77. +-------------+-----------------+
  78. F = 0
  79. Type = 49 (fragmentation unit (FU))
  80. LayerId = 0
  81. TID = 1
  82. */
  83. rtp_ctx->buf[0] = 49 << 1;
  84. rtp_ctx->buf[1] = 1;
  85. /*
  86. create the FU header
  87. 0 1 2 3 4 5 6 7
  88. +-+-+-+-+-+-+-+-+
  89. |S|E| FuType |
  90. +---------------+
  91. S = variable
  92. E = variable
  93. FuType = NAL unit type
  94. */
  95. rtp_ctx->buf[2] = nal_type;
  96. /* set the S bit: mark as start fragment */
  97. rtp_ctx->buf[2] |= 1 << 7;
  98. /* pass the original NAL header */
  99. buf += 2;
  100. len -= 2;
  101. while (len > rtp_payload_size) {
  102. /* complete and send current RTP packet */
  103. memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, rtp_payload_size);
  104. ff_rtp_send_data(ctx, rtp_ctx->buf, rtp_ctx->max_payload_size, 0);
  105. buf += rtp_payload_size;
  106. len -= rtp_payload_size;
  107. /* reset the S bit */
  108. rtp_ctx->buf[2] &= ~(1 << 7);
  109. }
  110. /* set the E bit: mark as last fragment */
  111. rtp_ctx->buf[2] |= 1 << 6;
  112. /* complete and send last RTP packet */
  113. memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, len);
  114. ff_rtp_send_data(ctx, rtp_ctx->buf, len + 2, last_packet_of_frame);
  115. }
  116. }
  117. void ff_rtp_send_hevc(AVFormatContext *ctx, const uint8_t *frame_buf, int frame_size)
  118. {
  119. const uint8_t *next_NAL_unit;
  120. const uint8_t *buf_ptr, *buf_end = frame_buf + frame_size;
  121. RTPMuxContext *rtp_ctx = ctx->priv_data;
  122. /* use the default 90 KHz time stamp */
  123. rtp_ctx->timestamp = rtp_ctx->cur_timestamp;
  124. rtp_ctx->buf_ptr = rtp_ctx->buf;
  125. if (rtp_ctx->nal_length_size)
  126. buf_ptr = ff_avc_mp4_find_startcode(frame_buf, buf_end, rtp_ctx->nal_length_size) ? frame_buf : buf_end;
  127. else
  128. buf_ptr = ff_avc_find_startcode(frame_buf, buf_end);
  129. /* find all NAL units and send them as separate packets */
  130. while (buf_ptr < buf_end) {
  131. if (rtp_ctx->nal_length_size) {
  132. next_NAL_unit = ff_avc_mp4_find_startcode(buf_ptr, buf_end, rtp_ctx->nal_length_size);
  133. if (!next_NAL_unit)
  134. next_NAL_unit = buf_end;
  135. buf_ptr += rtp_ctx->nal_length_size;
  136. } else {
  137. while (!*(buf_ptr++))
  138. ;
  139. next_NAL_unit = ff_avc_find_startcode(buf_ptr, buf_end);
  140. }
  141. /* send the next NAL unit */
  142. nal_send(ctx, buf_ptr, next_NAL_unit - buf_ptr, next_NAL_unit == buf_end);
  143. /* jump to the next NAL unit */
  144. buf_ptr = next_NAL_unit;
  145. }
  146. flush_buffered(ctx, 1);
  147. }