ffmpeg / libavformat / rtpdec_xiph.c @ 2874c81c
History | View | Annotate | Download (12.4 KB)
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 libavformat/rtpdec_xiph.c
|
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 %d 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,%d,%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(AVCodecContext * codec, |
290 |
PayloadContext *xiph_data, |
291 |
char *attr, char *value) |
292 |
{ |
293 |
int result = 0; |
294 |
|
295 |
if (!strcmp(attr, "sampling")) { |
296 |
return AVERROR_PATCHWELCOME;
|
297 |
} else if (!strcmp(attr, "width")) { |
298 |
/* This is an integer between 1 and 1048561
|
299 |
* and MUST be in multiples of 16. */
|
300 |
codec->width = atoi(value); |
301 |
return 0; |
302 |
} else if (!strcmp(attr, "height")) { |
303 |
/* This is an integer between 1 and 1048561
|
304 |
* and MUST be in multiples of 16. */
|
305 |
codec->height = atoi(value); |
306 |
return 0; |
307 |
} else if (!strcmp(attr, "delivery-method")) { |
308 |
/* Possible values are: inline, in_band, out_band/specific_name. */
|
309 |
return AVERROR_PATCHWELCOME;
|
310 |
} else if (!strcmp(attr, "configuration-uri")) { |
311 |
/* NOTE: configuration-uri is supported only under 2 conditions:
|
312 |
*--after the delivery-method tag
|
313 |
* --with a delivery-method value of out_band */
|
314 |
return AVERROR_PATCHWELCOME;
|
315 |
} else if (!strcmp(attr, "configuration")) { |
316 |
/* NOTE: configuration is supported only AFTER the delivery-method tag
|
317 |
* The configuration value is a base64 encoded packed header */
|
318 |
uint8_t *decoded_packet = NULL;
|
319 |
int packet_size;
|
320 |
size_t decoded_alloc = strlen(value) / 4 * 3 + 4; |
321 |
|
322 |
if (decoded_alloc <= INT_MAX) {
|
323 |
decoded_packet = av_malloc(decoded_alloc); |
324 |
if (decoded_packet) {
|
325 |
packet_size = |
326 |
av_base64_decode(decoded_packet, value, decoded_alloc); |
327 |
|
328 |
result = parse_packed_headers |
329 |
(decoded_packet, decoded_packet + packet_size, codec, |
330 |
xiph_data); |
331 |
} else {
|
332 |
av_log(codec, AV_LOG_ERROR, |
333 |
"Out of memory while decoding SDP configuration.\n");
|
334 |
result = AVERROR(ENOMEM); |
335 |
} |
336 |
} else {
|
337 |
av_log(codec, AV_LOG_ERROR, "Packet too large\n");
|
338 |
result = AVERROR_INVALIDDATA; |
339 |
} |
340 |
av_free(decoded_packet); |
341 |
} |
342 |
return result;
|
343 |
} |
344 |
|
345 |
static int xiph_parse_sdp_line(AVFormatContext *s, int st_index, |
346 |
PayloadContext *data, const char *line) |
347 |
{ |
348 |
const char *p; |
349 |
char *value;
|
350 |
char attr[25]; |
351 |
int value_size = strlen(line), attr_size = sizeof(attr), res = 0; |
352 |
AVCodecContext* codec = s->streams[st_index]->codec; |
353 |
|
354 |
assert(codec->id == CODEC_ID_THEORA); |
355 |
assert(data); |
356 |
|
357 |
if (!(value = av_malloc(value_size))) {
|
358 |
av_log(codec, AV_LOG_ERROR, "Out of memory\n");
|
359 |
return AVERROR(ENOMEM);
|
360 |
} |
361 |
|
362 |
if (av_strstart(line, "fmtp:", &p)) { |
363 |
// remove protocol identifier
|
364 |
while (*p && *p == ' ') p++; // strip spaces |
365 |
while (*p && *p != ' ') p++; // eat protocol identifier |
366 |
while (*p && *p == ' ') p++; // strip trailing spaces |
367 |
|
368 |
while (ff_rtsp_next_attr_and_value(&p,
|
369 |
attr, attr_size, |
370 |
value, value_size)) { |
371 |
res = xiph_parse_fmtp_pair(codec, data, attr, value); |
372 |
if (res < 0 && res != AVERROR_PATCHWELCOME) |
373 |
return res;
|
374 |
} |
375 |
} |
376 |
|
377 |
av_free(value); |
378 |
return 0; |
379 |
} |
380 |
|
381 |
RTPDynamicProtocolHandler ff_theora_dynamic_handler = { |
382 |
.enc_name = "theora",
|
383 |
.codec_type = AVMEDIA_TYPE_VIDEO, |
384 |
.codec_id = CODEC_ID_THEORA, |
385 |
.parse_sdp_a_line = xiph_parse_sdp_line, |
386 |
.open = xiph_new_context, |
387 |
.close = xiph_free_context, |
388 |
.parse_packet = xiph_handle_packet |
389 |
}; |
390 |
|
391 |
RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = { |
392 |
.enc_name = "vorbis",
|
393 |
.codec_type = AVMEDIA_TYPE_AUDIO, |
394 |
.codec_id = CODEC_ID_VORBIS, |
395 |
.parse_sdp_a_line = xiph_parse_sdp_line, |
396 |
.open = xiph_new_context, |
397 |
.close = xiph_free_context, |
398 |
.parse_packet = xiph_handle_packet |
399 |
}; |