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.

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