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.

351 lines
12KB

  1. /*
  2. * RTP parser 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/avstring.h"
  22. #include "libavutil/base64.h"
  23. #include "avformat.h"
  24. #include "rtpdec.h"
  25. #include "rtpdec_formats.h"
  26. #define RTP_HEVC_PAYLOAD_HEADER_SIZE 2
  27. #define RTP_HEVC_FU_HEADER_SIZE 1
  28. #define RTP_HEVC_DONL_FIELD_SIZE 2
  29. #define RTP_HEVC_DOND_FIELD_SIZE 1
  30. #define HEVC_SPECIFIED_NAL_UNIT_TYPES 48
  31. /* SDP out-of-band signaling data */
  32. struct PayloadContext {
  33. int using_donl_field;
  34. int profile_id;
  35. uint8_t *sps, *pps, *vps, *sei;
  36. int sps_size, pps_size, vps_size, sei_size;
  37. };
  38. static const uint8_t start_sequence[] = { 0x00, 0x00, 0x00, 0x01 };
  39. static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
  40. AVStream *stream,
  41. PayloadContext *hevc_data,
  42. const char *attr, const char *value)
  43. {
  44. /* profile-space: 0-3 */
  45. /* profile-id: 0-31 */
  46. if (!strcmp(attr, "profile-id")) {
  47. hevc_data->profile_id = atoi(value);
  48. av_log(s, AV_LOG_TRACE, "SDP: found profile-id: %d\n", hevc_data->profile_id);
  49. }
  50. /* tier-flag: 0-1 */
  51. /* level-id: 0-255 */
  52. /* interop-constraints: [base16] */
  53. /* profile-compatibility-indicator: [base16] */
  54. /* sprop-sub-layer-id: 0-6, defines highest possible value for TID, default: 6 */
  55. /* recv-sub-layer-id: 0-6 */
  56. /* max-recv-level-id: 0-255 */
  57. /* tx-mode: MSM,SSM */
  58. /* sprop-vps: [base64] */
  59. /* sprop-sps: [base64] */
  60. /* sprop-pps: [base64] */
  61. /* sprop-sei: [base64] */
  62. if (!strcmp(attr, "sprop-vps") || !strcmp(attr, "sprop-sps") ||
  63. !strcmp(attr, "sprop-pps") || !strcmp(attr, "sprop-sei")) {
  64. uint8_t **data_ptr = NULL;
  65. int *size_ptr = NULL;
  66. if (!strcmp(attr, "sprop-vps")) {
  67. data_ptr = &hevc_data->vps;
  68. size_ptr = &hevc_data->vps_size;
  69. } else if (!strcmp(attr, "sprop-sps")) {
  70. data_ptr = &hevc_data->sps;
  71. size_ptr = &hevc_data->sps_size;
  72. } else if (!strcmp(attr, "sprop-pps")) {
  73. data_ptr = &hevc_data->pps;
  74. size_ptr = &hevc_data->pps_size;
  75. } else if (!strcmp(attr, "sprop-sei")) {
  76. data_ptr = &hevc_data->sei;
  77. size_ptr = &hevc_data->sei_size;
  78. }
  79. ff_h264_parse_sprop_parameter_sets(s, data_ptr,
  80. size_ptr, value);
  81. }
  82. /* max-lsr, max-lps, max-cpb, max-dpb, max-br, max-tr, max-tc */
  83. /* max-fps */
  84. /* sprop-max-don-diff: 0-32767
  85. When the RTP stream depends on one or more other RTP
  86. streams (in this case tx-mode MUST be equal to "MSM" and
  87. MSM is in use), this parameter MUST be present and the
  88. value MUST be greater than 0.
  89. */
  90. if (!strcmp(attr, "sprop-max-don-diff")) {
  91. if (atoi(value) > 0)
  92. hevc_data->using_donl_field = 1;
  93. av_log(s, AV_LOG_TRACE, "Found sprop-max-don-diff in SDP, DON field usage is: %d\n",
  94. hevc_data->using_donl_field);
  95. }
  96. /* sprop-depack-buf-nalus: 0-32767 */
  97. if (!strcmp(attr, "sprop-depack-buf-nalus")) {
  98. if (atoi(value) > 0)
  99. hevc_data->using_donl_field = 1;
  100. av_log(s, AV_LOG_TRACE, "Found sprop-depack-buf-nalus in SDP, DON field usage is: %d\n",
  101. hevc_data->using_donl_field);
  102. }
  103. /* sprop-depack-buf-bytes: 0-4294967295 */
  104. /* depack-buf-cap */
  105. /* sprop-segmentation-id: 0-3 */
  106. /* sprop-spatial-segmentation-idc: [base16] */
  107. /* dec-parallel-ca: */
  108. /* include-dph */
  109. return 0;
  110. }
  111. static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index,
  112. PayloadContext *hevc_data, const char *line)
  113. {
  114. AVStream *current_stream;
  115. AVCodecParameters *par;
  116. const char *sdp_line_ptr = line;
  117. if (st_index < 0)
  118. return 0;
  119. current_stream = ctx->streams[st_index];
  120. par = current_stream->codecpar;
  121. if (av_strstart(sdp_line_ptr, "framesize:", &sdp_line_ptr)) {
  122. ff_h264_parse_framesize(par, sdp_line_ptr);
  123. } else if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
  124. int ret = ff_parse_fmtp(ctx, current_stream, hevc_data, sdp_line_ptr,
  125. hevc_sdp_parse_fmtp_config);
  126. if (hevc_data->vps_size || hevc_data->sps_size ||
  127. hevc_data->pps_size || hevc_data->sei_size) {
  128. av_freep(&par->extradata);
  129. par->extradata_size = hevc_data->vps_size + hevc_data->sps_size +
  130. hevc_data->pps_size + hevc_data->sei_size;
  131. par->extradata = av_malloc(par->extradata_size +
  132. AV_INPUT_BUFFER_PADDING_SIZE);
  133. if (!par->extradata) {
  134. ret = AVERROR(ENOMEM);
  135. par->extradata_size = 0;
  136. } else {
  137. int pos = 0;
  138. memcpy(par->extradata + pos, hevc_data->vps, hevc_data->vps_size);
  139. pos += hevc_data->vps_size;
  140. memcpy(par->extradata + pos, hevc_data->sps, hevc_data->sps_size);
  141. pos += hevc_data->sps_size;
  142. memcpy(par->extradata + pos, hevc_data->pps, hevc_data->pps_size);
  143. pos += hevc_data->pps_size;
  144. memcpy(par->extradata + pos, hevc_data->sei, hevc_data->sei_size);
  145. pos += hevc_data->sei_size;
  146. memset(par->extradata + pos, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  147. }
  148. av_freep(&hevc_data->vps);
  149. av_freep(&hevc_data->sps);
  150. av_freep(&hevc_data->pps);
  151. av_freep(&hevc_data->sei);
  152. hevc_data->vps_size = 0;
  153. hevc_data->sps_size = 0;
  154. hevc_data->pps_size = 0;
  155. hevc_data->sei_size = 0;
  156. }
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx,
  162. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  163. const uint8_t *buf, int len, uint16_t seq,
  164. int flags)
  165. {
  166. const uint8_t *rtp_pl = buf;
  167. int tid, lid, nal_type;
  168. int first_fragment, last_fragment, fu_type;
  169. uint8_t new_nal_header[2];
  170. int res = 0;
  171. /* sanity check for size of input packet: 1 byte payload at least */
  172. if (len < RTP_HEVC_PAYLOAD_HEADER_SIZE + 1) {
  173. av_log(ctx, AV_LOG_ERROR, "Too short RTP/HEVC packet, got %d bytes\n", len);
  174. return AVERROR_INVALIDDATA;
  175. }
  176. /*
  177. * decode the HEVC payload header according to section 4 of draft version 6:
  178. *
  179. * 0 1
  180. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  181. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  182. * |F| Type | LayerId | TID |
  183. * +-------------+-----------------+
  184. *
  185. * Forbidden zero (F): 1 bit
  186. * NAL unit type (Type): 6 bits
  187. * NUH layer ID (LayerId): 6 bits
  188. * NUH temporal ID plus 1 (TID): 3 bits
  189. */
  190. nal_type = (buf[0] >> 1) & 0x3f;
  191. lid = ((buf[0] << 5) & 0x20) | ((buf[1] >> 3) & 0x1f);
  192. tid = buf[1] & 0x07;
  193. /* sanity check for correct layer ID */
  194. if (lid) {
  195. /* future scalable or 3D video coding extensions */
  196. avpriv_report_missing_feature(ctx, "Multi-layer HEVC coding");
  197. return AVERROR_PATCHWELCOME;
  198. }
  199. /* sanity check for correct temporal ID */
  200. if (!tid) {
  201. av_log(ctx, AV_LOG_ERROR, "Illegal temporal ID in RTP/HEVC packet\n");
  202. return AVERROR_INVALIDDATA;
  203. }
  204. /* sanity check for correct NAL unit type */
  205. if (nal_type > 50) {
  206. av_log(ctx, AV_LOG_ERROR, "Unsupported (HEVC) NAL type (%d)\n", nal_type);
  207. return AVERROR_INVALIDDATA;
  208. }
  209. switch (nal_type) {
  210. /* video parameter set (VPS) */
  211. case 32:
  212. /* sequence parameter set (SPS) */
  213. case 33:
  214. /* picture parameter set (PPS) */
  215. case 34:
  216. /* supplemental enhancement information (SEI) */
  217. case 39:
  218. /* single NAL unit packet */
  219. default:
  220. /* create A/V packet */
  221. if ((res = av_new_packet(pkt, sizeof(start_sequence) + len)) < 0)
  222. return res;
  223. /* A/V packet: copy start sequence */
  224. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  225. /* A/V packet: copy NAL unit data */
  226. memcpy(pkt->data + sizeof(start_sequence), buf, len);
  227. break;
  228. /* aggregated packet (AP) - with two or more NAL units */
  229. case 48:
  230. /* pass the HEVC payload header */
  231. buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
  232. len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
  233. /* pass the HEVC DONL field */
  234. if (rtp_hevc_ctx->using_donl_field) {
  235. buf += RTP_HEVC_DONL_FIELD_SIZE;
  236. len -= RTP_HEVC_DONL_FIELD_SIZE;
  237. }
  238. res = ff_h264_handle_aggregated_packet(ctx, pkt, buf, len,
  239. rtp_hevc_ctx->using_donl_field ?
  240. RTP_HEVC_DOND_FIELD_SIZE : 0,
  241. NULL, 0);
  242. if (res < 0)
  243. return res;
  244. break;
  245. /* fragmentation unit (FU) */
  246. case 49:
  247. /* pass the HEVC payload header */
  248. buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
  249. len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
  250. /*
  251. * decode the FU header
  252. *
  253. * 0 1 2 3 4 5 6 7
  254. * +-+-+-+-+-+-+-+-+
  255. * |S|E| FuType |
  256. * +---------------+
  257. *
  258. * Start fragment (S): 1 bit
  259. * End fragment (E): 1 bit
  260. * FuType: 6 bits
  261. */
  262. first_fragment = buf[0] & 0x80;
  263. last_fragment = buf[0] & 0x40;
  264. fu_type = buf[0] & 0x3f;
  265. /* pass the HEVC FU header */
  266. buf += RTP_HEVC_FU_HEADER_SIZE;
  267. len -= RTP_HEVC_FU_HEADER_SIZE;
  268. /* pass the HEVC DONL field */
  269. if (rtp_hevc_ctx->using_donl_field) {
  270. buf += RTP_HEVC_DONL_FIELD_SIZE;
  271. len -= RTP_HEVC_DONL_FIELD_SIZE;
  272. }
  273. av_log(ctx, AV_LOG_TRACE, " FU type %d with %d bytes\n", fu_type, len);
  274. if (len <= 0) {
  275. /* sanity check for size of input packet: 1 byte payload at least */
  276. av_log(ctx, AV_LOG_ERROR,
  277. "Too short RTP/HEVC packet, got %d bytes of NAL unit type %d\n",
  278. len, nal_type);
  279. return AVERROR_INVALIDDATA;
  280. }
  281. if (first_fragment && last_fragment) {
  282. av_log(ctx, AV_LOG_ERROR, "Illegal combination of S and E bit in RTP/HEVC packet\n");
  283. return AVERROR_INVALIDDATA;
  284. }
  285. new_nal_header[0] = (rtp_pl[0] & 0x81) | (fu_type << 1);
  286. new_nal_header[1] = rtp_pl[1];
  287. res = ff_h264_handle_frag_packet(pkt, buf, len, first_fragment,
  288. new_nal_header, sizeof(new_nal_header));
  289. break;
  290. /* PACI packet */
  291. case 50:
  292. /* Temporal scalability control information (TSCI) */
  293. avpriv_report_missing_feature(ctx, "PACI packets for RTP/HEVC");
  294. res = AVERROR_PATCHWELCOME;
  295. break;
  296. }
  297. pkt->stream_index = st->index;
  298. return res;
  299. }
  300. RTPDynamicProtocolHandler ff_hevc_dynamic_handler = {
  301. .enc_name = "H265",
  302. .codec_type = AVMEDIA_TYPE_VIDEO,
  303. .codec_id = AV_CODEC_ID_HEVC,
  304. .need_parsing = AVSTREAM_PARSE_FULL,
  305. .priv_data_size = sizeof(PayloadContext),
  306. .parse_sdp_a_line = hevc_parse_sdp_line,
  307. .parse_packet = hevc_handle_packet,
  308. };