2 * RTP parser for HEVC/H.265 payload format (draft version 6)
3 * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/avstring.h"
24 #include "libavutil/base64.h"
28 #include "rtpdec_formats.h"
30 #define RTP_HEVC_PAYLOAD_HEADER_SIZE 2
31 #define RTP_HEVC_FU_HEADER_SIZE 1
32 #define RTP_HEVC_DONL_FIELD_SIZE 2
33 #define RTP_HEVC_DOND_FIELD_SIZE 1
34 #define HEVC_SPECIFIED_NAL_UNIT_TYPES 48
36 /* SDP out-of-band signaling data */
37 struct PayloadContext {
40 uint8_t *sps, *pps, *vps, *sei;
41 int sps_size, pps_size, vps_size, sei_size;
44 static const uint8_t start_sequence[] = { 0x00, 0x00, 0x00, 0x01 };
46 static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
48 PayloadContext *hevc_data,
49 const char *attr, const char *value)
51 /* profile-space: 0-3 */
52 /* profile-id: 0-31 */
53 if (!strcmp(attr, "profile-id")) {
54 hevc_data->profile_id = atoi(value);
55 av_log(s, AV_LOG_TRACE, "SDP: found profile-id: %d\n", hevc_data->profile_id);
60 /* interop-constraints: [base16] */
61 /* profile-compatibility-indicator: [base16] */
62 /* sprop-sub-layer-id: 0-6, defines highest possible value for TID, default: 6 */
63 /* recv-sub-layer-id: 0-6 */
64 /* max-recv-level-id: 0-255 */
65 /* tx-mode: MSM,SSM */
66 /* sprop-vps: [base64] */
67 /* sprop-sps: [base64] */
68 /* sprop-pps: [base64] */
69 /* sprop-sei: [base64] */
70 if (!strcmp(attr, "sprop-vps") || !strcmp(attr, "sprop-sps") ||
71 !strcmp(attr, "sprop-pps") || !strcmp(attr, "sprop-sei")) {
72 uint8_t **data_ptr = NULL;
74 if (!strcmp(attr, "sprop-vps")) {
75 data_ptr = &hevc_data->vps;
76 size_ptr = &hevc_data->vps_size;
77 } else if (!strcmp(attr, "sprop-sps")) {
78 data_ptr = &hevc_data->sps;
79 size_ptr = &hevc_data->sps_size;
80 } else if (!strcmp(attr, "sprop-pps")) {
81 data_ptr = &hevc_data->pps;
82 size_ptr = &hevc_data->pps_size;
83 } else if (!strcmp(attr, "sprop-sei")) {
84 data_ptr = &hevc_data->sei;
85 size_ptr = &hevc_data->sei_size;
88 ff_h264_parse_sprop_parameter_sets(s, data_ptr,
92 /* max-lsr, max-lps, max-cpb, max-dpb, max-br, max-tr, max-tc */
95 /* sprop-max-don-diff: 0-32767
97 When the RTP stream depends on one or more other RTP
98 streams (in this case tx-mode MUST be equal to "MSM" and
99 MSM is in use), this parameter MUST be present and the
100 value MUST be greater than 0.
102 if (!strcmp(attr, "sprop-max-don-diff")) {
104 hevc_data->using_donl_field = 1;
105 av_log(s, AV_LOG_TRACE, "Found sprop-max-don-diff in SDP, DON field usage is: %d\n",
106 hevc_data->using_donl_field);
109 /* sprop-depack-buf-nalus: 0-32767 */
110 if (!strcmp(attr, "sprop-depack-buf-nalus")) {
112 hevc_data->using_donl_field = 1;
113 av_log(s, AV_LOG_TRACE, "Found sprop-depack-buf-nalus in SDP, DON field usage is: %d\n",
114 hevc_data->using_donl_field);
117 /* sprop-depack-buf-bytes: 0-4294967295 */
119 /* sprop-segmentation-id: 0-3 */
120 /* sprop-spatial-segmentation-idc: [base16] */
121 /* dec-parallel-ca: */
127 static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index,
128 PayloadContext *hevc_data, const char *line)
130 AVStream *current_stream;
131 AVCodecContext *codec;
132 const char *sdp_line_ptr = line;
137 current_stream = ctx->streams[st_index];
138 codec = current_stream->codec;
140 if (av_strstart(sdp_line_ptr, "framesize:", &sdp_line_ptr)) {
141 ff_h264_parse_framesize(codec, sdp_line_ptr);
142 } else if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
143 int ret = ff_parse_fmtp(ctx, current_stream, hevc_data, sdp_line_ptr,
144 hevc_sdp_parse_fmtp_config);
145 if (hevc_data->vps_size || hevc_data->sps_size ||
146 hevc_data->pps_size || hevc_data->sei_size) {
147 av_freep(&codec->extradata);
148 codec->extradata_size = hevc_data->vps_size + hevc_data->sps_size +
149 hevc_data->pps_size + hevc_data->sei_size;
150 codec->extradata = av_malloc(codec->extradata_size +
151 FF_INPUT_BUFFER_PADDING_SIZE);
152 if (!codec->extradata) {
153 ret = AVERROR(ENOMEM);
154 codec->extradata_size = 0;
157 memcpy(codec->extradata + pos, hevc_data->vps, hevc_data->vps_size);
158 pos += hevc_data->vps_size;
159 memcpy(codec->extradata + pos, hevc_data->sps, hevc_data->sps_size);
160 pos += hevc_data->sps_size;
161 memcpy(codec->extradata + pos, hevc_data->pps, hevc_data->pps_size);
162 pos += hevc_data->pps_size;
163 memcpy(codec->extradata + pos, hevc_data->sei, hevc_data->sei_size);
164 pos += hevc_data->sei_size;
165 memset(codec->extradata + pos, 0, FF_INPUT_BUFFER_PADDING_SIZE);
168 av_freep(&hevc_data->vps);
169 av_freep(&hevc_data->sps);
170 av_freep(&hevc_data->pps);
171 av_freep(&hevc_data->sei);
172 hevc_data->vps_size = 0;
173 hevc_data->sps_size = 0;
174 hevc_data->pps_size = 0;
175 hevc_data->sei_size = 0;
183 static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx,
184 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
185 const uint8_t *buf, int len, uint16_t seq,
188 const uint8_t *rtp_pl = buf;
189 int tid, lid, nal_type;
190 int first_fragment, last_fragment, fu_type;
191 uint8_t new_nal_header[2];
194 /* sanity check for size of input packet: 1 byte payload at least */
195 if (len < RTP_HEVC_PAYLOAD_HEADER_SIZE + 1) {
196 av_log(ctx, AV_LOG_ERROR, "Too short RTP/HEVC packet, got %d bytes\n", len);
197 return AVERROR_INVALIDDATA;
201 * decode the HEVC payload header according to section 4 of draft version 6:
204 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
205 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206 * |F| Type | LayerId | TID |
207 * +-------------+-----------------+
209 * Forbidden zero (F): 1 bit
210 * NAL unit type (Type): 6 bits
211 * NUH layer ID (LayerId): 6 bits
212 * NUH temporal ID plus 1 (TID): 3 bits
214 nal_type = (buf[0] >> 1) & 0x3f;
215 lid = ((buf[0] << 5) & 0x20) | ((buf[1] >> 3) & 0x1f);
218 /* sanity check for correct layer ID */
220 /* future scalable or 3D video coding extensions */
221 avpriv_report_missing_feature(ctx, "Multi-layer HEVC coding\n");
222 return AVERROR_PATCHWELCOME;
225 /* sanity check for correct temporal ID */
227 av_log(ctx, AV_LOG_ERROR, "Illegal temporal ID in RTP/HEVC packet\n");
228 return AVERROR_INVALIDDATA;
231 /* sanity check for correct NAL unit type */
233 av_log(ctx, AV_LOG_ERROR, "Unsupported (HEVC) NAL type (%d)\n", nal_type);
234 return AVERROR_INVALIDDATA;
238 /* video parameter set (VPS) */
240 /* sequence parameter set (SPS) */
242 /* picture parameter set (PPS) */
244 /* supplemental enhancement information (SEI) */
246 /* single NAL unit packet */
248 /* create A/V packet */
249 if ((res = av_new_packet(pkt, sizeof(start_sequence) + len)) < 0)
251 /* A/V packet: copy start sequence */
252 memcpy(pkt->data, start_sequence, sizeof(start_sequence));
253 /* A/V packet: copy NAL unit data */
254 memcpy(pkt->data + sizeof(start_sequence), buf, len);
257 /* aggregated packet (AP) - with two or more NAL units */
259 /* pass the HEVC payload header */
260 buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
261 len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
263 /* pass the HEVC DONL field */
264 if (rtp_hevc_ctx->using_donl_field) {
265 buf += RTP_HEVC_DONL_FIELD_SIZE;
266 len -= RTP_HEVC_DONL_FIELD_SIZE;
269 res = ff_h264_handle_aggregated_packet(ctx, pkt, buf, len,
270 rtp_hevc_ctx->using_donl_field ?
271 RTP_HEVC_DOND_FIELD_SIZE : 0,
276 /* fragmentation unit (FU) */
278 /* pass the HEVC payload header */
279 buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
280 len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
283 * decode the FU header
290 * Start fragment (S): 1 bit
291 * End fragment (E): 1 bit
294 first_fragment = buf[0] & 0x80;
295 last_fragment = buf[0] & 0x40;
296 fu_type = buf[0] & 0x3f;
298 /* pass the HEVC FU header */
299 buf += RTP_HEVC_FU_HEADER_SIZE;
300 len -= RTP_HEVC_FU_HEADER_SIZE;
302 /* pass the HEVC DONL field */
303 if (rtp_hevc_ctx->using_donl_field) {
304 buf += RTP_HEVC_DONL_FIELD_SIZE;
305 len -= RTP_HEVC_DONL_FIELD_SIZE;
308 av_log(ctx, AV_LOG_TRACE, " FU type %d with %d bytes\n", fu_type, len);
311 /* sanity check for size of input packet: 1 byte payload at least */
312 av_log(ctx, AV_LOG_ERROR,
313 "Too short RTP/HEVC packet, got %d bytes of NAL unit type %d\n",
315 return AVERROR_INVALIDDATA;
318 if (first_fragment && last_fragment) {
319 av_log(ctx, AV_LOG_ERROR, "Illegal combination of S and E bit in RTP/HEVC packet\n");
320 return AVERROR_INVALIDDATA;
323 new_nal_header[0] = (rtp_pl[0] & 0x81) | (fu_type << 1);
324 new_nal_header[1] = rtp_pl[1];
326 res = ff_h264_handle_frag_packet(pkt, buf, len, first_fragment,
327 new_nal_header, sizeof(new_nal_header));
332 /* Temporal scalability control information (TSCI) */
333 avpriv_report_missing_feature(ctx, "PACI packets for RTP/HEVC\n");
334 res = AVERROR_PATCHWELCOME;
338 pkt->stream_index = st->index;
343 RTPDynamicProtocolHandler ff_hevc_dynamic_handler = {
345 .codec_type = AVMEDIA_TYPE_VIDEO,
346 .codec_id = AV_CODEC_ID_HEVC,
347 .need_parsing = AVSTREAM_PARSE_FULL,
348 .priv_data_size = sizeof(PayloadContext),
349 .parse_sdp_a_line = hevc_parse_sdp_line,
350 .parse_packet = hevc_handle_packet,