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