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.

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