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.

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