changeset 2540:ca3cba9c641f libavformat

Fix timestamps in RTP packets (now, MPEG1 video with B frames works correctly)
author lucabe
date Mon, 10 Sep 2007 07:01:29 +0000
parents ba933dfa4833
children 90609bab3de3
files rtp.c rtp_mpv.c
diffstat 2 files changed, 14 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/rtp.c	Mon Sep 10 06:58:19 2007 +0000
+++ b/rtp.c	Mon Sep 10 07:01:29 2007 +0000
@@ -737,6 +737,7 @@
 // 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->cur_timestamp = 0;
     s->ssrc = 0; /* FIXME: was random(), what should this be? */
     s->first_packet = 1;
     s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
@@ -746,14 +747,13 @@
         return AVERROR(EIO);
     s->max_payload_size = max_packet_size - 12;
 
+    av_set_pts_info(st, 32, 1, 90000);
     switch(st->codec->codec_id) {
     case CODEC_ID_MP2:
     case CODEC_ID_MP3:
         s->buf_ptr = s->buf + 4;
-        s->cur_timestamp = 0;
         break;
     case CODEC_ID_MPEG1VIDEO:
-        s->cur_timestamp = 0;
         break;
     case CODEC_ID_MPEG2TS:
         n = s->max_payload_size / TS_PACKET_SIZE;
@@ -835,24 +835,19 @@
     /* not needed, but who nows */
     if ((size % sample_size) != 0)
         av_abort();
+    n = 0;
     while (size > 0) {
-        len = (max_packet_size - (s->buf_ptr - s->buf));
-        if (len > size)
-            len = size;
+        s->buf_ptr = s->buf;
+        len = FFMIN(max_packet_size, size);
 
         /* copy data */
         memcpy(s->buf_ptr, buf1, len);
         s->buf_ptr += len;
         buf1 += len;
         size -= len;
-        n = (s->buf_ptr - s->buf);
-        /* if buffer full, then send it */
-        if (n >= max_packet_size) {
-            ff_rtp_send_data(s1, s->buf, n, 0);
-            s->buf_ptr = s->buf;
-            /* update timestamp */
-            s->timestamp += n / sample_size;
-        }
+        s->timestamp = s->cur_timestamp + n / sample_size;
+        ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
+        n += (s->buf_ptr - s->buf);
     }
 }
 
@@ -862,7 +857,6 @@
                                const uint8_t *buf1, int size)
 {
     RTPDemuxContext *s = s1->priv_data;
-    AVStream *st = s1->streams[0];
     int len, count, max_packet_size;
 
     max_packet_size = s->max_payload_size;
@@ -873,11 +867,11 @@
         if (len > 4) {
             ff_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;
         }
     }
+    if (s->buf_ptr == s->buf + 4) {
+        s->timestamp = s->cur_timestamp;
+    }
 
     /* add the packet */
     if (size > max_packet_size) {
@@ -909,14 +903,12 @@
         memcpy(s->buf_ptr, buf1, size);
         s->buf_ptr += size;
     }
-    s->cur_timestamp += st->codec->frame_size;
 }
 
 static void rtp_send_raw(AVFormatContext *s1,
                          const uint8_t *buf1, int size)
 {
     RTPDemuxContext *s = s1->priv_data;
-    AVStream *st = s1->streams[0];
     int len, max_packet_size;
 
     max_packet_size = s->max_payload_size;
@@ -926,15 +918,12 @@
         if (len > size)
             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
+        s->timestamp = s->cur_timestamp;
         ff_rtp_send_data(s1, buf1, len, (len == size));
 
         buf1 += len;
         size -= len;
     }
-    s->cur_timestamp++;
 }
 
 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
@@ -982,6 +971,7 @@
         s->last_octet_count = s->octet_count;
         s->first_packet = 0;
     }
+    s->cur_timestamp = s->base_timestamp + pkt->pts;
 
     switch(st->codec->codec_id) {
     case CODEC_ID_PCM_MULAW:
--- a/rtp_mpv.c	Mon Sep 10 06:58:19 2007 +0000
+++ b/rtp_mpv.c	Mon Sep 10 07:01:29 2007 +0000
@@ -28,7 +28,6 @@
 void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size)
 {
     RTPDemuxContext *s = s1->priv_data;
-    AVStream *st = s1->streams[0];
     int len, h, max_packet_size;
     uint8_t *q;
     int begin_of_slice, end_of_slice, frame_type, temporal_reference;
@@ -105,8 +104,7 @@
         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
+        s->timestamp = s->cur_timestamp;
         ff_rtp_send_data(s1, s->buf, q - s->buf, (len == size));
 
         buf1 += len;
@@ -114,7 +112,6 @@
         begin_of_slice = end_of_slice;
         end_of_slice = 0;
     }
-    s->cur_timestamp++;
 }