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.

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