[FFmpeg-devel] [PATCH 09/24] ffmpeg_mux: add private muxer context

Anton Khirnov anton at khirnov.net
Mon Dec 13 17:20:27 EET 2021


Move header_written into it, which is not (and should not be) used by
any code outside of ffmpeg_mux.

In the future this context will contain more muxer-private state that
should not be visible to other code.
---
 fftools/ffmpeg.h     |  6 ++++--
 fftools/ffmpeg_mux.c | 26 ++++++++++++++++++++++----
 fftools/ffmpeg_opt.c |  6 ++++++
 3 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index c28acad4f1..279a99cc48 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -573,9 +573,12 @@ typedef struct OutputStream {
     int64_t error[4];
 } OutputStream;
 
+typedef struct Muxer Muxer;
+
 typedef struct OutputFile {
     int index;
 
+    Muxer                *mux;
     const AVOutputFormat *format;
 
     AVFormatContext *ctx;
@@ -586,8 +589,6 @@ typedef struct OutputFile {
     uint64_t limit_filesize; /* filesize limit expressed in bytes */
 
     int shortest;
-
-    int header_written;
 } OutputFile;
 
 extern InputStream **input_streams;
@@ -686,6 +687,7 @@ int hw_device_setup_for_filter(FilterGraph *fg);
 
 int hwaccel_decode_init(AVCodecContext *avctx);
 
+int of_muxer_init(OutputFile *of);
 /* open the muxer when all the streams are initialized */
 int of_check_init(OutputFile *of);
 int of_write_trailer(OutputFile *of);
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 9787fe7f78..cf85db674e 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -32,6 +32,10 @@
 
 #include "ffmpeg.h"
 
+struct Muxer {
+    int header_written;
+};
+
 static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others)
 {
     int i;
@@ -64,7 +68,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
         ost->frame_number++;
     }
 
-    if (!of->header_written) {
+    if (!of->mux->header_written) {
         AVPacket *tmp_pkt;
         /* the muxer is not initialized yet, buffer the packet */
         if (!av_fifo_space(ost->muxing_queue)) {
@@ -195,7 +199,7 @@ static int print_sdp(void)
     AVFormatContext **avc;
 
     for (i = 0; i < nb_output_files; i++) {
-        if (!output_files[i]->header_written)
+        if (!output_files[i]->mux->header_written)
             return 0;
     }
 
@@ -259,7 +263,7 @@ int of_check_init(OutputFile *of)
         return ret;
     }
     //assert_avoptions(of->opts);
-    of->header_written = 1;
+    of->mux->header_written = 1;
 
     av_dump_format(of->ctx, of->index, of->ctx->url, 1);
     nb_output_dumped++;
@@ -296,7 +300,7 @@ int of_write_trailer(OutputFile *of)
 {
     int ret;
 
-    if (!of->header_written) {
+    if (!of->mux->header_written) {
         av_log(NULL, AV_LOG_ERROR,
                "Nothing was written into output file %d (%s), because "
                "at least one of its streams received no packets.\n",
@@ -327,5 +331,19 @@ void of_close(OutputFile **pof)
     avformat_free_context(s);
     av_dict_free(&of->opts);
 
+    av_freep(&of->mux);
+
     av_freep(pof);
 }
+
+int of_muxer_init(OutputFile *of)
+{
+    Muxer *mux = av_mallocz(sizeof(*mux));
+
+    if (!mux)
+        return AVERROR(ENOMEM);
+
+    of->mux  = mux;
+
+    return 0;
+}
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 433102bb49..5b42b450f3 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -2886,6 +2886,12 @@ loop_end:
         exit_program(1);
     }
 
+    err = of_muxer_init(of);
+    if (err < 0) {
+        av_log(NULL, AV_LOG_FATAL, "Error initializing the muxer\n");
+        exit_program(1);
+    }
+
     return 0;
 }
 
-- 
2.33.0



More information about the ffmpeg-devel mailing list