diff mbox series

[FFmpeg-devel,1/2] avformat/demux: Restore pkt->stream_index assert check

Message ID DU0P250MB07476CC694B668741E513E8E8F322@DU0P250MB0747.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit 07064f9bdac3f865e039373544d53cf633aecfa6
Headers show
Series [FFmpeg-devel,1/2] avformat/demux: Restore pkt->stream_index assert check | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt March 21, 2024, 12:34 p.m. UTC
It has been moved after "st  = s->streams[pkt->stream_index]"
in b140b8332c617b0eef4f872f3ef90c469e99920f.
Deduplicate ff_read_packet() and ff_buffer_packet()
while fixing this.
This also fixes shadowing in ff_read_packet().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/demux.c | 75 +++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 44 deletions(-)

Comments

Andreas Rheinhardt March 22, 2024, 11:41 p.m. UTC | #1
Andreas Rheinhardt:
> It has been moved after "st  = s->streams[pkt->stream_index]"
> in b140b8332c617b0eef4f872f3ef90c469e99920f.
> Deduplicate ff_read_packet() and ff_buffer_packet()
> while fixing this.
> This also fixes shadowing in ff_read_packet().
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavformat/demux.c | 75 +++++++++++++++++++--------------------------
>  1 file changed, 31 insertions(+), 44 deletions(-)
> 
> diff --git a/libavformat/demux.c b/libavformat/demux.c
> index 4c50eb5568..4345ed4c8c 100644
> --- a/libavformat/demux.c
> +++ b/libavformat/demux.c
> @@ -532,9 +532,6 @@ static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
>  {
>      FFStream *const sti = ffstream(st);
>  
> -    av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
> -               "Invalid stream index.\n");
> -
>      if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
>          // correct first time stamps to negative values
>          if (!is_relative(sti->first_dts))
> @@ -555,13 +552,24 @@ static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
>          pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
>  }
>  
> -int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
> +/**
> + * Handle a new packet and either return it directly if possible and
> + * allow_passthrough is true or queue the packet (or drop the packet
> + * if corrupt).
> + *
> + * @return < 0 on error, 0 if the packet was passed through,
> + *         1 if it was queued or dropped
> + */
> +static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
>  {
>      FFFormatContext *const si = ffformatcontext(s);
> -    AVStream *st  = s->streams[pkt->stream_index];
> -    FFStream *sti = ffstream(st);
> +    AVStream *st;
> +    FFStream *sti;
>      int err;
>  
> +    av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
> +               "Invalid stream index.\n");
> +
>      if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
>          av_log(s, AV_LOG_WARNING,
>                 "Packet corrupt (stream = %d, dts = %s)",
> @@ -569,13 +577,19 @@ int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
>          if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
>              av_log(s, AV_LOG_WARNING, ", dropping it.\n");
>              av_packet_unref(pkt);
> -            return 0;
> +            return 1;
>          }
>          av_log(s, AV_LOG_WARNING, ".\n");
>      }
>  
> +    st  = s->streams[pkt->stream_index];
> +    sti = ffstream(st);
> +
>      update_timestamps(s, st, pkt);
>  
> +    if (sti->request_probe <= 0 && allow_passthrough && !si->raw_packet_buffer.head)
> +        return 0;
> +
>      err = avpriv_packet_list_put(&si->raw_packet_buffer, pkt, NULL, 0);
>      if (err < 0) {
>          av_packet_unref(pkt);
> @@ -585,14 +599,18 @@ int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
>      pkt = &si->raw_packet_buffer.tail->pkt;
>      si->raw_packet_buffer_size += pkt->size;
>  
> -    if (sti->request_probe <= 0)
> -        return 0;
> -
>      err = probe_codec(s, st, pkt);
>      if (err < 0)
>          return err;
>  
> -    return 0;
> +    return 1;
> +}
> +
> +int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
> +{
> +    int err = handle_new_packet(s, pkt, 0);
> +
> +    return err < 0 ? err : 0;
>  }
>  
>  int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
> @@ -612,9 +630,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
>  
>      for (;;) {
>          PacketListEntry *pktl = si->raw_packet_buffer.head;
> -        AVStream *st;
> -        FFStream *sti;
> -        const AVPacket *pkt1;
>  
>          if (pktl) {
>              AVStream *const st = s->streams[pktl->pkt.stream_index];
> @@ -656,36 +671,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
>              return err;
>          }
>  
> -        if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
> -            av_log(s, AV_LOG_WARNING,
> -                   "Packet corrupt (stream = %d, dts = %s)",
> -                   pkt->stream_index, av_ts2str(pkt->dts));
> -            if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
> -                av_log(s, AV_LOG_WARNING, ", dropping it.\n");
> -                av_packet_unref(pkt);
> -                continue;
> -            }
> -            av_log(s, AV_LOG_WARNING, ".\n");
> -        }
> -
> -        st  = s->streams[pkt->stream_index];
> -        sti = ffstream(st);
> -
> -        update_timestamps(s, st, pkt);
> -
> -        if (!pktl && sti->request_probe <= 0)
> -            return 0;
> -
> -        err = avpriv_packet_list_put(&si->raw_packet_buffer,
> -                                     pkt, NULL, 0);
> -        if (err < 0) {
> -            av_packet_unref(pkt);
> -            return err;
> -        }
> -        pkt1 = &si->raw_packet_buffer.tail->pkt;
> -        si->raw_packet_buffer_size += pkt1->size;
> -
> -        if ((err = probe_codec(s, st, pkt1)) < 0)
> +        err = handle_new_packet(s, pkt, 1);
> +        if (err <= 0) /* Error or passthrough */
>              return err;
>      }
>  }

