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.

451 lines
16KB

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