3 * Copyright (c) 2009 Colin McQuillian
4 * Copyright (c) 2010 Josh Allmann
6 * This file is part of Libav.
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.
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.
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
25 * @brief Xiph / RTP Code
26 * @author Colin McQuillan <m.niloc@gmail.com>
27 * @author Josh Allmann <joshua.allmann@gmail.com>
30 #include "libavutil/avstring.h"
31 #include "libavutil/base64.h"
32 #include "libavcodec/bytestream.h"
37 #include "rtpdec_formats.h"
40 * RTP/Xiph specific private data.
42 struct PayloadContext {
43 unsigned ident; ///< 24-bit stream configuration identifier
45 AVIOContext* fragment; ///< buffer for split payloads
47 int split_pos, split_buf_len, split_buf_size;
51 static PayloadContext *xiph_new_context(void)
53 return av_mallocz(sizeof(PayloadContext));
56 static inline void free_fragment_if_needed(PayloadContext * data)
60 avio_close_dyn_buf(data->fragment, &p);
62 data->fragment = NULL;
66 static void xiph_free_context(PayloadContext * data)
68 free_fragment_if_needed(data);
69 av_free(data->split_buf);
73 static int xiph_handle_packet(AVFormatContext * ctx,
74 PayloadContext * data,
78 const uint8_t * buf, int len, int flags)
81 int ident, fragmented, tdt, num_pkts, pkt_len;
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;
89 pkt_len = AV_RB16(data->split_buf + data->split_pos);
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;
95 if (av_new_packet(pkt, pkt_len)) {
96 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
97 return AVERROR(ENOMEM);
99 pkt->stream_index = st->index;
100 memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
101 data->split_pos += pkt_len;
103 return data->split_pkts > 0;
107 av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
108 return AVERROR_INVALIDDATA;
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);
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,
122 return AVERROR_INVALIDDATA;
125 if (ident != data->ident) {
126 av_log(ctx, AV_LOG_ERROR,
127 "Unimplemented Xiph SDP configuration change detected\n");
128 return AVERROR_PATCHWELCOME;
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;
138 buf += 6; // move past header bits
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);
146 pkt->stream_index = st->index;
147 memcpy(pkt->data, buf, pkt_len);
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");
160 return AVERROR(ENOMEM);
163 memcpy(data->split_buf, buf, len);
164 data->split_buf_len = len;
166 data->split_pkts = num_pkts;
172 } else if (fragmented == 1) {
173 // start of xiph data fragment
176 // end packet has been lost somewhere, so drop buffered data
177 free_fragment_if_needed(data);
179 if((res = avio_open_dyn_buf(&data->fragment)) < 0)
182 avio_write(data->fragment, buf, pkt_len);
183 data->timestamp = *timestamp;
186 assert(fragmented < 4);
187 if (data->timestamp != *timestamp) {
188 // skip if fragmented timestamp is incorrect;
189 // a start packet has been lost somewhere
190 free_fragment_if_needed(data);
191 av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
192 return AVERROR_INVALIDDATA;
194 if (!data->fragment) {
195 av_log(ctx, AV_LOG_WARNING,
196 "Received packet without a start fragment; dropping.\n");
197 return AVERROR(EAGAIN);
200 // copy data to fragment buffer
201 avio_write(data->fragment, buf, pkt_len);
203 if (fragmented == 3) {
204 // end of xiph data packet
206 pkt->size = avio_close_dyn_buf(data->fragment, &pkt->data);
209 av_log(ctx, AV_LOG_ERROR,
210 "Error occurred when getting fragment buffer.");
214 pkt->stream_index = st->index;
215 pkt->destruct = av_destruct_packet;
217 data->fragment = NULL;
223 return AVERROR(EAGAIN);
227 * Length encoding described in RFC5215 section 3.1.1.
229 static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
232 for (; *buf < buf_end; ++*buf) {
235 if (!(**buf & 0x80)) {
244 * Based off parse_packed_headers in Vorbis RTP
247 parse_packed_headers(const uint8_t * packed_headers,
248 const uint8_t * packed_headers_end,
249 AVCodecContext * codec, PayloadContext * xiph_data)
252 unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
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;
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);
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;
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;
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;
290 ptr = codec->extradata = av_malloc(extradata_alloc);
292 av_log(codec, AV_LOG_ERROR, "Out of memory\n");
293 return AVERROR(ENOMEM);
296 ptr += av_xiphlacing(ptr, length1);
297 ptr += av_xiphlacing(ptr, length2);
298 memcpy(ptr, packed_headers, 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);
307 static int xiph_parse_fmtp_pair(AVStream* stream,
308 PayloadContext *xiph_data,
309 char *attr, char *value)
311 AVCodecContext *codec = stream->codec;
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;
322 av_log(codec, AV_LOG_ERROR,
323 "Unsupported pixel format %s\n", attr);
324 return AVERROR_INVALIDDATA;
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);
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);
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;
349 size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
351 if (decoded_alloc <= INT_MAX) {
352 decoded_packet = av_malloc(decoded_alloc);
353 if (decoded_packet) {
355 av_base64_decode(decoded_packet, value, decoded_alloc);
357 result = parse_packed_headers
358 (decoded_packet, decoded_packet + packet_size, codec,
361 av_log(codec, AV_LOG_ERROR,
362 "Out of memory while decoding SDP configuration.\n");
363 result = AVERROR(ENOMEM);
366 av_log(codec, AV_LOG_ERROR, "Packet too large\n");
367 result = AVERROR_INVALIDDATA;
369 av_free(decoded_packet);
374 static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
375 PayloadContext *data, const char *line)
382 if (av_strstart(line, "fmtp:", &p)) {
383 return ff_parse_fmtp(s->streams[st_index], data, p,
384 xiph_parse_fmtp_pair);
390 RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
391 .enc_name = "theora",
392 .codec_type = AVMEDIA_TYPE_VIDEO,
393 .codec_id = CODEC_ID_THEORA,
394 .parse_sdp_a_line = xiph_parse_sdp_line,
395 .alloc = xiph_new_context,
396 .free = xiph_free_context,
397 .parse_packet = xiph_handle_packet
400 RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
401 .enc_name = "vorbis",
402 .codec_type = AVMEDIA_TYPE_AUDIO,
403 .codec_id = CODEC_ID_VORBIS,
404 .parse_sdp_a_line = xiph_parse_sdp_line,
405 .alloc = xiph_new_context,
406 .free = xiph_free_context,
407 .parse_packet = xiph_handle_packet