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.

340 lines
9.5KB

  1. // https://aomediacodec.github.io/av1-rtp-spec/
  2. // 7.1. Media Type Definition: video/av1
  3. #include "rtp-packet.h"
  4. #include "rtp-profile.h"
  5. #include "rtp-payload-internal.h"
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <errno.h>
  11. // Timestamp: The RTP timestamp indicates the time when the input frame was sampled, at a clock rate of 90 kHz
  12. #define KHz 90 // 90000Hz
  13. #define N_AV1_HEADER 1
  14. #define OBU_SEQUENCE_HEADER 1
  15. #define OBU_TEMPORAL_DELIMITER 2
  16. #define OBU_FRAME_HEADER 3
  17. #define OBU_TILE_GROUP 4
  18. #define OBU_METADATA 5
  19. #define OBU_FRAME 6
  20. #define OBU_REDUNDANT_FRAME_HEADER 7
  21. #define OBU_TILE_LIST 8
  22. #define AV1_AGGREGATION_HEADER_Z 0x80 // set to 1 if the first OBU element is an OBU fragment that is a continuation of an OBU fragment from the previous packet, 0 otherwise.
  23. #define AV1_AGGREGATION_HEADER_Y 0x40 // set to 1 if the last OBU element is an OBU fragment that will continue in the next packet, 0 otherwise.
  24. #define AV1_AGGREGATION_HEADER_N 0x08 // set to 1 if the packet is the first packet of a coded video sequence, 0 otherwise. Note: if N equals 1 then Z must equal 0.
  25. struct rtp_encode_av1_t
  26. {
  27. struct rtp_packet_t pkt;
  28. struct rtp_payload_t handler;
  29. void* cbparam;
  30. int size;
  31. uint8_t* ptr;
  32. int offset;
  33. uint8_t aggregation;
  34. };
  35. static inline const uint8_t* leb128(const uint8_t* data, size_t bytes, uint64_t* size)
  36. {
  37. size_t i;
  38. for (*size = i = 0; i * 7 < 64 && i < bytes;)
  39. {
  40. *size |= ((uint64_t)(data[i] & 0x7F)) << (i * 7);
  41. if (0 == (data[i++] & 0x80))
  42. break;
  43. }
  44. return data + i;
  45. }
  46. static inline uint8_t* leb128_write(int64_t size, uint8_t* data, size_t bytes)
  47. {
  48. size_t i;
  49. for (i = 0; i * 7 < 64 && i < bytes;)
  50. {
  51. data[i] = (uint8_t)(size & 0x7F);
  52. size >>= 7;
  53. data[i++] |= size > 0 ? 0x80 : 0;
  54. if (0 == size)
  55. break;
  56. }
  57. return data + i;
  58. }
  59. static void* rtp_av1_pack_create(int size, uint8_t pt, uint16_t seq, uint32_t ssrc, struct rtp_payload_t *handler, void* cbparam)
  60. {
  61. struct rtp_encode_av1_t *packer;
  62. packer = (struct rtp_encode_av1_t *)calloc(1, sizeof(*packer));
  63. if (!packer) return NULL;
  64. memcpy(&packer->handler, handler, sizeof(packer->handler));
  65. packer->cbparam = cbparam;
  66. packer->size = size;
  67. packer->pkt.rtp.v = RTP_VERSION;
  68. packer->pkt.rtp.pt = pt;
  69. packer->pkt.rtp.seq = seq;
  70. packer->pkt.rtp.ssrc = ssrc;
  71. return packer;
  72. }
  73. static void rtp_av1_pack_destroy(void* pack)
  74. {
  75. struct rtp_encode_av1_t *packer;
  76. packer = (struct rtp_encode_av1_t *)pack;
  77. #if defined(_DEBUG) || defined(DEBUG)
  78. memset(packer, 0xCC, sizeof(*packer));
  79. #endif
  80. free(packer);
  81. }
  82. static void rtp_av1_pack_get_info(void* pack, uint16_t* seq, uint32_t* timestamp)
  83. {
  84. struct rtp_encode_av1_t *packer;
  85. packer = (struct rtp_encode_av1_t *)pack;
  86. *seq = (uint16_t)packer->pkt.rtp.seq;
  87. *timestamp = packer->pkt.rtp.timestamp;
  88. }
  89. static int rtp_av1_pack_flush(struct rtp_encode_av1_t *packer, uint8_t aggregation)
  90. {
  91. int r, n;
  92. if (!packer->ptr || packer->offset <= RTP_FIXED_HEADER)
  93. return 0; // nothing to send
  94. packer->ptr[RTP_FIXED_HEADER] = aggregation;
  95. packer->pkt.payloadlen = packer->offset - RTP_FIXED_HEADER;
  96. n = rtp_packet_serialize_header(&packer->pkt, packer->ptr, packer->size);
  97. if (n != RTP_FIXED_HEADER)
  98. {
  99. assert(0);
  100. return -1;
  101. }
  102. ++packer->pkt.rtp.seq;
  103. packer->pkt.rtp.m = 0; // clear marker bit
  104. packer->aggregation &= ~(AV1_AGGREGATION_HEADER_N | AV1_AGGREGATION_HEADER_Z);
  105. r = packer->handler.packet(packer->cbparam, packer->ptr, n + packer->pkt.payloadlen, packer->pkt.rtp.timestamp, 0);
  106. packer->handler.free(packer->cbparam, packer->ptr);
  107. packer->offset = 0;
  108. packer->ptr = NULL;
  109. return r;
  110. }
  111. static int rtp_av1_pack_obu(struct rtp_encode_av1_t *packer, const uint8_t* obu, int64_t bytes)
  112. {
  113. int r;
  114. int64_t n;
  115. uint8_t* ptr, *end;
  116. while (bytes > 0)
  117. {
  118. if (NULL == packer->ptr)
  119. {
  120. packer->ptr = (uint8_t*)packer->handler.alloc(packer->cbparam, packer->size);
  121. if (!packer->ptr)
  122. return -ENOMEM;
  123. packer->offset = RTP_FIXED_HEADER + 1; // RTP Header + AV1 aggregation header
  124. }
  125. ptr = packer->ptr + packer->offset;
  126. end = packer->ptr + packer->size;
  127. // OBU element size
  128. assert(packer->size < 0x3FFF); // 14bits
  129. if (ptr + bytes + ((bytes > 0x7F) ? 2 : 1) > end)
  130. n = end - ptr - 2;
  131. else
  132. n = bytes;
  133. ptr = leb128_write(n, ptr, end - ptr);
  134. memcpy(ptr, obu, (size_t)n);
  135. ptr += n;
  136. obu += n;
  137. bytes -= n;
  138. packer->offset = (int)(ptr - packer->ptr);
  139. if (packer->size - packer->offset < 8)
  140. {
  141. r = rtp_av1_pack_flush(packer, packer->aggregation | (bytes > 0 ? AV1_AGGREGATION_HEADER_Y : 0));
  142. if (0 != r) return r;
  143. }
  144. if (bytes > 0)
  145. packer->aggregation |= AV1_AGGREGATION_HEADER_Z;
  146. }
  147. return 0;
  148. }
  149. /// https://aomediacodec.github.io/av1-spec/av1-spec.pd
  150. /// Annex B: Length delimited bitstream format
  151. /// @param[in] data temporal_unit
  152. /// @param[in] bytes temporal_unit_sizetemporal_unit_size
  153. static int rtp_av1_pack_input_annexb(void* pack, const void* data, int bytes, uint32_t timestamp)
  154. {
  155. int r;
  156. // uint8_t obu_has_size_field;
  157. uint8_t obu_extension_flag;
  158. uint8_t temporal_id, temporal_id0;
  159. uint8_t spatial_id, spatial_id0;
  160. uint8_t obu_type;
  161. uint64_t obu_size, frame_size;
  162. const uint8_t *ptr, *end, *frame_end, *obu_end;
  163. struct rtp_encode_av1_t *packer;
  164. packer = (struct rtp_encode_av1_t *)pack;
  165. packer->pkt.rtp.timestamp = timestamp;
  166. packer->pkt.rtp.m = 0;
  167. packer->ptr = NULL; // TODO: ptr memory leak
  168. temporal_id0 = spatial_id0 = 0;
  169. ptr = (const uint8_t *)data;
  170. end = ptr + bytes;
  171. for (packer->aggregation = AV1_AGGREGATION_HEADER_N; ptr < end; ptr = frame_end)
  172. {
  173. ptr = leb128(ptr, end - ptr, &frame_size);
  174. frame_end = ptr + frame_size;
  175. if (frame_end > end)
  176. {
  177. assert(0);
  178. return -1;
  179. }
  180. for (; ptr < frame_end; ptr = obu_end)
  181. {
  182. ptr = leb128(ptr, bytes, &obu_size);
  183. obu_end = ptr + obu_size;
  184. if (obu_end > frame_end)
  185. {
  186. assert(0);
  187. return -1;
  188. }
  189. obu_type = (*ptr >> 3) & 0x0F;
  190. obu_extension_flag = *ptr & 0x04;
  191. //obu_has_size_field = *ptr & 0x02;
  192. if (obu_extension_flag)
  193. {
  194. temporal_id = (ptr[1] >> 5) & 0x07;
  195. spatial_id = (ptr[1] >> 3) & 0x03;
  196. // If more than one OBU contained in an RTP packet has an OBU extension header
  197. // then the values of the temporal_id and spatial_id must be the same in all such
  198. // OBUs in the RTP packet.
  199. if (temporal_id != temporal_id0 || spatial_id != spatial_id0)
  200. {
  201. r = rtp_av1_pack_flush(packer, packer->aggregation);
  202. if (0 != r) return r;
  203. temporal_id0 = temporal_id;
  204. spatial_id0 = spatial_id;
  205. }
  206. }
  207. // 5. Packetization rules
  208. // The temporal delimiter OBU, if present, SHOULD be removed
  209. // when transmitting, and MUST be ignored by receivers.
  210. if (OBU_TEMPORAL_DELIMITER == obu_type)
  211. continue;
  212. if (0 != rtp_av1_pack_obu(packer, ptr, obu_size))
  213. return -ENOMEM;
  214. }
  215. }
  216. // The RTP header Marker bit MUST be set equal to 0 if the packet is not the last
  217. // packet of the temporal unit, it SHOULD be set equal to 1 otherwise.
  218. // Note: It is possible for a receiver to receive the last packet of a temporal unit
  219. // without the marker bit being set equal to 1, and a receiver should be able to handle
  220. // this case. The last packet of a temporal unit is also indicated by the next packet,
  221. // in RTP sequence number order, having an incremented timestamp.
  222. packer->pkt.rtp.m = 1;
  223. return rtp_av1_pack_flush(packer, packer->aggregation);
  224. }
  225. /// http://aomedia.org/av1/specification/syntax/#general-obu-syntax
  226. /// Low overhead bitstream format
  227. static int rtp_av1_pack_input_obu(void* pack, const void* data, int bytes, uint32_t timestamp)
  228. {
  229. int r;
  230. size_t i;
  231. size_t offset;
  232. uint64_t len;
  233. uint8_t obu_type;
  234. const uint8_t* ptr, *raw;
  235. struct rtp_encode_av1_t* packer;
  236. packer = (struct rtp_encode_av1_t*)pack;
  237. packer->pkt.rtp.timestamp = timestamp;
  238. packer->pkt.rtp.m = 0;
  239. packer->ptr = NULL; // TODO: ptr memory leak
  240. packer->aggregation = 0;
  241. raw = (const uint8_t*)data;
  242. for (i = r = 0; i < bytes && 0 == r; i += (size_t)len)
  243. {
  244. // http://aomedia.org/av1/specification/syntax/#obu-header-syntax
  245. obu_type = (raw[i] >> 3) & 0x0F;
  246. if (raw[i] & 0x04) // obu_extension_flag
  247. {
  248. // http://aomedia.org/av1/specification/syntax/#obu-extension-header-syntax
  249. // temporal_id = (obu[1] >> 5) & 0x07;
  250. // spatial_id = (obu[1] >> 3) & 0x03;
  251. offset = 2;
  252. }
  253. else
  254. {
  255. offset = 1;
  256. }
  257. if (raw[i] & 0x02) // obu_has_size_field
  258. {
  259. ptr = leb128(raw + i + offset, (int)(bytes - i - offset), &len);
  260. if (ptr + len > raw + bytes)
  261. return -1;
  262. len += ptr - raw - i;
  263. }
  264. else
  265. {
  266. len = bytes - i;
  267. }
  268. // 5. Packetization rules
  269. // The temporal delimiter OBU, if present, SHOULD be removed
  270. // when transmitting, and MUST be ignored by receivers.
  271. if (OBU_TEMPORAL_DELIMITER == obu_type)
  272. continue;
  273. packer->aggregation |= OBU_SEQUENCE_HEADER == obu_type ? AV1_AGGREGATION_HEADER_N : 0;
  274. r = rtp_av1_pack_obu(packer, raw + i, (size_t)len);
  275. }
  276. // The RTP header Marker bit MUST be set equal to 0 if the packet is not the last
  277. // packet of the temporal unit, it SHOULD be set equal to 1 otherwise.
  278. // Note: It is possible for a receiver to receive the last packet of a temporal unit
  279. // without the marker bit being set equal to 1, and a receiver should be able to handle
  280. // this case. The last packet of a temporal unit is also indicated by the next packet,
  281. // in RTP sequence number order, having an incremented timestamp.
  282. packer->pkt.rtp.m = 1;
  283. return rtp_av1_pack_flush(packer, packer->aggregation);
  284. }
  285. struct rtp_payload_encode_t *rtp_av1_encode()
  286. {
  287. static struct rtp_payload_encode_t encode = {
  288. rtp_av1_pack_create,
  289. rtp_av1_pack_destroy,
  290. rtp_av1_pack_get_info,
  291. rtp_av1_pack_input_obu,
  292. };
  293. return &encode;
  294. }