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.

360 lines
13KB

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