[FFmpeg-devel] [PATCH 2/4] lavf/flacdec: stop accessing FFStream.avctx

Anton Khirnov anton at khirnov.net
Thu Feb 1 10:29:36 EET 2024


The demuxer opens an internal parser instance in read_timestamp(), which
requires a codec context. There is no need for it to access the FFStream
one which is used for other purposes, it can allocate its own internal
one.
---
 libavformat/flacdec.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c
index ab9ef052f9..bbb205078a 100644
--- a/libavformat/flacdec.c
+++ b/libavformat/flacdec.c
@@ -36,6 +36,8 @@
 typedef struct FLACDecContext {
     FFRawDemuxerContext rawctx;
     int found_seektable;
+
+    AVCodecContext *parser_dec;
 } FLACDecContext;
 
 static void reset_index_position(int64_t metadata_head_size, AVStream *st)
@@ -269,6 +271,7 @@ static int flac_probe(const AVProbeData *p)
 static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
                                              int64_t *ppos, int64_t pos_limit)
 {
+    FLACDecContext *flac = s->priv_data;
     FFFormatContext *const si = ffformatcontext(s);
     AVPacket *const pkt = si->parse_pkt;
     AVStream *st = s->streams[stream_index];
@@ -285,6 +288,16 @@ static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_inde
     }
     parser->flags |= PARSER_FLAG_USE_CODEC_TS;
 
+    if (!flac->parser_dec) {
+        flac->parser_dec = avcodec_alloc_context3(NULL);
+        if (!flac->parser_dec)
+            return AV_NOPTS_VALUE;
+
+        ret = avcodec_parameters_to_context(flac->parser_dec, st->codecpar);
+        if (ret < 0)
+            return ret;
+    }
+
     for (;;){
         uint8_t *data;
         int size;
@@ -298,7 +311,7 @@ static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_inde
                 av_assert1(!pkt->size);
             }
         }
-        av_parser_parse2(parser, ffstream(st)->avctx,
+        av_parser_parse2(parser, flac->parser_dec,
                          &data, &size, pkt->data, pkt->size,
                          pkt->pts, pkt->dts, *ppos);
 
@@ -318,6 +331,15 @@ static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_inde
     return pts;
 }
 
+static int flac_close(AVFormatContext *s)
+{
+    FLACDecContext *flac = s->priv_data;
+
+    avcodec_free_context(&flac->parser_dec);
+
+    return 0;
+}
+
 static int flac_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
     AVStream *const st  = s->streams[0];
     FFStream *const sti = ffstream(st);
@@ -347,6 +369,7 @@ const AVInputFormat ff_flac_demuxer = {
     .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
     .read_probe     = flac_probe,
     .read_header    = flac_read_header,
+    .read_close     = flac_close,
     .read_packet    = ff_raw_read_partial_packet,
     .read_seek      = flac_seek,
     .read_timestamp = flac_read_timestamp,
-- 
2.42.0



More information about the ffmpeg-devel mailing list