From eafb17d140f6772c9aac8fbf31641f24a371b2c0 Mon Sep 17 00:00:00 2001
From: "Ronald S. Bultje" <rsbultje@gmail.com>
Date: Tue, 3 Mar 2009 13:51:34 +0000
Subject: [PATCH] Don't let finalize_packet() touch pkt->stream_index. Instead,
 let individual payload handlers take care of that themselves at their own
 option. What this patch really does is "fix" a bug in MS-RTSP protocol where
 incoming packets are always coming in over the connection (UDP) or
 interleave-id (TCP) of the stream-id of the first ASF packet in the RTP
 packet. However, RTP packets may contain multiple ASF packets (and usually
 do, from what I can see), and therefore this leads to playback bugs. The
 intended stream-id per ASF packet is given in the respective ASF packet
 header. The ASF demuxer will correctly read this and set pkt->stream_index,
 but since the "stream" parameter can not be known to rtpdec.c or any of the
 RTP/RTSP code, the "st" parameter in all these functions is basically
 invalid. Therefore, using st->id as pkt->stream_index leads to various
 playback bugs. The result of this patch is that pkt->stream_index is left
 untouched for RTP/ASF (and possibly for other payloads that have similar
 behaviour).

The patch was discussed in the "[PATCH] rtpdec.c: don't overwrite
pkt->stream_index in finalize_packet()" thread on the mailinglist.

Originally committed as revision 17767 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavformat/rtp_h264.c | 2 ++
 libavformat/rtpdec.c   | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/libavformat/rtp_h264.c b/libavformat/rtp_h264.c
index f3ae6993f54..47cbf79db51 100644
--- a/libavformat/rtp_h264.c
+++ b/libavformat/rtp_h264.c
@@ -310,6 +310,8 @@ static int h264_handle_packet(AVFormatContext *ctx,
         break;
     }
 
+    pkt->stream_index = st->index;
+
     return result;
 }
 
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index 4a5257bc6d8..e1ba888cb1b 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -382,7 +382,6 @@ static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestam
         addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time, s->st->time_base.den, (uint64_t)s->st->time_base.num << 32);
         pkt->pts = addend + delta_timestamp;
     }
-    pkt->stream_index = s->st->index;
 }
 
 /**
@@ -536,6 +535,8 @@ int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
             memcpy(pkt->data, buf, len);
             break;
         }
+
+        pkt->stream_index = st->index;
     }
 
     // now perform timestamp things....
-- 
GitLab