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.

119 line
3.5KB

  1. // RTP Payload Format for VP9 Video draft-ietf-payload-vp9-03
  2. #include "rtp-packet.h"
  3. #include "rtp-profile.h"
  4. #include "rtp-payload-internal.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <errno.h>
  9. // Timestamp: The RTP timestamp indicates the time when the input frame was sampled, at a clock rate of 90 kHz
  10. #define KHz 90 // 90000Hz
  11. #define N_VP9_HEADER 1
  12. struct rtp_encode_vp9_t
  13. {
  14. struct rtp_packet_t pkt;
  15. struct rtp_payload_t handler;
  16. void* cbparam;
  17. int size;
  18. };
  19. static void* rtp_vp9_pack_create(int size, uint8_t pt, uint16_t seq, uint32_t ssrc, struct rtp_payload_t *handler, void* cbparam)
  20. {
  21. struct rtp_encode_vp9_t *packer;
  22. packer = (struct rtp_encode_vp9_t *)calloc(1, sizeof(*packer));
  23. if (!packer) return NULL;
  24. memcpy(&packer->handler, handler, sizeof(packer->handler));
  25. packer->cbparam = cbparam;
  26. packer->size = size;
  27. packer->pkt.rtp.v = RTP_VERSION;
  28. packer->pkt.rtp.pt = pt;
  29. packer->pkt.rtp.seq = seq;
  30. packer->pkt.rtp.ssrc = ssrc;
  31. return packer;
  32. }
  33. static void rtp_vp9_pack_destroy(void* pack)
  34. {
  35. struct rtp_encode_vp9_t *packer;
  36. packer = (struct rtp_encode_vp9_t *)pack;
  37. #if defined(_DEBUG) || defined(DEBUG)
  38. memset(packer, 0xCC, sizeof(*packer));
  39. #endif
  40. free(packer);
  41. }
  42. static void rtp_vp9_pack_get_info(void* pack, uint16_t* seq, uint32_t* timestamp)
  43. {
  44. struct rtp_encode_vp9_t *packer;
  45. packer = (struct rtp_encode_vp9_t *)pack;
  46. *seq = (uint16_t)packer->pkt.rtp.seq;
  47. *timestamp = packer->pkt.rtp.timestamp;
  48. }
  49. static int rtp_vp9_pack_input(void* pack, const void* data, int bytes, uint32_t timestamp)
  50. {
  51. int r, n;
  52. uint8_t *rtp;
  53. uint8_t vp9_payload_descriptor[1];
  54. const uint8_t *ptr;
  55. struct rtp_encode_vp9_t *packer;
  56. packer = (struct rtp_encode_vp9_t *)pack;
  57. packer->pkt.rtp.timestamp = timestamp;
  58. r = 0;
  59. ptr = (const uint8_t *)data;
  60. //In non-flexible mode (with the F bit below set to 0),
  61. for (vp9_payload_descriptor[0] = 0x08 /*Start of a layer frame*/; 0 == r && bytes > 0; ++packer->pkt.rtp.seq)
  62. {
  63. packer->pkt.payload = ptr;
  64. packer->pkt.payloadlen = (bytes + N_VP9_HEADER + RTP_FIXED_HEADER) < packer->size ? bytes : (packer->size - N_VP9_HEADER - RTP_FIXED_HEADER);
  65. ptr += packer->pkt.payloadlen;
  66. bytes -= packer->pkt.payloadlen;
  67. n = RTP_FIXED_HEADER + N_VP9_HEADER + packer->pkt.payloadlen;
  68. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  69. if (!rtp) return -ENOMEM;
  70. // Marker bit (M): MUST be set to 1 for the final packet of the highest
  71. // spatial layer frame (the final packet of the super frame), and 0
  72. // otherwise. Unless spatial scalability is in use for this super
  73. // frame, this will have the same value as the E bit described below.
  74. // Note this bit MUST be set to 1 for the target spatial layer frame
  75. // if a stream is being rewritten to remove higher spatial layers.
  76. packer->pkt.rtp.m = (0 == bytes) ? 1 : 0;
  77. vp9_payload_descriptor[0] |= (0 == bytes) ? 0x04 : 0; // End of a layer frame.
  78. n = rtp_packet_serialize_header(&packer->pkt, rtp, n);
  79. if (n != RTP_FIXED_HEADER)
  80. {
  81. assert(0);
  82. return -1;
  83. }
  84. memcpy(rtp + n, vp9_payload_descriptor, N_VP9_HEADER);
  85. memcpy(rtp + n + N_VP9_HEADER, packer->pkt.payload, packer->pkt.payloadlen);
  86. r = packer->handler.packet(packer->cbparam, rtp, n + N_VP9_HEADER + packer->pkt.payloadlen, packer->pkt.rtp.timestamp, 0);
  87. packer->handler.free(packer->cbparam, rtp);
  88. vp9_payload_descriptor[0] &= ~0x08;
  89. }
  90. return r;
  91. }
  92. struct rtp_payload_encode_t *rtp_vp9_encode()
  93. {
  94. static struct rtp_payload_encode_t encode = {
  95. rtp_vp9_pack_create,
  96. rtp_vp9_pack_destroy,
  97. rtp_vp9_pack_get_info,
  98. rtp_vp9_pack_input,
  99. };
  100. return &encode;
  101. }