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