[FFmpeg-devel] [PATCH] lavfi/src_movie: stop using AV_CODEC_FLAG_COPY_OPAQUE

Anton Khirnov anton at khirnov.net
Sun Jul 9 12:34:30 EEST 2023


That feature is overkill for a constant pointer to AVFilterLink which
can be stored in AVCodecContext.opaque (indirectly, because the link is
not allocated yet at the time the codec is opened).

This also avoid leaking non-NULL AVFrame.opaque to callers.
---
 libavfilter/src_movie.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index f0d295d4bb..e50ebc99dc 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -51,6 +51,7 @@
 #include "video.h"
 
 typedef struct MovieStream {
+    AVFilterLink *link;
     AVStream *st;
     AVCodecContext *codec_ctx;
     int64_t discontinuity_threshold;
@@ -162,7 +163,8 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
 static int get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
 {
     int linesize_align[AV_NUM_DATA_POINTERS];
-    AVFilterLink *outlink = frame->opaque;
+    MovieStream *st = avctx->opaque;
+    AVFilterLink *outlink = st->link;
     int w, h, ow, oh, copy = 0;
     AVFrame *new;
 
@@ -202,7 +204,6 @@ static int get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
     av_frame_move_ref(frame, new);
     av_frame_free(&new);
 
-    frame->opaque = outlink;
     frame->width  = ow;
     frame->height = oh;
 
@@ -224,7 +225,7 @@ static int open_stream(AVFilterContext *ctx, MovieStream *st, int dec_threads)
     if (!st->codec_ctx)
         return AVERROR(ENOMEM);
 
-    st->codec_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
+    st->codec_ctx->opaque = st;
     st->codec_ctx->get_buffer2 = get_buffer;
     ret = avcodec_parameters_to_context(st->codec_ctx, st->st->codecpar);
     if (ret < 0)
@@ -469,6 +470,8 @@ static int movie_config_output_props(AVFilterLink *outlink)
         break;
     }
 
+    st->link = outlink;
+
     return 0;
 }
 
@@ -581,7 +584,6 @@ static int activate(AVFilterContext *ctx)
                              movie->out_index[movie->pkt->stream_index];
 
             if (pkt_out_id >= 0) {
-                movie->pkt->opaque = ctx->outputs[pkt_out_id];
                 ret = decode_packet(ctx, pkt_out_id);
             }
             av_packet_unref(movie->pkt);
-- 
2.40.1



More information about the ffmpeg-devel mailing list