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
36
41
42 /**
43 * RTP/Xiph specific private data.
44 */
46 unsigned ident;
///< 24-bit stream configuration identifier
52 };
53
55 {
58 }
59
60
63 const uint8_t *buf,
int len, uint16_t seq,
65 {
66
67 int ident, fragmented, tdt, num_pkts, pkt_len,
ret;
68
69 if (!buf) {
70 if (!
data->split_buf ||
data->split_pos + 2 >
data->split_buf_len ||
71 data->split_pkts <= 0) {
74 }
77 if (pkt_len >
data->split_buf_len -
data->split_pos) {
80 }
84 }
87 data->split_pos += pkt_len;
89 return data->split_pkts > 0;
90 }
91
92 if (len < 6 || len > INT_MAX/2) {
95 }
96
97 // read xiph rtp headers
99 fragmented = buf[3] >> 6;
100 tdt = (buf[3] >> 4) & 3;
101 num_pkts = buf[3] & 0xf;
103
104 if (pkt_len >
len - 6) {
106 "Invalid packet length %d in %d byte packet\n", pkt_len,
109 }
110
111 if (ident !=
data->ident) {
114 }
115
116 if (tdt) {
118 "RTP Xiph packet settings (%d,%d,%d)",
119 fragmented, tdt, num_pkts);
121 }
122
123 buf += 6; // move past header bits
125
126 if (fragmented == 0) {
130 }
132 memcpy(
pkt->
data, buf, pkt_len);
133 buf += pkt_len;
135 num_pkts--;
136
137 if (num_pkts > 0) {
138 if (
len >
data->split_buf_size || !
data->split_buf) {
140 data->split_buf_size = 2 *
len;
142 if (!
data->split_buf) {
146 }
147 }
148 memcpy(
data->split_buf, buf,
len);
151 data->split_pkts = num_pkts;
152 return 1;
153 }
154
155 return 0;
156
157 } else if (fragmented == 1) {
158 // start of xiph data fragment
159 int res;
160
161 // end packet has been lost somewhere, so drop buffered data
163
165 return res;
166
168 data->timestamp = *timestamp;
169
170 } else {
172 if (
data->timestamp != *timestamp) {
173 // skip if fragmented timestamp is incorrect;
174 // a start packet has been lost somewhere
178 }
179 if (!
data->fragment) {
181 "Received packet without a start fragment; dropping.\n");
183 }
184
185 // copy data to fragment buffer
187
188 if (fragmented == 3) {
189 // end of xiph data packet
193 "Error occurred when getting fragment buffer.");
195 }
196
197 return 0;
198 }
199 }
200
202 }
203
204 /**
205 * Length encoding described in RFC5215 section 3.1.1.
206 */
207 static int get_base128(
const uint8_t ** buf,
const uint8_t * buf_end)
208 {
209 int n = 0;
210 for (; *buf < buf_end; ++*buf) {
211 n <<= 7;
212 n += **buf & 0x7f;
213 if (!(**buf & 0x80)) {
214 ++*buf;
215 return n;
216 }
217 }
218 return 0;
219 }
220
221 /**
222 * Based off parse_packed_headers in Vorbis RTP
223 */
224 static int
226 const uint8_t * packed_headers,
227 const uint8_t * packed_headers_end,
229 {
230
231 unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
233 uint8_t *ptr;
234
235 if (packed_headers_end - packed_headers < 9) {
238 packed_headers_end - packed_headers);
240 }
241
242 num_packed = bytestream_get_be32(&packed_headers);
243 xiph_data->
ident = bytestream_get_be24(&packed_headers);
244 length = bytestream_get_be16(&packed_headers);
245 num_headers =
get_base128(&packed_headers, packed_headers_end);
246 length1 =
get_base128(&packed_headers, packed_headers_end);
247 length2 =
get_base128(&packed_headers, packed_headers_end);
248
249 if (num_packed != 1 || num_headers > 3) {
251 num_packed, num_headers);
253 }
254
255 if (packed_headers_end - packed_headers != length ||
256 length1 > length || length2 > length - length1) {
259 length2, packed_headers_end - packed_headers, length);
261 }
262
263 /* allocate extra space:
264 * -- length/255 +2 for xiphlacing
265 * -- one for the '2' marker
266 * -- AV_INPUT_BUFFER_PADDING_SIZE required */
268
272 }
274 *ptr++ = 2;
277 memcpy(ptr, packed_headers, length);
278 ptr += length;
280 // clear out remaining parts of the buffer
282
283 return 0;
284 }
285
289 const char *attr,
const char *
value)
290 {
293
294 if (!strcmp(attr, "sampling")) {
295 if (!strcmp(
value,
"YCbCr-4:2:0")) {
297 }
else if (!strcmp(
value,
"YCbCr-4:4:2")) {
299 }
else if (!strcmp(
value,
"YCbCr-4:4:4")) {
301 } else {
303 "Unsupported pixel format %s\n", attr);
305 }
306 } else if (!strcmp(attr, "width")) {
307 /* This is an integer between 1 and 1048561
308 * and MUST be in multiples of 16. */
310 return 0;
311 } else if (!strcmp(attr, "height")) {
312 /* This is an integer between 1 and 1048561
313 * and MUST be in multiples of 16. */
315 return 0;
316 } else if (!strcmp(attr, "delivery-method")) {
317 /* Possible values are: inline, in_band, out_band/specific_name. */
319 } else if (!strcmp(attr, "configuration-uri")) {
320 /* NOTE: configuration-uri is supported only under 2 conditions:
321 *--after the delivery-method tag
322 * --with a delivery-method value of out_band */
324 } else if (!strcmp(attr, "configuration")) {
325 /* NOTE: configuration is supported only AFTER the delivery-method tag
326 * The configuration value is a base64 encoded packed header */
327 uint8_t *decoded_packet =
NULL;
328 int packet_size;
329 size_t decoded_alloc = strlen(
value) / 4 * 3 + 4;
330
331 if (decoded_alloc <= INT_MAX) {
332 decoded_packet =
av_malloc(decoded_alloc);
333 if (decoded_packet) {
334 packet_size =
336
338 (
s, decoded_packet, decoded_packet + packet_size, par,
339 xiph_data);
340 } else {
342 "Out of memory while decoding SDP configuration.\n");
344 }
345 } else {
348 }
350 }
352 }
353
356 {
358
359 if (st_index < 0)
360 return 0;
361
365 }
366
367 return 0;
368 }
369
378 };
379
389 };