diff mbox series

[FFmpeg-devel,20/22] fftools/ffmpeg_mux: embed OutputStream in a MuxStream

Message ID 20221014101548.3486-7-anton@khirnov.net
State Accepted
Commit 2266e0483470cb73147c36bf5ce044b9eef1e841
Headers show
Series [FFmpeg-devel,01/13] fftools/ffmpeg_mux: do not unref a NULL packet | 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 Oct. 14, 2022, 10:15 a.m. UTC
This is now possible since OutputStream is a child of OutputFile and the
code allocating it can access MuxStream. Avoids the overhead and extra
complexity of allocating two objects instead of one.

Similar to what was previously done for OutputFile/Muxer.
---
 fftools/ffmpeg_mux.c      | 33 +++++++++++++++++----------------
 fftools/ffmpeg_mux.h      |  5 ++---
 fftools/ffmpeg_mux_init.c | 23 +++++++++--------------
 3 files changed, 28 insertions(+), 33 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 032b2ac34c..4ad69a149a 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -45,6 +45,11 @@  static Muxer *mux_from_of(OutputFile *of)
     return (Muxer*)of;
 }
 
+static MuxStream *ms_from_ost(OutputStream *ost)
+{
+    return (MuxStream*)ost;
+}
+
 static int64_t filesize(AVIOContext *pb)
 {
     int64_t ret = -1;
@@ -60,7 +65,7 @@  static int64_t filesize(AVIOContext *pb)
 
 static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
 {
-    MuxStream *ms = &mux->streams[ost->index];
+    MuxStream *ms = ms_from_ost(ost);
     AVFormatContext *s = mux->fc;
     AVStream *st = ost->st;
     int64_t fs;
@@ -251,7 +256,7 @@  finish:
 
 static int queue_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
 {
-    MuxStream *ms = &mux->streams[ost->index];
+    MuxStream *ms = ms_from_ost(ost);
     AVPacket *tmp_pkt = NULL;
     int ret;
 
@@ -409,8 +414,8 @@  static int thread_start(Muxer *mux)
 
     /* flush the muxing queues */
     for (int i = 0; i < fc->nb_streams; i++) {
-        MuxStream     *ms = &mux->streams[i];
         OutputStream *ost = mux->of.streams[i];
+        MuxStream     *ms = ms_from_ost(ost);
         AVPacket *pkt;
 
         /* try to improve muxing time_base (only possible if nothing has been written yet) */
@@ -626,9 +631,11 @@  int of_write_trailer(OutputFile *of)
 static void ost_free(OutputStream **post)
 {
     OutputStream *ost = *post;
+    MuxStream *ms;
 
     if (!ost)
         return;
+    ms = ms_from_ost(ost);
 
     if (ost->logfile) {
         if (fclose(ost->logfile))
@@ -638,6 +645,13 @@  static void ost_free(OutputStream **post)
         ost->logfile = NULL;
     }
 
+    if (ms->muxing_queue) {
+        AVPacket *pkt;
+        while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0)
+            av_packet_free(&pkt);
+        av_fifo_freep2(&ms->muxing_queue);
+    }
+
     av_bsf_free(&ost->bsf_ctx);
 
     av_frame_free(&ost->filtered_frame);
@@ -697,19 +711,6 @@  void of_close(OutputFile **pof)
     sq_free(&of->sq_encode);
     sq_free(&mux->sq_mux);
 
-    for (int i = 0; i < mux->nb_streams; i++) {
-        MuxStream *ms = &mux->streams[i];
-        AVPacket *pkt;
-
-        if (!ms->muxing_queue)
-            continue;
-
-        while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0)
-            av_packet_free(&pkt);
-        av_fifo_freep2(&ms->muxing_queue);
-    }
-    av_freep(&mux->streams);
-
     for (int i = 0; i < of->nb_streams; i++)
         ost_free(&of->streams[i]);
     av_freep(&of->streams);
diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h
index b73b6c628b..45daeb3591 100644
--- a/fftools/ffmpeg_mux.h
+++ b/fftools/ffmpeg_mux.h
@@ -35,6 +35,8 @@ 
 #include "libavutil/thread.h"
 
 typedef struct MuxStream {
+    OutputStream ost;
+
     /* the packets are buffered here until the muxer is ready to be initialized */
     AVFifo *muxing_queue;
 
@@ -57,9 +59,6 @@  typedef struct Muxer {
     pthread_t    thread;
     ThreadQueue *tq;
 
-    MuxStream *streams;
-    int     nb_streams;
-
     AVDictionary *opts;
 
     int thread_queue_size;
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 6909a017d8..d6bae92513 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -168,6 +168,7 @@  static OutputStream *new_output_stream(Muxer *mux, OptionsContext *o,
                                        enum AVMediaType type, int source_index)
 {
     AVFormatContext *oc = mux->fc;
+    MuxStream     *ms;
     OutputStream *ost;
     const AVCodec *enc;
     AVStream *st = avformat_new_stream(oc, NULL);
@@ -183,7 +184,14 @@  static OutputStream *new_output_stream(Muxer *mux, OptionsContext *o,
     if (oc->nb_streams - 1 < o->nb_streamid_map)
         st->id = o->streamid_map[oc->nb_streams - 1];
 
-    ost = ALLOC_ARRAY_ELEM(mux->of.streams, mux->of.nb_streams);
+    ms  = allocate_array_elem(&mux->of.streams, sizeof(MuxStream),
+                              &mux->of.nb_streams);
+    ost = &ms->ost;
+
+    ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
+    if (!ms->muxing_queue)
+        report_and_exit(AVERROR(ENOMEM));
+    ms->last_mux_dts = AV_NOPTS_VALUE;
 
     ost->file_index = nb_output_files - 1;
     ost->index      = idx;
@@ -1900,19 +1908,6 @@  int of_open(OptionsContext *o, const char *filename)
 
     of->url        = filename;
 
-    mux->streams = av_calloc(oc->nb_streams, sizeof(*mux->streams));
-    if (!mux->streams)
-        return AVERROR(ENOMEM);
-    mux->nb_streams = oc->nb_streams;
-
-    for (int i = 0; i < mux->nb_streams; i++) {
-        MuxStream *ms = &mux->streams[i];
-        ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
-        if (!ms->muxing_queue)
-            return AVERROR(ENOMEM);
-        ms->last_mux_dts = AV_NOPTS_VALUE;
-    }
-
     /* write the header for files with no streams */
     if (of->format->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) {
         int ret = mux_check_init(mux);