Libav
rtpdec_hevc.c
Go to the documentation of this file.
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 Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "libavutil/avstring.h"
23 #include "libavutil/base64.h"
24 
25 #include "avformat.h"
26 #include "rtpdec.h"
27 #include "rtpdec_formats.h"
28 
29 #define RTP_HEVC_PAYLOAD_HEADER_SIZE 2
30 #define RTP_HEVC_FU_HEADER_SIZE 1
31 #define RTP_HEVC_DONL_FIELD_SIZE 2
32 #define RTP_HEVC_DOND_FIELD_SIZE 1
33 #define HEVC_SPECIFIED_NAL_UNIT_TYPES 48
34 
35 /* SDP out-of-band signaling data */
36 struct PayloadContext {
39  uint8_t *sps, *pps, *vps, *sei;
41 };
42 
43 static const uint8_t start_sequence[] = { 0x00, 0x00, 0x00, 0x01 };
44 
46  AVStream *stream,
47  PayloadContext *hevc_data,
48  const char *attr, const char *value)
49 {
50  /* profile-space: 0-3 */
51  /* profile-id: 0-31 */
52  if (!strcmp(attr, "profile-id")) {
53  hevc_data->profile_id = atoi(value);
54  av_log(s, AV_LOG_TRACE, "SDP: found profile-id: %d\n", hevc_data->profile_id);
55  }
56 
57  /* tier-flag: 0-1 */
58  /* level-id: 0-255 */
59  /* interop-constraints: [base16] */
60  /* profile-compatibility-indicator: [base16] */
61  /* sprop-sub-layer-id: 0-6, defines highest possible value for TID, default: 6 */
62  /* recv-sub-layer-id: 0-6 */
63  /* max-recv-level-id: 0-255 */
64  /* tx-mode: MSM,SSM */
65  /* sprop-vps: [base64] */
66  /* sprop-sps: [base64] */
67  /* sprop-pps: [base64] */
68  /* sprop-sei: [base64] */
69  if (!strcmp(attr, "sprop-vps") || !strcmp(attr, "sprop-sps") ||
70  !strcmp(attr, "sprop-pps") || !strcmp(attr, "sprop-sei")) {
71  uint8_t **data_ptr = NULL;
72  int *size_ptr = NULL;
73  if (!strcmp(attr, "sprop-vps")) {
74  data_ptr = &hevc_data->vps;
75  size_ptr = &hevc_data->vps_size;
76  } else if (!strcmp(attr, "sprop-sps")) {
77  data_ptr = &hevc_data->sps;
78  size_ptr = &hevc_data->sps_size;
79  } else if (!strcmp(attr, "sprop-pps")) {
80  data_ptr = &hevc_data->pps;
81  size_ptr = &hevc_data->pps_size;
82  } else if (!strcmp(attr, "sprop-sei")) {
83  data_ptr = &hevc_data->sei;
84  size_ptr = &hevc_data->sei_size;
85  }
86 
88  size_ptr, value);
89  }
90 
91  /* max-lsr, max-lps, max-cpb, max-dpb, max-br, max-tr, max-tc */
92  /* max-fps */
93 
94  /* sprop-max-don-diff: 0-32767
95 
96  When the RTP stream depends on one or more other RTP
97  streams (in this case tx-mode MUST be equal to "MSM" and
98  MSM is in use), this parameter MUST be present and the
99  value MUST be greater than 0.
100  */
101  if (!strcmp(attr, "sprop-max-don-diff")) {
102  if (atoi(value) > 0)
103  hevc_data->using_donl_field = 1;
104  av_log(s, AV_LOG_TRACE, "Found sprop-max-don-diff in SDP, DON field usage is: %d\n",
105  hevc_data->using_donl_field);
106  }
107 
108  /* sprop-depack-buf-nalus: 0-32767 */
109  if (!strcmp(attr, "sprop-depack-buf-nalus")) {
110  if (atoi(value) > 0)
111  hevc_data->using_donl_field = 1;
112  av_log(s, AV_LOG_TRACE, "Found sprop-depack-buf-nalus in SDP, DON field usage is: %d\n",
113  hevc_data->using_donl_field);
114  }
115 
116  /* sprop-depack-buf-bytes: 0-4294967295 */
117  /* depack-buf-cap */
118  /* sprop-segmentation-id: 0-3 */
119  /* sprop-spatial-segmentation-idc: [base16] */
120  /* dec-parallel-ca: */
121  /* include-dph */
122 
123  return 0;
124 }
125 
126 static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index,
127  PayloadContext *hevc_data, const char *line)
128 {
129  AVStream *current_stream;
130  AVCodecParameters *par;
131  const char *sdp_line_ptr = line;
132 
133  if (st_index < 0)
134  return 0;
135 
136  current_stream = ctx->streams[st_index];
137  par = current_stream->codecpar;
138 
139  if (av_strstart(sdp_line_ptr, "framesize:", &sdp_line_ptr)) {
140  ff_h264_parse_framesize(par, sdp_line_ptr);
141  } else if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
142  int ret = ff_parse_fmtp(ctx, current_stream, hevc_data, sdp_line_ptr,
144  if (hevc_data->vps_size || hevc_data->sps_size ||
145  hevc_data->pps_size || hevc_data->sei_size) {
146  av_freep(&par->extradata);
147  par->extradata_size = hevc_data->vps_size + hevc_data->sps_size +
148  hevc_data->pps_size + hevc_data->sei_size;
149  par->extradata = av_malloc(par->extradata_size +
151  if (!par->extradata) {
152  ret = AVERROR(ENOMEM);
153  par->extradata_size = 0;
154  } else {
155  int pos = 0;
156  memcpy(par->extradata + pos, hevc_data->vps, hevc_data->vps_size);
157  pos += hevc_data->vps_size;
158  memcpy(par->extradata + pos, hevc_data->sps, hevc_data->sps_size);
159  pos += hevc_data->sps_size;
160  memcpy(par->extradata + pos, hevc_data->pps, hevc_data->pps_size);
161  pos += hevc_data->pps_size;
162  memcpy(par->extradata + pos, hevc_data->sei, hevc_data->sei_size);
163  pos += hevc_data->sei_size;
164  memset(par->extradata + pos, 0, AV_INPUT_BUFFER_PADDING_SIZE);
165  }
166 
167  av_freep(&hevc_data->vps);
168  av_freep(&hevc_data->sps);
169  av_freep(&hevc_data->pps);
170  av_freep(&hevc_data->sei);
171  hevc_data->vps_size = 0;
172  hevc_data->sps_size = 0;
173  hevc_data->pps_size = 0;
174  hevc_data->sei_size = 0;
175  }
176  return ret;
177  }
178 
179  return 0;
180 }
181 
183  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
184  const uint8_t *buf, int len, uint16_t seq,
185  int flags)
186 {
187  const uint8_t *rtp_pl = buf;
188  int tid, lid, nal_type;
189  int first_fragment, last_fragment, fu_type;
190  uint8_t new_nal_header[2];
191  int res = 0;
192 
193  /* sanity check for size of input packet: 1 byte payload at least */
194  if (len < RTP_HEVC_PAYLOAD_HEADER_SIZE + 1) {
195  av_log(ctx, AV_LOG_ERROR, "Too short RTP/HEVC packet, got %d bytes\n", len);
196  return AVERROR_INVALIDDATA;
197  }
198 
199  /*
200  * decode the HEVC payload header according to section 4 of draft version 6:
201  *
202  * 0 1
203  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
204  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205  * |F| Type | LayerId | TID |
206  * +-------------+-----------------+
207  *
208  * Forbidden zero (F): 1 bit
209  * NAL unit type (Type): 6 bits
210  * NUH layer ID (LayerId): 6 bits
211  * NUH temporal ID plus 1 (TID): 3 bits
212  */
213  nal_type = (buf[0] >> 1) & 0x3f;
214  lid = ((buf[0] << 5) & 0x20) | ((buf[1] >> 3) & 0x1f);
215  tid = buf[1] & 0x07;
216 
217  /* sanity check for correct layer ID */
218  if (lid) {
219  /* future scalable or 3D video coding extensions */
220  avpriv_report_missing_feature(ctx, "Multi-layer HEVC coding");
221  return AVERROR_PATCHWELCOME;
222  }
223 
224  /* sanity check for correct temporal ID */
225  if (!tid) {
226  av_log(ctx, AV_LOG_ERROR, "Illegal temporal ID in RTP/HEVC packet\n");
227  return AVERROR_INVALIDDATA;
228  }
229 
230  /* sanity check for correct NAL unit type */
231  if (nal_type > 50) {
232  av_log(ctx, AV_LOG_ERROR, "Unsupported (HEVC) NAL type (%d)\n", nal_type);
233  return AVERROR_INVALIDDATA;
234  }
235 
236  switch (nal_type) {
237  /* video parameter set (VPS) */
238  case 32:
239  /* sequence parameter set (SPS) */
240  case 33:
241  /* picture parameter set (PPS) */
242  case 34:
243  /* supplemental enhancement information (SEI) */
244  case 39:
245  /* single NAL unit packet */
246  default:
247  /* create A/V packet */
248  if ((res = av_new_packet(pkt, sizeof(start_sequence) + len)) < 0)
249  return res;
250  /* A/V packet: copy start sequence */
251  memcpy(pkt->data, start_sequence, sizeof(start_sequence));
252  /* A/V packet: copy NAL unit data */
253  memcpy(pkt->data + sizeof(start_sequence), buf, len);
254 
255  break;
256  /* aggregated packet (AP) - with two or more NAL units */
257  case 48:
258  /* pass the HEVC payload header */
261 
262  /* pass the HEVC DONL field */
263  if (rtp_hevc_ctx->using_donl_field) {
266  }
267 
268  res = ff_h264_handle_aggregated_packet(ctx, pkt, buf, len,
269  rtp_hevc_ctx->using_donl_field ?
271  NULL, 0);
272  if (res < 0)
273  return res;
274  break;
275  /* fragmentation unit (FU) */
276  case 49:
277  /* pass the HEVC payload header */
280 
281  /*
282  * decode the FU header
283  *
284  * 0 1 2 3 4 5 6 7
285  * +-+-+-+-+-+-+-+-+
286  * |S|E| FuType |
287  * +---------------+
288  *
289  * Start fragment (S): 1 bit
290  * End fragment (E): 1 bit
291  * FuType: 6 bits
292  */
293  first_fragment = buf[0] & 0x80;
294  last_fragment = buf[0] & 0x40;
295  fu_type = buf[0] & 0x3f;
296 
297  /* pass the HEVC FU header */
300 
301  /* pass the HEVC DONL field */
302  if (rtp_hevc_ctx->using_donl_field) {
305  }
306 
307  av_log(ctx, AV_LOG_TRACE, " FU type %d with %d bytes\n", fu_type, len);
308 
309  if (len <= 0) {
310  /* sanity check for size of input packet: 1 byte payload at least */
311  av_log(ctx, AV_LOG_ERROR,
312  "Too short RTP/HEVC packet, got %d bytes of NAL unit type %d\n",
313  len, nal_type);
314  return AVERROR_INVALIDDATA;
315  }
316 
317  if (first_fragment && last_fragment) {
318  av_log(ctx, AV_LOG_ERROR, "Illegal combination of S and E bit in RTP/HEVC packet\n");
319  return AVERROR_INVALIDDATA;
320  }
321 
322  new_nal_header[0] = (rtp_pl[0] & 0x81) | (fu_type << 1);
323  new_nal_header[1] = rtp_pl[1];
324 
325  res = ff_h264_handle_frag_packet(pkt, buf, len, first_fragment,
326  new_nal_header, sizeof(new_nal_header));
327 
328  break;
329  /* PACI packet */
330  case 50:
331  /* Temporal scalability control information (TSCI) */
332  avpriv_report_missing_feature(ctx, "PACI packets for RTP/HEVC");
333  res = AVERROR_PATCHWELCOME;
334  break;
335  }
336 
337  pkt->stream_index = st->index;
338 
339  return res;
340 }
341 
343  .enc_name = "H265",
344  .codec_type = AVMEDIA_TYPE_VIDEO,
345  .codec_id = AV_CODEC_ID_HEVC,
346  .need_parsing = AVSTREAM_PARSE_FULL,
347  .priv_data_size = sizeof(PayloadContext),
348  .parse_sdp_a_line = hevc_parse_sdp_line,
350 };
AVPacket pkt
Definition: rtpdec_qt.c:37
#define RTP_HEVC_DOND_FIELD_SIZE
Definition: rtpdec_hevc.c:32
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int using_donl_field
Definition: rtpdec_hevc.c:37
static const uint8_t start_sequence[]
Definition: rtpdec_hevc.c:43
RTP/JPEG specific private data.
Definition: rdt.c:83
int index
stream index in AVFormatContext
Definition: avformat.h:706
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, AVPacket *pkt, const uint8_t *buf, int len, int start_skip, int *nal_counters, int nal_mask)
Definition: rtpdec_h264.c:202
static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index, PayloadContext *hevc_data, const char *line)
Definition: rtpdec_hevc.c:126
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
uint8_t * sei
Definition: rtpdec_hevc.c:39
Format I/O context.
Definition: avformat.h:940
static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s, AVStream *stream, PayloadContext *hevc_data, const char *attr, const char *value)
Definition: rtpdec_hevc.c:45
uint8_t
#define av_cold
Definition: attributes.h:66
uint8_t * pps
Definition: rtpdec_hevc.c:39
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
#define RTP_HEVC_FU_HEADER_SIZE
Definition: rtpdec_hevc.c:30
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR(e)
Definition: error.h:43
Definition: graph2dot.c:48
uint8_t * sps
Definition: rtpdec_hevc.c:39
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
uint8_t * vps
Definition: rtpdec_hevc.c:39
RTPDynamicProtocolHandler ff_hevc_dynamic_handler
Definition: rtpdec_hevc.c:342
AVFormatContext * ctx
Definition: movenc.c:48
static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, uint16_t seq, int flags)
Definition: rtpdec_hevc.c:182
Stream structure.
Definition: avformat.h:705
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:55
void ff_h264_parse_framesize(AVCodecParameters *par, const char *p)
Definition: rtpdec_h264.c:180
#define RTP_HEVC_PAYLOAD_HEADER_SIZE
Definition: rtpdec_hevc.c:29
int ff_h264_handle_frag_packet(AVPacket *pkt, const uint8_t *buf, int len, int start_bit, const uint8_t *nal_header, int nal_header_len)
Definition: rtpdec_h264.c:260
uint8_t * buf
the temporary storage buffer
Definition: rtpdec_asf.c:168
int ff_parse_fmtp(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *p, int(*parse_fmtp)(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *attr, const char *value))
Definition: rtpdec.c:853
const char * enc_name
Definition: rtpdec.h:116
int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s, uint8_t **data_ptr, int *size_ptr, const char *value)
Definition: rtpdec_h264.c:96
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:32
full parsing and repack
Definition: avformat.h:657
Main libavformat public API header.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
Parse a packet, add all split parts to parse_queue.
Definition: utils.c:816
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
#define RTP_HEVC_DONL_FIELD_SIZE
Definition: rtpdec_hevc.c:31
AVCodecParameters * codecpar
Definition: avformat.h:831
int stream_index
Definition: avcodec.h:1348
This structure stores compressed data.
Definition: avcodec.h:1323