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.

514 lines
18KB

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