Will apply this patchset tomorrow unless there are objections.

- Andreas
diff mbox series

Patch

diff --git a/libavformat/demux.c b/libavformat/demux.c
index 4c50eb5568..4345ed4c8c 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -532,9 +532,6 @@  static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
 {
     FFStream *const sti = ffstream(st);
 
-    av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
-               "Invalid stream index.\n");
-
     if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
         // correct first time stamps to negative values
         if (!is_relative(sti->first_dts))
@@ -555,13 +552,24 @@  static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
         pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
 }
 
-int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
+/**
+ * Handle a new packet and either return it directly if possible and
+ * allow_passthrough is true or queue the packet (or drop the packet
+ * if corrupt).
+ *
+ * @return < 0 on error, 0 if the packet was passed through,
+ *         1 if it was queued or dropped
+ */
+static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
 {
     FFFormatContext *const si = ffformatcontext(s);
-    AVStream *st  = s->streams[pkt->stream_index];
-    FFStream *sti = ffstream(st);
+    AVStream *st;
+    FFStream *sti;
     int err;
 
+    av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
+               "Invalid stream index.\n");
+
     if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
         av_log(s, AV_LOG_WARNING,
                "Packet corrupt (stream = %d, dts = %s)",
@@ -569,13 +577,19 @@  int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
         if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
             av_log(s, AV_LOG_WARNING, ", dropping it.\n");
             av_packet_unref(pkt);
-            return 0;
+            return 1;
         }
         av_log(s, AV_LOG_WARNING, ".\n");
     }
 
+    st  = s->streams[pkt->stream_index];
+    sti = ffstream(st);
+
     update_timestamps(s, st, pkt);
 
+    if (sti->request_probe <= 0 && allow_passthrough && !si->raw_packet_buffer.head)
+        return 0;
+
     err = avpriv_packet_list_put(&si->raw_packet_buffer, pkt, NULL, 0);
     if (err < 0) {
         av_packet_unref(pkt);
@@ -585,14 +599,18 @@  int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
     pkt = &si->raw_packet_buffer.tail->pkt;
     si->raw_packet_buffer_size += pkt->size;
 
-    if (sti->request_probe <= 0)
-        return 0;
-
     err = probe_codec(s, st, pkt);
     if (err < 0)
         return err;
 
-    return 0;
+    return 1;
+}
+
+int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    int err = handle_new_packet(s, pkt, 0);
+
+    return err < 0 ? err : 0;
 }
 
 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
@@ -612,9 +630,6 @@  FF_ENABLE_DEPRECATION_WARNINGS
 
     for (;;) {
         PacketListEntry *pktl = si->raw_packet_buffer.head;
-        AVStream *st;
-        FFStream *sti;
-        const AVPacket *pkt1;
 
         if (pktl) {
             AVStream *const st = s->streams[pktl->pkt.stream_index];
@@ -656,36 +671,8 @@  FF_ENABLE_DEPRECATION_WARNINGS
             return err;
         }
 
-        if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
-            av_log(s, AV_LOG_WARNING,
-                   "Packet corrupt (stream = %d, dts = %s)",
-                   pkt->stream_index, av_ts2str(pkt->dts));
-            if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
-                av_log(s, AV_LOG_WARNING, ", dropping it.\n");
-                av_packet_unref(pkt);
-                continue;
-            }
-            av_log(s, AV_LOG_WARNING, ".\n");
-        }
-
-        st  = s->streams[pkt->stream_index];
-        sti = ffstream(st);
-
-        update_timestamps(s, st, pkt);
-
-        if (!pktl && sti->request_probe <= 0)
-            return 0;
-
-        err = avpriv_packet_list_put(&si->raw_packet_buffer,
-                                     pkt, NULL, 0);
-        if (err < 0) {
-            av_packet_unref(pkt);
-            return err;
-        }
-        pkt1 = &si->raw_packet_buffer.tail->pkt;
-        si->raw_packet_buffer_size += pkt1->size;
-
-        if ((err = probe_codec(s, st, pkt1)) < 0)
+        err = handle_new_packet(s, pkt, 1);
+        if (err <= 0) /* Error or passthrough */
             return err;
     }
 }