diff --git a/libavformat/aacdec.c b/libavformat/aacdec.c
index 1f68f17bc9..ab97be60b5 100644
--- a/libavformat/aacdec.c
+++ b/libavformat/aacdec.c
@@ -112,7 +112,7 @@ static int adts_aac_read_header(AVFormatContext *s)
 
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id   = s->iformat->raw_codec_id;
-    st->internal->need_parsing         = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
 
     ff_id3v1_read(s);
     if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
diff --git a/libavformat/aadec.c b/libavformat/aadec.c
index 9771a1df81..1bb831b17f 100644
--- a/libavformat/aadec.c
+++ b/libavformat/aadec.c
@@ -182,7 +182,7 @@ static int aa_read_header(AVFormatContext *s)
     if (!strcmp(codec_name, "mp332")) {
         st->codecpar->codec_id = AV_CODEC_ID_MP3;
         st->codecpar->sample_rate = 22050;
-        st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
         avpriv_set_pts_info(st, 64, 8, 32000 * TIMEPREC);
         // encoded audio frame is MP3_FRAME_SIZE bytes (+1 with padding, unlikely)
     } else if (!strcmp(codec_name, "acelp85")) {
@@ -191,7 +191,7 @@ static int aa_read_header(AVFormatContext *s)
         st->codecpar->channels = 1;
         st->codecpar->sample_rate = 8500;
         st->codecpar->bit_rate = 8500;
-        st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
         avpriv_set_pts_info(st, 64, 8, 8500 * TIMEPREC);
     } else if (!strcmp(codec_name, "acelp16")) {
         st->codecpar->codec_id = AV_CODEC_ID_SIPR;
@@ -199,7 +199,7 @@ static int aa_read_header(AVFormatContext *s)
         st->codecpar->channels = 1;
         st->codecpar->sample_rate = 16000;
         st->codecpar->bit_rate = 16000;
-        st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
         avpriv_set_pts_info(st, 64, 8, 16000 * TIMEPREC);
     }
 
diff --git a/libavformat/acm.c b/libavformat/acm.c
index ccd043ee32..0ee9114f70 100644
--- a/libavformat/acm.c
+++ b/libavformat/acm.c
@@ -54,7 +54,7 @@ static int acm_read_header(AVFormatContext *s)
         return AVERROR_INVALIDDATA;
     st->start_time         = 0;
     st->duration           = AV_RL32(st->codecpar->extradata +  4) / st->codecpar->channels;
-    st->internal->need_parsing       = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
 
     return 0;
diff --git a/libavformat/ape.c b/libavformat/ape.c
index b6f9be4dbc..b1222d9ce0 100644
--- a/libavformat/ape.c
+++ b/libavformat/ape.c
@@ -434,7 +434,7 @@ static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
     if (index < 0)
         return -1;
 
-    if ((ret = avio_seek(s->pb, st->internal->index_entries[index].pos, SEEK_SET)) < 0)
+    if ((ret = avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET)) < 0)
         return ret;
     ape->currentframe = index;
     return 0;
diff --git a/libavformat/asfdec_f.c b/libavformat/asfdec_f.c
index eccf21f3b1..faba17aa03 100644
--- a/libavformat/asfdec_f.c
+++ b/libavformat/asfdec_f.c
@@ -378,13 +378,13 @@ static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
         if (is_dvr_ms_audio) {
             // codec_id and codec_tag are unreliable in dvr_ms
             // files. Set them later by probing stream.
-            st->internal->request_probe    = 1;
+            ffstream(st)->request_probe = 1;
             st->codecpar->codec_tag = 0;
         }
         if (st->codecpar->codec_id == AV_CODEC_ID_AAC)
-            st->internal->need_parsing = AVSTREAM_PARSE_NONE;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_NONE;
         else
-            st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
         /* We have to init the frame size at some point .... */
         pos2 = avio_tell(pb);
         if (size >= (pos2 + 8 - pos1 + 24)) {
@@ -443,7 +443,7 @@ static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
         st->codecpar->codec_tag = tag1;
         st->codecpar->codec_id  = ff_codec_get_id(ff_codec_bmp_tags, tag1);
         if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
-            st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
             /* issue658 contains wrong w/h and MS even puts a fake seq header
              * with wrong w/h in extradata while a correct one is in the stream.
              * maximum lameness */
@@ -453,9 +453,9 @@ static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
             st->codecpar->extradata_size = 0;
         }
         if (st->codecpar->codec_id == AV_CODEC_ID_H264)
-            st->internal->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
         if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4)
-            st->internal->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
     }
     pos2 = avio_tell(pb);
     avio_skip(pb, size - (pos2 - pos1 + 24));
@@ -1583,11 +1583,11 @@ static int asf_read_seek(AVFormatContext *s, int stream_index,
             asf->index_read = -1;
     }
 
