3 * Copyright (c) 2009 Colin McQuillian
4 * Copyright (c) 2010 Josh Allmann
6 * This file is part of FFmpeg.
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.
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.
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
25 * @brief Xiph / RTP Code
26 * @author Colin McQuillan <m.niloc@gmail.com>
27 * @author Josh Allmann <joshua.allmann@gmail.com>
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/base64.h"
33 #include "libavcodec/bytestream.h"
36 #include "rtpdec_formats.h"
39 * RTP/Xiph specific private data.
41 struct PayloadContext {
42 unsigned ident; ///< 24-bit stream configuration identifier
44 AVIOContext* fragment; ///< buffer for split payloads
46 int split_pos, split_buf_len, split_buf_size;
50 static PayloadContext *xiph_new_context(void)
52 return av_mallocz(sizeof(PayloadContext));
55 static inline void free_fragment_if_needed(PayloadContext * data)
59 avio_close_dyn_buf(data->fragment, &p);
61 data->fragment = NULL;
65 static void xiph_free_context(PayloadContext * data)
67 free_fragment_if_needed(data);
68 av_free(data->split_buf);
72 static int xiph_handle_packet(AVFormatContext * ctx,
73 PayloadContext * data,
77 const uint8_t * buf, int len, int flags)
80 int ident, fragmented, tdt, num_pkts, pkt_len;
83 if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
84 data->split_pkts <= 0) {
85 av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
86 return AVERROR_INVALIDDATA;
88 pkt_len = AV_RB16(data->split_buf + data->split_pos);
90 if (data->split_pos + pkt_len > data->split_buf_len) {
91 av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
92 return AVERROR_INVALIDDATA;
94 if (av_new_packet(pkt, pkt_len)) {
95 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
96 return AVERROR(ENOMEM);
98 pkt->stream_index = st->index;
99 memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
100 data->split_pos += pkt_len;
102 return data->split_pkts > 0;
106 av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
107 return AVERROR_INVALIDDATA;
110 // read xiph rtp headers
111 ident = AV_RB24(buf);
112 fragmented = buf[3] >> 6;
113 tdt = (buf[3] >> 4) & 3;
114 num_pkts = buf[3] & 0xf;
115 pkt_len = AV_RB16(buf + 4);
117 if (pkt_len > len - 6) {
118 av_log(ctx, AV_LOG_ERROR,
119 "Invalid packet length %d in %d byte packet\n", pkt_len,
121 return AVERROR_INVALIDDATA;
124 if (ident != data->ident) {
125 av_log(ctx, AV_LOG_ERROR,
126 "Unimplemented Xiph SDP configuration change detected\n");
127 return AVERROR_PATCHWELCOME;
131 av_log(ctx, AV_LOG_ERROR,
132 "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
133 fragmented, tdt, num_pkts);
134 return AVERROR_PATCHWELCOME;
137 buf += 6; // move past header bits
140 if (fragmented == 0) {
141 if (av_new_packet(pkt, pkt_len)) {
142 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
143 return AVERROR(ENOMEM);
145 pkt->stream_index = st->index;
146 memcpy(pkt->data, buf, pkt_len);
152 if (len > data->split_buf_size || !data->split_buf) {
153 av_freep(&data->split_buf);
154 data->split_buf_size = 2 * len;
155 data->split_buf = av_malloc(data->split_buf_size);
156 if (!data->split_buf) {
157 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
159 return AVERROR(ENOMEM);
162 memcpy(data->split_buf, buf, len);
163 data->split_buf_len = len;
165 data->split_pkts = num_pkts;
171 } else if (fragmented == 1) {
172 // start of xiph data fragment
175 // end packet has been lost somewhere, so drop buffered data
176 free_fragment_if_needed(data);
178 if((res = avio_open_dyn_buf(&data->fragment)) < 0)
181 avio_write(data->fragment, buf, pkt_len);
182 data->timestamp = *timestamp;
185 av_assert1(fragmented < 4);
186 if (data->timestamp != *timestamp) {
187 // skip if fragmented timestamp is incorrect;
188 // a start packet has been lost somewhere
189 free_fragment_if_needed(data);
190 av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
191 return AVERROR_INVALIDDATA;
193 if (!data->fragment) {
194 av_log(ctx, AV_LOG_WARNING,
195 "Received packet without a start fragment; dropping.\n");
196 return AVERROR(EAGAIN);
199 // copy data to fragment buffer
200 avio_write(data->fragment, buf, pkt_len);
202 if (fragmented == 3) {
203 // end of xiph data packet
205 pkt->size = avio_close_dyn_buf(data->fragment, &pkt->data);
208 av_log(ctx, AV_LOG_ERROR,
209 "Error occurred when getting fragment buffer.");
213 pkt->stream_index = st->index;
214 pkt->destruct = av_destruct_packet;
216 data->fragment = NULL;
222 return AVERROR(EAGAIN);
226 * Length encoding described in RFC5215 section 3.1.1.
228 static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
231 for (; *buf < buf_end; ++*buf) {
234 if (!(**buf & 0x80)) {
243 * Based off parse_packed_headers in Vorbis RTP
246 parse_packed_headers(const uint8_t * packed_headers,
247 const uint8_t * packed_headers_end,
248 AVCodecContext * codec, PayloadContext * xiph_data)
251 unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
254 if (packed_headers_end - packed_headers < 9) {
255 av_log(codec, AV_LOG_ERROR,
256 "Invalid %td byte packed header.",
257 packed_headers_end - packed_headers);
258 return AVERROR_INVALIDDATA;
261 num_packed = bytestream_get_be32(&packed_headers);
262 xiph_data->ident = bytestream_get_be24(&packed_headers);
263 length = bytestream_get_be16(&packed_headers);
264 num_headers = get_base128(&packed_headers, packed_headers_end);
265 length1 = get_base128(&packed_headers, packed_headers_end);
266 length2 = get_base128(&packed_headers, packed_headers_end);
268 if (num_packed != 1 || num_headers > 3) {
269 av_log(codec, AV_LOG_ERROR,
270 "Unimplemented number of headers: %d packed headers, %d headers\n",
271 num_packed, num_headers);
272 return AVERROR_PATCHWELCOME;
275 if (packed_headers_end - packed_headers != length ||
276 length1 > length || length2 > length - length1) {
277 av_log(codec, AV_LOG_ERROR,
278 "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
279 length2, packed_headers_end - packed_headers, length);
280 return AVERROR_INVALIDDATA;
283 /* allocate extra space:
284 * -- length/255 +2 for xiphlacing
285 * -- one for the '2' marker
286 * -- FF_INPUT_BUFFER_PADDING_SIZE required */
287 extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;
289 ptr = codec->extradata = av_malloc(extradata_alloc);
291 av_log(codec, AV_LOG_ERROR, "Out of memory\n");
292 return AVERROR(ENOMEM);
295 ptr += av_xiphlacing(ptr, length1);
296 ptr += av_xiphlacing(ptr, length2);
297 memcpy(ptr, packed_headers, length);
299 codec->extradata_size = ptr - codec->extradata;
300 // clear out remaining parts of the buffer
301 memset(ptr, 0, extradata_alloc - codec->extradata_size);
306 static int xiph_parse_fmtp_pair(AVStream* stream,
307 PayloadContext *xiph_data,
308 char *attr, char *value)
310 AVCodecContext *codec = stream->codec;
313 if (!strcmp(attr, "sampling")) {
314 if (!strcmp(value, "YCbCr-4:2:0")) {
315 codec->pix_fmt = AV_PIX_FMT_YUV420P;
316 } else if (!strcmp(value, "YCbCr-4:4:2")) {
317 codec->pix_fmt = AV_PIX_FMT_YUV422P;
318 } else if (!strcmp(value, "YCbCr-4:4:4")) {
319 codec->pix_fmt = AV_PIX_FMT_YUV444P;
321 av_log(codec, AV_LOG_ERROR,
322 "Unsupported pixel format %s\n", attr);
323 return AVERROR_INVALIDDATA;
325 } else if (!strcmp(attr, "width")) {
326 /* This is an integer between 1 and 1048561
327 * and MUST be in multiples of 16. */
328 codec->width = atoi(value);
330 } else if (!strcmp(attr, "height")) {
331 /* This is an integer between 1 and 1048561
332 * and MUST be in multiples of 16. */
333 codec->height = atoi(value);
335 } else if (!strcmp(attr, "delivery-method")) {
336 /* Possible values are: inline, in_band, out_band/specific_name. */
337 return AVERROR_PATCHWELCOME;
338 } else if (!strcmp(attr, "configuration-uri")) {
339 /* NOTE: configuration-uri is supported only under 2 conditions:
340 *--after the delivery-method tag
341 * --with a delivery-method value of out_band */
342 return AVERROR_PATCHWELCOME;
343 } else if (!strcmp(attr, "configuration")) {
344 /* NOTE: configuration is supported only AFTER the delivery-method tag
345 * The configuration value is a base64 encoded packed header */
346 uint8_t *decoded_packet = NULL;
348 size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
350 if (decoded_alloc <= INT_MAX) {
351 decoded_packet = av_malloc(decoded_alloc);
352 if (decoded_packet) {
354 av_base64_decode(decoded_packet, value, decoded_alloc);
356 result = parse_packed_headers
357 (decoded_packet, decoded_packet + packet_size, codec,
360 av_log(codec, AV_LOG_ERROR,
361 "Out of memory while decoding SDP configuration.\n");
362 result = AVERROR(ENOMEM);
365 av_log(codec, AV_LOG_ERROR, "Packet too large\n");
366 result = AVERROR_INVALIDDATA;
368 av_free(decoded_packet);
373 static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
374 PayloadContext *data, const char *line)
381 if (av_strstart(line, "fmtp:", &p)) {
382 return ff_parse_fmtp(s->streams[st_index], data, p,
383 xiph_parse_fmtp_pair);
389 RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
390 .enc_name = "theora",
391 .codec_type = AVMEDIA_TYPE_VIDEO,
392 .codec_id = AV_CODEC_ID_THEORA,
393 .parse_sdp_a_line = xiph_parse_sdp_line,
394 .alloc = xiph_new_context,
395 .free = xiph_free_context,
396 .parse_packet = xiph_handle_packet
399 RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
400 .enc_name = "vorbis",
401 .codec_type = AVMEDIA_TYPE_AUDIO,
402 .codec_id = AV_CODEC_ID_VORBIS,
403 .parse_sdp_a_line = xiph_parse_sdp_line,
404 .alloc = xiph_new_context,
405 .free = xiph_free_context,
406 .parse_packet = xiph_handle_packet