[FFmpeg-cvslog] fftools/ffmpeg: add muxer-input codec parameters to OutputStream

Anton Khirnov git at videolan.org
Mon Apr 17 13:07:32 EEST 2023


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Thu Apr 13 14:22:28 2023 +0200| [a34f483291e5e635e55e0d326c23bb8c352852c3] | committer: Anton Khirnov

fftools/ffmpeg: add muxer-input codec parameters to OutputStream

It stores codec parameters of the stream submitted to the muxer, which
may be different from the codec parameters in AVStream due to bitstream
filtering.

This avoids the confusing back and forth synchronisation between the
encoder, bitstream filters, and the muxer, now information flows only in
one direction. It also reduces the need for non-muxing code to access
AVStream.

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

 fftools/ffmpeg.h          |  6 ++++++
 fftools/ffmpeg_enc.c      |  2 +-
 fftools/ffmpeg_mux.c      |  6 ++++--
 fftools/ffmpeg_mux_init.c | 20 +++++++++++++-------
 4 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index f08f5db49e..8ec0f1b3f3 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -583,6 +583,12 @@ typedef struct OutputStream {
     int file_index;          /* file index */
     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 063053623f..2462f53a82 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -354,7 +354,7 @@ int enc_open(OutputStream *ost, AVFrame *frame)
         av_log(ost, 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->st->codecpar, ost->enc_ctx);
+    ret = avcodec_parameters_from_context(ost->par_in, ost->enc_ctx);
     if (ret < 0) {
         av_log(ost, AV_LOG_FATAL,
                "Error initializing the output stream codec context.\n");
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index eb64d8c3ff..40b439eea2 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -556,9 +556,9 @@ static int bsf_init(MuxStream *ms)
     int ret;
 
     if (!ctx)
-        return 0;
+        return avcodec_parameters_copy(ost->st->codecpar, ost->par_in);
 
-    ret = avcodec_parameters_copy(ctx->par_in, ost->st->codecpar);
+    ret = avcodec_parameters_copy(ctx->par_in, ost->par_in);
     if (ret < 0)
         return ret;
 
@@ -768,6 +768,8 @@ static void ost_free(OutputStream **post)
         av_fifo_freep2(&ms->muxing_queue);
     }
 
+    avcodec_parameters_free(&ost->par_in);
+
     av_bsf_free(&ms->bsf_ctx);
 
     av_frame_free(&ost->filtered_frame);
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index b53f5d278d..64a3e1ffe5 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -116,19 +116,19 @@ static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
     if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
         MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
         if (!codec_name) {
-            ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url,
+            ost->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url,
                                                          NULL, ost->type);
-            *enc = avcodec_find_encoder(ost->st->codecpar->codec_id);
+            *enc = avcodec_find_encoder(ost->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->st->codecpar->codec_id));
+                        s->oformat->name, avcodec_get_name(ost->par_in->codec_id));
                 return AVERROR_ENCODER_NOT_FOUND;
             }
         } else if (strcmp(codec_name, "copy")) {
             *enc = find_codec_or_die(ost, codec_name, ost->type, 1);
-            ost->st->codecpar->codec_id = (*enc)->id;
+            ost->par_in->codec_id = (*enc)->id;
         }
     }
 
@@ -844,7 +844,7 @@ static int streamcopy_init(const Muxer *mux, const OptionsContext *o,
     const InputStream   *ist        = ost->ist;
     const InputFile     *ifile      = input_files[ist->file_index];
 
-    AVCodecParameters   *par        = ost->st->codecpar;
+    AVCodecParameters   *par        = ost->par_in;
     uint32_t             codec_tag  = par->codec_tag;
 
     AVCodecContext      *codec_ctx  = NULL;
@@ -995,6 +995,10 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
     ms  = mux_stream_alloc(mux, type);
     ost = &ms->ost;
 
+    ost->par_in = avcodec_parameters_alloc();
+    if (!ost->par_in)
+        report_and_exit(AVERROR(ENOMEM));
+
     ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
     if (!ms->muxing_queue)
         report_and_exit(AVERROR(ENOMEM));
@@ -1003,6 +1007,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
     ost->st         = st;
     ost->ist        = ist;
     ost->kf.ref_pts = AV_NOPTS_VALUE;
+    ost->par_in->codec_type  = type;
     st->codecpar->codec_type = type;
 
     ret = choose_encoder(o, oc, ost, &enc);
@@ -1170,6 +1175,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
             tag = AV_RL32(buf);
         }
         ost->st->codecpar->codec_tag = tag;
+        ost->par_in->codec_tag = tag;
         if (ost->enc_ctx)
             ost->enc_ctx->codec_tag = tag;
     }
@@ -1523,8 +1529,8 @@ static void of_add_attachments(Muxer *mux, const OptionsContext *o)
 
         ost = ost_add(mux, o, AVMEDIA_TYPE_ATTACHMENT, NULL);
         ost->attachment_filename       = o->attachments[i];
-        ost->st->codecpar->extradata      = attachment;
-        ost->st->codecpar->extradata_size = len;
+        ost->par_in->extradata         = attachment;
+        ost->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