lavc: add a variant of av_get_audio_frame_duration working with AVCodecParameters
[libav.git] / libavformat / rtpdec_xiph.c
CommitLineData
887af2aa 1/*
96070b8b 2 * Xiph RTP Protocols
339f5f39 3 * Copyright (c) 2009 Colin McQuillian
887af2aa
JA
4 * Copyright (c) 2010 Josh Allmann
5 *
2912e87a 6 * This file is part of Libav.
887af2aa 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
887af2aa
JA
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
887af2aa
JA
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
887af2aa
JA
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
ba87f080 24 * @file
96070b8b 25 * @brief Xiph / RTP Code
339f5f39 26 * @author Colin McQuillan <m.niloc@gmail.com>
887af2aa
JA
27 * @author Josh Allmann <joshua.allmann@gmail.com>
28 */
29
7abd35a1 30#include "libavutil/attributes.h"
887af2aa
JA
31#include "libavutil/avstring.h"
32#include "libavutil/base64.h"
33#include "libavcodec/bytestream.h"
34
35#include <assert.h>
36
199fb402 37#include "avio_internal.h"
887af2aa 38#include "rtpdec.h"
965a3ddb 39#include "rtpdec_formats.h"
887af2aa
JA
40
41/**
96070b8b 42 * RTP/Xiph specific private data.
887af2aa
JA
43 */
44struct PayloadContext {
45 unsigned ident; ///< 24-bit stream configuration identifier
46 uint32_t timestamp;
ae628ec1 47 AVIOContext* fragment; ///< buffer for split payloads
504de2ec
MS
48 uint8_t *split_buf;
49 int split_pos, split_buf_len, split_buf_size;
50 int split_pkts;
887af2aa
JA
51};
52
d594dbec 53static void xiph_close_context(PayloadContext * data)
887af2aa 54{
199fb402 55 ffio_free_dyn_buf(&data->fragment);
504de2ec 56 av_free(data->split_buf);
887af2aa
JA
57}
58
c6f1dc8e 59
90c784cc
MS
60static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data,
61 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
62 const uint8_t *buf, int len, uint16_t seq,
63 int flags)
887af2aa
JA
64{
65
66 int ident, fragmented, tdt, num_pkts, pkt_len;
67
504de2ec
MS
68 if (!buf) {
69 if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
70 data->split_pkts <= 0) {
71 av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
72 return AVERROR_INVALIDDATA;
73 }
74 pkt_len = AV_RB16(data->split_buf + data->split_pos);
75 data->split_pos += 2;
76 if (data->split_pos + pkt_len > data->split_buf_len) {
77 av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
78 return AVERROR_INVALIDDATA;
79 }
80 if (av_new_packet(pkt, pkt_len)) {
81 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
82 return AVERROR(ENOMEM);
83 }
84 pkt->stream_index = st->index;
85 memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
86 data->split_pos += pkt_len;
87 data->split_pkts--;
88 return data->split_pkts > 0;
89 }
90
887af2aa
JA
91 if (len < 6) {
92 av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
93 return AVERROR_INVALIDDATA;
94 }
95
96070b8b 96 // read xiph rtp headers
887af2aa
JA
97 ident = AV_RB24(buf);
98 fragmented = buf[3] >> 6;
99 tdt = (buf[3] >> 4) & 3;
737b3972 100 num_pkts = buf[3] & 0xf;
887af2aa
JA
101 pkt_len = AV_RB16(buf + 4);
102
103 if (pkt_len > len - 6) {
104 av_log(ctx, AV_LOG_ERROR,
105 "Invalid packet length %d in %d byte packet\n", pkt_len,
106 len);
107 return AVERROR_INVALIDDATA;
108 }
109
110 if (ident != data->ident) {
111 av_log(ctx, AV_LOG_ERROR,
96070b8b 112 "Unimplemented Xiph SDP configuration change detected\n");
887af2aa
JA
113 return AVERROR_PATCHWELCOME;
114 }
115
116 if (tdt) {
117 av_log(ctx, AV_LOG_ERROR,
96070b8b 118 "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
887af2aa
JA
119 fragmented, tdt, num_pkts);
120 return AVERROR_PATCHWELCOME;
121 }
122
123 buf += 6; // move past header bits
124 len -= 6;
125
126 if (fragmented == 0) {
504de2ec 127 if (av_new_packet(pkt, pkt_len)) {
887af2aa 128 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
2874c81c 129 return AVERROR(ENOMEM);
887af2aa
JA
130 }
131 pkt->stream_index = st->index;
504de2ec
MS
132 memcpy(pkt->data, buf, pkt_len);
133 buf += pkt_len;
134 len -= pkt_len;
135 num_pkts--;
136
137 if (num_pkts > 0) {
138 if (len > data->split_buf_size || !data->split_buf) {
139 av_freep(&data->split_buf);
140 data->split_buf_size = 2 * len;
141 data->split_buf = av_malloc(data->split_buf_size);
142 if (!data->split_buf) {
143 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
ce70f28a 144 av_packet_unref(pkt);
504de2ec
MS
145 return AVERROR(ENOMEM);
146 }
147 }
148 memcpy(data->split_buf, buf, len);
149 data->split_buf_len = len;
150 data->split_pos = 0;
151 data->split_pkts = num_pkts;
152 return 1;
887af2aa 153 }
887af2aa
JA
154
155 return 0;
156
157 } else if (fragmented == 1) {
96070b8b 158 // start of xiph data fragment
887af2aa
JA
159 int res;
160
161 // end packet has been lost somewhere, so drop buffered data
199fb402 162 ffio_free_dyn_buf(&data->fragment);
887af2aa 163
b92c5452 164 if((res = avio_open_dyn_buf(&data->fragment)) < 0)
887af2aa
JA
165 return res;
166
77eb5504 167 avio_write(data->fragment, buf, pkt_len);
887af2aa
JA
168 data->timestamp = *timestamp;
169
170 } else {
171 assert(fragmented < 4);
172 if (data->timestamp != *timestamp) {
173 // skip if fragmented timestamp is incorrect;
174 // a start packet has been lost somewhere
199fb402 175 ffio_free_dyn_buf(&data->fragment);
887af2aa
JA
176 av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
177 return AVERROR_INVALIDDATA;
178 }
adc03a34
JA
179 if (!data->fragment) {
180 av_log(ctx, AV_LOG_WARNING,
181 "Received packet without a start fragment; dropping.\n");
182 return AVERROR(EAGAIN);
183 }
887af2aa
JA
184
185 // copy data to fragment buffer
77eb5504 186 avio_write(data->fragment, buf, pkt_len);
887af2aa
JA
187
188 if (fragmented == 3) {
96070b8b 189 // end of xiph data packet
179a5c37
AK
190 int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
191 if (ret < 0) {
887af2aa
JA
192 av_log(ctx, AV_LOG_ERROR,
193 "Error occurred when getting fragment buffer.");
179a5c37 194 return ret;
887af2aa
JA
195 }
196
887af2aa
JA
197 return 0;
198 }
199 }
200
201 return AVERROR(EAGAIN);
202}
203
204/**
205 * Length encoding described in RFC5215 section 3.1.1.
206 */
207static 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 */
0a7005be 224static int
887af2aa
JA
225parse_packed_headers(const uint8_t * packed_headers,
226 const uint8_t * packed_headers_end,
96070b8b 227 AVCodecContext * codec, PayloadContext * xiph_data)
887af2aa
JA
228{
229
230 unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
231 uint8_t *ptr;
232
233 if (packed_headers_end - packed_headers < 9) {
234 av_log(codec, AV_LOG_ERROR,
461628c6 235 "Invalid %td byte packed header.",
887af2aa
JA
236 packed_headers_end - packed_headers);
237 return AVERROR_INVALIDDATA;
238 }
239
240 num_packed = bytestream_get_be32(&packed_headers);
96070b8b 241 xiph_data->ident = bytestream_get_be24(&packed_headers);
887af2aa
JA
242 length = bytestream_get_be16(&packed_headers);
243 num_headers = get_base128(&packed_headers, packed_headers_end);
244 length1 = get_base128(&packed_headers, packed_headers_end);
245 length2 = get_base128(&packed_headers, packed_headers_end);
246
247 if (num_packed != 1 || num_headers > 3) {
248 av_log(codec, AV_LOG_ERROR,
249 "Unimplemented number of headers: %d packed headers, %d headers\n",
250 num_packed, num_headers);
251 return AVERROR_PATCHWELCOME;
252 }
253
254 if (packed_headers_end - packed_headers != length ||
255 length1 > length || length2 > length - length1) {
256 av_log(codec, AV_LOG_ERROR,
461628c6 257 "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
887af2aa
JA
258 length2, packed_headers_end - packed_headers, length);
259 return AVERROR_INVALIDDATA;
260 }
261
262 /* allocate extra space:
263 * -- length/255 +2 for xiphlacing
264 * -- one for the '2' marker
059a9348
VG
265 * -- AV_INPUT_BUFFER_PADDING_SIZE required */
266 extradata_alloc = length + length/255 + 3 + AV_INPUT_BUFFER_PADDING_SIZE;
887af2aa
JA
267
268 ptr = codec->extradata = av_malloc(extradata_alloc);
269 if (!ptr) {
270 av_log(codec, AV_LOG_ERROR, "Out of memory\n");
2874c81c 271 return AVERROR(ENOMEM);
887af2aa
JA
272 }
273 *ptr++ = 2;
274 ptr += av_xiphlacing(ptr, length1);
275 ptr += av_xiphlacing(ptr, length2);
276 memcpy(ptr, packed_headers, length);
277 ptr += length;
278 codec->extradata_size = ptr - codec->extradata;
279 // clear out remaining parts of the buffer
280 memset(ptr, 0, extradata_alloc - codec->extradata_size);
281
282 return 0;
283}
284
0307cc22
AK
285static int xiph_parse_fmtp_pair(AVFormatContext *s,
286 AVStream* stream,
96070b8b 287 PayloadContext *xiph_data,
ec96a89c 288 const char *attr, const char *value)
887af2aa 289{
af1308f0 290 AVCodecContext *codec = stream->codec;
887af2aa
JA
291 int result = 0;
292
293 if (!strcmp(attr, "sampling")) {
42c63263 294 if (!strcmp(value, "YCbCr-4:2:0")) {
716d413c 295 codec->pix_fmt = AV_PIX_FMT_YUV420P;
42c63263 296 } else if (!strcmp(value, "YCbCr-4:4:2")) {
716d413c 297 codec->pix_fmt = AV_PIX_FMT_YUV422P;
42c63263 298 } else if (!strcmp(value, "YCbCr-4:4:4")) {
716d413c 299 codec->pix_fmt = AV_PIX_FMT_YUV444P;
42c63263 300 } else {
0307cc22 301 av_log(s, AV_LOG_ERROR,
42c63263
JA
302 "Unsupported pixel format %s\n", attr);
303 return AVERROR_INVALIDDATA;
304 }
887af2aa
JA
305 } else if (!strcmp(attr, "width")) {
306 /* This is an integer between 1 and 1048561
307 * and MUST be in multiples of 16. */
308 codec->width = atoi(value);
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. */
313 codec->height = atoi(value);
314 return 0;
315 } else if (!strcmp(attr, "delivery-method")) {
316 /* Possible values are: inline, in_band, out_band/specific_name. */
317 return AVERROR_PATCHWELCOME;
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 */
322 return AVERROR_PATCHWELCOME;
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 =
334 av_base64_decode(decoded_packet, value, decoded_alloc);
335
336 result = parse_packed_headers
337 (decoded_packet, decoded_packet + packet_size, codec,
96070b8b 338 xiph_data);
887af2aa 339 } else {
0307cc22 340 av_log(s, AV_LOG_ERROR,
887af2aa 341 "Out of memory while decoding SDP configuration.\n");
2874c81c 342 result = AVERROR(ENOMEM);
887af2aa
JA
343 }
344 } else {
0307cc22 345 av_log(s, AV_LOG_ERROR, "Packet too large\n");
887af2aa
JA
346 result = AVERROR_INVALIDDATA;
347 }
348 av_free(decoded_packet);
349 }
350 return result;
351}
352
96070b8b 353static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
2ce7f4d4 354 PayloadContext *data, const char *line)
887af2aa
JA
355{
356 const char *p;
887af2aa 357
45600148
MS
358 if (st_index < 0)
359 return 0;
360
887af2aa 361 if (av_strstart(line, "fmtp:", &p)) {
0307cc22 362 return ff_parse_fmtp(s, s->streams[st_index], data, p,
af1308f0 363 xiph_parse_fmtp_pair);
887af2aa
JA
364 }
365
887af2aa
JA
366 return 0;
367}
368
369RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
370 .enc_name = "theora",
72415b2a 371 .codec_type = AVMEDIA_TYPE_VIDEO,
36ef5369 372 .codec_id = AV_CODEC_ID_THEORA,
5d8cae45 373 .priv_data_size = sizeof(PayloadContext),
96070b8b 374 .parse_sdp_a_line = xiph_parse_sdp_line,
d594dbec 375 .close = xiph_close_context,
3cd5828f 376 .parse_packet = xiph_handle_packet,
887af2aa 377};
339f5f39
JA
378
379RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
380 .enc_name = "vorbis",
4bebf2cf 381 .codec_type = AVMEDIA_TYPE_AUDIO,
36ef5369 382 .codec_id = AV_CODEC_ID_VORBIS,
2b982e92 383 .need_parsing = AVSTREAM_PARSE_HEADERS,
5d8cae45 384 .priv_data_size = sizeof(PayloadContext),
339f5f39 385 .parse_sdp_a_line = xiph_parse_sdp_line,
d594dbec 386 .close = xiph_close_context,
3cd5828f 387 .parse_packet = xiph_handle_packet,
339f5f39 388};