diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 0343825aa0..75699f3a32 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -322,6 +322,7 @@
 #include "libavformat/version.h"
 
 struct AVFormatContext;
+struct AVStream;
 
 struct AVDeviceInfoList;
 struct AVDeviceCapabilitiesQuery;
@@ -623,9 +624,13 @@ typedef struct AVOutputFormat {
     /**
      * Set up any necessary bitstream filtering and extract any extra data needed
      * for the global header.
+     *
+     * @note pkt might have been directly forwarded by a meta-muxer; therefore
+     *       pkt->stream_index as well as the pkt's timebase might be invalid.
      * Return 0 if more packets from this stream must be checked; 1 if not.
      */
-    int (*check_bitstream)(struct AVFormatContext *, const AVPacket *pkt);
+    int (*check_bitstream)(struct AVFormatContext *s, struct AVStream *st,
+                           const AVPacket *pkt);
 } AVOutputFormat;
 /**
  * @}
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 5faf06e11d..dd2b34afbb 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -2333,19 +2333,21 @@ static int dash_write_trailer(AVFormatContext *s)
     return 0;
 }
 
-static int dash_check_bitstream(struct AVFormatContext *s, const AVPacket *avpkt)
+static int dash_check_bitstream(AVFormatContext *s, AVStream *st,
+                                const AVPacket *avpkt)
 {
     DASHContext *c = s->priv_data;
-    OutputStream *os = &c->streams[avpkt->stream_index];
+    OutputStream *os = &c->streams[st->index];
     AVFormatContext *oc = os->ctx;
     if (oc->oformat->check_bitstream) {
+        AVStream *const ost = oc->streams[0];
         int ret;
         AVPacket pkt = *avpkt;
         pkt.stream_index = 0;
-        ret = oc->oformat->check_bitstream(oc, &pkt);
+        ret = oc->oformat->check_bitstream(oc, ost, &pkt);
         if (ret == 1) {
-            FFStream *const  sti = ffstream(s->streams[avpkt->stream_index]);
-            FFStream *const osti = ffstream(oc->streams[0]);
+            FFStream *const  sti = ffstream(st);
+            FFStream *const osti = ffstream(ost);
              sti->bsfc = osti->bsfc;
             osti->bsfc = NULL;
         }
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 3f24c7e192..c5926575a1 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -1083,10 +1083,10 @@ fail:
     return ret;
 }
 
-static int flv_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+static int flv_check_bitstream(AVFormatContext *s, AVStream *st,
+                               const AVPacket *pkt)
 {
     int ret = 1;
-    AVStream *st = s->streams[pkt->stream_index];
 
     if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
         if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
diff --git a/libavformat/latmenc.c b/libavformat/latmenc.c
index 21bb614f67..72865c9565 100644
--- a/libavformat/latmenc.c
+++ b/libavformat/latmenc.c
@@ -245,10 +245,10 @@ too_large:
     return AVERROR_INVALIDDATA;
 }
 
-static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+static int latm_check_bitstream(AVFormatContext *s, AVStream *st,
+                                const AVPacket *pkt)
 {
     int ret = 1;
-    AVStream *st = s->streams[pkt->stream_index];
 
     if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
         if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 6945c26f5a..f08ead0a96 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -2731,10 +2731,10 @@ static int mkv_init(struct AVFormatContext *s)
     return 0;
 }
 
-static int mkv_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+static int mkv_check_bitstream(AVFormatContext *s, AVStream *st,
+                               const AVPacket *pkt)
 {
     int ret = 1;
-    AVStream *st = s->streams[pkt->stream_index];
 
     if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
         if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 37d4403f7a..fec467979a 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -7343,10 +7343,10 @@ static int mov_write_trailer(AVFormatContext *s)
     return res;
 }
 
-static int mov_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+static int mov_check_bitstream(AVFormatContext *s, AVStream *st,
+                               const AVPacket *pkt)
 {
     int ret = 1;
-    AVStream *st = s->streams[pkt->stream_index];
 
     if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
         if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index e3fba54939..79ec1a7e5f 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -2208,10 +2208,10 @@ static void mpegts_deinit(AVFormatContext *s)
     av_freep(&ts->services);
 }
 
-static int mpegts_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+static int mpegts_check_bitstream(AVFormatContext *s, AVStream *st,
+                                  const AVPacket *pkt)
 {
     int ret = 1;
-    AVStream *st = s->streams[pkt->stream_index];
 
     if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
         if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
diff --git a/libavformat/mux.c b/libavformat/mux.c
index 1389bcc003..d93dc73f8e 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -1074,7 +1074,7 @@ static int check_bitstream(AVFormatContext *s, FFStream *sti, AVPacket *pkt)
 
     if (s->oformat->check_bitstream) {
         if (!sti->bitstream_checked) {
-            if ((ret = s->oformat->check_bitstream(s, pkt)) < 0)
+            if ((ret = s->oformat->check_bitstream(s, &sti->pub, pkt)) < 0)
                 return ret;
             else if (ret == 1)
                 sti->bitstream_checked = 1;
diff --git a/libavformat/rawenc.c b/libavformat/rawenc.c
index 15e7051873..4bbae7717b 100644
--- a/libavformat/rawenc.c
+++ b/libavformat/rawenc.c
@@ -341,9 +341,9 @@ const AVOutputFormat ff_h263_muxer = {
 #endif
 
 #if CONFIG_H264_MUXER
-static int h264_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+static int h264_check_bitstream(AVFormatContext *s, AVStream *st,
+                                const AVPacket *pkt)
 {
-    AVStream *st = s->streams[0];
     if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
                           AV_RB24(pkt->data) != 0x000001)
         return ff_stream_add_bitstream_filter(st, "h264_mp4toannexb", NULL);
@@ -364,9 +364,9 @@ const AVOutputFormat ff_h264_muxer = {
 #endif
 
 #if CONFIG_HEVC_MUXER
-static int hevc_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+static int hevc_check_bitstream(AVFormatContext *s, AVStream *st,
+                                const AVPacket *pkt)
 {
-    AVStream *st = s->streams[0];
     if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
                           AV_RB24(pkt->data) != 0x000001)
         return ff_stream_add_bitstream_filter(st, "hevc_mp4toannexb", NULL);
@@ -468,9 +468,9 @@ const AVOutputFormat ff_mpeg2video_muxer = {
 #endif
 
 #if CONFIG_OBU_MUXER
-static int obu_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+static int obu_check_bitstream(AVFormatContext *s, AVStream *st,
+                               const AVPacket *pkt)
 {
-    AVStream *st = s->streams[0];
     return ff_stream_add_bitstream_filter(st, "av1_metadata", "td=insert");
 }
 
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 2b024fd373..9861462405 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -984,15 +984,17 @@ static int seg_write_trailer(struct AVFormatContext *s)
     return ret;
 }
 
-static int seg_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+static int seg_check_bitstream(AVFormatContext *s, AVStream *st,
+                               const AVPacket *pkt)
 {
     SegmentContext *seg = s->priv_data;
     AVFormatContext *oc = seg->avf;
     if (oc->oformat->check_bitstream) {
-        int ret = oc->oformat->check_bitstream(oc, pkt);
+        AVStream *const ost = oc->streams[st->index];
+        int ret = oc->oformat->check_bitstream(oc, ost, pkt);
         if (ret == 1) {
-            FFStream *const  sti = ffstream( s->streams[pkt->stream_index]);
-            FFStream *const osti = ffstream(oc->streams[pkt->stream_index]);
+            FFStream *const  sti = ffstream(st);
+            FFStream *const osti = ffstream(ost);
              sti->bsfc = osti->bsfc;
             osti->bsfc = NULL;
         }
