[FFmpeg-devel] [PATCH] libavformat/rtspdec.c:flush pes buffer while rtsp seek

tanwei (D) tanwei123 at hisilicon.com
Mon Nov 14 09:46:31 EET 2022


Fixes ticket #9949.

Signed-off-by: tanwei123 <tanwei123 at hisilicon.com>
---
 libavformat/mpegts.c        | 20 ++++++++++++++++++++
 libavformat/mpegts.h        |  1 +
 libavformat/rtpdec.c        |  7 +++++++
 libavformat/rtpdec.h        |  2 ++
 libavformat/rtpdec_mpegts.c | 11 +++++++++++
 libavformat/rtspdec.c       | 11 +++++++++++
 6 files changed, 52 insertions(+)

diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index d97702fcd7..c5472bb464 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -3419,6 +3419,26 @@ int avpriv_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
     return len1 - len;
 }
 
+void avpriv_mpegts_seek_flush(MpegTSContext *ts)
+{
+    int i;
+    /* flush pes buffer */
+    for (i = 0; i < NB_PID_MAX; i++) {
+        if (ts->pids[i]) {
+            if (ts->pids[i]->type == MPEGTS_PES) {
+                PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
+                av_buffer_unref(&pes->buffer);
+                pes->data_index = 0;
+                pes->state = MPEGTS_SKIP; /* skip until pes header */
+            } else if (ts->pids[i]->type == MPEGTS_SECTION) {
+                ts->pids[i]->u.section_filter.last_ver = -1;
+            }
+            ts->pids[i]->last_cc = -1;
+            ts->pids[i]->last_pcr = -1;
+        }
+    }
+}
+
 void avpriv_mpegts_parse_close(MpegTSContext *ts)
 {
     mpegts_free(ts);
diff --git a/libavformat/mpegts.h b/libavformat/mpegts.h
index a48f14e768..b6f19e96bb 100644
--- a/libavformat/mpegts.h
+++ b/libavformat/mpegts.h
@@ -170,6 +170,7 @@ MpegTSContext *avpriv_mpegts_parse_open(AVFormatContext *s);
 int avpriv_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
                                const uint8_t *buf, int len);
 void avpriv_mpegts_parse_close(MpegTSContext *ts);
+void avpriv_mpegts_seek_flush(MpegTSContext *ts);
 
 typedef struct SLConfigDescr {
     int use_au_start;
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index fa7544cc07..d688afd1c1 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -954,6 +954,13 @@ int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
     return rv ? rv : has_next_packet(s);
 }
 
+void ff_rtp_seek_flush(RTPDemuxContext *s)
+{
+    ff_rtp_reset_packet_queue(s);
+    if (s->handler && s->handler->seek_flush)
+        s->handler->seek_flush(s->dynamic_protocol_context);
+}
+
 void ff_rtp_parse_close(RTPDemuxContext *s)
 {
     ff_rtp_reset_packet_queue(s);
diff --git a/libavformat/rtpdec.h b/libavformat/rtpdec.h
index 5a02e72dc2..8d6d857e28 100644
--- a/libavformat/rtpdec.h
+++ b/libavformat/rtpdec.h
@@ -52,6 +52,7 @@ int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
 void ff_rtp_parse_close(RTPDemuxContext *s);
 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s);
 void ff_rtp_reset_packet_queue(RTPDemuxContext *s);
+void ff_rtp_seek_flush(RTPDemuxContext *s);
 
 /**
  * Send a dummy packet on both port pairs to set up the connection
@@ -135,6 +136,7 @@ struct RTPDynamicProtocolHandler {
     /** Parse handler for this dynamic packet */
     DynamicPayloadPacketHandlerProc parse_packet;
     int (*need_keyframe)(PayloadContext *context);
+    void (*seek_flush)(PayloadContext *protocol_data);
 };
 
 typedef struct RTPPacket {
diff --git a/libavformat/rtpdec_mpegts.c b/libavformat/rtpdec_mpegts.c
index 405271f744..6e9abcae08 100644
--- a/libavformat/rtpdec_mpegts.c
+++ b/libavformat/rtpdec_mpegts.c
@@ -47,6 +47,16 @@ static av_cold int mpegts_init(AVFormatContext *ctx, int st_index,
     return 0;
 }
 
+static void mpegts_seek_flush(PayloadContext *data)
+{
+    if (!data)
+        return;
+    memset(data->buf, 0, data->read_buf_size);
+    data->read_buf_size = 0;
+    if (data->ts)
+        avpriv_mpegts_seek_flush(data->ts);
+}
+
 static int mpegts_handle_packet(AVFormatContext *ctx, PayloadContext *data,
                                 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
                                 const uint8_t *buf, int len, uint16_t seq,
@@ -94,6 +104,7 @@ const RTPDynamicProtocolHandler ff_mpegts_dynamic_handler = {
     .priv_data_size    = sizeof(PayloadContext),
     .parse_packet      = mpegts_handle_packet,
     .init              = mpegts_init,
+    .seek_flush        = mpegts_seek_flush,
     .close             = mpegts_close_context,
     .static_payload_id = 33,
 };
diff --git a/libavformat/rtspdec.c b/libavformat/rtspdec.c
index bbabec7db8..f743fac2ee 100644
--- a/libavformat/rtspdec.c
+++ b/libavformat/rtspdec.c
@@ -36,6 +36,7 @@
 #include "rdt.h"
 #include "tls.h"
 #include "url.h"
+#include "mpegts.h"
 #include "version.h"
 
 static const struct RTSPStatusMessage {
@@ -982,6 +983,16 @@ static int rtsp_read_seek(AVFormatContext *s, int stream_index,
         rt->state = RTSP_STATE_IDLE;
         break;
     }
+
+    if (rt->cur_transport_priv && rt->transport == RTSP_TRANSPORT_RTP) {
+        ff_rtp_seek_flush(rt->cur_transport_priv);
+    } else if (CONFIG_RTPDEC && rt->ts) {
+        av_freep(&rt->recvbuf);
+        rt->recvbuf_pos = 0;
+        rt->recvbuf_len = 0;
+        avpriv_mpegts_seek_flush(rt->ts);
+    }
+
     return 0;
 }
 
-- 
2.17.1




More information about the ffmpeg-devel mailing list