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.

366 lines
12KB

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