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
35
40
41 /**
42 * RTP/Xiph specific private data.
43 */
45 unsigned ident;
///< 24-bit stream configuration identifier
51 };
52
54 {
57 }
58
59
62 const uint8_t *buf,
int len, uint16_t seq,
64 {
65
66 int ident, fragmented, tdt, num_pkts, pkt_len,
ret;
67
68 if (!buf) {
69 if (!
data->split_buf ||
data->split_pos + 2 >
data->split_buf_len ||
70 data->split_pkts <= 0) {
73 }
76 if (pkt_len >
data->split_buf_len -
data->split_pos) {
79 }
83 }
86 data->split_pos += pkt_len;
88 return data->split_pkts > 0;
89 }
90
91 if (len < 6 || len > INT_MAX/2) {
94 }
95
96 // read xiph rtp headers
98 fragmented = buf[3] >> 6;
99 tdt = (buf[3] >> 4) & 3;
100 num_pkts = buf[3] & 0xf;
102
103 if (pkt_len >
len - 6) {
105 "Invalid packet length %d in %d byte packet\n", pkt_len,
108 }
109
110 if (ident !=
data->ident) {
113 }
114
115 if (tdt) {
117 "RTP Xiph packet settings (%d,%d,%d)",
118 fragmented, tdt, num_pkts);
120 }
121
122 buf += 6; // move past header bits
124
125 if (fragmented == 0) {
129 }
131 memcpy(
pkt->
data, buf, pkt_len);
132 buf += pkt_len;
134 num_pkts--;
135
136 if (num_pkts > 0) {
137 if (
len >
data->split_buf_size || !
data->split_buf) {
139 data->split_buf_size = 2 *
len;
141 if (!
data->split_buf) {
145 }
146 }
147 memcpy(
data->split_buf, buf,
len);
150 data->split_pkts = num_pkts;
151 return 1;
152 }
153
154 return 0;
155
156 } else if (fragmented == 1) {
157 // start of xiph data fragment
158 int res;
159
160 // end packet has been lost somewhere, so drop buffered data
162
164 return res;
165
167 data->timestamp = *timestamp;
168
169 } else {
171 if (
data->timestamp != *timestamp) {
172 // skip if fragmented timestamp is incorrect;
173 // a start packet has been lost somewhere
177 }
178 if (!
data->fragment) {
180 "Received packet without a start fragment; dropping.\n");
182 }
183
184 // copy data to fragment buffer
186
187 if (fragmented == 3) {
188 // end of xiph data packet
192 "Error occurred when getting fragment buffer.");
194 }
195
196 return 0;
197 }
198 }
199
201 }
202
203 /**
204 * Length encoding described in RFC5215 section 3.1.1.
205 */
206 static int get_base128(
const uint8_t ** buf,
const uint8_t * buf_end)
207 {
208 int n = 0;
209 for (; *buf < buf_end; ++*buf) {
210 n <<= 7;
211 n += **buf & 0x7f;
212 if (!(**buf & 0x80)) {
213 ++*buf;
214 return n;
215 }
216 }
217 return 0;
218 }
219
220 /**
221 * Based off parse_packed_headers in Vorbis RTP
222 */
223 static int
225 const uint8_t * packed_headers,
226 const uint8_t * packed_headers_end,
228 {
229
230 unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
232 uint8_t *ptr;
233
234 if (packed_headers_end - packed_headers < 9) {
237 packed_headers_end - packed_headers);
239 }
240
241 num_packed = bytestream_get_be32(&packed_headers);
242 xiph_data->
ident = bytestream_get_be24(&packed_headers);
243 length = bytestream_get_be16(&packed_headers);
244 num_headers =
get_base128(&packed_headers, packed_headers_end);
245 length1 =
get_base128(&packed_headers, packed_headers_end);
246 length2 =
get_base128(&packed_headers, packed_headers_end);
247
248 if (num_packed != 1 || num_headers > 3) {
250 num_packed, num_headers);
252 }
253
254 if (packed_headers_end - packed_headers != length ||
255 length1 > length || length2 > length - length1) {
258 length2, packed_headers_end - packed_headers, length);
260 }
261
262 /* allocate extra space:
263 * -- length/255 +2 for xiphlacing
264 * -- one for the '2' marker
265 * -- AV_INPUT_BUFFER_PADDING_SIZE required */
267
271 }
273 *ptr++ = 2;
276 memcpy(ptr, packed_headers, length);
277 ptr += length;
279 // clear out remaining parts of the buffer
281
282 return 0;
283 }
284
288 const char *attr,
const char *
value)
289 {
292
293 if (!strcmp(attr, "sampling")) {
294 if (!strcmp(
value,
"YCbCr-4:2:0")) {
296 }
else if (!strcmp(
value,
"YCbCr-4:4:2")) {
298 }
else if (!strcmp(
value,
"YCbCr-4:4:4")) {
300 } else {
302 "Unsupported pixel format %s\n", attr);
304 }
305 } else if (!strcmp(attr, "width")) {
306 /* This is an integer between 1 and 1048561
307 * and MUST be in multiples of 16. */
309 return 0;
310 } else if (!strcmp(attr, "height")) {
311 /* This is an integer between 1 and 1048561
312 * and MUST be in multiples of 16. */
314 return 0;
315 } else if (!strcmp(attr, "delivery-method")) {
316 /* Possible values are: inline, in_band, out_band/specific_name. */
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 */
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;
327 int packet_size;
328 size_t decoded_alloc = strlen(
value) / 4 * 3 + 4;
329
330 if (decoded_alloc <= INT_MAX) {
331 decoded_packet =
av_malloc(decoded_alloc);
332 if (decoded_packet) {
333 packet_size =
335
337 (
s, decoded_packet, decoded_packet + packet_size, par,
338 xiph_data);
339 } else {
341 "Out of memory while decoding SDP configuration.\n");
343 }
344 } else {
347 }
349 }
351 }
352
355 {
356 const char *p;
357
358 if (st_index < 0)
359 return 0;
360
364 }
365
366 return 0;
367 }
368
377 };
378
388 };