]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtp.c
fix -a^b which was interpreted as (-a)^b
[ffmpeg] / libavformat / rtp.c
index 63236f00c65d5992c8d1ff55a2845f6febd8dc4a..65c8d0b37d33b7232ade2ada9efee55eedc15bfd 100644 (file)
@@ -2,19 +2,21 @@
  * RTP input/output format
  * Copyright (c) 2002 Fabrice Bellard.
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
 #include "mpegts.h"
@@ -40,7 +42,7 @@
          buffer to 'rtp_write_packet' contains all the packets for ONE
          frame. Each packet should have a four byte header containing
          the length in big endian format (same trick as
-         'url_open_dyn_packet_buf') 
+         'url_open_dyn_packet_buf')
 */
 
 /* from http://www.iana.org/assignments/rtp-parameters last updated 05 January 2005 */
@@ -58,7 +60,7 @@ AVRtpPayloadType_t AVRtpPayloadTypes[]=
   {9, "G722",        CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
   {10, "L16",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_S16BE, 44100, 2},
   {11, "L16",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_S16BE, 44100, 1},
-  {12, "QCELP",      CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
+  {12, "QCELP",      CODEC_TYPE_AUDIO,   CODEC_ID_QCELP, 8000, 1},
   {13, "CN",         CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
   {14, "MPA",        CODEC_TYPE_AUDIO,   CODEC_ID_MP2, 90000, -1},
   {15, "G728",       CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
@@ -197,7 +199,7 @@ struct RTPDemuxContext {
     MpegTSContext *ts; /* only used for MP2T payloads */
     int read_buf_index;
     int read_buf_size;
-    
+
     /* rtcp sender statistics receive */
     int64_t last_rtcp_ntp_time;
     int64_t first_rtcp_ntp_time;
@@ -218,7 +220,7 @@ int rtp_get_codec_info(AVCodecContext *codec, int payload_type)
 {
     if (AVRtpPayloadTypes[payload_type].codec_id != CODEC_ID_NONE) {
         codec->codec_type = AVRtpPayloadTypes[payload_type].codec_type;
-        codec->codec_id = AVRtpPayloadTypes[payload_type].codec_type;
+        codec->codec_id = AVRtpPayloadTypes[payload_type].codec_id;
         if (AVRtpPayloadTypes[payload_type].audio_channels > 0)
             codec->channels = AVRtpPayloadTypes[payload_type].audio_channels;
         if (AVRtpPayloadTypes[payload_type].clock_rate > 0)
@@ -268,7 +270,7 @@ static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int l
 /**
  * open a new RTP parse context for stream 'st'. 'st' can be NULL for
  * MPEG2TS streams to indicate that they should be demuxed inside the
- * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned) 
+ * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned)
  */
 RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, int payload_type, rtp_payload_data_t *rtp_payload_data)
 {
@@ -290,7 +292,7 @@ RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, int payload_t
             return NULL;
         }
     } else {
-        switch(st->codec.codec_id) {
+        switch(st->codec->codec_id) {
         case CODEC_ID_MPEG1VIDEO:
         case CODEC_ID_MPEG2VIDEO:
         case CODEC_ID_MP2:
@@ -307,7 +309,6 @@ RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, int payload_t
 
 static int rtp_parse_mp4_au(RTPDemuxContext *s, const uint8_t *buf)
 {
-    AVCodecContext codec;
     int au_headers_length, au_header_size, i;
     GetBitContext getbitcontext;
     rtp_payload_data_t *infos;
@@ -317,8 +318,6 @@ static int rtp_parse_mp4_au(RTPDemuxContext *s, const uint8_t *buf)
     if (infos == NULL)
         return -1;
 
-    codec = s->st->codec;
-
     /* decode the first 2 bytes where are stored the AUHeader sections
        length in bits */
     au_headers_length = BE_16(buf);
@@ -357,27 +356,27 @@ static int rtp_parse_mp4_au(RTPDemuxContext *s, const uint8_t *buf)
 }
 
 /**
- * Parse an RTP or RTCP packet directly sent as a buffer. 
+ * Parse an RTP or RTCP packet directly sent as a buffer.
  * @param s RTP parse context.
  * @param pkt returned packet
  * @param buf input buffer or NULL to read the next packets
  * @param len buffer len
- * @return 0 if a packet is returned, 1 if a packet is returned and more can follow 
+ * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
  * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
  */
-int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt, 
+int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
                      const uint8_t *buf, int len)
 {
     unsigned int ssrc, h;
     int payload_type, seq, delta_timestamp, ret;
     AVStream *st;
     uint32_t timestamp;
-    
+
     if (!buf) {
         /* return the next packets, if any */
         if (s->read_buf_index >= s->read_buf_size)
             return -1;
-        ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index, 
+        ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
                                   s->read_buf_size - s->read_buf_index);
         if (ret < 0)
             return -1;
@@ -401,13 +400,15 @@ int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
     seq  = (buf[2] << 8) | buf[3];
     timestamp = decode_be32(buf + 4);
     ssrc = decode_be32(buf + 8);
-    
+
     /* NOTE: we can handle only one payload type */
     if (s->payload_type != payload_type)
         return -1;
+
+    st = s->st;
 #if defined(DEBUG) || 1
     if (seq != ((s->seq + 1) & 0xffff)) {
-        av_log(&s->st->codec, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n", 
+        av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
                payload_type, seq, ((s->seq + 1) & 0xffff));
     }
 #endif
@@ -415,7 +416,6 @@ int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
     len -= 12;
     buf += 12;
 
-    st = s->st;
     if (!st) {
         /* specific MPEG2TS demux support */
         ret = mpegts_parse_packet(s->ts, pkt, buf, len);
@@ -428,7 +428,7 @@ int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
             return 1;
         }
     } else {
-        switch(st->codec.codec_id) {
+        switch(st->codec->codec_id) {
         case CODEC_ID_MP2:
             /* better than nothing: skip mpeg audio RTP header */
             if (len <= 4)
@@ -461,8 +461,8 @@ int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
             memcpy(pkt->data, buf, len);
             break;
         }
-        
-        switch(st->codec.codec_id) {
+
+        switch(st->codec->codec_id) {
         case CODEC_ID_MP2:
         case CODEC_ID_MPEG1VIDEO:
             if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) {
@@ -497,7 +497,7 @@ int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
             len -= infos->au_headers[0].size;
             }
             s->read_buf_size = len;
-            s->buf_ptr = (char *)buf;
+            s->buf_ptr = buf;
             pkt->stream_index = s->st->index;
             return 0;
         default:
@@ -529,14 +529,15 @@ static int rtp_write_header(AVFormatContext *s1)
         return -1;
     st = s1->streams[0];
 
-    payload_type = rtp_get_payload_type(&st->codec);
+    payload_type = rtp_get_payload_type(st->codec);
     if (payload_type < 0)
         payload_type = RTP_PT_PRIVATE; /* private payload type */
     s->payload_type = payload_type;
 
-    s->base_timestamp = random();
+// following 2 FIXMies could be set based on the current time, theres normaly no info leak, as rtp will likely be transmitted immedeatly
+    s->base_timestamp = 0; /* FIXME: was random(), what should this be? */
     s->timestamp = s->base_timestamp;
-    s->ssrc = random();
+    s->ssrc = 0; /* FIXME: was random(), what should this be? */
     s->first_packet = 1;
 
     max_packet_size = url_fget_max_packet_size(&s1->pb);
@@ -544,7 +545,7 @@ static int rtp_write_header(AVFormatContext *s1)
         return AVERROR_IO;
     s->max_payload_size = max_packet_size - 12;
 
-    switch(st->codec.codec_id) {
+    switch(st->codec->codec_id) {
     case CODEC_ID_MP2:
     case CODEC_ID_MP3:
         s->buf_ptr = s->buf + 4;
@@ -588,7 +589,7 @@ static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
 
 /* send an rtp packet. sequence number is incremented, but the caller
    must update the timestamp itself */
-static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len)
+static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
 {
     RTPDemuxContext *s = s1->priv_data;
 
@@ -598,14 +599,14 @@ static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len)
 
     /* build the RTP header */
     put_byte(&s1->pb, (RTP_VERSION << 6));
-    put_byte(&s1->pb, s->payload_type & 0x7f);
+    put_byte(&s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
     put_be16(&s1->pb, s->seq);
     put_be32(&s1->pb, s->timestamp);
     put_be32(&s1->pb, s->ssrc);
-    
+
     put_buffer(&s1->pb, buf1, len);
     put_flush_packet(&s1->pb);
-    
+
     s->seq++;
     s->octet_count += len;
     s->packet_count++;
@@ -636,13 +637,13 @@ static void rtp_send_samples(AVFormatContext *s1,
         n = (s->buf_ptr - s->buf);
         /* if buffer full, then send it */
         if (n >= max_packet_size) {
-            rtp_send_data(s1, s->buf, n);
+            rtp_send_data(s1, s->buf, n, 0);
             s->buf_ptr = s->buf;
             /* update timestamp */
             s->timestamp += n / sample_size;
         }
     }
-} 
+}
 
 /* NOTE: we suppose that exactly one frame is given as argument here */
 /* XXX: test it */
@@ -659,11 +660,11 @@ static void rtp_send_mpegaudio(AVFormatContext *s1,
     len = (s->buf_ptr - s->buf);
     if ((len + size) > max_packet_size) {
         if (len > 4) {
-            rtp_send_data(s1, s->buf, s->buf_ptr - s->buf);
+            rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
             s->buf_ptr = s->buf + 4;
             /* 90 KHz time stamp */
-            s->timestamp = s->base_timestamp + 
-                (s->cur_timestamp * 90000LL) / st->codec.sample_rate;
+            s->timestamp = s->base_timestamp +
+                (s->cur_timestamp * 90000LL) / st->codec->sample_rate;
         }
     }
 
@@ -681,7 +682,7 @@ static void rtp_send_mpegaudio(AVFormatContext *s1,
             s->buf[2] = count >> 8;
             s->buf[3] = count;
             memcpy(s->buf + 4, buf1, len);
-            rtp_send_data(s1, s->buf, len + 4);
+            rtp_send_data(s1, s->buf, len + 4, 0);
             size -= len;
             buf1 += len;
             count += len;
@@ -697,7 +698,7 @@ static void rtp_send_mpegaudio(AVFormatContext *s1,
         memcpy(s->buf_ptr, buf1, size);
         s->buf_ptr += size;
     }
-    s->cur_timestamp += st->codec.frame_size;
+    s->cur_timestamp += st->codec->frame_size;
 }
 
 /* NOTE: a single frame must be passed with sequence header if
@@ -715,7 +716,7 @@ static void rtp_send_mpegvideo(AVFormatContext *s1,
     while (size > 0) {
         /* XXX: more correct headers */
         h = 0;
-        if (st->codec.sub_id == 2)
+        if (st->codec->sub_id == 2)
             h |= 1 << 26; /* mpeg 2 indicator */
         q = s->buf;
         *q++ = h >> 24;
@@ -723,14 +724,14 @@ static void rtp_send_mpegvideo(AVFormatContext *s1,
         *q++ = h >> 8;
         *q++ = h;
 
-        if (st->codec.sub_id == 2) {
+        if (st->codec->sub_id == 2) {
             h = 0;
             *q++ = h >> 24;
             *q++ = h >> 16;
             *q++ = h >> 8;
             *q++ = h;
         }
-        
+
         len = max_packet_size - (q - s->buf);
         if (len > size)
             len = size;
@@ -739,9 +740,9 @@ static void rtp_send_mpegvideo(AVFormatContext *s1,
         q += len;
 
         /* 90 KHz time stamp */
-        s->timestamp = s->base_timestamp + 
-            av_rescale((int64_t)s->cur_timestamp * st->codec.time_base.num, 90000, st->codec.time_base.den); //FIXME pass timestamps
-        rtp_send_data(s1, s->buf, q - s->buf);
+        s->timestamp = s->base_timestamp +
+            av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps
+        rtp_send_data(s1, s->buf, q - s->buf, (len == size));
 
         buf1 += len;
         size -= len;
@@ -764,9 +765,9 @@ static void rtp_send_raw(AVFormatContext *s1,
             len = size;
 
         /* 90 KHz time stamp */
-        s->timestamp = s->base_timestamp + 
-            av_rescale((int64_t)s->cur_timestamp * st->codec.time_base.num, 90000, st->codec.time_base.den); //FIXME pass timestamps
-        rtp_send_data(s1, buf1, len);
+        s->timestamp = s->base_timestamp +
+            av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps
+        rtp_send_data(s1, buf1, len, (len == size));
 
         buf1 += len;
         size -= len;
@@ -789,10 +790,10 @@ static void rtp_send_mpegts_raw(AVFormatContext *s1,
         buf1 += len;
         size -= len;
         s->buf_ptr += len;
-        
+
         out_len = s->buf_ptr - s->buf;
         if (out_len >= s->max_payload_size) {
-            rtp_send_data(s1, s->buf, out_len);
+            rtp_send_data(s1, s->buf, out_len, 0);
             s->buf_ptr = s->buf;
         }
     }
@@ -807,35 +808,35 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
     int64_t ntp_time;
     int size= pkt->size;
     uint8_t *buf1= pkt->data;
-    
+
 #ifdef DEBUG
     printf("%d: write len=%d\n", pkt->stream_index, size);
 #endif
 
     /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
-    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) / 
+    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
         RTCP_TX_RATIO_DEN;
     if (s->first_packet || rtcp_bytes >= 28) {
         /* compute NTP time */
         /* XXX: 90 kHz timestamp hardcoded */
         ntp_time = (pkt->pts << 28) / 5625;
-        rtcp_send_sr(s1, ntp_time); 
+        rtcp_send_sr(s1, ntp_time);
         s->last_octet_count = s->octet_count;
         s->first_packet = 0;
     }
 
-    switch(st->codec.codec_id) {
+    switch(st->codec->codec_id) {
     case CODEC_ID_PCM_MULAW:
     case CODEC_ID_PCM_ALAW:
     case CODEC_ID_PCM_U8:
     case CODEC_ID_PCM_S8:
-        rtp_send_samples(s1, buf1, size, 1 * st->codec.channels);
+        rtp_send_samples(s1, buf1, size, 1 * st->codec->channels);
         break;
     case CODEC_ID_PCM_U16BE:
     case CODEC_ID_PCM_U16LE:
     case CODEC_ID_PCM_S16BE:
     case CODEC_ID_PCM_S16LE:
-        rtp_send_samples(s1, buf1, size, 2 * st->codec.channels);
+        rtp_send_samples(s1, buf1, size, 2 * st->codec->channels);
         break;
     case CODEC_ID_MP2:
     case CODEC_ID_MP3:
@@ -861,7 +862,7 @@ static int rtp_write_trailer(AVFormatContext *s1)
     return 0;
 }
 
-AVOutputFormat rtp_mux = {
+AVOutputFormat rtp_muxer = {
     "rtp",
     "RTP output format",
     NULL,
@@ -873,9 +874,3 @@ AVOutputFormat rtp_mux = {
     rtp_write_packet,
     rtp_write_trailer,
 };
-
-int rtp_init(void)
-{
-    av_register_output_format(&rtp_mux);
-    return 0;
-}