[FFmpeg-cvslog] fftools/ffmpeg_enc: do not set output stream codec parameters directly

Anton Khirnov git at videolan.org
Thu Sep 26 19:33:09 EEST 2024


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Tue Sep 10 11:08:38 2024 +0200| [78038d51c5ba7eb449913b06f279d59c5d4d3162] | committer: Anton Khirnov

fftools/ffmpeg_enc: do not set output stream codec parameters directly

Have the muxer code read them out of the encoder context in
of_stream_init() instead.

OutputStream.par_in no longer needs to be public, so can be moved to
MuxStream.

This is a step towards decoupling encoders from muxers.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=78038d51c5ba7eb449913b06f279d59c5d4d3162
---

 fftools/ffmpeg.h          |  6 ------
 fftools/ffmpeg_enc.c      |  7 -------
 fftools/ffmpeg_mux.c      | 13 ++++++++++---
 fftools/ffmpeg_mux.h      |  6 ++++++
 fftools/ffmpeg_mux_init.c | 30 +++++++++++++++++-------------
 5 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 98080213fd..13be7c4872 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -594,12 +594,6 @@ typedef struct OutputStream {
 
     int index;               /* stream index in the output file */
 
-    /**
-     * Codec parameters for packets submitted to the muxer (i.e. before
-     * bitstream filtering, if any).
-     */
-    AVCodecParameters *par_in;
-
     /* input stream that is the source for this output stream;
      * may be NULL for streams with no well-defined source, e.g.
      * attachments or outputs from complex filtergraphs */
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index ba79f6a3fc..796849fdb9 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -360,13 +360,6 @@ int enc_open(void *opaque, const AVFrame *frame)
         av_log(e, AV_LOG_WARNING, "The bitrate parameter is set too low."
                                     " It takes bits/s as argument, not kbits/s\n");
 
-    ret = avcodec_parameters_from_context(ost->par_in, ost->enc_ctx);
-    if (ret < 0) {
-        av_log(e, AV_LOG_FATAL,
-               "Error initializing the output stream codec context.\n");
-        return ret;
-    }
-
     ret = of_stream_init(of, ost, enc_ctx);
     if (ret < 0)
         return ret;
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 71a771052f..ea0bbeed32 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -581,9 +581,9 @@ static int bsf_init(MuxStream *ms)
     int ret;
 
     if (!ctx)
-        return avcodec_parameters_copy(ost->st->codecpar, ost->par_in);
+        return avcodec_parameters_copy(ost->st->codecpar, ms->par_in);
 
-    ret = avcodec_parameters_copy(ctx->par_in, ost->par_in);
+    ret = avcodec_parameters_copy(ctx->par_in, ms->par_in);
     if (ret < 0)
         return ret;
 
@@ -619,6 +619,13 @@ int of_stream_init(OutputFile *of, OutputStream *ost,
         // use upstream time base unless it has been overridden previously
         if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
             ost->st->time_base = av_add_q(enc_ctx->time_base, (AVRational){0, 1});
+
+        ret = avcodec_parameters_from_context(ms->par_in, enc_ctx);
+        if (ret < 0) {
+            av_log(ost, AV_LOG_FATAL,
+                   "Error initializing the output stream codec parameters.\n");
+            return ret;
+        }
     }
 
     /* initialize bitstream filters for the output stream
@@ -813,7 +820,7 @@ static void ost_free(OutputStream **post)
         ost->logfile = NULL;
     }
 
-    avcodec_parameters_free(&ost->par_in);
+    avcodec_parameters_free(&ms->par_in);
 
     av_bsf_free(&ms->bsf_ctx);
     av_packet_free(&ms->bsf_pkt);
diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h
index 22d728a919..5df718faf0 100644
--- a/fftools/ffmpeg_mux.h
+++ b/fftools/ffmpeg_mux.h
@@ -36,6 +36,12 @@
 typedef struct MuxStream {
     OutputStream    ost;
 
+    /**
+     * Codec parameters for packets submitted to the muxer (i.e. before
+     * bitstream filtering, if any).
+     */
+    AVCodecParameters *par_in;
+
     // name used for logging
     char            log_name[32];
 
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index b2351de177..47d745aa65 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -67,8 +67,9 @@ static int check_opt_bitexact(void *ctx, const AVDictionary *opts,
 }
 
 static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
-                          OutputStream *ost, const AVCodec **enc)
+                          MuxStream *ms, const AVCodec **enc)
 {
+    OutputStream     *ost = &ms->ost;
     enum AVMediaType type = ost->type;
     const char *codec_name = NULL;
 
@@ -90,20 +91,20 @@ static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
     }
 
     if (!codec_name) {
-        ost->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->type);
-        *enc = avcodec_find_encoder(ost->par_in->codec_id);
+        ms->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->type);
+        *enc = avcodec_find_encoder(ms->par_in->codec_id);
         if (!*enc) {
             av_log(ost, AV_LOG_FATAL, "Automatic encoder selection failed "
                    "Default encoder for format %s (codec %s) is "
                    "probably disabled. Please choose an encoder manually.\n",
-                    s->oformat->name, avcodec_get_name(ost->par_in->codec_id));
+                    s->oformat->name, avcodec_get_name(ms->par_in->codec_id));
             return AVERROR_ENCODER_NOT_FOUND;
         }
     } else if (strcmp(codec_name, "copy")) {
         int ret = find_codec(ost, codec_name, ost->type, 1, enc);
         if (ret < 0)
             return ret;
-        ost->par_in->codec_id = (*enc)->id;
+        ms->par_in->codec_id = (*enc)->id;
     }
 
     return 0;
