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;
46  uint8_t *split_buf;
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 
75  AVStream * st,
76  AVPacket * pkt,
77  uint32_t * timestamp,
78  const uint8_t * buf, int len, int flags)
79 {
80 
81  int ident, fragmented, tdt, num_pkts, pkt_len;
82 
83  if (!buf) {
84  if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
85  data->split_pkts <= 0) {
86  av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
87  return AVERROR_INVALIDDATA;
88  }
89  pkt_len = AV_RB16(data->split_buf + data->split_pos);
90  data->split_pos += 2;
91  if (data->split_pos + pkt_len > data->split_buf_len) {
92  av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
93  return AVERROR_INVALIDDATA;
94  }
95  if (av_new_packet(pkt, pkt_len)) {
96  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
97  return AVERROR(ENOMEM);
98  }
99  pkt->stream_index = st->index;
100  memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
101  data->split_pos += pkt_len;
102  data->split_pkts--;
103  return data->split_pkts > 0;
104  }
105 
106  if (len < 6) {
107  av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
108  return AVERROR_INVALIDDATA;
109  }
110 
111  // read xiph rtp headers
112  ident = AV_RB24(buf);
113  fragmented = buf[3] >> 6;
114  tdt = (buf[3] >> 4) & 3;
115  num_pkts = buf[3] & 0xf;
116  pkt_len = AV_RB16(buf + 4);
117 
118  if (pkt_len > len - 6) {
119  av_log(ctx, AV_LOG_ERROR,
120  "Invalid packet length %d in %d byte packet\n", pkt_len,
121  len);
122  return AVERROR_INVALIDDATA;
123  }
124 
125  if (ident != data->ident) {
126  av_log(ctx, AV_LOG_ERROR,
127  "Unimplemented Xiph SDP configuration change detected\n");
128  return AVERROR_PATCHWELCOME;
129  }
130 
131  if (tdt) {
132  av_log(ctx, AV_LOG_ERROR,
133  "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
134  fragmented, tdt, num_pkts);
135  return AVERROR_PATCHWELCOME;
136  }
137 
138  buf += 6; // move past header bits
139  len -= 6;
140 
141  if (fragmented == 0) {
142  if (av_new_packet(pkt, pkt_len)) {
143  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
144  return AVERROR(ENOMEM);
145  }
146  pkt->stream_index = st->index;
147  memcpy(pkt->data, buf, pkt_len);
148  buf += pkt_len;
149  len -= pkt_len;
150  num_pkts--;
151 
152  if (num_pkts > 0) {
153  if (len > data->split_buf_size || !data->split_buf) {
154  av_freep(&data->split_buf);
155  data->split_buf_size = 2 * len;
156  data->split_buf = av_malloc(data->split_buf_size);
157  if (!data->split_buf) {
158  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
159  av_free_packet(pkt);
160  return AVERROR(ENOMEM);
161  }
162  }
163  memcpy(data->split_buf, buf, len);
164  data->split_buf_len = len;
165  data->split_pos = 0;
166  data->split_pkts = num_pkts;
167  return 1;
168  }
169 
170  return 0;
171 
172  } else if (fragmented == 1) {
173  // start of xiph data fragment
174  int res;
175 
176  // end packet has been lost somewhere, so drop buffered data
178 
179  if((res = avio_open_dyn_buf(&data->fragment)) < 0)
180  return res;
181 
182  avio_write(data->fragment, buf, pkt_len);
183  data->timestamp = *timestamp;
184 
185  } else {
186  assert(fragmented < 4);
187  if (data->timestamp != *timestamp) {
188  // skip if fragmented timestamp is incorrect;
189  // a start packet has been lost somewhere
191  av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
192  return AVERROR_INVALIDDATA;
193  }
194  if (!data->fragment) {
195  av_log(ctx, AV_LOG_WARNING,
196  "Received packet without a start fragment; dropping.\n");
197  return AVERROR(EAGAIN);
198  }
199 
200  // copy data to fragment buffer
201  avio_write(data->fragment, buf, pkt_len);
202 
203  if (fragmented == 3) {
204  // end of xiph data packet
205  av_init_packet(pkt);
206  pkt->size = avio_close_dyn_buf(data->fragment, &pkt->data);
207 
208  if (pkt->size < 0) {
209  av_log(ctx, AV_LOG_ERROR,
210  "Error occurred when getting fragment buffer.");
211  return pkt->size;
212  }
213 
214  pkt->stream_index = st->index;
216 
217  data->fragment = NULL;
218 
219  return 0;
220  }
221  }
222 
223  return AVERROR(EAGAIN);
224 }
225 
229 static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
230 {
231  int n = 0;
232  for (; *buf < buf_end; ++*buf) {
233  n <<= 7;
234  n += **buf & 0x7f;
235  if (!(**buf & 0x80)) {
236  ++*buf;
237  return n;
238  }
239  }
240  return 0;
241 }
242 
246 static unsigned int
247 parse_packed_headers(const uint8_t * packed_headers,
248  const uint8_t * packed_headers_end,
249  AVCodecContext * codec, PayloadContext * xiph_data)
250 {
251 
252  unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
253  uint8_t *ptr;
254 
255  if (packed_headers_end - packed_headers < 9) {
256  av_log(codec, AV_LOG_ERROR,
257  "Invalid %td byte packed header.",
258  packed_headers_end - packed_headers);
259  return AVERROR_INVALIDDATA;
260  }
261 
262  num_packed = bytestream_get_be32(&packed_headers);
263  xiph_data->ident = bytestream_get_be24(&packed_headers);
264  length = bytestream_get_be16(&packed_headers);
265  num_headers = get_base128(&packed_headers, packed_headers_end);
266  length1 = get_base128(&packed_headers, packed_headers_end);
267  length2 = get_base128(&packed_headers, packed_headers_end);
268 
269  if (num_packed != 1 || num_headers > 3) {
270  av_log(codec, AV_LOG_ERROR,
271  "Unimplemented number of headers: %d packed headers, %d headers\n",
272  num_packed, num_headers);
273  return AVERROR_PATCHWELCOME;
274  }
275 
276  if (packed_headers_end - packed_headers != length ||
277  length1 > length || length2 > length - length1) {
278  av_log(codec, AV_LOG_ERROR,
279  "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
280  length2, packed_headers_end - packed_headers, length);
281  return AVERROR_INVALIDDATA;
282  }
283 
284  /* allocate extra space:
285  * -- length/255 +2 for xiphlacing
286  * -- one for the '2' marker
287  * -- FF_INPUT_BUFFER_PADDING_SIZE required */
288  extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;
289 
290  ptr = codec->extradata = av_malloc(extradata_alloc);
291  if (!ptr) {
292  av_log(codec, AV_LOG_ERROR, "Out of memory\n");
293  return AVERROR(ENOMEM);
294  }
295  *ptr++ = 2;
296  ptr += av_xiphlacing(ptr, length1);
297  ptr += av_xiphlacing(ptr, length2);
298  memcpy(ptr, packed_headers, length);
299  ptr += length;
300  codec->extradata_size = ptr - codec->extradata;
301  // clear out remaining parts of the buffer
302  memset(ptr, 0, extradata_alloc - codec->extradata_size);
303 
304  return 0;
305 }
306 
307 static int xiph_parse_fmtp_pair(AVStream* stream,
308  PayloadContext *xiph_data,
309  char *attr, char *value)
310 {
311  AVCodecContext *codec = stream->codec;
312  int result = 0;
313 
314  if (!strcmp(attr, "sampling")) {
315  if (!strcmp(value, "YCbCr-4:2:0")) {
316  codec->pix_fmt = PIX_FMT_YUV420P;
317  } else if (!strcmp(value, "YCbCr-4:4:2")) {
318  codec->pix_fmt = PIX_FMT_YUV422P;
319  } else if (!strcmp(value, "YCbCr-4:4:4")) {
320  codec->pix_fmt = PIX_FMT_YUV444P;
321  } else {
322  av_log(codec, AV_LOG_ERROR,
323  "Unsupported pixel format %s\n", attr);
324  return AVERROR_INVALIDDATA;
325  }
326  } else if (!strcmp(attr, "width")) {
327  /* This is an integer between 1 and 1048561
328  * and MUST be in multiples of 16. */
329  codec->width = atoi(value);
330  return 0;
331  } else if (!strcmp(attr, "height")) {
332  /* This is an integer between 1 and 1048561
333  * and MUST be in multiples of 16. */
334  codec->height = atoi(value);
335  return 0;
336  } else if (!strcmp(attr, "delivery-method")) {
337  /* Possible values are: inline, in_band, out_band/specific_name. */
338  return AVERROR_PATCHWELCOME;
339  } else if (!strcmp(attr, "configuration-uri")) {
340  /* NOTE: configuration-uri is supported only under 2 conditions:
341  *--after the delivery-method tag
342  * --with a delivery-method value of out_band */
343  return AVERROR_PATCHWELCOME;
344  } else if (!strcmp(attr, "configuration")) {
345  /* NOTE: configuration is supported only AFTER the delivery-method tag
346  * The configuration value is a base64 encoded packed header */
347  uint8_t *decoded_packet = NULL;
348  int packet_size;
349  size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
350 
351  if (decoded_alloc <= INT_MAX) {
352  decoded_packet = av_malloc(decoded_alloc);
353  if (decoded_packet) {
354  packet_size =
355  av_base64_decode(decoded_packet, value, decoded_alloc);
356 
357  result = parse_packed_headers
358  (decoded_packet, decoded_packet + packet_size, codec,
359  xiph_data);
360  } else {
361  av_log(codec, AV_LOG_ERROR,
362  "Out of memory while decoding SDP configuration.\n");
363  result = AVERROR(ENOMEM);
364  }
365  } else {
366  av_log(codec, AV_LOG_ERROR, "Packet too large\n");
367  result = AVERROR_INVALIDDATA;
368  }
369  av_free(decoded_packet);
370  }
371  return result;
372 }
373 
374 static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
375  PayloadContext *data, const char *line)
376 {
377  const char *p;
378 
379  if (av_strstart(line, "fmtp:", &p)) {
380  return ff_parse_fmtp(s->streams[st_index], data, p,
382  }
383 
384  return 0;
385 }
386 
388  .enc_name = "theora",
389  .codec_type = AVMEDIA_TYPE_VIDEO,
390  .codec_id = CODEC_ID_THEORA,
391  .parse_sdp_a_line = xiph_parse_sdp_line,
392  .alloc = xiph_new_context,
393  .free = xiph_free_context,
394  .parse_packet = xiph_handle_packet
395 };
396 
398  .enc_name = "vorbis",
399  .codec_type = AVMEDIA_TYPE_AUDIO,
400  .codec_id = CODEC_ID_VORBIS,
401  .parse_sdp_a_line = xiph_parse_sdp_line,
402  .alloc = xiph_new_context,
403  .free = xiph_free_context,
404  .parse_packet = xiph_handle_packet
405 };
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:68
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:154
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static PayloadContext * xiph_new_context(void)
Definition: rtpdec_xiph.c:51
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_WB24 AV_RB16
Definition: bytestream.h:89
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1196
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
RTP/H264 specific private data.
Definition: rdt.c:83
int index
stream index in AVFormatContext
Definition: avformat.h:621
int size
Definition: avcodec.h:909
RTPDynamicProtocolHandler ff_theora_dynamic_handler
Definition: rtpdec_xiph.c:387
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1184
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:147
Format I/O context.
Definition: avformat.h:863
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:46
static int xiph_parse_sdp_line(AVFormatContext *s, int st_index, PayloadContext *data, const char *line)
Definition: rtpdec_xiph.c:374
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:64
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
AVStream ** streams
Definition: avformat.h:908
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:151
void av_destruct_packet(AVPacket *pkt)
Default packet destructor.
Definition: avpacket.c:33
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:190
AVIOContext * fragment
buffer for split payloads
Definition: rtpdec_xiph.c:45
void(* destruct)(struct AVPacket *)
Definition: avcodec.h:931
unsigned ident
24-bit stream configuration identifier
Definition: rtpdec_xiph.c:43
uint32_t timestamp
timestamp of next-to-be-returned packet
Definition: rtpdec_latm.c:31
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:137
#define AVERROR(e)
Definition: error.h:43
Definition: graph2dot.c:39
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char enc_name[50]
Definition: rtpdec.h:117
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:69
AVCodecContext * codec
codec context
Definition: avformat.h:623
int width
picture width / height.
Definition: avcodec.h:1408
static unsigned 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:247
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:229
static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, int flags)
Definition: rtpdec_xiph.c:73
Stream structure.
Definition: avformat.h:620
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:50
main external API structure.
Definition: avcodec.h:1329
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
int extradata_size
Definition: avcodec.h:1388
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:64
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
Encode extradata length to a buffer.
Definition: utils.c:1694
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:497
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_RB24
Definition: bytestream.h:89
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:29
RTPDynamicProtocolHandler ff_vorbis_dynamic_handler
Definition: rtpdec_xiph.c:397
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:757
int stream_index
Definition: avcodec.h:910
static int xiph_parse_fmtp_pair(AVStream *stream, PayloadContext *xiph_data, char *attr, char *value)
Definition: rtpdec_xiph.c:307