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.

413 lines
15KB

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