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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
30 #include "libavutil/avstring.h"
31 #include "libavutil/base64.h"
32 #include "libavcodec/bytestream.h"
33 
34 #include <assert.h>
35 
36 #include "rtpdec.h"
37 #include "rtpdec_formats.h"
38 
42 struct PayloadContext {
43  unsigned ident;
44  uint32_t timestamp;
49 };
50 
52 {
53  return av_mallocz(sizeof(PayloadContext));
54 }
55 
57 {
58  if (data->fragment) {
59  uint8_t* p;
60  avio_close_dyn_buf(data->fragment, &p);
61  av_free(p);
62  data->fragment = NULL;
63  }
64 }
65 
67 {
69  av_free(data->split_buf);
70  av_free(data);
71 }
72 
74  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
75  const uint8_t *buf, int len, uint16_t seq,
76  int flags)
77 {
78 
79  int ident, fragmented, tdt, num_pkts, pkt_len;
80 
81  if (!buf) {
82  if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
83  data->split_pkts <= 0) {
84  av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
85  return AVERROR_INVALIDDATA;
86  }
87  pkt_len = AV_RB16(data->split_buf + data->split_pos);
88  data->split_pos += 2;
89  if (data->split_pos + pkt_len > data->split_buf_len) {
90  av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
91  return AVERROR_INVALIDDATA;
92  }
93  if (av_new_packet(pkt, pkt_len)) {
94  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
95  return AVERROR(ENOMEM);
96  }
97  pkt->stream_index = st->index;
98  memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
99  data->split_pos += pkt_len;
100  data->split_pkts--;
101  return data->split_pkts > 0;
102  }
103 
104  if (len < 6) {
105  av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
106  return AVERROR_INVALIDDATA;
107  }
108 
109  // read xiph rtp headers
110  ident = AV_RB24(buf);
111  fragmented = buf[3] >> 6;
112  tdt = (buf[3] >> 4) & 3;
113  num_pkts = buf[3] & 0xf;
114  pkt_len = AV_RB16(buf + 4);
115 
116  if (pkt_len > len - 6) {
117  av_log(ctx, AV_LOG_ERROR,
118  "Invalid packet length %d in %d byte packet\n", pkt_len,
119  len);
120  return AVERROR_INVALIDDATA;
121  }
122 
123  if (ident != data->ident) {
124  av_log(ctx, AV_LOG_ERROR,
125  "Unimplemented Xiph SDP configuration change detected\n");
126  return AVERROR_PATCHWELCOME;
127  }
128 
129  if (tdt) {
130  av_log(ctx, AV_LOG_ERROR,
131  "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
132  fragmented, tdt, num_pkts);
133  return AVERROR_PATCHWELCOME;
134  }
135 
136  buf += 6; // move past header bits
137  len -= 6;
138 
139  if (fragmented == 0) {
140  if (av_new_packet(pkt, pkt_len)) {
141  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
142  return AVERROR(ENOMEM);
143  }
144  pkt->stream_index = st->index;
145  memcpy(pkt->data, buf, pkt_len);
146  buf += pkt_len;
147  len -= pkt_len;
148  num_pkts--;
149 
150  if (num_pkts > 0) {
151  if (len > data->split_buf_size || !data->split_buf) {
152  av_freep(&data->split_buf);
153  data->split_buf_size = 2 * len;
154  data->split_buf = av_malloc(data->split_buf_size);
155  if (!data->split_buf) {
156  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
157  av_free_packet(pkt);
158  return AVERROR(ENOMEM);
159  }
160  }
161  memcpy(data->split_buf, buf, len);
162  data->split_buf_len = len;
163  data->split_pos = 0;
164  data->split_pkts = num_pkts;
165  return 1;
166  }
167 
168  return 0;
169 
170  } else if (fragmented == 1) {
171  // start of xiph data fragment
172  int res;
173 
174  // end packet has been lost somewhere, so drop buffered data
176 
177  if((res = avio_open_dyn_buf(&data->fragment)) < 0)
178  return res;
179 
180  avio_write(data->fragment, buf, pkt_len);
181  data->timestamp = *timestamp;
182 
183  } else {
184  assert(fragmented < 4);
185  if (data->timestamp != *timestamp) {
186  // skip if fragmented timestamp is incorrect;
187  // a start packet has been lost somewhere
189  av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
190  return AVERROR_INVALIDDATA;
191  }
192  if (!data->fragment) {
193  av_log(ctx, AV_LOG_WARNING,
194  "Received packet without a start fragment; dropping.\n");
195  return AVERROR(EAGAIN);
196  }
197 
198  // copy data to fragment buffer
199  avio_write(data->fragment, buf, pkt_len);
200 
201  if (fragmented == 3) {
202  // end of xiph data packet
203  int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
204  if (ret < 0) {
205  av_log(ctx, AV_LOG_ERROR,
206  "Error occurred when getting fragment buffer.");
207  return ret;
208  }
209 
210  return 0;
211  }
212  }
213 
214  return AVERROR(EAGAIN);
215 }
216 
220 static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
221 {
222  int n = 0;
223  for (; *buf < buf_end; ++*buf) {
224  n <<= 7;
225  n += **buf & 0x7f;
226  if (!(**buf & 0x80)) {
227  ++*buf;
228  return n;
229  }
230  }
231  return 0;
232 }
233 
237 static int
238 parse_packed_headers(const uint8_t * packed_headers,
239  const uint8_t * packed_headers_end,
240  AVCodecContext * codec, PayloadContext * xiph_data)
241 {
242 
243  unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
244  uint8_t *ptr;
245 
246  if (packed_headers_end - packed_headers < 9) {
247  av_log(codec, AV_LOG_ERROR,
248  "Invalid %td byte packed header.",
249  packed_headers_end - packed_headers);
250  return AVERROR_INVALIDDATA;
251  }
252 
253  num_packed = bytestream_get_be32(&packed_headers);
254  xiph_data->ident = bytestream_get_be24(&packed_headers);
255  length = bytestream_get_be16(&packed_headers);
256  num_headers = get_base128(&packed_headers, packed_headers_end);
257  length1 = get_base128(&packed_headers, packed_headers_end);
258  length2 = get_base128(&packed_headers, packed_headers_end);
259 
260  if (num_packed != 1 || num_headers > 3) {
261  av_log(codec, AV_LOG_ERROR,
262  "Unimplemented number of headers: %d packed headers, %d headers\n",
263  num_packed, num_headers);
264  return AVERROR_PATCHWELCOME;
265  }
266 
267  if (packed_headers_end - packed_headers != length ||
268  length1 > length || length2 > length - length1) {
269  av_log(codec, AV_LOG_ERROR,
270  "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
271  length2, packed_headers_end - packed_headers, length);
272  return AVERROR_INVALIDDATA;
273  }
274 
275  /* allocate extra space:
276  * -- length/255 +2 for xiphlacing
277  * -- one for the '2' marker
278  * -- FF_INPUT_BUFFER_PADDING_SIZE required */
279  extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;
280 
281  ptr = codec->extradata = av_malloc(extradata_alloc);
282  if (!ptr) {
283  av_log(codec, AV_LOG_ERROR, "Out of memory\n");
284  return AVERROR(ENOMEM);
285  }
286  *ptr++ = 2;
287  ptr += av_xiphlacing(ptr, length1);
288  ptr += av_xiphlacing(ptr, length2);
289  memcpy(ptr, packed_headers, length);
290  ptr += length;
291  codec->extradata_size = ptr - codec->extradata;
292  // clear out remaining parts of the buffer
293  memset(ptr, 0, extradata_alloc - codec->extradata_size);
294 
295  return 0;
296 }
297 
298 static int xiph_parse_fmtp_pair(AVStream* stream,
299  PayloadContext *xiph_data,
300  char *attr, char *value)
301 {
302  AVCodecContext *codec = stream->codec;
303  int result = 0;
304 
305  if (!strcmp(attr, "sampling")) {
306  if (!strcmp(value, "YCbCr-4:2:0")) {
307  codec->pix_fmt = AV_PIX_FMT_YUV420P;
308  } else if (!strcmp(value, "YCbCr-4:4:2")) {
309  codec->pix_fmt = AV_PIX_FMT_YUV422P;
310  } else if (!strcmp(value, "YCbCr-4:4:4")) {
311  codec->pix_fmt = AV_PIX_FMT_YUV444P;
312  } else {
313  av_log(codec, AV_LOG_ERROR,
314  "Unsupported pixel format %s\n", attr);
315  return AVERROR_INVALIDDATA;
316  }
317  } else if (!strcmp(attr, "width")) {
318  /* This is an integer between 1 and 1048561
319  * and MUST be in multiples of 16. */
320  codec->width = atoi(value);
321  return 0;
322  } else if (!strcmp(attr, "height")) {
323  /* This is an integer between 1 and 1048561
324  * and MUST be in multiples of 16. */
325  codec->height = atoi(value);
326  return 0;
327  } else if (!strcmp(attr, "delivery-method")) {
328  /* Possible values are: inline, in_band, out_band/specific_name. */
329  return AVERROR_PATCHWELCOME;
330  } else if (!strcmp(attr, "configuration-uri")) {
331  /* NOTE: configuration-uri is supported only under 2 conditions:
332  *--after the delivery-method tag
333  * --with a delivery-method value of out_band */
334  return AVERROR_PATCHWELCOME;
335  } else if (!strcmp(attr, "configuration")) {
336  /* NOTE: configuration is supported only AFTER the delivery-method tag
337  * The configuration value is a base64 encoded packed header */
338  uint8_t *decoded_packet = NULL;
339  int packet_size;
340  size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
341 
342  if (decoded_alloc <= INT_MAX) {
343  decoded_packet = av_malloc(decoded_alloc);
344  if (decoded_packet) {
345  packet_size =
346  av_base64_decode(decoded_packet, value, decoded_alloc);
347 
348  result = parse_packed_headers
349  (decoded_packet, decoded_packet + packet_size, codec,
350  xiph_data);
351  } else {
352  av_log(codec, AV_LOG_ERROR,
353  "Out of memory while decoding SDP configuration.\n");
354  result = AVERROR(ENOMEM);
355  }
356  } else {
357  av_log(codec, AV_LOG_ERROR, "Packet too large\n");
358  result = AVERROR_INVALIDDATA;
359  }
360  av_free(decoded_packet);
361  }
362  return result;
363 }
364 
365 static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
366  PayloadContext *data, const char *line)
367 {
368  const char *p;
369 
370  if (st_index < 0)
371  return 0;
372 
373  if (av_strstart(line, "fmtp:", &p)) {
374  return ff_parse_fmtp(s->streams[st_index], data, p,
376  }
377 
378  return 0;
379 }
380 
382  .enc_name = "theora",
383  .codec_type = AVMEDIA_TYPE_VIDEO,
384  .codec_id = AV_CODEC_ID_THEORA,
385  .parse_sdp_a_line = xiph_parse_sdp_line,
386  .alloc = xiph_new_context,
387  .free = xiph_free_context,
388  .parse_packet = xiph_handle_packet
389 };
390 
392  .enc_name = "vorbis",
393  .codec_type = AVMEDIA_TYPE_AUDIO,
394  .codec_id = AV_CODEC_ID_VORBIS,
395  .parse_sdp_a_line = xiph_parse_sdp_line,
396  .alloc = xiph_new_context,
397  .free = xiph_free_context,
398  .parse_packet = xiph_handle_packet
399 };
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:61
Bytestream IO Context.
Definition: avio.h:68
static PayloadContext * xiph_new_context(void)
Definition: rtpdec_xiph.c:51
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:153
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:945
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
RTP/JPEG specific private data.
Definition: rdt.c:83
int index
stream index in AVFormatContext
Definition: avformat.h:623
RTPDynamicProtocolHandler ff_theora_dynamic_handler
Definition: rtpdec_xiph.c:381
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:933
static void free_fragment_if_needed(PayloadContext *data)
Definition: rtpdec_xiph.c:56
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:151
Format I/O context.
Definition: avformat.h:828
static int xiph_parse_sdp_line(AVFormatContext *s, int st_index, PayloadContext *data, const char *line)
Definition: rtpdec_xiph.c:365
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
AVStream ** streams
Definition: avformat.h:876
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int flags
Definition: log.c:42
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
AVIOContext * fragment
buffer for split payloads
Definition: rtpdec_xiph.c:45
unsigned ident
24-bit stream configuration identifier
Definition: rtpdec_xiph.c:43
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:56
uint32_t timestamp
current frame timestamp
static void xiph_free_context(PayloadContext *data)
Definition: rtpdec_xiph.c:66
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
#define AV_RB16
Definition: intreadwrite.h:53
Definition: graph2dot.c:48
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
int width
picture width / height.
Definition: avcodec.h:1508
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:220
Stream structure.
Definition: avformat.h:622
NULL
Definition: eval.c:52
main external API structure.
Definition: avcodec.h:1339
static int parse_packed_headers(const uint8_t *packed_headers, const uint8_t *packed_headers_end, AVCodecContext *codec, PayloadContext *xiph_data)
Based off parse_packed_headers in Vorbis RTP.
Definition: rtpdec_xiph.c:238
int extradata_size
Definition: avcodec.h:1455
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
Encode extradata length to a buffer.
Definition: utils.c:1984
const char enc_name[50]
Definition: rtpdec.h:115
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
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
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:73
RTPDynamicProtocolHandler ff_vorbis_dynamic_handler
Definition: rtpdec_xiph.c:391
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:809
int len
uint8_t * split_buf
Definition: rtpdec_xiph.c:46
int av_base64_decode(uint8_t *out, const char *in, int out_size)
Decode a base64-encoded string.
Definition: base64.c:45
int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p, int(*parse_fmtp)(AVStream *stream, PayloadContext *data, char *attr, char *value))
Definition: rtpdec.c:773
int stream_index
Definition: avcodec.h:917
This structure stores compressed data.
Definition: avcodec.h:898
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
static int xiph_parse_fmtp_pair(AVStream *stream, PayloadContext *xiph_data, char *attr, char *value)
Definition: rtpdec_xiph.c:298