]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec_xiph.c
rtpdec: Cleanup FMTP parsing code in Xiph RTP depacketizer
[ffmpeg] / libavformat / rtpdec_xiph.c
1 /*
2  * Xiph RTP Protocols
3  * Copyright (c) 2009 Colin McQuillian
4  * Copyright (c) 2010 Josh Allmann
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * @file
25  * @brief Xiph / RTP Code
26  * @author Colin McQuillan <m.niloc@gmail.com>
27  * @author Josh Allmann <joshua.allmann@gmail.com>
28  */
29
30 #include "libavutil/avstring.h"
31 #include "libavutil/base64.h"
32 #include "libavcodec/bytestream.h"
33
34 #include <assert.h>
35
36 #include "rtpdec.h"
37 #include "rtpdec_xiph.h"
38
39 /**
40  * RTP/Xiph specific private data.
41  */
42 struct PayloadContext {
43     unsigned ident;             ///< 24-bit stream configuration identifier
44     uint32_t timestamp;
45     ByteIOContext* fragment;    ///< buffer for split payloads
46 };
47
48 static PayloadContext *xiph_new_context(void)
49 {
50     return av_mallocz(sizeof(PayloadContext));
51 }
52
53 static inline void free_fragment_if_needed(PayloadContext * data)
54 {
55     if (data->fragment) {
56         uint8_t* p;
57         url_close_dyn_buf(data->fragment, &p);
58         av_free(p);
59         data->fragment = NULL;
60     }
61 }
62
63 static void xiph_free_context(PayloadContext * data)
64 {
65     free_fragment_if_needed(data);
66     av_free(data);
67 }
68
69 static int xiph_handle_packet(AVFormatContext * ctx,
70                               PayloadContext * data,
71                               AVStream * st,
72                               AVPacket * pkt,
73                               uint32_t * timestamp,
74                               const uint8_t * buf, int len, int flags)
75 {
76
77     int ident, fragmented, tdt, num_pkts, pkt_len;
78
79     if (len < 6) {
80         av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
81         return AVERROR_INVALIDDATA;
82     }
83
84     // read xiph rtp headers
85     ident       = AV_RB24(buf);
86     fragmented  = buf[3] >> 6;
87     tdt         = (buf[3] >> 4) & 3;
88     num_pkts    = buf[3] & 7;
89     pkt_len     = AV_RB16(buf + 4);
90
91     if (pkt_len > len - 6) {
92         av_log(ctx, AV_LOG_ERROR,
93                "Invalid packet length %d in %d byte packet\n", pkt_len,
94                len);
95         return AVERROR_INVALIDDATA;
96     }
97
98     if (ident != data->ident) {
99         av_log(ctx, AV_LOG_ERROR,
100                "Unimplemented Xiph SDP configuration change detected\n");
101         return AVERROR_PATCHWELCOME;
102     }
103
104     if (tdt) {
105         av_log(ctx, AV_LOG_ERROR,
106                "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
107                fragmented, tdt, num_pkts);
108         return AVERROR_PATCHWELCOME;
109     }
110
111     buf += 6; // move past header bits
112     len -= 6;
113
114     if (fragmented == 0) {
115         // whole frame(s)
116         int i, data_len, write_len;
117         buf -= 2;
118         len += 2;
119
120         // fast first pass to calculate total length
121         for (i = 0, data_len = 0;  (i < num_pkts) && (len >= 2);  i++) {
122             int off   = data_len + (i << 1);
123             pkt_len   = AV_RB16(buf + off);
124             data_len += pkt_len;
125             len      -= pkt_len + 2;
126         }
127
128         if (len < 0 || i < num_pkts) {
129             av_log(ctx, AV_LOG_ERROR,
130                    "Bad packet: %d bytes left at frame %d of %d\n",
131                    len, i, num_pkts);
132             return AVERROR_INVALIDDATA;
133         }
134
135         if (av_new_packet(pkt, data_len)) {
136             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
137             return AVERROR(ENOMEM);
138         }
139         pkt->stream_index = st->index;
140
141         // concatenate frames
142         for (i = 0, write_len = 0; write_len < data_len; i++) {
143             pkt_len = AV_RB16(buf);
144             buf += 2;
145             memcpy(pkt->data + write_len, buf, pkt_len);
146             write_len += pkt_len;
147             buf += pkt_len;
148         }
149         assert(write_len == data_len);
150
151         return 0;
152
153     } else if (fragmented == 1) {
154         // start of xiph data fragment
155         int res;
156
157         // end packet has been lost somewhere, so drop buffered data
158         free_fragment_if_needed(data);
159
160         if((res = url_open_dyn_buf(&data->fragment)) < 0)
161             return res;
162
163         put_buffer(data->fragment, buf, pkt_len);
164         data->timestamp = *timestamp;
165
166     } else {
167         assert(fragmented < 4);
168         if (data->timestamp != *timestamp) {
169             // skip if fragmented timestamp is incorrect;
170             // a start packet has been lost somewhere
171             free_fragment_if_needed(data);
172             av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
173             return AVERROR_INVALIDDATA;
174         }
175
176         // copy data to fragment buffer
177         put_buffer(data->fragment, buf, pkt_len);
178
179         if (fragmented == 3) {
180             // end of xiph data packet
181             uint8_t* xiph_data;
182             int frame_size = url_close_dyn_buf(data->fragment, &xiph_data);
183
184             if (frame_size < 0) {
185                 av_log(ctx, AV_LOG_ERROR,
186                        "Error occurred when getting fragment buffer.");
187                 return frame_size;
188             }
189
190             if (av_new_packet(pkt, frame_size)) {
191                 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
192                 return AVERROR(ENOMEM);
193             }
194
195             memcpy(pkt->data, xiph_data, frame_size);
196             pkt->stream_index = st->index;
197
198             av_free(xiph_data);
199             data->fragment = NULL;
200
201             return 0;
202         }
203     }
204
205    return AVERROR(EAGAIN);
206 }
207
208 /**
209  * Length encoding described in RFC5215 section 3.1.1.
210  */
211 static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
212 {
213     int n = 0;
214     for (; *buf < buf_end; ++*buf) {
215         n <<= 7;
216         n += **buf & 0x7f;
217         if (!(**buf & 0x80)) {
218             ++*buf;
219             return n;
220         }
221     }
222     return 0;
223 }
224
225 /**
226  * Based off parse_packed_headers in Vorbis RTP
227  */
228 static unsigned int
229 parse_packed_headers(const uint8_t * packed_headers,
230                      const uint8_t * packed_headers_end,
231                      AVCodecContext * codec, PayloadContext * xiph_data)
232 {
233
234     unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
235     uint8_t *ptr;
236
237     if (packed_headers_end - packed_headers < 9) {
238         av_log(codec, AV_LOG_ERROR,
239                "Invalid %td byte packed header.",
240                packed_headers_end - packed_headers);
241         return AVERROR_INVALIDDATA;
242     }
243
244     num_packed         = bytestream_get_be32(&packed_headers);
245     xiph_data->ident   = bytestream_get_be24(&packed_headers);
246     length             = bytestream_get_be16(&packed_headers);
247     num_headers        = get_base128(&packed_headers, packed_headers_end);
248     length1            = get_base128(&packed_headers, packed_headers_end);
249     length2            = get_base128(&packed_headers, packed_headers_end);
250
251     if (num_packed != 1 || num_headers > 3) {
252         av_log(codec, AV_LOG_ERROR,
253                "Unimplemented number of headers: %d packed headers, %d headers\n",
254                num_packed, num_headers);
255         return AVERROR_PATCHWELCOME;
256     }
257
258     if (packed_headers_end - packed_headers != length ||
259         length1 > length || length2 > length - length1) {
260         av_log(codec, AV_LOG_ERROR,
261                "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
262                length2, packed_headers_end - packed_headers, length);
263         return AVERROR_INVALIDDATA;
264     }
265
266     /* allocate extra space:
267      * -- length/255 +2 for xiphlacing
268      * -- one for the '2' marker
269      * -- FF_INPUT_BUFFER_PADDING_SIZE required */
270     extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;
271
272     ptr = codec->extradata = av_malloc(extradata_alloc);
273     if (!ptr) {
274         av_log(codec, AV_LOG_ERROR, "Out of memory\n");
275         return AVERROR(ENOMEM);
276     }
277     *ptr++ = 2;
278     ptr += av_xiphlacing(ptr, length1);
279     ptr += av_xiphlacing(ptr, length2);
280     memcpy(ptr, packed_headers, length);
281     ptr += length;
282     codec->extradata_size = ptr - codec->extradata;
283     // clear out remaining parts of the buffer
284     memset(ptr, 0, extradata_alloc - codec->extradata_size);
285
286     return 0;
287 }
288
289 static int xiph_parse_fmtp_pair(AVStream* stream,
290                                 PayloadContext *xiph_data,
291                                 char *attr, char *value)
292 {
293     AVCodecContext *codec = stream->codec;
294     int result = 0;
295
296     if (!strcmp(attr, "sampling")) {
297         return AVERROR_PATCHWELCOME;
298     } else if (!strcmp(attr, "width")) {
299         /* This is an integer between 1 and 1048561
300          * and MUST be in multiples of 16. */
301         codec->width = atoi(value);
302         return 0;
303     } else if (!strcmp(attr, "height")) {
304         /* This is an integer between 1 and 1048561
305          * and MUST be in multiples of 16. */
306         codec->height = atoi(value);
307         return 0;
308     } else if (!strcmp(attr, "delivery-method")) {
309         /* Possible values are: inline, in_band, out_band/specific_name. */
310         return AVERROR_PATCHWELCOME;
311     } else if (!strcmp(attr, "configuration-uri")) {
312         /* NOTE: configuration-uri is supported only under 2 conditions:
313          *--after the delivery-method tag
314          * --with a delivery-method value of out_band */
315         return AVERROR_PATCHWELCOME;
316     } else if (!strcmp(attr, "configuration")) {
317         /* NOTE: configuration is supported only AFTER the delivery-method tag
318          * The configuration value is a base64 encoded packed header */
319         uint8_t *decoded_packet = NULL;
320         int packet_size;
321         size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
322
323         if (decoded_alloc <= INT_MAX) {
324             decoded_packet = av_malloc(decoded_alloc);
325             if (decoded_packet) {
326                 packet_size =
327                     av_base64_decode(decoded_packet, value, decoded_alloc);
328
329                 result = parse_packed_headers
330                     (decoded_packet, decoded_packet + packet_size, codec,
331                     xiph_data);
332             } else {
333                 av_log(codec, AV_LOG_ERROR,
334                        "Out of memory while decoding SDP configuration.\n");
335                 result = AVERROR(ENOMEM);
336             }
337         } else {
338             av_log(codec, AV_LOG_ERROR, "Packet too large\n");
339             result = AVERROR_INVALIDDATA;
340         }
341         av_free(decoded_packet);
342     }
343     return result;
344 }
345
346 static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
347                                  PayloadContext *data, const char *line)
348 {
349     const char *p;
350
351     if (av_strstart(line, "fmtp:", &p)) {
352         return ff_parse_fmtp(s->streams[st_index], data, p,
353                              xiph_parse_fmtp_pair);
354     }
355
356     return 0;
357 }
358
359 RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
360     .enc_name         = "theora",
361     .codec_type       = AVMEDIA_TYPE_VIDEO,
362     .codec_id         = CODEC_ID_THEORA,
363     .parse_sdp_a_line = xiph_parse_sdp_line,
364     .open             = xiph_new_context,
365     .close            = xiph_free_context,
366     .parse_packet     = xiph_handle_packet
367 };
368
369 RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
370     .enc_name         = "vorbis",
371     .codec_type       = AVMEDIA_TYPE_AUDIO,
372     .codec_id         = CODEC_ID_VORBIS,
373     .parse_sdp_a_line = xiph_parse_sdp_line,
374     .open             = xiph_new_context,
375     .close            = xiph_free_context,
376     .parse_packet     = xiph_handle_packet
377 };