diff mbox series

[FFmpeg-devel,08/13] fftools/ffmpeg_mux: add muxing thread private data

Message ID 20231123191524.11296-10-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/13] lavfi/buffersink: avoid leaking peeked_frame on uninit | expand

Checks

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

Commit Message

Anton Khirnov Nov. 23, 2023, 7:15 p.m. UTC
To be used for data that never needs to be visible outside of the muxer
thread. Start by moving the muxed AVPacket in there.
---
 fftools/ffmpeg_mux.c | 44 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 30c033036d..82352b7981 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -39,6 +39,10 @@ 
 #include "libavformat/avformat.h"
 #include "libavformat/avio.h"
 
+typedef struct MuxThreadContext {
+    AVPacket *pkt;
+} MuxThreadContext;
+
 int want_sdp = 1;
 
 static Muxer *mux_from_of(OutputFile *of)
@@ -210,18 +214,40 @@  static void thread_set_name(OutputFile *of)
     ff_thread_setname(name);
 }
 
+static void mux_thread_uninit(MuxThreadContext *mt)
+{
+    av_packet_free(&mt->pkt);
+
+    memset(mt, 0, sizeof(*mt));
+}
+
+static int mux_thread_init(MuxThreadContext *mt)
+{
+    memset(mt, 0, sizeof(*mt));
+
+    mt->pkt = av_packet_alloc();
+    if (!mt->pkt)
+        goto fail;
+
+    return 0;
+
+fail:
+    mux_thread_uninit(mt);
+    return AVERROR(ENOMEM);
+}
+
 static void *muxer_thread(void *arg)
 {
     Muxer     *mux = arg;
     OutputFile *of = &mux->of;
-    AVPacket  *pkt = NULL;
+
+    MuxThreadContext mt;
+
     int        ret = 0;
 
-    pkt = av_packet_alloc();
-    if (!pkt) {
-        ret = AVERROR(ENOMEM);
+    ret = mux_thread_init(&mt);
+    if (ret < 0)
         goto finish;
-    }
 
     thread_set_name(of);
 
@@ -229,7 +255,7 @@  static void *muxer_thread(void *arg)
         OutputStream *ost;
         int stream_idx, stream_eof = 0;
 
-        ret = tq_receive(mux->tq, &stream_idx, pkt);
+        ret = tq_receive(mux->tq, &stream_idx, mt.pkt);
         if (stream_idx < 0) {
             av_log(mux, AV_LOG_VERBOSE, "All streams finished\n");
             ret = 0;
@@ -237,8 +263,8 @@  static void *muxer_thread(void *arg)
         }
 
         ost = of->streams[stream_idx];
-        ret = sync_queue_process(mux, ost, ret < 0 ? NULL : pkt, &stream_eof);
-        av_packet_unref(pkt);
+        ret = sync_queue_process(mux, ost, ret < 0 ? NULL : mt.pkt, &stream_eof);
+        av_packet_unref(mt.pkt);
         if (ret == AVERROR_EOF) {
             if (stream_eof) {
                 tq_receive_finish(mux->tq, stream_idx);
@@ -254,7 +280,7 @@  static void *muxer_thread(void *arg)
     }
 
 finish:
-    av_packet_free(&pkt);
+    mux_thread_uninit(&mt);
 
     for (unsigned int i = 0; i < mux->fc->nb_streams; i++)
         tq_receive_finish(mux->tq, i);