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 FFmpeg.
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.
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.
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
22 #include "libavutil/avassert.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/base64.h"
25 #include "libavcodec/get_bits.h"
29 #include "rtpdec_formats.h"
31 #define RTP_HEVC_PAYLOAD_HEADER_SIZE 2
32 #define RTP_HEVC_FU_HEADER_SIZE 1
33 #define RTP_HEVC_DONL_FIELD_SIZE 2
34 #define RTP_HEVC_DOND_FIELD_SIZE 1
35 #define RTP_HEVC_AP_NALU_LENGTH_FIELD_SIZE 2
36 #define HEVC_SPECIFIED_NAL_UNIT_TYPES 48
38 /* SDP out-of-band signaling data */
39 struct PayloadContext {
42 uint8_t *sps, *pps, *vps, *sei;
43 int sps_size, pps_size, vps_size, sei_size;
46 static const uint8_t start_sequence[] = { 0x00, 0x00, 0x00, 0x01 };
48 static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
50 PayloadContext *hevc_data,
51 const char *attr, const char *value)
53 /* profile-space: 0-3 */
54 /* profile-id: 0-31 */
55 if (!strcmp(attr, "profile-id")) {
56 hevc_data->profile_id = atoi(value);
57 av_log(s, AV_LOG_TRACE, "SDP: found profile-id: %d\n", hevc_data->profile_id);
62 /* interop-constraints: [base16] */
63 /* profile-compatibility-indicator: [base16] */
64 /* sprop-sub-layer-id: 0-6, defines highest possible value for TID, default: 6 */
65 /* recv-sub-layer-id: 0-6 */
66 /* max-recv-level-id: 0-255 */
67 /* tx-mode: MSM,SSM */
68 /* sprop-vps: [base64] */
69 /* sprop-sps: [base64] */
70 /* sprop-pps: [base64] */
71 /* sprop-sei: [base64] */
72 if (!strcmp(attr, "sprop-vps") || !strcmp(attr, "sprop-sps") ||
73 !strcmp(attr, "sprop-pps") || !strcmp(attr, "sprop-sei")) {
74 uint8_t **data_ptr = NULL;
76 if (!strcmp(attr, "sprop-vps")) {
77 data_ptr = &hevc_data->vps;
78 size_ptr = &hevc_data->vps_size;
79 } else if (!strcmp(attr, "sprop-sps")) {
80 data_ptr = &hevc_data->sps;
81 size_ptr = &hevc_data->sps_size;
82 } else if (!strcmp(attr, "sprop-pps")) {
83 data_ptr = &hevc_data->pps;
84 size_ptr = &hevc_data->pps_size;
85 } else if (!strcmp(attr, "sprop-sei")) {
86 data_ptr = &hevc_data->sei;
87 size_ptr = &hevc_data->sei_size;
91 ff_h264_parse_sprop_parameter_sets(s, data_ptr,
95 /* max-lsr, max-lps, max-cpb, max-dpb, max-br, max-tr, max-tc */
98 /* sprop-max-don-diff: 0-32767
100 When the RTP stream depends on one or more other RTP
101 streams (in this case tx-mode MUST be equal to "MSM" and
102 MSM is in use), this parameter MUST be present and the
103 value MUST be greater than 0.
105 if (!strcmp(attr, "sprop-max-don-diff")) {
107 hevc_data->using_donl_field = 1;
108 av_log(s, AV_LOG_TRACE, "Found sprop-max-don-diff in SDP, DON field usage is: %d\n",
109 hevc_data->using_donl_field);
112 /* sprop-depack-buf-nalus: 0-32767 */
113 if (!strcmp(attr, "sprop-depack-buf-nalus")) {
115 hevc_data->using_donl_field = 1;
116 av_log(s, AV_LOG_TRACE, "Found sprop-depack-buf-nalus in SDP, DON field usage is: %d\n",
117 hevc_data->using_donl_field);
120 /* sprop-depack-buf-bytes: 0-4294967295 */
122 /* sprop-segmentation-id: 0-3 */
123 /* sprop-spatial-segmentation-idc: [base16] */
124 /* dec-parallel-ca: */
130 static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index,
131 PayloadContext *hevc_data, const char *line)
133 AVStream *current_stream;
134 AVCodecParameters *par;
135 const char *sdp_line_ptr = line;
140 current_stream = ctx->streams[st_index];
141 par = current_stream->codecpar;
143 if (av_strstart(sdp_line_ptr, "framesize:", &sdp_line_ptr)) {
144 ff_h264_parse_framesize(par, sdp_line_ptr);
145 } else if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
146 int ret = ff_parse_fmtp(ctx, current_stream, hevc_data, sdp_line_ptr,
147 hevc_sdp_parse_fmtp_config);
148 if (hevc_data->vps_size || hevc_data->sps_size ||
149 hevc_data->pps_size || hevc_data->sei_size) {
150 av_freep(&par->extradata);
151 par->extradata_size = hevc_data->vps_size + hevc_data->sps_size +
152 hevc_data->pps_size + hevc_data->sei_size;
153 par->extradata = av_malloc(par->extradata_size +
154 AV_INPUT_BUFFER_PADDING_SIZE);
155 if (!par->extradata) {
156 ret = AVERROR(ENOMEM);
157 par->extradata_size = 0;
160 memcpy(par->extradata + pos, hevc_data->vps, hevc_data->vps_size);
161 pos += hevc_data->vps_size;
162 memcpy(par->extradata + pos, hevc_data->sps, hevc_data->sps_size);
163 pos += hevc_data->sps_size;
164 memcpy(par->extradata + pos, hevc_data->pps, hevc_data->pps_size);
165 pos += hevc_data->pps_size;
166 memcpy(par->extradata + pos, hevc_data->sei, hevc_data->sei_size);
167 pos += hevc_data->sei_size;
168 memset(par->extradata + pos, 0, AV_INPUT_BUFFER_PADDING_SIZE);
171 av_freep(&hevc_data->vps);
172 av_freep(&hevc_data->sps);
173 av_freep(&hevc_data->pps);
174 av_freep(&hevc_data->sei);
175 hevc_data->vps_size = 0;
176 hevc_data->sps_size = 0;
177 hevc_data->pps_size = 0;
178 hevc_data->sei_size = 0;
186 static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx,
187 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
188 const uint8_t *buf, int len, uint16_t seq,
191 const uint8_t *rtp_pl = buf;
192 int tid, lid, nal_type;
193 int first_fragment, last_fragment, fu_type;
194 uint8_t new_nal_header[2];
197 /* sanity check for size of input packet: 1 byte payload at least */
198 if (len < RTP_HEVC_PAYLOAD_HEADER_SIZE + 1) {
199 av_log(ctx, AV_LOG_ERROR, "Too short RTP/HEVC packet, got %d bytes\n", len);
200 return AVERROR_INVALIDDATA;
204 * decode the HEVC payload header according to section 4 of draft version 6:
207 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
208 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
209 * |F| Type | LayerId | TID |
210 * +-------------+-----------------+
212 * Forbidden zero (F): 1 bit
213 * NAL unit type (Type): 6 bits
214 * NUH layer ID (LayerId): 6 bits
215 * NUH temporal ID plus 1 (TID): 3 bits
217 nal_type = (buf[0] >> 1) & 0x3f;
218 lid = ((buf[0] << 5) & 0x20) | ((buf[1] >> 3) & 0x1f);
221 /* sanity check for correct layer ID */
223 /* future scalable or 3D video coding extensions */
224 avpriv_report_missing_feature(ctx, "Multi-layer HEVC coding");
225 return AVERROR_PATCHWELCOME;
228 /* sanity check for correct temporal ID */
230 av_log(ctx, AV_LOG_ERROR, "Illegal temporal ID in RTP/HEVC packet\n");
231 return AVERROR_INVALIDDATA;
234 /* sanity check for correct NAL unit type */
236 av_log(ctx, AV_LOG_ERROR, "Unsupported (HEVC) NAL type (%d)\n", nal_type);
237 return AVERROR_INVALIDDATA;
241 /* video parameter set (VPS) */
243 /* sequence parameter set (SPS) */
245 /* picture parameter set (PPS) */
247 /* supplemental enhancement information (SEI) */
249 /* single NAL unit packet */
251 /* create A/V packet */
252 if ((res = av_new_packet(pkt, sizeof(start_sequence) + len)) < 0)
254 /* A/V packet: copy start sequence */
255 memcpy(pkt->data, start_sequence, sizeof(start_sequence));
256 /* A/V packet: copy NAL unit data */
257 memcpy(pkt->data + sizeof(start_sequence), buf, len);
260 /* aggregated packet (AP) - with two or more NAL units */
262 /* pass the HEVC payload header */
263 buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
264 len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
266 /* pass the HEVC DONL field */
267 if (rtp_hevc_ctx->using_donl_field) {
268 buf += RTP_HEVC_DONL_FIELD_SIZE;
269 len -= RTP_HEVC_DONL_FIELD_SIZE;
272 res = ff_h264_handle_aggregated_packet(ctx, rtp_hevc_ctx, pkt, buf, len,
273 rtp_hevc_ctx->using_donl_field ?
274 RTP_HEVC_DOND_FIELD_SIZE : 0,
279 /* fragmentation unit (FU) */
281 /* pass the HEVC payload header */
282 buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
283 len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
286 * decode the FU header
293 * Start fragment (S): 1 bit
294 * End fragment (E): 1 bit
297 first_fragment = buf[0] & 0x80;
298 last_fragment = buf[0] & 0x40;
299 fu_type = buf[0] & 0x3f;
301 /* pass the HEVC FU header */
302 buf += RTP_HEVC_FU_HEADER_SIZE;
303 len -= RTP_HEVC_FU_HEADER_SIZE;
305 /* pass the HEVC DONL field */
306 if (rtp_hevc_ctx->using_donl_field) {
307 buf += RTP_HEVC_DONL_FIELD_SIZE;
308 len -= RTP_HEVC_DONL_FIELD_SIZE;
311 av_log(ctx, AV_LOG_TRACE, " FU type %d with %d bytes\n", fu_type, len);
313 /* sanity check for size of input packet: 1 byte payload at least */
316 av_log(ctx, AV_LOG_ERROR,
317 "Too short RTP/HEVC packet, got %d bytes of NAL unit type %d\n",
319 return AVERROR_INVALIDDATA;
321 return AVERROR(EAGAIN);
325 if (first_fragment && last_fragment) {
326 av_log(ctx, AV_LOG_ERROR, "Illegal combination of S and E bit in RTP/HEVC packet\n");
327 return AVERROR_INVALIDDATA;
330 new_nal_header[0] = (rtp_pl[0] & 0x81) | (fu_type << 1);
331 new_nal_header[1] = rtp_pl[1];
333 res = ff_h264_handle_frag_packet(pkt, buf, len, first_fragment,
334 new_nal_header, sizeof(new_nal_header));
339 /* Temporal scalability control information (TSCI) */
340 avpriv_report_missing_feature(ctx, "PACI packets for RTP/HEVC");
341 res = AVERROR_PATCHWELCOME;
345 pkt->stream_index = st->index;
350 const RTPDynamicProtocolHandler ff_hevc_dynamic_handler = {
352 .codec_type = AVMEDIA_TYPE_VIDEO,
353 .codec_id = AV_CODEC_ID_HEVC,
354 .need_parsing = AVSTREAM_PARSE_FULL,
355 .priv_data_size = sizeof(PayloadContext),
356 .parse_sdp_a_line = hevc_parse_sdp_line,
357 .parse_packet = hevc_handle_packet,