]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec.c
avcodec, avformat: deprecate anything related to side data merging
[ffmpeg] / libavformat / rtpdec.c
1 /*
2  * RTP input format
3  * Copyright (c) 2002 Fabrice Bellard
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  */
21
22 #include "libavutil/mathematics.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/time.h"
26
27 #include "avformat.h"
28 #include "network.h"
29 #include "srtp.h"
30 #include "url.h"
31 #include "rtpdec.h"
32 #include "rtpdec_formats.h"
33
34 #define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */
35
36 static RTPDynamicProtocolHandler l24_dynamic_handler = {
37     .enc_name   = "L24",
38     .codec_type = AVMEDIA_TYPE_AUDIO,
39     .codec_id   = AV_CODEC_ID_PCM_S24BE,
40 };
41
42 static RTPDynamicProtocolHandler gsm_dynamic_handler = {
43     .enc_name   = "GSM",
44     .codec_type = AVMEDIA_TYPE_AUDIO,
45     .codec_id   = AV_CODEC_ID_GSM,
46 };
47
48 static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
49     .enc_name   = "X-MP3-draft-00",
50     .codec_type = AVMEDIA_TYPE_AUDIO,
51     .codec_id   = AV_CODEC_ID_MP3ADU,
52 };
53
54 static RTPDynamicProtocolHandler speex_dynamic_handler = {
55     .enc_name   = "speex",
56     .codec_type = AVMEDIA_TYPE_AUDIO,
57     .codec_id   = AV_CODEC_ID_SPEEX,
58 };
59
60 static RTPDynamicProtocolHandler opus_dynamic_handler = {
61     .enc_name   = "opus",
62     .codec_type = AVMEDIA_TYPE_AUDIO,
63     .codec_id   = AV_CODEC_ID_OPUS,
64 };
65
66 static RTPDynamicProtocolHandler t140_dynamic_handler = { /* RFC 4103 */
67     .enc_name   = "t140",
68     .codec_type = AVMEDIA_TYPE_SUBTITLE,
69     .codec_id   = AV_CODEC_ID_TEXT,
70 };
71
72 static RTPDynamicProtocolHandler *rtp_first_dynamic_payload_handler = NULL;
73
74 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
75 {
76     handler->next = rtp_first_dynamic_payload_handler;
77     rtp_first_dynamic_payload_handler = handler;
78 }
79
80 void ff_register_rtp_dynamic_payload_handlers(void)
81 {
82     ff_register_dynamic_payload_handler(&ff_ac3_dynamic_handler);
83     ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
84     ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
85     ff_register_dynamic_payload_handler(&ff_dv_dynamic_handler);
86     ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
87     ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
88     ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
89     ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
90     ff_register_dynamic_payload_handler(&ff_g726le_16_dynamic_handler);
91     ff_register_dynamic_payload_handler(&ff_g726le_24_dynamic_handler);
92     ff_register_dynamic_payload_handler(&ff_g726le_32_dynamic_handler);
93     ff_register_dynamic_payload_handler(&ff_g726le_40_dynamic_handler);
94     ff_register_dynamic_payload_handler(&ff_h261_dynamic_handler);
95     ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
96     ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
97     ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
98     ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
99     ff_register_dynamic_payload_handler(&ff_hevc_dynamic_handler);
100     ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
101     ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
102     ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
103     ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
104     ff_register_dynamic_payload_handler(&ff_mpeg_audio_dynamic_handler);
105     ff_register_dynamic_payload_handler(&ff_mpeg_audio_robust_dynamic_handler);
106     ff_register_dynamic_payload_handler(&ff_mpeg_video_dynamic_handler);
107     ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
108     ff_register_dynamic_payload_handler(&ff_mpegts_dynamic_handler);
109     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
110     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
111     ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
112     ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
113     ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
114     ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
115     ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
116     ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
117     ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
118     ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
119     ff_register_dynamic_payload_handler(&ff_vc2hq_dynamic_handler);
120     ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
121     ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
122     ff_register_dynamic_payload_handler(&ff_vp9_dynamic_handler);
123     ff_register_dynamic_payload_handler(&gsm_dynamic_handler);
124     ff_register_dynamic_payload_handler(&l24_dynamic_handler);
125     ff_register_dynamic_payload_handler(&opus_dynamic_handler);
126     ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
127     ff_register_dynamic_payload_handler(&speex_dynamic_handler);
128     ff_register_dynamic_payload_handler(&t140_dynamic_handler);
129 }
130
131 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
132                                                        enum AVMediaType codec_type)
133 {
134     RTPDynamicProtocolHandler *handler;
135     for (handler = rtp_first_dynamic_payload_handler;
136          handler; handler = handler->next)
137         if (handler->enc_name &&
138             !av_strcasecmp(name, handler->enc_name) &&
139             codec_type == handler->codec_type)
140             return handler;
141     return NULL;
142 }
143
144 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
145                                                      enum AVMediaType codec_type)
146 {
147     RTPDynamicProtocolHandler *handler;
148     for (handler = rtp_first_dynamic_payload_handler;
149          handler; handler = handler->next)
150         if (handler->static_payload_id && handler->static_payload_id == id &&
151             codec_type == handler->codec_type)
152             return handler;
153     return NULL;
154 }
155
156 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
157                              int len)
158 {
159     int payload_len;
160     while (len >= 4) {
161         payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
162
163         switch (buf[1]) {
164         case RTCP_SR:
165             if (payload_len < 20) {
166                 av_log(s->ic, AV_LOG_ERROR, "Invalid RTCP SR packet length\n");
167                 return AVERROR_INVALIDDATA;
168             }
169
170             s->last_rtcp_reception_time = av_gettime_relative();
171             s->last_rtcp_ntp_time  = AV_RB64(buf + 8);
172             s->last_rtcp_timestamp = AV_RB32(buf + 16);
173             if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
174                 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
175                 if (!s->base_timestamp)
176                     s->base_timestamp = s->last_rtcp_timestamp;
177                 s->rtcp_ts_offset = (int32_t)(s->last_rtcp_timestamp - s->base_timestamp);
178             }
179
180             break;
181         case RTCP_BYE:
182             return -RTCP_BYE;
183         }
184
185         buf += payload_len;
186         len -= payload_len;
187     }
188     return -1;
189 }
190
191 #define RTP_SEQ_MOD (1 << 16)
192
193 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
194 {
195     memset(s, 0, sizeof(RTPStatistics));
196     s->max_seq   = base_sequence;
197     s->probation = 1;
198 }
199
200 /*
201  * Called whenever there is a large jump in sequence numbers,
202  * or when they get out of probation...
203  */
204 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
205 {
206     s->max_seq        = seq;
207     s->cycles         = 0;
208     s->base_seq       = seq - 1;
209     s->bad_seq        = RTP_SEQ_MOD + 1;
210     s->received       = 0;
211     s->expected_prior = 0;
212     s->received_prior = 0;
213     s->jitter         = 0;
214     s->transit        = 0;
215 }
216
217 /* Returns 1 if we should handle this packet. */
218 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
219 {
220     uint16_t udelta = seq - s->max_seq;
221     const int MAX_DROPOUT    = 3000;
222     const int MAX_MISORDER   = 100;
223     const int MIN_SEQUENTIAL = 2;
224
225     /* source not valid until MIN_SEQUENTIAL packets with sequence
226      * seq. numbers have been received */
227     if (s->probation) {
228         if (seq == s->max_seq + 1) {
229             s->probation--;
230             s->max_seq = seq;
231             if (s->probation == 0) {
232                 rtp_init_sequence(s, seq);
233                 s->received++;
234                 return 1;
235             }
236         } else {
237             s->probation = MIN_SEQUENTIAL - 1;
238             s->max_seq   = seq;
239         }
240     } else if (udelta < MAX_DROPOUT) {
241         // in order, with permissible gap
242         if (seq < s->max_seq) {
243             // sequence number wrapped; count another 64k cycles
244             s->cycles += RTP_SEQ_MOD;
245         }
246         s->max_seq = seq;
247     } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
248         // sequence made a large jump...
249         if (seq == s->bad_seq) {
250             /* two sequential packets -- assume that the other side
251              * restarted without telling us; just resync. */
252             rtp_init_sequence(s, seq);
253         } else {
254             s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
255             return 0;
256         }
257     } else {
258         // duplicate or reordered packet...
259     }
260     s->received++;
261     return 1;
262 }
263
264 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
265                                uint32_t arrival_timestamp)
266 {
267     // Most of this is pretty straight from RFC 3550 appendix A.8
268     uint32_t transit = arrival_timestamp - sent_timestamp;
269     uint32_t prev_transit = s->transit;
270     int32_t d = transit - prev_transit;
271     // Doing the FFABS() call directly on the "transit - prev_transit"
272     // expression doesn't work, since it's an unsigned expression. Doing the
273     // transit calculation in unsigned is desired though, since it most
274     // probably will need to wrap around.
275     d = FFABS(d);
276     s->transit = transit;
277     if (!prev_transit)
278         return;
279     s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
280 }
281
282 int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
283                                   AVIOContext *avio, int count)
284 {
285     AVIOContext *pb;
286     uint8_t *buf;
287     int len;
288     int rtcp_bytes;
289     RTPStatistics *stats = &s->statistics;
290     uint32_t lost;
291     uint32_t extended_max;
292     uint32_t expected_interval;
293     uint32_t received_interval;
294     int32_t  lost_interval;
295     uint32_t expected;
296     uint32_t fraction;
297
298     if ((!fd && !avio) || (count < 1))
299         return -1;
300
301     /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
302     /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
303     s->octet_count += count;
304     rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
305         RTCP_TX_RATIO_DEN;
306     rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
307     if (rtcp_bytes < 28)
308         return -1;
309     s->last_octet_count = s->octet_count;
310
311     if (!fd)
312         pb = avio;
313     else if (avio_open_dyn_buf(&pb) < 0)
314         return -1;
315
316     // Receiver Report
317     avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
318     avio_w8(pb, RTCP_RR);
319     avio_wb16(pb, 7); /* length in words - 1 */
320     // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
321     avio_wb32(pb, s->ssrc + 1);
322     avio_wb32(pb, s->ssrc); // server SSRC
323     // some placeholders we should really fill...
324     // RFC 1889/p64
325     extended_max          = stats->cycles + stats->max_seq;
326     expected              = extended_max - stats->base_seq;
327     lost                  = expected - stats->received;
328     lost                  = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
329     expected_interval     = expected - stats->expected_prior;
330     stats->expected_prior = expected;
331     received_interval     = stats->received - stats->received_prior;
332     stats->received_prior = stats->received;
333     lost_interval         = expected_interval - received_interval;
334     if (expected_interval == 0 || lost_interval <= 0)
335         fraction = 0;
336     else
337         fraction = (lost_interval << 8) / expected_interval;
338
339     fraction = (fraction << 24) | lost;
340
341     avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
342     avio_wb32(pb, extended_max); /* max sequence received */
343     avio_wb32(pb, stats->jitter >> 4); /* jitter */
344
345     if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
346         avio_wb32(pb, 0); /* last SR timestamp */
347         avio_wb32(pb, 0); /* delay since last SR */
348     } else {
349         uint32_t middle_32_bits   = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
350         uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time,
351                                                65536, AV_TIME_BASE);
352
353         avio_wb32(pb, middle_32_bits); /* last SR timestamp */
354         avio_wb32(pb, delay_since_last); /* delay since last SR */
355     }
356
357     // CNAME
358     avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
359     avio_w8(pb, RTCP_SDES);
360     len = strlen(s->hostname);
361     avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
362     avio_wb32(pb, s->ssrc + 1);
363     avio_w8(pb, 0x01);
364     avio_w8(pb, len);
365     avio_write(pb, s->hostname, len);
366     avio_w8(pb, 0); /* END */
367     // padding
368     for (len = (7 + len) % 4; len % 4; len++)
369         avio_w8(pb, 0);
370
371     avio_flush(pb);
372     if (!fd)
373         return 0;
374     len = avio_close_dyn_buf(pb, &buf);
375     if ((len > 0) && buf) {
376         int av_unused result;
377         av_log(s->ic, AV_LOG_TRACE, "sending %d bytes of RR\n", len);
378         result = ffurl_write(fd, buf, len);
379         av_log(s->ic, AV_LOG_TRACE, "result from ffurl_write: %d\n", result);
380         av_free(buf);
381     }
382     return 0;
383 }
384
385 void ff_rtp_send_punch_packets(URLContext *rtp_handle)
386 {
387     AVIOContext *pb;
388     uint8_t *buf;
389     int len;
390
391     /* Send a small RTP packet */
392     if (avio_open_dyn_buf(&pb) < 0)
393         return;
394
395     avio_w8(pb, (RTP_VERSION << 6));
396     avio_w8(pb, 0); /* Payload type */
397     avio_wb16(pb, 0); /* Seq */
398     avio_wb32(pb, 0); /* Timestamp */
399     avio_wb32(pb, 0); /* SSRC */
400
401     avio_flush(pb);
402     len = avio_close_dyn_buf(pb, &buf);
403     if ((len > 0) && buf)
404         ffurl_write(rtp_handle, buf, len);
405     av_free(buf);
406
407     /* Send a minimal RTCP RR */
408     if (avio_open_dyn_buf(&pb) < 0)
409         return;
410
411     avio_w8(pb, (RTP_VERSION << 6));
412     avio_w8(pb, RTCP_RR); /* receiver report */
413     avio_wb16(pb, 1); /* length in words - 1 */
414     avio_wb32(pb, 0); /* our own SSRC */
415
416     avio_flush(pb);
417     len = avio_close_dyn_buf(pb, &buf);
418     if ((len > 0) && buf)
419         ffurl_write(rtp_handle, buf, len);
420     av_free(buf);
421 }
422
423 static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
424                                 uint16_t *missing_mask)
425 {
426     int i;
427     uint16_t next_seq = s->seq + 1;
428     RTPPacket *pkt = s->queue;
429
430     if (!pkt || pkt->seq == next_seq)
431         return 0;
432
433     *missing_mask = 0;
434     for (i = 1; i <= 16; i++) {
435         uint16_t missing_seq = next_seq + i;
436         while (pkt) {
437             int16_t diff = pkt->seq - missing_seq;
438             if (diff >= 0)
439                 break;
440             pkt = pkt->next;
441         }
442         if (!pkt)
443             break;
444         if (pkt->seq == missing_seq)
445             continue;
446         *missing_mask |= 1 << (i - 1);
447     }
448
449     *first_missing = next_seq;
450     return 1;
451 }
452
453 int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
454                               AVIOContext *avio)
455 {
456     int len, need_keyframe, missing_packets;
457     AVIOContext *pb;
458     uint8_t *buf;
459     int64_t now;
460     uint16_t first_missing = 0, missing_mask = 0;
461
462     if (!fd && !avio)
463         return -1;
464
465     need_keyframe = s->handler && s->handler->need_keyframe &&
466                     s->handler->need_keyframe(s->dynamic_protocol_context);
467     missing_packets = find_missing_packets(s, &first_missing, &missing_mask);
468
469     if (!need_keyframe && !missing_packets)
470         return 0;
471
472     /* Send new feedback if enough time has elapsed since the last
473      * feedback packet. */
474
475     now = av_gettime_relative();
476     if (s->last_feedback_time &&
477         (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL)
478         return 0;
479     s->last_feedback_time = now;
480
481     if (!fd)
482         pb = avio;
483     else if (avio_open_dyn_buf(&pb) < 0)
484         return -1;
485
486     if (need_keyframe) {
487         avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */
488         avio_w8(pb, RTCP_PSFB);
489         avio_wb16(pb, 2); /* length in words - 1 */
490         // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
491         avio_wb32(pb, s->ssrc + 1);
492         avio_wb32(pb, s->ssrc); // server SSRC
493     }
494
495     if (missing_packets) {
496         avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */
497         avio_w8(pb, RTCP_RTPFB);
498         avio_wb16(pb, 3); /* length in words - 1 */
499         avio_wb32(pb, s->ssrc + 1);
500         avio_wb32(pb, s->ssrc); // server SSRC
501
502         avio_wb16(pb, first_missing);
503         avio_wb16(pb, missing_mask);
504     }
505
506     avio_flush(pb);
507     if (!fd)
508         return 0;
509     len = avio_close_dyn_buf(pb, &buf);
510     if (len > 0 && buf) {
511         ffurl_write(fd, buf, len);
512         av_free(buf);
513     }
514     return 0;
515 }
516
517 /**
518  * open a new RTP parse context for stream 'st'. 'st' can be NULL for
519  * MPEG-2 TS streams.
520  */
521 RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
522                                    int payload_type, int queue_size)
523 {
524     RTPDemuxContext *s;
525
526     s = av_mallocz(sizeof(RTPDemuxContext));
527     if (!s)
528         return NULL;
529     s->payload_type        = payload_type;
530     s->last_rtcp_ntp_time  = AV_NOPTS_VALUE;
531     s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
532     s->ic                  = s1;
533     s->st                  = st;
534     s->queue_size          = queue_size;
535
536     av_log(s->ic, AV_LOG_VERBOSE, "setting jitter buffer size to %d\n",
537            s->queue_size);
538
539     rtp_init_statistics(&s->statistics, 0);
540     if (st) {
541         switch (st->codecpar->codec_id) {
542         case AV_CODEC_ID_ADPCM_G722:
543             /* According to RFC 3551, the stream clock rate is 8000
544              * even if the sample rate is 16000. */
545             if (st->codecpar->sample_rate == 8000)
546                 st->codecpar->sample_rate = 16000;
547             break;
548         default:
549             break;
550         }
551     }
552     // needed to send back RTCP RR in RTSP sessions
553     gethostname(s->hostname, sizeof(s->hostname));
554     return s;
555 }
556
557 void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
558                                        RTPDynamicProtocolHandler *handler)
559 {
560     s->dynamic_protocol_context = ctx;
561     s->handler                  = handler;
562 }
563
564 void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
565                              const char *params)
566 {
567     if (!ff_srtp_set_crypto(&s->srtp, suite, params))
568         s->srtp_enabled = 1;
569 }
570
571 /**
572  * This was the second switch in rtp_parse packet.
573  * Normalizes time, if required, sets stream_index, etc.
574  */
575 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
576 {
577     if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
578         return; /* Timestamp already set by depacketizer */
579     if (timestamp == RTP_NOTS_VALUE)
580         return;
581
582     if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
583         int64_t addend;
584         int delta_timestamp;
585
586         /* compute pts from timestamp with received ntp_time */
587         delta_timestamp = timestamp - s->last_rtcp_timestamp;
588         /* convert to the PTS timebase */
589         addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time,
590                             s->st->time_base.den,
591                             (uint64_t) s->st->time_base.num << 32);
592         pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
593                    delta_timestamp;
594         return;
595     }
596
597     if (!s->base_timestamp)
598         s->base_timestamp = timestamp;
599     /* assume that the difference is INT32_MIN < x < INT32_MAX,
600      * but allow the first timestamp to exceed INT32_MAX */
601     if (!s->timestamp)
602         s->unwrapped_timestamp += timestamp;
603     else
604         s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
605     s->timestamp = timestamp;
606     pkt->pts     = s->unwrapped_timestamp + s->range_start_offset -
607                    s->base_timestamp;
608 }
609
610 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
611                                      const uint8_t *buf, int len)
612 {
613     unsigned int ssrc;
614     int payload_type, seq, flags = 0;
615     int ext, csrc;
616     AVStream *st;
617     uint32_t timestamp;
618     int rv = 0;
619
620     csrc         = buf[0] & 0x0f;
621     ext          = buf[0] & 0x10;
622     payload_type = buf[1] & 0x7f;
623     if (buf[1] & 0x80)
624         flags |= RTP_FLAG_MARKER;
625     seq       = AV_RB16(buf + 2);
626     timestamp = AV_RB32(buf + 4);
627     ssrc      = AV_RB32(buf + 8);
628     /* store the ssrc in the RTPDemuxContext */
629     s->ssrc = ssrc;
630
631     /* NOTE: we can handle only one payload type */
632     if (s->payload_type != payload_type)
633         return -1;
634
635     st = s->st;
636     // only do something with this if all the rtp checks pass...
637     if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
638         av_log(s->ic, AV_LOG_ERROR,
639                "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
640                payload_type, seq, ((s->seq + 1) & 0xffff));
641         return -1;
642     }
643
644     if (buf[0] & 0x20) {
645         int padding = buf[len - 1];
646         if (len >= 12 + padding)
647             len -= padding;
648     }
649
650     s->seq = seq;
651     len   -= 12;
652     buf   += 12;
653
654     len   -= 4 * csrc;
655     buf   += 4 * csrc;
656     if (len < 0)
657         return AVERROR_INVALIDDATA;
658
659     /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
660     if (ext) {
661         if (len < 4)
662             return -1;
663         /* calculate the header extension length (stored as number
664          * of 32-bit words) */
665         ext = (AV_RB16(buf + 2) + 1) << 2;
666
667         if (len < ext)
668             return -1;
669         // skip past RTP header extension
670         len -= ext;
671         buf += ext;
672     }
673
674     if (s->handler && s->handler->parse_packet) {
675         rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
676                                       s->st, pkt, &timestamp, buf, len, seq,
677                                       flags);
678     } else if (st) {
679         if ((rv = av_new_packet(pkt, len)) < 0)
680             return rv;
681         memcpy(pkt->data, buf, len);
682         pkt->stream_index = st->index;
683     } else {
684         return AVERROR(EINVAL);
685     }
686
687     // now perform timestamp things....
688     finalize_packet(s, pkt, timestamp);
689
690     return rv;
691 }
692
693 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
694 {
695     while (s->queue) {
696         RTPPacket *next = s->queue->next;
697         av_freep(&s->queue->buf);
698         av_freep(&s->queue);
699         s->queue = next;
700     }
701     s->seq       = 0;
702     s->queue_len = 0;
703     s->prev_ret  = 0;
704 }
705
706 static int enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
707 {
708     uint16_t seq   = AV_RB16(buf + 2);
709     RTPPacket **cur = &s->queue, *packet;
710
711     /* Find the correct place in the queue to insert the packet */
712     while (*cur) {
713         int16_t diff = seq - (*cur)->seq;
714         if (diff < 0)
715             break;
716         cur = &(*cur)->next;
717     }
718
719     packet = av_mallocz(sizeof(*packet));
720     if (!packet)
721         return AVERROR(ENOMEM);
722     packet->recvtime = av_gettime_relative();
723     packet->seq      = seq;
724     packet->len      = len;
725     packet->buf      = buf;
726     packet->next     = *cur;
727     *cur = packet;
728     s->queue_len++;
729
730     return 0;
731 }
732
733 static int has_next_packet(RTPDemuxContext *s)
734 {
735     return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
736 }
737
738 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
739 {
740     return s->queue ? s->queue->recvtime : 0;
741 }
742
743 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
744 {
745     int rv;
746     RTPPacket *next;
747
748     if (s->queue_len <= 0)
749         return -1;
750
751     if (!has_next_packet(s))
752         av_log(s->ic, AV_LOG_WARNING,
753                "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
754
755     /* Parse the first packet in the queue, and dequeue it */
756     rv   = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
757     next = s->queue->next;
758     av_freep(&s->queue->buf);
759     av_freep(&s->queue);
760     s->queue = next;
761     s->queue_len--;
762     return rv;
763 }
764
765 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
766                                 uint8_t **bufptr, int len)
767 {
768     uint8_t *buf = bufptr ? *bufptr : NULL;
769     int flags = 0;
770     uint32_t timestamp;
771     int rv = 0;
772
773     if (!buf) {
774         /* If parsing of the previous packet actually returned 0 or an error,
775          * there's nothing more to be parsed from that packet, but we may have
776          * indicated that we can return the next enqueued packet. */
777         if (s->prev_ret <= 0)
778             return rtp_parse_queued_packet(s, pkt);
779         /* return the next packets, if any */
780         if (s->handler && s->handler->parse_packet) {
781             /* timestamp should be overwritten by parse_packet, if not,
782              * the packet is left with pts == AV_NOPTS_VALUE */
783             timestamp = RTP_NOTS_VALUE;
784             rv        = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
785                                                  s->st, pkt, &timestamp, NULL, 0, 0,
786                                                  flags);
787             finalize_packet(s, pkt, timestamp);
788             return rv;
789         }
790     }
791
792     if (len < 12)
793         return -1;
794
795     if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
796         return -1;
797     if (RTP_PT_IS_RTCP(buf[1])) {
798         return rtcp_parse_packet(s, buf, len);
799     }
800
801     if (s->st) {
802         int64_t received = av_gettime_relative();
803         uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
804                                            s->st->time_base);
805         timestamp = AV_RB32(buf + 4);
806         // Calculate the jitter immediately, before queueing the packet
807         // into the reordering queue.
808         rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
809     }
810
811     if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
812         /* First packet, or no reordering */
813         return rtp_parse_packet_internal(s, pkt, buf, len);
814     } else {
815         uint16_t seq = AV_RB16(buf + 2);
816         int16_t diff = seq - s->seq;
817         if (diff < 0) {
818             /* Packet older than the previously emitted one, drop */
819             av_log(s->ic, AV_LOG_WARNING,
820                    "RTP: dropping old packet received too late\n");
821             return -1;
822         } else if (diff <= 1) {
823             /* Correct packet */
824             rv = rtp_parse_packet_internal(s, pkt, buf, len);
825             return rv;
826         } else {
827             /* Still missing some packet, enqueue this one. */
828             rv = enqueue_packet(s, buf, len);
829             if (rv < 0)
830                 return rv;
831             *bufptr = NULL;
832             /* Return the first enqueued packet if the queue is full,
833              * even if we're missing something */
834             if (s->queue_len >= s->queue_size) {
835                 av_log(s->ic, AV_LOG_WARNING, "jitter buffer full\n");
836                 return rtp_parse_queued_packet(s, pkt);
837             }
838             return -1;
839         }
840     }
841 }
842
843 /**
844  * Parse an RTP or RTCP packet directly sent as a buffer.
845  * @param s RTP parse context.
846  * @param pkt returned packet
847  * @param bufptr pointer to the input buffer or NULL to read the next packets
848  * @param len buffer len
849  * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
850  * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
851  */
852 int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
853                         uint8_t **bufptr, int len)
854 {
855     int rv;
856     if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
857         return -1;
858     rv = rtp_parse_one_packet(s, pkt, bufptr, len);
859     s->prev_ret = rv;
860     while (rv < 0 && has_next_packet(s))
861         rv = rtp_parse_queued_packet(s, pkt);
862     return rv ? rv : has_next_packet(s);
863 }
864
865 void ff_rtp_parse_close(RTPDemuxContext *s)
866 {
867     ff_rtp_reset_packet_queue(s);
868     ff_srtp_free(&s->srtp);
869     av_free(s);
870 }
871
872 int ff_parse_fmtp(AVFormatContext *s,
873                   AVStream *stream, PayloadContext *data, const char *p,
874                   int (*parse_fmtp)(AVFormatContext *s,
875                                     AVStream *stream,
876                                     PayloadContext *data,
877                                     const char *attr, const char *value))
878 {
879     char attr[256];
880     char *value;
881     int res;
882     int value_size = strlen(p) + 1;
883
884     if (!(value = av_malloc(value_size))) {
885         av_log(s, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
886         return AVERROR(ENOMEM);
887     }
888
889     // remove protocol identifier
890     while (*p && *p == ' ')
891         p++;                     // strip spaces
892     while (*p && *p != ' ')
893         p++;                     // eat protocol identifier
894     while (*p && *p == ' ')
895         p++;                     // strip trailing spaces
896
897     while (ff_rtsp_next_attr_and_value(&p,
898                                        attr, sizeof(attr),
899                                        value, value_size)) {
900         res = parse_fmtp(s, stream, data, attr, value);
901         if (res < 0 && res != AVERROR_PATCHWELCOME) {
902             av_free(value);
903             return res;
904         }
905     }
906     av_free(value);
907     return 0;
908 }
909
910 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
911 {
912     int ret;
913     av_init_packet(pkt);
914
915     pkt->size         = avio_close_dyn_buf(*dyn_buf, &pkt->data);
916     pkt->stream_index = stream_idx;
917     *dyn_buf = NULL;
918     if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) {
919         av_freep(&pkt->data);
920         return ret;
921     }
922     return pkt->size;
923 }