FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtpdec.c
Go to the documentation of this file.
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/time.h"
25 #include "libavcodec/get_bits.h"
26 #include "avformat.h"
27 #include "mpegts.h"
28 #include "network.h"
29 #include "url.h"
30 #include "rtpdec.h"
31 #include "rtpdec_formats.h"
32 
34  .enc_name = "X-MP3-draft-00",
35  .codec_type = AVMEDIA_TYPE_AUDIO,
36  .codec_id = AV_CODEC_ID_MP3ADU,
37 };
38 
40  .enc_name = "speex",
41  .codec_type = AVMEDIA_TYPE_AUDIO,
42  .codec_id = AV_CODEC_ID_SPEEX,
43 };
44 
46  .enc_name = "opus",
47  .codec_type = AVMEDIA_TYPE_AUDIO,
48  .codec_id = AV_CODEC_ID_OPUS,
49 };
50 
51 /* statistics functions */
53 
55 {
57  rtp_first_dynamic_payload_handler = handler;
58 }
59 
61 {
79  ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
80  ff_register_dynamic_payload_handler(&speex_dynamic_handler);
81  ff_register_dynamic_payload_handler(&opus_dynamic_handler);
82 
85 
90 
95 }
96 
99 {
100  RTPDynamicProtocolHandler *handler;
101  for (handler = rtp_first_dynamic_payload_handler;
102  handler; handler = handler->next)
103  if (!av_strcasecmp(name, handler->enc_name) &&
104  codec_type == handler->codec_type)
105  return handler;
106  return NULL;
107 }
108 
110  enum AVMediaType codec_type)
111 {
112  RTPDynamicProtocolHandler *handler;
113  for (handler = rtp_first_dynamic_payload_handler;
114  handler; handler = handler->next)
115  if (handler->static_payload_id && handler->static_payload_id == id &&
116  codec_type == handler->codec_type)
117  return handler;
118  return NULL;
119 }
120 
121 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
122  int len)
123 {
124  int payload_len;
125  while (len >= 4) {
126  payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
127 
128  switch (buf[1]) {
129  case RTCP_SR:
130  if (payload_len < 20) {
132  "Invalid length for RTCP SR packet\n");
133  return AVERROR_INVALIDDATA;
134  }
135 
136  s->last_rtcp_ntp_time = AV_RB64(buf + 8);
137  s->last_rtcp_timestamp = AV_RB32(buf + 16);
140  if (!s->base_timestamp)
143  }
144 
145  break;
146  case RTCP_BYE:
147  return -RTCP_BYE;
148  }
149 
150  buf += payload_len;
151  len -= payload_len;
152  }
153  return -1;
154 }
155 
156 #define RTP_SEQ_MOD (1 << 16)
157 
158 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
159 {
160  memset(s, 0, sizeof(RTPStatistics));
161  s->max_seq = base_sequence;
162  s->probation = 1;
163 }
164 
165 /*
166  * Called whenever there is a large jump in sequence numbers,
167  * or when they get out of probation...
168  */
169 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
170 {
171  s->max_seq = seq;
172  s->cycles = 0;
173  s->base_seq = seq - 1;
174  s->bad_seq = RTP_SEQ_MOD + 1;
175  s->received = 0;
176  s->expected_prior = 0;
177  s->received_prior = 0;
178  s->jitter = 0;
179  s->transit = 0;
180 }
181 
182 /* Returns 1 if we should handle this packet. */
183 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
184 {
185  uint16_t udelta = seq - s->max_seq;
186  const int MAX_DROPOUT = 3000;
187  const int MAX_MISORDER = 100;
188  const int MIN_SEQUENTIAL = 2;
189 
190  /* source not valid until MIN_SEQUENTIAL packets with sequence
191  * seq. numbers have been received */
192  if (s->probation) {
193  if (seq == s->max_seq + 1) {
194  s->probation--;
195  s->max_seq = seq;
196  if (s->probation == 0) {
197  rtp_init_sequence(s, seq);
198  s->received++;
199  return 1;
200  }
201  } else {
202  s->probation = MIN_SEQUENTIAL - 1;
203  s->max_seq = seq;
204  }
205  } else if (udelta < MAX_DROPOUT) {
206  // in order, with permissible gap
207  if (seq < s->max_seq) {
208  // sequence number wrapped; count another 64k cycles
209  s->cycles += RTP_SEQ_MOD;
210  }
211  s->max_seq = seq;
212  } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
213  // sequence made a large jump...
214  if (seq == s->bad_seq) {
215  /* two sequential packets -- assume that the other side
216  * restarted without telling us; just resync. */
217  rtp_init_sequence(s, seq);
218  } else {
219  s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
220  return 0;
221  }
222  } else {
223  // duplicate or reordered packet...
224  }
225  s->received++;
226  return 1;
227 }
228 
230  AVIOContext *avio, int count)
231 {
232  AVIOContext *pb;
233  uint8_t *buf;
234  int len;
235  int rtcp_bytes;
237  uint32_t lost;
238  uint32_t extended_max;
239  uint32_t expected_interval;
240  uint32_t received_interval;
241  uint32_t lost_interval;
242  uint32_t expected;
243  uint32_t fraction;
244  uint64_t ntp_time = s->last_rtcp_ntp_time; // TODO: Get local ntp time?
245 
246  if ((!fd && !avio) || (count < 1))
247  return -1;
248 
249  /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
250  /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
251  s->octet_count += count;
252  rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
254  rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
255  if (rtcp_bytes < 28)
256  return -1;
258 
259  if (!fd)
260  pb = avio;
261  else if (avio_open_dyn_buf(&pb) < 0)
262  return -1;
263 
264  // Receiver Report
265  avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
266  avio_w8(pb, RTCP_RR);
267  avio_wb16(pb, 7); /* length in words - 1 */
268  // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
269  avio_wb32(pb, s->ssrc + 1);
270  avio_wb32(pb, s->ssrc); // server SSRC
271  // some placeholders we should really fill...
272  // RFC 1889/p64
273  extended_max = stats->cycles + stats->max_seq;
274  expected = extended_max - stats->base_seq + 1;
275  lost = expected - stats->received;
276  lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
277  expected_interval = expected - stats->expected_prior;
278  stats->expected_prior = expected;
279  received_interval = stats->received - stats->received_prior;
280  stats->received_prior = stats->received;
281  lost_interval = expected_interval - received_interval;
282  if (expected_interval == 0 || lost_interval <= 0)
283  fraction = 0;
284  else
285  fraction = (lost_interval << 8) / expected_interval;
286 
287  fraction = (fraction << 24) | lost;
288 
289  avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
290  avio_wb32(pb, extended_max); /* max sequence received */
291  avio_wb32(pb, stats->jitter >> 4); /* jitter */
292 
294  avio_wb32(pb, 0); /* last SR timestamp */
295  avio_wb32(pb, 0); /* delay since last SR */
296  } else {
297  uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
298  uint32_t delay_since_last = ntp_time - s->last_rtcp_ntp_time;
299 
300  avio_wb32(pb, middle_32_bits); /* last SR timestamp */
301  avio_wb32(pb, delay_since_last); /* delay since last SR */
302  }
303 
304  // CNAME
305  avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
306  avio_w8(pb, RTCP_SDES);
307  len = strlen(s->hostname);
308  avio_wb16(pb, (6 + len + 3) / 4); /* length in words - 1 */
309  avio_wb32(pb, s->ssrc + 1);
310  avio_w8(pb, 0x01);
311  avio_w8(pb, len);
312  avio_write(pb, s->hostname, len);
313  // padding
314  for (len = (6 + len) % 4; len % 4; len++)
315  avio_w8(pb, 0);
316 
317  avio_flush(pb);
318  if (!fd)
319  return 0;
320  len = avio_close_dyn_buf(pb, &buf);
321  if ((len > 0) && buf) {
322  int av_unused result;
323  av_dlog(s->ic, "sending %d bytes of RR\n", len);
324  result = ffurl_write(fd, buf, len);
325  av_dlog(s->ic, "result from ffurl_write: %d\n", result);
326  av_free(buf);
327  }
328  return 0;
329 }
330 
332 {
333  AVIOContext *pb;
334  uint8_t *buf;
335  int len;
336 
337  /* Send a small RTP packet */
338  if (avio_open_dyn_buf(&pb) < 0)
339  return;
340 
341  avio_w8(pb, (RTP_VERSION << 6));
342  avio_w8(pb, 0); /* Payload type */
343  avio_wb16(pb, 0); /* Seq */
344  avio_wb32(pb, 0); /* Timestamp */
345  avio_wb32(pb, 0); /* SSRC */
346 
347  avio_flush(pb);
348  len = avio_close_dyn_buf(pb, &buf);
349  if ((len > 0) && buf)
350  ffurl_write(rtp_handle, buf, len);
351  av_free(buf);
352 
353  /* Send a minimal RTCP RR */
354  if (avio_open_dyn_buf(&pb) < 0)
355  return;
356 
357  avio_w8(pb, (RTP_VERSION << 6));
358  avio_w8(pb, RTCP_RR); /* receiver report */
359  avio_wb16(pb, 1); /* length in words - 1 */
360  avio_wb32(pb, 0); /* our own SSRC */
361 
362  avio_flush(pb);
363  len = avio_close_dyn_buf(pb, &buf);
364  if ((len > 0) && buf)
365  ffurl_write(rtp_handle, buf, len);
366  av_free(buf);
367 }
368 
369 /**
370  * open a new RTP parse context for stream 'st'. 'st' can be NULL for
371  * MPEG2-TS streams to indicate that they should be demuxed inside the
372  * rtp demux (otherwise AV_CODEC_ID_MPEG2TS packets are returned)
373  */
375  int payload_type, int queue_size)
376 {
377  RTPDemuxContext *s;
378 
379  s = av_mallocz(sizeof(RTPDemuxContext));
380  if (!s)
381  return NULL;
382  s->payload_type = payload_type;
385  s->ic = s1;
386  s->st = st;
387  s->queue_size = queue_size;
388  rtp_init_statistics(&s->statistics, 0); // do we know the initial sequence from sdp?
389  if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
390  s->ts = ff_mpegts_parse_open(s->ic);
391  if (s->ts == NULL) {
392  av_free(s);
393  return NULL;
394  }
395  } else if (st) {
396  switch (st->codec->codec_id) {
399  case AV_CODEC_ID_MP2:
400  case AV_CODEC_ID_MP3:
401  case AV_CODEC_ID_MPEG4:
402  case AV_CODEC_ID_H263:
403  case AV_CODEC_ID_H264:
405  break;
406  case AV_CODEC_ID_VORBIS:
408  break;
410  /* According to RFC 3551, the stream clock rate is 8000
411  * even if the sample rate is 16000. */
412  if (st->codec->sample_rate == 8000)
413  st->codec->sample_rate = 16000;
414  break;
415  default:
416  break;
417  }
418  }
419  // needed to send back RTCP RR in RTSP sessions
420  gethostname(s->hostname, sizeof(s->hostname));
421  return s;
422 }
423 
425  RTPDynamicProtocolHandler *handler)
426 {
427  s->dynamic_protocol_context = ctx;
428  s->parse_packet = handler->parse_packet;
429 }
430 
431 /**
432  * This was the second switch in rtp_parse packet.
433  * Normalizes time, if required, sets stream_index, etc.
434  */
435 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
436 {
437  if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
438  return; /* Timestamp already set by depacketizer */
439  if (timestamp == RTP_NOTS_VALUE)
440  return;
441 
442  if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
443  int64_t addend;
444  int delta_timestamp;
445 
446  /* compute pts from timestamp with received ntp_time */
447  delta_timestamp = timestamp - s->last_rtcp_timestamp;
448  /* convert to the PTS timebase */
450  s->st->time_base.den,
451  (uint64_t) s->st->time_base.num << 32);
452  pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
453  delta_timestamp;
454  return;
455  }
456 
457  if (!s->base_timestamp)
458  s->base_timestamp = timestamp;
459  /* assume that the difference is INT32_MIN < x < INT32_MAX,
460  * but allow the first timestamp to exceed INT32_MAX */
461  if (!s->timestamp)
462  s->unwrapped_timestamp += timestamp;
463  else
464  s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
465  s->timestamp = timestamp;
467  s->base_timestamp;
468 }
469 
471  const uint8_t *buf, int len)
472 {
473  unsigned int ssrc, h;
474  int payload_type, seq, ret, flags = 0;
475  int ext;
476  AVStream *st;
477  uint32_t timestamp;
478  int rv = 0;
479 
480  ext = buf[0] & 0x10;
481  payload_type = buf[1] & 0x7f;
482  if (buf[1] & 0x80)
483  flags |= RTP_FLAG_MARKER;
484  seq = AV_RB16(buf + 2);
485  timestamp = AV_RB32(buf + 4);
486  ssrc = AV_RB32(buf + 8);
487  /* store the ssrc in the RTPDemuxContext */
488  s->ssrc = ssrc;
489 
490  /* NOTE: we can handle only one payload type */
491  if (s->payload_type != payload_type)
492  return -1;
493 
494  st = s->st;
495  // only do something with this if all the rtp checks pass...
496  if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
497  av_log(st ? st->codec : NULL, AV_LOG_ERROR,
498  "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
499  payload_type, seq, ((s->seq + 1) & 0xffff));
500  return -1;
501  }
502 
503  if (buf[0] & 0x20) {
504  int padding = buf[len - 1];
505  if (len >= 12 + padding)
506  len -= padding;
507  }
508 
509  s->seq = seq;
510  len -= 12;
511  buf += 12;
512 
513  /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
514  if (ext) {
515  if (len < 4)
516  return -1;
517  /* calculate the header extension length (stored as number
518  * of 32-bit words) */
519  ext = (AV_RB16(buf + 2) + 1) << 2;
520 
521  if (len < ext)
522  return -1;
523  // skip past RTP header extension
524  len -= ext;
525  buf += ext;
526  }
527 
528  if (!st) {
529  /* specific MPEG2-TS demux support */
530  ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
531  /* The only error that can be returned from ff_mpegts_parse_packet
532  * is "no more data to return from the provided buffer", so return
533  * AVERROR(EAGAIN) for all errors */
534  if (ret < 0)
535  return AVERROR(EAGAIN);
536  if (ret < len) {
537  s->read_buf_size = FFMIN(len - ret, sizeof(s->buf));
538  memcpy(s->buf, buf + ret, s->read_buf_size);
539  s->read_buf_index = 0;
540  return 1;
541  }
542  return 0;
543  } else if (s->parse_packet) {
545  s->st, pkt, &timestamp, buf, len, seq, flags);
546  } else {
547  /* At this point, the RTP header has been stripped;
548  * This is ASSUMING that there is only 1 CSRC, which isn't wise. */
549  switch (st->codec->codec_id) {
550  case AV_CODEC_ID_MP2:
551  case AV_CODEC_ID_MP3:
552  /* better than nothing: skip MPEG audio RTP header */
553  if (len <= 4)
554  return -1;
555  h = AV_RB32(buf);
556  len -= 4;
557  buf += 4;
558  if (av_new_packet(pkt, len) < 0)
559  return AVERROR(ENOMEM);
560  memcpy(pkt->data, buf, len);
561  break;
564  /* better than nothing: skip MPEG video RTP header */
565  if (len <= 4)
566  return -1;
567  h = AV_RB32(buf);
568  buf += 4;
569  len -= 4;
570  if (h & (1 << 26)) {
571  /* MPEG-2 */
572  if (len <= 4)
573  return -1;
574  buf += 4;
575  len -= 4;
576  }
577  if (av_new_packet(pkt, len) < 0)
578  return AVERROR(ENOMEM);
579  memcpy(pkt->data, buf, len);
580  break;
581  default:
582  if (av_new_packet(pkt, len) < 0)
583  return AVERROR(ENOMEM);
584  memcpy(pkt->data, buf, len);
585  break;
586  }
587 
588  pkt->stream_index = st->index;
589  }
590 
591  // now perform timestamp things....
592  finalize_packet(s, pkt, timestamp);
593 
594  return rv;
595 }
596 
598 {
599  while (s->queue) {
600  RTPPacket *next = s->queue->next;
601  av_free(s->queue->buf);
602  av_free(s->queue);
603  s->queue = next;
604  }
605  s->seq = 0;
606  s->queue_len = 0;
607  s->prev_ret = 0;
608 }
609 
610 static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
611 {
612  uint16_t seq = AV_RB16(buf + 2);
613  RTPPacket *cur = s->queue, *prev = NULL, *packet;
614 
615  /* Find the correct place in the queue to insert the packet */
616  while (cur) {
617  int16_t diff = seq - cur->seq;
618  if (diff < 0)
619  break;
620  prev = cur;
621  cur = cur->next;
622  }
623 
624  packet = av_mallocz(sizeof(*packet));
625  if (!packet)
626  return;
627  packet->recvtime = av_gettime();
628  packet->seq = seq;
629  packet->len = len;
630  packet->buf = buf;
631  packet->next = cur;
632  if (prev)
633  prev->next = packet;
634  else
635  s->queue = packet;
636  s->queue_len++;
637 }
638 
640 {
641  return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
642 }
643 
645 {
646  return s->queue ? s->queue->recvtime : 0;
647 }
648 
650 {
651  int rv;
652  RTPPacket *next;
653 
654  if (s->queue_len <= 0)
655  return -1;
656 
657  if (!has_next_packet(s))
658  av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
659  "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
660 
661  /* Parse the first packet in the queue, and dequeue it */
662  rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
663  next = s->queue->next;
664  av_free(s->queue->buf);
665  av_free(s->queue);
666  s->queue = next;
667  s->queue_len--;
668  return rv;
669 }
670 
672  uint8_t **bufptr, int len)
673 {
674  uint8_t *buf = bufptr ? *bufptr : NULL;
675  int ret, flags = 0;
676  uint32_t timestamp;
677  int rv = 0;
678 
679  if (!buf) {
680  /* If parsing of the previous packet actually returned 0 or an error,
681  * there's nothing more to be parsed from that packet, but we may have
682  * indicated that we can return the next enqueued packet. */
683  if (s->prev_ret <= 0)
684  return rtp_parse_queued_packet(s, pkt);
685  /* return the next packets, if any */
686  if (s->st && s->parse_packet) {
687  /* timestamp should be overwritten by parse_packet, if not,
688  * the packet is left with pts == AV_NOPTS_VALUE */
689  timestamp = RTP_NOTS_VALUE;
691  s->st, pkt, &timestamp, NULL, 0, 0,
692  flags);
693  finalize_packet(s, pkt, timestamp);
694  return rv;
695  } else {
696  // TODO: Move to a dynamic packet handler (like above)
697  if (s->read_buf_index >= s->read_buf_size)
698  return AVERROR(EAGAIN);
699  ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
700  s->read_buf_size - s->read_buf_index);
701  if (ret < 0)
702  return AVERROR(EAGAIN);
703  s->read_buf_index += ret;
704  if (s->read_buf_index < s->read_buf_size)
705  return 1;
706  else
707  return 0;
708  }
709  }
710 
711  if (len < 12)
712  return -1;
713 
714  if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
715  return -1;
716  if (RTP_PT_IS_RTCP(buf[1])) {
717  return rtcp_parse_packet(s, buf, len);
718  }
719 
720  if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
721  /* First packet, or no reordering */
722  return rtp_parse_packet_internal(s, pkt, buf, len);
723  } else {
724  uint16_t seq = AV_RB16(buf + 2);
725  int16_t diff = seq - s->seq;
726  if (diff < 0) {
727  /* Packet older than the previously emitted one, drop */
728  av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
729  "RTP: dropping old packet received too late\n");
730  return -1;
731  } else if (diff <= 1) {
732  /* Correct packet */
733  rv = rtp_parse_packet_internal(s, pkt, buf, len);
734  return rv;
735  } else {
736  /* Still missing some packet, enqueue this one. */
737  enqueue_packet(s, buf, len);
738  *bufptr = NULL;
739  /* Return the first enqueued packet if the queue is full,
740  * even if we're missing something */
741  if (s->queue_len >= s->queue_size)
742  return rtp_parse_queued_packet(s, pkt);
743  return -1;
744  }
745  }
746 }
747 
748 /**
749  * Parse an RTP or RTCP packet directly sent as a buffer.
750  * @param s RTP parse context.
751  * @param pkt returned packet
752  * @param bufptr pointer to the input buffer or NULL to read the next packets
753  * @param len buffer len
754  * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
755  * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
756  */
758  uint8_t **bufptr, int len)
759 {
760  int rv = rtp_parse_one_packet(s, pkt, bufptr, len);
761  s->prev_ret = rv;
762  while (rv == AVERROR(EAGAIN) && has_next_packet(s))
763  rv = rtp_parse_queued_packet(s, pkt);
764  return rv ? rv : has_next_packet(s);
765 }
766 
768 {
770  if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
772  }
773  av_free(s);
774 }
775 
776 int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
777  int (*parse_fmtp)(AVStream *stream,
778  PayloadContext *data,
779  char *attr, char *value))
780 {
781  char attr[256];
782  char *value;
783  int res;
784  int value_size = strlen(p) + 1;
785 
786  if (!(value = av_malloc(value_size))) {
787  av_log(NULL, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
788  return AVERROR(ENOMEM);
789  }
790 
791  // remove protocol identifier
792  while (*p && *p == ' ')
793  p++; // strip spaces
794  while (*p && *p != ' ')
795  p++; // eat protocol identifier
796  while (*p && *p == ' ')
797  p++; // strip trailing spaces
798 
799  while (ff_rtsp_next_attr_and_value(&p,
800  attr, sizeof(attr),
801  value, value_size)) {
802  res = parse_fmtp(stream, data, attr, value);
803  if (res < 0 && res != AVERROR_PATCHWELCOME) {
804  av_free(value);
805  return res;
806  }
807  }
808  av_free(value);
809  return 0;
810 }
811 
812 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
813 {
814  av_init_packet(pkt);
815 
816  pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
817  pkt->stream_index = stream_idx;
819  *dyn_buf = NULL;
820  return pkt->size;
821 }