In yadif filter, declare asm constants directly to avoid dependency on libavcodec
[libav.git] / libavformat / rtpdec.c
CommitLineData
8eb793c4
LA
1/*
2 * RTP input format
406792e7 3 * Copyright (c) 2002 Fabrice Bellard
8eb793c4
LA
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
245976da 21
7246177d 22/* needed for gethostname() */
d0feff2a 23#define _XOPEN_SOURCE 600
7246177d 24
9106a698 25#include "libavcodec/get_bits.h"
8eb793c4
LA
26#include "avformat.h"
27#include "mpegts.h"
8eb793c4
LA
28
29#include <unistd.h>
1e515c42 30#include <strings.h>
8eb793c4
LA
31#include "network.h"
32
302879cb 33#include "rtpdec.h"
965a3ddb 34#include "rtpdec_formats.h"
8eb793c4
LA
35
36//#define DEBUG
37
38/* TODO: - add RTCP statistics reporting (should be optional).
39
40 - add support for h263/mpeg4 packetized output : IDEA: send a
41 buffer to 'rtp_write_packet' contains all the packets for ONE
42 frame. Each packet should have a four byte header containing
43 the length in big endian format (same trick as
44 'url_open_dyn_packet_buf')
45*/
46
47/* statistics functions */
48RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler= NULL;
49
0369d2b0 50void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
8eb793c4
LA
51{
52 handler->next= RTPFirstDynamicPayloadHandler;
53 RTPFirstDynamicPayloadHandler= handler;
54}
55
56void av_register_rtp_dynamic_payload_handlers(void)
57{
9b3788ef
JA
58 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
59 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
556aa7a1
RB
60 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
61 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
45aa9080
RB
62 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
63 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
0369d2b0 64 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
e6327fba 65 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
887af2aa 66 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
a59096e4 67 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
4449df6b 68 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
1ddc176e 69 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
51291e60 70 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
35014efc 71 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
e9fce261
RB
72
73 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
74 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
3ece3e4c
MS
75
76 ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
77 ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
78 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
79 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
8eb793c4
LA
80}
81
1e515c42
MS
82RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
83 enum AVMediaType codec_type)
84{
85 RTPDynamicProtocolHandler *handler;
86 for (handler = RTPFirstDynamicPayloadHandler;
87 handler; handler = handler->next)
88 if (!strcasecmp(name, handler->enc_name) &&
89 codec_type == handler->codec_type)
90 return handler;
91 return NULL;
92}
93
94RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
95 enum AVMediaType codec_type)
96{
97 RTPDynamicProtocolHandler *handler;
98 for (handler = RTPFirstDynamicPayloadHandler;
99 handler; handler = handler->next)
100 if (handler->static_payload_id && handler->static_payload_id == id &&
101 codec_type == handler->codec_type)
102 return handler;
103 return NULL;
104}
105
8eb793c4
LA
106static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
107{
ff328c02
JA
108 int payload_len;
109 while (len >= 2) {
110 switch (buf[1]) {
111 case RTCP_SR:
112 if (len < 16) {
113 av_log(NULL, AV_LOG_ERROR, "Invalid length for RTCP SR packet\n");
114 return AVERROR_INVALIDDATA;
115 }
116 payload_len = (AV_RB16(buf + 2) + 1) * 4;
117
682d28a9
JA
118 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
119 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE)
120 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
121 s->last_rtcp_timestamp = AV_RB32(buf + 16);
ff328c02
JA
122
123 buf += payload_len;
124 len -= payload_len;
125 break;
b20359f5
JA
126 case RTCP_BYE:
127 return -RTCP_BYE;
ff328c02
JA
128 default:
129 return -1;
130 }
131 }
b20359f5 132 return -1;
8eb793c4
LA
133}
134
135#define RTP_SEQ_MOD (1<<16)
136
137/**
138* called on parse open packet
139*/
140static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence) // called on parse open packet.
141{
142 memset(s, 0, sizeof(RTPStatistics));
143 s->max_seq= base_sequence;
144 s->probation= 1;
145}
146
147/**
148* called whenever there is a large jump in sequence numbers, or when they get out of probation...
149*/
150static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
151{
152 s->max_seq= seq;
153 s->cycles= 0;
154 s->base_seq= seq -1;
155 s->bad_seq= RTP_SEQ_MOD + 1;
156 s->received= 0;
157 s->expected_prior= 0;
158 s->received_prior= 0;
159 s->jitter= 0;
160 s->transit= 0;
161}
162
163/**
164* returns 1 if we should handle this packet.
165*/
166static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
167{
168 uint16_t udelta= seq - s->max_seq;
169 const int MAX_DROPOUT= 3000;
170 const int MAX_MISORDER = 100;
171 const int MIN_SEQUENTIAL = 2;
172
173 /* source not valid until MIN_SEQUENTIAL packets with sequence seq. numbers have been received */
174 if(s->probation)
175 {
176 if(seq==s->max_seq + 1) {
177 s->probation--;
178 s->max_seq= seq;
179 if(s->probation==0) {
180 rtp_init_sequence(s, seq);
181 s->received++;
182 return 1;
183 }
184 } else {
185 s->probation= MIN_SEQUENTIAL - 1;
186 s->max_seq = seq;
187 }
188 } else if (udelta < MAX_DROPOUT) {
189 // in order, with permissible gap
190 if(seq < s->max_seq) {
191 //sequence number wrapped; count antother 64k cycles
192 s->cycles += RTP_SEQ_MOD;
193 }
194 s->max_seq= seq;
195 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
196 // sequence made a large jump...
197 if(seq==s->bad_seq) {
198 // two sequential packets-- assume that the other side restarted without telling us; just resync.
199 rtp_init_sequence(s, seq);
200 } else {
201 s->bad_seq= (seq + 1) & (RTP_SEQ_MOD-1);
202 return 0;
203 }
204 } else {
205 // duplicate or reordered packet...
206 }
207 s->received++;
208 return 1;
209}
210
211#if 0
212/**
213* This function is currently unused; without a valid local ntp time, I don't see how we could calculate the
214* difference between the arrival and sent timestamp. As a result, the jitter and transit statistics values
215* never change. I left this in in case someone else can see a way. (rdm)
216*/
217static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp, uint32_t arrival_timestamp)
218{
219 uint32_t transit= arrival_timestamp - sent_timestamp;
220 int d;
221 s->transit= transit;
222 d= FFABS(transit - s->transit);
223 s->jitter += d - ((s->jitter + 8)>>4);
224}
225#endif
226
227int rtp_check_and_send_back_rr(RTPDemuxContext *s, int count)
228{
229 ByteIOContext *pb;
230 uint8_t *buf;
231 int len;
232 int rtcp_bytes;
233 RTPStatistics *stats= &s->statistics;
234 uint32_t lost;
235 uint32_t extended_max;
236 uint32_t expected_interval;
237 uint32_t received_interval;
238 uint32_t lost_interval;
239 uint32_t expected;
240 uint32_t fraction;
241 uint64_t ntp_time= s->last_rtcp_ntp_time; // TODO: Get local ntp time?
242
243 if (!s->rtp_ctx || (count < 1))
244 return -1;
245
246 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
247 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
248 s->octet_count += count;
249 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
250 RTCP_TX_RATIO_DEN;
251 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
252 if (rtcp_bytes < 28)
253 return -1;
254 s->last_octet_count = s->octet_count;
255
256 if (url_open_dyn_buf(&pb) < 0)
257 return -1;
258
259 // Receiver Report
260 put_byte(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
7f3468d3 261 put_byte(pb, RTCP_RR);
8eb793c4 262 put_be16(pb, 7); /* length in words - 1 */
952139a3
LA
263 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
264 put_be32(pb, s->ssrc + 1);
265 put_be32(pb, s->ssrc); // server SSRC
8eb793c4
LA
266 // some placeholders we should really fill...
267 // RFC 1889/p64
268 extended_max= stats->cycles + stats->max_seq;
269 expected= extended_max - stats->base_seq + 1;
270 lost= expected - stats->received;
271 lost= FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
272 expected_interval= expected - stats->expected_prior;
273 stats->expected_prior= expected;
274 received_interval= stats->received - stats->received_prior;
275 stats->received_prior= stats->received;
276 lost_interval= expected_interval - received_interval;
277 if (expected_interval==0 || lost_interval<=0) fraction= 0;
278 else fraction = (lost_interval<<8)/expected_interval;
279
280 fraction= (fraction<<24) | lost;
281
282 put_be32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
283 put_be32(pb, extended_max); /* max sequence received */
284 put_be32(pb, stats->jitter>>4); /* jitter */
285
286 if(s->last_rtcp_ntp_time==AV_NOPTS_VALUE)
287 {
288 put_be32(pb, 0); /* last SR timestamp */
289 put_be32(pb, 0); /* delay since last SR */
290 } else {
291 uint32_t middle_32_bits= s->last_rtcp_ntp_time>>16; // this is valid, right? do we need to handle 64 bit values special?
292 uint32_t delay_since_last= ntp_time - s->last_rtcp_ntp_time;
293
294 put_be32(pb, middle_32_bits); /* last SR timestamp */
295 put_be32(pb, delay_since_last); /* delay since last SR */
296 }
297
298 // CNAME
299 put_byte(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
7f3468d3 300 put_byte(pb, RTCP_SDES);
8eb793c4
LA
301 len = strlen(s->hostname);
302 put_be16(pb, (6 + len + 3) / 4); /* length in words - 1 */
303 put_be32(pb, s->ssrc);
304 put_byte(pb, 0x01);
305 put_byte(pb, len);
306 put_buffer(pb, s->hostname, len);
307 // padding
308 for (len = (6 + len) % 4; len % 4; len++) {
309 put_byte(pb, 0);
310 }
311
312 put_flush_packet(pb);
313 len = url_close_dyn_buf(pb, &buf);
314 if ((len > 0) && buf) {
315 int result;
e8420626 316 dprintf(s->ic, "sending %d bytes of RR\n", len);
8eb793c4 317 result= url_write(s->rtp_ctx, buf, len);
e8420626 318 dprintf(s->ic, "result from url_write: %d\n", result);
8eb793c4
LA
319 av_free(buf);
320 }
321 return 0;
322}
323
9c8fa20d
MS
324void rtp_send_punch_packets(URLContext* rtp_handle)
325{
326 ByteIOContext *pb;
327 uint8_t *buf;
328 int len;
329
330 /* Send a small RTP packet */
331 if (url_open_dyn_buf(&pb) < 0)
332 return;
333
334 put_byte(pb, (RTP_VERSION << 6));
335 put_byte(pb, 0); /* Payload type */
336 put_be16(pb, 0); /* Seq */
337 put_be32(pb, 0); /* Timestamp */
338 put_be32(pb, 0); /* SSRC */
339
340 put_flush_packet(pb);
341 len = url_close_dyn_buf(pb, &buf);
342 if ((len > 0) && buf)
343 url_write(rtp_handle, buf, len);
344 av_free(buf);
345
346 /* Send a minimal RTCP RR */
347 if (url_open_dyn_buf(&pb) < 0)
348 return;
349
350 put_byte(pb, (RTP_VERSION << 6));
7f3468d3 351 put_byte(pb, RTCP_RR); /* receiver report */
9c8fa20d
MS
352 put_be16(pb, 1); /* length in words - 1 */
353 put_be32(pb, 0); /* our own SSRC */
354
355 put_flush_packet(pb);
356 len = url_close_dyn_buf(pb, &buf);
357 if ((len > 0) && buf)
358 url_write(rtp_handle, buf, len);
359 av_free(buf);
360}
361
362
8eb793c4
LA
363/**
364 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
365 * MPEG2TS streams to indicate that they should be demuxed inside the
366 * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned)
8eb793c4 367 */
58ee0991 368RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, int queue_size)
8eb793c4
LA
369{
370 RTPDemuxContext *s;
371
372 s = av_mallocz(sizeof(RTPDemuxContext));
373 if (!s)
374 return NULL;
375 s->payload_type = payload_type;
376 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
2cab6b48 377 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
8eb793c4
LA
378 s->ic = s1;
379 s->st = st;
58ee0991 380 s->queue_size = queue_size;
8eb793c4
LA
381 rtp_init_statistics(&s->statistics, 0); // do we know the initial sequence from sdp?
382 if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
9125806e 383 s->ts = ff_mpegts_parse_open(s->ic);
8eb793c4
LA
384 if (s->ts == NULL) {
385 av_free(s);
386 return NULL;
387 }
388 } else {
26efefc5 389 av_set_pts_info(st, 32, 1, 90000);
8eb793c4
LA
390 switch(st->codec->codec_id) {
391 case CODEC_ID_MPEG1VIDEO:
392 case CODEC_ID_MPEG2VIDEO:
393 case CODEC_ID_MP2:
394 case CODEC_ID_MP3:
395 case CODEC_ID_MPEG4:
45aa9080 396 case CODEC_ID_H263:
8eb793c4
LA
397 case CODEC_ID_H264:
398 st->need_parsing = AVSTREAM_PARSE_FULL;
399 break;
0048a2a8
MS
400 case CODEC_ID_ADPCM_G722:
401 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
402 /* According to RFC 3551, the stream clock rate is 8000
403 * even if the sample rate is 16000. */
404 if (st->codec->sample_rate == 8000)
405 st->codec->sample_rate = 16000;
406 break;
8eb793c4 407 default:
72415b2a 408 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
d6b9e57a
LA
409 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
410 }
8eb793c4
LA
411 break;
412 }
413 }
414 // needed to send back RTCP RR in RTSP sessions
415 s->rtp_ctx = rtpc;
416 gethostname(s->hostname, sizeof(s->hostname));
417 return s;
418}
419
99a1d191
RB
420void
421rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
422 RTPDynamicProtocolHandler *handler)
423{
424 s->dynamic_protocol_context = ctx;
425 s->parse_packet = handler->parse_packet;
426}
427
8eb793c4
LA
428/**
429 * This was the second switch in rtp_parse packet. Normalizes time, if required, sets stream_index, etc.
430 */
431static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
432{
d74c6145 433 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && timestamp != RTP_NOTS_VALUE) {
fba7815d
LA
434 int64_t addend;
435 int delta_timestamp;
436
437 /* compute pts from timestamp with received ntp_time */
438 delta_timestamp = timestamp - s->last_rtcp_timestamp;
439 /* convert to the PTS timebase */
2cab6b48 440 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time, s->st->time_base.den, (uint64_t)s->st->time_base.num << 32);
5948f822 441 pkt->pts = s->range_start_offset + addend + delta_timestamp;
fba7815d 442 }
8eb793c4
LA
443}
444
02607418
MS
445static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
446 const uint8_t *buf, int len)
8eb793c4
LA
447{
448 unsigned int ssrc, h;
f841a0fc 449 int payload_type, seq, ret, flags = 0;
9446b4bb 450 int ext;
8eb793c4
LA
451 AVStream *st;
452 uint32_t timestamp;
453 int rv= 0;
454
9446b4bb 455 ext = buf[0] & 0x10;
8eb793c4 456 payload_type = buf[1] & 0x7f;
144ae29d
RB
457 if (buf[1] & 0x80)
458 flags |= RTP_FLAG_MARKER;
8eb793c4
LA
459 seq = AV_RB16(buf + 2);
460 timestamp = AV_RB32(buf + 4);
461 ssrc = AV_RB32(buf + 8);
462 /* store the ssrc in the RTPDemuxContext */
463 s->ssrc = ssrc;
464
465 /* NOTE: we can handle only one payload type */
466 if (s->payload_type != payload_type)
467 return -1;
468
469 st = s->st;
470 // only do something with this if all the rtp checks pass...
471 if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
472 {
473 av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
474 payload_type, seq, ((s->seq + 1) & 0xffff));
475 return -1;
476 }
477
478 s->seq = seq;
479 len -= 12;
480 buf += 12;
481
9446b4bb
RS
482 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
483 if (ext) {
484 if (len < 4)
485 return -1;
486 /* calculate the header extension length (stored as number
487 * of 32-bit words) */
488 ext = (AV_RB16(buf + 2) + 1) << 2;
489
490 if (len < ext)
491 return -1;
492 // skip past RTP header extension
493 len -= ext;
494 buf += ext;
495 }
496
8eb793c4
LA
497 if (!st) {
498 /* specific MPEG2TS demux support */
9125806e 499 ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
946df059
MS
500 /* The only error that can be returned from ff_mpegts_parse_packet
501 * is "no more data to return from the provided buffer", so return
502 * AVERROR(EAGAIN) for all errors */
4ffff367 503 if (ret < 0)
946df059 504 return AVERROR(EAGAIN);
8eb793c4
LA
505 if (ret < len) {
506 s->read_buf_size = len - ret;
507 memcpy(s->buf, buf + ret, s->read_buf_size);
508 s->read_buf_index = 0;
509 return 1;
510 }
f3e71942 511 return 0;
b4e3330c 512 } else if (s->parse_packet) {
1a45a9f4 513 rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
9b932b8a 514 s->st, pkt, &timestamp, buf, len, flags);
8eb793c4
LA
515 } else {
516 // at this point, the RTP header has been stripped; This is ASSUMING that there is only 1 CSRC, which in't wise.
517 switch(st->codec->codec_id) {
518 case CODEC_ID_MP2:
76faff6e 519 case CODEC_ID_MP3:
8eb793c4
LA
520 /* better than nothing: skip mpeg audio RTP header */
521 if (len <= 4)
522 return -1;
523 h = AV_RB32(buf);
524 len -= 4;
525 buf += 4;
526 av_new_packet(pkt, len);
527 memcpy(pkt->data, buf, len);
528 break;
529 case CODEC_ID_MPEG1VIDEO:
530 case CODEC_ID_MPEG2VIDEO:
531 /* better than nothing: skip mpeg video RTP header */
532 if (len <= 4)
533 return -1;
534 h = AV_RB32(buf);
535 buf += 4;
536 len -= 4;
537 if (h & (1 << 26)) {
538 /* mpeg2 */
539 if (len <= 4)
540 return -1;
541 buf += 4;
542 len -= 4;
543 }
544 av_new_packet(pkt, len);
545 memcpy(pkt->data, buf, len);
546 break;
8eb793c4 547 default:
f739b36d
RB
548 av_new_packet(pkt, len);
549 memcpy(pkt->data, buf, len);
8eb793c4
LA
550 break;
551 }
eafb17d1
RB
552
553 pkt->stream_index = st->index;
f3e71942 554 }
8eb793c4 555
95f03cf3
RB
556 // now perform timestamp things....
557 finalize_packet(s, pkt, timestamp);
f3e71942 558
8eb793c4
LA
559 return rv;
560}
561
58ee0991
MS
562void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
563{
564 while (s->queue) {
565 RTPPacket *next = s->queue->next;
566 av_free(s->queue->buf);
567 av_free(s->queue);
568 s->queue = next;
569 }
570 s->seq = 0;
571 s->queue_len = 0;
572 s->prev_ret = 0;
573}
574
575static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
576{
577 uint16_t seq = AV_RB16(buf + 2);
578 RTPPacket *cur = s->queue, *prev = NULL, *packet;
579
580 /* Find the correct place in the queue to insert the packet */
581 while (cur) {
582 int16_t diff = seq - cur->seq;
583 if (diff < 0)
584 break;
585 prev = cur;
586 cur = cur->next;
587 }
588
589 packet = av_mallocz(sizeof(*packet));
590 if (!packet)
591 return;
592 packet->recvtime = av_gettime();
593 packet->seq = seq;
594 packet->len = len;
595 packet->buf = buf;
596 packet->next = cur;
597 if (prev)
598 prev->next = packet;
599 else
600 s->queue = packet;
601 s->queue_len++;
602}
603
604static int has_next_packet(RTPDemuxContext *s)
605{
ddcf8411 606 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
58ee0991
MS
607}
608
609int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
610{
611 return s->queue ? s->queue->recvtime : 0;
612}
613
614static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
615{
616 int rv;
617 RTPPacket *next;
618
619 if (s->queue_len <= 0)
620 return -1;
621
622 if (!has_next_packet(s))
623 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
624 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
625
626 /* Parse the first packet in the queue, and dequeue it */
627 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
628 next = s->queue->next;
629 av_free(s->queue->buf);
630 av_free(s->queue);
631 s->queue = next;
632 s->queue_len--;
4ffff367 633 return rv;
58ee0991
MS
634}
635
4ffff367 636static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
02607418
MS
637 uint8_t **bufptr, int len)
638{
639 uint8_t* buf = bufptr ? *bufptr : NULL;
640 int ret, flags = 0;
641 uint32_t timestamp;
642 int rv= 0;
643
644 if (!buf) {
f6e138b4
MS
645 /* If parsing of the previous packet actually returned 0 or an error,
646 * there's nothing more to be parsed from that packet, but we may have
58ee0991 647 * indicated that we can return the next enqueued packet. */
f6e138b4 648 if (s->prev_ret <= 0)
58ee0991 649 return rtp_parse_queued_packet(s, pkt);
02607418
MS
650 /* return the next packets, if any */
651 if(s->st && s->parse_packet) {
652 /* timestamp should be overwritten by parse_packet, if not,
653 * the packet is left with pts == AV_NOPTS_VALUE */
654 timestamp = RTP_NOTS_VALUE;
655 rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
656 s->st, pkt, &timestamp, NULL, 0, flags);
657 finalize_packet(s, pkt, timestamp);
4ffff367 658 return rv;
02607418
MS
659 } else {
660 // TODO: Move to a dynamic packet handler (like above)
4ffff367 661 if (s->read_buf_index >= s->read_buf_size)
91ec7aea 662 return AVERROR(EAGAIN);
02607418
MS
663 ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
664 s->read_buf_size - s->read_buf_index);
4ffff367 665 if (ret < 0)
946df059 666 return AVERROR(EAGAIN);
02607418
MS
667 s->read_buf_index += ret;
668 if (s->read_buf_index < s->read_buf_size)
669 return 1;
4ffff367
MS
670 else
671 return 0;
02607418
MS
672 }
673 }
674
675 if (len < 12)
676 return -1;
677
678 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
679 return -1;
680 if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
681 return rtcp_parse_packet(s, buf, len);
682 }
683
65cdee9c 684 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
58ee0991
MS
685 /* First packet, or no reordering */
686 return rtp_parse_packet_internal(s, pkt, buf, len);
687 } else {
688 uint16_t seq = AV_RB16(buf + 2);
689 int16_t diff = seq - s->seq;
690 if (diff < 0) {
691 /* Packet older than the previously emitted one, drop */
692 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
693 "RTP: dropping old packet received too late\n");
694 return -1;
695 } else if (diff <= 1) {
696 /* Correct packet */
697 rv = rtp_parse_packet_internal(s, pkt, buf, len);
4ffff367 698 return rv;
58ee0991
MS
699 } else {
700 /* Still missing some packet, enqueue this one. */
701 enqueue_packet(s, buf, len);
702 *bufptr = NULL;
703 /* Return the first enqueued packet if the queue is full,
704 * even if we're missing something */
705 if (s->queue_len >= s->queue_size)
706 return rtp_parse_queued_packet(s, pkt);
707 return -1;
708 }
709 }
02607418
MS
710}
711
4ffff367
MS
712/**
713 * Parse an RTP or RTCP packet directly sent as a buffer.
714 * @param s RTP parse context.
715 * @param pkt returned packet
716 * @param bufptr pointer to the input buffer or NULL to read the next packets
717 * @param len buffer len
718 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
719 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
720 */
721int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
722 uint8_t **bufptr, int len)
723{
724 int rv = rtp_parse_one_packet(s, pkt, bufptr, len);
725 s->prev_ret = rv;
d678a6fd
MS
726 while (rv == AVERROR(EAGAIN) && has_next_packet(s))
727 rv = rtp_parse_queued_packet(s, pkt);
4ffff367
MS
728 return rv ? rv : has_next_packet(s);
729}
730
8eb793c4
LA
731void rtp_parse_close(RTPDemuxContext *s)
732{
58ee0991 733 ff_rtp_reset_packet_queue(s);
8eb793c4 734 if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
9125806e 735 ff_mpegts_parse_close(s->ts);
8eb793c4
LA
736 }
737 av_free(s);
738}
016bc031
JA
739
740int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
741 int (*parse_fmtp)(AVStream *stream,
742 PayloadContext *data,
743 char *attr, char *value))
744{
745 char attr[256];
824535e3 746 char *value;
016bc031 747 int res;
824535e3
JA
748 int value_size = strlen(p) + 1;
749
750 if (!(value = av_malloc(value_size))) {
751 av_log(stream, AV_LOG_ERROR, "Failed to allocate data for FMTP.");
752 return AVERROR(ENOMEM);
753 }
016bc031
JA
754
755 // remove protocol identifier
756 while (*p && *p == ' ') p++; // strip spaces
757 while (*p && *p != ' ') p++; // eat protocol identifier
758 while (*p && *p == ' ') p++; // strip trailing spaces
759
760 while (ff_rtsp_next_attr_and_value(&p,
761 attr, sizeof(attr),
824535e3 762 value, value_size)) {
016bc031
JA
763
764 res = parse_fmtp(stream, data, attr, value);
824535e3
JA
765 if (res < 0 && res != AVERROR_PATCHWELCOME) {
766 av_free(value);
016bc031 767 return res;
824535e3 768 }
016bc031 769 }
824535e3 770 av_free(value);
016bc031
JA
771 return 0;
772}