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/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/base64.h"
34 #include "libavcodec/bytestream.h"
36 #include "avio_internal.h"
39 #include "rtpdec_formats.h"
42 * RTP/Xiph specific private data.
44 struct PayloadContext {
45 unsigned ident; ///< 24-bit stream configuration identifier
47 AVIOContext* fragment; ///< buffer for split payloads
49 int split_pos, split_buf_len, split_buf_size;
53 static void xiph_close_context(PayloadContext * data)
55 ffio_free_dyn_buf(&data->fragment);
56 av_freep(&data->split_buf);
60 static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data,
61 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
62 const uint8_t *buf, int len, uint16_t seq,
66 int ident, fragmented, tdt, num_pkts, pkt_len;
69 if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
70 data->split_pkts <= 0) {
71 av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
72 return AVERROR_INVALIDDATA;
74 pkt_len = AV_RB16(data->split_buf + data->split_pos);
76 if (pkt_len > data->split_buf_len - data->split_pos) {
77 av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
78 return AVERROR_INVALIDDATA;
80 if (av_new_packet(pkt, pkt_len)) {
81 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
82 return AVERROR(ENOMEM);
84 pkt->stream_index = st->index;
85 memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
86 data->split_pos += pkt_len;
88 return data->split_pkts > 0;
91 if (len < 6 || len > INT_MAX/2) {
92 av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
93 return AVERROR_INVALIDDATA;
96 // read xiph rtp headers
98 fragmented = buf[3] >> 6;
99 tdt = (buf[3] >> 4) & 3;
100 num_pkts = buf[3] & 0xf;
101 pkt_len = AV_RB16(buf + 4);
103 if (pkt_len > len - 6) {
104 av_log(ctx, AV_LOG_ERROR,
105 "Invalid packet length %d in %d byte packet\n", pkt_len,
107 return AVERROR_INVALIDDATA;
110 if (ident != data->ident) {
111 av_log(ctx, AV_LOG_ERROR,
112 "Unimplemented Xiph SDP configuration change detected\n");
113 return AVERROR_PATCHWELCOME;
117 av_log(ctx, AV_LOG_ERROR,
118 "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
119 fragmented, tdt, num_pkts);
120 return AVERROR_PATCHWELCOME;
123 buf += 6; // move past header bits
126 if (fragmented == 0) {
127 if (av_new_packet(pkt, pkt_len)) {
128 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
129 return AVERROR(ENOMEM);
131 pkt->stream_index = st->index;
132 memcpy(pkt->data, buf, pkt_len);
138 if (len > data->split_buf_size || !data->split_buf) {
139 av_freep(&data->split_buf);
140 data->split_buf_size = 2 * len;
141 data->split_buf = av_malloc(data->split_buf_size);
142 if (!data->split_buf) {
143 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
145 return AVERROR(ENOMEM);
148 memcpy(data->split_buf, buf, len);
149 data->split_buf_len = len;
151 data->split_pkts = num_pkts;
157 } else if (fragmented == 1) {
158 // start of xiph data fragment
161 // end packet has been lost somewhere, so drop buffered data
162 ffio_free_dyn_buf(&data->fragment);
164 if((res = avio_open_dyn_buf(&data->fragment)) < 0)
167 avio_write(data->fragment, buf, pkt_len);
168 data->timestamp = *timestamp;
171 av_assert1(fragmented < 4);
172 if (data->timestamp != *timestamp) {
173 // skip if fragmented timestamp is incorrect;
174 // a start packet has been lost somewhere
175 ffio_free_dyn_buf(&data->fragment);
176 av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
177 return AVERROR_INVALIDDATA;
179 if (!data->fragment) {
180 av_log(ctx, AV_LOG_WARNING,
181 "Received packet without a start fragment; dropping.\n");
182 return AVERROR(EAGAIN);
185 // copy data to fragment buffer
186 avio_write(data->fragment, buf, pkt_len);
188 if (fragmented == 3) {
189 // end of xiph data packet
190 int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
192 av_log(ctx, AV_LOG_ERROR,
193 "Error occurred when getting fragment buffer.");
201 return AVERROR(EAGAIN);
205 * Length encoding described in RFC5215 section 3.1.1.
207 static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
210 for (; *buf < buf_end; ++*buf) {
213 if (!(**buf & 0x80)) {
222 * Based off parse_packed_headers in Vorbis RTP
225 parse_packed_headers(const uint8_t * packed_headers,
226 const uint8_t * packed_headers_end,
227 AVCodecContext * codec, PayloadContext * xiph_data)
230 unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
233 if (packed_headers_end - packed_headers < 9) {
234 av_log(codec, AV_LOG_ERROR,
235 "Invalid %"PTRDIFF_SPECIFIER" byte packed header.",
236 packed_headers_end - packed_headers);
237 return AVERROR_INVALIDDATA;
240 num_packed = bytestream_get_be32(&packed_headers);
241 xiph_data->ident = bytestream_get_be24(&packed_headers);
242 length = bytestream_get_be16(&packed_headers);
243 num_headers = get_base128(&packed_headers, packed_headers_end);
244 length1 = get_base128(&packed_headers, packed_headers_end);
245 length2 = get_base128(&packed_headers, packed_headers_end);
247 if (num_packed != 1 || num_headers > 3) {
248 av_log(codec, AV_LOG_ERROR,
249 "Unimplemented number of headers: %d packed headers, %d headers\n",
250 num_packed, num_headers);
251 return AVERROR_PATCHWELCOME;
254 if (packed_headers_end - packed_headers != length ||
255 length1 > length || length2 > length - length1) {
256 av_log(codec, AV_LOG_ERROR,
257 "Bad packed header lengths (%d,%d,%"PTRDIFF_SPECIFIER",%d)\n", length1,
258 length2, packed_headers_end - packed_headers, length);
259 return AVERROR_INVALIDDATA;
262 /* allocate extra space:
263 * -- length/255 +2 for xiphlacing
264 * -- one for the '2' marker
265 * -- FF_INPUT_BUFFER_PADDING_SIZE required */
266 extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;
268 if (ff_alloc_extradata(codec, extradata_alloc)) {
269 av_log(codec, AV_LOG_ERROR, "Out of memory\n");
270 return AVERROR(ENOMEM);
272 ptr = codec->extradata;
274 ptr += av_xiphlacing(ptr, length1);
275 ptr += av_xiphlacing(ptr, length2);
276 memcpy(ptr, packed_headers, length);
278 codec->extradata_size = ptr - codec->extradata;
279 // clear out remaining parts of the buffer
280 memset(ptr, 0, extradata_alloc - codec->extradata_size);
285 static int xiph_parse_fmtp_pair(AVFormatContext *s,
287 PayloadContext *xiph_data,
288 const char *attr, const char *value)
290 AVCodecContext *codec = stream->codec;
293 if (!strcmp(attr, "sampling")) {
294 if (!strcmp(value, "YCbCr-4:2:0")) {
295 codec->pix_fmt = AV_PIX_FMT_YUV420P;
296 } else if (!strcmp(value, "YCbCr-4:4:2")) {
297 codec->pix_fmt = AV_PIX_FMT_YUV422P;
298 } else if (!strcmp(value, "YCbCr-4:4:4")) {
299 codec->pix_fmt = AV_PIX_FMT_YUV444P;
301 av_log(s, AV_LOG_ERROR,
302 "Unsupported pixel format %s\n", attr);
303 return AVERROR_INVALIDDATA;
305 } else if (!strcmp(attr, "width")) {
306 /* This is an integer between 1 and 1048561
307 * and MUST be in multiples of 16. */
308 codec->width = atoi(value);
310 } else if (!strcmp(attr, "height")) {
311 /* This is an integer between 1 and 1048561
312 * and MUST be in multiples of 16. */
313 codec->height = atoi(value);
315 } else if (!strcmp(attr, "delivery-method")) {
316 /* Possible values are: inline, in_band, out_band/specific_name. */
317 return AVERROR_PATCHWELCOME;
318 } else if (!strcmp(attr, "configuration-uri")) {
319 /* NOTE: configuration-uri is supported only under 2 conditions:
320 *--after the delivery-method tag
321 * --with a delivery-method value of out_band */
322 return AVERROR_PATCHWELCOME;
323 } else if (!strcmp(attr, "configuration")) {
324 /* NOTE: configuration is supported only AFTER the delivery-method tag
325 * The configuration value is a base64 encoded packed header */
326 uint8_t *decoded_packet = NULL;
328 size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
330 if (decoded_alloc <= INT_MAX) {
331 decoded_packet = av_malloc(decoded_alloc);
332 if (decoded_packet) {
334 av_base64_decode(decoded_packet, value, decoded_alloc);
336 result = parse_packed_headers
337 (decoded_packet, decoded_packet + packet_size, codec,
340 av_log(s, AV_LOG_ERROR,
341 "Out of memory while decoding SDP configuration.\n");
342 result = AVERROR(ENOMEM);
345 av_log(s, AV_LOG_ERROR, "Packet too large\n");
346 result = AVERROR_INVALIDDATA;
348 av_free(decoded_packet);
353 static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
354 PayloadContext *data, const char *line)
361 if (av_strstart(line, "fmtp:", &p)) {
362 return ff_parse_fmtp(s, s->streams[st_index], data, p,
363 xiph_parse_fmtp_pair);
369 RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
370 .enc_name = "theora",
371 .codec_type = AVMEDIA_TYPE_VIDEO,
372 .codec_id = AV_CODEC_ID_THEORA,
373 .priv_data_size = sizeof(PayloadContext),
374 .parse_sdp_a_line = xiph_parse_sdp_line,
375 .close = xiph_close_context,
376 .parse_packet = xiph_handle_packet,
379 RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
380 .enc_name = "vorbis",
381 .codec_type = AVMEDIA_TYPE_AUDIO,
382 .codec_id = AV_CODEC_ID_VORBIS,
383 .need_parsing = AVSTREAM_PARSE_HEADERS,
384 .priv_data_size = sizeof(PayloadContext),
385 .parse_sdp_a_line = xiph_parse_sdp_line,
386 .close = xiph_close_context,
387 .parse_packet = xiph_handle_packet,