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.

408 lines
13KB

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