FFmpeg
rtpdec_xiph.c
Go to the documentation of this file.
1 /*
2  * Xiph RTP Protocols
3  * Copyright (c) 2009 Colin McQuillian
4  * Copyright (c) 2010 Josh Allmann
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * @brief Xiph / RTP Code
26  * @author Colin McQuillan <m.niloc@gmail.com>
27  * @author Josh Allmann <joshua.allmann@gmail.com>
28  */
29 
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/base64.h"
34 #include "libavcodec/bytestream.h"
35 
36 #include "avio_internal.h"
37 #include "internal.h"
38 #include "rtpdec.h"
39 #include "rtpdec_formats.h"
40 
41 /**
42  * RTP/Xiph specific private data.
43  */
44 struct PayloadContext {
45  unsigned ident; ///< 24-bit stream configuration identifier
46  uint32_t timestamp;
47  AVIOContext* fragment; ///< buffer for split payloads
50  int split_pkts;
51 };
52 
54 {
55  ffio_free_dyn_buf(&data->fragment);
56  av_freep(&data->split_buf);
57 }
58 
59 
61  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
62  const uint8_t *buf, int len, uint16_t seq,
63  int flags)
64 {
65 
66  int ident, fragmented, tdt, num_pkts, pkt_len;
67 
68  if (!buf) {
69  if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
70  data->split_pkts <= 0) {
71  av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
72  return AVERROR_INVALIDDATA;
73  }
74  pkt_len = AV_RB16(data->split_buf + data->split_pos);
75  data->split_pos += 2;
76  if (pkt_len > data->split_buf_len - data->split_pos) {
77  av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
78  return AVERROR_INVALIDDATA;
79  }
80  if (av_new_packet(pkt, pkt_len)) {
81  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
82  return AVERROR(ENOMEM);
83  }
84  pkt->stream_index = st->index;
85  memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
86  data->split_pos += pkt_len;
87  data->split_pkts--;
88  return data->split_pkts > 0;
89  }
90 
91  if (len < 6 || len > INT_MAX/2) {
92  av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
93  return AVERROR_INVALIDDATA;
94  }
95 
96  // read xiph rtp headers
97  ident = AV_RB24(buf);
98  fragmented = buf[3] >> 6;
99  tdt = (buf[3] >> 4) & 3;
100  num_pkts = buf[3] & 0xf;
101  pkt_len = AV_RB16(buf + 4);
102 
103  if (pkt_len > len - 6) {
105  "Invalid packet length %d in %d byte packet\n", pkt_len,
106  len);
107  return AVERROR_INVALIDDATA;
108  }
109 
110  if (ident != data->ident) {
111  avpriv_report_missing_feature(ctx, "Xiph SDP configuration change");
112  return AVERROR_PATCHWELCOME;
113  }
114 
115  if (tdt) {
117  "RTP Xiph packet settings (%d,%d,%d)",
118  fragmented, tdt, num_pkts);
119  return AVERROR_PATCHWELCOME;
120  }
121 
122  buf += 6; // move past header bits
123  len -= 6;
124 
125  if (fragmented == 0) {
126  if (av_new_packet(pkt, pkt_len)) {
127  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
128  return AVERROR(ENOMEM);
129  }
130  pkt->stream_index = st->index;
131  memcpy(pkt->data, buf, pkt_len);
132  buf += pkt_len;
133  len -= pkt_len;
134  num_pkts--;
135 
136  if (num_pkts > 0) {
137  if (len > data->split_buf_size || !data->split_buf) {
138  av_freep(&data->split_buf);
139  data->split_buf_size = 2 * len;
140  data->split_buf = av_malloc(data->split_buf_size);
141  if (!data->split_buf) {
142  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
144  return AVERROR(ENOMEM);
145  }
146  }
147  memcpy(data->split_buf, buf, len);
148  data->split_buf_len = len;
149  data->split_pos = 0;
150  data->split_pkts = num_pkts;
151  return 1;
152  }
153 
154  return 0;
155 
156  } else if (fragmented == 1) {
157  // start of xiph data fragment
158  int res;
159 
160  // end packet has been lost somewhere, so drop buffered data
161  ffio_free_dyn_buf(&data->fragment);
162 
163  if((res = avio_open_dyn_buf(&data->fragment)) < 0)
164  return res;
165 
166  avio_write(data->fragment, buf, pkt_len);
167  data->timestamp = *timestamp;
168 
169  } else {
170  av_assert1(fragmented < 4);
171  if (data->timestamp != *timestamp) {
172  // skip if fragmented timestamp is incorrect;
173  // a start packet has been lost somewhere
174  ffio_free_dyn_buf(&data->fragment);
175  av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
176  return AVERROR_INVALIDDATA;
177  }
178  if (!data->fragment) {
180  "Received packet without a start fragment; dropping.\n");
181  return AVERROR(EAGAIN);
182  }
183 
184  // copy data to fragment buffer
185  avio_write(data->fragment, buf, pkt_len);
186 
187  if (fragmented == 3) {
188  // end of xiph data packet
189  int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
190  if (ret < 0) {
192  "Error occurred when getting fragment buffer.");
193  return ret;
194  }
195 
196  return 0;
197  }
198  }
199 
200  return AVERROR(EAGAIN);
201 }
202 
203 /**
204  * Length encoding described in RFC5215 section 3.1.1.
205  */
206 static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
207 {
208  int n = 0;
209  for (; *buf < buf_end; ++*buf) {
210  n <<= 7;
211  n += **buf & 0x7f;
212  if (!(**buf & 0x80)) {
213  ++*buf;
214  return n;
215  }
216  }
217  return 0;
218 }
219 
220 /**
221  * Based off parse_packed_headers in Vorbis RTP
222  */
223 static int
225  const uint8_t * packed_headers,
226  const uint8_t * packed_headers_end,
227  AVCodecParameters *par, PayloadContext * xiph_data)
228 {
229 
230  unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
231  uint8_t *ptr;
232 
233  if (packed_headers_end - packed_headers < 9) {
235  "Invalid %"PTRDIFF_SPECIFIER" byte packed header.",
236  packed_headers_end - packed_headers);
237  return AVERROR_INVALIDDATA;
238  }
239 
240  num_packed = bytestream_get_be32(&packed_headers);
241  xiph_data->ident = bytestream_get_be24(&packed_headers);
242  length = bytestream_get_be16(&packed_headers);
243  num_headers = get_base128(&packed_headers, packed_headers_end);
244  length1 = get_base128(&packed_headers, packed_headers_end);
245  length2 = get_base128(&packed_headers, packed_headers_end);
246 
247  if (num_packed != 1 || num_headers > 3) {
248  avpriv_report_missing_feature(s, "%u packed headers, %u headers",
249  num_packed, num_headers);
250  return AVERROR_PATCHWELCOME;
251  }
252 
253  if (packed_headers_end - packed_headers != length ||
254  length1 > length || length2 > length - length1) {
256  "Bad packed header lengths (%d,%d,%"PTRDIFF_SPECIFIER",%u)\n", length1,
257  length2, packed_headers_end - packed_headers, length);
258  return AVERROR_INVALIDDATA;
259  }
260 
261  /* allocate extra space:
262  * -- length/255 +2 for xiphlacing
263  * -- one for the '2' marker
264  * -- AV_INPUT_BUFFER_PADDING_SIZE required */
265  extradata_alloc = length + length/255 + 3 + AV_INPUT_BUFFER_PADDING_SIZE;
266 
267  if (ff_alloc_extradata(par, extradata_alloc)) {
268  av_log(s, AV_LOG_ERROR, "Out of memory\n");
269  return AVERROR(ENOMEM);
270  }
271  ptr = par->extradata;
272  *ptr++ = 2;
273  ptr += av_xiphlacing(ptr, length1);
274  ptr += av_xiphlacing(ptr, length2);
275  memcpy(ptr, packed_headers, length);
276  ptr += length;
277  par->extradata_size = ptr - par->extradata;
278  // clear out remaining parts of the buffer
279  memset(ptr, 0, extradata_alloc - par->extradata_size);
280 
281  return 0;
282 }
283 
285  AVStream* stream,
286  PayloadContext *xiph_data,
287  const char *attr, const char *value)
288 {
289  AVCodecParameters *par = stream->codecpar;
290  int result = 0;
291 
292  if (!strcmp(attr, "sampling")) {
293  if (!strcmp(value, "YCbCr-4:2:0")) {
294  par->format = AV_PIX_FMT_YUV420P;
295  } else if (!strcmp(value, "YCbCr-4:4:2")) {
296  par->format = AV_PIX_FMT_YUV422P;
297  } else if (!strcmp(value, "YCbCr-4:4:4")) {
298  par->format = AV_PIX_FMT_YUV444P;
299  } else {
301  "Unsupported pixel format %s\n", attr);
302  return AVERROR_INVALIDDATA;
303  }
304  } else if (!strcmp(attr, "width")) {
305  /* This is an integer between 1 and 1048561
306  * and MUST be in multiples of 16. */
307  par->width = atoi(value);
308  return 0;
309  } else if (!strcmp(attr, "height")) {
310  /* This is an integer between 1 and 1048561
311  * and MUST be in multiples of 16. */
312  par->height = atoi(value);
313  return 0;
314  } else if (!strcmp(attr, "delivery-method")) {
315  /* Possible values are: inline, in_band, out_band/specific_name. */
316  return AVERROR_PATCHWELCOME;
317  } else if (!strcmp(attr, "configuration-uri")) {
318  /* NOTE: configuration-uri is supported only under 2 conditions:
319  *--after the delivery-method tag
320  * --with a delivery-method value of out_band */
321  return AVERROR_PATCHWELCOME;
322  } else if (!strcmp(attr, "configuration")) {
323  /* NOTE: configuration is supported only AFTER the delivery-method tag
324  * The configuration value is a base64 encoded packed header */
325  uint8_t *decoded_packet = NULL;
326  int packet_size;
327  size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
328 
329  if (decoded_alloc <= INT_MAX) {
330  decoded_packet = av_malloc(decoded_alloc);
331  if (decoded_packet) {
332  packet_size =
333  av_base64_decode(decoded_packet, value, decoded_alloc);
334 
336  (s, decoded_packet, decoded_packet + packet_size, par,
337  xiph_data);
338  } else {
340  "Out of memory while decoding SDP configuration.\n");
341  result = AVERROR(ENOMEM);
342  }
343  } else {
344  av_log(s, AV_LOG_ERROR, "Packet too large\n");
346  }
347  av_free(decoded_packet);
348  }
349  return result;
350 }
351 
352 static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
353  PayloadContext *data, const char *line)
354 {
355  const char *p;
356 
357  if (st_index < 0)
358  return 0;
359 
360  if (av_strstart(line, "fmtp:", &p)) {
361  return ff_parse_fmtp(s, s->streams[st_index], data, p,
363  }
364 
365  return 0;
366 }
367 
369  .enc_name = "theora",
370  .codec_type = AVMEDIA_TYPE_VIDEO,
371  .codec_id = AV_CODEC_ID_THEORA,
372  .priv_data_size = sizeof(PayloadContext),
373  .parse_sdp_a_line = xiph_parse_sdp_line,
374  .close = xiph_close_context,
376 };
377 
379  .enc_name = "vorbis",
380  .codec_type = AVMEDIA_TYPE_AUDIO,
381  .codec_id = AV_CODEC_ID_VORBIS,
382  .need_parsing = AVSTREAM_PARSE_HEADERS,
383  .priv_data_size = sizeof(PayloadContext),
384  .parse_sdp_a_line = xiph_parse_sdp_line,
385  .close = xiph_close_context,
387 };
get_base128
static int get_base128(const uint8_t **buf, const uint8_t *buf_end)
Length encoding described in RFC5215 section 3.1.1.
Definition: rtpdec_xiph.c:206
ff_theora_dynamic_handler
const RTPDynamicProtocolHandler ff_theora_dynamic_handler
Definition: rtpdec_xiph.c:368
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3971
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_xiphlacing
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
Encode extradata length to a buffer.
Definition: utils.c:1803
PayloadContext::split_buf_size
int split_buf_size
Definition: rtpdec_mpa_robust.c:33
xiph_parse_sdp_line
static int xiph_parse_sdp_line(AVFormatContext *s, int st_index, PayloadContext *data, const char *line)
Definition: rtpdec_xiph.c:352
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3949
n
int n
Definition: avisynth_c.h:760
rtpdec_formats.h
ff_parse_fmtp
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:889
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
data
const char data[16]
Definition: mxf.c:91
parse_packet
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
Parse a packet, add all split parts to parse_queue.
Definition: utils.c:1451
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
ff_rtp_finalize_packet
int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
Close the dynamic buffer and make a packet from it.
Definition: rtpdec.c:927
PayloadContext::timestamp
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
RTPDynamicProtocolHandler::enc_name
const char * enc_name
Definition: rtpdec.h:116
parse_packed_headers
static int parse_packed_headers(AVFormatContext *s, const uint8_t *packed_headers, const uint8_t *packed_headers_end, AVCodecParameters *par, PayloadContext *xiph_data)
Based off parse_packed_headers in Vorbis RTP.
Definition: rtpdec_xiph.c:224
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
PayloadContext::split_buf
uint8_t * split_buf
Definition: rtpdec_mpa_robust.c:32
buf
void * buf
Definition: avisynth_c.h:766
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1386
PayloadContext::fragment
AVIOContext * fragment
buffer for split payloads
Definition: rtpdec_ac3.c:32
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
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
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: avcodec.h:4023
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
PayloadContext::ident
unsigned ident
24-bit stream configuration identifier
Definition: rtpdec_xiph.c:45
PTRDIFF_SPECIFIER
#define PTRDIFF_SPECIFIER
Definition: internal.h:263
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
PayloadContext::split_buf_len
int split_buf_len
Definition: rtpdec_xiph.c:49
av_base64_decode
int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
Decode a base64-encoded string.
Definition: base64.c:79
base64.h
rtpdec.h
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3975
xiph_close_context
static void xiph_close_context(PayloadContext *data)
Definition: rtpdec_xiph.c:53
PayloadContext::split_pos
int split_pos
Definition: rtpdec_mpa_robust.c:33
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
line
Definition: graph2dot.c:48
attributes.h
av_strstart
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
ff_vorbis_dynamic_handler
const RTPDynamicProtocolHandler ff_vorbis_dynamic_handler
Definition: rtpdec_xiph.c:378
avio_internal.h
AV_CODEC_ID_THEORA
@ AV_CODEC_ID_THEORA
Definition: avcodec.h:248
AVCodecParameters::height
int height
Definition: avcodec.h:4024
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
uint8_t
uint8_t
Definition: audio_convert.c:194
PayloadContext::split_pkts
int split_pkts
Definition: rtpdec_mpa_robust.c:33
len
int len
Definition: vorbis_enc_data.h:452
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1445
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:870
AVSTREAM_PARSE_HEADERS
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition: avformat.h:792
xiph_handle_packet
static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, uint16_t seq, int flags)
Definition: rtpdec_xiph.c:60
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: avcodec.h:790
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:871
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
AVCodecParameters::format
int format
Definition: avcodec.h:3981
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
bytestream.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
length
const char int length
Definition: avisynth_c.h:860
AV_CODEC_ID_VORBIS
@ AV_CODEC_ID_VORBIS
Definition: avcodec.h:569
avstring.h
PayloadContext
RTP/JPEG specific private data.
Definition: rdt.c:83
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:93
xiph_parse_fmtp_pair
static int xiph_parse_fmtp_pair(AVFormatContext *s, AVStream *stream, PayloadContext *xiph_data, const char *attr, const char *value)
Definition: rtpdec_xiph.c:284
RTPDynamicProtocolHandler
Definition: rtpdec.h:115
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:94
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:3309