FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 "libavcodec/get_bits.h"
26 
27 #include "avformat.h"
28 #include "rtpdec.h"
29 #include "rtpdec_formats.h"
30 
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
37 
38 /* SDP out-of-band signaling data */
39 struct PayloadContext {
42  uint8_t *sps, *pps, *vps, *sei;
44 };
45 
46 static const uint8_t start_sequence[] = { 0x00, 0x00, 0x00, 0x01 };
47 
49  AVStream *stream,
50  PayloadContext *hevc_data,
51  const char *attr, const char *value)
52 {
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);
58  }
59 
60  /* tier-flag: 0-1 */
61  /* level-id: 0-255 */
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;
75  int *size_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;
88  } else
89  av_assert0(0);
90 
92  size_ptr, value);
93  }
94 
95  /* max-lsr, max-lps, max-cpb, max-dpb, max-br, max-tr, max-tc */
96  /* max-fps */
97 
98  /* sprop-max-don-diff: 0-32767
99 
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.
104  */
105  if (!strcmp(attr, "sprop-max-don-diff")) {
106  if (atoi(value) > 0)
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);
110  }
111 
112  /* sprop-depack-buf-nalus: 0-32767 */
113  if (!strcmp(attr, "sprop-depack-buf-nalus")) {
114  if (atoi(value) > 0)
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);
118  }
119 
120  /* sprop-depack-buf-bytes: 0-4294967295 */
121  /* depack-buf-cap */
122  /* sprop-segmentation-id: 0-3 */
123  /* sprop-spatial-segmentation-idc: [base16] */
124  /* dec-parallel-ca: */
125  /* include-dph */
126 
127  return 0;
128 }
129 
130 static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index,
131  PayloadContext *hevc_data, const char *line)
132 {
133  AVStream *current_stream;
134  AVCodecParameters *par;
135  const char *sdp_line_ptr = line;
136 
137  if (st_index < 0)
138  return 0;
139 
140  current_stream = ctx->streams[st_index];
141  par = current_stream->codecpar;
142 
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,
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 +
155  if (!par->extradata) {
156  ret = AVERROR(ENOMEM);
157  par->extradata_size = 0;
158  } else {
159  int pos = 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);
169  }
170 
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;
179  }
180  return ret;
181  }
182 
183  return 0;
184 }
185 
187  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
188  const uint8_t *buf, int len, uint16_t seq,
189  int flags)
190 {
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];
195  int res = 0;
196 
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;
201  }
202 
203  /*
204  * decode the HEVC payload header according to section 4 of draft version 6:
205  *
206  * 0 1
207  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
208  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
209  * |F| Type | LayerId | TID |
210  * +-------------+-----------------+
211  *
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
216  */
217  nal_type = (buf[0] >> 1) & 0x3f;
218  lid = ((buf[0] << 5) & 0x20) | ((buf[1] >> 3) & 0x1f);
219  tid = buf[1] & 0x07;
220 
221  /* sanity check for correct layer ID */
222  if (lid) {
223  /* future scalable or 3D video coding extensions */
224  avpriv_report_missing_feature(ctx, "Multi-layer HEVC coding");
225  return AVERROR_PATCHWELCOME;
226  }
227 
228  /* sanity check for correct temporal ID */
229  if (!tid) {
230  av_log(ctx, AV_LOG_ERROR, "Illegal temporal ID in RTP/HEVC packet\n");
231  return AVERROR_INVALIDDATA;
232  }
233 
234  /* sanity check for correct NAL unit type */
235  if (nal_type > 50) {
236  av_log(ctx, AV_LOG_ERROR, "Unsupported (HEVC) NAL type (%d)\n", nal_type);
237  return AVERROR_INVALIDDATA;
238  }
239 
240  switch (nal_type) {
241  /* video parameter set (VPS) */
242  case 32:
243  /* sequence parameter set (SPS) */
244  case 33:
245  /* picture parameter set (PPS) */
246  case 34:
247  /* supplemental enhancement information (SEI) */
248  case 39:
249  /* single NAL unit packet */
250  default:
251  /* create A/V packet */
252  if ((res = av_new_packet(pkt, sizeof(start_sequence) + len)) < 0)
253  return res;
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);
258 
259  break;
260  /* aggregated packet (AP) - with two or more NAL units */
261  case 48:
262  /* pass the HEVC payload header */
265 
266  /* pass the HEVC DONL field */
267  if (rtp_hevc_ctx->using_donl_field) {
270  }
271 
272  res = ff_h264_handle_aggregated_packet(ctx, rtp_hevc_ctx, pkt, buf, len,
273  rtp_hevc_ctx->using_donl_field ?
275  NULL, 0);
276  if (res < 0)
277  return res;
278  break;
279  /* fragmentation unit (FU) */
280  case 49:
281  /* pass the HEVC payload header */
284 
285  /*
286  * decode the FU header
287  *
288  * 0 1 2 3 4 5 6 7
289  * +-+-+-+-+-+-+-+-+
290  * |S|E| FuType |
291  * +---------------+
292  *
293  * Start fragment (S): 1 bit
294  * End fragment (E): 1 bit
295  * FuType: 6 bits
296  */
297  first_fragment = buf[0] & 0x80;
298  last_fragment = buf[0] & 0x40;
299  fu_type = buf[0] & 0x3f;
300 
301  /* pass the HEVC FU header */
304 
305  /* pass the HEVC DONL field */
306  if (rtp_hevc_ctx->using_donl_field) {
309  }
310 
311  av_log(ctx, AV_LOG_TRACE, " FU type %d with %d bytes\n", fu_type, len);
312 
313  /* sanity check for size of input packet: 1 byte payload at least */
314  if (len <= 0) {
315  if (len < 0) {
316  av_log(ctx, AV_LOG_ERROR,
317  "Too short RTP/HEVC packet, got %d bytes of NAL unit type %d\n",
318  len, nal_type);
319  return AVERROR_INVALIDDATA;
320  } else {
321  return AVERROR(EAGAIN);
322  }
323  }
324 
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;
328  }
329 
330  new_nal_header[0] = (rtp_pl[0] & 0x81) | (fu_type << 1);
331  new_nal_header[1] = rtp_pl[1];
332 
333  res = ff_h264_handle_frag_packet(pkt, buf, len, first_fragment,
334  new_nal_header, sizeof(new_nal_header));
335 
336  break;
337  /* PACI packet */
338  case 50:
339  /* Temporal scalability control information (TSCI) */
340  avpriv_report_missing_feature(ctx, "PACI packets for RTP/HEVC");
341  res = AVERROR_PATCHWELCOME;
342  break;
343  }
344 
345  pkt->stream_index = st->index;
346 
347  return res;
348 }
349 
351  .enc_name = "H265",
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,
358 };
#define RTP_HEVC_DOND_FIELD_SIZE
Definition: rtpdec_hevc.c:34
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int using_donl_field
Definition: rtpdec_hevc.c:40
static const uint8_t start_sequence[]
Definition: rtpdec_hevc.c:46
RTP/JPEG specific private data.
Definition: rdt.c:83
int index
stream index in AVFormatContext
Definition: avformat.h:890
static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index, PayloadContext *hevc_data, const char *line)
Definition: rtpdec_hevc.c:130
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4058
uint8_t * sei
Definition: rtpdec_hevc.c:42
Format I/O context.
Definition: avformat.h:1349
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:48
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
uint8_t * pps
Definition: rtpdec_hevc.c:42
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
uint8_t * data
Definition: avcodec.h:1657
static int flags
Definition: log.c:57
bitstream reader API header.
#define av_log(a,...)
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define RTP_HEVC_FU_HEADER_SIZE
Definition: rtpdec_hevc.c:32
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
Definition: graph2dot.c:48
simple assert() macros that are a bit more flexible than ISO C assert().
uint8_t * sps
Definition: rtpdec_hevc.c:42
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4084
uint8_t * vps
Definition: rtpdec_hevc.c:42
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
RTPDynamicProtocolHandler ff_hevc_dynamic_handler
Definition: rtpdec_hevc.c:350
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:186
Stream structure.
Definition: avformat.h:889
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
void ff_h264_parse_framesize(AVCodecParameters *par, const char *p)
Definition: rtpdec_h264.c:184
#define RTP_HEVC_PAYLOAD_HEADER_SIZE
Definition: rtpdec_hevc.c:31
void * buf
Definition: avisynth_c.h:690
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:264
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:872
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:34
full parsing and repack
Definition: avformat.h:810
Main libavformat public API header.
int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, PayloadContext *data, AVPacket *pkt, const uint8_t *buf, int len, int start_skip, int *nal_counters, int nal_mask)
Definition: rtpdec_h264.c:206
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:769
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
Parse a packet, add all split parts to parse_queue.
Definition: utils.c:1392
int len
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4080
#define RTP_HEVC_DONL_FIELD_SIZE
Definition: rtpdec_hevc.c:33
#define av_freep(p)
AVCodecParameters * codecpar
Definition: avformat.h:1252
int stream_index
Definition: avcodec.h:1659
This structure stores compressed data.
Definition: avcodec.h:1634