-    if (asf->index_read > 0 && st->internal->index_entries) {
+    if (asf->index_read > 0 && ffstream(st)->index_entries) {
         int index = av_index_search_timestamp(st, pts, flags);
         if (index >= 0) {
             /* find the position */
-            uint64_t pos = st->internal->index_entries[index].pos;
+            uint64_t pos = ffstream(st)->index_entries[index].pos;
 
             /* do the seek */
             av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
diff --git a/libavformat/asfdec_o.c b/libavformat/asfdec_o.c
index 0a3af53a01..e0663dbb38 100644
--- a/libavformat/asfdec_o.c
+++ b/libavformat/asfdec_o.c
@@ -1513,13 +1513,14 @@ static int asf_read_seek(AVFormatContext *s, int stream_index,
                          int64_t timestamp, int flags)
 {
     ASFContext *asf = s->priv_data;
+    AVStream *const st = s->streams[stream_index];
     int idx, ret;
 
-    if (s->streams[stream_index]->internal->nb_index_entries && asf->is_simple_index) {
-        idx = av_index_search_timestamp(s->streams[stream_index], timestamp, flags);
-        if (idx < 0 || idx >= s->streams[stream_index]->internal->nb_index_entries)
+    if (ffstream(st)->nb_index_entries && asf->is_simple_index) {
+        idx = av_index_search_timestamp(st, timestamp, flags);
+        if (idx < 0 || idx >= ffstream(st)->nb_index_entries)
             return AVERROR_INVALIDDATA;
-        avio_seek(s->pb, s->streams[stream_index]->internal->index_entries[idx].pos, SEEK_SET);
+        avio_seek(s->pb, ffstream(st)->index_entries[idx].pos, SEEK_SET);
     } else {
         if ((ret = ff_seek_frame_binary(s, stream_index, timestamp, flags)) < 0)
             return ret;
diff --git a/libavformat/av1dec.c b/libavformat/av1dec.c
index e994312253..263b2a0532 100644
--- a/libavformat/av1dec.c
+++ b/libavformat/av1dec.c
@@ -75,9 +75,9 @@ static int av1_read_header(AVFormatContext *s)
 
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id = AV_CODEC_ID_AV1;
-    st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
 
-    st->internal->avctx->framerate = c->framerate;
+    ffstream(st)->avctx->framerate = c->framerate;
     // taken from rawvideo demuxers
     avpriv_set_pts_info(st, 64, 1, 1200000);
 
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 95746ec46a..3a5bc8a06d 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -821,8 +821,6 @@ typedef struct AVIndexEntry {
  */
 #define AV_DISPOSITION_TIMED_THUMBNAILS  0x0800
 
-typedef struct AVStreamInternal AVStreamInternal;
-
 /**
  * To specify text track kind (different from subtitles default).
  */
@@ -1003,12 +1001,6 @@ typedef struct AVStream {
      *
      */
     int pts_wrap_bits;
-
-    /**
-     * An opaque field for libavformat internal usage.
-     * Must not be accessed in any way by callers.
-     */
-    AVStreamInternal *internal;
 } AVStream;
 
 struct AVCodecParserContext *av_stream_get_parser(const AVStream *s);
diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index 5d695b9839..661da7046a 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -279,8 +279,9 @@ static void clean_index(AVFormatContext *s)
 
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st   = s->streams[i];
+        FFStream *const sti = ffstream(st);
         AVIStream *ast = st->priv_data;
-        int n          = st->internal->nb_index_entries;
+        int n          = sti->nb_index_entries;
         int max        = ast->sample_size;
         int64_t pos, size, ts;
 
@@ -290,9 +291,9 @@ static void clean_index(AVFormatContext *s)
         while (max < 1024)
             max += max;
 
-        pos  = st->internal->index_entries[0].pos;
-        size = st->internal->index_entries[0].size;
-        ts   = st->internal->index_entries[0].timestamp;
+        pos  = sti->index_entries[0].pos;
+        size = sti->index_entries[0].size;
+        ts   = sti->index_entries[0].timestamp;
 
         for (j = 0; j < size; j += max)
             av_add_index_entry(st, pos + j, ts + j, FFMIN(max, size - j), 0,
@@ -439,14 +440,14 @@ static int calculate_bitrate(AVFormatContext *s)
 
     for (i = 0; i<s->nb_streams; i++) {
         int64_t len = 0;
-        AVStream *st = s->streams[i];
+        FFStream *const sti = ffstream(s->streams[i]);
 
-        if (!st->internal->nb_index_entries)
+        if (!sti->nb_index_entries)
             continue;
 
-        for (j = 0; j < st->internal->nb_index_entries; j++)
-            len += st->internal->index_entries[j].size;
-        maxpos = FFMAX(maxpos, st->internal->index_entries[j-1].pos);
+        for (j = 0; j < sti->nb_index_entries; j++)
+            len += sti->index_entries[j].size;
+        maxpos = FFMAX(maxpos, sti->index_entries[j-1].pos);
         lensum += len;
     }
     if (maxpos < av_rescale(avi->io_fsize, 9, 10)) // index does not cover the whole file
@@ -457,15 +458,16 @@ static int calculate_bitrate(AVFormatContext *s)
     for (i = 0; i<s->nb_streams; i++) {
         int64_t len = 0;
         AVStream *st = s->streams[i];
+        FFStream *const sti = ffstream(st);
         int64_t duration;
         int64_t bitrate;
 
-        for (j = 0; j < st->internal->nb_index_entries; j++)
-            len += st->internal->index_entries[j].size;
+        for (j = 0; j < sti->nb_index_entries; j++)
+            len += sti->index_entries[j].size;
 
-        if (st->internal->nb_index_entries < 2 || st->codecpar->bit_rate > 0)
+        if (sti->nb_index_entries < 2 || st->codecpar->bit_rate > 0)
             continue;
-        duration = st->internal->index_entries[j-1].timestamp - st->internal->index_entries[0].timestamp;
+        duration = sti->index_entries[j-1].timestamp - sti->index_entries[0].timestamp;
         bitrate = av_rescale(8*len, st->time_base.den, duration * st->time_base.num);
         if (bitrate > 0) {
             st->codecpar->bit_rate = bitrate;
@@ -823,19 +825,19 @@ static int avi_read_header(AVFormatContext *s)
 
                     /* This is needed to get the pict type which is necessary
                      * for generating correct pts. */
-                    st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+                    ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
 
                     if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4 &&
                         ast->handler == MKTAG('X', 'V', 'I', 'D'))
                         st->codecpar->codec_tag = MKTAG('X', 'V', 'I', 'D');
 
                     if (st->codecpar->codec_tag == MKTAG('V', 'S', 'S', 'H'))
-                        st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
                     if (st->codecpar->codec_id == AV_CODEC_ID_RV40)
-                        st->internal->need_parsing = AVSTREAM_PARSE_NONE;
+                        ffstream(st)->need_parsing = AVSTREAM_PARSE_NONE;
                     if (st->codecpar->codec_id == AV_CODEC_ID_HEVC &&
                         st->codecpar->codec_tag == MKTAG('H', '2', '6', '5'))
-                        st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
 
                     if (st->codecpar->codec_id  == AV_CODEC_ID_AVRN &&
                         st->codecpar->codec_tag == MKTAG('A', 'V', 'R', 'n') &&
@@ -879,16 +881,16 @@ static int avi_read_header(AVFormatContext *s)
                         avio_skip(pb, 1);
                     /* Force parsing as several audio frames can be in
                      * one packet and timestamps refer to packet start. */
-                    st->internal->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
+                    ffstream(st)->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
                     /* ADTS header is in extradata, AAC without header must be
                      * stored as exact frames. Parser not needed and it will
                      * fail. */
                     if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
                         st->codecpar->extradata_size)
-                        st->internal->need_parsing = AVSTREAM_PARSE_NONE;
+                        ffstream(st)->need_parsing = AVSTREAM_PARSE_NONE;
                     // The flac parser does not work with AVSTREAM_PARSE_TIMESTAMPS
                     if (st->codecpar->codec_id == AV_CODEC_ID_FLAC)
-                        st->internal->need_parsing = AVSTREAM_PARSE_NONE;
+                        ffstream(st)->need_parsing = AVSTREAM_PARSE_NONE;
                     /* AVI files with Xan DPCM audio (wrongly) declare PCM
                      * audio in the header but have Axan as stream_code_tag. */
                     if (ast->handler == AV_RL32("Axan")) {
@@ -915,7 +917,7 @@ static int avi_read_header(AVFormatContext *s)
                     break;
                 case AVMEDIA_TYPE_SUBTITLE:
                     st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
-                    st->internal->request_probe= 1;
+                    ffstream(st)->request_probe= 1;
                     avio_skip(pb, size);
                     break;
                 default:
@@ -1050,12 +1052,12 @@ end_of_header:
             AVStream *st = s->streams[i];
             if (   st->codecpar->codec_id == AV_CODEC_ID_MPEG1VIDEO
                 || st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO)
-                st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
         }
 
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
-        if (st->internal->nb_index_entries)
+        if (ffstream(st)->nb_index_entries)
             break;
     }
     // DV-in-AVI cannot be non-interleaved, if set this must be
@@ -1341,9 +1343,10 @@ start_sync:
                 ast->remaining    = size;
 
                 if (size) {
+                    FFStream *const sti = ffstream(st);
                     uint64_t pos = avio_tell(pb) - 8;
-                    if (!st->internal->index_entries || !st->internal->nb_index_entries ||
-                        st->internal->index_entries[st->internal->nb_index_entries - 1].pos < pos) {
+                    if (!sti->index_entries || !sti->nb_index_entries ||
+                        sti->index_entries[sti->nb_index_entries - 1].pos < pos) {
                         av_add_index_entry(st, pos, ast->frame_offset, size,
                                            0, AVINDEX_KEYFRAME);
                     }
@@ -1369,14 +1372,15 @@ static int ni_prepare_read(AVFormatContext *s)
 
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st   = s->streams[i];
+        FFStream *const sti = ffstream(st);
         AVIStream *ast = st->priv_data;
         int64_t ts     = ast->frame_offset;
         int64_t last_ts;
 
-        if (!st->internal->nb_index_entries)
+        if (!sti->nb_index_entries)
             continue;
 
-        last_ts = st->internal->index_entries[st->internal->nb_index_entries - 1].timestamp;
+        last_ts = sti->index_entries[sti->nb_index_entries - 1].timestamp;
         if (!ast->remaining && ts > last_ts)
             continue;
 
@@ -1405,11 +1409,11 @@ static int ni_prepare_read(AVFormatContext *s)
     } else {
         i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
         if (i >= 0)
-            best_ast->frame_offset = best_st->internal->index_entries[i].timestamp;
+            best_ast->frame_offset = ffstream(best_st)->index_entries[i].timestamp;
     }
 
     if (i >= 0) {
-        int64_t pos = best_st->internal->index_entries[i].pos;
+        int64_t pos = ffstream(best_st)->index_entries[i].pos;
         pos += best_ast->packet_size - best_ast->remaining;
         if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
           return AVERROR_EOF;
@@ -1419,7 +1423,7 @@ static int ni_prepare_read(AVFormatContext *s)
         avi->stream_index = best_stream_index;
         if (!best_ast->remaining)
             best_ast->packet_size =
-            best_ast->remaining   = best_st->internal->index_entries[i].size;
+            best_ast->remaining   = ffstream(best_st)->index_entries[i].size;
     }
     else
         return AVERROR_EOF;
@@ -1508,15 +1512,15 @@ resync:
                 pkt->dts /= ast->sample_size;
             pkt->stream_index = avi->stream_index;
 
-            if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->internal->index_entries) {
+            if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ffstream(st)->index_entries) {
                 AVIndexEntry *e;
                 int index;
 
                 index = av_index_search_timestamp(st, ast->frame_offset, AVSEEK_FLAG_ANY);
-                e     = &st->internal->index_entries[index];
+                e     = &ffstream(st)->index_entries[index];
 
                 if (index >= 0 && e->timestamp == ast->frame_offset) {
-                    if (index == st->internal->nb_index_entries-1) {
+                    if (index == ffstream(st)->nb_index_entries-1) {
                         int key=1;
                         uint32_t state=-1;
                         if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
@@ -1552,7 +1556,7 @@ resync:
         }
         ast->seek_pos= 0;
 
-        if (!avi->non_interleaved && st->internal->nb_index_entries>1 && avi->index_loaded>1) {
+        if (!avi->non_interleaved && ffstream(st)->nb_index_entries>1 && avi->index_loaded>1) {
             int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
 
             if (avi->dts_max < dts) {
@@ -1651,9 +1655,9 @@ static int avi_read_idx1(AVFormatContext *s, int size)
     }
     if (!anykey) {
         for (index = 0; index < s->nb_streams; index++) {
-            st = s->streams[index];
-            if (st->internal->nb_index_entries)
-                st->internal->index_entries[0].flags |= AVINDEX_KEYFRAME;
+            FFStream *const sti = ffstream(s->streams[index]);
+            if (sti->nb_index_entries)
+                sti->index_entries[0].flags |= AVINDEX_KEYFRAME;
         }
     }
     return 0;
@@ -1678,16 +1682,17 @@ static int check_stream_max_drift(AVFormatContext *s)
         for (i = 0; i < s->nb_streams; i++) {
             AVStream *st = s->streams[i];
             AVIStream *ast = st->priv_data;
-            int n = st->internal->nb_index_entries;
-            while (idx[i] < n && st->internal->index_entries[idx[i]].pos < pos)
+            FFStream *const sti = ffstream(st);
+            int n = sti->nb_index_entries;
+            while (idx[i] < n && sti->index_entries[idx[i]].pos < pos)
                 idx[i]++;
             if (idx[i] < n) {
                 int64_t dts;
-                dts = av_rescale_q(st->internal->index_entries[idx[i]].timestamp /
+                dts = av_rescale_q(sti->index_entries[idx[i]].timestamp /
                                    FFMAX(ast->sample_size, 1),
                                    st->time_base, AV_TIME_BASE_Q);
                 min_dts = FFMIN(min_dts, dts);
-                min_pos = FFMIN(min_pos, st->internal->index_entries[idx[i]].pos);
+                min_pos = FFMIN(min_pos, sti->index_entries[idx[i]].pos);
             }
         }
         for (i = 0; i < s->nb_streams; i++) {
@@ -1696,7 +1701,7 @@ static int check_stream_max_drift(AVFormatContext *s)
 
             if (idx[i] && min_dts != INT64_MAX / 2) {
                 int64_t dts, delta_dts;
-                dts = av_rescale_q(st->internal->index_entries[idx[i] - 1].timestamp /
+                dts = av_rescale_q(ffstream(st)->index_entries[idx[i] - 1].timestamp /
                                    FFMAX(ast->sample_size, 1),
                                    st->time_base, AV_TIME_BASE_Q);
                 delta_dts = av_sat_sub64(dts, min_dts);
@@ -1726,30 +1731,31 @@ static int guess_ni_flag(AVFormatContext *s)
 
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
-        int n        = st->internal->nb_index_entries;
+        FFStream *const sti = ffstream(st);
+        int n = sti->nb_index_entries;
         unsigned int size;
 
         if (n <= 0)
             continue;
 
         if (n >= 2) {
-            int64_t pos = st->internal->index_entries[0].pos;
+            int64_t pos = sti->index_entries[0].pos;
             unsigned tag[2];
             avio_seek(s->pb, pos, SEEK_SET);
             tag[0] = avio_r8(s->pb);
             tag[1] = avio_r8(s->pb);
             avio_rl16(s->pb);
             size = avio_rl32(s->pb);
-            if (get_stream_idx(tag) == i && pos + size > st->internal->index_entries[1].pos)
+            if (get_stream_idx(tag) == i && pos + size > sti->index_entries[1].pos)
                 last_start = INT64_MAX;
-            if (get_stream_idx(tag) == i && size == st->internal->index_entries[0].size + 8)
+            if (get_stream_idx(tag) == i && size == sti->index_entries[0].size + 8)
                 last_start = INT64_MAX;
         }
 
-        if (st->internal->index_entries[0].pos > last_start)
-            last_start = st->internal->index_entries[0].pos;
-        if (st->internal->index_entries[n - 1].pos < first_end)
-            first_end = st->internal->index_entries[n - 1].pos;
+        if (sti->index_entries[0].pos > last_start)
+            last_start = sti->index_entries[0].pos;
+        if (sti->index_entries[n - 1].pos < first_end)
+            first_end = sti->index_entries[n - 1].pos;
     }
     avio_seek(s->pb, oldpos, SEEK_SET);
 
@@ -1817,6 +1823,7 @@ static int avi_read_seek(AVFormatContext *s, int stream_index,
 {
     AVIContext *avi = s->priv_data;
     AVStream *st;
+    FFStream *sti;
     int i, index;
     int64_t pos, pos_min;
     AVIStream *ast;
@@ -1835,25 +1842,26 @@ static int avi_read_seek(AVFormatContext *s, int stream_index,
     av_assert0(stream_index >= 0);
 
     st    = s->streams[stream_index];
+    sti   = ffstream(st);
     ast   = st->priv_data;
     index = av_index_search_timestamp(st,
                                       timestamp * FFMAX(ast->sample_size, 1),
                                       flags);
     if (index < 0) {
-        if (st->internal->nb_index_entries > 0)
+        if (sti->nb_index_entries > 0)
             av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
                    timestamp * FFMAX(ast->sample_size, 1),
-                   st->internal->index_entries[0].timestamp,
-                   st->internal->index_entries[st->internal->nb_index_entries - 1].timestamp);
+                   sti->index_entries[0].timestamp,
+                   sti->index_entries[sti->nb_index_entries - 1].timestamp);
         return AVERROR_INVALIDDATA;
     }
 
     /* find the position */
-    pos       = st->internal->index_entries[index].pos;
-    timestamp = st->internal->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
+    pos       = sti->index_entries[index].pos;
+    timestamp = sti->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
 
     av_log(s, AV_LOG_TRACE, "XX %"PRId64" %d %"PRId64"\n",
-            timestamp, index, st->internal->index_entries[index].timestamp);
+            timestamp, index, sti->index_entries[index].timestamp);
 
     if (CONFIG_DV_DEMUXER && avi->dv_demux) {
         /* One and only one real stream for DV in AVI, and it has video  */
@@ -1884,7 +1892,7 @@ static int avi_read_seek(AVFormatContext *s, int stream_index,
             continue;
         }
 
-        if (st2->internal->nb_index_entries <= 0)
+        if (ffstream(st2)->nb_index_entries <= 0)
             continue;
 
 //        av_assert1(st2->codecpar->block_align);
@@ -1898,14 +1906,14 @@ static int avi_read_seek(AVFormatContext *s, int stream_index,
                                           (st2->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
         if (index < 0)
             index = 0;
-        ast2->seek_pos = st2->internal->index_entries[index].pos;
+        ast2->seek_pos = ffstream(st2)->index_entries[index].pos;
         pos_min = FFMIN(pos_min,ast2->seek_pos);
     }
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st2 = s->streams[i];
         AVIStream *ast2 = st2->priv_data;
 
-        if (ast2->sub_ctx || st2->internal->nb_index_entries <= 0)
+        if (ast2->sub_ctx || ffstream(st2)->nb_index_entries <= 0)
             continue;
 
         index = av_index_search_timestamp(
@@ -1914,9 +1922,9 @@ static int avi_read_seek(AVFormatContext *s, int stream_index,
                 flags | AVSEEK_FLAG_BACKWARD | (st2->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
         if (index < 0)
             index = 0;
-        while (!avi->non_interleaved && index>0 && st2->internal->index_entries[index-1].pos >= pos_min)
+        while (!avi->non_interleaved && index>0 && ffstream(st2)->index_entries[index-1].pos >= pos_min)
             index--;
-        ast2->frame_offset = st2->internal->index_entries[index].timestamp;
+        ast2->frame_offset = ffstream(st2)->index_entries[index].timestamp;
     }
 
     /* do the seek */
diff --git a/libavformat/bink.c b/libavformat/bink.c
index 2488aeae2f..8b8841dba9 100644
--- a/libavformat/bink.c
+++ b/libavformat/bink.c
@@ -225,8 +225,8 @@ static int read_header(AVFormatContext *s)
             return ret;
     }
 
-    if (vst->internal->index_entries)
-        avio_seek(pb, vst->internal->index_entries[0].pos + bink->smush_size, SEEK_SET);
+    if (ffstream(vst)->index_entries)
+        avio_seek(pb, ffstream(vst)->index_entries[0].pos + bink->smush_size, SEEK_SET);
     else
         avio_skip(pb, 4);
 
@@ -256,8 +256,8 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
             return AVERROR(EIO);
         }
 
-        bink->remain_packet_size = st->internal->index_entries[index_entry].size;
-        bink->flags = st->internal->index_entries[index_entry].flags;
+        bink->remain_packet_size = ffstream(st)->index_entries[index_entry].size;
+        bink->flags = ffstream(st)->index_entries[index_entry].flags;
         bink->current_track = 0;
     }
 
@@ -313,7 +313,7 @@ static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, in
         return -1;
 
     /* seek to the first frame */
-    ret = avio_seek(s->pb, vst->internal->index_entries[0].pos + bink->smush_size, SEEK_SET);
+    ret = avio_seek(s->pb, ffstream(vst)->index_entries[0].pos + bink->smush_size, SEEK_SET);
     if (ret < 0)
         return ret;
 
diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c
index e9d171e36b..defa0cda7f 100644
--- a/libavformat/cafdec.c
+++ b/libavformat/cafdec.c
@@ -342,7 +342,7 @@ found_data:
     if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
         if (caf->data_size > 0)
             st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet;
-    } else if (st->internal->nb_index_entries && st->duration > 0) {
+    } else if (ffstream(st)->nb_index_entries && st->duration > 0) {
         if (st->codecpar->sample_rate && caf->data_size / st->duration > INT64_MAX / st->codecpar->sample_rate / 8) {
             av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %d * 8 * %"PRId64"\n",
                    st->codecpar->sample_rate, caf->data_size / st->duration);
@@ -372,6 +372,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     AVIOContext *pb = s->pb;
     AVStream *st      = s->streams[0];
+    FFStream *const sti = ffstream(st);
     CafContext *caf   = s->priv_data;
     int res, pkt_size = 0, pkt_frames = 0;
     int64_t left      = CAF_MAX_PKT_SIZE;
@@ -395,13 +396,13 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
         pkt_size   = (CAF_MAX_PKT_SIZE / pkt_size) * pkt_size;
         pkt_size   = FFMIN(pkt_size, left);
         pkt_frames = pkt_size / caf->bytes_per_packet;
-    } else if (st->internal->nb_index_entries) {
-        if (caf->packet_cnt < st->internal->nb_index_entries - 1) {
-            pkt_size   = st->internal->index_entries[caf->packet_cnt + 1].pos       - st->internal->index_entries[caf->packet_cnt].pos;
-            pkt_frames = st->internal->index_entries[caf->packet_cnt + 1].timestamp - st->internal->index_entries[caf->packet_cnt].timestamp;
-        } else if (caf->packet_cnt == st->internal->nb_index_entries - 1) {
-            pkt_size   = caf->num_bytes - st->internal->index_entries[caf->packet_cnt].pos;
-            pkt_frames = st->duration   - st->internal->index_entries[caf->packet_cnt].timestamp;
+    } else if (sti->nb_index_entries) {
+        if (caf->packet_cnt < sti->nb_index_entries - 1) {
+            pkt_size   = sti->index_entries[caf->packet_cnt + 1].pos       - sti->index_entries[caf->packet_cnt].pos;
+            pkt_frames = sti->index_entries[caf->packet_cnt + 1].timestamp - sti->index_entries[caf->packet_cnt].timestamp;
+        } else if (caf->packet_cnt == sti->nb_index_entries - 1) {
+            pkt_size   = caf->num_bytes - sti->index_entries[caf->packet_cnt].pos;
+            pkt_frames = st->duration   - sti->index_entries[caf->packet_cnt].timestamp;
         } else {
             return AVERROR(EIO);
         }
@@ -440,10 +441,10 @@ static int read_seek(AVFormatContext *s, int stream_index,
             pos = FFMIN(pos, caf->data_size);
         packet_cnt = pos / caf->bytes_per_packet;
         frame_cnt  = caf->frames_per_packet * packet_cnt;
-    } else if (st->internal->nb_index_entries) {
+    } else if (ffstream(st)->nb_index_entries) {
         packet_cnt = av_index_search_timestamp(st, timestamp, flags);
-        frame_cnt  = st->internal->index_entries[packet_cnt].timestamp;
-        pos        = st->internal->index_entries[packet_cnt].pos;
+        frame_cnt  = ffstream(st)->index_entries[packet_cnt].timestamp;
+        pos        = ffstream(st)->index_entries[packet_cnt].pos;
     } else {
         return -1;
     }
diff --git a/libavformat/cinedec.c b/libavformat/cinedec.c
index f1fae6e5db..c53ccb9c91 100644
--- a/libavformat/cinedec.c
+++ b/libavformat/cinedec.c
@@ -288,10 +288,10 @@ static int cine_read_packet(AVFormatContext *avctx, AVPacket *pkt)
     AVIOContext *pb = avctx->pb;
     int n, size, ret;
 
-    if (cine->pts >= st->internal->nb_index_entries)
+    if (cine->pts >= ffstream(st)->nb_index_entries)
         return AVERROR_EOF;
 
-    avio_seek(pb, st->internal->index_entries[cine->pts].pos, SEEK_SET);
+    avio_seek(pb, ffstream(st)->index_entries[cine->pts].pos, SEEK_SET);
     n = avio_rl32(pb);
     if (n < 8)
         return AVERROR_INVALIDDATA;
diff --git a/libavformat/concatdec.c b/libavformat/concatdec.c
index 30db456b0e..8e3aeafed0 100644
--- a/libavformat/concatdec.c
+++ b/libavformat/concatdec.c
@@ -667,8 +667,8 @@ static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
         }
     }
 
-    if (cat->cur_file->duration == AV_NOPTS_VALUE && st->internal->cur_dts != AV_NOPTS_VALUE) {
-        int64_t next_dts = av_rescale_q(st->internal->cur_dts, st->time_base, AV_TIME_BASE_Q);
+    if (cat->cur_file->duration == AV_NOPTS_VALUE && ffstream(st)->cur_dts != AV_NOPTS_VALUE) {
+        int64_t next_dts = av_rescale_q(ffstream(st)->cur_dts, st->time_base, AV_TIME_BASE_Q);
         if (cat->cur_file->next_dts == AV_NOPTS_VALUE || next_dts > cat->cur_file->next_dts) {
             cat->cur_file->next_dts = next_dts;
         }
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 679e30611c..e36a9f6a07 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -2346,8 +2346,8 @@ static int dash_check_bitstream(struct AVFormatContext *s, const AVPacket *avpkt
         if (ret == 1) {
             AVStream *st = s->streams[avpkt->stream_index];
             AVStream *ost = oc->streams[0];
-            st->internal->bsfc = ost->internal->bsfc;
-            ost->internal->bsfc = NULL;
+            ffstream(st)->bsfc  = ffstream(ost)->bsfc;
+            ffstream(ost)->bsfc = NULL;
         }
         return ret;
     }
diff --git a/libavformat/dhav.c b/libavformat/dhav.c
index 92bbfab7c5..0915cd197d 100644
--- a/libavformat/dhav.c
+++ b/libavformat/dhav.c
@@ -434,10 +434,10 @@ static int dhav_read_seek(AVFormatContext *s, int stream_index,
 
     if (index < 0)
         return -1;
-    if (avio_seek(s->pb, st->internal->index_entries[index].pos, SEEK_SET) < 0)
+    if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
         return -1;
 
-    pts = st->internal->index_entries[index].timestamp;
+    pts = ffstream(st)->index_entries[index].timestamp;
 
     for (int n = 0; n < s->nb_streams; n++) {
         AVStream *st = s->streams[n];
diff --git a/libavformat/dtshddec.c b/libavformat/dtshddec.c
index 2900623690..b84588af7c 100644
--- a/libavformat/dtshddec.c
+++ b/libavformat/dtshddec.c
@@ -65,7 +65,7 @@ static int dtshd_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id   = AV_CODEC_ID_DTS;
-    st->internal->need_parsing         = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
 
     for (;;) {
         chunk_type = avio_rb64(pb);
diff --git a/libavformat/dump.c b/libavformat/dump.c
index cf892de107..4824965ee9 100644
--- a/libavformat/dump.c
+++ b/libavformat/dump.c
@@ -508,6 +508,7 @@ static void dump_stream_format(const AVFormatContext *ic, int i,
     char buf[256];
     int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
     const AVStream *st = ic->streams[i];
+    const FFStream *const sti = cffstream(st);
     const AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
     const char *separator = ic->dump_separator;
     AVCodecContext *avctx;
@@ -524,12 +525,12 @@ static void dump_stream_format(const AVFormatContext *ic, int i,
     }
 
     // Fields which are missing from AVCodecParameters need to be taken from the AVCodecContext
-    avctx->properties   = st->internal->avctx->properties;
-    avctx->codec        = st->internal->avctx->codec;
-    avctx->qmin         = st->internal->avctx->qmin;
-    avctx->qmax         = st->internal->avctx->qmax;
-    avctx->coded_width  = st->internal->avctx->coded_width;
-    avctx->coded_height = st->internal->avctx->coded_height;
+    avctx->properties   = sti->avctx->properties;
+    avctx->codec        = sti->avctx->codec;
+    avctx->qmin         = sti->avctx->qmin;
+    avctx->qmax         = sti->avctx->qmax;
+    avctx->coded_width  = sti->avctx->coded_width;
+    avctx->coded_height = sti->avctx->coded_height;
 
     if (separator)
         av_opt_set(avctx, "dump_separator", separator, 0);
@@ -544,7 +545,7 @@ static void dump_stream_format(const AVFormatContext *ic, int i,
         av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
     if (lang)
         av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
-    av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->internal->codec_info_nb_frames,
+    av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", sti->codec_info_nb_frames,
            st->time_base.num, st->time_base.den);
     av_log(NULL, AV_LOG_INFO, ": %s", buf);
 
diff --git a/libavformat/electronicarts.c b/libavformat/electronicarts.c
index 4d0fddab3d..2fb09b34ca 100644
--- a/libavformat/electronicarts.c
+++ b/libavformat/electronicarts.c
@@ -503,7 +503,7 @@ static int init_video_stream(AVFormatContext *s, VideoProperties *video)
     st->codecpar->codec_id    = video->codec;
     // parsing is necessary to make FFmpeg generate correct timestamps
     if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO)
-        st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
     st->codecpar->codec_tag   = 0; /* no fourcc */
     st->codecpar->width       = video->width;
     st->codecpar->height      = video->height;
diff --git a/libavformat/fifo.c b/libavformat/fifo.c
index 10511167cd..51d7fa5b94 100644
--- a/libavformat/fifo.c
+++ b/libavformat/fifo.c
@@ -139,7 +139,7 @@ static int fifo_thread_write_header(FifoThreadContext *ctx)
     }
 
     for (i = 0;i < avf2->nb_streams; i++)
-        avf2->streams[i]->internal->cur_dts = 0;
+        ffstream(avf2->streams[i])->cur_dts = 0;
 
     ret = avformat_write_header(avf2, &format_options);
     if (!ret)
diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c
index 2730e9cc50..6992220d40 100644
--- a/libavformat/flacdec.c
+++ b/libavformat/flacdec.c
@@ -38,10 +38,8 @@ typedef struct FLACDecContext {
 static void reset_index_position(int64_t metadata_head_size, AVStream *st)
 {
     /* the real seek index offset should be the size of metadata blocks with the offset in the frame blocks */
-    int i;
-    for(i=0; i<st->internal->nb_index_entries; i++) {
-        st->internal->index_entries[i].pos += metadata_head_size;
-    }
+    for (int i = 0; i < ffstream(st)->nb_index_entries; i++)
+        ffstream(st)->index_entries[i].pos += metadata_head_size;
 }
 
 static int flac_read_header(AVFormatContext *s)
@@ -55,7 +53,7 @@ static int flac_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id = AV_CODEC_ID_FLAC;
-    st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     /* the parameters will be extracted from the compressed bitstream */
 
     /* if fLaC marker is not found, assume there is no header */
@@ -286,7 +284,7 @@ static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_inde
                 av_assert1(!pkt->size);
             }
         }
-        av_parser_parse2(parser, st->internal->avctx,
+        av_parser_parse2(parser, ffstream(st)->avctx,
                          &data, &size, pkt->data, pkt->size,
                          pkt->pts, pkt->dts, *ppos);
 
@@ -317,10 +315,10 @@ static int flac_seek(AVFormatContext *s, int stream_index, int64_t timestamp, in
     }
 
     index = av_index_search_timestamp(s->streams[0], timestamp, flags);
-    if(index<0 || index >= s->streams[0]->internal->nb_index_entries)
+    if(index<0 || index >= ffstream(s->streams[0])->nb_index_entries)
         return -1;
 
-    e = s->streams[0]->internal->index_entries[index];
+    e = ffstream(s->streams[0])->index_entries[index];
     pos = avio_seek(s->pb, e.pos, SEEK_SET);
     if (pos >= 0) {
         return 0;
diff --git a/libavformat/flic.c b/libavformat/flic.c
index efd9e54308..14c614d831 100644
--- a/libavformat/flic.c
+++ b/libavformat/flic.c
@@ -268,7 +268,7 @@ static int flic_read_seek(AVFormatContext *s, int stream_index,
     int64_t pos, ts;
     int index;
 
-    if (!st->internal->index_entries || stream_index != flic->video_stream_index)
+    if (!ffstream(st)->index_entries || stream_index != flic->video_stream_index)
         return -1;
 
     index = av_index_search_timestamp(st, pts, flags);
@@ -278,8 +278,8 @@ static int flic_read_seek(AVFormatContext *s, int stream_index,
     if (index < 0)
         return -1;
 
-    pos = st->internal->index_entries[index].pos;
-    ts  = st->internal->index_entries[index].timestamp;
+    pos = ffstream(st)->index_entries[index].pos;
+    ts  = ffstream(st)->index_entries[index].timestamp;
     flic->frame_number = ts;
     avio_seek(s->pb, pos, SEEK_SET);
     return 0;
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index b4a419177a..c973de1681 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -143,7 +143,7 @@ static void add_keyframes_index(AVFormatContext *s)
     av_assert0(flv->last_keyframe_stream_index <= s->nb_streams);
     stream = s->streams[flv->last_keyframe_stream_index];
 
-    if (stream->internal->nb_index_entries == 0) {
+    if (ffstream(stream)->nb_index_entries == 0) {
         for (i = 0; i < flv->keyframe_count; i++) {
             av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
                    flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
@@ -272,7 +272,7 @@ static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
         break;
     case FLV_CODECID_MP3:
         apar->codec_id      = AV_CODEC_ID_MP3;
-        astream->internal->need_parsing = AVSTREAM_PARSE_FULL;
+        ffstream(astream)->need_parsing = AVSTREAM_PARSE_FULL;
         break;
     case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
         // in case metadata does not otherwise declare samplerate
@@ -363,7 +363,7 @@ static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
         break;
     case FLV_CODECID_H264:
         par->codec_id = AV_CODEC_ID_H264;
-        vstream->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+        ffstream(vstream)->need_parsing = AVSTREAM_PARSE_HEADERS;
         ret = 3;     // not 4, reading packet type will consume one byte
         break;
     case FLV_CODECID_MPEG4:
@@ -375,7 +375,7 @@ static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
         par->codec_tag = flv_codecid;
     }
 
-    if (!vstream->internal->need_context_update && par->codec_id != old_codec_id) {
+    if (!ffstream(vstream)->need_context_update && par->codec_id != old_codec_id) {
         avpriv_request_sample(s, "Changing the codec id midstream");
         return AVERROR_PATCHWELCOME;
     }
@@ -815,7 +815,7 @@ static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
 
     if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
         return ret;
-    st->internal->need_context_update = 1;
+    ffstream(st)->need_context_update = 1;
     return 0;
 }
 
@@ -837,17 +837,16 @@ static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
 
 static void clear_index_entries(AVFormatContext *s, int64_t pos)
 {
-    int i, j, out;
     av_log(s, AV_LOG_WARNING,
            "Found invalid index entries, clearing the index.\n");
-    for (i = 0; i < s->nb_streams; i++) {
-        AVStream *st = s->streams[i];
+    for (unsigned i = 0; i < s->nb_streams; i++) {
+        FFStream *const sti = ffstream(s->streams[i]);
+        int out = 0;
         /* Remove all index entries that point to >= pos */
-        out = 0;
-        for (j = 0; j < st->internal->nb_index_entries; j++)
-            if (st->internal->index_entries[j].pos < pos)
-                st->internal->index_entries[out++] = st->internal->index_entries[j];
-        st->internal->nb_index_entries = out;
+        for (int j = 0; j < sti->nb_index_entries; j++)
+            if (sti->index_entries[j].pos < pos)
+                sti->index_entries[out++] = sti->index_entries[j];
+        sti->nb_index_entries = out;
     }
 }
 
diff --git a/libavformat/gxf.c b/libavformat/gxf.c
index 7d23cbd692..ef1a50f5c2 100644
--- a/libavformat/gxf.c
+++ b/libavformat/gxf.c
@@ -130,13 +130,13 @@ static int get_sindex(AVFormatContext *s, int id, int format) {
         case 20:
             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
             st->codecpar->codec_id = AV_CODEC_ID_MPEG2VIDEO;
-            st->internal->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
             break;
         case 22:
         case 23:
             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
             st->codecpar->codec_id = AV_CODEC_ID_MPEG1VIDEO;
-            st->internal->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
             break;
         case 9:
             st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
@@ -169,7 +169,7 @@ static int get_sindex(AVFormatContext *s, int id, int format) {
         case 29: /* AVCHD */
             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
             st->codecpar->codec_id = AV_CODEC_ID_H264;
-            st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
             break;
         // timecode tracks:
         case 7:
@@ -575,9 +575,9 @@ static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int
                                     AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
     if (idx < 0)
         return -1;
-    pos = st->internal->index_entries[idx].pos;
-    if (idx < st->internal->nb_index_entries - 2)
-        maxlen = st->internal->index_entries[idx + 2].pos - pos;
+    pos = ffstream(st)->index_entries[idx].pos;
+    if (idx < ffstream(st)->nb_index_entries - 2)
+        maxlen = ffstream(st)->index_entries[idx + 2].pos - pos;
     maxlen = FFMAX(maxlen, 200 * 1024);
     res = avio_seek(s->pb, pos, SEEK_SET);
     if (res < 0)
diff --git a/libavformat/hdsenc.c b/libavformat/hdsenc.c
index 1793fb69d5..0c3c98fcc0 100644
--- a/libavformat/hdsenc.c
+++ b/libavformat/hdsenc.c
@@ -504,11 +504,11 @@ static int hds_write_packet(AVFormatContext *s, AVPacket *pkt)
     int64_t end_dts = os->fragment_index * (int64_t)c->min_frag_duration;
     int ret;
 
-    if (st->internal->first_dts == AV_NOPTS_VALUE)
-        st->internal->first_dts = pkt->dts;
+    if (ffstream(st)->first_dts == AV_NOPTS_VALUE)
+        ffstream(st)->first_dts = pkt->dts;
 
     if ((!os->has_video || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
-        av_compare_ts(pkt->dts - st->internal->first_dts, st->time_base,
+        av_compare_ts(pkt->dts - ffstream(st)->first_dts, st->time_base,
                       end_dts, AV_TIME_BASE_Q) >= 0 &&
         pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {
 
diff --git a/libavformat/hls.c b/libavformat/hls.c
index 421a60a476..9fd4ad9f32 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -1794,7 +1794,7 @@ static int set_stream_info_from_input_stream(AVStream *st, struct playlist *pls,
         memcpy(dst_data, sd_src->data, sd_src->size);
     }
 
-    st->internal->need_context_update = 1;
+    ffstream(st)->need_context_update = 1;
 
     return 0;
 }
diff --git a/libavformat/ifv.c b/libavformat/ifv.c
index b3b9213aa5..54c8fde1d2 100644
--- a/libavformat/ifv.c
+++ b/libavformat/ifv.c
@@ -196,15 +196,15 @@ static int ifv_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     if (ifv->next_video_index < ifv->total_vframes) {
         st = s->streams[ifv->video_stream_index];
-        if (ifv->next_video_index < st->internal->nb_index_entries)
-            e_next = ev = &st->internal->index_entries[ifv->next_video_index];
+        if (ifv->next_video_index < ffstream(st)->nb_index_entries)
+            e_next = ev = &ffstream(st)->index_entries[ifv->next_video_index];
     }
 
     if (ifv->is_audio_present &&
         ifv->next_audio_index < ifv->total_aframes) {
         st = s->streams[ifv->audio_stream_index];
-        if (ifv->next_audio_index < st->internal->nb_index_entries) {
-            ea = &st->internal->index_entries[ifv->next_audio_index];
+        if (ifv->next_audio_index < ffstream(st)->nb_index_entries) {
+            ea = &ffstream(st)->index_entries[ifv->next_audio_index];
             if (!ev || ea->timestamp < ev->timestamp)
                 e_next = ea;
         }
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index 485444ed31..ad4cc623c8 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -209,7 +209,7 @@ int ff_img_read_header(AVFormatContext *s1)
         s->is_pipe = 0;
     else {
         s->is_pipe       = 1;
-        st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
     }
 
     if (s->ts_from_file == 2) {
@@ -482,7 +482,7 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
             return AVERROR_EOF;
         if (s->frame_size > 0) {
             size[0] = s->frame_size;
-        } else if (!s1->streams[0]->internal->parser) {
+        } else if (!ffstream(s1->streams[0])->parser) {
             size[0] = avio_size(s1->pb);
         } else {
             size[0] = 4096;
@@ -590,7 +590,7 @@ static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
         int index = av_index_search_timestamp(st, timestamp, flags);
         if(index < 0)
             return -1;
-        s1->img_number = st->internal->index_entries[index].pos;
+        s1->img_number = ffstream(st)->index_entries[index].pos;
         return 0;
     }
 
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 813032870f..6f53bd44a9 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -185,7 +185,12 @@ static av_always_inline FFFormatContext *ffformatcontext(AVFormatContext *s)
     return (FFFormatContext*)s;
 }
 
-struct AVStreamInternal {
+typedef struct FFStream {
+    /**
+     * The public context.
+     */
+    AVStream pub;
+
     /**
      * Set to 1 if the codec allows reordering, so pts can be different
      * from dts.
@@ -416,7 +421,17 @@ struct AVStreamInternal {
      */
     int64_t first_dts;
     int64_t cur_dts;
-};
+} FFStream;
+
+static av_always_inline FFStream *ffstream(AVStream *st)
+{
+    return (FFStream*)st;
+}
+
+static av_always_inline const FFStream *cffstream(const AVStream *st)
+{
+    return (FFStream*)st;
+}
 
 void avpriv_stream_set_need_parsing(AVStream *st, enum AVStreamParseType type);
 
diff --git a/libavformat/ipudec.c b/libavformat/ipudec.c
index 27b6b59356..a314a89001 100644
--- a/libavformat/ipudec.c
+++ b/libavformat/ipudec.c
@@ -62,7 +62,7 @@ static int ipu_read_header(AVFormatContext *s)
     st->start_time         = 0;
     st->duration           =
     st->nb_frames          = avio_rl32(pb);
-    st->internal->need_parsing       = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     avpriv_set_pts_info(st, 64, 1, 25);
 
     return 0;
diff --git a/libavformat/iv8.c b/libavformat/iv8.c
index b06797cbf3..a3954ca1ed 100644
--- a/libavformat/iv8.c
+++ b/libavformat/iv8.c
@@ -47,7 +47,7 @@ static int read_header(AVFormatContext *s)
 
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id = AV_CODEC_ID_MPEG4;
-    st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
     avpriv_set_pts_info(st, 64, 1, 90000);
 
     return 0;
diff --git a/libavformat/ivfdec.c b/libavformat/ivfdec.c
index 26717c4999..511f2387ed 100644
--- a/libavformat/ivfdec.c
+++ b/libavformat/ivfdec.c
@@ -56,7 +56,7 @@ static int read_header(AVFormatContext *s)
     st->duration          = avio_rl32(s->pb);
     avio_skip(s->pb, 4); // unused
 
-    st->internal->need_parsing      = AVSTREAM_PARSE_HEADERS;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
 
     if (!time_base.den || !time_base.num) {
         av_log(s, AV_LOG_ERROR, "Invalid frame rate\n");
diff --git a/libavformat/jvdec.c b/libavformat/jvdec.c
index a044f50831..db76fedc4c 100644
--- a/libavformat/jvdec.c
+++ b/libavformat/jvdec.c
@@ -74,9 +74,9 @@ static int read_header(AVFormatContext *s)
     JVDemuxContext *jv = s->priv_data;
     AVIOContext *pb = s->pb;
     AVStream *vst, *ast;
+    FFStream *asti;
     int64_t audio_pts = 0;
     int64_t offset;
-    int i;
 
     avio_skip(pb, 80);
 
@@ -84,6 +84,7 @@ static int read_header(AVFormatContext *s)
     vst = avformat_new_stream(s, NULL);
     if (!ast || !vst)
         return AVERROR(ENOMEM);
+    asti = ffstream(ast);
 
     vst->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
     vst->codecpar->codec_id    = AV_CODEC_ID_JV;
@@ -92,7 +93,7 @@ static int read_header(AVFormatContext *s)
     vst->codecpar->height      = avio_rl16(pb);
     vst->duration           =
     vst->nb_frames          =
-    ast->internal->nb_index_entries   = avio_rl16(pb);
+    asti->nb_index_entries  = avio_rl16(pb);
     avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
 
     avio_skip(pb, 4);
@@ -107,17 +108,17 @@ static int read_header(AVFormatContext *s)
 
     avio_skip(pb, 10);
 
-    ast->internal->index_entries = av_malloc(ast->internal->nb_index_entries *
-                                   sizeof(*ast->internal->index_entries));
-    if (!ast->internal->index_entries)
+    asti->index_entries = av_malloc(asti->nb_index_entries *
+                                    sizeof(*asti->index_entries));
+    if (!asti->index_entries)
         return AVERROR(ENOMEM);
 
-    jv->frames = av_malloc(ast->internal->nb_index_entries * sizeof(JVFrame));
+    jv->frames = av_malloc(asti->nb_index_entries * sizeof(*jv->frames));
     if (!jv->frames)
         return AVERROR(ENOMEM);
-    offset = 0x68 + ast->internal->nb_index_entries * 16;
-    for (i = 0; i < ast->internal->nb_index_entries; i++) {
-        AVIndexEntry *e   = ast->internal->index_entries + i;
+    offset = 0x68 + asti->nb_index_entries * 16;
+    for (int i = 0; i < asti->nb_index_entries; i++) {
+        AVIndexEntry *e   = asti->index_entries + i;
         JVFrame      *jvf = jv->frames + i;
 
         /* total frame size including audio, video, palette data and padding */
@@ -164,8 +165,8 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
     AVStream *ast = s->streams[0];
     int ret;
 
-    while (!avio_feof(s->pb) && jv->pts < ast->internal->nb_index_entries) {
-        const AVIndexEntry *e   = ast->internal->index_entries + jv->pts;
+    while (!avio_feof(s->pb) && jv->pts < ffstream(ast)->nb_index_entries) {
+        const AVIndexEntry *const e = ffstream(ast)->index_entries + jv->pts;
         const JVFrame      *jvf = jv->frames + jv->pts;
 
         switch (jv->state) {
@@ -238,9 +239,9 @@ static int read_seek(AVFormatContext *s, int stream_index,
         return 0;
     }
 
-    if (i < 0 || i >= ast->internal->nb_index_entries)
+    if (i < 0 || i >= ffstream(ast)->nb_index_entries)
         return 0;
-    if (avio_seek(s->pb, ast->internal->index_entries[i].pos, SEEK_SET) < 0)
+    if (avio_seek(s->pb, ffstream(ast)->index_entries[i].pos, SEEK_SET) < 0)
         return -1;
 
     jv->state = JV_AUDIO;
diff --git a/libavformat/lmlm4.c b/libavformat/lmlm4.c
index dbeaceab9c..b0bfad001b 100644
--- a/libavformat/lmlm4.c
+++ b/libavformat/lmlm4.c
@@ -67,14 +67,14 @@ static int lmlm4_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id   = AV_CODEC_ID_MPEG4;
-    st->internal->need_parsing      = AVSTREAM_PARSE_HEADERS;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
     avpriv_set_pts_info(st, 64, 1001, 30000);
 
     if (!(st = avformat_new_stream(s, NULL)))
         return AVERROR(ENOMEM);
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id   = AV_CODEC_ID_MP2;
-    st->internal->need_parsing      = AVSTREAM_PARSE_HEADERS;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
 
     /* the parameters will be extracted from the compressed bitstream */
     return 0;
diff --git a/libavformat/loasdec.c b/libavformat/loasdec.c
index 3c84d9f37e..e739b6c196 100644
--- a/libavformat/loasdec.c
+++ b/libavformat/loasdec.c
@@ -75,7 +75,7 @@ static int loas_read_header(AVFormatContext *s)
 
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id = s->iformat->raw_codec_id;
-    st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
 
     //LCM of all possible AAC sample rates
     avpriv_set_pts_info(st, 64, 1, 28224000);
diff --git a/libavformat/lxfdec.c b/libavformat/lxfdec.c
index 79ccc77747..7889abb2c9 100644
--- a/libavformat/lxfdec.c
+++ b/libavformat/lxfdec.c
@@ -262,7 +262,7 @@ static int lxf_read_header(AVFormatContext *s)
     st->codecpar->bit_rate   = 1000000 * ((video_params >> 14) & 0xFF);
     st->codecpar->codec_tag  = video_params & 0xF;
     st->codecpar->codec_id   = ff_codec_get_id(lxf_tags, st->codecpar->codec_tag);
-    st->internal->need_parsing         = AVSTREAM_PARSE_HEADERS;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
 
     av_log(s, AV_LOG_DEBUG, "record: %x = %i-%02i-%02i\n",
            record_date, 1900 + (record_date & 0x7F), (record_date >> 7) & 0xF,
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 4bc6b40f44..17280e233e 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -2802,7 +2802,7 @@ static int matroska_parse_tracks(AVFormatContext *s)
                           255);
             }
             if (st->codecpar->codec_id != AV_CODEC_ID_HEVC)
-                st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
 
             if (track->default_duration) {
                 int div = track->default_duration <= INT64_MAX ? 1 : 2;
@@ -2861,9 +2861,9 @@ static int matroska_parse_tracks(AVFormatContext *s)
             if (st->codecpar->codec_id == AV_CODEC_ID_MP3 ||
                 st->codecpar->codec_id == AV_CODEC_ID_MLP ||
                 st->codecpar->codec_id == AV_CODEC_ID_TRUEHD)
-                st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
             else if (st->codecpar->codec_id != AV_CODEC_ID_AAC)
-                st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
             if (track->codec_delay > 0) {
                 st->codecpar->initial_padding = av_rescale_q(track->codec_delay,
                                                              (AVRational){1, 1000000000},
@@ -3651,7 +3651,7 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf
             return res;
         if (is_keyframe)
             matroska->skip_to_keyframe = 0;
-        else if (!st->internal->skip_to_keyframe) {
+        else if (!ffstream(st)->skip_to_keyframe) {
             av_log(matroska->ctx, AV_LOG_ERROR, "File is broken, keyframes not correctly marked!\n");
             matroska->skip_to_keyframe = 0;
         }
@@ -3813,6 +3813,7 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index,
     MatroskaDemuxContext *matroska = s->priv_data;
     MatroskaTrack *tracks = NULL;
     AVStream *st = s->streams[stream_index];
+    FFStream *const sti = ffstream(st);
     int i, index;
 
     /* Parse the CUES now since we need the index data to seek. */
@@ -3821,13 +3822,13 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index,
         matroska_parse_cues(matroska);
     }
 
-    if (!st->internal->nb_index_entries)
+    if (!sti->nb_index_entries)
         goto err;
-    timestamp = FFMAX(timestamp, st->internal->index_entries[0].timestamp);
+    timestamp = FFMAX(timestamp, sti->index_entries[0].timestamp);
 
-    if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0 || index == st->internal->nb_index_entries - 1) {
-        matroska_reset_status(matroska, 0, st->internal->index_entries[st->internal->nb_index_entries - 1].pos);
-        while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0 || index == st->internal->nb_index_entries - 1) {
+    if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0 || index == sti->nb_index_entries - 1) {
+        matroska_reset_status(matroska, 0, sti->index_entries[sti->nb_index_entries - 1].pos);
+        while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0 || index == sti->nb_index_entries - 1) {
             matroska_clear_queue(matroska);
             if (matroska_parse_cluster(matroska) < 0)
                 break;
@@ -3835,7 +3836,7 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index,
     }
 
     matroska_clear_queue(matroska);
-    if (index < 0 || (matroska->cues_parsing_deferred < 0 && index == st->internal->nb_index_entries - 1))
+    if (index < 0 || (matroska->cues_parsing_deferred < 0 && index == sti->nb_index_entries - 1))
         goto err;
 
     tracks = matroska->tracks.elem;
@@ -3847,17 +3848,17 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index,
     }
 
     /* We seek to a level 1 element, so set the appropriate status. */
-    matroska_reset_status(matroska, 0, st->internal->index_entries[index].pos);
+    matroska_reset_status(matroska, 0, sti->index_entries[index].pos);
     if (flags & AVSEEK_FLAG_ANY) {
-        st->internal->skip_to_keyframe = 0;
+        sti->skip_to_keyframe = 0;
         matroska->skip_to_timecode = timestamp;
     } else {
-        st->internal->skip_to_keyframe = 1;
-        matroska->skip_to_timecode = st->internal->index_entries[index].timestamp;
+        sti->skip_to_keyframe = 1;
+        matroska->skip_to_timecode = sti->index_entries[index].timestamp;
     }
     matroska->skip_to_keyframe = 1;
     matroska->done             = 0;
-    avpriv_update_cur_dts(s, st, st->internal->index_entries[index].timestamp);
+    avpriv_update_cur_dts(s, st, sti->index_entries[index].timestamp);
     return 0;
 err:
     // slightly hackish but allows proper fallback to
@@ -3865,7 +3866,7 @@ err:
     matroska_reset_status(matroska, 0, -1);
     matroska->resync_pos = -1;
     matroska_clear_queue(matroska);
-    st->internal->skip_to_keyframe =
+    sti->skip_to_keyframe =
     matroska->skip_to_keyframe = 0;
     matroska->done = 0;
     return -1;
@@ -3903,8 +3904,8 @@ static CueDesc get_cue_desc(AVFormatContext *s, int64_t ts, int64_t cues_start)
     MatroskaDemuxContext *matroska = s->priv_data;
     CueDesc cue_desc;
     int i;
-    int nb_index_entries = s->streams[0]->internal->nb_index_entries;
-    AVIndexEntry *index_entries = s->streams[0]->internal->index_entries;
+    int nb_index_entries = ffstream(s->streams[0])->nb_index_entries;
+    AVIndexEntry *index_entries = ffstream(s->streams[0])->index_entries;
     if (ts >= matroska->duration * matroska->time_scale) return (CueDesc) {-1, -1, -1, -1};
     for (i = 1; i < nb_index_entries; i++) {
         if (index_entries[i - 1].timestamp * matroska->time_scale <= ts &&
@@ -3934,11 +3935,11 @@ static int webm_clusters_start_with_keyframe(AVFormatContext *s)
     uint32_t id = matroska->current_id;
     int64_t cluster_pos, before_pos;
     int index, rv = 1;
-    if (s->streams[0]->internal->nb_index_entries <= 0) return 0;
+    if (ffstream(s->streams[0])->nb_index_entries <= 0) return 0;
     // seek to the first cluster using cues.
     index = av_index_search_timestamp(s->streams[0], 0, 0);
     if (index < 0)  return 0;
-    cluster_pos = s->streams[0]->internal->index_entries[index].pos;
+    cluster_pos = ffstream(s->streams[0])->index_entries[index].pos;
     before_pos = avio_tell(s->pb);
     while (1) {
         uint64_t cluster_id, cluster_length;
@@ -4060,11 +4061,10 @@ static int64_t webm_dash_manifest_compute_bandwidth(AVFormatContext *s, int64_t
     MatroskaDemuxContext *matroska = s->priv_data;
     AVStream *st = s->streams[0];
     double bandwidth = 0.0;
-    int i;
 
-    for (i = 0; i < st->internal->nb_index_entries; i++) {
+    for (int i = 0; i < ffstream(st)->nb_index_entries; i++) {
         int64_t prebuffer_ns = 1000000000;
-        int64_t time_ns = st->internal->index_entries[i].timestamp * matroska->time_scale;
+        int64_t time_ns = ffstream(st)->index_entries[i].timestamp * matroska->time_scale;
         double nano_seconds_per_second = 1000000000.0;
         int64_t prebuffered_ns = time_ns + prebuffer_ns;
         double prebuffer_bytes = 0.0;
@@ -4148,6 +4148,7 @@ static int webm_dash_manifest_cues(AVFormatContext *s, int64_t init_range)
     EbmlList *seekhead_list = &matroska->seekhead;
     MatroskaSeekhead *seekhead = seekhead_list->elem;
     AVStream *const st = s->streams[0];
+    FFStream *const sti = ffstream(st);
     AVBPrint bprint;
     char *buf;
     int64_t cues_start = -1, cues_end = -1, before_pos, bandwidth;
@@ -4183,7 +4184,7 @@ static int webm_dash_manifest_cues(AVFormatContext *s, int64_t init_range)
     // parse the cues
     matroska_parse_cues(matroska);
 
-    if (!st->internal->nb_index_entries)
+    if (!sti->nb_index_entries)
         return AVERROR_INVALIDDATA;
 
     // cues start
@@ -4208,8 +4209,8 @@ static int webm_dash_manifest_cues(AVFormatContext *s, int64_t init_range)
     // Store cue point timestamps as a comma separated list
     // for checking subsegment alignment in the muxer.
     av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
-    for (int i = 0; i < st->internal->nb_index_entries; i++)
-        av_bprintf(&bprint, "%" PRId64",", st->internal->index_entries[i].timestamp);
+    for (int i = 0; i < sti->nb_index_entries; i++)
+        av_bprintf(&bprint, "%" PRId64",", sti->index_entries[i].timestamp);
     if (!av_bprint_is_complete(&bprint)) {
         av_bprint_finalize(&bprint, NULL);
         return AVERROR(ENOMEM);
diff --git a/libavformat/mgsts.c b/libavformat/mgsts.c
index 5886d2475a..b5c704829d 100644
--- a/libavformat/mgsts.c
+++ b/libavformat/mgsts.c
@@ -50,7 +50,7 @@ static int read_header(AVFormatContext *s)
     if (!st)
         return AVERROR(ENOMEM);
 
-    st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
     st->start_time = 0;
     st->nb_frames  =
     st->duration   = avio_rb32(pb);
diff --git a/libavformat/mlvdec.c b/libavformat/mlvdec.c
index 6a3aa15a4e..19105cfc23 100644
--- a/libavformat/mlvdec.c
+++ b/libavformat/mlvdec.c
@@ -189,12 +189,12 @@ static int scan_file(AVFormatContext *avctx, AVStream *vst, AVStream *ast, int f
             }
         } else if (vst && type == MKTAG('V', 'I', 'D', 'F') && size >= 4) {
             uint64_t pts = avio_rl32(pb);
-            ff_add_index_entry(&vst->internal->index_entries, &vst->internal->nb_index_entries, &vst->internal->index_entries_allocated_size,
+            ff_add_index_entry(&ffstream(vst)->index_entries, &ffstream(vst)->nb_index_entries, &ffstream(vst)->index_entries_allocated_size,
                                avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
             size -= 4;
         } else if (ast && type == MKTAG('A', 'U', 'D', 'F') && size >= 4) {
             uint64_t pts = avio_rl32(pb);
-            ff_add_index_entry(&ast->internal->index_entries, &ast->internal->nb_index_entries, &ast->internal->index_entries_allocated_size,
+            ff_add_index_entry(&ffstream(ast)->index_entries, &ffstream(ast)->nb_index_entries, &ffstream(ast)->index_entries_allocated_size,
                                avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
             size -= 4;
         } else if (vst && type == MKTAG('W','B','A','L') && size >= 28) {
@@ -372,21 +372,21 @@ static int read_header(AVFormatContext *avctx)
     }
 
     if (vst)
-        vst->duration = vst->internal->nb_index_entries;
+        vst->duration = ffstream(vst)->nb_index_entries;
     if (ast)
-        ast->duration = ast->internal->nb_index_entries;
+        ast->duration = ffstream(ast)->nb_index_entries;
 
-    if ((vst && !vst->internal->nb_index_entries) || (ast && !ast->internal->nb_index_entries)) {
+    if ((vst && !ffstream(vst)->nb_index_entries) || (ast && !ffstream(ast)->nb_index_entries)) {
         av_log(avctx, AV_LOG_ERROR, "no index entries found\n");
         return AVERROR_INVALIDDATA;
     }
 
     if (vst && ast)
-        avio_seek(pb, FFMIN(vst->internal->index_entries[0].pos, ast->internal->index_entries[0].pos), SEEK_SET);
+        avio_seek(pb, FFMIN(ffstream(vst)->index_entries[0].pos, ffstream(ast)->index_entries[0].pos), SEEK_SET);
     else if (vst)
-        avio_seek(pb, vst->internal->index_entries[0].pos, SEEK_SET);
+        avio_seek(pb, ffstream(vst)->index_entries[0].pos, SEEK_SET);
     else if (ast)
-        avio_seek(pb, ast->internal->index_entries[0].pos, SEEK_SET);
+        avio_seek(pb, ffstream(ast)->index_entries[0].pos, SEEK_SET);
 
     return 0;
 }
@@ -412,12 +412,12 @@ static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
         return AVERROR(EIO);
     }
 
-    pb = mlv->pb[st->internal->index_entries[index].size];
+    pb = mlv->pb[ffstream(st)->index_entries[index].size];
     if (!pb) {
         ret = FFERROR_REDO;
         goto next_packet;
     }
-    avio_seek(pb, st->internal->index_entries[index].pos, SEEK_SET);
+    avio_seek(pb, ffstream(st)->index_entries[index].pos, SEEK_SET);
 
     avio_skip(pb, 4); // blockType
     size = avio_rl32(pb);
diff --git a/libavformat/mov.c b/libavformat/mov.c
index da4c5d60f8..367c0a1990 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -1794,8 +1794,8 @@ static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
                     return 0;
                 den *= 2;
             case 1:
-                c->fc->streams[c->fc->nb_streams-1]->internal->display_aspect_ratio.num = num;
-                c->fc->streams[c->fc->nb_streams-1]->internal->display_aspect_ratio.den = den;
+                ffstream(c->fc->streams[c->fc->nb_streams-1])->display_aspect_ratio.num = num;
+                ffstream(c->fc->streams[c->fc->nb_streams-1])->display_aspect_ratio.den = den;
             default:
                 return 0;
             }
@@ -2198,7 +2198,7 @@ static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
             switch (st->codecpar->codec_id) {
             case AV_CODEC_ID_MP2:
             case AV_CODEC_ID_MP3:
-                st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
                 break;
             }
         }
@@ -2436,10 +2436,10 @@ static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
     case AV_CODEC_ID_VC1:
     case AV_CODEC_ID_VP8:
     case AV_CODEC_ID_VP9:
-        st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
         break;
     case AV_CODEC_ID_AV1:
-        st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
         break;
     default:
         break;
@@ -2785,8 +2785,8 @@ static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 
     if (!entries) {
         sc->keyframe_absent = 1;
-        if (!st->internal->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
-            st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+        if (!ffstream(st)->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
         return 0;
     }
     if (sc->keyframes)
@@ -3191,8 +3191,8 @@ static int find_prev_closest_index(AVStream *st,
                                    int64_t* ctts_sample)
 {
     MOVStreamContext *msc = st->priv_data;
-    AVIndexEntry *e_keep = st->internal->index_entries;
-    int nb_keep = st->internal->nb_index_entries;
+    AVIndexEntry *e_keep = ffstream(st)->index_entries;
+    int nb_keep = ffstream(st)->nb_index_entries;
     int64_t i = 0;
     int64_t index_ctts_count;
 
@@ -3205,8 +3205,8 @@ static int find_prev_closest_index(AVStream *st,
         timestamp_pts -= msc->dts_shift;
     }
 
-    st->internal->index_entries = e_old;
-    st->internal->nb_index_entries = nb_old;
+    ffstream(st)->index_entries = e_old;
+    ffstream(st)->nb_index_entries = nb_old;
     *index = av_index_search_timestamp(st, timestamp_pts, flag | AVSEEK_FLAG_BACKWARD);
 
     // Keep going backwards in the index entries until the timestamp is the same.
@@ -3259,14 +3259,14 @@ static int find_prev_closest_index(AVStream *st,
     }
 
     /* restore AVStream state*/
-    st->internal->index_entries = e_keep;
-    st->internal->nb_index_entries = nb_keep;
+    ffstream(st)->index_entries = e_keep;
+    ffstream(st)->nb_index_entries = nb_keep;
     return *index >= 0 ? 0 : -1;
 }
 
 /**
- * Add index entry with the given values, to the end of st->internal->index_entries.
- * Returns the new size st->internal->index_entries if successful, else returns -1.
+ * Add index entry with the given values, to the end of ffstream(st)->index_entries.
+ * Returns the new size ffstream(st)->index_entries if successful, else returns -1.
  *
  * This function is similar to ff_add_index_entry in libavformat/utils.c
  * except that here we are always unconditionally adding an index entry to
@@ -3280,27 +3280,27 @@ static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
 {
     AVIndexEntry *entries, *ie;
     int64_t index = -1;
-    const size_t min_size_needed = (st->internal->nb_index_entries + 1) * sizeof(AVIndexEntry);
+    const size_t min_size_needed = (ffstream(st)->nb_index_entries + 1) * sizeof(AVIndexEntry);
 
     // Double the allocation each time, to lower memory fragmentation.
     // Another difference from ff_add_index_entry function.
     const size_t requested_size =
-        min_size_needed > st->internal->index_entries_allocated_size ?
-        FFMAX(min_size_needed, 2 * st->internal->index_entries_allocated_size) :
+        min_size_needed > ffstream(st)->index_entries_allocated_size ?
+        FFMAX(min_size_needed, 2 * ffstream(st)->index_entries_allocated_size) :
         min_size_needed;
 
-    if (st->internal->nb_index_entries + 1U >= UINT_MAX / sizeof(AVIndexEntry))
+    if (ffstream(st)->nb_index_entries + 1U >= UINT_MAX / sizeof(AVIndexEntry))
         return -1;
 
-    entries = av_fast_realloc(st->internal->index_entries,
-                              &st->internal->index_entries_allocated_size,
+    entries = av_fast_realloc(ffstream(st)->index_entries,
+                              &ffstream(st)->index_entries_allocated_size,
                               requested_size);
     if (!entries)
         return -1;
 
-    st->internal->index_entries= entries;
+    ffstream(st)->index_entries= entries;
 
-    index= st->internal->nb_index_entries++;
+    index= ffstream(st)->nb_index_entries++;
     ie= &entries[index];
 
     ie->pos = pos;
@@ -3319,10 +3319,10 @@ static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_
                                        int64_t* frame_duration_buffer,
                                        int frame_duration_buffer_size) {
     int i = 0;
-    av_assert0(end_index >= 0 && end_index <= st->internal->nb_index_entries);
+    av_assert0(end_index >= 0 && end_index <= ffstream(st)->nb_index_entries);
     for (i = 0; i < frame_duration_buffer_size; i++) {
         end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
-        st->internal->index_entries[end_index - 1 - i].timestamp = end_ts;
+        ffstream(st)->index_entries[end_index - 1 - i].timestamp = end_ts;
     }
 }
 
@@ -3374,14 +3374,14 @@ static void mov_estimate_video_delay(MOVContext *c, AVStream* st)
     if (st->codecpar->video_delay <= 0 && msc->ctts_data &&
         st->codecpar->codec_id == AV_CODEC_ID_H264) {
         st->codecpar->video_delay = 0;
-        for (ind = 0; ind < st->internal->nb_index_entries && ctts_ind < msc->ctts_count; ++ind) {
+        for (ind = 0; ind < ffstream(st)->nb_index_entries && ctts_ind < msc->ctts_count; ++ind) {
             // Point j to the last elem of the buffer and insert the current pts there.
             j = buf_start;
             buf_start = (buf_start + 1);
             if (buf_start == MAX_REORDER_DELAY + 1)
                 buf_start = 0;
 
-            pts_buf[j] = st->internal->index_entries[ind].timestamp + msc->ctts_data[ctts_ind].duration;
+            pts_buf[j] = ffstream(st)->index_entries[ind].timestamp + msc->ctts_data[ctts_ind].duration;
 
             // The timestamps that are already in the sorted buffer, and are greater than the
             // current pts, are exactly the timestamps that need to be buffered to output PTS
@@ -3461,7 +3461,7 @@ static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
 }
 
 /**
- * Fix st->internal->index_entries, so that it contains only the entries (and the entries
+ * Fix ffstream(st)->index_entries, so that it contains only the entries (and the entries
  * which are needed to decode them) that fall in the edit list time ranges.
  * Also fixes the timestamps of the index entries to match the timeline
  * specified the edit lists.
@@ -3469,8 +3469,8 @@ static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
 static void mov_fix_index(MOVContext *mov, AVStream *st)
 {
     MOVStreamContext *msc = st->priv_data;
-    AVIndexEntry *e_old = st->internal->index_entries;
-    int nb_old = st->internal->nb_index_entries;
+    AVIndexEntry *e_old = ffstream(st)->index_entries;
+    int nb_old = ffstream(st)->nb_index_entries;
     const AVIndexEntry *e_old_end = e_old + nb_old;
     const AVIndexEntry *current = NULL;
     MOVStts *ctts_data_old = msc->ctts_data;
@@ -3515,9 +3515,9 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
     current_index_range = msc->index_ranges - 1;
 
     // Clean AVStream from traces of old index
-    st->internal->index_entries = NULL;
-    st->internal->index_entries_allocated_size = 0;
-    st->internal->nb_index_entries = 0;
+    ffstream(st)->index_entries = NULL;
+    ffstream(st)->index_entries_allocated_size = 0;
+    ffstream(st)->nb_index_entries = 0;
 
     // Clean ctts fields of MOVStreamContext
     msc->ctts_data = NULL;
@@ -3562,7 +3562,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
             }
 
             if (first_non_zero_audio_edit > 0)
-                st->internal->skip_samples = msc->start_pad = 0;
+                ffstream(st)->skip_samples = msc->start_pad = 0;
         }
 
         // While reordering frame index according to edit list we must handle properly
@@ -3637,7 +3637,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
                     curr_cts < edit_list_media_time && curr_cts + frame_duration > edit_list_media_time &&
                     first_non_zero_audio_edit > 0) {
                     packet_skip_samples = edit_list_media_time - curr_cts;
-                    st->internal->skip_samples += packet_skip_samples;
+                    ffstream(st)->skip_samples += packet_skip_samples;
 
                     // Shift the index entry timestamp by packet_skip_samples to be correct.
                     edit_list_dts_counter -= packet_skip_samples;
@@ -3646,7 +3646,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
                         // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
                         // discarded packets.
                         if (frame_duration_buffer) {
-                            fix_index_entry_timestamps(st, st->internal->nb_index_entries, edit_list_dts_counter,
+                            fix_index_entry_timestamps(st, ffstream(st)->nb_index_entries, edit_list_dts_counter,
                                                        frame_duration_buffer, num_discarded_begin);
                             av_freep(&frame_duration_buffer);
                         }
@@ -3670,7 +3670,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
                         // Increment skip_samples for the first non-zero audio edit list
                         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
                             first_non_zero_audio_edit > 0 && st->codecpar->codec_id != AV_CODEC_ID_VORBIS) {
-                            st->internal->skip_samples += frame_duration;
+                            ffstream(st)->skip_samples += frame_duration;
                         }
                     }
                 }
@@ -3685,7 +3685,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
                     // Make timestamps strictly monotonically increasing by rewriting timestamps for
                     // discarded packets.
                     if (frame_duration_buffer) {
-                        fix_index_entry_timestamps(st, st->internal->nb_index_entries, edit_list_dts_counter,
+                        fix_index_entry_timestamps(st, ffstream(st)->nb_index_entries, edit_list_dts_counter,
                                                    frame_duration_buffer, num_discarded_begin);
                         av_freep(&frame_duration_buffer);
                     }
@@ -3746,8 +3746,8 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
         if (msc->min_corrected_pts > 0) {
             av_log(mov->fc, AV_LOG_DEBUG, "Offset DTS by %"PRId64" to make first pts zero.\n", msc->min_corrected_pts);
-            for (i = 0; i < st->internal->nb_index_entries; ++i) {
-                st->internal->index_entries[i].timestamp -= msc->min_corrected_pts;
+            for (i = 0; i < ffstream(st)->nb_index_entries; ++i) {
+                ffstream(st)->index_entries[i].timestamp -= msc->min_corrected_pts;
             }
         }
     }
@@ -3756,7 +3756,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
 
     // Update av stream length, if it ends up shorter than the track's media duration
     st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
-    msc->start_pad = st->internal->skip_samples;
+    msc->start_pad = ffstream(st)->skip_samples;
 
     // Free the old index and the old CTTS structures
     av_free(e_old);
@@ -3773,6 +3773,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
 static void mov_build_index(MOVContext *mov, AVStream *st)
 {
     MOVStreamContext *sc = st->priv_data;
+    FFStream *const sti = ffstream(st);
     int64_t current_offset;
     int64_t current_dts = 0;
     unsigned int stts_index = 0;
@@ -3844,17 +3845,17 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
         current_dts -= sc->dts_shift;
         last_dts     = current_dts;
 
-        if (!sc->sample_count || st->internal->nb_index_entries)
+        if (!sc->sample_count || sti->nb_index_entries)
             return;
-        if (sc->sample_count >= UINT_MAX / sizeof(*st->internal->index_entries) - st->internal->nb_index_entries)
+        if (sc->sample_count >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
             return;
-        if (av_reallocp_array(&st->internal->index_entries,
-                              st->internal->nb_index_entries + sc->sample_count,
-                              sizeof(*st->internal->index_entries)) < 0) {
-            st->internal->nb_index_entries = 0;
+        if (av_reallocp_array(&sti->index_entries,
+                              sti->nb_index_entries + sc->sample_count,
+                              sizeof(*sti->index_entries)) < 0) {
+            sti->nb_index_entries = 0;
             return;
         }
-        st->internal->index_entries_allocated_size = (st->internal->nb_index_entries + sc->sample_count) * sizeof(*st->internal->index_entries);
+        sti->index_entries_allocated_size = (sti->nb_index_entries + sc->sample_count) * sizeof(*sti->index_entries);
 
         if (ctts_data_old) {
             // Expand ctts entries such that we have a 1-1 mapping with samples
@@ -3937,7 +3938,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
                         av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
                         return;
                     }
-                    e = &st->internal->index_entries[st->internal->nb_index_entries++];
+                    e = &sti->index_entries[sti->nb_index_entries++];
                     e->pos = current_offset;
                     e->timestamp = current_dts;
                     e->size = sample_size;
@@ -3946,7 +3947,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
                     av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
                             "size %u, distance %u, keyframe %d\n", st->index, current_sample,
                             current_offset, current_dts, sample_size, distance, keyframe);
-                    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->internal->nb_index_entries < 100)
+                    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->nb_index_entries < 100)
                         ff_rfps_add_frame(mov->fc, st, current_dts);
                 }
 
@@ -4018,15 +4019,15 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
         }
 
         av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
-        if (total >= UINT_MAX / sizeof(*st->internal->index_entries) - st->internal->nb_index_entries)
+        if (total >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
             return;
-        if (av_reallocp_array(&st->internal->index_entries,
-                              st->internal->nb_index_entries + total,
-                              sizeof(*st->internal->index_entries)) < 0) {
-            st->internal->nb_index_entries = 0;
+        if (av_reallocp_array(&sti->index_entries,
+                              sti->nb_index_entries + total,
+                              sizeof(*sti->index_entries)) < 0) {
+            sti->nb_index_entries = 0;
             return;
         }
-        st->internal->index_entries_allocated_size = (st->internal->nb_index_entries + total) * sizeof(*st->internal->index_entries);
+        sti->index_entries_allocated_size = (sti->nb_index_entries + total) * sizeof(*sti->index_entries);
 
         // populate index
         for (i = 0; i < sc->chunk_count; i++) {
@@ -4061,7 +4062,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
                     }
                 }
 
-                if (st->internal->nb_index_entries >= total) {
+                if (sti->nb_index_entries >= total) {
                     av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
                     return;
                 }
@@ -4069,7 +4070,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
                     av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
                     return;
                 }
-                e = &st->internal->index_entries[st->internal->nb_index_entries++];
+                e = &sti->index_entries[sti->nb_index_entries++];
                 e->pos = current_offset;
                 e->timestamp = current_dts;
                 e->size = size;
@@ -4092,8 +4093,8 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
     }
 
     // Update start time of the stream.
-    if (st->start_time == AV_NOPTS_VALUE && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->internal->nb_index_entries > 0) {
-        st->start_time = st->internal->index_entries[0].timestamp + sc->dts_shift;
+    if (st->start_time == AV_NOPTS_VALUE && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ffstream(st)->nb_index_entries > 0) {
+        st->start_time = ffstream(st)->index_entries[0].timestamp + sc->dts_shift;
         if (sc->ctts_data) {
             st->start_time += sc->ctts_data[0].duration;
         }
@@ -4328,7 +4329,7 @@ static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
         && sc->stts_count > 3
         && sc->stts_count*10 > st->nb_frames
         && sc->time_scale == st->codecpar->sample_rate) {
-            st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
     }
     /* Do not need those anymore. */
     av_freep(&sc->chunk_offsets);
@@ -4740,6 +4741,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 {
     MOVFragment *frag = &c->fragment;
     AVStream *st = NULL;
+    FFStream *sti;
     MOVStreamContext *sc;
     MOVStts *ctts_data;
     uint64_t offset;
@@ -4762,6 +4764,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
     for (i = 0; i < c->fc->nb_streams; i++) {
         if (c->fc->streams[i]->id == frag->track_id) {
             st = c->fc->streams[i];
+            sti = ffstream(st);
             break;
         }
     }
@@ -4779,7 +4782,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
     // A valid index_entry means the trun for the fragment was read
     // and it's samples are in index_entries at the given position.
     // New index entries will be inserted before the index_entry found.
-    index_entry_pos = st->internal->nb_index_entries;
+    index_entry_pos = sti->nb_index_entries;
     for (i = c->frag_index.current + 1; i < c->frag_index.nb_items; i++) {
         frag_stream_info = get_frag_stream_info(&c->frag_index, i, frag->track_id);
         if (frag_stream_info && frag_stream_info->index_entry >= 0) {
@@ -4788,7 +4791,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
             break;
         }
     }
-    av_assert0(index_entry_pos <= st->internal->nb_index_entries);
+    av_assert0(index_entry_pos <= sti->nb_index_entries);
 
     avio_r8(pb); /* version */
     flags = avio_rb24(pb);
@@ -4839,22 +4842,22 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
     av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
 
     // realloc space for new index entries
-    if((uint64_t)st->internal->nb_index_entries + entries >= UINT_MAX / sizeof(AVIndexEntry)) {
-        entries = UINT_MAX / sizeof(AVIndexEntry) - st->internal->nb_index_entries;
+    if ((uint64_t)sti->nb_index_entries + entries >= UINT_MAX / sizeof(AVIndexEntry)) {
+        entries = UINT_MAX / sizeof(AVIndexEntry) - sti->nb_index_entries;
         av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
     }
     if (entries == 0)
         return 0;
 
-    requested_size = (st->internal->nb_index_entries + entries) * sizeof(AVIndexEntry);
-    new_entries = av_fast_realloc(st->internal->index_entries,
-                                  &st->internal->index_entries_allocated_size,
+    requested_size = (sti->nb_index_entries + entries) * sizeof(AVIndexEntry);
+    new_entries = av_fast_realloc(sti->index_entries,
+                                  &sti->index_entries_allocated_size,
                                   requested_size);
     if (!new_entries)
         return AVERROR(ENOMEM);
-    st->internal->index_entries= new_entries;
+    sti->index_entries= new_entries;
 
-    requested_size = (st->internal->nb_index_entries + entries) * sizeof(*sc->ctts_data);
+    requested_size = (sti->nb_index_entries + entries) * sizeof(*sc->ctts_data);
     old_ctts_allocated_size = sc->ctts_allocated_size;
     ctts_data = av_fast_realloc(sc->ctts_data, &sc->ctts_allocated_size,
                                 requested_size);
@@ -4868,12 +4871,12 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
     memset((uint8_t*)(sc->ctts_data) + old_ctts_allocated_size, 0,
            sc->ctts_allocated_size - old_ctts_allocated_size);
 
-    if (index_entry_pos < st->internal->nb_index_entries) {
+    if (index_entry_pos < sti->nb_index_entries) {
         // Make hole in index_entries and ctts_data for new samples
-        memmove(st->internal->index_entries + index_entry_pos + entries,
-                st->internal->index_entries + index_entry_pos,
-                sizeof(*st->internal->index_entries) *
-                (st->internal->nb_index_entries - index_entry_pos));
+        memmove(sti->index_entries + index_entry_pos + entries,
+                sti->index_entries + index_entry_pos,
+                sizeof(*sti->index_entries) *
+                (sti->nb_index_entries - index_entry_pos));
         memmove(sc->ctts_data + index_entry_pos + entries,
                 sc->ctts_data + index_entry_pos,
                 sizeof(*sc->ctts_data) * (sc->ctts_count - index_entry_pos));
@@ -4882,15 +4885,15 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
         }
     }
 
-    st->internal->nb_index_entries += entries;
-    sc->ctts_count = st->internal->nb_index_entries;
+    sti->nb_index_entries += entries;
+    sc->ctts_count = sti->nb_index_entries;
 
     // Record the index_entry position in frag_index of this fragment
     if (frag_stream_info)
         frag_stream_info->index_entry = index_entry_pos;
 
     if (index_entry_pos > 0)
-        prev_dts = st->internal->index_entries[index_entry_pos-1].timestamp;
+        prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
 
     for (i = 0; i < entries && !pb->eof_reached; i++) {
         unsigned sample_size = frag->size;
@@ -4939,11 +4942,11 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
         if (prev_dts >= dts)
             index_entry_flags |= AVINDEX_DISCARD_FRAME;
 
-        st->internal->index_entries[index_entry_pos].pos = offset;
-        st->internal->index_entries[index_entry_pos].timestamp = dts;
-        st->internal->index_entries[index_entry_pos].size= sample_size;
-        st->internal->index_entries[index_entry_pos].min_distance= distance;
-        st->internal->index_entries[index_entry_pos].flags = index_entry_flags;
+        sti->index_entries[index_entry_pos].pos   = offset;
+        sti->index_entries[index_entry_pos].timestamp = dts;
+        sti->index_entries[index_entry_pos].size  = sample_size;
+        sti->index_entries[index_entry_pos].min_distance = distance;
+        sti->index_entries[index_entry_pos].flags = index_entry_flags;
 
         sc->ctts_data[index_entry_pos].count = 1;
         sc->ctts_data[index_entry_pos].duration = ctts_duration;
@@ -4970,16 +4973,16 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
         // EOF found before reading all entries.  Fix the hole this would
         // leave in index_entries and ctts_data
         int gap = entries - i;
-        memmove(st->internal->index_entries + index_entry_pos,
-                st->internal->index_entries + index_entry_pos + gap,
-                sizeof(*st->internal->index_entries) *
-                (st->internal->nb_index_entries - (index_entry_pos + gap)));
+        memmove(sti->index_entries + index_entry_pos,
+                sti->index_entries + index_entry_pos + gap,
+                sizeof(*sti->index_entries) *
+                (sti->nb_index_entries - (index_entry_pos + gap)));
         memmove(sc->ctts_data + index_entry_pos,
                 sc->ctts_data + index_entry_pos + gap,
                 sizeof(*sc->ctts_data) *
                 (sc->ctts_count - (index_entry_pos + gap)));
 
-        st->internal->nb_index_entries -= gap;
+        sti->nb_index_entries -= gap;
         sc->ctts_count -= gap;
         if (index_entry_pos < sc->current_sample) {
             sc->current_sample -= gap;
@@ -4992,11 +4995,11 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
     // fragment that overlap with AVINDEX_DISCARD_FRAME
     prev_dts = AV_NOPTS_VALUE;
     if (index_entry_pos > 0)
-        prev_dts = st->internal->index_entries[index_entry_pos-1].timestamp;
-    for (i = index_entry_pos; i < st->internal->nb_index_entries; i++) {
-        if (prev_dts < st->internal->index_entries[i].timestamp)
+        prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
+    for (int i = index_entry_pos; i < sti->nb_index_entries; i++) {
+        if (prev_dts < sti->index_entries[i].timestamp)
             break;
-        st->internal->index_entries[i].flags |= AVINDEX_DISCARD_FRAME;
+        sti->index_entries[i].flags |= AVINDEX_DISCARD_FRAME;
     }
 
     // If a hole was created to insert the new index_entries into,
@@ -7149,15 +7152,15 @@ static int mov_probe(const AVProbeData *p)
 static void mov_read_chapters(AVFormatContext *s)
 {
     MOVContext *mov = s->priv_data;
-    AVStream *st;
     MOVStreamContext *sc;
     int64_t cur_pos;
     int i, j;
     int chapter_track;
 
     for (j = 0; j < mov->nb_chapter_tracks; j++) {
+        AVStream *st = NULL;
+        FFStream *sti;
         chapter_track = mov->chapter_tracks[j];
-        st = NULL;
         for (i = 0; i < s->nb_streams; i++)
             if (s->streams[i]->id == chapter_track) {
                 st = s->streams[i];
@@ -7167,15 +7170,16 @@ static void mov_read_chapters(AVFormatContext *s)
             av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
             continue;
         }
+        sti = ffstream(st);
 
         sc = st->priv_data;
         cur_pos = avio_tell(sc->pb);
 
         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
             st->disposition |= AV_DISPOSITION_ATTACHED_PIC | AV_DISPOSITION_TIMED_THUMBNAILS;
-            if (st->internal->nb_index_entries) {
+            if (sti->nb_index_entries) {
                 // Retrieve the first frame, if possible
-                AVIndexEntry *sample = &st->internal->index_entries[0];
+                AVIndexEntry *sample = &sti->index_entries[0];
                 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
                     av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
                     goto finish;
@@ -7188,9 +7192,9 @@ static void mov_read_chapters(AVFormatContext *s)
             st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
             st->codecpar->codec_id = AV_CODEC_ID_BIN_DATA;
             st->discard = AVDISCARD_ALL;
-            for (i = 0; i < st->internal->nb_index_entries; i++) {
-                AVIndexEntry *sample = &st->internal->index_entries[i];
-                int64_t end = i+1 < st->internal->nb_index_entries ? st->internal->index_entries[i+1].timestamp : st->duration;
+            for (int i = 0; i < sti->nb_index_entries; i++) {
+                AVIndexEntry *sample = &sti->index_entries[i];
+                int64_t end = i+1 < sti->nb_index_entries ? sti->index_entries[i+1].timestamp : st->duration;
                 uint8_t *title;
                 uint16_t ch;
                 int len, title_len;
@@ -7263,10 +7267,10 @@ static int mov_read_rtmd_track(AVFormatContext *s, AVStream *st)
     int64_t cur_pos = avio_tell(sc->pb);
     int hh, mm, ss, ff, drop;
 
-    if (!st->internal->nb_index_entries)
+    if (!ffstream(st)->nb_index_entries)
         return -1;
 
-    avio_seek(sc->pb, st->internal->index_entries->pos, SEEK_SET);
+    avio_seek(sc->pb, ffstream(st)->index_entries->pos, SEEK_SET);
     avio_skip(s->pb, 13);
     hh = avio_r8(s->pb);
     mm = avio_r8(s->pb);
@@ -7288,10 +7292,10 @@ static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
     int64_t cur_pos = avio_tell(sc->pb);
     uint32_t value;
 
-    if (!st->internal->nb_index_entries)
+    if (!ffstream(st)->nb_index_entries)
         return -1;
 
-    avio_seek(sc->pb, st->internal->index_entries->pos, SEEK_SET);
+    avio_seek(sc->pb, ffstream(st)->index_entries->pos, SEEK_SET);
     value = avio_rb32(s->pb);
 
     if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
@@ -7613,7 +7617,7 @@ static int mov_read_header(AVFormatContext *s)
         fix_timescale(mov, sc);
         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
             st->codecpar->codec_id   == AV_CODEC_ID_AAC) {
-            st->internal->skip_samples = sc->start_pad;
+            ffstream(st)->skip_samples = sc->start_pad;
         }
         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
             av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
@@ -7632,7 +7636,7 @@ static int mov_read_header(AVFormatContext *s)
             mov->handbrake_version <= 1000000*0 + 1000*10 + 2 &&  // 0.10.2
             st->codecpar->codec_id == AV_CODEC_ID_MP3) {
             av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
-            st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
         }
     }
 
@@ -7750,8 +7754,8 @@ static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *avst = s->streams[i];
         MOVStreamContext *msc = avst->priv_data;
-        if (msc->pb && msc->current_sample < avst->internal->nb_index_entries) {
-            AVIndexEntry *current_sample = &avst->internal->index_entries[msc->current_sample];
+        if (msc->pb && msc->current_sample < ffstream(avst)->nb_index_entries) {
+            AVIndexEntry *current_sample = &ffstream(avst)->index_entries[msc->current_sample];
             int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
             av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
             if (!sample || (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && current_sample->pos < sample->pos) ||
@@ -7933,9 +7937,9 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
                 sc->has_palette = 0;
             }
         }
-        if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !st->internal->need_parsing && pkt->size > 4) {
+        if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !ffstream(st)->need_parsing && pkt->size > 4) {
             if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
-                st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
         }
     }
 
@@ -7954,8 +7958,8 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
             sc->ctts_sample = 0;
         }
     } else {
-        int64_t next_dts = (sc->current_sample < st->internal->nb_index_entries) ?
-            st->internal->index_entries[sc->current_sample].timestamp : st->duration;
+        int64_t next_dts = (sc->current_sample < ffstream(st)->nb_index_entries) ?
+            ffstream(st)->index_entries[sc->current_sample].timestamp : st->duration;
 
         if (next_dts >= pkt->dts)
             pkt->duration = next_dts - pkt->dts;
@@ -8036,7 +8040,7 @@ static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp,
 
     sample = av_index_search_timestamp(st, timestamp, flags);
     av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
-    if (sample < 0 && st->internal->nb_index_entries && timestamp < st->internal->index_entries[0].timestamp)
+    if (sample < 0 && ffstream(st)->nb_index_entries && timestamp < ffstream(st)->index_entries[0].timestamp)
         sample = 0;
     if (sample < 0) /* not sure what to do */
         return AVERROR_INVALIDDATA;
@@ -8077,8 +8081,8 @@ static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp,
 static int64_t mov_get_skip_samples(AVStream *st, int sample)
 {
     MOVStreamContext *sc = st->priv_data;
-    int64_t first_ts = st->internal->index_entries[0].timestamp;
-    int64_t ts = st->internal->index_entries[sample].timestamp;
+    int64_t first_ts = ffstream(st)->index_entries[0].timestamp;
+    int64_t ts = ffstream(st)->index_entries[sample].timestamp;
     int64_t off;
 
     if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
@@ -8107,8 +8111,8 @@ static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti
 
     if (mc->seek_individually) {
         /* adjust seek timestamp to found sample timestamp */
-        int64_t seek_timestamp = st->internal->index_entries[sample].timestamp;
-        st->internal->skip_samples = mov_get_skip_samples(st, sample);
+        int64_t seek_timestamp = ffstream(st)->index_entries[sample].timestamp;
+        ffstream(st)->skip_samples = mov_get_skip_samples(st, sample);
 
         for (i = 0; i < s->nb_streams; i++) {
             int64_t timestamp;
@@ -8120,7 +8124,7 @@ static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti
             timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
             sample = mov_seek_stream(s, st, timestamp, flags);
             if (sample >= 0)
-                st->internal->skip_samples = mov_get_skip_samples(st, sample);
+                ffstream(st)->skip_samples = mov_get_skip_samples(st, sample);
         }
     } else {
         for (i = 0; i < s->nb_streams; i++) {
diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c
index 622b94e8ff..d3c6a3bde8 100644
--- a/libavformat/mp3dec.c
+++ b/libavformat/mp3dec.c
@@ -255,13 +255,13 @@ static void mp3_parse_info_tag(AVFormatContext *s, AVStream *st,
 
         mp3->start_pad = v>>12;
         mp3->  end_pad = v&4095;
-        st->internal->start_skip_samples = mp3->start_pad + 528 + 1;
+        ffstream(st)->start_skip_samples = mp3->start_pad + 528 + 1;
         if (mp3->frames) {
-            st->internal->first_discard_sample = -mp3->end_pad + 528 + 1 + mp3->frames * (int64_t)spf;
-            st->internal->last_discard_sample = mp3->frames * (int64_t)spf;
+            ffstream(st)->first_discard_sample = -mp3->end_pad + 528 + 1 + mp3->frames * (int64_t)spf;
+            ffstream(st)->last_discard_sample = mp3->frames * (int64_t)spf;
         }
         if (!st->start_time)
-            st->start_time = av_rescale_q(st->internal->start_skip_samples,
+            st->start_time = av_rescale_q(ffstream(st)->start_skip_samples,
                                             (AVRational){1, c->sample_rate},
                                             st->time_base);
         av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3->  end_pad);
@@ -374,7 +374,7 @@ static int mp3_read_header(AVFormatContext *s)
 
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id = AV_CODEC_ID_MP3;
-    st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     st->start_time = 0;
 
     // lcm of all mp3 sample rates
@@ -431,8 +431,8 @@ static int mp3_read_header(AVFormatContext *s)
     }
 
     // the seek index is relative to the end of the xing vbr headers
-    for (i = 0; i < st->internal->nb_index_entries; i++)
-        st->internal->index_entries[i].pos += avio_tell(s->pb);
+    for (i = 0; i < ffstream(st)->nb_index_entries; i++)
+        ffstream(st)->index_entries[i].pos += avio_tell(s->pb);
 
     /* the parameters will be extracted from the compressed bitstream */
     return 0;
@@ -567,7 +567,7 @@ static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
         if (ret < 0)
             return ret;
 
-        ie = &st->internal->index_entries[ret];
+        ie = &ffstream(st)->index_entries[ret];
     } else if (fast_seek && st->duration > 0 && filesize > 0) {
         if (!mp3->is_cbr)
             av_log(s, AV_LOG_WARNING, "Using scaling to seek VBR MP3; may be imprecise.\n");
diff --git a/libavformat/mpc.c b/libavformat/mpc.c
index 9eaf7de0bc..b5b2bab33c 100644
--- a/libavformat/mpc.c
+++ b/libavformat/mpc.c
@@ -187,6 +187,7 @@ static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
 static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
 {
     AVStream *st = s->streams[stream_index];
+    FFStream *const sti = ffstream(st);
     MPCContext *c = s->priv_data;
     AVPacket pkt1, *pkt = &pkt1;
     int ret;
@@ -194,8 +195,8 @@ static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
     uint32_t lastframe;
 
     /* if found, seek there */
-    if (index >= 0 && st->internal->index_entries[st->internal->nb_index_entries-1].timestamp >= timestamp - DELAY_FRAMES){
-        c->curframe = st->internal->index_entries[index].pos;
+    if (index >= 0 && sti->index_entries[sti->nb_index_entries-1].timestamp >= timestamp - DELAY_FRAMES) {
+        c->curframe = sti->index_entries[index].pos;
         return 0;
     }
     /* if timestamp is out of bounds, return error */
diff --git a/libavformat/mpc8.c b/libavformat/mpc8.c
index a0e661c2ab..cd0759cbae 100644
--- a/libavformat/mpc8.c
+++ b/libavformat/mpc8.c
@@ -327,9 +327,9 @@ static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestam
     int index = av_index_search_timestamp(st, timestamp, flags);
 
     if(index < 0) return -1;
-    if (avio_seek(s->pb, st->internal->index_entries[index].pos, SEEK_SET) < 0)
+    if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
         return -1;
-    avpriv_update_cur_dts(s, st, st->internal->index_entries[index].timestamp);
+    avpriv_update_cur_dts(s, st, ffstream(st)->index_entries[index].timestamp);
     return 0;
 }
 
diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c
index a7874cdae8..2d84fa7de9 100644
--- a/libavformat/mpeg.c
+++ b/libavformat/mpeg.c
@@ -623,8 +623,8 @@ skip:
         st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
         st->codecpar->sample_rate = 8000;
     }
-    st->internal->request_probe     = request_probe;
-    st->internal->need_parsing      = AVSTREAM_PARSE_FULL;
+    ffstream(st)->request_probe     = request_probe;
+    ffstream(st)->need_parsing      = AVSTREAM_PARSE_FULL;
 
 found:
     if (st->discard >= AVDISCARD_ALL)
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 9662ef1e61..20c8b3d22d 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -881,9 +881,9 @@ static void mpegts_find_stream_type(AVStream *st,
                 st->codecpar->codec_id   != types->codec_id) {
                 st->codecpar->codec_type = types->codec_type;
                 st->codecpar->codec_id   = types->codec_id;
-                st->internal->need_context_update = 1;
+                ffstream(st)->need_context_update = 1;
             }
-            st->internal->request_probe        = 0;
+            ffstream(st)->request_probe        = 0;
             return;
         }
 }
@@ -895,7 +895,7 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
     int old_codec_id   = st->codecpar->codec_id;
     int old_codec_tag  = st->codecpar->codec_tag;
 
-    if (avcodec_is_open(st->internal->avctx)) {
+    if (avcodec_is_open(ffstream(st)->avctx)) {
         av_log(pes->stream, AV_LOG_DEBUG, "cannot set stream info, internal codec is open\n");
         return 0;
     }
@@ -904,7 +904,7 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
     st->priv_data         = pes;
     st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
     st->codecpar->codec_id   = AV_CODEC_ID_NONE;
-    st->internal->need_parsing      = AVSTREAM_PARSE_FULL;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
     pes->st          = st;
     pes->stream_type = stream_type;
 
@@ -916,7 +916,7 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
 
     mpegts_find_stream_type(st, pes->stream_type, ISO_types);
     if (pes->stream_type == 4 || pes->stream_type == 0x0f)
-        st->internal->request_probe = 50;
+        ffstream(st)->request_probe = 50;
     if ((prog_reg_desc == AV_RL32("HDMV") ||
          prog_reg_desc == AV_RL32("HDPR")) &&
         st->codecpar->codec_id == AV_CODEC_ID_NONE) {
@@ -942,7 +942,7 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
             sub_st->priv_data         = sub_pes;
             sub_st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
             sub_st->codecpar->codec_id   = AV_CODEC_ID_AC3;
-            sub_st->internal->need_parsing      = AVSTREAM_PARSE_FULL;
+            ffstream(sub_st)->need_parsing      = AVSTREAM_PARSE_FULL;
             sub_pes->sub_st           = pes->sub_st = sub_st;
         }
     }
@@ -953,19 +953,19 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
         st->codecpar->codec_type = old_codec_type;
     }
     if ((st->codecpar->codec_id == AV_CODEC_ID_NONE ||
-            (st->internal->request_probe > 0 && st->internal->request_probe < AVPROBE_SCORE_STREAM_RETRY / 5)) &&
-        st->internal->probe_packets > 0 &&
+            (ffstream(st)->request_probe > 0 && ffstream(st)->request_probe < AVPROBE_SCORE_STREAM_RETRY / 5)) &&
+        ffstream(st)->probe_packets > 0 &&
         stream_type == STREAM_TYPE_PRIVATE_DATA) {
         st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
         st->codecpar->codec_id   = AV_CODEC_ID_BIN_DATA;
-        st->internal->request_probe = AVPROBE_SCORE_STREAM_RETRY / 5;
+        ffstream(st)->request_probe = AVPROBE_SCORE_STREAM_RETRY / 5;
     }
 
     /* queue a context update if properties changed */
     if (old_codec_type != st->codecpar->codec_type ||
         old_codec_id   != st->codecpar->codec_id   ||
         old_codec_tag  != st->codecpar->codec_tag)
-        st->internal->need_context_update = 1;
+        ffstream(st)->need_context_update = 1;
 
     return 0;
 }
@@ -1199,12 +1199,12 @@ static int mpegts_push_data(MpegTSFilter *filter,
                         code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
                         code != 0x1f8) {                  /* ITU-T Rec. H.222.1 type E stream */
                         pes->state = MPEGTS_PESHEADER;
-                        if (pes->st->codecpar->codec_id == AV_CODEC_ID_NONE && !pes->st->internal->request_probe) {
+                        if (pes->st->codecpar->codec_id == AV_CODEC_ID_NONE && !ffstream(pes->st)->request_probe) {
                             av_log(pes->stream, AV_LOG_TRACE,
                                     "pid=%x stream_type=%x probing\n",
                                     pes->pid,
                                     pes->stream_type);
-                            pes->st->internal->request_probe = 1;
+                            ffstream(pes->st)->request_probe = 1;
                         }
                     } else {
                         pes->pes_header_size = 6;
@@ -1326,8 +1326,8 @@ skip:
                                     int64_t pcr = f->last_pcr / 300;
                                     pcr_found = 1;
                                     if (st) {
-                                        pes->st->internal->pts_wrap_reference = st->internal->pts_wrap_reference;
-                                        pes->st->internal->pts_wrap_behavior = st->internal->pts_wrap_behavior;
+                                        ffstream(pes->st)->pts_wrap_reference = ffstream(st)->pts_wrap_reference;
+                                        ffstream(pes->st)->pts_wrap_behavior = ffstream(st)->pts_wrap_behavior;
                                     }
                                     if (pes->dts == AV_NOPTS_VALUE || pes->dts < pcr) {
                                         pes->pts = pes->dts = pcr;
@@ -1719,13 +1719,13 @@ static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section,
             ff_mp4_read_dec_config_descr(s, st, &pb.pub);
             if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
                 st->codecpar->extradata_size > 0)
-                st->internal->need_parsing = 0;
+                ffstream(st)->need_parsing = 0;
             if (st->codecpar->codec_id == AV_CODEC_ID_H264 &&
                 st->codecpar->extradata_size > 0)
-                st->internal->need_parsing = 0;
+                ffstream(st)->need_parsing = 0;
 
             st->codecpar->codec_type = avcodec_get_type(st->codecpar->codec_id);
-            st->internal->need_context_update = 1;
+            ffstream(st)->need_context_update = 1;
         }
     }
     for (i = 0; i < mp4_descr_count; i++)
@@ -1802,7 +1802,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
 
     av_log(fc, AV_LOG_TRACE, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
 
-    if ((st->codecpar->codec_id == AV_CODEC_ID_NONE || st->internal->request_probe > 0) &&
+    if ((st->codecpar->codec_id == AV_CODEC_ID_NONE || ffstream(st)->request_probe > 0) &&
         stream_type == STREAM_TYPE_PRIVATE_DATA)
         mpegts_find_stream_type(st, desc_tag, DESC_types);
 
@@ -1828,8 +1828,8 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
                 ff_mp4_read_dec_config_descr(fc, st, &pb.pub);
                 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
                     st->codecpar->extradata_size > 0) {
-                    st->internal->need_parsing = 0;
-                    st->internal->need_context_update = 1;
+                    ffstream(st)->need_parsing        = 0;
+                    ffstream(st)->need_context_update = 1;
                 }
                 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4SYSTEMS)
                     mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
@@ -1840,8 +1840,8 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
             break;
         if (mp4_descr_count > 0 &&
             (st->codecpar->codec_id == AV_CODEC_ID_AAC_LATM ||
-             (st->internal->request_probe == 0 && st->codecpar->codec_id == AV_CODEC_ID_NONE) ||
-             st->internal->request_probe > 0) &&
+             (ffstream(st)->request_probe == 0 && st->codecpar->codec_id == AV_CODEC_ID_NONE) ||
+             ffstream(st)->request_probe > 0) &&
             mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
             FFIOContext pb;
             ffio_init_context(&pb, mp4_descr->dec_config_descr,
@@ -1850,9 +1850,9 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
             ff_mp4_read_dec_config_descr(fc, st, &pb.pub);
             if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
                 st->codecpar->extradata_size > 0) {
-                st->internal->request_probe = st->internal->need_parsing = 0;
+                ffstream(st)->request_probe = ffstream(st)->need_parsing = 0;
                 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
-                st->internal->need_context_update = 1;
+                ffstream(st)->need_context_update = 1;
             }
         }
         break;
@@ -1893,7 +1893,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
 
                 language[i * 4 - 1] = 0;
                 av_dict_set(&st->metadata, "language", language, 0);
-                st->internal->need_context_update = 1;
+                ffstream(st)->need_context_update = 1;
             }
         }
         break;
@@ -1957,7 +1957,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
 
                 language[i * 4 - 1] = 0;
                 av_dict_set(&st->metadata, "language", language, 0);
-                st->internal->need_context_update = 1;
+                ffstream(st)->need_context_update = 1;
             }
         }
         break;
@@ -1990,14 +1990,14 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
     case REGISTRATION_DESCRIPTOR:
         st->codecpar->codec_tag = bytestream_get_le32(pp);
         av_log(fc, AV_LOG_TRACE, "reg_desc=%.4s\n", (char *)&st->codecpar->codec_tag);
-        if (st->codecpar->codec_id == AV_CODEC_ID_NONE || st->internal->request_probe > 0) {
+        if (st->codecpar->codec_id == AV_CODEC_ID_NONE || ffstream(st)->request_probe > 0) {
             mpegts_find_stream_type(st, st->codecpar->codec_tag, REGD_types);
             if (st->codecpar->codec_tag == MKTAG('B', 'S', 'S', 'D'))
-                st->internal->request_probe = 50;
+                ffstream(st)->request_probe = 50;
         }
         break;
     case 0x52: /* stream identifier descriptor */
-        st->internal->stream_identifier = 1 + get8(pp, desc_end);
+        ffstream(st)->stream_identifier = 1 + get8(pp, desc_end);
         break;
     case METADATA_DESCRIPTOR:
         if (get16(pp, desc_end) == 0xFFFF)
@@ -2037,8 +2037,8 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
                 } else {
                     avpriv_request_sample(fc, "Opus in MPEG-TS - channel_config_code > 0x8");
                 }
-                st->internal->need_parsing = AVSTREAM_PARSE_FULL;
-                st->internal->need_context_update = 1;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
+                ffstream(st)->need_context_update = 1;
             }
         }
         if (ext_desc_tag == 0x06) { /* supplementary audio descriptor */
@@ -2116,7 +2116,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
             // Listing of data_component_ids is in STD-B10, part 2, Annex J.
             // Component tag limits are documented in TR-B14, fascicle 2,
             // Vol. 3, Section 2, 4.2.8.1
-            int actual_component_tag = st->internal->stream_identifier - 1;
+            int actual_component_tag = ffstream(st)->stream_identifier - 1;
             int picked_profile = FF_PROFILE_UNKNOWN;
             int data_component_id = get16(pp, desc_end);
             if (data_component_id < 0)
@@ -2148,7 +2148,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
             st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
             st->codecpar->codec_id   = AV_CODEC_ID_ARIB_CAPTION;
             st->codecpar->profile    = picked_profile;
-            st->internal->request_probe        = 0;
+            ffstream(st)->request_probe = 0;
         }
         break;
     case 0xb0: /* DOVI video stream descriptor */
diff --git a/libavformat/msf.c b/libavformat/msf.c
index 7d8add387c..98a7ca11c3 100644
--- a/libavformat/msf.c
+++ b/libavformat/msf.c
@@ -82,7 +82,7 @@ static int msf_read_header(AVFormatContext *s)
             AV_WL16(st->codecpar->extradata+8, codec == 4 ? 1 : 0); /* joint stereo (repeat?) */
             AV_WL16(st->codecpar->extradata+10, 1);
             st->codecpar->codec_id = AV_CODEC_ID_ATRAC3;    break;
-    case 7: st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+    case 7: ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
             st->codecpar->codec_id = AV_CODEC_ID_MP3;       break;
     default:
             avpriv_request_sample(s, "Codec %d", codec);
diff --git a/libavformat/mtv.c b/libavformat/mtv.c
index da02965fd8..b6ed43d444 100644
--- a/libavformat/mtv.c
+++ b/libavformat/mtv.c
@@ -185,7 +185,7 @@ static int mtv_read_header(AVFormatContext *s)
     st->codecpar->codec_type      = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id        = AV_CODEC_ID_MP3;
     st->codecpar->bit_rate        = mtv->audio_br;
-    st->internal->need_parsing              = AVSTREAM_PARSE_FULL;
+    ffstream(st)->need_parsing    = AVSTREAM_PARSE_FULL;
 
     // Jump over header
 
diff --git a/libavformat/mux.c b/libavformat/mux.c
index fc3469c287..67c470c5b9 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -302,9 +302,9 @@ static int init_muxer(AVFormatContext *s, AVDictionary **options)
 
         desc = avcodec_descriptor_get(par->codec_id);
         if (desc && desc->props & AV_CODEC_PROP_REORDER)
-            st->internal->reorder = 1;
+            ffstream(st)->reorder = 1;
 
-        st->internal->is_intra_only = ff_is_intra_only(par->codec_id);
+        ffstream(st)->is_intra_only = ff_is_intra_only(par->codec_id);
 
         if (of->codec_tag) {
             if (   par->codec_tag
@@ -384,7 +384,7 @@ static int init_pts(AVFormatContext *s)
     /* init PTS generation */
     for (unsigned i = 0; i < s->nb_streams; i++) {
         AVStream *const st = s->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
         int64_t den = AV_NOPTS_VALUE;
 
         switch (st->codecpar->codec_type) {
@@ -505,7 +505,7 @@ FF_DISABLE_DEPRECATION_WARNINGS
 //FIXME merge with compute_pkt_fields
 static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     int delay = st->codecpar->video_delay;
     int frame_size;
 
@@ -656,7 +656,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
     if (s->avoid_negative_ts > 0) {
         FFFormatContext *const si = ffformatcontext(s);
         AVStream *st = s->streams[pkt->stream_index];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
         int64_t offset = sti->mux_ts_offset;
         int64_t ts = si->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
 
@@ -740,7 +740,7 @@ static int check_packet(AVFormatContext *s, AVPacket *pkt)
 
 static int prepare_input_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
 #if !FF_API_COMPUTE_PKT_FIELDS2
     /* sanitize the timestamps */
     if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
@@ -796,7 +796,7 @@ int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
     FFFormatContext *const si = ffformatcontext(s);
     PacketList **next_point, *this_pktl;
     AVStream *st = s->streams[pkt->stream_index];
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     int chunked  = s->max_chunk_size || s->max_chunk_duration;
 
     this_pktl    = av_malloc(sizeof(PacketList));
@@ -908,11 +908,12 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
     }
 
     for (unsigned i = 0; i < s->nb_streams; i++) {
-        if (s->streams[i]->internal->last_in_packet_buffer) {
+        const AVStream *const st = s->streams[i];
+        if (cffstream(st)->last_in_packet_buffer) {
             ++stream_count;
-        } else if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
-                   s->streams[i]->codecpar->codec_id != AV_CODEC_ID_VP8 &&
-                   s->streams[i]->codecpar->codec_id != AV_CODEC_ID_VP9) {
+        } else if (st->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
+                   st->codecpar->codec_id != AV_CODEC_ID_VP8 &&
+                   st->codecpar->codec_id != AV_CODEC_ID_VP9) {
             ++noninterleaved_count;
         }
     }
@@ -933,7 +934,7 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
 
         for (unsigned i = 0; i < s->nb_streams; i++) {
             int64_t last_dts;
-            const PacketList *last = s->streams[i]->internal->last_in_packet_buffer;
+            const PacketList *last = ffstream(s->streams[i])->last_in_packet_buffer;
 
             if (!last)
                 continue;
@@ -969,7 +970,7 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
             PacketList *pktl = si->packet_buffer;
             AVPacket *const top_pkt = &pktl->pkt;
             AVStream *const st = s->streams[top_pkt->stream_index];
-            AVStreamInternal *const sti = st->internal;
+            FFStream *const sti = ffstream(st);
             int64_t top_dts = av_rescale_q(top_pkt->dts, st->time_base,
                                         AV_TIME_BASE_Q);
 
@@ -992,7 +993,7 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
     if (stream_count && flush) {
         PacketList *pktl = si->packet_buffer;
         AVStream *const st = s->streams[pktl->pkt.stream_index];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
 
         *out = pktl->pkt;
 
@@ -1018,7 +1019,7 @@ int ff_get_muxer_ts_offset(AVFormatContext *s, int stream_index, int64_t *offset
         return AVERROR(EINVAL);
 
     st = s->streams[stream_index];
-    *offset = st->internal->mux_ts_offset;
+    *offset = ffstream(st)->mux_ts_offset;
 
     if (s->output_ts_offset)
         *offset += av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
@@ -1063,11 +1064,11 @@ static int check_bitstream(AVFormatContext *s, AVStream *st, AVPacket *pkt)
         return 1;
 
     if (s->oformat->check_bitstream) {
-        if (!st->internal->bitstream_checked) {
+        if (!ffstream(st)->bitstream_checked) {
             if ((ret = s->oformat->check_bitstream(s, pkt)) < 0)
                 return ret;
             else if (ret == 1)
-                st->internal->bitstream_checked = 1;
+                ffstream(st)->bitstream_checked = 1;
         }
     }
 
@@ -1119,7 +1120,7 @@ static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt,
 
 static int write_packets_from_bsfs(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
 {
-    AVBSFContext *bsfc = st->internal->bsfc;
+    AVBSFContext *bsfc = ffstream(st)->bsfc;
     int ret;
 
     if ((ret = av_bsf_send_packet(bsfc, pkt)) < 0) {
@@ -1165,7 +1166,7 @@ static int write_packets_common(AVFormatContext *s, AVPacket *pkt, int interleav
     if (ret < 0)
         return ret;
 
-    if (st->internal->bsfc) {
+    if (ffstream(st)->bsfc) {
         return write_packets_from_bsfs(s, st, pkt, interleaved);
     } else {
         return write_packet_common(s, st, pkt, interleaved);
@@ -1241,7 +1242,7 @@ int av_write_trailer(AVFormatContext *s)
 
     av_packet_unref(pkt);
     for (unsigned i = 0; i < s->nb_streams; i++) {
-        if (s->streams[i]->internal->bsfc) {
+        if (ffstream(s->streams[i])->bsfc) {
             ret1 = write_packets_from_bsfs(s, s->streams[i], pkt, 1/*interleaved*/);
             if (ret1 < 0)
                 av_packet_unref(pkt);
@@ -1271,7 +1272,7 @@ int av_write_trailer(AVFormatContext *s)
        ret = s->pb ? s->pb->error : 0;
     for (unsigned i = 0; i < s->nb_streams; i++) {
         av_freep(&s->streams[i]->priv_data);
-        av_freep(&s->streams[i]->internal->index_entries);
+        av_freep(&ffstream(s->streams[i])->index_entries);
     }
     if (s->oformat->priv_class)
         av_opt_free(s->priv_data);
diff --git a/libavformat/mvdec.c b/libavformat/mvdec.c
index b1450e08da..bec8e7366e 100644
--- a/libavformat/mvdec.c
+++ b/libavformat/mvdec.c
@@ -440,8 +440,8 @@ static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
     int64_t ret;
     uint64_t pos;
 
-    if (frame < st->internal->nb_index_entries) {
-        index = &st->internal->index_entries[frame];
+    if (frame < ffstream(st)->nb_index_entries) {
+        index = &ffstream(st)->index_entries[frame];
         pos   = avio_tell(pb);
         if (index->pos > pos)
             avio_skip(pb, index->pos - pos);
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index 55f2e5c767..39e9e04094 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -1789,7 +1789,7 @@ static int mxf_compute_ptses_fake_index(MXFContext *mxf, MXFIndexTable *index_ta
      * 6:  5   5
      *
      * We do this by bucket sorting x by x+TemporalOffset[x] into mxf->ptses,
-     * then settings mxf->internal->first_dts = -max(TemporalOffset[x]).
+     * then settings ffstream(mxf)->first_dts = -max(TemporalOffset[x]).
      * The latter makes DTS <= PTS.
      */
     for (i = x = 0; i < index_table->nb_segments; i++) {
@@ -2616,7 +2616,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
                     }
                 }
             }
-            st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
             if (material_track->sequence->origin) {
                 av_dict_set_int(&st->metadata, "material_track_origin", material_track->sequence->origin, 0);
             }
@@ -2624,7 +2624,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
                 av_dict_set_int(&st->metadata, "source_track_origin", source_track->sequence->origin, 0);
             }
             if (descriptor->aspect_ratio.num && descriptor->aspect_ratio.den)
-                st->internal->display_aspect_ratio = descriptor->aspect_ratio;
+                ffstream(st)->display_aspect_ratio = descriptor->aspect_ratio;
             st->codecpar->color_range     = mxf_get_color_range(mxf, descriptor);
             st->codecpar->color_primaries = mxf_get_codec_ul(ff_mxf_color_primaries_uls, &descriptor->color_primaries_ul)->id;
             st->codecpar->color_trc       = mxf_get_codec_ul(ff_mxf_color_trc_uls, &descriptor->color_trc_ul)->id;
@@ -2681,7 +2681,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
                 else if (descriptor->bits_per_sample == 32)
                     st->codecpar->codec_id = AV_CODEC_ID_PCM_S32BE;
             } else if (st->codecpar->codec_id == AV_CODEC_ID_MP2) {
-                st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
             }
             st->codecpar->bits_per_coded_sample = av_get_bits_per_sample(st->codecpar->codec_id);
         } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
@@ -2715,7 +2715,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
         }
         if (st->codecpar->codec_type != AVMEDIA_TYPE_DATA && source_track->wrapping != FrameWrapped) {
             /* TODO: decode timestamps */
-            st->internal->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
         }
     }
 
@@ -3676,7 +3676,7 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
                 if (next_ofs <= 0) {
                     // If we have no way to packetize the data, then return it in chunks...
                     if (klv.next_klv - klv.length == pos && max_data_size > MXF_MAX_CHUNK_SIZE) {
-                        st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
                         avpriv_request_sample(s, "Huge KLV without proper index in non-frame wrapped essence");
                     }
                     size = FFMIN(max_data_size, MXF_MAX_CHUNK_SIZE);
diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 8e57e10487..f37606ed89 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -3104,7 +3104,7 @@ static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket
     int i, stream_count = 0;
 
     for (i = 0; i < s->nb_streams; i++)
-        stream_count += !!s->streams[i]->internal->last_in_packet_buffer;
+        stream_count += !!ffstream(s->streams[i])->last_in_packet_buffer;
 
     if (stream_count && (s->nb_streams == stream_count || flush)) {
         PacketList *pktl = si->packet_buffer;
@@ -3115,8 +3115,8 @@ static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket
                 if (!stream_count || pktl->pkt.stream_index == 0)
                     break;
                 // update last packet in packet buffer
-                if (s->streams[pktl->pkt.stream_index]->internal->last_in_packet_buffer != pktl)
-                    s->streams[pktl->pkt.stream_index]->internal->last_in_packet_buffer = pktl;
+                if (ffstream(s->streams[pktl->pkt.stream_index])->last_in_packet_buffer != pktl)
+                    ffstream(s->streams[pktl->pkt.stream_index])->last_in_packet_buffer = pktl;
                 last = pktl;
                 pktl = pktl->next;
                 stream_count--;
@@ -3141,8 +3141,8 @@ static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket
         *out = pktl->pkt;
         av_log(s, AV_LOG_TRACE, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts);
         si->packet_buffer = pktl->next;
-        if(s->streams[pktl->pkt.stream_index]->internal->last_in_packet_buffer == pktl)
-            s->streams[pktl->pkt.stream_index]->internal->last_in_packet_buffer= NULL;
+        if (ffstream(s->streams[pktl->pkt.stream_index])->last_in_packet_buffer == pktl)
+            ffstream(s->streams[pktl->pkt.stream_index])->last_in_packet_buffer = NULL;
         if (!si->packet_buffer)
             si->packet_buffer_end = NULL;
         av_freep(&pktl);
diff --git a/libavformat/ncdec.c b/libavformat/ncdec.c
index 3fe189e729..b2f4dfe282 100644
--- a/libavformat/ncdec.c
+++ b/libavformat/ncdec.c
@@ -53,7 +53,7 @@ static int nc_read_header(AVFormatContext *s)
 
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id   = AV_CODEC_ID_MPEG4;
-    st->internal->need_parsing      = AVSTREAM_PARSE_FULL;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
 
     avpriv_set_pts_info(st, 64, 1, 100);
 
diff --git a/libavformat/nsvdec.c b/libavformat/nsvdec.c
index ec73cb7f48..97c044a5b2 100644
--- a/libavformat/nsvdec.c
+++ b/libavformat/nsvdec.c
@@ -461,7 +461,7 @@ static int nsv_parse_NSVs_header(AVFormatContext *s)
             st->codecpar->codec_tag = atag;
             st->codecpar->codec_id = ff_codec_get_id(nsv_codec_audio_tags, atag);
 
-            st->internal->need_parsing = AVSTREAM_PARSE_FULL; /* for PCM we will read a chunk later and put correct info */
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL; /* for PCM we will read a chunk later and put correct info */
 
             /* set timebase to common denominator of ms and framerate */
             avpriv_set_pts_info(st, 64, 1, framerate.num*1000);
@@ -609,7 +609,7 @@ null_chunk_retry:
             asize-=4;
             av_log(s, AV_LOG_TRACE, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);
             if (fill_header) {
-                st[NSV_ST_AUDIO]->internal->need_parsing = AVSTREAM_PARSE_NONE; /* we know everything */
+                ffstream(st[NSV_ST_AUDIO])->need_parsing = AVSTREAM_PARSE_NONE; /* we know everything */
                 if (bps != 16) {
                     av_log(s, AV_LOG_TRACE, "NSV AUDIO bit/sample != 16 (%d)!!!\n", bps);
                 }
@@ -676,10 +676,10 @@ static int nsv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
     if(index < 0)
         return -1;
 
-    if (avio_seek(s->pb, st->internal->index_entries[index].pos, SEEK_SET) < 0)
+    if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
         return -1;
 
-    nst->frame_offset = st->internal->index_entries[index].timestamp;
+    nst->frame_offset = ffstream(st)->index_entries[index].timestamp;
     nsv->state = NSV_UNSYNC;
     return 0;
 }
diff --git a/libavformat/nutdec.c b/libavformat/nutdec.c
index 50f8b64d10..1f02d098a1 100644
--- a/libavformat/nutdec.c
+++ b/libavformat/nutdec.c
@@ -1086,7 +1086,7 @@ static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
         stc->skip_until_key_frame = 0;
 
     discard     = s->streams[stream_id]->discard;
-    last_IP_pts = s->streams[stream_id]->internal->last_IP_pts;
+    last_IP_pts = ffstream(s->streams[stream_id])->last_IP_pts;
     if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
         (discard >= AVDISCARD_BIDIR  && last_IP_pts != AV_NOPTS_VALUE &&
          last_IP_pts > pts) ||
@@ -1235,15 +1235,15 @@ static int read_seek(AVFormatContext *s, int stream_index,
         return AVERROR(ENOSYS);
     }
 
-    if (st->internal->index_entries) {
+    if (ffstream(st)->index_entries) {
         int index = av_index_search_timestamp(st, pts, flags);
         if (index < 0)
             index = av_index_search_timestamp(st, pts, flags ^ AVSEEK_FLAG_BACKWARD);
         if (index < 0)
             return -1;
 
-        pos2 = st->internal->index_entries[index].pos;
-        ts   = st->internal->index_entries[index].timestamp;
+        pos2 = ffstream(st)->index_entries[index].pos;
+        ts   = ffstream(st)->index_entries[index].timestamp;
     } else {
         av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pts_cmp,
                      (void **) next_node);
diff --git a/libavformat/nutenc.c b/libavformat/nutenc.c
index 6fb0a810e1..7977980935 100644
--- a/libavformat/nutenc.c
+++ b/libavformat/nutenc.c
@@ -1006,6 +1006,7 @@ static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
         ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
         for (i = 0; i < s->nb_streams; i++) {
             AVStream *st   = s->streams[i];
+            FFStream *const sti = ffstream(st);
             int64_t dts_tb = av_rescale_rnd(pkt->dts,
                 nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
                 nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
@@ -1013,12 +1014,12 @@ static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
             int index = av_index_search_timestamp(st, dts_tb,
                                                   AVSEEK_FLAG_BACKWARD);
             if (index >= 0) {
-                sp_pos = FFMIN(sp_pos, st->internal->index_entries[index].pos);
-                if (!nut->write_index && 2*index > st->internal->nb_index_entries) {
-                    memmove(st->internal->index_entries,
-                            st->internal->index_entries + index,
-                            sizeof(*st->internal->index_entries) * (st->internal->nb_index_entries - index));
-                    st->internal->nb_index_entries -=  index;
+                sp_pos = FFMIN(sp_pos, sti->index_entries[index].pos);
+                if (!nut->write_index && 2*index > sti->nb_index_entries) {
+                    memmove(sti->index_entries,
+                            sti->index_entries + index,
+                            sizeof(*sti->index_entries) * (sti->nb_index_entries - index));
+                    sti->nb_index_entries -=  index;
                 }
             }
         }
diff --git a/libavformat/nuv.c b/libavformat/nuv.c
index 73521ec8e4..5f5cc50302 100644
--- a/libavformat/nuv.c
+++ b/libavformat/nuv.c
@@ -133,7 +133,7 @@ static int get_codec_data(AVFormatContext *s, AVIOContext *pb, AVStream *vst,
                 }
                 ast->codecpar->codec_id = id;
 
-                ast->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                ffstream(ast)->need_parsing = AVSTREAM_PARSE_FULL;
             } else
                 avio_skip(pb, 4 * 4);
 
diff --git a/libavformat/oggparseflac.c b/libavformat/oggparseflac.c
index 15eeca56aa..fa7459c162 100644
--- a/libavformat/oggparseflac.c
+++ b/libavformat/oggparseflac.c
@@ -59,7 +59,7 @@ flac_header (AVFormatContext * s, int idx)
 
         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
         st->codecpar->codec_id = AV_CODEC_ID_FLAC;
-        st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
 
         if ((ret = ff_alloc_extradata(st->codecpar, FLAC_STREAMINFO_SIZE)) < 0)
             return ret;
diff --git a/libavformat/oggparseogm.c b/libavformat/oggparseogm.c
index a8319b9dfa..1cbb6bcddd 100644
--- a/libavformat/oggparseogm.c
+++ b/libavformat/oggparseogm.c
@@ -60,7 +60,7 @@ ogm_header(AVFormatContext *s, int idx)
             st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag);
             st->codecpar->codec_tag = tag;
             if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4)
-                st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
         } else if (bytestream2_peek_byte(&p) == 't') {
             st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
             st->codecpar->codec_id = AV_CODEC_ID_TEXT;
@@ -76,7 +76,7 @@ ogm_header(AVFormatContext *s, int idx)
             st->codecpar->codec_id = ff_codec_get_id(ff_codec_wav_tags, cid);
             // our parser completely breaks AAC in Ogg
             if (st->codecpar->codec_id != AV_CODEC_ID_AAC)
-                st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
         }
 
         size        = bytestream2_get_le32(&p);
@@ -116,7 +116,7 @@ ogm_header(AVFormatContext *s, int idx)
         }
 
         // Update internal avctx with changes to codecpar above.
-        st->internal->need_context_update = 1;
+        ffstream(st)->need_context_update = 1;
     } else if (bytestream2_peek_byte(&p) == 3) {
         bytestream2_skip(&p, 7);
         if (bytestream2_get_bytes_left(&p) > 1)
diff --git a/libavformat/oggparsetheora.c b/libavformat/oggparsetheora.c
index 28684f6ea9..b9184eff7b 100644
--- a/libavformat/oggparsetheora.c
+++ b/libavformat/oggparsetheora.c
@@ -112,7 +112,7 @@ static int theora_header(AVFormatContext *s, int idx)
 
         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
         st->codecpar->codec_id   = AV_CODEC_ID_THEORA;
-        st->internal->need_parsing      = AVSTREAM_PARSE_HEADERS;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
     }
     break;
     case 0x81:
diff --git a/libavformat/oggparsevp8.c b/libavformat/oggparsevp8.c
index 85b3627c9c..676b3a0f8e 100644
--- a/libavformat/oggparsevp8.c
+++ b/libavformat/oggparsevp8.c
@@ -61,7 +61,7 @@ static int vp8_header(AVFormatContext *s, int idx)
         avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
         st->codecpar->codec_id   = AV_CODEC_ID_VP8;
-        st->internal->need_parsing      = AVSTREAM_PARSE_HEADERS;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
         break;
     case 0x02:
         if (p[6] != 0x20)
diff --git a/libavformat/omadec.c b/libavformat/omadec.c
index d2f7408709..f6b7697a43 100644
--- a/libavformat/omadec.c
+++ b/libavformat/omadec.c
@@ -515,7 +515,7 @@ static int oma_read_header(AVFormatContext *s)
         avpriv_set_pts_info(st, 64, 1, samplerate);
         break;
     case OMA_CODECID_MP3:
-        st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
         framesize = 1024;
         break;
     case OMA_CODECID_LPCM:
diff --git a/libavformat/pcm.c b/libavformat/pcm.c
index cfa5b969cb..e91117ce5a 100644
--- a/libavformat/pcm.c
+++ b/libavformat/pcm.c
@@ -79,7 +79,7 @@ int ff_pcm_read_seek(AVFormatContext *s,
     pos *= block_align;
 
     /* recompute exact position */
-    st->internal->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
+    ffstream(st)->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
     if ((ret = avio_seek(s->pb, pos + ffformatcontext(s)->data_offset, SEEK_SET)) < 0)
         return ret;
     return 0;
diff --git a/libavformat/pva.c b/libavformat/pva.c
index aecd049c03..da54b30bfc 100644
--- a/libavformat/pva.c
+++ b/libavformat/pva.c
@@ -62,7 +62,7 @@ static int pva_read_header(AVFormatContext *s) {
         return AVERROR(ENOMEM);
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id   = AV_CODEC_ID_MPEG2VIDEO;
-    st->internal->need_parsing      = AVSTREAM_PARSE_FULL;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
     avpriv_set_pts_info(st, 32, 1, 90000);
     av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
 
@@ -70,7 +70,7 @@ static int pva_read_header(AVFormatContext *s) {
         return AVERROR(ENOMEM);
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id   = AV_CODEC_ID_MP2;
-    st->internal->need_parsing      = AVSTREAM_PARSE_FULL;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
     avpriv_set_pts_info(st, 33, 1, 90000);
     av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
 
diff --git a/libavformat/rawdec.c b/libavformat/rawdec.c
index 06406424c8..7880f0c8a7 100644
--- a/libavformat/rawdec.c
+++ b/libavformat/rawdec.c
@@ -59,7 +59,7 @@ int ff_raw_audio_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id = s->iformat->raw_codec_id;
-    st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     st->start_time = 0;
     /* the parameters will be extracted from the compressed bitstream */
 
@@ -82,9 +82,9 @@ int ff_raw_video_read_header(AVFormatContext *s)
 
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id = s->iformat->raw_codec_id;
-    st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
 
-    st->internal->avctx->framerate = s1->framerate;
+    ffstream(st)->avctx->framerate = s1->framerate;
     avpriv_set_pts_info(st, 64, 1, 1200000);
 
 fail:
diff --git a/libavformat/rdt.c b/libavformat/rdt.c
index a015acb690..2fa3c2d266 100644
--- a/libavformat/rdt.c
+++ b/libavformat/rdt.c
@@ -416,7 +416,7 @@ rdt_parse_sdp_line (AVFormatContext *s, int st_index,
     if (av_strstart(p, "OpaqueData:buffer;", &p)) {
         rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
     } else if (av_strstart(p, "StartTime:integer;", &p))
-        stream->internal->first_dts = atoi(p);
+        ffstream(stream)->first_dts = atoi(p);
     else if (av_strstart(p, "ASMRuleBook:string;", &p)) {
         int n, first = -1;
 
@@ -466,7 +466,7 @@ add_dstream(AVFormatContext *s, AVStream *orig_st)
         return NULL;
     st->id = orig_st->id;
     st->codecpar->codec_type = orig_st->codecpar->codec_type;
-    st->internal->first_dts         = orig_st->internal->first_dts;
+    ffstream(st)->first_dts  = ffstream(orig_st)->first_dts;
 
     return st;
 }
diff --git a/libavformat/rl2.c b/libavformat/rl2.c
index 8b6a7f1827..6a559c7294 100644
--- a/libavformat/rl2.c
+++ b/libavformat/rl2.c
@@ -237,9 +237,10 @@ static int rl2_read_packet(AVFormatContext *s,
 
     /** check if there is a valid video or audio entry that can be used */
     for(i=0; i<s->nb_streams; i++){
-        if(rl2->index_pos[i] < s->streams[i]->internal->nb_index_entries
-              && s->streams[i]->internal->index_entries[ rl2->index_pos[i] ].pos < pos){
-            sample = &s->streams[i]->internal->index_entries[ rl2->index_pos[i] ];
+        const FFStream *const sti = ffstream(s->streams[i]);
+        if (rl2->index_pos[i] < sti->nb_index_entries
+              && sti->index_entries[ rl2->index_pos[i] ].pos < pos) {
+            sample = &sti->index_entries[ rl2->index_pos[i] ];
             pos= sample->pos;
             stream_id= i;
         }
@@ -283,7 +284,7 @@ static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
         return -1;
 
     rl2->index_pos[stream_index] = index;
-    timestamp = st->internal->index_entries[index].timestamp;
+    timestamp = ffstream(st)->index_entries[index].timestamp;
 
     for(i=0; i < s->nb_streams; i++){
         AVStream *st2 = s->streams[i];
diff --git a/libavformat/rmdec.c b/libavformat/rmdec.c
index 2529e8b1a1..29a0ec9bd9 100644
--- a/libavformat/rmdec.c
+++ b/libavformat/rmdec.c
@@ -202,7 +202,7 @@ static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
 
         switch (st->codecpar->codec_id) {
         case AV_CODEC_ID_AC3:
-            st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
             break;
         case AV_CODEC_ID_RA_288:
             st->codecpar->extradata_size= 0;
@@ -211,7 +211,7 @@ static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
             st->codecpar->block_align = coded_framesize;
             break;
         case AV_CODEC_ID_COOK:
-            st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
         case AV_CODEC_ID_ATRAC3:
         case AV_CODEC_ID_SIPR:
             if (read_all) {
@@ -235,7 +235,7 @@ static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
                     return -1;
                 }
                 st->codecpar->block_align = ff_sipr_subpk_size[flavor];
-                st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+                ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
             } else {
                 if(sub_packet_size <= 0){
                     av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
@@ -388,7 +388,7 @@ int ff_rm_read_mdpr_codecdata(AVFormatContext *s, AVIOContext *pb,
         avio_skip(pb, 2); // looks like bits per sample
         avio_skip(pb, 4); // always zero?
         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
-        st->internal->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
         fps = avio_rb32(pb);
 
         if ((ret = rm_read_extradata(s, pb, st->codecpar, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
diff --git a/libavformat/rpl.c b/libavformat/rpl.c
index 296cb2c984..187b03229b 100644
--- a/libavformat/rpl.c
+++ b/libavformat/rpl.c
@@ -321,10 +321,10 @@ static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     stream = s->streams[rpl->chunk_part];
 
-    if (rpl->chunk_number >= stream->internal->nb_index_entries)
+    if (rpl->chunk_number >= ffstream(stream)->nb_index_entries)
         return AVERROR_EOF;
 
-    index_entry = &stream->internal->index_entries[rpl->chunk_number];
+    index_entry = &ffstream(stream)->index_entries[rpl->chunk_number];
 
     if (rpl->frame_in_part == 0) {
         if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
diff --git a/libavformat/rtpdec_asf.c b/libavformat/rtpdec_asf.c
index 7812117228..eb19e85351 100644
--- a/libavformat/rtpdec_asf.c
+++ b/libavformat/rtpdec_asf.c
@@ -166,8 +166,8 @@ static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
                 if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
                     avcodec_parameters_copy(s->streams[stream_index]->codecpar,
                                             rt->asf_ctx->streams[i]->codecpar);
-                    s->streams[stream_index]->internal->need_parsing =
-                        rt->asf_ctx->streams[i]->internal->need_parsing;
+                    ffstream(s->streams[stream_index])->need_parsing =
+                        ffstream(rt->asf_ctx->streams[i])->need_parsing;
                     avpriv_set_pts_info(s->streams[stream_index], 32, 1, 1000);
                 }
            }
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index 3772a7f2a9..435b0be0b8 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -218,7 +218,7 @@ static void init_rtp_handler(const RTPDynamicProtocolHandler *handler,
         par->codec_id          = handler->codec_id;
     rtsp_st->dynamic_handler = handler;
     if (st)
-        st->internal->need_parsing = handler->need_parsing;
+        ffstream(st)->need_parsing = handler->need_parsing;
     if (handler->priv_data_size) {
         rtsp_st->dynamic_protocol_context = av_mallocz(handler->priv_data_size);
         if (!rtsp_st->dynamic_protocol_context)
diff --git a/libavformat/s337m.c b/libavformat/s337m.c
index e1d921c303..6fecfeffae 100644
--- a/libavformat/s337m.c
+++ b/libavformat/s337m.c
@@ -181,7 +181,7 @@ static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
         }
         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
         st->codecpar->codec_id   = codec;
-        st->internal->need_parsing         = AVSTREAM_PARSE_HEADERS;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
     }
 
     return 0;
diff --git a/libavformat/sbgdec.c b/libavformat/sbgdec.c
index 21a918db44..8f0ecf7889 100644
--- a/libavformat/sbgdec.c
+++ b/libavformat/sbgdec.c
@@ -1443,13 +1443,13 @@ static av_cold int sbg_read_header(AVFormatContext *avf)
     st->codecpar->sample_rate    = sbg->sample_rate;
     st->codecpar->frame_size     = sbg->frame_size;
     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
-    st->internal->probe_packets = 0;
+    ffstream(st)->probe_packets = 0;
     st->start_time    = av_rescale(script.start_ts,
                                    sbg->sample_rate, AV_TIME_BASE);
     st->duration      = script.end_ts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
                         av_rescale(script.end_ts - script.start_ts,
                                    sbg->sample_rate, AV_TIME_BASE);
-    st->internal->cur_dts       = st->start_time;
+    ffstream(st)->cur_dts = st->start_time;
     r = encode_intervals(&script, st->codecpar, &inter);
     if (r < 0)
         goto fail;
@@ -1470,7 +1470,7 @@ static int sbg_read_packet(AVFormatContext *avf, AVPacket *packet)
     int64_t ts, end_ts;
     int ret;
 
-    ts = avf->streams[0]->internal->cur_dts;
+    ts = ffstream(avf->streams[0])->cur_dts;
     end_ts = ts + avf->streams[0]->codecpar->frame_size;
     if (avf->streams[0]->duration != AV_NOPTS_VALUE)
         end_ts = FFMIN(avf->streams[0]->start_time + avf->streams[0]->duration,
@@ -1493,7 +1493,7 @@ static int sbg_read_seek2(AVFormatContext *avf, int stream_index,
         return AVERROR(EINVAL);
     if (stream_index < 0)
         ts = av_rescale_q(ts, AV_TIME_BASE_Q, avf->streams[0]->time_base);
-    avf->streams[0]->internal->cur_dts = ts;
+    ffstream(avf->streams[0])->cur_dts = ts;
     return 0;
 }
 
diff --git a/libavformat/sdr2.c b/libavformat/sdr2.c
index 0f506fd074..6799339420 100644
--- a/libavformat/sdr2.c
+++ b/libavformat/sdr2.c
@@ -51,7 +51,7 @@ static int sdr2_read_header(AVFormatContext *s)
     st->codecpar->width      = avio_rl32(s->pb);
     st->codecpar->height     = avio_rl32(s->pb);
     st->codecpar->codec_id   = AV_CODEC_ID_H264;
-    st->internal->need_parsing      = AVSTREAM_PARSE_FULL;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
 
     ast->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
     ast->codecpar->channels    = 1;
diff --git a/libavformat/segafilm.c b/libavformat/segafilm.c
index ce1166a05b..3af7518d03 100644
--- a/libavformat/segafilm.c
+++ b/libavformat/segafilm.c
@@ -183,7 +183,7 @@ static int film_read_header(AVFormatContext *s)
         if (film->audio_type == AV_CODEC_ID_ADPCM_ADX) {
             st->codecpar->bits_per_coded_sample = 18 * 8 / 32;
             st->codecpar->block_align = st->codecpar->channels * 18;
-            st->internal->need_parsing = AVSTREAM_PARSE_FULL;
+            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
         } else {
             st->codecpar->bits_per_coded_sample = film->audio_bits;
             st->codecpar->block_align = st->codecpar->channels *
@@ -315,7 +315,7 @@ static int film_read_seek(AVFormatContext *s, int stream_index, int64_t timestam
     if (ret < 0)
         return ret;
 
-    pos = avio_seek(s->pb, st->internal->index_entries[ret].pos, SEEK_SET);
+    pos = avio_seek(s->pb, ffstream(st)->index_entries[ret].pos, SEEK_SET);
     if (pos < 0)
         return pos;
 
diff --git a/libavformat/segment.c b/libavformat/segment.c
index ed671353d0..73cb05f783 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -991,8 +991,8 @@ static int seg_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
         if (ret == 1) {
             AVStream *st = s->streams[pkt->stream_index];
             AVStream *ost = oc->streams[pkt->stream_index];
-            st->internal->bsfc = ost->internal->bsfc;
-            ost->internal->bsfc = NULL;
+            ffstream( st)->bsfc = ffstream(ost)->bsfc;
+            ffstream(ost)->bsfc = NULL;
         }
         return ret;
     }
diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
index 65ef942367..6cd98b62c6 100644
--- a/libavformat/smoothstreamingenc.c
+++ b/libavformat/smoothstreamingenc.c
@@ -586,11 +586,11 @@ static int ism_write_packet(AVFormatContext *s, AVPacket *pkt)
     int64_t end_dts = (c->nb_fragments + 1) * (int64_t) c->min_frag_duration;
     int ret;
 
-    if (st->internal->first_dts == AV_NOPTS_VALUE)
-        st->internal->first_dts = pkt->dts;
+    if (ffstream(st)->first_dts == AV_NOPTS_VALUE)
+        ffstream(st)->first_dts = pkt->dts;
 
     if ((!c->has_video || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
-        av_compare_ts(pkt->dts - st->internal->first_dts, st->time_base,
+        av_compare_ts(pkt->dts - ffstream(st)->first_dts, st->time_base,
                       end_dts, AV_TIME_BASE_Q) >= 0 &&
         pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {
 
diff --git a/libavformat/swfdec.c b/libavformat/swfdec.c
index 8eda80b4f6..e549e4f50b 100644
--- a/libavformat/swfdec.c
+++ b/libavformat/swfdec.c
@@ -204,7 +204,7 @@ static AVStream *create_new_audio_stream(AVFormatContext *s, int id, int info)
     }
     ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     ast->codecpar->codec_id   = ff_codec_get_id(swf_audio_codec_tags, info>>4 & 15);
-    ast->internal->need_parsing = AVSTREAM_PARSE_FULL;
+    ffstream(ast)->need_parsing = AVSTREAM_PARSE_FULL;
     sample_rate_code = info>>2 & 3;
     sample_size_code = info>>1 & 1;
     if (!sample_size_code && ast->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE)
@@ -293,7 +293,7 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
                 return AVERROR(ENOMEM);
             ast->duration = avio_rl32(pb); // number of samples
             if (((v>>4) & 15) == 2) { // MP3 sound data record
-                ast->internal->skip_samples = avio_rl16(pb);
+                ffstream(ast)->skip_samples = avio_rl16(pb);
                 len -= 2;
             }
             len -= 7;
diff --git a/libavformat/takdec.c b/libavformat/takdec.c
index e3f611883b..839e3cc781 100644
--- a/libavformat/takdec.c
+++ b/libavformat/takdec.c
@@ -64,7 +64,7 @@ static int tak_read_header(AVFormatContext *s)
 
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id   = AV_CODEC_ID_TAK;
-    st->internal->need_parsing         = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
 
     tc->mlast_frame = 0;
     if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
diff --git a/libavformat/tedcaptionsdec.c b/libavformat/tedcaptionsdec.c
index 4bf93e26e2..2e039a0dbb 100644
--- a/libavformat/tedcaptionsdec.c
+++ b/libavformat/tedcaptionsdec.c
@@ -298,10 +298,10 @@ static av_cold int tedcaptions_read_header(AVFormatContext *avf)
     st->codecpar->codec_type     = AVMEDIA_TYPE_SUBTITLE;
     st->codecpar->codec_id       = AV_CODEC_ID_TEXT;
     avpriv_set_pts_info(st, 64, 1, 1000);
-    st->internal->probe_packets = 0;
+    ffstream(st)->probe_packets = 0;
     st->start_time    = 0;
     st->duration      = last->pts + last->duration;
-    st->internal->cur_dts       = 0;
+    ffstream(st)->cur_dts       = 0;
 
     return 0;
 }
diff --git a/libavformat/tta.c b/libavformat/tta.c
index e1f9cf27d8..3bdf60dc8d 100644
--- a/libavformat/tta.c
+++ b/libavformat/tta.c
@@ -157,15 +157,15 @@ static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
     if (c->currentframe >= c->totalframes)
         return AVERROR_EOF;
 
-    if (st->internal->nb_index_entries < c->totalframes) {
+    if (ffstream(st)->nb_index_entries < c->totalframes) {
         av_log(s, AV_LOG_ERROR, "Index entry disappeared\n");
         return AVERROR_INVALIDDATA;
     }
 
-    size = st->internal->index_entries[c->currentframe].size;
+    size = ffstream(st)->index_entries[c->currentframe].size;
 
     ret = av_get_packet(s->pb, pkt, size);
-    pkt->dts = st->internal->index_entries[c->currentframe++].timestamp;
+    pkt->dts = ffstream(st)->index_entries[c->currentframe++].timestamp;
     pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
                                                         c->frame_size;
     return ret;
@@ -178,7 +178,7 @@ static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
     int index = av_index_search_timestamp(st, timestamp, flags);
     if (index < 0)
         return -1;
-    if (avio_seek(s->pb, st->internal->index_entries[index].pos, SEEK_SET) < 0)
+    if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
         return -1;
 
     c->currentframe = index;
diff --git a/libavformat/ty.c b/libavformat/ty.c
index 5bb5d4267b..760dfda54c 100644
--- a/libavformat/ty.c
+++ b/libavformat/ty.c
@@ -308,7 +308,7 @@ static int ty_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id   = AV_CODEC_ID_MPEG2VIDEO;
-    st->internal->need_parsing         = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     avpriv_set_pts_info(st, 64, 1, 90000);
 
     ast = avformat_new_stream(s, NULL);
@@ -318,7 +318,7 @@ static int ty_read_header(AVFormatContext *s)
 
     if (ty->audio_type == TIVO_AUDIO_MPEG) {
         ast->codecpar->codec_id = AV_CODEC_ID_MP2;
-        ast->internal->need_parsing       = AVSTREAM_PARSE_FULL_RAW;
+        ffstream(ast)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     } else {
         ast->codecpar->codec_id = AV_CODEC_ID_AC3;
     }
diff --git a/libavformat/utils.c b/libavformat/utils.c
index ec37a6115e..cedea97593 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -103,7 +103,7 @@ static int is_relative(int64_t ts) {
  */
 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
 {
-    const AVStreamInternal *const sti = st->internal;
+    const FFStream *const sti = cffstream(st);
     if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
         sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
         if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
@@ -118,20 +118,20 @@ static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
 
 int64_t av_stream_get_end_pts(const AVStream *st)
 {
-    if (st->internal->priv_pts) {
-        return st->internal->priv_pts->val;
+    if (cffstream(st)->priv_pts) {
+        return cffstream(st)->priv_pts->val;
     } else
         return AV_NOPTS_VALUE;
 }
 
 struct AVCodecParserContext *av_stream_get_parser(const AVStream *st)
 {
-    return st->internal->parser;
+    return cffstream(st)->parser;
 }
 
 void avpriv_stream_set_need_parsing(AVStream *st, enum AVStreamParseType type)
 {
-    st->internal->need_parsing = type;
+    ffstream(st)->need_parsing = type;
 }
 
 void av_format_inject_global_side_data(AVFormatContext *s)
@@ -139,7 +139,7 @@ void av_format_inject_global_side_data(AVFormatContext *s)
     ffformatcontext(s)->inject_global_side_data = 1;
     for (unsigned i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
-        st->internal->inject_global_side_data = 1;
+        ffstream(st)->inject_global_side_data = 1;
     }
 }
 
@@ -313,7 +313,7 @@ static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
     const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
 
     if (fmt) {
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
         av_log(s, AV_LOG_DEBUG,
                "Probe with size=%d, packets=%d detected %s with score=%d\n",
                pd->buf_size, s->max_probe_packets - sti->probe_packets,
@@ -435,7 +435,7 @@ static int update_stream_avctx(AVFormatContext *s)
     int ret;
     for (unsigned i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
 
         if (!sti->need_context_update)
             continue;
@@ -631,7 +631,7 @@ static void force_codec_ids(AVFormatContext *s, AVStream *st)
 
 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     if (sti->request_probe > 0) {
         AVProbeData *pd = &sti->probe_data;
         int end;
@@ -682,7 +682,7 @@ no_packet:
 
 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     int64_t ref = pkt->dts;
     int pts_wrap_behavior;
     int64_t pts_wrap_reference;
@@ -705,10 +705,10 @@ static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_in
 
     if (!first_program) {
         int default_stream_index = av_find_default_stream_index(s);
-        AVStreamInternal *const default_sti = s->streams[default_stream_index]->internal;
+        FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
         if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
             for (unsigned i = 0; i < s->nb_streams; i++) {
-                AVStreamInternal *const sti = s->streams[i]->internal;
+                FFStream *const sti = ffstream(s->streams[i]);
                 if (av_find_program_from_stream(s, NULL, i))
                     continue;
                 sti->pts_wrap_reference = pts_wrap_reference;
@@ -736,7 +736,7 @@ static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_in
         while (program) {
             if (program->pts_wrap_reference != pts_wrap_reference) {
                 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
-                    AVStreamInternal *const sti = s->streams[program->stream_index[i]]->internal;
+                    FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
                     sti->pts_wrap_reference = pts_wrap_reference;
                     sti->pts_wrap_behavior  = pts_wrap_behavior;
                 }
@@ -768,7 +768,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
 
     for (;;) {
         PacketList *pktl = si->raw_packet_buffer;
-        AVStreamInternal *sti;
+        FFStream *sti;
         const AVPacket *pkt1;
 
         if (pktl) {
@@ -776,7 +776,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
             if (si->raw_packet_buffer_remaining_size <= 0)
                 if ((err = probe_codec(s, st, NULL)) < 0)
                     return err;
-            if (st->internal->request_probe <= 0) {
+            if (ffstream(st)->request_probe <= 0) {
                 avpriv_packet_list_get(&si->raw_packet_buffer,
                                        &si->raw_packet_buffer_end, pkt);
                 si->raw_packet_buffer_remaining_size += pkt->size;
@@ -797,7 +797,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
                 return err;
             for (unsigned i = 0; i < s->nb_streams; i++) {
                 AVStream *const st  = s->streams[i];
-                AVStreamInternal *const sti = st->internal;
+                FFStream *const sti = ffstream(st);
                 if (sti->probe_packets || sti->request_probe > 0)
                     if ((err = probe_codec(s, st, NULL)) < 0)
                         return err;
@@ -828,7 +828,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
                    "Invalid stream index.\n");
 
         st = s->streams[pkt->stream_index];
-        sti = st->internal;
+        sti = ffstream(st);
 
         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
@@ -890,7 +890,7 @@ static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
                                    AVStream *st, AVCodecParserContext *pc,
                                    AVPacket *pkt)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     AVRational codec_framerate = sti->avctx->framerate;
     int frame_size, sample_rate;
 
@@ -955,7 +955,7 @@ int ff_is_intra_only(enum AVCodecID id)
 
 static int has_decode_delay_been_guessed(AVStream *st)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
     if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
         return 1;
@@ -983,7 +983,7 @@ static PacketList *get_next_pkt(AVFormatContext *s, AVStream *st, PacketList *pk
 }
 
 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts) {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
                        st->codecpar->codec_id != AV_CODEC_ID_HEVC;
 
@@ -1032,7 +1032,7 @@ static void update_dts_from_pts(AVFormatContext *s, int stream_index,
                                 PacketList *pkt_buffer)
 {
     AVStream *st       = s->streams[stream_index];
-    int delay          = st->internal->avctx->has_b_frames;
+    int delay = ffstream(st)->avctx->has_b_frames;
 
     int64_t pts_buffer[MAX_REORDER_DELAY+1];
 
@@ -1058,7 +1058,7 @@ static void update_initial_timestamps(AVFormatContext *s, int stream_index,
 {
     FFFormatContext *const si = ffformatcontext(s);
     AVStream *st       = s->streams[stream_index];
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     PacketList *pktl = si->packet_buffer ? si->packet_buffer : si->parse_queue;
     PacketList *pktl_it;
 
@@ -1112,7 +1112,7 @@ static void update_initial_durations(AVFormatContext *s, AVStream *st,
                                      int stream_index, int64_t duration)
 {
     FFFormatContext *const si = ffformatcontext(s);
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     PacketList *pktl = si->packet_buffer ? si->packet_buffer : si->parse_queue;
     int64_t cur_dts    = RELATIVE_TS_BASE;
 
@@ -1171,7 +1171,7 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
                                AVCodecParserContext *pc, AVPacket *pkt,
                                int64_t next_dts, int64_t next_pts)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     int num, den, presentation_delayed, delay;
     int64_t offset;
     AVRational duration;
@@ -1365,7 +1365,7 @@ static int parse_packet(AVFormatContext *s, AVPacket *pkt,
     FFFormatContext *const si = ffformatcontext(s);
     AVPacket *out_pkt = si->parse_pkt;
     AVStream *st = s->streams[stream_index];
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     uint8_t *data = pkt->data;
     int size      = pkt->size;
     int ret = 0, got_output = flush;
@@ -1483,7 +1483,7 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
 
     while (!got_packet && !si->parse_queue) {
         AVStream *st;
-        AVStreamInternal *sti;
+        FFStream *sti;
 
         /* read next packet */
         ret = ff_read_packet(s, pkt);
@@ -1493,7 +1493,8 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
             /* flush the parsers */
             for (unsigned i = 0; i < s->nb_streams; i++) {
                 AVStream *const st  = s->streams[i];
-                if (st->internal->parser && st->internal->need_parsing)
+                FFStream *const sti = ffstream(st);
+                if (sti->parser && sti->need_parsing)
                     parse_packet(s, pkt, st->index, 1);
             }
             /* all remaining packets are now in parse_queue =>
@@ -1502,7 +1503,7 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
         }
         ret = 0;
         st  = s->streams[pkt->stream_index];
-        sti = st->internal;
+        sti = ffstream(st);
 
         st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
 
@@ -1598,7 +1599,7 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
 
     if (ret >= 0) {
         AVStream *st = s->streams[pkt->stream_index];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
         int discard_padding = 0;
         if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
             int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
@@ -1800,7 +1801,7 @@ int av_find_default_stream_index(AVFormatContext *s)
             if (st->codecpar->sample_rate)
                 score += 50;
         }
-        if (st->internal->codec_info_nb_frames)
+        if (cffstream(st)->codec_info_nb_frames)
             score += 12;
 
         if (st->discard != AVDISCARD_ALL)
@@ -1823,7 +1824,7 @@ void ff_read_frame_flush(AVFormatContext *s)
     /* Reset read state for each stream. */
     for (unsigned i = 0; i < s->nb_streams; i++) {
         AVStream *const st  = s->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
 
         if (sti->parser) {
             av_parser_close(sti->parser);
@@ -1854,7 +1855,7 @@ void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timesta
     for (unsigned i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
 
-        st->internal->cur_dts =
+        ffstream(st)->cur_dts =
             av_rescale(timestamp,
                        st->time_base.den * (int64_t) ref_st->time_base.num,
                        st->time_base.num * (int64_t) ref_st->time_base.den);
@@ -1864,7 +1865,7 @@ void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timesta
 void ff_reduce_index(AVFormatContext *s, int stream_index)
 {
     AVStream *st             = s->streams[stream_index];
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     unsigned int max_entries = s->max_index_size / sizeof(AVIndexEntry);
 
     if ((unsigned) sti->nb_index_entries >= max_entries) {
@@ -1937,7 +1938,7 @@ int ff_add_index_entry(AVIndexEntry **index_entries,
 int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
                        int size, int distance, int flags)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     timestamp = wrap_timestamp(st, timestamp);
     return ff_add_index_entry(&sti->index_entries, &sti->nb_index_entries,
                               &sti->index_entries_allocated_size, pos,
@@ -2009,10 +2010,10 @@ void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
 
     for (unsigned ist1 = 0; ist1 < s->nb_streams; ist1++) {
         AVStream *st1 = s->streams[ist1];
-        AVStreamInternal *const sti1 = st1->internal;
+        FFStream *const sti1 = ffstream(st1);
         for (unsigned ist2 = 0; ist2 < s->nb_streams; ist2++) {
             AVStream *st2 = s->streams[ist2];
-            AVStreamInternal *const sti2 = st2->internal;
+            FFStream *const sti2 = ffstream(st2);
 
             if (ist1 == ist2)
                 continue;
@@ -2056,19 +2057,19 @@ void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
 
 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     return ff_index_search_timestamp(sti->index_entries, sti->nb_index_entries,
                                      wanted_timestamp, flags);
 }
 
 int avformat_index_get_entries_count(const AVStream *st)
 {
-    return st->internal->nb_index_entries;
+    return cffstream(st)->nb_index_entries;
 }
 
 const AVIndexEntry *avformat_index_get_entry(AVStream *st, int idx)
 {
-    const AVStreamInternal *const sti = st->internal;
+    const FFStream *const sti = ffstream(st);
     if (idx < 0 || idx >= sti->nb_index_entries)
         return NULL;
 
@@ -2079,7 +2080,7 @@ const AVIndexEntry *avformat_index_get_entry_from_timestamp(AVStream *st,
                                                             int64_t wanted_timestamp,
                                                             int flags)
 {
-    const AVStreamInternal *const sti = st->internal;
+    const FFStream *const sti = ffstream(st);
     int idx = ff_index_search_timestamp(sti->index_entries,
                                         sti->nb_index_entries,
                                         wanted_timestamp, flags);
@@ -2108,7 +2109,7 @@ int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
     int index;
     int64_t ret;
     AVStream *st;
-    AVStreamInternal *sti;
+    FFStream *sti;
 
     if (stream_index < 0)
         return -1;
@@ -2120,7 +2121,7 @@ int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
     pos_limit = -1; // GCC falsely says it may be uninitialized.
 
     st = s->streams[stream_index];
-    sti = st->internal;
+    sti = ffstream(st);
     if (sti->index_entries) {
         AVIndexEntry *e;
 
@@ -2337,7 +2338,7 @@ static int seek_frame_generic(AVFormatContext *s, int stream_index,
                               int64_t timestamp, int flags)
 {
     AVStream *const st  = s->streams[stream_index];
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     int index;
     int64_t ret;
     AVIndexEntry *ie;
@@ -2678,7 +2679,7 @@ static void estimate_timings_from_bit_rate(AVFormatContext *ic)
         int64_t bit_rate = 0;
         for (unsigned i = 0; i < ic->nb_streams; i++) {
             const AVStream *const st  = ic->streams[i];
-            const AVStreamInternal *const sti = st->internal;
+            const FFStream *const sti = cffstream(st);
             if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
                 st->codecpar->bit_rate = sti->avctx->bit_rate;
             if (st->codecpar->bit_rate > 0) {
@@ -2739,7 +2740,7 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
 
     for (unsigned i = 0; i < ic->nb_streams; i++) {
         AVStream *const st  = ic->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
 
         if (st->start_time == AV_NOPTS_VALUE &&
             sti->first_dts == AV_NOPTS_VALUE &&
@@ -2772,7 +2773,7 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
         read_size = 0;
         for (;;) {
             AVStream *st;
-            AVStreamInternal *sti;
+            FFStream *sti;
             if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
                 break;
 
@@ -2783,7 +2784,7 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
                 break;
             read_size += pkt->size;
             st         = ic->streams[pkt->stream_index];
-            sti        = st->internal;
+            sti        = ffstream(st);
             if (pkt->pts != AV_NOPTS_VALUE &&
                 (st->start_time != AV_NOPTS_VALUE ||
                  sti->first_dts != AV_NOPTS_VALUE)) {
@@ -2839,7 +2840,7 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
             switch (st->codecpar->codec_type) {
             case AVMEDIA_TYPE_VIDEO:
             case AVMEDIA_TYPE_AUDIO:
-                if (st->start_time != AV_NOPTS_VALUE || st->internal->first_dts  != AV_NOPTS_VALUE) {
+                if (st->start_time != AV_NOPTS_VALUE || cffstream(st)->first_dts  != AV_NOPTS_VALUE) {
                     av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
                 } else
                     av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
@@ -2852,7 +2853,7 @@ skip_duration_calc:
     avio_seek(ic->pb, old_offset, SEEK_SET);
     for (unsigned i = 0; i < ic->nb_streams; i++) {
         AVStream *const st  = ic->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
 
         sti->cur_dts     = sti->first_dts;
         sti->last_IP_pts = AV_NOPTS_VALUE;
@@ -2925,7 +2926,7 @@ static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
 
 static int has_codec_parameters(AVStream *st, const char **errmsg_ptr)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     AVCodecContext *avctx = sti->avctx;
 
 #define FAIL(errmsg) do {                                         \
@@ -2975,7 +2976,7 @@ static int has_codec_parameters(AVStream *st, const char **errmsg_ptr)
 static int try_decode_frame(AVFormatContext *s, AVStream *st,
                             const AVPacket *avpkt, AVDictionary **options)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     AVCodecContext *avctx = sti->avctx;
     const AVCodec *codec;
     int got_picture = 1, ret = 0;
@@ -3303,7 +3304,7 @@ int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb
 
 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
 {
-    AVStreamInternal *const sti = st->internal;
+    FFStream *const sti = ffstream(st);
     int64_t last = sti->info->last_dts;
 
     if (   ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
@@ -3365,7 +3366,7 @@ void ff_rfps_calculate(AVFormatContext *ic)
 {
     for (unsigned i = 0; i < ic->nb_streams; i++) {
         AVStream *st = ic->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
 
         if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
             continue;
@@ -3445,7 +3446,7 @@ static int extract_extradata_check(AVStream *st)
 
 static int extract_extradata_init(AVStream *st)
 {
-    AVStreamInternal *sti = st->internal;
+    FFStream *sti = ffstream(st);
     const AVBitStreamFilter *f;
     int ret;
 
@@ -3484,7 +3485,7 @@ fail:
 
 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
 {
-    AVStreamInternal *sti = st->internal;
+    FFStream *sti = ffstream(st);
     AVPacket *pkt_ref = si->parse_pkt;
     int ret;
 
@@ -3587,7 +3588,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
         const AVCodec *codec;
         AVDictionary *thread_opt = NULL;
         AVStream *const st  = ic->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
         AVCodecContext *const avctx = sti->avctx;
 
         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
@@ -3651,7 +3652,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
     for (;;) {
         const AVPacket *pkt;
         AVStream *st;
-        AVStreamInternal *sti;
+        FFStream *sti;
         AVCodecContext *avctx;
         int analyzed_all_streams;
         unsigned i;
@@ -3664,7 +3665,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
         /* check if one codec still needs to be handled */
         for (i = 0; i < ic->nb_streams; i++) {
             AVStream *const st  = ic->streams[i];
-            AVStreamInternal *const sti = st->internal;
+            FFStream *const sti = ffstream(st);
             int fps_analyze_framecount = 20;
             int count;
 
@@ -3727,7 +3728,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
             for (unsigned i = 0; i < ic->nb_streams; i++) {
                 AVStream *const st = ic->streams[i];
                 if (!st->r_frame_rate.num &&
-                    st->internal->info->duration_count <= 1 &&
+                    ffstream(st)->info->duration_count <= 1 &&
                     st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
                     strcmp(ic->iformat->name, "image2"))
                     av_log(ic, AV_LOG_WARNING,
@@ -3762,7 +3763,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
         }
 
         st = ic->streams[pkt->stream_index];
-        sti = st->internal;
+        sti = ffstream(st);
         if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
             read_size += pkt->size;
 
@@ -3888,7 +3889,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
     if (eof_reached) {
         for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
             AVStream *const st = ic->streams[stream_index];
-            AVCodecContext *const avctx = st->internal->avctx;
+            AVCodecContext *const avctx = ffstream(st)->avctx;
             if (!has_codec_parameters(st, NULL)) {
                 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
                 if (codec && !avctx->codec) {
@@ -3919,7 +3920,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
             AVStream *const st = ic->streams[i];
 
             /* flush the decoders */
-            if (st->internal->info->found_decoder == 1) {
+            if (ffstream(st)->info->found_decoder == 1) {
                 do {
                     err = try_decode_frame(ic, st, empty_pkt,
                                             (options && i < orig_nb_streams)
@@ -3938,7 +3939,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
 
     for (unsigned i = 0; i < ic->nb_streams; i++) {
         AVStream         *const st  = ic->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
         AVCodecContext *const avctx = sti->avctx;
 
         if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
@@ -4040,7 +4041,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
         ret = -1;
     for (unsigned i = 0; i < ic->nb_streams; i++) {
         AVStream *const st  = ic->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
         const char *errmsg;
 
         /* if no packet was ever seen, update context now for has_codec_parameters */
@@ -4071,7 +4072,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
     /* update the stream parameters from the internal codec contexts */
     for (unsigned i = 0; i < ic->nb_streams; i++) {
         AVStream *const st  = ic->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
 
         if (sti->avctx_inited) {
             ret = avcodec_parameters_from_context(st->codecpar, sti->avctx);
@@ -4088,7 +4089,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
 find_stream_info_err:
     for (unsigned i = 0; i < ic->nb_streams; i++) {
         AVStream *const st  = ic->streams[i];
-        AVStreamInternal *const sti = st->internal;
+        FFStream *const sti = ffstream(st);
         if (sti->info) {
             av_freep(&sti->info->duration_error);
             av_freep(&sti->info);
@@ -4163,7 +4164,7 @@ int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type,
         }
         disposition = !(st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED | AV_DISPOSITION_VISUAL_IMPAIRED))
                       + !! (st->disposition & AV_DISPOSITION_DEFAULT);
-        count = st->internal->codec_info_nb_frames;
+        count = ffstream(st)->codec_info_nb_frames;
         bitrate = par->bit_rate;
         multiframe = FFMIN(5, count);
         if ((best_disposition >  disposition) ||
@@ -4261,7 +4262,7 @@ int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
 static void free_stream(AVStream **pst)
 {
     AVStream *st = *pst;
-    AVStreamInternal *sti;
+    FFStream *const sti = ffstream(st);
 
     if (!st)
         return;
@@ -4273,8 +4274,6 @@ static void free_stream(AVStream **pst)
     if (st->attached_pic.data)
         av_packet_unref(&st->attached_pic);
 
-    sti = st->internal;
-    if (sti) {
         av_parser_close(sti->parser);
         avcodec_free_context(&sti->avctx);
         av_bsf_free(&sti->bsfc);
@@ -4288,8 +4287,6 @@ static void free_stream(AVStream **pst)
             av_freep(&sti->info->duration_error);
         av_freep(&sti->info);
         }
-    }
-    av_freep(&st->internal);
 
     av_dict_free(&st->metadata);
     avcodec_parameters_free(&st->codecpar);
@@ -4379,8 +4376,8 @@ void avformat_close_input(AVFormatContext **ps)
 
 AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
 {
+    FFStream *sti;
     AVStream *st;
-    AVStreamInternal *sti;
     AVStream **streams;
 
     if (s->nb_streams >= s->max_streams) {
@@ -4394,13 +4391,11 @@ AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
         return NULL;
     s->streams = streams;
 
-    st = av_mallocz(sizeof(AVStream));
-    if (!st)
-        return NULL;
 
-    sti = st->internal = av_mallocz(sizeof(*st->internal));
+    sti = av_mallocz(sizeof(*sti));
     if (!sti)
-        goto fail;
+        return NULL;
+    st = &sti->pub;
 
     st->codecpar = avcodec_parameters_alloc();
     if (!st->codecpar)
@@ -4821,7 +4816,7 @@ void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
         return;
     }
     s->time_base     = new_tb;
-    s->internal->avctx->pkt_timebase = new_tb;
+    ffstream(s)->avctx->pkt_timebase = new_tb;
     s->pts_wrap_bits = pts_wrap_bits;
 }
 
@@ -4994,7 +4989,7 @@ AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *strea
 AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
 {
     AVRational fr = st->r_frame_rate;
-    AVCodecContext *const avctx = st->internal->avctx;
+    AVCodecContext *const avctx = ffstream(st)->avctx;
     AVRational codec_fr = avctx->framerate;
     AVRational   avg_fr = st->avg_frame_rate;
 
@@ -5398,7 +5393,7 @@ int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *a
     const AVBitStreamFilter *bsf;
     AVBSFContext *bsfc;
 
-    av_assert0(!st->internal->bsfc);
+    av_assert0(!ffstream(st)->bsfc);
 
     if (!(bsf = av_bsf_get_by_name(name))) {
         av_log(NULL, AV_LOG_ERROR, "Unknown bitstream filter '%s'\n", name);
@@ -5432,7 +5427,7 @@ int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *a
         return ret;
     }
 
-    st->internal->bsfc = bsfc;
+    ffstream(st)->bsfc = bsfc;
 
     av_log(NULL, AV_LOG_VERBOSE,
            "Automatically inserted bitstream filter '%s'; args='%s'\n",
@@ -5539,8 +5534,8 @@ int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
                                                   AVStream *ost, const AVStream *ist,
                                                   enum AVTimebaseSource copy_tb)
 {
-    const AVCodecContext *const dec_ctx = ist->internal->avctx;
-    AVCodecContext       *const enc_ctx = ost->internal->avctx;
+    const AVCodecContext *const dec_ctx = cffstream(ist)->avctx;
+    AVCodecContext       *const enc_ctx =  ffstream(ost)->avctx;
 
     enc_ctx->time_base = ist->time_base;
     /*
@@ -5596,7 +5591,7 @@ int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
 AVRational av_stream_get_codec_timebase(const AVStream *st)
 {
     // See avformat_transfer_internal_stream_timing_info() TODO.
-    return st->internal->avctx->time_base;
+    return cffstream(st)->avctx->time_base;
 }
 
 void ff_format_set_url(AVFormatContext *s, char *url)
diff --git a/libavformat/vocdec.c b/libavformat/vocdec.c
index 0bb2bc4fb1..f235d7635b 100644
--- a/libavformat/vocdec.c
+++ b/libavformat/vocdec.c
@@ -73,6 +73,7 @@ static int voc_read_seek(AVFormatContext *s, int stream_index,
 {
     VocDecContext *voc = s->priv_data;
     AVStream *st;
+    FFStream *sti;
     int index;
 
     if (s->nb_streams < 1) {
@@ -81,16 +82,17 @@ static int voc_read_seek(AVFormatContext *s, int stream_index,
     }
 
     st = s->streams[stream_index];
+    sti = ffstream(st);
     index = av_index_search_timestamp(st, timestamp, flags);
 
-    if (index >= 0 && index < st->internal->nb_index_entries - 1) {
-        AVIndexEntry *e = &st->internal->index_entries[index];
+    if (index >= 0 && index < sti->nb_index_entries - 1) {
+        const AVIndexEntry *const e = &sti->index_entries[index];
         avio_seek(s->pb, e->pos, SEEK_SET);
         voc->pts = e->timestamp;
         voc->remaining_size = e->size;
         return 0;
-    } else if (st->internal->nb_index_entries && st->internal->index_entries[0].timestamp <= timestamp) {
-        AVIndexEntry *e = &st->internal->index_entries[st->internal->nb_index_entries - 1];
+    } else if (sti->nb_index_entries && sti->index_entries[0].timestamp <= timestamp) {
+        const AVIndexEntry *const e = &sti->index_entries[sti->nb_index_entries - 1];
         // prepare context for seek_frame_generic()
         voc->pts = e->timestamp;
         voc->remaining_size = e->size;
diff --git a/libavformat/vqf.c b/libavformat/vqf.c
index 1810fca8b9..76131cdc55 100644
--- a/libavformat/vqf.c
+++ b/libavformat/vqf.c
@@ -277,7 +277,7 @@ static int vqf_read_seek(AVFormatContext *s,
                                                    AV_ROUND_DOWN : AV_ROUND_UP);
     pos *= c->frame_bit_len;
 
-    st->internal->cur_dts = av_rescale(pos, st->time_base.den,
+    ffstream(st)->cur_dts = av_rescale(pos, st->time_base.den,
                              st->codecpar->bit_rate * (int64_t)st->time_base.num);
 
     if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + ffformatcontext(s)->data_offset, SEEK_SET)) < 0)
diff --git a/libavformat/wavdec.c b/libavformat/wavdec.c
index 815d189671..7a7e765353 100644
--- a/libavformat/wavdec.c
+++ b/libavformat/wavdec.c
@@ -166,8 +166,9 @@ static int wav_probe(const AVProbeData *p)
 static void handle_stream_probing(AVStream *st)
 {
     if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE) {
-        st->internal->request_probe = AVPROBE_SCORE_EXTENSION;
-        st->internal->probe_packets = FFMIN(st->internal->probe_packets, 32);
+        FFStream *const sti = ffstream(st);
+        sti->request_probe = AVPROBE_SCORE_EXTENSION;
+        sti->probe_packets = FFMIN(sti->probe_packets, 32);
     }
 }
 
@@ -183,7 +184,7 @@ static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream *st)
         return ret;
     handle_stream_probing(st);
 
-    st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
 
     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
 
@@ -200,7 +201,7 @@ static int wav_parse_xma2_tag(AVFormatContext *s, int64_t size, AVStream *st)
 
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id   = AV_CODEC_ID_XMA2;
-    st->internal->need_parsing         = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
 
     version = avio_r8(pb);
     if (version != 3 && version != 4)
@@ -702,8 +703,8 @@ static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
         int64_t audio_dts, video_dts;
         AVStream *vst = wav->vst;
 smv_retry:
-        audio_dts = (int32_t)st->internal->cur_dts;
-        video_dts = (int32_t)vst->internal->cur_dts;
+        audio_dts = (int32_t)ffstream( st)->cur_dts;
+        video_dts = (int32_t)ffstream(vst)->cur_dts;
 
         if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
             /*We always return a video frame first to get the pixel format first*/
@@ -950,7 +951,7 @@ static int w64_read_header(AVFormatContext *s)
     ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
 
     handle_stream_probing(st);
-    st->internal->need_parsing = AVSTREAM_PARSE_FULL_RAW;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
 
     avio_seek(pb, data_ofs, SEEK_SET);
 
diff --git a/libavformat/wtvdec.c b/libavformat/wtvdec.c
index 191da2718b..22e4c92565 100644
--- a/libavformat/wtvdec.c
+++ b/libavformat/wtvdec.c
@@ -618,7 +618,7 @@ static AVStream * new_stream(AVFormatContext *s, AVStream *st, int sid, int code
         st->priv_data = wst;
     }
     st->codecpar->codec_type = codec_type;
-    st->internal->need_parsing      = AVSTREAM_PARSE_FULL;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
     avpriv_set_pts_info(st, 64, 1, 10000000);
     return st;
 }
diff --git a/libavformat/xvag.c b/libavformat/xvag.c
index 2a85d66ecb..994b710f8d 100644
--- a/libavformat/xvag.c
+++ b/libavformat/xvag.c
@@ -87,7 +87,7 @@ static int xvag_read_header(AVFormatContext *s)
     if (avio_rb16(s->pb) == 0xFFFB) {
         st->codecpar->codec_id    = AV_CODEC_ID_MP3;
         st->codecpar->block_align = 0x1000;
-        st->internal->need_parsing       = AVSTREAM_PARSE_FULL_RAW;
+        ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     }
 
     avio_skip(s->pb, -2);
diff --git a/libavformat/xwma.c b/libavformat/xwma.c
index d7fd05504b..37ab3a1ec0 100644
--- a/libavformat/xwma.c
+++ b/libavformat/xwma.c
@@ -78,7 +78,7 @@ static int xwma_read_header(AVFormatContext *s)
     ret = ff_get_wav_header(s, pb, st->codecpar, size, 0);
     if (ret < 0)
         return ret;
-    st->internal->need_parsing = AVSTREAM_PARSE_NONE;
+    ffstream(st)->need_parsing = AVSTREAM_PARSE_NONE;
 
     /* XWMA encoder only allows a few channel/sample rate/bitrate combinations,
      * but some create identical files with fake bitrate (1ch 22050hz at