@@ -1029,7 +1030,7 @@ static int streamcopy_init(const Muxer *mux, OutputStream *ost, AVDictionary **e
     const InputStream   *ist        = ost->ist;
     const InputFile     *ifile      = ist->file;
 
-    AVCodecParameters   *par        = ost->par_in;
+    AVCodecParameters   *par        = ms->par_in;
     uint32_t             codec_tag  = par->codec_tag;
 
     AVCodecContext      *codec_ctx  = NULL;
@@ -1198,8 +1199,8 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
         }
     }
 
-    ost->par_in = avcodec_parameters_alloc();
-    if (!ost->par_in)
+    ms->par_in = avcodec_parameters_alloc();
+    if (!ms->par_in)
         return AVERROR(ENOMEM);
 
     ms->last_mux_dts = AV_NOPTS_VALUE;
@@ -1207,10 +1208,10 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
     ost->st         = st;
     ost->ist        = ist;
     ost->kf.ref_pts = AV_NOPTS_VALUE;
-    ost->par_in->codec_type  = type;
+    ms->par_in->codec_type   = type;
     st->codecpar->codec_type = type;
 
-    ret = choose_encoder(o, oc, ost, &enc);
+    ret = choose_encoder(o, oc, ms, &enc);
     if (ret < 0) {
         av_log(ost, AV_LOG_FATAL, "Error selecting an encoder\n");
         return ret;
@@ -1447,7 +1448,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
             tag = AV_RL32(buf);
         }
         ost->st->codecpar->codec_tag = tag;
-        ost->par_in->codec_tag = tag;
+        ms->par_in->codec_tag = tag;
         if (ost->enc_ctx)
             ost->enc_ctx->codec_tag = tag;
     }
@@ -1799,6 +1800,7 @@ loop_end:
 
 static int of_add_attachments(Muxer *mux, const OptionsContext *o)
 {
+    MuxStream *ms;
     OutputStream *ost;
     int err;
 
@@ -1866,9 +1868,11 @@ read_fail:
             return err;
         }
 
+        ms = ms_from_ost(ost);
+
         ost->attachment_filename       = attachment_filename;
-        ost->par_in->extradata         = attachment;
-        ost->par_in->extradata_size    = len;
+        ms->par_in->extradata          = attachment;
+        ms->par_in->extradata_size     = len;
 
         p = strrchr(o->attachments[i], '/');
         av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);



More information about the ffmpeg-cvslog mailing list