No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

rtp-h265-pack.c 4.8KB

hace 6 meses
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // RFC7798 RTP Payload Format for High Efficiency Video Coding (HEVC)
  2. //
  3. // 4.1. RTP Header Usage (p20)
  4. // The RTP timestamp is set to the sampling timestamp of the content. A 90 kHz clock rate MUST be used.
  5. #include "rtp-packet.h"
  6. #include "rtp-payload-internal.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <errno.h>
  11. #define KHz 90 // 90000Hz
  12. #define FU_START 0x80
  13. #define FU_END 0x40
  14. #define N_FU_HEADER 3
  15. int rtp_h264_annexb_nalu(const void* h264, int bytes, int (*handler)(void* param, const uint8_t* nalu, int bytes, int last), void* param);
  16. struct rtp_encode_h265_t
  17. {
  18. struct rtp_packet_t pkt;
  19. struct rtp_payload_t handler;
  20. void* cbparam;
  21. int size;
  22. };
  23. static void* rtp_h265_pack_create(int size, uint8_t pt, uint16_t seq, uint32_t ssrc, struct rtp_payload_t *handler, void* param)
  24. {
  25. struct rtp_encode_h265_t *packer;
  26. packer = (struct rtp_encode_h265_t *)calloc(1, sizeof(*packer));
  27. if (!packer) return NULL;
  28. memcpy(&packer->handler, handler, sizeof(packer->handler));
  29. packer->cbparam = param;
  30. packer->size = size;
  31. packer->pkt.rtp.v = RTP_VERSION;
  32. packer->pkt.rtp.pt = pt;
  33. packer->pkt.rtp.seq = seq;
  34. packer->pkt.rtp.ssrc = ssrc;
  35. return packer;
  36. }
  37. static void rtp_h265_pack_destroy(void* pack)
  38. {
  39. struct rtp_encode_h265_t *packer;
  40. packer = (struct rtp_encode_h265_t *)pack;
  41. #if defined(_DEBUG) || defined(DEBUG)
  42. memset(packer, 0xCC, sizeof(*packer));
  43. #endif
  44. free(packer);
  45. }
  46. static void rtp_h265_pack_get_info(void* pack, uint16_t* seq, uint32_t* timestamp)
  47. {
  48. struct rtp_encode_h265_t *packer;
  49. packer = (struct rtp_encode_h265_t *)pack;
  50. *seq = (uint16_t)packer->pkt.rtp.seq;
  51. *timestamp = packer->pkt.rtp.timestamp;
  52. }
  53. static int rtp_h265_pack_nalu(struct rtp_encode_h265_t *packer, const uint8_t* nalu, int bytes, int mark)
  54. {
  55. int r, n;
  56. uint8_t *rtp;
  57. packer->pkt.payload = nalu;
  58. packer->pkt.payloadlen = bytes;
  59. n = RTP_FIXED_HEADER + packer->pkt.payloadlen;
  60. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  61. if (!rtp) return -ENOMEM;
  62. //packer->pkt.rtp.m = 1; // set marker flag
  63. packer->pkt.rtp.m = ((*nalu >> 1) & 0x3f) < 32 ? mark : 0; // VCL only
  64. n = rtp_packet_serialize(&packer->pkt, rtp, n);
  65. if (n != RTP_FIXED_HEADER + packer->pkt.payloadlen)
  66. {
  67. assert(0);
  68. return -1;
  69. }
  70. ++packer->pkt.rtp.seq;
  71. r = packer->handler.packet(packer->cbparam, rtp, n, packer->pkt.rtp.timestamp, 0);
  72. packer->handler.free(packer->cbparam, rtp);
  73. return r;
  74. }
  75. static int rtp_h265_pack_fu(struct rtp_encode_h265_t *packer, const uint8_t* ptr, int bytes, int mark)
  76. {
  77. int r, n;
  78. unsigned char *rtp;
  79. uint8_t fu_header = (ptr[0] >> 1) & 0x3F;
  80. r = 0;
  81. ptr += 2; // skip NAL Unit Type byte
  82. bytes -= 2;
  83. assert(bytes > 0);
  84. // FU-A start
  85. for (fu_header |= FU_START; 0 == r && bytes > 0; ++packer->pkt.rtp.seq)
  86. {
  87. if (bytes + RTP_FIXED_HEADER <= packer->size - N_FU_HEADER)
  88. {
  89. assert(0 == (fu_header & FU_START));
  90. fu_header = FU_END | (fu_header & 0x3F); // FU end
  91. packer->pkt.payloadlen = bytes;
  92. }
  93. else
  94. {
  95. packer->pkt.payloadlen = packer->size - RTP_FIXED_HEADER - N_FU_HEADER;
  96. }
  97. packer->pkt.payload = ptr;
  98. n = RTP_FIXED_HEADER + N_FU_HEADER + packer->pkt.payloadlen;
  99. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  100. if (!rtp) return -ENOMEM;
  101. packer->pkt.rtp.m = (FU_END & fu_header) ? mark : 0; // set marker flag
  102. n = rtp_packet_serialize_header(&packer->pkt, rtp, n);
  103. if (n != RTP_FIXED_HEADER)
  104. {
  105. assert(0);
  106. return -1;
  107. }
  108. /*header + fu_header*/
  109. rtp[n + 0] = 49 << 1;
  110. rtp[n + 1] = 1;
  111. rtp[n + 2] = fu_header;
  112. memcpy(rtp + n + N_FU_HEADER, packer->pkt.payload, packer->pkt.payloadlen);
  113. r = packer->handler.packet(packer->cbparam, rtp, n + N_FU_HEADER + packer->pkt.payloadlen, packer->pkt.rtp.timestamp, 0);
  114. packer->handler.free(packer->cbparam, rtp);
  115. bytes -= packer->pkt.payloadlen;
  116. ptr += packer->pkt.payloadlen;
  117. fu_header &= 0x3F; // clear flags
  118. }
  119. return r;
  120. }
  121. static int rtp_h265_pack_handler(void* pack, const uint8_t* nalu, int bytes, int last)
  122. {
  123. struct rtp_encode_h265_t* packer;
  124. packer = (struct rtp_encode_h265_t*)pack;
  125. if (bytes + RTP_FIXED_HEADER <= packer->size)
  126. {
  127. // single NAl unit packet
  128. return rtp_h265_pack_nalu(packer, nalu, bytes, last ? 1 : 0);
  129. }
  130. else
  131. {
  132. return rtp_h265_pack_fu(packer, nalu, bytes, last ? 1 : 0);
  133. }
  134. }
  135. static int rtp_h265_pack_input(void* pack, const void* h265, int bytes, uint32_t timestamp)
  136. {
  137. struct rtp_encode_h265_t* packer;
  138. packer = (struct rtp_encode_h265_t*)pack;
  139. // assert(packer->pkt.rtp.timestamp != timestamp || !packer->pkt.payload /*first packet*/);
  140. packer->pkt.rtp.timestamp = timestamp; //(uint32_t)time * KHz; // ms -> 90KHZ
  141. return rtp_h264_annexb_nalu(h265, bytes, rtp_h265_pack_handler, packer);
  142. }
  143. struct rtp_payload_encode_t *rtp_h265_encode()
  144. {
  145. static struct rtp_payload_encode_t packer = {
  146. rtp_h265_pack_create,
  147. rtp_h265_pack_destroy,
  148. rtp_h265_pack_get_info,
  149. rtp_h265_pack_input,
  150. };
  151. return &packer;
  152. }