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
204 int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
206 av_log(ctx, AV_LOG_ERROR,
207 "Error occurred when getting fragment buffer.");
215 return AVERROR(EAGAIN);
219 * Length encoding described in RFC5215 section 3.1.1.
221 static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
224 for (; *buf < buf_end; ++*buf) {
227 if (!(**buf & 0x80)) {
236 * Based off parse_packed_headers in Vorbis RTP
239 parse_packed_headers(const uint8_t * packed_headers,
240 const uint8_t * packed_headers_end,
241 AVCodecContext * codec, PayloadContext * xiph_data)
244 unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
247 if (packed_headers_end - packed_headers < 9) {
248 av_log(codec, AV_LOG_ERROR,
249 "Invalid %td byte packed header.",
250 packed_headers_end - packed_headers);
251 return AVERROR_INVALIDDATA;
254 num_packed = bytestream_get_be32(&packed_headers);
255 xiph_data->ident = bytestream_get_be24(&packed_headers);
256 length = bytestream_get_be16(&packed_headers);
257 num_headers = get_base128(&packed_headers, packed_headers_end);
258 length1 = get_base128(&packed_headers, packed_headers_end);
259 length2 = get_base128(&packed_headers, packed_headers_end);
261 if (num_packed != 1 || num_headers > 3) {
262 av_log(codec, AV_LOG_ERROR,
263 "Unimplemented number of headers: %d packed headers, %d headers\n",
264 num_packed, num_headers);
265 return AVERROR_PATCHWELCOME;
268 if (packed_headers_end - packed_headers != length ||
269 length1 > length || length2 > length - length1) {
270 av_log(codec, AV_LOG_ERROR,
271 "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
272 length2, packed_headers_end - packed_headers, length);
273 return AVERROR_INVALIDDATA;
276 /* allocate extra space:
277 * -- length/255 +2 for xiphlacing
278 * -- one for the '2' marker
279 * -- FF_INPUT_BUFFER_PADDING_SIZE required */
280 extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;
282 ptr = codec->extradata = av_malloc(extradata_alloc);
284 av_log(codec, AV_LOG_ERROR, "Out of memory\n");
285 return AVERROR(ENOMEM);
288 ptr += av_xiphlacing(ptr, length1);
289 ptr += av_xiphlacing(ptr, length2);
290 memcpy(ptr, packed_headers, length);
292 codec->extradata_size = ptr - codec->extradata;
293 // clear out remaining parts of the buffer
294 memset(ptr, 0, extradata_alloc - codec->extradata_size);
299 static int xiph_parse_fmtp_pair(AVStream* stream,
300 PayloadContext *xiph_data,
301 char *attr, char *value)
303 AVCodecContext *codec = stream->codec;
306 if (!strcmp(attr, "sampling")) {
307 if (!strcmp(value, "YCbCr-4:2:0")) {
308 codec->pix_fmt = AV_PIX_FMT_YUV420P;
309 } else if (!strcmp(value, "YCbCr-4:4:2")) {
310 codec->pix_fmt = AV_PIX_FMT_YUV422P;
311 } else if (!strcmp(value, "YCbCr-4:4:4")) {
312 codec->pix_fmt = AV_PIX_FMT_YUV444P;
314 av_log(codec, AV_LOG_ERROR,
315 "Unsupported pixel format %s\n", attr);
316 return AVERROR_INVALIDDATA;
318 } else if (!strcmp(attr, "width")) {
319 /* This is an integer between 1 and 1048561
320 * and MUST be in multiples of 16. */
321 codec->width = atoi(value);
323 } else if (!strcmp(attr, "height")) {
324 /* This is an integer between 1 and 1048561
325 * and MUST be in multiples of 16. */
326 codec->height = atoi(value);
328 } else if (!strcmp(attr, "delivery-method")) {
329 /* Possible values are: inline, in_band, out_band/specific_name. */
330 return AVERROR_PATCHWELCOME;
331 } else if (!strcmp(attr, "configuration-uri")) {
332 /* NOTE: configuration-uri is supported only under 2 conditions:
333 *--after the delivery-method tag
334 * --with a delivery-method value of out_band */
335 return AVERROR_PATCHWELCOME;
336 } else if (!strcmp(attr, "configuration")) {
337 /* NOTE: configuration is supported only AFTER the delivery-method tag
338 * The configuration value is a base64 encoded packed header */
339 uint8_t *decoded_packet = NULL;
341 size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
343 if (decoded_alloc <= INT_MAX) {
344 decoded_packet = av_malloc(decoded_alloc);
345 if (decoded_packet) {
347 av_base64_decode(decoded_packet, value, decoded_alloc);
349 result = parse_packed_headers
350 (decoded_packet, decoded_packet + packet_size, codec,
353 av_log(codec, AV_LOG_ERROR,
354 "Out of memory while decoding SDP configuration.\n");
355 result = AVERROR(ENOMEM);
358 av_log(codec, AV_LOG_ERROR, "Packet too large\n");
359 result = AVERROR_INVALIDDATA;
361 av_free(decoded_packet);
366 static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
367 PayloadContext *data, const char *line)
374 if (av_strstart(line, "fmtp:", &p)) {
375 return ff_parse_fmtp(s->streams[st_index], data, p,
376 xiph_parse_fmtp_pair);
382 RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
383 .enc_name = "theora",
384 .codec_type = AVMEDIA_TYPE_VIDEO,
385 .codec_id = AV_CODEC_ID_THEORA,
386 .parse_sdp_a_line = xiph_parse_sdp_line,
387 .alloc = xiph_new_context,
388 .free = xiph_free_context,
389 .parse_packet = xiph_handle_packet
392 RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
393 .enc_name = "vorbis",
394 .codec_type = AVMEDIA_TYPE_AUDIO,
395 .codec_id = AV_CODEC_ID_VORBIS,
396 .parse_sdp_a_line = xiph_parse_sdp_line,
397 .alloc = xiph_new_context,
398 .free = xiph_free_context,
399 .parse_packet = xiph_handle_packet