OSDN Git Service

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